]> The Tcpdump Group git mirrors - tcpdump/commitdiff
added -C option to rotate save file every optarg * 1,000,000 bytes.
authormcr <mcr>
Mon, 1 Oct 2001 01:12:00 +0000 (01:12 +0000)
committermcr <mcr>
Mon, 1 Oct 2001 01:12:00 +0000 (01:12 +0000)
Makefile.in
interface.h
pcap-dump-trunc.c [new file with mode: 0644]
tcpdump.c

index b3375b19488dc641756229ca9e7af645c22d3cb6..85557f667ceebc3b42add83b9e48ae2c47436601 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.255 2001-09-17 20:06:17 fenner Exp $ (LBL)
+# @(#) $Header: /tcpdump/master/tcpdump/Makefile.in,v 1.256 2001-10-01 01:12:00 mcr 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 \
+CSRC = addrtoname.c gmt2local.c machdep.c parsenfsfh.c pcap-dump-trunc.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 1db7c7b59268d4508055e1f5de4ff81c4ce2e824..d77a354a46b00cbd58fdde8a994d7f66e61755bb 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.171 2001-09-17 21:57:52 fenner Exp $ (LBL)
+ * @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.172 2001-10-01 01:12:00 mcr Exp $ (LBL)
  */
 
 #ifndef tcpdump_interface_h
@@ -124,7 +124,9 @@ extern int packettype;              /* as specified by -T */
 #endif
 #endif
 
-extern char *program_name;     /* used to generate self-identifying messages */
+extern char *program_name;     /*used to generate self-identifying messages */
+
+extern char *WFileName;
 
 extern int32_t thiszone;       /* seconds offset from gmt to local time */
 
@@ -177,6 +179,8 @@ 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);    
@@ -303,4 +307,7 @@ 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
new file mode 100644 (file)
index 0000000..04c01f1
--- /dev/null
@@ -0,0 +1,108 @@
+/*\r
+ * Copyright (c) 2001\r
+ *     Seth Webster <[email protected]>\r
+ *\r
+ * License: BSD\r
+ * \r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ *  \r
+ *  1. Redistributions of source code must retain the above copyright\r
+ *     notice, this list of conditions and the following disclaimer.\r
+ *  2. Redistributions in binary form must reproduce the above copyright\r
+ *     notice, this list of conditions and the following disclaimer in\r
+ *     the documentation and/or other materials provided with the\r
+ *     distribution.\r
+ *  3. The names of the authors may not be used to endorse or promote\r
+ *     products derived from this software without specific prior\r
+ *     written permission.\r
+ *  \r
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR\r
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED\r
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\r
+ *\r
+ */\r
+\r
+#include <sys/param.h>\r
+#include <sys/time.h>\r
+#include <sys/socket.h>\r
+\r
+#if __STDC__\r
+struct mbuf;\r
+struct rtentry;\r
+#endif\r
+#include <net/if.h>\r
+\r
+#include <netinet/in.h>\r
+#include <netinet/if_ether.h>\r
+#include <netinet/in_systm.h>\r
+#include <netinet/ip.h>\r
+#include <netinet/ip_var.h>\r
+#include <netinet/udp.h>\r
+#include <netinet/udp_var.h>\r
+#include <netinet/tcp.h>\r
+#include <netinet/tcpip.h>\r
+\r
+#include <stdio.h>\r
+#include <pcap.h>\r
+#include <unistd.h>\r
+#include <stdlib.h>\r
+\r
+#include "interface.h"\r
+#include "addrtoname.h"\r
+#include "ethertype.h"\r
+\r
+static void reverse(char *s);\r
+static void swebitoa(unsigned int n, char s[]);\r
+\r
+static void reverse (char s[]) {\r
+  int i, j, c;\r
+  \r
+  for (i = 0, j = strlen(s)-1; i < j; i++, j--) {\r
+    c = s[i];\r
+    s[i] = s[j];\r
+    s[j] = c;\r
+  }\r
+}\r
+\r
+\r
+static void swebitoa (unsigned int n, char s[]) {\r
+  \r
+  unsigned int i;\r
+  \r
+  i = 0;\r
+  do {\r
+    s[i++] = n % 10 + '0';\r
+  } while ((n /= 10) > 0);\r
+  \r
+  s[i] = '\0';\r
+  reverse(s);\r
+}\r
+\r
+void\r
+dump_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)\r
+{\r
+       extern char *WFileName;\r
+       register FILE *f;\r
+       static uint cnt = 2;\r
+       char *name;\r
+       extern pcap_t *pd;\r
+       extern long int Cflag;\r
+\r
+       f = (FILE *)user;\r
+       \r
+       if (Cflag && ftell(f) > Cflag) {\r
+         name = (char *) malloc(strlen(WFileName) + 4);\r
+         strcpy(name, WFileName);\r
+         swebitoa(cnt, name + strlen(WFileName));\r
+         cnt++;\r
+         pcap_dump_close((pcap_dumper_t *) f);\r
+         f = (FILE *) pcap_dump_open(pd, name); \r
+         free(name);\r
+       }\r
+\r
+       /* XXX we should check the return status */\r
+       (void)fwrite((char *)h, sizeof(*h), 1, f);\r
+       (void)fwrite((char *)sp, h->caplen, 1, f);\r
+}\r
index 1ec0a0ff7b0278713b87af6f5c59858f43d447eb..f19c54d43b344ec3b799c58198f9fcec7baab0b9 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -24,7 +24,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.166 2001-07-04 22:03:13 fenner Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.167 2001-10-01 01:12:01 mcr Exp $ (LBL)";
 #endif
 
 /*
@@ -76,6 +76,7 @@ int uflag = 0;                        /* Print undecoded NFS handles */
 int vflag;                     /* verbose */
 int xflag;                     /* print packet in hex */
 int Xflag;                     /* print packet in ascii as well as hex */
