This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

unix, windows: Use of address struct by reference in uv_tcp_connect, …

…uv_*_bind.
  • Loading branch information...
txdv committed Jan 18, 2013
1 parent e4d8cba commit 1ca0eb570f5bd58ab0917bd3f179390e60b0916c
View
@@ -661,23 +661,20 @@ UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle,
*/
UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable);
-UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr_in);
-UV_EXTERN int uv_tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6);
+UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr* addr);
UV_EXTERN int uv_tcp_getsockname(uv_tcp_t* handle, struct sockaddr* name,
int* namelen);
UV_EXTERN int uv_tcp_getpeername(uv_tcp_t* handle, struct sockaddr* name,
int* namelen);
/*
- * uv_tcp_connect, uv_tcp_connect6
- * These functions establish IPv4 and IPv6 TCP connections. Provide an
+ * uv_tcp_connect
+ * This function establishes an IPv4 or IPv6 TCP connection. Provide an
* initialized TCP handle and an uninitialized uv_connect_t*. The callback
* will be made when the connection is established.
*/
UV_EXTERN int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle,
- struct sockaddr_in address, uv_connect_cb cb);
-UV_EXTERN int uv_tcp_connect6(uv_connect_t* req, uv_tcp_t* handle,
- struct sockaddr_in6 address, uv_connect_cb cb);
+ struct sockaddr* addr, uv_connect_cb cb);
/* uv_connect_t is a subclass of uv_req_t */
struct uv_connect_s {
@@ -751,32 +748,19 @@ 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 IPv4 address and port.
+ * Bind to a an 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.
+ * addr struct sockaddr 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,
+UV_EXTERN int uv_udp_bind(uv_udp_t* handle, struct sockaddr* 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, -1 on error.
- */
-UV_EXTERN int uv_udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr,
- unsigned flags);
UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
int* namelen);
View
@@ -128,19 +128,16 @@ static int uv__connect(uv_connect_t* req,
}
-int uv__tcp_bind(uv_tcp_t* handle, struct sockaddr_in addr) {
- return uv__bind(handle,
- AF_INET,
- (struct sockaddr*)&addr,
- sizeof(struct sockaddr_in));
-}
-
+int uv__tcp_bind(uv_tcp_t* handle, struct sockaddr* addr) {
+ if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) {
+ uv__set_artificial_error(handle->loop, UV_EINVAL);
+ return -1;
+ }
-int uv__tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6 addr) {
return uv__bind(handle,
- AF_INET6,
- (struct sockaddr*)&addr,
- sizeof(struct sockaddr_in6));
+ addr->sa_family,
+ addr,
+ addr->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
}
@@ -257,28 +254,18 @@ int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
int uv__tcp_connect(uv_connect_t* req,
uv_tcp_t* handle,
- struct sockaddr_in addr,
+ struct sockaddr* addr,
uv_connect_cb cb) {
int saved_errno;
int status;
- saved_errno = errno;
- status = uv__connect(req, handle, (struct sockaddr*)&addr, sizeof addr, cb);
- errno = saved_errno;
-
- return status;
-}
-
-
-int uv__tcp_connect6(uv_connect_t* req,
- uv_tcp_t* handle,
- struct sockaddr_in6 addr,
- uv_connect_cb cb) {
- int saved_errno;
- int status;
+ if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) {
+ uv__set_artificial_error(handle->loop, UV_EINVAL);
+ return -1;
+ }
saved_errno = errno;
- status = uv__connect(req, handle, (struct sockaddr*)&addr, sizeof addr, cb);
+ status = uv__connect(req, handle, addr, addr->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), cb);
errno = saved_errno;
return status;
View
@@ -456,20 +456,16 @@ 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_bind(uv_udp_t* handle, struct sockaddr* addr, unsigned flags) {
+ if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) {
+ uv__set_artificial_error(handle->loop, UV_EINVAL);
+ return -1;
+ }
-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,
+ addr->sa_family,
+ addr,
+ addr->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
flags);
}
View
@@ -196,8 +196,8 @@ int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size) {
}
-int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr_in addr) {
- if (handle->type != UV_TCP || addr.sin_family != AF_INET) {
+int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr* addr) {
+ if (handle->type != UV_TCP) {
uv__set_artificial_error(handle->loop, UV_EINVAL);
return -1;
}
@@ -206,19 +206,9 @@ int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr_in addr) {
}
-int uv_tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6 addr) {
- if (handle->type != UV_TCP || addr.sin6_family != AF_INET6) {
- uv__set_artificial_error(handle->loop, UV_EINVAL);
- return -1;
- }
-
- return uv__tcp_bind6(handle, addr);
-}
-
-
-int uv_udp_bind(uv_udp_t* handle, struct sockaddr_in addr,
+int uv_udp_bind(uv_udp_t* handle, struct sockaddr *addr,
unsigned int flags) {
- if (handle->type != UV_UDP || addr.sin_family != AF_INET) {
+ if (handle->type != UV_UDP) {
uv__set_artificial_error(handle->loop, UV_EINVAL);
return -1;
}
@@ -227,40 +217,16 @@ int uv_udp_bind(uv_udp_t* handle, struct sockaddr_in addr,
}
-int uv_udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr,
- unsigned int flags) {
- if (handle->type != UV_UDP || addr.sin6_family != AF_INET6) {
- uv__set_artificial_error(handle->loop, UV_EINVAL);
- return -1;
- }
-
- return uv__udp_bind6(handle, addr, flags);
-}
-
-
int uv_tcp_connect(uv_connect_t* req,
uv_tcp_t* handle,
- struct sockaddr_in address,
+ struct sockaddr* addr,
uv_connect_cb cb) {
- if (handle->type != UV_TCP || address.sin_family != AF_INET) {
- uv__set_artificial_error(handle->loop, UV_EINVAL);
- return -1;
- }
-
- return uv__tcp_connect(req, handle, address, cb);
-}
-
-
-int uv_tcp_connect6(uv_connect_t* req,
- uv_tcp_t* handle,
- struct sockaddr_in6 address,
- uv_connect_cb cb) {
- if (handle->type != UV_TCP || address.sin6_family != AF_INET6) {
+ if (handle->type != UV_TCP) {
uv__set_artificial_error(handle->loop, UV_EINVAL);
return -1;
}
- return uv__tcp_connect6(req, handle, address, cb);
+ return uv__tcp_connect(req, handle, addr, cb);
}
View
@@ -77,22 +77,15 @@ int uv__set_artificial_error(uv_loop_t* loop, uv_err_code code);
uv_err_t uv__new_sys_error(int sys_error);
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__tcp_bind(uv_tcp_t* handle, struct sockaddr* 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, struct sockaddr* addr, unsigned flags);
int uv__tcp_connect(uv_connect_t* req,
uv_tcp_t* handle,
- struct sockaddr_in address,
+ struct sockaddr* addr,
uv_connect_cb cb);
-int uv__tcp_connect6(uv_connect_t* req,
- uv_tcp_t* handle,
- struct sockaddr_in6 address,
- uv_connect_cb cb);
-
void uv__fs_poll_close(uv_fs_poll_t* handle);
View
@@ -105,7 +105,7 @@ HELPER_IMPL(tcp4_blackhole_server) {
r = uv_tcp_init(loop, &tcp_server);
ASSERT(r == 0);
- r = uv_tcp_bind(&tcp_server, addr);
+ r = uv_tcp_bind(&tcp_server, (struct sockaddr *)&addr);
ASSERT(r == 0);
r = uv_listen((uv_stream_t*)&tcp_server, 128, connection_cb);
View
@@ -225,7 +225,7 @@ static int tcp4_echo_start(int port) {
return 1;
}
- r = uv_tcp_bind(&tcpServer, addr);
+ r = uv_tcp_bind(&tcpServer, (struct sockaddr *)&addr);
if (r) {
/* TODO: Error codes */
fprintf(stderr, "Bind error\n");
@@ -259,7 +259,7 @@ static int tcp6_echo_start(int port) {
}
/* IPv6 is optional as not all platforms support it */
- r = uv_tcp_bind6(&tcpServer, addr6);
+ r = uv_tcp_bind(&tcpServer, (struct sockaddr *)&addr6);
if (r) {
/* show message but return OK */
fprintf(stderr, "IPv6 not supported\n");
@@ -184,7 +184,7 @@ TEST_IMPL(callback_stack) {
nested++;
- if (uv_tcp_connect(&connect_req, &client, addr, connect_cb)) {
+ if (uv_tcp_connect(&connect_req, &client, (struct sockaddr *)&addr, connect_cb)) {
FATAL("uv_tcp_connect failed");
}
nested--;
@@ -103,8 +103,8 @@ static void connection_fail(uv_connect_cb connect_cb) {
/* We are never doing multiple reads/connects at a time anyway. */
/* so these handles can be pre-initialized. */
- uv_tcp_bind(&tcp, client_addr);
- r = uv_tcp_connect(&req, &tcp, server_addr, connect_cb);
+ uv_tcp_bind(&tcp, (struct sockaddr *)&client_addr);
+ r = uv_tcp_connect(&req, &tcp, (struct sockaddr *)&server_addr, connect_cb);
ASSERT(!r);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
@@ -109,7 +109,7 @@ static void start_server(void) {
r = uv_tcp_init(uv_default_loop(), server);
ASSERT(r == 0);
- r = uv_tcp_bind(server, addr);
+ r = uv_tcp_bind(server, (struct sockaddr *)&addr);
ASSERT(r == 0);
r = uv_listen((uv_stream_t*)server, 128, connection_cb);
@@ -165,7 +165,7 @@ static void client_connect(void) {
r = uv_tcp_init(uv_default_loop(), client);
ASSERT(r == 0);
- r = uv_tcp_connect(connect_req, client, addr, connect_cb);
+ r = uv_tcp_connect(connect_req, client, (struct sockaddr *)&addr, connect_cb);
ASSERT(r == 0);
}
View
@@ -177,7 +177,7 @@ static int tcp_listener(void) {
return 1;
}
- r = uv_tcp_bind(&tcpServer, addr);
+ r = uv_tcp_bind(&tcpServer, (struct sockaddr *)&addr);
if (r) {
fprintf(stderr, "Bind error\n");
return 1;
@@ -215,7 +215,7 @@ static void tcp_connector(void) {
tcp.data = &connect_req;
ASSERT(!r);
- r = uv_tcp_connect(&connect_req, &tcp, server_addr, on_connect);
+ r = uv_tcp_connect(&connect_req, &tcp, (struct sockaddr *)&server_addr, on_connect);
ASSERT(!r);
/* Fetch the actual port used by the connecting socket. */
@@ -273,7 +273,7 @@ static int udp_listener(void) {
return 1;
}
- r = uv_udp_bind(&udpServer, addr, 0);
+ r = uv_udp_bind(&udpServer, (struct sockaddr *)&addr, 0);
if (r) {
fprintf(stderr, "Bind error\n");
return 1;
@@ -132,14 +132,15 @@ TEST_IMPL(ipc_send_recv_pipe) {
TEST_IMPL(ipc_send_recv_tcp) {
+ struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
int r;
ctx.expected_type = UV_TCP;
r = uv_tcp_init(uv_default_loop(), &ctx.send.tcp);
ASSERT(r == 0);
- r = uv_tcp_bind(&ctx.send.tcp, uv_ip4_addr("127.0.0.1", TEST_PORT));
+ r = uv_tcp_bind(&ctx.send.tcp, (struct sockaddr*)&addr);
ASSERT(r == 0);
r = run_test();
Oops, something went wrong.

0 comments on commit 1ca0eb5

Please sign in to comment.