Skip to content

Commit 9758634

Browse files
Allow UDP to bind to the same ip/port (#188)
This pull request refactors how socket options are set for UDP sockets, improving consistency and reliability in both the main UDP implementation and the emit path. The changes focus on ensuring proper reuse of addresses and ports, and on clarifying when broadcast options are set.
1 parent d7b302a commit 9758634

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

src/dsl/word/UDP.hpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -199,32 +199,20 @@ namespace dsl {
199199
}
200200
}
201201

202-
// Broadcast and multicast reuse address and port
203-
if (options.type == ConnectOptions::Type::BROADCAST
204-
|| options.type == ConnectOptions::Type::MULTICAST) {
205-
206-
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&yes), sizeof(yes)) < 0) {
207-
throw std::system_error(network_errno,
208-
std::system_category(),
209-
"Unable to reuse address on the socket");
210-
}
202+
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&yes), sizeof(yes)) < 0) {
203+
throw std::system_error(network_errno,
204+
std::system_category(),
205+
"Unable to reuse address on the socket");
206+
}
211207

212208
// If SO_REUSEPORT is available set it too
213209
#ifdef SO_REUSEPORT
214-
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char*>(&yes), sizeof(yes)) < 0) {
215-
throw std::system_error(network_errno,
216-
std::system_category(),
217-
"Unable to reuse port on the socket");
218-
}
219-
#endif
220-
221-
// We enable SO_BROADCAST since sometimes we need to send broadcast packets
222-
if (::setsockopt(fd, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char*>(&yes), sizeof(yes)) < 0) {
223-
throw std::system_error(network_errno,
224-
std::system_category(),
225-
"Unable to set broadcast on the socket");
226-
}
210+
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char*>(&yes), sizeof(yes)) < 0) {
211+
throw std::system_error(network_errno,
212+
std::system_category(),
213+
"Unable to reuse port on the socket");
227214
}
215+
#endif
228216

229217
// Bind to the address
230218
if (::bind(fd, &bind_address.sock, bind_address.size()) != 0) {

src/dsl/word/emit/UDP.hpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ namespace dsl {
103103
throw std::system_error(network_errno, std::system_category(), "Unable to open the UDP socket");
104104
}
105105

106+
int yes = 1;
107+
// Set reuse address and port so that emit can use the same port multiple times
108+
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&yes), sizeof(yes))
109+
< 0) {
110+
throw std::system_error(network_errno,
111+
std::system_category(),
112+
"Unable to set the reuse address option on the UDP socket");
113+
}
114+
#ifdef SO_REUSEPORT
115+
// If SO_REUSEPORT is available set it too
116+
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<const char*>(&yes), sizeof(yes))
117+
< 0) {
118+
throw std::system_error(network_errno,
119+
std::system_category(),
120+
"Unable to set the reuse port option on the UDP socket");
121+
}
122+
#endif
123+
106124
// If we are using multicast and we have a specific from_addr we need to tell the system to use the
107125
// correct interface
108126
if (multicast && !from_addr.empty()) {
@@ -145,7 +163,6 @@ namespace dsl {
145163
}
146164

147165
// Assume that if the user is sending a broadcast they want to enable broadcasting
148-
int yes = 1;
149166
if (::setsockopt(fd, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<const char*>(&yes), sizeof(yes))
150167
< 0) {
151168
throw std::system_error(network_errno,

0 commit comments

Comments
 (0)