2 * w32socket-unix.c: Unix specific socket code.
4 * Copyright 2016 Microsoft
5 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include <sys/socket.h>
15 #ifdef HAVE_SYS_IOCTL_H
16 #include <sys/ioctl.h>
18 #include <netinet/in.h>
19 #include <netinet/tcp.h>
23 #include <arpa/inet.h>
29 #include <sys/types.h>
33 #ifdef HAVE_SYS_IOCTL_H
34 #include <sys/ioctl.h>
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
39 #ifdef HAVE_SYS_SOCKIO_H
40 #include <sys/sockio.h> /* defines SIOCATMARK */
42 #ifndef HAVE_MSG_NOSIGNAL
45 #ifdef HAVE_SYS_SENDFILE_H
46 #include <sys/sendfile.h>
50 #include "w32socket.h"
51 #include "w32socket-internals.h"
53 #include "w32handle.h"
54 #include "utils/mono-logger-internals.h"
55 #include "utils/mono-poll.h"
63 } MonoW32HandleSocket;
65 static guint32 in_cleanup = 0;
68 socket_close (gpointer handle, gpointer data)
71 MonoW32HandleSocket *socket_handle = (MonoW32HandleSocket *)data;
72 MonoThreadInfo *info = mono_thread_info_current ();
74 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing socket handle %p", __func__, handle);
76 /* Shutdown the socket for reading, to interrupt any potential
77 * receives that may be blocking for data. See bug 75705. */
78 shutdown (GPOINTER_TO_UINT (handle), SHUT_RD);
81 ret = close (GPOINTER_TO_UINT(handle));
82 } while (ret == -1 && errno == EINTR &&
83 !mono_thread_info_is_interrupt_state (info));
87 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: close error: %s", __func__, g_strerror (errno));
89 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
93 socket_handle->saved_error = 0;
97 socket_details (gpointer data)
99 /* FIXME: do something */
103 socket_typename (void)
109 socket_typesize (void)
111 return sizeof (MonoW32HandleSocket);
114 static MonoW32HandleOps ops = {
115 socket_close, /* close */
119 NULL, /* special_wait */
121 socket_details, /* details */
122 socket_typename, /* typename */
123 socket_typesize, /* typesize */
127 mono_w32socket_initialize (void)
129 mono_w32handle_register_ops (MONO_W32HANDLE_SOCKET, &ops);
133 cleanup_close (gpointer handle, gpointer data, gpointer user_data)
135 if (mono_w32handle_get_type (handle) == MONO_W32HANDLE_SOCKET)
136 mono_w32handle_force_close (handle, data);
142 mono_w32socket_cleanup (void)
144 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: cleaning up", __func__);
147 mono_w32handle_foreach (cleanup_close, NULL);
152 mono_w32socket_accept (SOCKET sock, struct sockaddr *addr, socklen_t *addrlen, gboolean blocking)
156 MonoW32HandleSocket *socket_handle;
157 MonoW32HandleSocket new_socket_handle;
159 MonoThreadInfo *info;
161 if (addr != NULL && *addrlen < sizeof(struct sockaddr)) {
162 mono_w32socket_set_last_error (WSAEFAULT);
163 return INVALID_SOCKET;
166 handle = GUINT_TO_POINTER (sock);
167 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET, (gpointer *)&socket_handle)) {
168 mono_w32socket_set_last_error (WSAENOTSOCK);
169 return INVALID_SOCKET;
172 info = mono_thread_info_current ();
175 new_fd = accept (sock, addr, addrlen);
176 } while (new_fd == -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
180 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: accept error: %s", __func__, g_strerror(errno));
181 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
182 return INVALID_SOCKET;
185 if (new_fd >= mono_w32handle_fd_reserve) {
186 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big", __func__);
188 mono_w32socket_set_last_error (WSASYSCALLFAILURE);
192 return INVALID_SOCKET;
195 new_socket_handle.domain = socket_handle->domain;
196 new_socket_handle.type = socket_handle->type;
197 new_socket_handle.protocol = socket_handle->protocol;
198 new_socket_handle.still_readable = 1;
200 new_handle = mono_w32handle_new_fd (MONO_W32HANDLE_SOCKET, new_fd,
202 if(new_handle == INVALID_HANDLE_VALUE) {
203 g_warning ("%s: error creating socket handle", __func__);
204 mono_w32socket_set_last_error (ERROR_GEN_FAILURE);
205 return INVALID_SOCKET;
208 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning newly accepted socket handle %p with",
209 __func__, new_handle);
215 mono_w32socket_connect (SOCKET sock, const struct sockaddr *addr, int addrlen, gboolean blocking)
218 MonoW32HandleSocket *socket_handle;
220 handle = GUINT_TO_POINTER (sock);
221 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET, (gpointer *)&socket_handle)) {
222 mono_w32socket_set_last_error (WSAENOTSOCK);
226 if (connect (sock, addr, addrlen) == -1) {
227 MonoThreadInfo *info;
229 gint errnum, so_error;
234 if (errno != EINTR) {
235 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: connect error: %s", __func__,
236 g_strerror (errnum));
238 errnum = mono_w32socket_convert_error (errnum);
239 if (errnum == WSAEINPROGRESS)
240 errnum = WSAEWOULDBLOCK; /* see bug #73053 */
242 mono_w32socket_set_last_error (errnum);
245 * On solaris x86 getsockopt (SO_ERROR) is not set after
246 * connect () fails so we need to save this error.
248 * But don't do this for EWOULDBLOCK (bug 317315)
250 if (errnum != WSAEWOULDBLOCK) {
251 /* ECONNRESET means the socket was closed by another thread */
252 /* Async close on mac raises ECONNABORTED. */
253 socket_handle->saved_error = errnum;
258 info = mono_thread_info_current ();
261 fds.events = MONO_POLLOUT;
262 while (mono_poll (&fds, 1, -1) == -1 && !mono_thread_info_is_interrupt_state (info)) {
263 if (errno != EINTR) {
265 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: connect poll error: %s", __func__, g_strerror (errno));
266 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
271 len = sizeof(so_error);
272 if (getsockopt (sock, SOL_SOCKET, SO_ERROR, &so_error, &len) == -1) {
274 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: connect getsockopt error: %s", __func__, g_strerror (errno));
275 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
280 gint errnum = mono_w32socket_convert_error (so_error);
282 /* Need to save this socket error */
283 socket_handle->saved_error = errnum;
285 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: connect getsockopt returned error: %s",
286 __func__, g_strerror (so_error));
288 mono_w32socket_set_last_error (errnum);
297 mono_w32socket_recv (SOCKET sock, char *buf, int len, int flags, gboolean blocking)
299 return mono_w32socket_recvfrom (sock, buf, len, flags, NULL, 0, blocking);
303 mono_w32socket_recvfrom (SOCKET sock, char *buf, int len, int flags, struct sockaddr *from, socklen_t *fromlen, gboolean blocking)
306 MonoW32HandleSocket *socket_handle;
308 MonoThreadInfo *info;
310 handle = GUINT_TO_POINTER (sock);
311 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET, (gpointer *)&socket_handle)) {
312 mono_w32socket_set_last_error (WSAENOTSOCK);
316 info = mono_thread_info_current ();
319 ret = recvfrom (sock, buf, len, flags, from, fromlen);
320 } while (ret == -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
322 if (ret == 0 && len > 0) {
323 /* According to the Linux man page, recvfrom only
324 * returns 0 when the socket has been shut down
325 * cleanly. Turn this into an EINTR to simulate win32
326 * behaviour of returning EINTR when a socket is
327 * closed while the recvfrom is blocking (we use a
328 * shutdown() in socket_close() to trigger this.) See
331 /* Distinguish between the socket being shut down at
332 * the local or remote ends, and reads that request 0
336 /* If this returns FALSE, it means the socket has been
337 * closed locally. If it returns TRUE, but
338 * still_readable != 1 then shutdown
339 * (SHUT_RD|SHUT_RDWR) has been called locally.
341 if (socket_handle->still_readable != 1) {
349 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: recv error: %s", __func__, g_strerror(errno));
350 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
357 wsabuf_to_msghdr (WSABUF *buffers, guint32 count, struct msghdr *hdr)
361 memset (hdr, 0, sizeof (struct msghdr));
362 hdr->msg_iovlen = count;
363 hdr->msg_iov = g_new0 (struct iovec, count);
364 for (i = 0; i < count; i++) {
365 hdr->msg_iov [i].iov_base = buffers [i].buf;
366 hdr->msg_iov [i].iov_len = buffers [i].len;
371 msghdr_iov_free (struct msghdr *hdr)
373 g_free (hdr->msg_iov);
377 mono_w32socket_recvbuffers (SOCKET sock, WSABUF *buffers, guint32 count, guint32 *received, guint32 *flags, gpointer overlapped, gpointer complete, gboolean blocking)
379 MonoW32HandleSocket *socket_handle;
380 MonoThreadInfo *info;
385 g_assert (overlapped == NULL);
386 g_assert (complete == NULL);
388 handle = GUINT_TO_POINTER (sock);
389 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET, (gpointer *)&socket_handle)) {
390 mono_w32socket_set_last_error (WSAENOTSOCK);
394 info = mono_thread_info_current ();
396 wsabuf_to_msghdr (buffers, count, &hdr);
399 ret = recvmsg (sock, &hdr, *flags);
400 } while (ret == -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
402 msghdr_iov_free (&hdr);
405 /* see mono_w32socket_recvfrom */
406 if (socket_handle->still_readable != 1) {
414 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: recvmsg error: %s", __func__, g_strerror(errno));
415 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
420 *flags = hdr.msg_flags;
426 mono_w32socket_send (SOCKET sock, char *buf, int len, int flags, gboolean blocking)
430 MonoThreadInfo *info;
432 handle = GUINT_TO_POINTER (sock);
433 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
434 mono_w32socket_set_last_error (WSAENOTSOCK);
438 info = mono_thread_info_current ();
441 ret = send (sock, buf, len, flags);
442 } while (ret == -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
446 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: send error: %s", __func__, g_strerror (errno));
449 /* At least linux returns EAGAIN/EWOULDBLOCK when the timeout has been set on
450 * a blocking socket. See bug #599488 */
451 if (errnum == EAGAIN) {
452 ret = fcntl (sock, F_GETFL, 0);
453 if (ret != -1 && (ret & O_NONBLOCK) == 0)
456 #endif /* O_NONBLOCK */
457 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
464 mono_w32socket_sendto (SOCKET sock, const char *buf, int len, int flags, const struct sockaddr *to, int tolen, gboolean blocking)
468 MonoThreadInfo *info;
470 handle = GUINT_TO_POINTER (sock);
471 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
472 mono_w32socket_set_last_error (WSAENOTSOCK);
476 info = mono_thread_info_current ();
479 ret = sendto (sock, buf, len, flags, to, tolen);
480 } while (ret == -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
484 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: send error: %s", __func__, g_strerror (errno));
485 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
492 mono_w32socket_sendbuffers (SOCKET sock, WSABUF *buffers, guint32 count, guint32 *sent, guint32 flags, gpointer overlapped, gpointer complete, gboolean blocking)
495 MonoThreadInfo *info;
499 g_assert (overlapped == NULL);
500 g_assert (complete == NULL);
502 handle = GUINT_TO_POINTER (sock);
503 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
504 mono_w32socket_set_last_error (WSAENOTSOCK);
508 info = mono_thread_info_current ();
510 wsabuf_to_msghdr (buffers, count, &hdr);
513 ret = sendmsg (sock, &hdr, flags);
514 } while (ret == -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
516 msghdr_iov_free (&hdr);
520 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: sendmsg error: %s", __func__, g_strerror (errno));
521 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
529 #define SF_BUFFER_SIZE 16384
532 mono_w32socket_transmit_file (SOCKET sock, gpointer file_handle, TRANSMIT_FILE_BUFFERS *buffers, guint32 flags, gboolean blocking)
534 MonoThreadInfo *info;
538 #if defined(HAVE_SENDFILE) && (defined(__linux__) || defined(DARWIN))
544 handle = GUINT_TO_POINTER (sock);
545 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
546 mono_w32socket_set_last_error (WSAENOTSOCK);
550 /* Write the header */
551 if (buffers != NULL && buffers->Head != NULL && buffers->HeadLength > 0) {
552 ret = mono_w32socket_send (sock, buffers->Head, buffers->HeadLength, 0, FALSE);
553 if (ret == SOCKET_ERROR)
557 info = mono_thread_info_current ();
559 file = GPOINTER_TO_INT (file_handle);
561 #if defined(HAVE_SENDFILE) && (defined(__linux__) || defined(DARWIN))
562 ret = fstat (file, &statbuf);
565 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
571 ret = sendfile (sock, file, NULL, statbuf.st_size);
572 #elif defined(DARWIN)
573 /* TODO: header/tail could be sent in the 5th argument */
574 /* TODO: Might not send the entire file for non-blocking sockets */
575 ret = sendfile (file, sock, 0, &statbuf.st_size, NULL, 0);
577 } while (ret != -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
579 buffer = g_malloc (SF_BUFFER_SIZE);
583 ret = read (file, buffer, SF_BUFFER_SIZE);
584 } while (ret == -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
586 if (ret == -1 || ret == 0)
590 ret = send (sock, buffer, ret, 0); /* short sends? enclose this in a loop? */
591 } while (ret == -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
592 } while (ret != -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
599 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
604 if (buffers != NULL && buffers->Tail != NULL && buffers->TailLength > 0) {
605 ret = mono_w32socket_send (sock, buffers->Tail, buffers->TailLength, 0, FALSE);
606 if (ret == SOCKET_ERROR)
610 if ((flags & TF_DISCONNECT) == TF_DISCONNECT)
611 mono_w32handle_close (handle);
617 mono_w32socket_socket (int domain, int type, int protocol)
619 MonoW32HandleSocket socket_handle = {0};
623 socket_handle.domain = domain;
624 socket_handle.type = type;
625 socket_handle.protocol = protocol;
626 socket_handle.still_readable = 1;
628 sock = socket (domain, type, protocol);
629 if (sock == -1 && domain == AF_INET && type == SOCK_RAW &&
631 /* Retry with protocol == 4 (see bug #54565) */
632 // https://bugzilla.novell.com/show_bug.cgi?id=MONO54565
633 socket_handle.protocol = 4;
634 sock = socket (AF_INET, SOCK_RAW, 4);
639 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: socket error: %s", __func__, g_strerror (errno));
640 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
641 return INVALID_SOCKET;
644 if (sock >= mono_w32handle_fd_reserve) {
645 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big (%d >= %d)",
646 __func__, sock, mono_w32handle_fd_reserve);
648 mono_w32socket_set_last_error (WSASYSCALLFAILURE);
651 return INVALID_SOCKET;
654 /* .net seems to set this by default for SOCK_STREAM, not for
655 * SOCK_DGRAM (see bug #36322)
656 * https://bugzilla.novell.com/show_bug.cgi?id=MONO36322
658 * It seems winsock has a rather different idea of what
659 * SO_REUSEADDR means. If it's set, then a new socket can be
660 * bound over an existing listening socket. There's a new
661 * windows-specific option called SO_EXCLUSIVEADDRUSE but
662 * using that means the socket MUST be closed properly, or a
663 * denial of service can occur. Luckily for us, winsock
664 * behaves as though any other system would when SO_REUSEADDR
665 * is true, so we don't need to do anything else here. See
667 * https://bugzilla.novell.com/show_bug.cgi?id=MONO53992
672 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &true_, sizeof (true_));
676 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error setting SO_REUSEADDR", __func__);
677 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
678 return INVALID_SOCKET;
683 handle = mono_w32handle_new_fd (MONO_W32HANDLE_SOCKET, sock, &socket_handle);
684 if (handle == INVALID_HANDLE_VALUE) {
685 g_warning ("%s: error creating socket handle", __func__);
686 mono_w32socket_set_last_error (WSASYSCALLFAILURE);
688 return INVALID_SOCKET;
691 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning socket handle %p", __func__, handle);
697 mono_w32socket_bind (SOCKET sock, struct sockaddr *addr, socklen_t addrlen)
702 handle = GUINT_TO_POINTER (sock);
703 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
704 mono_w32socket_set_last_error (WSAENOTSOCK);
708 ret = bind (sock, addr, addrlen);
711 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: bind error: %s", __func__, g_strerror(errno));
712 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
720 mono_w32socket_getpeername (SOCKET sock, struct sockaddr *name, socklen_t *namelen)
725 handle = GUINT_TO_POINTER (sock);
726 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
727 mono_w32socket_set_last_error (WSAENOTSOCK);
731 ret = getpeername (sock, name, namelen);
734 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: getpeername error: %s", __func__, g_strerror (errno));
735 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
743 mono_w32socket_getsockname (SOCKET sock, struct sockaddr *name, socklen_t *namelen)
748 handle = GUINT_TO_POINTER (sock);
749 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
750 mono_w32socket_set_last_error (WSAENOTSOCK);
754 ret = getsockname (sock, name, namelen);
757 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: getsockname error: %s", __func__, g_strerror (errno));
758 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
766 mono_w32socket_getsockopt (SOCKET sock, gint level, gint optname, gpointer optval, socklen_t *optlen)
772 MonoW32HandleSocket *socket_handle;
774 handle = GUINT_TO_POINTER (sock);
775 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET, (gpointer *)&socket_handle)) {
776 mono_w32socket_set_last_error (WSAENOTSOCK);
781 if (level == SOL_SOCKET &&
782 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
784 *optlen = sizeof (tv);
787 ret = getsockopt (sock, level, optname, tmp_val, optlen);
790 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: getsockopt error: %s", __func__, g_strerror (errno));
791 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
795 if (level == SOL_SOCKET && (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
796 *((int *) optval) = tv.tv_sec * 1000 + (tv.tv_usec / 1000); // milli from micro
797 *optlen = sizeof (int);
800 if (optname == SO_ERROR) {
801 if (*((int *)optval) != 0) {
802 *((int *) optval) = mono_w32socket_convert_error (*((int *)optval));
803 socket_handle->saved_error = *((int *)optval);
805 *((int *)optval) = socket_handle->saved_error;
813 mono_w32socket_setsockopt (SOCKET sock, gint level, gint optname, const gpointer optval, socklen_t optlen)
818 #if defined (__linux__)
819 /* This has its address taken so it cannot be moved to the if block which uses it */
824 handle = GUINT_TO_POINTER (sock);
825 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
826 mono_w32socket_set_last_error (WSAENOTSOCK);
831 if (level == SOL_SOCKET &&
832 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
833 int ms = *((int *) optval);
834 tv.tv_sec = ms / 1000;
835 tv.tv_usec = (ms % 1000) * 1000; // micro from milli
837 optlen = sizeof (tv);
839 #if defined (__linux__)
840 else if (level == SOL_SOCKET &&
841 (optname == SO_SNDBUF || optname == SO_RCVBUF)) {
842 /* According to socket(7) the Linux kernel doubles the
843 * buffer sizes "to allow space for bookkeeping
846 bufsize = *((int *) optval);
853 ret = setsockopt (sock, level, optname, tmp_val, optlen);
856 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setsockopt error: %s", __func__, g_strerror (errno));
857 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
861 #if defined (SO_REUSEPORT)
862 /* BSD's and MacOS X multicast sockets also need SO_REUSEPORT when SO_REUSEADDR is requested. */
863 if (level == SOL_SOCKET && optname == SO_REUSEADDR) {
865 socklen_t type_len = sizeof (type);
867 if (!getsockopt (sock, level, SO_TYPE, &type, &type_len)) {
868 if (type == SOCK_DGRAM || type == SOCK_STREAM)
869 setsockopt (sock, level, SO_REUSEPORT, tmp_val, optlen);
878 mono_w32socket_listen (SOCKET sock, gint backlog)
883 handle = GUINT_TO_POINTER (sock);
884 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
885 mono_w32socket_set_last_error (WSAENOTSOCK);
889 ret = listen (sock, backlog);
892 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: listen error: %s", __func__, g_strerror (errno));
893 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
901 mono_w32socket_shutdown (SOCKET sock, gint how)
903 MonoW32HandleSocket *socket_handle;
907 handle = GUINT_TO_POINTER (sock);
908 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET, (gpointer *)&socket_handle)) {
909 mono_w32socket_set_last_error (WSAENOTSOCK);
913 if (how == SHUT_RD || how == SHUT_RDWR)
914 socket_handle->still_readable = 0;
916 ret = shutdown (sock, how);
919 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: shutdown error: %s", __func__, g_strerror (errno));
920 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
928 mono_w32socket_disconnect (SOCKET sock, gboolean reuse)
930 MonoW32HandleSocket *socket_handle;
935 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: called on socket %d!", __func__, sock);
937 /* We could check the socket type here and fail unless its
938 * SOCK_STREAM, SOCK_SEQPACKET or SOCK_RDM (according to msdn)
939 * if we really wanted to */
941 handle = GUINT_TO_POINTER (sock);
942 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET, (gpointer *)&socket_handle)) {
943 mono_w32socket_set_last_error (WSAENOTSOCK);
947 newsock = socket (socket_handle->domain, socket_handle->type, socket_handle->protocol);
950 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: socket error: %s", __func__, g_strerror (errnum));
951 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
955 /* According to Stevens "Advanced Programming in the UNIX
956 * Environment: UNIX File I/O" dup2() is atomic so there
957 * should not be a race condition between the old fd being
958 * closed and the new socket fd being copied over */
960 ret = dup2 (newsock, sock);
961 } while (ret == -1 && errno == EAGAIN);
965 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: dup2 error: %s", __func__, g_strerror (errnum));
966 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
976 extension_disconect (SOCKET sock, OVERLAPPED *overlapped, guint32 flags, guint32 reserved)
978 return mono_w32socket_disconnect (sock, flags & TF_REUSE_SOCKET) == 0;
982 extension_transmit_file (SOCKET sock, gpointer file_handle, guint32 bytes_to_write, guint32 bytes_per_send,
983 OVERLAPPED *ol, TRANSMIT_FILE_BUFFERS *buffers, guint32 flags)
985 return mono_w32socket_transmit_file (sock, file_handle, buffers, flags, FALSE);
991 } extension_functions[] = {
992 { {0x7fda2e11,0x8630,0x436f,{0xa0,0x31,0xf5,0x36,0xa6,0xee,0xc1,0x57}} /* WSAID_DISCONNECTEX */, extension_disconect },
993 { {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}} /* WSAID_TRANSMITFILE */, extension_transmit_file },
998 mono_w32socket_ioctl (SOCKET sock, gint32 command, gchar *input, gint inputlen, gchar *output, gint outputlen, glong *written)
1004 handle = GUINT_TO_POINTER (sock);
1005 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
1006 mono_w32socket_set_last_error (WSAENOTSOCK);
1007 return SOCKET_ERROR;
1010 if (command == 0xC8000006 /* SIO_GET_EXTENSION_FUNCTION_POINTER */) {
1014 if (inputlen < sizeof(GUID)) {
1015 /* As far as I can tell, windows doesn't
1016 * actually set an error here...
1018 mono_w32socket_set_last_error (WSAEINVAL);
1019 return SOCKET_ERROR;
1022 if (outputlen < sizeof(gpointer)) {
1024 mono_w32socket_set_last_error (WSAEINVAL);
1025 return SOCKET_ERROR;
1028 if (output == NULL) {
1030 mono_w32socket_set_last_error (WSAEINVAL);
1031 return SOCKET_ERROR;
1034 guid = (GUID*) input;
1035 for (i = 0; extension_functions[i].func; i++) {
1036 if (memcmp (guid, &extension_functions[i].guid, sizeof(GUID)) == 0) {
1037 memcpy (output, &extension_functions[i].func, sizeof(gpointer));
1038 *written = sizeof(gpointer);
1043 mono_w32socket_set_last_error (WSAEINVAL);
1044 return SOCKET_ERROR;
1047 if (command == 0x98000004 /* SIO_KEEPALIVE_VALS */) {
1050 if (inputlen < 3 * sizeof (guint32)) {
1051 mono_w32socket_set_last_error (WSAEINVAL);
1052 return SOCKET_ERROR;
1055 onoff = *((guint32*) input);
1057 ret = setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, &onoff, sizeof (guint32));
1059 mono_w32socket_set_last_error (mono_w32socket_convert_error (errno));
1060 return SOCKET_ERROR;
1063 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
1065 /* Values are in ms, but we need s */
1066 guint32 keepalivetime, keepaliveinterval, rem;
1068 keepalivetime = *(((guint32*) input) + 1);
1069 keepaliveinterval = *(((guint32*) input) + 2);
1071 /* keepalivetime and keepaliveinterval are > 0 (checked in managed code) */
1072 rem = keepalivetime % 1000;
1073 keepalivetime /= 1000;
1074 if (keepalivetime == 0 || rem >= 500)
1076 ret = setsockopt (sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepalivetime, sizeof (guint32));
1078 rem = keepaliveinterval % 1000;
1079 keepaliveinterval /= 1000;
1080 if (keepaliveinterval == 0 || rem >= 500)
1081 keepaliveinterval++;
1082 ret = setsockopt (sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepaliveinterval, sizeof (guint32));
1085 mono_w32socket_set_last_error (mono_w32socket_convert_error (errno));
1086 return SOCKET_ERROR;
1096 buffer = inputlen > 0 ? (gchar*) g_memdup (input, inputlen) : NULL;
1098 ret = ioctl (sock, command, buffer);
1102 gint errnum = errno;
1103 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: WSAIoctl error: %s", __func__, g_strerror (errno));
1104 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
1105 return SOCKET_ERROR;
1113 /* We just copy the buffer to the output. Some ioctls
1114 * don't even output any data, but, well...
1116 * NB windows returns WSAEFAULT if outputlen is too small */
1117 inputlen = (inputlen > outputlen) ? outputlen : inputlen;
1119 if (inputlen > 0 && output != NULL)
1120 memcpy (output, buffer, inputlen);
1123 *written = inputlen;
1129 mono_w32socket_close (SOCKET sock)
1131 return mono_w32handle_close (GINT_TO_POINTER (sock));
1135 mono_w32socket_set_blocking (SOCKET socket, gboolean blocking)
1140 handle = GINT_TO_POINTER (socket);
1141 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
1142 mono_w32socket_set_last_error (WSAENOTSOCK);
1143 return SOCKET_ERROR;
1147 /* This works better than ioctl(...FIONBIO...)
1148 * on Linux (it causes connect to return
1149 * EINPROGRESS, but the ioctl doesn't seem to) */
1150 ret = fcntl (socket, F_GETFL, 0);
1152 ret = fcntl (socket, F_SETFL, blocking ? (ret & (~O_NONBLOCK)) : (ret | (O_NONBLOCK)));
1153 #endif /* O_NONBLOCK */
1156 gint errnum = errno;
1157 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: ioctl error: %s", __func__, g_strerror (errno));
1158 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
1159 return SOCKET_ERROR;
1166 mono_w32socket_get_available (SOCKET socket, guint64 *amount)
1171 handle = GINT_TO_POINTER (socket);
1172 if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
1173 mono_w32socket_set_last_error (WSAENOTSOCK);
1174 return SOCKET_ERROR;
1177 #if defined (PLATFORM_MACOSX)
1178 // ioctl (socket, FIONREAD, XXX) returns the size of
1179 // the UDP header as well on Darwin.
1181 // Use getsockopt SO_NREAD instead to get the
1182 // right values for TCP and UDP.
1184 // ai_canonname can be null in some cases on darwin,
1185 // where the runtime assumes it will be the value of
1188 socklen_t optlen = sizeof (int);
1189 ret = getsockopt (socket, SOL_SOCKET, SO_NREAD, (gulong*) amount, &optlen);
1191 ret = ioctl (socket, FIONREAD, (gulong*) amount);
1195 gint errnum = errno;
1196 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: ioctl error: %s", __func__, g_strerror (errno));
1197 mono_w32socket_set_last_error (mono_w32socket_convert_error (errnum));
1198 return SOCKET_ERROR;
1205 mono_w32socket_set_last_error (gint32 error)
1207 mono_w32error_set_last (error);
1211 mono_w32socket_get_last_error (void)
1213 return mono_w32error_get_last ();
1217 mono_w32socket_convert_error (gint error)
1220 case 0: return ERROR_SUCCESS;
1221 case EACCES: return WSAEACCES;
1223 case EADDRINUSE: return WSAEADDRINUSE;
1226 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
1228 #if EAGAIN != EWOULDBLOCK
1229 case EAGAIN: return WSAEWOULDBLOCK;
1232 case EALREADY: return WSAEALREADY;
1234 case EBADF: return WSAENOTSOCK;
1236 case ECONNABORTED: return WSAENETDOWN;
1239 case ECONNREFUSED: return WSAECONNREFUSED;
1242 case ECONNRESET: return WSAECONNRESET;
1244 case EFAULT: return WSAEFAULT;
1246 case EHOSTUNREACH: return WSAEHOSTUNREACH;
1249 case EINPROGRESS: return WSAEINPROGRESS;
1251 case EINTR: return WSAEINTR;
1252 case EINVAL: return WSAEINVAL;
1253 /*FIXME: case EIO: return WSAE????; */
1255 case EISCONN: return WSAEISCONN;
1257 /* FIXME: case ELOOP: return WSA????; */
1258 case EMFILE: return WSAEMFILE;
1260 case EMSGSIZE: return WSAEMSGSIZE;
1262 /* FIXME: case ENAMETOOLONG: return WSAEACCES; */
1264 case ENETUNREACH: return WSAENETUNREACH;
1267 case ENOBUFS: return WSAENOBUFS; /* not documented */
1269 /* case ENOENT: return WSAE????; */
1270 case ENOMEM: return WSAENOBUFS;
1272 case ENOPROTOOPT: return WSAENOPROTOOPT;
1275 case ENOSR: return WSAENETDOWN;
1278 case ENOTCONN: return WSAENOTCONN;
1280 /*FIXME: case ENOTDIR: return WSAE????; */
1282 case ENOTSOCK: return WSAENOTSOCK;
1284 case ENOTTY: return WSAENOTSOCK;
1286 case EOPNOTSUPP: return WSAEOPNOTSUPP;
1288 case EPERM: return WSAEACCES;
1289 case EPIPE: return WSAESHUTDOWN;
1290 #ifdef EPROTONOSUPPORT
1291 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
1294 case ERESTARTSYS: return WSAENETDOWN;
1296 /*FIXME: case EROFS: return WSAE????; */
1297 #ifdef ESOCKTNOSUPPORT
1298 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
1301 case ETIMEDOUT: return WSAETIMEDOUT;
1304 case EWOULDBLOCK: return WSAEWOULDBLOCK;
1306 #ifdef EADDRNOTAVAIL
1307 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
1309 /* This might happen with unix sockets */
1310 case ENOENT: return WSAECONNREFUSED;
1312 case EDESTADDRREQ: return WSAEDESTADDRREQ;
1315 case EHOSTDOWN: return WSAEHOSTDOWN;
1318 case ENETDOWN: return WSAENETDOWN;
1320 case ENODEV: return WSAENETDOWN;
1322 case EPROTOTYPE: return WSAEPROTOTYPE;
1325 g_error ("%s: no translation into winsock error for (%d) \"%s\"", __func__, error, g_strerror (error));
1330 ves_icall_System_Net_Sockets_Socket_SupportPortReuse (MonoProtocolType proto)
1332 #if defined (SO_REUSEPORT)
1336 /* Linux always supports double binding for UDP, even on older kernels. */
1337 if (proto == ProtocolType_Udp)