The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cdp.c
Go to the documentation of this file.
1 
22 #include <projectcommon.h>
23 #include <stdio.h>
24 #ifdef _MSC_VER
25 #include <winsock2.h>
26 #else
27 #include <netinet/in.h>
28 #endif
29 #include "cdp.h"
30 #include <server_dump.h>
31 
35 static const char* cdptypenames[] =
36 { "*INVALIDCDPTYPE-0*"
37 , "chassis_id"
38 , "mgmt_address"
39 , "port_id"
40 , "capabilities"
41 , "version"
42 , "platform"
43 , "ipprefix"
44 , "hello_protocol"
45 , "vtp_domain"
46 , "native_vlan"
47 , "duplex"
48 , "appliance_id"
49 , "power_consumption"
50 , "*UNKNOWNCDPTYPE-15*"
51 , "*UNKNOWNCDPTYPE-16*"
52 , "*UNKNOWNCDPTYPE-17*"
53 , "*UNKNOWNCDPTYPE-18*"
54 , "*UNKNOWNCDPTYPE-19*"
55 , "*UNKNOWNCDPTYPE-20*"
56 , "*UNKNOWNCDPTYPE-21*"
57 , "*UNKNOWNCDPTYPE-22*"
58 , "*UNKNOWNCDPTYPE-23*"
59 , "*UNKNOWNCDPTYPE-24*"
60 , "*UNKNOWNCDPTYPE-25*"
61 , "*UNKNOWNCDPTYPE-26*"
62 , "*UNKNOWNCDPTYPE-27*"
63 , "*UNKNOWNCDPTYPE-28*"
64 , "*UNKNOWNCDPTYPE-29*"
65 , "*UNKNOWNCDPTYPE-30*"
66 , "*UNKNOWNCDPTYPE-31*"
67 , "*UNKNOWNCDPTYPE-32*"
68 , "*UNKNOWNCDPTYPE-33*"
69 };
70 
71 const char * get_cdp_type_string(unsigned);
72 void dump_cdp_packet(const void*, const void*);
73 
76 const char *
77 get_cdp_type_string(unsigned cdptype)
78 {
79  if (cdptype < DIMOF(cdptypenames)) {
80  return cdptypenames[cdptype];
81  }
82  return "UNKNOWN";
83 }
84 
86 void
87 dump_cdp_packet(const void* vpacket,
88  const void* vpktend)
89 {
90  const guchar* packet = vpacket;
91  const guchar* pktend = vpktend;
92  const guchar* tlv_vp;
93 
94  if (NULL == packet
95  || !is_valid_cdp_packet(packet, pktend)) {
96  fprintf(stderr, "%ld byte packet at address %p is not a valid CDP packet.\n"
97  , (long)(pktend-packet), packet);
98  return;
99  }
100  fprintf(stdout, "{CDP vers: %d, cksum: 0x%04x, ttl: %d}\n"
101  , get_cdp_vers(packet, pktend), get_cdp_cksum(packet, pktend), get_cdp_ttl(packet, pktend));
102 
103  for (tlv_vp = get_cdptlv_first(packet, pktend)
104  ; tlv_vp != NULL
105  ; tlv_vp = get_cdptlv_next(tlv_vp, pktend)) {
106  unsigned ttype = get_cdptlv_type(tlv_vp, pktend);
107  gsize tlen = get_cdptlv_vlen(tlv_vp, pktend);
108  const unsigned char* tbody = get_cdptlv_body(tlv_vp, pktend);
109 
110  fprintf(stdout, "CDP TLV type: %s, length: %"G_GSIZE_FORMAT", values: "
111  , get_cdp_type_string(ttype), tlen);
112  dump_mem(tbody, tbody+tlen);
113  fprintf(stdout, "\n");
114  }
115 }
WINEXPORT const void * get_cdptlv_next(const void *tlv_vp, const void *tlv_vpend)
Locate the next CDP TLV triple (iterator).
Definition: cdp_min.c:254
WINEXPORT guint16 get_cdptlv_type(const void *tlv_vp, const void *pktend)
Return type from the given TLV triplet in a CDP packet.
Definition: cdp_min.c:179
WINEXPORT guint8 get_cdp_ttl(const void *pktptr, const void *pktend)
Return the time to live for this CDP packet.
Definition: cdp_min.c:159
WINEXPORT guint16 get_cdp_cksum(const void *pktptr, const void *pktend)
Return the 16-bit checksum for this CDP packet.
Definition: cdp_min.c:169
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...
Definition: cdp_min.c:200
WINEXPORT gboolean is_valid_cdp_packet(const void *packet, const void *pktend)
Check to see if this is a valid CDP packet.
Definition: cdp_min.c:93
WINEXPORT const void * get_cdptlv_first(const void *pkt, const void *pktend)
Return the first CDP TLV triple in a CDP packet.
Definition: cdp_min.c:223
void dump_cdp_packet(const void *, const void *)
Dump out a CDP packet - not very intelligently.
Definition: cdp.c:87
Project common header file.
#define DIMOF(a)
Definition: lldp_min.c:30
WINEXPORT guint8 get_cdp_vers(const void *pktptr, const void *pktend)
Return the CDP protocol version for this packet.
Definition: cdp_min.c:148
Header file for various basic CDP (Cisco Discovery Protocol definitions and accessor functions...
const char * get_cdp_type_string(unsigned)
Translate a CDP TLV type into a string.
Definition: cdp.c:77
WINEXPORT const void * get_cdptlv_body(const void *tlv_vp, const void *pktend)
Return the body (value) blob of a CDP TLV triplet.
Definition: cdp_min.c:212
WINEXPORT void dump_mem(const void *start, const void *end)
Dump out memory.
Definition: dump.c:60
Functions for server-side dumping of LLDP and CDP packets.