2 * socket-io.c: Socket IO internal calls
5 * Dick Porter (dick@ximian.com)
7 * (C) 2001 Ximian, Inc.
18 #include <mono/metadata/object.h>
19 #include <mono/io-layer/io-layer.h>
20 #include <mono/metadata/socket-io.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/assembly.h>
23 #include <mono/metadata/appdomain.h>
24 #include <mono/metadata/threads.h>
25 /* FIXME change this code to not mess so much with the internals */
26 #include <mono/metadata/class-internals.h>
27 #include <mono/metadata/threadpool-internals.h>
30 #ifdef HAVE_SYS_IOCTL_H
31 #include <sys/ioctl.h>
40 #ifdef HAVE_SYS_FILIO_H
41 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
43 #ifdef HAVE_SYS_SOCKIO_H
44 #include <sys/sockio.h> /* defines SIOCATMARK */
50 #include "mono/io-layer/socket-wrappers.h"
53 /* This is a kludge to make this file build under cygwin:
54 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
55 * prototypes for some but not all required functions (notably
56 * inet_ntop() is missing), but the libws2_32 library is missing the
57 * actual implementations of these functions.
64 static gint32 convert_family(MonoAddressFamily mono_family)
69 case AddressFamily_Unknown:
70 case AddressFamily_ImpLink:
71 case AddressFamily_Pup:
72 case AddressFamily_Chaos:
73 case AddressFamily_Iso:
74 case AddressFamily_Ecma:
75 case AddressFamily_DataKit:
76 case AddressFamily_Ccitt:
77 case AddressFamily_DataLink:
78 case AddressFamily_Lat:
79 case AddressFamily_HyperChannel:
80 case AddressFamily_NetBios:
81 case AddressFamily_VoiceView:
82 case AddressFamily_FireFox:
83 case AddressFamily_Banyan:
84 case AddressFamily_Atm:
85 case AddressFamily_Cluster:
86 case AddressFamily_Ieee12844:
87 case AddressFamily_NetworkDesigners:
88 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
91 case AddressFamily_Unspecified:
95 case AddressFamily_Unix:
99 case AddressFamily_InterNetwork:
103 case AddressFamily_Ipx:
109 case AddressFamily_Sna:
113 case AddressFamily_DecNet:
117 case AddressFamily_AppleTalk:
121 case AddressFamily_InterNetworkV6:
126 case AddressFamily_Irda:
132 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
138 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
140 MonoAddressFamily family=AddressFamily_Unknown;
144 family=AddressFamily_Unspecified;
148 family=AddressFamily_Unix;
152 family=AddressFamily_InterNetwork;
157 family=AddressFamily_Ipx;
162 family=AddressFamily_Sna;
166 family=AddressFamily_DecNet;
170 family=AddressFamily_AppleTalk;
175 family=AddressFamily_InterNetworkV6;
181 family=AddressFamily_Irda;
185 g_warning("unknown address family 0x%x", af_family);
191 static gint32 convert_type(MonoSocketType mono_type)
196 case SocketType_Stream:
200 case SocketType_Dgram:
212 case SocketType_Seqpacket:
216 case SocketType_Unknown:
217 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
221 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
227 static gint32 convert_proto(MonoProtocolType mono_proto)
232 case ProtocolType_IP:
233 case ProtocolType_IPv6:
234 case ProtocolType_Icmp:
235 case ProtocolType_Igmp:
236 case ProtocolType_Ggp:
237 case ProtocolType_Tcp:
238 case ProtocolType_Pup:
239 case ProtocolType_Udp:
240 case ProtocolType_Idp:
241 /* These protocols are known (on my system at least) */
245 case ProtocolType_ND:
246 case ProtocolType_Raw:
247 case ProtocolType_Ipx:
248 case ProtocolType_Spx:
249 case ProtocolType_SpxII:
250 case ProtocolType_Unknown:
251 /* These protocols arent */
252 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
262 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
263 MonoSocketOptionName mono_name,
267 switch (mono_level) {
268 case SocketOptionLevel_Socket:
269 *system_level = SOL_SOCKET;
272 case SocketOptionName_DontLinger:
273 /* This is SO_LINGER, because the setsockopt
274 * internal call maps DontLinger to SO_LINGER
277 *system_name = SO_LINGER;
279 case SocketOptionName_Debug:
280 *system_name = SO_DEBUG;
283 case SocketOptionName_AcceptConnection:
284 *system_name = SO_ACCEPTCONN;
287 case SocketOptionName_ReuseAddress:
288 *system_name = SO_REUSEADDR;
290 case SocketOptionName_KeepAlive:
291 *system_name = SO_KEEPALIVE;
293 case SocketOptionName_DontRoute:
294 *system_name = SO_DONTROUTE;
296 case SocketOptionName_Broadcast:
297 *system_name = SO_BROADCAST;
299 case SocketOptionName_Linger:
300 *system_name = SO_LINGER;
302 case SocketOptionName_OutOfBandInline:
303 *system_name = SO_OOBINLINE;
305 case SocketOptionName_SendBuffer:
306 *system_name = SO_SNDBUF;
308 case SocketOptionName_ReceiveBuffer:
309 *system_name = SO_RCVBUF;
311 case SocketOptionName_SendLowWater:
312 *system_name = SO_SNDLOWAT;
314 case SocketOptionName_ReceiveLowWater:
315 *system_name = SO_RCVLOWAT;
317 case SocketOptionName_SendTimeout:
318 *system_name = SO_SNDTIMEO;
320 case SocketOptionName_ReceiveTimeout:
321 *system_name = SO_RCVTIMEO;
323 case SocketOptionName_Error:
324 *system_name = SO_ERROR;
326 case SocketOptionName_Type:
327 *system_name = SO_TYPE;
330 case SocketOptionName_PeerCred:
331 *system_name = SO_PEERCRED;
334 case SocketOptionName_ExclusiveAddressUse:
335 case SocketOptionName_UseLoopback:
336 case SocketOptionName_MaxConnections:
337 /* Can't figure out how to map these, so fall
341 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
346 case SocketOptionLevel_IP:
348 *system_level = SOL_IP;
351 static int cached = 0;
355 struct protoent *pent;
357 pent = getprotobyname ("IP");
358 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
362 *system_level = proto;
364 #endif /* HAVE_SOL_IP */
367 case SocketOptionName_IPOptions:
368 *system_name = IP_OPTIONS;
371 case SocketOptionName_HeaderIncluded:
372 *system_name = IP_HDRINCL;
376 case SocketOptionName_TypeOfService:
377 *system_name = IP_TOS;
381 case SocketOptionName_IpTimeToLive:
382 *system_name = IP_TTL;
385 case SocketOptionName_MulticastInterface:
386 *system_name = IP_MULTICAST_IF;
388 case SocketOptionName_MulticastTimeToLive:
389 *system_name = IP_MULTICAST_TTL;
391 case SocketOptionName_MulticastLoopback:
392 *system_name = IP_MULTICAST_LOOP;
394 case SocketOptionName_AddMembership:
395 *system_name = IP_ADD_MEMBERSHIP;
397 case SocketOptionName_DropMembership:
398 *system_name = IP_DROP_MEMBERSHIP;
400 #ifdef HAVE_IP_PKTINFO
401 case SocketOptionName_PacketInformation:
402 *system_name = IP_PKTINFO;
404 #endif /* HAVE_IP_PKTINFO */
405 case SocketOptionName_DontFragment:
406 case SocketOptionName_AddSourceMembership:
407 case SocketOptionName_DropSourceMembership:
408 case SocketOptionName_BlockSource:
409 case SocketOptionName_UnblockSource:
410 /* Can't figure out how to map these, so fall
414 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
420 case SocketOptionLevel_IPv6:
422 *system_level = SOL_IPV6;
425 static int cached = 0;
429 struct protoent *pent;
431 pent = getprotobyname ("IPV6");
432 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
436 *system_level = proto;
438 #endif /* HAVE_SOL_IPV6 */
441 case SocketOptionName_IpTimeToLive:
442 *system_name = IPV6_UNICAST_HOPS;
444 case SocketOptionName_MulticastInterface:
445 *system_name = IPV6_MULTICAST_IF;
447 case SocketOptionName_MulticastTimeToLive:
448 *system_name = IPV6_MULTICAST_HOPS;
450 case SocketOptionName_MulticastLoopback:
451 *system_name = IPV6_MULTICAST_LOOP;
453 case SocketOptionName_AddMembership:
454 *system_name = IPV6_JOIN_GROUP;
456 case SocketOptionName_DropMembership:
457 *system_name = IPV6_LEAVE_GROUP;
459 case SocketOptionName_PacketInformation:
460 *system_name = IPV6_PKTINFO;
462 case SocketOptionName_HeaderIncluded:
463 case SocketOptionName_IPOptions:
464 case SocketOptionName_TypeOfService:
465 case SocketOptionName_DontFragment:
466 case SocketOptionName_AddSourceMembership:
467 case SocketOptionName_DropSourceMembership:
468 case SocketOptionName_BlockSource:
469 case SocketOptionName_UnblockSource:
470 /* Can't figure out how to map these, so fall
474 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
478 break; /* SocketOptionLevel_IPv6 */
481 case SocketOptionLevel_Tcp:
483 *system_level = SOL_TCP;
486 static int cached = 0;
490 struct protoent *pent;
492 pent = getprotobyname ("TCP");
493 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
497 *system_level = proto;
499 #endif /* HAVE_SOL_TCP */
502 case SocketOptionName_NoDelay:
503 *system_name = TCP_NODELAY;
506 /* The documentation is talking complete
507 * bollocks here: rfc-1222 is titled
508 * 'Advancing the NSFNET Routing Architecture'
509 * and doesn't mention either of the words
510 * "expedite" or "urgent".
512 case SocketOptionName_BsdUrgent:
513 case SocketOptionName_Expedited:
516 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
521 case SocketOptionLevel_Udp:
522 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
525 case SocketOptionName_NoChecksum:
526 case SocketOptionName_ChecksumCoverage:
528 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
535 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
542 #define STASH_SYS_ASS(this) \
543 if(system_assembly == NULL) { \
544 system_assembly=mono_image_loaded ("System"); \
545 if (!system_assembly) { \
546 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL); \
547 if (!sa) g_assert_not_reached (); \
548 else {system_assembly = mono_assembly_get_image (sa);} \
552 static MonoImage *system_assembly=NULL;
556 static gint32 get_family_hint(void)
558 MonoClass *socket_class;
559 MonoClassField *ipv6_field, *ipv4_field;
560 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
563 socket_class = mono_class_from_name (system_assembly,
564 "System.Net.Sockets", "Socket");
565 ipv4_field = mono_class_get_field_from_name (socket_class,
567 ipv6_field = mono_class_get_field_from_name (socket_class,
569 vtable = mono_class_vtable (mono_domain_get (), socket_class);
571 mono_field_static_get_value(vtable, ipv4_field, &ipv4_enabled);
572 mono_field_static_get_value(vtable, ipv6_field, &ipv6_enabled);
574 if(ipv4_enabled == 1 && ipv6_enabled == 1) {
576 } else if(ipv4_enabled == 1) {
584 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
597 sock_family=convert_family(family);
598 if(sock_family==-1) {
599 *error = WSAEAFNOSUPPORT;
603 sock_proto=convert_proto(proto);
605 *error = WSAEPROTONOSUPPORT;
609 sock_type=convert_type(type);
611 *error = WSAESOCKTNOSUPPORT;
615 sock = _wapi_socket (sock_family, sock_type, sock_proto,
616 NULL, 0, WSA_FLAG_OVERLAPPED);
618 if(sock==INVALID_SOCKET) {
619 *error = WSAGetLastError ();
623 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
624 return (GUINT_TO_POINTER (sock));
628 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
629 return (GUINT_TO_POINTER (sock));
633 #ifndef PLATFORM_WIN32
634 /* .net seems to set this by default for SOCK_STREAM,
635 * not for SOCK_DGRAM (see bug #36322)
637 * It seems winsock has a rather different idea of what
638 * SO_REUSEADDR means. If it's set, then a new socket can be
639 * bound over an existing listening socket. There's a new
640 * windows-specific option called SO_EXCLUSIVEADDRUSE but
641 * using that means the socket MUST be closed properly, or a
642 * denial of service can occur. Luckily for us, winsock
643 * behaves as though any other system would when SO_REUSEADDR
644 * is true, so we don't need to do anything else here. See
650 ret = _wapi_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof (true));
651 if(ret==SOCKET_ERROR) {
652 *error = WSAGetLastError ();
660 return(GUINT_TO_POINTER (sock));
663 /* FIXME: the SOCKET parameter (here and in other functions in this
664 * file) is really an IntPtr which needs to be converted to a guint32.
666 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
672 g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
677 /* Clear any pending work item from this socket if the underlying
678 * polling system does not notify when the socket is closed */
679 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
683 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
688 g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
691 return(WSAGetLastError());
694 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
704 ret=ioctlsocket(sock, FIONREAD, &amount);
705 if(ret==SOCKET_ERROR) {
706 *error = WSAGetLastError ();
713 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
724 * block == TRUE/FALSE means we will block/not block.
725 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
729 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
730 if(ret==SOCKET_ERROR) {
731 *error = WSAGetLastError ();
735 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
744 newsock = _wapi_accept (sock, NULL, 0);
745 if(newsock==INVALID_SOCKET) {
746 *error = WSAGetLastError ();
750 return(GUINT_TO_POINTER (newsock));
753 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
763 ret = _wapi_listen (sock, backlog);
764 if(ret==SOCKET_ERROR) {
765 *error = WSAGetLastError ();
769 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
770 int sa_size, gint32 *error)
772 MonoDomain *domain = mono_domain_get ();
773 MonoObject *sockaddr_obj;
774 MonoClass *sockaddr_class;
775 MonoClassField *field;
777 MonoAddressFamily family;
779 /* Build a System.Net.SocketAddress object instance */
780 sockaddr_class=mono_class_from_name(system_assembly, "System.Net", "SocketAddress");
781 sockaddr_obj=mono_object_new(domain, sockaddr_class);
783 /* Locate the SocketAddress data buffer in the object */
784 field=mono_class_get_field_from_name(sockaddr_class, "data");
786 /* Make sure there is space for the family and size bytes */
787 data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
789 /* The data buffer is laid out as follows:
790 * byte 0 is the address family
791 * byte 1 is the buffer length
792 * bytes 2 and 3 are the port info
793 * the rest is the address info
796 family=convert_to_mono_family(saddr->sa_family);
797 if(family==AddressFamily_Unknown) {
798 *error = WSAEAFNOSUPPORT;
802 mono_array_set(data, guint8, 0, family & 0x0FF);
803 mono_array_set(data, guint8, 1, ((family << 8) & 0x0FFFF));
805 if(saddr->sa_family==AF_INET) {
806 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
807 guint16 port=ntohs(sa_in->sin_port);
808 guint32 address=ntohl(sa_in->sin_addr.s_addr);
811 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
814 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
815 mono_array_set(data, guint8, 3, (port) & 0xff);
816 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
817 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
818 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
819 mono_array_set(data, guint8, 7, (address) & 0xff);
821 mono_field_set_value (sockaddr_obj, field, data);
823 return(sockaddr_obj);
825 } else if (saddr->sa_family == AF_INET6) {
826 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
829 guint16 port=ntohs(sa_in->sin6_port);
832 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
835 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
836 mono_array_set(data, guint8, 3, (port) & 0xff);
838 for(i=0; i<16; i++) {
839 mono_array_set(data, guint8, 8+i,
840 sa_in->sin6_addr.s6_addr[i]);
843 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
844 mono_array_set(data, guint8, 25,
845 (sa_in->sin6_scope_id >> 8) & 0xff);
846 mono_array_set(data, guint8, 26,
847 (sa_in->sin6_scope_id >> 16) & 0xff);
848 mono_array_set(data, guint8, 27,
849 (sa_in->sin6_scope_id >> 24) & 0xff);
851 mono_field_set_value (sockaddr_obj, field, data);
853 return(sockaddr_obj);
856 } else if (saddr->sa_family == AF_UNIX) {
859 for (i = 0; i < sa_size; i++) {
860 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
863 mono_field_set_value (sockaddr_obj, field, data);
868 *error = WSAEAFNOSUPPORT;
873 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
875 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
884 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
886 if(ret==SOCKET_ERROR) {
887 *error = WSAGetLastError ();
892 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));
895 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
899 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
901 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
910 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
912 if(ret==SOCKET_ERROR) {
913 *error = WSAGetLastError ();
918 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));
921 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
925 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
929 MonoClassField *field;
934 /* Dig the SocketAddress data buffer out of the object */
935 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
936 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
938 /* The data buffer is laid out as follows:
939 * byte 0 is the address family low byte
940 * byte 1 is the address family high byte
942 * bytes 2 and 3 are the port info
943 * the rest is the address info
945 * the rest is the file name
947 len = mono_array_length (data);
949 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
952 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
953 if(family==AF_INET) {
954 struct sockaddr_in *sa=g_new0(struct sockaddr_in, 1);
955 guint16 port=(mono_array_get(data, guint8, 2) << 8) +
956 mono_array_get(data, guint8, 3);
957 guint32 address=(mono_array_get(data, guint8, 4) << 24) +
958 (mono_array_get(data, guint8, 5) << 16 ) +
959 (mono_array_get(data, guint8, 6) << 8) +
960 mono_array_get(data, guint8, 7);
962 sa->sin_family=family;
963 sa->sin_addr.s_addr=htonl(address);
964 sa->sin_port=htons(port);
966 *sa_size=sizeof(struct sockaddr_in);
967 return((struct sockaddr *)sa);
970 } else if (family == AF_INET6) {
971 struct sockaddr_in6 *sa=g_new0(struct sockaddr_in6, 1);
974 guint16 port = mono_array_get(data, guint8, 3) + (mono_array_get(data, guint8, 2) << 8);
975 guint32 scopeid = mono_array_get(data, guint8, 24) +
976 (mono_array_get(data, guint8, 25)<<8) +
977 (mono_array_get(data, guint8, 26)<<16) +
978 (mono_array_get(data, guint8, 27)<<24);
980 sa->sin6_family=family;
981 sa->sin6_port=htons(port);
982 sa->sin6_scope_id = scopeid;
985 sa->sin6_addr.s6_addr[i] = mono_array_get(data, guint8, 8+i);
987 *sa_size=sizeof(struct sockaddr_in6);
988 return((struct sockaddr *)sa);
991 } else if (family == AF_UNIX) {
992 struct sockaddr_un *sock_un = g_new0 (struct sockaddr_un, 1);
995 if (len - 2 > MONO_SIZEOF_SUNPATH)
996 mono_raise_exception (mono_get_exception_index_out_of_range ());
998 sock_un->sun_family = family;
999 for (i = 0; i < len - 2; i++)
1000 sock_un->sun_path [i] = mono_array_get (data, guint8,
1002 sock_un->sun_path [len - 2] = '\0';
1003 *sa_size = sizeof (struct sockaddr_un);
1005 return (struct sockaddr *)sock_un;
1008 *error = WSAEAFNOSUPPORT;
1013 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1015 struct sockaddr *sa;
1019 MONO_ARCH_SAVE_REGS;
1023 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1029 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));
1032 ret = _wapi_bind (sock, sa, sa_size);
1033 if(ret==SOCKET_ERROR) {
1034 *error = WSAGetLastError ();
1047 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1048 gint timeout, gint32 *error)
1053 struct timeval *tvptr;
1056 MONO_ARCH_SAVE_REGS;
1059 /* FIXME: in case of extra iteration (WSAEINTR), substract time
1060 * from the initial timeout */
1063 _wapi_FD_SET (sock, &fds);
1065 divvy = div (timeout, 1000000);
1066 tv.tv_sec = divvy.quot;
1067 tv.tv_usec = divvy.rem;
1073 if (mode == SelectModeRead) {
1074 ret = _wapi_select (0, &fds, NULL, NULL, tvptr);
1075 } else if (mode == SelectModeWrite) {
1076 ret = _wapi_select (0, NULL, &fds, NULL, tvptr);
1077 } else if (mode == SelectModeError) {
1078 ret = _wapi_select (0, NULL, NULL, &fds, tvptr);
1080 g_assert_not_reached ();
1082 } while ((ret == SOCKET_ERROR) && (*error == WSAGetLastError ()) == WSAEINTR);
1084 return (ret != SOCKET_ERROR && _wapi_FD_ISSET (sock, &fds));
1087 extern void ves_icall_System_Net_Sockets_Socket_Connect_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 ": connecting to %s port %d", inet_ntoa(((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port));
1106 ret = _wapi_connect (sock, sa, sa_size);
1107 if(ret==SOCKET_ERROR) {
1108 *error = WSAGetLastError ();
1114 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1121 MONO_ARCH_SAVE_REGS;
1125 alen=mono_array_length(buffer);
1126 if(offset+count>alen) {
1130 buf=mono_array_addr(buffer, guchar, offset);
1132 ret = _wapi_recv (sock, buf, count, recvflags);
1133 if(ret==SOCKET_ERROR) {
1134 *error = WSAGetLastError ();
1141 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1147 struct sockaddr *sa;
1150 MONO_ARCH_SAVE_REGS;
1154 alen=mono_array_length(buffer);
1155 if(offset+count>alen) {
1159 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1164 buf=mono_array_addr(buffer, guchar, offset);
1166 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1167 if(ret==SOCKET_ERROR) {
1169 *error = WSAGetLastError ();
1173 /* If we didn't get a socket size, then we're probably a
1174 * connected connection-oriented socket and the stack hasn't
1175 * returned the remote address. All we can do is return null.
1178 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1187 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1194 MONO_ARCH_SAVE_REGS;
1198 alen=mono_array_length(buffer);
1199 if(offset+count>alen) {
1204 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1207 buf=mono_array_addr(buffer, guchar, offset);
1210 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1213 ret = _wapi_send (sock, buf, count, sendflags);
1214 if(ret==SOCKET_ERROR) {
1215 *error = WSAGetLastError ();
1222 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1228 struct sockaddr *sa;
1231 MONO_ARCH_SAVE_REGS;
1235 alen=mono_array_length(buffer);
1236 if(offset+count>alen) {
1240 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1246 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1249 buf=mono_array_addr(buffer, guchar, offset);
1252 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1255 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1256 if(ret==SOCKET_ERROR) {
1257 *error = WSAGetLastError ();
1265 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1268 MonoClassField *field;
1270 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1271 sock=*(SOCKET *)(((char *)sockobj)+field->offset);
1276 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **read_socks, MonoArray **write_socks, MonoArray **err_socks, gint32 timeout, gint32 *error)
1278 fd_set readfds, writefds, errfds;
1279 fd_set *readptr = NULL, *writeptr = NULL, *errptr = NULL;
1283 int readarrsize = 0, writearrsize = 0, errarrsize = 0;
1284 MonoDomain *domain=mono_domain_get();
1285 MonoClass *sock_arr_class;
1291 MONO_ARCH_SAVE_REGS;
1294 readarrsize=mono_array_length(*read_socks);
1298 if(readarrsize>FD_SETSIZE) {
1306 for(i=0; i<readarrsize; i++) {
1307 handle = Socket_to_SOCKET(mono_array_get(*read_socks, MonoObject *, i));
1308 _wapi_FD_SET(handle, &readfds);
1313 writearrsize=mono_array_length(*write_socks);
1315 if(writearrsize>FD_SETSIZE) {
1321 writeptr = &writefds;
1323 for(i=0; i<writearrsize; i++) {
1324 handle = Socket_to_SOCKET(mono_array_get(*write_socks, MonoObject *, i));
1325 _wapi_FD_SET(handle, &writefds);
1330 errarrsize=mono_array_length(*err_socks);
1332 if(errarrsize>FD_SETSIZE) {
1340 for(i=0; i<errarrsize; i++) {
1341 handle = Socket_to_SOCKET(mono_array_get(*err_socks, MonoObject *, i));
1342 _wapi_FD_SET(handle, &errfds);
1346 /* Negative timeout meaning block until ready is only
1347 * specified in Poll, not Select
1350 divvy = div (timeout, 1000000);
1354 tv.tv_sec=divvy.quot;
1355 tv.tv_usec=divvy.rem;
1357 ret = _wapi_select (0, readptr, writeptr, errptr, &tv);
1359 ret = _wapi_select (0, readptr, writeptr, errptr, NULL);
1361 } while ((ret==SOCKET_ERROR) && (WSAGetLastError() == WSAEINTR));
1363 if(ret==SOCKET_ERROR) {
1364 *error = WSAGetLastError ();
1369 sock_arr_class=((MonoObject *)*read_socks)->vtable->klass;
1372 for(i=0; i<readarrsize; i++) {
1373 if(_wapi_FD_ISSET(Socket_to_SOCKET(mono_array_get(*read_socks, MonoObject *, i)), &readfds)) {
1377 socks=mono_array_new_full(domain, sock_arr_class, &count, NULL);
1379 for(i=0; i<readarrsize; i++) {
1380 MonoObject *sock=mono_array_get(*read_socks, MonoObject *, i);
1382 if(_wapi_FD_ISSET(Socket_to_SOCKET(sock), &readfds)) {
1383 mono_array_set(socks, MonoObject *, count, sock);
1393 sock_arr_class=((MonoObject *)*write_socks)->vtable->klass;
1395 for(i=0; i<writearrsize; i++) {
1396 if(_wapi_FD_ISSET(Socket_to_SOCKET(mono_array_get(*write_socks, MonoObject *, i)), &writefds)) {
1400 socks=mono_array_new_full(domain, sock_arr_class, &count, NULL);
1402 for(i=0; i<writearrsize; i++) {
1403 MonoObject *sock=mono_array_get(*write_socks, MonoObject *, i);
1405 if(_wapi_FD_ISSET(Socket_to_SOCKET(sock), &writefds)) {
1406 mono_array_set(socks, MonoObject *, count, sock);
1412 *write_socks = NULL;
1416 sock_arr_class=((MonoObject *)*err_socks)->vtable->klass;
1418 for(i=0; i<errarrsize; i++) {
1419 if(_wapi_FD_ISSET(Socket_to_SOCKET(mono_array_get(*err_socks, MonoObject *, i)), &errfds)) {
1423 socks=mono_array_new_full(domain, sock_arr_class, &count, NULL);
1425 for(i=0; i<errarrsize; i++) {
1426 MonoObject *sock=mono_array_get(*err_socks, MonoObject *, i);
1428 if(_wapi_FD_ISSET(Socket_to_SOCKET(sock), &errfds)) {
1429 mono_array_set(socks, MonoObject *, count, sock);
1437 static MonoObject* int_to_object (MonoDomain *domain, int val)
1439 return mono_value_box (domain, mono_get_int32_class (), &val);
1443 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1449 int valsize=sizeof(val);
1450 struct linger linger;
1451 int lingersize=sizeof(linger);
1453 int time_ms_size = sizeof (time_ms);
1456 int credsize = sizeof(cred);
1458 MonoDomain *domain=mono_domain_get();
1460 MonoClass *obj_class;
1461 MonoClassField *field;
1463 MONO_ARCH_SAVE_REGS;
1467 ret=convert_sockopt_level_and_name(level, name, &system_level,
1470 *error = WSAENOPROTOOPT;
1474 /* No need to deal with MulticastOption names here, because
1475 * you cant getsockopt AddMembership or DropMembership (the
1476 * int getsockopt will error, causing an exception)
1479 case SocketOptionName_Linger:
1480 case SocketOptionName_DontLinger:
1481 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1485 case SocketOptionName_SendTimeout:
1486 case SocketOptionName_ReceiveTimeout:
1487 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1491 case SocketOptionName_PeerCred:
1492 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1498 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1502 if(ret==SOCKET_ERROR) {
1503 *error = WSAGetLastError ();
1508 case SocketOptionName_Linger:
1509 /* build a System.Net.Sockets.LingerOption */
1510 obj_class=mono_class_from_name(system_assembly,
1511 "System.Net.Sockets",
1513 obj=mono_object_new(domain, obj_class);
1515 /* Locate and set the fields "bool enabled" and "int
1518 field=mono_class_get_field_from_name(obj_class, "enabled");
1519 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1521 field=mono_class_get_field_from_name(obj_class, "seconds");
1522 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1526 case SocketOptionName_DontLinger:
1527 /* construct a bool int in val - true if linger is off */
1528 obj = int_to_object (domain, !linger.l_onoff);
1531 case SocketOptionName_SendTimeout:
1532 case SocketOptionName_ReceiveTimeout:
1533 obj = int_to_object (domain, time_ms);
1537 case SocketOptionName_PeerCred:
1539 /* build a Mono.Posix.PeerCred+PeerCredData if
1542 static MonoImage *mono_posix_image = NULL;
1543 MonoPeerCredData *cred_data;
1545 if (mono_posix_image == NULL) {
1546 mono_posix_image=mono_image_loaded ("Mono.Posix");
1547 if (!mono_posix_image) {
1548 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1550 *error = WSAENOPROTOOPT;
1553 mono_posix_image = mono_assembly_get_image (sa);
1558 obj_class = mono_class_from_name(mono_posix_image,
1560 "PeerCred/PeerCredData");
1561 obj = mono_object_new(domain, obj_class);
1562 cred_data = (MonoPeerCredData *)obj;
1563 cred_data->pid = cred.pid;
1564 cred_data->uid = cred.uid;
1565 cred_data->gid = cred.gid;
1571 obj = int_to_object (domain, val);
1577 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1585 MONO_ARCH_SAVE_REGS;
1589 ret=convert_sockopt_level_and_name(level, name, &system_level,
1592 *error = WSAENOPROTOOPT;
1596 valsize=mono_array_length(*byte_val);
1597 buf=mono_array_addr(*byte_val, guchar, 0);
1599 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1600 if(ret==SOCKET_ERROR) {
1601 *error = WSAGetLastError ();
1605 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1606 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1608 struct in_addr inaddr;
1609 MonoClassField *field;
1611 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "address");
1613 /* No idea why .net uses a 64bit type to hold a 32bit value...
1615 * Internal value of IPAddess is in Network Order, there is no need
1616 * to call htonl here.
1618 inaddr.s_addr=(guint32)*(guint64 *)(((char *)ipaddr)+field->offset);
1625 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1627 struct in6_addr in6addr;
1628 MonoClassField *field;
1632 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "_numbers");
1633 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1635 /* Solaris has only the 8 bit version. */
1637 for(i=0; i<8; i++) {
1638 guint16 s = mono_array_get (data, guint16, i);
1639 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1640 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1644 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1648 #endif /* AF_INET6 */
1650 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)
1661 #ifdef HAVE_SOL_IPV6
1662 sol_ipv6 = SOL_IPV6;
1665 struct protoent *pent;
1666 pent = getprotobyname ("ipv6");
1667 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
1675 struct protoent *pent;
1676 pent = getprotobyname ("ip");
1677 sol_ip = (pent != NULL) ? pent->p_proto : 0;
1680 #endif /* AF_INET6 */
1682 MONO_ARCH_SAVE_REGS;
1684 ret=convert_sockopt_level_and_name(level, name, &system_level,
1687 *error = WSAENOPROTOOPT;
1691 /* Only one of obj_val, byte_val or int_val has data */
1693 MonoClassField *field;
1694 struct linger linger;
1698 case SocketOptionName_DontLinger:
1701 valsize=sizeof(linger);
1702 ret = _wapi_setsockopt (sock, system_level,
1703 system_name, &linger, valsize);
1706 case SocketOptionName_Linger:
1707 /* Dig out "bool enabled" and "int seconds"
1710 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
1711 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
1712 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
1713 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
1715 valsize=sizeof(linger);
1716 ret = _wapi_setsockopt (sock, system_level,
1717 system_name, &linger, valsize);
1719 case SocketOptionName_AddMembership:
1720 case SocketOptionName_DropMembership:
1721 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1723 MonoObject *address = NULL;
1726 if(system_level == sol_ipv6) {
1727 struct ipv6_mreq mreq6;
1732 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1733 address = *(gpointer *)(((char *)obj_val) + field->offset);
1736 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
1739 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
1740 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
1742 ret = _wapi_setsockopt (sock, system_level,
1743 system_name, &mreq6,
1745 } else if(system_level == sol_ip)
1746 #endif /* AF_INET6 */
1748 #ifdef HAVE_STRUCT_IP_MREQN
1749 struct ip_mreqn mreq = {{0}};
1751 struct ip_mreq mreq = {{0}};
1752 #endif /* HAVE_STRUCT_IP_MREQN */
1754 /* pain! MulticastOption holds two IPAddress
1755 * members, so I have to dig the value out of
1758 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1759 address = *(gpointer *)(((char *)obj_val) + field->offset);
1761 /* address might not be defined and if so, set the address to ADDR_ANY.
1764 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
1767 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
1768 address = *(gpointer *)(((char *)obj_val) + field->offset);
1770 #ifdef HAVE_STRUCT_IP_MREQN
1772 mreq.imr_address = ipaddress_to_struct_in_addr (address);
1776 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
1778 #endif /* HAVE_STRUCT_IP_MREQN */
1780 ret = _wapi_setsockopt (sock, system_level,
1786 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
1788 /* Cause an exception to be thrown */
1792 } else if (byte_val!=NULL) {
1793 int valsize=mono_array_length(byte_val);
1794 guchar *buf=mono_array_addr(byte_val, guchar, 0);
1796 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
1797 if(ret==SOCKET_ERROR) {
1798 *error = WSAGetLastError ();
1802 /* ReceiveTimeout/SendTimeout get here */
1803 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
1806 if(ret==SOCKET_ERROR) {
1807 *error = WSAGetLastError ();
1811 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
1817 MONO_ARCH_SAVE_REGS;
1821 /* Currently, the values for how (recv=0, send=1, both=2) match
1824 ret = _wapi_shutdown (sock, how);
1825 if(ret==SOCKET_ERROR) {
1826 *error = WSAGetLastError ();
1831 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
1833 MonoArray *output, gint32 *error)
1835 gulong output_bytes = 0;
1836 gchar *i_buffer, *o_buffer;
1840 MONO_ARCH_SAVE_REGS;
1844 if (code == FIONBIO) {
1845 /* Invalid command. Must use Socket.Blocking */
1849 if (input == NULL) {
1853 i_buffer = mono_array_addr (input, gchar, 0);
1854 i_len = mono_array_length (input);
1857 if (output == NULL) {
1861 o_buffer = mono_array_addr (output, gchar, 0);
1862 o_len = mono_array_length (output);
1865 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
1866 if (ret == SOCKET_ERROR) {
1867 *error = WSAGetLastError ();
1871 return (gint) output_bytes;
1874 #ifdef HAVE_SIOCGIFCONF
1876 is_loopback (int family, void *ad)
1878 char *ptr = (char *) ad;
1880 if (family == AF_INET) {
1881 return (ptr [0] == 127);
1885 return (IN6_IS_ADDR_LOOPBACK (ptr));
1892 get_local_ips (int family, int *nips)
1894 int addr_size, offset, fd, i, count;
1895 int max_ifaces = 50; /* 50 interfaces should be enough... */
1898 struct ifreq iflags;
1899 char *result, *tmp_ptr;
1900 gboolean ignore_loopback = FALSE;
1903 if (family == AF_INET) {
1904 addr_size = sizeof (struct in_addr);
1905 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
1907 } else if (family == AF_INET6) {
1908 addr_size = sizeof (struct in6_addr);
1909 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
1915 fd = socket (family, SOCK_STREAM, 0);
1917 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
1918 ifc.ifc_buf = g_malloc (ifc.ifc_len);
1919 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
1921 g_free (ifc.ifc_buf);
1925 count = ifc.ifc_len / sizeof (struct ifreq);
1928 g_free (ifc.ifc_buf);
1933 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
1934 strcpy (iflags.ifr_name, ifr->ifr_name);
1935 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
1939 if ((iflags.ifr_flags & IFF_UP) == 0) {
1940 ifr->ifr_name [0] = '\0';
1944 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
1945 ignore_loopback = TRUE;
1950 result = g_malloc (addr_size * count);
1952 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
1953 if (ifr->ifr_name [0] == '\0') {
1958 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
1963 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
1964 tmp_ptr += addr_size;
1967 g_free (ifc.ifc_buf);
1972 get_local_ips (int family, int *nips)
1978 #endif /* HAVE_SIOCGIFCONF */
1981 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
1982 MonoArray **h_aliases,
1983 MonoArray **h_addr_list,
1984 gboolean add_local_ips)
1986 MonoDomain *domain = mono_domain_get ();
1988 struct in_addr *local_in = NULL;
1991 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
1995 *h_name=mono_string_new(domain, he->h_name);
1998 while(he->h_aliases[i]!=NULL) {
2002 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2004 while(he->h_aliases[i]!=NULL) {
2007 alias=mono_string_new(domain, he->h_aliases[i]);
2008 mono_array_set(*h_aliases, MonoString *, i, alias);
2012 if (add_local_ips) {
2013 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2015 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2016 for (i = 0; i < nlocal_in; i++) {
2017 MonoString *addr_string;
2018 char addr [16], *ptr;
2020 ptr = (char *) &local_in [i];
2021 g_snprintf(addr, 16, "%u.%u.%u.%u",
2022 (unsigned char) ptr [0],
2023 (unsigned char) ptr [1],
2024 (unsigned char) ptr [2],
2025 (unsigned char) ptr [3]);
2027 addr_string = mono_string_new (domain, addr);
2028 mono_array_set (*h_addr_list, MonoString *, i, addr_string);
2036 if (nlocal_in == 0) {
2038 while (he->h_addr_list[i]!=NULL) {
2042 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2044 while(he->h_addr_list[i]!=NULL) {
2045 MonoString *addr_string;
2048 g_snprintf(addr, 16, "%u.%u.%u.%u",
2049 (unsigned char)he->h_addr_list[i][0],
2050 (unsigned char)he->h_addr_list[i][1],
2051 (unsigned char)he->h_addr_list[i][2],
2052 (unsigned char)he->h_addr_list[i][3]);
2054 addr_string=mono_string_new(domain, addr);
2055 mono_array_set(*h_addr_list, MonoString *, i, addr_string);
2064 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2065 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2066 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2068 MonoDomain *domain = mono_domain_get ();
2069 int i, host_count, host_index, family_hint;
2070 struct in_addr *local_in = NULL;
2072 struct in6_addr *local_in6 = NULL;
2074 gboolean from_local = FALSE;
2076 family_hint = get_family_hint ();
2078 if(he1 == NULL && he2 == NULL) {
2083 * Check if address length and family are correct
2085 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2089 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2094 * Get the aliases and host name from he1 or he2 whichever is
2095 * not null, if he1 is not null then take aliases from he1
2097 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2098 family_hint == PF_INET)) {
2099 *h_name=mono_string_new (domain, he1->h_name);
2102 while(he1->h_aliases[i]!=NULL) {
2106 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2109 while(he1->h_aliases[i]!=NULL) {
2112 alias=mono_string_new (domain, he1->h_aliases[i]);
2113 mono_array_set (*h_aliases, MonoString *, i, alias);
2116 } else if (family_hint == PF_UNSPEC || family_hint == PF_INET6) {
2117 *h_name=mono_string_new (domain, he2->h_name);
2120 while(he2->h_aliases [i] != NULL) {
2124 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2127 while(he2->h_aliases[i]!=NULL) {
2130 alias=mono_string_new (domain, he2->h_aliases[i]);
2131 mono_array_set (*h_aliases, MonoString *, i, alias);
2137 * Count the number of addresses in he1 + he2
2140 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2141 family_hint == PF_INET)) {
2143 while(he1->h_addr_list[i]!=NULL) {
2149 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2150 family_hint == PF_INET6)) {
2152 while(he2->h_addr_list[i]!=NULL) {
2162 if (add_local_ips) {
2163 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2164 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2166 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2167 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2169 if (nlocal_in || nlocal_in6) {
2171 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2172 nlocal_in + nlocal_in6);
2176 for (n = 0; n < nlocal_in6; n++) {
2177 MonoString *addr_string;
2178 char addr[46]; /* INET6_ADDRSTRLEN == 46 */
2180 inet_ntop (AF_INET6, &local_in6 [n], addr,
2183 addr_string = mono_string_new (domain, addr);
2184 mono_array_set (*h_addr_list, MonoString *, host_index,
2192 for (n = 0; n < nlocal_in; n++) {
2193 MonoString *addr_string;
2194 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2196 inet_ntop (AF_INET, &local_in [n], addr,
2199 addr_string = mono_string_new (domain, addr);
2200 mono_array_set (*h_addr_list, MonoString *, host_index,
2214 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2216 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2217 family_hint == PF_INET6)) {
2219 while(he2->h_addr_list[i] != NULL) {
2220 MonoString *addr_string;
2221 char addr[46]; /* INET6_ADDRSTRLEN == 46 */
2223 inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2226 addr_string = mono_string_new (domain, addr);
2227 mono_array_set (*h_addr_list, MonoString *, host_index,
2234 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2235 family_hint == PF_INET)) {
2237 while(he1->h_addr_list[i] != NULL) {
2238 MonoString *addr_string;
2239 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2241 inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2244 addr_string=mono_string_new (domain, addr);
2245 mono_array_set (*h_addr_list, MonoString *, host_index,
2256 #if defined(AF_INET6)
2258 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2259 MonoArray **h_aliases,
2260 MonoArray **h_addr_list,
2261 gboolean add_local_ips)
2264 struct addrinfo *ai = NULL;
2265 struct in_addr *local_in = NULL;
2267 struct in6_addr *local_in6 = NULL;
2271 MonoDomain *domain = mono_domain_get ();
2274 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2275 if (add_local_ips) {
2276 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2277 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in);
2278 if (nlocal_in || nlocal_in6) {
2279 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2281 MonoString *addr_string;
2285 for (i = 0; i < nlocal_in; i++) {
2286 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2287 addr_string = mono_string_new (domain, addr);
2288 mono_array_set (*h_addr_list, MonoString *, addr_index, addr_string);
2294 MonoString *addr_string;
2298 for (i = 0; i < nlocal_in6; i++) {
2299 inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2300 addr_string = mono_string_new (domain, addr);
2301 mono_array_set (*h_addr_list, MonoString *, addr_index, addr_string);
2309 freeaddrinfo (info);
2318 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2319 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2325 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2327 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2328 MonoString *addr_string;
2330 char buffer [46]; /* Max. size for IPv6 */
2332 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2336 if(ai->ai_family == PF_INET) {
2337 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2339 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 46);
2343 addr_string=mono_string_new(domain, buffer);
2346 addr_string=mono_string_new(domain, "");
2349 mono_array_set(*h_addr_list, MonoString *, addr_index, addr_string);
2351 if(!i && ai->ai_canonname != NULL) {
2352 *h_name=mono_string_new(domain, ai->ai_canonname);
2367 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2369 gboolean add_local_ips = FALSE;
2370 #ifdef HAVE_SIOCGIFCONF
2371 guchar this_hostname [256];
2373 #if !defined(HAVE_GETHOSTBYNAME2_R)
2374 struct addrinfo *info = NULL, hints;
2377 MONO_ARCH_SAVE_REGS;
2379 hostname=mono_string_to_utf8 (host);
2380 #ifdef HAVE_SIOCGIFCONF
2381 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2382 if (!strcmp (hostname, this_hostname))
2383 add_local_ips = TRUE;
2387 memset(&hints, 0, sizeof(hints));
2388 hints.ai_family = get_family_hint ();
2389 hints.ai_socktype = SOCK_STREAM;
2390 hints.ai_flags = AI_CANONNAME;
2392 if (getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2398 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2400 struct hostent he1,*hp1, he2, *hp2;
2401 int buffer_size1, buffer_size2;
2402 char *buffer1, *buffer2;
2404 gboolean return_value;
2407 MONO_ARCH_SAVE_REGS;
2409 hostname=mono_string_to_utf8 (host);
2411 #ifdef HAVE_SIOCGIFCONF
2412 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2413 if (!strcmp (hostname, this_hostname))
2414 add_local_ips = TRUE;
2420 buffer1 = g_malloc0(buffer_size1);
2421 buffer2 = g_malloc0(buffer_size2);
2423 while (gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2424 &hp1, &herr) == ERANGE) {
2426 buffer1 = g_realloc(buffer1, buffer_size1);
2431 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2432 buffer_size2, &hp2, &herr) == ERANGE) {
2434 buffer2 = g_realloc(buffer2, buffer_size2);
2440 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2441 h_addr_list, add_local_ips);
2447 return(return_value);
2448 #endif /* HAVE_GETHOSTBYNAME2_R */
2450 #else /* AF_INET6 */
2451 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2455 gboolean add_local_ips = FALSE;
2456 #ifdef HAVE_SIOCGIFCONF
2457 guchar this_hostname [256];
2460 MONO_ARCH_SAVE_REGS;
2462 hostname=mono_string_to_utf8(host);
2463 #ifdef HAVE_SIOCGIFCONF
2464 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2465 if (!strcmp (hostname, this_hostname))
2466 add_local_ips = TRUE;
2470 he = _wapi_gethostbyname (hostname);
2477 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2479 #endif /* AF_INET6 */
2481 #ifndef HAVE_INET_PTON
2483 inet_pton (int family, const char *address, void *inaddrp)
2485 if (family == AF_INET) {
2486 #ifdef HAVE_INET_ATON
2487 struct in_addr inaddr;
2489 if (!inet_aton (address, &inaddr))
2492 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2495 /* assume the system has inet_addr(), if it doesn't
2496 have that we're pretty much screwed... */
2499 if (!strcmp (address, "255.255.255.255")) {
2500 /* special-case hack */
2501 inaddr = 0xffffffff;
2503 inaddr = inet_addr (address);
2505 #define INADDR_NONE ((in_addr_t) -1)
2507 if (inaddr == INADDR_NONE)
2511 memcpy (inaddrp, &inaddr, sizeof (guint32));
2513 #endif /* HAVE_INET_ATON */
2518 #endif /* !HAVE_INET_PTON */
2520 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2525 struct sockaddr_in saddr;
2526 struct sockaddr_in6 saddr6;
2527 struct addrinfo *info = NULL, hints;
2529 char hostname[1024] = {0};
2531 struct in_addr inaddr;
2535 MONO_ARCH_SAVE_REGS;
2537 address = mono_string_to_utf8 (addr);
2540 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2541 /* Maybe an ipv6 address */
2542 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2548 saddr6.sin6_family = AF_INET6;
2553 saddr.sin_family = AF_INET;
2557 if(family == AF_INET) {
2558 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2559 hostname, sizeof(hostname), NULL, 0,
2560 NI_NAMEREQD) != 0) {
2563 } else if(family == AF_INET6) {
2564 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2565 hostname, sizeof(hostname), NULL, 0,
2566 NI_NAMEREQD) != 0) {
2571 memset (&hints, 0, sizeof(hints));
2572 hints.ai_family = get_family_hint ();
2573 hints.ai_socktype = SOCK_STREAM;
2574 hints.ai_flags = AI_CANONNAME;
2576 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
2580 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
2582 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
2588 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
2592 return(hostent_to_IPHostEntry (he, h_name, h_aliases, h_addr_list, FALSE));
2596 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
2598 guchar hostname[256];
2601 MONO_ARCH_SAVE_REGS;
2603 ret = gethostname (hostname, sizeof (hostname));
2608 *h_name=mono_string_new(mono_domain_get (), hostname);
2613 void mono_network_init(void)
2618 err=WSAStartup(MAKEWORD(2,0), &wsadata);
2620 g_error(G_GNUC_PRETTY_FUNCTION ": Couldn't initialise networking");
2625 g_message(G_GNUC_PRETTY_FUNCTION ": Using socket library: %s", wsadata.szDescription);
2626 g_message(G_GNUC_PRETTY_FUNCTION ": Socket system status: %s", wsadata.szSystemStatus);
2630 void mono_network_cleanup(void)