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