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...
88 static gint32 convert_family(MonoAddressFamily mono_family)
93 case AddressFamily_Unknown:
94 case AddressFamily_ImpLink:
95 case AddressFamily_Pup:
96 case AddressFamily_Chaos:
97 case AddressFamily_Iso:
98 case AddressFamily_Ecma:
99 case AddressFamily_DataKit:
100 case AddressFamily_Ccitt:
101 case AddressFamily_DataLink:
102 case AddressFamily_Lat:
103 case AddressFamily_HyperChannel:
104 case AddressFamily_NetBios:
105 case AddressFamily_VoiceView:
106 case AddressFamily_FireFox:
107 case AddressFamily_Banyan:
108 case AddressFamily_Atm:
109 case AddressFamily_Cluster:
110 case AddressFamily_Ieee12844:
111 case AddressFamily_NetworkDesigners:
112 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
115 case AddressFamily_Unspecified:
119 case AddressFamily_Unix:
123 case AddressFamily_InterNetwork:
127 case AddressFamily_Ipx:
133 case AddressFamily_Sna:
137 case AddressFamily_DecNet:
141 case AddressFamily_AppleTalk:
145 case AddressFamily_InterNetworkV6:
150 case AddressFamily_Irda:
156 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
162 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
164 MonoAddressFamily family=AddressFamily_Unknown;
168 family=AddressFamily_Unspecified;
172 family=AddressFamily_Unix;
176 family=AddressFamily_InterNetwork;
181 family=AddressFamily_Ipx;
186 family=AddressFamily_Sna;
190 family=AddressFamily_DecNet;
194 family=AddressFamily_AppleTalk;
199 family=AddressFamily_InterNetworkV6;
205 family=AddressFamily_Irda;
209 g_warning("unknown address family 0x%x", af_family);
215 static gint32 convert_type(MonoSocketType mono_type)
220 case SocketType_Stream:
224 case SocketType_Dgram:
236 case SocketType_Seqpacket:
240 case SocketType_Unknown:
241 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
245 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
251 static gint32 convert_proto(MonoProtocolType mono_proto)
256 case ProtocolType_IP:
257 case ProtocolType_IPv6:
258 case ProtocolType_Icmp:
259 case ProtocolType_Igmp:
260 case ProtocolType_Ggp:
261 case ProtocolType_Tcp:
262 case ProtocolType_Pup:
263 case ProtocolType_Udp:
264 case ProtocolType_Idp:
265 /* These protocols are known (on my system at least) */
269 case ProtocolType_ND:
270 case ProtocolType_Raw:
271 case ProtocolType_Ipx:
272 case ProtocolType_Spx:
273 case ProtocolType_SpxII:
274 case ProtocolType_Unknown:
275 /* These protocols arent */
276 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
286 /* Convert MonoSocketFlags */
287 static gint32 convert_socketflags (gint32 sflags)
292 /* SocketFlags.None */
295 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
296 SocketFlags_DontRoute | SocketFlags_Partial))
297 /* Contains invalid flag values */
300 if (sflags & SocketFlags_OutOfBand)
302 if (sflags & SocketFlags_Peek)
304 if (sflags & SocketFlags_DontRoute)
305 flags |= MSG_DONTROUTE;
307 /* Ignore Partial - see bug 349688. Don't return -1, because
308 * according to the comment in that bug ms runtime doesn't for
309 * UDP sockets (this means we will silently ignore it for TCP
312 if (sflags & SocketFlags_Partial)
319 if (sflags & SocketFlags_MaxIOVectorLength)
320 /* FIXME: Don't know what to do for MaxIOVectorLength query */
323 return (flags ? flags : -1);
328 * 0 on success (mapped mono_level and mono_name to system_level and system_name
330 * -2 on non-fatal error (ie, must ignore)
332 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
333 MonoSocketOptionName mono_name,
337 switch (mono_level) {
338 case SocketOptionLevel_Socket:
339 *system_level = SOL_SOCKET;
342 case SocketOptionName_DontLinger:
343 /* This is SO_LINGER, because the setsockopt
344 * internal call maps DontLinger to SO_LINGER
347 *system_name = SO_LINGER;
349 case SocketOptionName_Debug:
350 *system_name = SO_DEBUG;
353 case SocketOptionName_AcceptConnection:
354 *system_name = SO_ACCEPTCONN;
357 case SocketOptionName_ReuseAddress:
358 *system_name = SO_REUSEADDR;
360 case SocketOptionName_KeepAlive:
361 *system_name = SO_KEEPALIVE;
363 case SocketOptionName_DontRoute:
364 *system_name = SO_DONTROUTE;
366 case SocketOptionName_Broadcast:
367 *system_name = SO_BROADCAST;
369 case SocketOptionName_Linger:
370 *system_name = SO_LINGER;
372 case SocketOptionName_OutOfBandInline:
373 *system_name = SO_OOBINLINE;
375 case SocketOptionName_SendBuffer:
376 *system_name = SO_SNDBUF;
378 case SocketOptionName_ReceiveBuffer:
379 *system_name = SO_RCVBUF;
381 case SocketOptionName_SendLowWater:
382 *system_name = SO_SNDLOWAT;
384 case SocketOptionName_ReceiveLowWater:
385 *system_name = SO_RCVLOWAT;
387 case SocketOptionName_SendTimeout:
388 *system_name = SO_SNDTIMEO;
390 case SocketOptionName_ReceiveTimeout:
391 *system_name = SO_RCVTIMEO;
393 case SocketOptionName_Error:
394 *system_name = SO_ERROR;
396 case SocketOptionName_Type:
397 *system_name = SO_TYPE;
400 case SocketOptionName_PeerCred:
401 *system_name = SO_PEERCRED;
404 case SocketOptionName_ExclusiveAddressUse:
405 case SocketOptionName_UseLoopback:
406 case SocketOptionName_MaxConnections:
407 /* Can't figure out how to map these, so fall
411 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
416 case SocketOptionLevel_IP:
418 *system_level = SOL_IP;
421 static int cached = 0;
425 struct protoent *pent;
427 pent = getprotobyname ("IP");
428 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
432 *system_level = proto;
434 #endif /* HAVE_SOL_IP */
437 case SocketOptionName_IPOptions:
438 *system_name = IP_OPTIONS;
441 case SocketOptionName_HeaderIncluded:
442 *system_name = IP_HDRINCL;
446 case SocketOptionName_TypeOfService:
447 *system_name = IP_TOS;
451 case SocketOptionName_IpTimeToLive:
452 *system_name = IP_TTL;
455 case SocketOptionName_MulticastInterface:
456 *system_name = IP_MULTICAST_IF;
458 case SocketOptionName_MulticastTimeToLive:
459 *system_name = IP_MULTICAST_TTL;
461 case SocketOptionName_MulticastLoopback:
462 *system_name = IP_MULTICAST_LOOP;
464 case SocketOptionName_AddMembership:
465 *system_name = IP_ADD_MEMBERSHIP;
467 case SocketOptionName_DropMembership:
468 *system_name = IP_DROP_MEMBERSHIP;
470 #ifdef HAVE_IP_PKTINFO
471 case SocketOptionName_PacketInformation:
472 *system_name = IP_PKTINFO;
474 #endif /* HAVE_IP_PKTINFO */
476 case SocketOptionName_DontFragment:
477 #ifdef HAVE_IP_DONTFRAGMENT
478 *system_name = IP_DONTFRAGMENT;
480 #elif defined HAVE_IP_MTU_DISCOVER
481 /* Not quite the same */
482 *system_name = IP_MTU_DISCOVER;
485 /* If the flag is not available on this system, we can ignore this error */
487 #endif /* HAVE_IP_DONTFRAGMENT */
488 case SocketOptionName_AddSourceMembership:
489 case SocketOptionName_DropSourceMembership:
490 case SocketOptionName_BlockSource:
491 case SocketOptionName_UnblockSource:
492 /* Can't figure out how to map these, so fall
496 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
502 case SocketOptionLevel_IPv6:
504 *system_level = SOL_IPV6;
507 static int cached = 0;
511 struct protoent *pent;
513 pent = getprotobyname ("IPV6");
514 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
518 *system_level = proto;
520 #endif /* HAVE_SOL_IPV6 */
523 case SocketOptionName_IpTimeToLive:
524 *system_name = IPV6_UNICAST_HOPS;
526 case SocketOptionName_MulticastInterface:
527 *system_name = IPV6_MULTICAST_IF;
529 case SocketOptionName_MulticastTimeToLive:
530 *system_name = IPV6_MULTICAST_HOPS;
532 case SocketOptionName_MulticastLoopback:
533 *system_name = IPV6_MULTICAST_LOOP;
535 case SocketOptionName_AddMembership:
536 *system_name = IPV6_JOIN_GROUP;
538 case SocketOptionName_DropMembership:
539 *system_name = IPV6_LEAVE_GROUP;
541 case SocketOptionName_PacketInformation:
542 #ifdef HAVE_IPV6_PKTINFO
543 *system_name = IPV6_PKTINFO;
546 case SocketOptionName_HeaderIncluded:
547 case SocketOptionName_IPOptions:
548 case SocketOptionName_TypeOfService:
549 case SocketOptionName_DontFragment:
550 case SocketOptionName_AddSourceMembership:
551 case SocketOptionName_DropSourceMembership:
552 case SocketOptionName_BlockSource:
553 case SocketOptionName_UnblockSource:
554 /* Can't figure out how to map these, so fall
558 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
562 break; /* SocketOptionLevel_IPv6 */
565 case SocketOptionLevel_Tcp:
567 *system_level = SOL_TCP;
570 static int cached = 0;
574 struct protoent *pent;
576 pent = getprotobyname ("TCP");
577 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
581 *system_level = proto;
583 #endif /* HAVE_SOL_TCP */
586 case SocketOptionName_NoDelay:
587 *system_name = TCP_NODELAY;
590 /* The documentation is talking complete
591 * bollocks here: rfc-1222 is titled
592 * 'Advancing the NSFNET Routing Architecture'
593 * and doesn't mention either of the words
594 * "expedite" or "urgent".
596 case SocketOptionName_BsdUrgent:
597 case SocketOptionName_Expedited:
600 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
605 case SocketOptionLevel_Udp:
606 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
609 case SocketOptionName_NoChecksum:
610 case SocketOptionName_ChecksumCoverage:
612 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
619 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
626 static MonoImage *get_socket_assembly (void)
628 static const char *version = NULL;
629 static gboolean moonlight;
630 static MonoImage *socket_assembly = NULL;
632 if (version == NULL) {
633 version = mono_get_runtime_info ()->framework_version;
634 moonlight = !strcmp (version, "2.1");
637 if (socket_assembly == NULL) {
639 socket_assembly = mono_image_loaded ("System.Net");
640 if (!socket_assembly) {
641 MonoAssembly *sa = mono_assembly_open ("System.Net.dll", NULL);
644 g_assert_not_reached ();
646 socket_assembly = mono_assembly_get_image (sa);
650 socket_assembly = mono_image_loaded ("System");
651 if (!socket_assembly) {
652 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
655 g_assert_not_reached ();
657 socket_assembly = mono_assembly_get_image (sa);
663 return(socket_assembly);
667 static gint32 get_family_hint(void)
669 MonoDomain *domain = mono_domain_get ();
671 if (!domain->inet_family_hint) {
672 MonoClass *socket_class;
673 MonoClassField *ipv6_field, *ipv4_field;
674 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
677 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
678 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
679 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
680 vtable = mono_class_vtable (mono_domain_get (), socket_class);
681 mono_runtime_class_init (vtable);
683 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
684 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
686 mono_domain_lock (domain);
687 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
688 domain->inet_family_hint = 1;
689 } else if (ipv4_enabled == 1) {
690 domain->inet_family_hint = 2;
692 domain->inet_family_hint = 3;
694 mono_domain_unlock (domain);
696 switch (domain->inet_family_hint) {
697 case 1: return PF_UNSPEC;
698 case 2: return PF_INET;
699 case 3: return PF_INET6;
706 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
717 sock_family=convert_family(family);
718 if(sock_family==-1) {
719 *error = WSAEAFNOSUPPORT;
723 sock_proto=convert_proto(proto);
725 *error = WSAEPROTONOSUPPORT;
729 sock_type=convert_type(type);
731 *error = WSAESOCKTNOSUPPORT;
735 sock = _wapi_socket (sock_family, sock_type, sock_proto,
736 NULL, 0, WSA_FLAG_OVERLAPPED);
738 if(sock==INVALID_SOCKET) {
739 *error = WSAGetLastError ();
743 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
744 return (GUINT_TO_POINTER (sock));
748 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
749 return (GUINT_TO_POINTER (sock));
753 return(GUINT_TO_POINTER (sock));
756 /* FIXME: the SOCKET parameter (here and in other functions in this
757 * file) is really an IntPtr which needs to be converted to a guint32.
759 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
765 g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
770 /* Clear any pending work item from this socket if the underlying
771 * polling system does not notify when the socket is closed */
772 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
776 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
781 g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
784 return(WSAGetLastError());
787 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
797 #if defined(PLATFORM_MACOSX)
799 socklen_t optlen = sizeof (amount);
800 ret=getsockopt (sock, SOL_SOCKET, SO_NREAD, &amount, &optlen);
803 ret=ioctlsocket(sock, FIONREAD, &amount);
805 if(ret==SOCKET_ERROR) {
806 *error = WSAGetLastError ();
813 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
824 * block == TRUE/FALSE means we will block/not block.
825 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
829 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
830 if(ret==SOCKET_ERROR) {
831 *error = WSAGetLastError ();
835 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
845 #ifdef PLATFORM_WIN32
846 /* Several applications are getting stuck during shutdown on Windows
847 * when an accept call is on a background thread.
851 MonoThread* curthread = mono_thread_current ();
856 int optlen = sizeof (gint);
860 FD_SET(sock, &readfds);
862 timeout.tv_usec = 1000;
863 selectret = select (0, &readfds, NULL, NULL, &timeout);
866 if (curthread->state & ThreadState_StopRequested)
868 /* A negative return from select means that an error has occurred.
869 * Let _wapi_accept handle that.*/
872 /* The socket's state may have changed. If it is no longer listening, stop.*/
873 if (!getsockopt (sock, SOL_SOCKET, SO_ACCEPTCONN, (char*)&selectret, &optlen)) {
882 newsock = _wapi_accept (sock, NULL, 0);
883 if(newsock==INVALID_SOCKET) {
884 *error = WSAGetLastError ();
888 return(GUINT_TO_POINTER (newsock));
891 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
901 ret = _wapi_listen (sock, backlog);
902 if(ret==SOCKET_ERROR) {
903 *error = WSAGetLastError ();
907 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
908 int sa_size, gint32 *error)
910 MonoDomain *domain = mono_domain_get ();
911 MonoObject *sockaddr_obj;
912 MonoClass *sockaddr_class;
913 MonoClassField *field;
915 MonoAddressFamily family;
917 /* Build a System.Net.SocketAddress object instance */
918 sockaddr_class=mono_class_from_name(get_socket_assembly (), "System.Net", "SocketAddress");
919 sockaddr_obj=mono_object_new(domain, sockaddr_class);
921 /* Locate the SocketAddress data buffer in the object */
922 field=mono_class_get_field_from_name(sockaddr_class, "data");
924 /* Make sure there is space for the family and size bytes */
926 if (saddr->sa_family == AF_UNIX) {
927 /* sa_len includes the entire sockaddr size, so we don't need the
928 * N bytes (sizeof (unsigned short)) of the family. */
929 data=mono_array_new(domain, mono_get_byte_class (), sa_size);
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 data=mono_array_new(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, 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 for(i=0; i<16; i++) {
988 mono_array_set(data, guint8, 8+i,
989 sa_in->sin6_addr.s6_addr[i]);
992 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
993 mono_array_set(data, guint8, 25,
994 (sa_in->sin6_scope_id >> 8) & 0xff);
995 mono_array_set(data, guint8, 26,
996 (sa_in->sin6_scope_id >> 16) & 0xff);
997 mono_array_set(data, guint8, 27,
998 (sa_in->sin6_scope_id >> 24) & 0xff);
1000 mono_field_set_value (sockaddr_obj, field, data);
1002 return(sockaddr_obj);
1004 #ifdef HAVE_SYS_UN_H
1005 } else if (saddr->sa_family == AF_UNIX) {
1008 for (i = 0; i < sa_size; i++) {
1009 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
1012 mono_field_set_value (sockaddr_obj, field, data);
1014 return sockaddr_obj;
1017 *error = WSAEAFNOSUPPORT;
1022 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
1024 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1028 MONO_ARCH_SAVE_REGS;
1033 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1035 if(ret==SOCKET_ERROR) {
1036 *error = WSAGetLastError ();
1041 g_message(G_GNUC_PRETTY_FUNCTION ": bound to %s port %d", inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr), ntohs(((struct sockaddr_in *)&sa)->sin_port));
1044 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1048 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
1050 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1054 MONO_ARCH_SAVE_REGS;
1059 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1061 if(ret==SOCKET_ERROR) {
1062 *error = WSAGetLastError ();
1067 g_message(G_GNUC_PRETTY_FUNCTION ": connected to %s port %d", inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr), ntohs(((struct sockaddr_in *)&sa)->sin_port));
1070 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1074 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1078 MonoClassField *field;
1083 /* Dig the SocketAddress data buffer out of the object */
1084 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1085 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1087 /* The data buffer is laid out as follows:
1088 * byte 0 is the address family low byte
1089 * byte 1 is the address family high byte
1091 * bytes 2 and 3 are the port info
1092 * the rest is the address info
1094 * the rest is the file name
1096 len = mono_array_length (data);
1098 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1101 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1102 if (family == AF_INET) {
1103 struct sockaddr_in *sa;
1108 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1111 sa = g_new0 (struct sockaddr_in, 1);
1112 port = (mono_array_get (data, guint8, 2) << 8) +
1113 mono_array_get (data, guint8, 3);
1114 address = (mono_array_get (data, guint8, 4) << 24) +
1115 (mono_array_get (data, guint8, 5) << 16 ) +
1116 (mono_array_get (data, guint8, 6) << 8) +
1117 mono_array_get (data, guint8, 7);
1119 sa->sin_family = family;
1120 sa->sin_addr.s_addr = htonl (address);
1121 sa->sin_port = htons (port);
1123 *sa_size = sizeof(struct sockaddr_in);
1124 return((struct sockaddr *)sa);
1127 } else if (family == AF_INET6) {
1128 struct sockaddr_in6 *sa;
1134 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1137 sa = g_new0 (struct sockaddr_in6, 1);
1138 port = mono_array_get (data, guint8, 3) +
1139 (mono_array_get (data, guint8, 2) << 8);
1140 scopeid = mono_array_get (data, guint8, 24) +
1141 (mono_array_get (data, guint8, 25) << 8) +
1142 (mono_array_get (data, guint8, 26) << 16) +
1143 (mono_array_get (data, guint8, 27) << 24);
1145 sa->sin6_family = family;
1146 sa->sin6_port = htons (port);
1147 sa->sin6_scope_id = scopeid;
1149 for(i=0; i<16; i++) {
1150 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1153 *sa_size = sizeof(struct sockaddr_in6);
1154 return((struct sockaddr *)sa);
1156 #ifdef HAVE_SYS_UN_H
1157 } else if (family == AF_UNIX) {
1158 struct sockaddr_un *sock_un;
1161 /* Need a byte for the '\0' terminator/prefix, and the first
1162 * two bytes hold the SocketAddress family
1164 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1165 mono_raise_exception (mono_get_exception_index_out_of_range ());
1168 sock_un = g_new0 (struct sockaddr_un, 1);
1170 sock_un->sun_family = family;
1171 for (i = 0; i < len - 2; i++) {
1172 sock_un->sun_path [i] = mono_array_get (data, guint8,
1178 return (struct sockaddr *)sock_un;
1181 *error = WSAEAFNOSUPPORT;
1186 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1188 struct sockaddr *sa;
1192 MONO_ARCH_SAVE_REGS;
1196 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1202 g_message(G_GNUC_PRETTY_FUNCTION ": binding to %s port %d", inet_ntoa(((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port));
1205 ret = _wapi_bind (sock, sa, sa_size);
1206 if(ret==SOCKET_ERROR) {
1207 *error = WSAGetLastError ();
1220 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1221 gint timeout, gint32 *error)
1223 MonoThread *thread = NULL;
1229 MONO_ARCH_SAVE_REGS;
1231 pfds = g_new0 (mono_pollfd, 1);
1232 pfds[0].fd = GPOINTER_TO_INT (sock);
1233 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1234 (mode == SelectModeWrite) ? MONO_POLLOUT :
1235 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1237 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1238 start = time (NULL);
1242 ret = mono_poll (pfds, 1, timeout);
1243 if (timeout > 0 && ret < 0) {
1245 int sec = time (NULL) - start;
1247 timeout -= sec * 1000;
1255 if (ret == -1 && errno == EINTR) {
1258 if (thread == NULL) {
1259 thread = mono_thread_current ();
1262 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1268 /* Suspend requested? */
1269 mono_thread_interruption_checkpoint ();
1273 } while (ret == -1 && errno == EINTR);
1276 #ifdef PLATFORM_WIN32
1277 *error = WSAGetLastError ();
1279 *error = errno_to_WSA (errno, __func__);
1294 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1296 struct sockaddr *sa;
1300 MONO_ARCH_SAVE_REGS;
1304 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1310 g_message(G_GNUC_PRETTY_FUNCTION ": connecting to %s port %d", inet_ntoa(((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port));
1313 ret = _wapi_connect (sock, sa, sa_size);
1314 if(ret==SOCKET_ERROR) {
1315 *error = WSAGetLastError ();
1321 /* These #defines from mswsock.h from wine. Defining them here allows
1322 * us to build this file on a mingw box that doesn't know the magic
1323 * numbers, but still run on a newer windows box that does.
1325 #ifndef WSAID_DISCONNECTEX
1326 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1327 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1330 #ifndef WSAID_TRANSMITFILE
1331 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1332 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1335 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1338 glong output_bytes = 0;
1339 GUID disco_guid = WSAID_DISCONNECTEX;
1340 GUID trans_guid = WSAID_TRANSMITFILE;
1341 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1342 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1345 MONO_ARCH_SAVE_REGS;
1350 g_message("%s: disconnecting from socket %p (reuse %d)", __func__,
1354 /* I _think_ the extension function pointers need to be looked
1355 * up for each socket. FIXME: check the best way to store
1356 * pointers to functions in managed objects that still works
1357 * on 64bit platforms.
1359 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1360 (void *)&disco_guid, sizeof(GUID),
1361 (void *)&_wapi_disconnectex, sizeof(void *),
1362 &output_bytes, NULL, NULL);
1364 /* make sure that WSAIoctl didn't put crap in the
1367 _wapi_disconnectex = NULL;
1369 /* Look up the TransmitFile extension function pointer
1370 * instead of calling TransmitFile() directly, because
1371 * apparently "Several of the extension functions have
1372 * been available since WinSock 1.1 and are exported
1373 * from MSWsock.dll, however it's not advisable to
1374 * link directly to this dll as this ties you to the
1375 * Microsoft WinSock provider. A provider neutral way
1376 * of accessing these extension functions is to load
1377 * them dynamically via WSAIoctl using the
1378 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1379 * should, theoretically, allow you to access these
1380 * functions from any provider that supports them..."
1381 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1383 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1384 (void *)&trans_guid, sizeof(GUID),
1385 (void *)&_wapi_transmitfile, sizeof(void *),
1386 &output_bytes, NULL, NULL);
1388 _wapi_transmitfile = NULL;
1392 if (_wapi_disconnectex != NULL) {
1393 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1394 } else if (_wapi_transmitfile != NULL) {
1395 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1396 TF_DISCONNECT | TF_REUSE_SOCKET);
1398 *error = ERROR_NOT_SUPPORTED;
1402 if (bret == FALSE) {
1403 *error = WSAGetLastError ();
1407 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1414 MONO_ARCH_SAVE_REGS;
1418 alen = mono_array_length (buffer);
1419 if (offset > alen - count) {
1423 buf=mono_array_addr(buffer, guchar, offset);
1425 recvflags = convert_socketflags (flags);
1426 if (recvflags == -1) {
1427 *error = WSAEOPNOTSUPP;
1431 ret = _wapi_recv (sock, buf, count, recvflags);
1432 if(ret==SOCKET_ERROR) {
1433 *error = WSAGetLastError ();
1440 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1445 DWORD recvflags = 0;
1447 MONO_ARCH_SAVE_REGS;
1451 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1452 count = mono_array_length (buffers);
1454 recvflags = convert_socketflags (flags);
1455 if (recvflags == -1) {
1456 *error = WSAEOPNOTSUPP;
1460 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1461 if (ret == SOCKET_ERROR) {
1462 *error = WSAGetLastError ();
1469 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1475 struct sockaddr *sa;
1478 MONO_ARCH_SAVE_REGS;
1482 alen = mono_array_length (buffer);
1483 if (offset > alen - count) {
1487 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1492 buf=mono_array_addr(buffer, guchar, offset);
1494 recvflags = convert_socketflags (flags);
1495 if (recvflags == -1) {
1496 *error = WSAEOPNOTSUPP;
1500 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1501 if(ret==SOCKET_ERROR) {
1503 *error = WSAGetLastError ();
1507 /* If we didn't get a socket size, then we're probably a
1508 * connected connection-oriented socket and the stack hasn't
1509 * returned the remote address. All we can do is return null.
1512 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1521 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1528 MONO_ARCH_SAVE_REGS;
1532 alen = mono_array_length (buffer);
1533 if (offset > alen - count) {
1538 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1541 buf=mono_array_addr(buffer, guchar, offset);
1544 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1547 sendflags = convert_socketflags (flags);
1548 if (sendflags == -1) {
1549 *error = WSAEOPNOTSUPP;
1553 ret = _wapi_send (sock, buf, count, sendflags);
1554 if(ret==SOCKET_ERROR) {
1555 *error = WSAGetLastError ();
1562 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1567 DWORD sendflags = 0;
1569 MONO_ARCH_SAVE_REGS;
1573 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1574 count = mono_array_length (buffers);
1576 sendflags = convert_socketflags (flags);
1577 if (sendflags == -1) {
1578 *error = WSAEOPNOTSUPP;
1582 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1583 if (ret == SOCKET_ERROR) {
1584 *error = WSAGetLastError ();
1591 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1597 struct sockaddr *sa;
1600 MONO_ARCH_SAVE_REGS;
1604 alen = mono_array_length (buffer);
1605 if (offset > alen - count) {
1609 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1615 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1618 buf=mono_array_addr(buffer, guchar, offset);
1621 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1624 sendflags = convert_socketflags (flags);
1625 if (sendflags == -1) {
1626 *error = WSAEOPNOTSUPP;
1630 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1631 if(ret==SOCKET_ERROR) {
1632 *error = WSAGetLastError ();
1640 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1643 MonoClassField *field;
1645 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1646 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1651 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1652 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1654 MonoThread *thread = NULL;
1661 MonoClass *sock_arr_class;
1664 mono_array_size_t socks_size;
1666 MONO_ARCH_SAVE_REGS;
1668 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1669 count = mono_array_length (*sockets);
1670 nfds = count - 3; /* NULL separators */
1671 pfds = g_new0 (mono_pollfd, nfds);
1673 for (i = 0; i < count; i++) {
1674 obj = mono_array_get (*sockets, MonoObject *, i);
1681 /* The socket array was bogus */
1687 pfds [idx].fd = Socket_to_SOCKET (obj);
1688 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1692 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1693 start = time (NULL);
1696 ret = mono_poll (pfds, nfds, timeout);
1697 if (timeout > 0 && ret < 0) {
1699 int sec = time (NULL) - start;
1701 timeout -= sec * 1000;
1707 if (ret == -1 && errno == EINTR) {
1710 thread = mono_thread_current ();
1712 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1719 /* Suspend requested? */
1720 mono_thread_interruption_checkpoint ();
1724 } while (ret == -1 && errno == EINTR);
1727 #ifdef PLATFORM_WIN32
1728 *error = WSAGetLastError ();
1730 *error = errno_to_WSA (errno, __func__);
1742 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1743 socks_size = ((mono_array_size_t)ret) + 3; /* space for the NULL delimiters */
1744 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1747 for (i = 0; i < count && ret > 0; i++) {
1750 obj = mono_array_get (*sockets, MonoObject *, i);
1757 pfd = &pfds [i - mode];
1758 if (pfd->revents == 0)
1762 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1763 mono_array_setref (socks, idx++, obj);
1764 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1765 mono_array_setref (socks, idx++, obj);
1766 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1767 mono_array_setref (socks, idx++, obj);
1775 static MonoObject* int_to_object (MonoDomain *domain, int val)
1777 return mono_value_box (domain, mono_get_int32_class (), &val);
1781 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1787 socklen_t valsize=sizeof(val);
1788 struct linger linger;
1789 socklen_t lingersize=sizeof(linger);
1791 socklen_t time_ms_size = sizeof (time_ms);
1794 socklen_t credsize = sizeof(cred);
1796 MonoDomain *domain=mono_domain_get();
1798 MonoClass *obj_class;
1799 MonoClassField *field;
1801 MONO_ARCH_SAVE_REGS;
1805 ret=convert_sockopt_level_and_name(level, name, &system_level,
1808 *error = WSAENOPROTOOPT;
1812 *obj_val = int_to_object (domain, 0);
1816 /* No need to deal with MulticastOption names here, because
1817 * you cant getsockopt AddMembership or DropMembership (the
1818 * int getsockopt will error, causing an exception)
1821 case SocketOptionName_Linger:
1822 case SocketOptionName_DontLinger:
1823 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1827 case SocketOptionName_SendTimeout:
1828 case SocketOptionName_ReceiveTimeout:
1829 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1833 case SocketOptionName_PeerCred:
1834 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1840 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1844 if(ret==SOCKET_ERROR) {
1845 *error = WSAGetLastError ();
1850 case SocketOptionName_Linger:
1851 /* build a System.Net.Sockets.LingerOption */
1852 obj_class=mono_class_from_name(get_socket_assembly (),
1853 "System.Net.Sockets",
1855 obj=mono_object_new(domain, obj_class);
1857 /* Locate and set the fields "bool enabled" and "int
1860 field=mono_class_get_field_from_name(obj_class, "enabled");
1861 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1863 field=mono_class_get_field_from_name(obj_class, "seconds");
1864 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1868 case SocketOptionName_DontLinger:
1869 /* construct a bool int in val - true if linger is off */
1870 obj = int_to_object (domain, !linger.l_onoff);
1873 case SocketOptionName_SendTimeout:
1874 case SocketOptionName_ReceiveTimeout:
1875 obj = int_to_object (domain, time_ms);
1879 case SocketOptionName_PeerCred:
1881 /* build a Mono.Posix.PeerCred+PeerCredData if
1884 static MonoImage *mono_posix_image = NULL;
1885 MonoPeerCredData *cred_data;
1887 if (mono_posix_image == NULL) {
1888 mono_posix_image=mono_image_loaded ("Mono.Posix");
1889 if (!mono_posix_image) {
1890 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1892 *error = WSAENOPROTOOPT;
1895 mono_posix_image = mono_assembly_get_image (sa);
1900 obj_class = mono_class_from_name(mono_posix_image,
1903 obj = mono_object_new(domain, obj_class);
1904 cred_data = (MonoPeerCredData *)obj;
1905 cred_data->pid = cred.pid;
1906 cred_data->uid = cred.uid;
1907 cred_data->gid = cred.gid;
1913 obj = int_to_object (domain, val);
1919 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1927 MONO_ARCH_SAVE_REGS;
1931 ret=convert_sockopt_level_and_name(level, name, &system_level,
1934 *error = WSAENOPROTOOPT;
1940 valsize=mono_array_length(*byte_val);
1941 buf=mono_array_addr(*byte_val, guchar, 0);
1943 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1944 if(ret==SOCKET_ERROR) {
1945 *error = WSAGetLastError ();
1949 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1950 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1952 struct in_addr inaddr;
1953 MonoClassField *field;
1955 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
1957 /* No idea why .net uses a 64bit type to hold a 32bit value...
1959 * Internal value of IPAddess is in little-endian order
1961 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
1968 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1970 struct in6_addr in6addr;
1971 MonoClassField *field;
1975 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
1976 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1978 /* Solaris has only the 8 bit version. */
1980 for(i=0; i<8; i++) {
1981 guint16 s = mono_array_get (data, guint16, i);
1982 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1983 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1987 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1991 #endif /* AF_INET6 */
1993 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)
2004 #ifdef HAVE_SOL_IPV6
2005 sol_ipv6 = SOL_IPV6;
2008 struct protoent *pent;
2009 pent = getprotobyname ("ipv6");
2010 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2018 struct protoent *pent;
2019 pent = getprotobyname ("ip");
2020 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2023 #endif /* AF_INET6 */
2025 MONO_ARCH_SAVE_REGS;
2027 ret=convert_sockopt_level_and_name(level, name, &system_level,
2030 *error = WSAENOPROTOOPT;
2037 /* Only one of obj_val, byte_val or int_val has data */
2039 MonoClassField *field;
2040 struct linger linger;
2044 case SocketOptionName_DontLinger:
2047 valsize=sizeof(linger);
2048 ret = _wapi_setsockopt (sock, system_level,
2049 system_name, &linger, valsize);
2052 case SocketOptionName_Linger:
2053 /* Dig out "bool enabled" and "int seconds"
2056 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2057 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2058 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2059 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2061 valsize=sizeof(linger);
2062 ret = _wapi_setsockopt (sock, system_level,
2063 system_name, &linger, valsize);
2065 case SocketOptionName_AddMembership:
2066 case SocketOptionName_DropMembership:
2067 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2069 MonoObject *address = NULL;
2072 if(system_level == sol_ipv6) {
2073 struct ipv6_mreq mreq6;
2078 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2079 address = *(gpointer *)(((char *)obj_val) + field->offset);
2082 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2085 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2086 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2088 ret = _wapi_setsockopt (sock, system_level,
2089 system_name, &mreq6,
2091 } else if(system_level == sol_ip)
2092 #endif /* AF_INET6 */
2094 #ifdef HAVE_STRUCT_IP_MREQN
2095 struct ip_mreqn mreq = {{0}};
2097 struct ip_mreq mreq = {{0}};
2098 #endif /* HAVE_STRUCT_IP_MREQN */
2100 /* pain! MulticastOption holds two IPAddress
2101 * members, so I have to dig the value out of
2104 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2105 address = *(gpointer *)(((char *)obj_val) + field->offset);
2107 /* address might not be defined and if so, set the address to ADDR_ANY.
2110 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2113 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2114 address = *(gpointer *)(((char *)obj_val) + field->offset);
2116 #ifdef HAVE_STRUCT_IP_MREQN
2118 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2122 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2124 #endif /* HAVE_STRUCT_IP_MREQN */
2126 ret = _wapi_setsockopt (sock, system_level,
2132 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2134 /* Cause an exception to be thrown */
2138 } else if (byte_val!=NULL) {
2139 int valsize=mono_array_length(byte_val);
2140 guchar *buf=mono_array_addr(byte_val, guchar, 0);
2142 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2143 if(ret==SOCKET_ERROR) {
2144 *error = WSAGetLastError ();
2148 /* ReceiveTimeout/SendTimeout get here */
2150 case SocketOptionName_DontFragment:
2151 #ifdef HAVE_IP_MTU_DISCOVER
2152 /* Fiddle with the value slightly if we're
2156 int_val = IP_PMTUDISC_DO;
2162 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2166 if(ret==SOCKET_ERROR) {
2167 *error = WSAGetLastError ();
2171 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2177 MONO_ARCH_SAVE_REGS;
2181 /* Currently, the values for how (recv=0, send=1, both=2) match
2184 ret = _wapi_shutdown (sock, how);
2185 if(ret==SOCKET_ERROR) {
2186 *error = WSAGetLastError ();
2191 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2193 MonoArray *output, gint32 *error)
2195 glong output_bytes = 0;
2196 gchar *i_buffer, *o_buffer;
2200 MONO_ARCH_SAVE_REGS;
2204 if (code == FIONBIO) {
2205 /* Invalid command. Must use Socket.Blocking */
2209 if (input == NULL) {
2213 i_buffer = mono_array_addr (input, gchar, 0);
2214 i_len = mono_array_length (input);
2217 if (output == NULL) {
2221 o_buffer = mono_array_addr (output, gchar, 0);
2222 o_len = mono_array_length (output);
2225 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2226 if (ret == SOCKET_ERROR) {
2227 *error = WSAGetLastError ();
2231 return (gint) output_bytes;
2234 #ifdef HAVE_SIOCGIFCONF
2236 is_loopback (int family, void *ad)
2238 char *ptr = (char *) ad;
2240 if (family == AF_INET) {
2241 return (ptr [0] == 127);
2245 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2252 get_local_ips (int family, int *nips)
2254 int addr_size, offset, fd, i, count;
2255 int max_ifaces = 50; /* 50 interfaces should be enough... */
2258 struct ifreq iflags;
2259 char *result, *tmp_ptr;
2260 gboolean ignore_loopback = FALSE;
2263 if (family == AF_INET) {
2264 addr_size = sizeof (struct in_addr);
2265 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2267 } else if (family == AF_INET6) {
2268 addr_size = sizeof (struct in6_addr);
2269 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2275 fd = socket (family, SOCK_STREAM, 0);
2277 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2278 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2279 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2281 g_free (ifc.ifc_buf);
2285 count = ifc.ifc_len / sizeof (struct ifreq);
2288 g_free (ifc.ifc_buf);
2293 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2294 strcpy (iflags.ifr_name, ifr->ifr_name);
2295 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2299 if ((iflags.ifr_flags & IFF_UP) == 0) {
2300 ifr->ifr_name [0] = '\0';
2304 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2305 ignore_loopback = TRUE;
2310 result = g_malloc (addr_size * count);
2312 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2313 if (ifr->ifr_name [0] == '\0') {
2318 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2323 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2324 tmp_ptr += addr_size;
2327 g_free (ifc.ifc_buf);
2332 get_local_ips (int family, int *nips)
2338 #endif /* HAVE_SIOCGIFCONF */
2341 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2342 MonoArray **h_aliases,
2343 MonoArray **h_addr_list,
2344 gboolean add_local_ips)
2346 MonoDomain *domain = mono_domain_get ();
2348 struct in_addr *local_in = NULL;
2352 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2356 *h_name=mono_string_new(domain, he->h_name);
2358 while(he->h_aliases[i]!=NULL) {
2362 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2364 while(he->h_aliases[i]!=NULL) {
2367 alias=mono_string_new(domain, he->h_aliases[i]);
2368 mono_array_setref (*h_aliases, i, alias);
2371 } else if (!add_local_ips) {
2375 if (add_local_ips) {
2376 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2378 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2379 for (i = 0; i < nlocal_in; i++) {
2380 MonoString *addr_string;
2381 char addr [16], *ptr;
2383 ptr = (char *) &local_in [i];
2384 g_snprintf(addr, 16, "%u.%u.%u.%u",
2385 (unsigned char) ptr [0],
2386 (unsigned char) ptr [1],
2387 (unsigned char) ptr [2],
2388 (unsigned char) ptr [3]);
2390 addr_string = mono_string_new (domain, addr);
2391 mono_array_setref (*h_addr_list, i, addr_string);
2396 } else if (he == NULL) {
2397 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2398 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2399 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2404 if (nlocal_in == 0 && he != NULL) {
2406 while (he->h_addr_list[i]!=NULL) {
2410 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2412 while(he->h_addr_list[i]!=NULL) {
2413 MonoString *addr_string;
2416 g_snprintf(addr, 16, "%u.%u.%u.%u",
2417 (unsigned char)he->h_addr_list[i][0],
2418 (unsigned char)he->h_addr_list[i][1],
2419 (unsigned char)he->h_addr_list[i][2],
2420 (unsigned char)he->h_addr_list[i][3]);
2422 addr_string=mono_string_new(domain, addr);
2423 mono_array_setref (*h_addr_list, i, addr_string);
2431 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2432 MonoArray **h_aliases,
2433 MonoArray **h_addr_list)
2435 MonoDomain *domain = mono_domain_get ();
2437 *h_name=mono_string_new(domain, addr);
2438 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2439 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2440 mono_array_setref (*h_addr_list, 0, *h_name);
2446 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2447 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2448 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2450 MonoDomain *domain = mono_domain_get ();
2451 int i, host_count, host_index, family_hint;
2452 struct in_addr *local_in = NULL;
2454 struct in6_addr *local_in6 = NULL;
2456 gboolean from_local = FALSE;
2458 family_hint = get_family_hint ();
2461 * Check if address length and family are correct
2463 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2467 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2472 * Get the aliases and host name from he1 or he2 whichever is
2473 * not null, if he1 is not null then take aliases from he1
2475 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2476 family_hint == PF_INET)) {
2477 *h_name=mono_string_new (domain, he1->h_name);
2480 while(he1->h_aliases[i]!=NULL) {
2484 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2487 while(he1->h_aliases[i]!=NULL) {
2490 alias=mono_string_new (domain, he1->h_aliases[i]);
2491 mono_array_setref (*h_aliases, i, alias);
2494 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2495 family_hint == PF_INET6)) {
2496 *h_name=mono_string_new (domain, he2->h_name);
2499 while(he2->h_aliases [i] != NULL) {
2503 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2506 while(he2->h_aliases[i]!=NULL) {
2509 alias=mono_string_new (domain, he2->h_aliases[i]);
2510 mono_array_setref (*h_aliases, i, alias);
2513 } else if (!add_local_ips) {
2518 * Count the number of addresses in he1 + he2
2521 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2522 family_hint == PF_INET)) {
2524 while(he1->h_addr_list[i]!=NULL) {
2530 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2531 family_hint == PF_INET6)) {
2533 while(he2->h_addr_list[i]!=NULL) {
2543 if (add_local_ips) {
2544 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2545 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2547 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2548 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2550 if (nlocal_in || nlocal_in6) {
2552 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2553 nlocal_in + nlocal_in6);
2557 for (n = 0; n < nlocal_in6; n++) {
2558 MonoString *addr_string;
2560 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2562 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2565 addr_string = mono_string_new (domain, addr);
2566 mono_array_setref (*h_addr_list, host_index, addr_string);
2574 for (n = 0; n < nlocal_in; n++) {
2575 MonoString *addr_string;
2577 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2579 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2582 addr_string = mono_string_new (domain, addr);
2583 mono_array_setref (*h_addr_list, host_index, addr_string);
2591 } else if (he1 == NULL && he2 == 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"));
2604 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2606 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2607 family_hint == PF_INET6)) {
2609 while(he2->h_addr_list[i] != NULL) {
2610 MonoString *addr_string;
2612 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2614 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2618 addr_string = mono_string_new (domain, addr);
2619 mono_array_setref (*h_addr_list, host_index, addr_string);
2626 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2627 family_hint == PF_INET)) {
2629 while(he1->h_addr_list[i] != NULL) {
2630 MonoString *addr_string;
2632 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2634 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2638 addr_string=mono_string_new (domain, addr);
2639 mono_array_setref (*h_addr_list, host_index, addr_string);
2650 #if defined(AF_INET6)
2652 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2653 MonoArray **h_aliases,
2654 MonoArray **h_addr_list,
2655 gboolean add_local_ips)
2658 struct addrinfo *ai = NULL;
2659 struct in_addr *local_in = NULL;
2661 struct in6_addr *local_in6 = NULL;
2665 MonoDomain *domain = mono_domain_get ();
2668 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2669 if (add_local_ips) {
2670 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2671 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2672 if (nlocal_in || nlocal_in6) {
2673 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2675 MonoString *addr_string;
2679 for (i = 0; i < nlocal_in; i++) {
2680 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2681 addr_string = mono_string_new (domain, addr);
2682 mono_array_setref (*h_addr_list, addr_index, addr_string);
2688 MonoString *addr_string;
2693 for (i = 0; i < nlocal_in6; i++) {
2694 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2696 addr_string = mono_string_new (domain, addr);
2697 mono_array_setref (*h_addr_list, addr_index, addr_string);
2706 freeaddrinfo (info);
2715 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2716 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2722 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2724 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2725 MonoString *addr_string;
2727 char buffer [48]; /* Max. size for IPv6 */
2729 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2733 if(ai->ai_family == PF_INET) {
2734 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2736 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2740 addr_string=mono_string_new(domain, buffer);
2742 addr_string=mono_string_new(domain, "");
2745 mono_array_setref (*h_addr_list, addr_index, addr_string);
2748 if (ai->ai_canonname != NULL) {
2749 *h_name=mono_string_new(domain, ai->ai_canonname);
2751 *h_name=mono_string_new(domain, buffer);
2767 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2769 gboolean add_local_ips = FALSE;
2770 #ifdef HAVE_SIOCGIFCONF
2771 gchar this_hostname [256];
2773 #if !defined(HAVE_GETHOSTBYNAME2_R)
2774 struct addrinfo *info = NULL, hints;
2777 MONO_ARCH_SAVE_REGS;
2779 hostname=mono_string_to_utf8 (host);
2780 if (*hostname == '\0')
2781 add_local_ips = TRUE;
2782 #ifdef HAVE_SIOCGIFCONF
2783 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2784 if (!strcmp (hostname, this_hostname))
2785 add_local_ips = TRUE;
2789 memset(&hints, 0, sizeof(hints));
2790 hints.ai_family = get_family_hint ();
2791 hints.ai_socktype = SOCK_STREAM;
2792 hints.ai_flags = AI_CANONNAME;
2794 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2800 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2802 struct hostent he1,*hp1, he2, *hp2;
2803 int buffer_size1, buffer_size2;
2804 char *buffer1, *buffer2;
2806 gboolean return_value;
2809 MONO_ARCH_SAVE_REGS;
2811 hostname=mono_string_to_utf8 (host);
2812 if (*hostname == '\0')
2813 add_local_ips = TRUE;
2815 #ifdef HAVE_SIOCGIFCONF
2816 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2817 if (!strcmp (hostname, this_hostname))
2818 add_local_ips = TRUE;
2824 buffer1 = g_malloc0(buffer_size1);
2825 buffer2 = g_malloc0(buffer_size2);
2829 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2830 &hp1, &herr) == ERANGE) {
2832 buffer1 = g_realloc(buffer1, buffer_size1);
2835 if (*hostname && hp1 == NULL)
2837 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2838 buffer_size2, &hp2, &herr) == ERANGE) {
2840 buffer2 = g_realloc(buffer2, buffer_size2);
2844 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2845 h_addr_list, add_local_ips);
2851 return(return_value);
2852 #endif /* HAVE_GETHOSTBYNAME2_R */
2854 #else /* AF_INET6 */
2855 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2859 gboolean add_local_ips = FALSE;
2860 #ifdef HAVE_SIOCGIFCONF
2861 gchar this_hostname [256];
2864 MONO_ARCH_SAVE_REGS;
2866 hostname=mono_string_to_utf8(host);
2867 if (*hostname == '\0')
2868 add_local_ips = TRUE;
2869 #ifdef HAVE_SIOCGIFCONF
2870 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2871 if (!strcmp (hostname, this_hostname))
2872 add_local_ips = TRUE;
2878 he = _wapi_gethostbyname (hostname);
2881 if (*hostname && he==NULL)
2884 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2886 #endif /* AF_INET6 */
2888 #ifndef HAVE_INET_PTON
2890 inet_pton (int family, const char *address, void *inaddrp)
2892 if (family == AF_INET) {
2893 #ifdef HAVE_INET_ATON
2894 struct in_addr inaddr;
2896 if (!inet_aton (address, &inaddr))
2899 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2902 /* assume the system has inet_addr(), if it doesn't
2903 have that we're pretty much screwed... */
2906 if (!strcmp (address, "255.255.255.255")) {
2907 /* special-case hack */
2908 inaddr = 0xffffffff;
2910 inaddr = inet_addr (address);
2912 #define INADDR_NONE ((in_addr_t) -1)
2914 if (inaddr == INADDR_NONE)
2918 memcpy (inaddrp, &inaddr, sizeof (guint32));
2920 #endif /* HAVE_INET_ATON */
2925 #endif /* !HAVE_INET_PTON */
2927 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2933 struct sockaddr_in saddr;
2934 struct sockaddr_in6 saddr6;
2935 struct addrinfo *info = NULL, hints;
2937 char hostname[1024] = {0};
2940 struct in_addr inaddr;
2945 v1 = mono_framework_version () == 1;
2947 address = mono_string_to_utf8 (addr);
2950 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2951 /* Maybe an ipv6 address */
2952 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2958 saddr6.sin6_family = AF_INET6;
2963 saddr.sin_family = AF_INET;
2968 flags = NI_NAMEREQD;
2971 if(family == AF_INET) {
2972 #if HAVE_SOCKADDR_IN_SIN_LEN
2973 saddr.sin_len = sizeof (saddr);
2975 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2976 hostname, sizeof(hostname), NULL, 0,
2980 } else if(family == AF_INET6) {
2981 #if HAVE_SOCKADDR_IN6_SIN_LEN
2982 saddr6.sin6_len = sizeof (saddr6);
2984 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2985 hostname, sizeof(hostname), NULL, 0,
2991 memset (&hints, 0, sizeof(hints));
2992 hints.ai_family = get_family_hint ();
2993 hints.ai_socktype = SOCK_STREAM;
2994 hints.ai_flags = AI_CANONNAME;
2996 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
3000 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
3002 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
3007 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
3011 ret = ipaddr_to_IPHostEntry (address, h_name,
3012 h_aliases, h_addr_list);
3015 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
3016 h_addr_list, FALSE);
3024 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
3026 gchar hostname[256];
3029 MONO_ARCH_SAVE_REGS;
3031 ret = gethostname (hostname, sizeof (hostname));
3036 *h_name=mono_string_new(mono_domain_get (), hostname);
3041 void mono_network_init(void)
3046 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3048 g_error(G_GNUC_PRETTY_FUNCTION ": Couldn't initialise networking");
3053 g_message(G_GNUC_PRETTY_FUNCTION ": Using socket library: %s", wsadata.szDescription);
3054 g_message(G_GNUC_PRETTY_FUNCTION ": Socket system status: %s", wsadata.szSystemStatus);
3058 void mono_network_cleanup(void)