Stuart Stanley's ISIS dissection support.
[obnox/wireshark/wip.git] / follow.c
1 /* follow.c
2  *
3  * $Id: follow.c,v 1.20 1999/12/10 04:25:59 gram 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 <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42
43 #include <glib.h>
44 #include "packet.h"
45 #include "follow.h"
46
47 extern FILE* data_out_file;
48
49 gboolean incomplete_tcp_stream = FALSE;
50
51 static guint32 ip_address[2];
52 static u_int   tcp_port[2];
53
54 static int check_fragments( int, tcp_stream_chunk * );
55 static void write_packet_data( tcp_stream_chunk *, const char * );
56
57 /* this will build libpcap filter text that will only 
58    pass the packets related to the stream. There is a 
59    chance that two streams could intersect, but not a 
60    very good one */
61 char* 
62 build_follow_filter( packet_info *pi ) {
63   char* buf = malloc(1024);
64   if( pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4
65         && pi->ipproto == 6 ) {
66     /* TCP over IPv4 */
67     sprintf( buf, 
68              "(ip.addr eq %s and ip.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
69              ip_to_str( pi->net_src.data), 
70              ip_to_str( pi->net_dst.data), 
71              pi->srcport, pi->destport );
72   }
73   else { 
74     free( buf );
75     return NULL;
76   }
77   memcpy(&ip_address[0], pi->net_src.data, sizeof ip_address[0]);
78   memcpy(&ip_address[1], pi->net_dst.data, sizeof ip_address[1]);
79   tcp_port[0] = pi->srcport;
80   tcp_port[1] = pi->destport;
81   return buf;
82 }
83
84 /* here we are going to try and reconstruct the data portion of a TCP
85    session. We will try and handle duplicates, TCP fragments, and out 
86    of order packets in a smart way. */
87
88 static tcp_frag *frags[2] = { 0, 0};
89 static u_long seq[2];
90 static guint32 src[2] = { 0, 0 };
91
92 void 
93 reassemble_tcp( u_long sequence, u_long length, const char* data,
94                 u_long data_length, int synflag, address *net_src,
95                 address *net_dst, u_int srcport, u_int dstport,
96                 guint32 secs, guint32 usecs) {
97   guint32 srcx, dstx;
98   int src_index, j, first = 0;
99   u_long newseq;
100   tcp_frag *tmp_frag;
101   tcp_stream_chunk sc;
102   
103   src_index = -1;
104   
105   /* first check if this packet should be processed */
106   if (net_src->type != AT_IPv4 || net_dst->type != AT_IPv4)
107     return;
108   memcpy(&srcx, net_src->data, sizeof srcx);
109   memcpy(&dstx, net_dst->data, sizeof dstx);
110   if ((srcx != ip_address[0] && srcx != ip_address[1]) ||
111       (dstx != ip_address[0] && dstx != ip_address[1]) ||
112       (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
113       (dstport != tcp_port[0] && dstport != tcp_port[1]))
114     return;
115
116   /* Initialize our stream chunk.  This data gets written to disk. */
117   sc.src_addr = srcx;
118   sc.src_port = srcport;
119   sc.secs     = secs;
120   sc.usecs    = usecs;
121   sc.dlen     = data_length;
122
123   /* first we check to see if we have seen this src ip before. */
124   for( j=0; j<2; j++ ) {
125     if( src[j] == srcx ) {
126       src_index = j;
127     }
128   }
129   /* we didn't find it if src_index == -1 */
130   if( src_index < 0 ) {
131     /* assign it to a src_index and get going */
132     for( j=0; j<2; j++ ) {
133       if( src[j] == 0 ) {
134         src[j] = srcx;
135         src_index = j;
136         first = 1;
137         break;
138       }
139     }
140   }
141   if( src_index < 0 ) {
142     fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
143     return;
144   }
145
146   if( data_length < length ) {
147     incomplete_tcp_stream = TRUE;
148   }
149
150   /* now that we have filed away the srcs, lets get the sequence number stuff 
151      figured out */
152   if( first ) {
153     /* this is the first time we have seen this src's sequence number */
154     seq[src_index] = sequence + length;
155     if( synflag ) {
156       seq[src_index]++;
157     }
158     /* write out the packet data */
159     write_packet_data( &sc, data );
160     return;
161   }
162   /* if we are here, we have already seen this src, let's
163      try and figure out if this packet is in the right place */
164   if( sequence < seq[src_index] ) {
165     /* this sequence number seems dated, but 
166        check the end to make sure it has no more
167        info than we have already seen */
168     newseq = sequence + length;
169     if( newseq > seq[src_index] ) {
170       u_long new_len;
171
172       /* this one has more than we have seen. let's get the 
173          payload that we have not seen. */
174
175       new_len = seq[src_index] - sequence;
176
177       if ( data_length <= new_len ) {
178         data = NULL;
179         data_length = 0;
180         incomplete_tcp_stream = TRUE;
181       } else {
182         data += new_len;
183         data_length -= new_len;
184       }
185       sequence = seq[src_index];
186       length = newseq - seq[src_index];
187       
188       /* this will now appear to be right on time :) */
189     }
190   }
191   if ( sequence == seq[src_index] ) {
192     /* right on time */
193     seq[src_index] += length;
194     if( synflag ) seq[src_index]++;
195     if( data ) {
196       write_packet_data( &sc, data );
197     }
198     /* done with the packet, see if it caused a fragment to fit */
199     while( check_fragments( src_index, &sc ) )
200       ;
201   }
202   else {
203     /* out of order packet */
204     if( sequence > seq[src_index] ) {
205       tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
206       tmp_frag->data = (u_char *)malloc( data_length );
207       tmp_frag->seq = sequence;
208       tmp_frag->len = length;
209       tmp_frag->data_len = data_length;
210       memcpy( tmp_frag->data, data, data_length );
211       if( frags[src_index] ) {
212         tmp_frag->next = frags[src_index];
213       } else {
214         tmp_frag->next = NULL;
215       }
216       frags[src_index] = tmp_frag;
217     }
218   }
219 } /* end reassemble_tcp */
220
221 /* here we search through all the frag we have collected to see if
222    one fits */
223 static int 
224 check_fragments( int index, tcp_stream_chunk *sc ) {
225   tcp_frag *prev = NULL;
226   tcp_frag *current;
227   current = frags[index];
228   while( current ) {
229     if( current->seq == seq[index] ) {
230       /* this fragment fits the stream */
231       if( current->data ) {
232         sc->dlen = current->data_len;
233         write_packet_data( sc, current->data );
234       }
235       seq[index] += current->len;
236       if( prev ) {
237         prev->next = current->next;
238       } else {
239         frags[index] = current->next;
240       }
241       free( current->data );
242       free( current );
243       return 1;
244     }
245     prev = current;
246     current = current->next;
247   }
248   return 0;
249 }
250
251 /* this should always be called before we start to reassemble a stream */
252 void 
253 reset_tcp_reassembly() {
254   tcp_frag *current, *next;
255   int i;
256   incomplete_tcp_stream = FALSE;
257   for( i=0; i<2; i++ ) {
258     seq[i] = 0;
259     src[i] = 0;
260     ip_address[i] = 0;
261     tcp_port[i] = 0;
262     current = frags[i];
263     while( current ) {
264       next = current->next;
265       free( current->data ); 
266       free( current );
267       current = next;
268     }
269     frags[i] = NULL;
270   }
271 }
272
273 static void 
274 write_packet_data( tcp_stream_chunk *sc, const char *data ) {
275   fwrite( sc, 1, sizeof(tcp_stream_chunk), data_out_file );
276   fwrite( data, 1, sc->dlen, data_out_file );
277 }