diff --git a/Makefile.am b/Makefile.am index 861b632b..420c526a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -177,6 +177,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \ test/test-shutdown-twice.c \ test/test-signal-multiple-loops.c \ test/test-signal.c \ + test/test-socket-buffer-size.c \ test/test-spawn.c \ test/test-stdio-over-pipes.c \ test/test-tcp-bind-error.c \ diff --git a/include/uv.h b/include/uv.h index df6d9549..649758de 100644 --- a/include/uv.h +++ b/include/uv.h @@ -602,6 +602,30 @@ UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg); */ UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb); +/* + * Returns or sets the size of the receive buffer that the operating + * system uses for the socket. + * + * If *value == 0, it will return the current receive buffer size, + * otherwise it will use *value to set the new receive buffer size. + * + * NOTE: linux will set double the size and return double the size + * of the original set value. + */ +UV_EXTERN int uv_recv_buffer_size(uv_handle_t* handle, int* value); + +/* + * Returns or sets the size of the send buffer that the operating + * system uses for the socket. + * + * If *value == 0, it will return the current send buffer size, + * otherwise it will use *value to set the new send buffer size. + * + * NOTE: linux will set double the size and return double the size + * of the original set value. + */ +UV_EXTERN int uv_send_buffer_size(uv_handle_t* handle, int* value); + /* * Constructor for uv_buf_t. diff --git a/src/unix/core.c b/src/unix/core.c index 4770d8d8..aaced4c9 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -163,6 +163,33 @@ void uv_close(uv_handle_t* handle, uv_close_cb close_cb) { uv__make_close_pending(handle); } +int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value) { + int r; + int fd; + socklen_t len; + + if (handle == NULL || value == NULL) + return -EINVAL; + + if (handle->type == UV_TCP || handle->type == UV_NAMED_PIPE) + fd = uv__stream_fd((uv_stream_t*) handle); + else if (handle->type == UV_UDP) + fd = ((uv_udp_t *) handle)->io_watcher.fd; + else + return -ENOTSUP; + + len = sizeof(*value); + + if (*value == 0) + r = getsockopt(fd, SOL_SOCKET, optname, value, &len); + else + r = setsockopt(fd, SOL_SOCKET, optname, (const void*) value, len); + + if (r < 0) + return -errno; + + return 0; +} void uv__make_close_pending(uv_handle_t* handle) { assert(handle->flags & UV_CLOSING); diff --git a/src/uv-common.c b/src/uv-common.c index 86f45a56..e5475acd 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -422,6 +422,13 @@ size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs) { return bytes; } +int uv_recv_buffer_size(uv_handle_t* handle, int* value) { + return uv__socket_sockopt(handle, SO_RCVBUF, value); +} + +int uv_send_buffer_size(uv_handle_t* handle, int *value) { + return uv__socket_sockopt(handle, SO_SNDBUF, value); +} int uv_fs_event_getpath(uv_fs_event_t* handle, char* buf, size_t* len) { size_t required_len; diff --git a/src/uv-common.h b/src/uv-common.h index 34c28789..e7af5e9b 100644 --- a/src/uv-common.h +++ b/src/uv-common.h @@ -107,6 +107,8 @@ void uv__work_done(uv_async_t* handle); size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs); +int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value); + #define uv__has_active_reqs(loop) \ (QUEUE_EMPTY(&(loop)->active_reqs) == 0) diff --git a/src/win/core.c b/src/win/core.c index c3959756..5c3c6390 100644 --- a/src/win/core.c +++ b/src/win/core.c @@ -428,3 +428,31 @@ int uv_run(uv_loop_t *loop, uv_run_mode mode) { return r; } + +int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value) { + int r; + int len; + SOCKET socket; + + if (handle == NULL || value == NULL) + return UV_EINVAL; + + if (handle->type == UV_TCP) + socket = ((uv_tcp_t*) handle)->socket; + else if (handle->type == UV_UDP) + socket = ((uv_udp_t*) handle)->socket; + else + return UV_ENOTSUP; + + len = sizeof(*value); + + if (*value == 0) + r = getsockopt(socket, SOL_SOCKET, optname, (char*) value, &len); + else + r = setsockopt(socket, SOL_SOCKET, optname, (const char*) value, len); + + if (r == SOCKET_ERROR) + return uv_translate_sys_error(WSAGetLastError()); + + return 0; +} diff --git a/test/test-list.h b/test/test-list.h index 0ce4b4a6..de8d340c 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -176,6 +176,7 @@ TEST_DECLARE (getsockname_tcp) TEST_DECLARE (getsockname_udp) TEST_DECLARE (fail_always) TEST_DECLARE (pass_always) +TEST_DECLARE (socket_buffer_size) TEST_DECLARE (spawn_fails) TEST_DECLARE (spawn_exit_code) TEST_DECLARE (spawn_stdout) @@ -506,6 +507,8 @@ TASK_LIST_START TEST_ENTRY (poll_unidirectional) TEST_ENTRY (poll_close) + TEST_ENTRY (socket_buffer_size) + TEST_ENTRY (spawn_fails) TEST_ENTRY (spawn_exit_code) TEST_ENTRY (spawn_stdout) diff --git a/test/test-socket-buffer-size.c b/test/test-socket-buffer-size.c new file mode 100644 index 00000000..72f8c252 --- /dev/null +++ b/test/test-socket-buffer-size.c @@ -0,0 +1,77 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" +#include "task.h" + +#include +#include +#include + +static uv_udp_t udp; +static uv_tcp_t tcp; +static int close_cb_called; + + +static void close_cb(uv_handle_t* handle) { + close_cb_called++; +} + + +static void check_buffer_size(uv_handle_t* handle) { + int value; + + value = 0; + ASSERT(0 == uv_recv_buffer_size(handle, &value)); + ASSERT(value > 0); + + value = 10000; + ASSERT(0 == uv_recv_buffer_size(handle, &value)); + + value = 0; + ASSERT(0 == uv_recv_buffer_size(handle, &value)); + /* linux sets double the value */ + ASSERT(value == 10000 || value == 20000); +} + + +TEST_IMPL(socket_buffer_size) { + struct sockaddr_in addr; + + ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); + + ASSERT(0 == uv_tcp_init(uv_default_loop(), &tcp)); + ASSERT(0 == uv_tcp_bind(&tcp, (struct sockaddr*) &addr, 0)); + check_buffer_size((uv_handle_t*) &tcp); + uv_close((uv_handle_t*) &tcp, close_cb); + + ASSERT(0 == uv_udp_init(uv_default_loop(), &udp)); + ASSERT(0 == uv_udp_bind(&udp, (struct sockaddr*) &addr, 0)); + check_buffer_size((uv_handle_t*) &udp); + uv_close((uv_handle_t*) &udp, close_cb); + + ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); + + ASSERT(close_cb_called == 2); + + MAKE_VALGRIND_HAPPY(); + return 0; +} diff --git a/uv.gyp b/uv.gyp index 5b4d69a9..f32f8b42 100644 --- a/uv.gyp +++ b/uv.gyp @@ -360,6 +360,7 @@ 'test/test-shutdown-twice.c', 'test/test-signal.c', 'test/test-signal-multiple-loops.c', + 'test/test-socket-buffer-size.c', 'test/test-spawn.c', 'test/test-fs-poll.c', 'test/test-stdio-over-pipes.c',