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>
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
35 #include <arpa/inet.h>
38 #include <mono/metadata/object.h>
39 #include <mono/io-layer/io-layer.h>
40 #include <mono/metadata/socket-io.h>
41 #include <mono/metadata/exception.h>
42 #include <mono/metadata/assembly.h>
43 #include <mono/metadata/appdomain.h>
44 #include <mono/metadata/file-io.h>
45 #include <mono/metadata/threads.h>
46 #include <mono/metadata/threads-types.h>
47 #include <mono/utils/mono-poll.h>
48 /* FIXME change this code to not mess so much with the internals */
49 #include <mono/metadata/class-internals.h>
50 #include <mono/metadata/threadpool-internals.h>
51 #include <mono/metadata/domain-internals.h>
52 #include <mono/utils/mono-threads.h>
55 #ifdef HAVE_SYS_TIME_H
58 #ifdef HAVE_SYS_IOCTL_H
59 #include <sys/ioctl.h>
68 #ifdef HAVE_SYS_FILIO_H
69 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
71 #ifdef HAVE_SYS_SOCKIO_H
72 #include <sys/sockio.h> /* defines SIOCATMARK */
78 #ifdef HAVE_GETIFADDRS
79 // <net/if.h> must be included before <ifaddrs.h>
83 #include "mono/io-layer/socket-wrappers.h"
85 #if defined(HOST_WIN32)
86 /* This is a kludge to make this file build under cygwin:
87 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
88 * prototypes for some but not all required functions (notably
89 * inet_ntop() is missing), but the libws2_32 library is missing the
90 * actual implementations of these functions.
95 #ifdef PLATFORM_ANDROID
96 // not yet actually implemented...
100 #define LOGDEBUG(...)
101 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
104 * Some older versions of libc provide IPV6 support without defining the AI_ADDRCONFIG
105 * flag for getaddrinfo.
107 #ifndef AI_ADDRCONFIG
108 #define AI_ADDRCONFIG 0
111 static gint32 convert_family(MonoAddressFamily mono_family)
115 switch(mono_family) {
116 case AddressFamily_Unknown:
117 case AddressFamily_ImpLink:
118 case AddressFamily_Pup:
119 case AddressFamily_Chaos:
120 case AddressFamily_Iso:
121 case AddressFamily_Ecma:
122 case AddressFamily_DataKit:
123 case AddressFamily_Ccitt:
124 case AddressFamily_DataLink:
125 case AddressFamily_Lat:
126 case AddressFamily_HyperChannel:
127 case AddressFamily_NetBios:
128 case AddressFamily_VoiceView:
129 case AddressFamily_FireFox:
130 case AddressFamily_Banyan:
131 case AddressFamily_Atm:
132 case AddressFamily_Cluster:
133 case AddressFamily_Ieee12844:
134 case AddressFamily_NetworkDesigners:
135 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
138 case AddressFamily_Unspecified:
142 case AddressFamily_Unix:
146 case AddressFamily_InterNetwork:
150 case AddressFamily_Ipx:
156 case AddressFamily_Sna:
162 case AddressFamily_DecNet:
168 case AddressFamily_AppleTalk:
172 case AddressFamily_InterNetworkV6:
177 case AddressFamily_Irda:
183 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
189 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
191 MonoAddressFamily family=AddressFamily_Unknown;
195 family=AddressFamily_Unspecified;
199 family=AddressFamily_Unix;
203 family=AddressFamily_InterNetwork;
208 family=AddressFamily_Ipx;
214 family=AddressFamily_Sna;
220 family=AddressFamily_DecNet;
225 family=AddressFamily_AppleTalk;
230 family=AddressFamily_InterNetworkV6;
236 family=AddressFamily_Irda;
240 g_warning("unknown address family 0x%x", af_family);
246 static gint32 convert_type(MonoSocketType mono_type)
251 case SocketType_Stream:
255 case SocketType_Dgram:
269 case SocketType_Seqpacket:
273 case SocketType_Unknown:
274 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
278 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
284 static gint32 convert_proto(MonoProtocolType mono_proto)
289 case ProtocolType_IP:
290 case ProtocolType_IPv6:
291 case ProtocolType_Icmp:
292 case ProtocolType_Igmp:
293 case ProtocolType_Ggp:
294 case ProtocolType_Tcp:
295 case ProtocolType_Pup:
296 case ProtocolType_Udp:
297 case ProtocolType_Idp:
298 /* These protocols are known (on my system at least) */
302 case ProtocolType_ND:
303 case ProtocolType_Raw:
304 case ProtocolType_Ipx:
305 case ProtocolType_Spx:
306 case ProtocolType_SpxII:
307 case ProtocolType_Unknown:
308 /* These protocols arent */
309 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
319 /* Convert MonoSocketFlags */
320 static gint32 convert_socketflags (gint32 sflags)
325 /* SocketFlags.None */
328 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
329 SocketFlags_DontRoute | SocketFlags_Partial))
330 /* Contains invalid flag values */
333 if (sflags & SocketFlags_OutOfBand)
335 if (sflags & SocketFlags_Peek)
337 if (sflags & SocketFlags_DontRoute)
338 flags |= MSG_DONTROUTE;
340 /* Ignore Partial - see bug 349688. Don't return -1, because
341 * according to the comment in that bug ms runtime doesn't for
342 * UDP sockets (this means we will silently ignore it for TCP
346 if (sflags & SocketFlags_Partial)
350 /* Don't do anything for MaxIOVectorLength */
351 if (sflags & SocketFlags_MaxIOVectorLength)
359 * 0 on success (mapped mono_level and mono_name to system_level and system_name
361 * -2 on non-fatal error (ie, must ignore)
363 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
364 MonoSocketOptionName mono_name,
368 switch (mono_level) {
369 case SocketOptionLevel_Socket:
370 *system_level = SOL_SOCKET;
373 case SocketOptionName_DontLinger:
374 /* This is SO_LINGER, because the setsockopt
375 * internal call maps DontLinger to SO_LINGER
378 *system_name = SO_LINGER;
380 case SocketOptionName_Debug:
381 *system_name = SO_DEBUG;
384 case SocketOptionName_AcceptConnection:
385 *system_name = SO_ACCEPTCONN;
388 case SocketOptionName_ReuseAddress:
389 *system_name = SO_REUSEADDR;
391 case SocketOptionName_KeepAlive:
392 *system_name = SO_KEEPALIVE;
394 case SocketOptionName_DontRoute:
395 *system_name = SO_DONTROUTE;
397 case SocketOptionName_Broadcast:
398 *system_name = SO_BROADCAST;
400 case SocketOptionName_Linger:
401 *system_name = SO_LINGER;
403 case SocketOptionName_OutOfBandInline:
404 *system_name = SO_OOBINLINE;
406 case SocketOptionName_SendBuffer:
407 *system_name = SO_SNDBUF;
409 case SocketOptionName_ReceiveBuffer:
410 *system_name = SO_RCVBUF;
412 case SocketOptionName_SendLowWater:
413 *system_name = SO_SNDLOWAT;
415 case SocketOptionName_ReceiveLowWater:
416 *system_name = SO_RCVLOWAT;
418 case SocketOptionName_SendTimeout:
419 *system_name = SO_SNDTIMEO;
421 case SocketOptionName_ReceiveTimeout:
422 *system_name = SO_RCVTIMEO;
424 case SocketOptionName_Error:
425 *system_name = SO_ERROR;
427 case SocketOptionName_Type:
428 *system_name = SO_TYPE;
431 case SocketOptionName_PeerCred:
432 *system_name = SO_PEERCRED;
435 case SocketOptionName_ExclusiveAddressUse:
436 #ifdef SO_EXCLUSIVEADDRUSE
437 *system_name = SO_EXCLUSIVEADDRUSE;
440 case SocketOptionName_UseLoopback:
441 #ifdef SO_USELOOPBACK
442 *system_name = SO_USELOOPBACK;
445 case SocketOptionName_MaxConnections:
447 *system_name = SO_MAXCONN;
449 #elif defined(SOMAXCONN)
450 *system_name = SOMAXCONN;
454 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
459 case SocketOptionLevel_IP:
461 *system_level = SOL_IP;
464 static int cached = 0;
468 struct protoent *pent;
470 pent = getprotobyname ("IP");
471 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
475 *system_level = proto;
477 #endif /* HAVE_SOL_IP */
480 case SocketOptionName_IPOptions:
481 *system_name = IP_OPTIONS;
484 case SocketOptionName_HeaderIncluded:
485 *system_name = IP_HDRINCL;
489 case SocketOptionName_TypeOfService:
490 *system_name = IP_TOS;
494 case SocketOptionName_IpTimeToLive:
495 *system_name = IP_TTL;
498 case SocketOptionName_MulticastInterface:
499 *system_name = IP_MULTICAST_IF;
501 case SocketOptionName_MulticastTimeToLive:
502 *system_name = IP_MULTICAST_TTL;
504 case SocketOptionName_MulticastLoopback:
505 *system_name = IP_MULTICAST_LOOP;
507 case SocketOptionName_AddMembership:
508 *system_name = IP_ADD_MEMBERSHIP;
510 case SocketOptionName_DropMembership:
511 *system_name = IP_DROP_MEMBERSHIP;
513 #ifdef HAVE_IP_PKTINFO
514 case SocketOptionName_PacketInformation:
515 *system_name = IP_PKTINFO;
517 #endif /* HAVE_IP_PKTINFO */
519 case SocketOptionName_DontFragment:
520 #ifdef HAVE_IP_DONTFRAGMENT
521 *system_name = IP_DONTFRAGMENT;
523 #elif defined HAVE_IP_MTU_DISCOVER
524 /* Not quite the same */
525 *system_name = IP_MTU_DISCOVER;
528 /* If the flag is not available on this system, we can ignore this error */
530 #endif /* HAVE_IP_DONTFRAGMENT */
531 case SocketOptionName_AddSourceMembership:
532 case SocketOptionName_DropSourceMembership:
533 case SocketOptionName_BlockSource:
534 case SocketOptionName_UnblockSource:
535 /* Can't figure out how to map these, so fall
539 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
545 case SocketOptionLevel_IPv6:
547 *system_level = SOL_IPV6;
550 static int cached = 0;
554 struct protoent *pent;
556 pent = getprotobyname ("IPV6");
557 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
561 *system_level = proto;
563 #endif /* HAVE_SOL_IPV6 */
566 case SocketOptionName_IpTimeToLive:
567 case SocketOptionName_HopLimit:
568 *system_name = IPV6_UNICAST_HOPS;
570 case SocketOptionName_MulticastInterface:
571 *system_name = IPV6_MULTICAST_IF;
573 case SocketOptionName_MulticastTimeToLive:
574 *system_name = IPV6_MULTICAST_HOPS;
576 case SocketOptionName_MulticastLoopback:
577 *system_name = IPV6_MULTICAST_LOOP;
579 case SocketOptionName_AddMembership:
580 *system_name = IPV6_JOIN_GROUP;
582 case SocketOptionName_DropMembership:
583 *system_name = IPV6_LEAVE_GROUP;
585 case SocketOptionName_PacketInformation:
586 #ifdef HAVE_IPV6_PKTINFO
587 *system_name = IPV6_PKTINFO;
590 case SocketOptionName_HeaderIncluded:
591 case SocketOptionName_IPOptions:
592 case SocketOptionName_TypeOfService:
593 case SocketOptionName_DontFragment:
594 case SocketOptionName_AddSourceMembership:
595 case SocketOptionName_DropSourceMembership:
596 case SocketOptionName_BlockSource:
597 case SocketOptionName_UnblockSource:
598 /* Can't figure out how to map these, so fall
602 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
606 break; /* SocketOptionLevel_IPv6 */
609 case SocketOptionLevel_Tcp:
611 *system_level = SOL_TCP;
614 static int cached = 0;
618 struct protoent *pent;
620 pent = getprotobyname ("TCP");
621 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
625 *system_level = proto;
627 #endif /* HAVE_SOL_TCP */
630 case SocketOptionName_NoDelay:
631 *system_name = TCP_NODELAY;
634 /* The documentation is talking complete
635 * bollocks here: rfc-1222 is titled
636 * 'Advancing the NSFNET Routing Architecture'
637 * and doesn't mention either of the words
638 * "expedite" or "urgent".
640 case SocketOptionName_BsdUrgent:
641 case SocketOptionName_Expedited:
644 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
649 case SocketOptionLevel_Udp:
650 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
653 case SocketOptionName_NoChecksum:
654 case SocketOptionName_ChecksumCoverage:
656 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
663 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
670 static MonoImage *get_socket_assembly (void)
672 static const char *version = NULL;
673 static gboolean moonlight;
674 MonoDomain *domain = mono_domain_get ();
676 if (version == NULL) {
677 version = mono_get_runtime_info ()->framework_version;
678 moonlight = !strcmp (version, "2.1");
681 if (domain->socket_assembly == NULL) {
682 MonoImage *socket_assembly;
685 socket_assembly = mono_image_loaded ("System.Net");
686 if (!socket_assembly) {
687 MonoAssembly *sa = mono_assembly_open ("System.Net.dll", NULL);
690 g_assert_not_reached ();
692 socket_assembly = mono_assembly_get_image (sa);
696 socket_assembly = mono_image_loaded ("System");
697 if (!socket_assembly) {
698 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
701 g_assert_not_reached ();
703 socket_assembly = mono_assembly_get_image (sa);
708 domain->socket_assembly = socket_assembly;
711 return domain->socket_assembly;
715 static gint32 get_family_hint(void)
717 MonoDomain *domain = mono_domain_get ();
719 if (!domain->inet_family_hint) {
720 MonoClass *socket_class;
721 MonoClassField *ipv6_field, *ipv4_field;
722 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
725 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
726 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
727 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
728 vtable = mono_class_vtable (mono_domain_get (), socket_class);
730 mono_runtime_class_init (vtable);
732 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
733 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
735 mono_domain_lock (domain);
736 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
737 domain->inet_family_hint = 1;
738 } else if (ipv4_enabled == 1) {
739 domain->inet_family_hint = 2;
741 domain->inet_family_hint = 3;
743 mono_domain_unlock (domain);
745 switch (domain->inet_family_hint) {
746 case 1: return PF_UNSPEC;
747 case 2: return PF_INET;
748 case 3: return PF_INET6;
755 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
766 sock_family=convert_family(family);
767 if(sock_family==-1) {
768 *error = WSAEAFNOSUPPORT;
772 sock_proto=convert_proto(proto);
774 *error = WSAEPROTONOSUPPORT;
778 sock_type=convert_type(type);
780 *error = WSAESOCKTNOSUPPORT;
784 sock = _wapi_socket (sock_family, sock_type, sock_proto,
785 NULL, 0, WSA_FLAG_OVERLAPPED);
787 if(sock==INVALID_SOCKET) {
788 *error = WSAGetLastError ();
792 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
793 return (GUINT_TO_POINTER (sock));
797 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
798 return (GUINT_TO_POINTER (sock));
802 return(GUINT_TO_POINTER (sock));
805 /* FIXME: the SOCKET parameter (here and in other functions in this
806 * file) is really an IntPtr which needs to be converted to a guint32.
808 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
813 LOGDEBUG (g_message ("%s: closing 0x%x", __func__, sock));
817 /* Clear any pending work item from this socket if the underlying
818 * polling system does not notify when the socket is closed */
819 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
823 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
827 LOGDEBUG (g_message("%s: returning %d", __func__, WSAGetLastError()));
829 return(WSAGetLastError());
832 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
842 ret=ioctlsocket(sock, FIONREAD, &amount);
843 if(ret==SOCKET_ERROR) {
844 *error = WSAGetLastError ();
851 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
862 * block == TRUE/FALSE means we will block/not block.
863 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
867 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
868 if(ret==SOCKET_ERROR) {
869 *error = WSAGetLastError ();
873 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
884 MonoInternalThread* curthread = mono_thread_internal_current ();
885 curthread->interrupt_on_stop = (gpointer)TRUE;
886 newsock = _wapi_accept (sock, NULL, 0);
887 curthread->interrupt_on_stop = (gpointer)FALSE;
890 newsock = _wapi_accept (sock, NULL, 0);
892 if(newsock==INVALID_SOCKET) {
893 *error = WSAGetLastError ();
897 return(GUINT_TO_POINTER (newsock));
900 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
910 ret = _wapi_listen (sock, backlog);
911 if(ret==SOCKET_ERROR) {
912 *error = WSAGetLastError ();
917 // Check whether it's ::ffff::0:0.
919 is_ipv4_mapped_any (const struct in6_addr *addr)
923 for (i = 0; i < 10; i++) {
924 if (addr->s6_addr [i])
927 if ((addr->s6_addr [10] != 0xff) || (addr->s6_addr [11] != 0xff))
929 for (i = 12; i < 16; i++) {
930 if (addr->s6_addr [i])
937 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
938 int sa_size, gint32 *error)
940 MonoDomain *domain = mono_domain_get ();
941 MonoObject *sockaddr_obj;
943 MonoAddressFamily family;
945 /* Build a System.Net.SocketAddress object instance */
946 if (!domain->sockaddr_class) {
947 domain->sockaddr_class=mono_class_from_name (get_socket_assembly (), "System.Net", "SocketAddress");
948 g_assert (domain->sockaddr_class);
950 sockaddr_obj=mono_object_new(domain, domain->sockaddr_class);
952 /* Locate the SocketAddress data buffer in the object */
953 if (!domain->sockaddr_data_field) {
954 domain->sockaddr_data_field=mono_class_get_field_from_name (domain->sockaddr_class, "data");
955 g_assert (domain->sockaddr_data_field);
958 /* Make sure there is space for the family and size bytes */
960 if (saddr->sa_family == AF_UNIX) {
961 /* sa_len includes the entire sockaddr size, so we don't need the
962 * N bytes (sizeof (unsigned short)) of the family. */
963 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size);
967 /* May be the +2 here is too conservative, as sa_len returns
968 * the length of the entire sockaddr_in/in6, including
969 * sizeof (unsigned short) of the family */
970 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
973 /* The data buffer is laid out as follows:
974 * bytes 0 and 1 are the address family
975 * bytes 2 and 3 are the port info
976 * the rest is the address info
979 family=convert_to_mono_family(saddr->sa_family);
980 if(family==AddressFamily_Unknown) {
981 *error = WSAEAFNOSUPPORT;
985 mono_array_set(data, guint8, 0, family & 0x0FF);
986 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
988 if(saddr->sa_family==AF_INET) {
989 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
990 guint16 port=ntohs(sa_in->sin_port);
991 guint32 address=ntohl(sa_in->sin_addr.s_addr);
994 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
997 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
998 mono_array_set(data, guint8, 3, (port) & 0xff);
999 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
1000 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
1001 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
1002 mono_array_set(data, guint8, 7, (address) & 0xff);
1004 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1006 return(sockaddr_obj);
1008 } else if (saddr->sa_family == AF_INET6) {
1009 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
1012 guint16 port=ntohs(sa_in->sin6_port);
1015 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1018 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
1019 mono_array_set(data, guint8, 3, (port) & 0xff);
1021 if (is_ipv4_mapped_any (&sa_in->sin6_addr)) {
1022 // Map ::ffff:0:0 to :: (bug #5502)
1023 for(i=0; i<16; i++) {
1024 mono_array_set(data, guint8, 8+i, 0);
1027 for(i=0; i<16; i++) {
1028 mono_array_set(data, guint8, 8+i,
1029 sa_in->sin6_addr.s6_addr[i]);
1033 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
1034 mono_array_set(data, guint8, 25,
1035 (sa_in->sin6_scope_id >> 8) & 0xff);
1036 mono_array_set(data, guint8, 26,
1037 (sa_in->sin6_scope_id >> 16) & 0xff);
1038 mono_array_set(data, guint8, 27,
1039 (sa_in->sin6_scope_id >> 24) & 0xff);
1041 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1043 return(sockaddr_obj);
1045 #ifdef HAVE_SYS_UN_H
1046 } else if (saddr->sa_family == AF_UNIX) {
1049 for (i = 0; i < sa_size; i++) {
1050 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
1053 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1055 return sockaddr_obj;
1058 *error = WSAEAFNOSUPPORT;
1064 get_sockaddr_size (int family)
1069 if (family == AF_INET) {
1070 size = sizeof (struct sockaddr_in);
1072 } else if (family == AF_INET6) {
1073 size = sizeof (struct sockaddr_in6);
1075 #ifdef HAVE_SYS_UN_H
1076 } else if (family == AF_UNIX) {
1077 size = sizeof (struct sockaddr_un);
1083 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1090 MONO_ARCH_SAVE_REGS;
1094 salen = get_sockaddr_size (convert_family (af));
1096 *error = WSAEAFNOSUPPORT;
1099 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1100 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1102 if(ret==SOCKET_ERROR) {
1103 *error = WSAGetLastError ();
1109 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)));
1111 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1117 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1124 MONO_ARCH_SAVE_REGS;
1128 salen = get_sockaddr_size (convert_family (af));
1130 *error = WSAEAFNOSUPPORT;
1133 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1134 /* Note: linux returns just 2 for AF_UNIX. Always. */
1135 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1136 if(ret==SOCKET_ERROR) {
1137 *error = WSAGetLastError ();
1143 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)));
1145 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1151 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1155 MonoClassField *field;
1160 /* Dig the SocketAddress data buffer out of the object */
1161 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1162 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1164 /* The data buffer is laid out as follows:
1165 * byte 0 is the address family low byte
1166 * byte 1 is the address family high byte
1168 * bytes 2 and 3 are the port info
1169 * the rest is the address info
1171 * the rest is the file name
1173 len = mono_array_length (data);
1175 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1178 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1179 if (family == AF_INET) {
1180 struct sockaddr_in *sa;
1185 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1188 sa = g_new0 (struct sockaddr_in, 1);
1189 port = (mono_array_get (data, guint8, 2) << 8) +
1190 mono_array_get (data, guint8, 3);
1191 address = (mono_array_get (data, guint8, 4) << 24) +
1192 (mono_array_get (data, guint8, 5) << 16 ) +
1193 (mono_array_get (data, guint8, 6) << 8) +
1194 mono_array_get (data, guint8, 7);
1196 sa->sin_family = family;
1197 sa->sin_addr.s_addr = htonl (address);
1198 sa->sin_port = htons (port);
1200 *sa_size = sizeof(struct sockaddr_in);
1201 return((struct sockaddr *)sa);
1204 } else if (family == AF_INET6) {
1205 struct sockaddr_in6 *sa;
1211 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1214 sa = g_new0 (struct sockaddr_in6, 1);
1215 port = mono_array_get (data, guint8, 3) +
1216 (mono_array_get (data, guint8, 2) << 8);
1217 scopeid = mono_array_get (data, guint8, 24) +
1218 (mono_array_get (data, guint8, 25) << 8) +
1219 (mono_array_get (data, guint8, 26) << 16) +
1220 (mono_array_get (data, guint8, 27) << 24);
1222 sa->sin6_family = family;
1223 sa->sin6_port = htons (port);
1224 sa->sin6_scope_id = scopeid;
1226 for(i=0; i<16; i++) {
1227 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1230 *sa_size = sizeof(struct sockaddr_in6);
1231 return((struct sockaddr *)sa);
1233 #ifdef HAVE_SYS_UN_H
1234 } else if (family == AF_UNIX) {
1235 struct sockaddr_un *sock_un;
1238 /* Need a byte for the '\0' terminator/prefix, and the first
1239 * two bytes hold the SocketAddress family
1241 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1242 mono_raise_exception (mono_get_exception_index_out_of_range ());
1245 sock_un = g_new0 (struct sockaddr_un, 1);
1247 sock_un->sun_family = family;
1248 for (i = 0; i < len - 2; i++) {
1249 sock_un->sun_path [i] = mono_array_get (data, guint8,
1255 return (struct sockaddr *)sock_un;
1258 *error = WSAEAFNOSUPPORT;
1263 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1265 struct sockaddr *sa;
1269 MONO_ARCH_SAVE_REGS;
1273 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1278 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)));
1280 ret = _wapi_bind (sock, sa, sa_size);
1281 if(ret==SOCKET_ERROR) {
1282 *error = WSAGetLastError ();
1295 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1296 gint timeout, gint32 *error)
1298 MonoInternalThread *thread = NULL;
1304 MONO_ARCH_SAVE_REGS;
1306 pfds = g_new0 (mono_pollfd, 1);
1307 pfds[0].fd = GPOINTER_TO_INT (sock);
1308 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1309 (mode == SelectModeWrite) ? MONO_POLLOUT :
1310 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1312 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1313 start = time (NULL);
1317 ret = mono_poll (pfds, 1, timeout);
1318 if (timeout > 0 && ret < 0) {
1320 int sec = time (NULL) - start;
1322 timeout -= sec * 1000;
1330 if (ret == -1 && errno == EINTR) {
1333 if (thread == NULL) {
1334 thread = mono_thread_internal_current ();
1337 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1343 /* Suspend requested? */
1344 mono_thread_interruption_checkpoint ();
1348 } while (ret == -1 && errno == EINTR);
1352 *error = WSAGetLastError ();
1354 *error = errno_to_WSA (errno, __func__);
1369 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1371 struct sockaddr *sa;
1375 MONO_ARCH_SAVE_REGS;
1379 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1384 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)));
1386 ret = _wapi_connect (sock, sa, sa_size);
1387 if(ret==SOCKET_ERROR) {
1388 *error = WSAGetLastError ();
1394 /* These #defines from mswsock.h from wine. Defining them here allows
1395 * us to build this file on a mingw box that doesn't know the magic
1396 * numbers, but still run on a newer windows box that does.
1398 #ifndef WSAID_DISCONNECTEX
1399 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1400 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1403 #ifndef WSAID_TRANSMITFILE
1404 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1405 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1408 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1411 glong output_bytes = 0;
1412 GUID disco_guid = WSAID_DISCONNECTEX;
1413 GUID trans_guid = WSAID_TRANSMITFILE;
1414 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1415 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1418 MONO_ARCH_SAVE_REGS;
1422 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1424 /* I _think_ the extension function pointers need to be looked
1425 * up for each socket. FIXME: check the best way to store
1426 * pointers to functions in managed objects that still works
1427 * on 64bit platforms.
1429 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1430 (void *)&disco_guid, sizeof(GUID),
1431 (void *)&_wapi_disconnectex, sizeof(void *),
1432 &output_bytes, NULL, NULL);
1434 /* make sure that WSAIoctl didn't put crap in the
1437 _wapi_disconnectex = NULL;
1439 /* Look up the TransmitFile extension function pointer
1440 * instead of calling TransmitFile() directly, because
1441 * apparently "Several of the extension functions have
1442 * been available since WinSock 1.1 and are exported
1443 * from MSWsock.dll, however it's not advisable to
1444 * link directly to this dll as this ties you to the
1445 * Microsoft WinSock provider. A provider neutral way
1446 * of accessing these extension functions is to load
1447 * them dynamically via WSAIoctl using the
1448 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1449 * should, theoretically, allow you to access these
1450 * functions from any provider that supports them..."
1451 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1453 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1454 (void *)&trans_guid, sizeof(GUID),
1455 (void *)&_wapi_transmitfile, sizeof(void *),
1456 &output_bytes, NULL, NULL);
1458 _wapi_transmitfile = NULL;
1462 if (_wapi_disconnectex != NULL) {
1463 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1464 } else if (_wapi_transmitfile != NULL) {
1465 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1466 TF_DISCONNECT | TF_REUSE_SOCKET);
1468 *error = ERROR_NOT_SUPPORTED;
1472 if (bret == FALSE) {
1473 *error = WSAGetLastError ();
1477 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1484 MONO_ARCH_SAVE_REGS;
1488 alen = mono_array_length (buffer);
1489 if (offset > alen - count) {
1493 buf=mono_array_addr(buffer, guchar, offset);
1495 recvflags = convert_socketflags (flags);
1496 if (recvflags == -1) {
1497 *error = WSAEOPNOTSUPP;
1503 MonoInternalThread* curthread = mono_thread_internal_current ();
1504 curthread->interrupt_on_stop = (gpointer)TRUE;
1505 ret = _wapi_recv (sock, buf, count, recvflags);
1506 curthread->interrupt_on_stop = (gpointer)FALSE;
1509 ret = _wapi_recv (sock, buf, count, recvflags);
1512 if(ret==SOCKET_ERROR) {
1513 *error = WSAGetLastError ();
1520 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1525 DWORD recvflags = 0;
1527 MONO_ARCH_SAVE_REGS;
1531 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1532 count = mono_array_length (buffers);
1534 recvflags = convert_socketflags (flags);
1535 if (recvflags == -1) {
1536 *error = WSAEOPNOTSUPP;
1540 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1541 if (ret == SOCKET_ERROR) {
1542 *error = WSAGetLastError ();
1549 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1555 struct sockaddr *sa;
1558 MONO_ARCH_SAVE_REGS;
1562 alen = mono_array_length (buffer);
1563 if (offset > alen - count) {
1567 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1572 buf=mono_array_addr(buffer, guchar, offset);
1574 recvflags = convert_socketflags (flags);
1575 if (recvflags == -1) {
1576 *error = WSAEOPNOTSUPP;
1580 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1581 if(ret==SOCKET_ERROR) {
1583 *error = WSAGetLastError ();
1587 /* If we didn't get a socket size, then we're probably a
1588 * connected connection-oriented socket and the stack hasn't
1589 * returned the remote address. All we can do is return null.
1592 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1601 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1608 MONO_ARCH_SAVE_REGS;
1612 alen = mono_array_length (buffer);
1613 if (offset > alen - count) {
1617 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1619 buf=mono_array_addr(buffer, guchar, offset);
1621 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1623 sendflags = convert_socketflags (flags);
1624 if (sendflags == -1) {
1625 *error = WSAEOPNOTSUPP;
1629 ret = _wapi_send (sock, buf, count, sendflags);
1630 if(ret==SOCKET_ERROR) {
1631 *error = WSAGetLastError ();
1638 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1643 DWORD sendflags = 0;
1645 MONO_ARCH_SAVE_REGS;
1649 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1650 count = mono_array_length (buffers);
1652 sendflags = convert_socketflags (flags);
1653 if (sendflags == -1) {
1654 *error = WSAEOPNOTSUPP;
1658 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1659 if (ret == SOCKET_ERROR) {
1660 *error = WSAGetLastError ();
1667 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1673 struct sockaddr *sa;
1676 MONO_ARCH_SAVE_REGS;
1680 alen = mono_array_length (buffer);
1681 if (offset > alen - count) {
1685 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1690 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1692 buf=mono_array_addr(buffer, guchar, offset);
1694 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1696 sendflags = convert_socketflags (flags);
1697 if (sendflags == -1) {
1698 *error = WSAEOPNOTSUPP;
1702 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1703 if(ret==SOCKET_ERROR) {
1704 *error = WSAGetLastError ();
1712 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1715 MonoClassField *field;
1717 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1718 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1723 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1724 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1726 MonoInternalThread *thread = NULL;
1733 MonoClass *sock_arr_class;
1736 uintptr_t socks_size;
1738 MONO_ARCH_SAVE_REGS;
1740 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1741 count = mono_array_length (*sockets);
1742 nfds = count - 3; /* NULL separators */
1743 pfds = g_new0 (mono_pollfd, nfds);
1745 for (i = 0; i < count; i++) {
1746 obj = mono_array_get (*sockets, MonoObject *, i);
1753 /* The socket array was bogus */
1759 pfds [idx].fd = Socket_to_SOCKET (obj);
1760 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1764 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1765 start = time (NULL);
1768 ret = mono_poll (pfds, nfds, timeout);
1769 if (timeout > 0 && ret < 0) {
1771 int sec = time (NULL) - start;
1773 timeout -= sec * 1000;
1779 if (ret == -1 && errno == EINTR) {
1782 thread = mono_thread_internal_current ();
1784 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1791 /* Suspend requested? */
1792 mono_thread_interruption_checkpoint ();
1796 } while (ret == -1 && errno == EINTR);
1800 *error = WSAGetLastError ();
1802 *error = errno_to_WSA (errno, __func__);
1814 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1815 socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
1816 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1819 for (i = 0; i < count && ret > 0; i++) {
1822 obj = mono_array_get (*sockets, MonoObject *, i);
1829 pfd = &pfds [i - mode];
1830 if (pfd->revents == 0)
1834 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1835 mono_array_setref (socks, idx++, obj);
1836 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1837 mono_array_setref (socks, idx++, obj);
1838 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1839 mono_array_setref (socks, idx++, obj);
1847 static MonoObject* int_to_object (MonoDomain *domain, int val)
1849 return mono_value_box (domain, mono_get_int32_class (), &val);
1853 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1859 socklen_t valsize=sizeof(val);
1860 struct linger linger;
1861 socklen_t lingersize=sizeof(linger);
1863 socklen_t time_ms_size = sizeof (time_ms);
1865 # if defined(__OpenBSD__)
1866 struct sockpeercred cred;
1870 socklen_t credsize = sizeof(cred);
1872 MonoDomain *domain=mono_domain_get();
1874 MonoClass *obj_class;
1875 MonoClassField *field;
1877 MONO_ARCH_SAVE_REGS;
1881 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1882 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
1883 system_level = SOL_SOCKET;
1884 system_name = SO_REUSEADDR;
1890 ret = convert_sockopt_level_and_name (level, name, &system_level, &system_name);
1894 *error = WSAENOPROTOOPT;
1898 *obj_val = int_to_object (domain, 0);
1902 /* No need to deal with MulticastOption names here, because
1903 * you cant getsockopt AddMembership or DropMembership (the
1904 * int getsockopt will error, causing an exception)
1907 case SocketOptionName_Linger:
1908 case SocketOptionName_DontLinger:
1909 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1913 case SocketOptionName_SendTimeout:
1914 case SocketOptionName_ReceiveTimeout:
1915 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1919 case SocketOptionName_PeerCred:
1920 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1926 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1930 if(ret==SOCKET_ERROR) {
1931 *error = WSAGetLastError ();
1936 case SocketOptionName_Linger:
1937 /* build a System.Net.Sockets.LingerOption */
1938 obj_class=mono_class_from_name(get_socket_assembly (),
1939 "System.Net.Sockets",
1941 obj=mono_object_new(domain, obj_class);
1943 /* Locate and set the fields "bool enabled" and "int
1946 field=mono_class_get_field_from_name(obj_class, "enabled");
1947 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1949 field=mono_class_get_field_from_name(obj_class, "seconds");
1950 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1954 case SocketOptionName_DontLinger:
1955 /* construct a bool int in val - true if linger is off */
1956 obj = int_to_object (domain, !linger.l_onoff);
1959 case SocketOptionName_SendTimeout:
1960 case SocketOptionName_ReceiveTimeout:
1961 obj = int_to_object (domain, time_ms);
1965 case SocketOptionName_PeerCred:
1967 /* build a Mono.Posix.PeerCred+PeerCredData if
1970 static MonoImage *mono_posix_image = NULL;
1971 MonoPeerCredData *cred_data;
1973 if (mono_posix_image == NULL) {
1974 mono_posix_image=mono_image_loaded ("Mono.Posix");
1975 if (!mono_posix_image) {
1976 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1978 *error = WSAENOPROTOOPT;
1981 mono_posix_image = mono_assembly_get_image (sa);
1986 obj_class = mono_class_from_name(mono_posix_image,
1989 obj = mono_object_new(domain, obj_class);
1990 cred_data = (MonoPeerCredData *)obj;
1991 cred_data->pid = cred.pid;
1992 cred_data->uid = cred.uid;
1993 cred_data->gid = cred.gid;
1999 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2000 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse)
2003 obj = int_to_object (domain, val);
2008 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
2016 MONO_ARCH_SAVE_REGS;
2020 ret=convert_sockopt_level_and_name(level, name, &system_level,
2023 *error = WSAENOPROTOOPT;
2029 valsize=mono_array_length(*byte_val);
2030 buf=mono_array_addr(*byte_val, guchar, 0);
2032 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
2033 if(ret==SOCKET_ERROR) {
2034 *error = WSAGetLastError ();
2038 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2039 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
2041 struct in_addr inaddr;
2042 MonoClassField *field;
2044 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
2046 /* No idea why .net uses a 64bit type to hold a 32bit value...
2048 * Internal value of IPAddess is in little-endian order
2050 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
2056 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
2058 struct in6_addr in6addr;
2059 MonoClassField *field;
2063 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
2064 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
2066 /* Solaris has only the 8 bit version. */
2068 for(i=0; i<8; i++) {
2069 guint16 s = mono_array_get (data, guint16, i);
2070 in6addr.s6_addr[2 * i + 1] = (s >> 8) & 0xff;
2071 in6addr.s6_addr[2 * i] = s & 0xff;
2075 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
2079 #endif /* AF_INET6 */
2082 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)
2084 struct linger linger;
2094 #ifdef HAVE_SOL_IPV6
2095 sol_ipv6 = SOL_IPV6;
2098 struct protoent *pent;
2099 pent = getprotobyname ("ipv6");
2100 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2108 struct protoent *pent;
2109 pent = getprotobyname ("ip");
2110 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2113 #endif /* AF_INET6 */
2115 MONO_ARCH_SAVE_REGS;
2117 ret=convert_sockopt_level_and_name(level, name, &system_level,
2120 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2121 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
2122 system_name = SO_REUSEADDR;
2123 int_val = int_val ? 0 : 1;
2129 *error = WSAENOPROTOOPT;
2136 /* Only one of obj_val, byte_val or int_val has data */
2138 MonoClassField *field;
2142 case SocketOptionName_Linger:
2143 /* Dig out "bool enabled" and "int seconds"
2146 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2147 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2148 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2149 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2151 valsize=sizeof(linger);
2152 ret = _wapi_setsockopt (sock, system_level,
2153 system_name, &linger, valsize);
2155 case SocketOptionName_AddMembership:
2156 case SocketOptionName_DropMembership:
2157 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2159 MonoObject *address = NULL;
2162 if(system_level == sol_ipv6) {
2163 struct ipv6_mreq mreq6;
2168 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2169 address = *(gpointer *)(((char *)obj_val) + field->offset);
2172 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2175 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2176 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2178 ret = _wapi_setsockopt (sock, system_level,
2179 system_name, &mreq6,
2181 } else if(system_level == sol_ip)
2182 #endif /* AF_INET6 */
2184 #ifdef HAVE_STRUCT_IP_MREQN
2185 struct ip_mreqn mreq = {{0}};
2187 struct ip_mreq mreq = {{0}};
2188 #endif /* HAVE_STRUCT_IP_MREQN */
2190 /* pain! MulticastOption holds two IPAddress
2191 * members, so I have to dig the value out of
2194 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2195 address = *(gpointer *)(((char *)obj_val) + field->offset);
2197 /* address might not be defined and if so, set the address to ADDR_ANY.
2200 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2203 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2204 address = *(gpointer *)(((char *)obj_val) + field->offset);
2206 #ifdef HAVE_STRUCT_IP_MREQN
2208 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2212 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2214 #endif /* HAVE_STRUCT_IP_MREQN */
2216 ret = _wapi_setsockopt (sock, system_level,
2222 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2224 /* Cause an exception to be thrown */
2228 } else if (byte_val!=NULL) {
2229 int valsize = mono_array_length (byte_val);
2230 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2233 case SocketOptionName_DontLinger:
2235 linger.l_onoff = (*buf) ? 0 : 1;
2236 linger.l_linger = 0;
2237 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2243 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2247 /* ReceiveTimeout/SendTimeout get here */
2249 case SocketOptionName_DontLinger:
2250 linger.l_onoff = !int_val;
2251 linger.l_linger = 0;
2252 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2254 case SocketOptionName_DontFragment:
2255 #ifdef HAVE_IP_MTU_DISCOVER
2256 /* Fiddle with the value slightly if we're
2260 int_val = IP_PMTUDISC_DO;
2266 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2270 if(ret==SOCKET_ERROR) {
2271 *error = WSAGetLastError ();
2275 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2281 MONO_ARCH_SAVE_REGS;
2285 /* Currently, the values for how (recv=0, send=1, both=2) match
2288 ret = _wapi_shutdown (sock, how);
2289 if(ret==SOCKET_ERROR) {
2290 *error = WSAGetLastError ();
2295 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2297 MonoArray *output, gint32 *error)
2299 glong output_bytes = 0;
2300 gchar *i_buffer, *o_buffer;
2304 MONO_ARCH_SAVE_REGS;
2308 if (code == FIONBIO) {
2309 /* Invalid command. Must use Socket.Blocking */
2313 if (input == NULL) {
2317 i_buffer = mono_array_addr (input, gchar, 0);
2318 i_len = mono_array_length (input);
2321 if (output == NULL) {
2325 o_buffer = mono_array_addr (output, gchar, 0);
2326 o_len = mono_array_length (output);
2329 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2330 if (ret == SOCKET_ERROR) {
2331 *error = WSAGetLastError ();
2335 return (gint) output_bytes;
2338 #ifdef HAVE_SIOCGIFCONF
2340 is_loopback (int family, void *ad)
2342 char *ptr = (char *) ad;
2344 if (family == AF_INET) {
2345 return (ptr [0] == 127);
2349 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2356 get_local_ips (int family, int *nips)
2358 int addr_size, offset, fd, i, count;
2359 int max_ifaces = 50; /* 50 interfaces should be enough... */
2362 struct ifreq iflags;
2363 char *result, *tmp_ptr;
2364 gboolean ignore_loopback = FALSE;
2367 if (family == AF_INET) {
2368 addr_size = sizeof (struct in_addr);
2369 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2371 } else if (family == AF_INET6) {
2372 addr_size = sizeof (struct in6_addr);
2373 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2379 fd = socket (family, SOCK_STREAM, 0);
2381 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2382 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2383 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2385 g_free (ifc.ifc_buf);
2389 count = ifc.ifc_len / sizeof (struct ifreq);
2392 g_free (ifc.ifc_buf);
2397 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2398 strcpy (iflags.ifr_name, ifr->ifr_name);
2399 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2403 if ((iflags.ifr_flags & IFF_UP) == 0) {
2404 ifr->ifr_name [0] = '\0';
2408 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2409 ignore_loopback = TRUE;
2414 result = g_malloc (addr_size * count);
2416 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2417 if (ifr->ifr_name [0] == '\0') {
2422 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2427 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2428 tmp_ptr += addr_size;
2431 g_free (ifc.ifc_buf);
2434 #elif defined(HAVE_GETIFADDRS)
2436 is_loopback (int family, void *ad)
2438 char *ptr = (char *) ad;
2440 if (family == AF_INET) {
2441 return (ptr [0] == 127);
2445 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2452 get_local_ips (int family, int *nips)
2454 struct ifaddrs *ifap = NULL, *ptr;
2455 int addr_size, offset, count, i;
2456 char *result, *tmp_ptr;
2459 if (family == AF_INET) {
2460 addr_size = sizeof (struct in_addr);
2461 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2463 } else if (family == AF_INET6) {
2464 addr_size = sizeof (struct in6_addr);
2465 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2471 if (getifaddrs (&ifap)) {
2472 fprintf(stderr, "get_local_ips() error: %s\n", strerror (errno));
2477 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2480 if (ptr->ifa_addr->sa_family != family)
2482 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2487 result = g_malloc (addr_size * count);
2489 for (i = 0, ptr = ifap; ptr; ptr = ptr->ifa_next) {
2492 if (ptr->ifa_addr->sa_family != family)
2494 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2497 memcpy (tmp_ptr, ((char *) ptr->ifa_addr) + offset, addr_size);
2498 tmp_ptr += addr_size;
2507 get_local_ips (int family, int *nips)
2513 #endif /* HAVE_SIOCGIFCONF */
2516 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2517 MonoArray **h_aliases,
2518 MonoArray **h_addr_list,
2519 gboolean add_local_ips)
2521 MonoDomain *domain = mono_domain_get ();
2523 struct in_addr *local_in = NULL;
2527 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2531 *h_name=mono_string_new(domain, he->h_name);
2533 while(he->h_aliases[i]!=NULL) {
2537 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2539 while(he->h_aliases[i]!=NULL) {
2542 alias=mono_string_new(domain, he->h_aliases[i]);
2543 mono_array_setref (*h_aliases, i, alias);
2546 } else if (!add_local_ips) {
2550 if (add_local_ips) {
2551 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2553 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2554 for (i = 0; i < nlocal_in; i++) {
2555 MonoString *addr_string;
2556 char addr [16], *ptr;
2558 ptr = (char *) &local_in [i];
2559 g_snprintf(addr, 16, "%u.%u.%u.%u",
2560 (unsigned char) ptr [0],
2561 (unsigned char) ptr [1],
2562 (unsigned char) ptr [2],
2563 (unsigned char) ptr [3]);
2565 addr_string = mono_string_new (domain, addr);
2566 mono_array_setref (*h_addr_list, i, addr_string);
2571 } else if (he == NULL) {
2572 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2573 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2574 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2579 if (nlocal_in == 0 && he != NULL) {
2581 while (he->h_addr_list[i]!=NULL) {
2585 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2587 while(he->h_addr_list[i]!=NULL) {
2588 MonoString *addr_string;
2591 g_snprintf(addr, 16, "%u.%u.%u.%u",
2592 (unsigned char)he->h_addr_list[i][0],
2593 (unsigned char)he->h_addr_list[i][1],
2594 (unsigned char)he->h_addr_list[i][2],
2595 (unsigned char)he->h_addr_list[i][3]);
2597 addr_string=mono_string_new(domain, addr);
2598 mono_array_setref (*h_addr_list, i, addr_string);
2606 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2607 MonoArray **h_aliases,
2608 MonoArray **h_addr_list)
2610 MonoDomain *domain = mono_domain_get ();
2612 *h_name=mono_string_new(domain, addr);
2613 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2614 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2615 mono_array_setref (*h_addr_list, 0, *h_name);
2621 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2622 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2623 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2625 MonoDomain *domain = mono_domain_get ();
2626 int i, host_count, host_index, family_hint;
2627 struct in_addr *local_in = NULL;
2629 struct in6_addr *local_in6 = NULL;
2631 gboolean from_local = FALSE;
2633 family_hint = get_family_hint ();
2636 * Check if address length and family are correct
2638 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2642 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2647 * Get the aliases and host name from he1 or he2 whichever is
2648 * not null, if he1 is not null then take aliases from he1
2650 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2651 family_hint == PF_INET)) {
2652 *h_name=mono_string_new (domain, he1->h_name);
2655 while(he1->h_aliases[i]!=NULL) {
2659 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2662 while(he1->h_aliases[i]!=NULL) {
2665 alias=mono_string_new (domain, he1->h_aliases[i]);
2666 mono_array_setref (*h_aliases, i, alias);
2669 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2670 family_hint == PF_INET6)) {
2671 *h_name=mono_string_new (domain, he2->h_name);
2674 while(he2->h_aliases [i] != NULL) {
2678 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2681 while(he2->h_aliases[i]!=NULL) {
2684 alias=mono_string_new (domain, he2->h_aliases[i]);
2685 mono_array_setref (*h_aliases, i, alias);
2688 } else if (!add_local_ips) {
2693 * Count the number of addresses in he1 + he2
2696 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2697 family_hint == PF_INET)) {
2699 while(he1->h_addr_list[i]!=NULL) {
2705 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2706 family_hint == PF_INET6)) {
2708 while(he2->h_addr_list[i]!=NULL) {
2718 if (add_local_ips) {
2719 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2720 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2722 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2723 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2725 if (nlocal_in || nlocal_in6) {
2727 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2728 nlocal_in + nlocal_in6);
2732 for (n = 0; n < nlocal_in6; n++) {
2733 MonoString *addr_string;
2735 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2737 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2740 addr_string = mono_string_new (domain, addr);
2741 mono_array_setref (*h_addr_list, host_index, addr_string);
2749 for (n = 0; n < nlocal_in; n++) {
2750 MonoString *addr_string;
2752 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2754 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2757 addr_string = mono_string_new (domain, addr);
2758 mono_array_setref (*h_addr_list, host_index, addr_string);
2766 } else if (he1 == NULL && he2 == NULL) {
2767 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2768 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2769 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2779 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2781 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2782 family_hint == PF_INET6)) {
2784 while(he2->h_addr_list[i] != NULL) {
2785 MonoString *addr_string;
2787 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2789 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2793 addr_string = mono_string_new (domain, addr);
2794 mono_array_setref (*h_addr_list, host_index, addr_string);
2801 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2802 family_hint == PF_INET)) {
2804 while(he1->h_addr_list[i] != NULL) {
2805 MonoString *addr_string;
2807 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2809 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2813 addr_string=mono_string_new (domain, addr);
2814 mono_array_setref (*h_addr_list, host_index, addr_string);
2825 #if defined(AF_INET6)
2827 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2828 MonoArray **h_aliases,
2829 MonoArray **h_addr_list,
2830 gboolean add_local_ips)
2833 struct addrinfo *ai = NULL;
2834 struct in_addr *local_in = NULL;
2836 struct in6_addr *local_in6 = NULL;
2840 MonoDomain *domain = mono_domain_get ();
2843 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2844 if (add_local_ips) {
2845 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2846 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2847 if (nlocal_in || nlocal_in6) {
2848 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2850 MonoString *addr_string;
2854 for (i = 0; i < nlocal_in; i++) {
2855 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2856 addr_string = mono_string_new (domain, addr);
2857 mono_array_setref (*h_addr_list, addr_index, addr_string);
2863 MonoString *addr_string;
2868 for (i = 0; i < nlocal_in6; i++) {
2869 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2871 addr_string = mono_string_new (domain, addr);
2872 mono_array_setref (*h_addr_list, addr_index, addr_string);
2881 freeaddrinfo (info);
2890 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2891 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2897 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2899 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2900 MonoString *addr_string;
2902 char buffer [48]; /* Max. size for IPv6 */
2904 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2908 if(ai->ai_family == PF_INET) {
2909 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2911 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2915 addr_string=mono_string_new(domain, buffer);
2917 addr_string=mono_string_new(domain, "");
2920 mono_array_setref (*h_addr_list, addr_index, addr_string);
2924 if (ai->ai_canonname != NULL) {
2925 *h_name=mono_string_new(domain, ai->ai_canonname);
2927 *h_name=mono_string_new(domain, buffer);
2943 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2945 gboolean add_local_ips = FALSE;
2946 #ifdef HAVE_SIOCGIFCONF
2947 gchar this_hostname [256];
2949 #if !defined(HAVE_GETHOSTBYNAME2_R)
2950 struct addrinfo *info = NULL, hints;
2953 MONO_ARCH_SAVE_REGS;
2955 hostname=mono_string_to_utf8 (host);
2956 if (*hostname == '\0') {
2957 add_local_ips = TRUE;
2960 #ifdef HAVE_SIOCGIFCONF
2961 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2962 if (!strcmp (hostname, this_hostname)) {
2963 add_local_ips = TRUE;
2969 memset(&hints, 0, sizeof(hints));
2970 hints.ai_family = get_family_hint ();
2971 hints.ai_socktype = SOCK_STREAM;
2972 hints.ai_flags = AI_CANONNAME;
2974 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2980 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2982 struct hostent he1,*hp1, he2, *hp2;
2983 int buffer_size1, buffer_size2;
2984 char *buffer1, *buffer2;
2986 gboolean return_value;
2989 MONO_ARCH_SAVE_REGS;
2991 hostname=mono_string_to_utf8 (host);
2992 if (*hostname == '\0')
2993 add_local_ips = TRUE;
2995 #ifdef HAVE_SIOCGIFCONF
2996 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2997 if (!strcmp (hostname, this_hostname))
2998 add_local_ips = TRUE;
3004 buffer1 = g_malloc0(buffer_size1);
3005 buffer2 = g_malloc0(buffer_size2);
3009 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
3010 &hp1, &herr) == ERANGE) {
3012 buffer1 = g_realloc(buffer1, buffer_size1);
3015 if (*hostname && hp1 == NULL)
3017 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
3018 buffer_size2, &hp2, &herr) == ERANGE) {
3020 buffer2 = g_realloc(buffer2, buffer_size2);
3024 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
3025 h_addr_list, add_local_ips);
3031 return(return_value);
3032 #endif /* HAVE_GETHOSTBYNAME2_R */
3034 #else /* AF_INET6 */
3035 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3039 gboolean add_local_ips = FALSE;
3040 #ifdef HAVE_SIOCGIFCONF
3041 gchar this_hostname [256];
3044 MONO_ARCH_SAVE_REGS;
3046 hostname=mono_string_to_utf8(host);
3047 if (*hostname == '\0')
3048 add_local_ips = TRUE;
3049 #ifdef HAVE_SIOCGIFCONF
3050 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3051 if (!strcmp (hostname, this_hostname))
3052 add_local_ips = TRUE;
3059 he = _wapi_gethostbyname (hostname);
3061 he = _wapi_gethostbyname (hostname);
3065 if (*hostname && he==NULL)
3068 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
3070 #endif /* AF_INET6 */
3072 #ifndef HAVE_INET_PTON
3074 inet_pton (int family, const char *address, void *inaddrp)
3076 if (family == AF_INET) {
3077 #ifdef HAVE_INET_ATON
3078 struct in_addr inaddr;
3080 if (!inet_aton (address, &inaddr))
3083 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
3086 /* assume the system has inet_addr(), if it doesn't
3087 have that we're pretty much screwed... */
3090 if (!strcmp (address, "255.255.255.255")) {
3091 /* special-case hack */
3092 inaddr = 0xffffffff;
3094 inaddr = inet_addr (address);
3096 #define INADDR_NONE ((in_addr_t) -1)
3098 if (inaddr == INADDR_NONE)
3102 memcpy (inaddrp, &inaddr, sizeof (guint32));
3104 #endif /* HAVE_INET_ATON */
3109 #endif /* !HAVE_INET_PTON */
3111 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3116 struct sockaddr_in saddr;
3117 struct sockaddr_in6 saddr6;
3118 struct addrinfo *info = NULL, hints;
3120 char hostname[1024] = {0};
3123 struct in_addr inaddr;
3128 address = mono_string_to_utf8 (addr);
3131 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
3132 /* Maybe an ipv6 address */
3133 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
3139 saddr6.sin6_family = AF_INET6;
3144 saddr.sin_family = AF_INET;
3148 if(family == AF_INET) {
3149 #if HAVE_SOCKADDR_IN_SIN_LEN
3150 saddr.sin_len = sizeof (saddr);
3152 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
3153 hostname, sizeof(hostname), NULL, 0,
3157 } else if(family == AF_INET6) {
3158 #if HAVE_SOCKADDR_IN6_SIN_LEN
3159 saddr6.sin6_len = sizeof (saddr6);
3161 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
3162 hostname, sizeof(hostname), NULL, 0,
3168 memset (&hints, 0, sizeof(hints));
3169 hints.ai_family = get_family_hint ();
3170 hints.ai_socktype = SOCK_STREAM;
3171 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
3173 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
3177 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
3179 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
3184 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
3185 ret = ipaddr_to_IPHostEntry (address, h_name, h_aliases, h_addr_list);
3187 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
3188 h_addr_list, FALSE);
3196 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
3198 gchar hostname[256];
3201 MONO_ARCH_SAVE_REGS;
3203 ret = gethostname (hostname, sizeof (hostname));
3208 *h_name=mono_string_new(mono_domain_get (), hostname);
3214 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
3218 TRANSMIT_FILE_BUFFERS buffers;
3220 MONO_ARCH_SAVE_REGS;
3222 if (filename == NULL)
3225 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
3226 if (file == INVALID_HANDLE_VALUE) {
3227 SetLastError (error);
3231 memset (&buffers, 0, sizeof (buffers));
3232 if (pre_buffer != NULL) {
3233 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
3234 buffers.HeadLength = mono_array_length (pre_buffer);
3236 if (post_buffer != NULL) {
3237 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
3238 buffers.TailLength = mono_array_length (post_buffer);
3241 if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
3250 void mono_network_init(void)
3255 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3257 g_error("%s: Couldn't initialise networking", __func__);
3261 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3262 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3265 void mono_network_cleanup(void)
3271 icall_cancel_blocking_socket_operation (MonoThread *thread)
3273 MonoInternalThread *internal = thread->internal_thread;
3275 if (mono_thread_info_new_interrupt_enabled ()) {
3276 mono_thread_info_abort_socket_syscall_for_close ((MonoNativeThreadId)(gsize)internal->tid);
3279 internal->ignore_next_signal = TRUE;
3280 mono_thread_kill (internal, mono_thread_get_abort_signal ());
3285 #endif /* #ifndef DISABLE_SOCKETS */