[BACK]Return to winsock.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / external / mit / libuv / dist / src / win

File: [cvs.NetBSD.org] / src / external / mit / libuv / dist / src / win / winsock.c (download)

Revision 1.1.1.2 (vendor branch), Thu Sep 22 21:20:40 2022 UTC (18 months, 3 weeks ago) by christos
Branch: MAIN, LIBUV
CVS Tags: netbsd-10-base, netbsd-10-0-RELEASE, netbsd-10-0-RC6, netbsd-10-0-RC5, netbsd-10-0-RC4, netbsd-10-0-RC3, netbsd-10-0-RC2, netbsd-10-0-RC1, netbsd-10, libuv-1-44-2, HEAD
Branch point for: netbsd-9
Changes since 1.1.1.1: +14 -14 lines

Import libuv-1.44.2 (last imported version was 1.38.0)

2022.07.12, Version 1.44.2 (Stable)

Changes since version 1.44.1:

* Add SHA to ChangeLog (Jameson Nash)

* aix, ibmi: handle server hang when remote sends TCP RST (V-for-Vasili)

* build: make CI a bit noisier (Jameson Nash)

* process: reset the signal mask if the fork fails (Jameson Nash)

* zos: implement cmpxchgi() using assembly (Shuowang (Wayne) Zhang)

* build: AC_SUBST for AM_CFLAGS (Claes Nästén)

* ibmi: Implement UDP disconnect (V-for-Vasili)

* doc: update active maintainers list (Ben Noordhuis)

* build: fix kFreeBSD build (James McCoy)

* build: remove Windows 2016 workflows (Darshan Sen)

* Revert "win,errors: remap ERROR_ACCESS_DENIED to UV_EACCES" (Darshan Sen)

* unix: simplify getpwuid call (Jameson Nash)

* build: filter CI by paths and branches (Jameson Nash)

* build: add iOS to macos CI (Jameson Nash)

* build: re-enable CI for windows changes (Jameson Nash)

* process,iOS: fix build breakage in process.c (Denny C. Dai)

* test: remove unused declarations in tcp_rst test (V-for-Vasili)

* core: add thread-safe strtok implementation (Guilherme scaro)

* win: fix incompatible-types warning (twosee)

* test: fix flaky file watcher test (Ben Noordhuis)

* build: fix AIX xlc autotools build (V-for-Vasili)

* unix,win: fix UV_RUN_ONCE + uv_idle_stop loop hang (Ben Noordhuis)

* win: fix unexpected ECONNRESET error on TCP socket (twosee)

* doc: make sample cross-platform build (gengjiawen)

* test: separate some static variables by test cases (Hannah Shi)

* sunos: fs-event callback can be called after uv_close() (Andy Fiddaman)

* uv: re-register interest in a file after change (Shuowang (Wayne) Zhang)

* uv: register UV_RENAME event for _RFIM_UNLINK (Shuowang (Wayne) Zhang)

* uv: register __rfim_event 156 as UV_RENAME (Shuowang (Wayne) Zhang)

* doc: remove smartos from supported platforms (Ben Noordhuis)

* macos: avoid posix_spawnp() cwd bug (Jameson Nash)

* release: check versions of autogen scripts are newer (Jameson Nash)

* test: rewrite embed test (Ben Noordhuis)

* openbsd: use utimensat instead of lutimes (tuftedocelot)

* doc: fix link to uvwget example main() function (blogdaren)

* unix: use MSG_CMSG_CLOEXEC where supported (Ben Noordhuis)

* test: remove disabled callback_order test (Ben Noordhuis)

* win,pipe: fix bugs with pipe resource lifetime management (Jameson Nash)

* loop: better align order-of-events behavior between platforms (Jameson Nash)

* aix,test: uv_backend_fd is not supported by poll (V-for-Vasili)

* kqueue: skip EVFILT_PROC when invalidating fds (chucksilvers)

* darwin: fix atomic-ops.h ppc64 build (Sergey Fedorov)

* zos: don't err when killing a zombie process (Shuowang (Wayne) Zhang)

* zos: avoid fs event callbacks after uv_close() (Shuowang (Wayne) Zhang)

* zos: correctly format interface addresses names (Shuowang (Wayne) Zhang)

* zos: add uv_interface_addresses() netmask support (Shuowang (Wayne) Zhang)

* zos: improve memory management of ip addresses (Shuowang (Wayne) Zhang)

* tcp,pipe: fail `bind` or `listen` after `close` (theanarkh)

* zos: implement uv_available_parallelism() (Shuowang (Wayne) Zhang)

* udp,win: fix UDP compiler warning (Jameson Nash)

* zos: fix early exit of epoll_wait() (Shuowang (Wayne) Zhang)

* unix,tcp: fix errno handling in uv__tcp_bind() (Samuel Cabrero)

* shutdown,unix: reduce code duplication (Jameson Nash)

* unix: fix c99 comments (Ben Noordhuis)

* unix: retry tcgetattr/tcsetattr() on EINTR (Ben Noordhuis)

* docs: update introduction.rst (Ikko Ashimine)

* unix,stream: optimize uv_shutdown() codepath (Jameson Nash)

* zos: delay signal handling until after normal i/o (Shuowang (Wayne) Zhang)

* stream: uv__drain() always needs to stop POLLOUT (Jameson Nash)

* unix,tcp: allow EINVAL errno from setsockopt in uv_tcp_close_reset() (Stacey
  Marshall)

* win,shutdown: improve how shutdown is dispatched (Jameson Nash)


