Fix TCP follow stream feature:
[obnox/wireshark/wip.git] / follow.c
1 /* follow.c
2  *
3  * $Id: follow.c,v 1.14 1999/07/31 13:55:16 deniel Exp $
4  *
5  * Copyright 1998 Mike Hall <mlh@io.com>
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  *
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <gtk/gtk.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
42 #endif
43
44 #include <glib.h>
45 #include "ethereal.h"
46 #include "packet.h"
47 #include "follow.h"
48
49 extern FILE* data_out_file;
50
51 gboolean incomplete_tcp_stream = FALSE;
52
53 static u_long  ip_address[2];
54 static u_int   tcp_port[2];
55
56 static int check_fragments( int );
57 static void write_packet_data( const u_char *, int );
58
59 /* this will build libpcap filter text that will only 
60    pass the packets related to the stream. There is a 
61    chance that two streams could intersect, but not a 
62    very good one */
63 char* 
64 build_follow_filter( packet_info *pi ) {
65   char* buf = malloc(1024);
66   if( pi->ipproto == 6 ) {
67     /* TCP */
68     sprintf( buf, 
69              "(ip.addr eq %s and ip.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
70              ip_to_str( (guint8 *) &pi->ip_src), 
71              ip_to_str( (guint8 *) &pi->ip_dst), 
72              pi->srcport, pi->destport );
73   }
74   else { 
75     free( buf );
76     return NULL;
77   }
78   ip_address[0] = pi->ip_src;
79   ip_address[1] = pi->ip_dst;
80   tcp_port[0] = pi->srcport;
81   tcp_port[1] = pi->destport;
82   return buf;
83 }
84
85 /* here we are going to try and reconstruct the data portion of a TCP
86    session. We will try and handle duplicates, TCP fragments, and out 
87    of order packets in a smart way. */
88
89 static tcp_frag *frags[2] = { 0, 0};
90 static u_long seq[2];
91 static u_long src[2] = { 0, 0 };
92
93 void 
94 reassemble_tcp( u_long sequence, u_long length, const char* data, u_long data_length, int synflag, u_long srcx, u_long dstx, u_int srcport, u_int dstport ) {
95   int src_index, j, first = 0;
96   u_long newseq;
97   tcp_frag *tmp_frag;
98   src_index = -1;
99
100   /* first check if this packet should be processed */
101   if ((srcx != ip_address[0] && srcx != ip_address[1]) ||
102       (dstx != ip_address[0] && dstx != ip_address[1]) ||
103       (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
104       (dstport != tcp_port[0] && dstport != tcp_port[1]))
105     return;
106
107   /* first we check to see if we have seen this src ip before. */
108   for( j=0; j<2; j++ ) {
109     if( src[j] == srcx ) {
110       src_index = j;
111     }
112   }
113   /* we didn't find it if src_index == -1 */
114   if( src_index < 0 ) {
115     /* assign it to a src_index and get going */
116     for( j=0; j<2; j++ ) {
117       if( src[j] == 0 ) {
118         src[j] = srcx;
119         src_index = j;
120         first = 1;
121         break;
122       }
123     }
124   }
125   if( src_index < 0 ) {
126     fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
127     return;
128   }
129
130   if( data_length < length ) {
131     incomplete_tcp_stream = TRUE;
132   }
133
134   /* now that we have filed away the srcs, lets get the sequence number stuff 
135      figured out */
136   if( first ) {
137     /* this is the first time we have seen this src's sequence number */
138     seq[src_index] = sequence + length;
139     if( synflag ) {
140       seq[src_index]++;
141     }
142     /* write out the packet data */
143     write_packet_data( data, data_length );
144     return;
145   }
146   /* if we are here, we have already seen this src, let's
147      try and figure out if this packet is in the right place */
148   if( sequence < seq[src_index] ) {
149     /* this sequence number seems dated, but 
150        check the end to make sure it has no more
151        info than we have already seen */
152     newseq = sequence + length;
153     if( newseq > seq[src_index] ) {
154       u_long new_len;
155
156       /* this one has more than we have seen. let's get the 
157          payload that we have not seen. */
158
159       new_len = seq[src_index] - sequence;
160
161       if ( data_length <= new_len ) {
162         data = NULL;
163         data_length = 0;
164         incomplete_tcp_stream = TRUE;
165       } else {
166         data += new_len;
167         data_length -= new_len;
168       }
169       sequence = seq[src_index];
170       length = newseq - seq[src_index];
171       
172       /* this will now appear to be right on time :) */
173     }
174   }
175   if ( sequence == seq[src_index] ) {
176     /* right on time */
177     seq[src_index] += length;
178     if( synflag ) seq[src_index]++;
179     if( data ) {
180       write_packet_data( data, data_length );
181     }
182     /* done with the packet, see if it caused a fragment to fit */
183     while( check_fragments( src_index ) )
184       ;
185   }
186   else {
187     /* out of order packet */
188     if( sequence > seq[src_index] ) {
189       tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
190       tmp_frag->data = (u_char *)malloc( data_length );
191       tmp_frag->seq = sequence;
192       tmp_frag->len = length;
193       tmp_frag->data_len = data_length;
194       memcpy( tmp_frag->data, data, data_length );
195       if( frags[src_index] ) {
196         tmp_frag->next = frags[src_index];
197       } else {
198         tmp_frag->next = NULL;
199       }
200       frags[src_index] = tmp_frag;
201     }
202   }
203 } /* end reassemble_tcp */
204
205 /* here we search through all the frag we have collected to see if
206    one fits */
207 static int 
208 check_fragments( int index ) {
209   tcp_frag *prev = NULL;
210   tcp_frag *current;
211   current = frags[index];
212   while( current ) {
213     if( current->seq == seq[index] ) {
214       /* this fragment fits the stream */
215       if( current->data ) {
216         write_packet_data( current->data, current->data_len );
217       }
218       seq[index] += current->len;
219       if( prev ) {
220         prev->next = current->next;
221       } else {
222         frags[index] = current->next;
223       }
224       free( current->data );
225       free( current );
226       return 1;
227     }
228     prev = current;
229     current = current->next;
230   }
231   return 0;
232 }
233
234 /* this should always be called before we start to reassemble a stream */
235 void 
236 reset_tcp_reassembly() {
237   tcp_frag *current, *next;
238   int i;
239   incomplete_tcp_stream = FALSE;
240   for( i=0; i<2; i++ ) {
241     seq[i] = 0;
242     src[i] = 0;
243     ip_address[i] = 0;
244     tcp_port[i] = 0;
245     current = frags[i];
246     while( current ) {
247       next = current->next;
248       free( current->data ); 
249       free( current );
250       current = next;
251     }
252     frags[i] = NULL;
253   }
254 }
255
256 static void 
257 write_packet_data( const u_char* data, int length ) {
258   fwrite( data, 1, length, data_out_file );
259 }
260