3 * $Id: follow.c,v 1.19 1999/12/02 04:27:46 gerald 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, tcp_stream_chunk * );
57 static void write_packet_data( tcp_stream_chunk *, const char * );
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,
98 guint32 secs, guint32 usecs) {
100 int src_index, j, first = 0;
107 /* first check if this packet should be processed */
108 if (net_src->type != AT_IPv4 || net_dst->type != AT_IPv4)
110 memcpy(&srcx, net_src->data, sizeof srcx);
111 memcpy(&dstx, net_dst->data, sizeof dstx);
112 if ((srcx != ip_address[0] && srcx != ip_address[1]) ||
113 (dstx != ip_address[0] && dstx != ip_address[1]) ||
114 (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
115 (dstport != tcp_port[0] && dstport != tcp_port[1]))
118 /* Initialize our stream chunk. This data gets written to disk. */
120 sc.src_port = srcport;
123 sc.dlen = data_length;
125 /* first we check to see if we have seen this src ip before. */
126 for( j=0; j<2; j++ ) {
127 if( src[j] == srcx ) {
131 /* we didn't find it if src_index == -1 */
132 if( src_index < 0 ) {
133 /* assign it to a src_index and get going */
134 for( j=0; j<2; j++ ) {
143 if( src_index < 0 ) {
144 fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
148 if( data_length < length ) {
149 incomplete_tcp_stream = TRUE;
152 /* now that we have filed away the srcs, lets get the sequence number stuff
155 /* this is the first time we have seen this src's sequence number */
156 seq[src_index] = sequence + length;
160 /* write out the packet data */
161 write_packet_data( &sc, data );
164 /* if we are here, we have already seen this src, let's
165 try and figure out if this packet is in the right place */
166 if( sequence < seq[src_index] ) {
167 /* this sequence number seems dated, but
168 check the end to make sure it has no more
169 info than we have already seen */
170 newseq = sequence + length;
171 if( newseq > seq[src_index] ) {
174 /* this one has more than we have seen. let's get the
175 payload that we have not seen. */
177 new_len = seq[src_index] - sequence;
179 if ( data_length <= new_len ) {
182 incomplete_tcp_stream = TRUE;
185 data_length -= new_len;
187 sequence = seq[src_index];
188 length = newseq - seq[src_index];
190 /* this will now appear to be right on time :) */
193 if ( sequence == seq[src_index] ) {
195 seq[src_index] += length;
196 if( synflag ) seq[src_index]++;
198 write_packet_data( &sc, data );
200 /* done with the packet, see if it caused a fragment to fit */
201 while( check_fragments( src_index, &sc ) )
205 /* out of order packet */
206 if( sequence > seq[src_index] ) {
207 tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
208 tmp_frag->data = (u_char *)malloc( data_length );
209 tmp_frag->seq = sequence;
210 tmp_frag->len = length;
211 tmp_frag->data_len = data_length;
212 memcpy( tmp_frag->data, data, data_length );
213 if( frags[src_index] ) {
214 tmp_frag->next = frags[src_index];
216 tmp_frag->next = NULL;
218 frags[src_index] = tmp_frag;
221 } /* end reassemble_tcp */
223 /* here we search through all the frag we have collected to see if
226 check_fragments( int index, tcp_stream_chunk *sc ) {
227 tcp_frag *prev = NULL;
229 current = frags[index];
231 if( current->seq == seq[index] ) {
232 /* this fragment fits the stream */
233 if( current->data ) {
234 sc->dlen = current->data_len;
235 write_packet_data( sc, current->data );
237 seq[index] += current->len;
239 prev->next = current->next;
241 frags[index] = current->next;
243 free( current->data );
248 current = current->next;
253 /* this should always be called before we start to reassemble a stream */
255 reset_tcp_reassembly() {
256 tcp_frag *current, *next;
258 incomplete_tcp_stream = FALSE;
259 for( i=0; i<2; i++ ) {
266 next = current->next;
267 free( current->data );
276 write_packet_data( tcp_stream_chunk *sc, const char *data ) {
277 fwrite( sc, 1, sizeof(tcp_stream_chunk), data_out_file );
278 fwrite( data, 1, sc->dlen, data_out_file );