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>
53 #include <mono/utils/mono-memory-model.h>
56 #ifdef HAVE_SYS_TIME_H
59 #ifdef HAVE_SYS_IOCTL_H
60 #include <sys/ioctl.h>
69 #ifdef HAVE_SYS_FILIO_H
70 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
72 #ifdef HAVE_SYS_SOCKIO_H
73 #include <sys/sockio.h> /* defines SIOCATMARK */
79 #ifdef HAVE_GETIFADDRS
80 // <net/if.h> must be included before <ifaddrs.h>
84 #include "mono/io-layer/socket-wrappers.h"
86 #if defined(HOST_WIN32)
87 /* This is a kludge to make this file build under cygwin:
88 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
89 * prototypes for some but not all required functions (notably
90 * inet_ntop() is missing), but the libws2_32 library is missing the
91 * actual implementations of these functions.
97 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
100 * Some older versions of libc provide IPV6 support without defining the AI_ADDRCONFIG
101 * flag for getaddrinfo.
103 #ifndef AI_ADDRCONFIG
104 #define AI_ADDRCONFIG 0
107 static gint32 convert_family(MonoAddressFamily mono_family)
111 switch(mono_family) {
112 case AddressFamily_Unknown:
113 case AddressFamily_ImpLink:
114 case AddressFamily_Pup:
115 case AddressFamily_Chaos:
116 case AddressFamily_Iso:
117 case AddressFamily_Ecma:
118 case AddressFamily_DataKit:
119 case AddressFamily_Ccitt:
120 case AddressFamily_DataLink:
121 case AddressFamily_Lat:
122 case AddressFamily_HyperChannel:
123 case AddressFamily_NetBios:
124 case AddressFamily_VoiceView:
125 case AddressFamily_FireFox:
126 case AddressFamily_Banyan:
127 case AddressFamily_Atm:
128 case AddressFamily_Cluster:
129 case AddressFamily_Ieee12844:
130 case AddressFamily_NetworkDesigners:
131 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
134 case AddressFamily_Unspecified:
138 case AddressFamily_Unix:
142 case AddressFamily_InterNetwork:
146 case AddressFamily_Ipx:
152 case AddressFamily_Sna:
158 case AddressFamily_DecNet:
164 case AddressFamily_AppleTalk:
168 case AddressFamily_InterNetworkV6:
173 case AddressFamily_Irda:
179 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
185 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
187 MonoAddressFamily family=AddressFamily_Unknown;
191 family=AddressFamily_Unspecified;
195 family=AddressFamily_Unix;
199 family=AddressFamily_InterNetwork;
204 family=AddressFamily_Ipx;
210 family=AddressFamily_Sna;
216 family=AddressFamily_DecNet;
221 family=AddressFamily_AppleTalk;
226 family=AddressFamily_InterNetworkV6;
232 family=AddressFamily_Irda;
236 g_warning("unknown address family 0x%x", af_family);
242 static gint32 convert_type(MonoSocketType mono_type)
247 case SocketType_Stream:
251 case SocketType_Dgram:
265 case SocketType_Seqpacket:
269 case SocketType_Unknown:
270 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
274 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
280 static gint32 convert_proto(MonoProtocolType mono_proto)
285 case ProtocolType_IP:
286 case ProtocolType_IPv6:
287 case ProtocolType_Icmp:
288 case ProtocolType_Igmp:
289 case ProtocolType_Ggp:
290 case ProtocolType_Tcp:
291 case ProtocolType_Pup:
292 case ProtocolType_Udp:
293 case ProtocolType_Idp:
294 /* These protocols are known (on my system at least) */
298 case ProtocolType_ND:
299 case ProtocolType_Raw:
300 case ProtocolType_Ipx:
301 case ProtocolType_Spx:
302 case ProtocolType_SpxII:
303 case ProtocolType_Unknown:
304 /* These protocols arent */
305 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
315 /* Convert MonoSocketFlags */
316 static gint32 convert_socketflags (gint32 sflags)
321 /* SocketFlags.None */
324 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
325 SocketFlags_DontRoute | SocketFlags_Partial))
326 /* Contains invalid flag values */
329 if (sflags & SocketFlags_OutOfBand)
331 if (sflags & SocketFlags_Peek)
333 if (sflags & SocketFlags_DontRoute)
334 flags |= MSG_DONTROUTE;
336 /* Ignore Partial - see bug 349688. Don't return -1, because
337 * according to the comment in that bug ms runtime doesn't for
338 * UDP sockets (this means we will silently ignore it for TCP
342 if (sflags & SocketFlags_Partial)
346 /* Don't do anything for MaxIOVectorLength */
347 if (sflags & SocketFlags_MaxIOVectorLength)
355 * 0 on success (mapped mono_level and mono_name to system_level and system_name
357 * -2 on non-fatal error (ie, must ignore)
359 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
360 MonoSocketOptionName mono_name,
364 switch (mono_level) {
365 case SocketOptionLevel_Socket:
366 *system_level = SOL_SOCKET;
369 case SocketOptionName_DontLinger:
370 /* This is SO_LINGER, because the setsockopt
371 * internal call maps DontLinger to SO_LINGER
374 *system_name = SO_LINGER;
376 case SocketOptionName_Debug:
377 *system_name = SO_DEBUG;
380 case SocketOptionName_AcceptConnection:
381 *system_name = SO_ACCEPTCONN;
384 case SocketOptionName_ReuseAddress:
385 *system_name = SO_REUSEADDR;
387 case SocketOptionName_KeepAlive:
388 *system_name = SO_KEEPALIVE;
390 case SocketOptionName_DontRoute:
391 *system_name = SO_DONTROUTE;
393 case SocketOptionName_Broadcast:
394 *system_name = SO_BROADCAST;
396 case SocketOptionName_Linger:
397 *system_name = SO_LINGER;
399 case SocketOptionName_OutOfBandInline:
400 *system_name = SO_OOBINLINE;
402 case SocketOptionName_SendBuffer:
403 *system_name = SO_SNDBUF;
405 case SocketOptionName_ReceiveBuffer:
406 *system_name = SO_RCVBUF;
408 case SocketOptionName_SendLowWater:
409 *system_name = SO_SNDLOWAT;
411 case SocketOptionName_ReceiveLowWater:
412 *system_name = SO_RCVLOWAT;
414 case SocketOptionName_SendTimeout:
415 *system_name = SO_SNDTIMEO;
417 case SocketOptionName_ReceiveTimeout:
418 *system_name = SO_RCVTIMEO;
420 case SocketOptionName_Error:
421 *system_name = SO_ERROR;
423 case SocketOptionName_Type:
424 *system_name = SO_TYPE;
427 case SocketOptionName_PeerCred:
428 *system_name = SO_PEERCRED;
431 case SocketOptionName_ExclusiveAddressUse:
432 #ifdef SO_EXCLUSIVEADDRUSE
433 *system_name = SO_EXCLUSIVEADDRUSE;
436 case SocketOptionName_UseLoopback:
437 #ifdef SO_USELOOPBACK
438 *system_name = SO_USELOOPBACK;
441 case SocketOptionName_MaxConnections:
443 *system_name = SO_MAXCONN;
445 #elif defined(SOMAXCONN)
446 *system_name = SOMAXCONN;
450 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
455 case SocketOptionLevel_IP:
457 *system_level = SOL_IP;
460 static int cached = 0;
464 struct protoent *pent;
466 pent = getprotobyname ("IP");
467 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
471 *system_level = proto;
473 #endif /* HAVE_SOL_IP */
476 case SocketOptionName_IPOptions:
477 *system_name = IP_OPTIONS;
480 case SocketOptionName_HeaderIncluded:
481 *system_name = IP_HDRINCL;
485 case SocketOptionName_TypeOfService:
486 *system_name = IP_TOS;
490 case SocketOptionName_IpTimeToLive:
491 *system_name = IP_TTL;
494 case SocketOptionName_MulticastInterface:
495 *system_name = IP_MULTICAST_IF;
497 case SocketOptionName_MulticastTimeToLive:
498 *system_name = IP_MULTICAST_TTL;
500 case SocketOptionName_MulticastLoopback:
501 *system_name = IP_MULTICAST_LOOP;
503 case SocketOptionName_AddMembership:
504 *system_name = IP_ADD_MEMBERSHIP;
506 case SocketOptionName_DropMembership:
507 *system_name = IP_DROP_MEMBERSHIP;
509 #ifdef HAVE_IP_PKTINFO
510 case SocketOptionName_PacketInformation:
511 *system_name = IP_PKTINFO;
513 #endif /* HAVE_IP_PKTINFO */
515 case SocketOptionName_DontFragment:
516 #ifdef HAVE_IP_DONTFRAGMENT
517 *system_name = IP_DONTFRAGMENT;
519 #elif defined HAVE_IP_MTU_DISCOVER
520 /* Not quite the same */
521 *system_name = IP_MTU_DISCOVER;
524 /* If the flag is not available on this system, we can ignore this error */
526 #endif /* HAVE_IP_DONTFRAGMENT */
527 case SocketOptionName_AddSourceMembership:
528 case SocketOptionName_DropSourceMembership:
529 case SocketOptionName_BlockSource:
530 case SocketOptionName_UnblockSource:
531 /* Can't figure out how to map these, so fall
535 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
541 case SocketOptionLevel_IPv6:
543 *system_level = SOL_IPV6;
546 static int cached = 0;
550 struct protoent *pent;
552 pent = getprotobyname ("IPV6");
553 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
557 *system_level = proto;
559 #endif /* HAVE_SOL_IPV6 */
562 case SocketOptionName_IpTimeToLive:
563 case SocketOptionName_HopLimit:
564 *system_name = IPV6_UNICAST_HOPS;
566 case SocketOptionName_MulticastInterface:
567 *system_name = IPV6_MULTICAST_IF;
569 case SocketOptionName_MulticastTimeToLive:
570 *system_name = IPV6_MULTICAST_HOPS;
572 case SocketOptionName_MulticastLoopback:
573 *system_name = IPV6_MULTICAST_LOOP;
575 case SocketOptionName_AddMembership:
576 *system_name = IPV6_JOIN_GROUP;
578 case SocketOptionName_DropMembership:
579 *system_name = IPV6_LEAVE_GROUP;
581 case SocketOptionName_PacketInformation:
582 #ifdef HAVE_IPV6_PKTINFO
583 *system_name = IPV6_PKTINFO;
586 case SocketOptionName_HeaderIncluded:
587 case SocketOptionName_IPOptions:
588 case SocketOptionName_TypeOfService:
589 case SocketOptionName_DontFragment:
590 case SocketOptionName_AddSourceMembership:
591 case SocketOptionName_DropSourceMembership:
592 case SocketOptionName_BlockSource:
593 case SocketOptionName_UnblockSource:
594 /* Can't figure out how to map these, so fall
598 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
602 break; /* SocketOptionLevel_IPv6 */
605 case SocketOptionLevel_Tcp:
607 *system_level = SOL_TCP;
610 static int cached = 0;
614 struct protoent *pent;
616 pent = getprotobyname ("TCP");
617 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
621 *system_level = proto;
623 #endif /* HAVE_SOL_TCP */
626 case SocketOptionName_NoDelay:
627 *system_name = TCP_NODELAY;
630 /* The documentation is talking complete
631 * bollocks here: rfc-1222 is titled
632 * 'Advancing the NSFNET Routing Architecture'
633 * and doesn't mention either of the words
634 * "expedite" or "urgent".
636 case SocketOptionName_BsdUrgent:
637 case SocketOptionName_Expedited:
640 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
645 case SocketOptionLevel_Udp:
646 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
649 case SocketOptionName_NoChecksum:
650 case SocketOptionName_ChecksumCoverage:
652 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
659 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
666 static MonoImage *get_socket_assembly (void)
668 MonoDomain *domain = mono_domain_get ();
670 if (domain->socket_assembly == NULL) {
671 MonoImage *socket_assembly;
673 socket_assembly = mono_image_loaded ("System");
674 if (!socket_assembly) {
675 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
678 g_assert_not_reached ();
680 socket_assembly = mono_assembly_get_image (sa);
683 mono_atomic_store_release (&domain->socket_assembly, socket_assembly);
686 return domain->socket_assembly;
690 static gint32 get_family_hint(void)
692 MonoDomain *domain = mono_domain_get ();
694 if (!domain->inet_family_hint) {
695 MonoClass *socket_class;
696 MonoClassField *ipv6_field, *ipv4_field;
697 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
700 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
701 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
702 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
703 vtable = mono_class_vtable (mono_domain_get (), socket_class);
705 mono_runtime_class_init (vtable);
707 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
708 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
710 mono_domain_lock (domain);
711 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
712 domain->inet_family_hint = 1;
713 } else if (ipv4_enabled == 1) {
714 domain->inet_family_hint = 2;
716 domain->inet_family_hint = 3;
718 mono_domain_unlock (domain);
720 switch (domain->inet_family_hint) {
721 case 1: return PF_UNSPEC;
722 case 2: return PF_INET;
723 case 3: return PF_INET6;
730 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
741 sock_family=convert_family(family);
742 if(sock_family==-1) {
743 *error = WSAEAFNOSUPPORT;
747 sock_proto=convert_proto(proto);
749 *error = WSAEPROTONOSUPPORT;
753 sock_type=convert_type(type);
755 *error = WSAESOCKTNOSUPPORT;
759 sock = _wapi_socket (sock_family, sock_type, sock_proto,
760 NULL, 0, WSA_FLAG_OVERLAPPED);
762 if(sock==INVALID_SOCKET) {
763 *error = WSAGetLastError ();
767 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
768 return (GUINT_TO_POINTER (sock));
772 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
773 return (GUINT_TO_POINTER (sock));
777 return(GUINT_TO_POINTER (sock));
780 /* FIXME: the SOCKET parameter (here and in other functions in this
781 * file) is really an IntPtr which needs to be converted to a guint32.
783 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
788 LOGDEBUG (g_message ("%s: closing 0x%x", __func__, sock));
792 /* Clear any pending work item from this socket if the underlying
793 * polling system does not notify when the socket is closed */
794 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
798 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
802 LOGDEBUG (g_message("%s: returning %d", __func__, WSAGetLastError()));
804 return(WSAGetLastError());
807 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
817 ret=ioctlsocket(sock, FIONREAD, &amount);
818 if(ret==SOCKET_ERROR) {
819 *error = WSAGetLastError ();
826 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
837 * block == TRUE/FALSE means we will block/not block.
838 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
842 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
843 if(ret==SOCKET_ERROR) {
844 *error = WSAGetLastError ();
848 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
859 MonoInternalThread* curthread = mono_thread_internal_current ();
860 curthread->interrupt_on_stop = (gpointer)TRUE;
861 newsock = _wapi_accept (sock, NULL, 0);
862 curthread->interrupt_on_stop = (gpointer)FALSE;
865 newsock = _wapi_accept (sock, NULL, 0);
867 if(newsock==INVALID_SOCKET) {
868 *error = WSAGetLastError ();
872 return(GUINT_TO_POINTER (newsock));
875 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
885 ret = _wapi_listen (sock, backlog);
886 if(ret==SOCKET_ERROR) {
887 *error = WSAGetLastError ();
892 // Check whether it's ::ffff::0:0.
894 is_ipv4_mapped_any (const struct in6_addr *addr)
898 for (i = 0; i < 10; i++) {
899 if (addr->s6_addr [i])
902 if ((addr->s6_addr [10] != 0xff) || (addr->s6_addr [11] != 0xff))
904 for (i = 12; i < 16; i++) {
905 if (addr->s6_addr [i])
912 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
913 int sa_size, gint32 *error)
915 MonoDomain *domain = mono_domain_get ();
916 MonoObject *sockaddr_obj;
918 MonoAddressFamily family;
920 /* Build a System.Net.SocketAddress object instance */
921 if (!domain->sockaddr_class) {
922 domain->sockaddr_class=mono_class_from_name (get_socket_assembly (), "System.Net", "SocketAddress");
923 g_assert (domain->sockaddr_class);
925 sockaddr_obj=mono_object_new(domain, domain->sockaddr_class);
927 /* Locate the SocketAddress data buffer in the object */
928 if (!domain->sockaddr_data_field) {
929 domain->sockaddr_data_field=mono_class_get_field_from_name (domain->sockaddr_class, "data");
930 g_assert (domain->sockaddr_data_field);
933 /* Make sure there is space for the family and size bytes */
935 if (saddr->sa_family == AF_UNIX) {
936 /* sa_len includes the entire sockaddr size, so we don't need the
937 * N bytes (sizeof (unsigned short)) of the family. */
938 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size);
942 /* May be the +2 here is too conservative, as sa_len returns
943 * the length of the entire sockaddr_in/in6, including
944 * sizeof (unsigned short) of the family */
945 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
948 /* The data buffer is laid out as follows:
949 * bytes 0 and 1 are the address family
950 * bytes 2 and 3 are the port info
951 * the rest is the address info
954 family=convert_to_mono_family(saddr->sa_family);
955 if(family==AddressFamily_Unknown) {
956 *error = WSAEAFNOSUPPORT;
960 mono_array_set(data, guint8, 0, family & 0x0FF);
961 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
963 if(saddr->sa_family==AF_INET) {
964 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
965 guint16 port=ntohs(sa_in->sin_port);
966 guint32 address=ntohl(sa_in->sin_addr.s_addr);
969 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
972 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
973 mono_array_set(data, guint8, 3, (port) & 0xff);
974 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
975 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
976 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
977 mono_array_set(data, guint8, 7, (address) & 0xff);
979 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
981 return(sockaddr_obj);
983 } else if (saddr->sa_family == AF_INET6) {
984 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
987 guint16 port=ntohs(sa_in->sin6_port);
990 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
993 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
994 mono_array_set(data, guint8, 3, (port) & 0xff);
996 if (is_ipv4_mapped_any (&sa_in->sin6_addr)) {
997 // Map ::ffff:0:0 to :: (bug #5502)
998 for(i=0; i<16; i++) {
999 mono_array_set(data, guint8, 8+i, 0);
1002 for(i=0; i<16; i++) {
1003 mono_array_set(data, guint8, 8+i,
1004 sa_in->sin6_addr.s6_addr[i]);
1008 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
1009 mono_array_set(data, guint8, 25,
1010 (sa_in->sin6_scope_id >> 8) & 0xff);
1011 mono_array_set(data, guint8, 26,
1012 (sa_in->sin6_scope_id >> 16) & 0xff);
1013 mono_array_set(data, guint8, 27,
1014 (sa_in->sin6_scope_id >> 24) & 0xff);
1016 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1018 return(sockaddr_obj);
1020 #ifdef HAVE_SYS_UN_H
1021 } else if (saddr->sa_family == AF_UNIX) {
1024 for (i = 0; i < sa_size; i++) {
1025 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
1028 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1030 return sockaddr_obj;
1033 *error = WSAEAFNOSUPPORT;
1039 get_sockaddr_size (int family)
1044 if (family == AF_INET) {
1045 size = sizeof (struct sockaddr_in);
1047 } else if (family == AF_INET6) {
1048 size = sizeof (struct sockaddr_in6);
1050 #ifdef HAVE_SYS_UN_H
1051 } else if (family == AF_UNIX) {
1052 size = sizeof (struct sockaddr_un);
1058 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1065 MONO_ARCH_SAVE_REGS;
1069 salen = get_sockaddr_size (convert_family (af));
1071 *error = WSAEAFNOSUPPORT;
1074 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1075 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1077 if(ret==SOCKET_ERROR) {
1078 *error = WSAGetLastError ();
1084 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)));
1086 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1092 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1099 MONO_ARCH_SAVE_REGS;
1103 salen = get_sockaddr_size (convert_family (af));
1105 *error = WSAEAFNOSUPPORT;
1108 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1109 /* Note: linux returns just 2 for AF_UNIX. Always. */
1110 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1111 if(ret==SOCKET_ERROR) {
1112 *error = WSAGetLastError ();
1118 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)));
1120 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1126 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1130 MonoClassField *field;
1135 /* Dig the SocketAddress data buffer out of the object */
1136 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1137 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1139 /* The data buffer is laid out as follows:
1140 * byte 0 is the address family low byte
1141 * byte 1 is the address family high byte
1143 * bytes 2 and 3 are the port info
1144 * the rest is the address info
1146 * the rest is the file name
1148 len = mono_array_length (data);
1150 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1153 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1154 if (family == AF_INET) {
1155 struct sockaddr_in *sa;
1160 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1163 sa = g_new0 (struct sockaddr_in, 1);
1164 port = (mono_array_get (data, guint8, 2) << 8) +
1165 mono_array_get (data, guint8, 3);
1166 address = (mono_array_get (data, guint8, 4) << 24) +
1167 (mono_array_get (data, guint8, 5) << 16 ) +
1168 (mono_array_get (data, guint8, 6) << 8) +
1169 mono_array_get (data, guint8, 7);
1171 sa->sin_family = family;
1172 sa->sin_addr.s_addr = htonl (address);
1173 sa->sin_port = htons (port);
1175 *sa_size = sizeof(struct sockaddr_in);
1176 return((struct sockaddr *)sa);
1179 } else if (family == AF_INET6) {
1180 struct sockaddr_in6 *sa;
1186 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1189 sa = g_new0 (struct sockaddr_in6, 1);
1190 port = mono_array_get (data, guint8, 3) +
1191 (mono_array_get (data, guint8, 2) << 8);
1192 scopeid = mono_array_get (data, guint8, 24) +
1193 (mono_array_get (data, guint8, 25) << 8) +
1194 (mono_array_get (data, guint8, 26) << 16) +
1195 (mono_array_get (data, guint8, 27) << 24);
1197 sa->sin6_family = family;
1198 sa->sin6_port = htons (port);
1199 sa->sin6_scope_id = scopeid;
1201 for(i=0; i<16; i++) {
1202 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1205 *sa_size = sizeof(struct sockaddr_in6);
1206 return((struct sockaddr *)sa);
1208 #ifdef HAVE_SYS_UN_H
1209 } else if (family == AF_UNIX) {
1210 struct sockaddr_un *sock_un;
1213 /* Need a byte for the '\0' terminator/prefix, and the first
1214 * two bytes hold the SocketAddress family
1216 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1217 mono_raise_exception (mono_get_exception_index_out_of_range ());
1220 sock_un = g_new0 (struct sockaddr_un, 1);
1222 sock_un->sun_family = family;
1223 for (i = 0; i < len - 2; i++) {
1224 sock_un->sun_path [i] = mono_array_get (data, guint8,
1230 return (struct sockaddr *)sock_un;
1233 *error = WSAEAFNOSUPPORT;
1238 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1240 struct sockaddr *sa;
1244 MONO_ARCH_SAVE_REGS;
1248 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1253 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)));
1255 ret = _wapi_bind (sock, sa, sa_size);
1256 if(ret==SOCKET_ERROR) {
1257 *error = WSAGetLastError ();
1270 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1271 gint timeout, gint32 *error)
1273 MonoInternalThread *thread = NULL;
1279 MONO_ARCH_SAVE_REGS;
1281 pfds = g_new0 (mono_pollfd, 1);
1282 pfds[0].fd = GPOINTER_TO_INT (sock);
1283 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1284 (mode == SelectModeWrite) ? MONO_POLLOUT :
1285 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1287 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1288 start = time (NULL);
1292 ret = mono_poll (pfds, 1, timeout);
1293 if (timeout > 0 && ret < 0) {
1295 int sec = time (NULL) - start;
1297 timeout -= sec * 1000;
1305 if (ret == -1 && errno == EINTR) {
1308 if (thread == NULL) {
1309 thread = mono_thread_internal_current ();
1312 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1318 /* Suspend requested? */
1319 mono_thread_interruption_checkpoint ();
1323 } while (ret == -1 && errno == EINTR);
1327 *error = WSAGetLastError ();
1329 *error = errno_to_WSA (errno, __func__);
1344 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1346 struct sockaddr *sa;
1350 MONO_ARCH_SAVE_REGS;
1354 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1359 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)));
1361 ret = _wapi_connect (sock, sa, sa_size);
1362 if(ret==SOCKET_ERROR) {
1363 *error = WSAGetLastError ();
1369 /* These #defines from mswsock.h from wine. Defining them here allows
1370 * us to build this file on a mingw box that doesn't know the magic
1371 * numbers, but still run on a newer windows box that does.
1373 #ifndef WSAID_DISCONNECTEX
1374 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1375 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1378 #ifndef WSAID_TRANSMITFILE
1379 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1380 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1383 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1386 glong output_bytes = 0;
1387 GUID disco_guid = WSAID_DISCONNECTEX;
1388 GUID trans_guid = WSAID_TRANSMITFILE;
1389 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1390 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1393 MONO_ARCH_SAVE_REGS;
1397 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1399 /* I _think_ the extension function pointers need to be looked
1400 * up for each socket. FIXME: check the best way to store
1401 * pointers to functions in managed objects that still works
1402 * on 64bit platforms.
1404 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1405 (void *)&disco_guid, sizeof(GUID),
1406 (void *)&_wapi_disconnectex, sizeof(void *),
1407 &output_bytes, NULL, NULL);
1409 /* make sure that WSAIoctl didn't put crap in the
1412 _wapi_disconnectex = NULL;
1414 /* Look up the TransmitFile extension function pointer
1415 * instead of calling TransmitFile() directly, because
1416 * apparently "Several of the extension functions have
1417 * been available since WinSock 1.1 and are exported
1418 * from MSWsock.dll, however it's not advisable to
1419 * link directly to this dll as this ties you to the
1420 * Microsoft WinSock provider. A provider neutral way
1421 * of accessing these extension functions is to load
1422 * them dynamically via WSAIoctl using the
1423 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1424 * should, theoretically, allow you to access these
1425 * functions from any provider that supports them..."
1426 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1428 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1429 (void *)&trans_guid, sizeof(GUID),
1430 (void *)&_wapi_transmitfile, sizeof(void *),
1431 &output_bytes, NULL, NULL);
1433 _wapi_transmitfile = NULL;
1437 if (_wapi_disconnectex != NULL) {
1438 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1439 } else if (_wapi_transmitfile != NULL) {
1440 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1441 TF_DISCONNECT | TF_REUSE_SOCKET);
1443 *error = ERROR_NOT_SUPPORTED;
1447 if (bret == FALSE) {
1448 *error = WSAGetLastError ();
1452 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1459 MONO_ARCH_SAVE_REGS;
1463 alen = mono_array_length (buffer);
1464 if (offset > alen - count) {
1468 buf=mono_array_addr(buffer, guchar, offset);
1470 recvflags = convert_socketflags (flags);
1471 if (recvflags == -1) {
1472 *error = WSAEOPNOTSUPP;
1478 MonoInternalThread* curthread = mono_thread_internal_current ();
1479 curthread->interrupt_on_stop = (gpointer)TRUE;
1480 ret = _wapi_recv (sock, buf, count, recvflags);
1481 curthread->interrupt_on_stop = (gpointer)FALSE;
1484 ret = _wapi_recv (sock, buf, count, recvflags);
1487 if(ret==SOCKET_ERROR) {
1488 *error = WSAGetLastError ();
1495 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1500 DWORD recvflags = 0;
1502 MONO_ARCH_SAVE_REGS;
1506 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1507 count = mono_array_length (buffers);
1509 recvflags = convert_socketflags (flags);
1510 if (recvflags == -1) {
1511 *error = WSAEOPNOTSUPP;
1515 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1516 if (ret == SOCKET_ERROR) {
1517 *error = WSAGetLastError ();
1524 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1530 struct sockaddr *sa;
1533 MONO_ARCH_SAVE_REGS;
1537 alen = mono_array_length (buffer);
1538 if (offset > alen - count) {
1542 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1547 buf=mono_array_addr(buffer, guchar, offset);
1549 recvflags = convert_socketflags (flags);
1550 if (recvflags == -1) {
1551 *error = WSAEOPNOTSUPP;
1555 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1556 if(ret==SOCKET_ERROR) {
1558 *error = WSAGetLastError ();
1562 /* If we didn't get a socket size, then we're probably a
1563 * connected connection-oriented socket and the stack hasn't
1564 * returned the remote address. All we can do is return null.
1567 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1576 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1583 MONO_ARCH_SAVE_REGS;
1587 alen = mono_array_length (buffer);
1588 if (offset > alen - count) {
1592 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1594 buf=mono_array_addr(buffer, guchar, offset);
1596 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1598 sendflags = convert_socketflags (flags);
1599 if (sendflags == -1) {
1600 *error = WSAEOPNOTSUPP;
1604 ret = _wapi_send (sock, buf, count, sendflags);
1605 if(ret==SOCKET_ERROR) {
1606 *error = WSAGetLastError ();
1613 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1618 DWORD sendflags = 0;
1620 MONO_ARCH_SAVE_REGS;
1624 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1625 count = mono_array_length (buffers);
1627 sendflags = convert_socketflags (flags);
1628 if (sendflags == -1) {
1629 *error = WSAEOPNOTSUPP;
1633 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1634 if (ret == SOCKET_ERROR) {
1635 *error = WSAGetLastError ();
1642 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1648 struct sockaddr *sa;
1651 MONO_ARCH_SAVE_REGS;
1655 alen = mono_array_length (buffer);
1656 if (offset > alen - count) {
1660 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1665 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1667 buf=mono_array_addr(buffer, guchar, offset);
1669 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1671 sendflags = convert_socketflags (flags);
1672 if (sendflags == -1) {
1673 *error = WSAEOPNOTSUPP;
1677 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1678 if(ret==SOCKET_ERROR) {
1679 *error = WSAGetLastError ();
1687 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1690 MonoClassField *field;
1692 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1693 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1698 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1699 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1701 MonoInternalThread *thread = NULL;
1708 MonoClass *sock_arr_class;
1711 uintptr_t socks_size;
1713 MONO_ARCH_SAVE_REGS;
1715 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1716 count = mono_array_length (*sockets);
1717 nfds = count - 3; /* NULL separators */
1718 pfds = g_new0 (mono_pollfd, nfds);
1720 for (i = 0; i < count; i++) {
1721 obj = mono_array_get (*sockets, MonoObject *, i);
1728 /* The socket array was bogus */
1734 pfds [idx].fd = Socket_to_SOCKET (obj);
1735 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1739 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1740 start = time (NULL);
1743 ret = mono_poll (pfds, nfds, timeout);
1744 if (timeout > 0 && ret < 0) {
1746 int sec = time (NULL) - start;
1748 timeout -= sec * 1000;
1754 if (ret == -1 && errno == EINTR) {
1757 thread = mono_thread_internal_current ();
1759 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1766 /* Suspend requested? */
1767 mono_thread_interruption_checkpoint ();
1771 } while (ret == -1 && errno == EINTR);
1775 *error = WSAGetLastError ();
1777 *error = errno_to_WSA (errno, __func__);
1789 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1790 socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
1791 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1794 for (i = 0; i < count && ret > 0; i++) {
1797 obj = mono_array_get (*sockets, MonoObject *, i);
1804 pfd = &pfds [i - mode];
1805 if (pfd->revents == 0)
1809 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1810 mono_array_setref (socks, idx++, obj);
1811 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1812 mono_array_setref (socks, idx++, obj);
1813 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1814 mono_array_setref (socks, idx++, obj);
1822 static MonoObject* int_to_object (MonoDomain *domain, int val)
1824 return mono_value_box (domain, mono_get_int32_class (), &val);
1828 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1834 socklen_t valsize=sizeof(val);
1835 struct linger linger;
1836 socklen_t lingersize=sizeof(linger);
1838 socklen_t time_ms_size = sizeof (time_ms);
1840 # if defined(__OpenBSD__)
1841 struct sockpeercred cred;
1845 socklen_t credsize = sizeof(cred);
1847 MonoDomain *domain=mono_domain_get();
1849 MonoClass *obj_class;
1850 MonoClassField *field;
1852 MONO_ARCH_SAVE_REGS;
1856 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1857 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
1858 system_level = SOL_SOCKET;
1859 system_name = SO_REUSEADDR;
1865 ret = convert_sockopt_level_and_name (level, name, &system_level, &system_name);
1869 *error = WSAENOPROTOOPT;
1873 *obj_val = int_to_object (domain, 0);
1877 /* No need to deal with MulticastOption names here, because
1878 * you cant getsockopt AddMembership or DropMembership (the
1879 * int getsockopt will error, causing an exception)
1882 case SocketOptionName_Linger:
1883 case SocketOptionName_DontLinger:
1884 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1888 case SocketOptionName_SendTimeout:
1889 case SocketOptionName_ReceiveTimeout:
1890 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1894 case SocketOptionName_PeerCred:
1895 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1901 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1905 if(ret==SOCKET_ERROR) {
1906 *error = WSAGetLastError ();
1911 case SocketOptionName_Linger:
1912 /* build a System.Net.Sockets.LingerOption */
1913 obj_class=mono_class_from_name(get_socket_assembly (),
1914 "System.Net.Sockets",
1916 obj=mono_object_new(domain, obj_class);
1918 /* Locate and set the fields "bool enabled" and "int
1921 field=mono_class_get_field_from_name(obj_class, "enabled");
1922 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1924 field=mono_class_get_field_from_name(obj_class, "seconds");
1925 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1929 case SocketOptionName_DontLinger:
1930 /* construct a bool int in val - true if linger is off */
1931 obj = int_to_object (domain, !linger.l_onoff);
1934 case SocketOptionName_SendTimeout:
1935 case SocketOptionName_ReceiveTimeout:
1936 obj = int_to_object (domain, time_ms);
1940 case SocketOptionName_PeerCred:
1942 /* build a Mono.Posix.PeerCred+PeerCredData if
1945 static MonoImage *mono_posix_image = NULL;
1946 MonoPeerCredData *cred_data;
1948 if (mono_posix_image == NULL) {
1949 mono_posix_image=mono_image_loaded ("Mono.Posix");
1950 if (!mono_posix_image) {
1951 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1953 *error = WSAENOPROTOOPT;
1956 mono_posix_image = mono_assembly_get_image (sa);
1961 obj_class = mono_class_from_name(mono_posix_image,
1964 obj = mono_object_new(domain, obj_class);
1965 cred_data = (MonoPeerCredData *)obj;
1966 cred_data->pid = cred.pid;
1967 cred_data->uid = cred.uid;
1968 cred_data->gid = cred.gid;
1974 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1975 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse)
1978 obj = int_to_object (domain, val);
1983 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1991 MONO_ARCH_SAVE_REGS;
1995 ret=convert_sockopt_level_and_name(level, name, &system_level,
1998 *error = WSAENOPROTOOPT;
2004 valsize=mono_array_length(*byte_val);
2005 buf=mono_array_addr(*byte_val, guchar, 0);
2007 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
2008 if(ret==SOCKET_ERROR) {
2009 *error = WSAGetLastError ();
2013 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2014 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
2016 struct in_addr inaddr;
2017 MonoClassField *field;
2019 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
2021 /* No idea why .net uses a 64bit type to hold a 32bit value...
2023 * Internal value of IPAddess is in little-endian order
2025 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
2031 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
2033 struct in6_addr in6addr;
2034 MonoClassField *field;
2038 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
2039 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
2041 /* Solaris has only the 8 bit version. */
2043 for(i=0; i<8; i++) {
2044 guint16 s = mono_array_get (data, guint16, i);
2045 in6addr.s6_addr[2 * i + 1] = (s >> 8) & 0xff;
2046 in6addr.s6_addr[2 * i] = s & 0xff;
2050 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
2054 #endif /* AF_INET6 */
2057 #if defined(HAVE_GETIFADDRS) && defined(HAVE_IF_NAMETOINDEX)
2059 get_local_interface_id (int family)
2061 struct ifaddrs *ifap = NULL, *ptr;
2064 if (getifaddrs (&ifap)) {
2068 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2069 if (!ptr->ifa_addr || !ptr->ifa_name)
2071 if (ptr->ifa_addr->sa_family != family)
2073 if ((ptr->ifa_flags & IFF_LOOPBACK) != 0)
2075 if ((ptr->ifa_flags & IFF_MULTICAST) == 0)
2078 idx = if_nametoindex (ptr->ifa_name);
2087 get_local_interface_id (int family)
2093 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)
2095 struct linger linger;
2105 #ifdef HAVE_SOL_IPV6
2106 sol_ipv6 = SOL_IPV6;
2109 struct protoent *pent;
2110 pent = getprotobyname ("ipv6");
2111 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2119 struct protoent *pent;
2120 pent = getprotobyname ("ip");
2121 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2124 #endif /* AF_INET6 */
2126 MONO_ARCH_SAVE_REGS;
2128 ret=convert_sockopt_level_and_name(level, name, &system_level,
2131 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2132 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
2133 system_name = SO_REUSEADDR;
2134 int_val = int_val ? 0 : 1;
2140 *error = WSAENOPROTOOPT;
2147 /* Only one of obj_val, byte_val or int_val has data */
2149 MonoClassField *field;
2153 case SocketOptionName_Linger:
2154 /* Dig out "bool enabled" and "int seconds"
2157 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2158 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2159 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2160 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2162 valsize=sizeof(linger);
2163 ret = _wapi_setsockopt (sock, system_level,
2164 system_name, &linger, valsize);
2166 case SocketOptionName_AddMembership:
2167 case SocketOptionName_DropMembership:
2168 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2170 MonoObject *address = NULL;
2173 if(system_level == sol_ipv6) {
2174 struct ipv6_mreq mreq6;
2179 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2180 address = *(gpointer *)(((char *)obj_val) + field->offset);
2183 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2186 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2187 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2189 #if defined(__APPLE__)
2193 * Mac OS Lion doesn't allow ipv6mr_interface = 0.
2195 * Tests on Windows and Linux show that the multicast group is only
2196 * joined on one NIC when interface = 0, so we simply use the interface
2197 * id from the first non-loopback interface (this is also what
2198 * Dns.GetHostName (string.Empty) would return).
2200 if (!mreq6.ipv6mr_interface)
2201 mreq6.ipv6mr_interface = get_local_interface_id (AF_INET6);
2204 ret = _wapi_setsockopt (sock, system_level,
2205 system_name, &mreq6,
2207 } else if(system_level == sol_ip)
2208 #endif /* AF_INET6 */
2210 #ifdef HAVE_STRUCT_IP_MREQN
2211 struct ip_mreqn mreq = {{0}};
2213 struct ip_mreq mreq = {{0}};
2214 #endif /* HAVE_STRUCT_IP_MREQN */
2216 /* pain! MulticastOption holds two IPAddress
2217 * members, so I have to dig the value out of
2220 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2221 address = *(gpointer *)(((char *)obj_val) + field->offset);
2223 /* address might not be defined and if so, set the address to ADDR_ANY.
2226 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2229 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2230 address = *(gpointer *)(((char *)obj_val) + field->offset);
2232 #ifdef HAVE_STRUCT_IP_MREQN
2234 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2238 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2240 #endif /* HAVE_STRUCT_IP_MREQN */
2242 ret = _wapi_setsockopt (sock, system_level,
2248 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2250 /* Cause an exception to be thrown */
2254 } else if (byte_val!=NULL) {
2255 int valsize = mono_array_length (byte_val);
2256 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2259 case SocketOptionName_DontLinger:
2261 linger.l_onoff = (*buf) ? 0 : 1;
2262 linger.l_linger = 0;
2263 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2269 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2273 /* ReceiveTimeout/SendTimeout get here */
2275 case SocketOptionName_DontLinger:
2276 linger.l_onoff = !int_val;
2277 linger.l_linger = 0;
2278 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2280 case SocketOptionName_DontFragment:
2281 #ifdef HAVE_IP_MTU_DISCOVER
2282 /* Fiddle with the value slightly if we're
2286 int_val = IP_PMTUDISC_DO;
2292 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2296 if(ret==SOCKET_ERROR) {
2297 *error = WSAGetLastError ();
2301 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2307 MONO_ARCH_SAVE_REGS;
2311 /* Currently, the values for how (recv=0, send=1, both=2) match
2314 ret = _wapi_shutdown (sock, how);
2315 if(ret==SOCKET_ERROR) {
2316 *error = WSAGetLastError ();
2321 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2323 MonoArray *output, gint32 *error)
2325 glong output_bytes = 0;
2326 gchar *i_buffer, *o_buffer;
2330 MONO_ARCH_SAVE_REGS;
2334 if (code == FIONBIO) {
2335 /* Invalid command. Must use Socket.Blocking */
2339 if (input == NULL) {
2343 i_buffer = mono_array_addr (input, gchar, 0);
2344 i_len = mono_array_length (input);
2347 if (output == NULL) {
2351 o_buffer = mono_array_addr (output, gchar, 0);
2352 o_len = mono_array_length (output);
2355 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2356 if (ret == SOCKET_ERROR) {
2357 *error = WSAGetLastError ();
2361 return (gint) output_bytes;
2364 #ifdef HAVE_SIOCGIFCONF
2366 is_loopback (int family, void *ad)
2368 char *ptr = (char *) ad;
2370 if (family == AF_INET) {
2371 return (ptr [0] == 127);
2375 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2382 get_local_ips (int family, int *nips)
2384 int addr_size, offset, fd, i, count;
2385 int max_ifaces = 50; /* 50 interfaces should be enough... */
2388 struct ifreq iflags;
2389 char *result, *tmp_ptr;
2390 gboolean ignore_loopback = FALSE;
2393 if (family == AF_INET) {
2394 addr_size = sizeof (struct in_addr);
2395 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2397 } else if (family == AF_INET6) {
2398 addr_size = sizeof (struct in6_addr);
2399 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2405 fd = socket (family, SOCK_STREAM, 0);
2407 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2408 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2409 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2411 g_free (ifc.ifc_buf);
2415 count = ifc.ifc_len / sizeof (struct ifreq);
2418 g_free (ifc.ifc_buf);
2423 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2424 strcpy (iflags.ifr_name, ifr->ifr_name);
2425 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2429 if ((iflags.ifr_flags & IFF_UP) == 0) {
2430 ifr->ifr_name [0] = '\0';
2434 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2435 ignore_loopback = TRUE;
2440 result = g_malloc (addr_size * count);
2442 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2443 if (ifr->ifr_name [0] == '\0') {
2448 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2453 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2454 tmp_ptr += addr_size;
2457 g_free (ifc.ifc_buf);
2460 #elif defined(HAVE_GETIFADDRS)
2462 is_loopback (int family, void *ad)
2464 char *ptr = (char *) ad;
2466 if (family == AF_INET) {
2467 return (ptr [0] == 127);
2471 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2478 get_local_ips (int family, int *nips)
2480 struct ifaddrs *ifap = NULL, *ptr;
2481 int addr_size, offset, count, i;
2482 char *result, *tmp_ptr;
2485 if (family == AF_INET) {
2486 addr_size = sizeof (struct in_addr);
2487 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2489 } else if (family == AF_INET6) {
2490 addr_size = sizeof (struct in6_addr);
2491 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2497 if (getifaddrs (&ifap)) {
2502 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2505 if (ptr->ifa_addr->sa_family != family)
2507 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2512 result = g_malloc (addr_size * count);
2514 for (i = 0, ptr = ifap; ptr; ptr = ptr->ifa_next) {
2517 if (ptr->ifa_addr->sa_family != family)
2519 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2522 memcpy (tmp_ptr, ((char *) ptr->ifa_addr) + offset, addr_size);
2523 tmp_ptr += addr_size;
2532 get_local_ips (int family, int *nips)
2538 #endif /* HAVE_SIOCGIFCONF */
2541 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2542 MonoArray **h_aliases,
2543 MonoArray **h_addr_list,
2544 gboolean add_local_ips)
2546 MonoDomain *domain = mono_domain_get ();
2548 struct in_addr *local_in = NULL;
2552 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2556 *h_name=mono_string_new(domain, he->h_name);
2558 while(he->h_aliases[i]!=NULL) {
2562 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2564 while(he->h_aliases[i]!=NULL) {
2567 alias=mono_string_new(domain, he->h_aliases[i]);
2568 mono_array_setref (*h_aliases, i, alias);
2571 } else if (!add_local_ips) {
2575 if (add_local_ips) {
2576 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2578 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2579 for (i = 0; i < nlocal_in; i++) {
2580 MonoString *addr_string;
2581 char addr [16], *ptr;
2583 ptr = (char *) &local_in [i];
2584 g_snprintf(addr, 16, "%u.%u.%u.%u",
2585 (unsigned char) ptr [0],
2586 (unsigned char) ptr [1],
2587 (unsigned char) ptr [2],
2588 (unsigned char) ptr [3]);
2590 addr_string = mono_string_new (domain, addr);
2591 mono_array_setref (*h_addr_list, i, addr_string);
2596 } else if (he == NULL) {
2597 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2598 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2599 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2604 if (nlocal_in == 0 && he != NULL) {
2606 while (he->h_addr_list[i]!=NULL) {
2610 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2612 while(he->h_addr_list[i]!=NULL) {
2613 MonoString *addr_string;
2616 g_snprintf(addr, 16, "%u.%u.%u.%u",
2617 (unsigned char)he->h_addr_list[i][0],
2618 (unsigned char)he->h_addr_list[i][1],
2619 (unsigned char)he->h_addr_list[i][2],
2620 (unsigned char)he->h_addr_list[i][3]);
2622 addr_string=mono_string_new(domain, addr);
2623 mono_array_setref (*h_addr_list, i, addr_string);
2631 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2632 MonoArray **h_aliases,
2633 MonoArray **h_addr_list)
2635 MonoDomain *domain = mono_domain_get ();
2637 *h_name=mono_string_new(domain, addr);
2638 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2639 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2640 mono_array_setref (*h_addr_list, 0, *h_name);
2646 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2647 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2648 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2650 MonoDomain *domain = mono_domain_get ();
2651 int i, host_count, host_index, family_hint;
2652 struct in_addr *local_in = NULL;
2654 struct in6_addr *local_in6 = NULL;
2656 gboolean from_local = FALSE;
2658 family_hint = get_family_hint ();
2661 * Check if address length and family are correct
2663 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2667 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2672 * Get the aliases and host name from he1 or he2 whichever is
2673 * not null, if he1 is not null then take aliases from he1
2675 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2676 family_hint == PF_INET)) {
2677 *h_name=mono_string_new (domain, he1->h_name);
2680 while(he1->h_aliases[i]!=NULL) {
2684 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2687 while(he1->h_aliases[i]!=NULL) {
2690 alias=mono_string_new (domain, he1->h_aliases[i]);
2691 mono_array_setref (*h_aliases, i, alias);
2694 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2695 family_hint == PF_INET6)) {
2696 *h_name=mono_string_new (domain, he2->h_name);
2699 while(he2->h_aliases [i] != NULL) {
2703 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2706 while(he2->h_aliases[i]!=NULL) {
2709 alias=mono_string_new (domain, he2->h_aliases[i]);
2710 mono_array_setref (*h_aliases, i, alias);
2713 } else if (!add_local_ips) {
2718 * Count the number of addresses in he1 + he2
2721 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2722 family_hint == PF_INET)) {
2724 while(he1->h_addr_list[i]!=NULL) {
2730 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2731 family_hint == PF_INET6)) {
2733 while(he2->h_addr_list[i]!=NULL) {
2743 if (add_local_ips) {
2744 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2745 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2747 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2748 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2750 if (nlocal_in || nlocal_in6) {
2752 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2753 nlocal_in + nlocal_in6);
2757 for (n = 0; n < nlocal_in6; n++) {
2758 MonoString *addr_string;
2760 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2762 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2765 addr_string = mono_string_new (domain, addr);
2766 mono_array_setref (*h_addr_list, host_index, addr_string);
2774 for (n = 0; n < nlocal_in; n++) {
2775 MonoString *addr_string;
2777 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2779 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2782 addr_string = mono_string_new (domain, addr);
2783 mono_array_setref (*h_addr_list, host_index, addr_string);
2791 } else if (he1 == NULL && he2 == NULL) {
2792 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2793 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2794 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2804 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2806 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2807 family_hint == PF_INET6)) {
2809 while(he2->h_addr_list[i] != NULL) {
2810 MonoString *addr_string;
2812 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2814 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2818 addr_string = mono_string_new (domain, addr);
2819 mono_array_setref (*h_addr_list, host_index, addr_string);
2826 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2827 family_hint == PF_INET)) {
2829 while(he1->h_addr_list[i] != NULL) {
2830 MonoString *addr_string;
2832 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2834 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2838 addr_string=mono_string_new (domain, addr);
2839 mono_array_setref (*h_addr_list, host_index, addr_string);
2850 #if defined(AF_INET6)
2852 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2853 MonoArray **h_aliases,
2854 MonoArray **h_addr_list,
2855 gboolean add_local_ips)
2858 struct addrinfo *ai = NULL;
2859 struct in_addr *local_in = NULL;
2861 struct in6_addr *local_in6 = NULL;
2865 MonoDomain *domain = mono_domain_get ();
2868 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2869 if (add_local_ips) {
2870 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2871 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2872 if (nlocal_in || nlocal_in6) {
2873 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2875 MonoString *addr_string;
2879 for (i = 0; i < nlocal_in; i++) {
2880 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2881 addr_string = mono_string_new (domain, addr);
2882 mono_array_setref (*h_addr_list, addr_index, addr_string);
2888 MonoString *addr_string;
2893 for (i = 0; i < nlocal_in6; i++) {
2894 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2896 addr_string = mono_string_new (domain, addr);
2897 mono_array_setref (*h_addr_list, addr_index, addr_string);
2906 freeaddrinfo (info);
2915 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2916 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2922 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2924 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2925 MonoString *addr_string;
2927 char buffer [48]; /* Max. size for IPv6 */
2929 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2933 if(ai->ai_family == PF_INET) {
2934 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2936 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2940 addr_string=mono_string_new(domain, buffer);
2942 addr_string=mono_string_new(domain, "");
2945 mono_array_setref (*h_addr_list, addr_index, addr_string);
2949 if (ai->ai_canonname != NULL) {
2950 *h_name=mono_string_new(domain, ai->ai_canonname);
2952 *h_name=mono_string_new(domain, buffer);
2968 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2970 gboolean add_local_ips = FALSE;
2971 #ifdef HAVE_SIOCGIFCONF
2972 gchar this_hostname [256];
2974 #if !defined(HAVE_GETHOSTBYNAME2_R)
2975 struct addrinfo *info = NULL, hints;
2978 MONO_ARCH_SAVE_REGS;
2980 hostname=mono_string_to_utf8 (host);
2981 if (*hostname == '\0') {
2982 add_local_ips = TRUE;
2985 #ifdef HAVE_SIOCGIFCONF
2986 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2987 if (!strcmp (hostname, this_hostname)) {
2988 add_local_ips = TRUE;
2994 memset(&hints, 0, sizeof(hints));
2995 hints.ai_family = get_family_hint ();
2996 hints.ai_socktype = SOCK_STREAM;
2997 hints.ai_flags = AI_CANONNAME;
2999 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
3005 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
3007 struct hostent he1,*hp1, he2, *hp2;
3008 int buffer_size1, buffer_size2;
3009 char *buffer1, *buffer2;
3011 gboolean return_value;
3014 MONO_ARCH_SAVE_REGS;
3016 hostname=mono_string_to_utf8 (host);
3017 if (*hostname == '\0')
3018 add_local_ips = TRUE;
3020 #ifdef HAVE_SIOCGIFCONF
3021 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3022 if (!strcmp (hostname, this_hostname))
3023 add_local_ips = TRUE;
3029 buffer1 = g_malloc0(buffer_size1);
3030 buffer2 = g_malloc0(buffer_size2);
3034 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
3035 &hp1, &herr) == ERANGE) {
3037 buffer1 = g_realloc(buffer1, buffer_size1);
3040 if (*hostname && hp1 == NULL)
3042 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
3043 buffer_size2, &hp2, &herr) == ERANGE) {
3045 buffer2 = g_realloc(buffer2, buffer_size2);
3049 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
3050 h_addr_list, add_local_ips);
3056 return(return_value);
3057 #endif /* HAVE_GETHOSTBYNAME2_R */
3059 #else /* AF_INET6 */
3060 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3064 gboolean add_local_ips = FALSE;
3065 #ifdef HAVE_SIOCGIFCONF
3066 gchar this_hostname [256];
3069 MONO_ARCH_SAVE_REGS;
3071 hostname=mono_string_to_utf8(host);
3072 if (*hostname == '\0')
3073 add_local_ips = TRUE;
3074 #ifdef HAVE_SIOCGIFCONF
3075 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3076 if (!strcmp (hostname, this_hostname))
3077 add_local_ips = TRUE;
3084 he = _wapi_gethostbyname (hostname);
3086 he = _wapi_gethostbyname (hostname);
3090 if (*hostname && he==NULL)
3093 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
3095 #endif /* AF_INET6 */
3097 #ifndef HAVE_INET_PTON
3099 inet_pton (int family, const char *address, void *inaddrp)
3101 if (family == AF_INET) {
3102 #ifdef HAVE_INET_ATON
3103 struct in_addr inaddr;
3105 if (!inet_aton (address, &inaddr))
3108 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
3111 /* assume the system has inet_addr(), if it doesn't
3112 have that we're pretty much screwed... */
3115 if (!strcmp (address, "255.255.255.255")) {
3116 /* special-case hack */
3117 inaddr = 0xffffffff;
3119 inaddr = inet_addr (address);
3121 #define INADDR_NONE ((in_addr_t) -1)
3123 if (inaddr == INADDR_NONE)
3127 memcpy (inaddrp, &inaddr, sizeof (guint32));
3129 #endif /* HAVE_INET_ATON */
3134 #endif /* !HAVE_INET_PTON */
3136 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3141 struct sockaddr_in saddr;
3142 struct sockaddr_in6 saddr6;
3143 struct addrinfo *info = NULL, hints;
3145 char hostname[1024] = {0};
3148 struct in_addr inaddr;
3153 address = mono_string_to_utf8 (addr);
3156 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
3157 /* Maybe an ipv6 address */
3158 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
3164 saddr6.sin6_family = AF_INET6;
3169 saddr.sin_family = AF_INET;
3173 if(family == AF_INET) {
3174 #if HAVE_SOCKADDR_IN_SIN_LEN
3175 saddr.sin_len = sizeof (saddr);
3177 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
3178 hostname, sizeof(hostname), NULL, 0,
3182 } else if(family == AF_INET6) {
3183 #if HAVE_SOCKADDR_IN6_SIN_LEN
3184 saddr6.sin6_len = sizeof (saddr6);
3186 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
3187 hostname, sizeof(hostname), NULL, 0,
3193 memset (&hints, 0, sizeof(hints));
3194 hints.ai_family = get_family_hint ();
3195 hints.ai_socktype = SOCK_STREAM;
3196 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
3198 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
3202 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
3204 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
3209 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
3210 ret = ipaddr_to_IPHostEntry (address, h_name, h_aliases, h_addr_list);
3212 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
3213 h_addr_list, FALSE);
3221 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
3223 gchar hostname[256];
3226 MONO_ARCH_SAVE_REGS;
3228 ret = gethostname (hostname, sizeof (hostname));
3233 *h_name=mono_string_new(mono_domain_get (), hostname);
3239 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
3243 TRANSMIT_FILE_BUFFERS buffers;
3245 MONO_ARCH_SAVE_REGS;
3247 if (filename == NULL)
3250 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
3251 if (file == INVALID_HANDLE_VALUE) {
3252 SetLastError (error);
3256 memset (&buffers, 0, sizeof (buffers));
3257 if (pre_buffer != NULL) {
3258 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
3259 buffers.HeadLength = mono_array_length (pre_buffer);
3261 if (post_buffer != NULL) {
3262 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
3263 buffers.TailLength = mono_array_length (post_buffer);
3266 if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
3275 void mono_network_init(void)
3280 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3282 g_error("%s: Couldn't initialise networking", __func__);
3286 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3287 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3290 void mono_network_cleanup(void)
3296 icall_cancel_blocking_socket_operation (MonoThread *thread)
3298 MonoInternalThread *internal = thread->internal_thread;
3300 if (mono_thread_info_new_interrupt_enabled ()) {
3301 mono_thread_info_abort_socket_syscall_for_close ((MonoNativeThreadId)(gsize)internal->tid);
3304 internal->ignore_next_signal = TRUE;
3305 mono_thread_kill (internal, mono_thread_get_abort_signal ());
3310 #endif /* #ifndef DISABLE_SOCKETS */