#include "byte-order.h"
#include "compiler.h"
#include "ofpbuf.h"
+#include "timeval.h"
#include "vlog.h"
VLOG_DEFINE_THIS_MODULE(pcap);
}
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;
/* 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 %"PRIuSIZE" or %"PRIu32" "
+ swap = len > 0xffff;
+ if (swap) {
+ len = uint32_byteswap(len);
+ if (len > 0xffff) {
+ VLOG_WARN("bad packet length %"PRIuSIZE" or %"PRIu32
"reading pcap file",
- len, swapped_len);
+ 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. */
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));
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 */
/*
- * 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.
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) {
struct flow flow;
int error;
- error = pcap_read(pcap, &packet);
+ error = pcap_read(pcap, &packet, NULL);
if (error == EOF) {
break;
} else if (error) {