static void test_load(const char *file, const unsigned int *len, const size_t count) { struct peak_load *trace = peak_load_init(file); struct ether_header *eth; size_t i; assert(trace); eth = (struct ether_header *)trace->buf; for (i = 0; i < count; ++i) { assert(peak_load_next(trace) == len[i]); assert(be16dec(ð->ether_type) == ETHERTYPE_IP); } assert(!peak_load_next(trace)); assert(!peak_load_next(trace)); peak_load_exit(trace); }
/* this test works for both TCP and UDP over IPv4 */ static void test_transport_ip4(const char* file) { struct peak_load *trace = peak_load_init(file); struct peak_packet packet_info; unsigned int packet_len = 0; assert(trace); packet_len = peak_load_packet(trace); assert(!peak_packet_parse(&packet_info, trace->buf, packet_len, LINKTYPE_ETHERNET)); /* layer 1 */ assert(packet_info.link_type == LINKTYPE_ETHERNET); /* layer 2 */ assert(packet_info.mac_len == packet_len); assert(packet_info.mac_type == ETHERTYPE_IP); packet_len -= sizeof(struct ether_header); /* layer 3 */ assert(!netcmp(&packet_info.net_saddr, &net_saddr4)); assert(!netcmp(&packet_info.net_daddr, &net_daddr4)); assert(packet_info.net_family == AF_INET); assert(packet_info.net_len == packet_len); packet_len -= packet_info.net_hlen; /* layer 4 */ assert(packet_info.flow_len == packet_len); assert(packet_info.flow_sport == sport); assert(packet_info.flow_dport == dport); packet_len -= packet_info.flow_hlen; /* layer 7 */ assert(packet_info.app_len = packet_len); peak_load_exit(trace); }
int main(int argc, char **argv) { struct peak_tracks *peek; struct peak_load *trace; timeslice_t timer; int c; while ((c = getopt(argc, argv, "AafNnt")) != -1) { switch (c) { case 'A': use_print[use_count++] = USE_APP_LEN; break; case 'a': use_print[use_count++] = USE_APP; break; case 'f': use_print[use_count++] = USE_FLOW; break; case 'N': use_print[use_count++] = USE_IP_LEN; break; case 'n': use_print[use_count++] = USE_IP_TYPE; break; case 't': use_print[use_count++] = USE_TIME; break; default: usage(); /* NOTREACHED */ } if (use_count > USE_MAX) { usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; if (argc < 1) { usage(); /* NOTREACHED */ } if (!use_count) { /* set the default output (as used by tests) */ use_print[use_count++] = USE_FLOW; use_print[use_count++] = USE_IP_TYPE; use_print[use_count++] = USE_IP_LEN; use_print[use_count++] = USE_APP; use_print[use_count++] = USE_TIME; } trace = peak_load_init(argv[0]); if (!trace) { panic("cannot init file loader\n"); } peek = peak_track_init(10000, 1); if (!peek) { panic("cannot init flow tracker\n"); } TIMESLICE_INIT(&timer); if (peak_load_packet(trace)) { TIMESLICE_CALIBRATE(&timer, &trace->ts); do { TIMESLICE_ADVANCE(&timer, &trace->ts); peek_packet(peek, &timer, trace->buf, trace->len, trace->ll); } while (peak_load_packet(trace)); } peak_track_exit(peek); peak_load_exit(trace); return (0); }