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)
14 #ifndef DISABLE_SOCKETS
17 #define __APPLE_USE_RFC_3542
28 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
37 #include <arpa/inet.h>
40 #include <mono/metadata/object.h>
41 #include <mono/io-layer/io-layer.h>
42 #include <mono/metadata/socket-io.h>
43 #include <mono/metadata/exception.h>
44 #include <mono/metadata/assembly.h>
45 #include <mono/metadata/appdomain.h>
46 #include <mono/metadata/file-io.h>
47 #include <mono/metadata/threads.h>
48 #include <mono/metadata/threads-types.h>
49 #include <mono/utils/mono-poll.h>
50 /* FIXME change this code to not mess so much with the internals */
51 #include <mono/metadata/class-internals.h>
52 #include <mono/metadata/threadpool-internals.h>
53 #include <mono/metadata/domain-internals.h>
54 #include <mono/utils/mono-threads.h>
55 #include <mono/utils/mono-memory-model.h>
58 #ifdef HAVE_SYS_TIME_H
61 #ifdef HAVE_SYS_IOCTL_H
62 #include <sys/ioctl.h>
71 #ifdef HAVE_SYS_FILIO_H
72 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
74 #ifdef HAVE_SYS_SOCKIO_H
75 #include <sys/sockio.h> /* defines SIOCATMARK */
81 #ifdef HAVE_GETIFADDRS
82 // <net/if.h> must be included before <ifaddrs.h>
86 #include "mono/io-layer/socket-wrappers.h"
88 #if defined(HOST_WIN32)
89 /* This is a kludge to make this file build under cygwin:
90 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
91 * prototypes for some but not all required functions (notably
92 * inet_ntop() is missing), but the libws2_32 library is missing the
93 * actual implementations of these functions.
99 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
102 * Some older versions of libc provide IPV6 support without defining the AI_ADDRCONFIG
103 * flag for getaddrinfo.
105 #ifndef AI_ADDRCONFIG
106 #define AI_ADDRCONFIG 0
109 static gint32 convert_family(MonoAddressFamily mono_family)
113 switch(mono_family) {
114 case AddressFamily_Unknown:
115 case AddressFamily_ImpLink:
116 case AddressFamily_Pup:
117 case AddressFamily_Chaos:
118 case AddressFamily_Iso:
119 case AddressFamily_Ecma:
120 case AddressFamily_DataKit:
121 case AddressFamily_Ccitt:
122 case AddressFamily_DataLink:
123 case AddressFamily_Lat:
124 case AddressFamily_HyperChannel:
125 case AddressFamily_NetBios:
126 case AddressFamily_VoiceView:
127 case AddressFamily_FireFox:
128 case AddressFamily_Banyan:
129 case AddressFamily_Atm:
130 case AddressFamily_Cluster:
131 case AddressFamily_Ieee12844:
132 case AddressFamily_NetworkDesigners:
133 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
136 case AddressFamily_Unspecified:
140 case AddressFamily_Unix:
144 case AddressFamily_InterNetwork:
148 case AddressFamily_Ipx:
154 case AddressFamily_Sna:
160 case AddressFamily_DecNet:
166 case AddressFamily_AppleTalk:
170 case AddressFamily_InterNetworkV6:
175 case AddressFamily_Irda:
181 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
187 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
189 MonoAddressFamily family=AddressFamily_Unknown;
193 family=AddressFamily_Unspecified;
197 family=AddressFamily_Unix;
201 family=AddressFamily_InterNetwork;
206 family=AddressFamily_Ipx;
212 family=AddressFamily_Sna;
218 family=AddressFamily_DecNet;
223 family=AddressFamily_AppleTalk;
228 family=AddressFamily_InterNetworkV6;
234 family=AddressFamily_Irda;
238 g_warning("unknown address family 0x%x", af_family);
244 static gint32 convert_type(MonoSocketType mono_type)
249 case SocketType_Stream:
253 case SocketType_Dgram:
267 case SocketType_Seqpacket:
271 case SocketType_Unknown:
272 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
276 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
282 static gint32 convert_proto(MonoProtocolType mono_proto)
287 case ProtocolType_IP:
288 case ProtocolType_IPv6:
289 case ProtocolType_Icmp:
290 case ProtocolType_Igmp:
291 case ProtocolType_Ggp:
292 case ProtocolType_Tcp:
293 case ProtocolType_Pup:
294 case ProtocolType_Udp:
295 case ProtocolType_Idp:
296 /* These protocols are known (on my system at least) */
300 case ProtocolType_ND:
301 case ProtocolType_Raw:
302 case ProtocolType_Ipx:
303 case ProtocolType_Spx:
304 case ProtocolType_SpxII:
305 case ProtocolType_Unknown:
306 /* These protocols arent */
307 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
317 /* Convert MonoSocketFlags */
318 static gint32 convert_socketflags (gint32 sflags)
323 /* SocketFlags.None */
326 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
327 SocketFlags_DontRoute | SocketFlags_Partial))
328 /* Contains invalid flag values */
331 if (sflags & SocketFlags_OutOfBand)
333 if (sflags & SocketFlags_Peek)
335 if (sflags & SocketFlags_DontRoute)
336 flags |= MSG_DONTROUTE;
338 /* Ignore Partial - see bug 349688. Don't return -1, because
339 * according to the comment in that bug ms runtime doesn't for
340 * UDP sockets (this means we will silently ignore it for TCP
344 if (sflags & SocketFlags_Partial)
348 /* Don't do anything for MaxIOVectorLength */
349 if (sflags & SocketFlags_MaxIOVectorLength)
357 * 0 on success (mapped mono_level and mono_name to system_level and system_name
359 * -2 on non-fatal error (ie, must ignore)
361 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
362 MonoSocketOptionName mono_name,
366 switch (mono_level) {
367 case SocketOptionLevel_Socket:
368 *system_level = SOL_SOCKET;
371 case SocketOptionName_DontLinger:
372 /* This is SO_LINGER, because the setsockopt
373 * internal call maps DontLinger to SO_LINGER
376 *system_name = SO_LINGER;
378 case SocketOptionName_Debug:
379 *system_name = SO_DEBUG;
382 case SocketOptionName_AcceptConnection:
383 *system_name = SO_ACCEPTCONN;
386 case SocketOptionName_ReuseAddress:
387 *system_name = SO_REUSEADDR;
389 case SocketOptionName_KeepAlive:
390 *system_name = SO_KEEPALIVE;
392 case SocketOptionName_DontRoute:
393 *system_name = SO_DONTROUTE;
395 case SocketOptionName_Broadcast:
396 *system_name = SO_BROADCAST;
398 case SocketOptionName_Linger:
399 *system_name = SO_LINGER;
401 case SocketOptionName_OutOfBandInline:
402 *system_name = SO_OOBINLINE;
404 case SocketOptionName_SendBuffer:
405 *system_name = SO_SNDBUF;
407 case SocketOptionName_ReceiveBuffer:
408 *system_name = SO_RCVBUF;
410 case SocketOptionName_SendLowWater:
411 *system_name = SO_SNDLOWAT;
413 case SocketOptionName_ReceiveLowWater:
414 *system_name = SO_RCVLOWAT;
416 case SocketOptionName_SendTimeout:
417 *system_name = SO_SNDTIMEO;
419 case SocketOptionName_ReceiveTimeout:
420 *system_name = SO_RCVTIMEO;
422 case SocketOptionName_Error:
423 *system_name = SO_ERROR;
425 case SocketOptionName_Type:
426 *system_name = SO_TYPE;
429 case SocketOptionName_PeerCred:
430 *system_name = SO_PEERCRED;
433 case SocketOptionName_ExclusiveAddressUse:
434 #ifdef SO_EXCLUSIVEADDRUSE
435 *system_name = SO_EXCLUSIVEADDRUSE;
438 case SocketOptionName_UseLoopback:
439 #ifdef SO_USELOOPBACK
440 *system_name = SO_USELOOPBACK;
443 case SocketOptionName_MaxConnections:
445 *system_name = SO_MAXCONN;
447 #elif defined(SOMAXCONN)
448 *system_name = SOMAXCONN;
452 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
457 case SocketOptionLevel_IP:
459 *system_level = SOL_IP;
462 static int cached = 0;
466 struct protoent *pent;
468 pent = getprotobyname ("IP");
469 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
473 *system_level = proto;
475 #endif /* HAVE_SOL_IP */
478 case SocketOptionName_IPOptions:
479 *system_name = IP_OPTIONS;
482 case SocketOptionName_HeaderIncluded:
483 *system_name = IP_HDRINCL;
487 case SocketOptionName_TypeOfService:
488 *system_name = IP_TOS;
492 case SocketOptionName_IpTimeToLive:
493 *system_name = IP_TTL;
496 case SocketOptionName_MulticastInterface:
497 *system_name = IP_MULTICAST_IF;
499 case SocketOptionName_MulticastTimeToLive:
500 *system_name = IP_MULTICAST_TTL;
502 case SocketOptionName_MulticastLoopback:
503 *system_name = IP_MULTICAST_LOOP;
505 case SocketOptionName_AddMembership:
506 *system_name = IP_ADD_MEMBERSHIP;
508 case SocketOptionName_DropMembership:
509 *system_name = IP_DROP_MEMBERSHIP;
511 #ifdef HAVE_IP_PKTINFO
512 case SocketOptionName_PacketInformation:
513 *system_name = IP_PKTINFO;
515 #endif /* HAVE_IP_PKTINFO */
517 case SocketOptionName_DontFragment:
518 #ifdef HAVE_IP_DONTFRAGMENT
519 *system_name = IP_DONTFRAGMENT;
521 #elif defined HAVE_IP_MTU_DISCOVER
522 /* Not quite the same */
523 *system_name = IP_MTU_DISCOVER;
526 /* If the flag is not available on this system, we can ignore this error */
528 #endif /* HAVE_IP_DONTFRAGMENT */
529 case SocketOptionName_AddSourceMembership:
530 case SocketOptionName_DropSourceMembership:
531 case SocketOptionName_BlockSource:
532 case SocketOptionName_UnblockSource:
533 /* Can't figure out how to map these, so fall
537 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
543 case SocketOptionLevel_IPv6:
545 *system_level = SOL_IPV6;
548 static int cached = 0;
552 struct protoent *pent;
554 pent = getprotobyname ("IPV6");
555 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
559 *system_level = proto;
561 #endif /* HAVE_SOL_IPV6 */
564 case SocketOptionName_IpTimeToLive:
565 case SocketOptionName_HopLimit:
566 *system_name = IPV6_UNICAST_HOPS;
568 case SocketOptionName_MulticastInterface:
569 *system_name = IPV6_MULTICAST_IF;
571 case SocketOptionName_MulticastTimeToLive:
572 *system_name = IPV6_MULTICAST_HOPS;
574 case SocketOptionName_MulticastLoopback:
575 *system_name = IPV6_MULTICAST_LOOP;
577 case SocketOptionName_AddMembership:
578 *system_name = IPV6_JOIN_GROUP;
580 case SocketOptionName_DropMembership:
581 *system_name = IPV6_LEAVE_GROUP;
583 case SocketOptionName_PacketInformation:
584 #ifdef HAVE_IPV6_PKTINFO
585 *system_name = IPV6_PKTINFO;
588 case SocketOptionName_HeaderIncluded:
589 case SocketOptionName_IPOptions:
590 case SocketOptionName_TypeOfService:
591 case SocketOptionName_DontFragment:
592 case SocketOptionName_AddSourceMembership:
593 case SocketOptionName_DropSourceMembership:
594 case SocketOptionName_BlockSource:
595 case SocketOptionName_UnblockSource:
596 /* Can't figure out how to map these, so fall
600 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
604 break; /* SocketOptionLevel_IPv6 */
607 case SocketOptionLevel_Tcp:
609 *system_level = SOL_TCP;
612 static int cached = 0;
616 struct protoent *pent;
618 pent = getprotobyname ("TCP");
619 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
623 *system_level = proto;
625 #endif /* HAVE_SOL_TCP */
628 case SocketOptionName_NoDelay:
629 *system_name = TCP_NODELAY;
632 /* The documentation is talking complete
633 * bollocks here: rfc-1222 is titled
634 * 'Advancing the NSFNET Routing Architecture'
635 * and doesn't mention either of the words
636 * "expedite" or "urgent".
638 case SocketOptionName_BsdUrgent:
639 case SocketOptionName_Expedited:
642 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
647 case SocketOptionLevel_Udp:
648 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
651 case SocketOptionName_NoChecksum:
652 case SocketOptionName_ChecksumCoverage:
654 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
661 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
668 static MonoImage *get_socket_assembly (void)
670 MonoDomain *domain = mono_domain_get ();
672 if (domain->socket_assembly == NULL) {
673 MonoImage *socket_assembly;
675 socket_assembly = mono_image_loaded ("System");
676 if (!socket_assembly) {
677 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
680 g_assert_not_reached ();
682 socket_assembly = mono_assembly_get_image (sa);
685 mono_atomic_store_release (&domain->socket_assembly, socket_assembly);
688 return domain->socket_assembly;
692 static gint32 get_family_hint(void)
694 MonoDomain *domain = mono_domain_get ();
696 if (!domain->inet_family_hint) {
697 MonoClass *socket_class;
698 MonoClassField *ipv6_field, *ipv4_field;
699 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
702 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
703 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
704 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
705 vtable = mono_class_vtable (mono_domain_get (), socket_class);
707 mono_runtime_class_init (vtable);
709 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
710 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
712 mono_domain_lock (domain);
713 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
714 domain->inet_family_hint = 1;
715 } else if (ipv4_enabled == 1) {
716 domain->inet_family_hint = 2;
718 domain->inet_family_hint = 3;
720 mono_domain_unlock (domain);
722 switch (domain->inet_family_hint) {
723 case 1: return PF_UNSPEC;
724 case 2: return PF_INET;
725 case 3: return PF_INET6;
732 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
743 sock_family=convert_family(family);
744 if(sock_family==-1) {
745 *error = WSAEAFNOSUPPORT;
749 sock_proto=convert_proto(proto);
751 *error = WSAEPROTONOSUPPORT;
755 sock_type=convert_type(type);
757 *error = WSAESOCKTNOSUPPORT;
761 sock = _wapi_socket (sock_family, sock_type, sock_proto,
762 NULL, 0, WSA_FLAG_OVERLAPPED);
764 if(sock==INVALID_SOCKET) {
765 *error = WSAGetLastError ();
769 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
770 return (GUINT_TO_POINTER (sock));
774 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
775 return (GUINT_TO_POINTER (sock));
779 return(GUINT_TO_POINTER (sock));
782 /* FIXME: the SOCKET parameter (here and in other functions in this
783 * file) is really an IntPtr which needs to be converted to a guint32.
785 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
790 LOGDEBUG (g_message ("%s: closing 0x%x", __func__, sock));
794 /* Clear any pending work item from this socket if the underlying
795 * polling system does not notify when the socket is closed */
796 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
800 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
804 LOGDEBUG (g_message("%s: returning %d", __func__, WSAGetLastError()));
806 return(WSAGetLastError());
809 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
819 ret=ioctlsocket(sock, FIONREAD, &amount);
820 if(ret==SOCKET_ERROR) {
821 *error = WSAGetLastError ();
828 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
839 * block == TRUE/FALSE means we will block/not block.
840 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
844 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
845 if(ret==SOCKET_ERROR) {
846 *error = WSAGetLastError ();
850 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
861 MonoInternalThread* curthread = mono_thread_internal_current ();
862 curthread->interrupt_on_stop = (gpointer)TRUE;
863 newsock = _wapi_accept (sock, NULL, 0);
864 curthread->interrupt_on_stop = (gpointer)FALSE;
867 newsock = _wapi_accept (sock, NULL, 0);
869 if(newsock==INVALID_SOCKET) {
870 *error = WSAGetLastError ();
874 return(GUINT_TO_POINTER (newsock));
877 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
887 ret = _wapi_listen (sock, backlog);
888 if(ret==SOCKET_ERROR) {
889 *error = WSAGetLastError ();
894 // Check whether it's ::ffff::0:0.
896 is_ipv4_mapped_any (const struct in6_addr *addr)
900 for (i = 0; i < 10; i++) {
901 if (addr->s6_addr [i])
904 if ((addr->s6_addr [10] != 0xff) || (addr->s6_addr [11] != 0xff))
906 for (i = 12; i < 16; i++) {
907 if (addr->s6_addr [i])
914 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
915 int sa_size, gint32 *error)
917 MonoDomain *domain = mono_domain_get ();
918 MonoObject *sockaddr_obj;
920 MonoAddressFamily family;
922 /* Build a System.Net.SocketAddress object instance */
923 if (!domain->sockaddr_class) {
924 domain->sockaddr_class=mono_class_from_name (get_socket_assembly (), "System.Net", "SocketAddress");
925 g_assert (domain->sockaddr_class);
927 sockaddr_obj=mono_object_new(domain, domain->sockaddr_class);
929 /* Locate the SocketAddress data buffer in the object */
930 if (!domain->sockaddr_data_field) {
931 domain->sockaddr_data_field=mono_class_get_field_from_name (domain->sockaddr_class, "data");
932 g_assert (domain->sockaddr_data_field);
935 /* May be the +2 here is too conservative, as sa_len returns
936 * the length of the entire sockaddr_in/in6, including
937 * sizeof (unsigned short) of the family */
938 /* We can't really avoid the +2 as all code below depends on this size - INCLUDING unix domain sockets.*/
939 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
941 /* The data buffer is laid out as follows:
942 * bytes 0 and 1 are the address family
943 * bytes 2 and 3 are the port info
944 * the rest is the address info
947 family=convert_to_mono_family(saddr->sa_family);
948 if(family==AddressFamily_Unknown) {
949 *error = WSAEAFNOSUPPORT;
953 mono_array_set(data, guint8, 0, family & 0x0FF);
954 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
956 if(saddr->sa_family==AF_INET) {
957 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
958 guint16 port=ntohs(sa_in->sin_port);
959 guint32 address=ntohl(sa_in->sin_addr.s_addr);
962 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
965 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
966 mono_array_set(data, guint8, 3, (port) & 0xff);
967 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
968 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
969 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
970 mono_array_set(data, guint8, 7, (address) & 0xff);
972 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
974 return(sockaddr_obj);
976 } else if (saddr->sa_family == AF_INET6) {
977 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
980 guint16 port=ntohs(sa_in->sin6_port);
983 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
986 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
987 mono_array_set(data, guint8, 3, (port) & 0xff);
989 if (is_ipv4_mapped_any (&sa_in->sin6_addr)) {
990 // Map ::ffff:0:0 to :: (bug #5502)
991 for(i=0; i<16; i++) {
992 mono_array_set(data, guint8, 8+i, 0);
995 for(i=0; i<16; i++) {
996 mono_array_set(data, guint8, 8+i,
997 sa_in->sin6_addr.s6_addr[i]);
1001 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
1002 mono_array_set(data, guint8, 25,
1003 (sa_in->sin6_scope_id >> 8) & 0xff);
1004 mono_array_set(data, guint8, 26,
1005 (sa_in->sin6_scope_id >> 16) & 0xff);
1006 mono_array_set(data, guint8, 27,
1007 (sa_in->sin6_scope_id >> 24) & 0xff);
1009 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1011 return(sockaddr_obj);
1013 #ifdef HAVE_SYS_UN_H
1014 } else if (saddr->sa_family == AF_UNIX) {
1017 for (i = 0; i < sa_size; i++) {
1018 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
1021 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1023 return sockaddr_obj;
1026 *error = WSAEAFNOSUPPORT;
1032 get_sockaddr_size (int family)
1037 if (family == AF_INET) {
1038 size = sizeof (struct sockaddr_in);
1040 } else if (family == AF_INET6) {
1041 size = sizeof (struct sockaddr_in6);
1043 #ifdef HAVE_SYS_UN_H
1044 } else if (family == AF_UNIX) {
1045 size = sizeof (struct sockaddr_un);
1051 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1058 MONO_ARCH_SAVE_REGS;
1062 salen = get_sockaddr_size (convert_family (af));
1064 *error = WSAEAFNOSUPPORT;
1067 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1068 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1070 if(ret==SOCKET_ERROR) {
1071 *error = WSAGetLastError ();
1077 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)));
1079 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1085 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1092 MONO_ARCH_SAVE_REGS;
1096 salen = get_sockaddr_size (convert_family (af));
1098 *error = WSAEAFNOSUPPORT;
1101 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1102 /* Note: linux returns just 2 for AF_UNIX. Always. */
1103 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1104 if(ret==SOCKET_ERROR) {
1105 *error = WSAGetLastError ();
1111 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)));
1113 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1119 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1123 MonoClassField *field;
1128 /* Dig the SocketAddress data buffer out of the object */
1129 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1130 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1132 /* The data buffer is laid out as follows:
1133 * byte 0 is the address family low byte
1134 * byte 1 is the address family high byte
1136 * bytes 2 and 3 are the port info
1137 * the rest is the address info
1139 * the rest is the file name
1141 len = mono_array_length (data);
1143 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1146 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1147 if (family == AF_INET) {
1148 struct sockaddr_in *sa;
1153 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1156 sa = g_new0 (struct sockaddr_in, 1);
1157 port = (mono_array_get (data, guint8, 2) << 8) +
1158 mono_array_get (data, guint8, 3);
1159 address = (mono_array_get (data, guint8, 4) << 24) +
1160 (mono_array_get (data, guint8, 5) << 16 ) +
1161 (mono_array_get (data, guint8, 6) << 8) +
1162 mono_array_get (data, guint8, 7);
1164 sa->sin_family = family;
1165 sa->sin_addr.s_addr = htonl (address);
1166 sa->sin_port = htons (port);
1168 *sa_size = sizeof(struct sockaddr_in);
1169 return((struct sockaddr *)sa);
1172 } else if (family == AF_INET6) {
1173 struct sockaddr_in6 *sa;
1179 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1182 sa = g_new0 (struct sockaddr_in6, 1);
1183 port = mono_array_get (data, guint8, 3) +
1184 (mono_array_get (data, guint8, 2) << 8);
1185 scopeid = mono_array_get (data, guint8, 24) +
1186 (mono_array_get (data, guint8, 25) << 8) +
1187 (mono_array_get (data, guint8, 26) << 16) +
1188 (mono_array_get (data, guint8, 27) << 24);
1190 sa->sin6_family = family;
1191 sa->sin6_port = htons (port);
1192 sa->sin6_scope_id = scopeid;
1194 for(i=0; i<16; i++) {
1195 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1198 *sa_size = sizeof(struct sockaddr_in6);
1199 return((struct sockaddr *)sa);
1201 #ifdef HAVE_SYS_UN_H
1202 } else if (family == AF_UNIX) {
1203 struct sockaddr_un *sock_un;
1206 /* Need a byte for the '\0' terminator/prefix, and the first
1207 * two bytes hold the SocketAddress family
1209 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1210 mono_raise_exception (mono_get_exception_index_out_of_range ());
1213 sock_un = g_new0 (struct sockaddr_un, 1);
1215 sock_un->sun_family = family;
1216 for (i = 0; i < len - 2; i++) {
1217 sock_un->sun_path [i] = mono_array_get (data, guint8,
1223 return (struct sockaddr *)sock_un;
1226 *error = WSAEAFNOSUPPORT;
1231 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1233 struct sockaddr *sa;
1237 MONO_ARCH_SAVE_REGS;
1241 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1246 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)));
1248 ret = _wapi_bind (sock, sa, sa_size);
1249 if(ret==SOCKET_ERROR) {
1250 *error = WSAGetLastError ();
1263 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1264 gint timeout, gint32 *error)
1266 MonoInternalThread *thread = NULL;
1272 MONO_ARCH_SAVE_REGS;
1274 pfds = g_new0 (mono_pollfd, 1);
1275 pfds[0].fd = GPOINTER_TO_INT (sock);
1276 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1277 (mode == SelectModeWrite) ? MONO_POLLOUT :
1278 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1280 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1281 start = time (NULL);
1285 ret = mono_poll (pfds, 1, timeout);
1286 if (timeout > 0 && ret < 0) {
1288 int sec = time (NULL) - start;
1290 timeout -= sec * 1000;
1298 if (ret == -1 && errno == EINTR) {
1301 if (thread == NULL) {
1302 thread = mono_thread_internal_current ();
1305 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1311 /* Suspend requested? */
1312 mono_thread_interruption_checkpoint ();
1316 } while (ret == -1 && errno == EINTR);
1320 *error = WSAGetLastError ();
1322 *error = errno_to_WSA (errno, __func__);
1337 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1339 struct sockaddr *sa;
1343 MONO_ARCH_SAVE_REGS;
1347 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1352 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)));
1354 ret = _wapi_connect (sock, sa, sa_size);
1355 if(ret==SOCKET_ERROR) {
1356 *error = WSAGetLastError ();
1362 /* These #defines from mswsock.h from wine. Defining them here allows
1363 * us to build this file on a mingw box that doesn't know the magic
1364 * numbers, but still run on a newer windows box that does.
1366 #ifndef WSAID_DISCONNECTEX
1367 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1368 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1371 #ifndef WSAID_TRANSMITFILE
1372 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1373 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1376 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1379 glong output_bytes = 0;
1380 GUID disco_guid = WSAID_DISCONNECTEX;
1381 GUID trans_guid = WSAID_TRANSMITFILE;
1382 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1383 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1386 MONO_ARCH_SAVE_REGS;
1390 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1392 /* I _think_ the extension function pointers need to be looked
1393 * up for each socket. FIXME: check the best way to store
1394 * pointers to functions in managed objects that still works
1395 * on 64bit platforms.
1397 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1398 (void *)&disco_guid, sizeof(GUID),
1399 (void *)&_wapi_disconnectex, sizeof(void *),
1400 &output_bytes, NULL, NULL);
1402 /* make sure that WSAIoctl didn't put crap in the
1405 _wapi_disconnectex = NULL;
1407 /* Look up the TransmitFile extension function pointer
1408 * instead of calling TransmitFile() directly, because
1409 * apparently "Several of the extension functions have
1410 * been available since WinSock 1.1 and are exported
1411 * from MSWsock.dll, however it's not advisable to
1412 * link directly to this dll as this ties you to the
1413 * Microsoft WinSock provider. A provider neutral way
1414 * of accessing these extension functions is to load
1415 * them dynamically via WSAIoctl using the
1416 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1417 * should, theoretically, allow you to access these
1418 * functions from any provider that supports them..."
1419 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1421 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1422 (void *)&trans_guid, sizeof(GUID),
1423 (void *)&_wapi_transmitfile, sizeof(void *),
1424 &output_bytes, NULL, NULL);
1426 _wapi_transmitfile = NULL;
1430 if (_wapi_disconnectex != NULL) {
1431 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1432 } else if (_wapi_transmitfile != NULL) {
1433 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1434 TF_DISCONNECT | TF_REUSE_SOCKET);
1436 *error = ERROR_NOT_SUPPORTED;
1440 if (bret == FALSE) {
1441 *error = WSAGetLastError ();
1445 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1452 MONO_ARCH_SAVE_REGS;
1456 alen = mono_array_length (buffer);
1457 if (offset > alen - count) {
1461 buf=mono_array_addr(buffer, guchar, offset);
1463 recvflags = convert_socketflags (flags);
1464 if (recvflags == -1) {
1465 *error = WSAEOPNOTSUPP;
1471 MonoInternalThread* curthread = mono_thread_internal_current ();
1472 curthread->interrupt_on_stop = (gpointer)TRUE;
1473 ret = _wapi_recv (sock, buf, count, recvflags);
1474 curthread->interrupt_on_stop = (gpointer)FALSE;
1477 ret = _wapi_recv (sock, buf, count, recvflags);
1480 if(ret==SOCKET_ERROR) {
1481 *error = WSAGetLastError ();
1488 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1493 DWORD recvflags = 0;
1495 MONO_ARCH_SAVE_REGS;
1499 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1500 count = mono_array_length (buffers);
1502 recvflags = convert_socketflags (flags);
1503 if (recvflags == -1) {
1504 *error = WSAEOPNOTSUPP;
1508 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1509 if (ret == SOCKET_ERROR) {
1510 *error = WSAGetLastError ();
1517 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1523 struct sockaddr *sa;
1526 MONO_ARCH_SAVE_REGS;
1530 alen = mono_array_length (buffer);
1531 if (offset > alen - count) {
1535 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1540 buf=mono_array_addr(buffer, guchar, offset);
1542 recvflags = convert_socketflags (flags);
1543 if (recvflags == -1) {
1544 *error = WSAEOPNOTSUPP;
1548 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1549 if(ret==SOCKET_ERROR) {
1551 *error = WSAGetLastError ();
1555 /* If we didn't get a socket size, then we're probably a
1556 * connected connection-oriented socket and the stack hasn't
1557 * returned the remote address. All we can do is return null.
1560 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1569 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1576 MONO_ARCH_SAVE_REGS;
1580 alen = mono_array_length (buffer);
1581 if (offset > alen - count) {
1585 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1587 buf=mono_array_addr(buffer, guchar, offset);
1589 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1591 sendflags = convert_socketflags (flags);
1592 if (sendflags == -1) {
1593 *error = WSAEOPNOTSUPP;
1597 ret = _wapi_send (sock, buf, count, sendflags);
1598 if(ret==SOCKET_ERROR) {
1599 *error = WSAGetLastError ();
1606 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1611 DWORD sendflags = 0;
1613 MONO_ARCH_SAVE_REGS;
1617 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1618 count = mono_array_length (buffers);
1620 sendflags = convert_socketflags (flags);
1621 if (sendflags == -1) {
1622 *error = WSAEOPNOTSUPP;
1626 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1627 if (ret == SOCKET_ERROR) {
1628 *error = WSAGetLastError ();
1635 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1641 struct sockaddr *sa;
1644 MONO_ARCH_SAVE_REGS;
1648 alen = mono_array_length (buffer);
1649 if (offset > alen - count) {
1653 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1658 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1660 buf=mono_array_addr(buffer, guchar, offset);
1662 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1664 sendflags = convert_socketflags (flags);
1665 if (sendflags == -1) {
1666 *error = WSAEOPNOTSUPP;
1670 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1671 if(ret==SOCKET_ERROR) {
1672 *error = WSAGetLastError ();
1680 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1683 MonoClassField *field;
1685 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1686 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1691 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1692 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1694 MonoInternalThread *thread = NULL;
1701 MonoClass *sock_arr_class;
1704 uintptr_t socks_size;
1706 MONO_ARCH_SAVE_REGS;
1708 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1709 count = mono_array_length (*sockets);
1710 nfds = count - 3; /* NULL separators */
1711 pfds = g_new0 (mono_pollfd, nfds);
1713 for (i = 0; i < count; i++) {
1714 obj = mono_array_get (*sockets, MonoObject *, i);
1721 /* The socket array was bogus */
1727 pfds [idx].fd = Socket_to_SOCKET (obj);
1728 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1732 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1733 start = time (NULL);
1736 ret = mono_poll (pfds, nfds, timeout);
1737 if (timeout > 0 && ret < 0) {
1739 int sec = time (NULL) - start;
1741 timeout -= sec * 1000;
1747 if (ret == -1 && errno == EINTR) {
1750 thread = mono_thread_internal_current ();
1752 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1759 /* Suspend requested? */
1760 mono_thread_interruption_checkpoint ();
1764 } while (ret == -1 && errno == EINTR);
1768 *error = WSAGetLastError ();
1770 *error = errno_to_WSA (errno, __func__);
1782 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1783 socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
1784 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1787 for (i = 0; i < count && ret > 0; i++) {
1790 obj = mono_array_get (*sockets, MonoObject *, i);
1797 pfd = &pfds [i - mode];
1798 if (pfd->revents == 0)
1802 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1803 mono_array_setref (socks, idx++, obj);
1804 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1805 mono_array_setref (socks, idx++, obj);
1806 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1807 mono_array_setref (socks, idx++, obj);
1815 static MonoObject* int_to_object (MonoDomain *domain, int val)
1817 return mono_value_box (domain, mono_get_int32_class (), &val);
1821 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1827 socklen_t valsize=sizeof(val);
1828 struct linger linger;
1829 socklen_t lingersize=sizeof(linger);
1831 socklen_t time_ms_size = sizeof (time_ms);
1833 # if defined(__OpenBSD__)
1834 struct sockpeercred cred;
1838 socklen_t credsize = sizeof(cred);
1840 MonoDomain *domain=mono_domain_get();
1842 MonoClass *obj_class;
1843 MonoClassField *field;
1845 MONO_ARCH_SAVE_REGS;
1849 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1850 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
1851 system_level = SOL_SOCKET;
1852 system_name = SO_REUSEADDR;
1858 ret = convert_sockopt_level_and_name (level, name, &system_level, &system_name);
1862 *error = WSAENOPROTOOPT;
1866 *obj_val = int_to_object (domain, 0);
1870 /* No need to deal with MulticastOption names here, because
1871 * you cant getsockopt AddMembership or DropMembership (the
1872 * int getsockopt will error, causing an exception)
1875 case SocketOptionName_Linger:
1876 case SocketOptionName_DontLinger:
1877 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1881 case SocketOptionName_SendTimeout:
1882 case SocketOptionName_ReceiveTimeout:
1883 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1887 case SocketOptionName_PeerCred:
1888 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1894 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1898 if(ret==SOCKET_ERROR) {
1899 *error = WSAGetLastError ();
1904 case SocketOptionName_Linger:
1905 /* build a System.Net.Sockets.LingerOption */
1906 obj_class=mono_class_from_name(get_socket_assembly (),
1907 "System.Net.Sockets",
1909 obj=mono_object_new(domain, obj_class);
1911 /* Locate and set the fields "bool enabled" and "int
1914 field=mono_class_get_field_from_name(obj_class, "enabled");
1915 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1917 field=mono_class_get_field_from_name(obj_class, "seconds");
1918 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1922 case SocketOptionName_DontLinger:
1923 /* construct a bool int in val - true if linger is off */
1924 obj = int_to_object (domain, !linger.l_onoff);
1927 case SocketOptionName_SendTimeout:
1928 case SocketOptionName_ReceiveTimeout:
1929 obj = int_to_object (domain, time_ms);
1933 case SocketOptionName_PeerCred:
1935 /* build a Mono.Posix.PeerCred+PeerCredData if
1938 static MonoImage *mono_posix_image = NULL;
1939 MonoPeerCredData *cred_data;
1941 if (mono_posix_image == NULL) {
1942 mono_posix_image=mono_image_loaded ("Mono.Posix");
1943 if (!mono_posix_image) {
1944 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1946 *error = WSAENOPROTOOPT;
1949 mono_posix_image = mono_assembly_get_image (sa);
1954 obj_class = mono_class_from_name(mono_posix_image,
1957 obj = mono_object_new(domain, obj_class);
1958 cred_data = (MonoPeerCredData *)obj;
1959 cred_data->pid = cred.pid;
1960 cred_data->uid = cred.uid;
1961 cred_data->gid = cred.gid;
1967 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1968 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse)
1971 obj = int_to_object (domain, val);
1976 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1984 MONO_ARCH_SAVE_REGS;
1988 ret=convert_sockopt_level_and_name(level, name, &system_level,
1991 *error = WSAENOPROTOOPT;
1997 valsize=mono_array_length(*byte_val);
1998 buf=mono_array_addr(*byte_val, guchar, 0);
2000 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
2001 if(ret==SOCKET_ERROR) {
2002 *error = WSAGetLastError ();
2006 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2007 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
2009 struct in_addr inaddr;
2010 MonoClassField *field;
2012 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
2014 /* No idea why .net uses a 64bit type to hold a 32bit value...
2016 * Internal value of IPAddess is in little-endian order
2018 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
2024 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
2026 struct in6_addr in6addr;
2027 MonoClassField *field;
2031 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
2032 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
2034 /* Solaris has only the 8 bit version. */
2036 for(i=0; i<8; i++) {
2037 guint16 s = mono_array_get (data, guint16, i);
2038 in6addr.s6_addr[2 * i + 1] = (s >> 8) & 0xff;
2039 in6addr.s6_addr[2 * i] = s & 0xff;
2043 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
2047 #endif /* AF_INET6 */
2050 #if defined(__APPLE__)
2052 #if defined(HAVE_GETIFADDRS) && defined(HAVE_IF_NAMETOINDEX)
2054 get_local_interface_id (int family)
2056 struct ifaddrs *ifap = NULL, *ptr;
2059 if (getifaddrs (&ifap)) {
2063 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2064 if (!ptr->ifa_addr || !ptr->ifa_name)
2066 if (ptr->ifa_addr->sa_family != family)
2068 if ((ptr->ifa_flags & IFF_LOOPBACK) != 0)
2070 if ((ptr->ifa_flags & IFF_MULTICAST) == 0)
2073 idx = if_nametoindex (ptr->ifa_name);
2082 get_local_interface_id (int family)
2088 #endif /* __APPLE__ */
2090 void ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal(SOCKET sock, gint32 level, gint32 name, MonoObject *obj_val, MonoArray *byte_val, gint32 int_val, gint32 *error)
2092 struct linger linger;
2102 #ifdef HAVE_SOL_IPV6
2103 sol_ipv6 = SOL_IPV6;
2106 struct protoent *pent;
2107 pent = getprotobyname ("ipv6");
2108 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2116 struct protoent *pent;
2117 pent = getprotobyname ("ip");
2118 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2121 #endif /* AF_INET6 */
2123 MONO_ARCH_SAVE_REGS;
2125 ret=convert_sockopt_level_and_name(level, name, &system_level,
2128 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2129 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
2130 system_name = SO_REUSEADDR;
2131 int_val = int_val ? 0 : 1;
2137 *error = WSAENOPROTOOPT;
2144 /* Only one of obj_val, byte_val or int_val has data */
2146 MonoClassField *field;
2150 case SocketOptionName_Linger:
2151 /* Dig out "bool enabled" and "int seconds"
2154 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2155 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2156 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2157 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2159 valsize=sizeof(linger);
2160 ret = _wapi_setsockopt (sock, system_level,
2161 system_name, &linger, valsize);
2163 case SocketOptionName_AddMembership:
2164 case SocketOptionName_DropMembership:
2165 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2167 MonoObject *address = NULL;
2170 if(system_level == sol_ipv6) {
2171 struct ipv6_mreq mreq6;
2176 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2177 address = *(gpointer *)(((char *)obj_val) + field->offset);
2180 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2183 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2184 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2186 #if defined(__APPLE__)
2190 * Mac OS Lion doesn't allow ipv6mr_interface = 0.
2192 * Tests on Windows and Linux show that the multicast group is only
2193 * joined on one NIC when interface = 0, so we simply use the interface
2194 * id from the first non-loopback interface (this is also what
2195 * Dns.GetHostName (string.Empty) would return).
2197 if (!mreq6.ipv6mr_interface)
2198 mreq6.ipv6mr_interface = get_local_interface_id (AF_INET6);
2201 ret = _wapi_setsockopt (sock, system_level,
2202 system_name, &mreq6,
2204 } else if(system_level == sol_ip)
2205 #endif /* AF_INET6 */
2207 #ifdef HAVE_STRUCT_IP_MREQN
2208 struct ip_mreqn mreq = {{0}};
2210 struct ip_mreq mreq = {{0}};
2211 #endif /* HAVE_STRUCT_IP_MREQN */
2213 /* pain! MulticastOption holds two IPAddress
2214 * members, so I have to dig the value out of
2217 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2218 address = *(gpointer *)(((char *)obj_val) + field->offset);
2220 /* address might not be defined and if so, set the address to ADDR_ANY.
2223 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2226 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2227 address = *(gpointer *)(((char *)obj_val) + field->offset);
2229 #ifdef HAVE_STRUCT_IP_MREQN
2231 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2235 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2237 #endif /* HAVE_STRUCT_IP_MREQN */
2239 ret = _wapi_setsockopt (sock, system_level,
2245 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2247 /* Cause an exception to be thrown */
2251 } else if (byte_val!=NULL) {
2252 int valsize = mono_array_length (byte_val);
2253 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2256 case SocketOptionName_DontLinger:
2258 linger.l_onoff = (*buf) ? 0 : 1;
2259 linger.l_linger = 0;
2260 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2266 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2270 /* ReceiveTimeout/SendTimeout get here */
2272 case SocketOptionName_DontLinger:
2273 linger.l_onoff = !int_val;
2274 linger.l_linger = 0;
2275 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2277 case SocketOptionName_DontFragment:
2278 #ifdef HAVE_IP_MTU_DISCOVER
2279 /* Fiddle with the value slightly if we're
2283 int_val = IP_PMTUDISC_DO;
2289 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2293 if(ret==SOCKET_ERROR) {
2294 *error = WSAGetLastError ();
2298 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2304 MONO_ARCH_SAVE_REGS;
2308 /* Currently, the values for how (recv=0, send=1, both=2) match
2311 ret = _wapi_shutdown (sock, how);
2312 if(ret==SOCKET_ERROR) {
2313 *error = WSAGetLastError ();
2318 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2320 MonoArray *output, gint32 *error)
2322 glong output_bytes = 0;
2323 gchar *i_buffer, *o_buffer;
2327 MONO_ARCH_SAVE_REGS;
2331 if ((guint32)code == FIONBIO) {
2332 /* Invalid command. Must use Socket.Blocking */
2336 if (input == NULL) {
2340 i_buffer = mono_array_addr (input, gchar, 0);
2341 i_len = mono_array_length (input);
2344 if (output == NULL) {
2348 o_buffer = mono_array_addr (output, gchar, 0);
2349 o_len = mono_array_length (output);
2352 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2353 if (ret == SOCKET_ERROR) {
2354 *error = WSAGetLastError ();
2358 return (gint) output_bytes;
2361 #ifdef HAVE_SIOCGIFCONF
2363 is_loopback (int family, void *ad)
2365 char *ptr = (char *) ad;
2367 if (family == AF_INET) {
2368 return (ptr [0] == 127);
2372 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2379 get_local_ips (int family, int *nips)
2381 int addr_size, offset, fd, i, count;
2382 int max_ifaces = 50; /* 50 interfaces should be enough... */
2385 struct ifreq iflags;
2386 char *result, *tmp_ptr;
2387 gboolean ignore_loopback = FALSE;
2390 if (family == AF_INET) {
2391 addr_size = sizeof (struct in_addr);
2392 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2394 } else if (family == AF_INET6) {
2395 addr_size = sizeof (struct in6_addr);
2396 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2402 fd = socket (family, SOCK_STREAM, 0);
2404 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2405 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2406 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2408 g_free (ifc.ifc_buf);
2412 count = ifc.ifc_len / sizeof (struct ifreq);
2415 g_free (ifc.ifc_buf);
2420 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2421 strcpy (iflags.ifr_name, ifr->ifr_name);
2422 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2426 if ((iflags.ifr_flags & IFF_UP) == 0) {
2427 ifr->ifr_name [0] = '\0';
2431 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2432 ignore_loopback = TRUE;
2437 result = g_malloc (addr_size * count);
2439 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2440 if (ifr->ifr_name [0] == '\0') {
2445 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2450 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2451 tmp_ptr += addr_size;
2454 g_free (ifc.ifc_buf);
2457 #elif defined(HAVE_GETIFADDRS)
2459 is_loopback (int family, void *ad)
2461 char *ptr = (char *) ad;
2463 if (family == AF_INET) {
2464 return (ptr [0] == 127);
2468 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2475 get_local_ips (int family, int *nips)
2477 struct ifaddrs *ifap = NULL, *ptr;
2478 int addr_size, offset, count, i;
2479 char *result, *tmp_ptr;
2482 if (family == AF_INET) {
2483 addr_size = sizeof (struct in_addr);
2484 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2486 } else if (family == AF_INET6) {
2487 addr_size = sizeof (struct in6_addr);
2488 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2494 if (getifaddrs (&ifap)) {
2499 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2502 if (ptr->ifa_addr->sa_family != family)
2504 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2509 result = g_malloc (addr_size * count);
2511 for (i = 0, ptr = ifap; ptr; ptr = ptr->ifa_next) {
2514 if (ptr->ifa_addr->sa_family != family)
2516 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2519 memcpy (tmp_ptr, ((char *) ptr->ifa_addr) + offset, addr_size);
2520 tmp_ptr += addr_size;
2529 get_local_ips (int family, int *nips)
2535 #endif /* HAVE_SIOCGIFCONF */
2538 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2539 MonoArray **h_aliases,
2540 MonoArray **h_addr_list,
2541 gboolean add_local_ips)
2543 MonoDomain *domain = mono_domain_get ();
2545 struct in_addr *local_in = NULL;
2549 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2553 *h_name=mono_string_new(domain, he->h_name);
2555 while(he->h_aliases[i]!=NULL) {
2559 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2561 while(he->h_aliases[i]!=NULL) {
2564 alias=mono_string_new(domain, he->h_aliases[i]);
2565 mono_array_setref (*h_aliases, i, alias);
2568 } else if (!add_local_ips) {
2572 if (add_local_ips) {
2573 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2575 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2576 for (i = 0; i < nlocal_in; i++) {
2577 MonoString *addr_string;
2578 char addr [16], *ptr;
2580 ptr = (char *) &local_in [i];
2581 g_snprintf(addr, 16, "%u.%u.%u.%u",
2582 (unsigned char) ptr [0],
2583 (unsigned char) ptr [1],
2584 (unsigned char) ptr [2],
2585 (unsigned char) ptr [3]);
2587 addr_string = mono_string_new (domain, addr);
2588 mono_array_setref (*h_addr_list, i, addr_string);
2593 } else if (he == NULL) {
2594 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2595 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2596 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2601 if (nlocal_in == 0 && he != NULL) {
2603 while (he->h_addr_list[i]!=NULL) {
2607 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2609 while(he->h_addr_list[i]!=NULL) {
2610 MonoString *addr_string;
2613 g_snprintf(addr, 16, "%u.%u.%u.%u",
2614 (unsigned char)he->h_addr_list[i][0],
2615 (unsigned char)he->h_addr_list[i][1],
2616 (unsigned char)he->h_addr_list[i][2],
2617 (unsigned char)he->h_addr_list[i][3]);
2619 addr_string=mono_string_new(domain, addr);
2620 mono_array_setref (*h_addr_list, i, addr_string);
2628 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2629 MonoArray **h_aliases,
2630 MonoArray **h_addr_list)
2632 MonoDomain *domain = mono_domain_get ();
2634 *h_name=mono_string_new(domain, addr);
2635 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2636 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2637 mono_array_setref (*h_addr_list, 0, *h_name);
2643 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2644 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2645 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2647 MonoDomain *domain = mono_domain_get ();
2648 int i, host_count, host_index, family_hint;
2649 struct in_addr *local_in = NULL;
2651 struct in6_addr *local_in6 = NULL;
2653 gboolean from_local = FALSE;
2655 family_hint = get_family_hint ();
2658 * Check if address length and family are correct
2660 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2664 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2669 * Get the aliases and host name from he1 or he2 whichever is
2670 * not null, if he1 is not null then take aliases from he1
2672 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2673 family_hint == PF_INET)) {
2674 *h_name=mono_string_new (domain, he1->h_name);
2677 while(he1->h_aliases[i]!=NULL) {
2681 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2684 while(he1->h_aliases[i]!=NULL) {
2687 alias=mono_string_new (domain, he1->h_aliases[i]);
2688 mono_array_setref (*h_aliases, i, alias);
2691 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2692 family_hint == PF_INET6)) {
2693 *h_name=mono_string_new (domain, he2->h_name);
2696 while(he2->h_aliases [i] != NULL) {
2700 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2703 while(he2->h_aliases[i]!=NULL) {
2706 alias=mono_string_new (domain, he2->h_aliases[i]);
2707 mono_array_setref (*h_aliases, i, alias);
2710 } else if (!add_local_ips) {
2715 * Count the number of addresses in he1 + he2
2718 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2719 family_hint == PF_INET)) {
2721 while(he1->h_addr_list[i]!=NULL) {
2727 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2728 family_hint == PF_INET6)) {
2730 while(he2->h_addr_list[i]!=NULL) {
2740 if (add_local_ips) {
2741 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2742 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2744 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2745 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2747 if (nlocal_in || nlocal_in6) {
2749 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2750 nlocal_in + nlocal_in6);
2754 for (n = 0; n < nlocal_in6; n++) {
2755 MonoString *addr_string;
2757 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2759 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2762 addr_string = mono_string_new (domain, addr);
2763 mono_array_setref (*h_addr_list, host_index, addr_string);
2771 for (n = 0; n < nlocal_in; n++) {
2772 MonoString *addr_string;
2774 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2776 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2779 addr_string = mono_string_new (domain, addr);
2780 mono_array_setref (*h_addr_list, host_index, addr_string);
2788 } else if (he1 == NULL && he2 == NULL) {
2789 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2790 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2791 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2801 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2803 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2804 family_hint == PF_INET6)) {
2806 while(he2->h_addr_list[i] != NULL) {
2807 MonoString *addr_string;
2809 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2811 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2815 addr_string = mono_string_new (domain, addr);
2816 mono_array_setref (*h_addr_list, host_index, addr_string);
2823 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2824 family_hint == PF_INET)) {
2826 while(he1->h_addr_list[i] != NULL) {
2827 MonoString *addr_string;
2829 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2831 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2835 addr_string=mono_string_new (domain, addr);
2836 mono_array_setref (*h_addr_list, host_index, addr_string);
2847 #if defined(AF_INET6)
2849 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2850 MonoArray **h_aliases,
2851 MonoArray **h_addr_list,
2852 gboolean add_local_ips)
2855 struct addrinfo *ai = NULL;
2856 struct in_addr *local_in = NULL;
2858 struct in6_addr *local_in6 = NULL;
2862 MonoDomain *domain = mono_domain_get ();
2865 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2866 if (add_local_ips) {
2867 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2868 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2869 if (nlocal_in || nlocal_in6) {
2870 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2872 MonoString *addr_string;
2876 for (i = 0; i < nlocal_in; i++) {
2877 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2878 addr_string = mono_string_new (domain, addr);
2879 mono_array_setref (*h_addr_list, addr_index, addr_string);
2885 MonoString *addr_string;
2890 for (i = 0; i < nlocal_in6; i++) {
2891 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2893 addr_string = mono_string_new (domain, addr);
2894 mono_array_setref (*h_addr_list, addr_index, addr_string);
2903 freeaddrinfo (info);
2912 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2913 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2919 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2921 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2922 MonoString *addr_string;
2924 char buffer [48]; /* Max. size for IPv6 */
2926 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2930 if(ai->ai_family == PF_INET) {
2931 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2933 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2937 addr_string=mono_string_new(domain, buffer);
2939 addr_string=mono_string_new(domain, "");
2942 mono_array_setref (*h_addr_list, addr_index, addr_string);
2946 if (ai->ai_canonname != NULL) {
2947 *h_name=mono_string_new(domain, ai->ai_canonname);
2949 *h_name=mono_string_new(domain, buffer);
2965 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2967 gboolean add_local_ips = FALSE;
2968 #ifdef HAVE_SIOCGIFCONF
2969 gchar this_hostname [256];
2971 #if !defined(HAVE_GETHOSTBYNAME2_R)
2972 struct addrinfo *info = NULL, hints;
2975 MONO_ARCH_SAVE_REGS;
2977 hostname=mono_string_to_utf8 (host);
2978 if (*hostname == '\0') {
2979 add_local_ips = TRUE;
2982 #ifdef HAVE_SIOCGIFCONF
2983 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2984 if (!strcmp (hostname, this_hostname)) {
2985 add_local_ips = TRUE;
2991 memset(&hints, 0, sizeof(hints));
2992 hints.ai_family = get_family_hint ();
2993 hints.ai_socktype = SOCK_STREAM;
2994 hints.ai_flags = AI_CANONNAME;
2996 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
3002 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
3004 struct hostent he1,*hp1, he2, *hp2;
3005 int buffer_size1, buffer_size2;
3006 char *buffer1, *buffer2;
3008 gboolean return_value;
3011 MONO_ARCH_SAVE_REGS;
3013 hostname=mono_string_to_utf8 (host);
3014 if (*hostname == '\0')
3015 add_local_ips = TRUE;
3017 #ifdef HAVE_SIOCGIFCONF
3018 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3019 if (!strcmp (hostname, this_hostname))
3020 add_local_ips = TRUE;
3026 buffer1 = g_malloc0(buffer_size1);
3027 buffer2 = g_malloc0(buffer_size2);
3031 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
3032 &hp1, &herr) == ERANGE) {
3034 buffer1 = g_realloc(buffer1, buffer_size1);
3037 if (*hostname && hp1 == NULL)
3039 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
3040 buffer_size2, &hp2, &herr) == ERANGE) {
3042 buffer2 = g_realloc(buffer2, buffer_size2);
3046 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
3047 h_addr_list, add_local_ips);
3053 return(return_value);
3054 #endif /* HAVE_GETHOSTBYNAME2_R */
3056 #else /* AF_INET6 */
3057 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3061 gboolean add_local_ips = FALSE;
3062 #ifdef HAVE_SIOCGIFCONF
3063 gchar this_hostname [256];
3066 MONO_ARCH_SAVE_REGS;
3068 hostname=mono_string_to_utf8(host);
3069 if (*hostname == '\0')
3070 add_local_ips = TRUE;
3071 #ifdef HAVE_SIOCGIFCONF
3072 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3073 if (!strcmp (hostname, this_hostname))
3074 add_local_ips = TRUE;
3081 he = _wapi_gethostbyname (hostname);
3083 he = _wapi_gethostbyname (hostname);
3087 if (*hostname && he==NULL)
3090 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
3092 #endif /* AF_INET6 */
3094 #ifndef HAVE_INET_PTON
3096 inet_pton (int family, const char *address, void *inaddrp)
3098 if (family == AF_INET) {
3099 #ifdef HAVE_INET_ATON
3100 struct in_addr inaddr;
3102 if (!inet_aton (address, &inaddr))
3105 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
3108 /* assume the system has inet_addr(), if it doesn't
3109 have that we're pretty much screwed... */
3112 if (!strcmp (address, "255.255.255.255")) {
3113 /* special-case hack */
3114 inaddr = 0xffffffff;
3116 inaddr = inet_addr (address);
3118 #define INADDR_NONE ((in_addr_t) -1)
3120 if (inaddr == INADDR_NONE)
3124 memcpy (inaddrp, &inaddr, sizeof (guint32));
3126 #endif /* HAVE_INET_ATON */
3131 #endif /* !HAVE_INET_PTON */
3133 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3138 struct sockaddr_in saddr;
3139 struct sockaddr_in6 saddr6;
3140 struct addrinfo *info = NULL, hints;
3142 char hostname[NI_MAXHOST] = {0};
3145 struct in_addr inaddr;
3150 address = mono_string_to_utf8 (addr);
3153 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
3154 /* Maybe an ipv6 address */
3155 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
3161 saddr6.sin6_family = AF_INET6;
3166 saddr.sin_family = AF_INET;
3170 if(family == AF_INET) {
3171 #if HAVE_SOCKADDR_IN_SIN_LEN
3172 saddr.sin_len = sizeof (saddr);
3174 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
3175 hostname, sizeof(hostname), NULL, 0,
3179 } else if(family == AF_INET6) {
3180 #if HAVE_SOCKADDR_IN6_SIN_LEN
3181 saddr6.sin6_len = sizeof (saddr6);
3183 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
3184 hostname, sizeof(hostname), NULL, 0,
3190 memset (&hints, 0, sizeof(hints));
3191 hints.ai_family = get_family_hint ();
3192 hints.ai_socktype = SOCK_STREAM;
3193 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
3195 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
3199 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
3201 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
3206 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
3207 ret = ipaddr_to_IPHostEntry (address, h_name, h_aliases, h_addr_list);
3209 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
3210 h_addr_list, FALSE);
3218 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
3220 gchar hostname[256];
3223 MONO_ARCH_SAVE_REGS;
3225 ret = gethostname (hostname, sizeof (hostname));
3230 *h_name=mono_string_new(mono_domain_get (), hostname);
3236 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
3240 TRANSMIT_FILE_BUFFERS buffers;
3242 MONO_ARCH_SAVE_REGS;
3244 if (filename == NULL)
3247 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
3248 if (file == INVALID_HANDLE_VALUE) {
3249 SetLastError (error);
3253 memset (&buffers, 0, sizeof (buffers));
3254 if (pre_buffer != NULL) {
3255 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
3256 buffers.HeadLength = mono_array_length (pre_buffer);
3258 if (post_buffer != NULL) {
3259 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
3260 buffers.TailLength = mono_array_length (post_buffer);
3263 if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
3272 void mono_network_init(void)
3277 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3279 g_error("%s: Couldn't initialise networking", __func__);
3283 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3284 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3287 void mono_network_cleanup(void)
3293 icall_cancel_blocking_socket_operation (MonoThread *thread)
3295 MonoInternalThread *internal = thread->internal_thread;
3297 if (mono_thread_info_new_interrupt_enabled ()) {
3298 mono_thread_info_abort_socket_syscall_for_close ((MonoNativeThreadId)(gsize)internal->tid);
3301 internal->ignore_next_signal = TRUE;
3302 mono_thread_kill (internal, mono_thread_get_abort_signal ());
3307 #endif /* #ifndef DISABLE_SOCKETS */