Clean up some ANSI C nits pointed out by "gcc -pedantic".
[obnox/wireshark/wip.git] / follow.c
1 /* follow.c
2  *
3  * $Id: follow.c,v 1.17 1999/11/18 21:04:53 guy 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 "gtk/main.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 guint32 ip_address[2];
54 static u_int   tcp_port[2];
55
56 static int check_fragments( int );
57 static void write_packet_data( const 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->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4
67         && pi->ipproto == 6 ) {
68     /* TCP over IPv4 */
69     sprintf( buf, 
70              "(ip.addr eq %s and ip.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
71              ip_to_str( pi->net_src.data), 
72              ip_to_str( pi->net_dst.data), 
73              pi->srcport, pi->destport );
74   }
75   else { 
76     free( buf );
77     return NULL;
78   }
79   memcpy(&ip_address[0], pi->net_src.data, sizeof ip_address[0]);
80   memcpy(&ip_address[1], pi->net_dst.data, sizeof ip_address[1]);
81   tcp_port[0] = pi->srcport;
82   tcp_port[1] = pi->destport;
83   return buf;
84 }
85
86 /* here we are going to try and reconstruct the data portion of a TCP
87    session. We will try and handle duplicates, TCP fragments, and out 
88    of order packets in a smart way. */
89
90 static tcp_frag *frags[2] = { 0, 0};
91 static u_long seq[2];
92 static guint32 src[2] = { 0, 0 };
93
94 void 
95 reassemble_tcp( u_long sequence, u_long length, const char* data,
96                 u_long data_length, int synflag, address *net_src,
97                 address *net_dst, u_int srcport, u_int dstport ) {
98   guint32 srcx, dstx;
99   int src_index, j, first = 0;
100   u_long newseq;
101   tcp_frag *tmp_frag;
102   src_index = -1;
103
104   /* first check if this packet should be processed */
105   if (net_src->type != AT_IPv4 || net_dst->type != AT_IPv4)
106     return;
107   memcpy(&srcx, net_src->data, sizeof srcx);
108   memcpy(&dstx, net_dst->data, sizeof dstx);
109   if ((srcx != ip_address[0] && srcx != ip_address[1]) ||
110       (dstx != ip_address[0] && dstx != ip_address[1]) ||
111       (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
112       (dstport != tcp_port[0] && dstport != tcp_port[1]))
113     return;
114
115   /* first we check to see if we have seen this src ip before. */
116   for( j=0; j<2; j++ ) {
117     if( src[j] == srcx ) {
118       src_index = j;
119     }
120   }
121   /* we didn't find it if src_index == -1 */
122   if( src_index < 0 ) {
123     /* assign it to a src_index and get going */
124     for( j=0; j<2; j++ ) {
125       if( src[j] == 0 ) {
126         src[j] = srcx;
127         src_index = j;
128         first = 1;
129         break;
130       }
131     }
132   }
133   if( src_index < 0 ) {
134     fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
135     return;
136   }
137
138   if( data_length < length ) {
139     incomplete_tcp_stream = TRUE;
140   }
141
142   /* now that we have filed away the srcs, lets get the sequence number stuff 
143      figured out */
144   if( first ) {
145     /* this is the first time we have seen this src's sequence number */
146     seq[src_index] = sequence + length;
147     if( synflag ) {
148       seq[src_index]++;
149     }
150     /* write out the packet data */
151     write_packet_data( data, data_length );
152     return;
153   }
154   /* if we are here, we have already seen this src, let's
155      try and figure out if this packet is in the right place */
156   if( sequence < seq[src_index] ) {
157     /* this sequence number seems dated, but 
158        check the end to make sure it has no more
159        info than we have already seen */
160     newseq = sequence + length;
161     if( newseq > seq[src_index] ) {
162       u_long new_len;
163
164       /* this one has more than we have seen. let's get the 
165          payload that we have not seen. */
166
167       new_len = seq[src_index] - sequence;
168
169       if ( data_length <= new_len ) {
170         data = NULL;
171         data_length = 0;
172         incomplete_tcp_stream = TRUE;
173       } else {
174         data += new_len;
175         data_length -= new_len;
176       }
177       sequence = seq[src_index];
178       length = newseq - seq[src_index];
179       
180       /* this will now appear to be right on time :) */
181     }
182   }
183   if ( sequence == seq[src_index] ) {
184     /* right on time */
185     seq[src_index] += length;
186     if( synflag ) seq[src_index]++;
187     if( data ) {
188       write_packet_data( data, data_length );
189     }
190     /* done with the packet, see if it caused a fragment to fit */
191     while( check_fragments( src_index ) )
192       ;
193   }
194   else {
195     /* out of order packet */
196     if( sequence > seq[src_index] ) {
197       tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
198       tmp_frag->data = (u_char *)malloc( data_length );
199       tmp_frag->seq = sequence;
200       tmp_frag->len = length;
201       tmp_frag->data_len = data_length;
202       memcpy( tmp_frag->data, data, data_length );
203       if( frags[src_index] ) {
204         tmp_frag->next = frags[src_index];
205       } else {
206         tmp_frag->next = NULL;
207       }
208       frags[src_index] = tmp_frag;
209     }
210   }
211 } /* end reassemble_tcp */
212
213 /* here we search through all the frag we have collected to see if
214    one fits */
215 static int 
216 check_fragments( int index ) {
217   tcp_frag *prev = NULL;
218   tcp_frag *current;
219   current = frags[index];
220   while( current ) {
221     if( current->seq == seq[index] ) {
222       /* this fragment fits the stream */
223       if( current->data ) {
224         write_packet_data( current->data, current->data_len );
225       }
226       seq[index] += current->len;
227       if( prev ) {
228         prev->next = current->next;
229       } else {
230         frags[index] = current->next;
231       }
232       free( current->data );
233       free( current );
234       return 1;
235     }
236     prev = current;
237     current = current->next;
238   }
239   return 0;
240 }
241
242 /* this should always be called before we start to reassemble a stream */
243 void 
244 reset_tcp_reassembly() {
245   tcp_frag *current, *next;
246   int i;
247   incomplete_tcp_stream = FALSE;
248   for( i=0; i<2; i++ ) {
249     seq[i] = 0;
250     src[i] = 0;
251     ip_address[i] = 0;
252     tcp_port[i] = 0;
253     current = frags[i];
254     while( current ) {
255       next = current->next;
256       free( current->data ); 
257       free( current );
258       current = next;
259     }
260     frags[i] = NULL;
261   }
262 }
263
264 static void 
265 write_packet_data( const char* data, int length ) {
266   fwrite( data, 1, length, data_out_file );
267 }
268