Path: blob/master/Utilities/cmlibuv/src/win/getnameinfo.c
3153 views
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.1*2* Permission is hereby granted, free of charge, to any person obtaining a copy3* of this software and associated documentation files (the "Software"), to4* deal in the Software without restriction, including without limitation the5* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or6* sell copies of the Software, and to permit persons to whom the Software is7* furnished to do so, subject to the following conditions:8*9* The above copyright notice and this permission notice shall be included in10* all copies or substantial portions of the Software.11*12* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING17* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS18* IN THE SOFTWARE.19*/2021#include <assert.h>22#include <stdio.h>2324#include "uv.h"25#include "internal.h"26#include "req-inl.h"2728#ifndef GetNameInfo29int WSAAPI GetNameInfoW(30const SOCKADDR *pSockaddr,31socklen_t SockaddrLength,32PWCHAR pNodeBuffer,33DWORD NodeBufferSize,34PWCHAR pServiceBuffer,35DWORD ServiceBufferSize,36INT Flags37);38#endif3940static void uv__getnameinfo_work(struct uv__work* w) {41uv_getnameinfo_t* req;42WCHAR host[NI_MAXHOST];43WCHAR service[NI_MAXSERV];44int ret;4546req = container_of(w, uv_getnameinfo_t, work_req);47if (GetNameInfoW((struct sockaddr*)&req->storage,48sizeof(req->storage),49host,50ARRAY_SIZE(host),51service,52ARRAY_SIZE(service),53req->flags)) {54ret = WSAGetLastError();55req->retcode = uv__getaddrinfo_translate_error(ret);56return;57}5859ret = WideCharToMultiByte(CP_UTF8,600,61host,62-1,63req->host,64sizeof(req->host),65NULL,66NULL);67if (ret == 0) {68req->retcode = uv_translate_sys_error(GetLastError());69return;70}7172ret = WideCharToMultiByte(CP_UTF8,730,74service,75-1,76req->service,77sizeof(req->service),78NULL,79NULL);80if (ret == 0) {81req->retcode = uv_translate_sys_error(GetLastError());82}83}848586/*87* Called from uv_run when complete.88*/89static void uv__getnameinfo_done(struct uv__work* w, int status) {90uv_getnameinfo_t* req;91char* host;92char* service;9394req = container_of(w, uv_getnameinfo_t, work_req);95uv__req_unregister(req->loop, req);96host = service = NULL;9798if (status == UV_ECANCELED) {99assert(req->retcode == 0);100req->retcode = UV_EAI_CANCELED;101} else if (req->retcode == 0) {102host = req->host;103service = req->service;104}105106if (req->getnameinfo_cb)107req->getnameinfo_cb(req, req->retcode, host, service);108}109110111/*112* Entry point for getnameinfo113* return 0 if a callback will be made114* return error code if validation fails115*/116int uv_getnameinfo(uv_loop_t* loop,117uv_getnameinfo_t* req,118uv_getnameinfo_cb getnameinfo_cb,119const struct sockaddr* addr,120int flags) {121if (req == NULL || addr == NULL)122return UV_EINVAL;123124if (addr->sa_family == AF_INET) {125memcpy(&req->storage,126addr,127sizeof(struct sockaddr_in));128} else if (addr->sa_family == AF_INET6) {129memcpy(&req->storage,130addr,131sizeof(struct sockaddr_in6));132} else {133return UV_EINVAL;134}135136UV_REQ_INIT(req, UV_GETNAMEINFO);137uv__req_register(loop, req);138139req->getnameinfo_cb = getnameinfo_cb;140req->flags = flags;141req->loop = loop;142req->retcode = 0;143144if (getnameinfo_cb) {145uv__work_submit(loop,146&req->work_req,147UV__WORK_SLOW_IO,148uv__getnameinfo_work,149uv__getnameinfo_done);150return 0;151} else {152uv__getnameinfo_work(&req->work_req);153uv__getnameinfo_done(&req->work_req, 0);154return req->retcode;155}156}157158159