1 | #include <libtrace.h> |
---|
2 | #include <err.h> |
---|
3 | #include <time.h> |
---|
4 | #include "libpacketdump.h" |
---|
5 | #include <stdio.h> |
---|
6 | #include <stdlib.h> |
---|
7 | #include <getopt.h> |
---|
8 | |
---|
9 | void usage(char *argv0) |
---|
10 | { |
---|
11 | fprintf(stderr,"Usage:\n" |
---|
12 | "%s flags inputfile\n" |
---|
13 | "-f --filter=expr BPF filter specification, quoted\n" |
---|
14 | "-c --count=num terminate after num packets\n" |
---|
15 | "-H --libtrace-help Print libtrace runtime documentation\n" |
---|
16 | ,argv0); |
---|
17 | exit(0); |
---|
18 | } |
---|
19 | |
---|
20 | int main(int argc,char **argv) |
---|
21 | { |
---|
22 | struct libtrace_t *trace = NULL; |
---|
23 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
24 | struct libtrace_filter_t *filter=NULL; |
---|
25 | uint64_t count=0; |
---|
26 | uint64_t numpackets=0; |
---|
27 | |
---|
28 | |
---|
29 | if (argc<2) |
---|
30 | usage(argv[0]); |
---|
31 | |
---|
32 | while(1) { |
---|
33 | int option_index; |
---|
34 | struct option long_options[] = { |
---|
35 | { "filter", 1, 0, 'f' }, |
---|
36 | { "count", 1, 0, 'c' }, |
---|
37 | { "libtrace-help", 0, 0, 'H' }, |
---|
38 | { NULL, 0, 0, 0 }, |
---|
39 | }; |
---|
40 | |
---|
41 | int c=getopt_long(argc,argv,"f:c:H", |
---|
42 | long_options, &option_index); |
---|
43 | if (c == -1) |
---|
44 | break; |
---|
45 | switch(c) { |
---|
46 | case 'f': |
---|
47 | if (filter!=NULL) { |
---|
48 | fprintf(stderr,"You can only have one filter (quote it with " ")\n"); |
---|
49 | usage(argv[0]); |
---|
50 | } |
---|
51 | filter=trace_create_filter(optarg); |
---|
52 | break; |
---|
53 | case 'c': count=atol(optarg); break; |
---|
54 | case 'H': |
---|
55 | trace_help(); |
---|
56 | exit(1); |
---|
57 | break; |
---|
58 | default: |
---|
59 | printf("unknown option: %c\n",c); |
---|
60 | usage(argv[0]); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | while(optind <argc) { |
---|
67 | trace = trace_create(argv[optind]); |
---|
68 | optind ++; |
---|
69 | numpackets = 0; |
---|
70 | if (trace_is_err(trace)) { |
---|
71 | trace_perror(trace,"trace_create"); |
---|
72 | trace_destroy(trace); |
---|
73 | continue; |
---|
74 | } |
---|
75 | |
---|
76 | trace_start(trace); |
---|
77 | if (trace_is_err(trace)) { |
---|
78 | trace_perror(trace,"trace_start"); |
---|
79 | trace_destroy(trace); |
---|
80 | continue; |
---|
81 | } |
---|
82 | while(trace_read_packet(trace,packet)> 0 ){ |
---|
83 | if (filter && !trace_apply_filter(filter,packet)) |
---|
84 | continue; |
---|
85 | |
---|
86 | trace_dump_packet(packet); |
---|
87 | |
---|
88 | if(count) { |
---|
89 | numpackets++; |
---|
90 | if (numpackets == count) |
---|
91 | break; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | printf("\n"); |
---|
96 | trace_destroy(trace); |
---|
97 | } |
---|
98 | return 0; |
---|
99 | } |
---|