1 | #include <stdio.h> |
---|
2 | #include "libtrace.h" |
---|
3 | #include <map> |
---|
4 | |
---|
5 | struct libtrace_packet_t packet; |
---|
6 | int psize; |
---|
7 | |
---|
8 | /** parse an option |
---|
9 | * @param ptr the pointer to the current option |
---|
10 | * @param plen the length of the remaining buffer |
---|
11 | * @param type the type of the option |
---|
12 | * @param optlen the length of the option |
---|
13 | * @param data the data of the option |
---|
14 | * |
---|
15 | * @returns bool true if there is another option (and the fields are filled in) |
---|
16 | */ |
---|
17 | int get_next_option(unsigned char **ptr,int *len, |
---|
18 | unsigned char *type, |
---|
19 | unsigned char *optlen, |
---|
20 | unsigned char **data) |
---|
21 | { |
---|
22 | if (*len<=0) { |
---|
23 | // printf("Missing End of Options\n"); |
---|
24 | return 0; |
---|
25 | } |
---|
26 | *type=**ptr; |
---|
27 | switch(*type) { |
---|
28 | case 0: /* End of options */ |
---|
29 | // printf("End of option\n"); |
---|
30 | return 0; |
---|
31 | case 1: /* Pad */ |
---|
32 | (*ptr)++; |
---|
33 | (*len)--; |
---|
34 | return 1; |
---|
35 | default: |
---|
36 | case 6: // ECHO (obsolete) |
---|
37 | case 7: // ECHO Reply (obsolete) |
---|
38 | case 9: // Partial ordering |
---|
39 | case 10: // Partial ordering service profile |
---|
40 | case 11: // CC |
---|
41 | case 13: // CC.ECHO |
---|
42 | case 14: // Alternative checksum request |
---|
43 | case 15: // Alternative checksum data |
---|
44 | case 16: // Skeeter |
---|
45 | case 17: // Bubba |
---|
46 | case 18: // Trailer checksum |
---|
47 | case 19: // Md5 signature |
---|
48 | case 20: // SCPS capability |
---|
49 | case 21: // Selective NACK |
---|
50 | case 22: // Record boundary |
---|
51 | case 23: // Corruption experienced |
---|
52 | case 24: // SNAP |
---|
53 | case 25: // Unassigned |
---|
54 | case 26: // TCP Compression filter |
---|
55 | printf("Unknown option type (%i)\n",*type); |
---|
56 | case 2: // MSS |
---|
57 | case 3: // WS |
---|
58 | case 4: // SACK permitted |
---|
59 | case 5: // SACK |
---|
60 | case 8: // Timestamp |
---|
61 | case 12: // CC.new |
---|
62 | *optlen = *(*ptr+1); |
---|
63 | if (*optlen<2) { |
---|
64 | printf("Optlen <2?! %i\n",*optlen); |
---|
65 | return 0; // I have no idea wtf is going on |
---|
66 | // with these packets |
---|
67 | } |
---|
68 | (*len)-=(unsigned int)*optlen; |
---|
69 | (*data)=(*ptr+2); |
---|
70 | (*ptr)+=*optlen; |
---|
71 | if (*len<0) { |
---|
72 | printf("Option longer than option area (%i > %i)\n",*optlen,*len+*optlen); |
---|
73 | return 0; |
---|
74 | } |
---|
75 | return 1; |
---|
76 | } |
---|
77 | assert(0); |
---|
78 | } |
---|
79 | |
---|
80 | int main(int argc, char *argv[]) |
---|
81 | { |
---|
82 | struct libtrace_t *trace; |
---|
83 | double last = 0; |
---|
84 | |
---|
85 | trace = trace_create(argv[1]); |
---|
86 | |
---|
87 | for (;;) { |
---|
88 | int psize; |
---|
89 | |
---|
90 | if ((psize = trace_read_packet(trace, &packet)) <= 0) { |
---|
91 | break; |
---|
92 | } |
---|
93 | |
---|
94 | struct libtrace_ip *ipptr = trace_get_ip(&packet); |
---|
95 | |
---|
96 | if (!ipptr) |
---|
97 | continue; |
---|
98 | |
---|
99 | struct libtrace_tcp *tcpptr = trace_get_tcp(&packet); |
---|
100 | |
---|
101 | if (!tcpptr) |
---|
102 | continue; |
---|
103 | |
---|
104 | int plen = (tcpptr->doff*4-sizeof *tcpptr); |
---|
105 | int plen2; |
---|
106 | |
---|
107 | // The len of the section that isn't the tcp header |
---|
108 | plen2 = ((char*)tcpptr - (char*)packet.buffer); |
---|
109 | // Minus the length of the tcp header |
---|
110 | plen2 += sizeof *tcpptr; |
---|
111 | // Now, how much do we have left? |
---|
112 | plen2 = psize - plen2; |
---|
113 | |
---|
114 | if (plen != plen2) { |
---|
115 | printf("Snapped wrong (%i != %i)\n",plen,plen2); |
---|
116 | printf(" %i != %i\n",psize-plen,psize-plen2); |
---|
117 | printf("iph %i tcp->doff %i\n",ipptr->ip_hl,tcpptr->doff); |
---|
118 | printf("psize = %i totlen=%i\n",psize,htons(ipptr->ip_len)); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | double now=trace_get_seconds(&packet); |
---|
123 | |
---|
124 | if (now-last>60) { |
---|
125 | printf("."); |
---|
126 | fflush(stdout); |
---|
127 | last=now; |
---|
128 | } |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | return 0; |
---|
133 | } |
---|