2022.03.09, Version 1.44.1 (Stable), e8b7eb6908a847ffbe6ab2eec7428e43a0aa53a2

Changes since version 1.44.0:

* process: simplify uv__write_int calls (Jameson Nash)

* macos: don't use thread-unsafe strtok() (Ben Noordhuis)

* process: fix hang after NOTE_EXIT (Jameson Nash)


2022.03.07, Version 1.44.0 (Stable), d2bff508457336d808ba7148b33088f6acbfe0a6

Changes since version 1.43.0:

* darwin: remove EPROTOTYPE error workaround (Ben Noordhuis)

* doc: fix v1.43.0 changelog entries (cjihrig)

* win: replace CRITICAL_SECTION+Semaphore with SRWLock (David Machaj)

* darwin: translate EPROTOTYPE to ECONNRESET (Ben Noordhuis)

* android: use libc getifaddrs() (Ben Noordhuis)

* unix: fix STATIC_ASSERT to check what it means to check (Jessica Clarke)

* unix: ensure struct msghdr is zeroed in recvmmsg (Ondej Surý)

* test: test with maximum recvmmsg buffer (Ondej Surý)

* unix: don't allow too small thread stack size (Ben Noordhuis)

* bsd: ensure mutex is initialized (Ben Noordhuis)

* doc: add gengjiawen as maintainer (gengjiawen)

* process: monitor for exit with kqueue on BSDs (Jeremy Rose)

* test: fix flaky uv_fs_lutime test (Momtchil Momtchev)

* build: fix cmake install locations (Jameson Nash)

* thread,win: fix C90 style nit (ssrlive)

* build: rename CFLAGS to AM_CFLAGS (Jameson Nash)

* doc/guide: update content and sample code (woclass)

* process,bsd: handle kevent NOTE_EXIT failure (Jameson Nash)

* test: remove flaky test ipc_closed_handle (Ben Noordhuis)

* darwin: bump minimum supported version to 10.15 (Ben Noordhuis)

* win: return fractional seconds in uv_uptime() (Luca Adrian L)

* build: export uv_a for cmake (WenTao Ou)

* loop: add pending work to loop-alive check (Jameson Nash)

* win: use GetTickCount64 for uptime again (Jameson Nash)

* win: restrict system DLL load paths (jonilaitinen)

* win,errors: remap ERROR_ACCESS_DENIED to UV_EACCES (Darshan Sen)

* bench: add `uv_queue_work` ping-pong measurement (Momtchil Momtchev)

* build: fix error C4146 on MSVC (UMU)

* test: fix benchmark-ping-udp (Ryan Liptak)

* win,fs: consider broken pipe error a normal EOF (Momtchil Momtchev)

* document the values of enum uv_stdio_flags (Paul Evans)

* win,loop: add missing uv_update_time (twosee)

* win,fs: avoid closing an invalid handle (Jameson Nash)

* fix oopsie from

* doc: clarify android api level (Ben Noordhuis)

* win: fix style nits [NFC] (Jameson Nash)

* test: fix flaky udp_mmsg test (Santiago Gimeno)

* test: fix ipc_send_recv_pipe flakiness (Ben Noordhuis)

* doc: checkout -> check out (wyckster)

* core: change uv_get_password uid/gid to unsigned (Jameson Nash)

* hurd: unbreak build on GNU/Hurd (Vittore F. Scolari)

* freebsd: use copy_file_range() in uv_fs_sendfile() (David Carlier)

* test: use closefd in runner-unix.c (Guilherme scaro)

* Reland "macos: use posix_spawn instead of fork" (Jameson Nash)

* android: fix build error when no ifaddrs.h (ssrlive)

* unix,win: add uv_available_parallelism() (Ben Noordhuis)

* process: remove OpenBSD from kevent list (Jameson Nash)

* zos: fix build breakage (Ben Noordhuis)

* process: only use F_DUPFD_CLOEXEC if it is defined (Jameson Nash)

* win,poll: add the MSAFD GUID for AF_UNIX (roflcopter4)

* unix: simplify uv__cloexec_fcntl() (Ben Noordhuis)

* doc: add secondary GPG ID for vtjnash (Jameson Nash)

* unix: remove uv__cloexec_ioctl() (Jameson Nash)


2022.01.05, Version 1.43.0 (Stable), 988f2bfc4defb9a85a536a3e645834c161143ee0

Changes since version 1.42.0:

* run test named ip6_sin6_len (Jameson Nash)

* docs: fix wrong information about scheduling (Mohamed Edrah)

* unix: protect fork in uv_spawn from signals (Jameson Nash)

* drop only successfully sent packets post sendmmsg (Supragya Raj)

* test: fix typo in test-tty-escape-sequence-processing.c (Ikko Ashimine)

* cmake: use standard installation layout always (Sylvain Corlay)

* win,spawn: allow UNC path with forward slash (earnal)

* win,fsevent: fix uv_fs_event_stop() assert (Ben Noordhuis)

* unix: remove redundant include in unix.h (Juan José Arboleda)

* doc: mark SmartOS as Tier 3 support (Ben Noordhuis)

* doc: fix broken links for netbsd's sysctl manpage (YAKSH BARIYA)

* misc: adjust stalebot deadline (Ben Noordhuis)

* test: remove `dns-server.c` as it is not used anywhere (Darshan Sen)

* build: fix non-cmake android builds (YAKSH BARIYA)

* doc: replace pyuv with uvloop (Ofek Lev)

* asan: fix some tests (Jameson Nash)

* build: add experimental TSAN configuration (Jameson Nash)

