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 /* May be the +2 here is too conservative, as sa_len returns
934 * the length of the entire sockaddr_in/in6, including
935 * sizeof (unsigned short) of the family */
936 /* We can't really avoid the +2 as all code below depends on this size - INCLUDING unix domain sockets.*/
937 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
939 /* The data buffer is laid out as follows:
940 * bytes 0 and 1 are the address family
941 * bytes 2 and 3 are the port info
942 * the rest is the address info
945 family=convert_to_mono_family(saddr->sa_family);
946 if(family==AddressFamily_Unknown) {
947 *error = WSAEAFNOSUPPORT;
951 mono_array_set(data, guint8, 0, family & 0x0FF);
952 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
954 if(saddr->sa_family==AF_INET) {
955 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
956 guint16 port=ntohs(sa_in->sin_port);
957 guint32 address=ntohl(sa_in->sin_addr.s_addr);
960 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
963 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
964 mono_array_set(data, guint8, 3, (port) & 0xff);
965 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
966 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
967 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
968 mono_array_set(data, guint8, 7, (address) & 0xff);
970 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
972 return(sockaddr_obj);
974 } else if (saddr->sa_family == AF_INET6) {
975 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
978 guint16 port=ntohs(sa_in->sin6_port);
981 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
984 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
985 mono_array_set(data, guint8, 3, (port) & 0xff);
987 if (is_ipv4_mapped_any (&sa_in->sin6_addr)) {
988 // Map ::ffff:0:0 to :: (bug #5502)
989 for(i=0; i<16; i++) {
990 mono_array_set(data, guint8, 8+i, 0);
993 for(i=0; i<16; i++) {
994 mono_array_set(data, guint8, 8+i,
995 sa_in->sin6_addr.s6_addr[i]);
999 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
1000 mono_array_set(data, guint8, 25,
1001 (sa_in->sin6_scope_id >> 8) & 0xff);
1002 mono_array_set(data, guint8, 26,
1003 (sa_in->sin6_scope_id >> 16) & 0xff);
1004 mono_array_set(data, guint8, 27,
1005 (sa_in->sin6_scope_id >> 24) & 0xff);
1007 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1009 return(sockaddr_obj);
1011 #ifdef HAVE_SYS_UN_H
1012 } else if (saddr->sa_family == AF_UNIX) {
1015 for (i = 0; i < sa_size; i++) {
1016 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
1019 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1021 return sockaddr_obj;
1024 *error = WSAEAFNOSUPPORT;
1030 get_sockaddr_size (int family)
1035 if (family == AF_INET) {
1036 size = sizeof (struct sockaddr_in);
1038 } else if (family == AF_INET6) {
1039 size = sizeof (struct sockaddr_in6);
1041 #ifdef HAVE_SYS_UN_H
1042 } else if (family == AF_UNIX) {
1043 size = sizeof (struct sockaddr_un);
1049 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1056 MONO_ARCH_SAVE_REGS;
1060 salen = get_sockaddr_size (convert_family (af));
1062 *error = WSAEAFNOSUPPORT;
1065 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1066 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1068 if(ret==SOCKET_ERROR) {
1069 *error = WSAGetLastError ();
1075 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)));
1077 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1083 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1090 MONO_ARCH_SAVE_REGS;
1094 salen = get_sockaddr_size (convert_family (af));
1096 *error = WSAEAFNOSUPPORT;
1099 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1100 /* Note: linux returns just 2 for AF_UNIX. Always. */
1101 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1102 if(ret==SOCKET_ERROR) {
1103 *error = WSAGetLastError ();
1109 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)));
1111 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1117 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1121 MonoClassField *field;
1126 /* Dig the SocketAddress data buffer out of the object */
1127 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1128 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1130 /* The data buffer is laid out as follows:
1131 * byte 0 is the address family low byte
1132 * byte 1 is the address family high byte
1134 * bytes 2 and 3 are the port info
1135 * the rest is the address info
1137 * the rest is the file name
1139 len = mono_array_length (data);
1141 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1144 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1145 if (family == AF_INET) {
1146 struct sockaddr_in *sa;
1151 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1154 sa = g_new0 (struct sockaddr_in, 1);
1155 port = (mono_array_get (data, guint8, 2) << 8) +
1156 mono_array_get (data, guint8, 3);
1157 address = (mono_array_get (data, guint8, 4) << 24) +
1158 (mono_array_get (data, guint8, 5) << 16 ) +
1159 (mono_array_get (data, guint8, 6) << 8) +
1160 mono_array_get (data, guint8, 7);
1162 sa->sin_family = family;
1163 sa->sin_addr.s_addr = htonl (address);
1164 sa->sin_port = htons (port);
1166 *sa_size = sizeof(struct sockaddr_in);
1167 return((struct sockaddr *)sa);
1170 } else if (family == AF_INET6) {
1171 struct sockaddr_in6 *sa;
1177 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1180 sa = g_new0 (struct sockaddr_in6, 1);
1181 port = mono_array_get (data, guint8, 3) +
1182 (mono_array_get (data, guint8, 2) << 8);
1183 scopeid = mono_array_get (data, guint8, 24) +
1184 (mono_array_get (data, guint8, 25) << 8) +
1185 (mono_array_get (data, guint8, 26) << 16) +
1186 (mono_array_get (data, guint8, 27) << 24);
1188 sa->sin6_family = family;
1189 sa->sin6_port = htons (port);
1190 sa->sin6_scope_id = scopeid;
1192 for(i=0; i<16; i++) {
1193 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1196 *sa_size = sizeof(struct sockaddr_in6);
1197 return((struct sockaddr *)sa);
1199 #ifdef HAVE_SYS_UN_H
1200 } else if (family == AF_UNIX) {
1201 struct sockaddr_un *sock_un;
1204 /* Need a byte for the '\0' terminator/prefix, and the first
1205 * two bytes hold the SocketAddress family
1207 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1208 mono_raise_exception (mono_get_exception_index_out_of_range ());
1211 sock_un = g_new0 (struct sockaddr_un, 1);
1213 sock_un->sun_family = family;
1214 for (i = 0; i < len - 2; i++) {
1215 sock_un->sun_path [i] = mono_array_get (data, guint8,
1221 return (struct sockaddr *)sock_un;
1224 *error = WSAEAFNOSUPPORT;
1229 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1231 struct sockaddr *sa;
1235 MONO_ARCH_SAVE_REGS;
1239 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1244 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)));
1246 ret = _wapi_bind (sock, sa, sa_size);
1247 if(ret==SOCKET_ERROR) {
1248 *error = WSAGetLastError ();
1261 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1262 gint timeout, gint32 *error)
1264 MonoInternalThread *thread = NULL;
1270 MONO_ARCH_SAVE_REGS;
1272 pfds = g_new0 (mono_pollfd, 1);
1273 pfds[0].fd = GPOINTER_TO_INT (sock);
1274 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1275 (mode == SelectModeWrite) ? MONO_POLLOUT :
1276 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1278 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1279 start = time (NULL);
1283 ret = mono_poll (pfds, 1, timeout);
1284 if (timeout > 0 && ret < 0) {
1286 int sec = time (NULL) - start;
1288 timeout -= sec * 1000;
1296 if (ret == -1 && errno == EINTR) {
1299 if (thread == NULL) {
1300 thread = mono_thread_internal_current ();
1303 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1309 /* Suspend requested? */
1310 mono_thread_interruption_checkpoint ();
1314 } while (ret == -1 && errno == EINTR);
1318 *error = WSAGetLastError ();
1320 *error = errno_to_WSA (errno, __func__);
1335 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1337 struct sockaddr *sa;
1341 MONO_ARCH_SAVE_REGS;
1345 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1350 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)));
1352 ret = _wapi_connect (sock, sa, sa_size);
1353 if(ret==SOCKET_ERROR) {
1354 *error = WSAGetLastError ();
1360 /* These #defines from mswsock.h from wine. Defining them here allows
1361 * us to build this file on a mingw box that doesn't know the magic
1362 * numbers, but still run on a newer windows box that does.
1364 #ifndef WSAID_DISCONNECTEX
1365 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1366 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1369 #ifndef WSAID_TRANSMITFILE
1370 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1371 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1374 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1377 glong output_bytes = 0;
1378 GUID disco_guid = WSAID_DISCONNECTEX;
1379 GUID trans_guid = WSAID_TRANSMITFILE;
1380 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1381 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1384 MONO_ARCH_SAVE_REGS;
1388 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1390 /* I _think_ the extension function pointers need to be looked
1391 * up for each socket. FIXME: check the best way to store
1392 * pointers to functions in managed objects that still works
1393 * on 64bit platforms.
1395 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1396 (void *)&disco_guid, sizeof(GUID),
1397 (void *)&_wapi_disconnectex, sizeof(void *),
1398 &output_bytes, NULL, NULL);
1400 /* make sure that WSAIoctl didn't put crap in the
1403 _wapi_disconnectex = NULL;
1405 /* Look up the TransmitFile extension function pointer
1406 * instead of calling TransmitFile() directly, because
1407 * apparently "Several of the extension functions have
1408 * been available since WinSock 1.1 and are exported
1409 * from MSWsock.dll, however it's not advisable to
1410 * link directly to this dll as this ties you to the
1411 * Microsoft WinSock provider. A provider neutral way
1412 * of accessing these extension functions is to load
1413 * them dynamically via WSAIoctl using the
1414 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1415 * should, theoretically, allow you to access these
1416 * functions from any provider that supports them..."
1417 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1419 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1420 (void *)&trans_guid, sizeof(GUID),
1421 (void *)&_wapi_transmitfile, sizeof(void *),
1422 &output_bytes, NULL, NULL);
1424 _wapi_transmitfile = NULL;
1428 if (_wapi_disconnectex != NULL) {
1429 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1430 } else if (_wapi_transmitfile != NULL) {
1431 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1432 TF_DISCONNECT | TF_REUSE_SOCKET);
1434 *error = ERROR_NOT_SUPPORTED;
1438 if (bret == FALSE) {
1439 *error = WSAGetLastError ();
1443 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1450 MONO_ARCH_SAVE_REGS;
1454 alen = mono_array_length (buffer);
1455 if (offset > alen - count) {
1459 buf=mono_array_addr(buffer, guchar, offset);
1461 recvflags = convert_socketflags (flags);
1462 if (recvflags == -1) {
1463 *error = WSAEOPNOTSUPP;
1469 MonoInternalThread* curthread = mono_thread_internal_current ();
1470 curthread->interrupt_on_stop = (gpointer)TRUE;
1471 ret = _wapi_recv (sock, buf, count, recvflags);
1472 curthread->interrupt_on_stop = (gpointer)FALSE;
1475 ret = _wapi_recv (sock, buf, count, recvflags);
1478 if(ret==SOCKET_ERROR) {
1479 *error = WSAGetLastError ();
1486 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1491 DWORD recvflags = 0;
1493 MONO_ARCH_SAVE_REGS;
1497 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1498 count = mono_array_length (buffers);
1500 recvflags = convert_socketflags (flags);
1501 if (recvflags == -1) {
1502 *error = WSAEOPNOTSUPP;
1506 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1507 if (ret == SOCKET_ERROR) {
1508 *error = WSAGetLastError ();
1515 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1521 struct sockaddr *sa;
1524 MONO_ARCH_SAVE_REGS;
1528 alen = mono_array_length (buffer);
1529 if (offset > alen - count) {
1533 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1538 buf=mono_array_addr(buffer, guchar, offset);
1540 recvflags = convert_socketflags (flags);
1541 if (recvflags == -1) {
1542 *error = WSAEOPNOTSUPP;
1546 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1547 if(ret==SOCKET_ERROR) {
1549 *error = WSAGetLastError ();
1553 /* If we didn't get a socket size, then we're probably a
1554 * connected connection-oriented socket and the stack hasn't
1555 * returned the remote address. All we can do is return null.
1558 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1567 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1574 MONO_ARCH_SAVE_REGS;
1578 alen = mono_array_length (buffer);
1579 if (offset > alen - count) {
1583 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1585 buf=mono_array_addr(buffer, guchar, offset);
1587 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1589 sendflags = convert_socketflags (flags);
1590 if (sendflags == -1) {
1591 *error = WSAEOPNOTSUPP;
1595 ret = _wapi_send (sock, buf, count, sendflags);
1596 if(ret==SOCKET_ERROR) {
1597 *error = WSAGetLastError ();
1604 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1609 DWORD sendflags = 0;
1611 MONO_ARCH_SAVE_REGS;
1615 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1616 count = mono_array_length (buffers);
1618 sendflags = convert_socketflags (flags);
1619 if (sendflags == -1) {
1620 *error = WSAEOPNOTSUPP;
1624 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1625 if (ret == SOCKET_ERROR) {
1626 *error = WSAGetLastError ();
1633 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1639 struct sockaddr *sa;
1642 MONO_ARCH_SAVE_REGS;
1646 alen = mono_array_length (buffer);
1647 if (offset > alen - count) {
1651 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1656 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1658 buf=mono_array_addr(buffer, guchar, offset);
1660 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1662 sendflags = convert_socketflags (flags);
1663 if (sendflags == -1) {
1664 *error = WSAEOPNOTSUPP;
1668 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1669 if(ret==SOCKET_ERROR) {
1670 *error = WSAGetLastError ();
1678 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1681 MonoClassField *field;
1683 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1684 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1689 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1690 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1692 MonoInternalThread *thread = NULL;
1699 MonoClass *sock_arr_class;
1702 uintptr_t socks_size;
1704 MONO_ARCH_SAVE_REGS;
1706 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1707 count = mono_array_length (*sockets);
1708 nfds = count - 3; /* NULL separators */
1709 pfds = g_new0 (mono_pollfd, nfds);
1711 for (i = 0; i < count; i++) {
1712 obj = mono_array_get (*sockets, MonoObject *, i);
1719 /* The socket array was bogus */
1725 pfds [idx].fd = Socket_to_SOCKET (obj);
1726 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1730 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1731 start = time (NULL);
1734 ret = mono_poll (pfds, nfds, timeout);
1735 if (timeout > 0 && ret < 0) {
1737 int sec = time (NULL) - start;
1739 timeout -= sec * 1000;
1745 if (ret == -1 && errno == EINTR) {
1748 thread = mono_thread_internal_current ();
1750 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1757 /* Suspend requested? */
1758 mono_thread_interruption_checkpoint ();
1762 } while (ret == -1 && errno == EINTR);
1766 *error = WSAGetLastError ();
1768 *error = errno_to_WSA (errno, __func__);
1780 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1781 socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
1782 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1785 for (i = 0; i < count && ret > 0; i++) {
1788 obj = mono_array_get (*sockets, MonoObject *, i);
1795 pfd = &pfds [i - mode];
1796 if (pfd->revents == 0)
1800 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1801 mono_array_setref (socks, idx++, obj);
1802 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1803 mono_array_setref (socks, idx++, obj);
1804 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1805 mono_array_setref (socks, idx++, obj);
1813 static MonoObject* int_to_object (MonoDomain *domain, int val)
1815 return mono_value_box (domain, mono_get_int32_class (), &val);
1819 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1825 socklen_t valsize=sizeof(val);
1826 struct linger linger;
1827 socklen_t lingersize=sizeof(linger);
1829 socklen_t time_ms_size = sizeof (time_ms);
1831 # if defined(__OpenBSD__)
1832 struct sockpeercred cred;
1836 socklen_t credsize = sizeof(cred);
1838 MonoDomain *domain=mono_domain_get();
1840 MonoClass *obj_class;
1841 MonoClassField *field;
1843 MONO_ARCH_SAVE_REGS;
1847 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1848 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
1849 system_level = SOL_SOCKET;
1850 system_name = SO_REUSEADDR;
1856 ret = convert_sockopt_level_and_name (level, name, &system_level, &system_name);
1860 *error = WSAENOPROTOOPT;
1864 *obj_val = int_to_object (domain, 0);
1868 /* No need to deal with MulticastOption names here, because
1869 * you cant getsockopt AddMembership or DropMembership (the
1870 * int getsockopt will error, causing an exception)
1873 case SocketOptionName_Linger:
1874 case SocketOptionName_DontLinger:
1875 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1879 case SocketOptionName_SendTimeout:
1880 case SocketOptionName_ReceiveTimeout:
1881 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1885 case SocketOptionName_PeerCred:
1886 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1892 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1896 if(ret==SOCKET_ERROR) {
1897 *error = WSAGetLastError ();
1902 case SocketOptionName_Linger:
1903 /* build a System.Net.Sockets.LingerOption */
1904 obj_class=mono_class_from_name(get_socket_assembly (),
1905 "System.Net.Sockets",
1907 obj=mono_object_new(domain, obj_class);
1909 /* Locate and set the fields "bool enabled" and "int
1912 field=mono_class_get_field_from_name(obj_class, "enabled");
1913 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1915 field=mono_class_get_field_from_name(obj_class, "seconds");
1916 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1920 case SocketOptionName_DontLinger:
1921 /* construct a bool int in val - true if linger is off */
1922 obj = int_to_object (domain, !linger.l_onoff);
1925 case SocketOptionName_SendTimeout:
1926 case SocketOptionName_ReceiveTimeout:
1927 obj = int_to_object (domain, time_ms);
1931 case SocketOptionName_PeerCred:
1933 /* build a Mono.Posix.PeerCred+PeerCredData if
1936 static MonoImage *mono_posix_image = NULL;
1937 MonoPeerCredData *cred_data;
1939 if (mono_posix_image == NULL) {
1940 mono_posix_image=mono_image_loaded ("Mono.Posix");
1941 if (!mono_posix_image) {
1942 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1944 *error = WSAENOPROTOOPT;
1947 mono_posix_image = mono_assembly_get_image (sa);
1952 obj_class = mono_class_from_name(mono_posix_image,
1955 obj = mono_object_new(domain, obj_class);
1956 cred_data = (MonoPeerCredData *)obj;
1957 cred_data->pid = cred.pid;
1958 cred_data->uid = cred.uid;
1959 cred_data->gid = cred.gid;
1965 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1966 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse)
1969 obj = int_to_object (domain, val);
1974 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1982 MONO_ARCH_SAVE_REGS;
1986 ret=convert_sockopt_level_and_name(level, name, &system_level,
1989 *error = WSAENOPROTOOPT;
1995 valsize=mono_array_length(*byte_val);
1996 buf=mono_array_addr(*byte_val, guchar, 0);
1998 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1999 if(ret==SOCKET_ERROR) {
2000 *error = WSAGetLastError ();
2004 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2005 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
2007 struct in_addr inaddr;
2008 MonoClassField *field;
2010 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
2012 /* No idea why .net uses a 64bit type to hold a 32bit value...
2014 * Internal value of IPAddess is in little-endian order
2016 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
2022 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
2024 struct in6_addr in6addr;
2025 MonoClassField *field;
2029 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
2030 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
2032 /* Solaris has only the 8 bit version. */
2034 for(i=0; i<8; i++) {
2035 guint16 s = mono_array_get (data, guint16, i);
2036 in6addr.s6_addr[2 * i + 1] = (s >> 8) & 0xff;
2037 in6addr.s6_addr[2 * i] = s & 0xff;
2041 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
2045 #endif /* AF_INET6 */
2048 #if defined(__APPLE__)
2050 #if defined(HAVE_GETIFADDRS) && defined(HAVE_IF_NAMETOINDEX)
2052 get_local_interface_id (int family)
2054 struct ifaddrs *ifap = NULL, *ptr;
2057 if (getifaddrs (&ifap)) {
2061 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2062 if (!ptr->ifa_addr || !ptr->ifa_name)
2064 if (ptr->ifa_addr->sa_family != family)
2066 if ((ptr->ifa_flags & IFF_LOOPBACK) != 0)
2068 if ((ptr->ifa_flags & IFF_MULTICAST) == 0)
2071 idx = if_nametoindex (ptr->ifa_name);
2080 get_local_interface_id (int family)
2086 #endif /* __APPLE__ */
2088 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)
2090 struct linger linger;
2100 #ifdef HAVE_SOL_IPV6
2101 sol_ipv6 = SOL_IPV6;
2104 struct protoent *pent;
2105 pent = getprotobyname ("ipv6");
2106 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2114 struct protoent *pent;
2115 pent = getprotobyname ("ip");
2116 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2119 #endif /* AF_INET6 */
2121 MONO_ARCH_SAVE_REGS;
2123 ret=convert_sockopt_level_and_name(level, name, &system_level,
2126 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2127 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
2128 system_name = SO_REUSEADDR;
2129 int_val = int_val ? 0 : 1;
2135 *error = WSAENOPROTOOPT;
2142 /* Only one of obj_val, byte_val or int_val has data */
2144 MonoClassField *field;
2148 case SocketOptionName_Linger:
2149 /* Dig out "bool enabled" and "int seconds"
2152 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2153 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2154 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2155 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2157 valsize=sizeof(linger);
2158 ret = _wapi_setsockopt (sock, system_level,
2159 system_name, &linger, valsize);
2161 case SocketOptionName_AddMembership:
2162 case SocketOptionName_DropMembership:
2163 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2165 MonoObject *address = NULL;
2168 if(system_level == sol_ipv6) {
2169 struct ipv6_mreq mreq6;
2174 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2175 address = *(gpointer *)(((char *)obj_val) + field->offset);
2178 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2181 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2182 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2184 #if defined(__APPLE__)
2188 * Mac OS Lion doesn't allow ipv6mr_interface = 0.
2190 * Tests on Windows and Linux show that the multicast group is only
2191 * joined on one NIC when interface = 0, so we simply use the interface
2192 * id from the first non-loopback interface (this is also what
2193 * Dns.GetHostName (string.Empty) would return).
2195 if (!mreq6.ipv6mr_interface)
2196 mreq6.ipv6mr_interface = get_local_interface_id (AF_INET6);
2199 ret = _wapi_setsockopt (sock, system_level,
2200 system_name, &mreq6,
2202 } else if(system_level == sol_ip)
2203 #endif /* AF_INET6 */
2205 #ifdef HAVE_STRUCT_IP_MREQN
2206 struct ip_mreqn mreq = {{0}};
2208 struct ip_mreq mreq = {{0}};
2209 #endif /* HAVE_STRUCT_IP_MREQN */
2211 /* pain! MulticastOption holds two IPAddress
2212 * members, so I have to dig the value out of
2215 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2216 address = *(gpointer *)(((char *)obj_val) + field->offset);
2218 /* address might not be defined and if so, set the address to ADDR_ANY.
2221 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2224 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2225 address = *(gpointer *)(((char *)obj_val) + field->offset);
2227 #ifdef HAVE_STRUCT_IP_MREQN
2229 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2233 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2235 #endif /* HAVE_STRUCT_IP_MREQN */
2237 ret = _wapi_setsockopt (sock, system_level,
2243 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2245 /* Cause an exception to be thrown */
2249 } else if (byte_val!=NULL) {
2250 int valsize = mono_array_length (byte_val);
2251 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2254 case SocketOptionName_DontLinger:
2256 linger.l_onoff = (*buf) ? 0 : 1;
2257 linger.l_linger = 0;
2258 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2264 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2268 /* ReceiveTimeout/SendTimeout get here */
2270 case SocketOptionName_DontLinger:
2271 linger.l_onoff = !int_val;
2272 linger.l_linger = 0;
2273 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2275 case SocketOptionName_DontFragment:
2276 #ifdef HAVE_IP_MTU_DISCOVER
2277 /* Fiddle with the value slightly if we're
2281 int_val = IP_PMTUDISC_DO;
2287 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2291 if(ret==SOCKET_ERROR) {
2292 *error = WSAGetLastError ();
2296 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2302 MONO_ARCH_SAVE_REGS;
2306 /* Currently, the values for how (recv=0, send=1, both=2) match
2309 ret = _wapi_shutdown (sock, how);
2310 if(ret==SOCKET_ERROR) {
2311 *error = WSAGetLastError ();
2316 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2318 MonoArray *output, gint32 *error)
2320 glong output_bytes = 0;
2321 gchar *i_buffer, *o_buffer;
2325 MONO_ARCH_SAVE_REGS;
2329 if (code == FIONBIO) {
2330 /* Invalid command. Must use Socket.Blocking */
2334 if (input == NULL) {
2338 i_buffer = mono_array_addr (input, gchar, 0);
2339 i_len = mono_array_length (input);
2342 if (output == NULL) {
2346 o_buffer = mono_array_addr (output, gchar, 0);
2347 o_len = mono_array_length (output);
2350 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2351 if (ret == SOCKET_ERROR) {
2352 *error = WSAGetLastError ();
2356 return (gint) output_bytes;
2359 #ifdef HAVE_SIOCGIFCONF
2361 is_loopback (int family, void *ad)
2363 char *ptr = (char *) ad;
2365 if (family == AF_INET) {
2366 return (ptr [0] == 127);
2370 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2377 get_local_ips (int family, int *nips)
2379 int addr_size, offset, fd, i, count;
2380 int max_ifaces = 50; /* 50 interfaces should be enough... */
2383 struct ifreq iflags;
2384 char *result, *tmp_ptr;
2385 gboolean ignore_loopback = FALSE;
2388 if (family == AF_INET) {
2389 addr_size = sizeof (struct in_addr);
2390 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2392 } else if (family == AF_INET6) {
2393 addr_size = sizeof (struct in6_addr);
2394 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2400 fd = socket (family, SOCK_STREAM, 0);
2402 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2403 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2404 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2406 g_free (ifc.ifc_buf);
2410 count = ifc.ifc_len / sizeof (struct ifreq);
2413 g_free (ifc.ifc_buf);
2418 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2419 strcpy (iflags.ifr_name, ifr->ifr_name);
2420 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2424 if ((iflags.ifr_flags & IFF_UP) == 0) {
2425 ifr->ifr_name [0] = '\0';
2429 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2430 ignore_loopback = TRUE;
2435 result = g_malloc (addr_size * count);
2437 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2438 if (ifr->ifr_name [0] == '\0') {
2443 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2448 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2449 tmp_ptr += addr_size;
2452 g_free (ifc.ifc_buf);
2455 #elif defined(HAVE_GETIFADDRS)
2457 is_loopback (int family, void *ad)
2459 char *ptr = (char *) ad;
2461 if (family == AF_INET) {
2462 return (ptr [0] == 127);
2466 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2473 get_local_ips (int family, int *nips)
2475 struct ifaddrs *ifap = NULL, *ptr;
2476 int addr_size, offset, count, i;
2477 char *result, *tmp_ptr;
2480 if (family == AF_INET) {
2481 addr_size = sizeof (struct in_addr);
2482 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2484 } else if (family == AF_INET6) {
2485 addr_size = sizeof (struct in6_addr);
2486 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2492 if (getifaddrs (&ifap)) {
2497 for (ptr = ifap; ptr; ptr = ptr->ifa_next) {
2500 if (ptr->ifa_addr->sa_family != family)
2502 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2507 result = g_malloc (addr_size * count);
2509 for (i = 0, ptr = ifap; ptr; ptr = ptr->ifa_next) {
2512 if (ptr->ifa_addr->sa_family != family)
2514 if (is_loopback (family, ((char *) ptr->ifa_addr) + offset))
2517 memcpy (tmp_ptr, ((char *) ptr->ifa_addr) + offset, addr_size);
2518 tmp_ptr += addr_size;
2527 get_local_ips (int family, int *nips)
2533 #endif /* HAVE_SIOCGIFCONF */
2536 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2537 MonoArray **h_aliases,
2538 MonoArray **h_addr_list,
2539 gboolean add_local_ips)
2541 MonoDomain *domain = mono_domain_get ();
2543 struct in_addr *local_in = NULL;
2547 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2551 *h_name=mono_string_new(domain, he->h_name);
2553 while(he->h_aliases[i]!=NULL) {
2557 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2559 while(he->h_aliases[i]!=NULL) {
2562 alias=mono_string_new(domain, he->h_aliases[i]);
2563 mono_array_setref (*h_aliases, i, alias);
2566 } else if (!add_local_ips) {
2570 if (add_local_ips) {
2571 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2573 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2574 for (i = 0; i < nlocal_in; i++) {
2575 MonoString *addr_string;
2576 char addr [16], *ptr;
2578 ptr = (char *) &local_in [i];
2579 g_snprintf(addr, 16, "%u.%u.%u.%u",
2580 (unsigned char) ptr [0],
2581 (unsigned char) ptr [1],
2582 (unsigned char) ptr [2],
2583 (unsigned char) ptr [3]);
2585 addr_string = mono_string_new (domain, addr);
2586 mono_array_setref (*h_addr_list, i, addr_string);
2591 } else if (he == NULL) {
2592 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2593 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2594 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2599 if (nlocal_in == 0 && he != NULL) {
2601 while (he->h_addr_list[i]!=NULL) {
2605 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2607 while(he->h_addr_list[i]!=NULL) {
2608 MonoString *addr_string;
2611 g_snprintf(addr, 16, "%u.%u.%u.%u",
2612 (unsigned char)he->h_addr_list[i][0],
2613 (unsigned char)he->h_addr_list[i][1],
2614 (unsigned char)he->h_addr_list[i][2],
2615 (unsigned char)he->h_addr_list[i][3]);
2617 addr_string=mono_string_new(domain, addr);
2618 mono_array_setref (*h_addr_list, i, addr_string);
2626 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2627 MonoArray **h_aliases,
2628 MonoArray **h_addr_list)
2630 MonoDomain *domain = mono_domain_get ();
2632 *h_name=mono_string_new(domain, addr);
2633 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2634 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2635 mono_array_setref (*h_addr_list, 0, *h_name);
2641 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2642 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2643 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2645 MonoDomain *domain = mono_domain_get ();
2646 int i, host_count, host_index, family_hint;
2647 struct in_addr *local_in = NULL;
2649 struct in6_addr *local_in6 = NULL;
2651 gboolean from_local = FALSE;
2653 family_hint = get_family_hint ();
2656 * Check if address length and family are correct
2658 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2662 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2667 * Get the aliases and host name from he1 or he2 whichever is
2668 * not null, if he1 is not null then take aliases from he1
2670 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2671 family_hint == PF_INET)) {
2672 *h_name=mono_string_new (domain, he1->h_name);
2675 while(he1->h_aliases[i]!=NULL) {
2679 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2682 while(he1->h_aliases[i]!=NULL) {
2685 alias=mono_string_new (domain, he1->h_aliases[i]);
2686 mono_array_setref (*h_aliases, i, alias);
2689 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2690 family_hint == PF_INET6)) {
2691 *h_name=mono_string_new (domain, he2->h_name);
2694 while(he2->h_aliases [i] != NULL) {
2698 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2701 while(he2->h_aliases[i]!=NULL) {
2704 alias=mono_string_new (domain, he2->h_aliases[i]);
2705 mono_array_setref (*h_aliases, i, alias);
2708 } else if (!add_local_ips) {
2713 * Count the number of addresses in he1 + he2
2716 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2717 family_hint == PF_INET)) {
2719 while(he1->h_addr_list[i]!=NULL) {
2725 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2726 family_hint == PF_INET6)) {
2728 while(he2->h_addr_list[i]!=NULL) {
2738 if (add_local_ips) {
2739 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2740 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2742 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2743 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2745 if (nlocal_in || nlocal_in6) {
2747 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2748 nlocal_in + nlocal_in6);
2752 for (n = 0; n < nlocal_in6; n++) {
2753 MonoString *addr_string;
2755 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2757 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2760 addr_string = mono_string_new (domain, addr);
2761 mono_array_setref (*h_addr_list, host_index, addr_string);
2769 for (n = 0; n < nlocal_in; n++) {
2770 MonoString *addr_string;
2772 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2774 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2777 addr_string = mono_string_new (domain, addr);
2778 mono_array_setref (*h_addr_list, host_index, addr_string);
2786 } else if (he1 == NULL && he2 == NULL) {
2787 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2788 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2789 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2799 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2801 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2802 family_hint == PF_INET6)) {
2804 while(he2->h_addr_list[i] != NULL) {
2805 MonoString *addr_string;
2807 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2809 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2813 addr_string = mono_string_new (domain, addr);
2814 mono_array_setref (*h_addr_list, host_index, addr_string);
2821 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2822 family_hint == PF_INET)) {
2824 while(he1->h_addr_list[i] != NULL) {
2825 MonoString *addr_string;
2827 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2829 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2833 addr_string=mono_string_new (domain, addr);
2834 mono_array_setref (*h_addr_list, host_index, addr_string);
2845 #if defined(AF_INET6)
2847 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2848 MonoArray **h_aliases,
2849 MonoArray **h_addr_list,
2850 gboolean add_local_ips)
2853 struct addrinfo *ai = NULL;
2854 struct in_addr *local_in = NULL;
2856 struct in6_addr *local_in6 = NULL;
2860 MonoDomain *domain = mono_domain_get ();
2863 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2864 if (add_local_ips) {
2865 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2866 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2867 if (nlocal_in || nlocal_in6) {
2868 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2870 MonoString *addr_string;
2874 for (i = 0; i < nlocal_in; i++) {
2875 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2876 addr_string = mono_string_new (domain, addr);
2877 mono_array_setref (*h_addr_list, addr_index, addr_string);
2883 MonoString *addr_string;
2888 for (i = 0; i < nlocal_in6; i++) {
2889 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2891 addr_string = mono_string_new (domain, addr);
2892 mono_array_setref (*h_addr_list, addr_index, addr_string);
2901 freeaddrinfo (info);
2910 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2911 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2917 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2919 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2920 MonoString *addr_string;
2922 char buffer [48]; /* Max. size for IPv6 */
2924 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2928 if(ai->ai_family == PF_INET) {
2929 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2931 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2935 addr_string=mono_string_new(domain, buffer);
2937 addr_string=mono_string_new(domain, "");
2940 mono_array_setref (*h_addr_list, addr_index, addr_string);
2944 if (ai->ai_canonname != NULL) {
2945 *h_name=mono_string_new(domain, ai->ai_canonname);
2947 *h_name=mono_string_new(domain, buffer);
2963 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2965 gboolean add_local_ips = FALSE;
2966 #ifdef HAVE_SIOCGIFCONF
2967 gchar this_hostname [256];
2969 #if !defined(HAVE_GETHOSTBYNAME2_R)
2970 struct addrinfo *info = NULL, hints;
2973 MONO_ARCH_SAVE_REGS;
2975 hostname=mono_string_to_utf8 (host);
2976 if (*hostname == '\0') {
2977 add_local_ips = TRUE;
2980 #ifdef HAVE_SIOCGIFCONF
2981 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2982 if (!strcmp (hostname, this_hostname)) {
2983 add_local_ips = TRUE;
2989 memset(&hints, 0, sizeof(hints));
2990 hints.ai_family = get_family_hint ();
2991 hints.ai_socktype = SOCK_STREAM;
2992 hints.ai_flags = AI_CANONNAME;
2994 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
3000 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
3002 struct hostent he1,*hp1, he2, *hp2;
3003 int buffer_size1, buffer_size2;
3004 char *buffer1, *buffer2;
3006 gboolean return_value;
3009 MONO_ARCH_SAVE_REGS;
3011 hostname=mono_string_to_utf8 (host);
3012 if (*hostname == '\0')
3013 add_local_ips = TRUE;
3015 #ifdef HAVE_SIOCGIFCONF
3016 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3017 if (!strcmp (hostname, this_hostname))
3018 add_local_ips = TRUE;
3024 buffer1 = g_malloc0(buffer_size1);
3025 buffer2 = g_malloc0(buffer_size2);
3029 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
3030 &hp1, &herr) == ERANGE) {
3032 buffer1 = g_realloc(buffer1, buffer_size1);
3035 if (*hostname && hp1 == NULL)
3037 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
3038 buffer_size2, &hp2, &herr) == ERANGE) {
3040 buffer2 = g_realloc(buffer2, buffer_size2);
3044 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
3045 h_addr_list, add_local_ips);
3051 return(return_value);
3052 #endif /* HAVE_GETHOSTBYNAME2_R */
3054 #else /* AF_INET6 */
3055 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3059 gboolean add_local_ips = FALSE;
3060 #ifdef HAVE_SIOCGIFCONF
3061 gchar this_hostname [256];
3064 MONO_ARCH_SAVE_REGS;
3066 hostname=mono_string_to_utf8(host);
3067 if (*hostname == '\0')
3068 add_local_ips = TRUE;
3069 #ifdef HAVE_SIOCGIFCONF
3070 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
3071 if (!strcmp (hostname, this_hostname))
3072 add_local_ips = TRUE;
3079 he = _wapi_gethostbyname (hostname);
3081 he = _wapi_gethostbyname (hostname);
3085 if (*hostname && he==NULL)
3088 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
3090 #endif /* AF_INET6 */
3092 #ifndef HAVE_INET_PTON
3094 inet_pton (int family, const char *address, void *inaddrp)
3096 if (family == AF_INET) {
3097 #ifdef HAVE_INET_ATON
3098 struct in_addr inaddr;
3100 if (!inet_aton (address, &inaddr))
3103 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
3106 /* assume the system has inet_addr(), if it doesn't
3107 have that we're pretty much screwed... */
3110 if (!strcmp (address, "255.255.255.255")) {
3111 /* special-case hack */
3112 inaddr = 0xffffffff;
3114 inaddr = inet_addr (address);
3116 #define INADDR_NONE ((in_addr_t) -1)
3118 if (inaddr == INADDR_NONE)
3122 memcpy (inaddrp, &inaddr, sizeof (guint32));
3124 #endif /* HAVE_INET_ATON */
3129 #endif /* !HAVE_INET_PTON */
3131 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3136 struct sockaddr_in saddr;
3137 struct sockaddr_in6 saddr6;
3138 struct addrinfo *info = NULL, hints;
3140 char hostname[1024] = {0};
3143 struct in_addr inaddr;
3148 address = mono_string_to_utf8 (addr);
3151 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
3152 /* Maybe an ipv6 address */
3153 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
3159 saddr6.sin6_family = AF_INET6;
3164 saddr.sin_family = AF_INET;
3168 if(family == AF_INET) {
3169 #if HAVE_SOCKADDR_IN_SIN_LEN
3170 saddr.sin_len = sizeof (saddr);
3172 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
3173 hostname, sizeof(hostname), NULL, 0,
3177 } else if(family == AF_INET6) {
3178 #if HAVE_SOCKADDR_IN6_SIN_LEN
3179 saddr6.sin6_len = sizeof (saddr6);
3181 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
3182 hostname, sizeof(hostname), NULL, 0,
3188 memset (&hints, 0, sizeof(hints));
3189 hints.ai_family = get_family_hint ();
3190 hints.ai_socktype = SOCK_STREAM;
3191 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
3193 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
3197 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
3199 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
3204 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
3205 ret = ipaddr_to_IPHostEntry (address, h_name, h_aliases, h_addr_list);
3207 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
3208 h_addr_list, FALSE);
3216 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
3218 gchar hostname[256];
3221 MONO_ARCH_SAVE_REGS;
3223 ret = gethostname (hostname, sizeof (hostname));
3228 *h_name=mono_string_new(mono_domain_get (), hostname);
3234 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
3238 TRANSMIT_FILE_BUFFERS buffers;
3240 MONO_ARCH_SAVE_REGS;
3242 if (filename == NULL)
3245 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
3246 if (file == INVALID_HANDLE_VALUE) {
3247 SetLastError (error);
3251 memset (&buffers, 0, sizeof (buffers));
3252 if (pre_buffer != NULL) {
3253 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
3254 buffers.HeadLength = mono_array_length (pre_buffer);
3256 if (post_buffer != NULL) {
3257 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
3258 buffers.TailLength = mono_array_length (post_buffer);
3261 if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
3270 void mono_network_init(void)
3275 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3277 g_error("%s: Couldn't initialise networking", __func__);
3281 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3282 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3285 void mono_network_cleanup(void)
3291 icall_cancel_blocking_socket_operation (MonoThread *thread)
3293 MonoInternalThread *internal = thread->internal_thread;
3295 if (mono_thread_info_new_interrupt_enabled ()) {
3296 mono_thread_info_abort_socket_syscall_for_close ((MonoNativeThreadId)(gsize)internal->tid);
3299 internal->ignore_next_signal = TRUE;
3300 mono_thread_kill (internal, mono_thread_get_abort_signal ());
3305 #endif /* #ifndef DISABLE_SOCKETS */