1 | #include <stdio.h> |
---|
2 | #include <inttypes.h> |
---|
3 | #include <dlfcn.h> |
---|
4 | #include <map> |
---|
5 | #include "tracedump.h" |
---|
6 | #include <netinet/tcp.h> |
---|
7 | #include <netinet/in.h> |
---|
8 | #include <assert.h> |
---|
9 | |
---|
10 | #define SAFE(x) \ |
---|
11 | ((unsigned int)len>=((char*)&tcp->x-(char*)tcp+sizeof(tcp->x))) |
---|
12 | #define DISPLAY_EXP(x,fmt,exp) \ |
---|
13 | if (SAFE(x)) \ |
---|
14 | printf(fmt,exp); \ |
---|
15 | else \ |
---|
16 | return; |
---|
17 | |
---|
18 | #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,tcp->x) |
---|
19 | |
---|
20 | #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(tcp->x)) |
---|
21 | #define DISPLAYL(x,fmt) DISPLAY_EXP(x,fmt,htonl(tcp->x)) |
---|
22 | #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&tcp->x)) |
---|
23 | |
---|
24 | int get_next_option(unsigned char **ptr,int *len, |
---|
25 | unsigned char *type, |
---|
26 | unsigned char *optlen, |
---|
27 | unsigned char **data) |
---|
28 | { |
---|
29 | if (*len<=0) |
---|
30 | return 0; |
---|
31 | *type=**ptr; |
---|
32 | switch(*type) { |
---|
33 | case 0: |
---|
34 | return 0; |
---|
35 | case 1: |
---|
36 | (*ptr)++; |
---|
37 | (*len)--; |
---|
38 | return 1; |
---|
39 | default: |
---|
40 | *optlen = *(*ptr+1); |
---|
41 | assert(*optlen>=2); |
---|
42 | (*len)-=*optlen; |
---|
43 | (*data)=(*ptr+2); |
---|
44 | (*ptr)+=*optlen+2; |
---|
45 | if (*len<0) |
---|
46 | return 0; |
---|
47 | return 1; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | extern "C" |
---|
52 | void decode(int link_type,char *packet,int len) |
---|
53 | { |
---|
54 | struct tcphdr *tcp = (struct tcphdr*)packet; |
---|
55 | printf(" TCP:"); |
---|
56 | DISPLAYS(source," Source %i") |
---|
57 | DISPLAYS(dest," Dest %i") |
---|
58 | printf("\n TCP:"); |
---|
59 | DISPLAYL(seq," Seq %u"); |
---|
60 | printf("\n TCP:"); |
---|
61 | DISPLAYL(ack_seq," Ack %u"); |
---|
62 | if ((char*)&tcp->window-(char *)tcp>len) { |
---|
63 | printf("\n"); |
---|
64 | return; |
---|
65 | } |
---|
66 | printf("\n TCP:"); |
---|
67 | printf(" DOFF %i",tcp->doff); |
---|
68 | printf(" Flags:"); |
---|
69 | if (tcp->fin) printf(" FIN"); |
---|
70 | if (tcp->syn) printf(" SYN"); |
---|
71 | if (tcp->rst) printf(" RST"); |
---|
72 | if (tcp->psh) printf(" PSH"); |
---|
73 | if (tcp->ack) printf(" ACK"); |
---|
74 | if (tcp->urg) printf(" URG"); |
---|
75 | DISPLAYS(window," Window %i"); |
---|
76 | printf("\n TCP:"); |
---|
77 | DISPLAYS(check," Checksum %i"); |
---|
78 | DISPLAYS(urg_ptr," Urgent %i"); |
---|
79 | unsigned char *pkt = (unsigned char*)packet+sizeof(*tcp); |
---|
80 | int plen = (len-sizeof *tcp) <? (tcp->doff*4-sizeof *tcp); |
---|
81 | unsigned char type,optlen,*data; |
---|
82 | while(get_next_option(&pkt,&plen,&type,&optlen,&data)) { |
---|
83 | printf("\n TCP: "); |
---|
84 | switch(type) { |
---|
85 | case 0: |
---|
86 | printf("End of options"); |
---|
87 | break; |
---|
88 | case 1: |
---|
89 | printf("NOP"); |
---|
90 | break; |
---|
91 | case 2: |
---|
92 | printf("MSS %i",htons(*(uint32_t *)(data))); |
---|
93 | break; |
---|
94 | case 3: |
---|
95 | printf("Winscale %i",data[0]); |
---|
96 | break; |
---|
97 | case 4: |
---|
98 | printf("SACK"); |
---|
99 | break; |
---|
100 | case 5: |
---|
101 | printf("SACK Information"); |
---|
102 | int i; |
---|
103 | i=0; |
---|
104 | while(i+8<optlen) { |
---|
105 | printf("\n TCP: %u-%u", |
---|
106 | htonl(*(uint32_t*)&data[i]), |
---|
107 | htonl(*(uint32_t*)&data[i+4])); |
---|
108 | i+=8; |
---|
109 | } |
---|
110 | break; |
---|
111 | case 8: |
---|
112 | printf("Timestamp %u %u", |
---|
113 | htonl(*(uint32_t *)&data[0]), |
---|
114 | htonl(*(uint32_t *)&data[4]) |
---|
115 | ); |
---|
116 | break; |
---|
117 | default: |
---|
118 | printf("Unknown option %i",type); |
---|
119 | } |
---|
120 | } |
---|
121 | printf("\n"); |
---|
122 | if (htons(tcp->source) < htons(tcp->dest)) |
---|
123 | decode_next(packet+tcp->doff*4,len-tcp->doff*4,"tcp",htons(tcp->source)); |
---|
124 | else |
---|
125 | decode_next(packet+tcp->doff*4,len-tcp->doff*4,"tcp",htons(tcp->dest)); |
---|
126 | return; |
---|
127 | } |
---|