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 #if defined(_MSC_VER)
26 # pragma warning(push,1)
27 #endif
28 
29 #include "uv.h"
30 #include "../uv-common.h"
31 
32 #include "uv/tree.h"
33 #include "winapi.h"
34 #include "winsock.h"
35 
36 #ifdef _MSC_VER
37 # define INLINE __inline
38 # define UV_THREAD_LOCAL __declspec( thread )
39 #else
40 # define INLINE inline
41 # define UV_THREAD_LOCAL __thread
42 #endif
43 
44 
45 #ifdef _DEBUG
46 
47 extern UV_THREAD_LOCAL int uv__crt_assert_enabled;
48 
49 #define UV_BEGIN_DISABLE_CRT_ASSERT()                           \
50   {                                                             \
51     int uv__saved_crt_assert_enabled = uv__crt_assert_enabled;  \
52     uv__crt_assert_enabled = FALSE;
53 
54 
55 #define UV_END_DISABLE_CRT_ASSERT()                             \
56     uv__crt_assert_enabled = uv__saved_crt_assert_enabled;      \
57   }
58 
59 #else
60 #define UV_BEGIN_DISABLE_CRT_ASSERT()
61 #define UV_END_DISABLE_CRT_ASSERT()
62 #endif
63 
64 /*
65  * TCP
66  */
67 
68 typedef enum {
69   UV__IPC_SOCKET_XFER_NONE = 0,
70   UV__IPC_SOCKET_XFER_TCP_CONNECTION,
71   UV__IPC_SOCKET_XFER_TCP_SERVER
72 } uv__ipc_socket_xfer_type_t;
73 
74 typedef struct {
75   WSAPROTOCOL_INFOW socket_info;
76   uint32_t delayed_error;
77 } uv__ipc_socket_xfer_info_t;
78 
79 int uv_tcp_listen(uv_tcp_t* handle, int backlog, uv_connection_cb cb);
80 int uv_tcp_accept(uv_tcp_t* server, uv_tcp_t* client);
81 int uv_tcp_read_start(uv_tcp_t* handle, uv_alloc_cb alloc_cb,
82     uv_read_cb read_cb);
83 int uv_tcp_write(uv_loop_t* loop, uv_write_t* req, uv_tcp_t* handle,
84     const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb);
85 int uv__tcp_try_write(uv_tcp_t* handle, const uv_buf_t bufs[],
86     unsigned int nbufs);
87 
88 void uv_process_tcp_read_req(uv_loop_t* loop, uv_tcp_t* handle, uv_req_t* req);
89 void uv_process_tcp_write_req(uv_loop_t* loop, uv_tcp_t* handle,
90     uv_write_t* req);
91 void uv_process_tcp_accept_req(uv_loop_t* loop, uv_tcp_t* handle,
92     uv_req_t* req);
93 void uv_process_tcp_connect_req(uv_loop_t* loop, uv_tcp_t* handle,
94     uv_connect_t* req);
95 
96 void uv_tcp_close(uv_loop_t* loop, uv_tcp_t* tcp);
97 void uv_tcp_endgame(uv_loop_t* loop, uv_tcp_t* handle);
98 
99 int uv__tcp_xfer_export(uv_tcp_t* handle,
100                         int pid,
101                         uv__ipc_socket_xfer_type_t* xfer_type,
102                         uv__ipc_socket_xfer_info_t* xfer_info);
103 int uv__tcp_xfer_import(uv_tcp_t* tcp,
104                         uv__ipc_socket_xfer_type_t xfer_type,
105                         uv__ipc_socket_xfer_info_t* xfer_info);
106 
107 
108 /*
109  * UDP
110  */
111 void uv_process_udp_recv_req(uv_loop_t* loop, uv_udp_t* handle, uv_req_t* req);
112 void uv_process_udp_send_req(uv_loop_t* loop, uv_udp_t* handle,
113     uv_udp_send_t* req);
114 
115 void uv_udp_close(uv_loop_t* loop, uv_udp_t* handle);
116 void uv_udp_endgame(uv_loop_t* loop, uv_udp_t* handle);
117 
118 
119 /*
120  * Pipes
121  */
122 int uv_stdio_pipe_server(uv_loop_t* loop, uv_pipe_t* handle, DWORD access,
123     char* name, size_t nameSize);
124 
125 int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
126 int uv_pipe_accept(uv_pipe_t* server, uv_stream_t* client);
127 int uv_pipe_read_start(uv_pipe_t* handle, uv_alloc_cb alloc_cb,
128     uv_read_cb read_cb);
129 void uv__pipe_read_stop(uv_pipe_t* handle);
130 int uv__pipe_write(uv_loop_t* loop,
131                    uv_write_t* req,
132                    uv_pipe_t* handle,
133                    const uv_buf_t bufs[],
134                    size_t nbufs,
135                    uv_stream_t* send_handle,
136                    uv_write_cb cb);
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_cleanup(uv_loop_t* loop, uv_pipe_t* handle);
151 void uv_pipe_endgame(uv_loop_t* loop, uv_pipe_t* handle);
152 
153 
154 /*
155  * TTY
156  */
157 void uv_console_init(void);
158 
159 int uv_tty_read_start(uv_tty_t* handle, uv_alloc_cb alloc_cb,
160     uv_read_cb read_cb);
161 int uv_tty_read_stop(uv_tty_t* handle);
162 int uv_tty_write(uv_loop_t* loop, uv_write_t* req, uv_tty_t* handle,
163     const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb);
164 int uv__tty_try_write(uv_tty_t* handle, const uv_buf_t bufs[],
165     unsigned int nbufs);
166 void uv_tty_close(uv_tty_t* handle);
167 
168 void uv_process_tty_read_req(uv_loop_t* loop, uv_tty_t* handle,
169     uv_req_t* req);
170 void uv_process_tty_write_req(uv_loop_t* loop, uv_tty_t* handle,
171     uv_write_t* req);
172 /*
173  * uv_process_tty_accept_req() is a stub to keep DELEGATE_STREAM_REQ working
174  * TODO: find a way to remove it
175  */
176 void uv_process_tty_accept_req(uv_loop_t* loop, uv_tty_t* handle,
177     uv_req_t* raw_req);
178 /*
179  * uv_process_tty_connect_req() is a stub to keep DELEGATE_STREAM_REQ working
180  * TODO: find a way to remove it
181  */
182 void uv_process_tty_connect_req(uv_loop_t* loop, uv_tty_t* handle,
183     uv_connect_t* req);
184 
185 void uv_tty_endgame(uv_loop_t* loop, uv_tty_t* handle);
186 
187 
188 /*
189  * Poll watchers
190  */
191 void uv_process_poll_req(uv_loop_t* loop, uv_poll_t* handle,
192     uv_req_t* req);
193 
194 int uv_poll_close(uv_loop_t* loop, uv_poll_t* handle);
195 void uv_poll_endgame(uv_loop_t* loop, uv_poll_t* handle);
196 
197 
198 /*
199  * Loop watchers
200  */
201 void uv_loop_watcher_endgame(uv_loop_t* loop, uv_handle_t* handle);
202 
203 void uv_prepare_invoke(uv_loop_t* loop);
204 void uv_check_invoke(uv_loop_t* loop);
205 void uv_idle_invoke(uv_loop_t* loop);
206 
207 void uv__once_init(void);
208 
209 
210 /*
211  * Async watcher
212  */
213 void uv_async_close(uv_loop_t* loop, uv_async_t* handle);
214 void uv_async_endgame(uv_loop_t* loop, uv_async_t* handle);
215 
216 void uv_process_async_wakeup_req(uv_loop_t* loop, uv_async_t* handle,
217     uv_req_t* req);
218 
219 
220 /*
221  * Signal watcher
222  */
223 void uv_signals_init(void);
224 int uv__signal_dispatch(int signum);
225 
226 void uv_signal_close(uv_loop_t* loop, uv_signal_t* handle);
227 void uv_signal_endgame(uv_loop_t* loop, uv_signal_t* handle);
228 
229 void uv_process_signal_req(uv_loop_t* loop, uv_signal_t* handle,
230     uv_req_t* req);
231 
232 
233 /*
234  * Spawn
235  */
236 void uv_process_proc_exit(uv_loop_t* loop, uv_process_t* handle);
237 void uv_process_close(uv_loop_t* loop, uv_process_t* handle);
238 void uv_process_endgame(uv_loop_t* loop, uv_process_t* handle);
239 
240 
241 /*
242  * Error
243  */
244 int uv_translate_sys_error(int sys_errno);
245 
246 
247 /*
248  * FS
249  */
250 void uv_fs_init(void);
251 
252 
253 /*
254  * FS Event
255  */
256 void uv_process_fs_event_req(uv_loop_t* loop, uv_req_t* req,
257     uv_fs_event_t* handle);
258 void uv_fs_event_close(uv_loop_t* loop, uv_fs_event_t* handle);
259 void uv_fs_event_endgame(uv_loop_t* loop, uv_fs_event_t* handle);
260 
261 
262 /*
263  * Stat poller.
264  */
265 void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle);
266 
267 
268 /*
269  * Utilities.
270  */
271 void uv__util_init(void);
272 
273 uint64_t uv__hrtime(unsigned int scale);
274 __declspec(noreturn) void uv_fatal_error(const int errorno, const char* syscall);
275 int uv__getpwuid_r(uv_passwd_t* pwd);
276 int uv__convert_utf16_to_utf8(const WCHAR* utf16, int utf16len, char** utf8);
277 int uv__convert_utf8_to_utf16(const char* utf8, int utf8len, WCHAR** utf16);
278 
279 typedef int (WINAPI *uv__peersockfunc)(SOCKET, struct sockaddr*, int*);
280 
281 int uv__getsockpeername(const uv_handle_t* handle,
282                         uv__peersockfunc func,
283                         struct sockaddr* name,
284                         int* namelen,
285                         int delayed_error);
286 
287 int uv__random_rtlgenrandom(void* buf, size_t buflen);
288 
289 
290 /*
291  * Process stdio handles.
292  */
293 int uv__stdio_create(uv_loop_t* loop,
294                      const uv_process_options_t* options,
295                      BYTE** buffer_ptr);
296 void uv__stdio_destroy(BYTE* buffer);
297 void uv__stdio_noinherit(BYTE* buffer);
298 int uv__stdio_verify(BYTE* buffer, WORD size);
299 WORD uv__stdio_size(BYTE* buffer);
300 HANDLE uv__stdio_handle(BYTE* buffer, int fd);
301 
302 
303 /*
304  * Winapi and ntapi utility functions
305  */
306 void uv_winapi_init(void);
307 
308 
309 /*
310  * Winsock utility functions
311  */
312 void uv_winsock_init(void);
313 
314 int uv_ntstatus_to_winsock_error(NTSTATUS status);
315 
316 BOOL uv_get_acceptex_function(SOCKET socket, LPFN_ACCEPTEX* target);
317 BOOL uv_get_connectex_function(SOCKET socket, LPFN_CONNECTEX* target);
318 
319 int WSAAPI uv_wsarecv_workaround(SOCKET socket, WSABUF* buffers,
320     DWORD buffer_count, DWORD* bytes, DWORD* flags, WSAOVERLAPPED *overlapped,
321     LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine);
322 int WSAAPI uv_wsarecvfrom_workaround(SOCKET socket, WSABUF* buffers,
323     DWORD buffer_count, DWORD* bytes, DWORD* flags, struct sockaddr* addr,
324     int* addr_len, WSAOVERLAPPED *overlapped,
325     LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine);
326 
327 int WSAAPI uv_msafd_poll(SOCKET socket, AFD_POLL_INFO* info_in,
328     AFD_POLL_INFO* info_out, OVERLAPPED* overlapped);
329 
330 /* Whether there are any non-IFS LSPs stacked on TCP */
331 extern int uv_tcp_non_ifs_lsp_ipv4;
332 extern int uv_tcp_non_ifs_lsp_ipv6;
333 
334 /* Ip address used to bind to any port at any interface */
335 extern struct sockaddr_in uv_addr_ip4_any_;
336 extern struct sockaddr_in6 uv_addr_ip6_any_;
337 
338 /*
339  * Wake all loops with fake message
340  */
341 void uv__wake_all_loops(void);
342 
343 /*
344  * Init system wake-up detection
345  */
346 void uv__init_detect_system_wakeup(void);
347 
348 #endif /* UV_WIN_INTERNAL_H_ */
349