3 * $Id: follow.c,v 1.29 2002/02/28 19:35:08 gram Exp $
5 * Copyright 1998 Mike Hall <mlh@io.com>
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
43 #include <epan/packet.h>
46 FILE* data_out_file = NULL;
48 gboolean incomplete_tcp_stream = FALSE;
50 static guint8 ip_address[2][MAX_IPADDR_LEN];
51 static u_int tcp_port[2];
52 static u_int bytes_written[2];
53 static gboolean is_ipv6 = FALSE;
55 static int check_fragments( int, tcp_stream_chunk * );
56 static void write_packet_data( int, tcp_stream_chunk *, const char * );
59 follow_tcp_stats(follow_tcp_stats_t* stats)
63 for (i = 0; i < 2 ; i++) {
64 memcpy(stats->ip_address[i], ip_address[i], MAX_IPADDR_LEN);
65 stats->tcp_port[i] = tcp_port[i];
66 stats->bytes_written[i] = bytes_written[i];
67 stats->is_ipv6 = is_ipv6;
71 /* this will build libpcap filter text that will only
72 pass the packets related to the stream. There is a
73 chance that two streams could intersect, but not a
76 build_follow_filter( packet_info *pi ) {
77 char* buf = g_malloc(1024);
79 if( pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4
80 && pi->ipproto == 6 ) {
83 "(ip.addr eq %s and ip.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
84 ip_to_str( pi->net_src.data),
85 ip_to_str( pi->net_dst.data),
86 pi->srcport, pi->destport );
90 else if( pi->net_src.type == AT_IPv6 && pi->net_dst.type == AT_IPv6
91 && pi->ipproto == 6 ) {
94 "(ipv6.addr eq %s and ipv6.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
95 ip6_to_str((struct e_in6_addr *)pi->net_src.data),
96 ip6_to_str((struct e_in6_addr *)pi->net_dst.data),
97 pi->srcport, pi->destport );
105 memcpy(ip_address[0], pi->net_src.data, len);
106 memcpy(ip_address[1], pi->net_dst.data, len);
107 tcp_port[0] = pi->srcport;
108 tcp_port[1] = pi->destport;
112 /* here we are going to try and reconstruct the data portion of a TCP
113 session. We will try and handle duplicates, TCP fragments, and out
114 of order packets in a smart way. */
116 static tcp_frag *frags[2] = { 0, 0 };
117 static u_long seq[2];
118 static guint8 src_addr[2][MAX_IPADDR_LEN];
119 static u_int src_port[2] = { 0, 0 };
122 reassemble_tcp( u_long sequence, u_long length, const char* data,
123 u_long data_length, int synflag, address *net_src,
124 address *net_dst, u_int srcport, u_int dstport) {
125 guint8 srcx[MAX_IPADDR_LEN], dstx[MAX_IPADDR_LEN];
126 int src_index, j, first = 0, len;
133 /* First, check if this packet should be processed. */
135 if ((net_src->type != AT_IPv4 && net_src->type != AT_IPv6) ||
136 (net_dst->type != AT_IPv4 && net_dst->type != AT_IPv6))
139 if (net_src->type == AT_IPv4)
144 /* Now check if the packet is for this connection. */
145 memcpy(srcx, net_src->data, len);
146 memcpy(dstx, net_dst->data, len);
147 if ((memcmp(srcx, ip_address[0], len) != 0 &&
148 memcmp(srcx, ip_address[1], len) != 0) ||
149 (memcmp(dstx, ip_address[0], len) != 0 &&
150 memcmp(dstx, ip_address[1], len) != 0) ||
151 (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
152 (dstport != tcp_port[0] && dstport != tcp_port[1]))
155 /* Initialize our stream chunk. This data gets written to disk. */
156 memcpy(sc.src_addr, srcx, len);
157 sc.src_port = srcport;
158 sc.dlen = data_length;
160 /* Check to see if we have seen this source IP and port before.
161 (Yes, we have to check both source IP and port; the connection
162 might be between two different ports on the same machine.) */
163 for( j=0; j<2; j++ ) {
164 if (memcmp(src_addr[j], srcx, len) == 0 && src_port[j] == srcport ) {
168 /* we didn't find it if src_index == -1 */
169 if( src_index < 0 ) {
170 /* assign it to a src_index and get going */
171 for( j=0; j<2; j++ ) {
172 if( src_port[j] == 0 ) {
173 memcpy(src_addr[j], srcx, len);
174 src_port[j] = srcport;
181 if( src_index < 0 ) {
182 fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
186 if( data_length < length ) {
187 incomplete_tcp_stream = TRUE;
190 /* now that we have filed away the srcs, lets get the sequence number stuff
193 /* this is the first time we have seen this src's sequence number */
194 seq[src_index] = sequence + length;
198 /* write out the packet data */
199 write_packet_data( src_index, &sc, data );
202 /* if we are here, we have already seen this src, let's
203 try and figure out if this packet is in the right place */
204 if( sequence < seq[src_index] ) {
205 /* this sequence number seems dated, but
206 check the end to make sure it has no more
207 info than we have already seen */
208 newseq = sequence + length;
209 if( newseq > seq[src_index] ) {
212 /* this one has more than we have seen. let's get the
213 payload that we have not seen. */
215 new_len = seq[src_index] - sequence;
217 if ( data_length <= new_len ) {
220 incomplete_tcp_stream = TRUE;
223 data_length -= new_len;
225 sc.dlen = data_length;
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;