From a381b681af0559605d48421da18636b3964d861f Mon Sep 17 00:00:00 2001 From: Florian Reimold <11774314+FlorianReimold@users.noreply.github.com> Date: Fri, 19 Apr 2024 15:08:50 +0200 Subject: [PATCH] Fixed a bug that caused a deadlock when binding to an IP that is not assigned to any network interface. (#15) --- tests/udpcap_test/src/udpcap_test.cpp | 24 ++++++++++++++++++++++++ udpcap/src/udpcap_socket_private.cpp | 5 ----- udpcap/version.cmake | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/tests/udpcap_test/src/udpcap_test.cpp b/tests/udpcap_test/src/udpcap_test.cpp index 2c504cd..228a0cb 100644 --- a/tests/udpcap_test/src/udpcap_test.cpp +++ b/tests/udpcap_test/src/udpcap_test.cpp @@ -94,6 +94,30 @@ TEST(udpcap, RAIIWithSomebodyWaiting) // Delete the socket } +// Test the return value of a bind with an invalid address +TEST(udpcap, BindInvalidAddress) +{ + // Create a udpcap socket + Udpcap::UdpcapSocket udpcap_socket; + ASSERT_TRUE(udpcap_socket.isValid()); + + // bind the socket + const bool success = udpcap_socket.bind(Udpcap::HostAddress("256.0.0.1"), 14000); + ASSERT_FALSE(success); +} + +// Test the return value of a bind with a valid address that however doesn't belong to any network interface +TEST(udpcap, BindInvalidAddress2) +{ + // Create a udpcap socket + Udpcap::UdpcapSocket udpcap_socket; + ASSERT_TRUE(udpcap_socket.isValid()); + + // bind the socket + const bool success = udpcap_socket.bind(Udpcap::HostAddress("239.0.0.1"), 14000); // This is a multicast address that cannot be bound to + ASSERT_FALSE(success); +} + // Receive a simple Hello World Message TEST(udpcap, SimpleReceive) { diff --git a/udpcap/src/udpcap_socket_private.cpp b/udpcap/src/udpcap_socket_private.cpp index d32c907..6e138fb 100644 --- a/udpcap/src/udpcap_socket_private.cpp +++ b/udpcap/src/udpcap_socket_private.cpp @@ -109,7 +109,6 @@ namespace Udpcap if (!openPcapDevice_nolock(GetLoopbackDeviceName())) { LOG_DEBUG(std::string("Bind error: Unable to bind to ") + GetLoopbackDeviceName()); - close(); return false; } } @@ -121,7 +120,6 @@ namespace Udpcap if (devices.empty()) { LOG_DEBUG("Bind error: No devices found"); - close(); return false; } @@ -143,7 +141,6 @@ namespace Udpcap if (dev.first.empty()) { LOG_DEBUG("Bind error: No local device with address " + local_address.toString()); - close(); return false; } @@ -152,7 +149,6 @@ namespace Udpcap if (!openPcapDevice_nolock(dev.first)) { LOG_DEBUG(std::string("Bind error: Unable to bind to ") + dev.first); - close(); return false; } @@ -162,7 +158,6 @@ namespace Udpcap if (!openPcapDevice_nolock(GetLoopbackDeviceName())) { LOG_DEBUG(std::string("Bind error: Unable to open ") + GetLoopbackDeviceName()); - close(); return false; } } diff --git a/udpcap/version.cmake b/udpcap/version.cmake index a57fa5b..714d177 100644 --- a/udpcap/version.cmake +++ b/udpcap/version.cmake @@ -1,3 +1,3 @@ set(UDPCAP_VERSION_MAJOR 2) set(UDPCAP_VERSION_MINOR 0) -set(UDPCAP_VERSION_PATCH 0) +set(UDPCAP_VERSION_PATCH 1)