3 * $Id: follow.c,v 1.14 1999/07/31 13:55:16 deniel 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 u_long ip_address[2];
54 static u_int tcp_port[2];
56 static int check_fragments( int );
57 static void write_packet_data( const u_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->ipproto == 6 ) {
69 "(ip.addr eq %s and ip.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
70 ip_to_str( (guint8 *) &pi->ip_src),
71 ip_to_str( (guint8 *) &pi->ip_dst),
72 pi->srcport, pi->destport );
78 ip_address[0] = pi->ip_src;
79 ip_address[1] = pi->ip_dst;
80 tcp_port[0] = pi->srcport;
81 tcp_port[1] = pi->destport;
85 /* here we are going to try and reconstruct the data portion of a TCP
86 session. We will try and handle duplicates, TCP fragments, and out
87 of order packets in a smart way. */
89 static tcp_frag *frags[2] = { 0, 0};
91 static u_long src[2] = { 0, 0 };
94 reassemble_tcp( u_long sequence, u_long length, const char* data, u_long data_length, int synflag, u_long srcx, u_long dstx, u_int srcport, u_int dstport ) {
95 int src_index, j, first = 0;
100 /* first check if this packet should be processed */
101 if ((srcx != ip_address[0] && srcx != ip_address[1]) ||
102 (dstx != ip_address[0] && dstx != ip_address[1]) ||
103 (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
104 (dstport != tcp_port[0] && dstport != tcp_port[1]))
107 /* first we check to see if we have seen this src ip before. */
108 for( j=0; j<2; j++ ) {
109 if( src[j] == srcx ) {
113 /* we didn't find it if src_index == -1 */
114 if( src_index < 0 ) {
115 /* assign it to a src_index and get going */
116 for( j=0; j<2; j++ ) {
125 if( src_index < 0 ) {
126 fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
130 if( data_length < length ) {
131 incomplete_tcp_stream = TRUE;
134 /* now that we have filed away the srcs, lets get the sequence number stuff
137 /* this is the first time we have seen this src's sequence number */
138 seq[src_index] = sequence + length;
142 /* write out the packet data */
143 write_packet_data( data, data_length );
146 /* if we are here, we have already seen this src, let's
147 try and figure out if this packet is in the right place */
148 if( sequence < seq[src_index] ) {
149 /* this sequence number seems dated, but
150 check the end to make sure it has no more
151 info than we have already seen */
152 newseq = sequence + length;
153 if( newseq > seq[src_index] ) {
156 /* this one has more than we have seen. let's get the
157 payload that we have not seen. */
159 new_len = seq[src_index] - sequence;
161 if ( data_length <= new_len ) {
164 incomplete_tcp_stream = TRUE;
167 data_length -= new_len;
169 sequence = seq[src_index];
170 length = newseq - seq[src_index];
172 /* this will now appear to be right on time :) */
175 if ( sequence == seq[src_index] ) {
177 seq[src_index] += length;
178 if( synflag ) seq[src_index]++;
180 write_packet_data( data, data_length );
182 /* done with the packet, see if it caused a fragment to fit */
183 while( check_fragments( src_index ) )
187 /* out of order packet */
188 if( sequence > seq[src_index] ) {
189 tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
190 tmp_frag->data = (u_char *)malloc( data_length );
191 tmp_frag->seq = sequence;
192 tmp_frag->len = length;
193 tmp_frag->data_len = data_length;
194 memcpy( tmp_frag->data, data, data_length );
195 if( frags[src_index] ) {
196 tmp_frag->next = frags[src_index];
198 tmp_frag->next = NULL;
200 frags[src_index] = tmp_frag;
203 } /* end reassemble_tcp */
205 /* here we search through all the frag we have collected to see if
208 check_fragments( int index ) {
209 tcp_frag *prev = NULL;
211 current = frags[index];
213 if( current->seq == seq[index] ) {
214 /* this fragment fits the stream */
215 if( current->data ) {
216 write_packet_data( current->data, current->data_len );
218 seq[index] += current->len;
220 prev->next = current->next;
222 frags[index] = current->next;
224 free( current->data );
229 current = current->next;
234 /* this should always be called before we start to reassemble a stream */
236 reset_tcp_reassembly() {
237 tcp_frag *current, *next;
239 incomplete_tcp_stream = FALSE;
240 for( i=0; i<2; i++ ) {
247 next = current->next;
248 free( current->data );
257 write_packet_data( const u_char* data, int length ) {
258 fwrite( data, 1, length, data_out_file );