The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cdp_min.c File Reference

Implements minimal client-oriented CDP capabilities. More...

#include <stdio.h>
#include <netinet/in.h>
#include <cdp.h>
#include <tlvhelper.h>
Include dependency graph for cdp_min.c:

Go to the source code of this file.

Macros

#define CDP_OVERHEAD   22
 Number of bytes before the CDP packet itself starts. More...
 
#define CDP_VERSSZ   1
 Size of the CDP version object in a CDP packet. More...
 
#define CDP_TTLSZ   1
 Size of the Time to Live (TTL) object in a CDP packet. More...
 
#define CDP_CKSUMSZ   2
 Size of the checksum object in a CDP packet. More...
 
#define CDPINITHDRSZ   (CDP_OVERHEAD+CDP_VERSSZ+CDP_TTLSZ+CDP_CKSUMSZ) /* 26 */
 Start of the TLV (type, length, value) portion of a CDP packet. More...
 
#define CDPTLV_TYPESZ   2
 Size of the type element in the CDP TLV triple. More...
 
#define CDPTLV_LENSZ   2
 Size of the length element in a CDP TLV. More...
 
#define CDPTLV_TYPELENSZ   (CDPTLV_TYPESZ+CDPTLV_LENSZ) /* 4 */
 Overhead - offset to the beginning of the CDP TLV Value. More...
 

Functions

WINEXPORT gboolean is_valid_cdp_packet (const void *packet, const void *pktend)
 Check to see if this is a valid CDP packet. More...
 
WINEXPORT guint8 get_cdp_vers (const void *pktptr, const void *pktend)
 Return the CDP protocol version for this packet. More...
 
WINEXPORT guint8 get_cdp_ttl (const void *pktptr, const void *pktend)
 Return the time to live for this CDP packet. More...
 
WINEXPORT guint16 get_cdp_cksum (const void *pktptr, const void *pktend)
 Return the 16-bit checksum for this CDP packet. More...
 
WINEXPORT guint16 get_cdptlv_type (const void *tlv_vp, const void *pktend)
 Return type from the given TLV triplet in a CDP packet. More...
 
WINEXPORT gsize get_cdptlv_len (const void *tlv_vp, const void *pktend)
 Return size of the given TLV triplet. More...
 
WINEXPORT gsize get_cdptlv_vlen (const void *tlv_vp, const void *pktend)
 Return length of the value blob int a given TLV triplet in a CDP packet - value size only. More...
 
WINEXPORT const void * get_cdptlv_body (const void *tlv_vp, const void *pktend)
 Return the body (value) blob of a CDP TLV triplet. More...
 
WINEXPORT const void * get_cdptlv_first (const void *pkt, const void *pktend)
 Return the first CDP TLV triple in a CDP packet. More...
 
WINEXPORT const void * get_cdptlv_next (const void *tlv_vp, const void *tlv_vpend)
 Locate the next CDP TLV triple (iterator). More...
 
WINEXPORT const void * get_cdp_chassis_id (gconstpointer packet, gssize *idlength, gconstpointer pktend)
 Get the chassis ID associated with this CDP packet. More...
 
WINEXPORT const void * get_cdp_port_id (gconstpointer packet, gssize *idlength, gconstpointer pktend)
 get the port ID associated with this CDP packet More...
 

Detailed Description

Implements minimal client-oriented CDP capabilities.

This file contains the minimal CDP capability for a client - enough for it to be able to understand and validate a CDP packet's structure, return any particular TLV and specifically to be able to locate the chassis id and port id (which the client needs).

This file is part of the Assimilation Project.

Author
Copyright © 2011, 2012 - Alan Robertson alanr.nosp@m.@uni.nosp@m.x.sh
The Assimilation software is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The Assimilation software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with the Assimilation Project software. If not, see http://www.gnu.org/licenses/.

Definition in file cdp_min.c.

Function Documentation

WINEXPORT const void* get_cdp_chassis_id ( gconstpointer  packet,
gssize *  idlength,
gconstpointer  pktend 
)

Get the chassis ID associated with this CDP packet.

Returns
pointer to chassis ID memory area and also length of the chassis ID
Parameters
[in]packetPointer to a the start of a CDP packet
[out]idlengthlength of chassis id
[in]pktendPointer of first byte past end of CDP packet

