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 #ifdef _MSC_VER 33 # define INLINE __inline 34 # define UV_THREAD_LOCAL __declspec( thread ) 35 #else 36 # define INLINE inline 37 # define UV_THREAD_LOCAL __thread 38 #endif 39 40 41 #ifdef _DEBUG 42 43 extern UV_THREAD_LOCAL int uv__crt_assert_enabled; 44 45 #define UV_BEGIN_DISABLE_CRT_ASSERT() \ 46 { \ 47 int uv__saved_crt_assert_enabled = uv__crt_assert_enabled; \ 48 uv__crt_assert_enabled = FALSE; 49 50 51 #define UV_END_DISABLE_CRT_ASSERT() \ 52 uv__crt_assert_enabled = uv__saved_crt_assert_enabled; \ 53 } 54 55 #else 56 #define UV_BEGIN_DISABLE_CRT_ASSERT() 57 #define UV_END_DISABLE_CRT_ASSERT() 58 #endif 59 60 /* 61 * TCP 62 */ 63 64 typedef enum { 65 UV__IPC_SOCKET_XFER_NONE = 0, 66 UV__IPC_SOCKET_XFER_TCP_CONNECTION, 67 UV__IPC_SOCKET_XFER_TCP_SERVER 68 } uv__ipc_socket_xfer_type_t; 69 70 typedef struct { 71 WSAPROTOCOL_INFOW socket_info; 72 uint32_t delayed_error; 73 } uv__ipc_socket_xfer_info_t; 74 75 int uv__tcp_listen(uv_tcp_t* handle, int backlog, uv_connection_cb cb); 76 int uv__tcp_accept(uv_tcp_t* server, uv_tcp_t* client); 77 int uv__tcp_read_start(uv_tcp_t* handle, uv_alloc_cb alloc_cb, 78 uv_read_cb read_cb); 79 int uv__tcp_write(uv_loop_t* loop, uv_write_t* req, uv_tcp_t* handle, 80 const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb); 81 int uv__tcp_try_write(uv_tcp_t* handle, const uv_buf_t bufs[], 82 unsigned int nbufs); 83 84 void uv__process_tcp_read_req(uv_loop_t* loop, uv_tcp_t* handle, uv_req_t* req); 85 void uv__process_tcp_write_req(uv_loop_t* loop, uv_tcp_t* handle, 86 uv_write_t* req); 87 void uv__process_tcp_accept_req(uv_loop_t* loop, uv_tcp_t* handle, 88 uv_req_t* req); 89 void uv__process_tcp_connect_req(uv_loop_t* loop, uv_tcp_t* handle, 90 uv_connect_t* req); 91 void uv__process_tcp_shutdown_req(uv_loop_t* loop, 92 uv_tcp_t* stream, 93 uv_shutdown_t* req); 94 95 void uv__tcp_close(uv_loop_t* loop, uv_tcp_t* tcp); 96 void uv__tcp_endgame(uv_loop_t* loop, uv_tcp_t* handle); 97 98 int uv__tcp_xfer_export(uv_tcp_t* handle, 99 int pid, 100 uv__ipc_socket_xfer_type_t* xfer_type, 101 uv__ipc_socket_xfer_info_t* xfer_info); 102 int uv__tcp_xfer_import(uv_tcp_t* tcp, 103 uv__ipc_socket_xfer_type_t xfer_type, 104 uv__ipc_socket_xfer_info_t* xfer_info); 105 106 107 /* 108 * UDP 109 */ 110 void uv__process_udp_recv_req(uv_loop_t* loop, uv_udp_t* handle, uv_req_t* req); 111 void uv__process_udp_send_req(uv_loop_t* loop, uv_udp_t* handle, 112 uv_udp_send_t* req); 113 114 void uv__udp_close(uv_loop_t* loop, uv_udp_t* handle); 115 void uv__udp_endgame(uv_loop_t* loop, uv_udp_t* handle); 116 117 118 /* 119 * Pipes 120 */ 121 int uv__create_stdio_pipe_pair(uv_loop_t* loop, 122 uv_pipe_t* parent_pipe, HANDLE* child_pipe_ptr, unsigned int flags); 123 124 int uv__pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb); 125 int uv__pipe_accept(uv_pipe_t* server, uv_stream_t* client); 126 int uv__pipe_read_start(uv_pipe_t* handle, uv_alloc_cb alloc_cb, 127 uv_read_cb read_cb); 128 void uv__pipe_read_stop(uv_pipe_t* handle); 129 int uv__pipe_write(uv_loop_t* loop, 130 uv_write_t* req, 131 uv_pipe_t* handle, 132 const uv_buf_t bufs[], 133 size_t nbufs, 134 uv_stream_t* send_handle, 135 uv_write_cb cb); 136 void uv__pipe_shutdown(uv_loop_t* loop, uv_pipe_t* handle, uv_shutdown_t* req); 137 138 void uv__process_pipe_read_req(uv_loop_t* loop, uv_pipe_t* handle, 139 uv_req_t* req); 140 void uv__process_pipe_write_req(uv_loop_t* loop, uv_pipe_t* handle, 141 uv_write_t* req); 142 void uv__process_pipe_accept_req(uv_loop_t* loop, uv_pipe_t* handle, 143 uv_req_t* raw_req); 144 void uv__process_pipe_connect_req(uv_loop_t* loop, uv_pipe_t* handle, 145 uv_connect_t* req); 146 void uv__process_pipe_shutdown_req(uv_loop_t* loop, uv_pipe_t* handle, 147 uv_shutdown_t* req); 148 149 void uv__pipe_close(uv_loop_t* loop, uv_pipe_t* handle); 150 void uv__pipe_endgame(uv_loop_t* loop, uv_pipe_t* handle); 151 152 153 /* 154 * TTY 155 */ 156 void uv__console_init(void); 157 158 int uv__tty_read_start(uv_tty_t* handle, uv_alloc_cb alloc_cb, 159 uv_read_cb read_cb); 160 int uv__tty_read_stop(uv_tty_t* handle); 161 int uv__tty_write(uv_loop_t* loop, uv_write_t* req, uv_tty_t* handle, 162 const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb); 163 int uv__tty_try_write(uv_tty_t* handle, const uv_buf_t bufs[], 164 unsigned int nbufs); 165 void uv__tty_close(uv_tty_t* handle); 166 167 void uv__process_tty_read_req(uv_loop_t* loop, uv_tty_t* handle, 168 uv_req_t* req); 169 void uv__process_tty_write_req(uv_loop_t* loop, uv_tty_t* handle, 170 uv_write_t* req); 171 /* 172 * uv__process_tty_accept_req() is a stub to keep DELEGATE_STREAM_REQ working 173 * TODO: find a way to remove it 174 */ 175 void uv__process_tty_accept_req(uv_loop_t* loop, uv_tty_t* handle, 176 uv_req_t* raw_req); 177 /* 178 * uv__process_tty_connect_req() is a stub to keep DELEGATE_STREAM_REQ working 179 * TODO: find a way to remove it 180 */ 181 void uv__process_tty_connect_req(uv_loop_t* loop, uv_tty_t* handle, 182 uv_connect_t* req); 183 void uv__process_tty_shutdown_req(uv_loop_t* loop, 184 uv_tty_t* stream, 185 uv_shutdown_t* req); 186 void uv__tty_endgame(uv_loop_t* loop, uv_tty_t* handle); 187 188 189 /* 190 * Poll watchers 191 */ 192 void uv__process_poll_req(uv_loop_t* loop, uv_poll_t* handle, 193 uv_req_t* req); 194 195 int uv__poll_close(uv_loop_t* loop, uv_poll_t* handle); 196 void uv__poll_endgame(uv_loop_t* loop, uv_poll_t* handle); 197 198 199 /* 200 * Loop watchers 201 */ 202 void uv__loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle); 203 204 void uv__prepare_invoke(uv_loop_t* loop); 205 void uv__check_invoke(uv_loop_t* loop); 206 void uv__idle_invoke(uv_loop_t* loop); 207 208 void uv__once_init(void); 209 210 211 /* 212 * Async watcher 213 */ 214 void uv__async_close(uv_loop_t* loop, uv_async_t* handle); 215 void uv__async_endgame(uv_loop_t* loop, uv_async_t* handle); 216 217 void uv__process_async_wakeup_req(uv_loop_t* loop, uv_async_t* handle, 218 uv_req_t* req); 219 220 221 /* 222 * Signal watcher 223 */ 224 void uv__signals_init(void); 225 int uv__signal_dispatch(int signum); 226 227 void uv__signal_close(uv_loop_t* loop, uv_signal_t* handle); 228 void uv__signal_endgame(uv_loop_t* loop, uv_signal_t* handle); 229 230 void uv__process_signal_req(uv_loop_t* loop, uv_signal_t* handle, 231 uv_req_t* req); 232 233 234 /* 235 * Spawn 236 */ 237 void uv__process_proc_exit(uv_loop_t* loop, uv_process_t* handle); 238 void uv__process_close(uv_loop_t* loop, uv_process_t* handle); 239 void uv__process_endgame(uv_loop_t* loop, uv_process_t* handle); 240 241 242 /* 243 * FS 244 */ 245 void uv__fs_init(void); 246 247 248 /* 249 * FS Event 250 */ 251 void uv__process_fs_event_req(uv_loop_t* loop, uv_req_t* req, 252 uv_fs_event_t* handle); 253 void uv__fs_event_close(uv_loop_t* loop, uv_fs_event_t* handle); 254 void uv__fs_event_endgame(uv_loop_t* loop, uv_fs_event_t* handle); 255 256 257 /* 258 * Stat poller. 259 */ 260 void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle); 261 262 263 /* 264 * Utilities. 265 */ 266 void uv__util_init(void); 267 268 uint64_t uv__hrtime(unsigned int scale); 269 __declspec(noreturn) void uv_fatal_error(const int errorno, const char* syscall); 270 int uv__getpwuid_r(uv_passwd_t* pwd); 271 int uv__convert_utf16_to_utf8(const WCHAR* utf16, int utf16len, char** utf8); 272 int uv__convert_utf8_to_utf16(const char* utf8, int utf8len, WCHAR** utf16); 273 274 typedef int (WINAPI *uv__peersockfunc)(SOCKET, struct sockaddr*, int*); 275 276 int uv__getsockpeername(const uv_handle_t* handle, 277 uv__peersockfunc func, 278 struct sockaddr* name, 279 int* namelen, 280 int delayed_error); 281 282 int uv__random_rtlgenrandom(void* buf, size_t buflen); 283 284 285 /* 286 * Process stdio handles. 287 */ 288 int uv__stdio_create(uv_loop_t* loop, 289 const uv_process_options_t* options, 290 BYTE** buffer_ptr); 291 void uv__stdio_destroy(BYTE* buffer); 292 void uv__stdio_noinherit(BYTE* buffer); 293 int uv__stdio_verify(BYTE* buffer, WORD size); 294 WORD uv__stdio_size(BYTE* buffer); 295 HANDLE uv__stdio_handle(BYTE* buffer, int fd); 296 297 298 /* 299 * Winapi and ntapi utility functions 300 */ 301 void uv__winapi_init(void); 302 303 304 /* 305 * Winsock utility functions 306 */ 307 void uv__winsock_init(void); 308 309 int uv__ntstatus_to_winsock_error(NTSTATUS status); 310 311 BOOL uv__get_acceptex_function(SOCKET socket, LPFN_ACCEPTEX* target); 312 BOOL uv__get_connectex_function(SOCKET socket, LPFN_CONNECTEX* target); 313 314 int WSAAPI uv__wsarecv_workaround(SOCKET socket, WSABUF* buffers, 315 DWORD buffer_count, DWORD* bytes, DWORD* flags, WSAOVERLAPPED *overlapped, 316 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine); 317 int WSAAPI uv__wsarecvfrom_workaround(SOCKET socket, WSABUF* buffers, 318 DWORD buffer_count, DWORD* bytes, DWORD* flags, struct sockaddr* addr, 319 int* addr_len, WSAOVERLAPPED *overlapped, 320 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine); 321 322 int WSAAPI uv__msafd_poll(SOCKET socket, AFD_POLL_INFO* info_in, 323 AFD_POLL_INFO* info_out, OVERLAPPED* overlapped); 324 325 /* Whether there are any non-IFS LSPs stacked on TCP */ 326 extern int uv_tcp_non_ifs_lsp_ipv4; 327 extern int uv_tcp_non_ifs_lsp_ipv6; 328 329 /* Ip address used to bind to any port at any interface */ 330 extern struct sockaddr_in uv_addr_ip4_any_; 331 extern struct sockaddr_in6 uv_addr_ip6_any_; 332 333 /* 334 * Wake all loops with fake message 335 */ 336 void uv__wake_all_loops(void); 337 338 /* 339 * Init system wake-up detection 340 */ 341 void uv__init_detect_system_wakeup(void); 342 343 #endif /* UV_WIN_INTERNAL_H_ */ 344