1 | #include "libtrace.h" |
---|
2 | #include <inttypes.h> |
---|
3 | #include <stdio.h> |
---|
4 | #include <getopt.h> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <string.h> |
---|
7 | #include "lt_inttypes.h" |
---|
8 | |
---|
9 | double lastts = 0.0; |
---|
10 | uint64_t v4=0; |
---|
11 | uint64_t v6=0; |
---|
12 | uint64_t udp=0; |
---|
13 | uint64_t tcp=0; |
---|
14 | uint64_t icmp=0; |
---|
15 | uint64_t ok=0; |
---|
16 | |
---|
17 | static void per_packet(libtrace_packet_t *packet) |
---|
18 | { |
---|
19 | /* Packet data */ |
---|
20 | uint32_t remaining; |
---|
21 | /* L3 data */ |
---|
22 | void *l3; |
---|
23 | uint16_t ethertype; |
---|
24 | /* Transport data */ |
---|
25 | void *transport; |
---|
26 | uint8_t proto; |
---|
27 | /* Payload data */ |
---|
28 | void *payload; |
---|
29 | |
---|
30 | if (lastts < 1) |
---|
31 | lastts = trace_get_seconds(packet); |
---|
32 | |
---|
33 | if (lastts+1.0 < trace_get_seconds(packet)) { |
---|
34 | ++lastts; |
---|
35 | printf("%.03f,",lastts); |
---|
36 | printf("%"PRIu64",%"PRIu64",",v4,v6); |
---|
37 | printf("%"PRIu64",%"PRIu64",%"PRIu64,icmp,tcp,udp); |
---|
38 | printf("\n"); |
---|
39 | v4=v6=0; |
---|
40 | icmp=tcp=udp=0; |
---|
41 | } |
---|
42 | |
---|
43 | l3 = trace_get_layer3(packet,ðertype,&remaining); |
---|
44 | |
---|
45 | if (!l3) |
---|
46 | /* Probable ARP or something */ |
---|
47 | return; |
---|
48 | |
---|
49 | /* Get the UDP/TCP/ICMP header from the IPv4/IPv6 packet */ |
---|
50 | switch (ethertype) { |
---|
51 | case 0x0800: |
---|
52 | transport = trace_get_payload_from_ip( |
---|
53 | (libtrace_ip_t*)l3, |
---|
54 | &proto, |
---|
55 | &remaining); |
---|
56 | if (!transport) |
---|
57 | return; |
---|
58 | ++v4; |
---|
59 | break; |
---|
60 | case 0x86DD: |
---|
61 | transport = trace_get_payload_from_ip6( |
---|
62 | (libtrace_ip6_t*)l3, |
---|
63 | &proto, |
---|
64 | &remaining); |
---|
65 | if (!transport) |
---|
66 | return; |
---|
67 | ++v6; |
---|
68 | break; |
---|
69 | default: |
---|
70 | return; |
---|
71 | } |
---|
72 | |
---|
73 | /* Parse the udp/tcp/icmp payload */ |
---|
74 | switch(proto) { |
---|
75 | case 1: |
---|
76 | ++icmp; |
---|
77 | return; |
---|
78 | case 6: |
---|
79 | payload = trace_get_payload_from_tcp( |
---|
80 | (libtrace_tcp_t*)transport, |
---|
81 | &remaining); |
---|
82 | if (!payload) |
---|
83 | return; |
---|
84 | |
---|
85 | ++tcp; |
---|
86 | break; |
---|
87 | case 17: |
---|
88 | |
---|
89 | payload = trace_get_payload_from_udp( |
---|
90 | (libtrace_udp_t*)transport, |
---|
91 | &remaining); |
---|
92 | if (!payload) |
---|
93 | return; |
---|
94 | ++udp; |
---|
95 | break; |
---|
96 | default: |
---|
97 | return; |
---|
98 | } |
---|
99 | ++ok; |
---|
100 | } |
---|
101 | |
---|
102 | static void usage(char *argv0) |
---|
103 | { |
---|
104 | fprintf(stderr,"usage: %s [ --filter | -f bpfexp ] [ --snaplen | -s snap ]\n\t\t[ --promisc | -p flag] [ --help | -h ] [ --libtrace-help | -H ] libtraceuri...\n",argv0); |
---|
105 | } |
---|
106 | |
---|
107 | int main(int argc, char *argv[]) |
---|
108 | { |
---|
109 | libtrace_t *trace; |
---|
110 | libtrace_packet_t *packet; |
---|
111 | libtrace_filter_t *filter=NULL; |
---|
112 | int snaplen=-1; |
---|
113 | int promisc=-1; |
---|
114 | |
---|
115 | while(1) { |
---|
116 | int option_index; |
---|
117 | struct option long_options[] = { |
---|
118 | { "filter", 1, 0, 'f' }, |
---|
119 | { "snaplen", 1, 0, 's' }, |
---|
120 | { "promisc", 1, 0, 'p' }, |
---|
121 | { "help", 0, 0, 'h' }, |
---|
122 | { "libtrace-help", 0, 0, 'H' }, |
---|
123 | { NULL, 0, 0, 0 } |
---|
124 | }; |
---|
125 | |
---|
126 | int c= getopt_long(argc, argv, "f:s:p:hH", |
---|
127 | long_options, &option_index); |
---|
128 | |
---|
129 | if (c==-1) |
---|
130 | break; |
---|
131 | |
---|
132 | switch (c) { |
---|
133 | case 'f': |
---|
134 | filter=trace_create_filter(optarg); |
---|
135 | break; |
---|
136 | case 's': |
---|
137 | snaplen=atoi(optarg); |
---|
138 | break; |
---|
139 | case 'p': |
---|
140 | promisc=atoi(optarg); |
---|
141 | break; |
---|
142 | case 'H': |
---|
143 | trace_help(); |
---|
144 | return 1; |
---|
145 | default: |
---|
146 | fprintf(stderr,"Unknown option: %c\n",c); |
---|
147 | /* FALL THRU */ |
---|
148 | case 'h': |
---|
149 | usage(argv[0]); |
---|
150 | return 1; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | if (optind>=argc) { |
---|
155 | fprintf(stderr,"Missing input uri\n"); |
---|
156 | usage(argv[0]); |
---|
157 | return 1; |
---|
158 | } |
---|
159 | |
---|
160 | while (optind<argc) { |
---|
161 | trace = trace_create(argv[optind]); |
---|
162 | ++optind; |
---|
163 | |
---|
164 | if (trace_is_err(trace)) { |
---|
165 | trace_perror(trace,"Opening trace file"); |
---|
166 | return 1; |
---|
167 | } |
---|
168 | |
---|
169 | if (snaplen>0) |
---|
170 | if (trace_config(trace,TRACE_OPTION_SNAPLEN,&snaplen)) { |
---|
171 | trace_perror(trace,"ignoring: "); |
---|
172 | } |
---|
173 | if (filter) |
---|
174 | if (trace_config(trace,TRACE_OPTION_FILTER,filter)) { |
---|
175 | trace_perror(trace,"ignoring: "); |
---|
176 | } |
---|
177 | if (promisc!=-1) { |
---|
178 | if (trace_config(trace,TRACE_OPTION_PROMISC,&promisc)) { |
---|
179 | trace_perror(trace,"ignoring: "); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | if (trace_start(trace)) { |
---|
184 | trace_perror(trace,"Starting trace"); |
---|
185 | trace_destroy(trace); |
---|
186 | return 1; |
---|
187 | } |
---|
188 | |
---|
189 | packet = trace_create_packet(); |
---|
190 | |
---|
191 | while (trace_read_packet(trace,packet)>0) { |
---|
192 | per_packet(packet); |
---|
193 | } |
---|
194 | |
---|
195 | trace_destroy_packet(packet); |
---|
196 | |
---|
197 | if (trace_is_err(trace)) { |
---|
198 | trace_perror(trace,"Reading packets"); |
---|
199 | } |
---|
200 | |
---|
201 | trace_destroy(trace); |
---|
202 | } |
---|
203 | |
---|
204 | return 0; |
---|
205 | } |
---|