* pipe: remove useless assertion (~locpyl-tidnyd)

* bsd: destroy mutex in uv__process_title_cleanup() (Darshan Sen)

* build: add windows build to CI (Darshan Sen)

* win,fs: fix error code in uv_fs_read() and uv_fs_write() (Darshan Sen)

* build: add macos-latest to ci matrix (Ben Noordhuis)

* udp: fix &/&& typo in macro condition (Evan Miller)

* build: install cmake package module (Petr Menšík)

* win: fix build for mingw32 (Nicolas Noble)

* build: fix build failures with MinGW new headers (erw7)

* build: fix win build with cmake versions before v3.14 (AJ Heller)

* unix: support aarch64 in uv_cpu_info() (Juan José Arboleda)

* linux: work around CIFS EPERM bug (Ben Noordhuis)

* sunos: Oracle Developer Studio support (Stacey Marshall)

* Revert "sunos: Oracle Developer Studio support (cjihrig)

* sunos: Oracle Developer Studio support (Stacey Marshall)

* stream: permit read after seeing EOF (Jameson Nash)

* thread: initialize uv_thread_self for all threads (Jameson Nash)

* kqueue: ignore write-end closed notifications (Jameson Nash)

* macos: fix the cfdata length in uv__get_cpu_speed (Jesper Storm Bache)

* unix,win: add uv_ip_name to get name from sockaddr (Campbell He)

* win,test: fix a few typos (AJ Heller)

* zos: use destructor for uv__threadpool_cleanup() (Wayne Zhang)

* linux: use MemAvailable instead of MemFree (Andrey Hohutkin)

* freebsd: call dlerror() only if necessary (Jameson Nash)

* bsd,windows,zos: fix udp disconnect EINVAL (deal)


2021.07.21, Version 1.42.0 (Stable), 6ce14710da7079eb248868171f6343bc409ea3a4

Changes since version 1.41.0:

* doc: fix code highlighting (Darshan Sen)

* test: move to ASSERT_NULL and ASSERT_NOT_NULL test macros (tjarlama)

* zos: build in ascii code page (Shuowang (Wayne) Zhang)

* zos: don't use nanosecond timestamp fields (Shuowang (Wayne) Zhang)

* zos: introduce zoslib (Shuowang (Wayne) Zhang)

* zos: use strnlen() from zoslib (Shuowang (Wayne) Zhang)

* zos: use nanosleep() from zoslib (Shuowang (Wayne) Zhang)

* zos: use __getargv() from zoslib to get exe path (Shuowang (Wayne) Zhang)

* zos: treat __rfim_utok as binary (Shuowang (Wayne) Zhang)

* zos: use execvpe() to set environ explictly (Shuowang (Wayne) Zhang)

* zos: use custom proctitle implementation (Shuowang (Wayne) Zhang)

* doc: add instructions for building on z/OS (Shuowang (Wayne) Zhang)

* linux,udp: enable full ICMP error reporting (Ondej Surý)

* test: fix test-udp-send-unreachable (Ondej Surý)

* include: fix typo in documentation (Tobias Nieen)

* chore: use for(;;) instead of while (Yash Ladha)

* test: remove string + int warning on udp-pummel (Juan José Arboleda)

* cmake: fix linker flags (Zhao Zhili)

* test: fix stack-use-after-scope (Zhao Zhili)

* unix: expose thread_stack_size() internally (Brandon Cheng)

* darwin: use RLIMIT_STACK for fsevents pthread (Brandon Cheng)

* darwin: abort on pthread_attr_init fail (Brandon Cheng)

* benchmark: remove unreachable code (Matvii Hodovaniuk)

* macos: fix memleaks in uv__get_cpu_speed (George Zhao)

* Make Thread Sanitizer aware of file descriptor close in uv__close() (Ondej
  Surý)

* darwin: fix iOS compilation and functionality (Hayden)

* linux: work around copy_file_range() cephfs bug (Ben Noordhuis)

* zos: implement uv_get_constrained_memory() (Shuowang (Wayne) Zhang)

* zos: fix uv_get_free_memory() (Shuowang (Wayne) Zhang)

* zos: use CVTRLSTG to get total memory accurately (Shuowang (Wayne) Zhang)

* ibmi: Handle interface names longer than 10 chars (Kevin Adler)

* docs: update read-the-docs version of sphinx (Jameson Nash)

* unix: refactor uv_try_write (twosee)

* linux-core: add proper divide by zero assert (yiyuaner)

* misc: remove unnecessary _GNU_SOURCE macros (Darshan Sen)

* test: log to stdout to conform TAP spec (bbara)

* win,fs: fix C4090 warning with MSVC (SeverinLeonhardt)

* build: some systems provide dlopen() in libc (Andy Fiddaman)

* include: add EOVERFLOW status code mapping (Darshan Sen)

* unix,fs: use uv__load_relaxed and uv__store_relaxed (Darshan Sen)

* win: fix string encoding issue of uv_os_gethostname (Eagle Liang)

* unix,process: add uv__write_errno helper function (Ricky Zhou)

* Re-merge "unix,stream: clear read/write states on close/eof" (Jameson Nash)

* unix,core: fix errno handling in uv__getpwuid_r (Darshan Sen)

* errors: map ESOCKTNOSUPPORT errno (Ryan Liptak)

* doc: uv_read_stop always succeeds (Simon Kissane)

* inet: fix inconsistent return value of inet_ntop6 (twosee)

* darwin: fix -Wsometimes-uninitialized warning (twosee)

* stream: introduce uv_try_write2 function (twosee)

