ext/libuv/src/win/getaddrinfo.c in uvrb-0.1.4 vs ext/libuv/src/win/getaddrinfo.c in uvrb-0.2.0

- old
+ new

@@ -54,30 +54,10 @@ /* adjust size value to be multiple of 4. Use to keep pointer aligned */ /* Do we need different versions of this for different architectures? */ #define ALIGNED_SIZE(X) ((((X) + 3) >> 2) << 2) -/* - * getaddrinfo error code mapping - * Falls back to uv_translate_sys_error if no match - */ -static uv_err_code uv_translate_eai_error(int eai_errno) { - switch (eai_errno) { - case ERROR_SUCCESS: return UV_OK; - case EAI_BADFLAGS: return UV_EBADF; - case EAI_FAIL: return UV_EFAULT; - case EAI_FAMILY: return UV_EAIFAMNOSUPPORT; - case EAI_MEMORY: return UV_ENOMEM; - case EAI_NONAME: return UV_ENOENT; - case EAI_AGAIN: return UV_EAGAIN; - case EAI_SERVICE: return UV_EAISERVICE; - case EAI_SOCKTYPE: return UV_EAISOCKTYPE; - default: return uv_translate_sys_error(eai_errno); - } -} - - /* getaddrinfo worker thread implementation */ static DWORD WINAPI getaddrinfo_thread_proc(void* parameter) { uv_getaddrinfo_t* req = (uv_getaddrinfo_t*) parameter; uv_loop_t* loop = req->loop; int ret; @@ -113,11 +93,11 @@ size_t addrinfo_struct_len = ALIGNED_SIZE(sizeof(struct addrinfo)); struct addrinfoW* addrinfow_ptr; struct addrinfo* addrinfo_ptr; char* alloc_ptr = NULL; char* cur_ptr = NULL; - int status = 0; + int err = 0; /* release input parameter memory */ if (req->alloc != NULL) { free(req->alloc); req->alloc = NULL; @@ -131,12 +111,12 @@ addrinfo_len += addrinfo_struct_len + ALIGNED_SIZE(addrinfow_ptr->ai_addrlen); if (addrinfow_ptr->ai_canonname != NULL) { name_len = uv_utf16_to_utf8(addrinfow_ptr->ai_canonname, -1, NULL, 0); if (name_len == 0) { - uv__set_sys_error(loop, GetLastError()); - status = -1; + /* FIXME(bnoordhuis) Retain GetLastError(). */ + err = UV_EAI_SYSTEM; goto complete; } addrinfo_len += ALIGNED_SIZE(name_len); } addrinfow_ptr = addrinfow_ptr->ai_next; @@ -197,17 +177,15 @@ if (addrinfow_ptr != NULL) { addrinfo_ptr->ai_next = (struct addrinfo*)cur_ptr; } } } else { - uv__set_artificial_error(loop, UV_ENOMEM); - status = -1; + err = UV_EAI_MEMORY; } } else { /* GetAddrInfo failed */ - uv__set_artificial_error(loop, uv_translate_eai_error(req->retcode)); - status = -1; + err = uv__getaddrinfo_translate_error(req->retcode); } /* return memory to system */ if (req->res != NULL) { FreeAddrInfoW(req->res); @@ -216,11 +194,11 @@ complete: uv__req_unregister(loop, req); /* finally do callback with converted result */ - req->getaddrinfo_cb(req, status, (struct addrinfo*)alloc_ptr); + req->getaddrinfo_cb(req, err, (struct addrinfo*)alloc_ptr); } void uv_freeaddrinfo(struct addrinfo* ai) { char* alloc_ptr = (char*)ai; @@ -236,11 +214,11 @@ * Entry point for getaddrinfo * we convert the UTF-8 strings to UNICODE * and save the UNICODE string pointers in the req * We also copy hints so that caller does not need to keep memory until the * callback. - * return UV_OK if a callback will be made + * return 0 if a callback will be made * return error code if validation fails * * To minimize allocation we calculate total size required, * and copy all structs and referenced strings into the one block. * Each size calculation is adjusted to avoid unaligned pointers. @@ -253,14 +231,15 @@ const struct addrinfo* hints) { int nodesize = 0; int servicesize = 0; int hintssize = 0; char* alloc_ptr = NULL; + int err; if (req == NULL || getaddrinfo_cb == NULL || (node == NULL && service == NULL)) { - uv__set_sys_error(loop, WSAEINVAL); + err = WSAEINVAL; goto error; } uv_req_init(loop, (uv_req_t*)req); @@ -271,31 +250,31 @@ /* calculate required memory size for all input values */ if (node != NULL) { nodesize = ALIGNED_SIZE(uv_utf8_to_utf16(node, NULL, 0) * sizeof(WCHAR)); if (nodesize == 0) { - uv__set_sys_error(loop, GetLastError()); + err = GetLastError(); goto error; } } if (service != NULL) { servicesize = ALIGNED_SIZE(uv_utf8_to_utf16(service, NULL, 0) * sizeof(WCHAR)); if (servicesize == 0) { - uv__set_sys_error(loop, GetLastError()); + err = GetLastError(); goto error; } } if (hints != NULL) { hintssize = ALIGNED_SIZE(sizeof(struct addrinfoW)); } /* allocate memory for inputs, and partition it as needed */ alloc_ptr = (char*)malloc(nodesize + servicesize + hintssize); if (!alloc_ptr) { - uv__set_sys_error(loop, WSAENOBUFS); + err = WSAENOBUFS; goto error; } /* save alloc_ptr now so we can free if error */ req->alloc = (void*)alloc_ptr; @@ -305,11 +284,11 @@ if (node != NULL) { req->node = (WCHAR*)alloc_ptr; if (uv_utf8_to_utf16(node, (WCHAR*) alloc_ptr, nodesize / sizeof(WCHAR)) == 0) { - uv__set_sys_error(loop, GetLastError()); + err = GetLastError(); goto error; } alloc_ptr += nodesize; } else { req->node = NULL; @@ -320,11 +299,11 @@ if (service != NULL) { req->service = (WCHAR*)alloc_ptr; if (uv_utf8_to_utf16(service, (WCHAR*) alloc_ptr, servicesize / sizeof(WCHAR)) == 0) { - uv__set_sys_error(loop, GetLastError()); + err = GetLastError(); goto error; } alloc_ptr += servicesize; } else { req->service = NULL; @@ -347,11 +326,11 @@ /* Ask thread to run. Treat this as a long operation */ if (QueueUserWorkItem(&getaddrinfo_thread_proc, req, WT_EXECUTELONGFUNCTION) == 0) { - uv__set_sys_error(loop, GetLastError()); + err = GetLastError(); goto error; } uv__req_register(loop, req); @@ -359,7 +338,7 @@ error: if (req != NULL && req->alloc != NULL) { free(req->alloc); } - return -1; + return uv_translate_sys_error(err); }