aboutgitcodebugslistschat
path: root/pcap.c
diff options
context:
space:
mode:
Diffstat (limited to 'pcap.c')
-rw-r--r--pcap.c65
1 files changed, 32 insertions, 33 deletions
diff --git a/pcap.c b/pcap.c
index a0f01ad..3d623cf 100644
--- a/pcap.c
+++ b/pcap.c
@@ -72,44 +72,43 @@ struct pcap_pkthdr {
* @iov: IO vector containing frame (with L2 headers and tap headers)
* @iovcnt: Number of buffers (@iov entries) in frame
* @offset: Byte offset of the L2 headers within @iov
- * @tv: Timestamp
+ * @now: Timestamp
*
* Returns: 0 on success, -errno on error writing to the file
*/
static void pcap_frame(const struct iovec *iov, size_t iovcnt,
- size_t offset, const struct timeval *tv)
+ size_t offset, const struct timespec *now)
{
- size_t len = iov_size(iov, iovcnt) - offset;
+ size_t l2len = iov_size(iov, iovcnt) - offset;
struct pcap_pkthdr h = {
- .tv_sec = tv->tv_sec,
- .tv_usec = tv->tv_usec,
- .caplen = len,
- .len = len
+ .tv_sec = now->tv_sec,
+ .tv_usec = DIV_ROUND_CLOSEST(now->tv_nsec, 1000),
+ .caplen = l2len,
+ .len = l2len
};
- struct iovec hiov = { &h, sizeof(h) };
- if (write_remainder(pcap_fd, &hiov, 1, 0) < 0 ||
- write_remainder(pcap_fd, iov, iovcnt, offset) < 0) {
- debug("Cannot log packet, length %zu: %s",
- len, strerror(errno));
- }
+ if (write_all_buf(pcap_fd, &h, sizeof(h)) < 0 ||
+ write_remainder(pcap_fd, iov, iovcnt, offset) < 0)
+ debug_perror("Cannot log packet, length %zu", l2len);
}
/**
* pcap() - Capture a single frame to pcap file
* @pkt: Pointer to data buffer, including L2 headers
- * @len: L2 packet length
+ * @l2len: L2 frame length
*/
-void pcap(const char *pkt, size_t len)
+void pcap(const char *pkt, size_t l2len)
{
- struct iovec iov = { (char *)pkt, len };
- struct timeval tv;
+ struct iovec iov = { (char *)pkt, l2len };
+ struct timespec now = { 0 };
if (pcap_fd == -1)
return;
- gettimeofday(&tv, NULL);
- pcap_frame(&iov, 1, 0, &tv);
+ if (clock_gettime(CLOCK_REALTIME, &now))
+ err_perror("Failed to get CLOCK_REALTIME time");
+
+ pcap_frame(&iov, 1, 0, &now);
}
/**
@@ -122,16 +121,17 @@ void pcap(const char *pkt, size_t len)
void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n,
size_t offset)
{
- struct timeval tv;
+ struct timespec now = { 0 };
unsigned int i;
if (pcap_fd == -1)
return;
- gettimeofday(&tv, NULL);
+ if (clock_gettime(CLOCK_REALTIME, &now))
+ err_perror("Failed to get CLOCK_REALTIME time");
for (i = 0; i < n; i++)
- pcap_frame(iov + i * frame_parts, frame_parts, offset, &tv);
+ pcap_frame(iov + i * frame_parts, frame_parts, offset, &now);
}
/*
@@ -141,17 +141,19 @@ void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n,
* @iov: Pointer to the array of struct iovec describing the I/O vector
* containing packet data to write, including L2 header
* @iovcnt: Number of buffers (@iov entries)
+ * @offset: Offset of the L2 frame within the full data length
*/
-/* cppcheck-suppress unusedFunction */
-void pcap_iov(const struct iovec *iov, size_t iovcnt)
+void pcap_iov(const struct iovec *iov, size_t iovcnt, size_t offset)
{
- struct timeval tv;
+ struct timespec now = { 0 };
if (pcap_fd == -1)
return;
- gettimeofday(&tv, NULL);
- pcap_frame(iov, iovcnt, 0, &tv);
+ if (clock_gettime(CLOCK_REALTIME, &now))
+ err_perror("Failed to get CLOCK_REALTIME time");
+
+ pcap_frame(iov, iovcnt, offset, &now);
}
/**
@@ -160,23 +162,20 @@ void pcap_iov(const struct iovec *iov, size_t iovcnt)
*/
void pcap_init(struct ctx *c)
{
- int flags = O_WRONLY | O_CREAT | O_TRUNC;
-
if (pcap_fd != -1)
return;
if (!*c->pcap)
return;
- flags |= c->foreground ? O_CLOEXEC : 0;
- pcap_fd = open(c->pcap, flags, S_IRUSR | S_IWUSR);
+ pcap_fd = output_file_open(c->pcap, O_WRONLY);
if (pcap_fd == -1) {
- perror("open");
+ err_perror("Couldn't open pcap file %s", c->pcap);
return;
}
info("Saving packet capture to %s", c->pcap);
if (write(pcap_fd, &pcap_hdr, sizeof(pcap_hdr)) < 0)
- warn("Cannot write PCAP header: %s", strerror(errno));
+ warn_perror("Cannot write PCAP header");
}