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);
736 int _wapi_shutdown(guint32 fd, int how)
738 struct _WapiHandle_socket *socket_handle;
740 gpointer handle = GUINT_TO_POINTER (fd);
743 if (startup_count == 0) {
744 WSASetLastError (WSANOTINITIALISED);
745 return(SOCKET_ERROR);
748 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
749 WSASetLastError (WSAENOTSOCK);
750 return(SOCKET_ERROR);
753 if (how == SHUT_RD ||
755 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
756 (gpointer *)&socket_handle);
758 g_warning ("%s: error looking up socket handle %p",
760 WSASetLastError (WSAENOTSOCK);
761 return(SOCKET_ERROR);
764 socket_handle->still_readable = 0;
767 ret = shutdown (fd, how);
771 g_message ("%s: shutdown error: %s", __func__,
775 errnum = errno_to_WSA (errnum, __func__);
776 WSASetLastError (errnum);
778 return(SOCKET_ERROR);
784 guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
785 guint32 unused2, guint32 unused3)
787 struct _WapiHandle_socket socket_handle = {0};
791 socket_handle.domain = domain;
792 socket_handle.type = type;
793 socket_handle.protocol = protocol;
794 socket_handle.still_readable = 1;
796 fd = socket (domain, type, protocol);
797 if (fd == -1 && domain == AF_INET && type == SOCK_RAW &&
799 /* Retry with protocol == 4 (see bug #54565) */
800 socket_handle.protocol = 4;
801 fd = socket (AF_INET, SOCK_RAW, 4);
807 g_message ("%s: socket error: %s", __func__, strerror (errno));
809 errnum = errno_to_WSA (errnum, __func__);
810 WSASetLastError (errnum);
812 return(INVALID_SOCKET);
815 if (fd >= _wapi_fd_reserve) {
817 g_message ("%s: File descriptor is too big (%d >= %d)",
818 __func__, fd, _wapi_fd_reserve);
821 WSASetLastError (WSASYSCALLFAILURE);
824 return(INVALID_SOCKET);
827 /* .net seems to set this by default for SOCK_STREAM, not for
828 * SOCK_DGRAM (see bug #36322)
830 * It seems winsock has a rather different idea of what
831 * SO_REUSEADDR means. If it's set, then a new socket can be
832 * bound over an existing listening socket. There's a new
833 * windows-specific option called SO_EXCLUSIVEADDRUSE but
834 * using that means the socket MUST be closed properly, or a
835 * denial of service can occur. Luckily for us, winsock
836 * behaves as though any other system would when SO_REUSEADDR
837 * is true, so we don't need to do anything else here. See
843 ret = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &true,
849 g_message ("%s: Error setting SO_REUSEADDR", __func__);
852 errnum = errno_to_WSA (errnum, __func__);
853 WSASetLastError (errnum);
857 return(INVALID_SOCKET);
862 mono_once (&socket_ops_once, socket_ops_init);
864 handle = _wapi_handle_new_fd (WAPI_HANDLE_SOCKET, fd, &socket_handle);
865 if (handle == _WAPI_HANDLE_INVALID) {
866 g_warning ("%s: error creating socket handle", __func__);
867 WSASetLastError (WSASYSCALLFAILURE);
869 return(INVALID_SOCKET);
873 g_message ("%s: returning socket handle %p", __func__, handle);
879 struct hostent *_wapi_gethostbyname(const char *hostname)
883 if (startup_count == 0) {
884 WSASetLastError (WSANOTINITIALISED);
888 he = gethostbyname (hostname);
891 g_message ("%s: gethostbyname error: %s", __func__,
897 WSASetLastError (WSAHOST_NOT_FOUND);
899 #if NO_ADDRESS != NO_DATA
903 WSASetLastError (WSANO_DATA);
906 WSASetLastError (WSANO_RECOVERY);
909 WSASetLastError (WSATRY_AGAIN);
912 g_warning ("%s: Need to translate %d into winsock error", __func__, h_errno);
920 static gboolean socket_disconnect (guint32 fd)
922 struct _WapiHandle_socket *socket_handle;
924 gpointer handle = GUINT_TO_POINTER (fd);
927 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
928 (gpointer *)&socket_handle);
930 g_warning ("%s: error looking up socket handle %p", __func__,
932 WSASetLastError (WSAENOTSOCK);
936 newsock = socket (socket_handle->domain, socket_handle->type,
937 socket_handle->protocol);
942 g_message ("%s: socket error: %s", __func__, strerror (errno));
945 errnum = errno_to_WSA (errnum, __func__);
946 WSASetLastError (errnum);
951 /* According to Stevens "Advanced Programming in the UNIX
952 * Environment: UNIX File I/O" dup2() is atomic so there
953 * should not be a race condition between the old fd being
954 * closed and the new socket fd being copied over
957 ret = dup2 (newsock, fd);
958 } while (ret == -1 && errno == EAGAIN);
964 g_message ("%s: dup2 error: %s", __func__, strerror (errno));
967 errnum = errno_to_WSA (errnum, __func__);
968 WSASetLastError (errnum);
978 static gboolean wapi_disconnectex (guint32 fd, WapiOverlapped *overlapped,
979 guint32 flags, guint32 reserved)
982 g_message ("%s: called on socket %d!", __func__, fd);
986 WSASetLastError (WSAEINVAL);
990 /* We could check the socket type here and fail unless its
991 * SOCK_STREAM, SOCK_SEQPACKET or SOCK_RDM (according to msdn)
992 * if we really wanted to
995 return(socket_disconnect (fd));
998 /* NB only supports NULL file handle, NULL buffers and
999 * TF_DISCONNECT|TF_REUSE_SOCKET flags to disconnect the socket fd.
1000 * Shouldn't actually ever need to be called anyway though, because we
1001 * have DisconnectEx ().
1003 static gboolean wapi_transmitfile (guint32 fd, gpointer file,
1004 guint32 num_write, guint32 num_per_send,
1005 WapiOverlapped *overlapped,
1006 WapiTransmitFileBuffers *buffers,
1007 WapiTransmitFileFlags flags)
1010 g_message ("%s: called on socket %d!", __func__, fd);
1013 g_assert (file == NULL);
1014 g_assert (overlapped == NULL);
1015 g_assert (buffers == NULL);
1016 g_assert (num_write == 0);
1017 g_assert (num_per_send == 0);
1018 g_assert (flags == (TF_DISCONNECT | TF_REUSE_SOCKET));
1020 return(socket_disconnect (fd));
1027 } extension_functions[] = {
1028 {WSAID_DISCONNECTEX, wapi_disconnectex},
1029 {WSAID_TRANSMITFILE, wapi_transmitfile},
1034 WSAIoctl (guint32 fd, gint32 command,
1035 gchar *input, gint i_len,
1036 gchar *output, gint o_len, glong *written,
1037 void *unused1, void *unused2)
1039 gpointer handle = GUINT_TO_POINTER (fd);
1041 gchar *buffer = NULL;
1043 if (startup_count == 0) {
1044 WSASetLastError (WSANOTINITIALISED);
1045 return(SOCKET_ERROR);
1048 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1049 WSASetLastError (WSAENOTSOCK);
1050 return SOCKET_ERROR;
1053 if (command == SIO_GET_EXTENSION_FUNCTION_POINTER) {
1055 WapiGuid *guid = (WapiGuid *)input;
1057 if (i_len < sizeof(WapiGuid)) {
1058 /* As far as I can tell, windows doesn't
1059 * actually set an error here...
1061 WSASetLastError (WSAEINVAL);
1062 return(SOCKET_ERROR);
1065 if (o_len < sizeof(gpointer)) {
1067 WSASetLastError (WSAEINVAL);
1068 return(SOCKET_ERROR);
1071 if (output == NULL) {
1073 WSASetLastError (WSAEINVAL);
1074 return(SOCKET_ERROR);
1077 while(extension_functions[i].func != NULL) {
1078 if (!memcmp (guid, &extension_functions[i].guid,
1079 sizeof(WapiGuid))) {
1080 memcpy (output, &extension_functions[i].func,
1082 *written = sizeof(gpointer);
1089 WSASetLastError (WSAEINVAL);
1090 return(SOCKET_ERROR);
1094 buffer = g_memdup (input, i_len);
1097 ret = ioctl (fd, command, buffer);
1099 gint errnum = errno;
1101 g_message("%s: WSAIoctl error: %s", __func__,
1105 errnum = errno_to_WSA (errnum, __func__);
1106 WSASetLastError (errnum);
1109 return(SOCKET_ERROR);
1112 if (buffer == NULL) {
1115 /* We just copy the buffer to the output. Some ioctls
1116 * don't even output any data, but, well...
1118 * NB windows returns WSAEFAULT if o_len is too small
1120 i_len = (i_len > o_len) ? o_len : i_len;
1122 if (i_len > 0 && output != NULL) {
1123 memcpy (output, buffer, i_len);
1133 int ioctlsocket(guint32 fd, gint32 command, gpointer arg)
1135 gpointer handle = GUINT_TO_POINTER (fd);
1138 if (startup_count == 0) {
1139 WSASetLastError (WSANOTINITIALISED);
1140 return(SOCKET_ERROR);
1143 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1144 WSASetLastError (WSAENOTSOCK);
1145 return(SOCKET_ERROR);
1151 /* This works better than ioctl(...FIONBIO...)
1152 * on Linux (it causes connect to return
1153 * EINPROGRESS, but the ioctl doesn't seem to)
1155 ret = fcntl(fd, F_GETFL, 0);
1157 if (*(gboolean *)arg) {
1162 ret = fcntl(fd, F_SETFL, ret);
1165 #endif /* O_NONBLOCK */
1168 ret = ioctl (fd, command, arg);
1171 WSASetLastError (WSAEINVAL);
1172 return(SOCKET_ERROR);
1176 gint errnum = errno;
1178 g_message ("%s: ioctl error: %s", __func__, strerror (errno));
1181 errnum = errno_to_WSA (errnum, __func__);
1182 WSASetLastError (errnum);
1184 return(SOCKET_ERROR);
1190 int _wapi_select(int nfds G_GNUC_UNUSED, fd_set *readfds, fd_set *writefds,
1191 fd_set *exceptfds, struct timeval *timeout)
1195 if (startup_count == 0) {
1196 WSASetLastError (WSANOTINITIALISED);
1197 return(SOCKET_ERROR);
1200 for (maxfd = FD_SETSIZE-1; maxfd >= 0; maxfd--) {
1201 if ((readfds && FD_ISSET (maxfd, readfds)) ||
1202 (writefds && FD_ISSET (maxfd, writefds)) ||
1203 (exceptfds && FD_ISSET (maxfd, exceptfds))) {
1209 WSASetLastError (WSAEINVAL);
1210 return(SOCKET_ERROR);
1214 ret = select(maxfd + 1, readfds, writefds, exceptfds,
1216 } while (ret == -1 && errno == EINTR &&
1217 !_wapi_thread_cur_apc_pending ());
1220 gint errnum = errno;
1222 g_message ("%s: select error: %s", __func__, strerror (errno));
1224 errnum = errno_to_WSA (errnum, __func__);
1225 WSASetLastError (errnum);
1227 return(SOCKET_ERROR);
1233 void _wapi_FD_CLR(guint32 fd, fd_set *set)
1235 gpointer handle = GUINT_TO_POINTER (fd);
1237 if (fd >= FD_SETSIZE) {
1238 WSASetLastError (WSAEINVAL);
1242 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1243 WSASetLastError (WSAENOTSOCK);
1250 int _wapi_FD_ISSET(guint32 fd, fd_set *set)
1252 gpointer handle = GUINT_TO_POINTER (fd);
1254 if (fd >= FD_SETSIZE) {
1255 WSASetLastError (WSAEINVAL);
1259 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1260 WSASetLastError (WSAENOTSOCK);
1264 return(FD_ISSET (fd, set));
1267 void _wapi_FD_SET(guint32 fd, fd_set *set)
1269 gpointer handle = GUINT_TO_POINTER (fd);
1271 if (fd >= FD_SETSIZE) {
1272 WSASetLastError (WSAEINVAL);
1276 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1277 WSASetLastError (WSAENOTSOCK);