Skip to content

Commit

Permalink
Fixed a bug that caused a deadlock when binding to an IP that is not …
Browse files Browse the repository at this point in the history
…assigned to any network interface. (#15)
  • Loading branch information
FlorianReimold authored Apr 19, 2024
1 parent 6b7622c commit a381b68
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
24 changes: 24 additions & 0 deletions tests/udpcap_test/src/udpcap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 0 additions & 5 deletions udpcap/src/udpcap_socket_private.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ namespace Udpcap
if (!openPcapDevice_nolock(GetLoopbackDeviceName()))
{
LOG_DEBUG(std::string("Bind error: Unable to bind to ") + GetLoopbackDeviceName());
close();
return false;
}
}
Expand All @@ -121,7 +120,6 @@ namespace Udpcap
if (devices.empty())
{
LOG_DEBUG("Bind error: No devices found");
close();
return false;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -162,7 +158,6 @@ namespace Udpcap
if (!openPcapDevice_nolock(GetLoopbackDeviceName()))
{
LOG_DEBUG(std::string("Bind error: Unable to open ") + GetLoopbackDeviceName());
close();
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion udpcap/version.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
set(UDPCAP_VERSION_MAJOR 2)
set(UDPCAP_VERSION_MINOR 0)
set(UDPCAP_VERSION_PATCH 0)
set(UDPCAP_VERSION_PATCH 1)

0 comments on commit a381b68

Please sign in to comment.