2 * socket-io.c: Socket IO internal calls
5 * Dick Porter (dick@ximian.com)
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * (C) 2001 Ximian, Inc.
9 * Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
20 #include <mono/metadata/object.h>
21 #include <mono/io-layer/io-layer.h>
22 #include <mono/metadata/socket-io.h>
23 #include <mono/metadata/exception.h>
24 #include <mono/metadata/assembly.h>
25 #include <mono/metadata/appdomain.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/threads-types.h>
28 #include <mono/utils/mono-poll.h>
29 /* FIXME change this code to not mess so much with the internals */
30 #include <mono/metadata/class-internals.h>
31 #include <mono/metadata/threadpool-internals.h>
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_FILIO_H
45 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
47 #ifdef HAVE_SYS_SOCKIO_H
48 #include <sys/sockio.h> /* defines SIOCATMARK */
54 #include "mono/io-layer/socket-wrappers.h"
57 /* This is a kludge to make this file build under cygwin:
58 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
59 * prototypes for some but not all required functions (notably
60 * inet_ntop() is missing), but the libws2_32 library is missing the
61 * actual implementations of these functions.
68 static gint32 convert_family(MonoAddressFamily mono_family)
73 case AddressFamily_Unknown:
74 case AddressFamily_ImpLink:
75 case AddressFamily_Pup:
76 case AddressFamily_Chaos:
77 case AddressFamily_Iso:
78 case AddressFamily_Ecma:
79 case AddressFamily_DataKit:
80 case AddressFamily_Ccitt:
81 case AddressFamily_DataLink:
82 case AddressFamily_Lat:
83 case AddressFamily_HyperChannel:
84 case AddressFamily_NetBios:
85 case AddressFamily_VoiceView:
86 case AddressFamily_FireFox:
87 case AddressFamily_Banyan:
88 case AddressFamily_Atm:
89 case AddressFamily_Cluster:
90 case AddressFamily_Ieee12844:
91 case AddressFamily_NetworkDesigners:
92 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
95 case AddressFamily_Unspecified:
99 case AddressFamily_Unix:
103 case AddressFamily_InterNetwork:
107 case AddressFamily_Ipx:
113 case AddressFamily_Sna:
117 case AddressFamily_DecNet:
121 case AddressFamily_AppleTalk:
125 case AddressFamily_InterNetworkV6:
130 case AddressFamily_Irda:
136 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
142 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
144 MonoAddressFamily family=AddressFamily_Unknown;
148 family=AddressFamily_Unspecified;
152 family=AddressFamily_Unix;
156 family=AddressFamily_InterNetwork;
161 family=AddressFamily_Ipx;
166 family=AddressFamily_Sna;
170 family=AddressFamily_DecNet;
174 family=AddressFamily_AppleTalk;
179 family=AddressFamily_InterNetworkV6;
185 family=AddressFamily_Irda;
189 g_warning("unknown address family 0x%x", af_family);
195 static gint32 convert_type(MonoSocketType mono_type)
200 case SocketType_Stream:
204 case SocketType_Dgram:
216 case SocketType_Seqpacket:
220 case SocketType_Unknown:
221 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
225 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
231 static gint32 convert_proto(MonoProtocolType mono_proto)
236 case ProtocolType_IP:
237 case ProtocolType_IPv6:
238 case ProtocolType_Icmp:
239 case ProtocolType_Igmp:
240 case ProtocolType_Ggp:
241 case ProtocolType_Tcp:
242 case ProtocolType_Pup:
243 case ProtocolType_Udp:
244 case ProtocolType_Idp:
245 /* These protocols are known (on my system at least) */
249 case ProtocolType_ND:
250 case ProtocolType_Raw:
251 case ProtocolType_Ipx:
252 case ProtocolType_Spx:
253 case ProtocolType_SpxII:
254 case ProtocolType_Unknown:
255 /* These protocols arent */
256 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
266 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
267 MonoSocketOptionName mono_name,
271 switch (mono_level) {
272 case SocketOptionLevel_Socket:
273 *system_level = SOL_SOCKET;
276 case SocketOptionName_DontLinger:
277 /* This is SO_LINGER, because the setsockopt
278 * internal call maps DontLinger to SO_LINGER
281 *system_name = SO_LINGER;
283 case SocketOptionName_Debug:
284 *system_name = SO_DEBUG;
287 case SocketOptionName_AcceptConnection:
288 *system_name = SO_ACCEPTCONN;
291 case SocketOptionName_ReuseAddress:
292 *system_name = SO_REUSEADDR;
294 case SocketOptionName_KeepAlive:
295 *system_name = SO_KEEPALIVE;
297 case SocketOptionName_DontRoute:
298 *system_name = SO_DONTROUTE;
300 case SocketOptionName_Broadcast:
301 *system_name = SO_BROADCAST;
303 case SocketOptionName_Linger:
304 *system_name = SO_LINGER;
306 case SocketOptionName_OutOfBandInline:
307 *system_name = SO_OOBINLINE;
309 case SocketOptionName_SendBuffer:
310 *system_name = SO_SNDBUF;
312 case SocketOptionName_ReceiveBuffer:
313 *system_name = SO_RCVBUF;
315 case SocketOptionName_SendLowWater:
316 *system_name = SO_SNDLOWAT;
318 case SocketOptionName_ReceiveLowWater:
319 *system_name = SO_RCVLOWAT;
321 case SocketOptionName_SendTimeout:
322 *system_name = SO_SNDTIMEO;
324 case SocketOptionName_ReceiveTimeout:
325 *system_name = SO_RCVTIMEO;
327 case SocketOptionName_Error:
328 *system_name = SO_ERROR;
330 case SocketOptionName_Type:
331 *system_name = SO_TYPE;
334 case SocketOptionName_PeerCred:
335 *system_name = SO_PEERCRED;
338 case SocketOptionName_ExclusiveAddressUse:
339 case SocketOptionName_UseLoopback:
340 case SocketOptionName_MaxConnections:
341 /* Can't figure out how to map these, so fall
345 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
350 case SocketOptionLevel_IP:
352 *system_level = SOL_IP;
355 static int cached = 0;
359 struct protoent *pent;
361 pent = getprotobyname ("IP");
362 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
366 *system_level = proto;
368 #endif /* HAVE_SOL_IP */
371 case SocketOptionName_IPOptions:
372 *system_name = IP_OPTIONS;
375 case SocketOptionName_HeaderIncluded:
376 *system_name = IP_HDRINCL;
380 case SocketOptionName_TypeOfService:
381 *system_name = IP_TOS;
385 case SocketOptionName_IpTimeToLive:
386 *system_name = IP_TTL;
389 case SocketOptionName_MulticastInterface:
390 *system_name = IP_MULTICAST_IF;
392 case SocketOptionName_MulticastTimeToLive:
393 *system_name = IP_MULTICAST_TTL;
395 case SocketOptionName_MulticastLoopback:
396 *system_name = IP_MULTICAST_LOOP;
398 case SocketOptionName_AddMembership:
399 *system_name = IP_ADD_MEMBERSHIP;
401 case SocketOptionName_DropMembership:
402 *system_name = IP_DROP_MEMBERSHIP;
404 #ifdef HAVE_IP_PKTINFO
405 case SocketOptionName_PacketInformation:
406 *system_name = IP_PKTINFO;
408 #endif /* HAVE_IP_PKTINFO */
409 case SocketOptionName_DontFragment:
410 case SocketOptionName_AddSourceMembership:
411 case SocketOptionName_DropSourceMembership:
412 case SocketOptionName_BlockSource:
413 case SocketOptionName_UnblockSource:
414 /* Can't figure out how to map these, so fall
418 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
424 case SocketOptionLevel_IPv6:
426 *system_level = SOL_IPV6;
429 static int cached = 0;
433 struct protoent *pent;
435 pent = getprotobyname ("IPV6");
436 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
440 *system_level = proto;
442 #endif /* HAVE_SOL_IPV6 */
445 case SocketOptionName_IpTimeToLive:
446 *system_name = IPV6_UNICAST_HOPS;
448 case SocketOptionName_MulticastInterface:
449 *system_name = IPV6_MULTICAST_IF;
451 case SocketOptionName_MulticastTimeToLive:
452 *system_name = IPV6_MULTICAST_HOPS;
454 case SocketOptionName_MulticastLoopback:
455 *system_name = IPV6_MULTICAST_LOOP;
457 case SocketOptionName_AddMembership:
458 *system_name = IPV6_JOIN_GROUP;
460 case SocketOptionName_DropMembership:
461 *system_name = IPV6_LEAVE_GROUP;
463 case SocketOptionName_PacketInformation:
464 *system_name = IPV6_PKTINFO;
466 case SocketOptionName_HeaderIncluded:
467 case SocketOptionName_IPOptions:
468 case SocketOptionName_TypeOfService:
469 case SocketOptionName_DontFragment:
470 case SocketOptionName_AddSourceMembership:
471 case SocketOptionName_DropSourceMembership:
472 case SocketOptionName_BlockSource:
473 case SocketOptionName_UnblockSource:
474 /* Can't figure out how to map these, so fall
478 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
482 break; /* SocketOptionLevel_IPv6 */
485 case SocketOptionLevel_Tcp:
487 *system_level = SOL_TCP;
490 static int cached = 0;
494 struct protoent *pent;
496 pent = getprotobyname ("TCP");
497 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
501 *system_level = proto;
503 #endif /* HAVE_SOL_TCP */
506 case SocketOptionName_NoDelay:
507 *system_name = TCP_NODELAY;
510 /* The documentation is talking complete
511 * bollocks here: rfc-1222 is titled
512 * 'Advancing the NSFNET Routing Architecture'
513 * and doesn't mention either of the words
514 * "expedite" or "urgent".
516 case SocketOptionName_BsdUrgent:
517 case SocketOptionName_Expedited:
520 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
525 case SocketOptionLevel_Udp:
526 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
529 case SocketOptionName_NoChecksum:
530 case SocketOptionName_ChecksumCoverage:
532 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
539 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
546 #define STASH_SYS_ASS(this) \
547 if(system_assembly == NULL) { \
548 system_assembly=mono_image_loaded ("System"); \
549 if (!system_assembly) { \
550 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL); \
551 if (!sa) g_assert_not_reached (); \
552 else {system_assembly = mono_assembly_get_image (sa);} \
556 static MonoImage *system_assembly=NULL;
560 static gint32 get_family_hint(void)
562 MonoClass *socket_class;
563 MonoClassField *ipv6_field, *ipv4_field;
564 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
567 socket_class = mono_class_from_name (system_assembly,
568 "System.Net.Sockets", "Socket");
569 ipv4_field = mono_class_get_field_from_name (socket_class,
571 ipv6_field = mono_class_get_field_from_name (socket_class,
573 vtable = mono_class_vtable (mono_domain_get (), socket_class);
575 mono_field_static_get_value(vtable, ipv4_field, &ipv4_enabled);
576 mono_field_static_get_value(vtable, ipv6_field, &ipv6_enabled);
578 if(ipv4_enabled == 1 && ipv6_enabled == 1) {
580 } else if(ipv4_enabled == 1) {
588 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
601 sock_family=convert_family(family);
602 if(sock_family==-1) {
603 *error = WSAEAFNOSUPPORT;
607 sock_proto=convert_proto(proto);
609 *error = WSAEPROTONOSUPPORT;
613 sock_type=convert_type(type);
615 *error = WSAESOCKTNOSUPPORT;
619 sock = _wapi_socket (sock_family, sock_type, sock_proto,
620 NULL, 0, WSA_FLAG_OVERLAPPED);
622 if(sock==INVALID_SOCKET) {
623 *error = WSAGetLastError ();
627 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
628 return (GUINT_TO_POINTER (sock));
632 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
633 return (GUINT_TO_POINTER (sock));
637 #ifndef PLATFORM_WIN32
638 /* .net seems to set this by default for SOCK_STREAM,
639 * not for SOCK_DGRAM (see bug #36322)
641 * It seems winsock has a rather different idea of what
642 * SO_REUSEADDR means. If it's set, then a new socket can be
643 * bound over an existing listening socket. There's a new
644 * windows-specific option called SO_EXCLUSIVEADDRUSE but
645 * using that means the socket MUST be closed properly, or a
646 * denial of service can occur. Luckily for us, winsock
647 * behaves as though any other system would when SO_REUSEADDR
648 * is true, so we don't need to do anything else here. See
654 ret = _wapi_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof (true));
655 if(ret==SOCKET_ERROR) {
656 *error = WSAGetLastError ();
664 return(GUINT_TO_POINTER (sock));
667 /* FIXME: the SOCKET parameter (here and in other functions in this
668 * file) is really an IntPtr which needs to be converted to a guint32.
670 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
676 g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
681 /* Clear any pending work item from this socket if the underlying
682 * polling system does not notify when the socket is closed */
683 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
687 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
692 g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
695 return(WSAGetLastError());
698 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
708 ret=ioctlsocket(sock, FIONREAD, &amount);
709 if(ret==SOCKET_ERROR) {
710 *error = WSAGetLastError ();
717 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
728 * block == TRUE/FALSE means we will block/not block.
729 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
733 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
734 if(ret==SOCKET_ERROR) {
735 *error = WSAGetLastError ();
739 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
748 newsock = _wapi_accept (sock, NULL, 0);
749 if(newsock==INVALID_SOCKET) {
750 *error = WSAGetLastError ();
754 return(GUINT_TO_POINTER (newsock));
757 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
767 ret = _wapi_listen (sock, backlog);
768 if(ret==SOCKET_ERROR) {
769 *error = WSAGetLastError ();
773 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
774 int sa_size, gint32 *error)
776 MonoDomain *domain = mono_domain_get ();
777 MonoObject *sockaddr_obj;
778 MonoClass *sockaddr_class;
779 MonoClassField *field;
781 MonoAddressFamily family;
783 /* Build a System.Net.SocketAddress object instance */
784 sockaddr_class=mono_class_from_name(system_assembly, "System.Net", "SocketAddress");
785 sockaddr_obj=mono_object_new(domain, sockaddr_class);
787 /* Locate the SocketAddress data buffer in the object */
788 field=mono_class_get_field_from_name(sockaddr_class, "data");
790 /* Make sure there is space for the family and size bytes */
791 data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
793 /* The data buffer is laid out as follows:
794 * byte 0 is the address family
795 * byte 1 is the buffer length
796 * bytes 2 and 3 are the port info
797 * the rest is the address info
800 family=convert_to_mono_family(saddr->sa_family);
801 if(family==AddressFamily_Unknown) {
802 *error = WSAEAFNOSUPPORT;
806 mono_array_set(data, guint8, 0, family & 0x0FF);
807 mono_array_set(data, guint8, 1, ((family << 8) & 0x0FFFF));
809 if(saddr->sa_family==AF_INET) {
810 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
811 guint16 port=ntohs(sa_in->sin_port);
812 guint32 address=ntohl(sa_in->sin_addr.s_addr);
815 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
818 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
819 mono_array_set(data, guint8, 3, (port) & 0xff);
820 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
821 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
822 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
823 mono_array_set(data, guint8, 7, (address) & 0xff);
825 mono_field_set_value (sockaddr_obj, field, data);
827 return(sockaddr_obj);
829 } else if (saddr->sa_family == AF_INET6) {
830 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
833 guint16 port=ntohs(sa_in->sin6_port);
836 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
839 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
840 mono_array_set(data, guint8, 3, (port) & 0xff);
842 for(i=0; i<16; i++) {
843 mono_array_set(data, guint8, 8+i,
844 sa_in->sin6_addr.s6_addr[i]);
847 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
848 mono_array_set(data, guint8, 25,
849 (sa_in->sin6_scope_id >> 8) & 0xff);
850 mono_array_set(data, guint8, 26,
851 (sa_in->sin6_scope_id >> 16) & 0xff);
852 mono_array_set(data, guint8, 27,
853 (sa_in->sin6_scope_id >> 24) & 0xff);
855 mono_field_set_value (sockaddr_obj, field, data);
857 return(sockaddr_obj);
860 } else if (saddr->sa_family == AF_UNIX) {
863 for (i = 0; i < sa_size; i++) {
864 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
867 mono_field_set_value (sockaddr_obj, field, data);
872 *error = WSAEAFNOSUPPORT;
877 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
879 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
888 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
890 if(ret==SOCKET_ERROR) {
891 *error = WSAGetLastError ();
896 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));
899 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
903 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
905 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
914 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
916 if(ret==SOCKET_ERROR) {
917 *error = WSAGetLastError ();
922 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));
925 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
929 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
933 MonoClassField *field;
938 /* Dig the SocketAddress data buffer out of the object */
939 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
940 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
942 /* The data buffer is laid out as follows:
943 * byte 0 is the address family low byte
944 * byte 1 is the address family high byte
946 * bytes 2 and 3 are the port info
947 * the rest is the address info
949 * the rest is the file name
951 len = mono_array_length (data);
953 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
956 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
957 if(family==AF_INET) {
958 struct sockaddr_in *sa=g_new0(struct sockaddr_in, 1);
959 guint16 port=(mono_array_get(data, guint8, 2) << 8) +
960 mono_array_get(data, guint8, 3);
961 guint32 address=(mono_array_get(data, guint8, 4) << 24) +
962 (mono_array_get(data, guint8, 5) << 16 ) +
963 (mono_array_get(data, guint8, 6) << 8) +
964 mono_array_get(data, guint8, 7);
966 sa->sin_family=family;
967 sa->sin_addr.s_addr=htonl(address);
968 sa->sin_port=htons(port);
970 *sa_size=sizeof(struct sockaddr_in);
971 return((struct sockaddr *)sa);
974 } else if (family == AF_INET6) {
975 struct sockaddr_in6 *sa=g_new0(struct sockaddr_in6, 1);
978 guint16 port = mono_array_get(data, guint8, 3) + (mono_array_get(data, guint8, 2) << 8);
979 guint32 scopeid = mono_array_get(data, guint8, 24) +
980 (mono_array_get(data, guint8, 25)<<8) +
981 (mono_array_get(data, guint8, 26)<<16) +
982 (mono_array_get(data, guint8, 27)<<24);
984 sa->sin6_family=family;
985 sa->sin6_port=htons(port);
986 sa->sin6_scope_id = scopeid;
989 sa->sin6_addr.s6_addr[i] = mono_array_get(data, guint8, 8+i);
991 *sa_size=sizeof(struct sockaddr_in6);
992 return((struct sockaddr *)sa);
995 } else if (family == AF_UNIX) {
996 struct sockaddr_un *sock_un = g_new0 (struct sockaddr_un, 1);
999 if (len - 2 > MONO_SIZEOF_SUNPATH)
1000 mono_raise_exception (mono_get_exception_index_out_of_range ());
1002 sock_un->sun_family = family;
1003 for (i = 0; i < len - 2; i++)
1004 sock_un->sun_path [i] = mono_array_get (data, guint8,
1006 sock_un->sun_path [len - 2] = '\0';
1007 *sa_size = sizeof (struct sockaddr_un);
1009 return (struct sockaddr *)sock_un;
1012 *error = WSAEAFNOSUPPORT;
1017 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1019 struct sockaddr *sa;
1023 MONO_ARCH_SAVE_REGS;
1027 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1033 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));
1036 ret = _wapi_bind (sock, sa, sa_size);
1037 if(ret==SOCKET_ERROR) {
1038 *error = WSAGetLastError ();
1051 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1052 gint timeout, gint32 *error)
1057 struct timeval *tvptr;
1061 MONO_ARCH_SAVE_REGS;
1063 start = time (NULL);
1067 _wapi_FD_SET (sock, &fds);
1069 divvy = div (timeout, 1000000);
1070 tv.tv_sec = divvy.quot;
1071 tv.tv_usec = divvy.rem;
1077 if (mode == SelectModeRead) {
1078 ret = _wapi_select (0, &fds, NULL, NULL, tvptr);
1079 } else if (mode == SelectModeWrite) {
1080 ret = _wapi_select (0, NULL, &fds, NULL, tvptr);
1081 } else if (mode == SelectModeError) {
1082 ret = _wapi_select (0, NULL, NULL, &fds, tvptr);
1084 g_assert_not_reached ();
1087 if (timeout > 0 && ret < 0) {
1089 int sec = time (NULL) - start;
1091 timeout -= sec * 1000;
1097 } while ((ret == SOCKET_ERROR) && (*error == WSAGetLastError ()) == WSAEINTR);
1099 return (ret != SOCKET_ERROR && _wapi_FD_ISSET (sock, &fds));
1102 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1104 struct sockaddr *sa;
1108 MONO_ARCH_SAVE_REGS;
1112 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1118 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));
1121 ret = _wapi_connect (sock, sa, sa_size);
1122 if(ret==SOCKET_ERROR) {
1123 *error = WSAGetLastError ();
1129 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1136 MONO_ARCH_SAVE_REGS;
1140 alen=mono_array_length(buffer);
1141 if(offset+count>alen) {
1145 buf=mono_array_addr(buffer, guchar, offset);
1147 ret = _wapi_recv (sock, buf, count, recvflags);
1148 if(ret==SOCKET_ERROR) {
1149 *error = WSAGetLastError ();
1156 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1162 struct sockaddr *sa;
1165 MONO_ARCH_SAVE_REGS;
1169 alen=mono_array_length(buffer);
1170 if(offset+count>alen) {
1174 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1179 buf=mono_array_addr(buffer, guchar, offset);
1181 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1182 if(ret==SOCKET_ERROR) {
1184 *error = WSAGetLastError ();
1188 /* If we didn't get a socket size, then we're probably a
1189 * connected connection-oriented socket and the stack hasn't
1190 * returned the remote address. All we can do is return null.
1193 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1202 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1209 MONO_ARCH_SAVE_REGS;
1213 alen=mono_array_length(buffer);
1214 if(offset+count>alen) {
1219 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1222 buf=mono_array_addr(buffer, guchar, offset);
1225 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1228 ret = _wapi_send (sock, buf, count, sendflags);
1229 if(ret==SOCKET_ERROR) {
1230 *error = WSAGetLastError ();
1237 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1243 struct sockaddr *sa;
1246 MONO_ARCH_SAVE_REGS;
1250 alen=mono_array_length(buffer);
1251 if(offset+count>alen) {
1255 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1261 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1264 buf=mono_array_addr(buffer, guchar, offset);
1267 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1270 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1271 if(ret==SOCKET_ERROR) {
1272 *error = WSAGetLastError ();
1280 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1283 MonoClassField *field;
1285 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1286 sock=*(SOCKET *)(((char *)sockobj)+field->offset);
1291 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1292 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1294 MonoThread *thread = NULL;
1301 MonoClass *sock_arr_class;
1305 MONO_ARCH_SAVE_REGS;
1307 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1308 count = mono_array_length (*sockets);
1309 nfds = count - 3; /* NULL separators */
1310 pfds = g_new0 (mono_pollfd, nfds);
1312 for (i = 0; i < count; i++) {
1313 obj = mono_array_get (*sockets, MonoObject *, i);
1319 pfds [idx].fd = GPOINTER_TO_INT (Socket_to_SOCKET (obj));
1320 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1324 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1325 start = time (NULL);
1328 ret = mono_poll (pfds, nfds, timeout);
1329 if (timeout > 0 && ret < 0) {
1331 int sec = time (NULL) - start;
1333 timeout -= sec * 1000;
1339 if (ret == -1 && errno == EINTR) {
1342 thread = mono_thread_current ();
1344 mono_monitor_enter (thread->synch_lock);
1345 leave = ((thread->state & ThreadState_AbortRequested) != 0 ||
1346 (thread->state & ThreadState_StopRequested) != 0);
1347 mono_monitor_exit (thread->synch_lock);
1353 /* Suspend requested? */
1354 mono_thread_interruption_checkpoint ();
1358 } while (ret == -1 && errno == EINTR);
1361 #ifdef PLATFORM_WIN32
1362 *error = WSAGetLastError ();
1364 *error = errno_to_WSA (errno, __func__);
1376 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1377 ret += 3; /* space for the NULL delimiters */
1378 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &ret, NULL);
1381 for (i = 0; i < count && ret > 0; i++) {
1384 obj = mono_array_get (*sockets, MonoObject *, i);
1391 pfd = &pfds [i - mode];
1392 if (pfd->revents == 0)
1396 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1397 mono_array_set (socks, MonoObject *, idx++, obj);
1398 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1399 mono_array_set (socks, MonoObject *, idx++, obj);
1400 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1401 mono_array_set (socks, MonoObject *, idx++, obj);
1409 static MonoObject* int_to_object (MonoDomain *domain, int val)
1411 return mono_value_box (domain, mono_get_int32_class (), &val);
1415 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1421 int valsize=sizeof(val);
1422 struct linger linger;
1423 int lingersize=sizeof(linger);
1425 int time_ms_size = sizeof (time_ms);
1428 int credsize = sizeof(cred);
1430 MonoDomain *domain=mono_domain_get();
1432 MonoClass *obj_class;
1433 MonoClassField *field;
1435 MONO_ARCH_SAVE_REGS;
1439 ret=convert_sockopt_level_and_name(level, name, &system_level,
1442 *error = WSAENOPROTOOPT;
1446 /* No need to deal with MulticastOption names here, because
1447 * you cant getsockopt AddMembership or DropMembership (the
1448 * int getsockopt will error, causing an exception)
1451 case SocketOptionName_Linger:
1452 case SocketOptionName_DontLinger:
1453 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1457 case SocketOptionName_SendTimeout:
1458 case SocketOptionName_ReceiveTimeout:
1459 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1463 case SocketOptionName_PeerCred:
1464 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1470 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1474 if(ret==SOCKET_ERROR) {
1475 *error = WSAGetLastError ();
1480 case SocketOptionName_Linger:
1481 /* build a System.Net.Sockets.LingerOption */
1482 obj_class=mono_class_from_name(system_assembly,
1483 "System.Net.Sockets",
1485 obj=mono_object_new(domain, obj_class);
1487 /* Locate and set the fields "bool enabled" and "int
1490 field=mono_class_get_field_from_name(obj_class, "enabled");
1491 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1493 field=mono_class_get_field_from_name(obj_class, "seconds");
1494 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1498 case SocketOptionName_DontLinger:
1499 /* construct a bool int in val - true if linger is off */
1500 obj = int_to_object (domain, !linger.l_onoff);
1503 case SocketOptionName_SendTimeout:
1504 case SocketOptionName_ReceiveTimeout:
1505 obj = int_to_object (domain, time_ms);
1509 case SocketOptionName_PeerCred:
1511 /* build a Mono.Posix.PeerCred+PeerCredData if
1514 static MonoImage *mono_posix_image = NULL;
1515 MonoPeerCredData *cred_data;
1517 if (mono_posix_image == NULL) {
1518 mono_posix_image=mono_image_loaded ("Mono.Posix");
1519 if (!mono_posix_image) {
1520 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1522 *error = WSAENOPROTOOPT;
1525 mono_posix_image = mono_assembly_get_image (sa);
1530 obj_class = mono_class_from_name(mono_posix_image,
1532 "PeerCred/PeerCredData");
1533 obj = mono_object_new(domain, obj_class);
1534 cred_data = (MonoPeerCredData *)obj;
1535 cred_data->pid = cred.pid;
1536 cred_data->uid = cred.uid;
1537 cred_data->gid = cred.gid;
1543 obj = int_to_object (domain, val);
1549 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1557 MONO_ARCH_SAVE_REGS;
1561 ret=convert_sockopt_level_and_name(level, name, &system_level,
1564 *error = WSAENOPROTOOPT;
1568 valsize=mono_array_length(*byte_val);
1569 buf=mono_array_addr(*byte_val, guchar, 0);
1571 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1572 if(ret==SOCKET_ERROR) {
1573 *error = WSAGetLastError ();
1577 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1578 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1580 struct in_addr inaddr;
1581 MonoClassField *field;
1583 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "address");
1585 /* No idea why .net uses a 64bit type to hold a 32bit value...
1587 * Internal value of IPAddess is in Network Order, there is no need
1588 * to call htonl here.
1590 inaddr.s_addr=(guint32)*(guint64 *)(((char *)ipaddr)+field->offset);
1597 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1599 struct in6_addr in6addr;
1600 MonoClassField *field;
1604 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "_numbers");
1605 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1607 /* Solaris has only the 8 bit version. */
1609 for(i=0; i<8; i++) {
1610 guint16 s = mono_array_get (data, guint16, i);
1611 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1612 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1616 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1620 #endif /* AF_INET6 */
1622 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)
1633 #ifdef HAVE_SOL_IPV6
1634 sol_ipv6 = SOL_IPV6;
1637 struct protoent *pent;
1638 pent = getprotobyname ("ipv6");
1639 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
1647 struct protoent *pent;
1648 pent = getprotobyname ("ip");
1649 sol_ip = (pent != NULL) ? pent->p_proto : 0;
1652 #endif /* AF_INET6 */
1654 MONO_ARCH_SAVE_REGS;
1656 ret=convert_sockopt_level_and_name(level, name, &system_level,
1659 *error = WSAENOPROTOOPT;
1663 /* Only one of obj_val, byte_val or int_val has data */
1665 MonoClassField *field;
1666 struct linger linger;
1670 case SocketOptionName_DontLinger:
1673 valsize=sizeof(linger);
1674 ret = _wapi_setsockopt (sock, system_level,
1675 system_name, &linger, valsize);
1678 case SocketOptionName_Linger:
1679 /* Dig out "bool enabled" and "int seconds"
1682 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
1683 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
1684 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
1685 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
1687 valsize=sizeof(linger);
1688 ret = _wapi_setsockopt (sock, system_level,
1689 system_name, &linger, valsize);
1691 case SocketOptionName_AddMembership:
1692 case SocketOptionName_DropMembership:
1693 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1695 MonoObject *address = NULL;
1698 if(system_level == sol_ipv6) {
1699 struct ipv6_mreq mreq6;
1704 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1705 address = *(gpointer *)(((char *)obj_val) + field->offset);
1708 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
1711 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
1712 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
1714 ret = _wapi_setsockopt (sock, system_level,
1715 system_name, &mreq6,
1717 } else if(system_level == sol_ip)
1718 #endif /* AF_INET6 */
1720 #ifdef HAVE_STRUCT_IP_MREQN
1721 struct ip_mreqn mreq = {{0}};
1723 struct ip_mreq mreq = {{0}};
1724 #endif /* HAVE_STRUCT_IP_MREQN */
1726 /* pain! MulticastOption holds two IPAddress
1727 * members, so I have to dig the value out of
1730 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1731 address = *(gpointer *)(((char *)obj_val) + field->offset);
1733 /* address might not be defined and if so, set the address to ADDR_ANY.
1736 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
1739 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
1740 address = *(gpointer *)(((char *)obj_val) + field->offset);
1742 #ifdef HAVE_STRUCT_IP_MREQN
1744 mreq.imr_address = ipaddress_to_struct_in_addr (address);
1748 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
1750 #endif /* HAVE_STRUCT_IP_MREQN */
1752 ret = _wapi_setsockopt (sock, system_level,
1758 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
1760 /* Cause an exception to be thrown */
1764 } else if (byte_val!=NULL) {
1765 int valsize=mono_array_length(byte_val);
1766 guchar *buf=mono_array_addr(byte_val, guchar, 0);
1768 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
1769 if(ret==SOCKET_ERROR) {
1770 *error = WSAGetLastError ();
1774 /* ReceiveTimeout/SendTimeout get here */
1775 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
1778 if(ret==SOCKET_ERROR) {
1779 *error = WSAGetLastError ();
1783 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
1789 MONO_ARCH_SAVE_REGS;
1793 /* Currently, the values for how (recv=0, send=1, both=2) match
1796 ret = _wapi_shutdown (sock, how);
1797 if(ret==SOCKET_ERROR) {
1798 *error = WSAGetLastError ();
1803 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
1805 MonoArray *output, gint32 *error)
1807 gulong output_bytes = 0;
1808 gchar *i_buffer, *o_buffer;
1812 MONO_ARCH_SAVE_REGS;
1816 if (code == FIONBIO) {
1817 /* Invalid command. Must use Socket.Blocking */
1821 if (input == NULL) {
1825 i_buffer = mono_array_addr (input, gchar, 0);
1826 i_len = mono_array_length (input);
1829 if (output == NULL) {
1833 o_buffer = mono_array_addr (output, gchar, 0);
1834 o_len = mono_array_length (output);
1837 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
1838 if (ret == SOCKET_ERROR) {
1839 *error = WSAGetLastError ();
1843 return (gint) output_bytes;
1846 #ifdef HAVE_SIOCGIFCONF
1848 is_loopback (int family, void *ad)
1850 char *ptr = (char *) ad;
1852 if (family == AF_INET) {
1853 return (ptr [0] == 127);
1857 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
1864 get_local_ips (int family, int *nips)
1866 int addr_size, offset, fd, i, count;
1867 int max_ifaces = 50; /* 50 interfaces should be enough... */
1870 struct ifreq iflags;
1871 char *result, *tmp_ptr;
1872 gboolean ignore_loopback = FALSE;
1875 if (family == AF_INET) {
1876 addr_size = sizeof (struct in_addr);
1877 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
1879 } else if (family == AF_INET6) {
1880 addr_size = sizeof (struct in6_addr);
1881 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
1887 fd = socket (family, SOCK_STREAM, 0);
1889 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
1890 ifc.ifc_buf = g_malloc (ifc.ifc_len);
1891 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
1893 g_free (ifc.ifc_buf);
1897 count = ifc.ifc_len / sizeof (struct ifreq);
1900 g_free (ifc.ifc_buf);
1905 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
1906 strcpy (iflags.ifr_name, ifr->ifr_name);
1907 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
1911 if ((iflags.ifr_flags & IFF_UP) == 0) {
1912 ifr->ifr_name [0] = '\0';
1916 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
1917 ignore_loopback = TRUE;
1922 result = g_malloc (addr_size * count);
1924 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
1925 if (ifr->ifr_name [0] == '\0') {
1930 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
1935 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
1936 tmp_ptr += addr_size;
1939 g_free (ifc.ifc_buf);
1944 get_local_ips (int family, int *nips)
1950 #endif /* HAVE_SIOCGIFCONF */
1953 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
1954 MonoArray **h_aliases,
1955 MonoArray **h_addr_list,
1956 gboolean add_local_ips)
1958 MonoDomain *domain = mono_domain_get ();
1960 struct in_addr *local_in = NULL;
1963 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
1967 *h_name=mono_string_new(domain, he->h_name);
1970 while(he->h_aliases[i]!=NULL) {
1974 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
1976 while(he->h_aliases[i]!=NULL) {
1979 alias=mono_string_new(domain, he->h_aliases[i]);
1980 mono_array_set(*h_aliases, MonoString *, i, alias);
1984 if (add_local_ips) {
1985 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
1987 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
1988 for (i = 0; i < nlocal_in; i++) {
1989 MonoString *addr_string;
1990 char addr [16], *ptr;
1992 ptr = (char *) &local_in [i];
1993 g_snprintf(addr, 16, "%u.%u.%u.%u",
1994 (unsigned char) ptr [0],
1995 (unsigned char) ptr [1],
1996 (unsigned char) ptr [2],
1997 (unsigned char) ptr [3]);
1999 addr_string = mono_string_new (domain, addr);
2000 mono_array_set (*h_addr_list, MonoString *, i, addr_string);
2008 if (nlocal_in == 0) {
2010 while (he->h_addr_list[i]!=NULL) {
2014 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2016 while(he->h_addr_list[i]!=NULL) {
2017 MonoString *addr_string;
2020 g_snprintf(addr, 16, "%u.%u.%u.%u",
2021 (unsigned char)he->h_addr_list[i][0],
2022 (unsigned char)he->h_addr_list[i][1],
2023 (unsigned char)he->h_addr_list[i][2],
2024 (unsigned char)he->h_addr_list[i][3]);
2026 addr_string=mono_string_new(domain, addr);
2027 mono_array_set(*h_addr_list, MonoString *, i, addr_string);
2036 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2037 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2038 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2040 MonoDomain *domain = mono_domain_get ();
2041 int i, host_count, host_index, family_hint;
2042 struct in_addr *local_in = NULL;
2044 struct in6_addr *local_in6 = NULL;
2046 gboolean from_local = FALSE;
2048 family_hint = get_family_hint ();
2050 if(he1 == NULL && he2 == NULL) {
2055 * Check if address length and family are correct
2057 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2061 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2066 * Get the aliases and host name from he1 or he2 whichever is
2067 * not null, if he1 is not null then take aliases from he1
2069 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2070 family_hint == PF_INET)) {
2071 *h_name=mono_string_new (domain, he1->h_name);
2074 while(he1->h_aliases[i]!=NULL) {
2078 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2081 while(he1->h_aliases[i]!=NULL) {
2084 alias=mono_string_new (domain, he1->h_aliases[i]);
2085 mono_array_set (*h_aliases, MonoString *, i, alias);
2088 } else if (family_hint == PF_UNSPEC || family_hint == PF_INET6) {
2089 *h_name=mono_string_new (domain, he2->h_name);
2092 while(he2->h_aliases [i] != NULL) {
2096 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2099 while(he2->h_aliases[i]!=NULL) {
2102 alias=mono_string_new (domain, he2->h_aliases[i]);
2103 mono_array_set (*h_aliases, MonoString *, i, alias);
2109 * Count the number of addresses in he1 + he2
2112 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2113 family_hint == PF_INET)) {
2115 while(he1->h_addr_list[i]!=NULL) {
2121 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2122 family_hint == PF_INET6)) {
2124 while(he2->h_addr_list[i]!=NULL) {
2134 if (add_local_ips) {
2135 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2136 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2138 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2139 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2141 if (nlocal_in || nlocal_in6) {
2143 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2144 nlocal_in + nlocal_in6);
2148 for (n = 0; n < nlocal_in6; n++) {
2149 MonoString *addr_string;
2150 char addr[46]; /* INET6_ADDRSTRLEN == 46 */
2152 inet_ntop (AF_INET6, &local_in6 [n], addr,
2155 addr_string = mono_string_new (domain, addr);
2156 mono_array_set (*h_addr_list, MonoString *, host_index,
2164 for (n = 0; n < nlocal_in; n++) {
2165 MonoString *addr_string;
2166 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2168 inet_ntop (AF_INET, &local_in [n], addr,
2171 addr_string = mono_string_new (domain, addr);
2172 mono_array_set (*h_addr_list, MonoString *, host_index,
2186 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2188 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2189 family_hint == PF_INET6)) {
2191 while(he2->h_addr_list[i] != NULL) {
2192 MonoString *addr_string;
2193 char addr[46]; /* INET6_ADDRSTRLEN == 46 */
2195 inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2198 addr_string = mono_string_new (domain, addr);
2199 mono_array_set (*h_addr_list, MonoString *, host_index,
2206 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2207 family_hint == PF_INET)) {
2209 while(he1->h_addr_list[i] != NULL) {
2210 MonoString *addr_string;
2211 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2213 inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2216 addr_string=mono_string_new (domain, addr);
2217 mono_array_set (*h_addr_list, MonoString *, host_index,
2228 #if defined(AF_INET6)
2230 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2231 MonoArray **h_aliases,
2232 MonoArray **h_addr_list,
2233 gboolean add_local_ips)
2236 struct addrinfo *ai = NULL;
2237 struct in_addr *local_in = NULL;
2239 struct in6_addr *local_in6 = NULL;
2243 MonoDomain *domain = mono_domain_get ();
2246 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2247 if (add_local_ips) {
2248 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2249 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in);
2250 if (nlocal_in || nlocal_in6) {
2251 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2253 MonoString *addr_string;
2257 for (i = 0; i < nlocal_in; i++) {
2258 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2259 addr_string = mono_string_new (domain, addr);
2260 mono_array_set (*h_addr_list, MonoString *, addr_index, addr_string);
2266 MonoString *addr_string;
2270 for (i = 0; i < nlocal_in6; i++) {
2271 inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2272 addr_string = mono_string_new (domain, addr);
2273 mono_array_set (*h_addr_list, MonoString *, addr_index, addr_string);
2281 freeaddrinfo (info);
2290 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2291 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2297 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2299 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2300 MonoString *addr_string;
2302 char buffer [46]; /* Max. size for IPv6 */
2304 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2308 if(ai->ai_family == PF_INET) {
2309 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2311 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 46);
2315 addr_string=mono_string_new(domain, buffer);
2317 addr_string=mono_string_new(domain, "");
2320 mono_array_set(*h_addr_list, MonoString *, addr_index, addr_string);
2322 if(!i && ai->ai_canonname != NULL) {
2323 *h_name=mono_string_new(domain, ai->ai_canonname);
2338 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2340 gboolean add_local_ips = FALSE;
2341 #ifdef HAVE_SIOCGIFCONF
2342 guchar this_hostname [256];
2344 #if !defined(HAVE_GETHOSTBYNAME2_R)
2345 struct addrinfo *info = NULL, hints;
2348 MONO_ARCH_SAVE_REGS;
2350 hostname=mono_string_to_utf8 (host);
2351 #ifdef HAVE_SIOCGIFCONF
2352 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2353 if (!strcmp (hostname, this_hostname))
2354 add_local_ips = TRUE;
2358 memset(&hints, 0, sizeof(hints));
2359 hints.ai_family = get_family_hint ();
2360 hints.ai_socktype = SOCK_STREAM;
2361 hints.ai_flags = AI_CANONNAME;
2363 if (getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2369 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2371 struct hostent he1,*hp1, he2, *hp2;
2372 int buffer_size1, buffer_size2;
2373 char *buffer1, *buffer2;
2375 gboolean return_value;
2378 MONO_ARCH_SAVE_REGS;
2380 hostname=mono_string_to_utf8 (host);
2382 #ifdef HAVE_SIOCGIFCONF
2383 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2384 if (!strcmp (hostname, this_hostname))
2385 add_local_ips = TRUE;
2391 buffer1 = g_malloc0(buffer_size1);
2392 buffer2 = g_malloc0(buffer_size2);
2394 while (gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2395 &hp1, &herr) == ERANGE) {
2397 buffer1 = g_realloc(buffer1, buffer_size1);
2402 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2403 buffer_size2, &hp2, &herr) == ERANGE) {
2405 buffer2 = g_realloc(buffer2, buffer_size2);
2411 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2412 h_addr_list, add_local_ips);
2418 return(return_value);
2419 #endif /* HAVE_GETHOSTBYNAME2_R */
2421 #else /* AF_INET6 */
2422 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2426 gboolean add_local_ips = FALSE;
2427 #ifdef HAVE_SIOCGIFCONF
2428 guchar this_hostname [256];
2431 MONO_ARCH_SAVE_REGS;
2433 hostname=mono_string_to_utf8(host);
2434 #ifdef HAVE_SIOCGIFCONF
2435 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2436 if (!strcmp (hostname, this_hostname))
2437 add_local_ips = TRUE;
2441 he = _wapi_gethostbyname (hostname);
2448 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2450 #endif /* AF_INET6 */
2452 #ifndef HAVE_INET_PTON
2454 inet_pton (int family, const char *address, void *inaddrp)
2456 if (family == AF_INET) {
2457 #ifdef HAVE_INET_ATON
2458 struct in_addr inaddr;
2460 if (!inet_aton (address, &inaddr))
2463 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2466 /* assume the system has inet_addr(), if it doesn't
2467 have that we're pretty much screwed... */
2470 if (!strcmp (address, "255.255.255.255")) {
2471 /* special-case hack */
2472 inaddr = 0xffffffff;
2474 inaddr = inet_addr (address);
2476 #define INADDR_NONE ((in_addr_t) -1)
2478 if (inaddr == INADDR_NONE)
2482 memcpy (inaddrp, &inaddr, sizeof (guint32));
2484 #endif /* HAVE_INET_ATON */
2489 #endif /* !HAVE_INET_PTON */
2491 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2496 struct sockaddr_in saddr;
2497 struct sockaddr_in6 saddr6;
2498 struct addrinfo *info = NULL, hints;
2500 char hostname[1024] = {0};
2502 struct in_addr inaddr;
2506 MONO_ARCH_SAVE_REGS;
2508 address = mono_string_to_utf8 (addr);
2511 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2512 /* Maybe an ipv6 address */
2513 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2519 saddr6.sin6_family = AF_INET6;
2524 saddr.sin_family = AF_INET;
2528 if(family == AF_INET) {
2529 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2530 hostname, sizeof(hostname), NULL, 0,
2531 NI_NAMEREQD) != 0) {
2534 } else if(family == AF_INET6) {
2535 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2536 hostname, sizeof(hostname), NULL, 0,
2537 NI_NAMEREQD) != 0) {
2542 memset (&hints, 0, sizeof(hints));
2543 hints.ai_family = get_family_hint ();
2544 hints.ai_socktype = SOCK_STREAM;
2545 hints.ai_flags = AI_CANONNAME;
2547 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
2551 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
2553 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
2559 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
2563 return(hostent_to_IPHostEntry (he, h_name, h_aliases, h_addr_list, FALSE));
2567 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
2569 guchar hostname[256];
2572 MONO_ARCH_SAVE_REGS;
2574 ret = gethostname (hostname, sizeof (hostname));
2579 *h_name=mono_string_new(mono_domain_get (), hostname);
2584 void mono_network_init(void)
2589 err=WSAStartup(MAKEWORD(2,0), &wsadata);
2591 g_error(G_GNUC_PRETTY_FUNCTION ": Couldn't initialise networking");
2596 g_message(G_GNUC_PRETTY_FUNCTION ": Using socket library: %s", wsadata.szDescription);
2597 g_message(G_GNUC_PRETTY_FUNCTION ": Socket system status: %s", wsadata.szSystemStatus);
2601 void mono_network_cleanup(void)