From b674187c38c70772fd2de14de3d727438eb87c8a Mon Sep 17 00:00:00 2001 From: Dan VerWeire Date: Tue, 22 Nov 2011 21:45:50 -0500 Subject: [PATCH 1/4] unix: set SO_REUSEADDR before binding --- src/unix/udp.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/unix/udp.c b/src/unix/udp.c index 6760c310..9bdfd417 100644 --- a/src/unix/udp.c +++ b/src/unix/udp.c @@ -334,6 +334,12 @@ static int uv__bind(uv_udp_t* handle, goto out; } + yes = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) == -1) { + uv__set_sys_error(handle->loop, errno); + goto out; + } + if (flags & UV_UDP_IPV6ONLY) { #ifdef IPV6_V6ONLY yes = 1; From 497b1ecd0008bd972d20937bed6eeec4578bf458 Mon Sep 17 00:00:00 2001 From: Dan VerWeire Date: Tue, 22 Nov 2011 21:47:21 -0500 Subject: [PATCH 2/4] unix: add uv_udp_set_broadcast() and uv_udp_set_multicast_ttl() --- include/uv.h | 26 ++++++++++++++++++++++++++ src/unix/udp.c | 18 ++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/uv.h b/include/uv.h index 5928b4c1..fb4e40ab 100644 --- a/include/uv.h +++ b/include/uv.h @@ -631,6 +631,32 @@ UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr, const char* interface_addr, uv_membership membership); +/* + * Set the multicast ttl + * + * Arguments: + * handle UDP handle. Should have been initialized with + * `uv_udp_init`. + * ttl 1 through 255 + * + * Returns: + * 0 on success, -1 on error. + */ +int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl); + +/* + * Set broadcast on or off + * + * Arguments: + * handle UDP handle. Should have been initialized with + * `uv_udp_init`. + * on 1 for on, 0 for off + * + * Returns: + * 0 on success, -1 on error. + */ +int uv_udp_set_broadcast(uv_udp_t* handle, int on); + /* * Send data. If the socket has not previously been bound with `uv_udp_bind` * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address) diff --git a/src/unix/udp.c b/src/unix/udp.c index 9bdfd417..0ebfe707 100644 --- a/src/unix/udp.c +++ b/src/unix/udp.c @@ -509,6 +509,24 @@ int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr, return 0; } +int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl) { + if (setsockopt(handle->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof ttl) == -1) { + uv__set_sys_error(handle->loop, errno); + return -1; + } + + return 0; +} + +int uv_udp_set_broadcast(uv_udp_t* handle, int on) { + if (setsockopt(handle->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof on) == -1) { + uv__set_sys_error(handle->loop, errno); + return -1; + } + + return 0; +} + int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name, int* namelen) { From 726e36cf7c1daf8b1ecb2d46e1d5eee07b52c7a1 Mon Sep 17 00:00:00 2001 From: Igor Zinkovsky Date: Sun, 22 Jan 2012 17:52:20 -0800 Subject: [PATCH 3/4] implement uv_udp_set_multicast_ttl and uv_udp_set_broadcast on windows --- src/win/udp.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/win/udp.c b/src/win/udp.c index 0299e775..14f937ac 100644 --- a/src/win/udp.c +++ b/src/win/udp.c @@ -578,3 +578,24 @@ void uv_process_udp_send_req(uv_loop_t* loop, uv_udp_t* handle, DECREASE_PENDING_REQ_COUNT(handle); } + +int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl) { + if (setsockopt(handle->socket, IPPROTO_IP, IP_MULTICAST_TTL, + (const char*)&ttl, sizeof ttl) == -1) { + uv__set_sys_error(handle->loop, errno); + return -1; + } + + return 0; +} + + +int uv_udp_set_broadcast(uv_udp_t* handle, int on) { + if (setsockopt(handle->socket, SOL_SOCKET, SO_BROADCAST, (const char*)&on, + sizeof on) == -1) { + uv__set_sys_error(handle->loop, errno); + return -1; + } + + return 0; +} From edbabe6f83f704aa2f52ef2c33597471bcc5f0f3 Mon Sep 17 00:00:00 2001 From: Igor Zinkovsky Date: Sun, 22 Jan 2012 18:00:07 -0800 Subject: [PATCH 4/4] windows: set error using WSAGetLastError --- src/win/udp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/win/udp.c b/src/win/udp.c index 14f937ac..8a36396b 100644 --- a/src/win/udp.c +++ b/src/win/udp.c @@ -582,7 +582,7 @@ void uv_process_udp_send_req(uv_loop_t* loop, uv_udp_t* handle, int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl) { if (setsockopt(handle->socket, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&ttl, sizeof ttl) == -1) { - uv__set_sys_error(handle->loop, errno); + uv__set_sys_error(handle->loop, WSAGetLastError()); return -1; } @@ -593,7 +593,7 @@ int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl) { int uv_udp_set_broadcast(uv_udp_t* handle, int on) { if (setsockopt(handle->socket, SOL_SOCKET, SO_BROADCAST, (const char*)&on, sizeof on) == -1) { - uv__set_sys_error(handle->loop, errno); + uv__set_sys_error(handle->loop, WSAGetLastError()); return -1; }