3 * $Id: follow.c,v 1.17 1999/11/18 21:04:53 guy Exp $
5 * Copyright 1998 Mike Hall <mlh@io.com>
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@zing.org>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
49 extern FILE* data_out_file;
51 gboolean incomplete_tcp_stream = FALSE;
53 static guint32 ip_address[2];
54 static u_int tcp_port[2];
56 static int check_fragments( int );
57 static void write_packet_data( const char *, int );
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
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 ) {
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 );
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;
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. */
90 static tcp_frag *frags[2] = { 0, 0};
92 static guint32 src[2] = { 0, 0 };
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 ) {
99 int src_index, j, first = 0;
104 /* first check if this packet should be processed */
105 if (net_src->type != AT_IPv4 || net_dst->type != AT_IPv4)
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]))
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 ) {
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++ ) {
133 if( src_index < 0 ) {
134 fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
138 if( data_length < length ) {
139 incomplete_tcp_stream = TRUE;
142 /* now that we have filed away the srcs, lets get the sequence number stuff
145 /* this is the first time we have seen this src's sequence number */
146 seq[src_index] = sequence + length;
150 /* write out the packet data */
151 write_packet_data( data, data_length );
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] ) {
164 /* this one has more than we have seen. let's get the
165 payload that we have not seen. */
167 new_len = seq[src_index] - sequence;
169 if ( data_length <= new_len ) {
172 incomplete_tcp_stream = TRUE;
175 data_length -= new_len;
177 sequence = seq[src_index];
178 length = newseq - seq[src_index];
180 /* this will now appear to be right on time :) */
183 if ( sequence == seq[src_index] ) {
185 seq[src_index] += length;
186 if( synflag ) seq[src_index]++;
188 write_packet_data( data, data_length );
190 /* done with the packet, see if it caused a fragment to fit */
191 while( check_fragments( src_index ) )
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];
206 tmp_frag->next = NULL;
208 frags[src_index] = tmp_frag;
211 } /* end reassemble_tcp */
213 /* here we search through all the frag we have collected to see if
216 check_fragments( int index ) {
217 tcp_frag *prev = NULL;
219 current = frags[index];
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 );
226 seq[index] += current->len;
228 prev->next = current->next;
230 frags[index] = current->next;
232 free( current->data );
237 current = current->next;
242 /* this should always be called before we start to reassemble a stream */
244 reset_tcp_reassembly() {
245 tcp_frag *current, *next;
247 incomplete_tcp_stream = FALSE;
248 for( i=0; i<2; i++ ) {
255 next = current->next;
256 free( current->data );
265 write_packet_data( const char* data, int length ) {
266 fwrite( data, 1, length, data_out_file );