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 /* FIXME: this might require amount to be unsigned long. */
820 ret=ioctlsocket(sock, FIONREAD, &amount);
821 if(ret==SOCKET_ERROR) {
822 *error = WSAGetLastError ();
829 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
840 * block == TRUE/FALSE means we will block/not block.
841 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
845 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
846 if(ret==SOCKET_ERROR) {
847 *error = WSAGetLastError ();
851 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
862 MonoInternalThread* curthread = mono_thread_internal_current ();
863 curthread->interrupt_on_stop = (gpointer)TRUE;
864 newsock = _wapi_accept (sock, NULL, 0);
865 curthread->interrupt_on_stop = (gpointer)FALSE;
868 newsock = _wapi_accept (sock, NULL, 0);
870 if(newsock==INVALID_SOCKET) {
871 *error = WSAGetLastError ();
875 return(GUINT_TO_POINTER (newsock));
878 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
888 ret = _wapi_listen (sock, backlog);
889 if(ret==SOCKET_ERROR) {
890 *error = WSAGetLastError ();
895 // Check whether it's ::ffff::0:0.
897 is_ipv4_mapped_any (const struct in6_addr *addr)
901 for (i = 0; i < 10; i++) {
902 if (addr->s6_addr [i])
905 if ((addr->s6_addr [10] != 0xff) || (addr->s6_addr [11] != 0xff))
907 for (i = 12; i < 16; i++) {
908 if (addr->s6_addr [i])
915 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
916 int sa_size, gint32 *error)
918 MonoDomain *domain = mono_domain_get ();
919 MonoObject *sockaddr_obj;
921 MonoAddressFamily family;
923 /* Build a System.Net.SocketAddress object instance */
924 if (!domain->sockaddr_class) {
925 domain->sockaddr_class=mono_class_from_name (get_socket_assembly (), "System.Net", "SocketAddress");
926 g_assert (domain->sockaddr_class);
928 sockaddr_obj=mono_object_new(domain, domain->sockaddr_class);
930 /* Locate the SocketAddress data buffer in the object */
931 if (!domain->sockaddr_data_field) {
932 domain->sockaddr_data_field=mono_class_get_field_from_name (domain->sockaddr_class, "data");
933 g_assert (domain->sockaddr_data_field);
936 /* May be the +2 here is too conservative, as sa_len returns
937 * the length of the entire sockaddr_in/in6, including
938 * sizeof (unsigned short) of the family */
939 /* We can't really avoid the +2 as all code below depends on this size - INCLUDING unix domain sockets.*/
940 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
942 /* The data buffer is laid out as follows:
943 * bytes 0 and 1 are the address family
944 * bytes 2 and 3 are the port info
945 * the rest is the address info
948 family=convert_to_mono_family(saddr->sa_family);
949 if(family==AddressFamily_Unknown) {
950 *error = WSAEAFNOSUPPORT;
954 mono_array_set(data, guint8, 0, family & 0x0FF);
955 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
957 if(saddr->sa_family==AF_INET) {
958 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
959 guint16 port=ntohs(sa_in->sin_port);
960 guint32 address=ntohl(sa_in->sin_addr.s_addr);
963 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
966 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
967 mono_array_set(data, guint8, 3, (port) & 0xff);
968 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
969 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
970 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
971 mono_array_set(data, guint8, 7, (address) & 0xff);
973 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
975 return(sockaddr_obj);
977 } else if (saddr->sa_family == AF_INET6) {
978 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
981 guint16 port=ntohs(sa_in->sin6_port);
984 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
987 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
988 mono_array_set(data, guint8, 3, (port) & 0xff);
990 if (is_ipv4_mapped_any (&sa_in->sin6_addr)) {
991 // Map ::ffff:0:0 to :: (bug #5502)
992 for(i=0; i<16; i++) {
993 mono_array_set(data, guint8, 8+i, 0);
996 for(i=0; i<16; i++) {
997 mono_array_set(data, guint8, 8+i,
998 sa_in->sin6_addr.s6_addr[i]);
1002 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
1003 mono_array_set(data, guint8, 25,
1004 (sa_in->sin6_scope_id >> 8) & 0xff);
1005 mono_array_set(data, guint8, 26,
1006 (sa_in->sin6_scope_id >> 16) & 0xff);
1007 mono_array_set(data, guint8, 27,
1008 (sa_in->sin6_scope_id >> 24) & 0xff);
1010 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1012 return(sockaddr_obj);
1014 #ifdef HAVE_SYS_UN_H
1015 } else if (saddr->sa_family == AF_UNIX) {
1018 for (i = 0; i < sa_size; i++) {
1019 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
1022 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1024 return sockaddr_obj;
1027 *error = WSAEAFNOSUPPORT;
1033 get_sockaddr_size (int family)
1038 if (family == AF_INET) {
1039 size = sizeof (struct sockaddr_in);
1041 } else if (family == AF_INET6) {
1042 size = sizeof (struct sockaddr_in6);
1044 #ifdef HAVE_SYS_UN_H
1045 } else if (family == AF_UNIX) {
1046 size = sizeof (struct sockaddr_un);
1052 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1059 MONO_ARCH_SAVE_REGS;
1063 salen = get_sockaddr_size (convert_family (af));
1065 *error = WSAEAFNOSUPPORT;
1068 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1069 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1071 if(ret==SOCKET_ERROR) {
1072 *error = WSAGetLastError ();
1078 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)));
1080 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1086 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1093 MONO_ARCH_SAVE_REGS;
1097 salen = get_sockaddr_size (convert_family (af));
1099 *error = WSAEAFNOSUPPORT;
1102 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1103 /* Note: linux returns just 2 for AF_UNIX. Always. */
1104 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1105 if(ret==SOCKET_ERROR) {
1106 *error = WSAGetLastError ();
1112 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)));
1114 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1120 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1124 MonoClassField *field;
1129 /* Dig the SocketAddress data buffer out of the object */
1130 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1131 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1133 /* The data buffer is laid out as follows:
1134 * byte 0 is the address family low byte
1135 * byte 1 is the address family high byte
1137 * bytes 2 and 3 are the port info
1138 * the rest is the address info
1140 * the rest is the file name
1142 len = mono_array_length (data);
1144 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1147 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1148 if (family == AF_INET) {
1149 struct sockaddr_in *sa;
1154 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1157 sa = g_new0 (struct sockaddr_in, 1);
1158 port = (mono_array_get (data, guint8, 2) << 8) +
1159 mono_array_get (data, guint8, 3);
1160 address = (mono_array_get (data, guint8, 4) << 24) +
1161 (mono_array_get (data, guint8, 5) << 16 ) +
1162 (mono_array_get (data, guint8, 6) << 8) +
1163 mono_array_get (data, guint8, 7);
1165 sa->sin_family = family;
1166 sa->sin_addr.s_addr = htonl (address);
1167 sa->sin_port = htons (port);
1169 *sa_size = sizeof(struct sockaddr_in);
1170 return((struct sockaddr *)sa);
1173 } else if (family == AF_INET6) {
1174 struct sockaddr_in6 *sa;
1180 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1183 sa = g_new0 (struct sockaddr_in6, 1);
1184 port = mono_array_get (data, guint8, 3) +
1185 (mono_array_get (data, guint8, 2) << 8);
1186 scopeid = mono_array_get (data, guint8, 24) +
1187 (mono_array_get (data, guint8, 25) << 8) +
1188 (mono_array_get (data, guint8, 26) << 16) +
1189 (mono_array_get (data, guint8, 27) << 24);
1191 sa->sin6_family = family;
1192 sa->sin6_port = htons (port);
1193 sa->sin6_scope_id = scopeid;
1195 for(i=0; i<16; i++) {
1196 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1199 *sa_size = sizeof(struct sockaddr_in6);
1200 return((struct sockaddr *)sa);
1202 #ifdef HAVE_SYS_UN_H
1203 } else if (family == AF_UNIX) {
1204 struct sockaddr_un *sock_un;
1207 /* Need a byte for the '\0' terminator/prefix, and the first
1208 * two bytes hold the SocketAddress family
1210 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1211 mono_raise_exception (mono_get_exception_index_out_of_range ());
1214 sock_un = g_new0 (struct sockaddr_un, 1);
1216 sock_un->sun_family = family;
1217 for (i = 0; i < len - 2; i++) {
1218 sock_un->sun_path [i] = mono_array_get (data, guint8,
1224 return (struct sockaddr *)sock_un;
1227 *error = WSAEAFNOSUPPORT;
1232 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1234 struct sockaddr *sa;
1238 MONO_ARCH_SAVE_REGS;
1242 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1247 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)));
1249 ret = _wapi_bind (sock, sa, sa_size);
1250 if(ret==SOCKET_ERROR) {
1251 *error = WSAGetLastError ();
1264 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1265 gint timeout, gint32 *error)
1267 MonoInternalThread *thread = NULL;
1273 MONO_ARCH_SAVE_REGS;
1275 pfds = g_new0 (mono_pollfd, 1);
1276 pfds[0].fd = GPOINTER_TO_INT (sock);
1277 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1278 (mode == SelectModeWrite) ? MONO_POLLOUT :
1279 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1281 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1282 start = time (NULL);
1286 ret = mono_poll (pfds, 1, timeout);
1287 if (timeout > 0 && ret < 0) {
1289 int sec = time (NULL) - start;
1291 timeout -= sec * 1000;
1299 if (ret == -1 && errno == EINTR) {
1302 if (thread == NULL) {
1303 thread = mono_thread_internal_current ();
1306 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1312 /* Suspend requested? */
1313 mono_thread_interruption_checkpoint ();
1317 } while (ret == -1 && errno == EINTR);
1321 *error = WSAGetLastError ();
1323 *error = errno_to_WSA (errno, __func__);
1338 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1340 struct sockaddr *sa;
1344 MONO_ARCH_SAVE_REGS;
1348 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1353 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)));
1355 ret = _wapi_connect (sock, sa, sa_size);
1356 if(ret==SOCKET_ERROR) {
1357 *error = WSAGetLastError ();
1363 /* These #defines from mswsock.h from wine. Defining them here allows
1364 * us to build this file on a mingw box that doesn't know the magic
1365 * numbers, but still run on a newer windows box that does.
1367 #ifndef WSAID_DISCONNECTEX
1368 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1369 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1372 #ifndef WSAID_TRANSMITFILE
1373 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1374 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1377 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1380 glong output_bytes = 0;
1381 GUID disco_guid = WSAID_DISCONNECTEX;
1382 GUID trans_guid = WSAID_TRANSMITFILE;
1383 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1384 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1387 MONO_ARCH_SAVE_REGS;
1391 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1393 /* I _think_ the extension function pointers need to be looked
1394 * up for each socket. FIXME: check the best way to store
1395 * pointers to functions in managed objects that still works
1396 * on 64bit platforms.
1398 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1399 (void *)&disco_guid, sizeof(GUID),
1400 (void *)&_wapi_disconnectex, sizeof(void *),
1401 &output_bytes, NULL, NULL);
1403 /* make sure that WSAIoctl didn't put crap in the
1406 _wapi_disconnectex = NULL;
1408 /* Look up the TransmitFile extension function pointer
1409 * instead of calling TransmitFile() directly, because
1410 * apparently "Several of the extension functions have
1411 * been available since WinSock 1.1 and are exported
1412 * from MSWsock.dll, however it's not advisable to
1413 * link directly to this dll as this ties you to the
1414 * Microsoft WinSock provider. A provider neutral way
1415 * of accessing these extension functions is to load
1416 * them dynamically via WSAIoctl using the
1417 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1418 * should, theoretically, allow you to access these
1419 * functions from any provider that supports them..."
1420 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1422 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1423 (void *)&trans_guid, sizeof(GUID),
1424 (void *)&_wapi_transmitfile, sizeof(void *),
1425 &output_bytes, NULL, NULL);
1427 _wapi_transmitfile = NULL;
1431 if (_wapi_disconnectex != NULL) {
1432 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1433 } else if (_wapi_transmitfile != NULL) {
1434 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1435 TF_DISCONNECT | TF_REUSE_SOCKET);
1437 *error = ERROR_NOT_SUPPORTED;
1441 if (bret == FALSE) {
1442 *error = WSAGetLastError ();
1446 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1453 MONO_ARCH_SAVE_REGS;
1457 alen = mono_array_length (buffer);
1458 if (offset > alen - count) {
1462 buf=mono_array_addr(buffer, guchar, offset);
1464 recvflags = convert_socketflags (flags);
1465 if (recvflags == -1) {
1466 *error = WSAEOPNOTSUPP;
1472 MonoInternalThread* curthread = mono_thread_internal_current ();
1473 curthread->interrupt_on_stop = (gpointer)TRUE;
1474 ret = _wapi_recv (sock, buf, count, recvflags);
1475 curthread->interrupt_on_stop = (gpointer)FALSE;
1478 ret = _wapi_recv (sock, buf, count, recvflags);
1481 if(ret==SOCKET_ERROR) {
1482 *error = WSAGetLastError ();
1489 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1494 DWORD recvflags = 0;
1496 MONO_ARCH_SAVE_REGS;
1500 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1501 count = mono_array_length (buffers);
1503 recvflags = convert_socketflags (flags);
1504 if (recvflags == -1) {
1505 *error = WSAEOPNOTSUPP;
1509 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1510 if (ret == SOCKET_ERROR) {
1511 *error = WSAGetLastError ();
1518 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1524 struct sockaddr *sa;
1527 MONO_ARCH_SAVE_REGS;
1531 alen = mono_array_length (buffer);
1532 if (offset > alen - count) {
1536 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1541 buf=mono_array_addr(buffer, guchar, offset);
1543 recvflags = convert_socketflags (flags);
1544 if (recvflags == -1) {
1545 *error = WSAEOPNOTSUPP;
1549 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1550 if(ret==SOCKET_ERROR) {
1552 *error = WSAGetLastError ();
1556 /* If we didn't get a socket size, then we're probably a
1557 * connected connection-oriented socket and the stack hasn't
1558 * returned the remote address. All we can do is return null.
1561 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1570 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1577 MONO_ARCH_SAVE_REGS;
1581 alen = mono_array_length (buffer);
1582 if (offset > alen - count) {
1586 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1588 buf=mono_array_addr(buffer, guchar, offset);
1590 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1592 sendflags = convert_socketflags (flags);
1593 if (sendflags == -1) {
1594 *error = WSAEOPNOTSUPP;
1598 ret = _wapi_send (sock, buf, count, sendflags);
1599 if(ret==SOCKET_ERROR) {
1600 *error = WSAGetLastError ();
1607 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1612 DWORD sendflags = 0;
1614 MONO_ARCH_SAVE_REGS;
1618 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1619 count = mono_array_length (buffers);
1621 sendflags = convert_socketflags (flags);
1622 if (sendflags == -1) {
1623 *error = WSAEOPNOTSUPP;
1627 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1628 if (ret == SOCKET_ERROR) {
1629 *error = WSAGetLastError ();
1636 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1642 struct sockaddr *sa;
1645 MONO_ARCH_SAVE_REGS;
1649 alen = mono_array_length (buffer);
1650 if (offset > alen - count) {
1654 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1659 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1661 buf=mono_array_addr(buffer, guchar, offset);
1663 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1665 sendflags = convert_socketflags (flags);
1666 if (sendflags == -1) {
1667 *error = WSAEOPNOTSUPP;
1671 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1672 if(ret==SOCKET_ERROR) {
1673 *error = WSAGetLastError ();
1681 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1684 MonoClassField *field;
1686 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1687 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1692 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1693 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1695 MonoInternalThread *thread = NULL;
1702 MonoClass *sock_arr_class;
1705 uintptr_t socks_size;
1707 MONO_ARCH_SAVE_REGS;
1709 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1710 count = mono_array_length (*sockets);
1711 nfds = count - 3; /* NULL separators */
1712 pfds = g_new0 (mono_pollfd, nfds);
1714 for (i = 0; i < count; i++) {
1715 obj = mono_array_get (*sockets, MonoObject *, i);
1722 /* The socket array was bogus */
1728 pfds [idx].fd = Socket_to_SOCKET (obj);
1729 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1733 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1734 start = time (NULL);
1737 ret = mono_poll (pfds, nfds, timeout);
1738 if (timeout > 0 && ret < 0) {
1740 int sec = time (NULL) - start;
1742 timeout -= sec * 1000;
1748 if (ret == -1 && errno == EINTR) {
1751 thread = mono_thread_internal_current ();
1753 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1760 /* Suspend requested? */
1761 mono_thread_interruption_checkpoint ();
1765 } while (ret == -1 && errno == EINTR);
1769 *error = WSAGetLastError ();
1771 *error = errno_to_WSA (errno, __func__);
1783 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1784 socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
1785 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1788 for (i = 0; i < count && ret > 0; i++) {
1791 obj = mono_array_get (*sockets, MonoObject *, i);
1798 pfd = &pfds [i - mode];
1799 if (pfd->revents == 0)
1803 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1804 mono_array_setref (socks, idx++, obj);
1805 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1806 mono_array_setref (socks, idx++, obj);
1807 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1808 mono_array_setref (socks, idx++, obj);
1816 static MonoObject* int_to_object (MonoDomain *domain, int val)
1818 return mono_value_box (domain, mono_get_int32_class (), &val);
1822 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1828 socklen_t valsize=sizeof(val);
1829 struct linger linger;
1830 socklen_t lingersize=sizeof(linger);
1832 socklen_t time_ms_size = sizeof (time_ms);
1834 # if defined(__OpenBSD__)
1835 struct sockpeercred cred;
1839 socklen_t credsize = sizeof(cred);
1841 MonoDomain *domain=mono_domain_get();
1843 MonoClass *obj_class;
1844 MonoClassField *field;
1846 MONO_ARCH_SAVE_REGS;
1850 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1851 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
1852 system_level = SOL_SOCKET;
1853 system_name = SO_REUSEADDR;
1859 ret = convert_sockopt_level_and_name (level, name, &system_level, &system_name);
1863 *error = WSAENOPROTOOPT;
1867 *obj_val = int_to_object (domain, 0);
1871 /* No need to deal with MulticastOption names here, because
1872 * you cant getsockopt AddMembership or DropMembership (the
1873 * int getsockopt will error, causing an exception)
1876 case SocketOptionName_Linger:
1877 case SocketOptionName_DontLinger:
1878 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1882 case SocketOptionName_SendTimeout:
1883 case SocketOptionName_ReceiveTimeout:
1884 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1888 case SocketOptionName_PeerCred:
1889 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1895 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1899 if(ret==SOCKET_ERROR) {
1900 *error = WSAGetLastError ();
1905 case SocketOptionName_Linger:
1906 /* build a System.Net.Sockets.LingerOption */
1907 obj_class=mono_class_from_name(get_socket_assembly (),
1908 "System.Net.Sockets",
1910 obj=mono_object_new(domain, obj_class);
1912 /* Locate and set the fields "bool enabled" and "int
1915 field=mono_class_get_field_from_name(obj_class, "enabled");
1916 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1918 field=mono_class_get_field_from_name(obj_class, "seconds");
1919 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1923 case SocketOptionName_DontLinger:
1924 /* construct a bool int in val - true if linger is off */
1925 obj = int_to_object (domain, !linger.l_onoff);
1928 case SocketOptionName_SendTimeout:
1929 case SocketOptionName_ReceiveTimeout:
1930 obj = int_to_object (domain, time_ms);
1934 case SocketOptionName_PeerCred:
1936 /* build a Mono.Posix.PeerCred+PeerCredData if
1939 static MonoImage *mono_posix_image = NULL;
1940 MonoPeerCredData *cred_data;
1942 if (mono_posix_image == NULL) {
1943 mono_posix_image=mono_image_loaded ("Mono.Posix");
1944 if (!mono_posix_image) {
1945 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1947 *error = WSAENOPROTOOPT;
1950 mono_posix_image = mono_assembly_get_image (sa);
1955 obj_class = mono_class_from_name(mono_posix_image,
1958 obj = mono_object_new(domain, obj_class);
1959 cred_data = (MonoPeerCredData *)obj;
1960 cred_data->pid = cred.pid;
1961 cred_data->uid = cred.uid;
1962 cred_data->gid = cred.gid;
1968 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1969 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse)
1972 obj = int_to_object (domain, val);
1977 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1985 MONO_ARCH_SAVE_REGS;
1989 ret=convert_sockopt_level_and_name(level, name, &system_level,
1992 *error = WSAENOPROTOOPT;
1998 valsize=mono_array_length(*byte_val);
1999 buf=mono_array_addr(*byte_val, guchar, 0);
2001 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
2002 if(ret==SOCKET_ERROR) {
2003 *error = WSAGetLastError ();
2007 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2008 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
2010 struct in_addr inaddr;
2011 MonoClassField *field;
2013 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
2015 /* No idea why .net uses a 64bit type to hold a 32bit value...
2017 * Internal value of IPAddess is in little-endian order
2019 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
2025 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
2027 struct in6_addr in6addr;
2028 MonoClassField *field;
2032 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
2033 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
2035 /* Solaris has only the 8 bit version. */
2037 for(i=0; i<8; i++) {
2038 guint16 s = mono_array_get (data, guint16, i);
2039 in6addr.s6_addr[2 * i + 1] = (s >> 8) & 0xff;
2040 in6addr.s6_addr[2 * i] = s & 0xff;
2044 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
2048 #endif /* AF_INET6 */
2051 #if defined(__APPLE__)
2053 #if defined(HAVE_GETIFADDRS) && defined(HAVE_IF_NAMETOINDEX)
2055 get_local_interface_id (int family)
2057 struct ifaddrs *ifap = NULL, *ptr;
2060 if (getifaddrs (&ifap)) {
2064 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2065 if (!ptr->ifa_addr || !ptr->ifa_name)
2067 if (ptr->ifa_addr->sa_family != family)
2069 if ((ptr->ifa_flags & IFF_LOOPBACK) != 0)
2071 if ((ptr->ifa_flags & IFF_MULTICAST) == 0)
2074 idx = if_nametoindex (ptr->ifa_name);
2083 get_local_interface_id (int family)
2089 #endif /* __APPLE__ */
2091 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)
2093 struct linger linger;
2103 #ifdef HAVE_SOL_IPV6
2104 sol_ipv6 = SOL_IPV6;
2107 struct protoent *pent;
2108 pent = getprotobyname ("ipv6");
2109 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2117 struct protoent *pent;
2118 pent = getprotobyname ("ip");
2119 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2122 #endif /* AF_INET6 */
2124 MONO_ARCH_SAVE_REGS;
2126 ret=convert_sockopt_level_and_name(level, name, &system_level,
2129 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2130 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
2131 system_name = SO_REUSEADDR;
2132 int_val = int_val ? 0 : 1;
2138 *error = WSAENOPROTOOPT;
2145 /* Only one of obj_val, byte_val or int_val has data */
2147 MonoClassField *field;
2151 case SocketOptionName_Linger:
2152 /* Dig out "bool enabled" and "int seconds"
2155 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2156 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2157 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2158 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2160 valsize=sizeof(linger);
2161 ret = _wapi_setsockopt (sock, system_level,
2162 system_name, &linger, valsize);
2164 case SocketOptionName_AddMembership:
2165 case SocketOptionName_DropMembership:
2166 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2168 MonoObject *address = NULL;
2171 if(system_level == sol_ipv6) {
2172 struct ipv6_mreq mreq6;
2177 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2178 address = *(gpointer *)(((char *)obj_val) + field->offset);
2181 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2184 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2185 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2187 #if defined(__APPLE__)
2191 * Mac OS Lion doesn't allow ipv6mr_interface = 0.
2193 * Tests on Windows and Linux show that the multicast group is only
2194 * joined on one NIC when interface = 0, so we simply use the interface
2195 * id from the first non-loopback interface (this is also what
2196 * Dns.GetHostName (string.Empty) would return).
2198 if (!mreq6.ipv6mr_interface)
2199 mreq6.ipv6mr_interface = get_local_interface_id (AF_INET6);
2202 ret = _wapi_setsockopt (sock, system_level,
2203 system_name, &mreq6,
2205 } else if(system_level == sol_ip)
2206 #endif /* AF_INET6 */
2208 #ifdef HAVE_STRUCT_IP_MREQN
2209 struct ip_mreqn mreq = {{0}};
2211 struct ip_mreq mreq = {{0}};
2212 #endif /* HAVE_STRUCT_IP_MREQN */
2214 /* pain! MulticastOption holds two IPAddress
2215 * members, so I have to dig the value out of
2218 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2219 address = *(gpointer *)(((char *)obj_val) + field->offset);
2221 /* address might not be defined and if so, set the address to ADDR_ANY.
2224 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2227 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2228 address = *(gpointer *)(((char *)obj_val) + field->offset);
2230 #ifdef HAVE_STRUCT_IP_MREQN
2232 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2236 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2238 #endif /* HAVE_STRUCT_IP_MREQN */
2240 ret = _wapi_setsockopt (sock, system_level,
2246 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2248 /* Cause an exception to be thrown */
2252 } else if (byte_val!=NULL) {
2253 int valsize = mono_array_length (byte_val);
2254 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2257 case SocketOptionName_DontLinger:
2259 linger.l_onoff = (*buf) ? 0 : 1;
2260 linger.l_linger = 0;
2261 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2267 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2271 /* ReceiveTimeout/SendTimeout get here */
2273 case SocketOptionName_DontLinger:
2274 linger.l_onoff = !int_val;
2275 linger.l_linger = 0;
2276 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2278 case SocketOptionName_DontFragment:
2279 #ifdef HAVE_IP_MTU_DISCOVER
2280 /* Fiddle with the value slightly if we're
2284 int_val = IP_PMTUDISC_DO;
2290 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2294 if(ret==SOCKET_ERROR) {
2295 *error = WSAGetLastError ();
2299 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2305 MONO_ARCH_SAVE_REGS;
2309 /* Currently, the values for how (recv=0, send=1, both=2) match
2312 ret = _wapi_shutdown (sock, how);
2313 if(ret==SOCKET_ERROR) {
2314 *error = WSAGetLastError ();
2319 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2321 MonoArray *output, gint32 *error)
2323 glong output_bytes = 0;
2324 gchar *i_buffer, *o_buffer;
2328 MONO_ARCH_SAVE_REGS;
2332 if ((guint32)code == FIONBIO) {
2333 /* Invalid command. Must use Socket.Blocking */
2337 if (input == NULL) {
2341 i_buffer = mono_array_addr (input, gchar, 0);
2342 i_len = mono_array_length (input);
2345 if (output == NULL) {
2349 o_buffer = mono_array_addr (output, gchar, 0);
2350 o_len = mono_array_length (output);
2353 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2354 if (ret == SOCKET_ERROR) {
2355 *error = WSAGetLastError ();
2359 return (gint) output_bytes;
2362 #ifdef HAVE_SIOCGIFCONF
2364 is_loopback (int family, void *ad)
2366 char *ptr = (char *) ad;
2368 if (family == AF_INET) {
2369 return (ptr [0] == 127);
2373 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2380 get_local_ips (int family, int *nips)
2382 int addr_size, offset, fd, i, count;
2383 int max_ifaces = 50; /* 50 interfaces should be enough... */
2386 struct ifreq iflags;
2387 char *result, *tmp_ptr;
2388 gboolean ignore_loopback = FALSE;
2391 if (family == AF_INET) {
2392 addr_size = sizeof (struct in_addr);
2393 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2395 } else if (family == AF_INET6) {
2396 addr_size = sizeof (struct in6_addr);
2397 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2403 fd = socket (family, SOCK_STREAM, 0);
2405 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2406 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2407 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2409 g_free (ifc.ifc_buf);
2413 count = ifc.ifc_len / sizeof (struct ifreq);
2416 g_free (ifc.ifc_buf);
2421 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2422 strcpy (iflags.ifr_name, ifr->ifr_name);
2423 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2427 if ((iflags.ifr_flags & IFF_UP) == 0) {
2428 ifr->ifr_name [0] = '\0';
2432 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2433 ignore_loopback = TRUE;
2438 result = g_malloc (addr_size * count);
2440 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2441 if (ifr->ifr_name [0] == '\0') {
2446 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2451 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2452 tmp_ptr += addr_size;
2455 g_free (ifc.ifc_buf);
2458 #elif defined(HAVE_GETIFADDRS)
2460 is_loopback (int family, void *ad)
2462 char *ptr = (char *) ad;
2464 if (family == AF_INET) {
2465 return (ptr [0] == 127);
2469 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2476 get_local_ips (int family, int *nips)
2478 struct ifaddrs *ifap = NULL, *ptr;
2479 int addr_size, offset, count, i;
2480 char *result, *tmp_ptr;
2483 if (family == AF_INET) {
2484 addr_size = sizeof (struct in_addr);
2485 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2487 } else if (family == AF_INET6) {
2488 addr_size = sizeof (struct in6_addr);
2489 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2495 if (getifaddrs (&ifap)) {
2500 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2503 if (ptr->ifa_addr->sa_family != family)
2505 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2510 result = g_malloc (addr_size * count);
2512 for (i = 0, ptr = ifap; ptr; ptr = ptr->ifa_next) {
2515 if (ptr->ifa_addr->sa_family != family)
2517 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2520 memcpy (tmp_ptr, ((char *) ptr->ifa_addr) + offset, addr_size);
2521 tmp_ptr += addr_size;
2530 get_local_ips (int family, int *nips)
2536 #endif /* HAVE_SIOCGIFCONF */
2539 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2540 MonoArray **h_aliases,
2541 MonoArray **h_addr_list,
2542 gboolean add_local_ips)
2544 MonoDomain *domain = mono_domain_get ();
2546 struct in_addr *local_in = NULL;
2550 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2554 *h_name=mono_string_new(domain, he->h_name);
2556 while(he->h_aliases[i]!=NULL) {
2560 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2562 while(he->h_aliases[i]!=NULL) {
2565 alias=mono_string_new(domain, he->h_aliases[i]);
2566 mono_array_setref (*h_aliases, i, alias);
2569 } else if (!add_local_ips) {
2573 if (add_local_ips) {
2574 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2576 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2577 for (i = 0; i < nlocal_in; i++) {
2578 MonoString *addr_string;
2579 char addr [16], *ptr;
2581 ptr = (char *) &local_in [i];
2582 g_snprintf(addr, 16, "%u.%u.%u.%u",
2583 (unsigned char) ptr [0],
2584 (unsigned char) ptr [1],
2585 (unsigned char) ptr [2],
2586 (unsigned char) ptr [3]);
2588 addr_string = mono_string_new (domain, addr);
2589 mono_array_setref (*h_addr_list, i, addr_string);
2594 } else if (he == NULL) {
2595 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2596 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2597 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2602 if (nlocal_in == 0 && he != NULL) {
2604 while (he->h_addr_list[i]!=NULL) {
2608 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2610 while(he->h_addr_list[i]!=NULL) {
2611 MonoString *addr_string;
2614 g_snprintf(addr, 16, "%u.%u.%u.%u",
2615 (unsigned char)he->h_addr_list[i][0],
2616 (unsigned char)he->h_addr_list[i][1],
2617 (unsigned char)he->h_addr_list[i][2],
2618 (unsigned char)he->h_addr_list[i][3]);
2620 addr_string=mono_string_new(domain, addr);
2621 mono_array_setref (*h_addr_list, i, addr_string);
2629 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2630 MonoArray **h_aliases,
2631 MonoArray **h_addr_list)
2633 MonoDomain *domain = mono_domain_get ();
2635 *h_name=mono_string_new(domain, addr);
2636 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2637 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2638 mono_array_setref (*h_addr_list, 0, *h_name);
2644 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2645 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2646 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2648 MonoDomain *domain = mono_domain_get ();
2649 int i, host_count, host_index, family_hint;
2650 struct in_addr *local_in = NULL;
2652 struct in6_addr *local_in6 = NULL;
2654 gboolean from_local = FALSE;
2656 family_hint = get_family_hint ();
2659 * Check if address length and family are correct
2661 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2665 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2670 * Get the aliases and host name from he1 or he2 whichever is
2671 * not null, if he1 is not null then take aliases from he1
2673 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2674 family_hint == PF_INET)) {
2675 *h_name=mono_string_new (domain, he1->h_name);
2678 while(he1->h_aliases[i]!=NULL) {
2682 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2685 while(he1->h_aliases[i]!=NULL) {
2688 alias=mono_string_new (domain, he1->h_aliases[i]);
2689 mono_array_setref (*h_aliases, i, alias);
2692 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2693 family_hint == PF_INET6)) {
2694 *h_name=mono_string_new (domain, he2->h_name);
2697 while(he2->h_aliases [i] != NULL) {
2701 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2704 while(he2->h_aliases[i]!=NULL) {
2707 alias=mono_string_new (domain, he2->h_aliases[i]);
2708 mono_array_setref (*h_aliases, i, alias);
2711 } else if (!add_local_ips) {
2716 * Count the number of addresses in he1 + he2
2719 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2720 family_hint == PF_INET)) {
2722 while(he1->h_addr_list[i]!=NULL) {
2728 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2729 family_hint == PF_INET6)) {
2731 while(he2->h_addr_list[i]!=NULL) {
2741 if (add_local_ips) {
2742 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2743 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2745 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2746 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2748 if (nlocal_in || nlocal_in6) {
2750 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2751 nlocal_in + nlocal_in6);
2755 for (n = 0; n < nlocal_in6; n++) {
2756 MonoString *addr_string;
2758 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2760 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2763 addr_string = mono_string_new (domain, addr);
2764 mono_array_setref (*h_addr_list, host_index, addr_string);
2772 for (n = 0; n < nlocal_in; n++) {
2773 MonoString *addr_string;
2775 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2777 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2780 addr_string = mono_string_new (domain, addr);
2781 mono_array_setref (*h_addr_list, host_index, addr_string);
2789 } else if (he1 == NULL && he2 == NULL) {
2790 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2791 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2792 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2802 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2804 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2805 family_hint == PF_INET6)) {
2807 while(he2->h_addr_list[i] != NULL) {
2808 MonoString *addr_string;
2810 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2812 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2816 addr_string = mono_string_new (domain, addr);
2817 mono_array_setref (*h_addr_list, host_index, addr_string);
2824 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2825 family_hint == PF_INET)) {
2827 while(he1->h_addr_list[i] != NULL) {
2828 MonoString *addr_string;
2830 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2832 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2836 addr_string=mono_string_new (domain, addr);
2837 mono_array_setref (*h_addr_list, host_index, addr_string);
2848 #if defined(AF_INET6)
2850 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2851 MonoArray **h_aliases,
2852 MonoArray **h_addr_list,
2853 gboolean add_local_ips)
2856 struct addrinfo *ai = NULL;
2857 struct in_addr *local_in = NULL;
2859 struct in6_addr *local_in6 = NULL;
2863 MonoDomain *domain = mono_domain_get ();
2866 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2867 if (add_local_ips) {
2868 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2869 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2870 if (nlocal_in || nlocal_in6) {
2871 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2873 MonoString *addr_string;
2877 for (i = 0; i < nlocal_in; i++) {
2878 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2879 addr_string = mono_string_new (domain, addr);
2880 mono_array_setref (*h_addr_list, addr_index, addr_string);
2886 MonoString *addr_string;
2891 for (i = 0; i < nlocal_in6; i++) {
2892 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2894 addr_string = mono_string_new (domain, addr);
2895 mono_array_setref (*h_addr_list, addr_index, addr_string);
2904 freeaddrinfo (info);
2913 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2914 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2920 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2922 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2923 MonoString *addr_string;
2925 char buffer [48]; /* Max. size for IPv6 */
2927 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2931 if(ai->ai_family == PF_INET) {
2932 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2934 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2938 addr_string=mono_string_new(domain, buffer);
2940 addr_string=mono_string_new(domain, "");
2943 mono_array_setref (*h_addr_list, addr_index, addr_string);
2947 if (ai->ai_canonname != NULL) {
2948 *h_name=mono_string_new(domain, ai->ai_canonname);
2950 *h_name=mono_string_new(domain, buffer);
2966 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2968 gboolean add_local_ips = FALSE;
2969 #ifdef HAVE_SIOCGIFCONF
2970 gchar this_hostname [256];
2972 #if !defined(HAVE_GETHOSTBYNAME2_R)
2973 struct addrinfo *info = NULL, hints;
2976 MONO_ARCH_SAVE_REGS;
2978 hostname=mono_string_to_utf8 (host);
2979 if (*hostname == '\0') {
2980 add_local_ips = TRUE;
2983 #ifdef HAVE_SIOCGIFCONF
2984 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2985 if (!strcmp (hostname, this_hostname)) {
2986 add_local_ips = TRUE;
2992 memset(&hints, 0, sizeof(hints));
2993 hints.ai_family = get_family_hint ();
2994 hints.ai_socktype = SOCK_STREAM;
2995 hints.ai_flags = AI_CANONNAME;
2997 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
3003 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
3005 struct hostent he1,*hp1, he2, *hp2;
3006 int buffer_size1, buffer_size2;
3007 char *buffer1, *buffer2;
3009 gboolean return_value;
3012 MONO_ARCH_SAVE_REGS;
3014 hostname=mono_string_to_utf8 (host);
3015 if (*hostname == '\0')
3016 add_local_ips = TRUE;
3018 #ifdef HAVE_SIOCGIFCONF
3019 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3020 if (!strcmp (hostname, this_hostname))
3021 add_local_ips = TRUE;
3027 buffer1 = g_malloc0(buffer_size1);
3028 buffer2 = g_malloc0(buffer_size2);
3032 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
3033 &hp1, &herr) == ERANGE) {
3035 buffer1 = g_realloc(buffer1, buffer_size1);
3038 if (*hostname && hp1 == NULL)
3040 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
3041 buffer_size2, &hp2, &herr) == ERANGE) {
3043 buffer2 = g_realloc(buffer2, buffer_size2);
3047 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
3048 h_addr_list, add_local_ips);
3054 return(return_value);
3055 #endif /* HAVE_GETHOSTBYNAME2_R */
3057 #else /* AF_INET6 */
3058 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3062 gboolean add_local_ips = FALSE;
3063 #ifdef HAVE_SIOCGIFCONF
3064 gchar this_hostname [256];
3067 MONO_ARCH_SAVE_REGS;
3069 hostname=mono_string_to_utf8(host);
3070 if (*hostname == '\0')
3071 add_local_ips = TRUE;
3072 #ifdef HAVE_SIOCGIFCONF
3073 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3074 if (!strcmp (hostname, this_hostname))
3075 add_local_ips = TRUE;
3082 he = _wapi_gethostbyname (hostname);
3084 he = _wapi_gethostbyname (hostname);
3088 if (*hostname && he==NULL)
3091 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
3093 #endif /* AF_INET6 */
3095 #ifndef HAVE_INET_PTON
3097 inet_pton (int family, const char *address, void *inaddrp)
3099 if (family == AF_INET) {
3100 #ifdef HAVE_INET_ATON
3101 struct in_addr inaddr;
3103 if (!inet_aton (address, &inaddr))
3106 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
3109 /* assume the system has inet_addr(), if it doesn't
3110 have that we're pretty much screwed... */
3113 if (!strcmp (address, "255.255.255.255")) {
3114 /* special-case hack */
3115 inaddr = 0xffffffff;
3117 inaddr = inet_addr (address);
3119 #define INADDR_NONE ((in_addr_t) -1)
3121 if (inaddr == INADDR_NONE)
3125 memcpy (inaddrp, &inaddr, sizeof (guint32));
3127 #endif /* HAVE_INET_ATON */
3132 #endif /* !HAVE_INET_PTON */
3134 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3139 struct sockaddr_in saddr;
3140 struct sockaddr_in6 saddr6;
3141 struct addrinfo *info = NULL, hints;
3143 char hostname[NI_MAXHOST] = {0};
3146 struct in_addr inaddr;
3151 address = mono_string_to_utf8 (addr);
3154 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
3155 /* Maybe an ipv6 address */
3156 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
3162 saddr6.sin6_family = AF_INET6;
3167 saddr.sin_family = AF_INET;
3171 if(family == AF_INET) {
3172 #if HAVE_SOCKADDR_IN_SIN_LEN
3173 saddr.sin_len = sizeof (saddr);
3175 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
3176 hostname, sizeof(hostname), NULL, 0,
3180 } else if(family == AF_INET6) {
3181 #if HAVE_SOCKADDR_IN6_SIN_LEN
3182 saddr6.sin6_len = sizeof (saddr6);
3184 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
3185 hostname, sizeof(hostname), NULL, 0,
3191 memset (&hints, 0, sizeof(hints));
3192 hints.ai_family = get_family_hint ();
3193 hints.ai_socktype = SOCK_STREAM;
3194 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
3196 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
3200 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
3202 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
3207 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
3208 ret = ipaddr_to_IPHostEntry (address, h_name, h_aliases, h_addr_list);
3210 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
3211 h_addr_list, FALSE);
3219 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
3221 gchar hostname[256];
3224 MONO_ARCH_SAVE_REGS;
3226 ret = gethostname (hostname, sizeof (hostname));
3231 *h_name=mono_string_new(mono_domain_get (), hostname);
3237 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
3241 TRANSMIT_FILE_BUFFERS buffers;
3243 MONO_ARCH_SAVE_REGS;
3245 if (filename == NULL)
3248 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
3249 if (file == INVALID_HANDLE_VALUE) {
3250 SetLastError (error);
3254 memset (&buffers, 0, sizeof (buffers));
3255 if (pre_buffer != NULL) {
3256 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
3257 buffers.HeadLength = mono_array_length (pre_buffer);
3259 if (post_buffer != NULL) {
3260 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
3261 buffers.TailLength = mono_array_length (post_buffer);
3264 if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
3273 void mono_network_init(void)
3278 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3280 g_error("%s: Couldn't initialise networking", __func__);
3284 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3285 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3288 void mono_network_cleanup(void)
3294 icall_cancel_blocking_socket_operation (MonoThread *thread)
3296 MonoInternalThread *internal = thread->internal_thread;
3298 if (mono_thread_info_new_interrupt_enabled ()) {
3299 mono_thread_info_abort_socket_syscall_for_close ((MonoNativeThreadId)(gsize)internal->tid);
3302 internal->ignore_next_signal = TRUE;
3303 mono_thread_kill (internal, mono_thread_get_abort_signal ());
3308 #endif /* #ifndef DISABLE_SOCKETS */