Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* coral_test.c - test libcoral's packet counters on pcap input
- * Author: Jeff Terrell, <first>.<last> at acm.org
- * Date: July 6, 2010
- * Environment:
- - (Linode) Ubuntu Linux system, v2.6.32.12-x86_64
- - CoralReef package version: 3.8.6-public
- - libpcap version: 1.0.0
- * Compiler command:
- gcc -I/usr/local/Coral/include -g coral_test.c -L/usr/local/Coral/lib \
- -lcoral -lpcap -lz -lstdc++ -o coral_test
- * Usage: specify a pcap trace file as the only argument.
- * Function: read a pcap trace file with libcoral and print 3 things:
- 1. the coral_pkt_stats_t counters for a day-long interval
- 2. the coral_pkt_stats_t counters for the pcap interface
- 3. the number of packets read from coral/pcap by this program
- The <received packets> field of #1 and #2 should be the same as #3.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <error.h>
- #include <libcoral.h>
- void print_coral_stats(const coral_pkt_stats_t*);
- int main(int argc, char **argv)
- {
- int retval;
- int pkts_read = 0;
- struct timeval interval = {60 * 60 * 24, 0};
- coral_iface_t *iface;
- coral_pkt_result_t pkt;
- coral_interval_result_t ival;
- const coral_pkt_stats_t *stats;
- if (argc != 2) {
- error(1, 0, "specify a pcap file as the only argument");
- }
- if (!coral_new_source(argv[1])) {
- error(2, errno, "failed to open pcap input file %s", argv[1]);
- }
- retval = coral_open_all();
- if (retval < 1) {
- error(3, errno, "error opening sources (retval is %d)", retval);
- }
- retval = coral_start_all();
- if (retval != 0) {
- error(4, errno, "error starting sources (retval is %d)", retval);
- }
- retval = coral_read_pkt_init(NULL, NULL, &interval);
- if (retval != 0) {
- error(5, errno, "error with coral_read_pkt_init (retval is %d)", retval);
- }
- while (1) {
- iface = coral_read_pkt(&pkt, &ival);
- if (!iface) {
- break;
- }
- if (pkt.packet) {
- pkts_read++;
- } else if (ival.stats == NULL) {
- /* no-op */
- } else {
- printf("---> Stats for interval from %ld.%06ld to %ld.%06ld:\n",
- ival.begin.tv_sec, ival.begin.tv_usec,
- ival.end.tv_sec, ival.end.tv_usec);
- print_coral_stats(ival.stats);
- }
- }
- printf("---> Stats for the pcap interface:\n");
- iface = coral_next_interface(NULL);
- stats = coral_get_iface_stats(iface);
- print_coral_stats(stats);
- printf("\n%d packets read\n", pkts_read);
- return 0;
- }
- void print_coral_stats(const coral_pkt_stats_t *stats)
- {
- fprintf(stderr, "%12lld - layer 2 PDUs received\n",
- (long long int)stats->l2_recv);
- fprintf(stderr, "%12lld - layer 2 PDUs dropped\n",
- (long long int)stats->l2_drop);
- fprintf(stderr, "%12lld - packets received\n",
- (long long int)stats->pkts_recv);
- fprintf(stderr, "%12lld - packets dropped\n",
- (long long int)stats->pkts_drop);
- fprintf(stderr, "%12lld - packets truncated by insuff. capture\n",
- (long long int)stats->truncated);
- fprintf(stderr, "%12lld - cell looks bad from the capture device\n",
- (long long int)stats->driver_corrupt);
- fprintf(stderr, "%12lld - too many simultaneous vpvc pairs\n",
- (long long int)stats->too_many_vpvc);
- fprintf(stderr, "%12lld - AAL5 cell stream > MAX_PACKET_SIZE\n",
- (long long int)stats->buffer_overflow);
- fprintf(stderr, "%12lld - AAL5 trailer had bad length\n",
- (long long int)stats->aal5_trailer);
- fprintf(stderr, "%12lld - OK\n",
- (long long int)stats->ok_packet);
- }
Add Comment
Please, Sign In to add comment