[48a20e3] | 1 | /* Complete libtrace skeleton program |
---|
| 2 | * |
---|
| 3 | * This libtrace skeleton includes everything you need for a useful libtrace |
---|
| 4 | * program, including command line parsing, dealing with bpf filters etc. |
---|
| 5 | * |
---|
| 6 | */ |
---|
| 7 | #include "libtrace.h" |
---|
| 8 | #include <stdio.h> |
---|
| 9 | #include <getopt.h> |
---|
| 10 | #include <stdlib.h> |
---|
[f724640] | 11 | #include <assert.h> |
---|
[48a20e3] | 12 | |
---|
[0f42351] | 13 | static void per_packet(libtrace_packet_t *packet) |
---|
[48a20e3] | 14 | { |
---|
[f724640] | 15 | assert(packet); |
---|
[48a20e3] | 16 | /* Your code goes here */ |
---|
| 17 | } |
---|
| 18 | |
---|
[0f42351] | 19 | static void usage(char *argv0) |
---|
[48a20e3] | 20 | { |
---|
[34ee7eb] | 21 | fprintf(stderr,"usage: %s [ --filter | -f bpfexp ] [ --snaplen | -s snap ]\n\t\t[ --promisc | -p flag] [ --help | -h ] [ --libtrace-help | -H ] libtraceuri...\n",argv0); |
---|
[48a20e3] | 22 | } |
---|
| 23 | |
---|
| 24 | int main(int argc, char *argv[]) |
---|
| 25 | { |
---|
| 26 | libtrace_t *trace; |
---|
| 27 | libtrace_packet_t *packet; |
---|
| 28 | libtrace_filter_t *filter=NULL; |
---|
| 29 | int snaplen=-1; |
---|
| 30 | int promisc=-1; |
---|
| 31 | |
---|
| 32 | while(1) { |
---|
| 33 | int option_index; |
---|
| 34 | struct option long_options[] = { |
---|
[34ee7eb] | 35 | { "filter", 1, 0, 'f' }, |
---|
| 36 | { "snaplen", 1, 0, 's' }, |
---|
| 37 | { "promisc", 1, 0, 'p' }, |
---|
| 38 | { "help", 0, 0, 'h' }, |
---|
| 39 | { "libtrace-help", 0, 0, 'H' }, |
---|
| 40 | { NULL, 0, 0, 0 } |
---|
[48a20e3] | 41 | }; |
---|
| 42 | |
---|
[34ee7eb] | 43 | int c= getopt_long(argc, argv, "f:s:p:hH", |
---|
[48a20e3] | 44 | long_options, &option_index); |
---|
| 45 | |
---|
| 46 | if (c==-1) |
---|
| 47 | break; |
---|
| 48 | |
---|
| 49 | switch (c) { |
---|
| 50 | case 'f': |
---|
[0eef5d4] | 51 | filter=trace_create_filter(optarg); |
---|
[48a20e3] | 52 | break; |
---|
| 53 | case 's': |
---|
| 54 | snaplen=atoi(optarg); |
---|
| 55 | break; |
---|
| 56 | case 'p': |
---|
| 57 | promisc=atoi(optarg); |
---|
| 58 | break; |
---|
[34ee7eb] | 59 | case 'H': |
---|
| 60 | trace_help(); |
---|
| 61 | return 1; |
---|
[48a20e3] | 62 | default: |
---|
| 63 | fprintf(stderr,"Unknown option: %c\n",c); |
---|
| 64 | /* FALL THRU */ |
---|
| 65 | case 'h': |
---|
| 66 | usage(argv[0]); |
---|
| 67 | return 1; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | if (optind>=argc) { |
---|
| 72 | fprintf(stderr,"Missing input uri\n"); |
---|
| 73 | usage(argv[0]); |
---|
| 74 | return 1; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | while (optind<argc) { |
---|
| 78 | trace = trace_create(argv[optind]); |
---|
| 79 | ++optind; |
---|
| 80 | |
---|
| 81 | if (trace_is_err(trace)) { |
---|
| 82 | trace_perror(trace,"Opening trace file"); |
---|
| 83 | return 1; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | if (snaplen>0) |
---|
| 87 | if (trace_config(trace,TRACE_OPTION_SNAPLEN,&snaplen)) { |
---|
| 88 | trace_perror(trace,"ignoring: "); |
---|
| 89 | } |
---|
| 90 | if (filter) |
---|
| 91 | if (trace_config(trace,TRACE_OPTION_FILTER,filter)) { |
---|
| 92 | trace_perror(trace,"ignoring: "); |
---|
| 93 | } |
---|
| 94 | if (promisc!=-1) { |
---|
| 95 | if (trace_config(trace,TRACE_OPTION_PROMISC,&promisc)) { |
---|
| 96 | trace_perror(trace,"ignoring: "); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | if (trace_start(trace)) { |
---|
| 101 | trace_perror(trace,"Starting trace"); |
---|
| 102 | trace_destroy(trace); |
---|
| 103 | return 1; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | packet = trace_create_packet(); |
---|
| 107 | |
---|
| 108 | while (trace_read_packet(trace,packet)>0) { |
---|
| 109 | per_packet(packet); |
---|
| 110 | } |
---|
| 111 | |
---|
[0eef5d4] | 112 | trace_destroy_packet(packet); |
---|
| 113 | |
---|
[48a20e3] | 114 | if (trace_is_err(trace)) { |
---|
| 115 | trace_perror(trace,"Reading packets"); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | trace_destroy(trace); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | return 0; |
---|
| 122 | } |
---|