From b3494c6efc2d719deeec1836a4d5cfd5f9929686 Mon Sep 17 00:00:00 2001 From: Trent Houliston Date: Tue, 5 Aug 2025 16:07:11 +1000 Subject: [PATCH 1/2] Allow any on to rebind to the same address not just broadcast or multicast --- src/dsl/word/UDP.hpp | 32 ++++++++++---------------------- src/dsl/word/emit/UDP.hpp | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/dsl/word/UDP.hpp b/src/dsl/word/UDP.hpp index 4b9d62b3..04206ec7 100644 --- a/src/dsl/word/UDP.hpp +++ b/src/dsl/word/UDP.hpp @@ -199,32 +199,20 @@ namespace dsl { } } - // Broadcast and multicast reuse address and port - if (options.type == ConnectOptions::Type::BROADCAST - || options.type == ConnectOptions::Type::MULTICAST) { - - if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), sizeof(yes)) < 0) { - throw std::system_error(network_errno, - std::system_category(), - "Unable to reuse address on the socket"); - } + if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), sizeof(yes)) < 0) { + throw std::system_error(network_errno, + std::system_category(), + "Unable to reuse address on the socket"); + } // If SO_REUSEPORT is available set it too #ifdef SO_REUSEPORT - if (::setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), sizeof(yes)) < 0) { - throw std::system_error(network_errno, - std::system_category(), - "Unable to reuse port on the socket"); - } -#endif - - // We enable SO_BROADCAST since sometimes we need to send broadcast packets - if (::setsockopt(fd, SOL_SOCKET, SO_BROADCAST, reinterpret_cast(&yes), sizeof(yes)) < 0) { - throw std::system_error(network_errno, - std::system_category(), - "Unable to set broadcast on the socket"); - } + if (::setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), sizeof(yes)) < 0) { + throw std::system_error(network_errno, + std::system_category(), + "Unable to reuse port on the socket"); } +#endif // Bind to the address if (::bind(fd, &bind_address.sock, bind_address.size()) != 0) { diff --git a/src/dsl/word/emit/UDP.hpp b/src/dsl/word/emit/UDP.hpp index ee2b33e5..bc66af79 100644 --- a/src/dsl/word/emit/UDP.hpp +++ b/src/dsl/word/emit/UDP.hpp @@ -103,6 +103,24 @@ namespace dsl { throw std::system_error(network_errno, std::system_category(), "Unable to open the UDP socket"); } + int yes = 1; + // Set reuse address and port so that emit can use the same port multiple times + if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), sizeof(yes)) + < 0) { + throw std::system_error(network_errno, + std::system_category(), + "Unable to set the reuse address option on the UDP socket"); + } +#ifdef SO_REUSEPORT + // If SO_REUSEPORT is available set it too + if (::setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), sizeof(yes)) + < 0) { + throw std::system_error(network_errno, + std::system_category(), + "Unable to set the reuse port option on the UDP socket"); + } +#endif + // If we are using multicast and we have a specific from_addr we need to tell the system to use the // correct interface if (multicast && !from_addr.empty()) { From 81536ab1e05429e32994745480e5d84cd5546e52 Mon Sep 17 00:00:00 2001 From: Aaron Wong Date: Wed, 6 Aug 2025 10:20:02 +1000 Subject: [PATCH 2/2] Update UDP.hpp Remove Duplicate int yes = 1 --- src/dsl/word/emit/UDP.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dsl/word/emit/UDP.hpp b/src/dsl/word/emit/UDP.hpp index bc66af79..dbdfb8af 100644 --- a/src/dsl/word/emit/UDP.hpp +++ b/src/dsl/word/emit/UDP.hpp @@ -163,7 +163,6 @@ namespace dsl { } // Assume that if the user is sending a broadcast they want to enable broadcasting - int yes = 1; if (::setsockopt(fd, SOL_SOCKET, SO_BROADCAST, reinterpret_cast(&yes), sizeof(yes)) < 0) { throw std::system_error(network_errno,