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 }