From cda7a280395108e0c84175822718e7436c8cb4f1 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Thu, 5 Apr 2012 16:47:49 +0200 Subject: [PATCH 1/2] Windows: backport pipe-connect-to-file fixes from master Conflicts: src/win/pipe.c commit e53ab6675ba12d97ad6d93c9913a473ba5172617 Author: Bert Belder Date: Fri Mar 9 17:04:03 2012 +0100 Windows: report UV_ENOTSOCK when we opened a file instead of a pipe Makes the pipe_connect_to_file test pass on Windows. commit 8cbbfbe4c6489868470a7e410f80d4729f4091bf Author: Igor Zinkovsky Date: Thu Mar 1 14:32:59 2012 -0800 test: make pipe_connect_to_file succeed with ECONNREFUSED commit 6bbccf1fe0e000fd73e945368466cd27291483e3 Author: Igor Zinkovsky Date: Thu Mar 1 12:11:12 2012 -0800 windows: return UV_ENOTSOCK when doing uv_pipe_connect to a file --- src/win/error.c | 1 + src/win/pipe.c | 5 +++++ test/test-list.h | 2 ++ test/test-pipe-connect-error.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/src/win/error.c b/src/win/error.c index dccd2e71..7bdc3cda 100644 --- a/src/win/error.c +++ b/src/win/error.c @@ -125,6 +125,7 @@ uv_err_code uv_translate_sys_error(int sys_errno) { case ERROR_SEM_TIMEOUT: return UV_ETIMEDOUT; case WSAETIMEDOUT: return UV_ETIMEDOUT; case WSAHOST_NOT_FOUND: return UV_ENOENT; + case WSAENOTSOCK: return UV_ENOTSOCK; default: return UV_UNKNOWN; } } diff --git a/src/win/pipe.c b/src/win/pipe.c index f99a32a9..83220a23 100644 --- a/src/win/pipe.c +++ b/src/win/pipe.c @@ -217,6 +217,11 @@ static int uv_set_pipe_handle(uv_loop_t* loop, uv_pipe_t* handle, DWORD mode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT; if (!SetNamedPipeHandleState(pipeHandle, &mode, NULL, NULL)) { + /* If this returns ERROR_INVALID_PARAMETER we probably opened something */ + /* that is not a pipe. */ + if (GetLastError() == ERROR_INVALID_PARAMETER) { + SetLastError(WSAENOTSOCK); + } return -1; } diff --git a/test/test-list.h b/test/test-list.h index 4c5e9f22..5aad6331 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -57,6 +57,7 @@ TEST_DECLARE (pipe_bind_error_addrnotavail) TEST_DECLARE (pipe_bind_error_inval) TEST_DECLARE (pipe_listen_without_bind) TEST_DECLARE (pipe_connect_bad_name) +TEST_DECLARE (pipe_connect_to_file) TEST_DECLARE (connection_fail) TEST_DECLARE (connection_fail_doesnt_auto_close) TEST_DECLARE (shutdown_close_tcp) @@ -155,6 +156,7 @@ HELPER_DECLARE (pipe_echo_server) TASK_LIST_START TEST_ENTRY (pipe_connect_bad_name) + TEST_ENTRY (pipe_connect_to_file) TEST_ENTRY (tty) TEST_ENTRY (stdio_over_pipes) diff --git a/test/test-pipe-connect-error.c b/test/test-pipe-connect-error.c index 2faa4461..4a6c5110 100644 --- a/test/test-pipe-connect-error.c +++ b/test/test-pipe-connect-error.c @@ -50,6 +50,15 @@ static void connect_cb(uv_connect_t* connect_req, int status) { } +static void connect_cb_file(uv_connect_t* connect_req, int status) { + ASSERT(status == -1); + ASSERT(uv_last_error(uv_default_loop()).code == UV_ENOTSOCK || + uv_last_error(uv_default_loop()).code == UV_ECONNREFUSED); + uv_close((uv_handle_t*)connect_req->handle, close_cb); + connect_cb_called++; +} + + TEST_IMPL(pipe_connect_bad_name) { uv_pipe_t client; uv_connect_t req; @@ -66,3 +75,22 @@ TEST_IMPL(pipe_connect_bad_name) { return 0; } + + +TEST_IMPL(pipe_connect_to_file) { + const char* path = "test/fixtures/empty_file"; + uv_pipe_t client; + uv_connect_t req; + int r; + + r = uv_pipe_init(uv_default_loop(), &client, 0); + ASSERT(r == 0); + uv_pipe_connect(&req, &client, path, connect_cb_file); + + uv_run(uv_default_loop()); + + ASSERT(close_cb_called == 1); + ASSERT(connect_cb_called == 1); + + return 0; +} \ No newline at end of file From 3506cd1dc5e941acb216b2118cfc784b78fcc4f1 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Thu, 5 Apr 2012 17:17:09 +0200 Subject: [PATCH 2/2] Windows: don't report ENOTSOCK when attempting to bind an udp handle twice --- src/win/tcp.c | 3 +-- src/win/udp.c | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/win/tcp.c b/src/win/tcp.c index 25ca26bd..71c5575f 100644 --- a/src/win/tcp.c +++ b/src/win/tcp.c @@ -239,10 +239,9 @@ static int uv__bind(uv_tcp_t* handle, int addrsize) { DWORD err; int r; - SOCKET sock; if (handle->socket == INVALID_SOCKET) { - sock = socket(domain, SOCK_STREAM, 0); + SOCKET sock = socket(domain, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { uv__set_sys_error(handle->loop, WSAGetLastError()); return -1; diff --git a/src/win/udp.c b/src/win/udp.c index 18ae4a2c..7c638e45 100644 --- a/src/win/udp.c +++ b/src/win/udp.c @@ -167,7 +167,6 @@ static int uv__bind(uv_udp_t* handle, int addrsize, unsigned int flags) { int r; - SOCKET sock; DWORD no = 0, yes = 1; if ((flags & UV_UDP_IPV6ONLY) && domain != AF_INET6) { @@ -177,7 +176,7 @@ static int uv__bind(uv_udp_t* handle, } if (handle->socket == INVALID_SOCKET) { - sock = socket(domain, SOCK_DGRAM, 0); + SOCKET sock = socket(domain, SOCK_DGRAM, 0); if (sock == INVALID_SOCKET) { uv__set_sys_error(handle->loop, WSAGetLastError()); return -1; @@ -196,14 +195,14 @@ static int uv__bind(uv_udp_t* handle, /* TODO: how to handle errors? This may fail if there is no ipv4 stack */ /* available, or when run on XP/2003 which have no support for dualstack */ /* sockets. For now we're silently ignoring the error. */ - setsockopt(sock, + setsockopt(handle->socket, IPPROTO_IPV6, IPV6_V6ONLY, (char*) &no, sizeof no); } - r = setsockopt(sock, + r = setsockopt(handle->socket, SOL_SOCKET, SO_REUSEADDR, (char*) &yes,