From bcee403ed295c036cc716770d77019f78d6c954e Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 3 Sep 2013 23:49:26 +0200 Subject: [PATCH] include: merge uv_udp_bind and uv_udp_bind6 Merge uv_udp_bind6() into uv_udp_bind(). uv_udp_bind() now takes a const struct sockaddr*. --- include/uv.h | 24 +++++------------------- src/uv-common.c | 31 ++++++++++++------------------- test/benchmark-udp-pummel.c | 2 +- test/test-getsockname.c | 2 +- test/test-ref.c | 2 +- test/test-udp-ipv6.c | 2 +- test/test-udp-multicast-join.c | 2 +- test/test-udp-multicast-ttl.c | 2 +- test/test-udp-open.c | 2 +- test/test-udp-options.c | 2 +- test/test-udp-send-and-recv.c | 2 +- 11 files changed, 26 insertions(+), 47 deletions(-) diff --git a/include/uv.h b/include/uv.h index 31637e94..b784d179 100644 --- a/include/uv.h +++ b/include/uv.h @@ -804,7 +804,7 @@ struct uv_connect_s { */ enum uv_udp_flags { - /* Disables dual stack mode. Used with uv_udp_bind6(). */ + /* Disables dual stack mode. */ UV_UDP_IPV6ONLY = 1, /* * Indicates message was truncated because read buffer was too small. The @@ -883,7 +883,8 @@ UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock); * * Arguments: * handle UDP handle. Should have been initialized with `uv_udp_init`. - * addr struct sockaddr_in with the address and port to bind to. + * addr struct sockaddr_in or struct sockaddr_in6 with the address and + * port to bind to. * flags Unused. * * Returns: @@ -898,23 +899,8 @@ UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock); * opt-in mechanism in future versions of libuv. */ UV_EXTERN int uv_udp_bind(uv_udp_t* handle, - const struct sockaddr_in* addr, - unsigned flags); - -/* - * Bind to a IPv6 address and port. - * - * Arguments: - * handle UDP handle. Should have been initialized with `uv_udp_init`. - * addr struct sockaddr_in with the address and port to bind to. - * flags Should be 0 or UV_UDP_IPV6ONLY. - * - * Returns: - * 0 on success, or an error code < 0 on failure. - */ -UV_EXTERN int uv_udp_bind6(uv_udp_t* handle, - const struct sockaddr_in6* addr, - unsigned flags); + const struct sockaddr* addr, + unsigned int flags); UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name, int* namelen); diff --git a/src/uv-common.c b/src/uv-common.c index eb7862aa..631a7cd6 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -209,28 +209,21 @@ int uv_tcp_bind(uv_tcp_t* handle, const struct sockaddr* addr) { int uv_udp_bind(uv_udp_t* handle, - const struct sockaddr_in* addr, + const struct sockaddr* addr, unsigned int flags) { - if (handle->type == UV_UDP && addr->sin_family == AF_INET) { - return uv__udp_bind(handle, - (const struct sockaddr*) addr, - sizeof(*addr), - flags); - } - return UV_EINVAL; -} + unsigned int addrlen; + if (handle->type != UV_UDP) + return UV_EINVAL; -int uv_udp_bind6(uv_udp_t* handle, - const struct sockaddr_in6* addr, - unsigned int flags) { - if (handle->type == UV_UDP && addr->sin6_family == AF_INET6) { - return uv__udp_bind(handle, - (const struct sockaddr*) addr, - sizeof(*addr), - flags); - } - return UV_EINVAL; + if (addr->sa_family == AF_INET) + addrlen = sizeof(struct sockaddr_in); + else if (addr->sa_family == AF_INET6) + addrlen = sizeof(struct sockaddr_in6); + else + return UV_EINVAL; + + return uv__udp_bind(handle, addr, addrlen, flags); } diff --git a/test/benchmark-udp-pummel.c b/test/benchmark-udp-pummel.c index 3dec2631..0ec18aae 100644 --- a/test/benchmark-udp-pummel.c +++ b/test/benchmark-udp-pummel.c @@ -174,7 +174,7 @@ static int pummel(unsigned int n_senders, struct sockaddr_in addr; ASSERT(0 == uv_ip4_addr("0.0.0.0", BASE_PORT + i, &addr)); ASSERT(0 == uv_udp_init(loop, &s->udp_handle)); - ASSERT(0 == uv_udp_bind(&s->udp_handle, &addr, 0)); + ASSERT(0 == uv_udp_bind(&s->udp_handle, (const struct sockaddr*) &addr, 0)); ASSERT(0 == uv_udp_recv_start(&s->udp_handle, alloc_cb, recv_cb)); uv_unref((uv_handle_t*)&s->udp_handle); } diff --git a/test/test-getsockname.c b/test/test-getsockname.c index 7567420b..8ceee16f 100644 --- a/test/test-getsockname.c +++ b/test/test-getsockname.c @@ -282,7 +282,7 @@ static int udp_listener(void) { return 1; } - r = uv_udp_bind(&udpServer, &addr, 0); + r = uv_udp_bind(&udpServer, (const struct sockaddr*) &addr, 0); if (r) { fprintf(stderr, "Bind error\n"); return 1; diff --git a/test/test-ref.c b/test/test-ref.c index ad2894c9..f8925d89 100644 --- a/test/test-ref.c +++ b/test/test-ref.c @@ -308,7 +308,7 @@ TEST_IMPL(udp_ref2) { uv_udp_t h; ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); uv_udp_init(uv_default_loop(), &h); - uv_udp_bind(&h, &addr, 0); + uv_udp_bind(&h, (const struct sockaddr*) &addr, 0); uv_udp_recv_start(&h, (uv_alloc_cb)fail_cb, (uv_udp_recv_cb)fail_cb); uv_unref((uv_handle_t*)&h); uv_run(uv_default_loop(), UV_RUN_DEFAULT); diff --git a/test/test-udp-ipv6.c b/test/test-udp-ipv6.c index f545f81b..ea7ff337 100644 --- a/test/test-udp-ipv6.c +++ b/test/test-udp-ipv6.c @@ -108,7 +108,7 @@ static void do_test(uv_udp_recv_cb recv_cb, int bind_flags) { r = uv_udp_init(uv_default_loop(), &server); ASSERT(r == 0); - r = uv_udp_bind6(&server, &addr6, bind_flags); + r = uv_udp_bind(&server, (const struct sockaddr*) &addr6, bind_flags); ASSERT(r == 0); r = uv_udp_recv_start(&server, alloc_cb, recv_cb); diff --git a/test/test-udp-multicast-join.c b/test/test-udp-multicast-join.c index 0e750d84..ebaf742b 100644 --- a/test/test-udp-multicast-join.c +++ b/test/test-udp-multicast-join.c @@ -111,7 +111,7 @@ TEST_IMPL(udp_multicast_join) { ASSERT(r == 0); /* bind to the desired port */ - r = uv_udp_bind(&client, &addr, 0); + r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0); ASSERT(r == 0); /* join the multicast channel */ diff --git a/test/test-udp-multicast-ttl.c b/test/test-udp-multicast-ttl.c index 390ea579..b3c0e11b 100644 --- a/test/test-udp-multicast-ttl.c +++ b/test/test-udp-multicast-ttl.c @@ -63,7 +63,7 @@ TEST_IMPL(udp_multicast_ttl) { ASSERT(r == 0); ASSERT(0 == uv_ip4_addr("0.0.0.0", 0, &addr)); - r = uv_udp_bind(&server, &addr, 0); + r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0); ASSERT(r == 0); r = uv_udp_set_multicast_ttl(&server, 32); diff --git a/test/test-udp-open.c b/test/test-udp-open.c index 3d64b00f..b23b8a25 100644 --- a/test/test-udp-open.c +++ b/test/test-udp-open.c @@ -140,7 +140,7 @@ TEST_IMPL(udp_open) { r = uv_udp_open(&client, sock); ASSERT(r == 0); - r = uv_udp_bind(&client, &addr, 0); + r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0); ASSERT(r == 0); r = uv_udp_recv_start(&client, alloc_cb, recv_cb); diff --git a/test/test-udp-options.c b/test/test-udp-options.c index af90a575..5cc36987 100644 --- a/test/test-udp-options.c +++ b/test/test-udp-options.c @@ -43,7 +43,7 @@ TEST_IMPL(udp_options) { uv_unref((uv_handle_t*)&h); /* don't keep the loop alive */ - r = uv_udp_bind(&h, &addr, 0); + r = uv_udp_bind(&h, (const struct sockaddr*) &addr, 0); ASSERT(r == 0); r = uv_udp_set_broadcast(&h, 1); diff --git a/test/test-udp-send-and-recv.c b/test/test-udp-send-and-recv.c index d8da37b7..9c73333f 100644 --- a/test/test-udp-send-and-recv.c +++ b/test/test-udp-send-and-recv.c @@ -175,7 +175,7 @@ TEST_IMPL(udp_send_and_recv) { r = uv_udp_init(uv_default_loop(), &server); ASSERT(r == 0); - r = uv_udp_bind(&server, &addr, 0); + r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0); ASSERT(r == 0); r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);