]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Move the code to handle the "-C" flag into "tcpdump.c" from
authorguy <guy>
Wed, 3 Oct 2001 07:35:42 +0000 (07:35 +0000)
committerguy <guy>
Wed, 3 Oct 2001 07:35:42 +0000 (07:35 +0000)
"pcap-dump-trunc.c".

Don't make the variables "dump_and_trunc()" manipulates external or even
static; fill in a structure with their values and pass a pointer to that
structure to "dump_and_trunc()".  "dump_and_trunc()" will update the
"pcap_dumper_t" pointer in that structure if it rotates capture files,
so that the next call to "dump_and_trunc()" will supply the updated
pointer (the previous code was just opening a new capture, but not
updating the variable passed to "dump_and_trunc()").

Instead of duplicating an old version of the "pcap_dump()" code (a
version that didn't write out time stamps as 32-bit seconds and
microsecond values on platforms where the fields in a "struct timeval"
are 64-bit) in "dump_and_trunc()", just call "pcap_dump()" from
"dump_and_trunc()".

Makefile.in
interface.h
pcap-dump-trunc.c [deleted file]
tcpdump.c

index 85557f667ceebc3b42add83b9e48ae2c47436601..5d514e917127ecb95b983b519255d79a3537994f 100644 (file)
@@ -17,7 +17,7 @@
 #  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 #  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 #
-# @(#) $Header: /tcpdump/master/tcpdump/Makefile.in,v 1.256 2001-10-01 01:12:00 mcr Exp $ (LBL)
+# @(#) $Header: /tcpdump/master/tcpdump/Makefile.in,v 1.257 2001-10-03 07:35:42 guy Exp $ (LBL)
 
 #
 # Various configurable paths (remember to edit Makefile.in, not Makefile)
@@ -65,7 +65,7 @@ INSTALL_DATA = @INSTALL_DATA@
        @rm -f $@
        $(CC) $(CFLAGS) -c $(srcdir)/$*.c
 
-CSRC = addrtoname.c gmt2local.c machdep.c parsenfsfh.c pcap-dump-trunc.c \
+CSRC = addrtoname.c gmt2local.c machdep.c parsenfsfh.c \
        print-802_11.c print-ah.c print-arcnet.c print-arp.c \
        print-ascii.c print-atalk.c print-atm.c print-bgp.c \
        print-bootp.c print-bxxp.c print-cdp.c print-chdlc.c \
index d77a354a46b00cbd58fdde8a994d7f66e61755bb..5819281e1e34c44fcdd1576be8e805c0b5921301 100644 (file)
@@ -18,7 +18,7 @@
  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
- * @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.172 2001-10-01 01:12:00 mcr Exp $ (LBL)
+ * @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.173 2001-10-03 07:35:42 guy Exp $ (LBL)
  */
 
 #ifndef tcpdump_interface_h
@@ -124,9 +124,7 @@ extern int packettype;              /* as specified by -T */
 #endif
 #endif
 
-extern char *program_name;     /*used to generate self-identifying messages */
-
-extern char *WFileName;
+extern char *program_name;     /* used to generate self-identifying messages */
 
 extern int32_t thiszone;       /* seconds offset from gmt to local time */
 
@@ -179,8 +177,6 @@ extern const char *dnnum_string(u_short);
 
 #include <pcap.h>
 
-extern void dump_and_trunc(u_char *, const struct pcap_pkthdr *, 
-                         const u_char *);
 extern void ascii_print_with_offset(const u_char *, u_int, u_int);    
 extern void ascii_print(const u_char *, u_int);    
 extern void hex_print_with_offset(const u_char *, u_int, u_int);    
@@ -307,7 +303,4 @@ extern u_short in_cksum(const u_short *, register u_int, int);
 struct bpf_program;
 
 extern void bpf_dump(struct bpf_program *, int);
-
-extern pcap_t *pd;
-
 #endif
diff --git a/pcap-dump-trunc.c b/pcap-dump-trunc.c
deleted file mode 100644 (file)
index 1acfe43..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright (c) 2001
- *     Seth Webster <[email protected]>
- *
- * License: BSD
- * 
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  
- *  1. Redistributions of source code must retain the above copyright
- *     notice, this list of conditions and the following disclaimer.
- *  2. Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimer in
- *     the documentation and/or other materials provided with the
- *     distribution.
- *  3. The names of the authors may not be used to endorse or promote
- *     products derived from this software without specific prior
- *     written permission.
- *  
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- *
- */
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <stdio.h>
-#include <pcap.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "interface.h"
-
-static void reverse(char *s);
-static void swebitoa(unsigned int n, char s[]);
-
-static void reverse (char s[])
-{
-       int i, j, c;
-
-       for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
-               c = s[i];
-               s[i] = s[j];
-               s[j] = c;
-       }
-}
-
-
-static void swebitoa (unsigned int n, char s[]) 
-{
-       unsigned int i;
-
-       i = 0;
-       do {
-               s[i++] = n % 10 + '0';
-       } while ((n /= 10) > 0);
-
-       s[i] = '\0';
-       reverse(s);
-}
-
-void
-dump_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
-{
-       extern char *WFileName;
-       register FILE *f;
-       static uint cnt = 2;
-       char *name;
-       extern pcap_t *pd;
-       extern long int Cflag;
-
-       f = (FILE *)user;
-       
-       if (Cflag && ftell(f) > Cflag) {
-         name = (char *) malloc(strlen(WFileName) + 4);
-         strcpy(name, WFileName);
-         swebitoa(cnt, name + strlen(WFileName));
-         cnt++;
-         pcap_dump_close((pcap_dumper_t *) f);
-         f = (FILE *) pcap_dump_open(pd, name); 
-         free(name);
-       }
-
-       /* XXX we should check the return status */
-       (void)fwrite((char *)h, sizeof(*h), 1, f);
-       (void)fwrite((char *)sp, h->caplen, 1, f);
-}
index f19c54d43b344ec3b799c58198f9fcec7baab0b9..83823c28d0609e46d293d7c19e4b8b6329a535be 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Support for splitting captures into multiple files with a maximum
+ * file size:
+ *
+ * Copyright (c) 2001
+ *     Seth Webster <[email protected]>
  */
 
 #ifndef lint
