aboutgitcodebugslistschat
path: root/pcap.c
diff options
context:
space:
mode:
Diffstat (limited to 'pcap.c')
-rw-r--r--pcap.c33
1 files changed, 17 insertions, 16 deletions
diff --git a/pcap.c b/pcap.c
index 46cc4b0..3d623cf 100644
--- a/pcap.c
+++ b/pcap.c
@@ -86,9 +86,8 @@ static void pcap_frame(const struct iovec *iov, size_t iovcnt,
.caplen = l2len,
.len = l2len
};
- struct iovec hiov = { &h, sizeof(h) };
- if (write_remainder(pcap_fd, &hiov, 1, 0) < 0 ||
+ 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);
}
@@ -101,12 +100,14 @@ static void pcap_frame(const struct iovec *iov, size_t iovcnt,
void pcap(const char *pkt, size_t l2len)
{
struct iovec iov = { (char *)pkt, l2len };
- struct timespec now;
+ struct timespec now = { 0 };
if (pcap_fd == -1)
return;
- clock_gettime(CLOCK_REALTIME, &now);
+ if (clock_gettime(CLOCK_REALTIME, &now))
+ err_perror("Failed to get CLOCK_REALTIME time");
+
pcap_frame(&iov, 1, 0, &now);
}
@@ -120,13 +121,14 @@ void pcap(const char *pkt, size_t l2len)
void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n,
size_t offset)
{
- struct timespec now;
+ struct timespec now = { 0 };
unsigned int i;
if (pcap_fd == -1)
return;
- clock_gettime(CLOCK_REALTIME, &now);
+ 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, &now);
@@ -139,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 timespec now;
+ struct timespec now = { 0 };
if (pcap_fd == -1)
return;
- clock_gettime(CLOCK_REALTIME, &now);
- pcap_frame(iov, iovcnt, 0, &now);
+ if (clock_gettime(CLOCK_REALTIME, &now))
+ err_perror("Failed to get CLOCK_REALTIME time");
+
+ pcap_frame(iov, iovcnt, offset, &now);
}
/**
@@ -158,18 +162,15 @@ 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;
}