Guest User

coral_test.c

a guest
Jul 6th, 2010
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. /* coral_test.c - test libcoral's packet counters on pcap input
  2.  * Author: Jeff Terrell, <first>.<last> at acm.org
  3.  * Date: July 6, 2010
  4.  * Environment:
  5.      - (Linode) Ubuntu Linux system, v2.6.32.12-x86_64
  6.      - CoralReef package version: 3.8.6-public
  7.      - libpcap version: 1.0.0
  8.  * Compiler command:
  9.      gcc -I/usr/local/Coral/include -g coral_test.c -L/usr/local/Coral/lib \
  10.         -lcoral -lpcap -lz -lstdc++ -o coral_test
  11.  * Usage: specify a pcap trace file as the only argument.
  12.  * Function: read a pcap trace file with libcoral and print 3 things:
  13.      1. the coral_pkt_stats_t counters for a day-long interval
  14.      2. the coral_pkt_stats_t counters for the pcap interface
  15.      3. the number of packets read from coral/pcap by this program
  16.      The <received packets> field of #1 and #2 should be the same as #3.
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <error.h>
  23. #include <libcoral.h>
  24.  
  25. void print_coral_stats(const coral_pkt_stats_t*);
  26.  
  27.  
  28. int main(int argc, char **argv)
  29. {
  30.   int retval;
  31.   int pkts_read = 0;
  32.   struct timeval interval = {60 * 60 * 24, 0};
  33.   coral_iface_t *iface;
  34.   coral_pkt_result_t pkt;
  35.   coral_interval_result_t ival;
  36.   const coral_pkt_stats_t *stats;
  37.  
  38.   if (argc != 2) {
  39.     error(1, 0, "specify a pcap file as the only argument");
  40.   }
  41.  
  42.   if (!coral_new_source(argv[1])) {
  43.     error(2, errno, "failed to open pcap input file %s", argv[1]);
  44.   }
  45.  
  46.   retval = coral_open_all();
  47.   if (retval < 1) {
  48.     error(3, errno, "error opening sources (retval is %d)", retval);
  49.   }
  50.  
  51.   retval = coral_start_all();
  52.   if (retval != 0) {
  53.     error(4, errno, "error starting sources (retval is %d)", retval);
  54.   }
  55.  
  56.   retval = coral_read_pkt_init(NULL, NULL, &interval);
  57.   if (retval != 0) {
  58.     error(5, errno, "error with coral_read_pkt_init (retval is %d)", retval);
  59.   }
  60.  
  61.   while (1) {
  62.     iface = coral_read_pkt(&pkt, &ival);
  63.     if (!iface) {
  64.       break;
  65.     }
  66.  
  67.     if (pkt.packet) {
  68.       pkts_read++;
  69.     } else if (ival.stats == NULL) {
  70.       /* no-op */
  71.     } else {
  72.       printf("---> Stats for interval from %ld.%06ld to %ld.%06ld:\n",
  73.           ival.begin.tv_sec, ival.begin.tv_usec,
  74.           ival.end.tv_sec,   ival.end.tv_usec);
  75.       print_coral_stats(ival.stats);
  76.     }
  77.   }
  78.  
  79.   printf("---> Stats for the pcap interface:\n");
  80.   iface = coral_next_interface(NULL);
  81.   stats = coral_get_iface_stats(iface);
  82.   print_coral_stats(stats);
  83.   printf("\n%d packets read\n", pkts_read);
  84.  
  85.   return 0;
  86. }
  87.  
  88. void print_coral_stats(const coral_pkt_stats_t *stats)
  89. {
  90.   fprintf(stderr, "%12lld - layer 2 PDUs received\n",
  91.       (long long int)stats->l2_recv);
  92.   fprintf(stderr, "%12lld - layer 2 PDUs dropped\n",
  93.       (long long int)stats->l2_drop);
  94.   fprintf(stderr, "%12lld - packets received\n",
  95.       (long long int)stats->pkts_recv);
  96.   fprintf(stderr, "%12lld - packets dropped\n",
  97.       (long long int)stats->pkts_drop);
  98.   fprintf(stderr, "%12lld - packets truncated by insuff. capture\n",
  99.       (long long int)stats->truncated);
  100.   fprintf(stderr, "%12lld - cell looks bad from the capture device\n",
  101.       (long long int)stats->driver_corrupt);
  102.   fprintf(stderr, "%12lld - too many simultaneous vpvc pairs\n",
  103.       (long long int)stats->too_many_vpvc);
  104.   fprintf(stderr, "%12lld - AAL5 cell stream > MAX_PACKET_SIZE\n",
  105.       (long long int)stats->buffer_overflow);
  106.   fprintf(stderr, "%12lld - AAL5 trailer had bad length\n",
  107.       (long long int)stats->aal5_trailer);
  108.   fprintf(stderr, "%12lld - OK\n",
  109.       (long long int)stats->ok_packet);
  110. }
Add Comment
Please, Sign In to add comment