1/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 * 3 * Permission is hereby granted, free of charge, to any person obtaining a copy 4 * of this software and associated documentation files (the "Software"), to 5 * deal in the Software without restriction, including without limitation the 6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 * sell copies of the Software, and to permit persons to whom the Software is 8 * furnished to do so, subject to the following conditions: 9 * 10 * The above copyright notice and this permission notice shall be included in 11 * all copies or substantial portions of the Software. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 * IN THE SOFTWARE. 20 */ 21 22#ifndef UV_WIN_INTERNAL_H_ 23#define UV_WIN_INTERNAL_H_ 24 25#include "uv.h" 26#include "../uv-common.h" 27 28#include "uv/tree.h" 29#include "winapi.h" 30#include "winsock.h" 31 32#define UV_LOOP_MAGIC 0x700B700BU 33 34#ifdef _MSC_VER 35# define INLINE __inline 36# define UV_THREAD_LOCAL __declspec( thread ) 37#else 38# define INLINE inline 39# define UV_THREAD_LOCAL __thread 40#endif 41 42 43#ifdef _DEBUG 44 45extern UV_THREAD_LOCAL int uv__crt_assert_enabled; 46 47#define UV_BEGIN_DISABLE_CRT_ASSERT() \ 48 { \ 49 int uv__saved_crt_assert_enabled = uv__crt_assert_enabled; \ 50 uv__crt_assert_enabled = FALSE; 51 52 53#define UV_END_DISABLE_CRT_ASSERT() \ 54 uv__crt_assert_enabled = uv__saved_crt_assert_enabled; \ 55 } 56 57#else 58#define UV_BEGIN_DISABLE_CRT_ASSERT() 59#define UV_END_DISABLE_CRT_ASSERT() 60#endif 61 62/* 63 * TCP 64 */ 65 66typedef enum { 67 UV__IPC_SOCKET_XFER_NONE = 0, 68 UV__IPC_SOCKET_XFER_TCP_CONNECTION, 69 UV__IPC_SOCKET_XFER_TCP_SERVER 70} uv__ipc_socket_xfer_type_t; 71 72typedef struct { 73 WSAPROTOCOL_INFOW socket_info; 74 uint32_t delayed_error; 75} uv__ipc_socket_xfer_info_t; 76 77int uv__tcp_listen(uv_tcp_t* handle, int backlog, uv_connection_cb cb); 78int uv__tcp_accept(uv_tcp_t* server, uv_tcp_t* client); 79int uv__tcp_read_start(uv_tcp_t* handle, uv_alloc_cb alloc_cb, 80 uv_read_cb read_cb); 81int uv__tcp_write(uv_loop_t* loop, uv_write_t* req, uv_tcp_t* handle, 82 const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb); 83int uv__tcp_try_write(uv_tcp_t* handle, const uv_buf_t bufs[], 84 unsigned int nbufs); 85 86void uv__process_tcp_read_req(uv_loop_t* loop, uv_tcp_t* handle, uv_req_t* req); 87void uv__process_tcp_write_req(uv_loop_t* loop, uv_tcp_t* handle, 88 uv_write_t* req); 89void uv__process_tcp_accept_req(uv_loop_t* loop, uv_tcp_t* handle, 90 uv_req_t* req); 91void uv__process_tcp_connect_req(uv_loop_t* loop, uv_tcp_t* handle, 92 uv_connect_t* req); 93void uv__process_tcp_shutdown_req(uv_loop_t* loop, 94 uv_tcp_t* stream, 95 uv_shutdown_t* req); 96 97void uv__tcp_close(uv_loop_t* loop, uv_tcp_t* tcp); 98void uv__tcp_endgame(uv_loop_t* loop, uv_tcp_t* handle); 99 100int uv__tcp_xfer_export(uv_tcp_t* handle, 101 int pid, 102 uv__ipc_socket_xfer_type_t* xfer_type, 103 uv__ipc_socket_xfer_info_t* xfer_info); 104int uv__tcp_xfer_import(uv_tcp_t* tcp, 105 uv__ipc_socket_xfer_type_t xfer_type, 106 uv__ipc_socket_xfer_info_t* xfer_info); 107 108 109/* 110 * UDP 111 */ 112void uv__process_udp_recv_req(uv_loop_t* loop, uv_udp_t* handle, uv_req_t* req); 113void uv__process_udp_send_req(uv_loop_t* loop, uv_udp_t* handle, 114 uv_udp_send_t* req); 115 116void uv__udp_close(uv_loop_t* loop, uv_udp_t* handle); 117void uv__udp_endgame(uv_loop_t* loop, uv_udp_t* handle); 118 119 120/* 121 * Pipes 122 */ 123int uv__create_stdio_pipe_pair(uv_loop_t* loop, 124 uv_pipe_t* parent_pipe, HANDLE* child_pipe_ptr, unsigned int flags); 125 126int uv__pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb); 127int uv__pipe_accept(uv_pipe_t* server, uv_stream_t* client); 128int uv__pipe_read_start(uv_pipe_t* handle, uv_alloc_cb alloc_cb, 129 uv_read_cb read_cb); 130void uv__pipe_read_stop(uv_pipe_t* handle); 131int uv__pipe_write(uv_loop_t* loop, 132 uv_write_t* req, 133 uv_pipe_t* handle, 134 const uv_buf_t bufs[], 135 size_t nbufs, 136 uv_stream_t* send_handle, 137 uv_write_cb cb); 138void uv__pipe_shutdown(uv_loop_t* loop, uv_pipe_t* handle, uv_shutdown_t* req); 139 140void uv__process_pipe_read_req(uv_loop_t* loop, uv_pipe_t* handle, 141 uv_req_t* req); 142void uv__process_pipe_write_req(uv_loop_t* loop, uv_pipe_t* handle, 143 uv_write_t* req); 144void uv__process_pipe_accept_req(uv_loop_t* loop, uv_pipe_t* handle, 145 uv_req_t* raw_req); 146void uv__process_pipe_connect_req(uv_loop_t* loop, uv_pipe_t* handle, 147 uv_connect_t* req); 148void uv__process_pipe_shutdown_req(uv_loop_t* loop, uv_pipe_t* handle, 149 uv_shutdown_t* req); 150 151void uv__pipe_close(uv_loop_t* loop, uv_pipe_t* handle); 152void uv__pipe_endgame(uv_loop_t* loop, uv_pipe_t* handle); 153 154 155/* 156 * TTY 157 */ 158void uv__console_init(void); 159 160int uv__tty_read_start(uv_tty_t* handle, uv_alloc_cb alloc_cb, 161 uv_read_cb read_cb); 162int uv__tty_read_stop(uv_tty_t* handle); 163int uv__tty_write(uv_loop_t* loop, uv_write_t* req, uv_tty_t* handle, 164 const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb); 165int uv__tty_try_write(uv_tty_t* handle, const uv_buf_t bufs[], 166 unsigned int nbufs); 167void uv__tty_close(uv_tty_t* handle); 168 169void uv__process_tty_read_req(uv_loop_t* loop, uv_tty_t* handle, 170 uv_req_t* req); 171void uv__process_tty_write_req(uv_loop_t* loop, uv_tty_t* handle, 172 uv_write_t* req); 173#define uv__process_tty_accept_req(loop, handle, req) abort() 174#define uv__process_tty_connect_req(loop, handle, req) abort() 175void uv__process_tty_shutdown_req(uv_loop_t* loop, 176 uv_tty_t* stream, 177 uv_shutdown_t* req); 178void uv__tty_endgame(uv_loop_t* loop, uv_tty_t* handle); 179 180 181/* 182 * Poll watchers 183 */ 184void uv__process_poll_req(uv_loop_t* loop, uv_poll_t* handle, 185 uv_req_t* req); 186 187int uv__poll_close(uv_loop_t* loop, uv_poll_t* handle); 188void uv__poll_endgame(uv_loop_t* loop, uv_poll_t* handle); 189 190 191/* 192 * Loop watchers 193 */ 194void uv__loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle); 195 196void uv__prepare_invoke(uv_loop_t* loop); 197void uv__check_invoke(uv_loop_t* loop); 198void uv__idle_invoke(uv_loop_t* loop); 199 200void uv__once_init(void); 201 202 203/* 204 * Async watcher 205 */ 206void uv__async_close(uv_loop_t* loop, uv_async_t* handle); 207void uv__async_endgame(uv_loop_t* loop, uv_async_t* handle); 208 209void uv__process_async_wakeup_req(uv_loop_t* loop, uv_async_t* handle, 210 uv_req_t* req); 211 212 213/* 214 * Signal watcher 215 */ 216void uv__signals_init(void); 217int uv__signal_dispatch(int signum); 218 219void uv__signal_close(uv_loop_t* loop, uv_signal_t* handle); 220void uv__signal_endgame(uv_loop_t* loop, uv_signal_t* handle); 221 222void uv__process_signal_req(uv_loop_t* loop, uv_signal_t* handle, 223 uv_req_t* req); 224 225 226/* 227 * Spawn 228 */ 229void uv__process_proc_exit(uv_loop_t* loop, uv_process_t* handle); 230void uv__process_close(uv_loop_t* loop, uv_process_t* handle); 231void uv__process_endgame(uv_loop_t* loop, uv_process_t* handle); 232 233 234/* 235 * FS 236 */ 237void uv__fs_init(void); 238 239 240/* 241 * FS Event 242 */ 243void uv__process_fs_event_req(uv_loop_t* loop, uv_req_t* req, 244 uv_fs_event_t* handle); 245void uv__fs_event_close(uv_loop_t* loop, uv_fs_event_t* handle); 246void uv__fs_event_endgame(uv_loop_t* loop, uv_fs_event_t* handle); 247 248 249/* 250 * Stat poller. 251 */ 252void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle); 253 254 255/* 256 * Utilities. 257 */ 258void uv__util_init(void); 259 260uint64_t uv__hrtime(unsigned int scale); 261__declspec(noreturn) void uv_fatal_error(const int errorno, const char* syscall); 262int uv__convert_utf16_to_utf8(const WCHAR* utf16, size_t utf16len, char** utf8); 263int uv__copy_utf16_to_utf8(const WCHAR* utf16, size_t utf16len, char* utf8, size_t *size); 264int uv__convert_utf8_to_utf16(const char* utf8, WCHAR** utf16); 265 266typedef int (WINAPI *uv__peersockfunc)(SOCKET, struct sockaddr*, int*); 267 268int uv__getsockpeername(const uv_handle_t* handle, 269 uv__peersockfunc func, 270 struct sockaddr* name, 271 int* namelen, 272 int delayed_error); 273 274int uv__random_rtlgenrandom(void* buf, size_t buflen); 275 276 277/* 278 * Process stdio handles. 279 */ 280int uv__stdio_create(uv_loop_t* loop, 281 const uv_process_options_t* options, 282 BYTE** buffer_ptr); 283void uv__stdio_destroy(BYTE* buffer); 284void uv__stdio_noinherit(BYTE* buffer); 285int uv__stdio_verify(BYTE* buffer, WORD size); 286WORD uv__stdio_size(BYTE* buffer); 287HANDLE uv__stdio_handle(BYTE* buffer, int fd); 288 289 290/* 291 * Winapi and ntapi utility functions 292 */ 293void uv__winapi_init(void); 294 295 296/* 297 * Winsock utility functions 298 */ 299void uv__winsock_init(void); 300 301int uv__ntstatus_to_winsock_error(NTSTATUS status); 302 303BOOL uv__get_acceptex_function(SOCKET socket, LPFN_ACCEPTEX* target); 304BOOL uv__get_connectex_function(SOCKET socket, LPFN_CONNECTEX* target); 305 306int WSAAPI uv__wsarecv_workaround(SOCKET socket, WSABUF* buffers, 307 DWORD buffer_count, DWORD* bytes, DWORD* flags, WSAOVERLAPPED *overlapped, 308 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine); 309int WSAAPI uv__wsarecvfrom_workaround(SOCKET socket, WSABUF* buffers, 310 DWORD buffer_count, DWORD* bytes, DWORD* flags, struct sockaddr* addr, 311 int* addr_len, WSAOVERLAPPED *overlapped, 312 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine); 313 314int WSAAPI uv__msafd_poll(SOCKET socket, AFD_POLL_INFO* info_in, 315 AFD_POLL_INFO* info_out, OVERLAPPED* overlapped); 316 317/* Whether there are any non-IFS LSPs stacked on TCP */ 318extern int uv_tcp_non_ifs_lsp_ipv4; 319extern int uv_tcp_non_ifs_lsp_ipv6; 320 321/* Ip address used to bind to any port at any interface */ 322extern struct sockaddr_in uv_addr_ip4_any_; 323extern struct sockaddr_in6 uv_addr_ip6_any_; 324 325/* 326 * Wake all loops with fake message 327 */ 328void uv__wake_all_loops(void); 329 330/* 331 * Init system wake-up detection 332 */ 333void uv__init_detect_system_wakeup(void); 334 335#endif /* UV_WIN_INTERNAL_H_ */ 336