Remove the check for cs->dlen > 0. We want to log the first (zero length)
[obnox/wireshark/wip.git] / follow.c
1 /* follow.c
2  *
3  * $Id: follow.c,v 1.19 1999/12/02 04:27:46 gerald 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, tcp_stream_chunk * );
57 static void write_packet_data( tcp_stream_chunk *, const char * );
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 secs, guint32 usecs) {
99   guint32 srcx, dstx;
100   int src_index, j, first = 0;
101   u_long newseq;
102   tcp_frag *tmp_frag;
103   tcp_stream_chunk sc;
104   
105   src_index = -1;
106   
107   /* first check if this packet should be processed */
108   if (net_src->type != AT_IPv4 || net_dst->type != AT_IPv4)
109     return;
110   memcpy(&srcx, net_src->data, sizeof srcx);
111   memcpy(&dstx, net_dst->data, sizeof dstx);
112   if ((srcx != ip_address[0] && srcx != ip_address[1]) ||
113       (dstx != ip_address[0] && dstx != ip_address[1]) ||
114       (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
115       (dstport != tcp_port[0] && dstport != tcp_port[1]))
116     return;
117
118   /* Initialize our stream chunk.  This data gets written to disk. */
119   sc.src_addr = srcx;
120   sc.src_port = srcport;
121   sc.secs     = secs;
122   sc.usecs    = usecs;
123   sc.dlen     = data_length;
124
125   /* first we check to see if we have seen this src ip before. */
126   for( j=0; j<2; j++ ) {
127     if( src[j] == srcx ) {
128       src_index = j;
129     }
130   }
131   /* we didn't find it if src_index == -1 */
132   if( src_index < 0 ) {
133     /* assign it to a src_index and get going */
134     for( j=0; j<2; j++ ) {
135       if( src[j] == 0 ) {
136         src[j] = srcx;
137         src_index = j;
138         first = 1;
139         break;
140       }
141     }
142   }
143   if( src_index < 0 ) {
144     fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
145     return;
146   }
147
148   if( data_length < length ) {
149     incomplete_tcp_stream = TRUE;
150   }
151
152   /* now that we have filed away the srcs, lets get the sequence number stuff 
153      figured out */
154   if( first ) {
155     /* this is the first time we have seen this src's sequence number */
156     seq[src_index] = sequence + length;
157     if( synflag ) {
158       seq[src_index]++;
159     }
160     /* write out the packet data */
161     write_packet_data( &sc, data );
162     return;
163   }
164   /* if we are here, we have already seen this src, let's
165      try and figure out if this packet is in the right place */
166   if( sequence < seq[src_index] ) {
167     /* this sequence number seems dated, but 
168        check the end to make sure it has no more
169        info than we have already seen */
170     newseq = sequence + length;
171     if( newseq > seq[src_index] ) {
172       u_long new_len;
173
174       /* this one has more than we have seen. let's get the 
175          payload that we have not seen. */
176
177       new_len = seq[src_index] - sequence;
178
179       if ( data_length <= new_len ) {
180         data = NULL;
181         data_length = 0;
182         incomplete_tcp_stream = TRUE;
183       } else {
184         data += new_len;
185         data_length -= new_len;
186       }
187       sequence = seq[src_index];
188       length = newseq - seq[src_index];
189       
190       /* this will now appear to be right on time :) */
191     }
192   }
193   if ( sequence == seq[src_index] ) {
194     /* right on time */
195     seq[src_index] += length;
196     if( synflag ) seq[src_index]++;
197     if( data ) {
198       write_packet_data( &sc, data );
199     }
200     /* done with the packet, see if it caused a fragment to fit */
201     while( check_fragments( src_index, &sc ) )
202       ;
203   }
204   else {
205     /* out of order packet */
206     if( sequence > seq[src_index] ) {
207       tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
208       tmp_frag->data = (u_char *)malloc( data_length );
209       tmp_frag->seq = sequence;
210       tmp_frag->len = length;
211       tmp_frag->data_len = data_length;
212       memcpy( tmp_frag->data, data, data_length );
213       if( frags[src_index] ) {
214         tmp_frag->next = frags[src_index];
215       } else {
216         tmp_frag->next = NULL;
217       }
218       frags[src_index] = tmp_frag;
219     }
220   }
221 } /* end reassemble_tcp */
222
223 /* here we search through all the frag we have collected to see if
224    one fits */
225 static int 
226 check_fragments( int index, tcp_stream_chunk *sc ) {
227   tcp_frag *prev = NULL;
228   tcp_frag *current;
229   current = frags[index];
230   while( current ) {
231     if( current->seq == seq[index] ) {
232       /* this fragment fits the stream */
233       if( current->data ) {
234         sc->dlen = current->data_len;
235         write_packet_data( sc, current->data );
236       }
237       seq[index] += current->len;
238       if( prev ) {
239         prev->next = current->next;
240       } else {
241         frags[index] = current->next;
242       }
243       free( current->data );
244       free( current );
245       return 1;
246     }
247     prev = current;
248     current = current->next;
249   }
250   return 0;
251 }
252
253 /* this should always be called before we start to reassemble a stream */
254 void 
255 reset_tcp_reassembly() {
256   tcp_frag *current, *next;
257   int i;
258   incomplete_tcp_stream = FALSE;
259   for( i=0; i<2; i++ ) {
260     seq[i] = 0;
261     src[i] = 0;
262     ip_address[i] = 0;
263     tcp_port[i] = 0;
264     current = frags[i];
265     while( current ) {
266       next = current->next;
267       free( current->data ); 
268       free( current );
269       current = next;
270     }
271     frags[i] = NULL;
272   }
273 }
274
275 static void 
276 write_packet_data( tcp_stream_chunk *sc, const char *data ) {
277   fwrite( sc, 1, sizeof(tcp_stream_chunk), data_out_file );
278   fwrite( data, 1, sc->dlen, data_out_file );
279 }