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.
96 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
99 * Some older versions of libc provide IPV6 support without defining the AI_ADDRCONFIG
100 * flag for getaddrinfo.
102 #ifndef AI_ADDRCONFIG
103 #define AI_ADDRCONFIG 0
106 static gint32 convert_family(MonoAddressFamily mono_family)
110 switch(mono_family) {
111 case AddressFamily_Unknown:
112 case AddressFamily_ImpLink:
113 case AddressFamily_Pup:
114 case AddressFamily_Chaos:
115 case AddressFamily_Iso:
116 case AddressFamily_Ecma:
117 case AddressFamily_DataKit:
118 case AddressFamily_Ccitt:
119 case AddressFamily_DataLink:
120 case AddressFamily_Lat:
121 case AddressFamily_HyperChannel:
122 case AddressFamily_NetBios:
123 case AddressFamily_VoiceView:
124 case AddressFamily_FireFox:
125 case AddressFamily_Banyan:
126 case AddressFamily_Atm:
127 case AddressFamily_Cluster:
128 case AddressFamily_Ieee12844:
129 case AddressFamily_NetworkDesigners:
130 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
133 case AddressFamily_Unspecified:
137 case AddressFamily_Unix:
141 case AddressFamily_InterNetwork:
145 case AddressFamily_Ipx:
151 case AddressFamily_Sna:
157 case AddressFamily_DecNet:
163 case AddressFamily_AppleTalk:
167 case AddressFamily_InterNetworkV6:
172 case AddressFamily_Irda:
178 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
184 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
186 MonoAddressFamily family=AddressFamily_Unknown;
190 family=AddressFamily_Unspecified;
194 family=AddressFamily_Unix;
198 family=AddressFamily_InterNetwork;
203 family=AddressFamily_Ipx;
209 family=AddressFamily_Sna;
215 family=AddressFamily_DecNet;
220 family=AddressFamily_AppleTalk;
225 family=AddressFamily_InterNetworkV6;
231 family=AddressFamily_Irda;
235 g_warning("unknown address family 0x%x", af_family);
241 static gint32 convert_type(MonoSocketType mono_type)
246 case SocketType_Stream:
250 case SocketType_Dgram:
264 case SocketType_Seqpacket:
268 case SocketType_Unknown:
269 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
273 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
279 static gint32 convert_proto(MonoProtocolType mono_proto)
284 case ProtocolType_IP:
285 case ProtocolType_IPv6:
286 case ProtocolType_Icmp:
287 case ProtocolType_Igmp:
288 case ProtocolType_Ggp:
289 case ProtocolType_Tcp:
290 case ProtocolType_Pup:
291 case ProtocolType_Udp:
292 case ProtocolType_Idp:
293 /* These protocols are known (on my system at least) */
297 case ProtocolType_ND:
298 case ProtocolType_Raw:
299 case ProtocolType_Ipx:
300 case ProtocolType_Spx:
301 case ProtocolType_SpxII:
302 case ProtocolType_Unknown:
303 /* These protocols arent */
304 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
314 /* Convert MonoSocketFlags */
315 static gint32 convert_socketflags (gint32 sflags)
320 /* SocketFlags.None */
323 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
324 SocketFlags_DontRoute | SocketFlags_Partial))
325 /* Contains invalid flag values */
328 if (sflags & SocketFlags_OutOfBand)
330 if (sflags & SocketFlags_Peek)
332 if (sflags & SocketFlags_DontRoute)
333 flags |= MSG_DONTROUTE;
335 /* Ignore Partial - see bug 349688. Don't return -1, because
336 * according to the comment in that bug ms runtime doesn't for
337 * UDP sockets (this means we will silently ignore it for TCP
341 if (sflags & SocketFlags_Partial)
345 /* Don't do anything for MaxIOVectorLength */
346 if (sflags & SocketFlags_MaxIOVectorLength)
354 * 0 on success (mapped mono_level and mono_name to system_level and system_name
356 * -2 on non-fatal error (ie, must ignore)
358 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
359 MonoSocketOptionName mono_name,
363 switch (mono_level) {
364 case SocketOptionLevel_Socket:
365 *system_level = SOL_SOCKET;
368 case SocketOptionName_DontLinger:
369 /* This is SO_LINGER, because the setsockopt
370 * internal call maps DontLinger to SO_LINGER
373 *system_name = SO_LINGER;
375 case SocketOptionName_Debug:
376 *system_name = SO_DEBUG;
379 case SocketOptionName_AcceptConnection:
380 *system_name = SO_ACCEPTCONN;
383 case SocketOptionName_ReuseAddress:
384 *system_name = SO_REUSEADDR;
386 case SocketOptionName_KeepAlive:
387 *system_name = SO_KEEPALIVE;
389 case SocketOptionName_DontRoute:
390 *system_name = SO_DONTROUTE;
392 case SocketOptionName_Broadcast:
393 *system_name = SO_BROADCAST;
395 case SocketOptionName_Linger:
396 *system_name = SO_LINGER;
398 case SocketOptionName_OutOfBandInline:
399 *system_name = SO_OOBINLINE;
401 case SocketOptionName_SendBuffer:
402 *system_name = SO_SNDBUF;
404 case SocketOptionName_ReceiveBuffer:
405 *system_name = SO_RCVBUF;
407 case SocketOptionName_SendLowWater:
408 *system_name = SO_SNDLOWAT;
410 case SocketOptionName_ReceiveLowWater:
411 *system_name = SO_RCVLOWAT;
413 case SocketOptionName_SendTimeout:
414 *system_name = SO_SNDTIMEO;
416 case SocketOptionName_ReceiveTimeout:
417 *system_name = SO_RCVTIMEO;
419 case SocketOptionName_Error:
420 *system_name = SO_ERROR;
422 case SocketOptionName_Type:
423 *system_name = SO_TYPE;
426 case SocketOptionName_PeerCred:
427 *system_name = SO_PEERCRED;
430 case SocketOptionName_ExclusiveAddressUse:
431 #ifdef SO_EXCLUSIVEADDRUSE
432 *system_name = SO_EXCLUSIVEADDRUSE;
435 case SocketOptionName_UseLoopback:
436 #ifdef SO_USELOOPBACK
437 *system_name = SO_USELOOPBACK;
440 case SocketOptionName_MaxConnections:
442 *system_name = SO_MAXCONN;
444 #elif defined(SOMAXCONN)
445 *system_name = SOMAXCONN;
449 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
454 case SocketOptionLevel_IP:
456 *system_level = SOL_IP;
459 static int cached = 0;
463 struct protoent *pent;
465 pent = getprotobyname ("IP");
466 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
470 *system_level = proto;
472 #endif /* HAVE_SOL_IP */
475 case SocketOptionName_IPOptions:
476 *system_name = IP_OPTIONS;
479 case SocketOptionName_HeaderIncluded:
480 *system_name = IP_HDRINCL;
484 case SocketOptionName_TypeOfService:
485 *system_name = IP_TOS;
489 case SocketOptionName_IpTimeToLive:
490 *system_name = IP_TTL;
493 case SocketOptionName_MulticastInterface:
494 *system_name = IP_MULTICAST_IF;
496 case SocketOptionName_MulticastTimeToLive:
497 *system_name = IP_MULTICAST_TTL;
499 case SocketOptionName_MulticastLoopback:
500 *system_name = IP_MULTICAST_LOOP;
502 case SocketOptionName_AddMembership:
503 *system_name = IP_ADD_MEMBERSHIP;
505 case SocketOptionName_DropMembership:
506 *system_name = IP_DROP_MEMBERSHIP;
508 #ifdef HAVE_IP_PKTINFO
509 case SocketOptionName_PacketInformation:
510 *system_name = IP_PKTINFO;
512 #endif /* HAVE_IP_PKTINFO */
514 case SocketOptionName_DontFragment:
515 #ifdef HAVE_IP_DONTFRAGMENT
516 *system_name = IP_DONTFRAGMENT;
518 #elif defined HAVE_IP_MTU_DISCOVER
519 /* Not quite the same */
520 *system_name = IP_MTU_DISCOVER;
523 /* If the flag is not available on this system, we can ignore this error */
525 #endif /* HAVE_IP_DONTFRAGMENT */
526 case SocketOptionName_AddSourceMembership:
527 case SocketOptionName_DropSourceMembership:
528 case SocketOptionName_BlockSource:
529 case SocketOptionName_UnblockSource:
530 /* Can't figure out how to map these, so fall
534 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
540 case SocketOptionLevel_IPv6:
542 *system_level = SOL_IPV6;
545 static int cached = 0;
549 struct protoent *pent;
551 pent = getprotobyname ("IPV6");
552 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
556 *system_level = proto;
558 #endif /* HAVE_SOL_IPV6 */
561 case SocketOptionName_IpTimeToLive:
562 case SocketOptionName_HopLimit:
563 *system_name = IPV6_UNICAST_HOPS;
565 case SocketOptionName_MulticastInterface:
566 *system_name = IPV6_MULTICAST_IF;
568 case SocketOptionName_MulticastTimeToLive:
569 *system_name = IPV6_MULTICAST_HOPS;
571 case SocketOptionName_MulticastLoopback:
572 *system_name = IPV6_MULTICAST_LOOP;
574 case SocketOptionName_AddMembership:
575 *system_name = IPV6_JOIN_GROUP;
577 case SocketOptionName_DropMembership:
578 *system_name = IPV6_LEAVE_GROUP;
580 case SocketOptionName_PacketInformation:
581 #ifdef HAVE_IPV6_PKTINFO
582 *system_name = IPV6_PKTINFO;
585 case SocketOptionName_HeaderIncluded:
586 case SocketOptionName_IPOptions:
587 case SocketOptionName_TypeOfService:
588 case SocketOptionName_DontFragment:
589 case SocketOptionName_AddSourceMembership:
590 case SocketOptionName_DropSourceMembership:
591 case SocketOptionName_BlockSource:
592 case SocketOptionName_UnblockSource:
593 /* Can't figure out how to map these, so fall
597 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
601 break; /* SocketOptionLevel_IPv6 */
604 case SocketOptionLevel_Tcp:
606 *system_level = SOL_TCP;
609 static int cached = 0;
613 struct protoent *pent;
615 pent = getprotobyname ("TCP");
616 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
620 *system_level = proto;
622 #endif /* HAVE_SOL_TCP */
625 case SocketOptionName_NoDelay:
626 *system_name = TCP_NODELAY;
629 /* The documentation is talking complete
630 * bollocks here: rfc-1222 is titled
631 * 'Advancing the NSFNET Routing Architecture'
632 * and doesn't mention either of the words
633 * "expedite" or "urgent".
635 case SocketOptionName_BsdUrgent:
636 case SocketOptionName_Expedited:
639 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
644 case SocketOptionLevel_Udp:
645 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
648 case SocketOptionName_NoChecksum:
649 case SocketOptionName_ChecksumCoverage:
651 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
658 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
665 static MonoImage *get_socket_assembly (void)
667 static const char *version = NULL;
668 static gboolean moonlight;
669 MonoDomain *domain = mono_domain_get ();
671 if (version == NULL) {
672 version = mono_get_runtime_info ()->framework_version;
673 moonlight = !strcmp (version, "2.1");
676 if (domain->socket_assembly == NULL) {
677 MonoImage *socket_assembly;
680 socket_assembly = mono_image_loaded ("System.Net");
681 if (!socket_assembly) {
682 MonoAssembly *sa = mono_assembly_open ("System.Net.dll", NULL);
685 g_assert_not_reached ();
687 socket_assembly = mono_assembly_get_image (sa);
691 socket_assembly = mono_image_loaded ("System");
692 if (!socket_assembly) {
693 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
696 g_assert_not_reached ();
698 socket_assembly = mono_assembly_get_image (sa);
703 domain->socket_assembly = socket_assembly;
706 return domain->socket_assembly;
710 static gint32 get_family_hint(void)
712 MonoDomain *domain = mono_domain_get ();
714 if (!domain->inet_family_hint) {
715 MonoClass *socket_class;
716 MonoClassField *ipv6_field, *ipv4_field;
717 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
720 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
721 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
722 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
723 vtable = mono_class_vtable (mono_domain_get (), socket_class);
725 mono_runtime_class_init (vtable);
727 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
728 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
730 mono_domain_lock (domain);
731 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
732 domain->inet_family_hint = 1;
733 } else if (ipv4_enabled == 1) {
734 domain->inet_family_hint = 2;
736 domain->inet_family_hint = 3;
738 mono_domain_unlock (domain);
740 switch (domain->inet_family_hint) {
741 case 1: return PF_UNSPEC;
742 case 2: return PF_INET;
743 case 3: return PF_INET6;
750 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
761 sock_family=convert_family(family);
762 if(sock_family==-1) {
763 *error = WSAEAFNOSUPPORT;
767 sock_proto=convert_proto(proto);
769 *error = WSAEPROTONOSUPPORT;
773 sock_type=convert_type(type);
775 *error = WSAESOCKTNOSUPPORT;
779 sock = _wapi_socket (sock_family, sock_type, sock_proto,
780 NULL, 0, WSA_FLAG_OVERLAPPED);
782 if(sock==INVALID_SOCKET) {
783 *error = WSAGetLastError ();
787 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
788 return (GUINT_TO_POINTER (sock));
792 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
793 return (GUINT_TO_POINTER (sock));
797 return(GUINT_TO_POINTER (sock));
800 /* FIXME: the SOCKET parameter (here and in other functions in this
801 * file) is really an IntPtr which needs to be converted to a guint32.
803 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
808 LOGDEBUG (g_message ("%s: closing 0x%x", __func__, sock));
812 /* Clear any pending work item from this socket if the underlying
813 * polling system does not notify when the socket is closed */
814 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
818 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
822 LOGDEBUG (g_message("%s: returning %d", __func__, WSAGetLastError()));
824 return(WSAGetLastError());
827 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
837 ret=ioctlsocket(sock, FIONREAD, &amount);
838 if(ret==SOCKET_ERROR) {
839 *error = WSAGetLastError ();
846 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
857 * block == TRUE/FALSE means we will block/not block.
858 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
862 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
863 if(ret==SOCKET_ERROR) {
864 *error = WSAGetLastError ();
868 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
879 MonoInternalThread* curthread = mono_thread_internal_current ();
880 curthread->interrupt_on_stop = (gpointer)TRUE;
881 newsock = _wapi_accept (sock, NULL, 0);
882 curthread->interrupt_on_stop = (gpointer)FALSE;
885 newsock = _wapi_accept (sock, NULL, 0);
887 if(newsock==INVALID_SOCKET) {
888 *error = WSAGetLastError ();
892 return(GUINT_TO_POINTER (newsock));
895 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
905 ret = _wapi_listen (sock, backlog);
906 if(ret==SOCKET_ERROR) {
907 *error = WSAGetLastError ();
912 // Check whether it's ::ffff::0:0.
914 is_ipv4_mapped_any (const struct in6_addr *addr)
918 for (i = 0; i < 10; i++) {
919 if (addr->s6_addr [i])
922 if ((addr->s6_addr [10] != 0xff) || (addr->s6_addr [11] != 0xff))
924 for (i = 12; i < 16; i++) {
925 if (addr->s6_addr [i])
932 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
933 int sa_size, gint32 *error)
935 MonoDomain *domain = mono_domain_get ();
936 MonoObject *sockaddr_obj;
938 MonoAddressFamily family;
940 /* Build a System.Net.SocketAddress object instance */
941 if (!domain->sockaddr_class) {
942 domain->sockaddr_class=mono_class_from_name (get_socket_assembly (), "System.Net", "SocketAddress");
943 g_assert (domain->sockaddr_class);
945 sockaddr_obj=mono_object_new(domain, domain->sockaddr_class);
947 /* Locate the SocketAddress data buffer in the object */
948 if (!domain->sockaddr_data_field) {
949 domain->sockaddr_data_field=mono_class_get_field_from_name (domain->sockaddr_class, "data");
950 g_assert (domain->sockaddr_data_field);
953 /* Make sure there is space for the family and size bytes */
955 if (saddr->sa_family == AF_UNIX) {
956 /* sa_len includes the entire sockaddr size, so we don't need the
957 * N bytes (sizeof (unsigned short)) of the family. */
958 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size);
962 /* May be the +2 here is too conservative, as sa_len returns
963 * the length of the entire sockaddr_in/in6, including
964 * sizeof (unsigned short) of the family */
965 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
968 /* The data buffer is laid out as follows:
969 * bytes 0 and 1 are the address family
970 * bytes 2 and 3 are the port info
971 * the rest is the address info
974 family=convert_to_mono_family(saddr->sa_family);
975 if(family==AddressFamily_Unknown) {
976 *error = WSAEAFNOSUPPORT;
980 mono_array_set(data, guint8, 0, family & 0x0FF);
981 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
983 if(saddr->sa_family==AF_INET) {
984 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
985 guint16 port=ntohs(sa_in->sin_port);
986 guint32 address=ntohl(sa_in->sin_addr.s_addr);
989 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
992 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
993 mono_array_set(data, guint8, 3, (port) & 0xff);
994 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
995 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
996 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
997 mono_array_set(data, guint8, 7, (address) & 0xff);
999 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1001 return(sockaddr_obj);
1003 } else if (saddr->sa_family == AF_INET6) {
1004 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
1007 guint16 port=ntohs(sa_in->sin6_port);
1010 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1013 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
1014 mono_array_set(data, guint8, 3, (port) & 0xff);
1016 if (is_ipv4_mapped_any (&sa_in->sin6_addr)) {
1017 // Map ::ffff:0:0 to :: (bug #5502)
1018 for(i=0; i<16; i++) {
1019 mono_array_set(data, guint8, 8+i, 0);
1022 for(i=0; i<16; i++) {
1023 mono_array_set(data, guint8, 8+i,
1024 sa_in->sin6_addr.s6_addr[i]);
1028 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
1029 mono_array_set(data, guint8, 25,
1030 (sa_in->sin6_scope_id >> 8) & 0xff);
1031 mono_array_set(data, guint8, 26,
1032 (sa_in->sin6_scope_id >> 16) & 0xff);
1033 mono_array_set(data, guint8, 27,
1034 (sa_in->sin6_scope_id >> 24) & 0xff);
1036 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1038 return(sockaddr_obj);
1040 #ifdef HAVE_SYS_UN_H
1041 } else if (saddr->sa_family == AF_UNIX) {
1044 for (i = 0; i < sa_size; i++) {
1045 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
1048 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1050 return sockaddr_obj;
1053 *error = WSAEAFNOSUPPORT;
1059 get_sockaddr_size (int family)
1064 if (family == AF_INET) {
1065 size = sizeof (struct sockaddr_in);
1067 } else if (family == AF_INET6) {
1068 size = sizeof (struct sockaddr_in6);
1070 #ifdef HAVE_SYS_UN_H
1071 } else if (family == AF_UNIX) {
1072 size = sizeof (struct sockaddr_un);
1078 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1085 MONO_ARCH_SAVE_REGS;
1089 salen = get_sockaddr_size (convert_family (af));
1091 *error = WSAEAFNOSUPPORT;
1094 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1095 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1097 if(ret==SOCKET_ERROR) {
1098 *error = WSAGetLastError ();
1104 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)));
1106 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1112 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1119 MONO_ARCH_SAVE_REGS;
1123 salen = get_sockaddr_size (convert_family (af));
1125 *error = WSAEAFNOSUPPORT;
1128 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1129 /* Note: linux returns just 2 for AF_UNIX. Always. */
1130 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1131 if(ret==SOCKET_ERROR) {
1132 *error = WSAGetLastError ();
1138 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)));
1140 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1146 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1150 MonoClassField *field;
1155 /* Dig the SocketAddress data buffer out of the object */
1156 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1157 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1159 /* The data buffer is laid out as follows:
1160 * byte 0 is the address family low byte
1161 * byte 1 is the address family high byte
1163 * bytes 2 and 3 are the port info
1164 * the rest is the address info
1166 * the rest is the file name
1168 len = mono_array_length (data);
1170 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1173 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1174 if (family == AF_INET) {
1175 struct sockaddr_in *sa;
1180 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1183 sa = g_new0 (struct sockaddr_in, 1);
1184 port = (mono_array_get (data, guint8, 2) << 8) +
1185 mono_array_get (data, guint8, 3);
1186 address = (mono_array_get (data, guint8, 4) << 24) +
1187 (mono_array_get (data, guint8, 5) << 16 ) +
1188 (mono_array_get (data, guint8, 6) << 8) +
1189 mono_array_get (data, guint8, 7);
1191 sa->sin_family = family;
1192 sa->sin_addr.s_addr = htonl (address);
1193 sa->sin_port = htons (port);
1195 *sa_size = sizeof(struct sockaddr_in);
1196 return((struct sockaddr *)sa);
1199 } else if (family == AF_INET6) {
1200 struct sockaddr_in6 *sa;
1206 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1209 sa = g_new0 (struct sockaddr_in6, 1);
1210 port = mono_array_get (data, guint8, 3) +
1211 (mono_array_get (data, guint8, 2) << 8);
1212 scopeid = mono_array_get (data, guint8, 24) +
1213 (mono_array_get (data, guint8, 25) << 8) +
1214 (mono_array_get (data, guint8, 26) << 16) +
1215 (mono_array_get (data, guint8, 27) << 24);
1217 sa->sin6_family = family;
1218 sa->sin6_port = htons (port);
1219 sa->sin6_scope_id = scopeid;
1221 for(i=0; i<16; i++) {
1222 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1225 *sa_size = sizeof(struct sockaddr_in6);
1226 return((struct sockaddr *)sa);
1228 #ifdef HAVE_SYS_UN_H
1229 } else if (family == AF_UNIX) {
1230 struct sockaddr_un *sock_un;
1233 /* Need a byte for the '\0' terminator/prefix, and the first
1234 * two bytes hold the SocketAddress family
1236 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1237 mono_raise_exception (mono_get_exception_index_out_of_range ());
1240 sock_un = g_new0 (struct sockaddr_un, 1);
1242 sock_un->sun_family = family;
1243 for (i = 0; i < len - 2; i++) {
1244 sock_un->sun_path [i] = mono_array_get (data, guint8,
1250 return (struct sockaddr *)sock_un;
1253 *error = WSAEAFNOSUPPORT;
1258 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1260 struct sockaddr *sa;
1264 MONO_ARCH_SAVE_REGS;
1268 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1273 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)));
1275 ret = _wapi_bind (sock, sa, sa_size);
1276 if(ret==SOCKET_ERROR) {
1277 *error = WSAGetLastError ();
1290 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1291 gint timeout, gint32 *error)
1293 MonoInternalThread *thread = NULL;
1299 MONO_ARCH_SAVE_REGS;
1301 pfds = g_new0 (mono_pollfd, 1);
1302 pfds[0].fd = GPOINTER_TO_INT (sock);
1303 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1304 (mode == SelectModeWrite) ? MONO_POLLOUT :
1305 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1307 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1308 start = time (NULL);
1312 ret = mono_poll (pfds, 1, timeout);
1313 if (timeout > 0 && ret < 0) {
1315 int sec = time (NULL) - start;
1317 timeout -= sec * 1000;
1325 if (ret == -1 && errno == EINTR) {
1328 if (thread == NULL) {
1329 thread = mono_thread_internal_current ();
1332 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1338 /* Suspend requested? */
1339 mono_thread_interruption_checkpoint ();
1343 } while (ret == -1 && errno == EINTR);
1347 *error = WSAGetLastError ();
1349 *error = errno_to_WSA (errno, __func__);
1364 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1366 struct sockaddr *sa;
1370 MONO_ARCH_SAVE_REGS;
1374 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1379 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)));
1381 ret = _wapi_connect (sock, sa, sa_size);
1382 if(ret==SOCKET_ERROR) {
1383 *error = WSAGetLastError ();
1389 /* These #defines from mswsock.h from wine. Defining them here allows
1390 * us to build this file on a mingw box that doesn't know the magic
1391 * numbers, but still run on a newer windows box that does.
1393 #ifndef WSAID_DISCONNECTEX
1394 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1395 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1398 #ifndef WSAID_TRANSMITFILE
1399 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1400 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1403 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1406 glong output_bytes = 0;
1407 GUID disco_guid = WSAID_DISCONNECTEX;
1408 GUID trans_guid = WSAID_TRANSMITFILE;
1409 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1410 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1413 MONO_ARCH_SAVE_REGS;
1417 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1419 /* I _think_ the extension function pointers need to be looked
1420 * up for each socket. FIXME: check the best way to store
1421 * pointers to functions in managed objects that still works
1422 * on 64bit platforms.
1424 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1425 (void *)&disco_guid, sizeof(GUID),
1426 (void *)&_wapi_disconnectex, sizeof(void *),
1427 &output_bytes, NULL, NULL);
1429 /* make sure that WSAIoctl didn't put crap in the
1432 _wapi_disconnectex = NULL;
1434 /* Look up the TransmitFile extension function pointer
1435 * instead of calling TransmitFile() directly, because
1436 * apparently "Several of the extension functions have
1437 * been available since WinSock 1.1 and are exported
1438 * from MSWsock.dll, however it's not advisable to
1439 * link directly to this dll as this ties you to the
1440 * Microsoft WinSock provider. A provider neutral way
1441 * of accessing these extension functions is to load
1442 * them dynamically via WSAIoctl using the
1443 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1444 * should, theoretically, allow you to access these
1445 * functions from any provider that supports them..."
1446 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1448 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1449 (void *)&trans_guid, sizeof(GUID),
1450 (void *)&_wapi_transmitfile, sizeof(void *),
1451 &output_bytes, NULL, NULL);
1453 _wapi_transmitfile = NULL;
1457 if (_wapi_disconnectex != NULL) {
1458 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1459 } else if (_wapi_transmitfile != NULL) {
1460 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1461 TF_DISCONNECT | TF_REUSE_SOCKET);
1463 *error = ERROR_NOT_SUPPORTED;
1467 if (bret == FALSE) {
1468 *error = WSAGetLastError ();
1472 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1479 MONO_ARCH_SAVE_REGS;
1483 alen = mono_array_length (buffer);
1484 if (offset > alen - count) {
1488 buf=mono_array_addr(buffer, guchar, offset);
1490 recvflags = convert_socketflags (flags);
1491 if (recvflags == -1) {
1492 *error = WSAEOPNOTSUPP;
1498 MonoInternalThread* curthread = mono_thread_internal_current ();
1499 curthread->interrupt_on_stop = (gpointer)TRUE;
1500 ret = _wapi_recv (sock, buf, count, recvflags);
1501 curthread->interrupt_on_stop = (gpointer)FALSE;
1504 ret = _wapi_recv (sock, buf, count, recvflags);
1507 if(ret==SOCKET_ERROR) {
1508 *error = WSAGetLastError ();
1515 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1520 DWORD recvflags = 0;
1522 MONO_ARCH_SAVE_REGS;
1526 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1527 count = mono_array_length (buffers);
1529 recvflags = convert_socketflags (flags);
1530 if (recvflags == -1) {
1531 *error = WSAEOPNOTSUPP;
1535 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1536 if (ret == SOCKET_ERROR) {
1537 *error = WSAGetLastError ();
1544 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1550 struct sockaddr *sa;
1553 MONO_ARCH_SAVE_REGS;
1557 alen = mono_array_length (buffer);
1558 if (offset > alen - count) {
1562 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1567 buf=mono_array_addr(buffer, guchar, offset);
1569 recvflags = convert_socketflags (flags);
1570 if (recvflags == -1) {
1571 *error = WSAEOPNOTSUPP;
1575 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1576 if(ret==SOCKET_ERROR) {
1578 *error = WSAGetLastError ();
1582 /* If we didn't get a socket size, then we're probably a
1583 * connected connection-oriented socket and the stack hasn't
1584 * returned the remote address. All we can do is return null.
1587 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1596 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1603 MONO_ARCH_SAVE_REGS;
1607 alen = mono_array_length (buffer);
1608 if (offset > alen - count) {
1612 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1614 buf=mono_array_addr(buffer, guchar, offset);
1616 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1618 sendflags = convert_socketflags (flags);
1619 if (sendflags == -1) {
1620 *error = WSAEOPNOTSUPP;
1624 ret = _wapi_send (sock, buf, count, sendflags);
1625 if(ret==SOCKET_ERROR) {
1626 *error = WSAGetLastError ();
1633 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1638 DWORD sendflags = 0;
1640 MONO_ARCH_SAVE_REGS;
1644 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1645 count = mono_array_length (buffers);
1647 sendflags = convert_socketflags (flags);
1648 if (sendflags == -1) {
1649 *error = WSAEOPNOTSUPP;
1653 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1654 if (ret == SOCKET_ERROR) {
1655 *error = WSAGetLastError ();
1662 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1668 struct sockaddr *sa;
1671 MONO_ARCH_SAVE_REGS;
1675 alen = mono_array_length (buffer);
1676 if (offset > alen - count) {
1680 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1685 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1687 buf=mono_array_addr(buffer, guchar, offset);
1689 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1691 sendflags = convert_socketflags (flags);
1692 if (sendflags == -1) {
1693 *error = WSAEOPNOTSUPP;
1697 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1698 if(ret==SOCKET_ERROR) {
1699 *error = WSAGetLastError ();
1707 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1710 MonoClassField *field;
1712 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1713 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1718 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1719 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1721 MonoInternalThread *thread = NULL;
1728 MonoClass *sock_arr_class;
1731 uintptr_t socks_size;
1733 MONO_ARCH_SAVE_REGS;
1735 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1736 count = mono_array_length (*sockets);
1737 nfds = count - 3; /* NULL separators */
1738 pfds = g_new0 (mono_pollfd, nfds);
1740 for (i = 0; i < count; i++) {
1741 obj = mono_array_get (*sockets, MonoObject *, i);
1748 /* The socket array was bogus */
1754 pfds [idx].fd = Socket_to_SOCKET (obj);
1755 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1759 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1760 start = time (NULL);
1763 ret = mono_poll (pfds, nfds, timeout);
1764 if (timeout > 0 && ret < 0) {
1766 int sec = time (NULL) - start;
1768 timeout -= sec * 1000;
1774 if (ret == -1 && errno == EINTR) {
1777 thread = mono_thread_internal_current ();
1779 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1786 /* Suspend requested? */
1787 mono_thread_interruption_checkpoint ();
1791 } while (ret == -1 && errno == EINTR);
1795 *error = WSAGetLastError ();
1797 *error = errno_to_WSA (errno, __func__);
1809 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1810 socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
1811 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1814 for (i = 0; i < count && ret > 0; i++) {
1817 obj = mono_array_get (*sockets, MonoObject *, i);
1824 pfd = &pfds [i - mode];
1825 if (pfd->revents == 0)
1829 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1830 mono_array_setref (socks, idx++, obj);
1831 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1832 mono_array_setref (socks, idx++, obj);
1833 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1834 mono_array_setref (socks, idx++, obj);
1842 static MonoObject* int_to_object (MonoDomain *domain, int val)
1844 return mono_value_box (domain, mono_get_int32_class (), &val);
1848 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1854 socklen_t valsize=sizeof(val);
1855 struct linger linger;
1856 socklen_t lingersize=sizeof(linger);
1858 socklen_t time_ms_size = sizeof (time_ms);
1860 # if defined(__OpenBSD__)
1861 struct sockpeercred cred;
1865 socklen_t credsize = sizeof(cred);
1867 MonoDomain *domain=mono_domain_get();
1869 MonoClass *obj_class;
1870 MonoClassField *field;
1872 MONO_ARCH_SAVE_REGS;
1876 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1877 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
1878 system_level = SOL_SOCKET;
1879 system_name = SO_REUSEADDR;
1885 ret = convert_sockopt_level_and_name (level, name, &system_level, &system_name);
1889 *error = WSAENOPROTOOPT;
1893 *obj_val = int_to_object (domain, 0);
1897 /* No need to deal with MulticastOption names here, because
1898 * you cant getsockopt AddMembership or DropMembership (the
1899 * int getsockopt will error, causing an exception)
1902 case SocketOptionName_Linger:
1903 case SocketOptionName_DontLinger:
1904 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1908 case SocketOptionName_SendTimeout:
1909 case SocketOptionName_ReceiveTimeout:
1910 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1914 case SocketOptionName_PeerCred:
1915 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1921 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1925 if(ret==SOCKET_ERROR) {
1926 *error = WSAGetLastError ();
1931 case SocketOptionName_Linger:
1932 /* build a System.Net.Sockets.LingerOption */
1933 obj_class=mono_class_from_name(get_socket_assembly (),
1934 "System.Net.Sockets",
1936 obj=mono_object_new(domain, obj_class);
1938 /* Locate and set the fields "bool enabled" and "int
1941 field=mono_class_get_field_from_name(obj_class, "enabled");
1942 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1944 field=mono_class_get_field_from_name(obj_class, "seconds");
1945 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1949 case SocketOptionName_DontLinger:
1950 /* construct a bool int in val - true if linger is off */
1951 obj = int_to_object (domain, !linger.l_onoff);
1954 case SocketOptionName_SendTimeout:
1955 case SocketOptionName_ReceiveTimeout:
1956 obj = int_to_object (domain, time_ms);
1960 case SocketOptionName_PeerCred:
1962 /* build a Mono.Posix.PeerCred+PeerCredData if
1965 static MonoImage *mono_posix_image = NULL;
1966 MonoPeerCredData *cred_data;
1968 if (mono_posix_image == NULL) {
1969 mono_posix_image=mono_image_loaded ("Mono.Posix");
1970 if (!mono_posix_image) {
1971 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1973 *error = WSAENOPROTOOPT;
1976 mono_posix_image = mono_assembly_get_image (sa);
1981 obj_class = mono_class_from_name(mono_posix_image,
1984 obj = mono_object_new(domain, obj_class);
1985 cred_data = (MonoPeerCredData *)obj;
1986 cred_data->pid = cred.pid;
1987 cred_data->uid = cred.uid;
1988 cred_data->gid = cred.gid;
1994 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1995 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse)
1998 obj = int_to_object (domain, val);
2003 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
2011 MONO_ARCH_SAVE_REGS;
2015 ret=convert_sockopt_level_and_name(level, name, &system_level,
2018 *error = WSAENOPROTOOPT;
2024 valsize=mono_array_length(*byte_val);
2025 buf=mono_array_addr(*byte_val, guchar, 0);
2027 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
2028 if(ret==SOCKET_ERROR) {
2029 *error = WSAGetLastError ();
2033 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2034 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
2036 struct in_addr inaddr;
2037 MonoClassField *field;
2039 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
2041 /* No idea why .net uses a 64bit type to hold a 32bit value...
2043 * Internal value of IPAddess is in little-endian order
2045 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
2051 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
2053 struct in6_addr in6addr;
2054 MonoClassField *field;
2058 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
2059 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
2061 /* Solaris has only the 8 bit version. */
2063 for(i=0; i<8; i++) {
2064 guint16 s = mono_array_get (data, guint16, i);
2065 in6addr.s6_addr[2 * i + 1] = (s >> 8) & 0xff;
2066 in6addr.s6_addr[2 * i] = s & 0xff;
2070 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
2074 #endif /* AF_INET6 */
2077 #if defined(HAVE_GETIFADDRS) && defined(HAVE_IF_NAMETOINDEX)
2079 get_local_interface_id (int family)
2081 struct ifaddrs *ifap = NULL, *ptr;
2084 if (getifaddrs (&ifap)) {
2088 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2089 if (!ptr->ifa_addr || !ptr->ifa_name)
2091 if (ptr->ifa_addr->sa_family != family)
2093 if ((ptr->ifa_flags & IFF_LOOPBACK) != 0)
2095 if ((ptr->ifa_flags & IFF_MULTICAST) == 0)
2098 idx = if_nametoindex (ptr->ifa_name);
2107 get_local_interface_id (int family)
2113 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)
2115 struct linger linger;
2125 #ifdef HAVE_SOL_IPV6
2126 sol_ipv6 = SOL_IPV6;
2129 struct protoent *pent;
2130 pent = getprotobyname ("ipv6");
2131 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2139 struct protoent *pent;
2140 pent = getprotobyname ("ip");
2141 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2144 #endif /* AF_INET6 */
2146 MONO_ARCH_SAVE_REGS;
2148 ret=convert_sockopt_level_and_name(level, name, &system_level,
2151 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2152 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
2153 system_name = SO_REUSEADDR;
2154 int_val = int_val ? 0 : 1;
2160 *error = WSAENOPROTOOPT;
2167 /* Only one of obj_val, byte_val or int_val has data */
2169 MonoClassField *field;
2173 case SocketOptionName_Linger:
2174 /* Dig out "bool enabled" and "int seconds"
2177 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2178 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2179 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2180 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2182 valsize=sizeof(linger);
2183 ret = _wapi_setsockopt (sock, system_level,
2184 system_name, &linger, valsize);
2186 case SocketOptionName_AddMembership:
2187 case SocketOptionName_DropMembership:
2188 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2190 MonoObject *address = NULL;
2193 if(system_level == sol_ipv6) {
2194 struct ipv6_mreq mreq6;
2199 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2200 address = *(gpointer *)(((char *)obj_val) + field->offset);
2203 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2206 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2207 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2209 #if defined(__APPLE__)
2213 * Mac OS Lion doesn't allow ipv6mr_interface = 0.
2215 * Tests on Windows and Linux show that the multicast group is only
2216 * joined on one NIC when interface = 0, so we simply use the interface
2217 * id from the first non-loopback interface (this is also what
2218 * Dns.GetHostName (string.Empty) would return).
2220 if (!mreq6.ipv6mr_interface)
2221 mreq6.ipv6mr_interface = get_local_interface_id (AF_INET6);
2224 ret = _wapi_setsockopt (sock, system_level,
2225 system_name, &mreq6,
2227 } else if(system_level == sol_ip)
2228 #endif /* AF_INET6 */
2230 #ifdef HAVE_STRUCT_IP_MREQN
2231 struct ip_mreqn mreq = {{0}};
2233 struct ip_mreq mreq = {{0}};
2234 #endif /* HAVE_STRUCT_IP_MREQN */
2236 /* pain! MulticastOption holds two IPAddress
2237 * members, so I have to dig the value out of
2240 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2241 address = *(gpointer *)(((char *)obj_val) + field->offset);
2243 /* address might not be defined and if so, set the address to ADDR_ANY.
2246 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2249 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2250 address = *(gpointer *)(((char *)obj_val) + field->offset);
2252 #ifdef HAVE_STRUCT_IP_MREQN
2254 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2258 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2260 #endif /* HAVE_STRUCT_IP_MREQN */
2262 ret = _wapi_setsockopt (sock, system_level,
2268 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2270 /* Cause an exception to be thrown */
2274 } else if (byte_val!=NULL) {
2275 int valsize = mono_array_length (byte_val);
2276 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2279 case SocketOptionName_DontLinger:
2281 linger.l_onoff = (*buf) ? 0 : 1;
2282 linger.l_linger = 0;
2283 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2289 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2293 /* ReceiveTimeout/SendTimeout get here */
2295 case SocketOptionName_DontLinger:
2296 linger.l_onoff = !int_val;
2297 linger.l_linger = 0;
2298 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2300 case SocketOptionName_DontFragment:
2301 #ifdef HAVE_IP_MTU_DISCOVER
2302 /* Fiddle with the value slightly if we're
2306 int_val = IP_PMTUDISC_DO;
2312 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2316 if(ret==SOCKET_ERROR) {
2317 *error = WSAGetLastError ();
2321 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2327 MONO_ARCH_SAVE_REGS;
2331 /* Currently, the values for how (recv=0, send=1, both=2) match
2334 ret = _wapi_shutdown (sock, how);
2335 if(ret==SOCKET_ERROR) {
2336 *error = WSAGetLastError ();
2341 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2343 MonoArray *output, gint32 *error)
2345 glong output_bytes = 0;
2346 gchar *i_buffer, *o_buffer;
2350 MONO_ARCH_SAVE_REGS;
2354 if (code == FIONBIO) {
2355 /* Invalid command. Must use Socket.Blocking */
2359 if (input == NULL) {
2363 i_buffer = mono_array_addr (input, gchar, 0);
2364 i_len = mono_array_length (input);
2367 if (output == NULL) {
2371 o_buffer = mono_array_addr (output, gchar, 0);
2372 o_len = mono_array_length (output);
2375 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2376 if (ret == SOCKET_ERROR) {
2377 *error = WSAGetLastError ();
2381 return (gint) output_bytes;
2384 #ifdef HAVE_SIOCGIFCONF
2386 is_loopback (int family, void *ad)
2388 char *ptr = (char *) ad;
2390 if (family == AF_INET) {
2391 return (ptr [0] == 127);
2395 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2402 get_local_ips (int family, int *nips)
2404 int addr_size, offset, fd, i, count;
2405 int max_ifaces = 50; /* 50 interfaces should be enough... */
2408 struct ifreq iflags;
2409 char *result, *tmp_ptr;
2410 gboolean ignore_loopback = FALSE;
2413 if (family == AF_INET) {
2414 addr_size = sizeof (struct in_addr);
2415 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2417 } else if (family == AF_INET6) {
2418 addr_size = sizeof (struct in6_addr);
2419 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2425 fd = socket (family, SOCK_STREAM, 0);
2427 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2428 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2429 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2431 g_free (ifc.ifc_buf);
2435 count = ifc.ifc_len / sizeof (struct ifreq);
2438 g_free (ifc.ifc_buf);
2443 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2444 strcpy (iflags.ifr_name, ifr->ifr_name);
2445 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2449 if ((iflags.ifr_flags & IFF_UP) == 0) {
2450 ifr->ifr_name [0] = '\0';
2454 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2455 ignore_loopback = TRUE;
2460 result = g_malloc (addr_size * count);
2462 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2463 if (ifr->ifr_name [0] == '\0') {
2468 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2473 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2474 tmp_ptr += addr_size;
2477 g_free (ifc.ifc_buf);
2480 #elif defined(HAVE_GETIFADDRS)
2482 is_loopback (int family, void *ad)
2484 char *ptr = (char *) ad;
2486 if (family == AF_INET) {
2487 return (ptr [0] == 127);
2491 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2498 get_local_ips (int family, int *nips)
2500 struct ifaddrs *ifap = NULL, *ptr;
2501 int addr_size, offset, count, i;
2502 char *result, *tmp_ptr;
2505 if (family == AF_INET) {
2506 addr_size = sizeof (struct in_addr);
2507 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2509 } else if (family == AF_INET6) {
2510 addr_size = sizeof (struct in6_addr);
2511 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2517 if (getifaddrs (&ifap)) {
2522 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2525 if (ptr->ifa_addr->sa_family != family)
2527 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2532 result = g_malloc (addr_size * count);
2534 for (i = 0, ptr = ifap; ptr; ptr = ptr->ifa_next) {
2537 if (ptr->ifa_addr->sa_family != family)
2539 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2542 memcpy (tmp_ptr, ((char *) ptr->ifa_addr) + offset, addr_size);
2543 tmp_ptr += addr_size;
2552 get_local_ips (int family, int *nips)
2558 #endif /* HAVE_SIOCGIFCONF */
2561 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2562 MonoArray **h_aliases,
2563 MonoArray **h_addr_list,
2564 gboolean add_local_ips)
2566 MonoDomain *domain = mono_domain_get ();
2568 struct in_addr *local_in = NULL;
2572 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2576 *h_name=mono_string_new(domain, he->h_name);
2578 while(he->h_aliases[i]!=NULL) {
2582 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2584 while(he->h_aliases[i]!=NULL) {
2587 alias=mono_string_new(domain, he->h_aliases[i]);
2588 mono_array_setref (*h_aliases, i, alias);
2591 } else if (!add_local_ips) {
2595 if (add_local_ips) {
2596 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2598 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2599 for (i = 0; i < nlocal_in; i++) {
2600 MonoString *addr_string;
2601 char addr [16], *ptr;
2603 ptr = (char *) &local_in [i];
2604 g_snprintf(addr, 16, "%u.%u.%u.%u",
2605 (unsigned char) ptr [0],
2606 (unsigned char) ptr [1],
2607 (unsigned char) ptr [2],
2608 (unsigned char) ptr [3]);
2610 addr_string = mono_string_new (domain, addr);
2611 mono_array_setref (*h_addr_list, i, addr_string);
2616 } else if (he == NULL) {
2617 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2618 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2619 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2624 if (nlocal_in == 0 && he != NULL) {
2626 while (he->h_addr_list[i]!=NULL) {
2630 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2632 while(he->h_addr_list[i]!=NULL) {
2633 MonoString *addr_string;
2636 g_snprintf(addr, 16, "%u.%u.%u.%u",
2637 (unsigned char)he->h_addr_list[i][0],
2638 (unsigned char)he->h_addr_list[i][1],
2639 (unsigned char)he->h_addr_list[i][2],
2640 (unsigned char)he->h_addr_list[i][3]);
2642 addr_string=mono_string_new(domain, addr);
2643 mono_array_setref (*h_addr_list, i, addr_string);
2651 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2652 MonoArray **h_aliases,
2653 MonoArray **h_addr_list)
2655 MonoDomain *domain = mono_domain_get ();
2657 *h_name=mono_string_new(domain, addr);
2658 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2659 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2660 mono_array_setref (*h_addr_list, 0, *h_name);
2666 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2667 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2668 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2670 MonoDomain *domain = mono_domain_get ();
2671 int i, host_count, host_index, family_hint;
2672 struct in_addr *local_in = NULL;
2674 struct in6_addr *local_in6 = NULL;
2676 gboolean from_local = FALSE;
2678 family_hint = get_family_hint ();
2681 * Check if address length and family are correct
2683 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2687 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2692 * Get the aliases and host name from he1 or he2 whichever is
2693 * not null, if he1 is not null then take aliases from he1
2695 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2696 family_hint == PF_INET)) {
2697 *h_name=mono_string_new (domain, he1->h_name);
2700 while(he1->h_aliases[i]!=NULL) {
2704 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2707 while(he1->h_aliases[i]!=NULL) {
2710 alias=mono_string_new (domain, he1->h_aliases[i]);
2711 mono_array_setref (*h_aliases, i, alias);
2714 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2715 family_hint == PF_INET6)) {
2716 *h_name=mono_string_new (domain, he2->h_name);
2719 while(he2->h_aliases [i] != NULL) {
2723 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2726 while(he2->h_aliases[i]!=NULL) {
2729 alias=mono_string_new (domain, he2->h_aliases[i]);
2730 mono_array_setref (*h_aliases, i, alias);
2733 } else if (!add_local_ips) {
2738 * Count the number of addresses in he1 + he2
2741 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2742 family_hint == PF_INET)) {
2744 while(he1->h_addr_list[i]!=NULL) {
2750 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2751 family_hint == PF_INET6)) {
2753 while(he2->h_addr_list[i]!=NULL) {
2763 if (add_local_ips) {
2764 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2765 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2767 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2768 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2770 if (nlocal_in || nlocal_in6) {
2772 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2773 nlocal_in + nlocal_in6);
2777 for (n = 0; n < nlocal_in6; n++) {
2778 MonoString *addr_string;
2780 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2782 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2785 addr_string = mono_string_new (domain, addr);
2786 mono_array_setref (*h_addr_list, host_index, addr_string);
2794 for (n = 0; n < nlocal_in; n++) {
2795 MonoString *addr_string;
2797 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2799 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2802 addr_string = mono_string_new (domain, addr);
2803 mono_array_setref (*h_addr_list, host_index, addr_string);
2811 } else if (he1 == NULL && he2 == NULL) {
2812 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2813 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2814 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2824 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2826 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2827 family_hint == PF_INET6)) {
2829 while(he2->h_addr_list[i] != NULL) {
2830 MonoString *addr_string;
2832 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2834 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2838 addr_string = mono_string_new (domain, addr);
2839 mono_array_setref (*h_addr_list, host_index, addr_string);
2846 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2847 family_hint == PF_INET)) {
2849 while(he1->h_addr_list[i] != NULL) {
2850 MonoString *addr_string;
2852 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2854 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2858 addr_string=mono_string_new (domain, addr);
2859 mono_array_setref (*h_addr_list, host_index, addr_string);
2870 #if defined(AF_INET6)
2872 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2873 MonoArray **h_aliases,
2874 MonoArray **h_addr_list,
2875 gboolean add_local_ips)
2878 struct addrinfo *ai = NULL;
2879 struct in_addr *local_in = NULL;
2881 struct in6_addr *local_in6 = NULL;
2885 MonoDomain *domain = mono_domain_get ();
2888 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2889 if (add_local_ips) {
2890 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2891 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2892 if (nlocal_in || nlocal_in6) {
2893 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2895 MonoString *addr_string;
2899 for (i = 0; i < nlocal_in; i++) {
2900 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2901 addr_string = mono_string_new (domain, addr);
2902 mono_array_setref (*h_addr_list, addr_index, addr_string);
2908 MonoString *addr_string;
2913 for (i = 0; i < nlocal_in6; i++) {
2914 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2916 addr_string = mono_string_new (domain, addr);
2917 mono_array_setref (*h_addr_list, addr_index, addr_string);
2926 freeaddrinfo (info);
2935 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2936 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2942 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2944 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2945 MonoString *addr_string;
2947 char buffer [48]; /* Max. size for IPv6 */
2949 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2953 if(ai->ai_family == PF_INET) {
2954 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2956 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2960 addr_string=mono_string_new(domain, buffer);
2962 addr_string=mono_string_new(domain, "");
2965 mono_array_setref (*h_addr_list, addr_index, addr_string);
2969 if (ai->ai_canonname != NULL) {
2970 *h_name=mono_string_new(domain, ai->ai_canonname);
2972 *h_name=mono_string_new(domain, buffer);
2988 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2990 gboolean add_local_ips = FALSE;
2991 #ifdef HAVE_SIOCGIFCONF
2992 gchar this_hostname [256];
2994 #if !defined(HAVE_GETHOSTBYNAME2_R)
2995 struct addrinfo *info = NULL, hints;
2998 MONO_ARCH_SAVE_REGS;
3000 hostname=mono_string_to_utf8 (host);
3001 if (*hostname == '\0') {
3002 add_local_ips = TRUE;
3005 #ifdef HAVE_SIOCGIFCONF
3006 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3007 if (!strcmp (hostname, this_hostname)) {
3008 add_local_ips = TRUE;
3014 memset(&hints, 0, sizeof(hints));
3015 hints.ai_family = get_family_hint ();
3016 hints.ai_socktype = SOCK_STREAM;
3017 hints.ai_flags = AI_CANONNAME;
3019 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
3025 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
3027 struct hostent he1,*hp1, he2, *hp2;
3028 int buffer_size1, buffer_size2;
3029 char *buffer1, *buffer2;
3031 gboolean return_value;
3034 MONO_ARCH_SAVE_REGS;
3036 hostname=mono_string_to_utf8 (host);
3037 if (*hostname == '\0')
3038 add_local_ips = TRUE;
3040 #ifdef HAVE_SIOCGIFCONF
3041 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3042 if (!strcmp (hostname, this_hostname))
3043 add_local_ips = TRUE;
3049 buffer1 = g_malloc0(buffer_size1);
3050 buffer2 = g_malloc0(buffer_size2);
3054 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
3055 &hp1, &herr) == ERANGE) {
3057 buffer1 = g_realloc(buffer1, buffer_size1);
3060 if (*hostname && hp1 == NULL)
3062 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
3063 buffer_size2, &hp2, &herr) == ERANGE) {
3065 buffer2 = g_realloc(buffer2, buffer_size2);
3069 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
3070 h_addr_list, add_local_ips);
3076 return(return_value);
3077 #endif /* HAVE_GETHOSTBYNAME2_R */
3079 #else /* AF_INET6 */
3080 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3084 gboolean add_local_ips = FALSE;
3085 #ifdef HAVE_SIOCGIFCONF
3086 gchar this_hostname [256];
3089 MONO_ARCH_SAVE_REGS;
3091 hostname=mono_string_to_utf8(host);
3092 if (*hostname == '\0')
3093 add_local_ips = TRUE;
3094 #ifdef HAVE_SIOCGIFCONF
3095 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3096 if (!strcmp (hostname, this_hostname))
3097 add_local_ips = TRUE;
3104 he = _wapi_gethostbyname (hostname);
3106 he = _wapi_gethostbyname (hostname);
3110 if (*hostname && he==NULL)
3113 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
3115 #endif /* AF_INET6 */
3117 #ifndef HAVE_INET_PTON
3119 inet_pton (int family, const char *address, void *inaddrp)
3121 if (family == AF_INET) {
3122 #ifdef HAVE_INET_ATON
3123 struct in_addr inaddr;
3125 if (!inet_aton (address, &inaddr))
3128 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
3131 /* assume the system has inet_addr(), if it doesn't
3132 have that we're pretty much screwed... */
3135 if (!strcmp (address, "255.255.255.255")) {
3136 /* special-case hack */
3137 inaddr = 0xffffffff;
3139 inaddr = inet_addr (address);
3141 #define INADDR_NONE ((in_addr_t) -1)
3143 if (inaddr == INADDR_NONE)
3147 memcpy (inaddrp, &inaddr, sizeof (guint32));
3149 #endif /* HAVE_INET_ATON */
3154 #endif /* !HAVE_INET_PTON */
3156 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3161 struct sockaddr_in saddr;
3162 struct sockaddr_in6 saddr6;
3163 struct addrinfo *info = NULL, hints;
3165 char hostname[1024] = {0};
3168 struct in_addr inaddr;
3173 address = mono_string_to_utf8 (addr);
3176 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
3177 /* Maybe an ipv6 address */
3178 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
3184 saddr6.sin6_family = AF_INET6;
3189 saddr.sin_family = AF_INET;
3193 if(family == AF_INET) {
3194 #if HAVE_SOCKADDR_IN_SIN_LEN
3195 saddr.sin_len = sizeof (saddr);
3197 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
3198 hostname, sizeof(hostname), NULL, 0,
3202 } else if(family == AF_INET6) {
3203 #if HAVE_SOCKADDR_IN6_SIN_LEN
3204 saddr6.sin6_len = sizeof (saddr6);
3206 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
3207 hostname, sizeof(hostname), NULL, 0,
3213 memset (&hints, 0, sizeof(hints));
3214 hints.ai_family = get_family_hint ();
3215 hints.ai_socktype = SOCK_STREAM;
3216 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
3218 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
3222 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
3224 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
3229 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
3230 ret = ipaddr_to_IPHostEntry (address, h_name, h_aliases, h_addr_list);
3232 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
3233 h_addr_list, FALSE);
3241 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
3243 gchar hostname[256];
3246 MONO_ARCH_SAVE_REGS;
3248 ret = gethostname (hostname, sizeof (hostname));
3253 *h_name=mono_string_new(mono_domain_get (), hostname);
3259 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
3263 TRANSMIT_FILE_BUFFERS buffers;
3265 MONO_ARCH_SAVE_REGS;
3267 if (filename == NULL)
3270 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
3271 if (file == INVALID_HANDLE_VALUE) {
3272 SetLastError (error);
3276 memset (&buffers, 0, sizeof (buffers));
3277 if (pre_buffer != NULL) {
3278 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
3279 buffers.HeadLength = mono_array_length (pre_buffer);
3281 if (post_buffer != NULL) {
3282 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
3283 buffers.TailLength = mono_array_length (post_buffer);
3286 if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
3295 void mono_network_init(void)
3300 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3302 g_error("%s: Couldn't initialise networking", __func__);
3306 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3307 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3310 void mono_network_cleanup(void)
3316 icall_cancel_blocking_socket_operation (MonoThread *thread)
3318 MonoInternalThread *internal = thread->internal_thread;
3320 if (mono_thread_info_new_interrupt_enabled ()) {
3321 mono_thread_info_abort_socket_syscall_for_close ((MonoNativeThreadId)(gsize)internal->tid);
3324 internal->ignore_next_signal = TRUE;
3325 mono_thread_kill (internal, mono_thread_get_abort_signal ());
3330 #endif /* #ifndef DISABLE_SOCKETS */