* poll,win: UV_PRIORITIZED option should not assert (twosee)

* src: DragonFlyBSD has mmsghdr struct too (David Carlier)

* cleanup,win: Remove _WIN32 guards on threadpool (James M Snell)

* freebsd: fix an incompatible pointer type warning (Darshan Sen)

* core: Correct the conditionals for {cloexec,nonblock}_ioctl (Ali Mohammad
  Pur)

* win,tcp: make uv_close work more like unix (Jameson Nash)

* doc: more accurate list of valid send_handle's (twosee)

* win,tcp: translate system errors correctly (twosee)

* unix: implement cpu_relax() on ppc64 (Ben Noordhuis)

* docs: move list of project links under PR control (Jameson Nash)

* test: wrong pointer arithmetic multiplier (Erkhes N)

* doc: switch discussion forum to github (Jameson Nash)

* idna: fix OOB read in punycode decoder (Ben Noordhuis)

* build: make sure -fvisibility=hidden is set (Santiago Gimeno)

* illumos: event ports to epoll (tjarlama)

* illumos,tty: UV_TTY_MODE_IO waits for 4 bytes (Joshua M. Clulow)

* doc: add vtjnash GPG ID (Jameson Nash)

* linux: read CPU model information on ppc (Richard Lau)

* darwin: fix uv_barrier race condition (Guilherme scaro)

* unix,stream: fix loop hang after uv_shutdown (Jameson Nash)

* doc,udp: note that suggested_size is 1 max-sized dgram (Ryan Liptak)

* mingw: fix building for ARM/AArch64 (Martin Storsjö)

* unix: strnlen is not available on Solaris 10 (Claes Nästén)

* sunos: restore use of event ports (Andy Fiddaman)

* sunos,cmake: use thread-safe errno (Andy Fiddaman)


2021.02.14, Version 1.41.0 (Stable), 1dff88e5161cba5c59276d2070d2e304e4dcb242

Changes since version 1.40.0:

* mailmap: update contact information for richardlau (Richard Lau)

* build: add asan checks (gengjiawen)

* unix: report bind error in uv_tcp_connect() (Ben Noordhuis)

* doc: uv_tcp_bind() never returns UV_EADDRINUSE (Ben Noordhuis)

* test: fix pump and tcp_write_batch benchmarks (Santiago Gimeno)

* doc: mark IBM i as Tier 2 support (Jesse Gorzinski)

* doc,poll: add notes (repeated cb & cancel pending cb) (Elad Nachmias)

* linux: fix -Wincompatible-pointer-types warning (Ben Noordhuis)

* linux: fix -Wsign-compare warning (Ben Noordhuis)

* android: add system call api guards (Ben Noordhuis)

* unix,win: harmonize uv_read_start() error handling (Ben Noordhuis)

* unix,win: more uv_read_start() argument validation (Ben Noordhuis)

* build: turn on -fno-strict-aliasing (Ben Noordhuis)

* stream: add uv_pipe and uv_socketpair to the API (Jameson Nash)

* unix,win: initialize timer `timeout` field (Ben Noordhuis)

* bsd-ifaddrs: improve comments (Darshan Sen)

* test: remove unnecessary uv_fs_stat() calls (Ben Noordhuis)

* fs: fix utime/futime timestamp rounding errors (Ben Noordhuis)

* test: ensure reliable floating point comparison (Jameson Nash)

* unix,fs: fix uv_fs_sendfile() (Santiago Gimeno)

* unix: fix uv_fs_stat when using statx (Simon Kadisch)

* linux,macos: fix uv_set_process_title regression (Momtchil Momtchev)

* doc: clarify UDP errors and recvmmsg (Ethel Weston)

* test-getaddrinfo: use example.invalid (Drew DeVault)

* Revert "build: fix android autotools build" (Bernardo Ramos)

* unix,fs: on DVS fs, statx returns EOPNOTSUPP (Mark Klein)

* win, fs: mkdir really return UV_EINVAL for invalid names (Nicholas Vavilov)

* tools: migrate tools/make_dist_html.py to python3 (Dominique Dumont)

* unix: fix uv_uptime() on linux (schamberg97)

* unix: check for partial copy_file_range support (Momtchil Momtchev)

* win: bump minimum supported version to windows 8 (Ben Noordhuis)

* poll,unix: ensure safety of rapid fd reuse (Bob Weinand)

* test: fix some warnings (Issam E. Maghni)

* unix: fix uv_uptime() regression (Santiago Gimeno)

* doc: fix versionadded metadata (cjihrig)

* test: fix 'incompatible pointer types' warnings (cjihrig)

* unix: check for EXDEV in uv__fs_sendfile() (Darshan Sen)


2020.09.26, Version 1.40.0 (Stable), 4e69e333252693bd82d6338d6124f0416538dbfc

Changes since version 1.39.0:

* udp: add UV_UDP_MMSG_FREE recv_cb flag (Ryan Liptak)

* include: re-map UV__EPROTO from 4046 to -4046 (YuMeiJie)

* doc: correct UV_UDP_MMSG_FREE version added (cjihrig)

* doc: add uv_metrics_idle_time() version metadata (Ryan Liptak)

* win,tty: pass through utf-16 surrogate pairs (Mustafa M)

* unix: fix DragonFly BSD build (Aleksej Lebedev)

* win,udp: fix error code returned by connect() (Santiago Gimeno)

* src: suppress user_timeout maybe-uninitialized (Daniel Bevenius)

* test: fix compiler warning (Vladimír unát)

