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 #ifdef HAVE_IPV6_PKTINFO
465 *system_name = IPV6_PKTINFO;
468 case SocketOptionName_HeaderIncluded:
469 case SocketOptionName_IPOptions:
470 case SocketOptionName_TypeOfService:
471 case SocketOptionName_DontFragment:
472 case SocketOptionName_AddSourceMembership:
473 case SocketOptionName_DropSourceMembership:
474 case SocketOptionName_BlockSource:
475 case SocketOptionName_UnblockSource:
476 /* Can't figure out how to map these, so fall
480 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
484 break; /* SocketOptionLevel_IPv6 */
487 case SocketOptionLevel_Tcp:
489 *system_level = SOL_TCP;
492 static int cached = 0;
496 struct protoent *pent;
498 pent = getprotobyname ("TCP");
499 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
503 *system_level = proto;
505 #endif /* HAVE_SOL_TCP */
508 case SocketOptionName_NoDelay:
509 *system_name = TCP_NODELAY;
512 /* The documentation is talking complete
513 * bollocks here: rfc-1222 is titled
514 * 'Advancing the NSFNET Routing Architecture'
515 * and doesn't mention either of the words
516 * "expedite" or "urgent".
518 case SocketOptionName_BsdUrgent:
519 case SocketOptionName_Expedited:
522 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
527 case SocketOptionLevel_Udp:
528 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
531 case SocketOptionName_NoChecksum:
532 case SocketOptionName_ChecksumCoverage:
534 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
541 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
548 #define STASH_SYS_ASS(this) \
549 if(system_assembly == NULL) { \
550 system_assembly=mono_image_loaded ("System"); \
551 if (!system_assembly) { \
552 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL); \
553 if (!sa) g_assert_not_reached (); \
554 else {system_assembly = mono_assembly_get_image (sa);} \
558 static MonoImage *system_assembly=NULL;
562 static gint32 get_family_hint(void)
564 MonoClass *socket_class;
565 MonoClassField *ipv6_field, *ipv4_field;
566 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
569 socket_class = mono_class_from_name (system_assembly,
570 "System.Net.Sockets", "Socket");
571 ipv4_field = mono_class_get_field_from_name (socket_class,
573 ipv6_field = mono_class_get_field_from_name (socket_class,
575 vtable = mono_class_vtable (mono_domain_get (), socket_class);
577 mono_field_static_get_value(vtable, ipv4_field, &ipv4_enabled);
578 mono_field_static_get_value(vtable, ipv6_field, &ipv6_enabled);
580 if(ipv4_enabled == 1 && ipv6_enabled == 1) {
582 } else if(ipv4_enabled == 1) {
590 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
603 sock_family=convert_family(family);
604 if(sock_family==-1) {
605 *error = WSAEAFNOSUPPORT;
609 sock_proto=convert_proto(proto);
611 *error = WSAEPROTONOSUPPORT;
615 sock_type=convert_type(type);
617 *error = WSAESOCKTNOSUPPORT;
621 sock = _wapi_socket (sock_family, sock_type, sock_proto,
622 NULL, 0, WSA_FLAG_OVERLAPPED);
624 if(sock==INVALID_SOCKET) {
625 *error = WSAGetLastError ();
629 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
630 return (GUINT_TO_POINTER (sock));
634 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
635 return (GUINT_TO_POINTER (sock));
639 #ifndef PLATFORM_WIN32
640 /* .net seems to set this by default for SOCK_STREAM,
641 * not for SOCK_DGRAM (see bug #36322)
643 * It seems winsock has a rather different idea of what
644 * SO_REUSEADDR means. If it's set, then a new socket can be
645 * bound over an existing listening socket. There's a new
646 * windows-specific option called SO_EXCLUSIVEADDRUSE but
647 * using that means the socket MUST be closed properly, or a
648 * denial of service can occur. Luckily for us, winsock
649 * behaves as though any other system would when SO_REUSEADDR
650 * is true, so we don't need to do anything else here. See
656 ret = _wapi_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof (true));
657 if(ret==SOCKET_ERROR) {
658 *error = WSAGetLastError ();
666 return(GUINT_TO_POINTER (sock));
669 /* FIXME: the SOCKET parameter (here and in other functions in this
670 * file) is really an IntPtr which needs to be converted to a guint32.
672 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
678 g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
683 /* Clear any pending work item from this socket if the underlying
684 * polling system does not notify when the socket is closed */
685 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
689 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
694 g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
697 return(WSAGetLastError());
700 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
710 ret=ioctlsocket(sock, FIONREAD, &amount);
711 if(ret==SOCKET_ERROR) {
712 *error = WSAGetLastError ();
719 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
730 * block == TRUE/FALSE means we will block/not block.
731 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
735 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
736 if(ret==SOCKET_ERROR) {
737 *error = WSAGetLastError ();
741 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
750 newsock = _wapi_accept (sock, NULL, 0);
751 if(newsock==INVALID_SOCKET) {
752 *error = WSAGetLastError ();
756 return(GUINT_TO_POINTER (newsock));
759 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
769 ret = _wapi_listen (sock, backlog);
770 if(ret==SOCKET_ERROR) {
771 *error = WSAGetLastError ();
775 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
776 int sa_size, gint32 *error)
778 MonoDomain *domain = mono_domain_get ();
779 MonoObject *sockaddr_obj;
780 MonoClass *sockaddr_class;
781 MonoClassField *field;
783 MonoAddressFamily family;
785 /* Build a System.Net.SocketAddress object instance */
786 sockaddr_class=mono_class_from_name(system_assembly, "System.Net", "SocketAddress");
787 sockaddr_obj=mono_object_new(domain, sockaddr_class);
789 /* Locate the SocketAddress data buffer in the object */
790 field=mono_class_get_field_from_name(sockaddr_class, "data");
792 /* Make sure there is space for the family and size bytes */
793 data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
795 /* The data buffer is laid out as follows:
796 * byte 0 is the address family
797 * byte 1 is the buffer length
798 * bytes 2 and 3 are the port info
799 * the rest is the address info
802 family=convert_to_mono_family(saddr->sa_family);
803 if(family==AddressFamily_Unknown) {
804 *error = WSAEAFNOSUPPORT;
808 mono_array_set(data, guint8, 0, family & 0x0FF);
809 mono_array_set(data, guint8, 1, ((family << 8) & 0x0FFFF));
811 if(saddr->sa_family==AF_INET) {
812 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
813 guint16 port=ntohs(sa_in->sin_port);
814 guint32 address=ntohl(sa_in->sin_addr.s_addr);
817 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
820 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
821 mono_array_set(data, guint8, 3, (port) & 0xff);
822 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
823 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
824 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
825 mono_array_set(data, guint8, 7, (address) & 0xff);
827 mono_field_set_value (sockaddr_obj, field, data);
829 return(sockaddr_obj);
831 } else if (saddr->sa_family == AF_INET6) {
832 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
835 guint16 port=ntohs(sa_in->sin6_port);
838 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
841 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
842 mono_array_set(data, guint8, 3, (port) & 0xff);
844 for(i=0; i<16; i++) {
845 mono_array_set(data, guint8, 8+i,
846 sa_in->sin6_addr.s6_addr[i]);
849 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
850 mono_array_set(data, guint8, 25,
851 (sa_in->sin6_scope_id >> 8) & 0xff);
852 mono_array_set(data, guint8, 26,
853 (sa_in->sin6_scope_id >> 16) & 0xff);
854 mono_array_set(data, guint8, 27,
855 (sa_in->sin6_scope_id >> 24) & 0xff);
857 mono_field_set_value (sockaddr_obj, field, data);
859 return(sockaddr_obj);
862 } else if (saddr->sa_family == AF_UNIX) {
865 for (i = 0; i < sa_size; i++) {
866 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
869 mono_field_set_value (sockaddr_obj, field, data);
874 *error = WSAEAFNOSUPPORT;
879 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
881 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
890 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
892 if(ret==SOCKET_ERROR) {
893 *error = WSAGetLastError ();
898 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));
901 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
905 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
907 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
916 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
918 if(ret==SOCKET_ERROR) {
919 *error = WSAGetLastError ();
924 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));
927 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
931 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
935 MonoClassField *field;
940 /* Dig the SocketAddress data buffer out of the object */
941 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
942 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
944 /* The data buffer is laid out as follows:
945 * byte 0 is the address family low byte
946 * byte 1 is the address family high byte
948 * bytes 2 and 3 are the port info
949 * the rest is the address info
951 * the rest is the file name
953 len = mono_array_length (data);
955 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
958 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
959 if(family==AF_INET) {
960 struct sockaddr_in *sa=g_new0(struct sockaddr_in, 1);
961 guint16 port=(mono_array_get(data, guint8, 2) << 8) +
962 mono_array_get(data, guint8, 3);
963 guint32 address=(mono_array_get(data, guint8, 4) << 24) +
964 (mono_array_get(data, guint8, 5) << 16 ) +
965 (mono_array_get(data, guint8, 6) << 8) +
966 mono_array_get(data, guint8, 7);
968 sa->sin_family=family;
969 sa->sin_addr.s_addr=htonl(address);
970 sa->sin_port=htons(port);
972 *sa_size=sizeof(struct sockaddr_in);
973 return((struct sockaddr *)sa);
976 } else if (family == AF_INET6) {
977 struct sockaddr_in6 *sa=g_new0(struct sockaddr_in6, 1);
980 guint16 port = mono_array_get(data, guint8, 3) + (mono_array_get(data, guint8, 2) << 8);
981 guint32 scopeid = mono_array_get(data, guint8, 24) +
982 (mono_array_get(data, guint8, 25)<<8) +
983 (mono_array_get(data, guint8, 26)<<16) +
984 (mono_array_get(data, guint8, 27)<<24);
986 sa->sin6_family=family;
987 sa->sin6_port=htons(port);
988 sa->sin6_scope_id = scopeid;
991 sa->sin6_addr.s6_addr[i] = mono_array_get(data, guint8, 8+i);
993 *sa_size=sizeof(struct sockaddr_in6);
994 return((struct sockaddr *)sa);
997 } else if (family == AF_UNIX) {
998 struct sockaddr_un *sock_un = g_new0 (struct sockaddr_un, 1);
1001 if (len - 2 > MONO_SIZEOF_SUNPATH)
1002 mono_raise_exception (mono_get_exception_index_out_of_range ());
1004 sock_un->sun_family = family;
1005 for (i = 0; i < len - 2; i++)
1006 sock_un->sun_path [i] = mono_array_get (data, guint8,
1008 sock_un->sun_path [len - 2] = '\0';
1009 *sa_size = sizeof (struct sockaddr_un);
1011 return (struct sockaddr *)sock_un;
1014 *error = WSAEAFNOSUPPORT;
1019 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1021 struct sockaddr *sa;
1025 MONO_ARCH_SAVE_REGS;
1029 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1035 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));
1038 ret = _wapi_bind (sock, sa, sa_size);
1039 if(ret==SOCKET_ERROR) {
1040 *error = WSAGetLastError ();
1053 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1054 gint timeout, gint32 *error)
1059 struct timeval *tvptr;
1063 MONO_ARCH_SAVE_REGS;
1065 start = time (NULL);
1069 _wapi_FD_SET (sock, &fds);
1071 divvy = div (timeout, 1000000);
1072 tv.tv_sec = divvy.quot;
1073 tv.tv_usec = divvy.rem;
1079 if (mode == SelectModeRead) {
1080 ret = _wapi_select (0, &fds, NULL, NULL, tvptr);
1081 } else if (mode == SelectModeWrite) {
1082 ret = _wapi_select (0, NULL, &fds, NULL, tvptr);
1083 } else if (mode == SelectModeError) {
1084 ret = _wapi_select (0, NULL, NULL, &fds, tvptr);
1086 g_assert_not_reached ();
1089 if (timeout > 0 && ret < 0) {
1091 int sec = time (NULL) - start;
1093 timeout -= sec * 1000;
1099 } while ((ret == SOCKET_ERROR) && (*error == WSAGetLastError ()) == WSAEINTR);
1101 return (ret != SOCKET_ERROR && _wapi_FD_ISSET (sock, &fds));
1104 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1106 struct sockaddr *sa;
1110 MONO_ARCH_SAVE_REGS;
1114 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1120 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));
1123 ret = _wapi_connect (sock, sa, sa_size);
1124 if(ret==SOCKET_ERROR) {
1125 *error = WSAGetLastError ();
1131 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1138 MONO_ARCH_SAVE_REGS;
1142 alen=mono_array_length(buffer);
1143 if(offset+count>alen) {
1147 buf=mono_array_addr(buffer, guchar, offset);
1149 ret = _wapi_recv (sock, buf, count, recvflags);
1150 if(ret==SOCKET_ERROR) {
1151 *error = WSAGetLastError ();
1158 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1164 struct sockaddr *sa;
1167 MONO_ARCH_SAVE_REGS;
1171 alen=mono_array_length(buffer);
1172 if(offset+count>alen) {
1176 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1181 buf=mono_array_addr(buffer, guchar, offset);
1183 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1184 if(ret==SOCKET_ERROR) {
1186 *error = WSAGetLastError ();
1190 /* If we didn't get a socket size, then we're probably a
1191 * connected connection-oriented socket and the stack hasn't
1192 * returned the remote address. All we can do is return null.
1195 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1204 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1211 MONO_ARCH_SAVE_REGS;
1215 alen=mono_array_length(buffer);
1216 if(offset+count>alen) {
1221 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1224 buf=mono_array_addr(buffer, guchar, offset);
1227 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1230 ret = _wapi_send (sock, buf, count, sendflags);
1231 if(ret==SOCKET_ERROR) {
1232 *error = WSAGetLastError ();
1239 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1245 struct sockaddr *sa;
1248 MONO_ARCH_SAVE_REGS;
1252 alen=mono_array_length(buffer);
1253 if(offset+count>alen) {
1257 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1263 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1266 buf=mono_array_addr(buffer, guchar, offset);
1269 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1272 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1273 if(ret==SOCKET_ERROR) {
1274 *error = WSAGetLastError ();
1282 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1285 MonoClassField *field;
1287 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1288 sock=*(SOCKET *)(((char *)sockobj)+field->offset);
1293 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1294 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1296 MonoThread *thread = NULL;
1303 MonoClass *sock_arr_class;
1307 MONO_ARCH_SAVE_REGS;
1309 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1310 count = mono_array_length (*sockets);
1311 nfds = count - 3; /* NULL separators */
1312 pfds = g_new0 (mono_pollfd, nfds);
1314 for (i = 0; i < count; i++) {
1315 obj = mono_array_get (*sockets, MonoObject *, i);
1321 pfds [idx].fd = GPOINTER_TO_INT (Socket_to_SOCKET (obj));
1322 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1326 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1327 start = time (NULL);
1330 ret = mono_poll (pfds, nfds, timeout);
1331 if (timeout > 0 && ret < 0) {
1333 int sec = time (NULL) - start;
1335 timeout -= sec * 1000;
1341 if (ret == -1 && errno == EINTR) {
1344 thread = mono_thread_current ();
1346 mono_monitor_enter (thread->synch_lock);
1347 leave = ((thread->state & ThreadState_AbortRequested) != 0 ||
1348 (thread->state & ThreadState_StopRequested) != 0);
1349 mono_monitor_exit (thread->synch_lock);
1355 /* Suspend requested? */
1356 mono_thread_interruption_checkpoint ();
1360 } while (ret == -1 && errno == EINTR);
1363 #ifdef PLATFORM_WIN32
1364 *error = WSAGetLastError ();
1366 *error = errno_to_WSA (errno, __func__);
1378 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1379 ret += 3; /* space for the NULL delimiters */
1380 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &ret, NULL);
1383 for (i = 0; i < count && ret > 0; i++) {
1386 obj = mono_array_get (*sockets, MonoObject *, i);
1393 pfd = &pfds [i - mode];
1394 if (pfd->revents == 0)
1398 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1399 mono_array_set (socks, MonoObject *, idx++, obj);
1400 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1401 mono_array_set (socks, MonoObject *, idx++, obj);
1402 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1403 mono_array_set (socks, MonoObject *, idx++, obj);
1411 static MonoObject* int_to_object (MonoDomain *domain, int val)
1413 return mono_value_box (domain, mono_get_int32_class (), &val);
1417 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1423 int valsize=sizeof(val);
1424 struct linger linger;
1425 int lingersize=sizeof(linger);
1427 int time_ms_size = sizeof (time_ms);
1430 int credsize = sizeof(cred);
1432 MonoDomain *domain=mono_domain_get();
1434 MonoClass *obj_class;
1435 MonoClassField *field;
1437 MONO_ARCH_SAVE_REGS;
1441 ret=convert_sockopt_level_and_name(level, name, &system_level,
1444 *error = WSAENOPROTOOPT;
1448 /* No need to deal with MulticastOption names here, because
1449 * you cant getsockopt AddMembership or DropMembership (the
1450 * int getsockopt will error, causing an exception)
1453 case SocketOptionName_Linger:
1454 case SocketOptionName_DontLinger:
1455 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1459 case SocketOptionName_SendTimeout:
1460 case SocketOptionName_ReceiveTimeout:
1461 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1465 case SocketOptionName_PeerCred:
1466 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1472 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1476 if(ret==SOCKET_ERROR) {
1477 *error = WSAGetLastError ();
1482 case SocketOptionName_Linger:
1483 /* build a System.Net.Sockets.LingerOption */
1484 obj_class=mono_class_from_name(system_assembly,
1485 "System.Net.Sockets",
1487 obj=mono_object_new(domain, obj_class);
1489 /* Locate and set the fields "bool enabled" and "int
1492 field=mono_class_get_field_from_name(obj_class, "enabled");
1493 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1495 field=mono_class_get_field_from_name(obj_class, "seconds");
1496 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1500 case SocketOptionName_DontLinger:
1501 /* construct a bool int in val - true if linger is off */
1502 obj = int_to_object (domain, !linger.l_onoff);
1505 case SocketOptionName_SendTimeout:
1506 case SocketOptionName_ReceiveTimeout:
1507 obj = int_to_object (domain, time_ms);
1511 case SocketOptionName_PeerCred:
1513 /* build a Mono.Posix.PeerCred+PeerCredData if
1516 static MonoImage *mono_posix_image = NULL;
1517 MonoPeerCredData *cred_data;
1519 if (mono_posix_image == NULL) {
1520 mono_posix_image=mono_image_loaded ("Mono.Posix");
1521 if (!mono_posix_image) {
1522 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1524 *error = WSAENOPROTOOPT;
1527 mono_posix_image = mono_assembly_get_image (sa);
1532 obj_class = mono_class_from_name(mono_posix_image,
1534 "PeerCred/PeerCredData");
1535 obj = mono_object_new(domain, obj_class);
1536 cred_data = (MonoPeerCredData *)obj;
1537 cred_data->pid = cred.pid;
1538 cred_data->uid = cred.uid;
1539 cred_data->gid = cred.gid;
1545 obj = int_to_object (domain, val);
1551 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1559 MONO_ARCH_SAVE_REGS;
1563 ret=convert_sockopt_level_and_name(level, name, &system_level,
1566 *error = WSAENOPROTOOPT;
1570 valsize=mono_array_length(*byte_val);
1571 buf=mono_array_addr(*byte_val, guchar, 0);
1573 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1574 if(ret==SOCKET_ERROR) {
1575 *error = WSAGetLastError ();
1579 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1580 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1582 struct in_addr inaddr;
1583 MonoClassField *field;
1585 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
1587 /* No idea why .net uses a 64bit type to hold a 32bit value...
1589 * Internal value of IPAddess is in Network Order, there is no need
1590 * to call htonl here.
1592 inaddr.s_addr=(guint32)*(guint64 *)(((char *)ipaddr)+field->offset);
1599 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1601 struct in6_addr in6addr;
1602 MonoClassField *field;
1606 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
1607 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1609 /* Solaris has only the 8 bit version. */
1611 for(i=0; i<8; i++) {
1612 guint16 s = mono_array_get (data, guint16, i);
1613 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1614 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1618 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1622 #endif /* AF_INET6 */
1624 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)
1635 #ifdef HAVE_SOL_IPV6
1636 sol_ipv6 = SOL_IPV6;
1639 struct protoent *pent;
1640 pent = getprotobyname ("ipv6");
1641 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
1649 struct protoent *pent;
1650 pent = getprotobyname ("ip");
1651 sol_ip = (pent != NULL) ? pent->p_proto : 0;
1654 #endif /* AF_INET6 */
1656 MONO_ARCH_SAVE_REGS;
1658 ret=convert_sockopt_level_and_name(level, name, &system_level,
1661 *error = WSAENOPROTOOPT;
1665 /* Only one of obj_val, byte_val or int_val has data */
1667 MonoClassField *field;
1668 struct linger linger;
1672 case SocketOptionName_DontLinger:
1675 valsize=sizeof(linger);
1676 ret = _wapi_setsockopt (sock, system_level,
1677 system_name, &linger, valsize);
1680 case SocketOptionName_Linger:
1681 /* Dig out "bool enabled" and "int seconds"
1684 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
1685 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
1686 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
1687 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
1689 valsize=sizeof(linger);
1690 ret = _wapi_setsockopt (sock, system_level,
1691 system_name, &linger, valsize);
1693 case SocketOptionName_AddMembership:
1694 case SocketOptionName_DropMembership:
1695 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1697 MonoObject *address = NULL;
1700 if(system_level == sol_ipv6) {
1701 struct ipv6_mreq mreq6;
1706 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1707 address = *(gpointer *)(((char *)obj_val) + field->offset);
1710 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
1713 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
1714 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
1716 ret = _wapi_setsockopt (sock, system_level,
1717 system_name, &mreq6,
1719 } else if(system_level == sol_ip)
1720 #endif /* AF_INET6 */
1722 #ifdef HAVE_STRUCT_IP_MREQN
1723 struct ip_mreqn mreq = {{0}};
1725 struct ip_mreq mreq = {{0}};
1726 #endif /* HAVE_STRUCT_IP_MREQN */
1728 /* pain! MulticastOption holds two IPAddress
1729 * members, so I have to dig the value out of
1732 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1733 address = *(gpointer *)(((char *)obj_val) + field->offset);
1735 /* address might not be defined and if so, set the address to ADDR_ANY.
1738 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
1741 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
1742 address = *(gpointer *)(((char *)obj_val) + field->offset);
1744 #ifdef HAVE_STRUCT_IP_MREQN
1746 mreq.imr_address = ipaddress_to_struct_in_addr (address);
1750 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
1752 #endif /* HAVE_STRUCT_IP_MREQN */
1754 ret = _wapi_setsockopt (sock, system_level,
1760 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
1762 /* Cause an exception to be thrown */
1766 } else if (byte_val!=NULL) {
1767 int valsize=mono_array_length(byte_val);
1768 guchar *buf=mono_array_addr(byte_val, guchar, 0);
1770 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
1771 if(ret==SOCKET_ERROR) {
1772 *error = WSAGetLastError ();
1776 /* ReceiveTimeout/SendTimeout get here */
1777 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
1780 if(ret==SOCKET_ERROR) {
1781 *error = WSAGetLastError ();
1785 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
1791 MONO_ARCH_SAVE_REGS;
1795 /* Currently, the values for how (recv=0, send=1, both=2) match
1798 ret = _wapi_shutdown (sock, how);
1799 if(ret==SOCKET_ERROR) {
1800 *error = WSAGetLastError ();
1805 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
1807 MonoArray *output, gint32 *error)
1809 gulong output_bytes = 0;
1810 gchar *i_buffer, *o_buffer;
1814 MONO_ARCH_SAVE_REGS;
1818 if (code == FIONBIO) {
1819 /* Invalid command. Must use Socket.Blocking */
1823 if (input == NULL) {
1827 i_buffer = mono_array_addr (input, gchar, 0);
1828 i_len = mono_array_length (input);
1831 if (output == NULL) {
1835 o_buffer = mono_array_addr (output, gchar, 0);
1836 o_len = mono_array_length (output);
1839 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
1840 if (ret == SOCKET_ERROR) {
1841 *error = WSAGetLastError ();
1845 return (gint) output_bytes;
1848 #ifdef HAVE_SIOCGIFCONF
1850 is_loopback (int family, void *ad)
1852 char *ptr = (char *) ad;
1854 if (family == AF_INET) {
1855 return (ptr [0] == 127);
1859 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
1866 get_local_ips (int family, int *nips)
1868 int addr_size, offset, fd, i, count;
1869 int max_ifaces = 50; /* 50 interfaces should be enough... */
1872 struct ifreq iflags;
1873 char *result, *tmp_ptr;
1874 gboolean ignore_loopback = FALSE;
1877 if (family == AF_INET) {
1878 addr_size = sizeof (struct in_addr);
1879 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
1881 } else if (family == AF_INET6) {
1882 addr_size = sizeof (struct in6_addr);
1883 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
1889 fd = socket (family, SOCK_STREAM, 0);
1891 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
1892 ifc.ifc_buf = g_malloc (ifc.ifc_len);
1893 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
1895 g_free (ifc.ifc_buf);
1899 count = ifc.ifc_len / sizeof (struct ifreq);
1902 g_free (ifc.ifc_buf);
1907 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
1908 strcpy (iflags.ifr_name, ifr->ifr_name);
1909 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
1913 if ((iflags.ifr_flags & IFF_UP) == 0) {
1914 ifr->ifr_name [0] = '\0';
1918 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
1919 ignore_loopback = TRUE;
1924 result = g_malloc (addr_size * count);
1926 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
1927 if (ifr->ifr_name [0] == '\0') {
1932 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
1937 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
1938 tmp_ptr += addr_size;
1941 g_free (ifc.ifc_buf);
1946 get_local_ips (int family, int *nips)
1952 #endif /* HAVE_SIOCGIFCONF */
1955 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
1956 MonoArray **h_aliases,
1957 MonoArray **h_addr_list,
1958 gboolean add_local_ips)
1960 MonoDomain *domain = mono_domain_get ();
1962 struct in_addr *local_in = NULL;
1965 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
1969 *h_name=mono_string_new(domain, he->h_name);
1972 while(he->h_aliases[i]!=NULL) {
1976 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
1978 while(he->h_aliases[i]!=NULL) {
1981 alias=mono_string_new(domain, he->h_aliases[i]);
1982 mono_array_set(*h_aliases, MonoString *, i, alias);
1986 if (add_local_ips) {
1987 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
1989 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
1990 for (i = 0; i < nlocal_in; i++) {
1991 MonoString *addr_string;
1992 char addr [16], *ptr;
1994 ptr = (char *) &local_in [i];
1995 g_snprintf(addr, 16, "%u.%u.%u.%u",
1996 (unsigned char) ptr [0],
1997 (unsigned char) ptr [1],
1998 (unsigned char) ptr [2],
1999 (unsigned char) ptr [3]);
2001 addr_string = mono_string_new (domain, addr);
2002 mono_array_set (*h_addr_list, MonoString *, i, addr_string);
2010 if (nlocal_in == 0) {
2012 while (he->h_addr_list[i]!=NULL) {
2016 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2018 while(he->h_addr_list[i]!=NULL) {
2019 MonoString *addr_string;
2022 g_snprintf(addr, 16, "%u.%u.%u.%u",
2023 (unsigned char)he->h_addr_list[i][0],
2024 (unsigned char)he->h_addr_list[i][1],
2025 (unsigned char)he->h_addr_list[i][2],
2026 (unsigned char)he->h_addr_list[i][3]);
2028 addr_string=mono_string_new(domain, addr);
2029 mono_array_set(*h_addr_list, MonoString *, i, addr_string);
2038 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2039 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2040 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2042 MonoDomain *domain = mono_domain_get ();
2043 int i, host_count, host_index, family_hint;
2044 struct in_addr *local_in = NULL;
2046 struct in6_addr *local_in6 = NULL;
2048 gboolean from_local = FALSE;
2050 family_hint = get_family_hint ();
2052 if(he1 == NULL && he2 == NULL) {
2057 * Check if address length and family are correct
2059 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2063 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2068 * Get the aliases and host name from he1 or he2 whichever is
2069 * not null, if he1 is not null then take aliases from he1
2071 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2072 family_hint == PF_INET)) {
2073 *h_name=mono_string_new (domain, he1->h_name);
2076 while(he1->h_aliases[i]!=NULL) {
2080 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2083 while(he1->h_aliases[i]!=NULL) {
2086 alias=mono_string_new (domain, he1->h_aliases[i]);
2087 mono_array_set (*h_aliases, MonoString *, i, alias);
2090 } else if (family_hint == PF_UNSPEC || family_hint == PF_INET6) {
2091 *h_name=mono_string_new (domain, he2->h_name);
2094 while(he2->h_aliases [i] != NULL) {
2098 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2101 while(he2->h_aliases[i]!=NULL) {
2104 alias=mono_string_new (domain, he2->h_aliases[i]);
2105 mono_array_set (*h_aliases, MonoString *, i, alias);
2111 * Count the number of addresses in he1 + he2
2114 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2115 family_hint == PF_INET)) {
2117 while(he1->h_addr_list[i]!=NULL) {
2123 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2124 family_hint == PF_INET6)) {
2126 while(he2->h_addr_list[i]!=NULL) {
2136 if (add_local_ips) {
2137 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2138 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2140 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2141 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2143 if (nlocal_in || nlocal_in6) {
2145 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2146 nlocal_in + nlocal_in6);
2150 for (n = 0; n < nlocal_in6; n++) {
2151 MonoString *addr_string;
2152 char addr[46]; /* INET6_ADDRSTRLEN == 46 */
2154 inet_ntop (AF_INET6, &local_in6 [n], addr,
2157 addr_string = mono_string_new (domain, addr);
2158 mono_array_set (*h_addr_list, MonoString *, host_index,
2166 for (n = 0; n < nlocal_in; n++) {
2167 MonoString *addr_string;
2168 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2170 inet_ntop (AF_INET, &local_in [n], addr,
2173 addr_string = mono_string_new (domain, addr);
2174 mono_array_set (*h_addr_list, MonoString *, host_index,
2188 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2190 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2191 family_hint == PF_INET6)) {
2193 while(he2->h_addr_list[i] != NULL) {
2194 MonoString *addr_string;
2195 char addr[46]; /* INET6_ADDRSTRLEN == 46 */
2197 inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2200 addr_string = mono_string_new (domain, addr);
2201 mono_array_set (*h_addr_list, MonoString *, host_index,
2208 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2209 family_hint == PF_INET)) {
2211 while(he1->h_addr_list[i] != NULL) {
2212 MonoString *addr_string;
2213 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2215 inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2218 addr_string=mono_string_new (domain, addr);
2219 mono_array_set (*h_addr_list, MonoString *, host_index,
2230 #if defined(AF_INET6)
2232 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2233 MonoArray **h_aliases,
2234 MonoArray **h_addr_list,
2235 gboolean add_local_ips)
2238 struct addrinfo *ai = NULL;
2239 struct in_addr *local_in = NULL;
2241 struct in6_addr *local_in6 = NULL;
2245 MonoDomain *domain = mono_domain_get ();
2248 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2249 if (add_local_ips) {
2250 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2251 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in);
2252 if (nlocal_in || nlocal_in6) {
2253 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2255 MonoString *addr_string;
2259 for (i = 0; i < nlocal_in; i++) {
2260 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2261 addr_string = mono_string_new (domain, addr);
2262 mono_array_set (*h_addr_list, MonoString *, addr_index, addr_string);
2268 MonoString *addr_string;
2272 for (i = 0; i < nlocal_in6; i++) {
2273 inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2274 addr_string = mono_string_new (domain, addr);
2275 mono_array_set (*h_addr_list, MonoString *, addr_index, addr_string);
2283 freeaddrinfo (info);
2292 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2293 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2299 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2301 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2302 MonoString *addr_string;
2304 char buffer [46]; /* Max. size for IPv6 */
2306 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2310 if(ai->ai_family == PF_INET) {
2311 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2313 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 46);
2317 addr_string=mono_string_new(domain, buffer);
2319 addr_string=mono_string_new(domain, "");
2322 mono_array_set(*h_addr_list, MonoString *, addr_index, addr_string);
2324 if(!i && ai->ai_canonname != NULL) {
2325 *h_name=mono_string_new(domain, ai->ai_canonname);
2340 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2342 gboolean add_local_ips = FALSE;
2343 #ifdef HAVE_SIOCGIFCONF
2344 guchar this_hostname [256];
2346 #if !defined(HAVE_GETHOSTBYNAME2_R)
2347 struct addrinfo *info = NULL, hints;
2350 MONO_ARCH_SAVE_REGS;
2352 hostname=mono_string_to_utf8 (host);
2353 #ifdef HAVE_SIOCGIFCONF
2354 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2355 if (!strcmp (hostname, this_hostname))
2356 add_local_ips = TRUE;
2360 memset(&hints, 0, sizeof(hints));
2361 hints.ai_family = get_family_hint ();
2362 hints.ai_socktype = SOCK_STREAM;
2363 hints.ai_flags = AI_CANONNAME;
2365 if (getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2371 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2373 struct hostent he1,*hp1, he2, *hp2;
2374 int buffer_size1, buffer_size2;
2375 char *buffer1, *buffer2;
2377 gboolean return_value;
2380 MONO_ARCH_SAVE_REGS;
2382 hostname=mono_string_to_utf8 (host);
2384 #ifdef HAVE_SIOCGIFCONF
2385 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2386 if (!strcmp (hostname, this_hostname))
2387 add_local_ips = TRUE;
2393 buffer1 = g_malloc0(buffer_size1);
2394 buffer2 = g_malloc0(buffer_size2);
2396 while (gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2397 &hp1, &herr) == ERANGE) {
2399 buffer1 = g_realloc(buffer1, buffer_size1);
2404 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2405 buffer_size2, &hp2, &herr) == ERANGE) {
2407 buffer2 = g_realloc(buffer2, buffer_size2);
2413 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2414 h_addr_list, add_local_ips);
2420 return(return_value);
2421 #endif /* HAVE_GETHOSTBYNAME2_R */
2423 #else /* AF_INET6 */
2424 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2428 gboolean add_local_ips = FALSE;
2429 #ifdef HAVE_SIOCGIFCONF
2430 guchar this_hostname [256];
2433 MONO_ARCH_SAVE_REGS;
2435 hostname=mono_string_to_utf8(host);
2436 #ifdef HAVE_SIOCGIFCONF
2437 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2438 if (!strcmp (hostname, this_hostname))
2439 add_local_ips = TRUE;
2443 he = _wapi_gethostbyname (hostname);
2450 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2452 #endif /* AF_INET6 */
2454 #ifndef HAVE_INET_PTON
2456 inet_pton (int family, const char *address, void *inaddrp)
2458 if (family == AF_INET) {
2459 #ifdef HAVE_INET_ATON
2460 struct in_addr inaddr;
2462 if (!inet_aton (address, &inaddr))
2465 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2468 /* assume the system has inet_addr(), if it doesn't
2469 have that we're pretty much screwed... */
2472 if (!strcmp (address, "255.255.255.255")) {
2473 /* special-case hack */
2474 inaddr = 0xffffffff;
2476 inaddr = inet_addr (address);
2478 #define INADDR_NONE ((in_addr_t) -1)
2480 if (inaddr == INADDR_NONE)
2484 memcpy (inaddrp, &inaddr, sizeof (guint32));
2486 #endif /* HAVE_INET_ATON */
2491 #endif /* !HAVE_INET_PTON */
2493 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2498 struct sockaddr_in saddr;
2499 struct sockaddr_in6 saddr6;
2500 struct addrinfo *info = NULL, hints;
2502 char hostname[1024] = {0};
2504 struct in_addr inaddr;
2508 MONO_ARCH_SAVE_REGS;
2510 address = mono_string_to_utf8 (addr);
2513 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2514 /* Maybe an ipv6 address */
2515 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2521 saddr6.sin6_family = AF_INET6;
2526 saddr.sin_family = AF_INET;
2530 if(family == AF_INET) {
2531 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2532 hostname, sizeof(hostname), NULL, 0,
2533 NI_NAMEREQD) != 0) {
2536 } else if(family == AF_INET6) {
2537 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2538 hostname, sizeof(hostname), NULL, 0,
2539 NI_NAMEREQD) != 0) {
2544 memset (&hints, 0, sizeof(hints));
2545 hints.ai_family = get_family_hint ();
2546 hints.ai_socktype = SOCK_STREAM;
2547 hints.ai_flags = AI_CANONNAME;
2549 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
2553 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
2555 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
2561 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
2565 return(hostent_to_IPHostEntry (he, h_name, h_aliases, h_addr_list, FALSE));
2569 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
2571 guchar hostname[256];
2574 MONO_ARCH_SAVE_REGS;
2576 ret = gethostname (hostname, sizeof (hostname));
2581 *h_name=mono_string_new(mono_domain_get (), hostname);
2586 void mono_network_init(void)
2591 err=WSAStartup(MAKEWORD(2,0), &wsadata);
2593 g_error(G_GNUC_PRETTY_FUNCTION ": Couldn't initialise networking");
2598 g_message(G_GNUC_PRETTY_FUNCTION ": Using socket library: %s", wsadata.szDescription);
2599 g_message(G_GNUC_PRETTY_FUNCTION ": Socket system status: %s", wsadata.szSystemStatus);
2603 void mono_network_cleanup(void)