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>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
41 #include <arpa/inet.h>
45 static guint32 startup_count=0;
47 static void socket_close (gpointer handle, gpointer data);
49 struct _WapiHandleOps _wapi_socket_ops = {
50 socket_close, /* close */
54 NULL, /* special_wait */
58 static mono_once_t socket_ops_once=MONO_ONCE_INIT;
60 static void socket_ops_init (void)
62 /* No capabilities to register */
65 static void socket_close (gpointer handle, gpointer data)
68 struct _WapiHandle_socket *socket_handle = (struct _WapiHandle_socket *)data;
71 g_message ("%s: closing socket handle %p", __func__, handle);
74 if (startup_count == 0) {
75 WSASetLastError (WSANOTINITIALISED);
79 /* Shutdown the socket for reading, to interrupt any potential
80 * receives that may be blocking for data. See bug 75705.
82 shutdown (GPOINTER_TO_UINT (handle), SHUT_RD);
85 ret = close (GPOINTER_TO_UINT(handle));
86 } while (ret == -1 && errno == EINTR &&
87 !_wapi_thread_cur_apc_pending ());
92 g_message ("%s: close error: %s", __func__, strerror (errno));
94 errnum = errno_to_WSA (errnum, __func__);
95 WSASetLastError (errnum);
98 socket_handle->saved_error = 0;
101 int WSAStartup(guint32 requested, WapiWSAData *data)
107 /* Insist on v2.0+ */
108 if (requested < MAKEWORD(2,0)) {
109 return(WSAVERNOTSUPPORTED);
114 /* I've no idea what is the minor version of the spec I read */
115 data->wHighVersion = MAKEWORD(2,2);
117 data->wVersion = requested < data->wHighVersion? requested:
121 g_message ("%s: high version 0x%x", __func__, data->wHighVersion);
124 strncpy (data->szDescription, "WAPI", WSADESCRIPTION_LEN);
125 strncpy (data->szSystemStatus, "groovy", WSASYS_STATUS_LEN);
131 cleanup_close (gpointer handle, gpointer data)
133 _wapi_handle_ops_close (handle, NULL);
140 g_message ("%s: cleaning up", __func__);
143 if (--startup_count) {
148 _wapi_handle_foreach (WAPI_HANDLE_SOCKET, cleanup_close, NULL);
152 void WSASetLastError(int error)
154 SetLastError (error);
157 int WSAGetLastError(void)
159 return(GetLastError ());
162 int closesocket(guint32 fd)
164 gpointer handle = GUINT_TO_POINTER (fd);
166 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
167 WSASetLastError (WSAENOTSOCK);
171 _wapi_handle_unref (handle);
175 guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
177 gpointer handle = GUINT_TO_POINTER (fd);
179 struct _WapiHandle_socket *socket_handle;
180 struct _WapiHandle_socket new_socket_handle = {0};
184 if (startup_count == 0) {
185 WSASetLastError (WSANOTINITIALISED);
186 return(INVALID_SOCKET);
189 if (addr != NULL && *addrlen < sizeof(struct sockaddr)) {
190 WSASetLastError (WSAEFAULT);
191 return(INVALID_SOCKET);
194 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
195 WSASetLastError (WSAENOTSOCK);
196 return(INVALID_SOCKET);
199 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
200 (gpointer *)&socket_handle);
202 g_warning ("%s: error looking up socket handle %p",
204 WSASetLastError (WSAENOTSOCK);
205 return(INVALID_SOCKET);
209 new_fd = accept (fd, addr, addrlen);
210 } while (new_fd == -1 && errno == EINTR &&
211 !_wapi_thread_cur_apc_pending());
216 g_message ("%s: accept error: %s", __func__, strerror(errno));
219 errnum = errno_to_WSA (errnum, __func__);
220 WSASetLastError (errnum);
222 return(INVALID_SOCKET);
225 if (new_fd >= _wapi_fd_reserve) {
227 g_message ("%s: File descriptor is too big", __func__);
230 WSASetLastError (WSASYSCALLFAILURE);
234 return(INVALID_SOCKET);
237 new_socket_handle.domain = socket_handle->domain;
238 new_socket_handle.type = socket_handle->type;
239 new_socket_handle.protocol = socket_handle->protocol;
240 new_socket_handle.still_readable = 1;
242 new_handle = _wapi_handle_new_fd (WAPI_HANDLE_SOCKET, new_fd,
244 if(new_handle == _WAPI_HANDLE_INVALID) {
245 g_warning ("%s: error creating socket handle", __func__);
246 WSASetLastError (ERROR_GEN_FAILURE);
247 return(INVALID_SOCKET);
251 g_message ("%s: returning newly accepted socket handle %p with",
252 __func__, new_handle);
258 int _wapi_bind(guint32 fd, struct sockaddr *my_addr, socklen_t addrlen)
260 gpointer handle = GUINT_TO_POINTER (fd);
263 if (startup_count == 0) {
264 WSASetLastError (WSANOTINITIALISED);
265 return(SOCKET_ERROR);
268 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
269 WSASetLastError (WSAENOTSOCK);
270 return(SOCKET_ERROR);
273 ret = bind (fd, my_addr, addrlen);
277 g_message ("%s: bind error: %s", __func__, strerror(errno));
279 errnum = errno_to_WSA (errnum, __func__);
280 WSASetLastError (errnum);
282 return(SOCKET_ERROR);
287 int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
290 gpointer handle = GUINT_TO_POINTER (fd);
291 struct _WapiHandle_socket *socket_handle;
295 if (startup_count == 0) {
296 WSASetLastError (WSANOTINITIALISED);
297 return(SOCKET_ERROR);
300 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
301 WSASetLastError (WSAENOTSOCK);
302 return(SOCKET_ERROR);
305 if (connect (fd, serv_addr, addrlen) == -1) {
312 if (errno != EINTR) {
314 g_message ("%s: connect error: %s", __func__,
318 errnum = errno_to_WSA (errnum, __func__);
319 if (errnum == WSAEINPROGRESS)
320 errnum = WSAEWOULDBLOCK; /* see bug #73053 */
322 WSASetLastError (errnum);
325 * On solaris x86 getsockopt (SO_ERROR) is not set after
326 * connect () fails so we need to save this error.
328 * But don't do this for EWOULDBLOCK (bug 317315)
330 if (errnum != WSAEWOULDBLOCK) {
331 ok = _wapi_lookup_handle (handle,
333 (gpointer *)&socket_handle);
335 g_warning ("%s: error looking up socket handle %p", __func__, handle);
337 socket_handle->saved_error = errnum;
340 return(SOCKET_ERROR);
344 fds.events = POLLOUT;
345 while (poll (&fds, 1, -1) == -1 &&
346 !_wapi_thread_cur_apc_pending ()) {
347 if (errno != EINTR) {
348 errnum = errno_to_WSA (errno, __func__);
351 g_message ("%s: connect poll error: %s",
352 __func__, strerror (errno));
355 WSASetLastError (errnum);
356 return(SOCKET_ERROR);
360 len = sizeof(so_error);
361 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &so_error,
363 errnum = errno_to_WSA (errno, __func__);
366 g_message ("%s: connect getsockopt error: %s",
367 __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;
387 g_message ("%s: connect getsockopt returned error: %s",
388 __func__, strerror (so_error));
391 WSASetLastError (errnum);
392 return(SOCKET_ERROR);
399 int _wapi_getpeername(guint32 fd, struct sockaddr *name, socklen_t *namelen)
401 gpointer handle = GUINT_TO_POINTER (fd);
404 if (startup_count == 0) {
405 WSASetLastError (WSANOTINITIALISED);
406 return(SOCKET_ERROR);
409 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
410 WSASetLastError (WSAENOTSOCK);
411 return(SOCKET_ERROR);
414 ret = getpeername (fd, name, namelen);
418 g_message ("%s: getpeername error: %s", __func__,
422 errnum = errno_to_WSA (errnum, __func__);
423 WSASetLastError (errnum);
425 return(SOCKET_ERROR);
431 int _wapi_getsockname(guint32 fd, struct sockaddr *name, socklen_t *namelen)
433 gpointer handle = GUINT_TO_POINTER (fd);
436 if (startup_count == 0) {
437 WSASetLastError (WSANOTINITIALISED);
438 return(SOCKET_ERROR);
441 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
442 WSASetLastError (WSAENOTSOCK);
443 return(SOCKET_ERROR);
446 ret = getsockname (fd, name, namelen);
450 g_message ("%s: getsockname error: %s", __func__,
454 errnum = errno_to_WSA (errnum, __func__);
455 WSASetLastError (errnum);
457 return(SOCKET_ERROR);
463 int _wapi_getsockopt(guint32 fd, int level, int optname, void *optval,
466 gpointer handle = GUINT_TO_POINTER (fd);
470 struct _WapiHandle_socket *socket_handle;
473 if (startup_count == 0) {
474 WSASetLastError (WSANOTINITIALISED);
475 return(SOCKET_ERROR);
478 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
479 WSASetLastError (WSAENOTSOCK);
480 return(SOCKET_ERROR);
484 if (level == SOL_SOCKET &&
485 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
487 *optlen = sizeof (tv);
490 ret = getsockopt (fd, level, optname, tmp_val, optlen);
494 g_message ("%s: getsockopt error: %s", __func__,
498 errnum = errno_to_WSA (errnum, __func__);
499 WSASetLastError (errnum);
501 return(SOCKET_ERROR);
504 if (level == SOL_SOCKET &&
505 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
506 *((int *) optval) = tv.tv_sec * 1000 + (tv.tv_usec / 1000); // milli from micro
507 *optlen = sizeof (int);
510 if (optname == SO_ERROR) {
511 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
512 (gpointer *)&socket_handle);
514 g_warning ("%s: error looking up socket handle %p",
517 /* can't extract the last error */
518 *((int *) optval) = errno_to_WSA (*((int *)optval),
521 if (*((int *)optval) != 0) {
522 *((int *) optval) = errno_to_WSA (*((int *)optval),
524 socket_handle->saved_error = *((int *)optval);
526 *((int *)optval) = socket_handle->saved_error;
534 int _wapi_listen(guint32 fd, int backlog)
536 gpointer handle = GUINT_TO_POINTER (fd);
539 if (startup_count == 0) {
540 WSASetLastError (WSANOTINITIALISED);
541 return(SOCKET_ERROR);
544 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
545 WSASetLastError (WSAENOTSOCK);
546 return(SOCKET_ERROR);
549 ret = listen (fd, backlog);
553 g_message ("%s: listen error: %s", __func__, strerror (errno));
556 errnum = errno_to_WSA (errnum, __func__);
557 WSASetLastError (errnum);
559 return(SOCKET_ERROR);
565 int _wapi_recv(guint32 fd, void *buf, size_t len, int recv_flags)
567 return(_wapi_recvfrom (fd, buf, len, recv_flags, NULL, 0));
570 int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
571 struct sockaddr *from, socklen_t *fromlen)
573 gpointer handle = GUINT_TO_POINTER (fd);
574 struct _WapiHandle_socket *socket_handle;
578 if (startup_count == 0) {
579 WSASetLastError (WSANOTINITIALISED);
580 return(SOCKET_ERROR);
583 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
584 WSASetLastError (WSAENOTSOCK);
585 return(SOCKET_ERROR);
589 ret = recvfrom (fd, buf, len, recv_flags, from, fromlen);
590 } while (ret == -1 && errno == EINTR &&
591 !_wapi_thread_cur_apc_pending ());
593 if (ret == 0 && len > 0) {
594 /* According to the Linux man page, recvfrom only
595 * returns 0 when the socket has been shut down
596 * cleanly. Turn this into an EINTR to simulate win32
597 * behaviour of returning EINTR when a socket is
598 * closed while the recvfrom is blocking (we use a
599 * shutdown() in socket_close() to trigger this.) See
602 /* Distinguish between the socket being shut down at
603 * the local or remote ends, and reads that request 0
607 /* If this returns FALSE, it means the socket has been
608 * closed locally. If it returns TRUE, but
609 * still_readable != 1 then shutdown
610 * (SHUT_RD|SHUT_RDWR) has been called locally.
612 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
613 (gpointer *)&socket_handle);
614 if (ok == FALSE || socket_handle->still_readable != 1) {
623 g_message ("%s: recv error: %s", __func__, strerror(errno));
626 errnum = errno_to_WSA (errnum, __func__);
627 WSASetLastError (errnum);
629 return(SOCKET_ERROR);
635 _wapi_recvmsg(guint32 fd, struct msghdr *msg, int recv_flags)
637 gpointer handle = GUINT_TO_POINTER (fd);
638 struct _WapiHandle_socket *socket_handle;
642 if (startup_count == 0) {
643 WSASetLastError (WSANOTINITIALISED);
644 return(SOCKET_ERROR);
647 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
648 WSASetLastError (WSAENOTSOCK);
649 return(SOCKET_ERROR);
653 ret = recvmsg (fd, msg, recv_flags);
654 } while (ret == -1 && errno == EINTR &&
655 !_wapi_thread_cur_apc_pending ());
658 /* see _wapi_recvfrom */
659 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
660 (gpointer *)&socket_handle);
661 if (ok == FALSE || socket_handle->still_readable != 1) {
670 g_message ("%s: recvmsg error: %s", __func__, strerror(errno));
673 errnum = errno_to_WSA (errnum, __func__);
674 WSASetLastError (errnum);
676 return(SOCKET_ERROR);
681 int _wapi_send(guint32 fd, const void *msg, size_t len, int send_flags)
683 gpointer handle = GUINT_TO_POINTER (fd);
686 if (startup_count == 0) {
687 WSASetLastError (WSANOTINITIALISED);
688 return(SOCKET_ERROR);
691 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
692 WSASetLastError (WSAENOTSOCK);
693 return(SOCKET_ERROR);
697 ret = send (fd, msg, len, send_flags);
698 } while (ret == -1 && errno == EINTR &&
699 !_wapi_thread_cur_apc_pending ());
704 g_message ("%s: send error: %s", __func__, strerror (errno));
707 errnum = errno_to_WSA (errnum, __func__);
708 WSASetLastError (errnum);
710 return(SOCKET_ERROR);
715 int _wapi_sendto(guint32 fd, const void *msg, size_t len, int send_flags,
716 const struct sockaddr *to, socklen_t tolen)
718 gpointer handle = GUINT_TO_POINTER (fd);
721 if (startup_count == 0) {
722 WSASetLastError (WSANOTINITIALISED);
723 return(SOCKET_ERROR);
726 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
727 WSASetLastError (WSAENOTSOCK);
728 return(SOCKET_ERROR);
732 ret = sendto (fd, msg, len, send_flags, to, tolen);
733 } while (ret == -1 && errno == EINTR &&
734 !_wapi_thread_cur_apc_pending ());
739 g_message ("%s: send error: %s", __func__, strerror (errno));
742 errnum = errno_to_WSA (errnum, __func__);
743 WSASetLastError (errnum);
745 return(SOCKET_ERROR);
751 _wapi_sendmsg(guint32 fd, const struct msghdr *msg, int send_flags)
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);
767 ret = sendmsg (fd, msg, send_flags);
768 } while (ret == -1 && errno == EINTR &&
769 !_wapi_thread_cur_apc_pending ());
774 g_message ("%s: sendmsg error: %s", __func__, strerror (errno));
777 errnum = errno_to_WSA (errnum, __func__);
778 WSASetLastError (errnum);
780 return(SOCKET_ERROR);
785 int _wapi_setsockopt(guint32 fd, int level, int optname,
786 const void *optval, socklen_t optlen)
788 gpointer handle = GUINT_TO_POINTER (fd);
793 if (startup_count == 0) {
794 WSASetLastError (WSANOTINITIALISED);
795 return(SOCKET_ERROR);
798 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
799 WSASetLastError (WSAENOTSOCK);
800 return(SOCKET_ERROR);
804 if (level == SOL_SOCKET &&
805 (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
806 int ms = *((int *) optval);
807 tv.tv_sec = ms / 1000;
808 tv.tv_usec = (ms % 1000) * 1000; // micro from milli
810 optlen = sizeof (tv);
811 #if defined (__linux__)
812 } else if (level == SOL_SOCKET &&
813 (optname == SO_SNDBUF || optname == SO_RCVBUF)) {
814 /* According to socket(7) the Linux kernel doubles the
815 * buffer sizes "to allow space for bookkeeping
818 int bufsize = *((int *) optval);
825 ret = setsockopt (fd, level, optname, tmp_val, optlen);
829 g_message ("%s: setsockopt error: %s", __func__,
833 errnum = errno_to_WSA (errnum, __func__);
834 WSASetLastError (errnum);
836 return(SOCKET_ERROR);
839 #if defined (SO_REUSEPORT)
840 /* BSD's and MacOS X multicast sockets also need SO_REUSEPORT when SO_REUSEADDR is requested. */
841 if (level == SOL_SOCKET && optname == SO_REUSEADDR) {
843 int type_len = sizeof (type);
845 if (!getsockopt (fd, level, SO_TYPE, &type, &type_len)) {
846 if (type == SOCK_DGRAM)
847 setsockopt (fd, level, SO_REUSEPORT, tmp_val, optlen);
855 int _wapi_shutdown(guint32 fd, int how)
857 struct _WapiHandle_socket *socket_handle;
859 gpointer handle = GUINT_TO_POINTER (fd);
862 if (startup_count == 0) {
863 WSASetLastError (WSANOTINITIALISED);
864 return(SOCKET_ERROR);
867 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
868 WSASetLastError (WSAENOTSOCK);
869 return(SOCKET_ERROR);
872 if (how == SHUT_RD ||
874 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
875 (gpointer *)&socket_handle);
877 g_warning ("%s: error looking up socket handle %p",
879 WSASetLastError (WSAENOTSOCK);
880 return(SOCKET_ERROR);
883 socket_handle->still_readable = 0;
886 ret = shutdown (fd, how);
890 g_message ("%s: shutdown error: %s", __func__,
894 errnum = errno_to_WSA (errnum, __func__);
895 WSASetLastError (errnum);
897 return(SOCKET_ERROR);
903 guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
904 guint32 unused2, guint32 unused3)
906 struct _WapiHandle_socket socket_handle = {0};
910 socket_handle.domain = domain;
911 socket_handle.type = type;
912 socket_handle.protocol = protocol;
913 socket_handle.still_readable = 1;
915 fd = socket (domain, type, protocol);
916 if (fd == -1 && domain == AF_INET && type == SOCK_RAW &&
918 /* Retry with protocol == 4 (see bug #54565) */
919 socket_handle.protocol = 4;
920 fd = socket (AF_INET, SOCK_RAW, 4);
926 g_message ("%s: socket error: %s", __func__, strerror (errno));
928 errnum = errno_to_WSA (errnum, __func__);
929 WSASetLastError (errnum);
931 return(INVALID_SOCKET);
934 if (fd >= _wapi_fd_reserve) {
936 g_message ("%s: File descriptor is too big (%d >= %d)",
937 __func__, fd, _wapi_fd_reserve);
940 WSASetLastError (WSASYSCALLFAILURE);
943 return(INVALID_SOCKET);
946 /* .net seems to set this by default for SOCK_STREAM, not for
947 * SOCK_DGRAM (see bug #36322)
949 * It seems winsock has a rather different idea of what
950 * SO_REUSEADDR means. If it's set, then a new socket can be
951 * bound over an existing listening socket. There's a new
952 * windows-specific option called SO_EXCLUSIVEADDRUSE but
953 * using that means the socket MUST be closed properly, or a
954 * denial of service can occur. Luckily for us, winsock
955 * behaves as though any other system would when SO_REUSEADDR
956 * is true, so we don't need to do anything else here. See
962 ret = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &true,
968 g_message ("%s: Error setting SO_REUSEADDR", __func__);
971 errnum = errno_to_WSA (errnum, __func__);
972 WSASetLastError (errnum);
976 return(INVALID_SOCKET);
981 mono_once (&socket_ops_once, socket_ops_init);
983 handle = _wapi_handle_new_fd (WAPI_HANDLE_SOCKET, fd, &socket_handle);
984 if (handle == _WAPI_HANDLE_INVALID) {
985 g_warning ("%s: error creating socket handle", __func__);
986 WSASetLastError (WSASYSCALLFAILURE);
988 return(INVALID_SOCKET);
992 g_message ("%s: returning socket handle %p", __func__, handle);
998 struct hostent *_wapi_gethostbyname(const char *hostname)
1002 if (startup_count == 0) {
1003 WSASetLastError (WSANOTINITIALISED);
1007 he = gethostbyname (hostname);
1010 g_message ("%s: gethostbyname error: %s", __func__,
1011 strerror (h_errno));
1015 case HOST_NOT_FOUND:
1016 WSASetLastError (WSAHOST_NOT_FOUND);
1018 #if NO_ADDRESS != NO_DATA
1022 WSASetLastError (WSANO_DATA);
1025 WSASetLastError (WSANO_RECOVERY);
1028 WSASetLastError (WSATRY_AGAIN);
1031 g_warning ("%s: Need to translate %d into winsock error", __func__, h_errno);
1039 static gboolean socket_disconnect (guint32 fd)
1041 struct _WapiHandle_socket *socket_handle;
1043 gpointer handle = GUINT_TO_POINTER (fd);
1046 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
1047 (gpointer *)&socket_handle);
1049 g_warning ("%s: error looking up socket handle %p", __func__,
1051 WSASetLastError (WSAENOTSOCK);
1055 newsock = socket (socket_handle->domain, socket_handle->type,
1056 socket_handle->protocol);
1057 if (newsock == -1) {
1058 gint errnum = errno;
1061 g_message ("%s: socket error: %s", __func__, strerror (errno));
1064 errnum = errno_to_WSA (errnum, __func__);
1065 WSASetLastError (errnum);
1070 /* According to Stevens "Advanced Programming in the UNIX
1071 * Environment: UNIX File I/O" dup2() is atomic so there
1072 * should not be a race condition between the old fd being
1073 * closed and the new socket fd being copied over
1076 ret = dup2 (newsock, fd);
1077 } while (ret == -1 && errno == EAGAIN);
1080 gint errnum = errno;
1083 g_message ("%s: dup2 error: %s", __func__, strerror (errno));
1086 errnum = errno_to_WSA (errnum, __func__);
1087 WSASetLastError (errnum);
1097 static gboolean wapi_disconnectex (guint32 fd, WapiOverlapped *overlapped,
1098 guint32 flags, guint32 reserved)
1101 g_message ("%s: called on socket %d!", __func__, fd);
1104 if (reserved != 0) {
1105 WSASetLastError (WSAEINVAL);
1109 /* We could check the socket type here and fail unless its
1110 * SOCK_STREAM, SOCK_SEQPACKET or SOCK_RDM (according to msdn)
1111 * if we really wanted to
1114 return(socket_disconnect (fd));
1117 /* NB only supports NULL file handle, NULL buffers and
1118 * TF_DISCONNECT|TF_REUSE_SOCKET flags to disconnect the socket fd.
1119 * Shouldn't actually ever need to be called anyway though, because we
1120 * have DisconnectEx ().
1122 static gboolean wapi_transmitfile (guint32 fd, gpointer file,
1123 guint32 num_write, guint32 num_per_send,
1124 WapiOverlapped *overlapped,
1125 WapiTransmitFileBuffers *buffers,
1126 WapiTransmitFileFlags flags)
1129 g_message ("%s: called on socket %d!", __func__, fd);
1132 g_assert (file == NULL);
1133 g_assert (overlapped == NULL);
1134 g_assert (buffers == NULL);
1135 g_assert (num_write == 0);
1136 g_assert (num_per_send == 0);
1137 g_assert (flags == (TF_DISCONNECT | TF_REUSE_SOCKET));
1139 return(socket_disconnect (fd));
1146 } extension_functions[] = {
1147 {WSAID_DISCONNECTEX, wapi_disconnectex},
1148 {WSAID_TRANSMITFILE, wapi_transmitfile},
1153 WSAIoctl (guint32 fd, gint32 command,
1154 gchar *input, gint i_len,
1155 gchar *output, gint o_len, glong *written,
1156 void *unused1, void *unused2)
1158 gpointer handle = GUINT_TO_POINTER (fd);
1160 gchar *buffer = NULL;
1162 if (startup_count == 0) {
1163 WSASetLastError (WSANOTINITIALISED);
1164 return(SOCKET_ERROR);
1167 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1168 WSASetLastError (WSAENOTSOCK);
1169 return SOCKET_ERROR;
1172 if (command == SIO_GET_EXTENSION_FUNCTION_POINTER) {
1174 WapiGuid *guid = (WapiGuid *)input;
1176 if (i_len < sizeof(WapiGuid)) {
1177 /* As far as I can tell, windows doesn't
1178 * actually set an error here...
1180 WSASetLastError (WSAEINVAL);
1181 return(SOCKET_ERROR);
1184 if (o_len < sizeof(gpointer)) {
1186 WSASetLastError (WSAEINVAL);
1187 return(SOCKET_ERROR);
1190 if (output == NULL) {
1192 WSASetLastError (WSAEINVAL);
1193 return(SOCKET_ERROR);
1196 while(extension_functions[i].func != NULL) {
1197 if (!memcmp (guid, &extension_functions[i].guid,
1198 sizeof(WapiGuid))) {
1199 memcpy (output, &extension_functions[i].func,
1201 *written = sizeof(gpointer);
1208 WSASetLastError (WSAEINVAL);
1209 return(SOCKET_ERROR);
1213 buffer = g_memdup (input, i_len);
1216 ret = ioctl (fd, command, buffer);
1218 gint errnum = errno;
1220 g_message("%s: WSAIoctl error: %s", __func__,
1224 errnum = errno_to_WSA (errnum, __func__);
1225 WSASetLastError (errnum);
1228 return(SOCKET_ERROR);
1231 if (buffer == NULL) {
1234 /* We just copy the buffer to the output. Some ioctls
1235 * don't even output any data, but, well...
1237 * NB windows returns WSAEFAULT if o_len is too small
1239 i_len = (i_len > o_len) ? o_len : i_len;
1241 if (i_len > 0 && output != NULL) {
1242 memcpy (output, buffer, i_len);
1252 int ioctlsocket(guint32 fd, gint32 command, gpointer arg)
1254 gpointer handle = GUINT_TO_POINTER (fd);
1257 if (startup_count == 0) {
1258 WSASetLastError (WSANOTINITIALISED);
1259 return(SOCKET_ERROR);
1262 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1263 WSASetLastError (WSAENOTSOCK);
1264 return(SOCKET_ERROR);
1270 /* This works better than ioctl(...FIONBIO...)
1271 * on Linux (it causes connect to return
1272 * EINPROGRESS, but the ioctl doesn't seem to)
1274 ret = fcntl(fd, F_GETFL, 0);
1276 if (*(gboolean *)arg) {
1281 ret = fcntl(fd, F_SETFL, ret);
1284 #endif /* O_NONBLOCK */
1287 ret = ioctl (fd, command, arg);
1290 WSASetLastError (WSAEINVAL);
1291 return(SOCKET_ERROR);
1295 gint errnum = errno;
1297 g_message ("%s: ioctl error: %s", __func__, strerror (errno));
1300 errnum = errno_to_WSA (errnum, __func__);
1301 WSASetLastError (errnum);
1303 return(SOCKET_ERROR);
1309 int _wapi_select(int nfds G_GNUC_UNUSED, fd_set *readfds, fd_set *writefds,
1310 fd_set *exceptfds, struct timeval *timeout)
1314 if (startup_count == 0) {
1315 WSASetLastError (WSANOTINITIALISED);
1316 return(SOCKET_ERROR);
1319 for (maxfd = FD_SETSIZE-1; maxfd >= 0; maxfd--) {
1320 if ((readfds && FD_ISSET (maxfd, readfds)) ||
1321 (writefds && FD_ISSET (maxfd, writefds)) ||
1322 (exceptfds && FD_ISSET (maxfd, exceptfds))) {
1328 WSASetLastError (WSAEINVAL);
1329 return(SOCKET_ERROR);
1333 ret = select(maxfd + 1, readfds, writefds, exceptfds,
1335 } while (ret == -1 && errno == EINTR &&
1336 !_wapi_thread_cur_apc_pending ());
1339 gint errnum = errno;
1341 g_message ("%s: select error: %s", __func__, strerror (errno));
1343 errnum = errno_to_WSA (errnum, __func__);
1344 WSASetLastError (errnum);
1346 return(SOCKET_ERROR);
1352 void _wapi_FD_CLR(guint32 fd, fd_set *set)
1354 gpointer handle = GUINT_TO_POINTER (fd);
1356 if (fd >= FD_SETSIZE) {
1357 WSASetLastError (WSAEINVAL);
1361 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1362 WSASetLastError (WSAENOTSOCK);
1369 int _wapi_FD_ISSET(guint32 fd, fd_set *set)
1371 gpointer handle = GUINT_TO_POINTER (fd);
1373 if (fd >= FD_SETSIZE) {
1374 WSASetLastError (WSAEINVAL);
1378 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1379 WSASetLastError (WSAENOTSOCK);
1383 return(FD_ISSET (fd, set));
1386 void _wapi_FD_SET(guint32 fd, fd_set *set)
1388 gpointer handle = GUINT_TO_POINTER (fd);
1390 if (fd >= FD_SETSIZE) {
1391 WSASetLastError (WSAEINVAL);
1395 if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
1396 WSASetLastError (WSAENOTSOCK);
1404 wsabuf_to_msghdr (WapiWSABuf *buffers, guint32 count, struct msghdr *hdr)
1408 memset (hdr, 0, sizeof (struct msghdr));
1409 hdr->msg_iovlen = count;
1410 hdr->msg_iov = g_new0 (struct iovec, count);
1411 for (i = 0; i < count; i++) {
1412 hdr->msg_iov [i].iov_base = buffers [i].buf;
1413 hdr->msg_iov [i].iov_len = buffers [i].len;
1418 msghdr_iov_free (struct msghdr *hdr)
1420 g_free (hdr->msg_iov);
1423 int WSARecv (guint32 fd, WapiWSABuf *buffers, guint32 count, guint32 *received,
1424 guint32 *flags, WapiOverlapped *overlapped,
1425 WapiOverlappedCB *complete)
1430 g_assert (overlapped == NULL);
1431 g_assert (complete == NULL);
1433 wsabuf_to_msghdr (buffers, count, &hdr);
1434 ret = _wapi_recvmsg (fd, &hdr, *flags);
1435 msghdr_iov_free (&hdr);
1437 if(ret == SOCKET_ERROR) {
1442 *flags = hdr.msg_flags;
1447 int WSASend (guint32 fd, WapiWSABuf *buffers, guint32 count, guint32 *sent,
1448 guint32 flags, WapiOverlapped *overlapped,
1449 WapiOverlappedCB *complete)
1454 g_assert (overlapped == NULL);
1455 g_assert (complete == NULL);
1457 wsabuf_to_msghdr (buffers, count, &hdr);
1458 ret = _wapi_sendmsg (fd, &hdr, flags);
1459 msghdr_iov_free (&hdr);
1461 if(ret == SOCKET_ERROR)