1 | #include <stdio.h> |
---|
2 | #include <assert.h> |
---|
3 | #include "libtrace.h" |
---|
4 | #include <map> |
---|
5 | |
---|
6 | uint64_t with_ts[4][65536]; |
---|
7 | uint64_t without_ts[4][65536]; |
---|
8 | |
---|
9 | struct libtrace_packet_t packet; |
---|
10 | |
---|
11 | /** parse an option |
---|
12 | * @param ptr the pointer to the current option |
---|
13 | * @param plen the length of the remaining buffer |
---|
14 | * @param type the type of the option |
---|
15 | * @param optlen the length of the option |
---|
16 | * @param data the data of the option |
---|
17 | * |
---|
18 | * @returns bool true if there is another option (and the fields are filled in) |
---|
19 | */ |
---|
20 | int get_next_option(unsigned char **ptr,int *len, |
---|
21 | unsigned char *type, |
---|
22 | unsigned char *optlen, |
---|
23 | unsigned char **data) |
---|
24 | { |
---|
25 | if (*len<=0) { |
---|
26 | // printf("Missing End of Options\n"); |
---|
27 | return 0; |
---|
28 | } |
---|
29 | *type=**ptr; |
---|
30 | switch(*type) { |
---|
31 | case 0: /* End of options */ |
---|
32 | // printf("End of option\n"); |
---|
33 | return 0; |
---|
34 | case 1: /* Pad */ |
---|
35 | (*ptr)++; |
---|
36 | (*len)--; |
---|
37 | return 1; |
---|
38 | default: |
---|
39 | case 6: // ECHO (obsolete) |
---|
40 | case 7: // ECHO Reply (obsolete) |
---|
41 | case 9: // Partial ordering |
---|
42 | case 10: // Partial ordering service profile |
---|
43 | case 11: // CC |
---|
44 | case 13: // CC.ECHO |
---|
45 | case 14: // Alternative checksum request |
---|
46 | case 15: // Alternative checksum data |
---|
47 | case 16: // Skeeter |
---|
48 | case 17: // Bubba |
---|
49 | case 18: // Trailer checksum |
---|
50 | case 19: // Md5 signature |
---|
51 | case 20: // SCPS capability |
---|
52 | case 21: // Selective NACK |
---|
53 | case 22: // Record boundary |
---|
54 | case 23: // Corruption experienced |
---|
55 | case 24: // SNAP |
---|
56 | case 25: // Unassigned |
---|
57 | case 26: // TCP Compression filter |
---|
58 | printf("Unknown option type (%i)\n",*type); |
---|
59 | case 2: // MSS |
---|
60 | case 3: // WS |
---|
61 | case 4: // SACK permitted |
---|
62 | case 5: // SACK |
---|
63 | case 8: // Timestamp |
---|
64 | case 12: // CC.new |
---|
65 | *optlen = *(*ptr+1); |
---|
66 | if (*optlen<2) { |
---|
67 | printf("Optlen <2?! %i\n",*optlen); |
---|
68 | return 0; // I have no idea wtf is going on |
---|
69 | // with these packets |
---|
70 | } |
---|
71 | (*len)-=(unsigned int)*optlen; |
---|
72 | (*data)=(*ptr+2); |
---|
73 | (*ptr)+=*optlen; |
---|
74 | if (*len<0) { |
---|
75 | printf("Option longer than option area (%i > %i)\n",*optlen,*len+*optlen); |
---|
76 | return 0; |
---|
77 | } |
---|
78 | return 1; |
---|
79 | } |
---|
80 | assert(0); |
---|
81 | } |
---|
82 | |
---|
83 | int main(int argc, char *argv[]) |
---|
84 | { |
---|
85 | struct libtrace_t *trace; |
---|
86 | double last = 0; |
---|
87 | |
---|
88 | trace = trace_create(argv[1]); |
---|
89 | |
---|
90 | for (;;) { |
---|
91 | struct libtrace_tcp *tcpptr; |
---|
92 | int psize; |
---|
93 | |
---|
94 | if ((psize = trace_read_packet(trace, &packet)) <= 0) { |
---|
95 | break; |
---|
96 | } |
---|
97 | |
---|
98 | tcpptr = trace_get_tcp(&packet); |
---|
99 | |
---|
100 | int dir = trace_get_direction(&packet); |
---|
101 | |
---|
102 | if (!tcpptr) |
---|
103 | continue; |
---|
104 | |
---|
105 | double now = trace_get_seconds(&packet); |
---|
106 | |
---|
107 | /* search for the timestamp option */ |
---|
108 | unsigned char *pkt = (unsigned char *)tcpptr + sizeof(*tcpptr); |
---|
109 | //int plen = (packet.size-(pkt-(unsigned char*)packet.buffer)) <? (tcpptr->doff*4-sizeof *tcpptr); |
---|
110 | int plen = (tcpptr->doff*4-sizeof *tcpptr); |
---|
111 | unsigned char type = 0; |
---|
112 | unsigned char optlen = 0; |
---|
113 | unsigned char *data = 0; |
---|
114 | bool flag = false; |
---|
115 | |
---|
116 | while (get_next_option(&pkt,&plen,&type,&optlen,&data) != 0) { |
---|
117 | // ignore non timestamp options |
---|
118 | if (type!=8) { |
---|
119 | continue; |
---|
120 | } |
---|
121 | |
---|
122 | flag=true; |
---|
123 | } |
---|
124 | |
---|
125 | if (flag) { |
---|
126 | if (htons(tcpptr->dest) < htons(tcpptr->source)) |
---|
127 | with_ts[dir][htons(tcpptr->dest)]++; |
---|
128 | else |
---|
129 | with_ts[dir][htons(tcpptr->source)]++; |
---|
130 | } |
---|
131 | else { |
---|
132 | if (htons(tcpptr->dest) < htons(tcpptr->source)) |
---|
133 | without_ts[dir][htons(tcpptr->dest)]++; |
---|
134 | else |
---|
135 | without_ts[dir][htons(tcpptr->source)]++; |
---|
136 | } |
---|
137 | |
---|
138 | if (now-last>60) { |
---|
139 | int i=0; |
---|
140 | |
---|
141 | last=now; |
---|
142 | |
---|
143 | printf("\n"); |
---|
144 | uint64_t with=0; |
---|
145 | uint64_t without=0; |
---|
146 | for(i=0;i<65535;i++) { |
---|
147 | if (with_ts[0][i]+without_ts[0][i]+ |
---|
148 | with_ts[1][i]+without_ts[1][i] <20) |
---|
149 | continue; |
---|
150 | printf("%6i: %6lli %6lli %6lli %6lli (%6.02f%%)\n", |
---|
151 | i, |
---|
152 | with_ts[0][i], |
---|
153 | without_ts[0][i], |
---|
154 | with_ts[1][i], |
---|
155 | without_ts[1][i], |
---|
156 | (with_ts[0][i]+with_ts[1][i])*100.0/( |
---|
157 | with_ts[0][i]+without_ts[0][i]+ |
---|
158 | with_ts[1][i]+without_ts[1][i]) |
---|
159 | ); |
---|
160 | with+=with_ts[0][i]+with_ts[1][i]; |
---|
161 | without+=without_ts[0][i]+without_ts[1][i]; |
---|
162 | with_ts[0][i]=0; |
---|
163 | without_ts[0][i]=0; |
---|
164 | with_ts[1][i]=0; |
---|
165 | without_ts[1][i]=0; |
---|
166 | } |
---|
167 | printf("%6s: %6lli %6lli (%6.02f%%)\n", |
---|
168 | "", |
---|
169 | with, |
---|
170 | without, |
---|
171 | with*100.0/(with+without) |
---|
172 | ); |
---|
173 | } |
---|
174 | |
---|
175 | } |
---|
176 | |
---|
177 | return 0; |
---|
178 | } |
---|