[ovs-dev] [tcpdump 3/5] pcap-file: Add timestamp support for reading and writing pcap files.

Ben Pfaff blp at nicira.com
Fri Nov 22 21:37:38 UTC 2013


Only the write support is initially useful, but an upcoming commit will add
a user for the read support.

Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 lib/pcap-file.c       |   31 +++++++++++++++++++++----------
 lib/pcap-file.h       |    2 +-
 tests/test-flows.c    |    4 ++--
 utilities/ovs-ofctl.c |    2 +-
 4 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/lib/pcap-file.c b/lib/pcap-file.c
index fbb0751..f13fe19 100644
--- a/lib/pcap-file.c
+++ b/lib/pcap-file.c
@@ -24,6 +24,7 @@
 #include "byte-order.h"
 #include "compiler.h"
 #include "ofpbuf.h"
+#include "timeval.h"
 #include "vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(pcap);
@@ -128,12 +129,13 @@ pcap_write_header(FILE *file)
 }
 
 int
-pcap_read(FILE *file, struct ofpbuf **bufp)
+pcap_read(FILE *file, struct ofpbuf **bufp, long long int *when)
 {
     struct pcaprec_hdr prh;
     struct ofpbuf *buf;
     void *data;
     size_t len;
+    bool swap;
 
     *bufp = NULL;
 
@@ -151,15 +153,21 @@ pcap_read(FILE *file, struct ofpbuf **bufp)
 
     /* Calculate length. */
     len = prh.incl_len;
-    if (len > 0xffff) {
-        uint32_t swapped_len = uint32_byteswap(len);
-        if (swapped_len > 0xffff) {
-            VLOG_WARN("bad packet length %zu or %"PRIu32" "
-                      "reading pcap file",
-                      len, swapped_len);
+    swap = len > 0xffff;
+    if (swap) {
+        len = uint32_byteswap(len);
+        if (len > 0xffff) {
+            VLOG_WARN("bad packet length %zu or %"PRIu32" reading pcap file",
+                      len, uint32_byteswap(len));
             return EPROTO;
         }
-        len = swapped_len;
+    }
+
+    /* Calculate time. */
+    if (when) {
+        uint32_t ts_sec = swap ? uint32_byteswap(prh.ts_sec) : prh.ts_sec;
+        uint32_t ts_usec = swap ? uint32_byteswap(prh.ts_usec) : prh.ts_usec;
+        *when = ts_sec * 1000LL + ts_usec / 1000;
     }
 
     /* Read packet. */
@@ -180,8 +188,11 @@ void
 pcap_write(FILE *file, struct ofpbuf *buf)
 {
     struct pcaprec_hdr prh;
-    prh.ts_sec = 0;
-    prh.ts_usec = 0;
+    struct timeval tv;
+
+    xgettimeofday(&tv);
+    prh.ts_sec = tv.tv_sec;
+    prh.ts_usec = tv.tv_usec;
     prh.incl_len = buf->size;
     prh.orig_len = buf->size;
     ignore(fwrite(&prh, sizeof prh, 1, file));
diff --git a/lib/pcap-file.h b/lib/pcap-file.h
index 46625c3..7148b18 100644
--- a/lib/pcap-file.h
+++ b/lib/pcap-file.h
@@ -24,7 +24,7 @@ struct ofpbuf;
 FILE *pcap_open(const char *file_name, const char *mode);
 int pcap_read_header(FILE *);
 void pcap_write_header(FILE *);
-int pcap_read(FILE *, struct ofpbuf **);
+int pcap_read(FILE *, struct ofpbuf **, long long int *when);
 void pcap_write(FILE *, struct ofpbuf *);
 
 #endif /* pcap-file.h */
diff --git a/tests/test-flows.c b/tests/test-flows.c
index 6528b07..99a9e69 100644
--- a/tests/test-flows.c
+++ b/tests/test-flows.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -61,7 +61,7 @@ main(int argc OVS_UNUSED, char *argv[])
         union flow_in_port in_port_;
         n++;
 
-        retval = pcap_read(pcap, &packet);
+        retval = pcap_read(pcap, &packet, NULL);
         if (retval == EOF) {
             ovs_fatal(0, "unexpected end of file reading pcap file");
         } else if (retval) {
diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c
index 1d1b47f..d7c2a12 100644
--- a/utilities/ovs-ofctl.c
+++ b/utilities/ovs-ofctl.c
@@ -3122,7 +3122,7 @@ ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[])
         struct flow flow;
         int error;
 
-        error = pcap_read(pcap, &packet);
+        error = pcap_read(pcap, &packet, NULL);
         if (error == EOF) {
             break;
         } else if (error) {
-- 
1.7.10.4




More information about the dev mailing list