2 * sockets.c: Socket handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <sys/ioctl.h>
19 #ifdef HAVE_SYS_FILIO_H
20 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
22 #ifdef HAVE_SYS_SOCKIO_H
23 #include <sys/sockio.h> /* defines SIOCATMARK */
28 #ifndef HAVE_MSG_NOSIGNAL
32 #include <mono/io-layer/wapi.h>
33 #include <mono/io-layer/wapi-private.h>
34 #include <mono/io-layer/socket-private.h>
35 #include <mono/io-layer/handles-private.h>
36 #include <mono/io-layer/socket-wrappers.h>
40 static guint32 startup_count=0;
42 static void socket_close (gpointer handle, gpointer data);
44 struct _WapiHandleOps _wapi_socket_ops = {
45 socket_close, /* close */
49 NULL, /* special_wait */
53 static mono_once_t socket_ops_once=MONO_ONCE_INIT;
55 static void socket_ops_init (void)
57 /* No capabilities to register */
60 static void socket_close (gpointer handle, gpointer data G_GNUC_UNUSED)
65 g_message ("%s: closing socket handle %p", __func__, handle);
68 if (startup_count == 0) {
69 WSASetLastError (WSANOTINITIALISED);
73 /* Shutdown the socket for reading, to interrupt any potential
74 * receives that may be blocking for data. See bug 75705.
76 shutdown (GPOINTER_TO_UINT (handle), SHUT_RD);
79 ret = close (GPOINTER_TO_UINT(handle));
80 } while (ret == -1 && errno == EINTR &&
81 !_wapi_thread_cur_apc_pending ());
86 g_message ("%s: close error: %s", __func__, strerror (errno));
88 errnum = errno_to_WSA (errnum, __func__);
89 WSASetLastError (errnum);
93 int WSAStartup(guint32 requested, WapiWSAData *data)
100 if (requested < MAKEWORD(2,0)) {
101 return(WSAVERNOTSUPPORTED);
106 /* I've no idea what is the minor version of the spec I read */
107 data->wHighVersion = MAKEWORD(2,2);
109 data->wVersion = requested < data->wHighVersion? requested:
113 g_message ("%s: high version 0x%x", __func__, data->wHighVersion);
116 strncpy (data->szDescription, "WAPI", WSADESCRIPTION_LEN);
117 strncpy (data->szSystemStatus, "groovy", WSASYS_STATUS_LEN);
123 cleanup_close (gpointer handle, gpointer data)
125 _wapi_handle_ops_close (handle, NULL);
132 g_message ("%s: cleaning up", __func__);
135 if (--startup_count) {
140 _wapi_handle_foreach (WAPI_HANDLE_SOCKET, cleanup_close, NULL);
144 void WSASetLastError(int error)
146 SetLastError (error);
149 int WSAGetLastError(void)
151 return(GetLastError ());
154 int closesocket(guint32 fd)
156 gpointer handle = GUINT_TO_POINTER (fd);
158 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
159 WSASetLastError (WSAENOTSOCK);
163 _wapi_handle_unref (handle);
167 guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
169 gpointer handle = GUINT_TO_POINTER (fd);
171 struct _WapiHandle_socket *socket_handle;
172 struct _WapiHandle_socket new_socket_handle = {0};
176 if (startup_count == 0) {
177 WSASetLastError (WSANOTINITIALISED);
178 return(INVALID_SOCKET);
181 if (addr != NULL && *addrlen < sizeof(struct sockaddr)) {
182 WSASetLastError (WSAEFAULT);
183 return(INVALID_SOCKET);
186 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
187 WSASetLastError (WSAENOTSOCK);
188 return(INVALID_SOCKET);
191 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
192 (gpointer *)&socket_handle);
194 g_warning ("%s: error looking up socket handle %p",
196 WSASetLastError (WSAENOTSOCK);
197 return(INVALID_SOCKET);
201 new_fd = accept (fd, addr, addrlen);
202 } while (new_fd == -1 && errno == EINTR &&
203 !_wapi_thread_cur_apc_pending());
208 g_message ("%s: accept error: %s", __func__, strerror(errno));
211 errnum = errno_to_WSA (errnum, __func__);
212 WSASetLastError (errnum);
214 return(INVALID_SOCKET);
217 if (new_fd >= _wapi_fd_reserve) {
219 g_message ("%s: File descriptor is too big", __func__);
222 WSASetLastError (WSASYSCALLFAILURE);
226 return(INVALID_SOCKET);
229 new_socket_handle.domain = socket_handle->domain;
230 new_socket_handle.type = socket_handle->type;
231 new_socket_handle.protocol = socket_handle->protocol;
232 new_socket_handle.still_readable = 1;
234 new_handle = _wapi_handle_new_fd (WAPI_HANDLE_SOCKET, new_fd,
236 if(new_handle == _WAPI_HANDLE_INVALID) {
237 g_warning ("%s: error creating socket handle", __func__);
238 WSASetLastError (ERROR_GEN_FAILURE);
239 return(INVALID_SOCKET);
243 g_message ("%s: returning newly accepted socket handle %p with",
244 __func__, new_handle);
250 int _wapi_bind(guint32 fd, struct sockaddr *my_addr, socklen_t addrlen)
252 gpointer handle = GUINT_TO_POINTER (fd);
255 if (startup_count == 0) {
256 WSASetLastError (WSANOTINITIALISED);
257 return(SOCKET_ERROR);
260 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
261 WSASetLastError (WSAENOTSOCK);
262 return(SOCKET_ERROR);
265 ret = bind (fd, my_addr, addrlen);
269 g_message ("%s: bind error: %s", __func__, strerror(errno));
271 errnum = errno_to_WSA (errnum, __func__);
272 WSASetLastError (errnum);
274 return(SOCKET_ERROR);
279 int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
282 gpointer handle = GUINT_TO_POINTER (fd);
283 struct _WapiHandle_socket *socket_handle;
287 if (startup_count == 0) {
288 WSASetLastError (WSANOTINITIALISED);
289 return(SOCKET_ERROR);
292 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
293 WSASetLastError (WSAENOTSOCK);
294 return(SOCKET_ERROR);
297 if (connect (fd, serv_addr, addrlen) == -1) {
304 if (errno != EINTR) {
306 g_message ("%s: connect error: %s", __func__,
310 errnum = errno_to_WSA (errnum, __func__);
311 if (errnum == WSAEINPROGRESS)
312 errnum = WSAEWOULDBLOCK; /* see bug #73053 */
314 WSASetLastError (errnum);
316 return(SOCKET_ERROR);
320 fds.events = POLLOUT;
321 while (poll (&fds, 1, -1) == -1 &&
322 !_wapi_thread_cur_apc_pending ()) {
323 if (errno != EINTR) {
324 errnum = errno_to_WSA (errno, __func__);
327 g_message ("%s: connect poll error: %s",
328 __func__, strerror (errno));
331 WSASetLastError (errnum);
332 return(SOCKET_ERROR);
336 len = sizeof(so_error);
337 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &so_error,
339 errnum = errno_to_WSA (errno, __func__);
342 g_message ("%s: connect getsockopt error: %s",
343 __func__, strerror (errno));
346 WSASetLastError (errnum);
347 return(SOCKET_ERROR);
351 errnum = errno_to_WSA (so_error, __func__);
353 /* Need to save this socket error */
354 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
355 (gpointer *)&socket_handle);
357 g_warning ("%s: error looking up socket handle %p", __func__, handle);
359 socket_handle->saved_error = errnum;
363 g_message ("%s: connect getsockopt returned error: %s",
364 __func__, strerror (so_error));
367 WSASetLastError (errnum);
368 return(SOCKET_ERROR);
375 int _wapi_getpeername(guint32 fd, struct sockaddr *name, socklen_t *namelen)
377 gpointer handle = GUINT_TO_POINTER (fd);
380 if (startup_count == 0) {
381 WSASetLastError (WSANOTINITIALISED);
382 return(SOCKET_ERROR);
385 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
386 WSASetLastError (WSAENOTSOCK);
387 return(SOCKET_ERROR);
390 ret = getpeername (fd, name, namelen);
394 g_message ("%s: getpeername error: %s", __func__,
398 errnum = errno_to_WSA (errnum, __func__);
399 WSASetLastError (errnum);
401 return(SOCKET_ERROR);
407 int _wapi_getsockname(guint32 fd, struct sockaddr *name, socklen_t *namelen)
409 gpointer handle = GUINT_TO_POINTER (fd);
412 if (startup_count == 0) {
413 WSASetLastError (WSANOTINITIALISED);
414 return(SOCKET_ERROR);
417 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
418 WSASetLastError (WSAENOTSOCK);
419 return(SOCKET_ERROR);
422 ret = getsockname (fd, name, namelen);
426 g_message ("%s: getsockname error: %s", __func__,
430 errnum = errno_to_WSA (errnum, __func__);
431 WSASetLastError (errnum);
433 return(SOCKET_ERROR);
439 int _wapi_getsockopt(guint32 fd, int level, int optname, void *optval,
442 gpointer handle = GUINT_TO_POINTER (fd);
446 struct _WapiHandle_socket *socket_handle;
449 if (startup_count == 0) {
450 WSASetLastError (WSANOTINITIALISED);
451 return(SOCKET_ERROR);
454 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
455 WSASetLastError (WSAENOTSOCK);
456 return(SOCKET_ERROR);
460 if (level == SOL_SOCKET &&
461 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
463 *optlen = sizeof (tv);
466 ret = getsockopt (fd, level, optname, tmp_val, optlen);
470 g_message ("%s: getsockopt error: %s", __func__,
474 errnum = errno_to_WSA (errnum, __func__);
475 WSASetLastError (errnum);
477 return(SOCKET_ERROR);
480 if (level == SOL_SOCKET &&
481 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
482 *((int *) optval) = tv.tv_sec * 1000 + (tv.tv_usec / 1000); // milli from micro
483 *optlen = sizeof (int);
486 if (optname == SO_ERROR) {
487 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
488 (gpointer *)&socket_handle);
490 g_warning ("%s: error looking up socket handle %p",
493 /* can't extract the last error */
494 *((int *) optval) = errno_to_WSA (*((int *)optval),
497 if (*((int *)optval) != 0) {
498 *((int *) optval) = errno_to_WSA (*((int *)optval),
500 socket_handle->saved_error = *((int *)optval);
502 *((int *)optval) = socket_handle->saved_error;
510 int _wapi_listen(guint32 fd, int backlog)
512 gpointer handle = GUINT_TO_POINTER (fd);
515 if (startup_count == 0) {
516 WSASetLastError (WSANOTINITIALISED);
517 return(SOCKET_ERROR);
520 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
521 WSASetLastError (WSAENOTSOCK);
522 return(SOCKET_ERROR);
525 ret = listen (fd, backlog);
529 g_message ("%s: listen error: %s", __func__, strerror (errno));
532 errnum = errno_to_WSA (errnum, __func__);
533 WSASetLastError (errnum);
535 return(SOCKET_ERROR);
541 int _wapi_recv(guint32 fd, void *buf, size_t len, int recv_flags)
543 return(_wapi_recvfrom (fd, buf, len, recv_flags, NULL, 0));
546 int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
547 struct sockaddr *from, socklen_t *fromlen)
549 gpointer handle = GUINT_TO_POINTER (fd);
550 struct _WapiHandle_socket *socket_handle;
554 if (startup_count == 0) {
555 WSASetLastError (WSANOTINITIALISED);
556 return(SOCKET_ERROR);
559 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
560 WSASetLastError (WSAENOTSOCK);
561 return(SOCKET_ERROR);
565 ret = recvfrom (fd, buf, len, recv_flags, from, fromlen);
566 } while (ret == -1 && errno == EINTR &&
567 !_wapi_thread_cur_apc_pending ());
569 if (ret == 0 && len > 0) {
570 /* According to the Linux man page, recvfrom only
571 * returns 0 when the socket has been shut down
572 * cleanly. Turn this into an EINTR to simulate win32
573 * behaviour of returning EINTR when a socket is
574 * closed while the recvfrom is blocking (we use a
575 * shutdown() in socket_close() to trigger this.) See
578 /* Distinguish between the socket being shut down at
579 * the local or remote ends, and reads that request 0
583 /* If this returns FALSE, it means the socket has been
584 * closed locally. If it returns TRUE, but
585 * still_readable != 1 then shutdown
586 * (SHUT_RD|SHUT_RDWR) has been called locally.
588 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
589 (gpointer *)&socket_handle);
590 if (ok == FALSE || socket_handle->still_readable != 1) {
599 g_message ("%s: recv error: %s", __func__, strerror(errno));
602 errnum = errno_to_WSA (errnum, __func__);
603 WSASetLastError (errnum);
605 return(SOCKET_ERROR);
610 int _wapi_send(guint32 fd, const void *msg, size_t len, int send_flags)
612 gpointer handle = GUINT_TO_POINTER (fd);
615 if (startup_count == 0) {
616 WSASetLastError (WSANOTINITIALISED);
617 return(SOCKET_ERROR);
620 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
621 WSASetLastError (WSAENOTSOCK);
622 return(SOCKET_ERROR);
626 ret = send (fd, msg, len, send_flags);
627 } while (ret == -1 && errno == EINTR &&
628 !_wapi_thread_cur_apc_pending ());
633 g_message ("%s: send error: %s", __func__, strerror (errno));
636 errnum = errno_to_WSA (errnum, __func__);
637 WSASetLastError (errnum);
639 return(SOCKET_ERROR);
644 int _wapi_sendto(guint32 fd, const void *msg, size_t len, int send_flags,
645 const struct sockaddr *to, socklen_t tolen)
647 gpointer handle = GUINT_TO_POINTER (fd);
650 if (startup_count == 0) {
651 WSASetLastError (WSANOTINITIALISED);
652 return(SOCKET_ERROR);
655 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
656 WSASetLastError (WSAENOTSOCK);
657 return(SOCKET_ERROR);
661 ret = sendto (fd, msg, len, send_flags, to, tolen);
662 } while (ret == -1 && errno == EINTR &&
663 !_wapi_thread_cur_apc_pending ());
668 g_message ("%s: send error: %s", __func__, strerror (errno));
671 errnum = errno_to_WSA (errnum, __func__);
672 WSASetLastError (errnum);
674 return(SOCKET_ERROR);
679 int _wapi_setsockopt(guint32 fd, int level, int optname,
680 const void *optval, socklen_t optlen)
682 gpointer handle = GUINT_TO_POINTER (fd);
687 if (startup_count == 0) {
688 WSASetLastError (WSANOTINITIALISED);
689 return(SOCKET_ERROR);
692 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
693 WSASetLastError (WSAENOTSOCK);
694 return(SOCKET_ERROR);
698 if (level == SOL_SOCKET &&
699 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
700 int ms = *((int *) optval);
701 tv.tv_sec = ms / 1000;
702 tv.tv_usec = (ms % 1000) * 1000; // micro from milli
704 optlen = sizeof (tv);
705 #if defined (__linux__)
706 } else if (level == SOL_SOCKET &&
707 (optname == SO_SNDBUF || optname == SO_RCVBUF)) {
708 /* According to socket(7) the Linux kernel doubles the
709 * buffer sizes "to allow space for bookkeeping
712 int bufsize = *((int *) optval);
719 ret = setsockopt (fd, level, optname, tmp_val, optlen);
723 g_message ("%s: setsockopt error: %s", __func__,
727 errnum = errno_to_WSA (errnum, __func__);
728 WSASetLastError (errnum);
730 return(SOCKET_ERROR);
733 #if defined(__FreeBSD__)
734 /* FreeBSD's multicast sockets also need SO_REUSEPORT when SO_REUSEADDR is requested. */
735 if (level == SOL_SOCKET && optname == SO_REUSEADDR) {
737 int type_len = sizeof (type);
739 if (!getsockopt (fd, level, SO_TYPE, &type, &type_len)) {
740 if (type == SOCK_DGRAM)
741 setsockopt (fd, level, SO_REUSEPORT, tmp_val, optlen);
749 int _wapi_shutdown(guint32 fd, int how)
751 struct _WapiHandle_socket *socket_handle;
753 gpointer handle = GUINT_TO_POINTER (fd);
756 if (startup_count == 0) {
757 WSASetLastError (WSANOTINITIALISED);
758 return(SOCKET_ERROR);
761 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
762 WSASetLastError (WSAENOTSOCK);
763 return(SOCKET_ERROR);
766 if (how == SHUT_RD ||
768 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
769 (gpointer *)&socket_handle);
771 g_warning ("%s: error looking up socket handle %p",
773 WSASetLastError (WSAENOTSOCK);
774 return(SOCKET_ERROR);
777 socket_handle->still_readable = 0;
780 ret = shutdown (fd, how);
784 g_message ("%s: shutdown error: %s", __func__,
788 errnum = errno_to_WSA (errnum, __func__);
789 WSASetLastError (errnum);
791 return(SOCKET_ERROR);
797 guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
798 guint32 unused2, guint32 unused3)
800 struct _WapiHandle_socket socket_handle = {0};
804 socket_handle.domain = domain;
805 socket_handle.type = type;
806 socket_handle.protocol = protocol;
807 socket_handle.still_readable = 1;
809 fd = socket (domain, type, protocol);
810 if (fd == -1 && domain == AF_INET && type == SOCK_RAW &&
812 /* Retry with protocol == 4 (see bug #54565) */
813 socket_handle.protocol = 4;
814 fd = socket (AF_INET, SOCK_RAW, 4);
820 g_message ("%s: socket error: %s", __func__, strerror (errno));
822 errnum = errno_to_WSA (errnum, __func__);
823 WSASetLastError (errnum);
825 return(INVALID_SOCKET);
828 if (fd >= _wapi_fd_reserve) {
830 g_message ("%s: File descriptor is too big (%d >= %d)",
831 __func__, fd, _wapi_fd_reserve);
834 WSASetLastError (WSASYSCALLFAILURE);
837 return(INVALID_SOCKET);
840 /* .net seems to set this by default for SOCK_STREAM, not for
841 * SOCK_DGRAM (see bug #36322)
843 * It seems winsock has a rather different idea of what
844 * SO_REUSEADDR means. If it's set, then a new socket can be
845 * bound over an existing listening socket. There's a new
846 * windows-specific option called SO_EXCLUSIVEADDRUSE but
847 * using that means the socket MUST be closed properly, or a
848 * denial of service can occur. Luckily for us, winsock
849 * behaves as though any other system would when SO_REUSEADDR
850 * is true, so we don't need to do anything else here. See
856 ret = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &true,
862 g_message ("%s: Error setting SO_REUSEADDR", __func__);
865 errnum = errno_to_WSA (errnum, __func__);
866 WSASetLastError (errnum);
870 return(INVALID_SOCKET);
875 mono_once (&socket_ops_once, socket_ops_init);
877 handle = _wapi_handle_new_fd (WAPI_HANDLE_SOCKET, fd, &socket_handle);
878 if (handle == _WAPI_HANDLE_INVALID) {
879 g_warning ("%s: error creating socket handle", __func__);
880 WSASetLastError (WSASYSCALLFAILURE);
882 return(INVALID_SOCKET);
886 g_message ("%s: returning socket handle %p", __func__, handle);
892 struct hostent *_wapi_gethostbyname(const char *hostname)
896 if (startup_count == 0) {
897 WSASetLastError (WSANOTINITIALISED);
901 he = gethostbyname (hostname);
904 g_message ("%s: gethostbyname error: %s", __func__,
910 WSASetLastError (WSAHOST_NOT_FOUND);
912 #if NO_ADDRESS != NO_DATA
916 WSASetLastError (WSANO_DATA);
919 WSASetLastError (WSANO_RECOVERY);
922 WSASetLastError (WSATRY_AGAIN);
925 g_warning ("%s: Need to translate %d into winsock error", __func__, h_errno);
933 static gboolean socket_disconnect (guint32 fd)
935 struct _WapiHandle_socket *socket_handle;
937 gpointer handle = GUINT_TO_POINTER (fd);
940 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
941 (gpointer *)&socket_handle);
943 g_warning ("%s: error looking up socket handle %p", __func__,
945 WSASetLastError (WSAENOTSOCK);
949 newsock = socket (socket_handle->domain, socket_handle->type,
950 socket_handle->protocol);
955 g_message ("%s: socket error: %s", __func__, strerror (errno));
958 errnum = errno_to_WSA (errnum, __func__);
959 WSASetLastError (errnum);
964 /* According to Stevens "Advanced Programming in the UNIX
965 * Environment: UNIX File I/O" dup2() is atomic so there
966 * should not be a race condition between the old fd being
967 * closed and the new socket fd being copied over
970 ret = dup2 (newsock, fd);
971 } while (ret == -1 && errno == EAGAIN);
977 g_message ("%s: dup2 error: %s", __func__, strerror (errno));
980 errnum = errno_to_WSA (errnum, __func__);
981 WSASetLastError (errnum);
991 static gboolean wapi_disconnectex (guint32 fd, WapiOverlapped *overlapped,
992 guint32 flags, guint32 reserved)
995 g_message ("%s: called on socket %d!", __func__, fd);
999 WSASetLastError (WSAEINVAL);
1003 /* We could check the socket type here and fail unless its
1004 * SOCK_STREAM, SOCK_SEQPACKET or SOCK_RDM (according to msdn)
1005 * if we really wanted to
1008 return(socket_disconnect (fd));
1011 /* NB only supports NULL file handle, NULL buffers and
1012 * TF_DISCONNECT|TF_REUSE_SOCKET flags to disconnect the socket fd.
1013 * Shouldn't actually ever need to be called anyway though, because we
1014 * have DisconnectEx ().
1016 static gboolean wapi_transmitfile (guint32 fd, gpointer file,
1017 guint32 num_write, guint32 num_per_send,
1018 WapiOverlapped *overlapped,
1019 WapiTransmitFileBuffers *buffers,
1020 WapiTransmitFileFlags flags)
1023 g_message ("%s: called on socket %d!", __func__, fd);
1026 g_assert (file == NULL);
1027 g_assert (overlapped == NULL);
1028 g_assert (buffers == NULL);
1029 g_assert (num_write == 0);
1030 g_assert (num_per_send == 0);
1031 g_assert (flags == (TF_DISCONNECT | TF_REUSE_SOCKET));
1033 return(socket_disconnect (fd));
1040 } extension_functions[] = {
1041 {WSAID_DISCONNECTEX, wapi_disconnectex},
1042 {WSAID_TRANSMITFILE, wapi_transmitfile},
1047 WSAIoctl (guint32 fd, gint32 command,
1048 gchar *input, gint i_len,
1049 gchar *output, gint o_len, glong *written,
1050 void *unused1, void *unused2)
1052 gpointer handle = GUINT_TO_POINTER (fd);
1054 gchar *buffer = NULL;
1056 if (startup_count == 0) {
1057 WSASetLastError (WSANOTINITIALISED);
1058 return(SOCKET_ERROR);
1061 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1062 WSASetLastError (WSAENOTSOCK);
1063 return SOCKET_ERROR;
1066 if (command == SIO_GET_EXTENSION_FUNCTION_POINTER) {
1068 WapiGuid *guid = (WapiGuid *)input;
1070 if (i_len < sizeof(WapiGuid)) {
1071 /* As far as I can tell, windows doesn't
1072 * actually set an error here...
1074 WSASetLastError (WSAEINVAL);
1075 return(SOCKET_ERROR);
1078 if (o_len < sizeof(gpointer)) {
1080 WSASetLastError (WSAEINVAL);
1081 return(SOCKET_ERROR);
1084 if (output == NULL) {
1086 WSASetLastError (WSAEINVAL);
1087 return(SOCKET_ERROR);
1090 while(extension_functions[i].func != NULL) {
1091 if (!memcmp (guid, &extension_functions[i].guid,
1092 sizeof(WapiGuid))) {
1093 memcpy (output, &extension_functions[i].func,
1095 *written = sizeof(gpointer);
1102 WSASetLastError (WSAEINVAL);
1103 return(SOCKET_ERROR);
1107 buffer = g_memdup (input, i_len);
1110 ret = ioctl (fd, command, buffer);
1112 gint errnum = errno;
1114 g_message("%s: WSAIoctl error: %s", __func__,
1118 errnum = errno_to_WSA (errnum, __func__);
1119 WSASetLastError (errnum);
1122 return(SOCKET_ERROR);
1125 if (buffer == NULL) {
1128 /* We just copy the buffer to the output. Some ioctls
1129 * don't even output any data, but, well...
1131 * NB windows returns WSAEFAULT if o_len is too small
1133 i_len = (i_len > o_len) ? o_len : i_len;
1135 if (i_len > 0 && output != NULL) {
1136 memcpy (output, buffer, i_len);
1146 int ioctlsocket(guint32 fd, gint32 command, gpointer arg)
1148 gpointer handle = GUINT_TO_POINTER (fd);
1151 if (startup_count == 0) {
1152 WSASetLastError (WSANOTINITIALISED);
1153 return(SOCKET_ERROR);
1156 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1157 WSASetLastError (WSAENOTSOCK);
1158 return(SOCKET_ERROR);
1164 /* This works better than ioctl(...FIONBIO...)
1165 * on Linux (it causes connect to return
1166 * EINPROGRESS, but the ioctl doesn't seem to)
1168 ret = fcntl(fd, F_GETFL, 0);
1170 if (*(gboolean *)arg) {
1175 ret = fcntl(fd, F_SETFL, ret);
1178 #endif /* O_NONBLOCK */
1181 ret = ioctl (fd, command, arg);
1184 WSASetLastError (WSAEINVAL);
1185 return(SOCKET_ERROR);
1189 gint errnum = errno;
1191 g_message ("%s: ioctl error: %s", __func__, strerror (errno));
1194 errnum = errno_to_WSA (errnum, __func__);
1195 WSASetLastError (errnum);
1197 return(SOCKET_ERROR);
1203 int _wapi_select(int nfds G_GNUC_UNUSED, fd_set *readfds, fd_set *writefds,
1204 fd_set *exceptfds, struct timeval *timeout)
1208 if (startup_count == 0) {
1209 WSASetLastError (WSANOTINITIALISED);
1210 return(SOCKET_ERROR);
1213 for (maxfd = FD_SETSIZE-1; maxfd >= 0; maxfd--) {
1214 if ((readfds && FD_ISSET (maxfd, readfds)) ||
1215 (writefds && FD_ISSET (maxfd, writefds)) ||
1216 (exceptfds && FD_ISSET (maxfd, exceptfds))) {
1222 WSASetLastError (WSAEINVAL);
1223 return(SOCKET_ERROR);
1227 ret = select(maxfd + 1, readfds, writefds, exceptfds,
1229 } while (ret == -1 && errno == EINTR &&
1230 !_wapi_thread_cur_apc_pending ());
1233 gint errnum = errno;
1235 g_message ("%s: select error: %s", __func__, strerror (errno));
1237 errnum = errno_to_WSA (errnum, __func__);
1238 WSASetLastError (errnum);
1240 return(SOCKET_ERROR);
1246 void _wapi_FD_CLR(guint32 fd, fd_set *set)
1248 gpointer handle = GUINT_TO_POINTER (fd);
1250 if (fd >= FD_SETSIZE) {
1251 WSASetLastError (WSAEINVAL);
1255 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1256 WSASetLastError (WSAENOTSOCK);
1263 int _wapi_FD_ISSET(guint32 fd, fd_set *set)
1265 gpointer handle = GUINT_TO_POINTER (fd);
1267 if (fd >= FD_SETSIZE) {
1268 WSASetLastError (WSAEINVAL);
1272 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1273 WSASetLastError (WSAENOTSOCK);
1277 return(FD_ISSET (fd, set));
1280 void _wapi_FD_SET(guint32 fd, fd_set *set)
1282 gpointer handle = GUINT_TO_POINTER (fd);
1284 if (fd >= FD_SETSIZE) {
1285 WSASetLastError (WSAEINVAL);
1289 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1290 WSASetLastError (WSAENOTSOCK);