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