unix: move getaddrinfo code to getaddrinfo.c
This commit is contained in:
parent
4f8e2a8e8e
commit
7ac23ee0c6
@ -32,6 +32,7 @@ OBJS += src/unix/core.o
|
||||
OBJS += src/unix/dl.o
|
||||
OBJS += src/unix/error.o
|
||||
OBJS += src/unix/fs.o
|
||||
OBJS += src/unix/getaddrinfo.o
|
||||
OBJS += src/unix/loop.o
|
||||
OBJS += src/unix/loop-watcher.o
|
||||
OBJS += src/unix/pipe.o
|
||||
|
||||
128
src/unix/core.c
128
src/unix/core.c
@ -299,134 +299,6 @@ int uv_is_active(const uv_handle_t* handle) {
|
||||
}
|
||||
|
||||
|
||||
static int uv_getaddrinfo_done(eio_req* req_) {
|
||||
uv_getaddrinfo_t* req = req_->data;
|
||||
struct addrinfo *res = req->res;
|
||||
#if __sun
|
||||
size_t hostlen = strlen(req->hostname);
|
||||
#endif
|
||||
|
||||
req->res = NULL;
|
||||
|
||||
uv__req_unregister(req->loop, req);
|
||||
|
||||
/* see initialization in uv_getaddrinfo() */
|
||||
if (req->hints)
|
||||
free(req->hints);
|
||||
else if (req->service)
|
||||
free(req->service);
|
||||
else if (req->hostname)
|
||||
free(req->hostname);
|
||||
else
|
||||
assert(0);
|
||||
|
||||
if (req->retcode == 0) {
|
||||
/* OK */
|
||||
#if EAI_NODATA /* FreeBSD deprecated EAI_NODATA */
|
||||
} else if (req->retcode == EAI_NONAME || req->retcode == EAI_NODATA) {
|
||||
#else
|
||||
} else if (req->retcode == EAI_NONAME) {
|
||||
#endif
|
||||
uv__set_sys_error(req->loop, ENOENT); /* FIXME compatibility hack */
|
||||
#if __sun
|
||||
} else if (req->retcode == EAI_MEMORY && hostlen >= MAXHOSTNAMELEN) {
|
||||
uv__set_sys_error(req->loop, ENOENT);
|
||||
#endif
|
||||
} else {
|
||||
req->loop->last_err.code = UV_EADDRINFO;
|
||||
req->loop->last_err.sys_errno_ = req->retcode;
|
||||
}
|
||||
|
||||
req->cb(req, req->retcode, res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void getaddrinfo_thread_proc(eio_req *req_) {
|
||||
uv_getaddrinfo_t* req = req_->data;
|
||||
|
||||
req->retcode = getaddrinfo(req->hostname,
|
||||
req->service,
|
||||
req->hints,
|
||||
&req->res);
|
||||
}
|
||||
|
||||
|
||||
int uv_getaddrinfo(uv_loop_t* loop,
|
||||
uv_getaddrinfo_t* req,
|
||||
uv_getaddrinfo_cb cb,
|
||||
const char* hostname,
|
||||
const char* service,
|
||||
const struct addrinfo* hints) {
|
||||
size_t hostname_len;
|
||||
size_t service_len;
|
||||
size_t hints_len;
|
||||
eio_req* req_;
|
||||
size_t len;
|
||||
char* buf;
|
||||
|
||||
if (req == NULL || cb == NULL || (hostname == NULL && service == NULL))
|
||||
return uv__set_artificial_error(loop, UV_EINVAL);
|
||||
|
||||
uv_eio_init(loop);
|
||||
|
||||
hostname_len = hostname ? strlen(hostname) + 1 : 0;
|
||||
service_len = service ? strlen(service) + 1 : 0;
|
||||
hints_len = hints ? sizeof(*hints) : 0;
|
||||
buf = malloc(hostname_len + service_len + hints_len);
|
||||
|
||||
if (buf == NULL)
|
||||
return uv__set_artificial_error(loop, UV_ENOMEM);
|
||||
|
||||
uv__req_init(loop, req, UV_GETADDRINFO);
|
||||
req->loop = loop;
|
||||
req->cb = cb;
|
||||
req->res = NULL;
|
||||
req->hints = NULL;
|
||||
req->service = NULL;
|
||||
req->hostname = NULL;
|
||||
req->retcode = 0;
|
||||
|
||||
/* order matters, see uv_getaddrinfo_done() */
|
||||
len = 0;
|
||||
|
||||
if (hints) {
|
||||
req->hints = memcpy(buf + len, hints, sizeof(*hints));
|
||||
len += sizeof(*hints);
|
||||
}
|
||||
|
||||
if (service) {
|
||||
req->service = memcpy(buf + len, service, service_len);
|
||||
len += service_len;
|
||||
}
|
||||
|
||||
if (hostname) {
|
||||
req->hostname = memcpy(buf + len, hostname, hostname_len);
|
||||
len += hostname_len;
|
||||
}
|
||||
|
||||
req_ = eio_custom(getaddrinfo_thread_proc,
|
||||
EIO_PRI_DEFAULT,
|
||||
uv_getaddrinfo_done,
|
||||
req,
|
||||
&loop->uv_eio_channel);
|
||||
|
||||
if (req_)
|
||||
return 0;
|
||||
|
||||
free(buf);
|
||||
|
||||
return uv__set_artificial_error(loop, UV_ENOMEM);
|
||||
}
|
||||
|
||||
|
||||
void uv_freeaddrinfo(struct addrinfo* ai) {
|
||||
if (ai)
|
||||
freeaddrinfo(ai);
|
||||
}
|
||||
|
||||
|
||||
/* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
|
||||
int uv__socket(int domain, int type, int protocol) {
|
||||
int sockfd;
|
||||
|
||||
155
src/unix/getaddrinfo.c
Normal file
155
src/unix/getaddrinfo.c
Normal file
@ -0,0 +1,155 @@
|
||||
/* 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 "internal.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h> /* NULL */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
static void uv__getaddrinfo_work(eio_req *req_) {
|
||||
uv_getaddrinfo_t* req = req_->data;
|
||||
|
||||
req->retcode = getaddrinfo(req->hostname,
|
||||
req->service,
|
||||
req->hints,
|
||||
&req->res);
|
||||
}
|
||||
|
||||
|
||||
static int uv__getaddrinfo_done(eio_req* req_) {
|
||||
uv_getaddrinfo_t* req = req_->data;
|
||||
struct addrinfo *res = req->res;
|
||||
#if __sun
|
||||
size_t hostlen = strlen(req->hostname);
|
||||
#endif
|
||||
|
||||
req->res = NULL;
|
||||
|
||||
uv__req_unregister(req->loop, req);
|
||||
|
||||
/* see initialization in uv_getaddrinfo() */
|
||||
if (req->hints)
|
||||
free(req->hints);
|
||||
else if (req->service)
|
||||
free(req->service);
|
||||
else if (req->hostname)
|
||||
free(req->hostname);
|
||||
else
|
||||
assert(0);
|
||||
|
||||
if (req->retcode == 0) {
|
||||
/* OK */
|
||||
#if EAI_NODATA /* FreeBSD deprecated EAI_NODATA */
|
||||
} else if (req->retcode == EAI_NONAME || req->retcode == EAI_NODATA) {
|
||||
#else
|
||||
} else if (req->retcode == EAI_NONAME) {
|
||||
#endif
|
||||
uv__set_sys_error(req->loop, ENOENT); /* FIXME compatibility hack */
|
||||
#if __sun
|
||||
} else if (req->retcode == EAI_MEMORY && hostlen >= MAXHOSTNAMELEN) {
|
||||
uv__set_sys_error(req->loop, ENOENT);
|
||||
#endif
|
||||
} else {
|
||||
req->loop->last_err.code = UV_EADDRINFO;
|
||||
req->loop->last_err.sys_errno_ = req->retcode;
|
||||
}
|
||||
|
||||
req->cb(req, req->retcode, res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int uv_getaddrinfo(uv_loop_t* loop,
|
||||
uv_getaddrinfo_t* req,
|
||||
uv_getaddrinfo_cb cb,
|
||||
const char* hostname,
|
||||
const char* service,
|
||||
const struct addrinfo* hints) {
|
||||
size_t hostname_len;
|
||||
size_t service_len;
|
||||
size_t hints_len;
|
||||
eio_req* req_;
|
||||
size_t len;
|
||||
char* buf;
|
||||
|
||||
if (req == NULL || cb == NULL || (hostname == NULL && service == NULL))
|
||||
return uv__set_artificial_error(loop, UV_EINVAL);
|
||||
|
||||
uv_eio_init(loop);
|
||||
|
||||
hostname_len = hostname ? strlen(hostname) + 1 : 0;
|
||||
service_len = service ? strlen(service) + 1 : 0;
|
||||
hints_len = hints ? sizeof(*hints) : 0;
|
||||
buf = malloc(hostname_len + service_len + hints_len);
|
||||
|
||||
if (buf == NULL)
|
||||
return uv__set_artificial_error(loop, UV_ENOMEM);
|
||||
|
||||
uv__req_init(loop, req, UV_GETADDRINFO);
|
||||
req->loop = loop;
|
||||
req->cb = cb;
|
||||
req->res = NULL;
|
||||
req->hints = NULL;
|
||||
req->service = NULL;
|
||||
req->hostname = NULL;
|
||||
req->retcode = 0;
|
||||
|
||||
/* order matters, see uv_getaddrinfo_done() */
|
||||
len = 0;
|
||||
|
||||
if (hints) {
|
||||
req->hints = memcpy(buf + len, hints, sizeof(*hints));
|
||||
len += sizeof(*hints);
|
||||
}
|
||||
|
||||
if (service) {
|
||||
req->service = memcpy(buf + len, service, service_len);
|
||||
len += service_len;
|
||||
}
|
||||
|
||||
if (hostname) {
|
||||
req->hostname = memcpy(buf + len, hostname, hostname_len);
|
||||
len += hostname_len;
|
||||
}
|
||||
|
||||
req_ = eio_custom(uv__getaddrinfo_work,
|
||||
EIO_PRI_DEFAULT,
|
||||
uv__getaddrinfo_done,
|
||||
req,
|
||||
&loop->uv_eio_channel);
|
||||
|
||||
if (req_)
|
||||
return 0;
|
||||
|
||||
free(buf);
|
||||
|
||||
return uv__set_artificial_error(loop, UV_ENOMEM);
|
||||
}
|
||||
|
||||
|
||||
void uv_freeaddrinfo(struct addrinfo* ai) {
|
||||
if (ai)
|
||||
freeaddrinfo(ai);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user