Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpcap always write out and read in files using 32bit t...



details:   https://anonhg.NetBSD.org/src/rev/8264d3fd73be
branches:  trunk
changeset: 503516:8264d3fd73be
user:      itojun <itojun%NetBSD.org@localhost>
date:      Wed Feb 07 17:35:56 2001 +0000

description:
always write out and read in files using 32bit timeval.
(if you have files written by 64bit arch, sorry, we can't read
those... there's no magic number or anything like that)
sync with tcpdump.org change.

diffstat:

 lib/libpcap/pcap-int.h |  46 +++++++++++++++++++++++++++++++++++++++++++++-
 lib/libpcap/savefile.c |  42 +++++++++++++++++++++++++++++++++---------
 2 files changed, 78 insertions(+), 10 deletions(-)

diffs (163 lines):

diff -r 15ebac7fb8ff -r 8264d3fd73be lib/libpcap/pcap-int.h
--- a/lib/libpcap/pcap-int.h    Wed Feb 07 17:07:07 2001 +0000
+++ b/lib/libpcap/pcap-int.h    Wed Feb 07 17:35:56 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pcap-int.h,v 1.7 1999/07/02 16:03:41 simonb Exp $      */
+/*     $NetBSD: pcap-int.h,v 1.8 2001/02/07 17:35:56 itojun Exp $      */
 
 /*
  * Copyright (c) 1994, 1995, 1996
@@ -46,6 +46,7 @@
 struct pcap_sf {
        FILE *rfile;
        int swapped;
+       int hdrsize;
        int version_major;
        int version_minor;
        u_char *base;
@@ -99,6 +100,49 @@
        char errbuf[PCAP_ERRBUF_SIZE];
 };
 
+/*
+ * This is a timeval as stored in disk in a dumpfile.
+ * It has to use the same types everywhere, independent of the actual
+ * `struct timeval'
+ */
+
+struct pcap_timeval {
+    bpf_int32 tv_sec;          /* seconds */
+    bpf_int32 tv_usec;         /* microseconds */
+};
+
+/*
+ * How a `pcap_pkthdr' is actually stored in the dumpfile.
+ *
+ * Do not change the format of this structure, in any way (this includes
+ * changes that only affect the length of fields in this structure),
+ * and do not make the time stamp anything other than seconds and
+ * microseconds (e.g., seconds and nanoseconds).  Instead:
+ *
+ *     introduce a new structure for the new format;
+ *
+ *     send mail to "tcpdump-workers%tcpdump.org@localhost", requesting a new
+ *     magic number for your new capture file format, and, when
+ *     you get the new magic number, put it in "savefile.c";
+ *
+ *     use that magic number for save files with the changed record
+ *     header;
+ *
+ *     make the code in "savefile.c" capable of reading files with
+ *     the old record header as well as files with the new record header
+ *     (using the magic number to determine the header format).
+ *
+ * Then supply the changes to "patches%tcpdump.org@localhost", so that future
+ * versions of libpcap and programs that use it (such as tcpdump) will
+ * be able to read your new capture file format.
+ */
+
+struct pcap_sf_pkthdr {
+    struct pcap_timeval ts;    /* time stamp */
+    bpf_u_int32 caplen;                /* length of portion present */
+    bpf_u_int32 len;           /* length this packet (off wire) */
+};
+
 int    yylex(void);
 
 #ifndef min
diff -r 15ebac7fb8ff -r 8264d3fd73be lib/libpcap/savefile.c
--- a/lib/libpcap/savefile.c    Wed Feb 07 17:07:07 2001 +0000
+++ b/lib/libpcap/savefile.c    Wed Feb 07 17:35:56 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: savefile.c,v 1.6 2000/04/13 05:10:17 itojun Exp $      */
+/*     $NetBSD: savefile.c,v 1.7 2001/02/07 17:35:56 itojun Exp $      */
 
 /*
  * Copyright (c) 1993, 1994, 1995, 1996
@@ -36,7 +36,7 @@
 static const char rcsid[] =
     "@(#) Header: savefile.c,v 1.36 96/12/10 23:15:02 leres Exp  (LBL)";
 #else
-__RCSID("$NetBSD: savefile.c,v 1.6 2000/04/13 05:10:17 itojun Exp $");
+__RCSID("$NetBSD: savefile.c,v 1.7 2001/02/07 17:35:56 itojun Exp $");
 #endif
 #endif
 
@@ -153,6 +153,7 @@
                p->sf.swapped = 1;
                swap_hdr(&hdr);
        }
+       p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
        if (hdr.version_major < PCAP_VERSION_MAJOR) {
                (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
                goto bad;
@@ -181,7 +182,13 @@
                break;
        }
 
+       if (p->bufsize < 0)
+               p->bufsize = BPF_MAXBUFSIZE;
        p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
+       if (p->sf.base == NULL) {
+               strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
+               goto bad;
+       }
        p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
        p->sf.version_major = hdr.version_major;
        p->sf.version_minor = hdr.version_minor;
@@ -204,20 +211,32 @@
 static int
 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, int buflen)
 {
+       struct pcap_sf_pkthdr sf_hdr;
        FILE *fp = p->sf.rfile;
 
-       /* read the stamp */
-       if (fread((char *)hdr, sizeof(struct pcap_pkthdr), 1, fp) != 1) {
+       /*
+        * Read the packet header; the structure we use as a buffer
+        * is the longer structure for files generated by the patched
+        * libpcap, but if the file has the magic number for an
+        * unpatched libpcap we only read as many bytes as the regular
+        * header has.
+        */
+       if (fread(&sf_hdr, p->sf.hdrsize, 1, fp) != 1) {
                /* probably an EOF, though could be a truncated packet */
                return (1);
        }
 
        if (p->sf.swapped) {
                /* these were written in opposite byte order */
-               hdr->caplen = SWAPLONG(hdr->caplen);
-               hdr->len = SWAPLONG(hdr->len);
-               hdr->ts.tv_sec = SWAPLONG(hdr->ts.tv_sec);
-               hdr->ts.tv_usec = SWAPLONG(hdr->ts.tv_usec);
+               hdr->caplen = SWAPLONG(sf_hdr.caplen);
+               hdr->len = SWAPLONG(sf_hdr.len);
+               hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
+               hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
+       } else {
+               hdr->caplen = sf_hdr.caplen;
+               hdr->len = sf_hdr.len;
+               hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
+               hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
        }
        /*
         * We interchanged the caplen and len fields at version 2.3,
@@ -323,10 +342,15 @@
 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
 {
        register FILE *f;
+       struct pcap_sf_pkthdr sf_hdr;
 
        f = (FILE *)user;
+       sf_hdr.ts.tv_sec  = h->ts.tv_sec;
+       sf_hdr.ts.tv_usec = h->ts.tv_usec;
+       sf_hdr.caplen     = h->caplen;
+       sf_hdr.len        = h->len;
        /* XXX we should check the return status */
-       (void)fwrite((char *)h, sizeof(*h), 1, f);
+       (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
        (void)fwrite((char *)sp, h->caplen, 1, f);
 }
 



Home | Main Index | Thread Index | Old Index