bgp route refresh/MP capability option.
[obnox/wireshark/wip.git] / follow.c
1 /* follow.c
2  *
3  * $Id: follow.c,v 1.26 2000/08/24 21:05:16 deniel Exp $
4  *
5  * Copyright 1998 Mike Hall <mlh@io.com>
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * 
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.
16  * 
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.
21  * 
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.
25  *
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42
43 #include <glib.h>
44 #include "packet.h"
45 #include "follow.h"
46
47 FILE* data_out_file;
48
49 gboolean incomplete_tcp_stream = FALSE;
50
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;
55
56 static int check_fragments( int, tcp_stream_chunk * );
57 static void write_packet_data( int, tcp_stream_chunk *, const char * );
58
59 void
60 follow_tcp_stats(follow_tcp_stats_t* stats)
61 {
62         int i;
63
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;
69         }
70 }
71
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 
75    very good one */
76 char* 
77 build_follow_filter( packet_info *pi ) {
78   char* buf = g_malloc(1024);
79   int len;
80   if( pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4
81         && pi->ipproto == 6 ) {
82     /* TCP over IPv4 */
83     sprintf( buf, 
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 );
88     len = 4;
89     is_ipv6 = FALSE;
90   }
91   else if( pi->net_src.type == AT_IPv6 && pi->net_dst.type == AT_IPv6
92         && pi->ipproto == 6 ) {
93     /* TCP over IPv6 */
94     sprintf( buf, 
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 );
99     len = 16;
100     is_ipv6 = TRUE;
101   }
102   else { 
103     g_free( buf );
104     return NULL;
105   }
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;
110   return buf;
111 }
112
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. */
116
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 };
121
122 void 
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;
128   u_long newseq;
129   tcp_frag *tmp_frag;
130   tcp_stream_chunk sc;
131   
132   src_index = -1;
133   
134   /* First, check if this packet should be processed. */
135
136   if ((net_src->type != AT_IPv4 && net_src->type != AT_IPv6) ||
137       (net_dst->type != AT_IPv4 && net_dst->type != AT_IPv6))
138     return;
139
140   if (net_src->type == AT_IPv4)
141     len = 4;
142   else
143     len = 16;
144
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]))
154     return;
155
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;
160
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 ) {
166       src_index = j;
167     }
168   }
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;
176         src_index = j;
177         first = 1;
178         break;
179       }
180     }
181   }
182   if( src_index < 0 ) {
183     fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
184     return;
185   }
186
187   if( data_length < length ) {
188     incomplete_tcp_stream = TRUE;
189   }
190
191   /* now that we have filed away the srcs, lets get the sequence number stuff 
192      figured out */
193   if( first ) {
194     /* this is the first time we have seen this src's sequence number */
195     seq[src_index] = sequence + length;
196     if( synflag ) {
197       seq[src_index]++;
198     }
199     /* write out the packet data */
200     write_packet_data( src_index, &sc, data );
201     return;
202   }
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] ) {
211       u_long new_len;
212
213       /* this one has more than we have seen. let's get the 
214          payload that we have not seen. */
215
216       new_len = seq[src_index] - sequence;
217
218       if ( data_length <= new_len ) {
219         data = NULL;
220         data_length = 0;
221         incomplete_tcp_stream = TRUE;
222       } else {
223         data += new_len;
224         data_length -= new_len;
225       }
226       sequence = seq[src_index];
227       length = newseq - seq[src_index];
228       
229       /* this will now appear to be right on time :) */
230     }
231   }
232   if ( sequence == seq[src_index] ) {
233     /* right on time */
234     seq[src_index] += length;
235     if( synflag ) seq[src_index]++;
236     if( data ) {
237       write_packet_data( src_index, &sc, data );
238     }
239     /* done with the packet, see if it caused a fragment to fit */
240     while( check_fragments( src_index, &sc ) )
241       ;
242   }
243   else {
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];
254       } else {
255         tmp_frag->next = NULL;
256       }
257       frags[src_index] = tmp_frag;
258     }
259   }
260 } /* end reassemble_tcp */
261
262 /* here we search through all the frag we have collected to see if
263    one fits */
264 static int 
265 check_fragments( int index, tcp_stream_chunk *sc ) {
266   tcp_frag *prev = NULL;
267   tcp_frag *current;
268   current = frags[index];
269   while( current ) {
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 );
275       }
276       seq[index] += current->len;
277       if( prev ) {
278         prev->next = current->next;
279       } else {
280         frags[index] = current->next;
281       }
282       free( current->data );
283       free( current );
284       return 1;
285     }
286     prev = current;
287     current = current->next;
288   }
289   return 0;
290 }
291
292 /* this should always be called before we start to reassemble a stream */
293 void 
294 reset_tcp_reassembly() {
295   tcp_frag *current, *next;
296   int i;
297   incomplete_tcp_stream = FALSE;
298   for( i=0; i<2; i++ ) {
299     seq[i] = 0;
300     memset(src_addr[i], '\0', MAX_IPADDR_LEN);
301     src_port[i] = 0;
302     memset(ip_address[i], '\0', MAX_IPADDR_LEN);
303     tcp_port[i] = 0;
304     bytes_written[i] = 0;
305     current = frags[i];
306     while( current ) {
307       next = current->next;
308       free( current->data ); 
309       free( current );
310       current = next;
311     }
312     frags[i] = NULL;
313   }
314 }
315
316 static void 
317 write_packet_data( int index, tcp_stream_chunk *sc, const char *data )
318 {
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;
322 }