Merged in a _huge_ patch from Guy Harris. It adds a time stap column,
[obnox/wireshark/wip.git] / follow.c
1 /* follow.c
2  *
3  * $Id: follow.c,v 1.2 1998/09/27 22:12:25 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 #include <gtk/gtk.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "ethereal.h"
33 #include "packet.h"
34 #include "follow.h"
35
36 extern FILE* data_out_file;
37
38 /* this will build libpcap filter text that will only 
39    pass the packets related to the stream. There is a 
40    chance that two streams could intersect, but not a 
41    very good one */
42 char* 
43 build_follow_filter( packet_info *pi ) {
44   char* buf = malloc(1024);
45   if( pi->ipproto == 6 ) {
46     /* TCP */
47     sprintf( buf, "host %s and host %s and (ip proto \\tcp) and (port %d and port %d)",
48              pi->srcip, pi->destip, pi->srcport, pi->destport );
49   }
50   else { 
51     free( buf );
52     return NULL;
53   }
54   return buf;
55 }
56
57 /* here we are going to try and reconstruct the data portion of a TCP
58    session. We will try and handle duplicates, TCP fragments, and out 
59    of order packets in a smart way. */
60
61 static tcp_frag *frags[2] = { 0, 0};
62 static u_long seq[2];
63 static u_long src[2] = { 0, 0 };
64
65 void 
66 reassemble_tcp( u_long sequence, u_long length, const char* data, int synflag, u_long srcx ) {
67   int src_index, j, first = 0;
68   u_long newseq;
69   tcp_frag *tmp_frag;
70   src_index = -1;
71   /* first we check to see if we have seen this src ip before. */
72   for( j=0; j<2; j++ ) {
73     if( src[j] == srcx ) {
74       src_index = j;
75     }
76   }
77   /* we didn't find it if src_index == -1 */
78   if( src_index < 0 ) {
79     /* assign it to a src_index and get going */
80     for( j=0; j<2; j++ ) {
81       if( src[j] == 0 ) {
82         src[j] = srcx;
83         src_index = j;
84         first = 1;
85         break;
86       }
87     }
88   }
89   if( src_index < 0 ) {
90     fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
91     return;
92   }
93   /* now that we have filed away the srcs, lets get the sequence number stuff 
94      figured out */
95   if( first ) {
96     /* this is the first time we have seen this src's sequence number */
97     seq[src_index] = sequence + length;
98     if( synflag ) {
99       seq[src_index]++;
100     }
101     /* write out the packet data */
102     write_packet_data( data, length );
103     return;
104   }
105   /* if we are here, we have already seen this src, let's
106      try and figure out if this packet is in the right place */
107   if( sequence < seq[src_index] ) {
108     /* this sequence number seems dated, but 
109        check the end to make sure it has no more
110        info than we have already seen */
111     newseq = sequence + length;
112     if( newseq > seq[src_index] ) {
113       /* this one has more than we have seen. let's get the 
114          payload that we have not seen. */
115       data += ( seq[src_index] - sequence );
116       sequence = seq[src_index];
117       length = newseq - seq[src_index];
118       /* this will now appear to be right on time :) */
119     }
120   }
121   if ( sequence == seq[src_index] ) {
122     /* right on time */
123     seq[src_index] += length;
124     if( synflag ) seq[src_index]++;
125     write_packet_data( data, length );
126     /* done with the packet, see if it caused a fragment to fit */
127     while( check_fragments( src_index ) )
128       ;
129   }
130   else {
131     /* out of order packet */
132     if( sequence > seq[src_index] ) {
133       tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
134       tmp_frag->data = (u_char *)malloc( length );
135       tmp_frag->seq = sequence;
136       tmp_frag->len = length;
137       bcopy( data, tmp_frag->data, length );
138       if( frags[src_index] ) {
139         tmp_frag->next = frags[src_index];
140       } else {
141         tmp_frag->next = NULL;
142       }
143       frags[src_index] = tmp_frag;
144     }
145   }
146 } /* end reassemble_tcp */
147
148 /* here we search through all the frag we have collected to see if
149    one fits */
150 int 
151 check_fragments( int index ) {
152   tcp_frag *prev = NULL;
153   tcp_frag *current;
154   current = frags[index];
155   while( current ) {
156     if( current->seq == seq[index] ) {
157       /* this fragment fits the stream */
158       write_packet_data( current->data, current->len );
159       seq[index] += current->len;
160       if( prev ) {
161         prev->next = current->next;
162       } else {
163         src[index] = current->next;
164       }
165       free( current->data );
166       free( current );
167       return 1;
168     }
169     prev = current;
170     current = current->next;
171   }
172   return 0;
173 }
174
175 /* this should always be called before we start to reassemble a stream */
176 void 
177 reset_tcp_reassembly() {
178   tcp_frag *current, *next;
179   int i;
180   for( i=0; i<2; i++ ) {
181     seq[i] = 0;
182     src[i] = 0;
183     current = frags[i];
184     while( current ) {
185       next = current->next;
186       free( current->data ); 
187       free( current );
188       current = next;
189     }
190     frags[i] = NULL;
191   }
192 }
193
194 void 
195 write_packet_data( const u_char* data, int length ) {
196   fwrite( data, 1, length, data_out_file );
197 }
198