Definition at line 276 of file cdp_min.c.

References CDP_TLV_DEVID, get_cdptlv_body(), get_cdptlv_first(), get_cdptlv_next(), get_cdptlv_type(), and get_cdptlv_vlen().

Here is the call graph for this function:

WINEXPORT guint16 get_cdp_cksum ( const void *  pktptr,
const void *  pktend 
)

Return the 16-bit checksum for this CDP packet.

The checksum is not part of the TLVs, so the input pointer is to the CDP packet not the first TLV in the packet.

Parameters
[in]pktptrPointer to beginning of CDP packet
[in]pktendPointer of first byte past end of CDP packet

Definition at line 169 of file cdp_min.c.

References CDP_OVERHEAD, and tlv_get_guint16().

Referenced by dump_cdp_packet().

Here is the call graph for this function:

Here is the caller graph for this function:

WINEXPORT const void* get_cdp_port_id ( gconstpointer  packet,
gssize *  idlength,
gconstpointer  pktend 
)

get the port ID associated with this CDP packet

Returns
pointer to port ID memory area and also length of the port ID
Parameters
[in]packetPointer to a the start of a CDP packet
[out]idlengthlength of chassis id
[in]pktendPointer of first byte past end of CDP packet

Definition at line 297 of file cdp_min.c.

References CDP_TLV_PORTID, get_cdptlv_body(), get_cdptlv_first(), get_cdptlv_next(), get_cdptlv_type(), and get_cdptlv_vlen().

Here is the call graph for this function:

WINEXPORT guint8 get_cdp_ttl ( const void *  pktptr,
const void *  pktend 
)

Return the time to live for this CDP packet.

The TTL is not part of the TLVs in a CDP packet.

Returns
time to live (TTL) in seconds
Parameters
[in]pktptrPointer to beginning of the CDP packet
[in]pktendPointer of first byte past end of CDP packet

Definition at line 159 of file cdp_min.c.

References CDP_OVERHEAD, and tlv_get_guint8().

Referenced by dump_cdp_packet(), get_cdptlv_first(), and is_valid_cdp_packet().

Here is the call graph for this function:

Here is the caller graph for this function:

WINEXPORT guint8 get_cdp_vers ( const void *  pktptr,
const void *  pktend 
)

Return the CDP protocol version for this packet.

This is normally 2. The CDP protocol version is not part of the TLVs in a CDP packet.

Parameters
[in]pktptrPointer to beginning of the CDP packet
[in]pktendPointer of first byte past end of CDP packet

Definition at line 148 of file cdp_min.c.

References CDP_OVERHEAD, and tlv_get_guint8().

Referenced by dump_cdp_packet(), get_cdptlv_first(), and is_valid_cdp_packet().

Here is the call graph for this function:

Here is the caller graph for this function:

WINEXPORT const void* get_cdptlv_body ( const void *  tlv_vp,
const void *  pktend 
)

Return the body (value) blob of a CDP TLV triplet.

Returns
pointer to the value blob of a CDP TLV triplet. Length of this blob is given by get_cdptlv_vlen().
See Also
get_cdptlv_vlen
Parameters
[in]tlv_vpShould be the a CDP TLV object from get_cdbtlv_first() or get_cdp_tlv_next(), etc.
[in]pktendPointer of first byte past end of CDP packet

Definition at line 212 of file cdp_min.c.

References CDPTLV_TYPELENSZ.

Referenced by cma.AssimCclasses.pySwitchDiscovery::decode_discovery(), dump_cdp_packet(), get_cdp_chassis_id(), and get_cdp_port_id().

Here is the caller graph for this function:

WINEXPORT const void* get_cdptlv_first ( const void *  pkt,
const void *  pktend 
)

Return the first CDP TLV triple in a CDP packet.

Returns
pointer to the first CDP TLV triple in the packet - or NULL if none. Note that this will never return a pointer to a TLV which extends past tlv_vpend.
Parameters
[in]pktPointer to start of CDP packet
[in]pktendFirst byte after the last CDP packet byte

Definition at line 223 of file cdp_min.c.

References CDPINITHDRSZ, CDPTLV_TYPELENSZ, get_cdp_ttl(), get_cdp_vers(), and get_cdptlv_len().

