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)
22 #include <sys/types.h>
23 #ifndef PLATFORM_WIN32
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
29 #include <arpa/inet.h>
32 #include <mono/metadata/object.h>
33 #include <mono/io-layer/io-layer.h>
34 #include <mono/metadata/socket-io.h>
35 #include <mono/metadata/exception.h>
36 #include <mono/metadata/assembly.h>
37 #include <mono/metadata/appdomain.h>
38 #include <mono/metadata/threads.h>
39 #include <mono/metadata/threads-types.h>
40 #include <mono/utils/mono-poll.h>
41 /* FIXME change this code to not mess so much with the internals */
42 #include <mono/metadata/class-internals.h>
43 #include <mono/metadata/threadpool-internals.h>
44 #include <mono/metadata/domain-internals.h>
46 #ifdef HAVE_SYS_TIME_H
49 #ifdef HAVE_SYS_IOCTL_H
50 #include <sys/ioctl.h>
59 #ifdef HAVE_SYS_FILIO_H
60 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
62 #ifdef HAVE_SYS_SOCKIO_H
63 #include <sys/sockio.h> /* defines SIOCATMARK */
69 #include "mono/io-layer/socket-wrappers.h"
72 /* This is a kludge to make this file build under cygwin:
73 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
74 * prototypes for some but not all required functions (notably
75 * inet_ntop() is missing), but the libws2_32 library is missing the
76 * actual implementations of these functions.
81 #ifdef PLATFORM_ANDROID
82 // not yet actually implemented...
87 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
89 static gint32 convert_family(MonoAddressFamily mono_family)
94 case AddressFamily_Unknown:
95 case AddressFamily_ImpLink:
96 case AddressFamily_Pup:
97 case AddressFamily_Chaos:
98 case AddressFamily_Iso:
99 case AddressFamily_Ecma:
100 case AddressFamily_DataKit:
101 case AddressFamily_Ccitt:
102 case AddressFamily_DataLink:
103 case AddressFamily_Lat:
104 case AddressFamily_HyperChannel:
105 case AddressFamily_NetBios:
106 case AddressFamily_VoiceView:
107 case AddressFamily_FireFox:
108 case AddressFamily_Banyan:
109 case AddressFamily_Atm:
110 case AddressFamily_Cluster:
111 case AddressFamily_Ieee12844:
112 case AddressFamily_NetworkDesigners:
113 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
116 case AddressFamily_Unspecified:
120 case AddressFamily_Unix:
124 case AddressFamily_InterNetwork:
128 case AddressFamily_Ipx:
134 case AddressFamily_Sna:
138 case AddressFamily_DecNet:
142 case AddressFamily_AppleTalk:
146 case AddressFamily_InterNetworkV6:
151 case AddressFamily_Irda:
157 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
163 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
165 MonoAddressFamily family=AddressFamily_Unknown;
169 family=AddressFamily_Unspecified;
173 family=AddressFamily_Unix;
177 family=AddressFamily_InterNetwork;
182 family=AddressFamily_Ipx;
187 family=AddressFamily_Sna;
191 family=AddressFamily_DecNet;
195 family=AddressFamily_AppleTalk;
200 family=AddressFamily_InterNetworkV6;
206 family=AddressFamily_Irda;
210 g_warning("unknown address family 0x%x", af_family);
216 static gint32 convert_type(MonoSocketType mono_type)
221 case SocketType_Stream:
225 case SocketType_Dgram:
237 case SocketType_Seqpacket:
241 case SocketType_Unknown:
242 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
246 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
252 static gint32 convert_proto(MonoProtocolType mono_proto)
257 case ProtocolType_IP:
258 case ProtocolType_IPv6:
259 case ProtocolType_Icmp:
260 case ProtocolType_Igmp:
261 case ProtocolType_Ggp:
262 case ProtocolType_Tcp:
263 case ProtocolType_Pup:
264 case ProtocolType_Udp:
265 case ProtocolType_Idp:
266 /* These protocols are known (on my system at least) */
270 case ProtocolType_ND:
271 case ProtocolType_Raw:
272 case ProtocolType_Ipx:
273 case ProtocolType_Spx:
274 case ProtocolType_SpxII:
275 case ProtocolType_Unknown:
276 /* These protocols arent */
277 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
287 /* Convert MonoSocketFlags */
288 static gint32 convert_socketflags (gint32 sflags)
293 /* SocketFlags.None */
296 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
297 SocketFlags_DontRoute | SocketFlags_Partial))
298 /* Contains invalid flag values */
301 if (sflags & SocketFlags_OutOfBand)
303 if (sflags & SocketFlags_Peek)
305 if (sflags & SocketFlags_DontRoute)
306 flags |= MSG_DONTROUTE;
308 /* Ignore Partial - see bug 349688. Don't return -1, because
309 * according to the comment in that bug ms runtime doesn't for
310 * UDP sockets (this means we will silently ignore it for TCP
314 if (sflags & SocketFlags_Partial)
318 /* Don't do anything for MaxIOVectorLength */
319 if (sflags & SocketFlags_MaxIOVectorLength)
327 * 0 on success (mapped mono_level and mono_name to system_level and system_name
329 * -2 on non-fatal error (ie, must ignore)
331 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
332 MonoSocketOptionName mono_name,
336 switch (mono_level) {
337 case SocketOptionLevel_Socket:
338 *system_level = SOL_SOCKET;
341 case SocketOptionName_DontLinger:
342 /* This is SO_LINGER, because the setsockopt
343 * internal call maps DontLinger to SO_LINGER
346 *system_name = SO_LINGER;
348 case SocketOptionName_Debug:
349 *system_name = SO_DEBUG;
352 case SocketOptionName_AcceptConnection:
353 *system_name = SO_ACCEPTCONN;
356 case SocketOptionName_ReuseAddress:
357 *system_name = SO_REUSEADDR;
359 case SocketOptionName_KeepAlive:
360 *system_name = SO_KEEPALIVE;
362 case SocketOptionName_DontRoute:
363 *system_name = SO_DONTROUTE;
365 case SocketOptionName_Broadcast:
366 *system_name = SO_BROADCAST;
368 case SocketOptionName_Linger:
369 *system_name = SO_LINGER;
371 case SocketOptionName_OutOfBandInline:
372 *system_name = SO_OOBINLINE;
374 case SocketOptionName_SendBuffer:
375 *system_name = SO_SNDBUF;
377 case SocketOptionName_ReceiveBuffer:
378 *system_name = SO_RCVBUF;
380 case SocketOptionName_SendLowWater:
381 *system_name = SO_SNDLOWAT;
383 case SocketOptionName_ReceiveLowWater:
384 *system_name = SO_RCVLOWAT;
386 case SocketOptionName_SendTimeout:
387 *system_name = SO_SNDTIMEO;
389 case SocketOptionName_ReceiveTimeout:
390 *system_name = SO_RCVTIMEO;
392 case SocketOptionName_Error:
393 *system_name = SO_ERROR;
395 case SocketOptionName_Type:
396 *system_name = SO_TYPE;
399 case SocketOptionName_PeerCred:
400 *system_name = SO_PEERCRED;
403 case SocketOptionName_ExclusiveAddressUse:
404 #ifdef SO_EXCLUSIVEADDRUSE
405 *system_name = SO_EXCLUSIVEADDRUSE;
408 case SocketOptionName_UseLoopback:
409 #ifdef SO_USELOOPBACK
410 *system_name = SO_USELOOPBACK;
413 case SocketOptionName_MaxConnections:
415 *system_name = SO_MAXCONN;
417 #elif defined(SOMAXCONN)
418 *system_name = SOMAXCONN;
422 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
427 case SocketOptionLevel_IP:
429 *system_level = SOL_IP;
432 static int cached = 0;
436 struct protoent *pent;
438 pent = getprotobyname ("IP");
439 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
443 *system_level = proto;
445 #endif /* HAVE_SOL_IP */
448 case SocketOptionName_IPOptions:
449 *system_name = IP_OPTIONS;
452 case SocketOptionName_HeaderIncluded:
453 *system_name = IP_HDRINCL;
457 case SocketOptionName_TypeOfService:
458 *system_name = IP_TOS;
462 case SocketOptionName_IpTimeToLive:
463 *system_name = IP_TTL;
466 case SocketOptionName_MulticastInterface:
467 *system_name = IP_MULTICAST_IF;
469 case SocketOptionName_MulticastTimeToLive:
470 *system_name = IP_MULTICAST_TTL;
472 case SocketOptionName_MulticastLoopback:
473 *system_name = IP_MULTICAST_LOOP;
475 case SocketOptionName_AddMembership:
476 *system_name = IP_ADD_MEMBERSHIP;
478 case SocketOptionName_DropMembership:
479 *system_name = IP_DROP_MEMBERSHIP;
481 #ifdef HAVE_IP_PKTINFO
482 case SocketOptionName_PacketInformation:
483 *system_name = IP_PKTINFO;
485 #endif /* HAVE_IP_PKTINFO */
487 case SocketOptionName_DontFragment:
488 #ifdef HAVE_IP_DONTFRAGMENT
489 *system_name = IP_DONTFRAGMENT;
491 #elif defined HAVE_IP_MTU_DISCOVER
492 /* Not quite the same */
493 *system_name = IP_MTU_DISCOVER;
496 /* If the flag is not available on this system, we can ignore this error */
498 #endif /* HAVE_IP_DONTFRAGMENT */
499 case SocketOptionName_AddSourceMembership:
500 case SocketOptionName_DropSourceMembership:
501 case SocketOptionName_BlockSource:
502 case SocketOptionName_UnblockSource:
503 /* Can't figure out how to map these, so fall
507 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
513 case SocketOptionLevel_IPv6:
515 *system_level = SOL_IPV6;
518 static int cached = 0;
522 struct protoent *pent;
524 pent = getprotobyname ("IPV6");
525 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
529 *system_level = proto;
531 #endif /* HAVE_SOL_IPV6 */
534 case SocketOptionName_IpTimeToLive:
535 *system_name = IPV6_UNICAST_HOPS;
537 case SocketOptionName_MulticastInterface:
538 *system_name = IPV6_MULTICAST_IF;
540 case SocketOptionName_MulticastTimeToLive:
541 *system_name = IPV6_MULTICAST_HOPS;
543 case SocketOptionName_MulticastLoopback:
544 *system_name = IPV6_MULTICAST_LOOP;
546 case SocketOptionName_AddMembership:
547 *system_name = IPV6_JOIN_GROUP;
549 case SocketOptionName_DropMembership:
550 *system_name = IPV6_LEAVE_GROUP;
552 case SocketOptionName_PacketInformation:
553 #ifdef HAVE_IPV6_PKTINFO
554 *system_name = IPV6_PKTINFO;
557 case SocketOptionName_HeaderIncluded:
558 case SocketOptionName_IPOptions:
559 case SocketOptionName_TypeOfService:
560 case SocketOptionName_DontFragment:
561 case SocketOptionName_AddSourceMembership:
562 case SocketOptionName_DropSourceMembership:
563 case SocketOptionName_BlockSource:
564 case SocketOptionName_UnblockSource:
565 /* Can't figure out how to map these, so fall
569 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
573 break; /* SocketOptionLevel_IPv6 */
576 case SocketOptionLevel_Tcp:
578 *system_level = SOL_TCP;
581 static int cached = 0;
585 struct protoent *pent;
587 pent = getprotobyname ("TCP");
588 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
592 *system_level = proto;
594 #endif /* HAVE_SOL_TCP */
597 case SocketOptionName_NoDelay:
598 *system_name = TCP_NODELAY;
601 /* The documentation is talking complete
602 * bollocks here: rfc-1222 is titled
603 * 'Advancing the NSFNET Routing Architecture'
604 * and doesn't mention either of the words
605 * "expedite" or "urgent".
607 case SocketOptionName_BsdUrgent:
608 case SocketOptionName_Expedited:
611 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
616 case SocketOptionLevel_Udp:
617 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
620 case SocketOptionName_NoChecksum:
621 case SocketOptionName_ChecksumCoverage:
623 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
630 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
637 static MonoImage *get_socket_assembly (void)
639 static const char *version = NULL;
640 static gboolean moonlight;
641 static MonoImage *socket_assembly = NULL;
643 if (version == NULL) {
644 version = mono_get_runtime_info ()->framework_version;
645 moonlight = !strcmp (version, "2.1");
648 if (socket_assembly == NULL) {
650 socket_assembly = mono_image_loaded ("System.Net");
651 if (!socket_assembly) {
652 MonoAssembly *sa = mono_assembly_open ("System.Net.dll", NULL);
655 g_assert_not_reached ();
657 socket_assembly = mono_assembly_get_image (sa);
661 socket_assembly = mono_image_loaded ("System");
662 if (!socket_assembly) {
663 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
666 g_assert_not_reached ();
668 socket_assembly = mono_assembly_get_image (sa);
674 return(socket_assembly);
678 static gint32 get_family_hint(void)
680 MonoDomain *domain = mono_domain_get ();
682 if (!domain->inet_family_hint) {
683 MonoClass *socket_class;
684 MonoClassField *ipv6_field, *ipv4_field;
685 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
688 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
689 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
690 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
691 vtable = mono_class_vtable (mono_domain_get (), socket_class);
693 mono_runtime_class_init (vtable);
695 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
696 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
698 mono_domain_lock (domain);
699 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
700 domain->inet_family_hint = 1;
701 } else if (ipv4_enabled == 1) {
702 domain->inet_family_hint = 2;
704 domain->inet_family_hint = 3;
706 mono_domain_unlock (domain);
708 switch (domain->inet_family_hint) {
709 case 1: return PF_UNSPEC;
710 case 2: return PF_INET;
711 case 3: return PF_INET6;
718 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
729 sock_family=convert_family(family);
730 if(sock_family==-1) {
731 *error = WSAEAFNOSUPPORT;
735 sock_proto=convert_proto(proto);
737 *error = WSAEPROTONOSUPPORT;
741 sock_type=convert_type(type);
743 *error = WSAESOCKTNOSUPPORT;
747 sock = _wapi_socket (sock_family, sock_type, sock_proto,
748 NULL, 0, WSA_FLAG_OVERLAPPED);
750 if(sock==INVALID_SOCKET) {
751 *error = WSAGetLastError ();
755 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
756 return (GUINT_TO_POINTER (sock));
760 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
761 return (GUINT_TO_POINTER (sock));
765 return(GUINT_TO_POINTER (sock));
768 /* FIXME: the SOCKET parameter (here and in other functions in this
769 * file) is really an IntPtr which needs to be converted to a guint32.
771 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
776 LOGDEBUG (g_message ("%s: closing 0x%x", __func__, sock));
780 /* Clear any pending work item from this socket if the underlying
781 * polling system does not notify when the socket is closed */
782 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
786 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
790 LOGDEBUG (g_message("%s: returning %d", __func__, WSAGetLastError()));
792 return(WSAGetLastError());
795 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
805 ret=ioctlsocket(sock, FIONREAD, &amount);
806 if(ret==SOCKET_ERROR) {
807 *error = WSAGetLastError ();
814 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
825 * block == TRUE/FALSE means we will block/not block.
826 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
830 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
831 if(ret==SOCKET_ERROR) {
832 *error = WSAGetLastError ();
836 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
845 #ifdef PLATFORM_WIN32
847 MonoThread* curthread = mono_thread_current ();
848 curthread->interrupt_on_stop = (gpointer)TRUE;
849 newsock = _wapi_accept (sock, NULL, 0);
850 curthread->interrupt_on_stop = (gpointer)FALSE;
853 newsock = _wapi_accept (sock, NULL, 0);
855 if(newsock==INVALID_SOCKET) {
856 *error = WSAGetLastError ();
860 return(GUINT_TO_POINTER (newsock));
863 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
873 ret = _wapi_listen (sock, backlog);
874 if(ret==SOCKET_ERROR) {
875 *error = WSAGetLastError ();
879 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
880 int sa_size, gint32 *error)
882 MonoDomain *domain = mono_domain_get ();
883 MonoObject *sockaddr_obj;
884 MonoClass *sockaddr_class;
885 MonoClassField *field;
887 MonoAddressFamily family;
889 /* Build a System.Net.SocketAddress object instance */
890 sockaddr_class=mono_class_from_name_cached (get_socket_assembly (), "System.Net", "SocketAddress");
891 sockaddr_obj=mono_object_new(domain, sockaddr_class);
893 /* Locate the SocketAddress data buffer in the object */
894 field=mono_class_get_field_from_name_cached (sockaddr_class, "data");
896 /* Make sure there is space for the family and size bytes */
898 if (saddr->sa_family == AF_UNIX) {
899 /* sa_len includes the entire sockaddr size, so we don't need the
900 * N bytes (sizeof (unsigned short)) of the family. */
901 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size);
905 /* May be the +2 here is too conservative, as sa_len returns
906 * the length of the entire sockaddr_in/in6, including
907 * sizeof (unsigned short) of the family */
908 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
911 /* The data buffer is laid out as follows:
912 * bytes 0 and 1 are the address family
913 * bytes 2 and 3 are the port info
914 * the rest is the address info
917 family=convert_to_mono_family(saddr->sa_family);
918 if(family==AddressFamily_Unknown) {
919 *error = WSAEAFNOSUPPORT;
923 mono_array_set(data, guint8, 0, family & 0x0FF);
924 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
926 if(saddr->sa_family==AF_INET) {
927 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
928 guint16 port=ntohs(sa_in->sin_port);
929 guint32 address=ntohl(sa_in->sin_addr.s_addr);
932 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
935 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
936 mono_array_set(data, guint8, 3, (port) & 0xff);
937 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
938 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
939 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
940 mono_array_set(data, guint8, 7, (address) & 0xff);
942 mono_field_set_value (sockaddr_obj, field, data);
944 return(sockaddr_obj);
946 } else if (saddr->sa_family == AF_INET6) {
947 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
950 guint16 port=ntohs(sa_in->sin6_port);
953 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
956 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
957 mono_array_set(data, guint8, 3, (port) & 0xff);
959 for(i=0; i<16; i++) {
960 mono_array_set(data, guint8, 8+i,
961 sa_in->sin6_addr.s6_addr[i]);
964 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
965 mono_array_set(data, guint8, 25,
966 (sa_in->sin6_scope_id >> 8) & 0xff);
967 mono_array_set(data, guint8, 26,
968 (sa_in->sin6_scope_id >> 16) & 0xff);
969 mono_array_set(data, guint8, 27,
970 (sa_in->sin6_scope_id >> 24) & 0xff);
972 mono_field_set_value (sockaddr_obj, field, data);
974 return(sockaddr_obj);
977 } else if (saddr->sa_family == AF_UNIX) {
980 for (i = 0; i < sa_size; i++) {
981 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
984 mono_field_set_value (sockaddr_obj, field, data);
989 *error = WSAEAFNOSUPPORT;
994 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
996 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1000 MONO_ARCH_SAVE_REGS;
1005 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1007 if(ret==SOCKET_ERROR) {
1008 *error = WSAGetLastError ();
1012 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)));
1014 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1018 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
1020 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1024 MONO_ARCH_SAVE_REGS;
1029 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1031 if(ret==SOCKET_ERROR) {
1032 *error = WSAGetLastError ();
1036 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)));
1038 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1042 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1046 MonoClassField *field;
1051 /* Dig the SocketAddress data buffer out of the object */
1052 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1053 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1055 /* The data buffer is laid out as follows:
1056 * byte 0 is the address family low byte
1057 * byte 1 is the address family high byte
1059 * bytes 2 and 3 are the port info
1060 * the rest is the address info
1062 * the rest is the file name
1064 len = mono_array_length (data);
1066 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1069 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1070 if (family == AF_INET) {
1071 struct sockaddr_in *sa;
1076 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1079 sa = g_new0 (struct sockaddr_in, 1);
1080 port = (mono_array_get (data, guint8, 2) << 8) +
1081 mono_array_get (data, guint8, 3);
1082 address = (mono_array_get (data, guint8, 4) << 24) +
1083 (mono_array_get (data, guint8, 5) << 16 ) +
1084 (mono_array_get (data, guint8, 6) << 8) +
1085 mono_array_get (data, guint8, 7);
1087 sa->sin_family = family;
1088 sa->sin_addr.s_addr = htonl (address);
1089 sa->sin_port = htons (port);
1091 *sa_size = sizeof(struct sockaddr_in);
1092 return((struct sockaddr *)sa);
1095 } else if (family == AF_INET6) {
1096 struct sockaddr_in6 *sa;
1102 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1105 sa = g_new0 (struct sockaddr_in6, 1);
1106 port = mono_array_get (data, guint8, 3) +
1107 (mono_array_get (data, guint8, 2) << 8);
1108 scopeid = mono_array_get (data, guint8, 24) +
1109 (mono_array_get (data, guint8, 25) << 8) +
1110 (mono_array_get (data, guint8, 26) << 16) +
1111 (mono_array_get (data, guint8, 27) << 24);
1113 sa->sin6_family = family;
1114 sa->sin6_port = htons (port);
1115 sa->sin6_scope_id = scopeid;
1117 for(i=0; i<16; i++) {
1118 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1121 *sa_size = sizeof(struct sockaddr_in6);
1122 return((struct sockaddr *)sa);
1124 #ifdef HAVE_SYS_UN_H
1125 } else if (family == AF_UNIX) {
1126 struct sockaddr_un *sock_un;
1129 /* Need a byte for the '\0' terminator/prefix, and the first
1130 * two bytes hold the SocketAddress family
1132 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1133 mono_raise_exception (mono_get_exception_index_out_of_range ());
1136 sock_un = g_new0 (struct sockaddr_un, 1);
1138 sock_un->sun_family = family;
1139 for (i = 0; i < len - 2; i++) {
1140 sock_un->sun_path [i] = mono_array_get (data, guint8,
1146 return (struct sockaddr *)sock_un;
1149 *error = WSAEAFNOSUPPORT;
1154 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1156 struct sockaddr *sa;
1160 MONO_ARCH_SAVE_REGS;
1164 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1169 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)));
1171 ret = _wapi_bind (sock, sa, sa_size);
1172 if(ret==SOCKET_ERROR) {
1173 *error = WSAGetLastError ();
1186 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1187 gint timeout, gint32 *error)
1189 MonoThread *thread = NULL;
1195 MONO_ARCH_SAVE_REGS;
1197 pfds = g_new0 (mono_pollfd, 1);
1198 pfds[0].fd = GPOINTER_TO_INT (sock);
1199 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1200 (mode == SelectModeWrite) ? MONO_POLLOUT :
1201 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1203 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1204 start = time (NULL);
1208 ret = mono_poll (pfds, 1, timeout);
1209 if (timeout > 0 && ret < 0) {
1211 int sec = time (NULL) - start;
1213 timeout -= sec * 1000;
1221 if (ret == -1 && errno == EINTR) {
1224 if (thread == NULL) {
1225 thread = mono_thread_current ();
1228 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1234 /* Suspend requested? */
1235 mono_thread_interruption_checkpoint ();
1239 } while (ret == -1 && errno == EINTR);
1242 #ifdef PLATFORM_WIN32
1243 *error = WSAGetLastError ();
1245 *error = errno_to_WSA (errno, __func__);
1260 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1262 struct sockaddr *sa;
1266 MONO_ARCH_SAVE_REGS;
1270 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1275 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)));
1277 ret = _wapi_connect (sock, sa, sa_size);
1278 if(ret==SOCKET_ERROR) {
1279 *error = WSAGetLastError ();
1285 /* These #defines from mswsock.h from wine. Defining them here allows
1286 * us to build this file on a mingw box that doesn't know the magic
1287 * numbers, but still run on a newer windows box that does.
1289 #ifndef WSAID_DISCONNECTEX
1290 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1291 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1294 #ifndef WSAID_TRANSMITFILE
1295 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1296 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1299 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1302 glong output_bytes = 0;
1303 GUID disco_guid = WSAID_DISCONNECTEX;
1304 GUID trans_guid = WSAID_TRANSMITFILE;
1305 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1306 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1309 MONO_ARCH_SAVE_REGS;
1313 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1315 /* I _think_ the extension function pointers need to be looked
1316 * up for each socket. FIXME: check the best way to store
1317 * pointers to functions in managed objects that still works
1318 * on 64bit platforms.
1320 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1321 (void *)&disco_guid, sizeof(GUID),
1322 (void *)&_wapi_disconnectex, sizeof(void *),
1323 &output_bytes, NULL, NULL);
1325 /* make sure that WSAIoctl didn't put crap in the
1328 _wapi_disconnectex = NULL;
1330 /* Look up the TransmitFile extension function pointer
1331 * instead of calling TransmitFile() directly, because
1332 * apparently "Several of the extension functions have
1333 * been available since WinSock 1.1 and are exported
1334 * from MSWsock.dll, however it's not advisable to
1335 * link directly to this dll as this ties you to the
1336 * Microsoft WinSock provider. A provider neutral way
1337 * of accessing these extension functions is to load
1338 * them dynamically via WSAIoctl using the
1339 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1340 * should, theoretically, allow you to access these
1341 * functions from any provider that supports them..."
1342 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1344 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1345 (void *)&trans_guid, sizeof(GUID),
1346 (void *)&_wapi_transmitfile, sizeof(void *),
1347 &output_bytes, NULL, NULL);
1349 _wapi_transmitfile = NULL;
1353 if (_wapi_disconnectex != NULL) {
1354 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1355 } else if (_wapi_transmitfile != NULL) {
1356 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1357 TF_DISCONNECT | TF_REUSE_SOCKET);
1359 *error = ERROR_NOT_SUPPORTED;
1363 if (bret == FALSE) {
1364 *error = WSAGetLastError ();
1368 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1375 MONO_ARCH_SAVE_REGS;
1379 alen = mono_array_length (buffer);
1380 if (offset > alen - count) {
1384 buf=mono_array_addr(buffer, guchar, offset);
1386 recvflags = convert_socketflags (flags);
1387 if (recvflags == -1) {
1388 *error = WSAEOPNOTSUPP;
1392 #ifdef PLATFORM_WIN32
1394 MonoThread* curthread = mono_thread_current ();
1395 curthread->interrupt_on_stop = (gpointer)TRUE;
1396 ret = _wapi_recv (sock, buf, count, recvflags);
1397 curthread->interrupt_on_stop = (gpointer)FALSE;
1400 ret = _wapi_recv (sock, buf, count, recvflags);
1403 if(ret==SOCKET_ERROR) {
1404 *error = WSAGetLastError ();
1411 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1416 DWORD recvflags = 0;
1418 MONO_ARCH_SAVE_REGS;
1422 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1423 count = mono_array_length (buffers);
1425 recvflags = convert_socketflags (flags);
1426 if (recvflags == -1) {
1427 *error = WSAEOPNOTSUPP;
1431 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1432 if (ret == SOCKET_ERROR) {
1433 *error = WSAGetLastError ();
1440 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1446 struct sockaddr *sa;
1449 MONO_ARCH_SAVE_REGS;
1453 alen = mono_array_length (buffer);
1454 if (offset > alen - count) {
1458 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1463 buf=mono_array_addr(buffer, guchar, offset);
1465 recvflags = convert_socketflags (flags);
1466 if (recvflags == -1) {
1467 *error = WSAEOPNOTSUPP;
1471 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1472 if(ret==SOCKET_ERROR) {
1474 *error = WSAGetLastError ();
1478 /* If we didn't get a socket size, then we're probably a
1479 * connected connection-oriented socket and the stack hasn't
1480 * returned the remote address. All we can do is return null.
1483 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1492 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1499 MONO_ARCH_SAVE_REGS;
1503 alen = mono_array_length (buffer);
1504 if (offset > alen - count) {
1508 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1510 buf=mono_array_addr(buffer, guchar, offset);
1512 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1514 sendflags = convert_socketflags (flags);
1515 if (sendflags == -1) {
1516 *error = WSAEOPNOTSUPP;
1520 ret = _wapi_send (sock, buf, count, sendflags);
1521 if(ret==SOCKET_ERROR) {
1522 *error = WSAGetLastError ();
1529 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1534 DWORD sendflags = 0;
1536 MONO_ARCH_SAVE_REGS;
1540 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1541 count = mono_array_length (buffers);
1543 sendflags = convert_socketflags (flags);
1544 if (sendflags == -1) {
1545 *error = WSAEOPNOTSUPP;
1549 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1550 if (ret == SOCKET_ERROR) {
1551 *error = WSAGetLastError ();
1558 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1564 struct sockaddr *sa;
1567 MONO_ARCH_SAVE_REGS;
1571 alen = mono_array_length (buffer);
1572 if (offset > alen - count) {
1576 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1581 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1583 buf=mono_array_addr(buffer, guchar, offset);
1585 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1587 sendflags = convert_socketflags (flags);
1588 if (sendflags == -1) {
1589 *error = WSAEOPNOTSUPP;
1593 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1594 if(ret==SOCKET_ERROR) {
1595 *error = WSAGetLastError ();
1603 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1606 MonoClassField *field;
1608 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1609 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1614 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1615 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1617 MonoThread *thread = NULL;
1624 MonoClass *sock_arr_class;
1627 mono_array_size_t socks_size;
1629 MONO_ARCH_SAVE_REGS;
1631 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1632 count = mono_array_length (*sockets);
1633 nfds = count - 3; /* NULL separators */
1634 pfds = g_new0 (mono_pollfd, nfds);
1636 for (i = 0; i < count; i++) {
1637 obj = mono_array_get (*sockets, MonoObject *, i);
1644 /* The socket array was bogus */
1650 pfds [idx].fd = Socket_to_SOCKET (obj);
1651 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1655 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1656 start = time (NULL);
1659 ret = mono_poll (pfds, nfds, timeout);
1660 if (timeout > 0 && ret < 0) {
1662 int sec = time (NULL) - start;
1664 timeout -= sec * 1000;
1670 if (ret == -1 && errno == EINTR) {
1673 thread = mono_thread_current ();
1675 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1682 /* Suspend requested? */
1683 mono_thread_interruption_checkpoint ();
1687 } while (ret == -1 && errno == EINTR);
1690 #ifdef PLATFORM_WIN32
1691 *error = WSAGetLastError ();
1693 *error = errno_to_WSA (errno, __func__);
1705 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1706 socks_size = ((mono_array_size_t)ret) + 3; /* space for the NULL delimiters */
1707 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1710 for (i = 0; i < count && ret > 0; i++) {
1713 obj = mono_array_get (*sockets, MonoObject *, i);
1720 pfd = &pfds [i - mode];
1721 if (pfd->revents == 0)
1725 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1726 mono_array_setref (socks, idx++, obj);
1727 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1728 mono_array_setref (socks, idx++, obj);
1729 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1730 mono_array_setref (socks, idx++, obj);
1738 static MonoObject* int_to_object (MonoDomain *domain, int val)
1740 return mono_value_box (domain, mono_get_int32_class (), &val);
1744 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1750 socklen_t valsize=sizeof(val);
1751 struct linger linger;
1752 socklen_t lingersize=sizeof(linger);
1754 socklen_t time_ms_size = sizeof (time_ms);
1757 socklen_t credsize = sizeof(cred);
1759 MonoDomain *domain=mono_domain_get();
1761 MonoClass *obj_class;
1762 MonoClassField *field;
1764 MONO_ARCH_SAVE_REGS;
1768 ret=convert_sockopt_level_and_name(level, name, &system_level,
1771 *error = WSAENOPROTOOPT;
1775 *obj_val = int_to_object (domain, 0);
1779 /* No need to deal with MulticastOption names here, because
1780 * you cant getsockopt AddMembership or DropMembership (the
1781 * int getsockopt will error, causing an exception)
1784 case SocketOptionName_Linger:
1785 case SocketOptionName_DontLinger:
1786 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1790 case SocketOptionName_SendTimeout:
1791 case SocketOptionName_ReceiveTimeout:
1792 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1796 case SocketOptionName_PeerCred:
1797 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1803 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1807 if(ret==SOCKET_ERROR) {
1808 *error = WSAGetLastError ();
1813 case SocketOptionName_Linger:
1814 /* build a System.Net.Sockets.LingerOption */
1815 obj_class=mono_class_from_name(get_socket_assembly (),
1816 "System.Net.Sockets",
1818 obj=mono_object_new(domain, obj_class);
1820 /* Locate and set the fields "bool enabled" and "int
1823 field=mono_class_get_field_from_name(obj_class, "enabled");
1824 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1826 field=mono_class_get_field_from_name(obj_class, "seconds");
1827 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1831 case SocketOptionName_DontLinger:
1832 /* construct a bool int in val - true if linger is off */
1833 obj = int_to_object (domain, !linger.l_onoff);
1836 case SocketOptionName_SendTimeout:
1837 case SocketOptionName_ReceiveTimeout:
1838 obj = int_to_object (domain, time_ms);
1842 case SocketOptionName_PeerCred:
1844 /* build a Mono.Posix.PeerCred+PeerCredData if
1847 static MonoImage *mono_posix_image = NULL;
1848 MonoPeerCredData *cred_data;
1850 if (mono_posix_image == NULL) {
1851 mono_posix_image=mono_image_loaded ("Mono.Posix");
1852 if (!mono_posix_image) {
1853 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1855 *error = WSAENOPROTOOPT;
1858 mono_posix_image = mono_assembly_get_image (sa);
1863 obj_class = mono_class_from_name(mono_posix_image,
1866 obj = mono_object_new(domain, obj_class);
1867 cred_data = (MonoPeerCredData *)obj;
1868 cred_data->pid = cred.pid;
1869 cred_data->uid = cred.uid;
1870 cred_data->gid = cred.gid;
1876 obj = int_to_object (domain, val);
1882 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1890 MONO_ARCH_SAVE_REGS;
1894 ret=convert_sockopt_level_and_name(level, name, &system_level,
1897 *error = WSAENOPROTOOPT;
1903 valsize=mono_array_length(*byte_val);
1904 buf=mono_array_addr(*byte_val, guchar, 0);
1906 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1907 if(ret==SOCKET_ERROR) {
1908 *error = WSAGetLastError ();
1912 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1913 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1915 struct in_addr inaddr;
1916 MonoClassField *field;
1918 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
1920 /* No idea why .net uses a 64bit type to hold a 32bit value...
1922 * Internal value of IPAddess is in little-endian order
1924 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
1931 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1933 struct in6_addr in6addr;
1934 MonoClassField *field;
1938 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
1939 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1941 /* Solaris has only the 8 bit version. */
1943 for(i=0; i<8; i++) {
1944 guint16 s = mono_array_get (data, guint16, i);
1945 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1946 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1950 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1954 #endif /* AF_INET6 */
1956 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)
1958 struct linger linger;
1968 #ifdef HAVE_SOL_IPV6
1969 sol_ipv6 = SOL_IPV6;
1972 struct protoent *pent;
1973 pent = getprotobyname ("ipv6");
1974 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
1982 struct protoent *pent;
1983 pent = getprotobyname ("ip");
1984 sol_ip = (pent != NULL) ? pent->p_proto : 0;
1987 #endif /* AF_INET6 */
1989 MONO_ARCH_SAVE_REGS;
1991 ret=convert_sockopt_level_and_name(level, name, &system_level,
1994 *error = WSAENOPROTOOPT;
2001 /* Only one of obj_val, byte_val or int_val has data */
2003 MonoClassField *field;
2007 case SocketOptionName_Linger:
2008 /* Dig out "bool enabled" and "int seconds"
2011 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2012 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2013 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2014 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2016 valsize=sizeof(linger);
2017 ret = _wapi_setsockopt (sock, system_level,
2018 system_name, &linger, valsize);
2020 case SocketOptionName_AddMembership:
2021 case SocketOptionName_DropMembership:
2022 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2024 MonoObject *address = NULL;
2027 if(system_level == sol_ipv6) {
2028 struct ipv6_mreq mreq6;
2033 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2034 address = *(gpointer *)(((char *)obj_val) + field->offset);
2037 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2040 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2041 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2043 ret = _wapi_setsockopt (sock, system_level,
2044 system_name, &mreq6,
2046 } else if(system_level == sol_ip)
2047 #endif /* AF_INET6 */
2049 #ifdef HAVE_STRUCT_IP_MREQN
2050 struct ip_mreqn mreq = {{0}};
2052 struct ip_mreq mreq = {{0}};
2053 #endif /* HAVE_STRUCT_IP_MREQN */
2055 /* pain! MulticastOption holds two IPAddress
2056 * members, so I have to dig the value out of
2059 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2060 address = *(gpointer *)(((char *)obj_val) + field->offset);
2062 /* address might not be defined and if so, set the address to ADDR_ANY.
2065 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2068 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2069 address = *(gpointer *)(((char *)obj_val) + field->offset);
2071 #ifdef HAVE_STRUCT_IP_MREQN
2073 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2077 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2079 #endif /* HAVE_STRUCT_IP_MREQN */
2081 ret = _wapi_setsockopt (sock, system_level,
2087 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2089 /* Cause an exception to be thrown */
2093 } else if (byte_val!=NULL) {
2094 int valsize = mono_array_length (byte_val);
2095 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2098 case SocketOptionName_DontLinger:
2100 linger.l_onoff = (*buf) ? 0 : 1;
2101 linger.l_linger = 0;
2102 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2108 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2112 /* ReceiveTimeout/SendTimeout get here */
2114 case SocketOptionName_DontLinger:
2115 linger.l_onoff = !int_val;
2116 linger.l_linger = 0;
2117 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2119 case SocketOptionName_DontFragment:
2120 #ifdef HAVE_IP_MTU_DISCOVER
2121 /* Fiddle with the value slightly if we're
2125 int_val = IP_PMTUDISC_DO;
2131 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2135 if(ret==SOCKET_ERROR) {
2136 *error = WSAGetLastError ();
2140 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2146 MONO_ARCH_SAVE_REGS;
2150 /* Currently, the values for how (recv=0, send=1, both=2) match
2153 ret = _wapi_shutdown (sock, how);
2154 if(ret==SOCKET_ERROR) {
2155 *error = WSAGetLastError ();
2160 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2162 MonoArray *output, gint32 *error)
2164 glong output_bytes = 0;
2165 gchar *i_buffer, *o_buffer;
2169 MONO_ARCH_SAVE_REGS;
2173 if (code == FIONBIO) {
2174 /* Invalid command. Must use Socket.Blocking */
2178 if (input == NULL) {
2182 i_buffer = mono_array_addr (input, gchar, 0);
2183 i_len = mono_array_length (input);
2186 if (output == NULL) {
2190 o_buffer = mono_array_addr (output, gchar, 0);
2191 o_len = mono_array_length (output);
2194 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2195 if (ret == SOCKET_ERROR) {
2196 *error = WSAGetLastError ();
2200 return (gint) output_bytes;
2203 #ifdef HAVE_SIOCGIFCONF
2205 is_loopback (int family, void *ad)
2207 char *ptr = (char *) ad;
2209 if (family == AF_INET) {
2210 return (ptr [0] == 127);
2214 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2221 get_local_ips (int family, int *nips)
2223 int addr_size, offset, fd, i, count;
2224 int max_ifaces = 50; /* 50 interfaces should be enough... */
2227 struct ifreq iflags;
2228 char *result, *tmp_ptr;
2229 gboolean ignore_loopback = FALSE;
2232 if (family == AF_INET) {
2233 addr_size = sizeof (struct in_addr);
2234 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2236 } else if (family == AF_INET6) {
2237 addr_size = sizeof (struct in6_addr);
2238 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2244 fd = socket (family, SOCK_STREAM, 0);
2246 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2247 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2248 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2250 g_free (ifc.ifc_buf);
2254 count = ifc.ifc_len / sizeof (struct ifreq);
2257 g_free (ifc.ifc_buf);
2262 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2263 strcpy (iflags.ifr_name, ifr->ifr_name);
2264 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2268 if ((iflags.ifr_flags & IFF_UP) == 0) {
2269 ifr->ifr_name [0] = '\0';
2273 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2274 ignore_loopback = TRUE;
2279 result = g_malloc (addr_size * count);
2281 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2282 if (ifr->ifr_name [0] == '\0') {
2287 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2292 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2293 tmp_ptr += addr_size;
2296 g_free (ifc.ifc_buf);
2301 get_local_ips (int family, int *nips)
2307 #endif /* HAVE_SIOCGIFCONF */
2310 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2311 MonoArray **h_aliases,
2312 MonoArray **h_addr_list,
2313 gboolean add_local_ips)
2315 MonoDomain *domain = mono_domain_get ();
2317 struct in_addr *local_in = NULL;
2321 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2325 *h_name=mono_string_new(domain, he->h_name);
2327 while(he->h_aliases[i]!=NULL) {
2331 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2333 while(he->h_aliases[i]!=NULL) {
2336 alias=mono_string_new(domain, he->h_aliases[i]);
2337 mono_array_setref (*h_aliases, i, alias);
2340 } else if (!add_local_ips) {
2344 if (add_local_ips) {
2345 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2347 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2348 for (i = 0; i < nlocal_in; i++) {
2349 MonoString *addr_string;
2350 char addr [16], *ptr;
2352 ptr = (char *) &local_in [i];
2353 g_snprintf(addr, 16, "%u.%u.%u.%u",
2354 (unsigned char) ptr [0],
2355 (unsigned char) ptr [1],
2356 (unsigned char) ptr [2],
2357 (unsigned char) ptr [3]);
2359 addr_string = mono_string_new (domain, addr);
2360 mono_array_setref (*h_addr_list, i, addr_string);
2365 } else if (he == NULL) {
2366 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2367 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2368 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2373 if (nlocal_in == 0 && he != NULL) {
2375 while (he->h_addr_list[i]!=NULL) {
2379 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2381 while(he->h_addr_list[i]!=NULL) {
2382 MonoString *addr_string;
2385 g_snprintf(addr, 16, "%u.%u.%u.%u",
2386 (unsigned char)he->h_addr_list[i][0],
2387 (unsigned char)he->h_addr_list[i][1],
2388 (unsigned char)he->h_addr_list[i][2],
2389 (unsigned char)he->h_addr_list[i][3]);
2391 addr_string=mono_string_new(domain, addr);
2392 mono_array_setref (*h_addr_list, i, addr_string);
2400 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2401 MonoArray **h_aliases,
2402 MonoArray **h_addr_list)
2404 MonoDomain *domain = mono_domain_get ();
2406 *h_name=mono_string_new(domain, addr);
2407 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2408 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2409 mono_array_setref (*h_addr_list, 0, *h_name);
2415 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2416 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2417 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2419 MonoDomain *domain = mono_domain_get ();
2420 int i, host_count, host_index, family_hint;
2421 struct in_addr *local_in = NULL;
2423 struct in6_addr *local_in6 = NULL;
2425 gboolean from_local = FALSE;
2427 family_hint = get_family_hint ();
2430 * Check if address length and family are correct
2432 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2436 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2441 * Get the aliases and host name from he1 or he2 whichever is
2442 * not null, if he1 is not null then take aliases from he1
2444 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2445 family_hint == PF_INET)) {
2446 *h_name=mono_string_new (domain, he1->h_name);
2449 while(he1->h_aliases[i]!=NULL) {
2453 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2456 while(he1->h_aliases[i]!=NULL) {
2459 alias=mono_string_new (domain, he1->h_aliases[i]);
2460 mono_array_setref (*h_aliases, i, alias);
2463 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2464 family_hint == PF_INET6)) {
2465 *h_name=mono_string_new (domain, he2->h_name);
2468 while(he2->h_aliases [i] != NULL) {
2472 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2475 while(he2->h_aliases[i]!=NULL) {
2478 alias=mono_string_new (domain, he2->h_aliases[i]);
2479 mono_array_setref (*h_aliases, i, alias);
2482 } else if (!add_local_ips) {
2487 * Count the number of addresses in he1 + he2
2490 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2491 family_hint == PF_INET)) {
2493 while(he1->h_addr_list[i]!=NULL) {
2499 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2500 family_hint == PF_INET6)) {
2502 while(he2->h_addr_list[i]!=NULL) {
2512 if (add_local_ips) {
2513 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2514 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2516 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2517 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2519 if (nlocal_in || nlocal_in6) {
2521 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2522 nlocal_in + nlocal_in6);
2526 for (n = 0; n < nlocal_in6; n++) {
2527 MonoString *addr_string;
2529 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2531 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2534 addr_string = mono_string_new (domain, addr);
2535 mono_array_setref (*h_addr_list, host_index, addr_string);
2543 for (n = 0; n < nlocal_in; n++) {
2544 MonoString *addr_string;
2546 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2548 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2551 addr_string = mono_string_new (domain, addr);
2552 mono_array_setref (*h_addr_list, host_index, addr_string);
2560 } else if (he1 == NULL && he2 == NULL) {
2561 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2562 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2563 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2573 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2575 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2576 family_hint == PF_INET6)) {
2578 while(he2->h_addr_list[i] != NULL) {
2579 MonoString *addr_string;
2581 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2583 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2587 addr_string = mono_string_new (domain, addr);
2588 mono_array_setref (*h_addr_list, host_index, addr_string);
2595 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2596 family_hint == PF_INET)) {
2598 while(he1->h_addr_list[i] != NULL) {
2599 MonoString *addr_string;
2601 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2603 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2607 addr_string=mono_string_new (domain, addr);
2608 mono_array_setref (*h_addr_list, host_index, addr_string);
2619 #if defined(AF_INET6)
2621 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2622 MonoArray **h_aliases,
2623 MonoArray **h_addr_list,
2624 gboolean add_local_ips)
2627 struct addrinfo *ai = NULL;
2628 struct in_addr *local_in = NULL;
2630 struct in6_addr *local_in6 = NULL;
2634 MonoDomain *domain = mono_domain_get ();
2637 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2638 if (add_local_ips) {
2639 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2640 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2641 if (nlocal_in || nlocal_in6) {
2642 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2644 MonoString *addr_string;
2648 for (i = 0; i < nlocal_in; i++) {
2649 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2650 addr_string = mono_string_new (domain, addr);
2651 mono_array_setref (*h_addr_list, addr_index, addr_string);
2657 MonoString *addr_string;
2662 for (i = 0; i < nlocal_in6; i++) {
2663 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2665 addr_string = mono_string_new (domain, addr);
2666 mono_array_setref (*h_addr_list, addr_index, addr_string);
2675 freeaddrinfo (info);
2684 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2685 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2691 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2693 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2694 MonoString *addr_string;
2696 char buffer [48]; /* Max. size for IPv6 */
2698 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2702 if(ai->ai_family == PF_INET) {
2703 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2705 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2709 addr_string=mono_string_new(domain, buffer);
2711 addr_string=mono_string_new(domain, "");
2714 mono_array_setref (*h_addr_list, addr_index, addr_string);
2717 if (ai->ai_canonname != NULL) {
2718 *h_name=mono_string_new(domain, ai->ai_canonname);
2720 *h_name=mono_string_new(domain, buffer);
2736 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2738 gboolean add_local_ips = FALSE;
2739 #ifdef HAVE_SIOCGIFCONF
2740 gchar this_hostname [256];
2742 #if !defined(HAVE_GETHOSTBYNAME2_R)
2743 struct addrinfo *info = NULL, hints;
2746 MONO_ARCH_SAVE_REGS;
2748 hostname=mono_string_to_utf8 (host);
2749 if (*hostname == '\0')
2750 add_local_ips = TRUE;
2751 #ifdef HAVE_SIOCGIFCONF
2752 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2753 if (!strcmp (hostname, this_hostname))
2754 add_local_ips = TRUE;
2758 memset(&hints, 0, sizeof(hints));
2759 hints.ai_family = get_family_hint ();
2760 hints.ai_socktype = SOCK_STREAM;
2761 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
2763 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2769 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2771 struct hostent he1,*hp1, he2, *hp2;
2772 int buffer_size1, buffer_size2;
2773 char *buffer1, *buffer2;
2775 gboolean return_value;
2778 MONO_ARCH_SAVE_REGS;
2780 hostname=mono_string_to_utf8 (host);
2781 if (*hostname == '\0')
2782 add_local_ips = TRUE;
2784 #ifdef HAVE_SIOCGIFCONF
2785 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2786 if (!strcmp (hostname, this_hostname))
2787 add_local_ips = TRUE;
2793 buffer1 = g_malloc0(buffer_size1);
2794 buffer2 = g_malloc0(buffer_size2);
2798 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2799 &hp1, &herr) == ERANGE) {
2801 buffer1 = g_realloc(buffer1, buffer_size1);
2804 if (*hostname && hp1 == NULL)
2806 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2807 buffer_size2, &hp2, &herr) == ERANGE) {
2809 buffer2 = g_realloc(buffer2, buffer_size2);
2813 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2814 h_addr_list, add_local_ips);
2820 return(return_value);
2821 #endif /* HAVE_GETHOSTBYNAME2_R */
2823 #else /* AF_INET6 */
2824 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2828 gboolean add_local_ips = FALSE;
2829 #ifdef HAVE_SIOCGIFCONF
2830 gchar this_hostname [256];
2833 MONO_ARCH_SAVE_REGS;
2835 hostname=mono_string_to_utf8(host);
2836 if (*hostname == '\0')
2837 add_local_ips = TRUE;
2838 #ifdef HAVE_SIOCGIFCONF
2839 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2840 if (!strcmp (hostname, this_hostname))
2841 add_local_ips = TRUE;
2847 he = _wapi_gethostbyname (hostname);
2850 if (*hostname && he==NULL)
2853 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2855 #endif /* AF_INET6 */
2857 #ifndef HAVE_INET_PTON
2859 inet_pton (int family, const char *address, void *inaddrp)
2861 if (family == AF_INET) {
2862 #ifdef HAVE_INET_ATON
2863 struct in_addr inaddr;
2865 if (!inet_aton (address, &inaddr))
2868 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2871 /* assume the system has inet_addr(), if it doesn't
2872 have that we're pretty much screwed... */
2875 if (!strcmp (address, "255.255.255.255")) {
2876 /* special-case hack */
2877 inaddr = 0xffffffff;
2879 inaddr = inet_addr (address);
2881 #define INADDR_NONE ((in_addr_t) -1)
2883 if (inaddr == INADDR_NONE)
2887 memcpy (inaddrp, &inaddr, sizeof (guint32));
2889 #endif /* HAVE_INET_ATON */
2894 #endif /* !HAVE_INET_PTON */
2896 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2902 struct sockaddr_in saddr;
2903 struct sockaddr_in6 saddr6;
2904 struct addrinfo *info = NULL, hints;
2906 char hostname[1024] = {0};
2909 struct in_addr inaddr;
2914 v1 = mono_framework_version () == 1;
2916 address = mono_string_to_utf8 (addr);
2919 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2920 /* Maybe an ipv6 address */
2921 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2927 saddr6.sin6_family = AF_INET6;
2932 saddr.sin_family = AF_INET;
2937 flags = NI_NAMEREQD;
2940 if(family == AF_INET) {
2941 #if HAVE_SOCKADDR_IN_SIN_LEN
2942 saddr.sin_len = sizeof (saddr);
2944 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2945 hostname, sizeof(hostname), NULL, 0,
2949 } else if(family == AF_INET6) {
2950 #if HAVE_SOCKADDR_IN6_SIN_LEN
2951 saddr6.sin6_len = sizeof (saddr6);
2953 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2954 hostname, sizeof(hostname), NULL, 0,
2960 memset (&hints, 0, sizeof(hints));
2961 hints.ai_family = get_family_hint ();
2962 hints.ai_socktype = SOCK_STREAM;
2963 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
2965 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
2969 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
2971 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
2976 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
2980 ret = ipaddr_to_IPHostEntry (address, h_name,
2981 h_aliases, h_addr_list);
2984 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
2985 h_addr_list, FALSE);
2993 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
2995 gchar hostname[256];
2998 MONO_ARCH_SAVE_REGS;
3000 ret = gethostname (hostname, sizeof (hostname));
3005 *h_name=mono_string_new(mono_domain_get (), hostname);
3010 void mono_network_init(void)
3015 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3017 g_error("%s: Couldn't initialise networking", __func__);
3021 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3022 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3025 void mono_network_cleanup(void)