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