+off_t Cflag = 0;                /* rotate dump files after this many bytes */
 
 char *espsecret = NULL;                /* ESP secret key */
 
@@ -85,12 +86,16 @@ int infodelay;
 int infoprint;
 
 char *program_name;
+char *WFileName;
 
 int32_t thiszone;              /* seconds offset from gmt to local time */
 
 /* Forwards */
 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);
+
 #ifdef SIGINFO
 RETSIGTYPE requestinfo(int);
 #endif
@@ -164,7 +169,7 @@ lookup_printer(int type)
        /* NOTREACHED */
 }
 
-static pcap_t *pd;
+pcap_t *pd;
 
 extern int optind;
 extern int opterr;
@@ -175,7 +180,8 @@ main(int argc, char **argv)
 {
        register int cnt, op, i;
        bpf_u_int32 localnet, netmask;
-       register char *cp, *infile, *cmdbuf, *device, *RFileName, *WFileName;
+       register char *cp, *infile, *cmdbuf, *device, *RFileName;
+       extern char *WFileName;
        pcap_handler printer;
        struct bpf_program fcode;
        RETSIGTYPE (*oldhandler)(int);
@@ -201,7 +207,7 @@ main(int argc, char **argv)
        
        opterr = 0;
        while (
-           (op = getopt(argc, argv, "ac:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
+           (op = getopt(argc, argv, "ac:C:deE:fF:i:lm:nNOpqr:Rs:StT:uvw:xXY")) != -1)
                switch (op) {
 
                case 'a':
@@ -214,6 +220,12 @@ main(int argc, char **argv)
                                error("invalid packet count %s", optarg);
                        break;
 
+               case 'C':
+                       Cflag = atoi(optarg) * 1000000;
+                       if (Cflag < 0) 
+                               error("invalid file size %s", optarg);
+                       break;
+
                case 'd':
                        ++dflag;
                        break;
@@ -436,7 +448,7 @@ main(int argc, char **argv)
                pcap_dumper_t *p = pcap_dump_open(pd, WFileName);
                if (p == NULL)
                        error("%s", pcap_geterr(pd));
-               printer = pcap_dump;
+               printer = dump_and_trunc;
                pcap_userdata = (u_char *)p;
        } else {
                printer = lookup_printer(pcap_datalink(pd));
@@ -445,6 +457,7 @@ main(int argc, char **argv)
                (void)setsignal(SIGINFO, requestinfo);
 #endif
        }
+
        if (RFileName == NULL) {
                (void)fprintf(stderr, "%s: listening on %s\n",
                    program_name, device);