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>
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 /* Convert MonoSocketFlags */
267 static gint32 convert_socketflags (gint32 sflags)
272 /* SocketFlags.None */
275 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
276 SocketFlags_DontRoute | SocketFlags_Partial))
277 /* Contains invalid flag values */
280 if (sflags & SocketFlags_OutOfBand)
282 if (sflags & SocketFlags_Peek)
284 if (sflags & SocketFlags_DontRoute)
285 flags |= MSG_DONTROUTE;
286 if (sflags & SocketFlags_Partial)
292 if (sflags & SocketFlags_MaxIOVectorLength)
293 /* FIXME: Don't know what to do for MaxIOVectorLength query */
296 return (flags ? flags : -1);
299 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
300 MonoSocketOptionName mono_name,
304 switch (mono_level) {
305 case SocketOptionLevel_Socket:
306 *system_level = SOL_SOCKET;
309 case SocketOptionName_DontLinger:
310 /* This is SO_LINGER, because the setsockopt
311 * internal call maps DontLinger to SO_LINGER
314 *system_name = SO_LINGER;
316 case SocketOptionName_Debug:
317 *system_name = SO_DEBUG;
320 case SocketOptionName_AcceptConnection:
321 *system_name = SO_ACCEPTCONN;
324 case SocketOptionName_ReuseAddress:
325 *system_name = SO_REUSEADDR;
327 case SocketOptionName_KeepAlive:
328 *system_name = SO_KEEPALIVE;
330 case SocketOptionName_DontRoute:
331 *system_name = SO_DONTROUTE;
333 case SocketOptionName_Broadcast:
334 *system_name = SO_BROADCAST;
336 case SocketOptionName_Linger:
337 *system_name = SO_LINGER;
339 case SocketOptionName_OutOfBandInline:
340 *system_name = SO_OOBINLINE;
342 case SocketOptionName_SendBuffer:
343 *system_name = SO_SNDBUF;
345 case SocketOptionName_ReceiveBuffer:
346 *system_name = SO_RCVBUF;
348 case SocketOptionName_SendLowWater:
349 *system_name = SO_SNDLOWAT;
351 case SocketOptionName_ReceiveLowWater:
352 *system_name = SO_RCVLOWAT;
354 case SocketOptionName_SendTimeout:
355 *system_name = SO_SNDTIMEO;
357 case SocketOptionName_ReceiveTimeout:
358 *system_name = SO_RCVTIMEO;
360 case SocketOptionName_Error:
361 *system_name = SO_ERROR;
363 case SocketOptionName_Type:
364 *system_name = SO_TYPE;
367 case SocketOptionName_PeerCred:
368 *system_name = SO_PEERCRED;
371 case SocketOptionName_ExclusiveAddressUse:
372 case SocketOptionName_UseLoopback:
373 case SocketOptionName_MaxConnections:
374 /* Can't figure out how to map these, so fall
378 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
383 case SocketOptionLevel_IP:
385 *system_level = SOL_IP;
388 static int cached = 0;
392 struct protoent *pent;
394 pent = getprotobyname ("IP");
395 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
399 *system_level = proto;
401 #endif /* HAVE_SOL_IP */
404 case SocketOptionName_IPOptions:
405 *system_name = IP_OPTIONS;
408 case SocketOptionName_HeaderIncluded:
409 *system_name = IP_HDRINCL;
413 case SocketOptionName_TypeOfService:
414 *system_name = IP_TOS;
418 case SocketOptionName_IpTimeToLive:
419 *system_name = IP_TTL;
422 case SocketOptionName_MulticastInterface:
423 *system_name = IP_MULTICAST_IF;
425 case SocketOptionName_MulticastTimeToLive:
426 *system_name = IP_MULTICAST_TTL;
428 case SocketOptionName_MulticastLoopback:
429 *system_name = IP_MULTICAST_LOOP;
431 case SocketOptionName_AddMembership:
432 *system_name = IP_ADD_MEMBERSHIP;
434 case SocketOptionName_DropMembership:
435 *system_name = IP_DROP_MEMBERSHIP;
437 #ifdef HAVE_IP_PKTINFO
438 case SocketOptionName_PacketInformation:
439 *system_name = IP_PKTINFO;
441 #endif /* HAVE_IP_PKTINFO */
442 case SocketOptionName_DontFragment:
443 case SocketOptionName_AddSourceMembership:
444 case SocketOptionName_DropSourceMembership:
445 case SocketOptionName_BlockSource:
446 case SocketOptionName_UnblockSource:
447 /* Can't figure out how to map these, so fall
451 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
457 case SocketOptionLevel_IPv6:
459 *system_level = SOL_IPV6;
462 static int cached = 0;
466 struct protoent *pent;
468 pent = getprotobyname ("IPV6");
469 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
473 *system_level = proto;
475 #endif /* HAVE_SOL_IPV6 */
478 case SocketOptionName_IpTimeToLive:
479 *system_name = IPV6_UNICAST_HOPS;
481 case SocketOptionName_MulticastInterface:
482 *system_name = IPV6_MULTICAST_IF;
484 case SocketOptionName_MulticastTimeToLive:
485 *system_name = IPV6_MULTICAST_HOPS;
487 case SocketOptionName_MulticastLoopback:
488 *system_name = IPV6_MULTICAST_LOOP;
490 case SocketOptionName_AddMembership:
491 *system_name = IPV6_JOIN_GROUP;
493 case SocketOptionName_DropMembership:
494 *system_name = IPV6_LEAVE_GROUP;
496 case SocketOptionName_PacketInformation:
497 #ifdef HAVE_IPV6_PKTINFO
498 *system_name = IPV6_PKTINFO;
501 case SocketOptionName_HeaderIncluded:
502 case SocketOptionName_IPOptions:
503 case SocketOptionName_TypeOfService:
504 case SocketOptionName_DontFragment:
505 case SocketOptionName_AddSourceMembership:
506 case SocketOptionName_DropSourceMembership:
507 case SocketOptionName_BlockSource:
508 case SocketOptionName_UnblockSource:
509 /* Can't figure out how to map these, so fall
513 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
517 break; /* SocketOptionLevel_IPv6 */
520 case SocketOptionLevel_Tcp:
522 *system_level = SOL_TCP;
525 static int cached = 0;
529 struct protoent *pent;
531 pent = getprotobyname ("TCP");
532 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
536 *system_level = proto;
538 #endif /* HAVE_SOL_TCP */
541 case SocketOptionName_NoDelay:
542 *system_name = TCP_NODELAY;
545 /* The documentation is talking complete
546 * bollocks here: rfc-1222 is titled
547 * 'Advancing the NSFNET Routing Architecture'
548 * and doesn't mention either of the words
549 * "expedite" or "urgent".
551 case SocketOptionName_BsdUrgent:
552 case SocketOptionName_Expedited:
555 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
560 case SocketOptionLevel_Udp:
561 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
564 case SocketOptionName_NoChecksum:
565 case SocketOptionName_ChecksumCoverage:
567 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
574 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
581 #define STASH_SYS_ASS(this) \
582 if(system_assembly == NULL) { \
583 system_assembly=mono_image_loaded ("System"); \
584 if (!system_assembly) { \
585 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL); \
586 if (!sa) g_assert_not_reached (); \
587 else {system_assembly = mono_assembly_get_image (sa);} \
591 static MonoImage *system_assembly=NULL;
595 static gint32 get_family_hint(void)
597 MonoClass *socket_class;
598 MonoClassField *ipv6_field, *ipv4_field;
599 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
602 socket_class = mono_class_from_name (system_assembly,
603 "System.Net.Sockets", "Socket");
604 ipv4_field = mono_class_get_field_from_name (socket_class,
606 ipv6_field = mono_class_get_field_from_name (socket_class,
608 vtable = mono_class_vtable (mono_domain_get (), socket_class);
610 mono_field_static_get_value(vtable, ipv4_field, &ipv4_enabled);
611 mono_field_static_get_value(vtable, ipv6_field, &ipv6_enabled);
613 if(ipv4_enabled == 1 && ipv6_enabled == 1) {
615 } else if(ipv4_enabled == 1) {
623 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
636 sock_family=convert_family(family);
637 if(sock_family==-1) {
638 *error = WSAEAFNOSUPPORT;
642 sock_proto=convert_proto(proto);
644 *error = WSAEPROTONOSUPPORT;
648 sock_type=convert_type(type);
650 *error = WSAESOCKTNOSUPPORT;
654 sock = _wapi_socket (sock_family, sock_type, sock_proto,
655 NULL, 0, WSA_FLAG_OVERLAPPED);
657 if(sock==INVALID_SOCKET) {
658 *error = WSAGetLastError ();
662 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
663 return (GUINT_TO_POINTER (sock));
667 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
668 return (GUINT_TO_POINTER (sock));
672 #ifndef PLATFORM_WIN32
673 /* .net seems to set this by default for SOCK_STREAM,
674 * not for SOCK_DGRAM (see bug #36322)
676 * It seems winsock has a rather different idea of what
677 * SO_REUSEADDR means. If it's set, then a new socket can be
678 * bound over an existing listening socket. There's a new
679 * windows-specific option called SO_EXCLUSIVEADDRUSE but
680 * using that means the socket MUST be closed properly, or a
681 * denial of service can occur. Luckily for us, winsock
682 * behaves as though any other system would when SO_REUSEADDR
683 * is true, so we don't need to do anything else here. See
689 ret = _wapi_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof (true));
690 if(ret==SOCKET_ERROR) {
691 *error = WSAGetLastError ();
699 return(GUINT_TO_POINTER (sock));
702 /* FIXME: the SOCKET parameter (here and in other functions in this
703 * file) is really an IntPtr which needs to be converted to a guint32.
705 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
711 g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
716 /* Clear any pending work item from this socket if the underlying
717 * polling system does not notify when the socket is closed */
718 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
722 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
727 g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
730 return(WSAGetLastError());
733 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
743 ret=ioctlsocket(sock, FIONREAD, &amount);
744 if(ret==SOCKET_ERROR) {
745 *error = WSAGetLastError ();
752 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
763 * block == TRUE/FALSE means we will block/not block.
764 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
768 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
769 if(ret==SOCKET_ERROR) {
770 *error = WSAGetLastError ();
774 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
783 newsock = _wapi_accept (sock, NULL, 0);
784 if(newsock==INVALID_SOCKET) {
785 *error = WSAGetLastError ();
789 return(GUINT_TO_POINTER (newsock));
792 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
802 ret = _wapi_listen (sock, backlog);
803 if(ret==SOCKET_ERROR) {
804 *error = WSAGetLastError ();
808 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
809 int sa_size, gint32 *error)
811 MonoDomain *domain = mono_domain_get ();
812 MonoObject *sockaddr_obj;
813 MonoClass *sockaddr_class;
814 MonoClassField *field;
816 MonoAddressFamily family;
818 /* Build a System.Net.SocketAddress object instance */
819 sockaddr_class=mono_class_from_name(system_assembly, "System.Net", "SocketAddress");
820 sockaddr_obj=mono_object_new(domain, sockaddr_class);
822 /* Locate the SocketAddress data buffer in the object */
823 field=mono_class_get_field_from_name(sockaddr_class, "data");
825 /* Make sure there is space for the family and size bytes */
827 if (saddr->sa_family == AF_UNIX) {
828 /* sa_len includes the entire sockaddr size, so we don't need the
829 * N bytes (sizeof (unsigned short)) of the family. */
830 data=mono_array_new(domain, mono_get_byte_class (), sa_size);
834 /* May be the +2 here is too conservative, as sa_len returns
835 * the length of the entire sockaddr_in/in6, including
836 * sizeof (unsigned short) of the family */
837 data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
840 /* The data buffer is laid out as follows:
841 * bytes 0 and 1 are the address family
842 * bytes 2 and 3 are the port info
843 * the rest is the address info
846 family=convert_to_mono_family(saddr->sa_family);
847 if(family==AddressFamily_Unknown) {
848 *error = WSAEAFNOSUPPORT;
852 mono_array_set(data, guint8, 0, family & 0x0FF);
853 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
855 if(saddr->sa_family==AF_INET) {
856 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
857 guint16 port=ntohs(sa_in->sin_port);
858 guint32 address=ntohl(sa_in->sin_addr.s_addr);
861 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
864 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
865 mono_array_set(data, guint8, 3, (port) & 0xff);
866 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
867 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
868 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
869 mono_array_set(data, guint8, 7, (address) & 0xff);
871 mono_field_set_value (sockaddr_obj, field, data);
873 return(sockaddr_obj);
875 } else if (saddr->sa_family == AF_INET6) {
876 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
879 guint16 port=ntohs(sa_in->sin6_port);
882 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
885 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
886 mono_array_set(data, guint8, 3, (port) & 0xff);
888 for(i=0; i<16; i++) {
889 mono_array_set(data, guint8, 8+i,
890 sa_in->sin6_addr.s6_addr[i]);
893 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
894 mono_array_set(data, guint8, 25,
895 (sa_in->sin6_scope_id >> 8) & 0xff);
896 mono_array_set(data, guint8, 26,
897 (sa_in->sin6_scope_id >> 16) & 0xff);
898 mono_array_set(data, guint8, 27,
899 (sa_in->sin6_scope_id >> 24) & 0xff);
901 mono_field_set_value (sockaddr_obj, field, data);
903 return(sockaddr_obj);
906 } else if (saddr->sa_family == AF_UNIX) {
909 for (i = 0; i < sa_size; i++) {
910 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
913 mono_field_set_value (sockaddr_obj, field, data);
918 *error = WSAEAFNOSUPPORT;
923 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
925 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
934 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
936 if(ret==SOCKET_ERROR) {
937 *error = WSAGetLastError ();
942 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));
945 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
949 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
951 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
960 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
962 if(ret==SOCKET_ERROR) {
963 *error = WSAGetLastError ();
968 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));
971 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
975 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
979 MonoClassField *field;
984 /* Dig the SocketAddress data buffer out of the object */
985 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
986 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
988 /* The data buffer is laid out as follows:
989 * byte 0 is the address family low byte
990 * byte 1 is the address family high byte
992 * bytes 2 and 3 are the port info
993 * the rest is the address info
995 * the rest is the file name
997 len = mono_array_length (data);
999 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1002 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1003 if (family == AF_INET) {
1004 struct sockaddr_in *sa;
1009 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1012 sa = g_new0 (struct sockaddr_in, 1);
1013 port = (mono_array_get (data, guint8, 2) << 8) +
1014 mono_array_get (data, guint8, 3);
1015 address = (mono_array_get (data, guint8, 4) << 24) +
1016 (mono_array_get (data, guint8, 5) << 16 ) +
1017 (mono_array_get (data, guint8, 6) << 8) +
1018 mono_array_get (data, guint8, 7);
1020 sa->sin_family = family;
1021 sa->sin_addr.s_addr = htonl (address);
1022 sa->sin_port = htons (port);
1024 *sa_size = sizeof(struct sockaddr_in);
1025 return((struct sockaddr *)sa);
1028 } else if (family == AF_INET6) {
1029 struct sockaddr_in6 *sa;
1035 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1038 sa = g_new0 (struct sockaddr_in6, 1);
1039 port = mono_array_get (data, guint8, 3) +
1040 (mono_array_get (data, guint8, 2) << 8);
1041 scopeid = mono_array_get (data, guint8, 24) +
1042 (mono_array_get (data, guint8, 25) << 8) +
1043 (mono_array_get (data, guint8, 26) << 16) +
1044 (mono_array_get (data, guint8, 27) << 24);
1046 sa->sin6_family = family;
1047 sa->sin6_port = htons (port);
1048 sa->sin6_scope_id = scopeid;
1050 for(i=0; i<16; i++) {
1051 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1054 *sa_size = sizeof(struct sockaddr_in6);
1055 return((struct sockaddr *)sa);
1057 #ifdef HAVE_SYS_UN_H
1058 } else if (family == AF_UNIX) {
1059 struct sockaddr_un *sock_un;
1062 /* Need a byte for the '\0' terminator/prefix, and the first
1063 * two bytes hold the SocketAddress family
1065 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1066 mono_raise_exception (mono_get_exception_index_out_of_range ());
1069 sock_un = g_new0 (struct sockaddr_un, 1);
1071 sock_un->sun_family = family;
1072 for (i = 0; i < len - 2; i++) {
1073 sock_un->sun_path [i] = mono_array_get (data, guint8,
1079 return (struct sockaddr *)sock_un;
1082 *error = WSAEAFNOSUPPORT;
1087 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1089 struct sockaddr *sa;
1093 MONO_ARCH_SAVE_REGS;
1097 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1103 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));
1106 ret = _wapi_bind (sock, sa, sa_size);
1107 if(ret==SOCKET_ERROR) {
1108 *error = WSAGetLastError ();
1121 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1122 gint timeout, gint32 *error)
1127 struct timeval *tvptr;
1131 MONO_ARCH_SAVE_REGS;
1133 start = time (NULL);
1137 _wapi_FD_SET (sock, &fds);
1139 divvy = div (timeout, 1000000);
1140 tv.tv_sec = divvy.quot;
1141 tv.tv_usec = divvy.rem;
1147 if (mode == SelectModeRead) {
1148 ret = _wapi_select (0, &fds, NULL, NULL, tvptr);
1149 } else if (mode == SelectModeWrite) {
1150 ret = _wapi_select (0, NULL, &fds, NULL, tvptr);
1151 } else if (mode == SelectModeError) {
1152 ret = _wapi_select (0, NULL, NULL, &fds, tvptr);
1154 g_assert_not_reached ();
1157 if (timeout > 0 && ret < 0) {
1159 int sec = time (NULL) - start;
1161 timeout -= sec * 1000;
1167 } while ((ret == SOCKET_ERROR) && (*error == WSAGetLastError ()) == WSAEINTR);
1169 return (ret != SOCKET_ERROR && _wapi_FD_ISSET (sock, &fds));
1172 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1174 struct sockaddr *sa;
1178 MONO_ARCH_SAVE_REGS;
1182 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1188 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));
1191 ret = _wapi_connect (sock, sa, sa_size);
1192 if(ret==SOCKET_ERROR) {
1193 *error = WSAGetLastError ();
1199 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1206 MONO_ARCH_SAVE_REGS;
1210 alen = mono_array_length (buffer);
1211 if (offset > alen - count) {
1215 buf=mono_array_addr(buffer, guchar, offset);
1217 recvflags = convert_socketflags (flags);
1218 if (recvflags == -1) {
1219 *error = WSAEOPNOTSUPP;
1223 ret = _wapi_recv (sock, buf, count, recvflags);
1224 if(ret==SOCKET_ERROR) {
1225 *error = WSAGetLastError ();
1232 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1238 struct sockaddr *sa;
1241 MONO_ARCH_SAVE_REGS;
1245 alen = mono_array_length (buffer);
1246 if (offset > alen - count) {
1250 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1255 buf=mono_array_addr(buffer, guchar, offset);
1257 recvflags = convert_socketflags (flags);
1258 if (recvflags == -1) {
1259 *error = WSAEOPNOTSUPP;
1263 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1264 if(ret==SOCKET_ERROR) {
1266 *error = WSAGetLastError ();
1270 /* If we didn't get a socket size, then we're probably a
1271 * connected connection-oriented socket and the stack hasn't
1272 * returned the remote address. All we can do is return null.
1275 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1284 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1291 MONO_ARCH_SAVE_REGS;
1295 alen = mono_array_length (buffer);
1296 if (offset > alen - count) {
1301 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1304 buf=mono_array_addr(buffer, guchar, offset);
1307 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1310 sendflags = convert_socketflags (flags);
1311 if (sendflags == -1) {
1312 *error = WSAEOPNOTSUPP;
1316 ret = _wapi_send (sock, buf, count, sendflags);
1317 if(ret==SOCKET_ERROR) {
1318 *error = WSAGetLastError ();
1325 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1331 struct sockaddr *sa;
1334 MONO_ARCH_SAVE_REGS;
1338 alen = mono_array_length (buffer);
1339 if (offset > alen - count) {
1343 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1349 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1352 buf=mono_array_addr(buffer, guchar, offset);
1355 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1358 sendflags = convert_socketflags (flags);
1359 if (sendflags == -1) {
1360 *error = WSAEOPNOTSUPP;
1364 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1365 if(ret==SOCKET_ERROR) {
1366 *error = WSAGetLastError ();
1374 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1377 MonoClassField *field;
1379 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1380 sock=*(SOCKET *)(((char *)sockobj)+field->offset);
1385 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1386 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1388 MonoThread *thread = NULL;
1395 MonoClass *sock_arr_class;
1399 MONO_ARCH_SAVE_REGS;
1401 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1402 count = mono_array_length (*sockets);
1403 nfds = count - 3; /* NULL separators */
1404 pfds = g_new0 (mono_pollfd, nfds);
1406 for (i = 0; i < count; i++) {
1407 obj = mono_array_get (*sockets, MonoObject *, i);
1414 /* The socket array was bogus */
1420 pfds [idx].fd = GPOINTER_TO_INT (Socket_to_SOCKET (obj));
1421 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1425 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1426 start = time (NULL);
1429 ret = mono_poll (pfds, nfds, timeout);
1430 if (timeout > 0 && ret < 0) {
1432 int sec = time (NULL) - start;
1434 timeout -= sec * 1000;
1440 if (ret == -1 && errno == EINTR) {
1443 thread = mono_thread_current ();
1445 mono_monitor_enter (thread->synch_lock);
1446 leave = ((thread->state & ThreadState_AbortRequested) != 0 ||
1447 (thread->state & ThreadState_StopRequested) != 0);
1448 mono_monitor_exit (thread->synch_lock);
1454 /* Suspend requested? */
1455 mono_thread_interruption_checkpoint ();
1459 } while (ret == -1 && errno == EINTR);
1462 #ifdef PLATFORM_WIN32
1463 *error = WSAGetLastError ();
1465 *error = errno_to_WSA (errno, __func__);
1477 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1478 ret += 3; /* space for the NULL delimiters */
1479 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, (guint32*)&ret, NULL);
1482 for (i = 0; i < count && ret > 0; i++) {
1485 obj = mono_array_get (*sockets, MonoObject *, i);
1492 pfd = &pfds [i - mode];
1493 if (pfd->revents == 0)
1497 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1498 mono_array_setref (socks, idx++, obj);
1499 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1500 mono_array_setref (socks, idx++, obj);
1501 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1502 mono_array_setref (socks, idx++, obj);
1510 static MonoObject* int_to_object (MonoDomain *domain, int val)
1512 return mono_value_box (domain, mono_get_int32_class (), &val);
1516 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1522 socklen_t valsize=sizeof(val);
1523 struct linger linger;
1524 socklen_t lingersize=sizeof(linger);
1526 socklen_t time_ms_size = sizeof (time_ms);
1529 socklen_t credsize = sizeof(cred);
1531 MonoDomain *domain=mono_domain_get();
1533 MonoClass *obj_class;
1534 MonoClassField *field;
1536 MONO_ARCH_SAVE_REGS;
1540 ret=convert_sockopt_level_and_name(level, name, &system_level,
1543 *error = WSAENOPROTOOPT;
1547 /* No need to deal with MulticastOption names here, because
1548 * you cant getsockopt AddMembership or DropMembership (the
1549 * int getsockopt will error, causing an exception)
1552 case SocketOptionName_Linger:
1553 case SocketOptionName_DontLinger:
1554 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1558 case SocketOptionName_SendTimeout:
1559 case SocketOptionName_ReceiveTimeout:
1560 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1564 case SocketOptionName_PeerCred:
1565 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1571 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1575 if(ret==SOCKET_ERROR) {
1576 *error = WSAGetLastError ();
1581 case SocketOptionName_Linger:
1582 /* build a System.Net.Sockets.LingerOption */
1583 obj_class=mono_class_from_name(system_assembly,
1584 "System.Net.Sockets",
1586 obj=mono_object_new(domain, obj_class);
1588 /* Locate and set the fields "bool enabled" and "int
1591 field=mono_class_get_field_from_name(obj_class, "enabled");
1592 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1594 field=mono_class_get_field_from_name(obj_class, "seconds");
1595 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1599 case SocketOptionName_DontLinger:
1600 /* construct a bool int in val - true if linger is off */
1601 obj = int_to_object (domain, !linger.l_onoff);
1604 case SocketOptionName_SendTimeout:
1605 case SocketOptionName_ReceiveTimeout:
1606 obj = int_to_object (domain, time_ms);
1610 case SocketOptionName_PeerCred:
1612 /* build a Mono.Posix.PeerCred+PeerCredData if
1615 static MonoImage *mono_posix_image = NULL;
1616 MonoPeerCredData *cred_data;
1618 if (mono_posix_image == NULL) {
1619 mono_posix_image=mono_image_loaded ("Mono.Posix");
1620 if (!mono_posix_image) {
1621 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1623 *error = WSAENOPROTOOPT;
1626 mono_posix_image = mono_assembly_get_image (sa);
1631 obj_class = mono_class_from_name(mono_posix_image,
1634 obj = mono_object_new(domain, obj_class);
1635 cred_data = (MonoPeerCredData *)obj;
1636 cred_data->pid = cred.pid;
1637 cred_data->uid = cred.uid;
1638 cred_data->gid = cred.gid;
1644 obj = int_to_object (domain, val);
1650 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1658 MONO_ARCH_SAVE_REGS;
1662 ret=convert_sockopt_level_and_name(level, name, &system_level,
1665 *error = WSAENOPROTOOPT;
1669 valsize=mono_array_length(*byte_val);
1670 buf=mono_array_addr(*byte_val, guchar, 0);
1672 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1673 if(ret==SOCKET_ERROR) {
1674 *error = WSAGetLastError ();
1678 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1679 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1681 struct in_addr inaddr;
1682 MonoClassField *field;
1684 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
1686 /* No idea why .net uses a 64bit type to hold a 32bit value...
1688 * Internal value of IPAddess is in Network Order, there is no need
1689 * to call htonl here.
1691 inaddr.s_addr=(guint32)*(guint64 *)(((char *)ipaddr)+field->offset);
1698 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1700 struct in6_addr in6addr;
1701 MonoClassField *field;
1705 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
1706 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1708 /* Solaris has only the 8 bit version. */
1710 for(i=0; i<8; i++) {
1711 guint16 s = mono_array_get (data, guint16, i);
1712 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1713 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1717 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1721 #endif /* AF_INET6 */
1723 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)
1734 #ifdef HAVE_SOL_IPV6
1735 sol_ipv6 = SOL_IPV6;
1738 struct protoent *pent;
1739 pent = getprotobyname ("ipv6");
1740 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
1748 struct protoent *pent;
1749 pent = getprotobyname ("ip");
1750 sol_ip = (pent != NULL) ? pent->p_proto : 0;
1753 #endif /* AF_INET6 */
1755 MONO_ARCH_SAVE_REGS;
1757 ret=convert_sockopt_level_and_name(level, name, &system_level,
1760 *error = WSAENOPROTOOPT;
1764 /* Only one of obj_val, byte_val or int_val has data */
1766 MonoClassField *field;
1767 struct linger linger;
1771 case SocketOptionName_DontLinger:
1774 valsize=sizeof(linger);
1775 ret = _wapi_setsockopt (sock, system_level,
1776 system_name, &linger, valsize);
1779 case SocketOptionName_Linger:
1780 /* Dig out "bool enabled" and "int seconds"
1783 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
1784 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
1785 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
1786 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
1788 valsize=sizeof(linger);
1789 ret = _wapi_setsockopt (sock, system_level,
1790 system_name, &linger, valsize);
1792 case SocketOptionName_AddMembership:
1793 case SocketOptionName_DropMembership:
1794 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1796 MonoObject *address = NULL;
1799 if(system_level == sol_ipv6) {
1800 struct ipv6_mreq mreq6;
1805 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1806 address = *(gpointer *)(((char *)obj_val) + field->offset);
1809 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
1812 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
1813 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
1815 ret = _wapi_setsockopt (sock, system_level,
1816 system_name, &mreq6,
1818 } else if(system_level == sol_ip)
1819 #endif /* AF_INET6 */
1821 #ifdef HAVE_STRUCT_IP_MREQN
1822 struct ip_mreqn mreq = {{0}};
1824 struct ip_mreq mreq = {{0}};
1825 #endif /* HAVE_STRUCT_IP_MREQN */
1827 /* pain! MulticastOption holds two IPAddress
1828 * members, so I have to dig the value out of
1831 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1832 address = *(gpointer *)(((char *)obj_val) + field->offset);
1834 /* address might not be defined and if so, set the address to ADDR_ANY.
1837 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
1840 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
1841 address = *(gpointer *)(((char *)obj_val) + field->offset);
1843 #ifdef HAVE_STRUCT_IP_MREQN
1845 mreq.imr_address = ipaddress_to_struct_in_addr (address);
1849 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
1851 #endif /* HAVE_STRUCT_IP_MREQN */
1853 ret = _wapi_setsockopt (sock, system_level,
1859 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
1861 /* Cause an exception to be thrown */
1865 } else if (byte_val!=NULL) {
1866 int valsize=mono_array_length(byte_val);
1867 guchar *buf=mono_array_addr(byte_val, guchar, 0);
1869 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
1870 if(ret==SOCKET_ERROR) {
1871 *error = WSAGetLastError ();
1875 /* ReceiveTimeout/SendTimeout get here */
1876 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
1879 if(ret==SOCKET_ERROR) {
1880 *error = WSAGetLastError ();
1884 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
1890 MONO_ARCH_SAVE_REGS;
1894 /* Currently, the values for how (recv=0, send=1, both=2) match
1897 ret = _wapi_shutdown (sock, how);
1898 if(ret==SOCKET_ERROR) {
1899 *error = WSAGetLastError ();
1904 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
1906 MonoArray *output, gint32 *error)
1908 glong output_bytes = 0;
1909 gchar *i_buffer, *o_buffer;
1913 MONO_ARCH_SAVE_REGS;
1917 if (code == FIONBIO) {
1918 /* Invalid command. Must use Socket.Blocking */
1922 if (input == NULL) {
1926 i_buffer = mono_array_addr (input, gchar, 0);
1927 i_len = mono_array_length (input);
1930 if (output == NULL) {
1934 o_buffer = mono_array_addr (output, gchar, 0);
1935 o_len = mono_array_length (output);
1938 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
1939 if (ret == SOCKET_ERROR) {
1940 *error = WSAGetLastError ();
1944 return (gint) output_bytes;
1947 #ifdef HAVE_SIOCGIFCONF
1949 is_loopback (int family, void *ad)
1951 char *ptr = (char *) ad;
1953 if (family == AF_INET) {
1954 return (ptr [0] == 127);
1958 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
1965 get_local_ips (int family, int *nips)
1967 int addr_size, offset, fd, i, count;
1968 int max_ifaces = 50; /* 50 interfaces should be enough... */
1971 struct ifreq iflags;
1972 char *result, *tmp_ptr;
1973 gboolean ignore_loopback = FALSE;
1976 if (family == AF_INET) {
1977 addr_size = sizeof (struct in_addr);
1978 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
1980 } else if (family == AF_INET6) {
1981 addr_size = sizeof (struct in6_addr);
1982 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
1988 fd = socket (family, SOCK_STREAM, 0);
1990 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
1991 ifc.ifc_buf = g_malloc (ifc.ifc_len);
1992 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
1994 g_free (ifc.ifc_buf);
1998 count = ifc.ifc_len / sizeof (struct ifreq);
2001 g_free (ifc.ifc_buf);
2006 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2007 strcpy (iflags.ifr_name, ifr->ifr_name);
2008 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2012 if ((iflags.ifr_flags & IFF_UP) == 0) {
2013 ifr->ifr_name [0] = '\0';
2017 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2018 ignore_loopback = TRUE;
2023 result = g_malloc (addr_size * count);
2025 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2026 if (ifr->ifr_name [0] == '\0') {
2031 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2036 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2037 tmp_ptr += addr_size;
2040 g_free (ifc.ifc_buf);
2045 get_local_ips (int family, int *nips)
2051 #endif /* HAVE_SIOCGIFCONF */
2054 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2055 MonoArray **h_aliases,
2056 MonoArray **h_addr_list,
2057 gboolean add_local_ips)
2059 MonoDomain *domain = mono_domain_get ();
2061 struct in_addr *local_in = NULL;
2064 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2068 *h_name=mono_string_new(domain, he->h_name);
2071 while(he->h_aliases[i]!=NULL) {
2075 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2077 while(he->h_aliases[i]!=NULL) {
2080 alias=mono_string_new(domain, he->h_aliases[i]);
2081 mono_array_setref (*h_aliases, i, alias);
2085 if (add_local_ips) {
2086 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2088 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2089 for (i = 0; i < nlocal_in; i++) {
2090 MonoString *addr_string;
2091 char addr [16], *ptr;
2093 ptr = (char *) &local_in [i];
2094 g_snprintf(addr, 16, "%u.%u.%u.%u",
2095 (unsigned char) ptr [0],
2096 (unsigned char) ptr [1],
2097 (unsigned char) ptr [2],
2098 (unsigned char) ptr [3]);
2100 addr_string = mono_string_new (domain, addr);
2101 mono_array_setref (*h_addr_list, i, addr_string);
2109 if (nlocal_in == 0) {
2111 while (he->h_addr_list[i]!=NULL) {
2115 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2117 while(he->h_addr_list[i]!=NULL) {
2118 MonoString *addr_string;
2121 g_snprintf(addr, 16, "%u.%u.%u.%u",
2122 (unsigned char)he->h_addr_list[i][0],
2123 (unsigned char)he->h_addr_list[i][1],
2124 (unsigned char)he->h_addr_list[i][2],
2125 (unsigned char)he->h_addr_list[i][3]);
2127 addr_string=mono_string_new(domain, addr);
2128 mono_array_setref (*h_addr_list, i, addr_string);
2137 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2138 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2139 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2141 MonoDomain *domain = mono_domain_get ();
2142 int i, host_count, host_index, family_hint;
2143 struct in_addr *local_in = NULL;
2145 struct in6_addr *local_in6 = NULL;
2147 gboolean from_local = FALSE;
2149 family_hint = get_family_hint ();
2151 if(he1 == NULL && he2 == NULL) {
2156 * Check if address length and family are correct
2158 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2162 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2167 * Get the aliases and host name from he1 or he2 whichever is
2168 * not null, if he1 is not null then take aliases from he1
2170 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2171 family_hint == PF_INET)) {
2172 *h_name=mono_string_new (domain, he1->h_name);
2175 while(he1->h_aliases[i]!=NULL) {
2179 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2182 while(he1->h_aliases[i]!=NULL) {
2185 alias=mono_string_new (domain, he1->h_aliases[i]);
2186 mono_array_setref (*h_aliases, i, alias);
2189 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2190 family_hint == PF_INET6)) {
2191 *h_name=mono_string_new (domain, he2->h_name);
2194 while(he2->h_aliases [i] != NULL) {
2198 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2201 while(he2->h_aliases[i]!=NULL) {
2204 alias=mono_string_new (domain, he2->h_aliases[i]);
2205 mono_array_setref (*h_aliases, i, alias);
2213 * Count the number of addresses in he1 + he2
2216 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2217 family_hint == PF_INET)) {
2219 while(he1->h_addr_list[i]!=NULL) {
2225 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2226 family_hint == PF_INET6)) {
2228 while(he2->h_addr_list[i]!=NULL) {
2238 if (add_local_ips) {
2239 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2240 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2242 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2243 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2245 if (nlocal_in || nlocal_in6) {
2247 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2248 nlocal_in + nlocal_in6);
2252 for (n = 0; n < nlocal_in6; n++) {
2253 MonoString *addr_string;
2255 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2257 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2260 addr_string = mono_string_new (domain, addr);
2261 mono_array_setref (*h_addr_list, host_index, addr_string);
2269 for (n = 0; n < nlocal_in; n++) {
2270 MonoString *addr_string;
2272 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2274 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2277 addr_string = mono_string_new (domain, addr);
2278 mono_array_setref (*h_addr_list, host_index, addr_string);
2292 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2294 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2295 family_hint == PF_INET6)) {
2297 while(he2->h_addr_list[i] != NULL) {
2298 MonoString *addr_string;
2300 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2302 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2306 addr_string = mono_string_new (domain, addr);
2307 mono_array_setref (*h_addr_list, host_index, addr_string);
2314 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2315 family_hint == PF_INET)) {
2317 while(he1->h_addr_list[i] != NULL) {
2318 MonoString *addr_string;
2320 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2322 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2326 addr_string=mono_string_new (domain, addr);
2327 mono_array_setref (*h_addr_list, host_index, addr_string);
2338 #if defined(AF_INET6)
2340 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2341 MonoArray **h_aliases,
2342 MonoArray **h_addr_list,
2343 gboolean add_local_ips)
2346 struct addrinfo *ai = NULL;
2347 struct in_addr *local_in = NULL;
2349 struct in6_addr *local_in6 = NULL;
2353 MonoDomain *domain = mono_domain_get ();
2356 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2357 if (add_local_ips) {
2358 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2359 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2360 if (nlocal_in || nlocal_in6) {
2361 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2363 MonoString *addr_string;
2367 for (i = 0; i < nlocal_in; i++) {
2368 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2369 addr_string = mono_string_new (domain, addr);
2370 mono_array_setref (*h_addr_list, addr_index, addr_string);
2376 MonoString *addr_string;
2381 for (i = 0; i < nlocal_in6; i++) {
2382 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2384 addr_string = mono_string_new (domain, addr);
2385 mono_array_setref (*h_addr_list, addr_index, addr_string);
2394 freeaddrinfo (info);
2403 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2404 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2410 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2412 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2413 MonoString *addr_string;
2415 char buffer [48]; /* Max. size for IPv6 */
2417 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2421 if(ai->ai_family == PF_INET) {
2422 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2424 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2428 addr_string=mono_string_new(domain, buffer);
2430 addr_string=mono_string_new(domain, "");
2433 mono_array_setref (*h_addr_list, addr_index, addr_string);
2435 if(!i && ai->ai_canonname != NULL) {
2436 *h_name=mono_string_new(domain, ai->ai_canonname);
2451 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2453 gboolean add_local_ips = FALSE;
2454 #ifdef HAVE_SIOCGIFCONF
2455 gchar this_hostname [256];
2457 #if !defined(HAVE_GETHOSTBYNAME2_R)
2458 struct addrinfo *info = NULL, hints;
2461 MONO_ARCH_SAVE_REGS;
2463 hostname=mono_string_to_utf8 (host);
2464 #ifdef HAVE_SIOCGIFCONF
2465 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2466 if (!strcmp (hostname, this_hostname))
2467 add_local_ips = TRUE;
2471 memset(&hints, 0, sizeof(hints));
2472 hints.ai_family = get_family_hint ();
2473 hints.ai_socktype = SOCK_STREAM;
2474 hints.ai_flags = AI_CANONNAME;
2476 if (getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2482 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2484 struct hostent he1,*hp1, he2, *hp2;
2485 int buffer_size1, buffer_size2;
2486 char *buffer1, *buffer2;
2488 gboolean return_value;
2491 MONO_ARCH_SAVE_REGS;
2493 hostname=mono_string_to_utf8 (host);
2495 #ifdef HAVE_SIOCGIFCONF
2496 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2497 if (!strcmp (hostname, this_hostname))
2498 add_local_ips = TRUE;
2504 buffer1 = g_malloc0(buffer_size1);
2505 buffer2 = g_malloc0(buffer_size2);
2507 while (gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2508 &hp1, &herr) == ERANGE) {
2510 buffer1 = g_realloc(buffer1, buffer_size1);
2515 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2516 buffer_size2, &hp2, &herr) == ERANGE) {
2518 buffer2 = g_realloc(buffer2, buffer_size2);
2524 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2525 h_addr_list, add_local_ips);
2531 return(return_value);
2532 #endif /* HAVE_GETHOSTBYNAME2_R */
2534 #else /* AF_INET6 */
2535 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2539 gboolean add_local_ips = FALSE;
2540 #ifdef HAVE_SIOCGIFCONF
2541 guchar this_hostname [256];
2544 MONO_ARCH_SAVE_REGS;
2546 hostname=mono_string_to_utf8(host);
2547 #ifdef HAVE_SIOCGIFCONF
2548 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2549 if (!strcmp (hostname, this_hostname))
2550 add_local_ips = TRUE;
2554 he = _wapi_gethostbyname (hostname);
2561 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2563 #endif /* AF_INET6 */
2565 #ifndef HAVE_INET_PTON
2567 inet_pton (int family, const char *address, void *inaddrp)
2569 if (family == AF_INET) {
2570 #ifdef HAVE_INET_ATON
2571 struct in_addr inaddr;
2573 if (!inet_aton (address, &inaddr))
2576 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2579 /* assume the system has inet_addr(), if it doesn't
2580 have that we're pretty much screwed... */
2583 if (!strcmp (address, "255.255.255.255")) {
2584 /* special-case hack */
2585 inaddr = 0xffffffff;
2587 inaddr = inet_addr (address);
2589 #define INADDR_NONE ((in_addr_t) -1)
2591 if (inaddr == INADDR_NONE)
2595 memcpy (inaddrp, &inaddr, sizeof (guint32));
2597 #endif /* HAVE_INET_ATON */
2602 #endif /* !HAVE_INET_PTON */
2604 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2609 struct sockaddr_in saddr;
2610 struct sockaddr_in6 saddr6;
2611 struct addrinfo *info = NULL, hints;
2613 char hostname[1024] = {0};
2615 struct in_addr inaddr;
2619 MONO_ARCH_SAVE_REGS;
2621 address = mono_string_to_utf8 (addr);
2624 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2625 /* Maybe an ipv6 address */
2626 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2632 saddr6.sin6_family = AF_INET6;
2637 saddr.sin_family = AF_INET;
2641 if(family == AF_INET) {
2642 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2643 hostname, sizeof(hostname), NULL, 0,
2644 NI_NAMEREQD) != 0) {
2647 } else if(family == AF_INET6) {
2648 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2649 hostname, sizeof(hostname), NULL, 0,
2650 NI_NAMEREQD) != 0) {
2655 memset (&hints, 0, sizeof(hints));
2656 hints.ai_family = get_family_hint ();
2657 hints.ai_socktype = SOCK_STREAM;
2658 hints.ai_flags = AI_CANONNAME;
2660 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
2664 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
2666 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
2672 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
2676 return(hostent_to_IPHostEntry (he, h_name, h_aliases, h_addr_list, FALSE));
2680 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
2682 gchar hostname[256];
2685 MONO_ARCH_SAVE_REGS;
2687 ret = gethostname (hostname, sizeof (hostname));
2692 *h_name=mono_string_new(mono_domain_get (), hostname);
2697 void mono_network_init(void)
2702 err=WSAStartup(MAKEWORD(2,0), &wsadata);
2704 g_error(G_GNUC_PRETTY_FUNCTION ": Couldn't initialise networking");
2709 g_message(G_GNUC_PRETTY_FUNCTION ": Using socket library: %s", wsadata.szDescription);
2710 g_message(G_GNUC_PRETTY_FUNCTION ": Socket system status: %s", wsadata.szSystemStatus);
2714 void mono_network_cleanup(void)