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-2006 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>
32 #include <mono/metadata/domain-internals.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
45 #ifdef HAVE_SYS_FILIO_H
46 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
48 #ifdef HAVE_SYS_SOCKIO_H
49 #include <sys/sockio.h> /* defines SIOCATMARK */
55 #include "mono/io-layer/socket-wrappers.h"
58 /* This is a kludge to make this file build under cygwin:
59 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
60 * prototypes for some but not all required functions (notably
61 * inet_ntop() is missing), but the libws2_32 library is missing the
62 * actual implementations of these functions.
69 static gint32 convert_family(MonoAddressFamily mono_family)
74 case AddressFamily_Unknown:
75 case AddressFamily_ImpLink:
76 case AddressFamily_Pup:
77 case AddressFamily_Chaos:
78 case AddressFamily_Iso:
79 case AddressFamily_Ecma:
80 case AddressFamily_DataKit:
81 case AddressFamily_Ccitt:
82 case AddressFamily_DataLink:
83 case AddressFamily_Lat:
84 case AddressFamily_HyperChannel:
85 case AddressFamily_NetBios:
86 case AddressFamily_VoiceView:
87 case AddressFamily_FireFox:
88 case AddressFamily_Banyan:
89 case AddressFamily_Atm:
90 case AddressFamily_Cluster:
91 case AddressFamily_Ieee12844:
92 case AddressFamily_NetworkDesigners:
93 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
96 case AddressFamily_Unspecified:
100 case AddressFamily_Unix:
104 case AddressFamily_InterNetwork:
108 case AddressFamily_Ipx:
114 case AddressFamily_Sna:
118 case AddressFamily_DecNet:
122 case AddressFamily_AppleTalk:
126 case AddressFamily_InterNetworkV6:
131 case AddressFamily_Irda:
137 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
143 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
145 MonoAddressFamily family=AddressFamily_Unknown;
149 family=AddressFamily_Unspecified;
153 family=AddressFamily_Unix;
157 family=AddressFamily_InterNetwork;
162 family=AddressFamily_Ipx;
167 family=AddressFamily_Sna;
171 family=AddressFamily_DecNet;
175 family=AddressFamily_AppleTalk;
180 family=AddressFamily_InterNetworkV6;
186 family=AddressFamily_Irda;
190 g_warning("unknown address family 0x%x", af_family);
196 static gint32 convert_type(MonoSocketType mono_type)
201 case SocketType_Stream:
205 case SocketType_Dgram:
217 case SocketType_Seqpacket:
221 case SocketType_Unknown:
222 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
226 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
232 static gint32 convert_proto(MonoProtocolType mono_proto)
237 case ProtocolType_IP:
238 case ProtocolType_IPv6:
239 case ProtocolType_Icmp:
240 case ProtocolType_Igmp:
241 case ProtocolType_Ggp:
242 case ProtocolType_Tcp:
243 case ProtocolType_Pup:
244 case ProtocolType_Udp:
245 case ProtocolType_Idp:
246 /* These protocols are known (on my system at least) */
250 case ProtocolType_ND:
251 case ProtocolType_Raw:
252 case ProtocolType_Ipx:
253 case ProtocolType_Spx:
254 case ProtocolType_SpxII:
255 case ProtocolType_Unknown:
256 /* These protocols arent */
257 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
267 /* Convert MonoSocketFlags */
268 static gint32 convert_socketflags (gint32 sflags)
273 /* SocketFlags.None */
276 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
277 SocketFlags_DontRoute | SocketFlags_Partial))
278 /* Contains invalid flag values */
281 if (sflags & SocketFlags_OutOfBand)
283 if (sflags & SocketFlags_Peek)
285 if (sflags & SocketFlags_DontRoute)
286 flags |= MSG_DONTROUTE;
287 if (sflags & SocketFlags_Partial)
293 if (sflags & SocketFlags_MaxIOVectorLength)
294 /* FIXME: Don't know what to do for MaxIOVectorLength query */
297 return (flags ? flags : -1);
300 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
301 MonoSocketOptionName mono_name,
305 switch (mono_level) {
306 case SocketOptionLevel_Socket:
307 *system_level = SOL_SOCKET;
310 case SocketOptionName_DontLinger:
311 /* This is SO_LINGER, because the setsockopt
312 * internal call maps DontLinger to SO_LINGER
315 *system_name = SO_LINGER;
317 case SocketOptionName_Debug:
318 *system_name = SO_DEBUG;
321 case SocketOptionName_AcceptConnection:
322 *system_name = SO_ACCEPTCONN;
325 case SocketOptionName_ReuseAddress:
326 *system_name = SO_REUSEADDR;
328 case SocketOptionName_KeepAlive:
329 *system_name = SO_KEEPALIVE;
331 case SocketOptionName_DontRoute:
332 *system_name = SO_DONTROUTE;
334 case SocketOptionName_Broadcast:
335 *system_name = SO_BROADCAST;
337 case SocketOptionName_Linger:
338 *system_name = SO_LINGER;
340 case SocketOptionName_OutOfBandInline:
341 *system_name = SO_OOBINLINE;
343 case SocketOptionName_SendBuffer:
344 *system_name = SO_SNDBUF;
346 case SocketOptionName_ReceiveBuffer:
347 *system_name = SO_RCVBUF;
349 case SocketOptionName_SendLowWater:
350 *system_name = SO_SNDLOWAT;
352 case SocketOptionName_ReceiveLowWater:
353 *system_name = SO_RCVLOWAT;
355 case SocketOptionName_SendTimeout:
356 *system_name = SO_SNDTIMEO;
358 case SocketOptionName_ReceiveTimeout:
359 *system_name = SO_RCVTIMEO;
361 case SocketOptionName_Error:
362 *system_name = SO_ERROR;
364 case SocketOptionName_Type:
365 *system_name = SO_TYPE;
368 case SocketOptionName_PeerCred:
369 *system_name = SO_PEERCRED;
372 case SocketOptionName_ExclusiveAddressUse:
373 case SocketOptionName_UseLoopback:
374 case SocketOptionName_MaxConnections:
375 /* Can't figure out how to map these, so fall
379 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
384 case SocketOptionLevel_IP:
386 *system_level = SOL_IP;
389 static int cached = 0;
393 struct protoent *pent;
395 pent = getprotobyname ("IP");
396 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
400 *system_level = proto;
402 #endif /* HAVE_SOL_IP */
405 case SocketOptionName_IPOptions:
406 *system_name = IP_OPTIONS;
409 case SocketOptionName_HeaderIncluded:
410 *system_name = IP_HDRINCL;
414 case SocketOptionName_TypeOfService:
415 *system_name = IP_TOS;
419 case SocketOptionName_IpTimeToLive:
420 *system_name = IP_TTL;
423 case SocketOptionName_MulticastInterface:
424 *system_name = IP_MULTICAST_IF;
426 case SocketOptionName_MulticastTimeToLive:
427 *system_name = IP_MULTICAST_TTL;
429 case SocketOptionName_MulticastLoopback:
430 *system_name = IP_MULTICAST_LOOP;
432 case SocketOptionName_AddMembership:
433 *system_name = IP_ADD_MEMBERSHIP;
435 case SocketOptionName_DropMembership:
436 *system_name = IP_DROP_MEMBERSHIP;
438 #ifdef HAVE_IP_PKTINFO
439 case SocketOptionName_PacketInformation:
440 *system_name = IP_PKTINFO;
442 #endif /* HAVE_IP_PKTINFO */
443 case SocketOptionName_DontFragment:
444 case SocketOptionName_AddSourceMembership:
445 case SocketOptionName_DropSourceMembership:
446 case SocketOptionName_BlockSource:
447 case SocketOptionName_UnblockSource:
448 /* Can't figure out how to map these, so fall
452 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
458 case SocketOptionLevel_IPv6:
460 *system_level = SOL_IPV6;
463 static int cached = 0;
467 struct protoent *pent;
469 pent = getprotobyname ("IPV6");
470 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
474 *system_level = proto;
476 #endif /* HAVE_SOL_IPV6 */
479 case SocketOptionName_IpTimeToLive:
480 *system_name = IPV6_UNICAST_HOPS;
482 case SocketOptionName_MulticastInterface:
483 *system_name = IPV6_MULTICAST_IF;
485 case SocketOptionName_MulticastTimeToLive:
486 *system_name = IPV6_MULTICAST_HOPS;
488 case SocketOptionName_MulticastLoopback:
489 *system_name = IPV6_MULTICAST_LOOP;
491 case SocketOptionName_AddMembership:
492 *system_name = IPV6_JOIN_GROUP;
494 case SocketOptionName_DropMembership:
495 *system_name = IPV6_LEAVE_GROUP;
497 case SocketOptionName_PacketInformation:
498 #ifdef HAVE_IPV6_PKTINFO
499 *system_name = IPV6_PKTINFO;
502 case SocketOptionName_HeaderIncluded:
503 case SocketOptionName_IPOptions:
504 case SocketOptionName_TypeOfService:
505 case SocketOptionName_DontFragment:
506 case SocketOptionName_AddSourceMembership:
507 case SocketOptionName_DropSourceMembership:
508 case SocketOptionName_BlockSource:
509 case SocketOptionName_UnblockSource:
510 /* Can't figure out how to map these, so fall
514 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
518 break; /* SocketOptionLevel_IPv6 */
521 case SocketOptionLevel_Tcp:
523 *system_level = SOL_TCP;
526 static int cached = 0;
530 struct protoent *pent;
532 pent = getprotobyname ("TCP");
533 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
537 *system_level = proto;
539 #endif /* HAVE_SOL_TCP */
542 case SocketOptionName_NoDelay:
543 *system_name = TCP_NODELAY;
546 /* The documentation is talking complete
547 * bollocks here: rfc-1222 is titled
548 * 'Advancing the NSFNET Routing Architecture'
549 * and doesn't mention either of the words
550 * "expedite" or "urgent".
552 case SocketOptionName_BsdUrgent:
553 case SocketOptionName_Expedited:
556 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
561 case SocketOptionLevel_Udp:
562 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
565 case SocketOptionName_NoChecksum:
566 case SocketOptionName_ChecksumCoverage:
568 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
575 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
582 #define STASH_SYS_ASS(this) \
583 if(system_assembly == NULL) { \
584 system_assembly=mono_image_loaded ("System"); \
585 if (!system_assembly) { \
586 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL); \
587 if (!sa) g_assert_not_reached (); \
588 else {system_assembly = mono_assembly_get_image (sa);} \
592 static MonoImage *system_assembly=NULL;
596 static gint32 get_family_hint(void)
598 MonoClass *socket_class;
599 MonoClassField *ipv6_field, *ipv4_field;
600 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
603 socket_class = mono_class_from_name (system_assembly,
604 "System.Net.Sockets", "Socket");
605 ipv4_field = mono_class_get_field_from_name (socket_class,
607 ipv6_field = mono_class_get_field_from_name (socket_class,
609 vtable = mono_class_vtable (mono_domain_get (), socket_class);
611 mono_field_static_get_value(vtable, ipv4_field, &ipv4_enabled);
612 mono_field_static_get_value(vtable, ipv6_field, &ipv6_enabled);
614 if(ipv4_enabled == 1 && ipv6_enabled == 1) {
616 } else if(ipv4_enabled == 1) {
624 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
637 sock_family=convert_family(family);
638 if(sock_family==-1) {
639 *error = WSAEAFNOSUPPORT;
643 sock_proto=convert_proto(proto);
645 *error = WSAEPROTONOSUPPORT;
649 sock_type=convert_type(type);
651 *error = WSAESOCKTNOSUPPORT;
655 sock = _wapi_socket (sock_family, sock_type, sock_proto,
656 NULL, 0, WSA_FLAG_OVERLAPPED);
658 if(sock==INVALID_SOCKET) {
659 *error = WSAGetLastError ();
663 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
664 return (GUINT_TO_POINTER (sock));
668 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
669 return (GUINT_TO_POINTER (sock));
673 #ifndef PLATFORM_WIN32
674 /* .net seems to set this by default for SOCK_STREAM,
675 * not for SOCK_DGRAM (see bug #36322)
677 * It seems winsock has a rather different idea of what
678 * SO_REUSEADDR means. If it's set, then a new socket can be
679 * bound over an existing listening socket. There's a new
680 * windows-specific option called SO_EXCLUSIVEADDRUSE but
681 * using that means the socket MUST be closed properly, or a
682 * denial of service can occur. Luckily for us, winsock
683 * behaves as though any other system would when SO_REUSEADDR
684 * is true, so we don't need to do anything else here. See
690 ret = _wapi_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof (true));
691 if(ret==SOCKET_ERROR) {
692 *error = WSAGetLastError ();
700 return(GUINT_TO_POINTER (sock));
703 /* FIXME: the SOCKET parameter (here and in other functions in this
704 * file) is really an IntPtr which needs to be converted to a guint32.
706 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
712 g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
717 /* Clear any pending work item from this socket if the underlying
718 * polling system does not notify when the socket is closed */
719 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
723 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
728 g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
731 return(WSAGetLastError());
734 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
744 ret=ioctlsocket(sock, FIONREAD, &amount);
745 if(ret==SOCKET_ERROR) {
746 *error = WSAGetLastError ();
753 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
764 * block == TRUE/FALSE means we will block/not block.
765 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
769 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
770 if(ret==SOCKET_ERROR) {
771 *error = WSAGetLastError ();
775 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
784 newsock = _wapi_accept (sock, NULL, 0);
785 if(newsock==INVALID_SOCKET) {
786 *error = WSAGetLastError ();
790 return(GUINT_TO_POINTER (newsock));
793 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
803 ret = _wapi_listen (sock, backlog);
804 if(ret==SOCKET_ERROR) {
805 *error = WSAGetLastError ();
809 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
810 int sa_size, gint32 *error)
812 MonoDomain *domain = mono_domain_get ();
813 MonoObject *sockaddr_obj;
814 MonoClass *sockaddr_class;
815 MonoClassField *field;
817 MonoAddressFamily family;
819 /* Build a System.Net.SocketAddress object instance */
820 sockaddr_class=mono_class_from_name(system_assembly, "System.Net", "SocketAddress");
821 sockaddr_obj=mono_object_new(domain, sockaddr_class);
823 /* Locate the SocketAddress data buffer in the object */
824 field=mono_class_get_field_from_name(sockaddr_class, "data");
826 /* Make sure there is space for the family and size bytes */
828 if (saddr->sa_family == AF_UNIX) {
829 /* sa_len includes the entire sockaddr size, so we don't need the
830 * N bytes (sizeof (unsigned short)) of the family. */
831 data=mono_array_new(domain, mono_get_byte_class (), sa_size);
835 /* May be the +2 here is too conservative, as sa_len returns
836 * the length of the entire sockaddr_in/in6, including
837 * sizeof (unsigned short) of the family */
838 data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
841 /* The data buffer is laid out as follows:
842 * bytes 0 and 1 are the address family
843 * bytes 2 and 3 are the port info
844 * the rest is the address info
847 family=convert_to_mono_family(saddr->sa_family);
848 if(family==AddressFamily_Unknown) {
849 *error = WSAEAFNOSUPPORT;
853 mono_array_set(data, guint8, 0, family & 0x0FF);
854 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
856 if(saddr->sa_family==AF_INET) {
857 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
858 guint16 port=ntohs(sa_in->sin_port);
859 guint32 address=ntohl(sa_in->sin_addr.s_addr);
862 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
865 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
866 mono_array_set(data, guint8, 3, (port) & 0xff);
867 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
868 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
869 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
870 mono_array_set(data, guint8, 7, (address) & 0xff);
872 mono_field_set_value (sockaddr_obj, field, data);
874 return(sockaddr_obj);
876 } else if (saddr->sa_family == AF_INET6) {
877 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
880 guint16 port=ntohs(sa_in->sin6_port);
883 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
886 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
887 mono_array_set(data, guint8, 3, (port) & 0xff);
889 for(i=0; i<16; i++) {
890 mono_array_set(data, guint8, 8+i,
891 sa_in->sin6_addr.s6_addr[i]);
894 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
895 mono_array_set(data, guint8, 25,
896 (sa_in->sin6_scope_id >> 8) & 0xff);
897 mono_array_set(data, guint8, 26,
898 (sa_in->sin6_scope_id >> 16) & 0xff);
899 mono_array_set(data, guint8, 27,
900 (sa_in->sin6_scope_id >> 24) & 0xff);
902 mono_field_set_value (sockaddr_obj, field, data);
904 return(sockaddr_obj);
907 } else if (saddr->sa_family == AF_UNIX) {
910 for (i = 0; i < sa_size; i++) {
911 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
914 mono_field_set_value (sockaddr_obj, field, data);
919 *error = WSAEAFNOSUPPORT;
924 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
926 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
935 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
937 if(ret==SOCKET_ERROR) {
938 *error = WSAGetLastError ();
943 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));
946 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
950 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
952 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
961 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
963 if(ret==SOCKET_ERROR) {
964 *error = WSAGetLastError ();
969 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));
972 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
976 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
980 MonoClassField *field;
985 /* Dig the SocketAddress data buffer out of the object */
986 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
987 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
989 /* The data buffer is laid out as follows:
990 * byte 0 is the address family low byte
991 * byte 1 is the address family high byte
993 * bytes 2 and 3 are the port info
994 * the rest is the address info
996 * the rest is the file name
998 len = mono_array_length (data);
1000 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1003 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1004 if (family == AF_INET) {
1005 struct sockaddr_in *sa;
1010 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1013 sa = g_new0 (struct sockaddr_in, 1);
1014 port = (mono_array_get (data, guint8, 2) << 8) +
1015 mono_array_get (data, guint8, 3);
1016 address = (mono_array_get (data, guint8, 4) << 24) +
1017 (mono_array_get (data, guint8, 5) << 16 ) +
1018 (mono_array_get (data, guint8, 6) << 8) +
1019 mono_array_get (data, guint8, 7);
1021 sa->sin_family = family;
1022 sa->sin_addr.s_addr = htonl (address);
1023 sa->sin_port = htons (port);
1025 *sa_size = sizeof(struct sockaddr_in);
1026 return((struct sockaddr *)sa);
1029 } else if (family == AF_INET6) {
1030 struct sockaddr_in6 *sa;
1036 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1039 sa = g_new0 (struct sockaddr_in6, 1);
1040 port = mono_array_get (data, guint8, 3) +
1041 (mono_array_get (data, guint8, 2) << 8);
1042 scopeid = mono_array_get (data, guint8, 24) +
1043 (mono_array_get (data, guint8, 25) << 8) +
1044 (mono_array_get (data, guint8, 26) << 16) +
1045 (mono_array_get (data, guint8, 27) << 24);
1047 sa->sin6_family = family;
1048 sa->sin6_port = htons (port);
1049 sa->sin6_scope_id = scopeid;
1051 for(i=0; i<16; i++) {
1052 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1055 *sa_size = sizeof(struct sockaddr_in6);
1056 return((struct sockaddr *)sa);
1058 #ifdef HAVE_SYS_UN_H
1059 } else if (family == AF_UNIX) {
1060 struct sockaddr_un *sock_un;
1063 /* Need a byte for the '\0' terminator/prefix, and the first
1064 * two bytes hold the SocketAddress family
1066 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1067 mono_raise_exception (mono_get_exception_index_out_of_range ());
1070 sock_un = g_new0 (struct sockaddr_un, 1);
1072 sock_un->sun_family = family;
1073 for (i = 0; i < len - 2; i++) {
1074 sock_un->sun_path [i] = mono_array_get (data, guint8,
1080 return (struct sockaddr *)sock_un;
1083 *error = WSAEAFNOSUPPORT;
1088 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1090 struct sockaddr *sa;
1094 MONO_ARCH_SAVE_REGS;
1098 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1104 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));
1107 ret = _wapi_bind (sock, sa, sa_size);
1108 if(ret==SOCKET_ERROR) {
1109 *error = WSAGetLastError ();
1122 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1123 gint timeout, gint32 *error)
1125 MonoThread *thread = NULL;
1131 MONO_ARCH_SAVE_REGS;
1133 pfds = g_new0 (mono_pollfd, 1);
1134 pfds[0].fd = GPOINTER_TO_INT (sock);
1135 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1136 (mode == SelectModeWrite) ? MONO_POLLOUT :
1137 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1139 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1140 start = time (NULL);
1144 ret = mono_poll (pfds, 1, timeout);
1145 if (timeout > 0 && ret < 0) {
1147 int sec = time (NULL) - start;
1149 timeout -= sec * 1000;
1157 if (ret == -1 && errno == EINTR) {
1160 if (thread == NULL) {
1161 thread = mono_thread_current ();
1164 mono_monitor_enter (thread->synch_lock);
1165 leave = ((thread->state & ThreadState_AbortRequested) != 0 ||
1166 (thread->state & ThreadState_StopRequested) != 0);
1167 mono_monitor_exit (thread->synch_lock);
1173 /* Suspend requested? */
1174 mono_thread_interruption_checkpoint ();
1178 } while (ret == -1 && errno == EINTR);
1181 #ifdef PLATFORM_WIN32
1182 *error = WSAGetLastError ();
1184 *error = errno_to_WSA (errno, __func__);
1199 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1201 struct sockaddr *sa;
1205 MONO_ARCH_SAVE_REGS;
1209 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1215 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));
1218 ret = _wapi_connect (sock, sa, sa_size);
1219 if(ret==SOCKET_ERROR) {
1220 *error = WSAGetLastError ();
1226 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1233 MONO_ARCH_SAVE_REGS;
1237 alen = mono_array_length (buffer);
1238 if (offset > alen - count) {
1242 buf=mono_array_addr(buffer, guchar, offset);
1244 recvflags = convert_socketflags (flags);
1245 if (recvflags == -1) {
1246 *error = WSAEOPNOTSUPP;
1250 ret = _wapi_recv (sock, buf, count, recvflags);
1251 if(ret==SOCKET_ERROR) {
1252 *error = WSAGetLastError ();
1259 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1265 struct sockaddr *sa;
1268 MONO_ARCH_SAVE_REGS;
1272 alen = mono_array_length (buffer);
1273 if (offset > alen - count) {
1277 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1282 buf=mono_array_addr(buffer, guchar, offset);
1284 recvflags = convert_socketflags (flags);
1285 if (recvflags == -1) {
1286 *error = WSAEOPNOTSUPP;
1290 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1291 if(ret==SOCKET_ERROR) {
1293 *error = WSAGetLastError ();
1297 /* If we didn't get a socket size, then we're probably a
1298 * connected connection-oriented socket and the stack hasn't
1299 * returned the remote address. All we can do is return null.
1302 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1311 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1318 MONO_ARCH_SAVE_REGS;
1322 alen = mono_array_length (buffer);
1323 if (offset > alen - count) {
1328 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1331 buf=mono_array_addr(buffer, guchar, offset);
1334 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1337 sendflags = convert_socketflags (flags);
1338 if (sendflags == -1) {
1339 *error = WSAEOPNOTSUPP;
1343 ret = _wapi_send (sock, buf, count, sendflags);
1344 if(ret==SOCKET_ERROR) {
1345 *error = WSAGetLastError ();
1352 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1358 struct sockaddr *sa;
1361 MONO_ARCH_SAVE_REGS;
1365 alen = mono_array_length (buffer);
1366 if (offset > alen - count) {
1370 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1376 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1379 buf=mono_array_addr(buffer, guchar, offset);
1382 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1385 sendflags = convert_socketflags (flags);
1386 if (sendflags == -1) {
1387 *error = WSAEOPNOTSUPP;
1391 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1392 if(ret==SOCKET_ERROR) {
1393 *error = WSAGetLastError ();
1401 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1404 MonoClassField *field;
1406 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1407 sock=*(SOCKET *)(((char *)sockobj)+field->offset);
1412 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1413 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1415 MonoThread *thread = NULL;
1422 MonoClass *sock_arr_class;
1426 MONO_ARCH_SAVE_REGS;
1428 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1429 count = mono_array_length (*sockets);
1430 nfds = count - 3; /* NULL separators */
1431 pfds = g_new0 (mono_pollfd, nfds);
1433 for (i = 0; i < count; i++) {
1434 obj = mono_array_get (*sockets, MonoObject *, i);
1441 /* The socket array was bogus */
1447 pfds [idx].fd = GPOINTER_TO_INT (Socket_to_SOCKET (obj));
1448 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1452 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1453 start = time (NULL);
1456 ret = mono_poll (pfds, nfds, timeout);
1457 if (timeout > 0 && ret < 0) {
1459 int sec = time (NULL) - start;
1461 timeout -= sec * 1000;
1467 if (ret == -1 && errno == EINTR) {
1470 thread = mono_thread_current ();
1472 mono_monitor_enter (thread->synch_lock);
1473 leave = ((thread->state & ThreadState_AbortRequested) != 0 ||
1474 (thread->state & ThreadState_StopRequested) != 0);
1475 mono_monitor_exit (thread->synch_lock);
1481 /* Suspend requested? */
1482 mono_thread_interruption_checkpoint ();
1486 } while (ret == -1 && errno == EINTR);
1489 #ifdef PLATFORM_WIN32
1490 *error = WSAGetLastError ();
1492 *error = errno_to_WSA (errno, __func__);
1504 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1505 ret += 3; /* space for the NULL delimiters */
1506 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, (guint32*)&ret, NULL);
1509 for (i = 0; i < count && ret > 0; i++) {
1512 obj = mono_array_get (*sockets, MonoObject *, i);
1519 pfd = &pfds [i - mode];
1520 if (pfd->revents == 0)
1524 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1525 mono_array_setref (socks, idx++, obj);
1526 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1527 mono_array_setref (socks, idx++, obj);
1528 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1529 mono_array_setref (socks, idx++, obj);
1537 static MonoObject* int_to_object (MonoDomain *domain, int val)
1539 return mono_value_box (domain, mono_get_int32_class (), &val);
1543 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1549 socklen_t valsize=sizeof(val);
1550 struct linger linger;
1551 socklen_t lingersize=sizeof(linger);
1553 socklen_t time_ms_size = sizeof (time_ms);
1556 socklen_t credsize = sizeof(cred);
1558 MonoDomain *domain=mono_domain_get();
1560 MonoClass *obj_class;
1561 MonoClassField *field;
1563 MONO_ARCH_SAVE_REGS;
1567 ret=convert_sockopt_level_and_name(level, name, &system_level,
1570 *error = WSAENOPROTOOPT;
1574 /* No need to deal with MulticastOption names here, because
1575 * you cant getsockopt AddMembership or DropMembership (the
1576 * int getsockopt will error, causing an exception)
1579 case SocketOptionName_Linger:
1580 case SocketOptionName_DontLinger:
1581 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1585 case SocketOptionName_SendTimeout:
1586 case SocketOptionName_ReceiveTimeout:
1587 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1591 case SocketOptionName_PeerCred:
1592 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1598 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1602 if(ret==SOCKET_ERROR) {
1603 *error = WSAGetLastError ();
1608 case SocketOptionName_Linger:
1609 /* build a System.Net.Sockets.LingerOption */
1610 obj_class=mono_class_from_name(system_assembly,
1611 "System.Net.Sockets",
1613 obj=mono_object_new(domain, obj_class);
1615 /* Locate and set the fields "bool enabled" and "int
1618 field=mono_class_get_field_from_name(obj_class, "enabled");
1619 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1621 field=mono_class_get_field_from_name(obj_class, "seconds");
1622 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1626 case SocketOptionName_DontLinger:
1627 /* construct a bool int in val - true if linger is off */
1628 obj = int_to_object (domain, !linger.l_onoff);
1631 case SocketOptionName_SendTimeout:
1632 case SocketOptionName_ReceiveTimeout:
1633 obj = int_to_object (domain, time_ms);
1637 case SocketOptionName_PeerCred:
1639 /* build a Mono.Posix.PeerCred+PeerCredData if
1642 static MonoImage *mono_posix_image = NULL;
1643 MonoPeerCredData *cred_data;
1645 if (mono_posix_image == NULL) {
1646 mono_posix_image=mono_image_loaded ("Mono.Posix");
1647 if (!mono_posix_image) {
1648 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1650 *error = WSAENOPROTOOPT;
1653 mono_posix_image = mono_assembly_get_image (sa);
1658 obj_class = mono_class_from_name(mono_posix_image,
1661 obj = mono_object_new(domain, obj_class);
1662 cred_data = (MonoPeerCredData *)obj;
1663 cred_data->pid = cred.pid;
1664 cred_data->uid = cred.uid;
1665 cred_data->gid = cred.gid;
1671 obj = int_to_object (domain, val);
1677 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1685 MONO_ARCH_SAVE_REGS;
1689 ret=convert_sockopt_level_and_name(level, name, &system_level,
1692 *error = WSAENOPROTOOPT;
1696 valsize=mono_array_length(*byte_val);
1697 buf=mono_array_addr(*byte_val, guchar, 0);
1699 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1700 if(ret==SOCKET_ERROR) {
1701 *error = WSAGetLastError ();
1705 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1706 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1708 struct in_addr inaddr;
1709 MonoClassField *field;
1711 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
1713 /* No idea why .net uses a 64bit type to hold a 32bit value...
1715 * Internal value of IPAddess is in Network Order, there is no need
1716 * to call htonl here.
1718 inaddr.s_addr=(guint32)*(guint64 *)(((char *)ipaddr)+field->offset);
1725 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1727 struct in6_addr in6addr;
1728 MonoClassField *field;
1732 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
1733 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1735 /* Solaris has only the 8 bit version. */
1737 for(i=0; i<8; i++) {
1738 guint16 s = mono_array_get (data, guint16, i);
1739 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1740 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1744 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1748 #endif /* AF_INET6 */
1750 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)
1761 #ifdef HAVE_SOL_IPV6
1762 sol_ipv6 = SOL_IPV6;
1765 struct protoent *pent;
1766 pent = getprotobyname ("ipv6");
1767 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
1775 struct protoent *pent;
1776 pent = getprotobyname ("ip");
1777 sol_ip = (pent != NULL) ? pent->p_proto : 0;
1780 #endif /* AF_INET6 */
1782 MONO_ARCH_SAVE_REGS;
1784 ret=convert_sockopt_level_and_name(level, name, &system_level,
1787 *error = WSAENOPROTOOPT;
1791 /* Only one of obj_val, byte_val or int_val has data */
1793 MonoClassField *field;
1794 struct linger linger;
1798 case SocketOptionName_DontLinger:
1801 valsize=sizeof(linger);
1802 ret = _wapi_setsockopt (sock, system_level,
1803 system_name, &linger, valsize);
1806 case SocketOptionName_Linger:
1807 /* Dig out "bool enabled" and "int seconds"
1810 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
1811 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
1812 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
1813 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
1815 valsize=sizeof(linger);
1816 ret = _wapi_setsockopt (sock, system_level,
1817 system_name, &linger, valsize);
1819 case SocketOptionName_AddMembership:
1820 case SocketOptionName_DropMembership:
1821 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1823 MonoObject *address = NULL;
1826 if(system_level == sol_ipv6) {
1827 struct ipv6_mreq mreq6;
1832 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1833 address = *(gpointer *)(((char *)obj_val) + field->offset);
1836 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
1839 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
1840 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
1842 ret = _wapi_setsockopt (sock, system_level,
1843 system_name, &mreq6,
1845 } else if(system_level == sol_ip)
1846 #endif /* AF_INET6 */
1848 #ifdef HAVE_STRUCT_IP_MREQN
1849 struct ip_mreqn mreq = {{0}};
1851 struct ip_mreq mreq = {{0}};
1852 #endif /* HAVE_STRUCT_IP_MREQN */
1854 /* pain! MulticastOption holds two IPAddress
1855 * members, so I have to dig the value out of
1858 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1859 address = *(gpointer *)(((char *)obj_val) + field->offset);
1861 /* address might not be defined and if so, set the address to ADDR_ANY.
1864 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
1867 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
1868 address = *(gpointer *)(((char *)obj_val) + field->offset);
1870 #ifdef HAVE_STRUCT_IP_MREQN
1872 mreq.imr_address = ipaddress_to_struct_in_addr (address);
1876 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
1878 #endif /* HAVE_STRUCT_IP_MREQN */
1880 ret = _wapi_setsockopt (sock, system_level,
1886 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
1888 /* Cause an exception to be thrown */
1892 } else if (byte_val!=NULL) {
1893 int valsize=mono_array_length(byte_val);
1894 guchar *buf=mono_array_addr(byte_val, guchar, 0);
1896 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
1897 if(ret==SOCKET_ERROR) {
1898 *error = WSAGetLastError ();
1902 /* ReceiveTimeout/SendTimeout get here */
1903 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
1906 if(ret==SOCKET_ERROR) {
1907 *error = WSAGetLastError ();
1911 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
1917 MONO_ARCH_SAVE_REGS;
1921 /* Currently, the values for how (recv=0, send=1, both=2) match
1924 ret = _wapi_shutdown (sock, how);
1925 if(ret==SOCKET_ERROR) {
1926 *error = WSAGetLastError ();
1931 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
1933 MonoArray *output, gint32 *error)
1935 glong output_bytes = 0;
1936 gchar *i_buffer, *o_buffer;
1940 MONO_ARCH_SAVE_REGS;
1944 if (code == FIONBIO) {
1945 /* Invalid command. Must use Socket.Blocking */
1949 if (input == NULL) {
1953 i_buffer = mono_array_addr (input, gchar, 0);
1954 i_len = mono_array_length (input);
1957 if (output == NULL) {
1961 o_buffer = mono_array_addr (output, gchar, 0);
1962 o_len = mono_array_length (output);
1965 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
1966 if (ret == SOCKET_ERROR) {
1967 *error = WSAGetLastError ();
1971 return (gint) output_bytes;
1974 #ifdef HAVE_SIOCGIFCONF
1976 is_loopback (int family, void *ad)
1978 char *ptr = (char *) ad;
1980 if (family == AF_INET) {
1981 return (ptr [0] == 127);
1985 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
1992 get_local_ips (int family, int *nips)
1994 int addr_size, offset, fd, i, count;
1995 int max_ifaces = 50; /* 50 interfaces should be enough... */
1998 struct ifreq iflags;
1999 char *result, *tmp_ptr;
2000 gboolean ignore_loopback = FALSE;
2003 if (family == AF_INET) {
2004 addr_size = sizeof (struct in_addr);
2005 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2007 } else if (family == AF_INET6) {
2008 addr_size = sizeof (struct in6_addr);
2009 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2015 fd = socket (family, SOCK_STREAM, 0);
2017 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2018 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2019 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2021 g_free (ifc.ifc_buf);
2025 count = ifc.ifc_len / sizeof (struct ifreq);
2028 g_free (ifc.ifc_buf);
2033 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2034 strcpy (iflags.ifr_name, ifr->ifr_name);
2035 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2039 if ((iflags.ifr_flags & IFF_UP) == 0) {
2040 ifr->ifr_name [0] = '\0';
2044 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2045 ignore_loopback = TRUE;
2050 result = g_malloc (addr_size * count);
2052 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2053 if (ifr->ifr_name [0] == '\0') {
2058 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2063 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2064 tmp_ptr += addr_size;
2067 g_free (ifc.ifc_buf);
2072 get_local_ips (int family, int *nips)
2078 #endif /* HAVE_SIOCGIFCONF */
2081 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2082 MonoArray **h_aliases,
2083 MonoArray **h_addr_list,
2084 gboolean add_local_ips)
2086 MonoDomain *domain = mono_domain_get ();
2088 struct in_addr *local_in = NULL;
2091 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2095 *h_name=mono_string_new(domain, he->h_name);
2098 while(he->h_aliases[i]!=NULL) {
2102 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2104 while(he->h_aliases[i]!=NULL) {
2107 alias=mono_string_new(domain, he->h_aliases[i]);
2108 mono_array_setref (*h_aliases, i, alias);
2112 if (add_local_ips) {
2113 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2115 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2116 for (i = 0; i < nlocal_in; i++) {
2117 MonoString *addr_string;
2118 char addr [16], *ptr;
2120 ptr = (char *) &local_in [i];
2121 g_snprintf(addr, 16, "%u.%u.%u.%u",
2122 (unsigned char) ptr [0],
2123 (unsigned char) ptr [1],
2124 (unsigned char) ptr [2],
2125 (unsigned char) ptr [3]);
2127 addr_string = mono_string_new (domain, addr);
2128 mono_array_setref (*h_addr_list, i, addr_string);
2136 if (nlocal_in == 0) {
2138 while (he->h_addr_list[i]!=NULL) {
2142 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2144 while(he->h_addr_list[i]!=NULL) {
2145 MonoString *addr_string;
2148 g_snprintf(addr, 16, "%u.%u.%u.%u",
2149 (unsigned char)he->h_addr_list[i][0],
2150 (unsigned char)he->h_addr_list[i][1],
2151 (unsigned char)he->h_addr_list[i][2],
2152 (unsigned char)he->h_addr_list[i][3]);
2154 addr_string=mono_string_new(domain, addr);
2155 mono_array_setref (*h_addr_list, i, addr_string);
2163 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2164 MonoArray **h_aliases,
2165 MonoArray **h_addr_list)
2167 MonoDomain *domain = mono_domain_get ();
2169 *h_name=mono_string_new(domain, addr);
2170 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2171 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2172 mono_array_setref (*h_addr_list, 0, *h_name);
2178 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2179 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2180 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2182 MonoDomain *domain = mono_domain_get ();
2183 int i, host_count, host_index, family_hint;
2184 struct in_addr *local_in = NULL;
2186 struct in6_addr *local_in6 = NULL;
2188 gboolean from_local = FALSE;
2190 family_hint = get_family_hint ();
2192 if(he1 == NULL && he2 == NULL) {
2197 * Check if address length and family are correct
2199 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2203 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2208 * Get the aliases and host name from he1 or he2 whichever is
2209 * not null, if he1 is not null then take aliases from he1
2211 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2212 family_hint == PF_INET)) {
2213 *h_name=mono_string_new (domain, he1->h_name);
2216 while(he1->h_aliases[i]!=NULL) {
2220 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2223 while(he1->h_aliases[i]!=NULL) {
2226 alias=mono_string_new (domain, he1->h_aliases[i]);
2227 mono_array_setref (*h_aliases, i, alias);
2230 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2231 family_hint == PF_INET6)) {
2232 *h_name=mono_string_new (domain, he2->h_name);
2235 while(he2->h_aliases [i] != NULL) {
2239 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2242 while(he2->h_aliases[i]!=NULL) {
2245 alias=mono_string_new (domain, he2->h_aliases[i]);
2246 mono_array_setref (*h_aliases, i, alias);
2254 * Count the number of addresses in he1 + he2
2257 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2258 family_hint == PF_INET)) {
2260 while(he1->h_addr_list[i]!=NULL) {
2266 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2267 family_hint == PF_INET6)) {
2269 while(he2->h_addr_list[i]!=NULL) {
2279 if (add_local_ips) {
2280 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2281 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2283 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2284 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2286 if (nlocal_in || nlocal_in6) {
2288 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2289 nlocal_in + nlocal_in6);
2293 for (n = 0; n < nlocal_in6; n++) {
2294 MonoString *addr_string;
2296 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2298 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2301 addr_string = mono_string_new (domain, addr);
2302 mono_array_setref (*h_addr_list, host_index, addr_string);
2310 for (n = 0; n < nlocal_in; n++) {
2311 MonoString *addr_string;
2313 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2315 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2318 addr_string = mono_string_new (domain, addr);
2319 mono_array_setref (*h_addr_list, host_index, addr_string);
2333 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2335 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2336 family_hint == PF_INET6)) {
2338 while(he2->h_addr_list[i] != NULL) {
2339 MonoString *addr_string;
2341 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2343 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2347 addr_string = mono_string_new (domain, addr);
2348 mono_array_setref (*h_addr_list, host_index, addr_string);
2355 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2356 family_hint == PF_INET)) {
2358 while(he1->h_addr_list[i] != NULL) {
2359 MonoString *addr_string;
2361 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2363 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2367 addr_string=mono_string_new (domain, addr);
2368 mono_array_setref (*h_addr_list, host_index, addr_string);
2379 #if defined(AF_INET6)
2381 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2382 MonoArray **h_aliases,
2383 MonoArray **h_addr_list,
2384 gboolean add_local_ips)
2387 struct addrinfo *ai = NULL;
2388 struct in_addr *local_in = NULL;
2390 struct in6_addr *local_in6 = NULL;
2394 MonoDomain *domain = mono_domain_get ();
2397 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2398 if (add_local_ips) {
2399 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2400 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2401 if (nlocal_in || nlocal_in6) {
2402 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2404 MonoString *addr_string;
2408 for (i = 0; i < nlocal_in; i++) {
2409 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2410 addr_string = mono_string_new (domain, addr);
2411 mono_array_setref (*h_addr_list, addr_index, addr_string);
2417 MonoString *addr_string;
2422 for (i = 0; i < nlocal_in6; i++) {
2423 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2425 addr_string = mono_string_new (domain, addr);
2426 mono_array_setref (*h_addr_list, addr_index, addr_string);
2435 freeaddrinfo (info);
2444 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2445 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2451 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2453 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2454 MonoString *addr_string;
2456 char buffer [48]; /* Max. size for IPv6 */
2458 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2462 if(ai->ai_family == PF_INET) {
2463 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2465 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2469 addr_string=mono_string_new(domain, buffer);
2471 addr_string=mono_string_new(domain, "");
2474 mono_array_setref (*h_addr_list, addr_index, addr_string);
2476 if(!i && ai->ai_canonname != NULL) {
2477 *h_name=mono_string_new(domain, ai->ai_canonname);
2492 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2494 gboolean add_local_ips = FALSE;
2495 #ifdef HAVE_SIOCGIFCONF
2496 gchar this_hostname [256];
2498 #if !defined(HAVE_GETHOSTBYNAME2_R)
2499 struct addrinfo *info = NULL, hints;
2502 MONO_ARCH_SAVE_REGS;
2504 hostname=mono_string_to_utf8 (host);
2505 #ifdef HAVE_SIOCGIFCONF
2506 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2507 if (!strcmp (hostname, this_hostname))
2508 add_local_ips = TRUE;
2512 memset(&hints, 0, sizeof(hints));
2513 hints.ai_family = get_family_hint ();
2514 hints.ai_socktype = SOCK_STREAM;
2515 hints.ai_flags = AI_CANONNAME;
2517 if (getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2523 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2525 struct hostent he1,*hp1, he2, *hp2;
2526 int buffer_size1, buffer_size2;
2527 char *buffer1, *buffer2;
2529 gboolean return_value;
2532 MONO_ARCH_SAVE_REGS;
2534 hostname=mono_string_to_utf8 (host);
2536 #ifdef HAVE_SIOCGIFCONF
2537 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2538 if (!strcmp (hostname, this_hostname))
2539 add_local_ips = TRUE;
2545 buffer1 = g_malloc0(buffer_size1);
2546 buffer2 = g_malloc0(buffer_size2);
2548 while (gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2549 &hp1, &herr) == ERANGE) {
2551 buffer1 = g_realloc(buffer1, buffer_size1);
2556 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2557 buffer_size2, &hp2, &herr) == ERANGE) {
2559 buffer2 = g_realloc(buffer2, buffer_size2);
2565 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2566 h_addr_list, add_local_ips);
2572 return(return_value);
2573 #endif /* HAVE_GETHOSTBYNAME2_R */
2575 #else /* AF_INET6 */
2576 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2580 gboolean add_local_ips = FALSE;
2581 #ifdef HAVE_SIOCGIFCONF
2582 gchar this_hostname [256];
2585 MONO_ARCH_SAVE_REGS;
2587 hostname=mono_string_to_utf8(host);
2588 #ifdef HAVE_SIOCGIFCONF
2589 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2590 if (!strcmp (hostname, this_hostname))
2591 add_local_ips = TRUE;
2595 he = _wapi_gethostbyname (hostname);
2602 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2604 #endif /* AF_INET6 */
2606 #ifndef HAVE_INET_PTON
2608 inet_pton (int family, const char *address, void *inaddrp)
2610 if (family == AF_INET) {
2611 #ifdef HAVE_INET_ATON
2612 struct in_addr inaddr;
2614 if (!inet_aton (address, &inaddr))
2617 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2620 /* assume the system has inet_addr(), if it doesn't
2621 have that we're pretty much screwed... */
2624 if (!strcmp (address, "255.255.255.255")) {
2625 /* special-case hack */
2626 inaddr = 0xffffffff;
2628 inaddr = inet_addr (address);
2630 #define INADDR_NONE ((in_addr_t) -1)
2632 if (inaddr == INADDR_NONE)
2636 memcpy (inaddrp, &inaddr, sizeof (guint32));
2638 #endif /* HAVE_INET_ATON */
2643 #endif /* !HAVE_INET_PTON */
2645 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2648 const char *version;
2652 struct sockaddr_in saddr;
2653 struct sockaddr_in6 saddr6;
2654 struct addrinfo *info = NULL, hints;
2656 char hostname[1024] = {0};
2659 struct in_addr inaddr;
2664 MONO_ARCH_SAVE_REGS;
2666 version = mono_get_runtime_info ()->framework_version;
2667 v1 = (version[0] == '1');
2669 address = mono_string_to_utf8 (addr);
2672 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2673 /* Maybe an ipv6 address */
2674 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2680 saddr6.sin6_family = AF_INET6;
2685 saddr.sin_family = AF_INET;
2690 flags = NI_NAMEREQD;
2693 if(family == AF_INET) {
2694 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2695 hostname, sizeof(hostname), NULL, 0,
2699 } else if(family == AF_INET6) {
2700 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2701 hostname, sizeof(hostname), NULL, 0,
2707 memset (&hints, 0, sizeof(hints));
2708 hints.ai_family = get_family_hint ();
2709 hints.ai_socktype = SOCK_STREAM;
2710 hints.ai_flags = AI_CANONNAME;
2712 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
2716 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
2718 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
2723 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
2727 ret = ipaddr_to_IPHostEntry (address, h_name,
2728 h_aliases, h_addr_list);
2731 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
2732 h_addr_list, FALSE);
2740 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
2742 gchar hostname[256];
2745 MONO_ARCH_SAVE_REGS;
2747 ret = gethostname (hostname, sizeof (hostname));
2752 *h_name=mono_string_new(mono_domain_get (), hostname);
2757 void mono_network_init(void)
2762 err=WSAStartup(MAKEWORD(2,0), &wsadata);
2764 g_error(G_GNUC_PRETTY_FUNCTION ": Couldn't initialise networking");
2769 g_message(G_GNUC_PRETTY_FUNCTION ": Using socket library: %s", wsadata.szDescription);
2770 g_message(G_GNUC_PRETTY_FUNCTION ": Socket system status: %s", wsadata.szSystemStatus);
2774 void mono_network_cleanup(void)