2 * sockets.c: Socket handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
12 #ifndef DISABLE_SOCKETS
18 #include <sys/types.h>
19 #include <sys/socket.h>
23 #ifdef HAVE_SYS_IOCTL_H
24 # include <sys/ioctl.h>
26 #ifdef HAVE_SYS_FILIO_H
27 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
29 #ifdef HAVE_SYS_SOCKIO_H
30 #include <sys/sockio.h> /* defines SIOCATMARK */
35 #ifndef HAVE_MSG_NOSIGNAL
39 #include <mono/io-layer/wapi.h>
40 #include <mono/io-layer/wapi-private.h>
41 #include <mono/io-layer/socket-private.h>
42 #include <mono/io-layer/handles-private.h>
43 #include <mono/io-layer/socket-wrappers.h>
44 #include <mono/utils/mono-poll.h>
46 #include <netinet/in.h>
47 #include <netinet/tcp.h>
49 #include <arpa/inet.h>
50 #ifdef HAVE_SYS_SENDFILE_H
51 #include <sys/sendfile.h>
55 #define DEBUG(...) g_message(__VA_ARGS__)
60 static guint32 startup_count=0;
61 static guint32 in_cleanup = 0;
63 static void socket_close (gpointer handle, gpointer data);
65 struct _WapiHandleOps _wapi_socket_ops = {
66 socket_close, /* close */
70 NULL, /* special_wait */
74 static mono_once_t socket_ops_once=MONO_ONCE_INIT;
76 static void socket_ops_init (void)
78 /* No capabilities to register */
81 static void socket_close (gpointer handle, gpointer data)
84 struct _WapiHandle_socket *socket_handle = (struct _WapiHandle_socket *)data;
86 DEBUG ("%s: closing socket handle %p", __func__, handle);
88 if (startup_count == 0 && !in_cleanup) {
89 WSASetLastError (WSANOTINITIALISED);
93 /* Shutdown the socket for reading, to interrupt any potential
94 * receives that may be blocking for data. See bug 75705.
96 shutdown (GPOINTER_TO_UINT (handle), SHUT_RD);
99 ret = close (GPOINTER_TO_UINT(handle));
100 } while (ret == -1 && errno == EINTR &&
101 !_wapi_thread_cur_apc_pending ());
105 DEBUG ("%s: close error: %s", __func__, strerror (errno));
106 errnum = errno_to_WSA (errnum, __func__);
108 WSASetLastError (errnum);
112 socket_handle->saved_error = 0;
115 int WSAStartup(guint32 requested, WapiWSAData *data)
121 /* Insist on v2.0+ */
122 if (requested < MAKEWORD(2,0)) {
123 return(WSAVERNOTSUPPORTED);
128 /* I've no idea what is the minor version of the spec I read */
129 data->wHighVersion = MAKEWORD(2,2);
131 data->wVersion = requested < data->wHighVersion? requested:
134 DEBUG ("%s: high version 0x%x", __func__, data->wHighVersion);
136 strncpy (data->szDescription, "WAPI", WSADESCRIPTION_LEN);
137 strncpy (data->szSystemStatus, "groovy", WSASYS_STATUS_LEN);
143 cleanup_close (gpointer handle, gpointer data)
145 _wapi_handle_ops_close (handle, NULL);
151 DEBUG ("%s: cleaning up", __func__);
153 if (--startup_count) {
159 _wapi_handle_foreach (WAPI_HANDLE_SOCKET, cleanup_close, NULL);
164 void WSASetLastError(int error)
166 SetLastError (error);
169 int WSAGetLastError(void)
171 return(GetLastError ());
174 int closesocket(guint32 fd)
176 gpointer handle = GUINT_TO_POINTER (fd);
178 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
179 WSASetLastError (WSAENOTSOCK);
183 _wapi_handle_unref (handle);
187 guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
189 gpointer handle = GUINT_TO_POINTER (fd);
191 struct _WapiHandle_socket *socket_handle;
192 struct _WapiHandle_socket new_socket_handle = {0};
196 if (startup_count == 0) {
197 WSASetLastError (WSANOTINITIALISED);
198 return(INVALID_SOCKET);
201 if (addr != NULL && *addrlen < sizeof(struct sockaddr)) {
202 WSASetLastError (WSAEFAULT);
203 return(INVALID_SOCKET);
206 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
207 WSASetLastError (WSAENOTSOCK);
208 return(INVALID_SOCKET);
211 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
212 (gpointer *)&socket_handle);
214 g_warning ("%s: error looking up socket handle %p",
216 WSASetLastError (WSAENOTSOCK);
217 return(INVALID_SOCKET);
221 new_fd = accept (fd, addr, addrlen);
222 } while (new_fd == -1 && errno == EINTR &&
223 !_wapi_thread_cur_apc_pending());
227 DEBUG ("%s: accept error: %s", __func__, strerror(errno));
229 errnum = errno_to_WSA (errnum, __func__);
230 WSASetLastError (errnum);
232 return(INVALID_SOCKET);
235 if (new_fd >= _wapi_fd_reserve) {
236 DEBUG ("%s: File descriptor is too big", __func__);
238 WSASetLastError (WSASYSCALLFAILURE);
242 return(INVALID_SOCKET);
245 new_socket_handle.domain = socket_handle->domain;
246 new_socket_handle.type = socket_handle->type;
247 new_socket_handle.protocol = socket_handle->protocol;
248 new_socket_handle.still_readable = 1;
250 new_handle = _wapi_handle_new_fd (WAPI_HANDLE_SOCKET, new_fd,
252 if(new_handle == _WAPI_HANDLE_INVALID) {
253 g_warning ("%s: error creating socket handle", __func__);
254 WSASetLastError (ERROR_GEN_FAILURE);
255 return(INVALID_SOCKET);
258 DEBUG ("%s: returning newly accepted socket handle %p with",
259 __func__, new_handle);
264 int _wapi_bind(guint32 fd, struct sockaddr *my_addr, socklen_t addrlen)
266 gpointer handle = GUINT_TO_POINTER (fd);
269 if (startup_count == 0) {
270 WSASetLastError (WSANOTINITIALISED);
271 return(SOCKET_ERROR);
274 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
275 WSASetLastError (WSAENOTSOCK);
276 return(SOCKET_ERROR);
279 ret = bind (fd, my_addr, addrlen);
282 DEBUG ("%s: bind error: %s", __func__, strerror(errno));
283 errnum = errno_to_WSA (errnum, __func__);
284 WSASetLastError (errnum);
286 return(SOCKET_ERROR);
291 int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
294 gpointer handle = GUINT_TO_POINTER (fd);
295 struct _WapiHandle_socket *socket_handle;
299 if (startup_count == 0) {
300 WSASetLastError (WSANOTINITIALISED);
301 return(SOCKET_ERROR);
304 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
305 WSASetLastError (WSAENOTSOCK);
306 return(SOCKET_ERROR);
309 if (connect (fd, serv_addr, addrlen) == -1) {
316 if (errno != EINTR) {
317 DEBUG ("%s: connect error: %s", __func__,
320 errnum = errno_to_WSA (errnum, __func__);
321 if (errnum == WSAEINPROGRESS)
322 errnum = WSAEWOULDBLOCK; /* see bug #73053 */
324 WSASetLastError (errnum);
327 * On solaris x86 getsockopt (SO_ERROR) is not set after
328 * connect () fails so we need to save this error.
330 * But don't do this for EWOULDBLOCK (bug 317315)
332 if (errnum != WSAEWOULDBLOCK) {
333 ok = _wapi_lookup_handle (handle,
335 (gpointer *)&socket_handle);
337 /* ECONNRESET means the socket was closed by another thread */
338 /* Async close on mac raises ECONNABORTED. */
339 if (errnum != WSAECONNRESET && errnum != WSAENETDOWN)
340 g_warning ("%s: error looking up socket handle %p (error %d)", __func__, handle, errnum);
342 socket_handle->saved_error = errnum;
345 return(SOCKET_ERROR);
349 fds.events = POLLOUT;
350 while (mono_poll (&fds, 1, -1) == -1 &&
351 !_wapi_thread_cur_apc_pending ()) {
352 if (errno != EINTR) {
353 errnum = errno_to_WSA (errno, __func__);
355 DEBUG ("%s: connect poll error: %s",
356 __func__, strerror (errno));
358 WSASetLastError (errnum);
359 return(SOCKET_ERROR);
363 len = sizeof(so_error);
364 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &so_error,
366 errnum = errno_to_WSA (errno, __func__);
368 DEBUG ("%s: connect getsockopt error: %s",
369 __func__, strerror (errno));
371 WSASetLastError (errnum);
372 return(SOCKET_ERROR);
376 errnum = errno_to_WSA (so_error, __func__);
378 /* Need to save this socket error */
379 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
380 (gpointer *)&socket_handle);
382 g_warning ("%s: error looking up socket handle %p", __func__, handle);
384 socket_handle->saved_error = errnum;
387 DEBUG ("%s: connect getsockopt returned error: %s",
388 __func__, strerror (so_error));
390 WSASetLastError (errnum);
391 return(SOCKET_ERROR);
398 int _wapi_getpeername(guint32 fd, struct sockaddr *name, socklen_t *namelen)
400 gpointer handle = GUINT_TO_POINTER (fd);
403 if (startup_count == 0) {
404 WSASetLastError (WSANOTINITIALISED);
405 return(SOCKET_ERROR);
408 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
409 WSASetLastError (WSAENOTSOCK);
410 return(SOCKET_ERROR);
413 ret = getpeername (fd, name, namelen);
416 DEBUG ("%s: getpeername error: %s", __func__,
419 errnum = errno_to_WSA (errnum, __func__);
420 WSASetLastError (errnum);
422 return(SOCKET_ERROR);
428 int _wapi_getsockname(guint32 fd, struct sockaddr *name, socklen_t *namelen)
430 gpointer handle = GUINT_TO_POINTER (fd);
433 if (startup_count == 0) {
434 WSASetLastError (WSANOTINITIALISED);
435 return(SOCKET_ERROR);
438 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
439 WSASetLastError (WSAENOTSOCK);
440 return(SOCKET_ERROR);
443 ret = getsockname (fd, name, namelen);
446 DEBUG ("%s: getsockname error: %s", __func__,
449 errnum = errno_to_WSA (errnum, __func__);
450 WSASetLastError (errnum);
452 return(SOCKET_ERROR);
458 int _wapi_getsockopt(guint32 fd, int level, int optname, void *optval,
461 gpointer handle = GUINT_TO_POINTER (fd);
465 struct _WapiHandle_socket *socket_handle;
468 if (startup_count == 0) {
469 WSASetLastError (WSANOTINITIALISED);
470 return(SOCKET_ERROR);
473 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
474 WSASetLastError (WSAENOTSOCK);
475 return(SOCKET_ERROR);
479 if (level == SOL_SOCKET &&
480 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
482 *optlen = sizeof (tv);
485 ret = getsockopt (fd, level, optname, tmp_val, optlen);
488 DEBUG ("%s: getsockopt error: %s", __func__,
491 errnum = errno_to_WSA (errnum, __func__);
492 WSASetLastError (errnum);
494 return(SOCKET_ERROR);
497 if (level == SOL_SOCKET &&
498 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
499 *((int *) optval) = tv.tv_sec * 1000 + (tv.tv_usec / 1000); // milli from micro
500 *optlen = sizeof (int);
503 if (optname == SO_ERROR) {
504 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
505 (gpointer *)&socket_handle);
507 g_warning ("%s: error looking up socket handle %p",
510 /* can't extract the last error */
511 *((int *) optval) = errno_to_WSA (*((int *)optval),
514 if (*((int *)optval) != 0) {
515 *((int *) optval) = errno_to_WSA (*((int *)optval),
517 socket_handle->saved_error = *((int *)optval);
519 *((int *)optval) = socket_handle->saved_error;
527 int _wapi_listen(guint32 fd, int backlog)
529 gpointer handle = GUINT_TO_POINTER (fd);
532 if (startup_count == 0) {
533 WSASetLastError (WSANOTINITIALISED);
534 return(SOCKET_ERROR);
537 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
538 WSASetLastError (WSAENOTSOCK);
539 return(SOCKET_ERROR);
542 ret = listen (fd, backlog);
545 DEBUG ("%s: listen error: %s", __func__, strerror (errno));
547 errnum = errno_to_WSA (errnum, __func__);
548 WSASetLastError (errnum);
550 return(SOCKET_ERROR);
556 int _wapi_recv(guint32 fd, void *buf, size_t len, int recv_flags)
558 return(_wapi_recvfrom (fd, buf, len, recv_flags, NULL, 0));
561 int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
562 struct sockaddr *from, socklen_t *fromlen)
564 gpointer handle = GUINT_TO_POINTER (fd);
565 struct _WapiHandle_socket *socket_handle;
569 if (startup_count == 0) {
570 WSASetLastError (WSANOTINITIALISED);
571 return(SOCKET_ERROR);
574 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
575 WSASetLastError (WSAENOTSOCK);
576 return(SOCKET_ERROR);
580 ret = recvfrom (fd, buf, len, recv_flags, from, fromlen);
581 } while (ret == -1 && errno == EINTR &&
582 !_wapi_thread_cur_apc_pending ());
584 if (ret == 0 && len > 0) {
585 /* According to the Linux man page, recvfrom only
586 * returns 0 when the socket has been shut down
587 * cleanly. Turn this into an EINTR to simulate win32
588 * behaviour of returning EINTR when a socket is
589 * closed while the recvfrom is blocking (we use a
590 * shutdown() in socket_close() to trigger this.) See
593 /* Distinguish between the socket being shut down at
594 * the local or remote ends, and reads that request 0
598 /* If this returns FALSE, it means the socket has been
599 * closed locally. If it returns TRUE, but
600 * still_readable != 1 then shutdown
601 * (SHUT_RD|SHUT_RDWR) has been called locally.
603 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
604 (gpointer *)&socket_handle);
605 if (ok == FALSE || socket_handle->still_readable != 1) {
613 DEBUG ("%s: recv error: %s", __func__, strerror(errno));
615 errnum = errno_to_WSA (errnum, __func__);
616 WSASetLastError (errnum);
618 return(SOCKET_ERROR);
624 _wapi_recvmsg(guint32 fd, struct msghdr *msg, int recv_flags)
626 gpointer handle = GUINT_TO_POINTER (fd);
627 struct _WapiHandle_socket *socket_handle;
631 if (startup_count == 0) {
632 WSASetLastError (WSANOTINITIALISED);
633 return(SOCKET_ERROR);
636 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
637 WSASetLastError (WSAENOTSOCK);
638 return(SOCKET_ERROR);
642 ret = recvmsg (fd, msg, recv_flags);
643 } while (ret == -1 && errno == EINTR &&
644 !_wapi_thread_cur_apc_pending ());
647 /* see _wapi_recvfrom */
648 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
649 (gpointer *)&socket_handle);
650 if (ok == FALSE || socket_handle->still_readable != 1) {
658 DEBUG ("%s: recvmsg error: %s", __func__, strerror(errno));
660 errnum = errno_to_WSA (errnum, __func__);
661 WSASetLastError (errnum);
663 return(SOCKET_ERROR);
668 int _wapi_send(guint32 fd, const void *msg, size_t len, int send_flags)
670 gpointer handle = GUINT_TO_POINTER (fd);
673 if (startup_count == 0) {
674 WSASetLastError (WSANOTINITIALISED);
675 return(SOCKET_ERROR);
678 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
679 WSASetLastError (WSAENOTSOCK);
680 return(SOCKET_ERROR);
684 ret = send (fd, msg, len, send_flags);
685 } while (ret == -1 && errno == EINTR &&
686 !_wapi_thread_cur_apc_pending ());
690 DEBUG ("%s: send error: %s", __func__, strerror (errno));
693 /* At least linux returns EAGAIN/EWOULDBLOCK when the timeout has been set on
694 * a blocking socket. See bug #599488 */
695 if (errnum == EAGAIN) {
696 ret = fcntl (fd, F_GETFL, 0);
697 if (ret != -1 && (ret & O_NONBLOCK) == 0)
700 #endif /* O_NONBLOCK */
701 errnum = errno_to_WSA (errnum, __func__);
702 WSASetLastError (errnum);
704 return(SOCKET_ERROR);
709 int _wapi_sendto(guint32 fd, const void *msg, size_t len, int send_flags,
710 const struct sockaddr *to, socklen_t tolen)
712 gpointer handle = GUINT_TO_POINTER (fd);
715 if (startup_count == 0) {
716 WSASetLastError (WSANOTINITIALISED);
717 return(SOCKET_ERROR);
720 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
721 WSASetLastError (WSAENOTSOCK);
722 return(SOCKET_ERROR);
726 ret = sendto (fd, msg, len, send_flags, to, tolen);
727 } while (ret == -1 && errno == EINTR &&
728 !_wapi_thread_cur_apc_pending ());
732 DEBUG ("%s: send error: %s", __func__, strerror (errno));
734 errnum = errno_to_WSA (errnum, __func__);
735 WSASetLastError (errnum);
737 return(SOCKET_ERROR);
743 _wapi_sendmsg(guint32 fd, const struct msghdr *msg, int send_flags)
745 gpointer handle = GUINT_TO_POINTER (fd);
748 if (startup_count == 0) {
749 WSASetLastError (WSANOTINITIALISED);
750 return(SOCKET_ERROR);
753 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
754 WSASetLastError (WSAENOTSOCK);
755 return(SOCKET_ERROR);
759 ret = sendmsg (fd, msg, send_flags);
760 } while (ret == -1 && errno == EINTR &&
761 !_wapi_thread_cur_apc_pending ());
765 DEBUG ("%s: sendmsg error: %s", __func__, strerror (errno));
767 errnum = errno_to_WSA (errnum, __func__);
768 WSASetLastError (errnum);
770 return(SOCKET_ERROR);
775 int _wapi_setsockopt(guint32 fd, int level, int optname,
776 const void *optval, socklen_t optlen)
778 gpointer handle = GUINT_TO_POINTER (fd);
783 if (startup_count == 0) {
784 WSASetLastError (WSANOTINITIALISED);
785 return(SOCKET_ERROR);
788 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
789 WSASetLastError (WSAENOTSOCK);
790 return(SOCKET_ERROR);
794 if (level == SOL_SOCKET &&
795 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
796 int ms = *((int *) optval);
797 tv.tv_sec = ms / 1000;
798 tv.tv_usec = (ms % 1000) * 1000; // micro from milli
800 optlen = sizeof (tv);
801 #if defined (__linux__)
802 } else if (level == SOL_SOCKET &&
803 (optname == SO_SNDBUF || optname == SO_RCVBUF)) {
804 /* According to socket(7) the Linux kernel doubles the
805 * buffer sizes "to allow space for bookkeeping
808 int bufsize = *((int *) optval);
815 ret = setsockopt (fd, level, optname, tmp_val, optlen);
818 DEBUG ("%s: setsockopt error: %s", __func__,
821 errnum = errno_to_WSA (errnum, __func__);
822 WSASetLastError (errnum);
824 return(SOCKET_ERROR);
827 #if defined (SO_REUSEPORT)
828 /* BSD's and MacOS X multicast sockets also need SO_REUSEPORT when SO_REUSEADDR is requested. */
829 if (level == SOL_SOCKET && optname == SO_REUSEADDR) {
831 socklen_t type_len = sizeof (type);
833 if (!getsockopt (fd, level, SO_TYPE, &type, &type_len)) {
834 if (type == SOCK_DGRAM)
835 setsockopt (fd, level, SO_REUSEPORT, tmp_val, optlen);
843 int _wapi_shutdown(guint32 fd, int how)
845 struct _WapiHandle_socket *socket_handle;
847 gpointer handle = GUINT_TO_POINTER (fd);
850 if (startup_count == 0) {
851 WSASetLastError (WSANOTINITIALISED);
852 return(SOCKET_ERROR);
855 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
856 WSASetLastError (WSAENOTSOCK);
857 return(SOCKET_ERROR);
860 if (how == SHUT_RD ||
862 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
863 (gpointer *)&socket_handle);
865 g_warning ("%s: error looking up socket handle %p",
867 WSASetLastError (WSAENOTSOCK);
868 return(SOCKET_ERROR);
871 socket_handle->still_readable = 0;
874 ret = shutdown (fd, how);
877 DEBUG ("%s: shutdown error: %s", __func__,
880 errnum = errno_to_WSA (errnum, __func__);
881 WSASetLastError (errnum);
883 return(SOCKET_ERROR);
889 guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
890 guint32 unused2, guint32 unused3)
892 struct _WapiHandle_socket socket_handle = {0};
896 socket_handle.domain = domain;
897 socket_handle.type = type;
898 socket_handle.protocol = protocol;
899 socket_handle.still_readable = 1;
901 fd = socket (domain, type, protocol);
902 if (fd == -1 && domain == AF_INET && type == SOCK_RAW &&
904 /* Retry with protocol == 4 (see bug #54565) */
905 // https://bugzilla.novell.com/show_bug.cgi?id=MONO54565
906 socket_handle.protocol = 4;
907 fd = socket (AF_INET, SOCK_RAW, 4);
912 DEBUG ("%s: socket error: %s", __func__, strerror (errno));
913 errnum = errno_to_WSA (errnum, __func__);
914 WSASetLastError (errnum);
916 return(INVALID_SOCKET);
919 if (fd >= _wapi_fd_reserve) {
920 DEBUG ("%s: File descriptor is too big (%d >= %d)",
921 __func__, fd, _wapi_fd_reserve);
923 WSASetLastError (WSASYSCALLFAILURE);
926 return(INVALID_SOCKET);
929 /* .net seems to set this by default for SOCK_STREAM, not for
930 * SOCK_DGRAM (see bug #36322)
931 * https://bugzilla.novell.com/show_bug.cgi?id=MONO36322
933 * It seems winsock has a rather different idea of what
934 * SO_REUSEADDR means. If it's set, then a new socket can be
935 * bound over an existing listening socket. There's a new
936 * windows-specific option called SO_EXCLUSIVEADDRUSE but
937 * using that means the socket MUST be closed properly, or a
938 * denial of service can occur. Luckily for us, winsock
939 * behaves as though any other system would when SO_REUSEADDR
940 * is true, so we don't need to do anything else here. See
942 * https://bugzilla.novell.com/show_bug.cgi?id=MONO53992
947 ret = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &true,
952 DEBUG ("%s: Error setting SO_REUSEADDR", __func__);
954 errnum = errno_to_WSA (errnum, __func__);
955 WSASetLastError (errnum);
959 return(INVALID_SOCKET);
964 mono_once (&socket_ops_once, socket_ops_init);
966 handle = _wapi_handle_new_fd (WAPI_HANDLE_SOCKET, fd, &socket_handle);
967 if (handle == _WAPI_HANDLE_INVALID) {
968 g_warning ("%s: error creating socket handle", __func__);
969 WSASetLastError (WSASYSCALLFAILURE);
971 return(INVALID_SOCKET);
974 DEBUG ("%s: returning socket handle %p", __func__, handle);
979 struct hostent *_wapi_gethostbyname(const char *hostname)
983 if (startup_count == 0) {
984 WSASetLastError (WSANOTINITIALISED);
988 he = gethostbyname (hostname);
990 DEBUG ("%s: gethostbyname error: %s", __func__,
995 WSASetLastError (WSAHOST_NOT_FOUND);
997 #if NO_ADDRESS != NO_DATA
1001 WSASetLastError (WSANO_DATA);
1004 WSASetLastError (WSANO_RECOVERY);
1007 WSASetLastError (WSATRY_AGAIN);
1010 g_warning ("%s: Need to translate %d into winsock error", __func__, h_errno);
1018 static gboolean socket_disconnect (guint32 fd)
1020 struct _WapiHandle_socket *socket_handle;
1022 gpointer handle = GUINT_TO_POINTER (fd);
1025 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
1026 (gpointer *)&socket_handle);
1028 g_warning ("%s: error looking up socket handle %p", __func__,
1030 WSASetLastError (WSAENOTSOCK);
1034 newsock = socket (socket_handle->domain, socket_handle->type,
1035 socket_handle->protocol);
1036 if (newsock == -1) {
1037 gint errnum = errno;
1039 DEBUG ("%s: socket error: %s", __func__, strerror (errno));
1041 errnum = errno_to_WSA (errnum, __func__);
1042 WSASetLastError (errnum);
1047 /* According to Stevens "Advanced Programming in the UNIX
1048 * Environment: UNIX File I/O" dup2() is atomic so there
1049 * should not be a race condition between the old fd being
1050 * closed and the new socket fd being copied over
1053 ret = dup2 (newsock, fd);
1054 } while (ret == -1 && errno == EAGAIN);
1057 gint errnum = errno;
1059 DEBUG ("%s: dup2 error: %s", __func__, strerror (errno));
1061 errnum = errno_to_WSA (errnum, __func__);
1062 WSASetLastError (errnum);
1072 static gboolean wapi_disconnectex (guint32 fd, WapiOverlapped *overlapped,
1073 guint32 flags, guint32 reserved)
1075 DEBUG ("%s: called on socket %d!", __func__, fd);
1077 if (reserved != 0) {
1078 WSASetLastError (WSAEINVAL);
1082 /* We could check the socket type here and fail unless its
1083 * SOCK_STREAM, SOCK_SEQPACKET or SOCK_RDM (according to msdn)
1084 * if we really wanted to
1087 return(socket_disconnect (fd));
1090 #define SF_BUFFER_SIZE 16384
1092 wapi_sendfile (guint32 socket, gpointer fd, guint32 bytes_to_write, guint32 bytes_per_send, guint32 flags)
1094 #if defined(HAVE_SENDFILE) && (defined(__linux__) || defined(DARWIN))
1095 gint file = GPOINTER_TO_INT (fd);
1099 struct stat statbuf;
1101 n = fstat (file, &statbuf);
1104 errnum = errno_to_WSA (errnum, __func__);
1105 WSASetLastError (errnum);
1106 return SOCKET_ERROR;
1110 res = sendfile (socket, file, NULL, statbuf.st_size);
1111 #elif defined(DARWIN)
1112 /* TODO: header/tail could be sent in the 5th argument */
1113 /* TODO: Might not send the entire file for non-blocking sockets */
1114 res = sendfile (file, socket, 0, &statbuf.st_size, NULL, 0);
1116 } while (res != -1 && (errno == EINTR || errno == EAGAIN) && !_wapi_thread_cur_apc_pending ());
1119 errnum = errno_to_WSA (errnum, __func__);
1120 WSASetLastError (errnum);
1121 return SOCKET_ERROR;
1124 /* Default implementation */
1125 gint file = GPOINTER_TO_INT (fd);
1129 buffer = g_malloc (SF_BUFFER_SIZE);
1132 n = read (file, buffer, SF_BUFFER_SIZE);
1133 } while (n == -1 && errno == EINTR && !_wapi_thread_cur_apc_pending ());
1138 return 0; /* We're done reading */
1141 n = send (socket, buffer, n, 0); /* short sends? enclose this in a loop? */
1142 } while (n == -1 && errno == EINTR && !_wapi_thread_cur_apc_pending ());
1146 gint errnum = errno;
1147 errnum = errno_to_WSA (errnum, __func__);
1148 WSASetLastError (errnum);
1150 return SOCKET_ERROR;
1158 TransmitFile (guint32 socket, gpointer file, guint32 bytes_to_write, guint32 bytes_per_send, WapiOverlapped *ol,
1159 WapiTransmitFileBuffers *buffers, guint32 flags)
1161 gpointer sock = GUINT_TO_POINTER (socket);
1164 if (startup_count == 0) {
1165 WSASetLastError (WSANOTINITIALISED);
1169 if (_wapi_handle_type (sock) != WAPI_HANDLE_SOCKET) {
1170 WSASetLastError (WSAENOTSOCK);
1174 /* Write the header */
1175 if (buffers != NULL && buffers->Head != NULL && buffers->HeadLength > 0) {
1176 ret = _wapi_send (socket, buffers->Head, buffers->HeadLength, 0);
1177 if (ret == SOCKET_ERROR)
1181 ret = wapi_sendfile (socket, file, bytes_to_write, bytes_per_send, flags);
1182 if (ret == SOCKET_ERROR)
1185 /* Write the tail */
1186 if (buffers != NULL && buffers->Tail != NULL && buffers->TailLength > 0) {
1187 ret = _wapi_send (socket, buffers->Tail, buffers->TailLength, 0);
1188 if (ret == SOCKET_ERROR)
1192 if ((flags & TF_DISCONNECT) == TF_DISCONNECT)
1193 closesocket (socket);
1202 } extension_functions[] = {
1203 {WSAID_DISCONNECTEX, wapi_disconnectex},
1204 {WSAID_TRANSMITFILE, TransmitFile},
1209 WSAIoctl (guint32 fd, gint32 command,
1210 gchar *input, gint i_len,
1211 gchar *output, gint o_len, glong *written,
1212 void *unused1, void *unused2)
1214 gpointer handle = GUINT_TO_POINTER (fd);
1216 gchar *buffer = NULL;
1218 if (startup_count == 0) {
1219 WSASetLastError (WSANOTINITIALISED);
1220 return(SOCKET_ERROR);
1223 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1224 WSASetLastError (WSAENOTSOCK);
1225 return SOCKET_ERROR;
1228 if (command == SIO_GET_EXTENSION_FUNCTION_POINTER) {
1230 WapiGuid *guid = (WapiGuid *)input;
1232 if (i_len < sizeof(WapiGuid)) {
1233 /* As far as I can tell, windows doesn't
1234 * actually set an error here...
1236 WSASetLastError (WSAEINVAL);
1237 return(SOCKET_ERROR);
1240 if (o_len < sizeof(gpointer)) {
1242 WSASetLastError (WSAEINVAL);
1243 return(SOCKET_ERROR);
1246 if (output == NULL) {
1248 WSASetLastError (WSAEINVAL);
1249 return(SOCKET_ERROR);
1252 while(extension_functions[i].func != NULL) {
1253 if (!memcmp (guid, &extension_functions[i].guid,
1254 sizeof(WapiGuid))) {
1255 memcpy (output, &extension_functions[i].func,
1257 *written = sizeof(gpointer);
1264 WSASetLastError (WSAEINVAL);
1265 return(SOCKET_ERROR);
1268 if (command == SIO_KEEPALIVE_VALS) {
1270 uint32_t keepalivetime;
1271 uint32_t keepaliveinterval;
1273 if (i_len < (3 * sizeof (uint32_t))) {
1274 WSASetLastError (WSAEINVAL);
1275 return SOCKET_ERROR;
1277 memcpy (&onoff, input, sizeof (uint32_t));
1278 memcpy (&keepalivetime, input + sizeof (uint32_t), sizeof (uint32_t));
1279 memcpy (&keepaliveinterval, input + 2 * sizeof (uint32_t), sizeof (uint32_t));
1280 ret = setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE, &onoff, sizeof (uint32_t));
1282 gint errnum = errno;
1283 errnum = errno_to_WSA (errnum, __func__);
1284 WSASetLastError (errnum);
1285 return SOCKET_ERROR;
1288 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
1289 /* Values are in ms, but we need s */
1292 /* keepalivetime and keepaliveinterval are > 0 (checked in managed code) */
1293 rem = keepalivetime % 1000;
1294 keepalivetime /= 1000;
1295 if (keepalivetime == 0 || rem >= 500)
1297 ret = setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &keepalivetime, sizeof (uint32_t));
1299 rem = keepaliveinterval % 1000;
1300 keepaliveinterval /= 1000;
1301 if (keepaliveinterval == 0 || rem >= 500)
1302 keepaliveinterval++;
1303 ret = setsockopt (fd, IPPROTO_TCP, TCP_KEEPINTVL, &keepaliveinterval, sizeof (uint32_t));
1306 gint errnum = errno;
1307 errnum = errno_to_WSA (errnum, __func__);
1308 WSASetLastError (errnum);
1309 return SOCKET_ERROR;
1318 buffer = g_memdup (input, i_len);
1321 ret = ioctl (fd, command, buffer);
1323 gint errnum = errno;
1324 DEBUG("%s: WSAIoctl error: %s", __func__,
1327 errnum = errno_to_WSA (errnum, __func__);
1328 WSASetLastError (errnum);
1331 return(SOCKET_ERROR);
1334 if (buffer == NULL) {
1337 /* We just copy the buffer to the output. Some ioctls
1338 * don't even output any data, but, well...
1340 * NB windows returns WSAEFAULT if o_len is too small
1342 i_len = (i_len > o_len) ? o_len : i_len;
1344 if (i_len > 0 && output != NULL) {
1345 memcpy (output, buffer, i_len);
1355 #ifndef PLATFORM_PORT_PROVIDES_IOCTLSOCKET
1356 int ioctlsocket(guint32 fd, gint32 command, gpointer arg)
1358 gpointer handle = GUINT_TO_POINTER (fd);
1361 if (startup_count == 0) {
1362 WSASetLastError (WSANOTINITIALISED);
1363 return(SOCKET_ERROR);
1366 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1367 WSASetLastError (WSAENOTSOCK);
1368 return(SOCKET_ERROR);
1374 /* This works better than ioctl(...FIONBIO...)
1375 * on Linux (it causes connect to return
1376 * EINPROGRESS, but the ioctl doesn't seem to)
1378 ret = fcntl(fd, F_GETFL, 0);
1380 if (*(gboolean *)arg) {
1385 ret = fcntl(fd, F_SETFL, ret);
1388 #endif /* O_NONBLOCK */
1389 /* Unused in Mono */
1391 ret = ioctl (fd, command, arg);
1396 #if defined (PLATFORM_MACOSX)
1398 // ioctl (fd, FIONREAD, XXX) returns the size of
1399 // the UDP header as well on
1402 // Use getsockopt SO_NREAD instead to get the
1403 // right values for TCP and UDP.
1405 // ai_canonname can be null in some cases on darwin, where the runtime assumes it will
1406 // be the value of the ip buffer.
1408 socklen_t optlen = sizeof (int);
1409 ret = getsockopt (fd, SOL_SOCKET, SO_NREAD, arg, &optlen);
1411 ret = ioctl (fd, command, arg);
1416 WSASetLastError (WSAEINVAL);
1417 return(SOCKET_ERROR);
1421 gint errnum = errno;
1422 DEBUG ("%s: ioctl error: %s", __func__, strerror (errno));
1424 errnum = errno_to_WSA (errnum, __func__);
1425 WSASetLastError (errnum);
1427 return(SOCKET_ERROR);
1433 int _wapi_select(int nfds G_GNUC_UNUSED, fd_set *readfds, fd_set *writefds,
1434 fd_set *exceptfds, struct timeval *timeout)
1438 if (startup_count == 0) {
1439 WSASetLastError (WSANOTINITIALISED);
1440 return(SOCKET_ERROR);
1443 for (maxfd = FD_SETSIZE-1; maxfd >= 0; maxfd--) {
1444 if ((readfds && FD_ISSET (maxfd, readfds)) ||
1445 (writefds && FD_ISSET (maxfd, writefds)) ||
1446 (exceptfds && FD_ISSET (maxfd, exceptfds))) {
1452 WSASetLastError (WSAEINVAL);
1453 return(SOCKET_ERROR);
1457 ret = select(maxfd + 1, readfds, writefds, exceptfds,
1459 } while (ret == -1 && errno == EINTR &&
1460 !_wapi_thread_cur_apc_pending ());
1463 gint errnum = errno;
1464 DEBUG ("%s: select error: %s", __func__, strerror (errno));
1465 errnum = errno_to_WSA (errnum, __func__);
1466 WSASetLastError (errnum);
1468 return(SOCKET_ERROR);
1474 void _wapi_FD_CLR(guint32 fd, fd_set *set)
1476 gpointer handle = GUINT_TO_POINTER (fd);
1478 if (fd >= FD_SETSIZE) {
1479 WSASetLastError (WSAEINVAL);
1483 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1484 WSASetLastError (WSAENOTSOCK);
1491 int _wapi_FD_ISSET(guint32 fd, fd_set *set)
1493 gpointer handle = GUINT_TO_POINTER (fd);
1495 if (fd >= FD_SETSIZE) {
1496 WSASetLastError (WSAEINVAL);
1500 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1501 WSASetLastError (WSAENOTSOCK);
1505 return(FD_ISSET (fd, set));
1508 void _wapi_FD_SET(guint32 fd, fd_set *set)
1510 gpointer handle = GUINT_TO_POINTER (fd);
1512 if (fd >= FD_SETSIZE) {
1513 WSASetLastError (WSAEINVAL);
1517 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1518 WSASetLastError (WSAENOTSOCK);
1527 wsabuf_to_msghdr (WapiWSABuf *buffers, guint32 count, struct msghdr *hdr)
1531 memset (hdr, 0, sizeof (struct msghdr));
1532 hdr->msg_iovlen = count;
1533 hdr->msg_iov = g_new0 (struct iovec, count);
1534 for (i = 0; i < count; i++) {
1535 hdr->msg_iov [i].iov_base = buffers [i].buf;
1536 hdr->msg_iov [i].iov_len = buffers [i].len;
1541 msghdr_iov_free (struct msghdr *hdr)
1543 g_free (hdr->msg_iov);
1546 int WSARecv (guint32 fd, WapiWSABuf *buffers, guint32 count, guint32 *received,
1547 guint32 *flags, WapiOverlapped *overlapped,
1548 WapiOverlappedCB *complete)
1553 g_assert (overlapped == NULL);
1554 g_assert (complete == NULL);
1556 wsabuf_to_msghdr (buffers, count, &hdr);
1557 ret = _wapi_recvmsg (fd, &hdr, *flags);
1558 msghdr_iov_free (&hdr);
1560 if(ret == SOCKET_ERROR) {
1565 *flags = hdr.msg_flags;
1570 int WSASend (guint32 fd, WapiWSABuf *buffers, guint32 count, guint32 *sent,
1571 guint32 flags, WapiOverlapped *overlapped,
1572 WapiOverlappedCB *complete)
1577 g_assert (overlapped == NULL);
1578 g_assert (complete == NULL);
1580 wsabuf_to_msghdr (buffers, count, &hdr);
1581 ret = _wapi_sendmsg (fd, &hdr, flags);
1582 msghdr_iov_free (&hdr);
1584 if(ret == SOCKET_ERROR)
1591 #endif /* ifndef DISABLE_SOCKETS */