mirror of https://github.com/lukechilds/node.git
62 changed files with 955 additions and 194 deletions
@ -0,0 +1,114 @@ |
|||
/* 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 <assert.h> |
|||
#include <stdlib.h> |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
|
|||
#include "uv.h" |
|||
#include "internal.h" |
|||
|
|||
|
|||
static void uv__getnameinfo_work(struct uv__work* w) { |
|||
uv_getnameinfo_t* req; |
|||
int err; |
|||
socklen_t salen; |
|||
|
|||
req = container_of(w, uv_getnameinfo_t, work_req); |
|||
|
|||
if (req->storage.ss_family == AF_INET) |
|||
salen = sizeof(struct sockaddr_in); |
|||
else if (req->storage.ss_family == AF_INET6) |
|||
salen = sizeof(struct sockaddr_in6); |
|||
else |
|||
abort(); |
|||
|
|||
err = getnameinfo((struct sockaddr*) &req->storage, |
|||
salen, |
|||
req->host, |
|||
sizeof(req->host), |
|||
req->service, |
|||
sizeof(req->service), |
|||
req->flags); |
|||
req->retcode = uv__getaddrinfo_translate_error(err); |
|||
} |
|||
|
|||
static void uv__getnameinfo_done(struct uv__work* w, int status) { |
|||
uv_getnameinfo_t* req; |
|||
char* host; |
|||
char* service; |
|||
|
|||
req = container_of(w, uv_getnameinfo_t, work_req); |
|||
uv__req_unregister(req->loop, req); |
|||
host = service = NULL; |
|||
|
|||
if (status == -ECANCELED) { |
|||
assert(req->retcode == 0); |
|||
req->retcode = UV_EAI_CANCELED; |
|||
} else if (req->retcode == 0) { |
|||
host = req->host; |
|||
service = req->service; |
|||
} |
|||
|
|||
req->getnameinfo_cb(req, req->retcode, host, service); |
|||
} |
|||
|
|||
/*
|
|||
* Entry point for getnameinfo |
|||
* return 0 if a callback will be made |
|||
* return error code if validation fails |
|||
*/ |
|||
int uv_getnameinfo(uv_loop_t* loop, |
|||
uv_getnameinfo_t* req, |
|||
uv_getnameinfo_cb getnameinfo_cb, |
|||
const struct sockaddr* addr, |
|||
int flags) { |
|||
if (req == NULL || getnameinfo_cb == NULL || addr == NULL) |
|||
return UV_EINVAL; |
|||
|
|||
if (addr->sa_family == AF_INET) { |
|||
memcpy(&req->storage, |
|||
addr, |
|||
sizeof(struct sockaddr_in)); |
|||
} else if (addr->sa_family == AF_INET6) { |
|||
memcpy(&req->storage, |
|||
addr, |
|||
sizeof(struct sockaddr_in6)); |
|||
} else { |
|||
return UV_EINVAL; |
|||
} |
|||
|
|||
uv__req_init(loop, (uv_req_t*)req, UV_GETNAMEINFO); |
|||
|
|||
req->getnameinfo_cb = getnameinfo_cb; |
|||
req->flags = flags; |
|||
req->type = UV_GETNAMEINFO; |
|||
req->loop = loop; |
|||
req->retcode = 0; |
|||
|
|||
uv__work_submit(loop, |
|||
&req->work_req, |
|||
uv__getnameinfo_work, |
|||
uv__getnameinfo_done); |
|||
|
|||
return 0; |
|||
} |
@ -0,0 +1,138 @@ |
|||
/* 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 <assert.h> |
|||
#include <malloc.h> |
|||
#include <stdio.h> |
|||
|
|||
#include "uv.h" |
|||
#include "internal.h" |
|||
#include "req-inl.h" |
|||
|
|||
|
|||
/* getnameinfo worker thread implementation */ |
|||
static DWORD WINAPI getnameinfo_thread_proc(void* parameter) { |
|||
uv_getnameinfo_t* req = (uv_getnameinfo_t*)parameter; |
|||
uv_loop_t* loop = req->loop; |
|||
WCHAR host[NI_MAXHOST]; |
|||
WCHAR service[NI_MAXSERV]; |
|||
int ret = 0; |
|||
|
|||
assert(req != NULL); |
|||
|
|||
ret = GetNameInfoW((struct sockaddr*)&req->storage, |
|||
sizeof(req->storage), |
|||
host, |
|||
sizeof(host), |
|||
service, |
|||
sizeof(service), |
|||
req->flags); |
|||
req->retcode = uv__getaddrinfo_translate_error(ret); |
|||
|
|||
/* convert results to UTF-8 */ |
|||
WideCharToMultiByte(CP_UTF8, |
|||
0, |
|||
host, |
|||
-1, |
|||
req->host, |
|||
sizeof(req->host), |
|||
NULL, |
|||
NULL); |
|||
|
|||
WideCharToMultiByte(CP_UTF8, |
|||
0, |
|||
service, |
|||
-1, |
|||
req->service, |
|||
sizeof(req->service), |
|||
NULL, |
|||
NULL); |
|||
|
|||
/* post getnameinfo completed */ |
|||
POST_COMPLETION_FOR_REQ(loop, req); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
|
|||
/*
|
|||
* Called from uv_run when complete. |
|||
*/ |
|||
void uv_process_getnameinfo_req(uv_loop_t* loop, uv_getnameinfo_t* req) { |
|||
char* host; |
|||
char* service; |
|||
|
|||
if (req->retcode == 0) { |
|||
host = req->host; |
|||
service = req->service; |
|||
} else { |
|||
host = NULL; |
|||
service = NULL; |
|||
} |
|||
|
|||
uv__req_unregister(loop, req); |
|||
req->getnameinfo_cb(req, req->retcode, host, service); |
|||
} |
|||
|
|||
|
|||
/*
|
|||
* Entry point for getnameinfo |
|||
* return 0 if a callback will be made |
|||
* return error code if validation fails |
|||
*/ |
|||
int uv_getnameinfo(uv_loop_t* loop, |
|||
uv_getnameinfo_t* req, |
|||
uv_getnameinfo_cb getnameinfo_cb, |
|||
const struct sockaddr* addr, |
|||
int flags) { |
|||
if (req == NULL || getnameinfo_cb == NULL || addr == NULL) |
|||
return UV_EINVAL; |
|||
|
|||
if (addr->sa_family == AF_INET) { |
|||
memcpy(&req->storage, |
|||
addr, |
|||
sizeof(struct sockaddr_in)); |
|||
} else if (addr->sa_family == AF_INET6) { |
|||
memcpy(&req->storage, |
|||
addr, |
|||
sizeof(struct sockaddr_in6)); |
|||
} else { |
|||
return UV_EINVAL; |
|||
} |
|||
|
|||
uv_req_init(loop, (uv_req_t*)req); |
|||
|
|||
req->getnameinfo_cb = getnameinfo_cb; |
|||
req->flags = flags; |
|||
req->type = UV_GETNAMEINFO; |
|||
req->loop = loop; |
|||
|
|||
/* Ask thread to run. Treat this as a long operation. */ |
|||
if (QueueUserWorkItem(&getnameinfo_thread_proc, |
|||
req, |
|||
WT_EXECUTELONGFUNCTION) == 0) { |
|||
return uv_translate_sys_error(GetLastError()); |
|||
} |
|||
|
|||
uv__req_register(loop, req); |
|||
|
|||
return 0; |
|||
} |
@ -0,0 +1,83 @@ |
|||
/* 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 <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
|
|||
|
|||
static const char* address_ip4 = "127.0.0.1"; |
|||
static const char* address_ip6 = "::1"; |
|||
static const int port = 80; |
|||
|
|||
static struct sockaddr_in addr4; |
|||
static struct sockaddr_in6 addr6; |
|||
static uv_getnameinfo_t req; |
|||
|
|||
static void getnameinfo_req(uv_getnameinfo_t* handle, |
|||
int status, |
|||
const char* hostname, |
|||
const char* service) { |
|||
ASSERT(handle != NULL); |
|||
ASSERT(status == 0); |
|||
ASSERT(hostname != NULL); |
|||
ASSERT(service != NULL); |
|||
} |
|||
|
|||
TEST_IMPL(getnameinfo_basic_ip4) { |
|||
int r; |
|||
|
|||
r = uv_ip4_addr(address_ip4, port, &addr4); |
|||
ASSERT(r == 0); |
|||
|
|||
r = uv_getnameinfo(uv_default_loop(), |
|||
&req, |
|||
&getnameinfo_req, |
|||
(const struct sockaddr*)&addr4, |
|||
0); |
|||
ASSERT(r == 0); |
|||
|
|||
uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
|||
|
|||
MAKE_VALGRIND_HAPPY(); |
|||
return 0; |
|||
} |
|||
|
|||
TEST_IMPL(getnameinfo_basic_ip6) { |
|||
int r; |
|||
|
|||
r = uv_ip6_addr(address_ip6, port, &addr6); |
|||
ASSERT(r == 0); |
|||
|
|||
r = uv_getnameinfo(uv_default_loop(), |
|||
&req, |
|||
&getnameinfo_req, |
|||
(const struct sockaddr*)&addr6, |
|||
0); |
|||
ASSERT(r == 0); |
|||
|
|||
uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
|||
|
|||
MAKE_VALGRIND_HAPPY(); |
|||
return 0; |
|||
} |
@ -0,0 +1,89 @@ |
|||
/* 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. |
|||
*/ |
|||
|
|||
#ifdef _WIN32 |
|||
|
|||
#include <errno.h> |
|||
|
|||
#include "uv.h" |
|||
#include "task.h" |
|||
|
|||
uv_os_sock_t sock; |
|||
uv_poll_t handle; |
|||
|
|||
static int close_cb_called = 0; |
|||
|
|||
|
|||
static void close_cb(uv_handle_t* h) { |
|||
close_cb_called++; |
|||
} |
|||
|
|||
|
|||
static void poll_cb(uv_poll_t* h, int status, int events) { |
|||
int r; |
|||
|
|||
ASSERT(status == 0); |
|||
ASSERT(h == &handle); |
|||
|
|||
r = uv_poll_start(&handle, UV_READABLE, poll_cb); |
|||
ASSERT(r == 0); |
|||
|
|||
closesocket(sock); |
|||
uv_close((uv_handle_t*) &handle, close_cb); |
|||
|
|||
} |
|||
|
|||
|
|||
TEST_IMPL(poll_closesocket) { |
|||
struct WSAData wsa_data; |
|||
int r; |
|||
unsigned long on; |
|||
struct sockaddr_in addr; |
|||
|
|||
r = WSAStartup(MAKEWORD(2, 2), &wsa_data); |
|||
ASSERT(r == 0); |
|||
|
|||
sock = socket(AF_INET, SOCK_STREAM, 0); |
|||
ASSERT(sock != INVALID_SOCKET); |
|||
on = 1; |
|||
r = ioctlsocket(sock, FIONBIO, &on); |
|||
ASSERT(r == 0); |
|||
|
|||
r = uv_ip4_addr("127.0.0.1", TEST_PORT, &addr); |
|||
ASSERT(r == 0); |
|||
|
|||
r = connect(sock, (const struct sockaddr*) &addr, sizeof addr); |
|||
ASSERT(r != 0); |
|||
ASSERT(WSAGetLastError() == WSAEWOULDBLOCK); |
|||
|
|||
r = uv_poll_init_socket(uv_default_loop(), &handle, sock); |
|||
ASSERT(r == 0); |
|||
r = uv_poll_start(&handle, UV_WRITABLE, poll_cb); |
|||
ASSERT(r == 0); |
|||
|
|||
uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
|||
|
|||
ASSERT(close_cb_called == 1); |
|||
|
|||
MAKE_VALGRIND_HAPPY(); |
|||
return 0; |
|||
} |
|||
#endif |
Loading…
Reference in new issue