aboutgitcodebugslistschat
path: root/pcap.c
diff options
context:
space:
mode:
authorStefano Brivio <sbrivio@redhat.com>2021-10-19 17:28:18 +0200
committerStefano Brivio <sbrivio@redhat.com>2021-10-20 08:29:30 +0200
commit1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc (patch)
tree13ee3b6da82e5eb53a89bb881ccf3a7092dab9fe /pcap.c
parent087b5f4dbb9e3f767a8afbb6c1001c509965940b (diff)
downloadpasst-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar
passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar.gz
passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar.bz2
passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar.lz
passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar.xz
passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.tar.zst
passt-1a563a0cbd4926d0dfe9065a4fcd8771c5b292cc.zip
passt: Address gcc 11 warnings
A mix of unchecked return values, a missing permission mask for open(2) with O_CREAT, and some false positives from -Wstringop-overflow and -Wmaybe-uninitialized. Reported-by: Martin Hauke <mardnh@gmx.de> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'pcap.c')
-rw-r--r--pcap.c35
1 files changed, 25 insertions, 10 deletions
diff --git a/pcap.c b/pcap.c
index 9cf585f..8ff1636 100644
--- a/pcap.c
+++ b/pcap.c
@@ -23,6 +23,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
+#include <errno.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <unistd.h>
@@ -87,8 +88,8 @@ void pcap(char *pkt, size_t len)
h.tv_usec = tv.tv_usec;
h.caplen = h.len = len;
- write(pcap_fd, &h, sizeof(h));
- write(pcap_fd, pkt, len);
+ if (write(pcap_fd, &h, sizeof(h)) < 0 || write(pcap_fd, pkt, len) < 0)
+ debug("Cannot log packet, length %u", len);
}
/**
@@ -98,6 +99,7 @@ void pcap(char *pkt, size_t len)
void pcapm(struct msghdr *mh)
{
struct pcap_pkthdr h;
+ struct iovec *iov;
struct timeval tv;
unsigned int i;
@@ -109,13 +111,19 @@ void pcapm(struct msghdr *mh)
h.tv_usec = tv.tv_usec;
for (i = 0; i < mh->msg_iovlen; i++) {
- struct iovec *iov = &mh->msg_iov[i];
+ iov = &mh->msg_iov[i];
h.caplen = h.len = iov->iov_len - 4;
- write(pcap_fd, &h, sizeof(h));
- write(pcap_fd, (char *)iov->iov_base + 4, iov->iov_len - 4);
+ if (write(pcap_fd, &h, sizeof(h)) < 0)
+ goto fail;
+ if (write(pcap_fd, (char *)iov->iov_base + 4, iov->iov_len - 4))
+ goto fail;
}
+
+ return;
+fail:
+ debug("Cannot log packet, length %u", iov->iov_len - 4);
}
/**
@@ -125,6 +133,7 @@ void pcapm(struct msghdr *mh)
void pcapmm(struct mmsghdr *mmh, unsigned int vlen)
{
struct pcap_pkthdr h;
+ struct iovec *iov;
struct timeval tv;
unsigned int i, j;
@@ -139,15 +148,20 @@ void pcapmm(struct mmsghdr *mmh, unsigned int vlen)
struct msghdr *mh = &mmh[i].msg_hdr;
for (j = 0; j < mh->msg_iovlen; j++) {
- struct iovec *iov = &mh->msg_iov[j];
+ iov = &mh->msg_iov[j];
h.caplen = h.len = iov->iov_len - 4;
- write(pcap_fd, &h, sizeof(h));
- write(pcap_fd, (char *)iov->iov_base + 4,
- iov->iov_len - 4);
+ if (write(pcap_fd, &h, sizeof(h)) < 0)
+ goto fail;
+ if (write(pcap_fd, (char *)iov->iov_base + 4,
+ iov->iov_len - 4) < 0)
+ goto fail;
}
}
+ return;
+fail:
+ debug("Cannot log packet, length %u", iov->iov_len - 4);
}
/**
@@ -194,5 +208,6 @@ void pcap_init(struct ctx *c, int index)
info("Saving packet capture at %s", c->pcap);
- write(pcap_fd, &pcap_hdr, sizeof(pcap_hdr));
+ if (write(pcap_fd, &pcap_hdr, sizeof(pcap_hdr)) < 0)
+ warn("Cannot write PCAP header: %s", strerror(errno));
}