@@ -24,7 +30,7 @@ static const char copyright[] =
     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
 The Regents of the University of California.  All rights reserved.\n";
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.167 2001-10-01 01:12:01 mcr Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.168 2001-10-03 07:35:44 guy Exp $ (LBL)";
 #endif
 
 /*
@@ -86,7 +92,6 @@ int infodelay;
 int infoprint;
 
 char *program_name;
-char *WFileName;
 
 int32_t thiszone;              /* seconds offset from gmt to local time */
 
@@ -94,7 +99,7 @@ int32_t thiszone;             /* seconds offset from gmt to local time */
 static RETSIGTYPE cleanup(int);
 static void usage(void) __attribute__((noreturn));
 
-extern void dump_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp);
+static void dump_and_trunc(u_char *, const struct pcap_pkthdr *, const u_char *);
 
 #ifdef SIGINFO
 RETSIGTYPE requestinfo(int);
@@ -169,22 +174,28 @@ lookup_printer(int type)
        /* NOTREACHED */
 }
 
-pcap_t *pd;
+static pcap_t *pd;
 
 extern int optind;
 extern int opterr;
 extern char *optarg;
 
+struct dump_info {
+       char    *WFileName;
+       pcap_t  *pd;
+       pcap_dumper_t *p;
+};
+
 int
 main(int argc, char **argv)
 {
        register int cnt, op, i;
        bpf_u_int32 localnet, netmask;
-       register char *cp, *infile, *cmdbuf, *device, *RFileName;
-       extern char *WFileName;
+       register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
        pcap_handler printer;
        struct bpf_program fcode;
        RETSIGTYPE (*oldhandler)(int);
+       struct dump_info info;
        u_char *pcap_userdata;
        char ebuf[PCAP_ERRBUF_SIZE];
 
@@ -449,7 +460,10 @@ main(int argc, char **argv)
                if (p == NULL)
                        error("%s", pcap_geterr(pd));
                printer = dump_and_trunc;
-               pcap_userdata = (u_char *)p;
+               info.WFileName = WFileName;
+               info.pd = pd;
+               info.p = p;
+               pcap_userdata = (u_char *)&info;
        } else {
                printer = lookup_printer(pcap_datalink(pd));
                pcap_userdata = 0;
@@ -457,7 +471,6 @@ main(int argc, char **argv)
                (void)setsignal(SIGINFO, requestinfo);
 #endif
        }
-
        if (RFileName == NULL) {
                (void)fprintf(stderr, "%s: listening on %s\n",
                    program_name, device);
@@ -506,6 +519,60 @@ info(register int verbose)
        infoprint = 0;
 }
 
+static void
+reverse(char *s)
+{
+       int i, j, c;
+
+       for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
+               c = s[i];
+               s[i] = s[j];
+               s[j] = c;
+       }
+}
+
+
+static void
+swebitoa(unsigned int n, char *s)
+{
+       unsigned int i;
+
+       i = 0;
+       do {
+               s[i++] = n % 10 + '0';
+       } while ((n /= 10) > 0);
+
+       s[i] = '\0';
+       reverse(s);
+}
+
+static void
+dump_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
+{
+       struct dump_info *info;
+       static uint cnt = 2;
+       char *name;
+
+       info = (struct dump_info *)user;
+       
+       /*
+        * XXX - this won't prevent capture files from getting
+        * larger than Cflag - the last packet written to the
+        * file could put it over Cflag.
+        */
+       if (Cflag && ftell((FILE *)info->p) > Cflag) {
+               name = (char *) malloc(strlen(info->WFileName) + 4);
+               strcpy(name, info->WFileName);
+               swebitoa(cnt, name + strlen(info->WFileName));
+               cnt++;
+               pcap_dump_close(info->p);
+               info->p = pcap_dump_open(info->pd, name);
+               free(name);
+       }
+
+       pcap_dump((u_char *)info->p, h, sp);
+}
+
 /* Like default_print() but data need not be aligned */
 void
 default_print_unaligned(register const u_char *cp, register u_int length)