Referenced by cma.AssimCclasses.pySwitchDiscovery::decode_discovery(), dump_cdp_packet(), get_cdp_chassis_id(), get_cdp_port_id(), and is_valid_cdp_packet().

Here is the call graph for this function:

Here is the caller graph for this function:

WINEXPORT gsize get_cdptlv_len ( const void *  tlv_vp,
const void *  pktend 
)

Return size of the given TLV triplet.

Returns
size of the entire TLV triplet - including size of T and L as well as V.
Parameters
[in]tlv_vpShould be the a CDP TLV object from get_cdbtlv_first or get_cdp_tlv_next, etc.
[in]pktendPointer of first byte past end of CDP packet

Definition at line 189 of file cdp_min.c.

References tlv_get_guint16().

Referenced by cma.AssimCclasses.pySwitchDiscovery::decode_discovery(), get_cdptlv_first(), get_cdptlv_next(), get_cdptlv_vlen(), and is_valid_cdp_packet().

Here is the call graph for this function:

Here is the caller graph for this function:

WINEXPORT const void* get_cdptlv_next ( const void *  tlv_vp,
const void *  tlv_vpend 
)

Locate the next CDP TLV triple (iterator).

Returns
pointer to the next CDP TLV triple in the sequence - or NULL if none. Note that this will never return a pointer to a TLV which extends past tlv_vpend.
Parameters
[in]tlv_vpPointer to first byte of current TLV triple
[in]tlv_vpendFirst byte after the last CDP packet byte

Definition at line 254 of file cdp_min.c.

References CDPTLV_TYPELENSZ, and get_cdptlv_len().

Referenced by cma.AssimCclasses.pySwitchDiscovery::decode_discovery(), dump_cdp_packet(), get_cdp_chassis_id(), and get_cdp_port_id().

Here is the call graph for this function:

Here is the caller graph for this function:

WINEXPORT guint16 get_cdptlv_type ( const void *  tlv_vp,
const void *  pktend 
)

Return type from the given TLV triplet in a CDP packet.

Returns
type value from CDP TLV triple
Parameters
[in]tlv_vpShould be the a CDP TLV object from get_cdbtlv_first or get_cdp_tlv_next, etc.
[in]pktendPointer of first byte past end of CDP packet

Definition at line 179 of file cdp_min.c.

References tlv_get_guint16().

Referenced by cma.AssimCclasses.pySwitchDiscovery::decode_discovery(), dump_cdp_packet(), get_cdp_chassis_id(), get_cdp_port_id(), and is_valid_cdp_packet().

Here is the call graph for this function:

Here is the caller graph for this function:

WINEXPORT gsize get_cdptlv_vlen ( const void *  tlv_vp,
const void *  pktend 
)

Return length of the value blob int a given TLV triplet in a CDP packet - value size only.

Returns
number of bytes in the Value portion of the CDP TLV pointed to in the input
See Also
get_cdptlv_body
Parameters
[in]tlv_vpShould be the a CDP TLV object from get_cdbtlv_first(), or get_cdp_tlv_next(), etc.
[in]pktendPointer of first byte past end of CDP packet

Definition at line 200 of file cdp_min.c.

References CDPTLV_TYPELENSZ, and get_cdptlv_len().

Referenced by dump_cdp_packet(), get_cdp_chassis_id(), and get_cdp_port_id().

Here is the call graph for this function:

Here is the caller graph for this function:

WINEXPORT gboolean is_valid_cdp_packet ( const void *  packet,
const void *  pktend 
)

Check to see if this is a valid CDP packet.

We validate that it is completely well-formed, and not dangerous to process to the best of our knowledge - watching especially for buffer-overrun type issues in the structure of the packet.

Returns
TRUE if this is a valid CDP packet, FALSE otherwise.
Todo:
validate dest MAC address (01:00:0c:cc:cc:cc) before looking at anything else...
Parameters
[in]packetStart of CDP packet
[in]pktendFirst byte after the last CDP packet byte

Definition at line 93 of file cdp_min.c.

References CDPTLV_TYPELENSZ, DIMOF, get_cdp_ttl(), get_cdp_vers(), get_cdptlv_first(), get_cdptlv_len(), and get_cdptlv_type().

Referenced by cma.AssimCclasses.pySwitchDiscovery::decode_discovery(), dump_cdp_packet(), and main().

Here is the call graph for this function:

Here is the caller graph for this function: