Path: blob/master/Utilities/cmlibuv/src/unix/getaddrinfo.c
3156 views
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.1* Permission is hereby granted, free of charge, to any person obtaining a copy2* of this software and associated documentation files (the "Software"), to3* deal in the Software without restriction, including without limitation the4* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or5* sell copies of the Software, and to permit persons to whom the Software is6* furnished to do so, subject to the following conditions:7*8* The above copyright notice and this permission notice shall be included in9* all copies or substantial portions of the Software.10*11* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR12* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,13* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE14* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER15* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING16* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS17* IN THE SOFTWARE.18*/1920/* Expose glibc-specific EAI_* error codes. Needs to be defined before we21* include any headers.22*/2324#include "uv.h"25#include "internal.h"26#include "idna.h"2728#include <errno.h>29#include <stddef.h> /* NULL */30#include <stdlib.h>31#include <string.h>32#include <net/if.h> /* if_indextoname() */3334/* EAI_* constants. */35#include <netdb.h>363738int uv__getaddrinfo_translate_error(int sys_err) {39switch (sys_err) {40case 0: return 0;41#if defined(EAI_ADDRFAMILY)42case EAI_ADDRFAMILY: return UV_EAI_ADDRFAMILY;43#endif44#if defined(EAI_AGAIN)45case EAI_AGAIN: return UV_EAI_AGAIN;46#endif47#if defined(EAI_BADFLAGS)48case EAI_BADFLAGS: return UV_EAI_BADFLAGS;49#endif50#if defined(EAI_BADHINTS)51case EAI_BADHINTS: return UV_EAI_BADHINTS;52#endif53#if defined(EAI_CANCELED)54case EAI_CANCELED: return UV_EAI_CANCELED;55#endif56#if defined(EAI_FAIL)57case EAI_FAIL: return UV_EAI_FAIL;58#endif59#if defined(EAI_FAMILY)60case EAI_FAMILY: return UV_EAI_FAMILY;61#endif62#if defined(EAI_MEMORY)63case EAI_MEMORY: return UV_EAI_MEMORY;64#endif65#if defined(EAI_NODATA)66case EAI_NODATA: return UV_EAI_NODATA;67#endif68#if defined(EAI_NONAME)69# if !defined(EAI_NODATA) || EAI_NODATA != EAI_NONAME70case EAI_NONAME: return UV_EAI_NONAME;71# endif72#endif73#if defined(EAI_OVERFLOW)74case EAI_OVERFLOW: return UV_EAI_OVERFLOW;75#endif76#if defined(EAI_PROTOCOL)77case EAI_PROTOCOL: return UV_EAI_PROTOCOL;78#endif79#if defined(EAI_SERVICE)80case EAI_SERVICE: return UV_EAI_SERVICE;81#endif82#if defined(EAI_SOCKTYPE)83case EAI_SOCKTYPE: return UV_EAI_SOCKTYPE;84#endif85#if defined(EAI_SYSTEM)86case EAI_SYSTEM: return UV__ERR(errno);87#endif88}89assert(!"unknown EAI_* error code");90abort();91#ifndef __SUNPRO_C92return 0; /* Pacify compiler. */93#endif94}959697static void uv__getaddrinfo_work(struct uv__work* w) {98uv_getaddrinfo_t* req;99int err;100101req = container_of(w, uv_getaddrinfo_t, work_req);102err = getaddrinfo(req->hostname, req->service, req->hints, &req->addrinfo);103req->retcode = uv__getaddrinfo_translate_error(err);104}105106107static void uv__getaddrinfo_done(struct uv__work* w, int status) {108uv_getaddrinfo_t* req;109110req = container_of(w, uv_getaddrinfo_t, work_req);111uv__req_unregister(req->loop, req);112113/* See initialization in uv_getaddrinfo(). */114if (req->hints)115uv__free(req->hints);116else if (req->service)117uv__free(req->service);118else if (req->hostname)119uv__free(req->hostname);120else121assert(0);122123req->hints = NULL;124req->service = NULL;125req->hostname = NULL;126127if (status == UV_ECANCELED) {128assert(req->retcode == 0);129req->retcode = UV_EAI_CANCELED;130}131132if (req->cb)133req->cb(req, req->retcode, req->addrinfo);134}135136137int uv_getaddrinfo(uv_loop_t* loop,138uv_getaddrinfo_t* req,139uv_getaddrinfo_cb cb,140const char* hostname,141const char* service,142const struct addrinfo* hints) {143char hostname_ascii[256];144size_t hostname_len;145size_t service_len;146size_t hints_len;147size_t len;148char* buf;149long rc;150151if (req == NULL || (hostname == NULL && service == NULL))152return UV_EINVAL;153154/* FIXME(bnoordhuis) IDNA does not seem to work z/OS,155* probably because it uses EBCDIC rather than ASCII.156*/157#ifdef __MVS__158(void) &hostname_ascii;159#else160if (hostname != NULL) {161rc = uv__idna_toascii(hostname,162hostname + strlen(hostname),163hostname_ascii,164hostname_ascii + sizeof(hostname_ascii));165if (rc < 0)166return rc;167hostname = hostname_ascii;168}169#endif170171hostname_len = hostname ? strlen(hostname) + 1 : 0;172service_len = service ? strlen(service) + 1 : 0;173hints_len = hints ? sizeof(*hints) : 0;174buf = uv__malloc(hostname_len + service_len + hints_len);175176if (buf == NULL)177return UV_ENOMEM;178179uv__req_init(loop, req, UV_GETADDRINFO);180req->loop = loop;181req->cb = cb;182req->addrinfo = NULL;183req->hints = NULL;184req->service = NULL;185req->hostname = NULL;186req->retcode = 0;187188/* order matters, see uv_getaddrinfo_done() */189len = 0;190191if (hints) {192req->hints = memcpy(buf + len, hints, sizeof(*hints));193len += sizeof(*hints);194}195196if (service) {197req->service = memcpy(buf + len, service, service_len);198len += service_len;199}200201if (hostname)202req->hostname = memcpy(buf + len, hostname, hostname_len);203204if (cb) {205uv__work_submit(loop,206&req->work_req,207UV__WORK_SLOW_IO,208uv__getaddrinfo_work,209uv__getaddrinfo_done);210return 0;211} else {212uv__getaddrinfo_work(&req->work_req);213uv__getaddrinfo_done(&req->work_req, 0);214return req->retcode;215}216}217218219void uv_freeaddrinfo(struct addrinfo* ai) {220if (ai)221freeaddrinfo(ai);222}223224225int uv_if_indextoname(unsigned int ifindex, char* buffer, size_t* size) {226char ifname_buf[UV_IF_NAMESIZE];227size_t len;228229if (buffer == NULL || size == NULL || *size == 0)230return UV_EINVAL;231232if (if_indextoname(ifindex, ifname_buf) == NULL)233return UV__ERR(errno);234235len = strnlen(ifname_buf, sizeof(ifname_buf));236237if (*size <= len) {238*size = len + 1;239return UV_ENOBUFS;240}241242memcpy(buffer, ifname_buf, len);243buffer[len] = '\0';244*size = len;245246return 0;247}248249int uv_if_indextoiid(unsigned int ifindex, char* buffer, size_t* size) {250return uv_if_indextoname(ifindex, buffer, size);251}252253254