3 * $Id: follow.c,v 1.26 2000/08/24 21:05: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.
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
49 gboolean incomplete_tcp_stream = FALSE;
51 static guint8 ip_address[2][MAX_IPADDR_LEN];
52 static u_int tcp_port[2];
53 static u_int bytes_written[2];
54 static gboolean is_ipv6 = FALSE;
56 static int check_fragments( int, tcp_stream_chunk * );
57 static void write_packet_data( int, tcp_stream_chunk *, const char * );
60 follow_tcp_stats(follow_tcp_stats_t* stats)
64 for (i = 0; i < 2 ; i++) {
65 memcpy(stats->ip_address[i], ip_address[i], MAX_IPADDR_LEN);
66 stats->tcp_port[i] = tcp_port[i];
67 stats->bytes_written[i] = bytes_written[i];
68 stats->is_ipv6 = is_ipv6;
72 /* this will build libpcap filter text that will only
73 pass the packets related to the stream. There is a
74 chance that two streams could intersect, but not a
77 build_follow_filter( packet_info *pi ) {
78 char* buf = g_malloc(1024);
80 if( pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4
81 && pi->ipproto == 6 ) {
84 "(ip.addr eq %s and ip.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
85 ip_to_str( pi->net_src.data),
86 ip_to_str( pi->net_dst.data),
87 pi->srcport, pi->destport );
91 else if( pi->net_src.type == AT_IPv6 && pi->net_dst.type == AT_IPv6
92 && pi->ipproto == 6 ) {
95 "(ipv6.addr eq %s and ipv6.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
96 ip6_to_str((struct e_in6_addr *)pi->net_src.data),
97 ip6_to_str((struct e_in6_addr *)pi->net_dst.data),
98 pi->srcport, pi->destport );
106 memcpy(ip_address[0], pi->net_src.data, len);
107 memcpy(ip_address[1], pi->net_dst.data, len);
108 tcp_port[0] = pi->srcport;
109 tcp_port[1] = pi->destport;
113 /* here we are going to try and reconstruct the data portion of a TCP
114 session. We will try and handle duplicates, TCP fragments, and out
115 of order packets in a smart way. */
117 static tcp_frag *frags[2] = { 0, 0 };
118 static u_long seq[2];
119 static guint8 src_addr[2][MAX_IPADDR_LEN];
120 static u_int src_port[2] = { 0, 0 };
123 reassemble_tcp( u_long sequence, u_long length, const char* data,
124 u_long data_length, int synflag, address *net_src,
125 address *net_dst, u_int srcport, u_int dstport) {
126 guint8 srcx[MAX_IPADDR_LEN], dstx[MAX_IPADDR_LEN];
127 int src_index, j, first = 0, len;
134 /* First, check if this packet should be processed. */
136 if ((net_src->type != AT_IPv4 && net_src->type != AT_IPv6) ||
137 (net_dst->type != AT_IPv4 && net_dst->type != AT_IPv6))
140 if (net_src->type == AT_IPv4)
145 /* Now check if the packet is for this connection. */
146 memcpy(srcx, net_src->data, len);
147 memcpy(dstx, net_dst->data, len);
148 if ((memcmp(srcx, ip_address[0], len) != 0 &&
149 memcmp(srcx, ip_address[1], len) != 0) ||
150 (memcmp(dstx, ip_address[0], len) != 0 &&
151 memcmp(dstx, ip_address[1], len) != 0) ||
152 (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
153 (dstport != tcp_port[0] && dstport != tcp_port[1]))
156 /* Initialize our stream chunk. This data gets written to disk. */
157 memcpy(sc.src_addr, srcx, len);
158 sc.src_port = srcport;
159 sc.dlen = data_length;
161 /* Check to see if we have seen this source IP and port before.
162 (Yes, we have to check both source IP and port; the connection
163 might be between two different ports on the same machine.) */
164 for( j=0; j<2; j++ ) {
165 if (memcmp(src_addr[j], srcx, len) == 0 && src_port[j] == srcport ) {
169 /* we didn't find it if src_index == -1 */
170 if( src_index < 0 ) {
171 /* assign it to a src_index and get going */
172 for( j=0; j<2; j++ ) {
173 if( src_port[j] == 0 ) {
174 memcpy(src_addr[j], srcx, len);
175 src_port[j] = srcport;
182 if( src_index < 0 ) {
183 fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
187 if( data_length < length ) {
188 incomplete_tcp_stream = TRUE;
191 /* now that we have filed away the srcs, lets get the sequence number stuff
194 /* this is the first time we have seen this src's sequence number */
195 seq[src_index] = sequence + length;
199 /* write out the packet data */
200 write_packet_data( src_index, &sc, data );
203 /* if we are here, we have already seen this src, let's
204 try and figure out if this packet is in the right place */
205 if( sequence < seq[src_index] ) {
206 /* this sequence number seems dated, but
207 check the end to make sure it has no more
208 info than we have already seen */
209 newseq = sequence + length;
210 if( newseq > seq[src_index] ) {
213 /* this one has more than we have seen. let's get the
214 payload that we have not seen. */
216 new_len = seq[src_index] - sequence;
218 if ( data_length <= new_len ) {
221 incomplete_tcp_stream = TRUE;
224 data_length -= new_len;
226 sequence = seq[src_index];
227 length = newseq - seq[src_index];
229 /* this will now appear to be right on time :) */
232 if ( sequence == seq[src_index] ) {
234 seq[src_index] += length;
235 if( synflag ) seq[src_index]++;
237 write_packet_data( src_index, &sc, data );
239 /* done with the packet, see if it caused a fragment to fit */
240 while( check_fragments( src_index, &sc ) )
244 /* out of order packet */
245 if(data_length > 0 && sequence > seq[src_index] ) {
246 tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
247 tmp_frag->data = (u_char *)malloc( data_length );
248 tmp_frag->seq = sequence;
249 tmp_frag->len = length;
250 tmp_frag->data_len = data_length;
251 memcpy( tmp_frag->data, data, data_length );
252 if( frags[src_index] ) {
253 tmp_frag->next = frags[src_index];
255 tmp_frag->next = NULL;
257 frags[src_index] = tmp_frag;
260 } /* end reassemble_tcp */
262 /* here we search through all the frag we have collected to see if
265 check_fragments( int index, tcp_stream_chunk *sc ) {
266 tcp_frag *prev = NULL;
268 current = frags[index];
270 if( current->seq == seq[index] ) {
271 /* this fragment fits the stream */
272 if( current->data ) {
273 sc->dlen = current->data_len;
274 write_packet_data( index, sc, current->data );
276 seq[index] += current->len;
278 prev->next = current->next;
280 frags[index] = current->next;
282 free( current->data );
287 current = current->next;
292 /* this should always be called before we start to reassemble a stream */
294 reset_tcp_reassembly() {
295 tcp_frag *current, *next;
297 incomplete_tcp_stream = FALSE;
298 for( i=0; i<2; i++ ) {
300 memset(src_addr[i], '\0', MAX_IPADDR_LEN);
302 memset(ip_address[i], '\0', MAX_IPADDR_LEN);
304 bytes_written[i] = 0;
307 next = current->next;
308 free( current->data );
317 write_packet_data( int index, tcp_stream_chunk *sc, const char *data )
319 fwrite( sc, 1, sizeof(tcp_stream_chunk), data_out_file );
320 fwrite( data, 1, sc->dlen, data_out_file );
321 bytes_written[index] += sc->dlen;