From cc762e8db1a319141e471c85c4b1a8aa5aa1c369 Mon Sep 17 00:00:00 2001 From: seladb Date: Tue, 26 Nov 2024 23:54:30 -0800 Subject: [PATCH 1/2] Update to `bind()` --- Pcap++/src/RawSocketDevice.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Pcap++/src/RawSocketDevice.cpp b/Pcap++/src/RawSocketDevice.cpp index 816d10b76c..84f975159a 100644 --- a/Pcap++/src/RawSocketDevice.cpp +++ b/Pcap++/src/RawSocketDevice.cpp @@ -456,7 +456,7 @@ namespace pcpp int fd = socket(AF_PACKET, SOCK_RAW, htobe16(ETH_P_ALL)); if (fd < 0) { - PCPP_LOG_ERROR("Failed to create raw socket. Error code was " << errno); + PCPP_LOG_ERROR("Failed to create raw socket. Error code was " << strerror(errno)); return false; } @@ -505,12 +505,15 @@ namespace pcpp } // bind raw socket to interface - struct ifreq ifr; - memset(&ifr, 0, sizeof(ifr)); - snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifaceName.c_str()); - if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(ifr)) == -1) + struct sockaddr_ll saddr; + memset(&saddr, 0, sizeof(saddr)); + saddr.sll_family = AF_PACKET; + saddr.sll_protocol = htons(ETH_P_ALL); + saddr.sll_ifindex = if_nametoindex(ifaceName.c_str()); + + if (bind(fd, reinterpret_cast(&saddr), sizeof(saddr)) < 0) { - PCPP_LOG_ERROR("Cannot bind raw socket to interface '" << ifaceName << "'"); + PCPP_LOG_ERROR("Cannot bind raw socket to interface '" << ifaceName << "': " << strerror(errno)); ::close(fd); return false; } From c43e11019c27c046629d01382c59b4a70a4c778e Mon Sep 17 00:00:00 2001 From: seladb Date: Sun, 1 Dec 2024 17:26:44 -0800 Subject: [PATCH 2/2] Remove the `struct` keyword --- Pcap++/src/RawSocketDevice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/src/RawSocketDevice.cpp b/Pcap++/src/RawSocketDevice.cpp index 84f975159a..40da2331e7 100644 --- a/Pcap++/src/RawSocketDevice.cpp +++ b/Pcap++/src/RawSocketDevice.cpp @@ -505,13 +505,13 @@ namespace pcpp } // bind raw socket to interface - struct sockaddr_ll saddr; + sockaddr_ll saddr; memset(&saddr, 0, sizeof(saddr)); saddr.sll_family = AF_PACKET; saddr.sll_protocol = htons(ETH_P_ALL); saddr.sll_ifindex = if_nametoindex(ifaceName.c_str()); - if (bind(fd, reinterpret_cast(&saddr), sizeof(saddr)) < 0) + if (bind(fd, reinterpret_cast(&saddr), sizeof(saddr)) < 0) { PCPP_LOG_ERROR("Cannot bind raw socket to interface '" << ifaceName << "': " << strerror(errno)); ::close(fd);