From 0c283630590142cbc6348cce3c9a7f2c82784096 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 2 Jun 2018 23:08:10 -0400 Subject: [PATCH] win: allow setting udp socket options before bind Unix allows socket options to be set prior to binding. This commit aligns Windows with this behavior. Refs: https://github.com/joyent/libuv/issues/1205 Refs: https://github.com/joyent/libuv/pull/1270 Fixes: https://github.com/libuv/libuv/issues/1842 PR-URL: https://github.com/libuv/libuv/pull/1861 Reviewed-By: Bert Belder Reviewed-By: Santiago Gimeno --- src/win/udp.c | 6 +++--- test/test-udp-options.c | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/win/udp.c b/src/win/udp.c index 131d7028..ad45dadb 100644 --- a/src/win/udp.c +++ b/src/win/udp.c @@ -741,7 +741,7 @@ int uv_udp_set_multicast_interface(uv_udp_t* handle, const char* interface_addr) return UV_EINVAL; } - if (!(handle->flags & UV_HANDLE_BOUND)) + if (handle->socket == INVALID_SOCKET) return UV_EBADF; if (addr_st.ss_family == AF_INET) { @@ -772,7 +772,7 @@ int uv_udp_set_multicast_interface(uv_udp_t* handle, const char* interface_addr) int uv_udp_set_broadcast(uv_udp_t* handle, int value) { BOOL optval = (BOOL) value; - if (!(handle->flags & UV_HANDLE_BOUND)) + if (handle->socket == INVALID_SOCKET) return UV_EBADF; if (setsockopt(handle->socket, @@ -818,7 +818,7 @@ int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock) { return UV_EINVAL; \ } \ \ - if (!(handle->flags & UV_HANDLE_BOUND)) \ + if (handle->socket == INVALID_SOCKET) \ return UV_EBADF; \ \ if (!(handle->flags & UV_HANDLE_IPV6)) { \ diff --git a/test/test-udp-options.c b/test/test-udp-options.c index 8f913675..d8c9d68d 100644 --- a/test/test-udp-options.c +++ b/test/test-udp-options.c @@ -114,9 +114,11 @@ TEST_IMPL(udp_options6) { TEST_IMPL(udp_no_autobind) { uv_loop_t* loop; uv_udp_t h; + uv_udp_t h2; loop = uv_default_loop(); + /* Test a lazy initialized socket. */ ASSERT(0 == uv_udp_init(loop, &h)); ASSERT(UV_EBADF == uv_udp_set_multicast_ttl(&h, 32)); ASSERT(UV_EBADF == uv_udp_set_broadcast(&h, 1)); @@ -130,6 +132,23 @@ TEST_IMPL(udp_no_autobind) { uv_close((uv_handle_t*) &h, NULL); + /* Test a non-lazily initialized socket. */ + ASSERT(0 == uv_udp_init_ex(loop, &h2, AF_INET)); + ASSERT(0 == uv_udp_set_multicast_ttl(&h2, 32)); + ASSERT(0 == uv_udp_set_broadcast(&h2, 1)); + +#if defined(__MVS__) + /* zOS only supports setting ttl for IPv6 sockets. */ + ASSERT(UV_ENOTSUP == uv_udp_set_ttl(&h2, 1)); +#else + ASSERT(0 == uv_udp_set_ttl(&h2, 1)); +#endif + + ASSERT(0 == uv_udp_set_multicast_loop(&h2, 1)); + ASSERT(0 == uv_udp_set_multicast_interface(&h2, "0.0.0.0")); + + uv_close((uv_handle_t*) &h2, NULL); + ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT)); MAKE_VALGRIND_HAPPY();