Get rid of an unused variable.
[obnox/wireshark/wip.git] / follow.c
index 8a42705c2da2c08e23c4864fe9f28607827bf6f3..c3b5ad0f3f42bb16b3e752aa8f35d1ba5b5283dd 100644 (file)
--- a/follow.c
+++ b/follow.c
@@ -1,13 +1,12 @@
 /* follow.c
  *
- * $Id: follow.c,v 1.3 1998/10/10 03:32:09 gerald Exp $
+ * $Id: follow.c,v 1.29 2002/02/28 19:35:08 gram Exp $
  *
  * Copyright 1998 Mike Hall <mlh@io.com>
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
- *
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 # include "config.h"
 #endif
 
-#include <gtk/gtk.h>
+#include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 
 #ifdef HAVE_SYS_TYPES_H
 # include <sys/types.h>
 #endif
 
-#include "ethereal.h"
-#include "packet.h"
+#include <glib.h>
+#include <epan/packet.h>
 #include "follow.h"
 
-extern FILE* data_out_file;
+FILE* data_out_file = NULL;
+
+gboolean incomplete_tcp_stream = FALSE;
+
+static guint8  ip_address[2][MAX_IPADDR_LEN];
+static u_int   tcp_port[2];
+static u_int   bytes_written[2];
+static gboolean is_ipv6 = FALSE;
+
+static int check_fragments( int, tcp_stream_chunk * );
+static void write_packet_data( int, tcp_stream_chunk *, const char * );
+
+void
+follow_tcp_stats(follow_tcp_stats_t* stats)
+{
+       int i;
+
+       for (i = 0; i < 2 ; i++) {
+               memcpy(stats->ip_address[i], ip_address[i], MAX_IPADDR_LEN);
+               stats->tcp_port[i] = tcp_port[i];
+               stats->bytes_written[i] = bytes_written[i];
+               stats->is_ipv6 = is_ipv6;
+       }
+}
 
 /* this will build libpcap filter text that will only 
    pass the packets related to the stream. There is a 
@@ -49,16 +74,38 @@ extern FILE* data_out_file;
    very good one */
 char* 
 build_follow_filter( packet_info *pi ) {
-  char* buf = malloc(1024);
-  if( pi->ipproto == 6 ) {
-    /* TCP */
-    sprintf( buf, "host %s and host %s and (ip proto \\tcp) and (port %d and port %d)",
-            pi->srcip, pi->destip, pi->srcport, pi->destport );
+  char* buf = g_malloc(1024);
+  int len;
+  if( pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4
+       && pi->ipproto == 6 ) {
+    /* TCP over IPv4 */
+    sprintf( buf, 
+            "(ip.addr eq %s and ip.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
+            ip_to_str( pi->net_src.data), 
+            ip_to_str( pi->net_dst.data), 
+            pi->srcport, pi->destport );
+    len = 4;
+    is_ipv6 = FALSE;
+  }
+  else if( pi->net_src.type == AT_IPv6 && pi->net_dst.type == AT_IPv6
+       && pi->ipproto == 6 ) {
+    /* TCP over IPv6 */
+    sprintf( buf, 
+            "(ipv6.addr eq %s and ipv6.addr eq %s) and (tcp.port eq %d and tcp.port eq %d)",
+            ip6_to_str((struct e_in6_addr *)pi->net_src.data), 
+            ip6_to_str((struct e_in6_addr *)pi->net_dst.data), 
+            pi->srcport, pi->destport );
+    len = 16;
+    is_ipv6 = TRUE;
   }
   else { 
-    free( buf );
+    g_free( buf );
     return NULL;
   }
+  memcpy(ip_address[0], pi->net_src.data, len);
+  memcpy(ip_address[1], pi->net_dst.data, len);
+  tcp_port[0] = pi->srcport;
+  tcp_port[1] = pi->destport;
   return buf;
 }
 
@@ -66,19 +113,55 @@ build_follow_filter( packet_info *pi ) {
    session. We will try and handle duplicates, TCP fragments, and out 
    of order packets in a smart way. */
 
-static tcp_frag *frags[2] = { 0, 0};
+static tcp_frag *frags[2] = { 0, 0 };
 static u_long seq[2];
-static u_long src[2] = { 0, 0 };
+static guint8 src_addr[2][MAX_IPADDR_LEN];
+static u_int src_port[2] = { 0, 0 };
 
 void 
-reassemble_tcp( u_long sequence, u_long length, const char* data, int synflag, u_long srcx ) {
-  int src_index, j, first = 0;
+reassemble_tcp( u_long sequence, u_long length, const char* data,
+               u_long data_length, int synflag, address *net_src,
+               address *net_dst, u_int srcport, u_int dstport) {
+  guint8 srcx[MAX_IPADDR_LEN], dstx[MAX_IPADDR_LEN];
+  int src_index, j, first = 0, len;
   u_long newseq;
   tcp_frag *tmp_frag;
+  tcp_stream_chunk sc;
+  
   src_index = -1;
-  /* first we check to see if we have seen this src ip before. */
+  
+  /* First, check if this packet should be processed. */
+
+  if ((net_src->type != AT_IPv4 && net_src->type != AT_IPv6) ||
+      (net_dst->type != AT_IPv4 && net_dst->type != AT_IPv6))
+    return;
+
+  if (net_src->type == AT_IPv4)
+    len = 4;
+  else
+    len = 16;
+
+  /* Now check if the packet is for this connection. */
+  memcpy(srcx, net_src->data, len);
+  memcpy(dstx, net_dst->data, len);
+  if ((memcmp(srcx, ip_address[0], len) != 0 && 
+       memcmp(srcx, ip_address[1], len) != 0) ||
+      (memcmp(dstx, ip_address[0], len) != 0 &&
+       memcmp(dstx, ip_address[1], len) != 0) ||
+      (srcport != tcp_port[0] && srcport != tcp_port[1]) ||
+      (dstport != tcp_port[0] && dstport != tcp_port[1]))
+    return;
+
+  /* Initialize our stream chunk.  This data gets written to disk. */
+  memcpy(sc.src_addr, srcx, len);
+  sc.src_port = srcport;
+  sc.dlen     = data_length;
+
+  /* Check to see if we have seen this source IP and port before.
+     (Yes, we have to check both source IP and port; the connection
+     might be between two different ports on the same machine.) */
   for( j=0; j<2; j++ ) {
-    if( src[j] == srcx ) {
+    if (memcmp(src_addr[j], srcx, len) == 0 && src_port[j] == srcport ) {
       src_index = j;
     }
   }
@@ -86,8 +169,9 @@ reassemble_tcp( u_long sequence, u_long length, const char* data, int synflag, u
   if( src_index < 0 ) {
     /* assign it to a src_index and get going */
     for( j=0; j<2; j++ ) {
-      if( src[j] == 0 ) {
-       src[j] = srcx;
+      if( src_port[j] == 0 ) {
+       memcpy(src_addr[j], srcx, len);
+       src_port[j] = srcport;
        src_index = j;
        first = 1;
        break;
@@ -98,6 +182,11 @@ reassemble_tcp( u_long sequence, u_long length, const char* data, int synflag, u
     fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
     return;
   }
+
+  if( data_length < length ) {
+    incomplete_tcp_stream = TRUE;
+  }
+
   /* now that we have filed away the srcs, lets get the sequence number stuff 
      figured out */
   if( first ) {
@@ -107,7 +196,7 @@ reassemble_tcp( u_long sequence, u_long length, const char* data, int synflag, u
       seq[src_index]++;
     }
     /* write out the packet data */
-    write_packet_data( data, length );
+    write_packet_data( src_index, &sc, data );
     return;
   }
   /* if we are here, we have already seen this src, let's
@@ -118,11 +207,25 @@ reassemble_tcp( u_long sequence, u_long length, const char* data, int synflag, u
        info than we have already seen */
     newseq = sequence + length;
     if( newseq > seq[src_index] ) {
+      u_long new_len;
+
       /* this one has more than we have seen. let's get the 
         payload that we have not seen. */
-      data += ( seq[src_index] - sequence );
+
+      new_len = seq[src_index] - sequence;
+
+      if ( data_length <= new_len ) {
+       data = NULL;
+       data_length = 0;
+       incomplete_tcp_stream = TRUE;
+      } else {
+       data += new_len;
+       data_length -= new_len;
+      }
+      sc.dlen = data_length;
       sequence = seq[src_index];
       length = newseq - seq[src_index];
+      
       /* this will now appear to be right on time :) */
     }
   }
@@ -130,19 +233,22 @@ reassemble_tcp( u_long sequence, u_long length, const char* data, int synflag, u
     /* right on time */
     seq[src_index] += length;
     if( synflag ) seq[src_index]++;
-    write_packet_data( data, length );
+    if( data ) {
+      write_packet_data( src_index, &sc, data );
+    }
     /* done with the packet, see if it caused a fragment to fit */
-    while( check_fragments( src_index ) )
+    while( check_fragments( src_index, &sc ) )
       ;
   }
   else {
     /* out of order packet */
-    if( sequence > seq[src_index] ) {
+    if(data_length > 0 && sequence > seq[src_index] ) {
       tmp_frag = (tcp_frag *)malloc( sizeof( tcp_frag ) );
-      tmp_frag->data = (u_char *)malloc( length );
+      tmp_frag->data = (u_char *)malloc( data_length );
       tmp_frag->seq = sequence;
       tmp_frag->len = length;
-      bcopy( data, tmp_frag->data, length );
+      tmp_frag->data_len = data_length;
+      memcpy( tmp_frag->data, data, data_length );
       if( frags[src_index] ) {
        tmp_frag->next = frags[src_index];
       } else {
@@ -155,20 +261,23 @@ reassemble_tcp( u_long sequence, u_long length, const char* data, int synflag, u
 
 /* here we search through all the frag we have collected to see if
    one fits */
-int 
-check_fragments( int index ) {
+static int 
+check_fragments( int index, tcp_stream_chunk *sc ) {
   tcp_frag *prev = NULL;
   tcp_frag *current;
   current = frags[index];
   while( current ) {
     if( current->seq == seq[index] ) {
       /* this fragment fits the stream */
-      write_packet_data( current->data, current->len );
+      if( current->data ) {
+        sc->dlen = current->data_len;
+       write_packet_data( index, sc, current->data );
+      }
       seq[index] += current->len;
       if( prev ) {
        prev->next = current->next;
       } else {
-       src[index] = current->next;
+       frags[index] = current->next;
       }
       free( current->data );
       free( current );
@@ -185,9 +294,14 @@ void
 reset_tcp_reassembly() {
   tcp_frag *current, *next;
   int i;
+  incomplete_tcp_stream = FALSE;
   for( i=0; i<2; i++ ) {
     seq[i] = 0;
-    src[i] = 0;
+    memset(src_addr[i], '\0', MAX_IPADDR_LEN);
+    src_port[i] = 0;
+    memset(ip_address[i], '\0', MAX_IPADDR_LEN);
+    tcp_port[i] = 0;
+    bytes_written[i] = 0;
     current = frags[i];
     while( current ) {
       next = current->next;
@@ -199,8 +313,10 @@ reset_tcp_reassembly() {
   }
 }
 
-void 
-write_packet_data( const u_char* data, int length ) {
-  fwrite( data, 1, length, data_out_file );
+static void 
+write_packet_data( int index, tcp_stream_chunk *sc, const char *data )
+{
+  fwrite( sc, 1, sizeof(tcp_stream_chunk), data_out_file );
+  fwrite( data, 1, sc->dlen, data_out_file );
+  bytes_written[index] += sc->dlen;
 }
-