2 * socket-io.c: Socket IO internal calls
5 * Dick Porter (dick@ximian.com)
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11 * This file has been re-licensed under the MIT License:
12 * http://opensource.org/licenses/MIT
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
18 #ifndef DISABLE_SOCKETS
20 #if defined(__APPLE__) || defined(__FreeBSD__)
21 #define __APPLE_USE_RFC_3542
30 #include <sys/socket.h>
31 #ifdef HAVE_SYS_IOCTL_H
32 #include <sys/ioctl.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
39 #include <arpa/inet.h>
46 #include <sys/types.h>
48 #include <mono/metadata/object.h>
49 #include <mono/io-layer/io-layer.h>
50 #include <mono/metadata/socket-io.h>
51 #include <mono/metadata/exception.h>
52 #include <mono/metadata/assembly.h>
53 #include <mono/metadata/appdomain.h>
54 #include <mono/metadata/file-io.h>
55 #include <mono/metadata/threads.h>
56 #include <mono/metadata/threads-types.h>
57 #include <mono/metadata/threadpool-ms-io.h>
58 #include <mono/utils/mono-poll.h>
59 /* FIXME change this code to not mess so much with the internals */
60 #include <mono/metadata/class-internals.h>
61 #include <mono/metadata/domain-internals.h>
62 #include <mono/utils/mono-threads.h>
63 #include <mono/utils/mono-memory-model.h>
64 #include <mono/utils/networking.h>
67 #ifdef HAVE_SYS_TIME_H
70 #ifdef HAVE_SYS_IOCTL_H
71 #include <sys/ioctl.h>
80 #ifdef HAVE_SYS_FILIO_H
81 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
83 #ifdef HAVE_SYS_SOCKIO_H
84 #include <sys/sockio.h> /* defines SIOCATMARK */
90 #ifdef HAVE_GETIFADDRS
91 // <net/if.h> must be included before <ifaddrs.h>
95 #include "mono/io-layer/socket-wrappers.h"
98 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
101 abort_syscall (gpointer data)
103 mono_thread_info_abort_socket_syscall_for_close ((MonoNativeThreadId) (gsize) data);
107 convert_family (MonoAddressFamily mono_family)
109 switch (mono_family) {
110 case AddressFamily_Unknown:
111 case AddressFamily_ImpLink:
112 case AddressFamily_Pup:
113 case AddressFamily_Chaos:
114 case AddressFamily_Iso:
115 case AddressFamily_Ecma:
116 case AddressFamily_DataKit:
117 case AddressFamily_Ccitt:
118 case AddressFamily_DataLink:
119 case AddressFamily_Lat:
120 case AddressFamily_HyperChannel:
121 case AddressFamily_NetBios:
122 case AddressFamily_VoiceView:
123 case AddressFamily_FireFox:
124 case AddressFamily_Banyan:
125 case AddressFamily_Atm:
126 case AddressFamily_Cluster:
127 case AddressFamily_Ieee12844:
128 case AddressFamily_NetworkDesigners:
129 g_warning ("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
131 case AddressFamily_Unspecified:
133 case AddressFamily_Unix:
135 case AddressFamily_InterNetwork:
137 case AddressFamily_AppleTalk:
139 case AddressFamily_InterNetworkV6:
141 case AddressFamily_DecNet:
147 case AddressFamily_Ipx:
153 case AddressFamily_Sna:
159 case AddressFamily_Irda:
166 g_warning ("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
171 static MonoAddressFamily
172 convert_to_mono_family (guint16 af_family)
176 return AddressFamily_Unspecified;
178 return AddressFamily_Unix;
180 return AddressFamily_InterNetwork;
183 return AddressFamily_Ipx;
187 return AddressFamily_Sna;
191 return AddressFamily_DecNet;
194 return AddressFamily_AppleTalk;
196 return AddressFamily_InterNetworkV6;
199 return AddressFamily_Irda;
202 g_warning ("unknown address family 0x%x", af_family);
203 return AddressFamily_Unknown;
208 convert_type (MonoSocketType mono_type)
211 case SocketType_Stream:
213 case SocketType_Dgram:
223 case SocketType_Seqpacket:
224 return SOCK_SEQPACKET;
225 case SocketType_Unknown:
226 g_warning ("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
229 g_warning ("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
235 convert_proto (MonoProtocolType mono_proto)
237 switch (mono_proto) {
238 case ProtocolType_IP:
239 case ProtocolType_IPv6:
240 case ProtocolType_Icmp:
241 case ProtocolType_Igmp:
242 case ProtocolType_Ggp:
243 case ProtocolType_Tcp:
244 case ProtocolType_Pup:
245 case ProtocolType_Udp:
246 case ProtocolType_Idp:
247 /* These protocols are known (on my system at least) */
249 case ProtocolType_ND:
250 case ProtocolType_Raw:
251 case ProtocolType_Ipx:
252 case ProtocolType_Spx:
253 case ProtocolType_SpxII:
254 case ProtocolType_Unknown:
255 /* These protocols arent */
256 g_warning ("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
263 /* Convert MonoSocketFlags */
265 convert_socketflags (gint32 sflags)
270 /* SocketFlags.None */
273 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
274 SocketFlags_DontRoute | SocketFlags_Partial))
275 /* Contains invalid flag values */
278 if (sflags & SocketFlags_OutOfBand)
280 if (sflags & SocketFlags_Peek)
282 if (sflags & SocketFlags_DontRoute)
283 flags |= MSG_DONTROUTE;
285 /* Ignore Partial - see bug 349688. Don't return -1, because
286 * according to the comment in that bug ms runtime doesn't for
287 * UDP sockets (this means we will silently ignore it for TCP
291 if (sflags & SocketFlags_Partial)
295 /* Don't do anything for MaxIOVectorLength */
296 if (sflags & SocketFlags_MaxIOVectorLength)
304 * 0 on success (mapped mono_level and mono_name to system_level and system_name
306 * -2 on non-fatal error (ie, must ignore)
309 convert_sockopt_level_and_name (MonoSocketOptionLevel mono_level, MonoSocketOptionName mono_name, int *system_level, int *system_name)
311 switch (mono_level) {
312 case SocketOptionLevel_Socket:
313 *system_level = SOL_SOCKET;
316 case SocketOptionName_DontLinger:
317 /* This is SO_LINGER, because the setsockopt
318 * internal call maps DontLinger to SO_LINGER
321 *system_name = SO_LINGER;
323 case SocketOptionName_Debug:
324 *system_name = SO_DEBUG;
327 case SocketOptionName_AcceptConnection:
328 *system_name = SO_ACCEPTCONN;
331 case SocketOptionName_ReuseAddress:
332 *system_name = SO_REUSEADDR;
334 case SocketOptionName_KeepAlive:
335 *system_name = SO_KEEPALIVE;
337 case SocketOptionName_DontRoute:
338 *system_name = SO_DONTROUTE;
340 case SocketOptionName_Broadcast:
341 *system_name = SO_BROADCAST;
343 case SocketOptionName_Linger:
344 *system_name = SO_LINGER;
346 case SocketOptionName_OutOfBandInline:
347 *system_name = SO_OOBINLINE;
349 case SocketOptionName_SendBuffer:
350 *system_name = SO_SNDBUF;
352 case SocketOptionName_ReceiveBuffer:
353 *system_name = SO_RCVBUF;
355 case SocketOptionName_SendLowWater:
356 *system_name = SO_SNDLOWAT;
358 case SocketOptionName_ReceiveLowWater:
359 *system_name = SO_RCVLOWAT;
361 case SocketOptionName_SendTimeout:
362 *system_name = SO_SNDTIMEO;
364 case SocketOptionName_ReceiveTimeout:
365 *system_name = SO_RCVTIMEO;
367 case SocketOptionName_Error:
368 *system_name = SO_ERROR;
370 case SocketOptionName_Type:
371 *system_name = SO_TYPE;
374 case SocketOptionName_PeerCred:
375 *system_name = SO_PEERCRED;
378 case SocketOptionName_ExclusiveAddressUse:
379 #ifdef SO_EXCLUSIVEADDRUSE
380 *system_name = SO_EXCLUSIVEADDRUSE;
383 case SocketOptionName_UseLoopback:
384 #ifdef SO_USELOOPBACK
385 *system_name = SO_USELOOPBACK;
388 case SocketOptionName_MaxConnections:
390 *system_name = SO_MAXCONN;
392 #elif defined(SOMAXCONN)
393 *system_name = SOMAXCONN;
397 g_warning ("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
402 case SocketOptionLevel_IP:
403 *system_level = mono_networking_get_ip_protocol ();
406 case SocketOptionName_IPOptions:
407 *system_name = IP_OPTIONS;
410 case SocketOptionName_HeaderIncluded:
411 *system_name = IP_HDRINCL;
415 case SocketOptionName_TypeOfService:
416 *system_name = IP_TOS;
420 case SocketOptionName_IpTimeToLive:
421 *system_name = IP_TTL;
424 case SocketOptionName_MulticastInterface:
425 *system_name = IP_MULTICAST_IF;
427 case SocketOptionName_MulticastTimeToLive:
428 *system_name = IP_MULTICAST_TTL;
430 case SocketOptionName_MulticastLoopback:
431 *system_name = IP_MULTICAST_LOOP;
433 case SocketOptionName_AddMembership:
434 *system_name = IP_ADD_MEMBERSHIP;
436 case SocketOptionName_DropMembership:
437 *system_name = IP_DROP_MEMBERSHIP;
439 #ifdef HAVE_IP_PKTINFO
440 case SocketOptionName_PacketInformation:
441 *system_name = IP_PKTINFO;
443 #endif /* HAVE_IP_PKTINFO */
445 case SocketOptionName_DontFragment:
446 #ifdef HAVE_IP_DONTFRAGMENT
447 *system_name = IP_DONTFRAGMENT;
449 #elif defined HAVE_IP_MTU_DISCOVER
450 /* Not quite the same */
451 *system_name = IP_MTU_DISCOVER;
454 /* If the flag is not available on this system, we can ignore this error */
456 #endif /* HAVE_IP_DONTFRAGMENT */
457 case SocketOptionName_AddSourceMembership:
458 case SocketOptionName_DropSourceMembership:
459 case SocketOptionName_BlockSource:
460 case SocketOptionName_UnblockSource:
461 /* Can't figure out how to map these, so fall
465 g_warning ("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
470 case SocketOptionLevel_IPv6:
471 *system_level = mono_networking_get_ipv6_protocol ();
474 case SocketOptionName_IpTimeToLive:
475 case SocketOptionName_HopLimit:
476 *system_name = IPV6_UNICAST_HOPS;
478 case SocketOptionName_MulticastInterface:
479 *system_name = IPV6_MULTICAST_IF;
481 case SocketOptionName_MulticastTimeToLive:
482 *system_name = IPV6_MULTICAST_HOPS;
484 case SocketOptionName_MulticastLoopback:
485 *system_name = IPV6_MULTICAST_LOOP;
487 case SocketOptionName_AddMembership:
488 *system_name = IPV6_JOIN_GROUP;
490 case SocketOptionName_DropMembership:
491 *system_name = IPV6_LEAVE_GROUP;
493 case SocketOptionName_IPv6Only:
495 *system_name = IPV6_V6ONLY;
500 case SocketOptionName_PacketInformation:
501 #ifdef HAVE_IPV6_PKTINFO
502 *system_name = IPV6_PKTINFO;
505 case SocketOptionName_HeaderIncluded:
506 case SocketOptionName_IPOptions:
507 case SocketOptionName_TypeOfService:
508 case SocketOptionName_DontFragment:
509 case SocketOptionName_AddSourceMembership:
510 case SocketOptionName_DropSourceMembership:
511 case SocketOptionName_BlockSource:
512 case SocketOptionName_UnblockSource:
513 /* Can't figure out how to map these, so fall
517 g_warning ("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
520 break; /* SocketOptionLevel_IPv6 */
522 case SocketOptionLevel_Tcp:
523 *system_level = mono_networking_get_tcp_protocol ();
526 case SocketOptionName_NoDelay:
527 *system_name = TCP_NODELAY;
530 /* The documentation is talking complete
531 * bollocks here: rfc-1222 is titled
532 * 'Advancing the NSFNET Routing Architecture'
533 * and doesn't mention either of the words
534 * "expedite" or "urgent".
536 case SocketOptionName_BsdUrgent:
537 case SocketOptionName_Expedited:
540 g_warning ("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
545 case SocketOptionLevel_Udp:
546 g_warning ("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
549 case SocketOptionName_NoChecksum:
550 case SocketOptionName_ChecksumCoverage:
552 g_warning ("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
559 g_warning ("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
567 get_socket_assembly (void)
569 MonoDomain *domain = mono_domain_get ();
571 if (domain->socket_assembly == NULL) {
572 MonoImage *socket_assembly;
574 socket_assembly = mono_image_loaded ("System");
575 if (!socket_assembly) {
576 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
579 g_assert_not_reached ();
581 socket_assembly = mono_assembly_get_image (sa);
584 mono_atomic_store_release (&domain->socket_assembly, socket_assembly);
587 return domain->socket_assembly;
591 get_family_hint (MonoError *error)
593 MonoDomain *domain = mono_domain_get ();
595 mono_error_init (error);
597 if (!domain->inet_family_hint) {
598 MonoImage *socket_assembly;
599 MonoClass *socket_class;
600 MonoClassField *ipv6_field, *ipv4_field;
601 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
604 socket_assembly = get_socket_assembly ();
605 g_assert (socket_assembly);
607 socket_class = mono_class_load_from_name (socket_assembly, "System.Net.Sockets", "Socket");
609 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4_supported");
610 g_assert (ipv4_field);
612 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6_supported");
613 g_assert (ipv6_field);
615 vtable = mono_class_vtable (mono_domain_get (), socket_class);
618 mono_runtime_class_init_full (vtable, error);
619 return_val_if_nok (error, -1);
621 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
622 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
624 mono_domain_lock (domain);
625 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
626 domain->inet_family_hint = 1;
627 } else if (ipv4_enabled == 1) {
628 domain->inet_family_hint = 2;
630 domain->inet_family_hint = 3;
632 mono_domain_unlock (domain);
634 switch (domain->inet_family_hint) {
635 case 1: return PF_UNSPEC;
636 case 2: return PF_INET;
637 case 3: return PF_INET6;
644 ves_icall_System_Net_Sockets_Socket_Socket_internal (MonoObject *this_obj, gint32 family, gint32 type, gint32 proto, gint32 *werror)
653 sock_family = convert_family ((MonoAddressFamily)family);
654 if (sock_family == -1) {
655 *werror = WSAEAFNOSUPPORT;
659 sock_proto = convert_proto ((MonoProtocolType)proto);
660 if (sock_proto == -1) {
661 *werror = WSAEPROTONOSUPPORT;
665 sock_type = convert_type ((MonoSocketType)type);
666 if (sock_type == -1) {
667 *werror = WSAESOCKTNOSUPPORT;
671 sock = _wapi_socket (sock_family, sock_type, sock_proto,
672 NULL, 0, WSA_FLAG_OVERLAPPED);
674 if (sock == INVALID_SOCKET) {
675 *werror = WSAGetLastError ();
679 return GUINT_TO_POINTER (sock);
682 /* FIXME: the SOCKET parameter (here and in other functions in this
683 * file) is really an IntPtr which needs to be converted to a guint32.
686 ves_icall_System_Net_Sockets_Socket_Close_internal (SOCKET sock, gint32 *werror)
688 LOGDEBUG (g_message ("%s: closing 0x%x", __func__, sock));
692 /* Clear any pending work item from this socket if the underlying
693 * polling system does not notify when the socket is closed */
694 mono_threadpool_ms_io_remove_socket (GPOINTER_TO_INT (sock));
696 MONO_PREPARE_BLOCKING;
698 MONO_FINISH_BLOCKING;
702 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal (void)
704 LOGDEBUG (g_message("%s: returning %d", __func__, WSAGetLastError ()));
706 return WSAGetLastError ();
710 ves_icall_System_Net_Sockets_Socket_Available_internal (SOCKET sock, gint32 *werror)
717 /* FIXME: this might require amount to be unsigned long. */
718 ret = ioctlsocket (sock, FIONREAD, &amount);
719 if (ret == SOCKET_ERROR) {
720 *werror = WSAGetLastError ();
728 ves_icall_System_Net_Sockets_Socket_Blocking_internal (SOCKET sock, gboolean block, gint32 *werror)
735 * block == TRUE/FALSE means we will block/not block.
736 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
740 ret = ioctlsocket (sock, FIONBIO, (gulong *)&block);
741 if (ret == SOCKET_ERROR)
742 *werror = WSAGetLastError ();
746 ves_icall_System_Net_Sockets_Socket_Accept_internal (SOCKET sock, gint32 *werror, gboolean blocking)
748 gboolean interrupted;
753 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
759 MONO_PREPARE_BLOCKING;
763 MonoInternalThread *curthread = mono_thread_internal_current ();
764 curthread->interrupt_on_stop = (gpointer)TRUE;
765 newsock = _wapi_accept (sock, NULL, 0);
766 curthread->interrupt_on_stop = (gpointer)FALSE;
769 newsock = _wapi_accept (sock, NULL, 0);
772 MONO_FINISH_BLOCKING;
774 mono_thread_info_uninstall_interrupt (&interrupted);
780 if (newsock == INVALID_SOCKET) {
781 *werror = WSAGetLastError ();
785 return GUINT_TO_POINTER (newsock);
789 ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock, guint32 backlog, gint32 *werror)
795 MONO_PREPARE_BLOCKING;
797 ret = _wapi_listen (sock, backlog);
799 MONO_FINISH_BLOCKING;
801 if (ret == SOCKET_ERROR)
802 *werror = WSAGetLastError ();
805 // Check whether it's ::ffff::0:0.
807 is_ipv4_mapped_any (const struct in6_addr *addr)
811 for (i = 0; i < 10; i++) {
812 if (addr->s6_addr [i])
815 if ((addr->s6_addr [10] != 0xff) || (addr->s6_addr [11] != 0xff))
817 for (i = 12; i < 16; i++) {
818 if (addr->s6_addr [i])
825 create_object_from_sockaddr (struct sockaddr *saddr, int sa_size, gint32 *werror, MonoError *error)
827 MonoDomain *domain = mono_domain_get ();
828 MonoObject *sockaddr_obj;
830 MonoAddressFamily family;
832 mono_error_init (error);
834 /* Build a System.Net.SocketAddress object instance */
835 if (!domain->sockaddr_class)
836 domain->sockaddr_class = mono_class_load_from_name (get_socket_assembly (), "System.Net", "SocketAddress");
837 sockaddr_obj = mono_object_new_checked (domain, domain->sockaddr_class, error);
838 return_val_if_nok (error, NULL);
840 /* Locate the SocketAddress data buffer in the object */
841 if (!domain->sockaddr_data_field) {
842 domain->sockaddr_data_field = mono_class_get_field_from_name (domain->sockaddr_class, "data");
843 g_assert (domain->sockaddr_data_field);
846 /* May be the +2 here is too conservative, as sa_len returns
847 * the length of the entire sockaddr_in/in6, including
848 * sizeof (unsigned short) of the family */
849 /* We can't really avoid the +2 as all code below depends on this size - INCLUDING unix domain sockets.*/
850 data = mono_array_new_cached (domain, mono_get_byte_class (), sa_size + 2, error);
851 return_val_if_nok (error, NULL);
853 /* The data buffer is laid out as follows:
854 * bytes 0 and 1 are the address family
855 * bytes 2 and 3 are the port info
856 * the rest is the address info
859 family = convert_to_mono_family (saddr->sa_family);
860 if (family == AddressFamily_Unknown) {
861 *werror = WSAEAFNOSUPPORT;
865 mono_array_set (data, guint8, 0, family & 0x0FF);
866 mono_array_set (data, guint8, 1, (family >> 8) & 0x0FF);
868 if (saddr->sa_family == AF_INET) {
869 struct sockaddr_in *sa_in = (struct sockaddr_in *)saddr;
870 guint16 port = ntohs (sa_in->sin_port);
871 guint32 address = ntohl (sa_in->sin_addr.s_addr);
874 mono_error_set_exception_instance (error, mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
878 mono_array_set (data, guint8, 2, (port>>8) & 0xff);
879 mono_array_set (data, guint8, 3, (port) & 0xff);
880 mono_array_set (data, guint8, 4, (address>>24) & 0xff);
881 mono_array_set (data, guint8, 5, (address>>16) & 0xff);
882 mono_array_set (data, guint8, 6, (address>>8) & 0xff);
883 mono_array_set (data, guint8, 7, (address) & 0xff);
885 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
888 } else if (saddr->sa_family == AF_INET6) {
889 struct sockaddr_in6 *sa_in = (struct sockaddr_in6 *)saddr;
892 guint16 port = ntohs (sa_in->sin6_port);
895 mono_error_set_exception_instance (error, mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
899 mono_array_set (data, guint8, 2, (port>>8) & 0xff);
900 mono_array_set (data, guint8, 3, (port) & 0xff);
902 if (is_ipv4_mapped_any (&sa_in->sin6_addr)) {
903 // Map ::ffff:0:0 to :: (bug #5502)
904 for (i = 0; i < 16; i++)
905 mono_array_set (data, guint8, 8 + i, 0);
907 for (i = 0; i < 16; i++) {
908 mono_array_set (data, guint8, 8 + i,
909 sa_in->sin6_addr.s6_addr [i]);
913 mono_array_set (data, guint8, 24, sa_in->sin6_scope_id & 0xff);
914 mono_array_set (data, guint8, 25,
915 (sa_in->sin6_scope_id >> 8) & 0xff);
916 mono_array_set (data, guint8, 26,
917 (sa_in->sin6_scope_id >> 16) & 0xff);
918 mono_array_set (data, guint8, 27,
919 (sa_in->sin6_scope_id >> 24) & 0xff);
921 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
926 else if (saddr->sa_family == AF_UNIX) {
929 for (i = 0; i < sa_size; i++)
930 mono_array_set (data, guint8, i + 2, saddr->sa_data [i]);
932 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
938 *werror = WSAEAFNOSUPPORT;
944 get_sockaddr_size (int family)
949 if (family == AF_INET) {
950 size = sizeof (struct sockaddr_in);
951 } else if (family == AF_INET6) {
952 size = sizeof (struct sockaddr_in6);
955 else if (family == AF_UNIX) {
956 size = sizeof (struct sockaddr_un);
963 ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal (SOCKET sock, gint32 af, gint32 *werror)
973 salen = get_sockaddr_size (convert_family ((MonoAddressFamily)af));
975 *werror = WSAEAFNOSUPPORT;
978 sa = (salen <= 128) ? (gchar *)alloca (salen) : (gchar *)g_malloc0 (salen);
980 MONO_PREPARE_BLOCKING;
982 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
984 MONO_FINISH_BLOCKING;
986 if (ret == SOCKET_ERROR) {
987 *werror = WSAGetLastError ();
993 LOGDEBUG (g_message("%s: bound to %s port %d", __func__, inet_ntoa (((struct sockaddr_in *)&sa)->sin_addr), ntohs (((struct sockaddr_in *)&sa)->sin_port)));
995 result = create_object_from_sockaddr ((struct sockaddr *)sa, salen, werror, &error);
998 if (!mono_error_ok (&error))
999 mono_error_set_pending_exception (&error);
1004 ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal (SOCKET sock, gint32 af, gint32 *werror)
1014 salen = get_sockaddr_size (convert_family ((MonoAddressFamily)af));
1016 *werror = WSAEAFNOSUPPORT;
1019 sa = (salen <= 128) ? (gchar *)alloca (salen) : (gchar *)g_malloc0 (salen);
1020 /* Note: linux returns just 2 for AF_UNIX. Always. */
1022 MONO_PREPARE_BLOCKING;
1024 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1026 MONO_FINISH_BLOCKING;
1028 if (ret == SOCKET_ERROR) {
1029 *werror = WSAGetLastError ();
1035 LOGDEBUG (g_message("%s: connected to %s port %d", __func__, inet_ntoa (((struct sockaddr_in *)&sa)->sin_addr), ntohs (((struct sockaddr_in *)&sa)->sin_port)));
1037 result = create_object_from_sockaddr ((struct sockaddr *)sa, salen, werror, &error);
1040 if (!mono_error_ok (&error))
1041 mono_error_set_pending_exception (&error);
1045 static struct sockaddr*
1046 create_sockaddr_from_object (MonoObject *saddr_obj, socklen_t *sa_size, gint32 *werror, MonoError *error)
1048 MonoClassField *field;
1053 mono_error_init (error);
1055 /* Dig the SocketAddress data buffer out of the object */
1056 field = mono_class_get_field_from_name (saddr_obj->vtable->klass, "data");
1057 data = *(MonoArray **)(((char *)saddr_obj) + field->offset);
1059 /* The data buffer is laid out as follows:
1060 * byte 0 is the address family low byte
1061 * byte 1 is the address family high byte
1063 * bytes 2 and 3 are the port info
1064 * the rest is the address info
1066 * the rest is the file name
1068 len = mono_array_length (data);
1070 mono_error_set_exception_instance (error, mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1074 family = convert_family ((MonoAddressFamily)(mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8)));
1075 if (family == AF_INET) {
1076 struct sockaddr_in *sa;
1081 mono_error_set_exception_instance (error, mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1085 sa = g_new0 (struct sockaddr_in, 1);
1086 port = (mono_array_get (data, guint8, 2) << 8) +
1087 mono_array_get (data, guint8, 3);
1088 address = (mono_array_get (data, guint8, 4) << 24) +
1089 (mono_array_get (data, guint8, 5) << 16 ) +
1090 (mono_array_get (data, guint8, 6) << 8) +
1091 mono_array_get (data, guint8, 7);
1093 sa->sin_family = family;
1094 sa->sin_addr.s_addr = htonl (address);
1095 sa->sin_port = htons (port);
1097 *sa_size = sizeof (struct sockaddr_in);
1098 return (struct sockaddr *)sa;
1099 } else if (family == AF_INET6) {
1100 struct sockaddr_in6 *sa;
1106 mono_error_set_exception_instance (error, mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1110 sa = g_new0 (struct sockaddr_in6, 1);
1111 port = mono_array_get (data, guint8, 3) +
1112 (mono_array_get (data, guint8, 2) << 8);
1113 scopeid = mono_array_get (data, guint8, 24) +
1114 (mono_array_get (data, guint8, 25) << 8) +
1115 (mono_array_get (data, guint8, 26) << 16) +
1116 (mono_array_get (data, guint8, 27) << 24);
1118 sa->sin6_family = family;
1119 sa->sin6_port = htons (port);
1120 sa->sin6_scope_id = scopeid;
1122 for (i = 0; i < 16; i++)
1123 sa->sin6_addr.s6_addr [i] = mono_array_get (data, guint8, 8 + i);
1125 *sa_size = sizeof (struct sockaddr_in6);
1126 return (struct sockaddr *)sa;
1128 #ifdef HAVE_SYS_UN_H
1129 else if (family == AF_UNIX) {
1130 struct sockaddr_un *sock_un;
1133 /* Need a byte for the '\0' terminator/prefix, and the first
1134 * two bytes hold the SocketAddress family
1136 if (len - 2 >= sizeof (sock_un->sun_path)) {
1137 mono_error_set_exception_instance (error, mono_get_exception_index_out_of_range ());
1141 sock_un = g_new0 (struct sockaddr_un, 1);
1143 sock_un->sun_family = family;
1144 for (i = 0; i < len - 2; i++)
1145 sock_un->sun_path [i] = mono_array_get (data, guint8, i + 2);
1148 return (struct sockaddr *)sock_un;
1152 *werror = WSAEAFNOSUPPORT;
1158 ves_icall_System_Net_Sockets_Socket_Bind_internal (SOCKET sock, MonoObject *sockaddr, gint32 *werror)
1161 struct sockaddr *sa;
1167 sa = create_sockaddr_from_object (sockaddr, &sa_size, werror, &error);
1170 if (!mono_error_ok (&error)) {
1171 mono_error_set_pending_exception (&error);
1175 LOGDEBUG (g_message("%s: binding to %s port %d", __func__, inet_ntoa (((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port)));
1177 ret = _wapi_bind (sock, sa, sa_size);
1179 if (ret == SOCKET_ERROR)
1180 *werror = WSAGetLastError ();
1192 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1193 gint timeout, gint32 *werror)
1195 MonoInternalThread *thread = mono_thread_internal_current ();
1198 gboolean interrupted;
1203 pfds = g_new0 (mono_pollfd, 1);
1204 pfds->fd = GPOINTER_TO_INT (sock);
1207 case SelectModeRead:
1208 pfds->events = MONO_POLLIN;
1210 case SelectModeWrite:
1211 pfds->events = MONO_POLLOUT;
1214 pfds->events = MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL;
1218 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1219 start = time (NULL);
1222 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
1229 MONO_PREPARE_BLOCKING;
1231 ret = mono_poll (pfds, 1, timeout);
1233 MONO_FINISH_BLOCKING;
1235 mono_thread_info_uninstall_interrupt (&interrupted);
1242 if (timeout > 0 && ret < 0) {
1244 int sec = time (NULL) - start;
1246 timeout -= sec * 1000;
1254 if (ret == -1 && errno == EINTR) {
1255 if (mono_thread_test_state (thread, (MonoThreadState)(ThreadState_AbortRequested | ThreadState_StopRequested))) {
1260 /* Suspend requested? */
1261 mono_thread_interruption_checkpoint ();
1265 } while (ret == -1 && errno == EINTR);
1269 *werror = WSAGetLastError ();
1271 *werror = errno_to_WSA (errno, __func__);
1282 ves_icall_System_Net_Sockets_Socket_Connect_internal (SOCKET sock, MonoObject *sockaddr, gint32 *werror)
1285 struct sockaddr *sa;
1288 gboolean interrupted;
1292 sa = create_sockaddr_from_object (sockaddr, &sa_size, werror, &error);
1295 if (!mono_error_ok (&error)) {
1296 mono_error_set_pending_exception (&error);
1300 LOGDEBUG (g_message("%s: connecting to %s port %d", __func__, inet_ntoa (((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port)));
1302 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
1308 MONO_PREPARE_BLOCKING;
1310 ret = _wapi_connect (sock, sa, sa_size);
1312 MONO_FINISH_BLOCKING;
1314 mono_thread_info_uninstall_interrupt (&interrupted);
1320 if (ret == SOCKET_ERROR)
1321 *werror = WSAGetLastError ();
1326 /* These #defines from mswsock.h from wine. Defining them here allows
1327 * us to build this file on a mingw box that doesn't know the magic
1328 * numbers, but still run on a newer windows box that does.
1330 #ifndef WSAID_DISCONNECTEX
1331 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1332 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1335 #ifndef WSAID_TRANSMITFILE
1336 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1337 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1341 ves_icall_System_Net_Sockets_Socket_Disconnect_internal (SOCKET sock, MonoBoolean reuse, gint32 *werror)
1344 glong output_bytes = 0;
1345 GUID disco_guid = WSAID_DISCONNECTEX;
1346 GUID trans_guid = WSAID_TRANSMITFILE;
1347 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1348 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1349 gboolean interrupted;
1353 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1355 MONO_PREPARE_BLOCKING;
1357 /* I _think_ the extension function pointers need to be looked
1358 * up for each socket. FIXME: check the best way to store
1359 * pointers to functions in managed objects that still works
1360 * on 64bit platforms.
1362 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER, (gchar *)&disco_guid, sizeof (GUID),
1363 (gchar *)&_wapi_disconnectex, sizeof (void *), &output_bytes, NULL, NULL);
1365 MONO_FINISH_BLOCKING;
1368 /* make sure that WSAIoctl didn't put crap in the
1371 _wapi_disconnectex = NULL;
1373 MONO_PREPARE_BLOCKING;
1376 * Use the SIO_GET_EXTENSION_FUNCTION_POINTER to
1377 * determine the address of the disconnect method without
1378 * taking a hard dependency on a single provider
1380 * For an explanation of why this is done, you can read
1381 * the article at http://www.codeproject.com/internet/jbsocketserver3.asp
1383 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER, (gchar *)&trans_guid, sizeof(GUID),
1384 (gchar *)&_wapi_transmitfile, sizeof(void *), &output_bytes, NULL, NULL);
1386 MONO_FINISH_BLOCKING;
1389 _wapi_transmitfile = NULL;
1392 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
1398 MONO_PREPARE_BLOCKING;
1400 if (_wapi_disconnectex != NULL) {
1401 if (!_wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0))
1402 *werror = WSAGetLastError ();
1403 } else if (_wapi_transmitfile != NULL) {
1404 if (!_wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL, TF_DISCONNECT | TF_REUSE_SOCKET))
1405 *werror = WSAGetLastError ();
1407 *werror = ERROR_NOT_SUPPORTED;
1410 MONO_FINISH_BLOCKING;
1412 mono_thread_info_uninstall_interrupt (&interrupted);
1418 ves_icall_System_Net_Sockets_Socket_Receive_internal (SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *werror)
1424 gboolean interrupted;
1425 MonoInternalThread* curthread G_GNUC_UNUSED = mono_thread_internal_current ();
1429 alen = mono_array_length (buffer);
1430 if (offset > alen - count)
1433 buf = mono_array_addr (buffer, guchar, offset);
1435 recvflags = convert_socketflags (flags);
1436 if (recvflags == -1) {
1437 *werror = WSAEOPNOTSUPP;
1441 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
1445 MONO_PREPARE_BLOCKING;
1449 curthread->interrupt_on_stop = (gpointer)TRUE;
1450 ret = _wapi_recv (sock, buf, count, recvflags);
1451 curthread->interrupt_on_stop = (gpointer)FALSE;
1454 ret = _wapi_recv (sock, buf, count, recvflags);
1457 MONO_FINISH_BLOCKING;
1459 mono_thread_info_uninstall_interrupt (&interrupted);
1465 if (ret == SOCKET_ERROR) {
1466 *werror = WSAGetLastError ();
1474 ves_icall_System_Net_Sockets_Socket_Receive_array_internal (SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *werror)
1477 gboolean interrupted;
1480 DWORD recvflags = 0;
1484 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1485 count = mono_array_length (buffers);
1487 recvflags = convert_socketflags (flags);
1488 if (recvflags == -1) {
1489 *werror = WSAEOPNOTSUPP;
1493 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
1499 MONO_PREPARE_BLOCKING;
1501 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1503 MONO_FINISH_BLOCKING;
1505 mono_thread_info_uninstall_interrupt (&interrupted);
1511 if (ret == SOCKET_ERROR) {
1512 *werror = WSAGetLastError ();
1520 ves_icall_System_Net_Sockets_Socket_ReceiveFrom_internal (SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *werror)
1527 struct sockaddr *sa;
1529 gboolean interrupted;
1533 alen = mono_array_length (buffer);
1534 if (offset > alen - count)
1537 sa = create_sockaddr_from_object (*sockaddr, &sa_size, werror, &error);
1540 if (!mono_error_ok (&error)) {
1541 mono_error_set_pending_exception (&error);
1545 buf = mono_array_addr (buffer, guchar, offset);
1547 recvflags = convert_socketflags (flags);
1548 if (recvflags == -1) {
1549 *werror = WSAEOPNOTSUPP;
1553 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
1560 MONO_PREPARE_BLOCKING;
1562 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1564 MONO_FINISH_BLOCKING;
1566 mono_thread_info_uninstall_interrupt (&interrupted);
1573 if (ret==SOCKET_ERROR) {
1575 *werror = WSAGetLastError ();
1579 /* If we didn't get a socket size, then we're probably a
1580 * connected connection-oriented socket and the stack hasn't
1581 * returned the remote address. All we can do is return null.
1584 *sockaddr = create_object_from_sockaddr (sa, sa_size, werror, &error);
1585 if (!mono_error_ok (&error)) {
1586 mono_error_set_pending_exception (&error);
1600 ves_icall_System_Net_Sockets_Socket_Send_internal (SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *werror)
1606 gboolean interrupted;
1610 alen = mono_array_length (buffer);
1611 if (offset > alen - count)
1614 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1616 buf = mono_array_addr (buffer, guchar, offset);
1618 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1620 sendflags = convert_socketflags (flags);
1621 if (sendflags == -1) {
1622 *werror = WSAEOPNOTSUPP;
1626 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
1632 MONO_PREPARE_BLOCKING;
1634 ret = _wapi_send (sock, buf, count, sendflags);
1636 MONO_FINISH_BLOCKING;
1638 mono_thread_info_uninstall_interrupt (&interrupted);
1644 if (ret == SOCKET_ERROR) {
1645 *werror = WSAGetLastError ();
1653 ves_icall_System_Net_Sockets_Socket_Send_array_internal (SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *werror)
1658 DWORD sendflags = 0;
1659 gboolean interrupted;
1663 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1664 count = mono_array_length (buffers);
1666 sendflags = convert_socketflags (flags);
1667 if (sendflags == -1) {
1668 *werror = WSAEOPNOTSUPP;
1672 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
1678 MONO_PREPARE_BLOCKING;
1680 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1682 MONO_FINISH_BLOCKING;
1684 mono_thread_info_uninstall_interrupt (&interrupted);
1690 if (ret == SOCKET_ERROR) {
1691 *werror = WSAGetLastError ();
1699 ves_icall_System_Net_Sockets_Socket_SendTo_internal (SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *werror)
1706 struct sockaddr *sa;
1708 gboolean interrupted;
1712 alen = mono_array_length (buffer);
1713 if (offset > alen - count) {
1717 sa = create_sockaddr_from_object(sockaddr, &sa_size, werror, &error);
1720 if (!mono_error_ok (&error)) {
1721 mono_error_set_pending_exception (&error);
1725 LOGDEBUG (g_message ("%s: alen: %d", __func__, alen));
1727 buf = mono_array_addr (buffer, guchar, offset);
1729 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1731 sendflags = convert_socketflags (flags);
1732 if (sendflags == -1) {
1734 *werror = WSAEOPNOTSUPP;
1738 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
1745 MONO_PREPARE_BLOCKING;
1747 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1749 MONO_FINISH_BLOCKING;
1751 mono_thread_info_uninstall_interrupt (&interrupted);
1758 if (ret == SOCKET_ERROR)
1759 *werror = WSAGetLastError ();
1767 Socket_to_SOCKET (MonoObject *sockobj)
1769 MonoSafeHandle *safe_handle;
1770 MonoClassField *field;
1772 field = mono_class_get_field_from_name (sockobj->vtable->klass, "safe_handle");
1773 safe_handle = ((MonoSafeHandle *)(*(gpointer *)(((char *)sockobj) + field->offset)));
1775 if (safe_handle == NULL)
1778 return (SOCKET)safe_handle->handle;
1781 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1784 ves_icall_System_Net_Sockets_Socket_Select_internal (MonoArray **sockets, gint32 timeout, gint32 *werror)
1787 MonoInternalThread *thread = mono_thread_internal_current ();
1794 MonoClass *sock_arr_class;
1797 uintptr_t socks_size;
1798 gboolean interrupted;
1802 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1803 count = mono_array_length (*sockets);
1804 nfds = count - 3; /* NULL separators */
1805 pfds = g_new0 (mono_pollfd, nfds);
1807 for (i = 0; i < count; i++) {
1808 obj = mono_array_get (*sockets, MonoObject *, i);
1815 /* The socket array was bogus */
1817 *werror = WSAEFAULT;
1821 pfds [idx].fd = Socket_to_SOCKET (obj);
1822 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1826 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1827 start = time (NULL);
1829 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
1836 MONO_PREPARE_BLOCKING;
1838 ret = mono_poll (pfds, nfds, timeout);
1840 MONO_FINISH_BLOCKING;
1842 mono_thread_info_uninstall_interrupt (&interrupted);
1849 if (timeout > 0 && ret < 0) {
1851 int sec = time (NULL) - start;
1853 timeout -= sec * 1000;
1859 if (ret == -1 && errno == EINTR) {
1860 if (mono_thread_test_state (thread, (MonoThreadState)(ThreadState_AbortRequested | ThreadState_StopRequested))) {
1866 /* Suspend requested? */
1867 mono_thread_interruption_checkpoint ();
1871 } while (ret == -1 && errno == EINTR);
1875 *werror = WSAGetLastError ();
1877 *werror = errno_to_WSA (errno, __func__);
1889 sock_arr_class = ((MonoObject *)*sockets)->vtable->klass;
1890 socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
1891 socks = mono_array_new_full_checked (mono_domain_get (), sock_arr_class, &socks_size, NULL, &error);
1892 if (!mono_error_ok (&error)) {
1893 mono_error_set_pending_exception (&error);
1899 for (i = 0; i < count && ret > 0; i++) {
1902 obj = mono_array_get (*sockets, MonoObject *, i);
1909 pfd = &pfds [i - mode];
1910 if (pfd->revents == 0)
1914 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1915 mono_array_setref (socks, idx++, obj);
1916 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1917 mono_array_setref (socks, idx++, obj);
1918 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1919 mono_array_setref (socks, idx++, obj);
1928 int_to_object (MonoDomain *domain, int val, MonoError *error)
1930 return mono_value_box_checked (domain, mono_get_int32_class (), &val, error);
1934 ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal (SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *werror)
1936 int system_level = 0;
1937 int system_name = 0;
1940 socklen_t valsize = sizeof (val);
1941 struct linger linger;
1942 socklen_t lingersize = sizeof (linger);
1944 socklen_t time_ms_size = sizeof (time_ms);
1946 # if defined(__OpenBSD__)
1947 struct sockpeercred cred;
1951 socklen_t credsize = sizeof (cred);
1954 MonoDomain *domain = mono_domain_get ();
1956 MonoClass *obj_class;
1957 MonoClassField *field;
1961 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1962 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
1963 system_level = SOL_SOCKET;
1964 system_name = SO_REUSEADDR;
1969 ret = convert_sockopt_level_and_name ((MonoSocketOptionLevel)level, (MonoSocketOptionName)name, &system_level, &system_name);
1973 *werror = WSAENOPROTOOPT;
1977 *obj_val = int_to_object (domain, 0, &error);
1978 mono_error_set_pending_exception (&error);
1982 MONO_PREPARE_BLOCKING;
1984 /* No need to deal with MulticastOption names here, because
1985 * you cant getsockopt AddMembership or DropMembership (the
1986 * int getsockopt will error, causing an exception)
1989 case SocketOptionName_Linger:
1990 case SocketOptionName_DontLinger:
1991 ret = _wapi_getsockopt (sock, system_level, system_name, &linger, &lingersize);
1994 case SocketOptionName_SendTimeout:
1995 case SocketOptionName_ReceiveTimeout:
1996 ret = _wapi_getsockopt (sock, system_level, system_name, (char *)&time_ms, &time_ms_size);
2000 case SocketOptionName_PeerCred:
2001 ret = _wapi_getsockopt (sock, system_level, system_name, &cred, &credsize);
2006 ret = _wapi_getsockopt (sock, system_level, system_name, &val, &valsize);
2009 MONO_FINISH_BLOCKING;
2011 if (ret == SOCKET_ERROR) {
2012 *werror = WSAGetLastError ();
2017 case SocketOptionName_Linger:
2018 /* build a System.Net.Sockets.LingerOption */
2019 obj_class = mono_class_load_from_name (get_socket_assembly (),
2020 "System.Net.Sockets",
2022 obj = mono_object_new_checked (domain, obj_class, &error);
2023 if (!mono_error_ok (&error)) {
2024 mono_error_set_pending_exception (&error);
2028 /* Locate and set the fields "bool enabled" and "int
2031 field = mono_class_get_field_from_name(obj_class, "enabled");
2032 *(guint8 *)(((char *)obj)+field->offset) = linger.l_onoff;
2034 field = mono_class_get_field_from_name(obj_class, "lingerTime");
2035 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
2037 case SocketOptionName_DontLinger:
2038 /* construct a bool int in val - true if linger is off */
2039 obj = int_to_object (domain, !linger.l_onoff, &error);
2040 mono_error_set_pending_exception (&error);
2042 case SocketOptionName_SendTimeout:
2043 case SocketOptionName_ReceiveTimeout:
2044 obj = int_to_object (domain, time_ms, &error);
2045 mono_error_set_pending_exception (&error);
2049 case SocketOptionName_PeerCred: {
2051 * build a Mono.Posix.PeerCred+PeerCredData if
2054 static MonoImage *mono_posix_image = NULL;
2055 MonoPeerCredData *cred_data;
2057 if (mono_posix_image == NULL) {
2058 mono_posix_image = mono_image_loaded ("Mono.Posix");
2059 if (!mono_posix_image) {
2060 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
2062 *werror = WSAENOPROTOOPT;
2065 mono_posix_image = mono_assembly_get_image (sa);
2070 obj_class = mono_class_load_from_name (mono_posix_image,
2073 obj = mono_object_new_checked (domain, obj_class, &error);
2074 if (!mono_error_ok (&error)) {
2075 mono_error_set_pending_exception (&error);
2078 cred_data = (MonoPeerCredData *)obj;
2079 cred_data->pid = cred.pid;
2080 cred_data->uid = cred.uid;
2081 cred_data->gid = cred.gid;
2087 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2088 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse)
2091 obj = int_to_object (domain, val, &error);
2092 mono_error_set_pending_exception (&error);
2099 ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal (SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *werror)
2101 int system_level = 0;
2102 int system_name = 0;
2109 ret = convert_sockopt_level_and_name((MonoSocketOptionLevel)level, (MonoSocketOptionName)name, &system_level,
2112 *werror = WSAENOPROTOOPT;
2118 valsize = mono_array_length (*byte_val);
2119 buf = mono_array_addr (*byte_val, guchar, 0);
2121 MONO_PREPARE_BLOCKING;
2123 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
2125 MONO_FINISH_BLOCKING;
2127 if (ret == SOCKET_ERROR)
2128 *werror = WSAGetLastError ();
2131 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2132 static struct in_addr
2133 ipaddress_to_struct_in_addr (MonoObject *ipaddr)
2135 struct in_addr inaddr;
2136 MonoClassField *field;
2138 field = mono_class_get_field_from_name (ipaddr->vtable->klass, "m_Address");
2140 /* No idea why .net uses a 64bit type to hold a 32bit value...
2142 * Internal value of IPAddess is in little-endian order
2144 inaddr.s_addr = GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr) + field->offset));
2149 static struct in6_addr
2150 ipaddress_to_struct_in6_addr (MonoObject *ipaddr)
2152 struct in6_addr in6addr;
2153 MonoClassField *field;
2157 field = mono_class_get_field_from_name (ipaddr->vtable->klass, "m_Numbers");
2158 data = *(MonoArray **)(((char *)ipaddr) + field->offset);
2160 /* Solaris has only the 8 bit version. */
2162 for (i = 0; i < 8; i++) {
2163 guint16 s = mono_array_get (data, guint16, i);
2164 in6addr.s6_addr[2 * i + 1] = (s >> 8) & 0xff;
2165 in6addr.s6_addr[2 * i] = s & 0xff;
2168 for (i = 0; i < 8; i++)
2169 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
2175 #if defined(__APPLE__) || defined(__FreeBSD__)
2178 get_local_interface_id (int family)
2180 #if !defined(HAVE_GETIFADDRS) || !defined(HAVE_IF_NAMETOINDEX)
2183 struct ifaddrs *ifap = NULL, *ptr;
2186 if (getifaddrs (&ifap))
2189 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2190 if (!ptr->ifa_addr || !ptr->ifa_name)
2192 if (ptr->ifa_addr->sa_family != family)
2194 if ((ptr->ifa_flags & IFF_LOOPBACK) != 0)
2196 if ((ptr->ifa_flags & IFF_MULTICAST) == 0)
2199 idx = if_nametoindex (ptr->ifa_name);
2208 #endif /* defined(__APPLE__) || defined(__FreeBSD__) */
2211 ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal (SOCKET sock, gint32 level, gint32 name, MonoObject *obj_val, MonoArray *byte_val, gint32 int_val, gint32 *werror)
2213 struct linger linger;
2214 int system_level = 0;
2215 int system_name = 0;
2222 sol_ipv6 = mono_networking_get_ipv6_protocol ();
2223 sol_ip = mono_networking_get_ip_protocol ();
2225 ret = convert_sockopt_level_and_name ((MonoSocketOptionLevel)level, (MonoSocketOptionName)name, &system_level,
2228 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2229 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
2230 system_name = SO_REUSEADDR;
2231 int_val = int_val ? 0 : 1;
2237 *werror = WSAENOPROTOOPT;
2243 /* Only one of obj_val, byte_val or int_val has data */
2245 MonoClassField *field;
2249 case SocketOptionName_Linger:
2250 /* Dig out "bool enabled" and "int lingerTime"
2253 field = mono_class_get_field_from_name (obj_val->vtable->klass, "enabled");
2254 linger.l_onoff = *(guint8 *)(((char *)obj_val) + field->offset);
2255 field = mono_class_get_field_from_name (obj_val->vtable->klass, "lingerTime");
2256 linger.l_linger = *(guint32 *)(((char *)obj_val) + field->offset);
2258 valsize = sizeof (linger);
2259 ret = _wapi_setsockopt (sock, system_level,
2260 system_name, &linger, valsize);
2262 case SocketOptionName_AddMembership:
2263 case SocketOptionName_DropMembership:
2264 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2266 MonoObject *address = NULL;
2268 if (system_level == sol_ipv6) {
2269 struct ipv6_mreq mreq6;
2274 field = mono_class_get_field_from_name (obj_val->vtable->klass, "m_Group");
2275 address = *(MonoObject **)(((char *)obj_val) + field->offset);
2278 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2280 field = mono_class_get_field_from_name (obj_val->vtable->klass, "m_Interface");
2281 mreq6.ipv6mr_interface = *(guint64 *)(((char *)obj_val) + field->offset);
2283 #if defined(__APPLE__) || defined(__FreeBSD__)
2287 * Mac OS Lion doesn't allow ipv6mr_interface = 0.
2289 * Tests on Windows and Linux show that the multicast group is only
2290 * joined on one NIC when interface = 0, so we simply use the interface
2291 * id from the first non-loopback interface (this is also what
2292 * Dns.GetHostName (string.Empty) would return).
2294 if (!mreq6.ipv6mr_interface)
2295 mreq6.ipv6mr_interface = get_local_interface_id (AF_INET6);
2298 ret = _wapi_setsockopt (sock, system_level,
2299 system_name, &mreq6,
2301 } else if (system_level == sol_ip) {
2302 #ifdef HAVE_STRUCT_IP_MREQN
2303 struct ip_mreqn mreq = {{0}};
2305 struct ip_mreq mreq = {{0}};
2306 #endif /* HAVE_STRUCT_IP_MREQN */
2309 * pain! MulticastOption holds two IPAddress
2310 * members, so I have to dig the value out of
2313 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2314 address = *(MonoObject **)(((char *)obj_val) + field->offset);
2316 /* address might not be defined and if so, set the address to ADDR_ANY.
2319 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2321 field = mono_class_get_field_from_name (obj_val->vtable->klass, "localAddress");
2322 address = *(MonoObject **)(((char *)obj_val) + field->offset);
2324 #ifdef HAVE_STRUCT_IP_MREQN
2326 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2328 field = mono_class_get_field_from_name (obj_val->vtable->klass, "ifIndex");
2329 mreq.imr_ifindex = *(gint32 *)(((char *)obj_val) + field->offset);
2332 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2333 #endif /* HAVE_STRUCT_IP_MREQN */
2335 ret = _wapi_setsockopt (sock, system_level,
2341 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2343 /* Cause an exception to be thrown */
2344 *werror = WSAEINVAL;
2347 } else if (byte_val!=NULL) {
2348 int valsize = mono_array_length (byte_val);
2349 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2352 case SocketOptionName_DontLinger:
2354 linger.l_onoff = (*buf) ? 0 : 1;
2355 linger.l_linger = 0;
2356 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2358 *werror = WSAEINVAL;
2362 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2366 /* ReceiveTimeout/SendTimeout get here */
2368 case SocketOptionName_DontLinger:
2369 linger.l_onoff = !int_val;
2370 linger.l_linger = 0;
2371 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2373 case SocketOptionName_MulticastInterface:
2375 #ifdef HAVE_STRUCT_IP_MREQN
2376 int_val = GUINT32_FROM_BE (int_val);
2377 if ((int_val & 0xff000000) == 0) {
2378 /* int_val is interface index */
2379 struct ip_mreqn mreq = {{0}};
2380 mreq.imr_ifindex = int_val;
2381 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &mreq, sizeof (mreq));
2384 int_val = GUINT32_TO_BE (int_val);
2385 #endif /* HAVE_STRUCT_IP_MREQN */
2386 #endif /* HOST_WIN32 */
2387 /* int_val is in_addr */
2388 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2390 case SocketOptionName_DontFragment:
2391 #ifdef HAVE_IP_MTU_DISCOVER
2392 /* Fiddle with the value slightly if we're
2396 int_val = IP_PMTUDISC_DO;
2401 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2405 if (ret == SOCKET_ERROR)
2406 *werror = WSAGetLastError ();
2410 ves_icall_System_Net_Sockets_Socket_Shutdown_internal (SOCKET sock, gint32 how, gint32 *werror)
2413 gboolean interrupted;
2417 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
2423 MONO_PREPARE_BLOCKING;
2425 /* Currently, the values for how (recv=0, send=1, both=2) match the BSD API */
2426 ret = _wapi_shutdown (sock, how);
2428 MONO_FINISH_BLOCKING;
2430 mono_thread_info_uninstall_interrupt (&interrupted);
2436 if (ret == SOCKET_ERROR)
2437 *werror = WSAGetLastError ();
2441 ves_icall_System_Net_Sockets_Socket_IOControl_internal (SOCKET sock, gint32 code, MonoArray *input, MonoArray *output, gint32 *werror)
2443 glong output_bytes = 0;
2444 gchar *i_buffer, *o_buffer;
2450 if ((guint32)code == FIONBIO)
2451 /* Invalid command. Must use Socket.Blocking */
2454 if (input == NULL) {
2458 i_buffer = mono_array_addr (input, gchar, 0);
2459 i_len = mono_array_length (input);
2462 if (output == NULL) {
2466 o_buffer = mono_array_addr (output, gchar, 0);
2467 o_len = mono_array_length (output);
2470 MONO_PREPARE_BLOCKING;
2472 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2474 MONO_FINISH_BLOCKING;
2476 if (ret == SOCKET_ERROR) {
2477 *werror = WSAGetLastError ();
2481 return (gint)output_bytes;
2485 addrinfo_to_IPHostEntry (MonoAddressInfo *info, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips, MonoError *error)
2488 MonoAddressEntry *ai = NULL;
2489 struct in_addr *local_in = NULL;
2491 struct in6_addr *local_in6 = NULL;
2494 MonoDomain *domain = mono_domain_get ();
2496 mono_error_init (error);
2498 *h_aliases = mono_array_new_checked (domain, mono_get_string_class (), 0, error);
2499 return_val_if_nok (error, FALSE);
2500 if (add_local_ips) {
2501 local_in = (struct in_addr *) mono_get_local_interfaces (AF_INET, &nlocal_in);
2502 local_in6 = (struct in6_addr *) mono_get_local_interfaces (AF_INET6, &nlocal_in6);
2503 if (nlocal_in || nlocal_in6) {
2504 char addr [INET6_ADDRSTRLEN];
2505 *h_addr_list = mono_array_new_checked (domain, mono_get_string_class (), nlocal_in + nlocal_in6, error);
2510 MonoString *addr_string;
2513 for (i = 0; i < nlocal_in; i++) {
2515 mono_address_init (&maddr, AF_INET, &local_in [i]);
2516 if (mono_networking_addr_to_str (&maddr, addr, sizeof (addr))) {
2517 addr_string = mono_string_new (domain, addr);
2518 mono_array_setref (*h_addr_list, addr_index, addr_string);
2525 MonoString *addr_string;
2528 for (i = 0; i < nlocal_in6; i++) {
2530 mono_address_init (&maddr, AF_INET6, &local_in6 [i]);
2531 if (mono_networking_addr_to_str (&maddr, addr, sizeof (addr))) {
2532 addr_string = mono_string_new (domain, addr);
2533 mono_array_setref (*h_addr_list, addr_index, addr_string);
2543 mono_free_address_info (info);
2544 return is_ok (error);;
2551 for (count = 0, ai = info->entries; ai != NULL; ai = ai->next) {
2552 if (ai->family != AF_INET && ai->family != AF_INET6)
2557 *h_addr_list = mono_array_new_checked (domain, mono_get_string_class (), count, error);
2561 for (ai = info->entries, i = 0; ai != NULL; ai = ai->next) {
2563 MonoString *addr_string;
2564 char buffer [INET6_ADDRSTRLEN]; /* Max. size for IPv6 */
2566 if ((ai->family != PF_INET) && (ai->family != PF_INET6))
2569 mono_address_init (&maddr, ai->family, &ai->address);
2570 if (mono_networking_addr_to_str (&maddr, buffer, sizeof (buffer)))
2571 addr_string = mono_string_new (domain, buffer);
2573 addr_string = mono_string_new (domain, "");
2575 mono_array_setref (*h_addr_list, addr_index, addr_string);
2579 if (ai->canonical_name != NULL) {
2580 *h_name = mono_string_new (domain, ai->canonical_name);
2582 *h_name = mono_string_new (domain, buffer);
2591 mono_free_address_info (info);
2593 return is_ok (error);
2597 get_addrinfo_family_hint (MonoError *error)
2601 mono_error_init (error);
2603 hint = get_family_hint (error);
2604 return_val_if_nok (error, 0);
2608 return MONO_HINT_UNSPECIFIED;
2610 return MONO_HINT_IPV4;
2613 return MONO_HINT_IPV6;
2616 g_error ("invalid hint");
2622 ves_icall_System_Net_Dns_GetHostByName_internal (MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2624 gboolean add_local_ips = FALSE, add_info_ok = TRUE;
2625 gchar this_hostname [256];
2626 MonoAddressInfo *info = NULL;
2627 char *hostname = mono_string_to_utf8 (host);
2631 hint = get_addrinfo_family_hint (&error);
2632 if (!mono_error_ok (&error)) {
2633 mono_error_set_pending_exception (&error);
2637 if (*hostname == '\0') {
2638 add_local_ips = TRUE;
2642 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2643 if (!strcmp (hostname, this_hostname)) {
2644 add_local_ips = TRUE;
2649 if (*hostname && mono_get_address_info (hostname, 0, MONO_HINT_CANONICAL_NAME | hint, &info))
2650 add_info_ok = FALSE;
2655 MonoBoolean result = addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, add_local_ips, &error);
2656 mono_error_set_pending_exception (&error);
2663 ves_icall_System_Net_Dns_GetHostByAddr_internal (MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2666 struct sockaddr_in saddr;
2667 struct sockaddr_in6 saddr6;
2668 MonoAddressInfo *info = NULL;
2670 gint32 family, hint;
2671 gchar hostname [NI_MAXHOST] = { 0 };
2674 address = mono_string_to_utf8 (addr);
2676 if (inet_pton (AF_INET, address, &saddr.sin_addr ) == 1) {
2678 saddr.sin_family = AF_INET;
2679 } else if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) == 1) {
2681 saddr6.sin6_family = AF_INET6;
2689 MONO_PREPARE_BLOCKING;
2693 #if HAVE_SOCKADDR_IN_SIN_LEN
2694 saddr.sin_len = sizeof (saddr);
2696 ret = getnameinfo ((struct sockaddr*)&saddr, sizeof (saddr), hostname, sizeof (hostname), NULL, 0, 0) == 0;
2700 #if HAVE_SOCKADDR_IN6_SIN_LEN
2701 saddr6.sin6_len = sizeof (saddr6);
2703 ret = getnameinfo ((struct sockaddr*)&saddr6, sizeof (saddr6), hostname, sizeof (hostname), NULL, 0, 0) == 0;
2707 g_assert_not_reached ();
2710 MONO_FINISH_BLOCKING;
2715 hint = get_addrinfo_family_hint (&error);
2716 if (!mono_error_ok (&error)) {
2717 mono_error_set_pending_exception (&error);
2720 if (mono_get_address_info (hostname, 0, hint | MONO_HINT_CANONICAL_NAME | MONO_HINT_CONFIGURED_ONLY, &info) != 0)
2723 MonoBoolean result = addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE, &error);
2724 mono_error_set_pending_exception (&error);
2729 ves_icall_System_Net_Dns_GetHostName_internal (MonoString **h_name)
2731 gchar hostname [NI_MAXHOST] = { 0 };
2734 ret = gethostname (hostname, sizeof (hostname));
2738 *h_name = mono_string_new (mono_domain_get (), hostname);
2744 ves_icall_System_Net_Sockets_Socket_SendFile_internal (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
2749 gboolean interrupted;
2750 TRANSMIT_FILE_BUFFERS buffers;
2752 if (filename == NULL)
2755 /* FIXME: replace file by a proper fd that we can call open and close on, as they are interruptible */
2757 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &werror);
2759 if (file == INVALID_HANDLE_VALUE) {
2760 SetLastError (werror);
2764 memset (&buffers, 0, sizeof (buffers));
2765 if (pre_buffer != NULL) {
2766 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
2767 buffers.HeadLength = mono_array_length (pre_buffer);
2769 if (post_buffer != NULL) {
2770 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
2771 buffers.TailLength = mono_array_length (post_buffer);
2774 mono_thread_info_install_interrupt (abort_syscall, (gpointer) (gsize) mono_native_thread_id_get (), &interrupted);
2777 SetLastError (WSAEINTR);
2781 MONO_PREPARE_BLOCKING;
2783 ret = TransmitFile (sock, file, 0, 0, NULL, &buffers, flags);
2785 MONO_FINISH_BLOCKING;
2787 mono_thread_info_uninstall_interrupt (&interrupted);
2790 SetLastError (WSAEINTR);
2794 MONO_PREPARE_BLOCKING;
2798 MONO_FINISH_BLOCKING;
2804 ves_icall_System_Net_Sockets_Socket_SupportPortReuse (void)
2806 #if defined (SO_REUSEPORT) || defined (HOST_WIN32)
2814 mono_network_init (void)
2816 mono_networking_init ();
2820 mono_network_cleanup (void)
2822 _wapi_cleanup_networking ();
2823 mono_networking_shutdown ();
2827 icall_cancel_blocking_socket_operation (MonoThread *thread)
2829 MonoInternalThread *internal;
2831 internal = thread->internal_thread;
2832 g_assert (internal);
2834 mono_thread_info_abort_socket_syscall_for_close (MONO_UINT_TO_NATIVE_THREAD_ID (internal->tid));
2837 #endif /* #ifndef DISABLE_SOCKETS */