Skip to content

Loading…

WIP Rewrite uv_udp_bind. #651

Closed
wants to merge 2 commits into from

1 participant

@txdv

uv_udp_bind(handle, port) binds now in dual stack mode,
uv_udp_bind4(handle, addr) only to ipv4 addresses and
uv_udp_bind6(handle, addr6) only to ipv6 addresses.

We don't need the flags attribute anymore, but have instead have
an additional method.

What do you think about the API?

txdv added some commits
@txdv txdv Rewrite uv_udp_bind.
uv_udp_bind binds now in dual stack mode,
uv_udp_bind4 only to ipv4 addresses and
uv_udp_bind6 only to ipv6 addresses.

We don't need the flags attribute anymore, but have instead
an additional method.
4d14edf
@txdv txdv Fix all tests. fa4f88c
@txdv txdv closed this
@txdv txdv deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 12, 2012
  1. @txdv

    Rewrite uv_udp_bind.

    txdv committed
    uv_udp_bind binds now in dual stack mode,
    uv_udp_bind4 only to ipv4 addresses and
    uv_udp_bind6 only to ipv6 addresses.
    
    We don't need the flags attribute anymore, but have instead
    an additional method.
  2. @txdv

    Fix all tests.

    txdv committed
This page is out of date. Refresh to see the latest.
View
20 include/uv.h
@@ -744,18 +744,28 @@ UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle);
UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock);
/*
+ * Bind to a all addresses in dual stack mode and a specific port.
+ *
+ * Arguments:
+ * handle UDP handle. Should have been initialized with `uv_udp_init`.
+ * port the port to be bound to.
+ *
+ * Returns:
+ * 0 on success, -1 on error.
+ */
+UV_EXTERN int uv_udp_bind(uv_udp_t* handle, int port);
+
+/*
* Bind to a IPv4 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 Unused.
*
* Returns:
* 0 on success, -1 on error.
*/
-UV_EXTERN int uv_udp_bind(uv_udp_t* handle, struct sockaddr_in addr,
- unsigned flags);
+UV_EXTERN int uv_udp_bind4(uv_udp_t* handle, struct sockaddr_in addr);
/*
* Bind to a IPv6 address and port.
@@ -763,13 +773,11 @@ UV_EXTERN int uv_udp_bind(uv_udp_t* handle, struct sockaddr_in addr,
* 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, -1 on error.
*/
-UV_EXTERN int uv_udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr,
- unsigned flags);
+UV_EXTERN int uv_udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr);
UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
int* namelen);
View
30 src/unix/udp.c
@@ -282,11 +282,11 @@ static void uv__udp_sendmsg(uv_loop_t* loop,
}
-static int uv__bind(uv_udp_t* handle,
- int domain,
- struct sockaddr* addr,
- socklen_t len,
- unsigned flags) {
+int uv__udp_bind(uv_udp_t* handle,
+ int domain,
+ struct sockaddr* addr,
+ socklen_t len,
+ unsigned flags) {
int saved_errno;
int status;
int yes;
@@ -404,7 +404,7 @@ static int uv__udp_maybe_deferred_bind(uv_udp_t* handle, int domain) {
abort();
}
- return uv__bind(handle, domain, (struct sockaddr*)&taddr, addrlen, 0);
+ return uv__udp_bind(handle, domain, (struct sockaddr*)&taddr, addrlen, 0);
}
@@ -458,24 +458,6 @@ int uv_udp_init(uv_loop_t* loop, uv_udp_t* handle) {
}
-int uv__udp_bind(uv_udp_t* handle, struct sockaddr_in addr, unsigned flags) {
- return uv__bind(handle,
- AF_INET,
- (struct sockaddr*)&addr,
- sizeof addr,
- flags);
-}
-
-
-int uv__udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr, unsigned flags) {
- return uv__bind(handle,
- AF_INET6,
- (struct sockaddr*)&addr,
- sizeof addr,
- flags);
-}
-
-
int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock) {
int saved_errno;
int status;
View
28 src/uv-common.c
@@ -216,25 +216,41 @@ int uv_tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6 addr) {
}
-int uv_udp_bind(uv_udp_t* handle, struct sockaddr_in addr,
- unsigned int flags) {
+int uv_udp_bind(uv_udp_t *handle, int port) {
+ struct sockaddr_in6 addr = uv_ip6_addr("::", port);
+ return uv__udp_bind(handle,
+ AF_INET6,
+ (struct sockaddr*)&addr,
+ sizeof addr,
+ 0);
+}
+
+
+int uv_udp_bind4(uv_udp_t* handle, struct sockaddr_in addr) {
if (handle->type != UV_UDP || addr.sin_family != AF_INET) {
uv__set_artificial_error(handle->loop, UV_EFAULT);
return -1;
}
- return uv__udp_bind(handle, addr, flags);
+ return uv__udp_bind(handle,
+ AF_INET,
+ (struct sockaddr*)&addr,
+ sizeof addr,
+ 0);
}
-int uv_udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr,
- unsigned int flags) {
+int uv_udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr) {
if (handle->type != UV_UDP || addr.sin6_family != AF_INET6) {
uv__set_artificial_error(handle->loop, UV_EFAULT);
return -1;
}
- return uv__udp_bind6(handle, addr, flags);
+ return uv__udp_bind(handle,
+ AF_INET6,
+ (struct sockaddr*)&addr,
+ sizeof addr,
+ UV_UDP_IPV6ONLY);
}
View
7 src/uv-common.h
@@ -80,8 +80,11 @@ uv_err_t uv__new_artificial_error(uv_err_code code);
int uv__tcp_bind(uv_tcp_t* handle, struct sockaddr_in addr);
int uv__tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6 addr);
-int uv__udp_bind(uv_udp_t* handle, struct sockaddr_in addr, unsigned flags);
-int uv__udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr, unsigned flags);
+int uv__udp_bind(uv_udp_t* handle,
+ int domain,
+ struct sockaddr* addr,
+ socklen_t len,
+ unsigned flags);
int uv__tcp_connect(uv_connect_t* req,
uv_tcp_t* handle,
View
2 test/test-getsockname.c
@@ -273,7 +273,7 @@ static int udp_listener() {
return 1;
}
- r = uv_udp_bind(&udpServer, addr, 0);
+ r = uv_udp_bind4(&udpServer, addr);
if (r) {
fprintf(stderr, "Bind error\n");
return 1;
View
2 test/test-ref.c
@@ -301,7 +301,7 @@ TEST_IMPL(udp_ref2) {
struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
uv_udp_t h;
uv_udp_init(uv_default_loop(), &h);
- uv_udp_bind(&h, addr, 0);
+ uv_udp_bind4(&h, addr);
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());
View
11 test/test-udp-ipv6.c
@@ -94,7 +94,7 @@ static void timeout_cb(uv_timer_t* timer, int status) {
}
-static void do_test(uv_udp_recv_cb recv_cb, int bind_flags) {
+static void do_test(uv_udp_recv_cb recv_cb, int dual_stack) {
struct sockaddr_in6 addr6;
struct sockaddr_in addr;
uv_buf_t buf;
@@ -105,7 +105,10 @@ 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);
+ if (dual_stack > 0)
+ r = uv_udp_bind(&server, TEST_PORT);
+ else
+ r = uv_udp_bind6(&server, addr6);
ASSERT(r == 0);
r = uv_udp_recv_start(&server, alloc_cb, recv_cb);
@@ -139,7 +142,7 @@ static void do_test(uv_udp_recv_cb recv_cb, int bind_flags) {
TEST_IMPL(udp_dual_stack) {
- do_test(ipv6_recv_ok, 0);
+ do_test(ipv6_recv_ok, 1);
ASSERT(recv_cb_called == 1);
ASSERT(send_cb_called == 1);
@@ -149,7 +152,7 @@ TEST_IMPL(udp_dual_stack) {
TEST_IMPL(udp_ipv6_only) {
- do_test(ipv6_recv_fail, UV_UDP_IPV6ONLY);
+ do_test(ipv6_recv_fail, 0);
ASSERT(recv_cb_called == 0);
ASSERT(send_cb_called == 1);
View
2 test/test-udp-multicast-join.c
@@ -108,7 +108,7 @@ TEST_IMPL(udp_multicast_join) {
ASSERT(r == 0);
/* bind to the desired port */
- r = uv_udp_bind(&client, addr, 0);
+ r = uv_udp_bind4(&client, addr);
ASSERT(r == 0);
/* join the multicast channel */
View
2 test/test-udp-multicast-ttl.c
@@ -62,7 +62,7 @@ TEST_IMPL(udp_multicast_ttl) {
r = uv_udp_init(uv_default_loop(), &server);
ASSERT(r == 0);
- r = uv_udp_bind(&server, uv_ip4_addr("0.0.0.0", 0), 0);
+ r = uv_udp_bind4(&server, uv_ip4_addr("0.0.0.0", 0));
ASSERT(r == 0);
r = uv_udp_set_multicast_ttl(&server, 32);
View
2 test/test-udp-open.c
@@ -135,7 +135,7 @@ TEST_IMPL(udp_open) {
r = uv_udp_open(&client, sock);
ASSERT(r == 0);
- r = uv_udp_bind(&client, addr, 0);
+ r = uv_udp_bind4(&client, addr);
ASSERT(r == 0);
r = uv_udp_recv_start(&client, alloc_cb, recv_cb);
View
2 test/test-udp-options.c
@@ -40,7 +40,7 @@ TEST_IMPL(udp_options) {
uv_unref((uv_handle_t*)&h); /* don't keep the loop alive */
- r = uv_udp_bind(&h, uv_ip4_addr("0.0.0.0", TEST_PORT), 0);
+ r = uv_udp_bind4(&h, uv_ip4_addr("0.0.0.0", TEST_PORT));
ASSERT(r == 0);
r = uv_udp_set_broadcast(&h, 1);
View
2 test/test-udp-send-and-recv.c
@@ -173,7 +173,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_bind4(&server, addr);
ASSERT(r == 0);
r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);
Something went wrong with that request. Please try again.