Support for NT Rename SMB, from Steven French.
[obnox/wireshark/wip.git] / follow.c
1 /* follow.c
2  *
3  * $Id: follow.c,v 1.29 2002/02/28 19:35:08 gram Exp $
4  *
5  * Copyright 1998 Mike Hall <mlh@io.com>
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  * 
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.
15  * 
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.
20  * 
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.
24  *
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41
42 #include <glib.h>
43 #include <epan/packet.h>
44 #include "follow.h"
45
46 FILE* data_out_file = NULL;
47
48 gboolean incomplete_tcp_stream = FALSE;
49
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;
54
55 static int check_fragments( int, tcp_stream_chunk * );
56 static void write_packet_data( int, tcp_stream_chunk *, const char * );
57
58 void
59 follow_tcp_stats(follow_tcp_stats_t* stats)
60 {
61         int i;
62
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;
68         }
69 }
70
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 
74    very good one */
75 char* 
76 build_follow_filter( packet_info *pi ) {
77   char* buf = g_malloc(1024);
78   int len;
79   if( pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4
80         && pi->ipproto == 6 ) {
81     /* TCP over IPv4 */
82     sprintf( buf, 
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 );
87     len = 4;
88     is_ipv6 = FALSE;
89   }
90   else if( pi->net_src.type == AT_IPv6 && pi->net_dst.type == AT_IPv6
91         && pi->ipproto == 6 ) {
92     /* TCP over IPv6 */
93     sprintf( buf, 
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 );
98     len = 16;
99     is_ipv6 = TRUE;
100   }
101   else { 
102     g_free( buf );
103     return NULL;
104   }
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;
109   return buf;
110 }
111
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. */
115
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 };
120
121 void 
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;
127   u_long newseq;
128   tcp_frag *tmp_frag;
129   tcp_stream_chunk sc;
130   
131   src_index = -1;
132   
133   /* First, check if this packet should be processed. */
134
135   if ((net_src->type != AT_IPv4 && net_src->type != AT_IPv6) ||
136       (net_dst->type != AT_IPv4 && net_dst->type != AT_IPv6))
137     return;
138
139   if (net_src->type == AT_IPv4)
140     len = 4;
141   else
142     len = 16;
143
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]))
153     return;
154
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;
159
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 ) {
165       src_index = j;
166     }
167   }
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;
175         src_index = j;
176         first = 1;
177         break;
178       }
179     }
180   }
181   if( src_index < 0 ) {
182     fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
183     return;
184   }
185
186   if( data_length < length ) {
187     incomplete_tcp_stream = TRUE;
188   }
189
190   /* now that we have filed away the srcs, lets get the sequence number stuff 
191      figured out */
192   if( first ) {
193     /* this is the first time we have seen this src's sequence number */
194     seq[src_index] = sequence + length;
195     if( synflag ) {
196       seq[src_index]++;
197     }
198     /* write out the packet data */
199     write_packet_data( src_index, &sc, data );
200     return;
201   }
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] ) {
210       u_long new_len;
211
212       /* this one has more than we have seen. let's get the 
213          payload that we have not seen. */
214
215       new_len = seq[src_index] - sequence;
216
217       if ( data_length <= new_len ) {
218         data = NULL;
219         data_length = 0;
220         incomplete_tcp_stream = TRUE;
221       } else {
222         data += new_len;
223         data_length -= new_len;
224       }
225       sc.dlen = data_length;
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 }