2011-05-19 21:04:54 +00:00
|
|
|
/* 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"
|
2011-06-28 15:02:50 +00:00
|
|
|
#include "uv-common.h"
|
2011-05-19 21:04:54 +00:00
|
|
|
|
2012-06-06 02:24:33 +00:00
|
|
|
#include <stdio.h>
|
2011-05-19 21:04:54 +00:00
|
|
|
#include <assert.h>
|
2014-11-27 07:43:48 +00:00
|
|
|
#include <stdarg.h>
|
2011-05-19 22:03:21 +00:00
|
|
|
#include <stddef.h> /* NULL */
|
2011-11-21 15:12:36 +00:00
|
|
|
#include <stdlib.h> /* malloc */
|
2011-06-17 16:56:43 +00:00
|
|
|
#include <string.h> /* memset */
|
2011-05-19 21:04:54 +00:00
|
|
|
|
2015-06-10 13:29:48 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
# include <malloc.h> /* malloc */
|
|
|
|
|
#else
|
2013-03-14 21:20:32 +00:00
|
|
|
# include <net/if.h> /* if_nametoindex */
|
|
|
|
|
#endif
|
2011-06-27 04:34:04 +00:00
|
|
|
|
2015-05-26 08:51:10 +00:00
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
uv_malloc_func local_malloc;
|
|
|
|
|
uv_realloc_func local_realloc;
|
|
|
|
|
uv_calloc_func local_calloc;
|
|
|
|
|
uv_free_func local_free;
|
|
|
|
|
} uv__allocator_t;
|
|
|
|
|
|
|
|
|
|
static uv__allocator_t uv__allocator = {
|
|
|
|
|
malloc,
|
|
|
|
|
realloc,
|
|
|
|
|
calloc,
|
|
|
|
|
free,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
char* uv__strdup(const char* s) {
|
|
|
|
|
size_t len = strlen(s) + 1;
|
|
|
|
|
char* m = uv__malloc(len);
|
|
|
|
|
if (m == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
return memcpy(m, s, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* uv__strndup(const char* s, size_t n) {
|
|
|
|
|
char* m;
|
|
|
|
|
size_t len = strlen(s);
|
|
|
|
|
if (n < len)
|
|
|
|
|
len = n;
|
|
|
|
|
m = uv__malloc(len + 1);
|
|
|
|
|
if (m == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
m[len] = '\0';
|
|
|
|
|
return memcpy(m, s, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* uv__malloc(size_t size) {
|
|
|
|
|
return uv__allocator.local_malloc(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void uv__free(void* ptr) {
|
|
|
|
|
uv__allocator.local_free(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* uv__calloc(size_t count, size_t size) {
|
|
|
|
|
return uv__allocator.local_calloc(count, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* uv__realloc(void* ptr, size_t size) {
|
|
|
|
|
return uv__allocator.local_realloc(ptr, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int uv_replace_allocator(uv_malloc_func malloc_func,
|
|
|
|
|
uv_realloc_func realloc_func,
|
|
|
|
|
uv_calloc_func calloc_func,
|
|
|
|
|
uv_free_func free_func) {
|
|
|
|
|
if (malloc_func == NULL || realloc_func == NULL ||
|
|
|
|
|
calloc_func == NULL || free_func == NULL) {
|
|
|
|
|
return UV_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uv__allocator.local_malloc = malloc_func;
|
|
|
|
|
uv__allocator.local_realloc = realloc_func;
|
|
|
|
|
uv__allocator.local_calloc = calloc_func;
|
|
|
|
|
uv__allocator.local_free = free_func;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-04 23:39:20 +00:00
|
|
|
#define XX(uc, lc) case UV_##uc: return sizeof(uv_##lc##_t);
|
|
|
|
|
|
|
|
|
|
size_t uv_handle_size(uv_handle_type type) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
UV_HANDLE_TYPE_MAP(XX)
|
|
|
|
|
default:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t uv_req_size(uv_req_type type) {
|
|
|
|
|
switch(type) {
|
|
|
|
|
UV_REQ_TYPE_MAP(XX)
|
|
|
|
|
default:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef XX
|
2011-05-19 21:04:54 +00:00
|
|
|
|
2011-11-23 16:24:36 +00:00
|
|
|
|
2014-06-23 14:13:21 +00:00
|
|
|
size_t uv_loop_size(void) {
|
|
|
|
|
return sizeof(uv_loop_t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-06-04 16:00:39 +00:00
|
|
|
uv_buf_t uv_buf_init(char* base, unsigned int len) {
|
2011-08-18 00:38:39 +00:00
|
|
|
uv_buf_t buf;
|
|
|
|
|
buf.base = base;
|
|
|
|
|
buf.len = len;
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-06 21:10:50 +00:00
|
|
|
#define UV_ERR_NAME_GEN(name, _) case UV_ ## name: return #name;
|
|
|
|
|
const char* uv_err_name(int err) {
|
|
|
|
|
switch (err) {
|
2011-11-10 01:31:53 +00:00
|
|
|
UV_ERRNO_MAP(UV_ERR_NAME_GEN)
|
2011-05-19 21:04:54 +00:00
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-11-10 01:31:53 +00:00
|
|
|
#undef UV_ERR_NAME_GEN
|
2011-06-17 16:56:43 +00:00
|
|
|
|
|
|
|
|
|
2013-06-06 21:10:50 +00:00
|
|
|
#define UV_STRERROR_GEN(name, msg) case UV_ ## name: return msg;
|
|
|
|
|
const char* uv_strerror(int err) {
|
|
|
|
|
switch (err) {
|
2011-11-10 02:06:49 +00:00
|
|
|
UV_ERRNO_MAP(UV_STRERROR_GEN)
|
|
|
|
|
default:
|
|
|
|
|
return "Unknown system error";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#undef UV_STRERROR_GEN
|
|
|
|
|
|
|
|
|
|
|
2013-08-31 01:00:34 +00:00
|
|
|
int uv_ip4_addr(const char* ip, int port, struct sockaddr_in* addr) {
|
|
|
|
|
memset(addr, 0, sizeof(*addr));
|
|
|
|
|
addr->sin_family = AF_INET;
|
|
|
|
|
addr->sin_port = htons(port);
|
2013-12-12 17:01:38 +00:00
|
|
|
return uv_inet_pton(AF_INET, ip, &(addr->sin_addr.s_addr));
|
2011-06-17 16:56:43 +00:00
|
|
|
}
|
2011-06-27 04:34:04 +00:00
|
|
|
|
|
|
|
|
|
2013-08-31 01:00:34 +00:00
|
|
|
int uv_ip6_addr(const char* ip, int port, struct sockaddr_in6* addr) {
|
2013-03-14 21:20:32 +00:00
|
|
|
char address_part[40];
|
|
|
|
|
size_t address_part_size;
|
|
|
|
|
const char* zone_index;
|
2011-06-27 04:34:04 +00:00
|
|
|
|
2013-08-31 01:00:34 +00:00
|
|
|
memset(addr, 0, sizeof(*addr));
|
|
|
|
|
addr->sin6_family = AF_INET6;
|
|
|
|
|
addr->sin6_port = htons(port);
|
2013-03-14 21:20:32 +00:00
|
|
|
|
|
|
|
|
zone_index = strchr(ip, '%');
|
|
|
|
|
if (zone_index != NULL) {
|
2013-08-19 20:18:36 +00:00
|
|
|
address_part_size = zone_index - ip;
|
|
|
|
|
if (address_part_size >= sizeof(address_part))
|
|
|
|
|
address_part_size = sizeof(address_part) - 1;
|
2013-03-14 21:20:32 +00:00
|
|
|
|
2013-08-19 20:18:36 +00:00
|
|
|
memcpy(address_part, ip, address_part_size);
|
|
|
|
|
address_part[address_part_size] = '\0';
|
2013-03-14 21:20:32 +00:00
|
|
|
ip = address_part;
|
|
|
|
|
|
|
|
|
|
zone_index++; /* skip '%' */
|
|
|
|
|
/* NOTE: unknown interface (id=0) is silently ignored */
|
|
|
|
|
#ifdef _WIN32
|
2013-08-31 01:00:34 +00:00
|
|
|
addr->sin6_scope_id = atoi(zone_index);
|
2013-03-14 21:20:32 +00:00
|
|
|
#else
|
2013-08-31 01:00:34 +00:00
|
|
|
addr->sin6_scope_id = if_nametoindex(zone_index);
|
2013-03-14 21:20:32 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 17:01:38 +00:00
|
|
|
return uv_inet_pton(AF_INET6, ip, &addr->sin6_addr);
|
2011-06-27 04:34:04 +00:00
|
|
|
}
|
2011-06-28 15:02:50 +00:00
|
|
|
|
|
|
|
|
|
2014-04-17 12:13:33 +00:00
|
|
|
int uv_ip4_name(const struct sockaddr_in* src, char* dst, size_t size) {
|
2013-06-06 21:10:50 +00:00
|
|
|
return uv_inet_ntop(AF_INET, &src->sin_addr, dst, size);
|
2011-07-18 20:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-04-17 12:13:33 +00:00
|
|
|
int uv_ip6_name(const struct sockaddr_in6* src, char* dst, size_t size) {
|
2013-06-06 21:10:50 +00:00
|
|
|
return uv_inet_ntop(AF_INET6, &src->sin6_addr, dst, size);
|
2011-07-18 20:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-03-19 21:14:10 +00:00
|
|
|
int uv_tcp_bind(uv_tcp_t* handle,
|
|
|
|
|
const struct sockaddr* addr,
|
|
|
|
|
unsigned int flags) {
|
2013-09-03 20:48:17 +00:00
|
|
|
unsigned int addrlen;
|
2011-09-28 15:28:01 +00:00
|
|
|
|
2013-09-03 20:48:17 +00:00
|
|
|
if (handle->type != UV_TCP)
|
|
|
|
|
return UV_EINVAL;
|
2012-03-15 23:49:26 +00:00
|
|
|
|
2013-09-03 20:48:17 +00:00
|
|
|
if (addr->sa_family == AF_INET)
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
|
else if (addr->sa_family == AF_INET6)
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
2013-02-15 09:12:44 +00:00
|
|
|
else
|
2013-08-31 01:56:38 +00:00
|
|
|
return UV_EINVAL;
|
2013-09-03 20:48:17 +00:00
|
|
|
|
2013-03-19 21:14:10 +00:00
|
|
|
return uv__tcp_bind(handle, addr, addrlen, flags);
|
2011-09-28 15:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-15 23:49:26 +00:00
|
|
|
|
2013-02-15 09:12:44 +00:00
|
|
|
int uv_udp_bind(uv_udp_t* handle,
|
2013-09-03 21:49:26 +00:00
|
|
|
const struct sockaddr* addr,
|
2013-02-15 09:12:44 +00:00
|
|
|
unsigned int flags) {
|
2013-09-03 21:49:26 +00:00
|
|
|
unsigned int addrlen;
|
2011-09-28 15:28:01 +00:00
|
|
|
|
2013-09-03 21:49:26 +00:00
|
|
|
if (handle->type != UV_UDP)
|
|
|
|
|
return UV_EINVAL;
|
2012-03-15 23:49:26 +00:00
|
|
|
|
2013-09-03 21:49:26 +00:00
|
|
|
if (addr->sa_family == AF_INET)
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
|
else if (addr->sa_family == AF_INET6)
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
|
else
|
|
|
|
|
return UV_EINVAL;
|
|
|
|
|
|
|
|
|
|
return uv__udp_bind(handle, addr, addrlen, flags);
|
2011-09-28 15:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-15 23:49:26 +00:00
|
|
|
|
2011-09-28 15:22:00 +00:00
|
|
|
int uv_tcp_connect(uv_connect_t* req,
|
|
|
|
|
uv_tcp_t* handle,
|
2013-09-03 21:12:07 +00:00
|
|
|
const struct sockaddr* addr,
|
2011-09-28 15:22:00 +00:00
|
|
|
uv_connect_cb cb) {
|
2013-09-03 21:12:07 +00:00
|
|
|
unsigned int addrlen;
|
2011-09-28 15:22:00 +00:00
|
|
|
|
2013-09-03 21:12:07 +00:00
|
|
|
if (handle->type != UV_TCP)
|
|
|
|
|
return UV_EINVAL;
|
2012-03-15 23:49:26 +00:00
|
|
|
|
2013-09-03 21:12:07 +00:00
|
|
|
if (addr->sa_family == AF_INET)
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
|
else if (addr->sa_family == AF_INET6)
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
|
else
|
|
|
|
|
return UV_EINVAL;
|
|
|
|
|
|
|
|
|
|
return uv__tcp_connect(req, handle, addr, addrlen, cb);
|
2011-09-28 15:22:00 +00:00
|
|
|
}
|
2011-11-21 15:12:36 +00:00
|
|
|
|
|
|
|
|
|
2013-02-15 03:27:32 +00:00
|
|
|
int uv_udp_send(uv_udp_send_t* req,
|
|
|
|
|
uv_udp_t* handle,
|
2013-08-31 06:13:29 +00:00
|
|
|
const uv_buf_t bufs[],
|
|
|
|
|
unsigned int nbufs,
|
2013-09-03 23:48:50 +00:00
|
|
|
const struct sockaddr* addr,
|
2013-02-15 03:27:32 +00:00
|
|
|
uv_udp_send_cb send_cb) {
|
2013-09-03 23:48:50 +00:00
|
|
|
unsigned int addrlen;
|
2013-02-15 03:27:32 +00:00
|
|
|
|
2013-09-03 23:48:50 +00:00
|
|
|
if (handle->type != UV_UDP)
|
|
|
|
|
return UV_EINVAL;
|
2013-02-15 03:27:32 +00:00
|
|
|
|
2013-09-03 23:48:50 +00:00
|
|
|
if (addr->sa_family == AF_INET)
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
|
else if (addr->sa_family == AF_INET6)
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
|
else
|
|
|
|
|
return UV_EINVAL;
|
|
|
|
|
|
|
|
|
|
return uv__udp_send(req, handle, bufs, nbufs, addr, addrlen, send_cb);
|
2013-02-15 03:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-06-29 22:01:10 +00:00
|
|
|
int uv_udp_try_send(uv_udp_t* handle,
|
|
|
|
|
const uv_buf_t bufs[],
|
|
|
|
|
unsigned int nbufs,
|
|
|
|
|
const struct sockaddr* addr) {
|
|
|
|
|
unsigned int addrlen;
|
|
|
|
|
|
|
|
|
|
if (handle->type != UV_UDP)
|
|
|
|
|
return UV_EINVAL;
|
|
|
|
|
|
|
|
|
|
if (addr->sa_family == AF_INET)
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
|
else if (addr->sa_family == AF_INET6)
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
|
else
|
|
|
|
|
return UV_EINVAL;
|
|
|
|
|
|
|
|
|
|
return uv__udp_try_send(handle, bufs, nbufs, addr, addrlen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-02-15 03:27:32 +00:00
|
|
|
int uv_udp_recv_start(uv_udp_t* handle,
|
|
|
|
|
uv_alloc_cb alloc_cb,
|
|
|
|
|
uv_udp_recv_cb recv_cb) {
|
2013-06-06 21:10:50 +00:00
|
|
|
if (handle->type != UV_UDP || alloc_cb == NULL || recv_cb == NULL)
|
|
|
|
|
return UV_EINVAL;
|
|
|
|
|
else
|
|
|
|
|
return uv__udp_recv_start(handle, alloc_cb, recv_cb);
|
2013-02-15 03:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int uv_udp_recv_stop(uv_udp_t* handle) {
|
2013-06-06 21:10:50 +00:00
|
|
|
if (handle->type != UV_UDP)
|
|
|
|
|
return UV_EINVAL;
|
|
|
|
|
else
|
|
|
|
|
return uv__udp_recv_stop(handle);
|
2013-02-15 03:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-06 08:57:14 +00:00
|
|
|
|
2012-05-29 23:51:28 +00:00
|
|
|
void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg) {
|
2013-03-26 20:11:29 +00:00
|
|
|
QUEUE* q;
|
2012-05-29 23:51:28 +00:00
|
|
|
uv_handle_t* h;
|
|
|
|
|
|
2013-03-26 20:11:29 +00:00
|
|
|
QUEUE_FOREACH(q, &loop->handle_queue) {
|
|
|
|
|
h = QUEUE_DATA(q, uv_handle_t, handle_queue);
|
2012-05-29 23:51:28 +00:00
|
|
|
if (h->flags & UV__HANDLE_INTERNAL) continue;
|
|
|
|
|
walk_cb(h, arg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-06-06 02:24:33 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
static void uv__print_handles(uv_loop_t* loop, int only_active) {
|
|
|
|
|
const char* type;
|
2013-03-26 20:11:29 +00:00
|
|
|
QUEUE* q;
|
2012-06-06 02:24:33 +00:00
|
|
|
uv_handle_t* h;
|
|
|
|
|
|
|
|
|
|
if (loop == NULL)
|
|
|
|
|
loop = uv_default_loop();
|
|
|
|
|
|
2013-03-26 20:11:29 +00:00
|
|
|
QUEUE_FOREACH(q, &loop->handle_queue) {
|
|
|
|
|
h = QUEUE_DATA(q, uv_handle_t, handle_queue);
|
2012-06-06 02:24:33 +00:00
|
|
|
|
|
|
|
|
if (only_active && !uv__is_active(h))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
switch (h->type) {
|
|
|
|
|
#define X(uc, lc) case UV_##uc: type = #lc; break;
|
|
|
|
|
UV_HANDLE_TYPE_MAP(X)
|
|
|
|
|
#undef X
|
|
|
|
|
default: type = "<unknown>";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"[%c%c%c] %-8s %p\n",
|
|
|
|
|
"R-"[!(h->flags & UV__HANDLE_REF)],
|
|
|
|
|
"A-"[!(h->flags & UV__HANDLE_ACTIVE)],
|
|
|
|
|
"I-"[!(h->flags & UV__HANDLE_INTERNAL)],
|
|
|
|
|
type,
|
|
|
|
|
(void*)h);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uv_print_all_handles(uv_loop_t* loop) {
|
|
|
|
|
uv__print_handles(loop, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uv_print_active_handles(uv_loop_t* loop) {
|
|
|
|
|
uv__print_handles(loop, 1);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2012-05-17 04:28:46 +00:00
|
|
|
void uv_ref(uv_handle_t* handle) {
|
|
|
|
|
uv__handle_ref(handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uv_unref(uv_handle_t* handle) {
|
|
|
|
|
uv__handle_unref(handle);
|
|
|
|
|
}
|
2013-01-16 08:36:20 +00:00
|
|
|
|
|
|
|
|
|
2013-04-16 22:16:42 +00:00
|
|
|
int uv_has_ref(const uv_handle_t* handle) {
|
|
|
|
|
return uv__has_ref(handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-01-16 08:36:20 +00:00
|
|
|
void uv_stop(uv_loop_t* loop) {
|
|
|
|
|
loop->stop_flag = 1;
|
|
|
|
|
}
|
2013-05-29 14:13:34 +00:00
|
|
|
|
|
|
|
|
|
2014-04-28 19:38:44 +00:00
|
|
|
uint64_t uv_now(const uv_loop_t* loop) {
|
2013-05-29 14:13:34 +00:00
|
|
|
return loop->time;
|
|
|
|
|
}
|
2013-06-06 21:10:50 +00:00
|
|
|
|
|
|
|
|
|
2014-06-24 10:18:15 +00:00
|
|
|
|
|
|
|
|
size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs) {
|
|
|
|
|
unsigned int i;
|
|
|
|
|
size_t bytes;
|
|
|
|
|
|
|
|
|
|
bytes = 0;
|
|
|
|
|
for (i = 0; i < nbufs; i++)
|
|
|
|
|
bytes += (size_t) bufs[i].len;
|
|
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-07 12:56:11 +00:00
|
|
|
int uv_recv_buffer_size(uv_handle_t* handle, int* value) {
|
|
|
|
|
return uv__socket_sockopt(handle, SO_RCVBUF, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int uv_send_buffer_size(uv_handle_t* handle, int *value) {
|
|
|
|
|
return uv__socket_sockopt(handle, SO_SNDBUF, value);
|
|
|
|
|
}
|
2014-06-24 10:18:15 +00:00
|
|
|
|
2015-01-23 12:29:36 +00:00
|
|
|
int uv_fs_event_getpath(uv_fs_event_t* handle, char* buffer, size_t* size) {
|
2014-02-23 23:51:13 +00:00
|
|
|
size_t required_len;
|
|
|
|
|
|
|
|
|
|
if (!uv__is_active(handle)) {
|
2015-01-23 12:29:36 +00:00
|
|
|
*size = 0;
|
2014-02-23 23:51:13 +00:00
|
|
|
return UV_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-23 12:28:47 +00:00
|
|
|
required_len = strlen(handle->path);
|
2015-01-23 12:29:36 +00:00
|
|
|
if (required_len > *size) {
|
|
|
|
|
*size = required_len;
|
2014-02-23 23:51:13 +00:00
|
|
|
return UV_ENOBUFS;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-23 12:29:36 +00:00
|
|
|
memcpy(buffer, handle->path, required_len);
|
|
|
|
|
*size = required_len;
|
2014-02-23 23:51:13 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-08-13 13:48:08 +00:00
|
|
|
|
2015-03-03 04:03:31 +00:00
|
|
|
/* The windows implementation does not have the same structure layout as
|
|
|
|
|
* the unix implementation (nbufs is not directly inside req but is
|
|
|
|
|
* contained in a nested union/struct) so this function locates it.
|
|
|
|
|
*/
|
|
|
|
|
static unsigned int* uv__get_nbufs(uv_fs_t* req) {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
return &req->fs.info.nbufs;
|
|
|
|
|
#else
|
|
|
|
|
return &req->nbufs;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2014-08-13 13:48:08 +00:00
|
|
|
|
2014-09-25 08:09:58 +00:00
|
|
|
void uv__fs_scandir_cleanup(uv_fs_t* req) {
|
2014-08-13 13:48:08 +00:00
|
|
|
uv__dirent_t** dents;
|
|
|
|
|
|
2015-03-03 04:03:31 +00:00
|
|
|
unsigned int* nbufs = uv__get_nbufs(req);
|
|
|
|
|
|
2014-08-13 13:48:08 +00:00
|
|
|
dents = req->ptr;
|
2015-03-03 04:03:31 +00:00
|
|
|
if (*nbufs > 0 && *nbufs != (unsigned int) req->result)
|
|
|
|
|
(*nbufs)--;
|
|
|
|
|
for (; *nbufs < (unsigned int) req->result; (*nbufs)++)
|
2015-05-26 08:51:10 +00:00
|
|
|
uv__free(dents[*nbufs]);
|
2014-08-13 13:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-09-25 08:09:58 +00:00
|
|
|
int uv_fs_scandir_next(uv_fs_t* req, uv_dirent_t* ent) {
|
2014-08-13 13:48:08 +00:00
|
|
|
uv__dirent_t** dents;
|
|
|
|
|
uv__dirent_t* dent;
|
|
|
|
|
|
2015-03-03 04:03:31 +00:00
|
|
|
unsigned int* nbufs = uv__get_nbufs(req);
|
|
|
|
|
|
2014-08-13 13:48:08 +00:00
|
|
|
dents = req->ptr;
|
|
|
|
|
|
|
|
|
|
/* Free previous entity */
|
2015-03-03 04:03:31 +00:00
|
|
|
if (*nbufs > 0)
|
2015-05-26 08:51:10 +00:00
|
|
|
uv__free(dents[*nbufs - 1]);
|
2014-08-13 13:48:08 +00:00
|
|
|
|
|
|
|
|
/* End was already reached */
|
2015-03-03 04:03:31 +00:00
|
|
|
if (*nbufs == (unsigned int) req->result) {
|
2015-05-26 08:51:10 +00:00
|
|
|
uv__free(dents);
|
2014-08-13 13:48:08 +00:00
|
|
|
req->ptr = NULL;
|
|
|
|
|
return UV_EOF;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-03 04:03:31 +00:00
|
|
|
dent = dents[(*nbufs)++];
|
2014-08-13 13:48:08 +00:00
|
|
|
|
|
|
|
|
ent->name = dent->d_name;
|
2014-08-14 20:18:28 +00:00
|
|
|
#ifdef HAVE_DIRENT_TYPES
|
|
|
|
|
switch (dent->d_type) {
|
|
|
|
|
case UV__DT_DIR:
|
|
|
|
|
ent->type = UV_DIRENT_DIR;
|
|
|
|
|
break;
|
|
|
|
|
case UV__DT_FILE:
|
|
|
|
|
ent->type = UV_DIRENT_FILE;
|
|
|
|
|
break;
|
|
|
|
|
case UV__DT_LINK:
|
|
|
|
|
ent->type = UV_DIRENT_LINK;
|
|
|
|
|
break;
|
|
|
|
|
case UV__DT_FIFO:
|
|
|
|
|
ent->type = UV_DIRENT_FIFO;
|
|
|
|
|
break;
|
|
|
|
|
case UV__DT_SOCKET:
|
|
|
|
|
ent->type = UV_DIRENT_SOCKET;
|
|
|
|
|
break;
|
|
|
|
|
case UV__DT_CHAR:
|
|
|
|
|
ent->type = UV_DIRENT_CHAR;
|
|
|
|
|
break;
|
|
|
|
|
case UV__DT_BLOCK:
|
|
|
|
|
ent->type = UV_DIRENT_BLOCK;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ent->type = UV_DIRENT_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
ent->type = UV_DIRENT_UNKNOWN;
|
|
|
|
|
#endif
|
2014-08-13 13:48:08 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-12-05 18:18:36 +00:00
|
|
|
|
|
|
|
|
|
2014-11-27 07:43:48 +00:00
|
|
|
int uv_loop_configure(uv_loop_t* loop, uv_loop_option option, ...) {
|
|
|
|
|
va_list ap;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
va_start(ap, option);
|
|
|
|
|
/* Any platform-agnostic options should be handled here. */
|
|
|
|
|
err = uv__loop_configure(loop, option, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2015-01-20 10:00:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
static uv_loop_t default_loop_struct;
|
|
|
|
|
static uv_loop_t* default_loop_ptr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_loop_t* uv_default_loop(void) {
|
|
|
|
|
if (default_loop_ptr != NULL)
|
|
|
|
|
return default_loop_ptr;
|
|
|
|
|
|
|
|
|
|
if (uv_loop_init(&default_loop_struct))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
default_loop_ptr = &default_loop_struct;
|
|
|
|
|
return default_loop_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uv_loop_t* uv_loop_new(void) {
|
|
|
|
|
uv_loop_t* loop;
|
|
|
|
|
|
2015-05-26 08:51:10 +00:00
|
|
|
loop = uv__malloc(sizeof(*loop));
|
2015-01-20 10:00:00 +00:00
|
|
|
if (loop == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (uv_loop_init(loop)) {
|
2015-05-26 08:51:10 +00:00
|
|
|
uv__free(loop);
|
2015-01-20 10:00:00 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return loop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int uv_loop_close(uv_loop_t* loop) {
|
|
|
|
|
QUEUE* q;
|
|
|
|
|
uv_handle_t* h;
|
|
|
|
|
|
|
|
|
|
if (!QUEUE_EMPTY(&(loop)->active_reqs))
|
|
|
|
|
return UV_EBUSY;
|
|
|
|
|
|
|
|
|
|
QUEUE_FOREACH(q, &loop->handle_queue) {
|
|
|
|
|
h = QUEUE_DATA(q, uv_handle_t, handle_queue);
|
|
|
|
|
if (!(h->flags & UV__HANDLE_INTERNAL))
|
|
|
|
|
return UV_EBUSY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uv__loop_close(loop);
|
|
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
memset(loop, -1, sizeof(*loop));
|
|
|
|
|
#endif
|
|
|
|
|
if (loop == default_loop_ptr)
|
|
|
|
|
default_loop_ptr = NULL;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uv_loop_delete(uv_loop_t* loop) {
|
|
|
|
|
uv_loop_t* default_loop;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
default_loop = default_loop_ptr;
|
|
|
|
|
|
|
|
|
|
err = uv_loop_close(loop);
|
2015-05-05 20:40:15 +00:00
|
|
|
(void) err; /* Squelch compiler warnings. */
|
2015-01-20 10:00:00 +00:00
|
|
|
assert(err == 0);
|
|
|
|
|
if (loop != default_loop)
|
2015-05-26 08:51:10 +00:00
|
|
|
uv__free(loop);
|
2015-01-20 10:00:00 +00:00
|
|
|
}
|