* build: fix the Haiku cmake build (David Carlier)

* linux: fix i386 sendmmsg/recvmmsg support (Ben Noordhuis)

* build: add libuv-static pkg-config file (Nikolay Mitev)

* unix,win: add uv_timer_get_due_in() (Ulrik Strid)

* build,unix: add QNX support (Elad Lahav)

* include: remove incorrect UV__ERR() for EPROTO (cjihrig)


2020.08.26, Version 1.39.0 (Stable), 25f4b8b8a3c0f934158cd37a37b0525d75ca488e

Changes since version 1.38.1:

* unix: use relaxed loads/stores for clock id (Ben Noordhuis)

* build,win: link to user32.lib and advapi32.lib (George Zhao)

* unix: squelch harmless valgrind warning (ssrlive)

* include: fx c++ style comments warnings (Turbinya)

* build,cmake: Change installation location on MinGW (erw7)

* linux: use copy_file_range for uv_fs_copyfile when possible (Carter Li)

* win,tcp: avoid reinserting a pending request (

* docs: improve the descriptions for get memory info (Juan Sebastian velez
  Posada)

* test: add udp-mmsg test (Ryan Liptak)

* udp: add uv_udp_using_recvmmsg query (Ryan Liptak)

* doc: add more error constants (TK-one)

* zos: fix potential event loop stall (Trevor Norris)

* include: add internal fields struct to uv_loop_t (Trevor Norris)

* core: add API to measure event loop idle time (Trevor Norris)

* win,fs: use CreateDirectoryW instead of _wmkdir (Mustafa M)

* win,nfc: fix integer comparison signedness (escherstair)

* win,nfc: use

* win,nfc: removed some unused variables (escherstair)

* win,nfc: add missing return statement (escherstair)

* win,nfc: disable clang-format for

* darwin: use IOKit for uv_cpu_info (Evan Lucas)

* test: fix thread race in process_title_threadsafe (Ben Noordhuis)

* win,fs: avoid implicit access to _doserrno (Jameson Nash)

* test: give hrtime test a custom 20s timeout (Jameson Nash)

* build: add more failed test, for qemu version bump (gengjiawen)

* unix: handle src, dest same in uv_fs_copyfile() (cjihrig)

* unix: error when uv_setup_args() is not called (Ryan Liptak)

* aix: protect uv_exepath() from uv_set_process_title() (Richard Lau)

* fs: clobber req->path on uv_fs_mkstemp() error (tjarlama)

* cmake: fix compile error C2001 on Chinese Windows (¸åíîÞé¿å)

* test: avoid double evaluation in ASSERT_BASE macro (tjarlama)

* tcp: fail instantly if local port is unbound (Bartosz Sosnowski)

* doc: fix most sphinx warnings (Jameson Nash)

* nfci: address some style nits (Jameson Nash)

* unix: don't use _POSIX_PATH_MAX (Ben Noordhuis)


2020.07.04, Version 1.38.1 (Stable), e8b989ea1f7f9d4083511a2caec7791e9abd1871

Changes since version 1.38.0:

* test: use last matching qemu version (cjihrig)

* win, util: rearrange uv_hrtime (Bartosz Sosnowski)

* test: skip signal_multiple_loops test on QEMU (gengjiawen)

* build: add android build to CI (gengjiawen)

* test: extend fs_event_error_reporting timeout (cjihrig)

* build: link libkvm on netbsd only (Alexander Tokmakov)

* linux: refactor /proc file reader logic (Ben Noordhuis)

* linux: read load average from /proc/loadavg (Ben Noordhuis)

* android: remove patch code for below 21 (gengjiawen)

* win: fix visual studio 2008 build (Arenoros)

* win,tty: fix deadlock caused by inconsistent state (lander0s)

* unix: use relaxed loads/stores for feature checks (Ben Noordhuis)

* build: don't .gitignore m4/ax_pthread.m4 (Ben Noordhuis)

* unix: fix gcc atomics feature check (Ben Noordhuis)

* darwin: work around clock jumping back in time (Ben Noordhuis)

* udp: fix write_queue cleanup on sendmmsg error (Santiago Gimeno)

* src: build fix for Android (David Carlier)

/* 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 <assert.h>
#include <stdlib.h>

#include "uv.h"
#include "internal.h"


/* Whether there are any non-IFS LSPs stacked on TCP */
int uv_tcp_non_ifs_lsp_ipv4;
int uv_tcp_non_ifs_lsp_ipv6;

/* Ip address used to bind to any port at any interface */
struct sockaddr_in uv_addr_ip4_any_;
struct sockaddr_in6 uv_addr_ip6_any_;


/*
 * Retrieves the pointer to a winsock extension function.
 */
static BOOL uv__get_extension_function(SOCKET socket, GUID guid,
    void **target) {
  int result;
  DWORD bytes;

  result = WSAIoctl(socket,
                    SIO_GET_EXTENSION_FUNCTION_POINTER,
                    &guid,
                    sizeof(guid),
                    (void*)target,
                    sizeof(*target),
                    &bytes,
                    NULL,
                    NULL);

  if (result == SOCKET_ERROR) {
    *target = NULL;
    return FALSE;
  } else {
    return TRUE;
  }
}


BOOL uv__get_acceptex_function(SOCKET socket, LPFN_ACCEPTEX* target) {
  const GUID wsaid_acceptex = WSAID_ACCEPTEX;
  return uv__get_extension_function(socket, wsaid_acceptex, (void**)target);
}


BOOL uv__get_connectex_function(SOCKET socket, LPFN_CONNECTEX* target) {
  const GUID wsaid_connectex = WSAID_CONNECTEX;
  return uv__get_extension_function(socket, wsaid_connectex, (void**)target);
}



void uv__winsock_init(void) {
  WSADATA wsa_data;
  int errorno;
  SOCKET dummy;
  WSAPROTOCOL_INFOW protocol_info;
  int opt_len;

  /* Set implicit binding address used by connectEx */
  if (uv_ip4_addr("0.0.0.0", 0, &uv_addr_ip4_any_)) {
    abort();
  }

  if (uv_ip6_addr("::", 0, &uv_addr_ip6_any_)) {
    abort();
  }

  /* Skip initialization in safe mode without network support */
  if (1 == GetSystemMetrics(SM_CLEANBOOT)) return;

  /* Initialize winsock */
  errorno = WSAStartup(MAKEWORD(2, 2), &wsa_data);
  if (errorno != 0) {
    uv_fatal_error(errorno, "WSAStartup");
  }

  /* Try to detect non-IFS LSPs */
  uv_tcp_non_ifs_lsp_ipv4 = 1;
  dummy = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
  if (dummy != INVALID_SOCKET) {
    opt_len = (int) sizeof protocol_info;
    if (getsockopt(dummy,
                   SOL_SOCKET,
                   SO_PROTOCOL_INFOW,
                   (char*) &protocol_info,
                   &opt_len) == 0) {
      if (protocol_info.dwServiceFlags1 & XP1_IFS_HANDLES)
        uv_tcp_non_ifs_lsp_ipv4 = 0;
    }
    closesocket(dummy);
  }

  /* Try to detect IPV6 support and non-IFS LSPs */
  uv_tcp_non_ifs_lsp_ipv6 = 1;
  dummy = socket(AF_INET6, SOCK_STREAM, IPPROTO_IP);
  if (dummy != INVALID_SOCKET) {
    opt_len = (int) sizeof protocol_info;
    if (getsockopt(dummy,
                   SOL_SOCKET,
                   SO_PROTOCOL_INFOW,
                   (char*) &protocol_info,
                   &opt_len) == 0) {
      if (protocol_info.dwServiceFlags1 & XP1_IFS_HANDLES)
        uv_tcp_non_ifs_lsp_ipv6 = 0;
    }
    closesocket(dummy);
  }
}


int uv__ntstatus_to_winsock_error(NTSTATUS status) {
  switch (status) {
    case STATUS_SUCCESS:
      return ERROR_SUCCESS;

    case STATUS_PENDING:
      return ERROR_IO_PENDING;

    case STATUS_INVALID_HANDLE:
    case STATUS_OBJECT_TYPE_MISMATCH:
      return WSAENOTSOCK;

    case STATUS_INSUFFICIENT_RESOURCES:
    case STATUS_PAGEFILE_QUOTA:
    case STATUS_COMMITMENT_LIMIT:
    case STATUS_WORKING_SET_QUOTA:
    case STATUS_NO_MEMORY:
    case STATUS_QUOTA_EXCEEDED:
    case STATUS_TOO_MANY_PAGING_FILES:
    case STATUS_REMOTE_RESOURCES:
      return WSAENOBUFS;

    case STATUS_TOO_MANY_ADDRESSES:
    case STATUS_SHARING_VIOLATION:
    case STATUS_ADDRESS_ALREADY_EXISTS:
      return WSAEADDRINUSE;

    case STATUS_LINK_TIMEOUT:
    case STATUS_IO_TIMEOUT:
    case STATUS_TIMEOUT:
      return WSAETIMEDOUT;

    case STATUS_GRACEFUL_DISCONNECT:
      return WSAEDISCON;

    case STATUS_REMOTE_DISCONNECT:
    case STATUS_CONNECTION_RESET:
    case STATUS_LINK_FAILED:
    case STATUS_CONNECTION_DISCONNECTED:
    case STATUS_PORT_UNREACHABLE:
    case STATUS_HOPLIMIT_EXCEEDED:
      return WSAECONNRESET;

    case STATUS_LOCAL_DISCONNECT:
    case STATUS_TRANSACTION_ABORTED:
    case STATUS_CONNECTION_ABORTED:
      return WSAECONNABORTED;

    case STATUS_BAD_NETWORK_PATH:
    case STATUS_NETWORK_UNREACHABLE:
    case STATUS_PROTOCOL_UNREACHABLE:
      return WSAENETUNREACH;

    case STATUS_HOST_UNREACHABLE:
      return WSAEHOSTUNREACH;

    case STATUS_CANCELLED:
    case STATUS_REQUEST_ABORTED:
      return WSAEINTR;

    case STATUS_BUFFER_OVERFLOW:
    case STATUS_INVALID_BUFFER_SIZE:
      return WSAEMSGSIZE;

    case STATUS_BUFFER_TOO_SMALL:
    case STATUS_ACCESS_VIOLATION:
      return WSAEFAULT;

    case STATUS_DEVICE_NOT_READY:
    case STATUS_REQUEST_NOT_ACCEPTED:
      return WSAEWOULDBLOCK;

    case STATUS_INVALID_NETWORK_RESPONSE:
    case STATUS_NETWORK_BUSY:
    case STATUS_NO_SUCH_DEVICE:
    case STATUS_NO_SUCH_FILE:
    case STATUS_OBJECT_PATH_NOT_FOUND:
    case STATUS_OBJECT_NAME_NOT_FOUND:
    case STATUS_UNEXPECTED_NETWORK_ERROR:
      return WSAENETDOWN;

    case STATUS_INVALID_CONNECTION:
      return WSAENOTCONN;

    case STATUS_REMOTE_NOT_LISTENING:
    case STATUS_CONNECTION_REFUSED:
      return WSAECONNREFUSED;

    case STATUS_PIPE_DISCONNECTED:
      return WSAESHUTDOWN;

    case STATUS_CONFLICTING_ADDRESSES:
    case STATUS_INVALID_ADDRESS:
    case STATUS_INVALID_ADDRESS_COMPONENT:
      return WSAEADDRNOTAVAIL;

    case STATUS_NOT_SUPPORTED:
    case STATUS_NOT_IMPLEMENTED:
      return WSAEOPNOTSUPP;

    case STATUS_ACCESS_DENIED:
      return WSAEACCES;

    default:
      if ((status & (FACILITY_NTWIN32 << 16)) == (FACILITY_NTWIN32 << 16) &&
          (status & (ERROR_SEVERITY_ERROR | ERROR_SEVERITY_WARNING))) {
        /* It's a windows error that has been previously mapped to an ntstatus
         * code. */
        return (DWORD) (status & 0xffff);
      } else {
        /* The default fallback for unmappable ntstatus codes. */
        return WSAEINVAL;
      }
  }
}


/*
 * This function provides a workaround for a bug in the winsock implementation
 * of WSARecv. The problem is that when SetFileCompletionNotificationModes is
 * used to avoid IOCP notifications of completed reads, WSARecv does not
 * reliably indicate whether we can expect a completion package to be posted
 * when the receive buffer is smaller than the received datagram.
 *
 * However it is desirable to use SetFileCompletionNotificationModes because
 * it yields a massive performance increase.
 *
 * This function provides a workaround for that bug, but it only works for the
 * specific case that we need it for. E.g. it assumes that the "avoid iocp"
 * bit has been set, and supports only overlapped operation. It also requires
 * the user to use the default msafd driver, doesn't work when other LSPs are
 * stacked on top of it.
 */
int WSAAPI uv__wsarecv_workaround(SOCKET socket, WSABUF* buffers,
    DWORD buffer_count, DWORD* bytes, DWORD* flags, WSAOVERLAPPED *overlapped,
    LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine) {
  NTSTATUS status;
  void* apc_context;
  IO_STATUS_BLOCK* iosb = (IO_STATUS_BLOCK*) &overlapped->Internal;
  AFD_RECV_INFO info;
  DWORD error;

  if (overlapped == NULL || completion_routine != NULL) {
    WSASetLastError(WSAEINVAL);
    return SOCKET_ERROR;
  }

  info.BufferArray = buffers;
  info.BufferCount = buffer_count;
  info.AfdFlags = AFD_OVERLAPPED;
  info.TdiFlags = TDI_RECEIVE_NORMAL;

  if (*flags & MSG_PEEK) {
    info.TdiFlags |= TDI_RECEIVE_PEEK;
  }

  if (*flags & MSG_PARTIAL) {
    info.TdiFlags |= TDI_RECEIVE_PARTIAL;
  }

  if (!((intptr_t) overlapped->hEvent & 1)) {
    apc_context = (void*) overlapped;
  } else {
    apc_context = NULL;
  }

  iosb->Status = STATUS_PENDING;
  iosb->Pointer = 0;

  status = pNtDeviceIoControlFile((HANDLE) socket,
                                  overlapped->hEvent,
                                  NULL,
                                  apc_context,
                                  iosb,
                                  IOCTL_AFD_RECEIVE,
                                  &info,
                                  sizeof(info),
                                  NULL,
                                  0);

  *flags = 0;
  *bytes = (DWORD) iosb->Information;

  switch (status) {
    case STATUS_SUCCESS:
      error = ERROR_SUCCESS;
      break;

    case STATUS_PENDING:
      error = WSA_IO_PENDING;
      break;

    case STATUS_BUFFER_OVERFLOW:
      error = WSAEMSGSIZE;
      break;

    case STATUS_RECEIVE_EXPEDITED:
      error = ERROR_SUCCESS;
      *flags = MSG_OOB;
      break;

    case STATUS_RECEIVE_PARTIAL_EXPEDITED:
      error = ERROR_SUCCESS;
      *flags = MSG_PARTIAL | MSG_OOB;
      break;

    case STATUS_RECEIVE_PARTIAL:
      error = ERROR_SUCCESS;
      *flags = MSG_PARTIAL;
      break;

    default:
      error = uv__ntstatus_to_winsock_error(status);
      break;
  }

  WSASetLastError(error);

  if (error == ERROR_SUCCESS) {
    return 0;
  } else {
    return SOCKET_ERROR;
  }
}


/* See description of uv__wsarecv_workaround. */
int WSAAPI uv__wsarecvfrom_workaround(SOCKET socket, WSABUF* buffers,
    DWORD buffer_count, DWORD* bytes, DWORD* flags, struct sockaddr* addr,
    int* addr_len, WSAOVERLAPPED *overlapped,
    LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine) {
  NTSTATUS status;
  void* apc_context;
  IO_STATUS_BLOCK* iosb = (IO_STATUS_BLOCK*) &overlapped->Internal;
  AFD_RECV_DATAGRAM_INFO info;
  DWORD error;

  if (overlapped == NULL || addr == NULL || addr_len == NULL ||
      completion_routine != NULL) {
    WSASetLastError(WSAEINVAL);
    return SOCKET_ERROR;
  }

  info.BufferArray = buffers;
  info.BufferCount = buffer_count;
  info.AfdFlags = AFD_OVERLAPPED;
  info.TdiFlags = TDI_RECEIVE_NORMAL;
  info.Address = addr;
  info.AddressLength = addr_len;

  if (*flags & MSG_PEEK) {
    info.TdiFlags |= TDI_RECEIVE_PEEK;
  }

  if (*flags & MSG_PARTIAL) {
    info.TdiFlags |= TDI_RECEIVE_PARTIAL;
  }

  if (!((intptr_t) overlapped->hEvent & 1)) {
    apc_context = (void*) overlapped;
  } else {
    apc_context = NULL;
  }

  iosb->Status = STATUS_PENDING;
  iosb->Pointer = 0;

  status = pNtDeviceIoControlFile((HANDLE) socket,
                                  overlapped->hEvent,
                                  NULL,
                                  apc_context,
                                  iosb,
                                  IOCTL_AFD_RECEIVE_DATAGRAM,
                                  &info,
                                  sizeof(info),
                                  NULL,
                                  0);

  *flags = 0;
  *bytes = (DWORD) iosb->Information;

  switch (status) {
    case STATUS_SUCCESS:
      error = ERROR_SUCCESS;
      break;

    case STATUS_PENDING:
      error = WSA_IO_PENDING;
      break;

    case STATUS_BUFFER_OVERFLOW:
      error = WSAEMSGSIZE;
      break;

    case STATUS_RECEIVE_EXPEDITED:
      error = ERROR_SUCCESS;
      *flags = MSG_OOB;
      break;

    case STATUS_RECEIVE_PARTIAL_EXPEDITED:
      error = ERROR_SUCCESS;
      *flags = MSG_PARTIAL | MSG_OOB;
      break;

    case STATUS_RECEIVE_PARTIAL:
      error = ERROR_SUCCESS;
      *flags = MSG_PARTIAL;
      break;

    default:
      error = uv__ntstatus_to_winsock_error(status);
      break;
  }

  WSASetLastError(error);

  if (error == ERROR_SUCCESS) {
    return 0;
  } else {
    return SOCKET_ERROR;
  }
}


int WSAAPI uv__msafd_poll(SOCKET socket, AFD_POLL_INFO* info_in,
    AFD_POLL_INFO* info_out, OVERLAPPED* overlapped) {
  IO_STATUS_BLOCK iosb;
  IO_STATUS_BLOCK* iosb_ptr;
  HANDLE event = NULL;
  void* apc_context;
  NTSTATUS status;
  DWORD error;

  if (overlapped != NULL) {
    /* Overlapped operation. */
    iosb_ptr = (IO_STATUS_BLOCK*) &overlapped->Internal;
    event = overlapped->hEvent;

    /* Do not report iocp completion if hEvent is tagged. */
    if ((uintptr_t) event & 1) {
      event = (HANDLE)((uintptr_t) event & ~(uintptr_t) 1);
      apc_context = NULL;
    } else {
      apc_context = overlapped;
    }

  } else {
    /* Blocking operation. */
    iosb_ptr = &iosb;
    event = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (event == NULL) {
      return SOCKET_ERROR;
    }
    apc_context = NULL;
  }

  iosb_ptr->Status = STATUS_PENDING;
  status = pNtDeviceIoControlFile((HANDLE) socket,
                                  event,
                                  NULL,
                                  apc_context,
                                  iosb_ptr,
                                  IOCTL_AFD_POLL,
                                  info_in,
                                  sizeof *info_in,
                                  info_out,
                                  sizeof *info_out);

  if (overlapped == NULL) {
    /* If this is a blocking operation, wait for the event to become signaled,
     * and then grab the real status from the io status block. */
    if (status == STATUS_PENDING) {
      DWORD r = WaitForSingleObject(event, INFINITE);

      if (r == WAIT_FAILED) {
        DWORD saved_error = GetLastError();
        CloseHandle(event);
        WSASetLastError(saved_error);
        return SOCKET_ERROR;
      }

      status = iosb.Status;
    }

    CloseHandle(event);
  }

  switch (status) {
    case STATUS_SUCCESS:
      error = ERROR_SUCCESS;
      break;

    case STATUS_PENDING:
      error = WSA_IO_PENDING;
      break;

    default:
      error = uv__ntstatus_to_winsock_error(status);
      break;
  }

  WSASetLastError(error);

  if (error == ERROR_SUCCESS) {
    return 0;
  } else {
    return SOCKET_ERROR;
  }
}

int uv__convert_to_localhost_if_unspecified(const struct sockaddr* addr,
                                            struct sockaddr_storage* storage) {
  struct sockaddr_in* dest4;
  struct sockaddr_in6* dest6;

  if (addr == NULL)
    return UV_EINVAL;

  switch (addr->sa_family) {
  case AF_INET:
    dest4 = (struct sockaddr_in*) storage;
    memcpy(dest4, addr, sizeof(*dest4));
    if (dest4->sin_addr.s_addr == 0)
      dest4->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    return 0;
  case AF_INET6:
    dest6 = (struct sockaddr_in6*) storage;
    memcpy(dest6, addr, sizeof(*dest6));
    if (memcmp(&dest6->sin6_addr,
               &uv_addr_ip6_any_.sin6_addr,
               sizeof(uv_addr_ip6_any_.sin6_addr)) == 0) {
      struct in6_addr init_sin6_addr = IN6ADDR_LOOPBACK_INIT;
      dest6->sin6_addr = init_sin6_addr;
    }
    return 0;
  default:
    return UV_EINVAL;
  }
}