fc385bdd90651b4c71f9561759f86d60ab3094f3
[obnox/wireshark/wip.git] / follow.c
1 /* follow.c
2  *
3  * $Id: follow.c,v 1.30 2002/08/02 23:35:46 jmayer Exp $
4  *
5  * Copyright 1998 Mike Hall <mlh@io.com>
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program 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 General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <glib.h>
39 #include <epan/packet.h>
40 #include "follow.h"
41
42 FILE* data_out_file = NULL;
43
44 gboolean incomplete_tcp_stream = FALSE;
45
46 static guint8  ip_address[2][MAX_IPADDR_LEN];
47 static guint   tcp_port[2];
48 static guint   bytes_written[2];
49 static gboolean is_ipv6 = FALSE;
50
51 static int check_fragments( int, tcp_stream_chunk * );
52 static void write_packet_data( int, tcp_stream_chunk *, const char * );
53
54 void
55 follow_tcp_stats(follow_tcp_stats_t* stats)
56 {
57         int i;
58
59         for (i = 0; i < 2 ; i++) {
60                 memcpy(stats->ip_address[i], ip_address[i], MAX_IPADDR_LEN);
61                 stats->tcp_port[i] = tcp_port[i];
62                 stats->bytes_written[i] = bytes_written[i];
63                 stats->is_ipv6 = is_ipv6;
64         }
65 }
66
67 /* this will build libpcap filter text that will only 
68    pass the packets related to the stream. There is a 
69    chance that two streams could intersect, but not a 
70    very good one */
71 char* 
72 build_follow_filter( packet_info *pi ) {
73   char* buf = g_malloc(1024);
74   int len;
75   if( pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4
76         && pi->ipproto == 6 ) {
77     /* TCP over IPv4 */
78     sprintf( buf, 
79              "(ip.addr eq %s and ip.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
80              ip_to_str( pi->net_src.data), 
81              ip_to_str( pi->net_dst.data), 
82              pi->srcport, pi->destport );
83     len = 4;
84     is_ipv6 = FALSE;
85   }
86   else if( pi->net_src.type == AT_IPv6 && pi->net_dst.type == AT_IPv6
87         && pi->ipproto == 6 ) {
88     /* TCP over IPv6 */
89     sprintf( buf, 
90              "(ipv6.addr eq %s and ipv6.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
91              ip6_to_str((struct e_in6_addr *)pi->net_src.data), 
92              ip6_to_str((struct e_in6_addr *)pi->net_dst.data), 
93              pi->srcport, pi->destport );
94     len = 16;
95     is_ipv6 = TRUE;
96   }
97   else { 
98     g_free( buf );
99     return NULL;
100   }
101   memcpy(ip_address[0], pi->net_src.data, len);
102   memcpy(ip_address[1], pi->net_dst.data, len);
103   tcp_port[0] = pi->srcport;
104   tcp_port[1] = pi->destport;
105   return buf;
106 }
107
108 /* here we are going to try and reconstruct the data portion of a TCP
109    session. We will try and handle duplicates, TCP fragments, and out 
110    of order packets in a smart way. */
111
112 static tcp_frag *frags[2] = { 0, 0 };
113 static gulong seq[2];
114 static guint8 src_addr[2][MAX_IPADDR_LEN];
115 static guint src_port[2] = { 0, 0 };
116
117 void 
118 reassemble_tcp( gulong sequence, gulong length, const char* data,
119                 gulong data_length, int synflag, address *net_src,
120                 address *net_dst, guint srcport, guint dstport) {
121   guint8 srcx[MAX_IPADDR_LEN], dstx[MAX_IPADDR_LEN];
122   int src_index, j, first = 0, len;
123   gulong newseq;
124   tcp_frag *tmp_frag;
125   tcp_stream_chunk sc;
126   
127   src_index = -1;
128   
129   /* First, check if this packet should be processed. */
130
131   if ((net_src->type != AT_IPv4 && net_src->type != AT_IPv6) ||
132       (net_dst->type != AT_IPv4 && net_dst->type != AT_IPv6))
133     return;
134
135   if (net_src->type == AT_IPv4)
136     len = 4;
137   else
138     len = 16;
139
140   /* Now check if the packet is for this connection. */
141   memcpy(srcx, net_src->data, len);
142   memcpy(dstx, net_dst->data, len);
143   if ((memcmp(srcx, ip_address[0], len) != 0 && 
144        memcmp(srcx, ip_address[1], len) != 0) ||
145       (memcmp(dstx, ip_address[0], len) != 0 &&
146        memcmp(dstx, ip_address[1], len) != 0) ||
147       (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
148       (dstport != tcp_port[0] && dstport != tcp_port[1]))
149     return;
150
151   /* Initialize our stream chunk.  This data gets written to disk. */
152   memcpy(sc.src_addr, srcx, len);
153   sc.src_port = srcport;
154   sc.dlen     = data_length;
155
156   /* Check to see if we have seen this source IP and port before.
157      (Yes, we have to check both source IP and port; the connection
158      might be between two different ports on the same machine.) */
159   for( j=0; j<2; j++ ) {
160     if (memcmp(src_addr[j], srcx, len) == 0 && src_port[j] == srcport ) {
161       src_index = j;
162     }
163   }
164   /* we didn't find it if src_index == -1 */
165   if( src_index < 0 ) {
166     /* assign it to a src_index and get going */
167     for( j=0; j<2; j++ ) {
168       if( src_port[j] == 0 ) {
169         memcpy(src_addr[j], srcx, len);
170         src_port[j] = srcport;
171         src_index = j;
172         first = 1;
173         break;
174       }
175     }
176   }
177   if( src_index < 0 ) {
178     fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
179     return;
180   }
181
182   if( data_length < length ) {
183     incomplete_tcp_stream = TRUE;
184   }
185
186   /* now that we have filed away the srcs, lets get the sequence number stuff 
187      figured out */
188   if( first ) {
189     /* this is the first time we have seen this src's sequence number */
190     seq[src_index] = sequence + length;
191     if( synflag ) {
192       seq[src_index]++;
193     }
194     /* write out the packet data */
195     write_packet_data( src_index, &sc, data );
196     return;
197   }
198   /* if we are here, we have already seen this src, let's
199      try and figure out if this packet is in the right place */
200   if( sequence < seq[src_index] ) {
201     /* this sequence number seems dated, but 
202        check the end to make sure it has no more
203        info than we have already seen */
204     newseq = sequence + length;
205     if( newseq > seq[src_index] ) {
206       gulong new_len;
207
208       /* this one has more than we have seen. let's get the 
209          payload that we have not seen. */
210
211       new_len = seq[src_index] - sequence;
212
213       if ( data_length <= new_len ) {
214         data = NULL;
215         data_length = 0;
216         incomplete_tcp_stream = TRUE;
217       } else {
218         data += new_len;
219         data_length -= new_len;
220       }
221       sc.dlen = data_length;
222       sequence = seq[src_index];
223       length = newseq - seq[src_index];
224       
225       /* this will now appear to be right on time :) */
226     }
227   }
228   if ( sequence == seq[src_index] ) {
229     /* right on time */
230     seq[src_index] += length;
231     if( synflag ) seq[src_index]++;
232     if( data ) {
233       write_packet_data( src_index, &sc, data );
234     }
235     /* done with the packet, see if it caused a fragment to fit */
236     while( check_fragments( src_index, &sc ) )
237       ;
238   }
239   else {
240     /* out of order packet */
241     if(data_length > 0 && sequence > seq[src_index] ) {
242       tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
243       tmp_frag->data = (guchar *)malloc( data_length );
244       tmp_frag->seq = sequence;
245       tmp_frag->len = length;
246       tmp_frag->data_len = data_length;
247       memcpy( tmp_frag->data, data, data_length );
248       if( frags[src_index] ) {
249         tmp_frag->next = frags[src_index];
250       } else {
251         tmp_frag->next = NULL;
252       }
253       frags[src_index] = tmp_frag;
254     }
255   }
256 } /* end reassemble_tcp */
257
258 /* here we search through all the frag we have collected to see if
259    one fits */
260 static int 
261 check_fragments( int index, tcp_stream_chunk *sc ) {
262   tcp_frag *prev = NULL;
263   tcp_frag *current;
264   current = frags[index];
265   while( current ) {
266     if( current->seq == seq[index] ) {
267       /* this fragment fits the stream */
268       if( current->data ) {
269         sc->dlen = current->data_len;
270         write_packet_data( index, sc, current->data );
271       }
272       seq[index] += current->len;
273       if( prev ) {
274         prev->next = current->next;
275       } else {
276         frags[index] = current->next;
277       }
278       free( current->data );
279       free( current );
280       return 1;
281     }
282     prev = current;
283     current = current->next;
284   }
285   return 0;
286 }
287
288 /* this should always be called before we start to reassemble a stream */
289 void 
290 reset_tcp_reassembly() {
291   tcp_frag *current, *next;
292   int i;
293   incomplete_tcp_stream = FALSE;
294   for( i=0; i<2; i++ ) {
295     seq[i] = 0;
296     memset(src_addr[i], '\0', MAX_IPADDR_LEN);
297     src_port[i] = 0;
298     memset(ip_address[i], '\0', MAX_IPADDR_LEN);
299     tcp_port[i] = 0;
300     bytes_written[i] = 0;
301     current = frags[i];
302     while( current ) {
303       next = current->next;
304       free( current->data ); 
305       free( current );
306       current = next;
307     }
308     frags[i] = NULL;
309   }
310 }
311
312 static void 
313 write_packet_data( int index, tcp_stream_chunk *sc, const char *data )
314 {
315   fwrite( sc, 1, sizeof(tcp_stream_chunk), data_out_file );
316   fwrite( data, 1, sc->dlen, data_out_file );
317   bytes_written[index] += sc->dlen;
318 }