The Assimilation Project  based on Assimilation version 1.1.6.1462071269
pcap_test.c
Go to the documentation of this file.
1 
25 #include <projectcommon.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <cdp.h>
31 #include <lldp.h>
32 #include <address_family_numbers.h>
33 #include <server_dump.h>
34 #include <pcap_min.h>
35 #include <frameset.h>
36 #include <frame.h>
37 #include <addrframe.h>
38 #include <cstringframe.h>
39 #include <netio.h>
40 #include <intframe.h>
41 #include <frametypes.h>
42 #include <tlvhelper.h>
43 
44 #define DIMOF(a) (sizeof(a)/sizeof(a[0]))
45 
46 FSTATIC void frameset_tests(void);
47 FSTATIC void cast_frameset_tests(void);
48 FSTATIC void address_tests(void);
49 
50 
52 FSTATIC void
54 {
55  Frame* f = frame_new(10, 0);
56  CstringFrame* csf = cstringframe_new(11,0);
57  AddrFrame* af = addrframe_new(12,0);
58  IntFrame* intf = intframe_new(13,sizeof(long));
59  SignFrame* sigf = signframe_glib_new(G_CHECKSUM_SHA256, 0);
60  guint8 address[] = {127, 0, 0, 1};
61  gchar fred[] = "fred";
62  gchar george[] = "king_george_the_third"; // At least 8 characters for stack protector
63 
64  Frame* fcast;
65  AddrFrame* afcast;
66  CstringFrame* cscast;
67  IntFrame* intfcast;
68  FrameSet* fs = frameset_new(42);
69 
70 
71  g_message("Performing c-class cast tests");
72  fcast = CASTTOCLASS(Frame, f);
73  (void)fcast;
74  fcast = CASTTOCLASS(Frame, csf);
75  (void)fcast;
76  fcast = CASTTOCLASS(Frame, af);
77  (void)fcast;
78  fcast = CASTTOCLASS(Frame, intf);
79  (void)fcast;
80  fcast = CASTTOCLASS(Frame, sigf);
81  (void)fcast;
82  cscast = CASTTOCLASS(CstringFrame, csf);
83  (void)cscast;
84  afcast = CASTTOCLASS(AddrFrame, af);
85  (void)afcast;
86  intfcast = CASTTOCLASS(IntFrame, intf);
87  (void)intfcast;
88  sigf = CASTTOCLASS(SignFrame, sigf);
89  (void)sigf;
90 
91 
92  f->setvalue(f, fred, strlen(fred), NULL);
93  csf->baseclass.setvalue(CASTTOCLASS(Frame,csf), george, sizeof(george), NULL);
94  intf->setint(intf, 42);
95  tlv_set_guint16(address, 1, address + DIMOF(address));
96  af->setaddr(af, ADDR_FAMILY_IPV4, address, sizeof(address));
98  UNREF(f);
100  UNREF2(csf);
102  UNREF2(af);
104  UNREF2(intf);
105  frameset_construct_packet(fs, sigf, NULL, NULL);
106  UNREF2(sigf);
108  g_message("finalizing the FrameSet (and presumably frames)");
109  UNREF(fs);
111  g_message("C-class cast tests complete! - please check the output for errors.");
112 }
113 
115 FSTATIC void
117 {
118  const guint8 addr_ipv4_localhost[4] = CONST_IPV4_LOOPBACK;
119  const guint8 addr_ipv4_other[4] = {10, 10, 10, 5};
120  const guint8 addr_ipv6_localhost[16] = CONST_IPV6_LOOPBACK;
121  const guint8 addr_ipv46_localhost[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff,127,0,0,1};
122  const guint8 addr_macaddr48 [6]= {0x00, 0x1b, 0xfc, 0x1b, 0xa8, 0x73};
123  const guint8 addr_macaddr64 [8]= {0x00, 0x1b, 0xfc, 0x1b, 0xa8, 0x73, 0x42, 0x42};
124  const guint8 addr_ipv6_other[16] = {0xfe, 0x80, 0,0,0,0,0,0, 0x2, 0x1b, 0xfc, 0xff, 0xfe, 0x1b, 0xa8, 0x73};
125  AddrFrame* ipv4_localhost = addrframe_ipv4_new(FRAMETYPE_IPADDR, addr_ipv4_localhost);
126  AddrFrame* ipv6_localhost = addrframe_ipv6_new(FRAMETYPE_IPADDR, addr_ipv6_localhost);
127  AddrFrame* ipv46_localhost = addrframe_ipv6_new(FRAMETYPE_IPADDR, addr_ipv46_localhost);
128  AddrFrame* macaddr48 = addrframe_mac48_new(FRAMETYPE_IPADDR, addr_macaddr48);
129  AddrFrame* macaddr64 = addrframe_mac64_new(FRAMETYPE_IPADDR, addr_macaddr64);
130  AddrFrame* ipv6_other = addrframe_ipv6_new(FRAMETYPE_IPADDR, addr_ipv6_other);
131  AddrFrame* ipv4_other = addrframe_ipv4_new(FRAMETYPE_IPADDR, addr_ipv4_other);
132  AddrFrame* bframeipv4_1 = addrframe_new(FRAMETYPE_IPADDR, 0);
133  AddrFrame* bframeipv4_2 = addrframe_new(FRAMETYPE_IPADDR, 0);
134  AddrFrame* bframeipv6_1 = addrframe_new(FRAMETYPE_IPADDR, 0);
135  AddrFrame* bframeipv6_2 = addrframe_new(FRAMETYPE_IPADDR, 0);
136  AddrFrame* bframemac_1 = addrframe_new(FRAMETYPE_MACADDR, 0);
137  AddrFrame* bframemac_2 = addrframe_new(FRAMETYPE_MACADDR, 0);
138  AddrFrame* bframemac_3 = addrframe_new(FRAMETYPE_MACADDR, 0);
139  AddrFrame* gframes[] = {ipv4_localhost, ipv6_localhost, ipv46_localhost, macaddr48, macaddr64, ipv6_other, ipv4_other};
140  AddrFrame* bframes[] = {bframeipv4_1, bframeipv4_2, bframeipv6_1, bframeipv6_2, bframemac_1, bframemac_2, bframemac_3};
141  AddrFrame* af;
142  SignFrame* gsigf = signframe_glib_new(G_CHECKSUM_SHA256, 0);
143  unsigned j;
144  FrameSet* gfs = frameset_new(42);
145 
146 
147  g_message("Starting Known Good AddressFrame tests.");
148  for (j=0; j < DIMOF(gframes); ++j) {
149  Frame * faf;
150  af = gframes[j];
151  faf = CASTTOCLASS(Frame, af);
152  if (!af->baseclass.isvalid(faf, NULL, NULL)) {
153  g_critical("OOPS Good AddressFrame %d is NOT valid!", j);
154  }
155  frameset_append_frame(gfs, faf);
156  UNREF2(af);
157  }
158  frameset_construct_packet(gfs, gsigf, NULL, NULL);
159  UNREF2(gsigf);
160  UNREF(gfs);
161 
162 
163  bframeipv4_1->setaddr(bframeipv4_1, ADDR_FAMILY_IPV4, addr_ipv46_localhost, 3);
164  bframeipv4_1->setaddr(bframeipv4_2, ADDR_FAMILY_IPV4, addr_ipv46_localhost, 5);
165  bframeipv6_1->setaddr(bframeipv6_1, ADDR_FAMILY_IPV6, addr_ipv46_localhost, 15);
166  bframeipv6_2->setaddr(bframeipv6_2, ADDR_FAMILY_IPV6, addr_ipv46_localhost, 17);
167  bframemac_1->setaddr(bframemac_1, ADDR_FAMILY_802, addr_ipv46_localhost, 5);
168  bframemac_2->setaddr(bframemac_2, ADDR_FAMILY_802, addr_ipv46_localhost, 7);
169  bframemac_3->setaddr(bframemac_3, ADDR_FAMILY_802, addr_ipv46_localhost, 9);
170 
171  g_message("Starting Known Bad AddressFrame tests.");
172  for (j=0; j < DIMOF(bframes); ++j) {
173  af = bframes[j];
174  if (af->baseclass.isvalid(CASTTOCLASS(Frame, af), NULL, NULL)) {
175  g_critical("Bad AddressFrame %d SHOULD NOT BE valid!", j);
176  }
177  UNREF2(af);
178  bframes[j] = NULL;
179  }
180 
182  g_message("End of AddressFrame tests.");
183 }
184 
185 
186 
187 
188 #define PCAP "../pcap/"
189 
191 int
192 main(int argc, char **argv)
193 {
194  char errbuf[PCAP_ERRBUF_SIZE];
195  struct pcap_pkthdr hdr;
196  const guchar* packet;
197  unsigned j;
198  const char * lldpfilenames [] = {PCAP "lldp.detailed.pcap", PCAP "procurve.lldp.pcap", PCAP "lldpmed_civicloc.pcap"};
199  const char * cdpfilenames [] = {PCAP "cdp.pcap", PCAP "n0.eth2.cdp.pcap"};
200 
201  (void)argc; (void)argv;
202  // make CRITICAL messages terminate the program too...
203  g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL);
205  address_tests();
206 
207  // Parse some existing CDP files
208  for (j=0; j < DIMOF(cdpfilenames); ++j) {
209  pcap_t* handle;
210  const char * filename = cdpfilenames[j];
211  int count = 0;
212  if (NULL == (handle = pcap_open_offline(filename, errbuf))) {
213  g_error("open_offline failed.../: %s", errbuf);
214  exit(1);
215  }
216  while (NULL != (packet = pcap_next(handle, &hdr))) {
217  const guchar * pend = packet + hdr.caplen;
218  ++count;
219  g_message("Found a %d/%d byte CDP packet!", hdr.caplen, hdr.len);
220  if (is_valid_cdp_packet(packet, pend)) {
221  dump_cdp_packet(packet, pend);
222  }else{
223  g_warning("ERROR: %d byte CDP packet %d in [%s] is NOT valid!\n",
224  (int)(pend-packet), count, filename);
225  }
226  dump_mem(packet, packet+hdr.caplen);
227  g_message("%s", "");
228  }
229  pcap_close(handle);
230  }
231 
232  // Parse some existing LLDP files
233  for (j=0; j < DIMOF(lldpfilenames); ++j) {
234  pcap_t* handle;
235  const char * filename = lldpfilenames[j];
236  if (NULL == (handle = pcap_open_offline(filename, errbuf))) {
237  g_error("open_offline failed.../: %s\n", errbuf);
238  }
239  while (NULL != (packet = pcap_next(handle, &hdr))) {
240  const guchar * pend = packet + hdr.caplen;
241  g_message("Found a %d/%d byte LLDP packet!\n", hdr.caplen, hdr.len);
242  dump_lldp_packet(packet, pend);
243  dump_mem(packet, packet+hdr.caplen);
244  g_message("%s", "");
245  }
246  pcap_close(handle);
247  }
248  return(0);
249 }
int main(int argc, char **argv)
Main program for performing tests that don't need a network.
Definition: pcap_test.c:192
IETF/IANA Address family assignments.
WINEXPORT AddrFrame * addrframe_new(guint16 frame_type, gsize framesize)
Construct a new AddrFrame class - allowing for "derived" frame types...
Definition: addrframe.c:201
This is an IntFrame class TLV (type, length, value) frame - representing an integer of some specified...
Definition: intframe.h:39
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:94
WINEXPORT AddrFrame * addrframe_ipv6_new(guint16 frame_type, gconstpointer addr)
Construct and initialize an IPv6 AddrFrame class.
Definition: addrframe.c:241
WINEXPORT SignFrame * signframe_glib_new(GChecksumType sigtype, gsize framesize)
Construct a new SignFrame - allowing for "derived" frame types...
Definition: signframe.c:334
Implements minimal client-oriented Frame and Frameset capabilities.
#define FRAMETYPE_IPADDR
FRAMETYPE_IPADDR Frame (frametype 10) Frame subclass - AddrFrame class.
Definition: frametypes.h:206
#define DIMOF(a)
Definition: pcap_test.c:44
This is the base Frame class object (in-memory TLV (type, length, value)) for every general component...
Definition: frame.h:43
IntFrame * intframe_new(guint16 frametype, int intbytes)
Construct new IntFrame object.
Definition: intframe.c:179
Describes interfaces to C-String Frame (Cstringframe) C-Class It holds conventional zero-terminated b...
WINEXPORT void dump_cdp_packet(const void *packet, const void *packend)
Dump out a CDP packet - not very intelligently.
Definition: cdp.c:87
FrameSet * frameset_new(guint16 frameset_type)
Construct a new frameset of the given type.
Definition: frameset.c:110
The SignFrame class object - implements digital signatures for FrameSets.
Definition: signframe.h:47
WINEXPORT void dump_lldp_packet(const void *packet, const void *packend)
Dump an LLDP packet to stdout.
Definition: lldp.c:120
Simple libpcap interface definitions.
WINEXPORT AddrFrame * addrframe_mac48_new(guint16 frame_type, gconstpointer addr)
Construct and initialize a 48-bit MAC address AddrFrame class.
Definition: addrframe.c:253
#define FSTATIC
Definition: projectcommon.h:31
void(* setvalue)(Frame *self, gpointer value, guint32 length, GDestroyNotify valfinal)
member function for setting value
Definition: frame.h:52
Defines an abstract Network I/O class.
void tlv_set_guint16(void *vitem, guint16 item, const void *bufend)
Set an unsigned 16 bit integer to the given location with error checking and without caring about byt...
Definition: tlvhelper.c:67
Frame baseclass
Definition: addrframe.h:39
Implements the IntFrame class - integers in a frame.
void frameset_append_frame(FrameSet *fs, Frame *f)
Append frame to the front of the end of the frame list.
Definition: frameset.c:143
void(* setint)(IntFrame *self, guint64 value)
set the integer to the given value
Definition: intframe.h:43
FSTATIC void cast_frameset_tests(void)
Basic tests of our Class system, and for good measure testing of some Frame and FrameSet objects...
Definition: pcap_test.c:53
FSTATIC void address_tests(void)
Basic tests of a few different kinds of AddrFrame class objects.
Definition: pcap_test.c:116
FSTATIC void frameset_tests(void)
Frame * frame_new(guint16 frame_type, gsize framesize)
Construct a new frame - allowing for "derived" frame types...
Definition: frame.c:125
#define FRAMETYPE_MACADDR
FRAMETYPE_MACADDR Frame (frametype 11) Frame subclass - AddrFrame class.
Definition: frametypes.h:219
#define CONST_IPV6_LOOPBACK
Definition: netaddr.h:77
Implements basic Frame class.
#define ADDR_FAMILY_IPV6
IPv6.
This is our CstringFrame class object - used for holding C-style NULL terminated strings.
Definition: cstringframe.h:36
Provides basic Link Layer Discovery Protocol (LLDP) definitions and accessor functions for LLPD packe...
Project common header file.
Header file defining the data layouts for our Frames.
TLV helper interfaces definitions.
#define PCAP
Definition: pcap_test.c:188
gboolean(* isvalid)(const Frame *self, gconstpointer tlvptr, gconstpointer pktend)
TRUE if TLV data looks valid...
Definition: frame.h:50
Header file for various basic CDP (Cisco Discovery Protocol definitions and accessor functions...
WINEXPORT AddrFrame * addrframe_mac64_new(guint16 frame_type, gconstpointer addr)
Construct and initialize a 64-bit MAC address AddrFrame class.
Definition: addrframe.c:264
Describes interfaces to Address Frame (AddrFrame) C-Class.
WINEXPORT AddrFrame * addrframe_ipv4_new(guint16 frame_type, gconstpointer addr)
Construct and initialize an IPv4 AddrFrame class.
Definition: addrframe.c:229
void frameset_construct_packet(FrameSet *fs, SignFrame *sigframe, CryptFrame *cryptframe, CompressFrame *compressframe)
Construct packet to go correspond to this frameset.
Definition: frameset.c:159
FrameSet class - used for collecting Frames when not on the wire, and for marshalling/demarshalling t...
Definition: frameset.h:46
void(* setaddr)(AddrFrame *f, guint16 addrtype, gconstpointer addr, gsize addrlen)
Definition: addrframe.h:42
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.
#define ADDR_FAMILY_802
Level 2 physical (MAC) addresses.
void proj_class_dump_live_objects(void)
Dump all live C class objects (address and Class)
Definition: proj_classes.c:356
This is our AddrFrame class object - used for holding NetAddr class network addresses.
Definition: addrframe.h:38
#define ADDR_FAMILY_IPV4
IPv4.
#define UNREF2(obj)
Definition: assimobj.h:36
#define CASTTOCLASS(Cclass, obj)
Safely cast 'obj' to C-class 'class' - verifying that it was registerd as being of type class ...
Definition: proj_classes.h:66
#define UNREF(obj)
Definition: assimobj.h:35
WINEXPORT CstringFrame * cstringframe_new(guint16 frame_type, gsize framesize)
Construct a new CstringFrame - allowing for "derived" frame types...
Definition: cstringframe.c:101
#define CONST_IPV4_LOOPBACK
Definition: netaddr.h:78