1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | #include <stdio.h> |
---|
27 | #include <inttypes.h> |
---|
28 | #include <dlfcn.h> |
---|
29 | #include "libpacketdump.h" |
---|
30 | #include <assert.h> |
---|
31 | #include <netdb.h> |
---|
32 | |
---|
33 | DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len) |
---|
34 | { |
---|
35 | unsigned char *pkt = NULL; |
---|
36 | unsigned char type,optlen,*data; |
---|
37 | int plen, i; |
---|
38 | libtrace_tcp_t *tcp = (libtrace_tcp_t *)packet; |
---|
39 | printf(" TCP:"); |
---|
40 | if (SAFE(tcp, source)) { |
---|
41 | struct servent *ent=getservbyport(tcp->source,"tcp"); |
---|
42 | if(ent) { |
---|
43 | printf(" Source %i (%s)",htons(tcp->source),ent->s_name); |
---|
44 | } else { |
---|
45 | printf(" Source %i",htons(tcp->source)); |
---|
46 | } |
---|
47 | } |
---|
48 | else { |
---|
49 | printf("\n"); |
---|
50 | return; |
---|
51 | } |
---|
52 | if (SAFE(tcp, dest)) { |
---|
53 | struct servent *ent=getservbyport(tcp->dest,"tcp"); |
---|
54 | if(ent) { |
---|
55 | printf(" Dest %i (%s)",htons(tcp->dest),ent->s_name); |
---|
56 | } else { |
---|
57 | printf(" Dest %i",htons(tcp->dest)); |
---|
58 | } |
---|
59 | } |
---|
60 | else { |
---|
61 | printf("\n"); |
---|
62 | return; |
---|
63 | } |
---|
64 | printf("\n TCP:"); |
---|
65 | DISPLAYL(tcp, seq," Seq %u"); |
---|
66 | printf("\n TCP:"); |
---|
67 | DISPLAYL(tcp, ack_seq," Ack %u"); |
---|
68 | if ((char*)&tcp->window-(char *)tcp>len) { |
---|
69 | printf("\n"); |
---|
70 | return; |
---|
71 | } |
---|
72 | printf("\n TCP:"); |
---|
73 | printf(" DOFF %i",tcp->doff); |
---|
74 | printf(" Flags:"); |
---|
75 | if (tcp->ecn_ns) printf(" ECN_NS"); |
---|
76 | if (tcp->cwr) printf(" CWR"); |
---|
77 | if (tcp->ece) printf(" ECE"); |
---|
78 | if (tcp->fin) printf(" FIN"); |
---|
79 | if (tcp->syn) printf(" SYN"); |
---|
80 | if (tcp->rst) printf(" RST"); |
---|
81 | if (tcp->psh) printf(" PSH"); |
---|
82 | if (tcp->ack) printf(" ACK"); |
---|
83 | if (tcp->urg) printf(" URG"); |
---|
84 | DISPLAYS(tcp, window," Window %i"); |
---|
85 | printf("\n TCP:"); |
---|
86 | DISPLAYS(tcp, check," Checksum %i"); |
---|
87 | DISPLAYS(tcp, urg_ptr," Urgent %i"); |
---|
88 | pkt = (unsigned char*)packet+sizeof(*tcp); |
---|
89 | plen = (len-sizeof *tcp) < (tcp->doff*4-sizeof(*tcp))?(len-sizeof(*tcp)):(tcp->doff*4-sizeof *tcp); |
---|
90 | while(trace_get_next_option(&pkt,&plen,&type,&optlen,&data)) { |
---|
91 | printf("\n TCP: "); |
---|
92 | switch(type) { |
---|
93 | case 0: |
---|
94 | printf("End of options"); |
---|
95 | break; |
---|
96 | case 1: |
---|
97 | printf("NOP"); |
---|
98 | break; |
---|
99 | case 2: |
---|
100 | printf("MSS %i",htons(*(uint32_t *)(data))); |
---|
101 | break; |
---|
102 | case 3: |
---|
103 | printf("Winscale %i",data[0]); |
---|
104 | break; |
---|
105 | case 4: |
---|
106 | printf("SACK"); |
---|
107 | break; |
---|
108 | case 5: |
---|
109 | printf("SACK Information"); |
---|
110 | i=0; |
---|
111 | while(i+8<optlen) { |
---|
112 | printf("\n TCP: %u-%u", |
---|
113 | htonl(*(uint32_t*)&data[i]), |
---|
114 | htonl(*(uint32_t*)&data[i+4])); |
---|
115 | i+=8; |
---|
116 | } |
---|
117 | break; |
---|
118 | case 8: |
---|
119 | printf("Timestamp %u %u", |
---|
120 | htonl(*(uint32_t *)&data[0]), |
---|
121 | htonl(*(uint32_t *)&data[4]) |
---|
122 | ); |
---|
123 | break; |
---|
124 | default: |
---|
125 | printf("Unknown option %i",type); |
---|
126 | } |
---|
127 | } |
---|
128 | printf("\n"); |
---|
129 | if (htons(tcp->source) < htons(tcp->dest)) |
---|
130 | decode_next(packet+tcp->doff*4,len-tcp->doff*4,"tcp",htons(tcp->source)); |
---|
131 | else |
---|
132 | decode_next(packet+tcp->doff*4,len-tcp->doff*4,"tcp",htons(tcp->dest)); |
---|
133 | return; |
---|
134 | } |
---|