3 * $Id: follow.c,v 1.20 1999/12/10 04:25:59 gram 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.
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
47 extern FILE* data_out_file;
49 gboolean incomplete_tcp_stream = FALSE;
51 static guint32 ip_address[2];
52 static u_int tcp_port[2];
54 static int check_fragments( int, tcp_stream_chunk * );
55 static void write_packet_data( tcp_stream_chunk *, const char * );
57 /* this will build libpcap filter text that will only
58 pass the packets related to the stream. There is a
59 chance that two streams could intersect, but not a
62 build_follow_filter( packet_info *pi ) {
63 char* buf = malloc(1024);
64 if( pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4
65 && pi->ipproto == 6 ) {
68 "(ip.addr eq %s and ip.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
69 ip_to_str( pi->net_src.data),
70 ip_to_str( pi->net_dst.data),
71 pi->srcport, pi->destport );
77 memcpy(&ip_address[0], pi->net_src.data, sizeof ip_address[0]);
78 memcpy(&ip_address[1], pi->net_dst.data, sizeof ip_address[1]);
79 tcp_port[0] = pi->srcport;
80 tcp_port[1] = pi->destport;
84 /* here we are going to try and reconstruct the data portion of a TCP
85 session. We will try and handle duplicates, TCP fragments, and out
86 of order packets in a smart way. */
88 static tcp_frag *frags[2] = { 0, 0};
90 static guint32 src[2] = { 0, 0 };
93 reassemble_tcp( u_long sequence, u_long length, const char* data,
94 u_long data_length, int synflag, address *net_src,
95 address *net_dst, u_int srcport, u_int dstport,
96 guint32 secs, guint32 usecs) {
98 int src_index, j, first = 0;
105 /* first check if this packet should be processed */
106 if (net_src->type != AT_IPv4 || net_dst->type != AT_IPv4)
108 memcpy(&srcx, net_src->data, sizeof srcx);
109 memcpy(&dstx, net_dst->data, sizeof dstx);
110 if ((srcx != ip_address[0] && srcx != ip_address[1]) ||
111 (dstx != ip_address[0] && dstx != ip_address[1]) ||
112 (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
113 (dstport != tcp_port[0] && dstport != tcp_port[1]))
116 /* Initialize our stream chunk. This data gets written to disk. */
118 sc.src_port = srcport;
121 sc.dlen = data_length;
123 /* first we check to see if we have seen this src ip before. */
124 for( j=0; j<2; j++ ) {
125 if( src[j] == srcx ) {
129 /* we didn't find it if src_index == -1 */
130 if( src_index < 0 ) {
131 /* assign it to a src_index and get going */
132 for( j=0; j<2; j++ ) {
141 if( src_index < 0 ) {
142 fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
146 if( data_length < length ) {
147 incomplete_tcp_stream = TRUE;
150 /* now that we have filed away the srcs, lets get the sequence number stuff
153 /* this is the first time we have seen this src's sequence number */
154 seq[src_index] = sequence + length;
158 /* write out the packet data */
159 write_packet_data( &sc, data );
162 /* if we are here, we have already seen this src, let's
163 try and figure out if this packet is in the right place */
164 if( sequence < seq[src_index] ) {
165 /* this sequence number seems dated, but
166 check the end to make sure it has no more
167 info than we have already seen */
168 newseq = sequence + length;
169 if( newseq > seq[src_index] ) {
172 /* this one has more than we have seen. let's get the
173 payload that we have not seen. */
175 new_len = seq[src_index] - sequence;
177 if ( data_length <= new_len ) {
180 incomplete_tcp_stream = TRUE;
183 data_length -= new_len;
185 sequence = seq[src_index];
186 length = newseq - seq[src_index];
188 /* this will now appear to be right on time :) */
191 if ( sequence == seq[src_index] ) {
193 seq[src_index] += length;
194 if( synflag ) seq[src_index]++;
196 write_packet_data( &sc, data );
198 /* done with the packet, see if it caused a fragment to fit */
199 while( check_fragments( src_index, &sc ) )
203 /* out of order packet */
204 if( sequence > seq[src_index] ) {
205 tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
206 tmp_frag->data = (u_char *)malloc( data_length );
207 tmp_frag->seq = sequence;
208 tmp_frag->len = length;
209 tmp_frag->data_len = data_length;
210 memcpy( tmp_frag->data, data, data_length );
211 if( frags[src_index] ) {
212 tmp_frag->next = frags[src_index];
214 tmp_frag->next = NULL;
216 frags[src_index] = tmp_frag;
219 } /* end reassemble_tcp */
221 /* here we search through all the frag we have collected to see if
224 check_fragments( int index, tcp_stream_chunk *sc ) {
225 tcp_frag *prev = NULL;
227 current = frags[index];
229 if( current->seq == seq[index] ) {
230 /* this fragment fits the stream */
231 if( current->data ) {
232 sc->dlen = current->data_len;
233 write_packet_data( sc, current->data );
235 seq[index] += current->len;
237 prev->next = current->next;
239 frags[index] = current->next;
241 free( current->data );
246 current = current->next;
251 /* this should always be called before we start to reassemble a stream */
253 reset_tcp_reassembly() {
254 tcp_frag *current, *next;
256 incomplete_tcp_stream = FALSE;
257 for( i=0; i<2; i++ ) {
264 next = current->next;
265 free( current->data );
274 write_packet_data( tcp_stream_chunk *sc, const char *data ) {
275 fwrite( sc, 1, sizeof(tcp_stream_chunk), data_out_file );
276 fwrite( data, 1, sc->dlen, data_out_file );