Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/v0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Jan 27, 2012
2 parents 9974825 + 3f1bad2 commit 3de0411
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 23 deletions.
34 changes: 31 additions & 3 deletions include/uv.h
Expand Up @@ -115,7 +115,8 @@ typedef intptr_t ssize_t;
XX( 45, EAISOCKTYPE, "") \
XX( 46, ESHUTDOWN, "") \
XX( 47, EEXIST, "file already exists") \
XX( 48, ESRCH, "no such process")
XX( 48, ESRCH, "no such process") \
XX( 49, ENAMETOOLONG, "name too long")


#define UV_ERRNO_GEN(val, name, s) UV_##name = val,
Expand Down Expand Up @@ -653,6 +654,20 @@ UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle,
const char* multicast_addr, const char* interface_addr,
uv_membership membership);

/*
* Set IP multicast loop flag. Makes multicast packets loop back to
* local sockets.
*
* Arguments:
* handle UDP handle. Should have been initialized with
* `uv_udp_init`.
* on 1 for on, 0 for off
*
* Returns:
* 0 on success, -1 on error.
*/
UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on);

/*
* Set the multicast ttl
*
Expand All @@ -664,7 +679,7 @@ UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle,
* Returns:
* 0 on success, -1 on error.
*/
int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);

/*
* Set broadcast on or off
Expand All @@ -677,7 +692,20 @@ int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
* Returns:
* 0 on success, -1 on error.
*/
int uv_udp_set_broadcast(uv_udp_t* handle, int on);
UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on);

/*
* Set the time to live
*
* Arguments:
* handle UDP handle. Should have been initialized with
* `uv_udp_init`.
* ttl 1 through 255
*
* Returns:
* 0 on success, -1 on error.
*/
UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl);

/*
* Send data. If the socket has not previously been bound with `uv_udp_bind`
Expand Down
1 change: 1 addition & 0 deletions src/unix/error.c
Expand Up @@ -71,6 +71,7 @@ uv_err_code uv_translate_sys_error(int sys_errno) {
case EFAULT: return UV_EFAULT;
case EMFILE: return UV_EMFILE;
case EMSGSIZE: return UV_EMSGSIZE;
case ENAMETOOLONG: return UV_ENAMETOOLONG;
case EINVAL: return UV_EINVAL;
case ECONNREFUSED: return UV_ECONNREFUSED;
case EADDRINUSE: return UV_EADDRINUSE;
Expand Down
38 changes: 22 additions & 16 deletions src/unix/udp.c
Expand Up @@ -344,6 +344,14 @@ static int uv__bind(uv_udp_t* handle,
goto out;
}

#ifdef SO_REUSEPORT /* Apple's version of SO_REUSEADDR... */
yes = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof yes) == -1) {
uv__set_sys_error(handle->loop, errno);
goto out;
}
#endif

if (flags & UV_UDP_IPV6ONLY) {
#ifdef IPV6_V6ONLY
yes = 1;
Expand Down Expand Up @@ -513,27 +521,25 @@ int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr,
return 0;
}

int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl) {
if (setsockopt(handle->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof ttl) == -1) {
uv__set_sys_error(handle->loop, errno);
return -1;
}

return 0;
}

int uv_udp_set_broadcast(uv_udp_t* handle, int on) {
if (setsockopt(handle->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof on) == -1) {
uv__set_sys_error(handle->loop, errno);
return -1;
#define X(name, level, option) \
int uv_udp_set_##name(uv_udp_t* handle, int flag) { \
if (setsockopt(handle->fd, level, option, &flag, sizeof(flag))) { \
uv__set_sys_error(handle->loop, errno); \
return -1; \
} \
return 0; \
}

return 0;
}
X(multicast_loop, IPPROTO_IP, IP_MULTICAST_LOOP)
X(multicast_ttl, IPPROTO_IP, IP_MULTICAST_TTL)
X(broadcast, SOL_SOCKET, SO_BROADCAST)
X(ttl, IPPROTO_IP, IP_TTL)

#undef X


