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