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 }
int main(int argc, char **argv)
Main program for performing tests that don't need a network.
Definition: pcap_test.c:195
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:93
WINEXPORT AddrFrame * addrframe_ipv6_new(guint16 frame_type, gconstpointer addr)
Construct and initialize an IPv6 AddrFrame class.
Definition: addrframe.c:241
Implements minimal client-oriented Frame and Frameset capabilities.
#define FRAMETYPE_IPADDR
FRAMETYPE_IPADDR Frame (frametype 10) Frame subclass - AddrFrame class.
Definition: frametypes.h:200
#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:181
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:40
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
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:68
Frame baseclass
Definition: addrframe.h:39
Implements the IntFrame class - integers in a frame.
void(* setaddr)(AddrFrame *f, guint16 addrtype, gconstpointer addr, gsize addrlen)
Definition: addrframe.h:42
void frameset_append_frame(FrameSet *fs, Frame *f)
Append frame to the front of the end of the frame list.
Definition: frameset.c:143
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:119
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:124
#define FRAMETYPE_MACADDR
FRAMETYPE_MACADDR Frame (frametype 11) Frame subclass - AddrFrame class.
Definition: frametypes.h:213
#define CONST_IPV6_LOOPBACK
Definition: netaddr.h:76
gboolean(* isvalid)(const Frame *self, gconstpointer tlvptr, gconstpointer pktend)
TRUE if TLV data looks valid...
Definition: frame.h:50
void frameset_construct_packet(FrameSet *fs, SignFrame *sigframe, Frame *cryptframe, CompressFrame *compressframe)
Construct packet to go correspond to this frameset.
Definition: frameset.c:159
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:191
SignFrame * signframe_new(GChecksumType sigtype, gsize framesize)
Construct a new SignFrame - allowing for "derived" frame types...
Definition: signframe.c:208
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(* setint)(IntFrame *self, guint64 value)
set the integer to the given value
Definition: intframe.h:43
FrameSet class - used for collecting Frames when not on the wire, and for marshalling/demarshalling t...
Definition: frameset.h:45
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
void(* setvalue)(Frame *self, gpointer value, guint32 length, GDestroyNotify valfinal)
member function for setting value
Definition: frame.h:52
#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:77