The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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_new(G_CHECKSUM_SHA256, 0);
60  guint8 address[] = {127, 0, 0, 1};
61  gchar fred[] = "fred";
62  gchar george[] = "george";
63  char stackprotectionstuff[8];
64 
65  Frame* fcast;
66  AddrFrame* afcast;
67  CstringFrame* cscast;
68  IntFrame* intfcast;
69  FrameSet* fs = frameset_new(42);
70 
71  stackprotectionstuff[0] = '\0'; // Make it think we need stackprotection stuff...
72  (void)stackprotectionstuff;
73 
74  g_message("Performing c-class cast tests");
75  fcast = CASTTOCLASS(Frame, f);
76  (void)fcast;
77  fcast = CASTTOCLASS(Frame, csf);
78  (void)fcast;
79  fcast = CASTTOCLASS(Frame, af);
80  (void)fcast;
81  fcast = CASTTOCLASS(Frame, intf);
82  (void)fcast;
83  fcast = CASTTOCLASS(Frame, sigf);
84  (void)fcast;
85  cscast = CASTTOCLASS(CstringFrame, csf);
86  (void)cscast;
87  afcast = CASTTOCLASS(AddrFrame, af);
88  (void)afcast;
89  intfcast = CASTTOCLASS(IntFrame, intf);
90  (void)intfcast;
91  sigf = CASTTOCLASS(SignFrame, sigf);
92  (void)sigf;
93 
94 
95  f->setvalue(f, fred, strlen(fred), NULL);
96  csf->baseclass.setvalue(CASTTOCLASS(Frame,csf), george, sizeof(george), NULL);
97  intf->setint(intf, 42);
98  tlv_set_guint16(address, 1, address + DIMOF(address));
99  af->setaddr(af, ADDR_FAMILY_IPV4, address, sizeof(address));
101  UNREF(f);
103  UNREF2(csf);
105  UNREF2(af);
107  UNREF2(intf);
108  frameset_construct_packet(fs, sigf, NULL, NULL);
109  UNREF2(sigf);
111  g_message("finalizing the FrameSet (and presumably frames)");
112  UNREF(fs);
114  g_message("C-class cast tests complete! - please check the output for errors.");
115 }
116 
118 FSTATIC void
120 {
121  const guint8 addr_ipv4_localhost[4] = CONST_IPV4_LOOPBACK;
122  const guint8 addr_ipv4_other[4] = {10, 10, 10, 5};
123  const guint8 addr_ipv6_localhost[16] = CONST_IPV6_LOOPBACK;
124  const guint8 addr_ipv46_localhost[16] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff,127,0,0,1};
125  const guint8 addr_macaddr48 [6]= {0x00, 0x1b, 0xfc, 0x1b, 0xa8, 0x73};
126  const guint8 addr_macaddr64 [8]= {0x00, 0x1b, 0xfc, 0x1b, 0xa8, 0x73, 0x42, 0x42};
127  const guint8 addr_ipv6_other[16] = {0xfe, 0x80, 0,0,0,0,0,0, 0x2, 0x1b, 0xfc, 0xff, 0xfe, 0x1b, 0xa8, 0x73};
128  AddrFrame* ipv4_localhost = addrframe_ipv4_new(FRAMETYPE_IPADDR, addr_ipv4_localhost);
129  AddrFrame* ipv6_localhost = addrframe_ipv6_new(FRAMETYPE_IPADDR, addr_ipv6_localhost);
130  AddrFrame* ipv46_localhost = addrframe_ipv6_new(FRAMETYPE_IPADDR, addr_ipv46_localhost);
131  AddrFrame* macaddr48 = addrframe_mac48_new(FRAMETYPE_IPADDR, addr_macaddr48);
132  AddrFrame* macaddr64 = addrframe_mac64_new(FRAMETYPE_IPADDR, addr_macaddr64);
133  AddrFrame* ipv6_other = addrframe_ipv6_new(FRAMETYPE_IPADDR, addr_ipv6_other);
134  AddrFrame* ipv4_other = addrframe_ipv4_new(FRAMETYPE_IPADDR, addr_ipv4_other);
135  AddrFrame* bframeipv4_1 = addrframe_new(FRAMETYPE_IPADDR, 0);
136  AddrFrame* bframeipv4_2 = addrframe_new(FRAMETYPE_IPADDR, 0);
137  AddrFrame* bframeipv6_1 = addrframe_new(FRAMETYPE_IPADDR, 0);
138  AddrFrame* bframeipv6_2 = addrframe_new(FRAMETYPE_IPADDR, 0);
139  AddrFrame* bframemac_1 = addrframe_new(FRAMETYPE_MACADDR, 0);
140  AddrFrame* bframemac_2 = addrframe_new(FRAMETYPE_MACADDR, 0);
141  AddrFrame* bframemac_3 = addrframe_new(FRAMETYPE_MACADDR, 0);
142  AddrFrame* gframes[] = {ipv4_localhost, ipv6_localhost, ipv46_localhost, macaddr48, macaddr64, ipv6_other, ipv4_other};
143  AddrFrame* bframes[] = {bframeipv4_1, bframeipv4_2, bframeipv6_1, bframeipv6_2, bframemac_1, bframemac_2, bframemac_3};
144  AddrFrame* af;
145  SignFrame* gsigf = signframe_new(G_CHECKSUM_SHA256, 0);
146  unsigned j;
147  FrameSet* gfs = frameset_new(42);
148 
149 
150  g_message("Starting Known Good AddressFrame tests.");
151  for (j=0; j < DIMOF(gframes); ++j) {
152  Frame * faf;
153  af = gframes[j];
154  faf = CASTTOCLASS(Frame, af);
155  if (!af->baseclass.isvalid(faf, NULL, NULL)) {
156  g_critical("OOPS Good AddressFrame %d is NOT valid!", j);
157  }
158  frameset_append_frame(gfs, faf);
159  UNREF2(af);
160  }
161  frameset_construct_packet(gfs, gsigf, NULL, NULL);
162  UNREF2(gsigf);
163  UNREF(gfs);
164 
165 
166  bframeipv4_1->setaddr(bframeipv4_1, ADDR_FAMILY_IPV4, addr_ipv46_localhost, 3);
167  bframeipv4_1->setaddr(bframeipv4_2, ADDR_FAMILY_IPV4, addr_ipv46_localhost, 5);
168  bframeipv6_1->setaddr(bframeipv6_1, ADDR_FAMILY_IPV6, addr_ipv46_localhost, 15);
169  bframeipv6_2->setaddr(bframeipv6_2, ADDR_FAMILY_IPV6, addr_ipv46_localhost, 17);
170  bframemac_1->setaddr(bframemac_1, ADDR_FAMILY_802, addr_ipv46_localhost, 5);
171  bframemac_2->setaddr(bframemac_2, ADDR_FAMILY_802, addr_ipv46_localhost, 7);
172  bframemac_3->setaddr(bframemac_3, ADDR_FAMILY_802, addr_ipv46_localhost, 9);
173 
174  g_message("Starting Known Bad AddressFrame tests.");
175  for (j=0; j < DIMOF(bframes); ++j) {
176  af = bframes[j];
177  if (af->baseclass.isvalid(CASTTOCLASS(Frame, af), NULL, NULL)) {
178  g_critical("Bad AddressFrame %d SHOULD NOT BE valid!", j);
179  }
180  UNREF2(af);
181  bframes[j] = NULL;
182  }
183 
185  g_message("End of AddressFrame tests.");
186 }
187 
188 
189 
190 
191 #define PCAP "../pcap/"
192 
194 int
195 main(int argc, char **argv)
196 {
197  char errbuf[PCAP_ERRBUF_SIZE];
198  struct pcap_pkthdr hdr;
199  const guchar* packet;
200  unsigned j;
201  const char * lldpfilenames [] = {PCAP "lldp.detailed.pcap", PCAP "procurve.lldp.pcap", PCAP "lldpmed_civicloc.pcap"};
202  const char * cdpfilenames [] = {PCAP "cdp.pcap", PCAP "n0.eth2.cdp.pcap"};
203 
204  (void)argc; (void)argv;
205  // make CRITICAL messages terminate the program too...
206  g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL);
208  address_tests();
209 
210  // Parse some existing CDP files
211  for (j=0; j < DIMOF(cdpfilenames); ++j) {
212  pcap_t* handle;
213  const char * filename = cdpfilenames[j];
214  int count = 0;
215  if (NULL == (handle = pcap_open_offline(filename, errbuf))) {
216  g_error("open_offline failed.../: %s", errbuf);
217  exit(1);
218  }
219  while (NULL != (packet = pcap_next(handle, &hdr))) {
220  const guchar * pend = packet + hdr.caplen;
221  ++count;
222  g_message("Found a %d/%d byte CDP packet!", hdr.caplen, hdr.len);
223  if (is_valid_cdp_packet(packet, pend)) {
224  dump_cdp_packet(packet, pend);
225  }else{
226  g_warning("ERROR: %d byte CDP packet %d in [%s] is NOT valid!\n",
227  (int)(pend-packet), count, filename);
228  }
229  dump_mem(packet, packet+hdr.caplen);
230  g_message("%s", "");
231  }
232  pcap_close(handle);
233  }
234 
235  // Parse some existing LLDP files
236  for (j=0; j < DIMOF(lldpfilenames); ++j) {
237  pcap_t* handle;
238  const char * filename = lldpfilenames[j];
239  if (NULL == (handle = pcap_open_offline(filename, errbuf))) {
240  g_error("open_offline failed.../: %s\n", errbuf);
241  }
242  while (NULL != (packet = pcap_next(handle, &hdr))) {
243  const guchar * pend = packet + hdr.caplen;
244  g_message("Found a %d/%d byte LLDP packet!\n", hdr.caplen, hdr.len);
245  dump_lldp_packet(packet, pend);
246  dump_mem(packet, packet+hdr.caplen);
247  g_message("%s", "");
248  }
249  pcap_close(handle);
250  }
251  return(0);
252 }