int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
int* namelen) {
int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name, int* namelen) {
socklen_t socklen;
int saved_errno;
int rv = 0;
Expand Down
1 change: 1 addition & 0 deletions src/win/error.c
Expand Up @@ -94,6 +94,7 @@ uv_err_code uv_translate_sys_error(int sys_errno) {
case WSAEMSGSIZE: return UV_EMSGSIZE;
case ERROR_NETWORK_UNREACHABLE: return UV_ENETUNREACH;
case WSAENETUNREACH: return UV_ENETUNREACH;
case WSAENOBUFS: return UV_ENOBUFS;
case ERROR_OUTOFMEMORY: return UV_ENOMEM;
case ERROR_NOT_CONNECTED: return UV_ENOTCONN;
case WSAENOTCONN: return UV_ENOTCONN;
Expand Down
123 changes: 122 additions & 1 deletion src/win/fs.c
Expand Up @@ -489,13 +489,134 @@ void fs__readdir(uv_fs_t* req, const wchar_t* path, int flags) {
}


#define IS_SLASH(c) \
((wchar_t) c == L'/' || (wchar_t) c == L'\\')
#define IS_COLON(c) \
((wchar_t) c == L':')
#define IS_LETTER(c) \
((((wchar_t) c >= L'a') && ((wchar_t) c <= L'z')) || \
(((wchar_t) c >= L'A') && ((wchar_t) c <= L'Z')))
#define IS_QUESTION(c) \
((wchar_t) c == L'?')


static int uv__count_slash_separated_words(const wchar_t* pos,
const wchar_t* end,
int limit) {
char last_was_slash = 1, count = 0;

for (; pos < end; pos++) {
if (IS_SLASH(*pos)) {
/* Don't accept double slashes */
if (last_was_slash) {
return 0;
} else {
last_was_slash = 1;
}
} else {
if (last_was_slash) {
/* Found a new word */
count++;
if (count > limit) {
return -1;
}
last_was_slash = 0;
}
}
}

return count;
}

/*
* Returns true if the given path is a root directory. The following patterns
* are recognized:
* \
* c:\ (must have trailing slash)
* \\server\share (trailing slash optional)
* \\?\c: (trailing slash optional)
* \\?\UNC\server\share (trailing slash optional)
*/
static int uv__is_root(const wchar_t* path) {
size_t len = wcslen(path);

/* Test for \ */
if (len == 0 && IS_SLASH(path[0])) {
return 1;
}

if (len < 3) {
return 0;
}

/* Test for c:\ */
if (IS_LETTER(path[0]) && IS_COLON(path[1]) && IS_SLASH(path[2])) {
return 1;
}

if (!IS_SLASH(path[0]) || !IS_SLASH(path[1])) {
return 0;
}

/* Test for \\server\share */
if (!IS_QUESTION(path[2])) {
return uv__count_slash_separated_words(path + 2, path + len, 2) == 2;
}

if (!IS_SLASH(path[3])) {
return 0;
}

if ((len == 6 || len == 7) &&
IS_LETTER(path[4]) && IS_COLON(path[5]) &&
(len == 6 || IS_SLASH(path[6]))) {
return 1;
}

/* Test for \\?\UNC\server\share */
if (len >= 8 &&
(path[4] == L'u' || path[4] == L'U') &&
(path[5] == L'n' || path[5] == L'N') &&
(path[6] == L'c' || path[6] == L'C') &&
IS_SLASH(path[7])) {
return uv__count_slash_separated_words(path + 8, path + len, 2) == 2;
}

return 0;
}


void fs__stat(uv_fs_t* req, const wchar_t* path) {
HANDLE file;
WIN32_FIND_DATAW ent;
int result;

req->ptr = NULL;

if (uv__is_root(path)) {
/* We can't stat root directories like c:\. _wstati64 can't either, but */
/* it will make up something reasonable. */
DWORD drive_type = GetDriveTypeW(path);
if (drive_type == DRIVE_UNKNOWN || drive_type == DRIVE_NO_ROOT_DIR) {
req->last_error = ERROR_PATH_NOT_FOUND;
req->errorno = UV_ENOENT;
req->result = -1;
return;
}

memset(&req->stat, 0, sizeof req->stat);

req->stat.st_nlink = 1;
req->stat.st_mode = ((_S_IREAD|_S_IWRITE) + ((_S_IREAD|_S_IWRITE) >> 3) +
((_S_IREAD|_S_IWRITE) >> 6)) | S_IFDIR;

req->last_error = ERROR_SUCCESS;
req->errorno = UV_OK;
req->result = 0;
req->ptr = &req->stat;
return;
}

file = FindFirstFileExW(path, FindExInfoStandard, &ent,
FindExSearchNameMatch, NULL, 0);

Expand All @@ -516,7 +637,7 @@ void fs__stat(uv_fs_t* req, const wchar_t* path) {
if (result != -1) {
req->ptr = &req->stat;
}

SET_REQ_RESULT(req, result);
}

Expand Down
28 changes: 26 additions & 2 deletions src/win/tty.c
Expand Up @@ -1069,13 +1069,26 @@ static int uv_tty_set_style(uv_tty_t* handle, DWORD* error) {
bg_bright = 0;

} else if (arg == 1) {
/* Bright */
/* Foreground bright on */
fg_bright = 1;

} else if (arg == 2) {
/* Both bright off */
fg_bright = 0;
bg_bright = 0;

} else if (arg == 5) {
/* Background bright on */
bg_bright = 1;

} else if (arg == 21 || arg == 22) {
/* Bright off. */
/* Foreground bright off */
fg_bright = 0;

} else if (arg == 25) {
/* Background bright off */
bg_bright = 0;

} else if (arg >= 30 && arg <= 37) {
/* Set foreground color */
fg_color = arg - 30;
Expand All @@ -1091,6 +1104,17 @@ static int uv_tty_set_style(uv_tty_t* handle, DWORD* error) {
} else if (arg == 49) {
/* Default background color */
bg_color = 0;

} else if (arg >= 90 && arg <= 97) {
/* Set bold foreground color */
fg_bright = 1;
fg_color = arg - 90;

} else if (arg >= 100 && arg <= 107) {
/* Set bold background color */
bg_bright = 1;
bg_color = arg - 100;

}
}

Expand Down
12 changes: 12 additions & 0 deletions src/win/udp.c
Expand Up @@ -575,6 +575,12 @@ void uv_process_udp_send_req(uv_loop_t* loop, uv_udp_t* handle,
}


int uv_udp_set_multicast_loop(uv_udp_t* handle, int on) {
uv__set_artificial_error(handle->loop, UV_ENOSYS);
return -1;
}


int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl) {
if (setsockopt(handle->socket, IPPROTO_IP, IP_MULTICAST_TTL,
(const char*)&ttl, sizeof ttl) == -1) {
Expand All @@ -595,3 +601,9 @@ int uv_udp_set_broadcast(uv_udp_t* handle, int on) {

return 0;
}


int uv_udp_set_ttl(uv_udp_t* handle, int ttl) {
uv__set_artificial_error(handle->loop, UV_ENOSYS);
return -1;
}

0 comments on commit 3de0411

Please sign in to comment.