[io-layer] Extract error (#4279)
[mono.git] / mono / metadata / w32socket-win32.c
1 /*
2  * w32socket-win32.c: Windows specific socket code.
3  *
4  * Copyright 2016 Microsoft
5  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
6  */
7
8 #include <config.h>
9 #include <glib.h>
10
11 #include <string.h>
12 #include <stdlib.h>
13 #include <ws2tcpip.h>
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <errno.h>
18
19 #include <sys/types.h>
20
21 #include "w32socket.h"
22 #include "w32socket-internals.h"
23
24 #include "utils/w32api.h"
25
26 #define LOGDEBUG(...)  
27
28 void
29 mono_w32socket_initialize (void)
30 {
31 }
32
33 void
34 mono_w32socket_cleanup (void)
35 {
36 }
37
38 static gboolean set_blocking (SOCKET sock, gboolean block)
39 {
40         u_long non_block = block ? 0 : 1;
41         return ioctlsocket (sock, FIONBIO, &non_block) != SOCKET_ERROR;
42 }
43
44 static DWORD get_socket_timeout (SOCKET sock, int optname)
45 {
46         DWORD timeout = 0;
47         int optlen = sizeof (DWORD);
48         if (getsockopt (sock, SOL_SOCKET, optname, (char *)&timeout, &optlen) == SOCKET_ERROR) {
49                 WSASetLastError (0);
50                 return WSA_INFINITE;
51         }
52         if (timeout == 0)
53                 timeout = WSA_INFINITE; // 0 means infinite
54         return timeout;
55 }
56
57 /*
58 * Performs an alertable wait for the specified event (FD_ACCEPT_BIT,
59 * FD_CONNECT_BIT, FD_READ_BIT, FD_WRITE_BIT) on the specified socket.
60 * Returns TRUE if the event is fired without errors. Calls WSASetLastError()
61 * with WSAEINTR and returns FALSE if the thread is alerted. If the event is
62 * fired but with an error WSASetLastError() is called to set the error and the
63 * function returns FALSE.
64 */
65 static gboolean alertable_socket_wait (SOCKET sock, int event_bit)
66 {
67         static char *EVENT_NAMES[] = { "FD_READ", "FD_WRITE", NULL /*FD_OOB*/, "FD_ACCEPT", "FD_CONNECT", "FD_CLOSE" };
68         gboolean success = FALSE;
69         int error = -1;
70         DWORD timeout = WSA_INFINITE;
71         if (event_bit == FD_READ_BIT || event_bit == FD_WRITE_BIT) {
72                 timeout = get_socket_timeout (sock, event_bit == FD_READ_BIT ? SO_RCVTIMEO : SO_SNDTIMEO);
73         }
74         WSASetLastError (0);
75         WSAEVENT event = WSACreateEvent ();
76         if (event != WSA_INVALID_EVENT) {
77                 if (WSAEventSelect (sock, event, (1 << event_bit) | FD_CLOSE) != SOCKET_ERROR) {
78                         LOGDEBUG (g_message ("%06d - Calling WSAWaitForMultipleEvents () on socket %d", GetCurrentThreadId (), sock));
79                         DWORD ret = WSAWaitForMultipleEvents (1, &event, TRUE, timeout, TRUE);
80                         if (ret == WSA_WAIT_IO_COMPLETION) {
81                                 LOGDEBUG (g_message ("%06d - WSAWaitForMultipleEvents () returned WSA_WAIT_IO_COMPLETION for socket %d", GetCurrentThreadId (), sock));
82                                 error = WSAEINTR;
83                         } else if (ret == WSA_WAIT_TIMEOUT) {
84                                 error = WSAETIMEDOUT;
85                         } else {
86                                 g_assert (ret == WSA_WAIT_EVENT_0);
87                                 WSANETWORKEVENTS ne = { 0 };
88                                 if (WSAEnumNetworkEvents (sock, event, &ne) != SOCKET_ERROR) {
89                                         if (ne.lNetworkEvents & (1 << event_bit) && ne.iErrorCode[event_bit]) {
90                                                 LOGDEBUG (g_message ("%06d - %s error %d on socket %d", GetCurrentThreadId (), EVENT_NAMES[event_bit], ne.iErrorCode[event_bit], sock));
91                                                 error = ne.iErrorCode[event_bit];
92                                         } else if (ne.lNetworkEvents & FD_CLOSE_BIT && ne.iErrorCode[FD_CLOSE_BIT]) {
93                                                 LOGDEBUG (g_message ("%06d - FD_CLOSE error %d on socket %d", GetCurrentThreadId (), ne.iErrorCode[FD_CLOSE_BIT], sock));
94                                                 error = ne.iErrorCode[FD_CLOSE_BIT];
95                                         } else {
96                                                 LOGDEBUG (g_message ("%06d - WSAEnumNetworkEvents () finished successfully on socket %d", GetCurrentThreadId (), sock));
97                                                 success = TRUE;
98                                                 error = 0;
99                                         }
100                                 }
101                         }
102                         WSAEventSelect (sock, NULL, 0);
103                 }
104                 WSACloseEvent (event);
105         }
106         if (error != -1) {
107                 WSASetLastError (error);
108         }
109         return success;
110 }
111
112 #define ALERTABLE_SOCKET_CALL(event_bit, blocking, repeat, ret, op, sock, ...) \
113         LOGDEBUG (g_message ("%06d - Performing %s " #op " () on socket %d", GetCurrentThreadId (), blocking ? "blocking" : "non-blocking", sock)); \
114         if (blocking) { \
115                 if (set_blocking(sock, FALSE)) { \
116                         while (-1 == (int) (ret = op (sock, __VA_ARGS__))) { \
117                                 int _error = WSAGetLastError ();\
118                                 if (_error != WSAEWOULDBLOCK && _error != WSA_IO_PENDING) \
119                                         break; \
120                                 if (!alertable_socket_wait (sock, event_bit) || !repeat) \
121                                         break; \
122                         } \
123                         int _saved_error = WSAGetLastError (); \
124                         set_blocking (sock, TRUE); \
125                         WSASetLastError (_saved_error); \
126                 } \
127         } else { \
128                 ret = op (sock, __VA_ARGS__); \
129         } \
130         int _saved_error = WSAGetLastError (); \
131         LOGDEBUG (g_message ("%06d - Finished %s " #op " () on socket %d (ret = %d, WSAGetLastError() = %d)", GetCurrentThreadId (), \
132                 blocking ? "blocking" : "non-blocking", sock, ret, _saved_error)); \
133         WSASetLastError (_saved_error);
134
135 SOCKET mono_w32socket_accept (SOCKET s, struct sockaddr *addr, socklen_t *addrlen, gboolean blocking)
136 {
137         MonoInternalThread *curthread = mono_thread_internal_current ();
138         SOCKET newsock = INVALID_SOCKET;
139         curthread->interrupt_on_stop = (gpointer)TRUE;
140         ALERTABLE_SOCKET_CALL (FD_ACCEPT_BIT, blocking, TRUE, newsock, accept, s, addr, addrlen);
141         curthread->interrupt_on_stop = (gpointer)FALSE;
142         return newsock;
143 }
144
145 int mono_w32socket_connect (SOCKET s, const struct sockaddr *name, int namelen, gboolean blocking)
146 {
147         int ret = SOCKET_ERROR;
148         ALERTABLE_SOCKET_CALL (FD_CONNECT_BIT, blocking, FALSE, ret, connect, s, name, namelen);
149         ret = WSAGetLastError () != 0 ? SOCKET_ERROR : 0;
150         return ret;
151 }
152
153 int mono_w32socket_recv (SOCKET s, char *buf, int len, int flags, gboolean blocking)
154 {
155         MonoInternalThread *curthread = mono_thread_internal_current ();
156         int ret = SOCKET_ERROR;
157         curthread->interrupt_on_stop = (gpointer)TRUE;
158         ALERTABLE_SOCKET_CALL (FD_READ_BIT, blocking, TRUE, ret, recv, s, buf, len, flags);
159         curthread->interrupt_on_stop = (gpointer)FALSE;
160         return ret;
161 }
162
163 int mono_w32socket_recvfrom (SOCKET s, char *buf, int len, int flags, struct sockaddr *from, socklen_t *fromlen, gboolean blocking)
164 {
165         int ret = SOCKET_ERROR;
166         ALERTABLE_SOCKET_CALL (FD_READ_BIT, blocking, TRUE, ret, recvfrom, s, buf, len, flags, from, fromlen);
167         return ret;
168 }
169
170 int mono_w32socket_recvbuffers (SOCKET s, WSABUF *lpBuffers, guint32 dwBufferCount, guint32 *lpNumberOfBytesRecvd, guint32 *lpFlags, gpointer lpOverlapped, gpointer lpCompletionRoutine, gboolean blocking)
171 {
172         int ret = SOCKET_ERROR;
173         ALERTABLE_SOCKET_CALL (FD_READ_BIT, blocking, TRUE, ret, WSARecv, s, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd, lpFlags, lpOverlapped, lpCompletionRoutine);
174         return ret;
175 }
176
177 int mono_w32socket_send (SOCKET s, char *buf, int len, int flags, gboolean blocking)
178 {
179         int ret = SOCKET_ERROR;
180         ALERTABLE_SOCKET_CALL (FD_WRITE_BIT, blocking, FALSE, ret, send, s, buf, len, flags);
181         return ret;
182 }
183
184 int mono_w32socket_sendto (SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen, gboolean blocking)
185 {
186         int ret = SOCKET_ERROR;
187         ALERTABLE_SOCKET_CALL (FD_WRITE_BIT, blocking, FALSE, ret, sendto, s, buf, len, flags, to, tolen);
188         return ret;
189 }
190
191 int mono_w32socket_sendbuffers (SOCKET s, WSABUF *lpBuffers, guint32 dwBufferCount, guint32 *lpNumberOfBytesRecvd, guint32 lpFlags, gpointer lpOverlapped, gpointer lpCompletionRoutine, gboolean blocking)
192 {
193         int ret = SOCKET_ERROR;
194         ALERTABLE_SOCKET_CALL (FD_WRITE_BIT, blocking, FALSE, ret, WSASend, s, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd, lpFlags, lpOverlapped, lpCompletionRoutine);
195         return ret;
196 }
197
198 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
199 BOOL mono_w32socket_transmit_file (SOCKET hSocket, gpointer hFile, TRANSMIT_FILE_BUFFERS *lpTransmitBuffers, guint32 dwReserved, gboolean blocking)
200 {
201         LOGDEBUG (g_message ("%06d - Performing %s TransmitFile () on socket %d", GetCurrentThreadId (), blocking ? "blocking" : "non-blocking", hSocket));
202
203         int error = 0;
204         if (blocking) {
205                 OVERLAPPED overlapped = { 0 };
206                 overlapped.hEvent = WSACreateEvent ();
207                 if (overlapped.hEvent == WSA_INVALID_EVENT)
208                         return FALSE;
209                 if (!TransmitFile (hSocket, hFile, 0, 0, &overlapped, lpTransmitBuffers, dwReserved)) {
210                         error = WSAGetLastError ();
211                         if (error == WSA_IO_PENDING) {
212                                 error = 0;
213                                 // NOTE: .NET's Socket.SendFile() doesn't honor the Socket's SendTimeout so we shouldn't either
214                                 DWORD ret = WaitForSingleObjectEx (overlapped.hEvent, INFINITE, TRUE);
215                                 if (ret == WAIT_IO_COMPLETION) {
216                                         LOGDEBUG (g_message ("%06d - WaitForSingleObjectEx () returned WSA_WAIT_IO_COMPLETION for socket %d", GetCurrentThreadId (), hSocket));
217                                         error = WSAEINTR;
218                                 } else if (ret == WAIT_TIMEOUT) {
219                                         error = WSAETIMEDOUT;
220                                 } else if (ret != WAIT_OBJECT_0) {
221                                         error = GetLastError ();
222                                 }
223                         }
224                 }
225                 WSACloseEvent (overlapped.hEvent);
226         } else {
227                 if (!TransmitFile (hSocket, hFile, 0, 0, NULL, lpTransmitBuffers, dwReserved)) {
228                         error = WSAGetLastError ();
229                 }
230         }
231
232         LOGDEBUG (g_message ("%06d - Finished %s TransmitFile () on socket %d (ret = %d, WSAGetLastError() = %d)", GetCurrentThreadId (), \
233                 blocking ? "blocking" : "non-blocking", hSocket, error == 0, error));
234         WSASetLastError (error);
235
236         return error == 0;
237 }
238 #endif /* #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
239
240 gint
241 mono_w32socket_disconnect (SOCKET sock, gboolean reuse)
242 {
243         LPFN_DISCONNECTEX disconnect;
244         LPFN_TRANSMITFILE transmit_file;
245         DWORD output_bytes;
246         gint ret;
247
248         /* Use the SIO_GET_EXTENSION_FUNCTION_POINTER to determine
249          * the address of the disconnect method without taking
250          * a hard dependency on a single provider
251          *
252          * For an explanation of why this is done, you can read the
253          * article at http://www.codeproject.com/internet/jbsocketserver3.asp
254          *
255          * I _think_ the extension function pointers need to be looked
256          * up for each socket.
257          *
258          * FIXME: check the best way to store pointers to functions in
259          * managed objects that still works on 64bit platforms. */
260
261         GUID disconnect_guid = WSAID_DISCONNECTEX;
262         ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &disconnect_guid, sizeof (GUID), &disconnect, sizeof (LPFN_DISCONNECTEX), &output_bytes, NULL, NULL);
263         if (ret == 0) {
264                 if (!disconnect (sock, NULL, reuse ? TF_REUSE_SOCKET : 0, 0))
265                         return WSAGetLastError ();
266
267                 return 0;
268         }
269
270         GUID transmit_file_guid = WSAID_TRANSMITFILE;
271         ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &transmit_file_guid, sizeof (GUID), &transmit_file, sizeof (LPFN_TRANSMITFILE), &output_bytes, NULL, NULL);
272         if (ret == 0) {
273                 if (!transmit_file (sock, NULL, 0, 0, NULL, NULL, TF_DISCONNECT | (reuse ? TF_REUSE_SOCKET : 0)))
274                         return WSAGetLastError ();
275
276                 return 0;
277         }
278
279         return ERROR_NOT_SUPPORTED;
280 }
281
282 gint
283 mono_w32socket_set_blocking (SOCKET sock, gboolean blocking)
284 {
285         gulong nonblocking_long = !blocking;
286         return ioctlsocket (sock, FIONBIO, &nonblocking_long);
287 }
288
289 gint
290 mono_w32socket_get_available (SOCKET sock, guint64 *amount)
291 {
292         return ioctlsocket (sock, FIONREAD, (int*) amount);
293 }
294
295 void
296 mono_w32socket_set_last_error (gint32 error)
297 {
298         WSASetLastError (error);
299 }
300
301 gint32
302 mono_w32socket_get_last_error (void)
303 {
304         return WSAGetLastError ();
305 }
306
307 gint32
308 mono_w32socket_convert_error (gint error)
309 {
310         return (error > 0 && error < WSABASEERR) ? error + WSABASEERR : error;
311 }
312
313 gboolean
314 ves_icall_System_Net_Sockets_Socket_SupportPortReuse (MonoProtocolType proto)
315 {
316         return TRUE;
317 }