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 if (errnum != WSAECONNRESET)
339 g_warning ("%s: error looking up socket handle %p (error %d)", __func__, handle, errnum);
341 socket_handle->saved_error = errnum;
344 return(SOCKET_ERROR);
348 fds.events = POLLOUT;
349 while (mono_poll (&fds, 1, -1) == -1 &&
350 !_wapi_thread_cur_apc_pending ()) {
351 if (errno != EINTR) {
352 errnum = errno_to_WSA (errno, __func__);
354 DEBUG ("%s: connect poll error: %s",
355 __func__, strerror (errno));
357 WSASetLastError (errnum);
358 return(SOCKET_ERROR);
362 len = sizeof(so_error);
363 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &so_error,
365 errnum = errno_to_WSA (errno, __func__);
367 DEBUG ("%s: connect getsockopt error: %s",
368 __func__, strerror (errno));
370 WSASetLastError (errnum);
371 return(SOCKET_ERROR);
375 errnum = errno_to_WSA (so_error, __func__);
377 /* Need to save this socket error */
378 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
379 (gpointer *)&socket_handle);
381 g_warning ("%s: error looking up socket handle %p", __func__, handle);
383 socket_handle->saved_error = errnum;
386 DEBUG ("%s: connect getsockopt returned error: %s",
387 __func__, strerror (so_error));
389 WSASetLastError (errnum);
390 return(SOCKET_ERROR);
397 int _wapi_getpeername(guint32 fd, struct sockaddr *name, socklen_t *namelen)
399 gpointer handle = GUINT_TO_POINTER (fd);
402 if (startup_count == 0) {
403 WSASetLastError (WSANOTINITIALISED);
404 return(SOCKET_ERROR);
407 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
408 WSASetLastError (WSAENOTSOCK);
409 return(SOCKET_ERROR);
412 ret = getpeername (fd, name, namelen);
415 DEBUG ("%s: getpeername error: %s", __func__,
418 errnum = errno_to_WSA (errnum, __func__);
419 WSASetLastError (errnum);
421 return(SOCKET_ERROR);
427 int _wapi_getsockname(guint32 fd, struct sockaddr *name, socklen_t *namelen)
429 gpointer handle = GUINT_TO_POINTER (fd);
432 if (startup_count == 0) {
433 WSASetLastError (WSANOTINITIALISED);
434 return(SOCKET_ERROR);
437 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
438 WSASetLastError (WSAENOTSOCK);
439 return(SOCKET_ERROR);
442 ret = getsockname (fd, name, namelen);
445 DEBUG ("%s: getsockname error: %s", __func__,
448 errnum = errno_to_WSA (errnum, __func__);
449 WSASetLastError (errnum);
451 return(SOCKET_ERROR);
457 int _wapi_getsockopt(guint32 fd, int level, int optname, void *optval,
460 gpointer handle = GUINT_TO_POINTER (fd);
464 struct _WapiHandle_socket *socket_handle;
467 if (startup_count == 0) {
468 WSASetLastError (WSANOTINITIALISED);
469 return(SOCKET_ERROR);
472 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
473 WSASetLastError (WSAENOTSOCK);
474 return(SOCKET_ERROR);
478 if (level == SOL_SOCKET &&
479 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
481 *optlen = sizeof (tv);
484 ret = getsockopt (fd, level, optname, tmp_val, optlen);
487 DEBUG ("%s: getsockopt error: %s", __func__,
490 errnum = errno_to_WSA (errnum, __func__);
491 WSASetLastError (errnum);
493 return(SOCKET_ERROR);
496 if (level == SOL_SOCKET &&
497 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
498 *((int *) optval) = tv.tv_sec * 1000 + (tv.tv_usec / 1000); // milli from micro
499 *optlen = sizeof (int);
502 if (optname == SO_ERROR) {
503 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
504 (gpointer *)&socket_handle);
506 g_warning ("%s: error looking up socket handle %p",
509 /* can't extract the last error */
510 *((int *) optval) = errno_to_WSA (*((int *)optval),
513 if (*((int *)optval) != 0) {
514 *((int *) optval) = errno_to_WSA (*((int *)optval),
516 socket_handle->saved_error = *((int *)optval);
518 *((int *)optval) = socket_handle->saved_error;
526 int _wapi_listen(guint32 fd, int backlog)
528 gpointer handle = GUINT_TO_POINTER (fd);
531 if (startup_count == 0) {
532 WSASetLastError (WSANOTINITIALISED);
533 return(SOCKET_ERROR);
536 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
537 WSASetLastError (WSAENOTSOCK);
538 return(SOCKET_ERROR);
541 ret = listen (fd, backlog);
544 DEBUG ("%s: listen error: %s", __func__, strerror (errno));
546 errnum = errno_to_WSA (errnum, __func__);
547 WSASetLastError (errnum);
549 return(SOCKET_ERROR);
555 int _wapi_recv(guint32 fd, void *buf, size_t len, int recv_flags)
557 return(_wapi_recvfrom (fd, buf, len, recv_flags, NULL, 0));
560 int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
561 struct sockaddr *from, socklen_t *fromlen)
563 gpointer handle = GUINT_TO_POINTER (fd);
564 struct _WapiHandle_socket *socket_handle;
568 if (startup_count == 0) {
569 WSASetLastError (WSANOTINITIALISED);
570 return(SOCKET_ERROR);
573 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
574 WSASetLastError (WSAENOTSOCK);
575 return(SOCKET_ERROR);
579 ret = recvfrom (fd, buf, len, recv_flags, from, fromlen);
580 } while (ret == -1 && errno == EINTR &&
581 !_wapi_thread_cur_apc_pending ());
583 if (ret == 0 && len > 0) {
584 /* According to the Linux man page, recvfrom only
585 * returns 0 when the socket has been shut down
586 * cleanly. Turn this into an EINTR to simulate win32
587 * behaviour of returning EINTR when a socket is
588 * closed while the recvfrom is blocking (we use a
589 * shutdown() in socket_close() to trigger this.) See
592 /* Distinguish between the socket being shut down at
593 * the local or remote ends, and reads that request 0
597 /* If this returns FALSE, it means the socket has been
598 * closed locally. If it returns TRUE, but
599 * still_readable != 1 then shutdown
600 * (SHUT_RD|SHUT_RDWR) has been called locally.
602 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
603 (gpointer *)&socket_handle);
604 if (ok == FALSE || socket_handle->still_readable != 1) {
612 DEBUG ("%s: recv error: %s", __func__, strerror(errno));
614 errnum = errno_to_WSA (errnum, __func__);
615 WSASetLastError (errnum);
617 return(SOCKET_ERROR);
623 _wapi_recvmsg(guint32 fd, struct msghdr *msg, int recv_flags)
625 gpointer handle = GUINT_TO_POINTER (fd);
626 struct _WapiHandle_socket *socket_handle;
630 if (startup_count == 0) {
631 WSASetLastError (WSANOTINITIALISED);
632 return(SOCKET_ERROR);
635 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
636 WSASetLastError (WSAENOTSOCK);
637 return(SOCKET_ERROR);
641 ret = recvmsg (fd, msg, recv_flags);
642 } while (ret == -1 && errno == EINTR &&
643 !_wapi_thread_cur_apc_pending ());
646 /* see _wapi_recvfrom */
647 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
648 (gpointer *)&socket_handle);
649 if (ok == FALSE || socket_handle->still_readable != 1) {
657 DEBUG ("%s: recvmsg error: %s", __func__, strerror(errno));
659 errnum = errno_to_WSA (errnum, __func__);
660 WSASetLastError (errnum);
662 return(SOCKET_ERROR);
667 int _wapi_send(guint32 fd, const void *msg, size_t len, int send_flags)
669 gpointer handle = GUINT_TO_POINTER (fd);
672 if (startup_count == 0) {
673 WSASetLastError (WSANOTINITIALISED);
674 return(SOCKET_ERROR);
677 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
678 WSASetLastError (WSAENOTSOCK);
679 return(SOCKET_ERROR);
683 ret = send (fd, msg, len, send_flags);
684 } while (ret == -1 && errno == EINTR &&
685 !_wapi_thread_cur_apc_pending ());
689 DEBUG ("%s: send error: %s", __func__, strerror (errno));
692 /* At least linux returns EAGAIN/EWOULDBLOCK when the timeout has been set on
693 * a blocking socket. See bug #599488 */
694 if (errnum == EAGAIN) {
695 ret = fcntl (fd, F_GETFL, 0);
696 if (ret != -1 && (ret & O_NONBLOCK) == 0)
699 #endif /* O_NONBLOCK */
700 errnum = errno_to_WSA (errnum, __func__);
701 WSASetLastError (errnum);
703 return(SOCKET_ERROR);
708 int _wapi_sendto(guint32 fd, const void *msg, size_t len, int send_flags,
709 const struct sockaddr *to, socklen_t tolen)
711 gpointer handle = GUINT_TO_POINTER (fd);
714 if (startup_count == 0) {
715 WSASetLastError (WSANOTINITIALISED);
716 return(SOCKET_ERROR);
719 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
720 WSASetLastError (WSAENOTSOCK);
721 return(SOCKET_ERROR);
725 ret = sendto (fd, msg, len, send_flags, to, tolen);
726 } while (ret == -1 && errno == EINTR &&
727 !_wapi_thread_cur_apc_pending ());
731 DEBUG ("%s: send error: %s", __func__, strerror (errno));
733 errnum = errno_to_WSA (errnum, __func__);
734 WSASetLastError (errnum);
736 return(SOCKET_ERROR);
742 _wapi_sendmsg(guint32 fd, const struct msghdr *msg, int send_flags)
744 gpointer handle = GUINT_TO_POINTER (fd);
747 if (startup_count == 0) {
748 WSASetLastError (WSANOTINITIALISED);
749 return(SOCKET_ERROR);
752 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
753 WSASetLastError (WSAENOTSOCK);
754 return(SOCKET_ERROR);
758 ret = sendmsg (fd, msg, send_flags);
759 } while (ret == -1 && errno == EINTR &&
760 !_wapi_thread_cur_apc_pending ());
764 DEBUG ("%s: sendmsg error: %s", __func__, strerror (errno));
766 errnum = errno_to_WSA (errnum, __func__);
767 WSASetLastError (errnum);
769 return(SOCKET_ERROR);
774 int _wapi_setsockopt(guint32 fd, int level, int optname,
775 const void *optval, socklen_t optlen)
777 gpointer handle = GUINT_TO_POINTER (fd);
782 if (startup_count == 0) {
783 WSASetLastError (WSANOTINITIALISED);
784 return(SOCKET_ERROR);
787 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
788 WSASetLastError (WSAENOTSOCK);
789 return(SOCKET_ERROR);
793 if (level == SOL_SOCKET &&
794 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
795 int ms = *((int *) optval);
796 tv.tv_sec = ms / 1000;
797 tv.tv_usec = (ms % 1000) * 1000; // micro from milli
799 optlen = sizeof (tv);
800 #if defined (__linux__)
801 } else if (level == SOL_SOCKET &&
802 (optname == SO_SNDBUF || optname == SO_RCVBUF)) {
803 /* According to socket(7) the Linux kernel doubles the
804 * buffer sizes "to allow space for bookkeeping
807 int bufsize = *((int *) optval);
814 ret = setsockopt (fd, level, optname, tmp_val, optlen);
817 DEBUG ("%s: setsockopt error: %s", __func__,
820 errnum = errno_to_WSA (errnum, __func__);
821 WSASetLastError (errnum);
823 return(SOCKET_ERROR);
826 #if defined (SO_REUSEPORT)
827 /* BSD's and MacOS X multicast sockets also need SO_REUSEPORT when SO_REUSEADDR is requested. */
828 if (level == SOL_SOCKET && optname == SO_REUSEADDR) {
830 socklen_t type_len = sizeof (type);
832 if (!getsockopt (fd, level, SO_TYPE, &type, &type_len)) {
833 if (type == SOCK_DGRAM)
834 setsockopt (fd, level, SO_REUSEPORT, tmp_val, optlen);
842 int _wapi_shutdown(guint32 fd, int how)
844 struct _WapiHandle_socket *socket_handle;
846 gpointer handle = GUINT_TO_POINTER (fd);
849 if (startup_count == 0) {
850 WSASetLastError (WSANOTINITIALISED);
851 return(SOCKET_ERROR);
854 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
855 WSASetLastError (WSAENOTSOCK);
856 return(SOCKET_ERROR);
859 if (how == SHUT_RD ||
861 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
862 (gpointer *)&socket_handle);
864 g_warning ("%s: error looking up socket handle %p",
866 WSASetLastError (WSAENOTSOCK);
867 return(SOCKET_ERROR);
870 socket_handle->still_readable = 0;
873 ret = shutdown (fd, how);
876 DEBUG ("%s: shutdown error: %s", __func__,
879 errnum = errno_to_WSA (errnum, __func__);
880 WSASetLastError (errnum);
882 return(SOCKET_ERROR);
888 guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
889 guint32 unused2, guint32 unused3)
891 struct _WapiHandle_socket socket_handle = {0};
895 socket_handle.domain = domain;
896 socket_handle.type = type;
897 socket_handle.protocol = protocol;
898 socket_handle.still_readable = 1;
900 fd = socket (domain, type, protocol);
901 if (fd == -1 && domain == AF_INET && type == SOCK_RAW &&
903 /* Retry with protocol == 4 (see bug #54565) */
904 socket_handle.protocol = 4;
905 fd = socket (AF_INET, SOCK_RAW, 4);
910 DEBUG ("%s: socket error: %s", __func__, strerror (errno));
911 errnum = errno_to_WSA (errnum, __func__);
912 WSASetLastError (errnum);
914 return(INVALID_SOCKET);
917 if (fd >= _wapi_fd_reserve) {
918 DEBUG ("%s: File descriptor is too big (%d >= %d)",
919 __func__, fd, _wapi_fd_reserve);
921 WSASetLastError (WSASYSCALLFAILURE);
924 return(INVALID_SOCKET);
927 /* .net seems to set this by default for SOCK_STREAM, not for
928 * SOCK_DGRAM (see bug #36322)
930 * It seems winsock has a rather different idea of what
931 * SO_REUSEADDR means. If it's set, then a new socket can be
932 * bound over an existing listening socket. There's a new
933 * windows-specific option called SO_EXCLUSIVEADDRUSE but
934 * using that means the socket MUST be closed properly, or a
935 * denial of service can occur. Luckily for us, winsock
936 * behaves as though any other system would when SO_REUSEADDR
937 * is true, so we don't need to do anything else here. See
943 ret = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &true,
948 DEBUG ("%s: Error setting SO_REUSEADDR", __func__);
950 errnum = errno_to_WSA (errnum, __func__);
951 WSASetLastError (errnum);
955 return(INVALID_SOCKET);
960 mono_once (&socket_ops_once, socket_ops_init);
962 handle = _wapi_handle_new_fd (WAPI_HANDLE_SOCKET, fd, &socket_handle);
963 if (handle == _WAPI_HANDLE_INVALID) {
964 g_warning ("%s: error creating socket handle", __func__);
965 WSASetLastError (WSASYSCALLFAILURE);
967 return(INVALID_SOCKET);
970 DEBUG ("%s: returning socket handle %p", __func__, handle);
975 struct hostent *_wapi_gethostbyname(const char *hostname)
979 if (startup_count == 0) {
980 WSASetLastError (WSANOTINITIALISED);
984 he = gethostbyname (hostname);
986 DEBUG ("%s: gethostbyname error: %s", __func__,
991 WSASetLastError (WSAHOST_NOT_FOUND);
993 #if NO_ADDRESS != NO_DATA
997 WSASetLastError (WSANO_DATA);
1000 WSASetLastError (WSANO_RECOVERY);
1003 WSASetLastError (WSATRY_AGAIN);
1006 g_warning ("%s: Need to translate %d into winsock error", __func__, h_errno);
1014 static gboolean socket_disconnect (guint32 fd)
1016 struct _WapiHandle_socket *socket_handle;
1018 gpointer handle = GUINT_TO_POINTER (fd);
1021 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
1022 (gpointer *)&socket_handle);
1024 g_warning ("%s: error looking up socket handle %p", __func__,
1026 WSASetLastError (WSAENOTSOCK);
1030 newsock = socket (socket_handle->domain, socket_handle->type,
1031 socket_handle->protocol);
1032 if (newsock == -1) {
1033 gint errnum = errno;
1035 DEBUG ("%s: socket error: %s", __func__, strerror (errno));
1037 errnum = errno_to_WSA (errnum, __func__);
1038 WSASetLastError (errnum);
1043 /* According to Stevens "Advanced Programming in the UNIX
1044 * Environment: UNIX File I/O" dup2() is atomic so there
1045 * should not be a race condition between the old fd being
1046 * closed and the new socket fd being copied over
1049 ret = dup2 (newsock, fd);
1050 } while (ret == -1 && errno == EAGAIN);
1053 gint errnum = errno;
1055 DEBUG ("%s: dup2 error: %s", __func__, strerror (errno));
1057 errnum = errno_to_WSA (errnum, __func__);
1058 WSASetLastError (errnum);
1068 static gboolean wapi_disconnectex (guint32 fd, WapiOverlapped *overlapped,
1069 guint32 flags, guint32 reserved)
1071 DEBUG ("%s: called on socket %d!", __func__, fd);
1073 if (reserved != 0) {
1074 WSASetLastError (WSAEINVAL);
1078 /* We could check the socket type here and fail unless its
1079 * SOCK_STREAM, SOCK_SEQPACKET or SOCK_RDM (according to msdn)
1080 * if we really wanted to
1083 return(socket_disconnect (fd));
1086 #define SF_BUFFER_SIZE 16384
1088 wapi_sendfile (guint32 socket, gpointer fd, guint32 bytes_to_write, guint32 bytes_per_send, guint32 flags)
1090 #if defined(HAVE_SENDFILE) && (defined(__linux__) || defined(DARWIN))
1091 gint file = GPOINTER_TO_INT (fd);
1095 struct stat statbuf;
1097 n = fstat (file, &statbuf);
1100 errnum = errno_to_WSA (errnum, __func__);
1101 WSASetLastError (errnum);
1102 return SOCKET_ERROR;
1106 res = sendfile (socket, file, NULL, statbuf.st_size);
1107 #elif defined(DARWIN)
1108 /* TODO: header/tail could be sent in the 5th argument */
1109 /* TODO: Might not send the entire file for non-blocking sockets */
1110 res = sendfile (file, socket, 0, &statbuf.st_size, NULL, 0);
1112 } while (res != -1 && (errno == EINTR || errno == EAGAIN) && !_wapi_thread_cur_apc_pending ());
1115 errnum = errno_to_WSA (errnum, __func__);
1116 WSASetLastError (errnum);
1117 return SOCKET_ERROR;
1120 /* Default implementation */
1121 gint file = GPOINTER_TO_INT (fd);
1125 buffer = g_malloc (SF_BUFFER_SIZE);
1128 n = read (file, buffer, SF_BUFFER_SIZE);
1129 } while (n == -1 && errno == EINTR && !_wapi_thread_cur_apc_pending ());
1134 return 0; /* We're done reading */
1137 n = send (socket, buffer, n, 0); /* short sends? enclose this in a loop? */
1138 } while (n == -1 && errno == EINTR && !_wapi_thread_cur_apc_pending ());
1142 gint errnum = errno;
1143 errnum = errno_to_WSA (errnum, __func__);
1144 WSASetLastError (errnum);
1146 return SOCKET_ERROR;
1154 TransmitFile (guint32 socket, gpointer file, guint32 bytes_to_write, guint32 bytes_per_send, WapiOverlapped *ol,
1155 WapiTransmitFileBuffers *buffers, guint32 flags)
1157 gpointer sock = GUINT_TO_POINTER (socket);
1160 if (startup_count == 0) {
1161 WSASetLastError (WSANOTINITIALISED);
1165 if (_wapi_handle_type (sock) != WAPI_HANDLE_SOCKET) {
1166 WSASetLastError (WSAENOTSOCK);
1170 /* Write the header */
1171 if (buffers != NULL && buffers->Head != NULL && buffers->HeadLength > 0) {
1172 ret = _wapi_send (socket, buffers->Head, buffers->HeadLength, 0);
1173 if (ret == SOCKET_ERROR)
1177 ret = wapi_sendfile (socket, file, bytes_to_write, bytes_per_send, flags);
1178 if (ret == SOCKET_ERROR)
1181 /* Write the tail */
1182 if (buffers != NULL && buffers->Tail != NULL && buffers->TailLength > 0) {
1183 ret = _wapi_send (socket, buffers->Tail, buffers->TailLength, 0);
1184 if (ret == SOCKET_ERROR)
1188 if ((flags & TF_DISCONNECT) == TF_DISCONNECT)
1189 closesocket (socket);
1198 } extension_functions[] = {
1199 {WSAID_DISCONNECTEX, wapi_disconnectex},
1200 {WSAID_TRANSMITFILE, TransmitFile},
1205 WSAIoctl (guint32 fd, gint32 command,
1206 gchar *input, gint i_len,
1207 gchar *output, gint o_len, glong *written,
1208 void *unused1, void *unused2)
1210 gpointer handle = GUINT_TO_POINTER (fd);
1212 gchar *buffer = NULL;
1214 if (startup_count == 0) {
1215 WSASetLastError (WSANOTINITIALISED);
1216 return(SOCKET_ERROR);
1219 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1220 WSASetLastError (WSAENOTSOCK);
1221 return SOCKET_ERROR;
1224 if (command == SIO_GET_EXTENSION_FUNCTION_POINTER) {
1226 WapiGuid *guid = (WapiGuid *)input;
1228 if (i_len < sizeof(WapiGuid)) {
1229 /* As far as I can tell, windows doesn't
1230 * actually set an error here...
1232 WSASetLastError (WSAEINVAL);
1233 return(SOCKET_ERROR);
1236 if (o_len < sizeof(gpointer)) {
1238 WSASetLastError (WSAEINVAL);
1239 return(SOCKET_ERROR);
1242 if (output == NULL) {
1244 WSASetLastError (WSAEINVAL);
1245 return(SOCKET_ERROR);
1248 while(extension_functions[i].func != NULL) {
1249 if (!memcmp (guid, &extension_functions[i].guid,
1250 sizeof(WapiGuid))) {
1251 memcpy (output, &extension_functions[i].func,
1253 *written = sizeof(gpointer);
1260 WSASetLastError (WSAEINVAL);
1261 return(SOCKET_ERROR);
1264 if (command == SIO_KEEPALIVE_VALS) {
1266 uint32_t keepalivetime;
1267 uint32_t keepaliveinterval;
1269 if (i_len < (3 * sizeof (uint32_t))) {
1270 WSASetLastError (WSAEINVAL);
1271 return SOCKET_ERROR;
1273 memcpy (&onoff, input, sizeof (uint32_t));
1274 memcpy (&keepalivetime, input + sizeof (uint32_t), sizeof (uint32_t));
1275 memcpy (&keepaliveinterval, input + 2 * sizeof (uint32_t), sizeof (uint32_t));
1276 ret = setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE, &onoff, sizeof (uint32_t));
1278 gint errnum = errno;
1279 errnum = errno_to_WSA (errnum, __func__);
1280 WSASetLastError (errnum);
1281 return SOCKET_ERROR;
1284 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
1285 /* Values are in ms, but we need s */
1288 /* keepalivetime and keepaliveinterval are > 0 (checked in managed code) */
1289 rem = keepalivetime % 1000;
1290 keepalivetime /= 1000;
1291 if (keepalivetime == 0 || rem >= 500)
1293 ret = setsockopt (fd, SOL_TCP, TCP_KEEPIDLE, &keepalivetime, sizeof (uint32_t));
1295 rem = keepaliveinterval % 1000;
1296 keepaliveinterval /= 1000;
1297 if (keepaliveinterval == 0 || rem >= 500)
1298 keepaliveinterval++;
1299 ret = setsockopt (fd, SOL_TCP, TCP_KEEPINTVL, &keepaliveinterval, sizeof (uint32_t));
1302 gint errnum = errno;
1303 errnum = errno_to_WSA (errnum, __func__);
1304 WSASetLastError (errnum);
1305 return SOCKET_ERROR;
1314 buffer = g_memdup (input, i_len);
1317 ret = ioctl (fd, command, buffer);
1319 gint errnum = errno;
1320 DEBUG("%s: WSAIoctl error: %s", __func__,
1323 errnum = errno_to_WSA (errnum, __func__);
1324 WSASetLastError (errnum);
1327 return(SOCKET_ERROR);
1330 if (buffer == NULL) {
1333 /* We just copy the buffer to the output. Some ioctls
1334 * don't even output any data, but, well...
1336 * NB windows returns WSAEFAULT if o_len is too small
1338 i_len = (i_len > o_len) ? o_len : i_len;
1340 if (i_len > 0 && output != NULL) {
1341 memcpy (output, buffer, i_len);
1351 #ifndef PLATFORM_PORT_PROVIDES_IOCTLSOCKET
1352 int ioctlsocket(guint32 fd, gint32 command, gpointer arg)
1354 gpointer handle = GUINT_TO_POINTER (fd);
1357 if (startup_count == 0) {
1358 WSASetLastError (WSANOTINITIALISED);
1359 return(SOCKET_ERROR);
1362 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1363 WSASetLastError (WSAENOTSOCK);
1364 return(SOCKET_ERROR);
1370 /* This works better than ioctl(...FIONBIO...)
1371 * on Linux (it causes connect to return
1372 * EINPROGRESS, but the ioctl doesn't seem to)
1374 ret = fcntl(fd, F_GETFL, 0);
1376 if (*(gboolean *)arg) {
1381 ret = fcntl(fd, F_SETFL, ret);
1384 #endif /* O_NONBLOCK */
1385 /* Unused in Mono */
1387 ret = ioctl (fd, command, arg);
1392 #if defined (PLATFORM_MACOSX)
1394 // ioctl (fd, FIONREAD, XXX) returns the size of
1395 // the UDP header as well on
1398 // Use getsockopt SO_NREAD instead to get the
1399 // right values for TCP and UDP.
1401 // ai_canonname can be null in some cases on darwin, where the runtime assumes it will
1402 // be the value of the ip buffer.
1404 socklen_t optlen = sizeof (int);
1405 ret = getsockopt (fd, SOL_SOCKET, SO_NREAD, arg, &optlen);
1407 ret = ioctl (fd, command, arg);
1412 WSASetLastError (WSAEINVAL);
1413 return(SOCKET_ERROR);
1417 gint errnum = errno;
1418 DEBUG ("%s: ioctl error: %s", __func__, strerror (errno));
1420 errnum = errno_to_WSA (errnum, __func__);
1421 WSASetLastError (errnum);
1423 return(SOCKET_ERROR);
1429 int _wapi_select(int nfds G_GNUC_UNUSED, fd_set *readfds, fd_set *writefds,
1430 fd_set *exceptfds, struct timeval *timeout)
1434 if (startup_count == 0) {
1435 WSASetLastError (WSANOTINITIALISED);
1436 return(SOCKET_ERROR);
1439 for (maxfd = FD_SETSIZE-1; maxfd >= 0; maxfd--) {
1440 if ((readfds && FD_ISSET (maxfd, readfds)) ||
1441 (writefds && FD_ISSET (maxfd, writefds)) ||
1442 (exceptfds && FD_ISSET (maxfd, exceptfds))) {
1448 WSASetLastError (WSAEINVAL);
1449 return(SOCKET_ERROR);
1453 ret = select(maxfd + 1, readfds, writefds, exceptfds,
1455 } while (ret == -1 && errno == EINTR &&
1456 !_wapi_thread_cur_apc_pending ());
1459 gint errnum = errno;
1460 DEBUG ("%s: select error: %s", __func__, strerror (errno));
1461 errnum = errno_to_WSA (errnum, __func__);
1462 WSASetLastError (errnum);
1464 return(SOCKET_ERROR);
1470 void _wapi_FD_CLR(guint32 fd, fd_set *set)
1472 gpointer handle = GUINT_TO_POINTER (fd);
1474 if (fd >= FD_SETSIZE) {
1475 WSASetLastError (WSAEINVAL);
1479 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1480 WSASetLastError (WSAENOTSOCK);
1487 int _wapi_FD_ISSET(guint32 fd, fd_set *set)
1489 gpointer handle = GUINT_TO_POINTER (fd);
1491 if (fd >= FD_SETSIZE) {
1492 WSASetLastError (WSAEINVAL);
1496 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1497 WSASetLastError (WSAENOTSOCK);
1501 return(FD_ISSET (fd, set));
1504 void _wapi_FD_SET(guint32 fd, fd_set *set)
1506 gpointer handle = GUINT_TO_POINTER (fd);
1508 if (fd >= FD_SETSIZE) {
1509 WSASetLastError (WSAEINVAL);
1513 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1514 WSASetLastError (WSAENOTSOCK);
1523 wsabuf_to_msghdr (WapiWSABuf *buffers, guint32 count, struct msghdr *hdr)
1527 memset (hdr, 0, sizeof (struct msghdr));
1528 hdr->msg_iovlen = count;
1529 hdr->msg_iov = g_new0 (struct iovec, count);
1530 for (i = 0; i < count; i++) {
1531 hdr->msg_iov [i].iov_base = buffers [i].buf;
1532 hdr->msg_iov [i].iov_len = buffers [i].len;
1537 msghdr_iov_free (struct msghdr *hdr)
1539 g_free (hdr->msg_iov);
1542 int WSARecv (guint32 fd, WapiWSABuf *buffers, guint32 count, guint32 *received,
1543 guint32 *flags, WapiOverlapped *overlapped,
1544 WapiOverlappedCB *complete)
1549 g_assert (overlapped == NULL);
1550 g_assert (complete == NULL);
1552 wsabuf_to_msghdr (buffers, count, &hdr);
1553 ret = _wapi_recvmsg (fd, &hdr, *flags);
1554 msghdr_iov_free (&hdr);
1556 if(ret == SOCKET_ERROR) {
1561 *flags = hdr.msg_flags;
1566 int WSASend (guint32 fd, WapiWSABuf *buffers, guint32 count, guint32 *sent,
1567 guint32 flags, WapiOverlapped *overlapped,
1568 WapiOverlappedCB *complete)
1573 g_assert (overlapped == NULL);
1574 g_assert (complete == NULL);
1576 wsabuf_to_msghdr (buffers, count, &hdr);
1577 ret = _wapi_sendmsg (fd, &hdr, flags);
1578 msghdr_iov_free (&hdr);
1580 if(ret == SOCKET_ERROR)
1587 #endif /* ifndef DISABLE_SOCKETS */