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>
34 #ifdef HAVE_SYS_FILIO_H
35 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
37 #ifdef HAVE_SYS_SOCKIO_H
38 #include <sys/sockio.h> /* defines SIOCATMARK */
44 #include "mono/io-layer/socket-wrappers.h"
47 /* This is a kludge to make this file build under cygwin:
48 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
49 * prototypes for some but not all required functions (notably
50 * inet_ntop() is missing), but the libws2_32 library is missing the
51 * actual implementations of these functions.
58 static gint32 convert_family(MonoAddressFamily mono_family)
63 case AddressFamily_Unknown:
64 case AddressFamily_ImpLink:
65 case AddressFamily_Pup:
66 case AddressFamily_Chaos:
67 case AddressFamily_Iso:
68 case AddressFamily_Ecma:
69 case AddressFamily_DataKit:
70 case AddressFamily_Ccitt:
71 case AddressFamily_DataLink:
72 case AddressFamily_Lat:
73 case AddressFamily_HyperChannel:
74 case AddressFamily_NetBios:
75 case AddressFamily_VoiceView:
76 case AddressFamily_FireFox:
77 case AddressFamily_Banyan:
78 case AddressFamily_Atm:
79 case AddressFamily_Cluster:
80 case AddressFamily_Ieee12844:
81 case AddressFamily_NetworkDesigners:
82 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
85 case AddressFamily_Unspecified:
89 case AddressFamily_Unix:
93 case AddressFamily_InterNetwork:
97 case AddressFamily_Ipx:
103 case AddressFamily_Sna:
107 case AddressFamily_DecNet:
111 case AddressFamily_AppleTalk:
115 case AddressFamily_InterNetworkV6:
120 case AddressFamily_Irda:
126 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
132 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
134 MonoAddressFamily family=AddressFamily_Unknown;
138 family=AddressFamily_Unspecified;
142 family=AddressFamily_Unix;
146 family=AddressFamily_InterNetwork;
151 family=AddressFamily_Ipx;
156 family=AddressFamily_Sna;
160 family=AddressFamily_DecNet;
164 family=AddressFamily_AppleTalk;
169 family=AddressFamily_InterNetworkV6;
175 family=AddressFamily_Irda;
179 g_warning("unknown address family 0x%x", af_family);
185 static gint32 convert_type(MonoSocketType mono_type)
190 case SocketType_Stream:
194 case SocketType_Dgram:
206 case SocketType_Seqpacket:
210 case SocketType_Unknown:
211 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
215 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
221 static gint32 convert_proto(MonoProtocolType mono_proto)
226 case ProtocolType_IP:
227 case ProtocolType_IPv6:
228 case ProtocolType_Icmp:
229 case ProtocolType_Igmp:
230 case ProtocolType_Ggp:
231 case ProtocolType_Tcp:
232 case ProtocolType_Pup:
233 case ProtocolType_Udp:
234 case ProtocolType_Idp:
235 /* These protocols are known (on my system at least) */
239 case ProtocolType_ND:
240 case ProtocolType_Raw:
241 case ProtocolType_Ipx:
242 case ProtocolType_Spx:
243 case ProtocolType_SpxII:
244 case ProtocolType_Unknown:
245 /* These protocols arent */
246 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
256 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
257 MonoSocketOptionName mono_name,
261 switch (mono_level) {
262 case SocketOptionLevel_Socket:
263 *system_level = SOL_SOCKET;
266 case SocketOptionName_DontLinger:
267 /* This is SO_LINGER, because the setsockopt
268 * internal call maps DontLinger to SO_LINGER
271 *system_name = SO_LINGER;
273 case SocketOptionName_Debug:
274 *system_name = SO_DEBUG;
277 case SocketOptionName_AcceptConnection:
278 *system_name = SO_ACCEPTCONN;
281 case SocketOptionName_ReuseAddress:
282 *system_name = SO_REUSEADDR;
284 case SocketOptionName_KeepAlive:
285 *system_name = SO_KEEPALIVE;
287 case SocketOptionName_DontRoute:
288 *system_name = SO_DONTROUTE;
290 case SocketOptionName_Broadcast:
291 *system_name = SO_BROADCAST;
293 case SocketOptionName_Linger:
294 *system_name = SO_LINGER;
296 case SocketOptionName_OutOfBandInline:
297 *system_name = SO_OOBINLINE;
299 case SocketOptionName_SendBuffer:
300 *system_name = SO_SNDBUF;
302 case SocketOptionName_ReceiveBuffer:
303 *system_name = SO_RCVBUF;
305 case SocketOptionName_SendLowWater:
306 *system_name = SO_SNDLOWAT;
308 case SocketOptionName_ReceiveLowWater:
309 *system_name = SO_RCVLOWAT;
311 case SocketOptionName_SendTimeout:
312 *system_name = SO_SNDTIMEO;
314 case SocketOptionName_ReceiveTimeout:
315 *system_name = SO_RCVTIMEO;
317 case SocketOptionName_Error:
318 *system_name = SO_ERROR;
320 case SocketOptionName_Type:
321 *system_name = SO_TYPE;
324 case SocketOptionName_PeerCred:
325 *system_name = SO_PEERCRED;
328 case SocketOptionName_ExclusiveAddressUse:
329 case SocketOptionName_UseLoopback:
330 case SocketOptionName_MaxConnections:
331 /* Can't figure out how to map these, so fall
335 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
340 case SocketOptionLevel_IP:
342 *system_level = SOL_IP;
345 static int cached = 0;
349 struct protoent *pent;
351 pent = getprotobyname ("IP");
352 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
356 *system_level = proto;
358 #endif /* HAVE_SOL_IP */
361 case SocketOptionName_IPOptions:
362 *system_name = IP_OPTIONS;
365 case SocketOptionName_HeaderIncluded:
366 *system_name = IP_HDRINCL;
370 case SocketOptionName_TypeOfService:
371 *system_name = IP_TOS;
375 case SocketOptionName_IpTimeToLive:
376 *system_name = IP_TTL;
379 case SocketOptionName_MulticastInterface:
380 *system_name = IP_MULTICAST_IF;
382 case SocketOptionName_MulticastTimeToLive:
383 *system_name = IP_MULTICAST_TTL;
385 case SocketOptionName_MulticastLoopback:
386 *system_name = IP_MULTICAST_LOOP;
388 case SocketOptionName_AddMembership:
389 *system_name = IP_ADD_MEMBERSHIP;
391 case SocketOptionName_DropMembership:
392 *system_name = IP_DROP_MEMBERSHIP;
394 #ifdef HAVE_IP_PKTINFO
395 case SocketOptionName_PacketInformation:
396 *system_name = IP_PKTINFO;
398 #endif /* HAVE_IP_PKTINFO */
399 case SocketOptionName_DontFragment:
400 case SocketOptionName_AddSourceMembership:
401 case SocketOptionName_DropSourceMembership:
402 case SocketOptionName_BlockSource:
403 case SocketOptionName_UnblockSource:
404 /* Can't figure out how to map these, so fall
408 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
414 case SocketOptionLevel_IPv6:
416 *system_level = SOL_IPV6;
419 static int cached = 0;
423 struct protoent *pent;
425 pent = getprotobyname ("IPV6");
426 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
430 *system_level = proto;
432 #endif /* HAVE_SOL_IPV6 */
435 case SocketOptionName_IpTimeToLive:
436 *system_name = IPV6_UNICAST_HOPS;
438 case SocketOptionName_MulticastInterface:
439 *system_name = IPV6_MULTICAST_IF;
441 case SocketOptionName_MulticastTimeToLive:
442 *system_name = IPV6_MULTICAST_HOPS;
444 case SocketOptionName_MulticastLoopback:
445 *system_name = IPV6_MULTICAST_LOOP;
447 case SocketOptionName_AddMembership:
448 *system_name = IPV6_JOIN_GROUP;
450 case SocketOptionName_DropMembership:
451 *system_name = IPV6_LEAVE_GROUP;
453 case SocketOptionName_PacketInformation:
454 *system_name = IPV6_PKTINFO;
456 case SocketOptionName_HeaderIncluded:
457 case SocketOptionName_IPOptions:
458 case SocketOptionName_TypeOfService:
459 case SocketOptionName_DontFragment:
460 case SocketOptionName_AddSourceMembership:
461 case SocketOptionName_DropSourceMembership:
462 case SocketOptionName_BlockSource:
463 case SocketOptionName_UnblockSource:
464 /* Can't figure out how to map these, so fall
468 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
472 break; /* SocketOptionLevel_IPv6 */
475 case SocketOptionLevel_Tcp:
477 *system_level = SOL_TCP;
480 static int cached = 0;
484 struct protoent *pent;
486 pent = getprotobyname ("TCP");
487 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
491 *system_level = proto;
493 #endif /* HAVE_SOL_TCP */
496 case SocketOptionName_NoDelay:
497 *system_name = TCP_NODELAY;
500 /* The documentation is talking complete
501 * bollocks here: rfc-1222 is titled
502 * 'Advancing the NSFNET Routing Architecture'
503 * and doesn't mention either of the words
504 * "expedite" or "urgent".
506 case SocketOptionName_BsdUrgent:
507 case SocketOptionName_Expedited:
510 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
515 case SocketOptionLevel_Udp:
516 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
519 case SocketOptionName_NoChecksum:
520 case SocketOptionName_ChecksumCoverage:
522 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
529 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
536 #define STASH_SYS_ASS(this) \
537 if(system_assembly == NULL) { \
538 system_assembly=mono_image_loaded ("System"); \
539 if (!system_assembly) { \
540 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL); \
541 if (!sa) g_assert_not_reached (); \
542 else {system_assembly = mono_assembly_get_image (sa);} \
546 static MonoImage *system_assembly=NULL;
550 static gint32 get_family_hint(void)
552 MonoClass *socket_class;
553 MonoClassField *ipv6_field, *ipv4_field;
554 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
557 socket_class = mono_class_from_name (system_assembly,
558 "System.Net.Sockets", "Socket");
559 ipv4_field = mono_class_get_field_from_name (socket_class,
561 ipv6_field = mono_class_get_field_from_name (socket_class,
563 vtable = mono_class_vtable (mono_domain_get (), socket_class);
565 mono_field_static_get_value(vtable, ipv4_field, &ipv4_enabled);
566 mono_field_static_get_value(vtable, ipv6_field, &ipv6_enabled);
568 if(ipv4_enabled == 1 && ipv6_enabled == 1) {
570 } else if(ipv4_enabled == 1) {
578 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
591 sock_family=convert_family(family);
592 if(sock_family==-1) {
593 *error = WSAEAFNOSUPPORT;
597 sock_proto=convert_proto(proto);
599 *error = WSAEPROTONOSUPPORT;
603 sock_type=convert_type(type);
605 *error = WSAESOCKTNOSUPPORT;
609 sock = _wapi_socket (sock_family, sock_type, sock_proto,
610 NULL, 0, WSA_FLAG_OVERLAPPED);
612 if(sock==INVALID_SOCKET) {
613 *error = WSAGetLastError ();
617 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
618 return (GUINT_TO_POINTER (sock));
622 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
623 return (GUINT_TO_POINTER (sock));
627 #ifndef PLATFORM_WIN32
628 /* .net seems to set this by default for SOCK_STREAM,
629 * not for SOCK_DGRAM (see bug #36322)
631 * It seems winsock has a rather different idea of what
632 * SO_REUSEADDR means. If it's set, then a new socket can be
633 * bound over an existing listening socket. There's a new
634 * windows-specific option called SO_EXCLUSIVEADDRUSE but
635 * using that means the socket MUST be closed properly, or a
636 * denial of service can occur. Luckily for us, winsock
637 * behaves as though any other system would when SO_REUSEADDR
638 * is true, so we don't need to do anything else here. See
644 ret = _wapi_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof (true));
645 if(ret==SOCKET_ERROR) {
646 *error = WSAGetLastError ();
654 return(GUINT_TO_POINTER (sock));
657 /* FIXME: the SOCKET parameter (here and in other functions in this
658 * file) is really an IntPtr which needs to be converted to a guint32.
660 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
666 g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
671 /* Clear any pending work item from this socket if the underlying
672 * polling system does not notify when the socket is closed */
673 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
677 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
682 g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
685 return(WSAGetLastError());
688 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
698 ret=ioctlsocket(sock, FIONREAD, &amount);
699 if(ret==SOCKET_ERROR) {
700 *error = WSAGetLastError ();
707 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
718 * block == TRUE/FALSE means we will block/not block.
719 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
723 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
724 if(ret==SOCKET_ERROR) {
725 *error = WSAGetLastError ();
729 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
738 newsock = _wapi_accept (sock, NULL, 0);
739 if(newsock==INVALID_SOCKET) {
740 *error = WSAGetLastError ();
744 return(GUINT_TO_POINTER (newsock));
747 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
757 ret = _wapi_listen (sock, backlog);
758 if(ret==SOCKET_ERROR) {
759 *error = WSAGetLastError ();
763 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
764 int sa_size, gint32 *error)
766 MonoDomain *domain = mono_domain_get ();
767 MonoObject *sockaddr_obj;
768 MonoClass *sockaddr_class;
769 MonoClassField *field;
771 MonoAddressFamily family;
773 /* Build a System.Net.SocketAddress object instance */
774 sockaddr_class=mono_class_from_name(system_assembly, "System.Net", "SocketAddress");
775 sockaddr_obj=mono_object_new(domain, sockaddr_class);
777 /* Locate the SocketAddress data buffer in the object */
778 field=mono_class_get_field_from_name(sockaddr_class, "data");
780 /* Make sure there is space for the family and size bytes */
781 data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
783 /* The data buffer is laid out as follows:
784 * byte 0 is the address family
785 * byte 1 is the buffer length
786 * bytes 2 and 3 are the port info
787 * the rest is the address info
790 family=convert_to_mono_family(saddr->sa_family);
791 if(family==AddressFamily_Unknown) {
792 *error = WSAEAFNOSUPPORT;
796 mono_array_set(data, guint8, 0, family & 0x0FF);
797 mono_array_set(data, guint8, 1, ((family << 8) & 0x0FFFF));
799 if(saddr->sa_family==AF_INET) {
800 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
801 guint16 port=ntohs(sa_in->sin_port);
802 guint32 address=ntohl(sa_in->sin_addr.s_addr);
805 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
808 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
809 mono_array_set(data, guint8, 3, (port) & 0xff);
810 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
811 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
812 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
813 mono_array_set(data, guint8, 7, (address) & 0xff);
815 mono_field_set_value (sockaddr_obj, field, data);
817 return(sockaddr_obj);
819 } else if (saddr->sa_family == AF_INET6) {
820 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
823 guint16 port=ntohs(sa_in->sin6_port);
826 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
829 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
830 mono_array_set(data, guint8, 3, (port) & 0xff);
832 for(i=0; i<16; i++) {
833 mono_array_set(data, guint8, 8+i,
834 sa_in->sin6_addr.s6_addr[i]);
837 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
838 mono_array_set(data, guint8, 25,
839 (sa_in->sin6_scope_id >> 8) & 0xff);
840 mono_array_set(data, guint8, 26,
841 (sa_in->sin6_scope_id >> 16) & 0xff);
842 mono_array_set(data, guint8, 27,
843 (sa_in->sin6_scope_id >> 24) & 0xff);
845 mono_field_set_value (sockaddr_obj, field, data);
847 return(sockaddr_obj);
850 } else if (saddr->sa_family == AF_UNIX) {
853 for (i = 0; i < sa_size; i++) {
854 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
857 mono_field_set_value (sockaddr_obj, field, data);
862 *error = WSAEAFNOSUPPORT;
867 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
869 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
878 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
880 if(ret==SOCKET_ERROR) {
881 *error = WSAGetLastError ();
886 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));
889 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
893 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
895 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
904 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
906 if(ret==SOCKET_ERROR) {
907 *error = WSAGetLastError ();
912 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));
915 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
919 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
923 MonoClassField *field;
928 /* Dig the SocketAddress data buffer out of the object */
929 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
930 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
932 /* The data buffer is laid out as follows:
933 * byte 0 is the address family low byte
934 * byte 1 is the address family high byte
936 * bytes 2 and 3 are the port info
937 * the rest is the address info
939 * the rest is the file name
941 len = mono_array_length (data);
943 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
946 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
947 if(family==AF_INET) {
948 struct sockaddr_in *sa=g_new0(struct sockaddr_in, 1);
949 guint16 port=(mono_array_get(data, guint8, 2) << 8) +
950 mono_array_get(data, guint8, 3);
951 guint32 address=(mono_array_get(data, guint8, 4) << 24) +
952 (mono_array_get(data, guint8, 5) << 16 ) +
953 (mono_array_get(data, guint8, 6) << 8) +
954 mono_array_get(data, guint8, 7);
956 sa->sin_family=family;
957 sa->sin_addr.s_addr=htonl(address);
958 sa->sin_port=htons(port);
960 *sa_size=sizeof(struct sockaddr_in);
961 return((struct sockaddr *)sa);
964 } else if (family == AF_INET6) {
965 struct sockaddr_in6 *sa=g_new0(struct sockaddr_in6, 1);
968 guint16 port = mono_array_get(data, guint8, 3) + (mono_array_get(data, guint8, 2) << 8);
969 guint32 scopeid = mono_array_get(data, guint8, 24) +
970 (mono_array_get(data, guint8, 25)<<8) +
971 (mono_array_get(data, guint8, 26)<<16) +
972 (mono_array_get(data, guint8, 27)<<24);
974 sa->sin6_family=family;
975 sa->sin6_port=htons(port);
976 sa->sin6_scope_id = scopeid;
979 sa->sin6_addr.s6_addr[i] = mono_array_get(data, guint8, 8+i);
981 *sa_size=sizeof(struct sockaddr_in6);
982 return((struct sockaddr *)sa);
985 } else if (family == AF_UNIX) {
986 struct sockaddr_un *sock_un = g_new0 (struct sockaddr_un, 1);
989 if (len - 2 > MONO_SIZEOF_SUNPATH)
990 mono_raise_exception (mono_get_exception_index_out_of_range ());
992 sock_un->sun_family = family;
993 for (i = 0; i < len - 2; i++)
994 sock_un->sun_path [i] = mono_array_get (data, guint8,
996 sock_un->sun_path [len - 2] = '\0';
997 *sa_size = sizeof (struct sockaddr_un);
999 return (struct sockaddr *)sock_un;
1002 *error = WSAEAFNOSUPPORT;
1007 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1009 struct sockaddr *sa;
1013 MONO_ARCH_SAVE_REGS;
1017 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1023 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));
1026 ret = _wapi_bind (sock, sa, sa_size);
1027 if(ret==SOCKET_ERROR) {
1028 *error = WSAGetLastError ();
1041 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1042 gint timeout, gint32 *error)
1047 struct timeval *tvptr;
1050 MONO_ARCH_SAVE_REGS;
1053 /* FIXME: in case of extra iteration (WSAEINTR), substract time
1054 * from the initial timeout */
1057 _wapi_FD_SET (sock, &fds);
1059 divvy = div (timeout, 1000000);
1060 tv.tv_sec = divvy.quot;
1061 tv.tv_usec = divvy.rem;
1067 if (mode == SelectModeRead) {
1068 ret = _wapi_select (0, &fds, NULL, NULL, tvptr);
1069 } else if (mode == SelectModeWrite) {
1070 ret = _wapi_select (0, NULL, &fds, NULL, tvptr);
1071 } else if (mode == SelectModeError) {
1072 ret = _wapi_select (0, NULL, NULL, &fds, tvptr);
1074 g_assert_not_reached ();
1076 } while ((ret == SOCKET_ERROR) && (*error == WSAGetLastError ()) == WSAEINTR);
1078 return (ret != SOCKET_ERROR && _wapi_FD_ISSET (sock, &fds));
1081 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1083 struct sockaddr *sa;
1087 MONO_ARCH_SAVE_REGS;
1091 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1097 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));
1100 ret = _wapi_connect (sock, sa, sa_size);
1101 if(ret==SOCKET_ERROR) {
1102 *error = WSAGetLastError ();
1108 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1115 MONO_ARCH_SAVE_REGS;
1119 alen=mono_array_length(buffer);
1120 if(offset+count>alen) {
1124 buf=mono_array_addr(buffer, guchar, offset);
1126 ret = _wapi_recv (sock, buf, count, recvflags);
1127 if(ret==SOCKET_ERROR) {
1128 *error = WSAGetLastError ();
1135 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1141 struct sockaddr *sa;
1144 MONO_ARCH_SAVE_REGS;
1148 alen=mono_array_length(buffer);
1149 if(offset+count>alen) {
1153 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1158 buf=mono_array_addr(buffer, guchar, offset);
1160 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1161 if(ret==SOCKET_ERROR) {
1163 *error = WSAGetLastError ();
1167 /* If we didn't get a socket size, then we're probably a
1168 * connected connection-oriented socket and the stack hasn't
1169 * returned the remote address. All we can do is return null.
1172 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1181 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1188 MONO_ARCH_SAVE_REGS;
1192 alen=mono_array_length(buffer);
1193 if(offset+count>alen) {
1198 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1201 buf=mono_array_addr(buffer, guchar, offset);
1204 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1207 ret = _wapi_send (sock, buf, count, sendflags);
1208 if(ret==SOCKET_ERROR) {
1209 *error = WSAGetLastError ();
1216 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1222 struct sockaddr *sa;
1225 MONO_ARCH_SAVE_REGS;
1229 alen=mono_array_length(buffer);
1230 if(offset+count>alen) {
1234 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1240 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1243 buf=mono_array_addr(buffer, guchar, offset);
1246 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1249 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1250 if(ret==SOCKET_ERROR) {
1251 *error = WSAGetLastError ();
1259 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1262 MonoClassField *field;
1264 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1265 sock=*(SOCKET *)(((char *)sockobj)+field->offset);
1270 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **read_socks, MonoArray **write_socks, MonoArray **err_socks, gint32 timeout, gint32 *error)
1272 fd_set readfds, writefds, errfds;
1273 fd_set *readptr = NULL, *writeptr = NULL, *errptr = NULL;
1277 int readarrsize = 0, writearrsize = 0, errarrsize = 0;
1278 MonoDomain *domain=mono_domain_get();
1279 MonoClass *sock_arr_class;
1285 MONO_ARCH_SAVE_REGS;
1288 readarrsize=mono_array_length(*read_socks);
1292 if(readarrsize>FD_SETSIZE) {
1300 for(i=0; i<readarrsize; i++) {
1301 handle = Socket_to_SOCKET(mono_array_get(*read_socks, MonoObject *, i));
1302 _wapi_FD_SET(handle, &readfds);
1307 writearrsize=mono_array_length(*write_socks);
1309 if(writearrsize>FD_SETSIZE) {
1315 writeptr = &writefds;
1317 for(i=0; i<writearrsize; i++) {
1318 handle = Socket_to_SOCKET(mono_array_get(*write_socks, MonoObject *, i));
1319 _wapi_FD_SET(handle, &writefds);
1324 errarrsize=mono_array_length(*err_socks);
1326 if(errarrsize>FD_SETSIZE) {
1334 for(i=0; i<errarrsize; i++) {
1335 handle = Socket_to_SOCKET(mono_array_get(*err_socks, MonoObject *, i));
1336 _wapi_FD_SET(handle, &errfds);
1340 /* Negative timeout meaning block until ready is only
1341 * specified in Poll, not Select
1344 divvy = div (timeout, 1000000);
1348 tv.tv_sec=divvy.quot;
1349 tv.tv_usec=divvy.rem;
1351 ret = _wapi_select (0, readptr, writeptr, errptr, &tv);
1353 ret = _wapi_select (0, readptr, writeptr, errptr, NULL);
1355 } while ((ret==SOCKET_ERROR) && (WSAGetLastError() == WSAEINTR));
1357 if(ret==SOCKET_ERROR) {
1358 *error = WSAGetLastError ();
1363 sock_arr_class=((MonoObject *)*read_socks)->vtable->klass;
1366 for(i=0; i<readarrsize; i++) {
1367 if(_wapi_FD_ISSET(Socket_to_SOCKET(mono_array_get(*read_socks, MonoObject *, i)), &readfds)) {
1371 socks=mono_array_new_full(domain, sock_arr_class, &count, NULL);
1373 for(i=0; i<readarrsize; i++) {
1374 MonoObject *sock=mono_array_get(*read_socks, MonoObject *, i);
1376 if(_wapi_FD_ISSET(Socket_to_SOCKET(sock), &readfds)) {
1377 mono_array_set(socks, MonoObject *, count, sock);
1387 sock_arr_class=((MonoObject *)*write_socks)->vtable->klass;
1389 for(i=0; i<writearrsize; i++) {
1390 if(_wapi_FD_ISSET(Socket_to_SOCKET(mono_array_get(*write_socks, MonoObject *, i)), &writefds)) {
1394 socks=mono_array_new_full(domain, sock_arr_class, &count, NULL);
1396 for(i=0; i<writearrsize; i++) {
1397 MonoObject *sock=mono_array_get(*write_socks, MonoObject *, i);
1399 if(_wapi_FD_ISSET(Socket_to_SOCKET(sock), &writefds)) {
1400 mono_array_set(socks, MonoObject *, count, sock);
1406 *write_socks = NULL;
1410 sock_arr_class=((MonoObject *)*err_socks)->vtable->klass;
1412 for(i=0; i<errarrsize; i++) {
1413 if(_wapi_FD_ISSET(Socket_to_SOCKET(mono_array_get(*err_socks, MonoObject *, i)), &errfds)) {
1417 socks=mono_array_new_full(domain, sock_arr_class, &count, NULL);
1419 for(i=0; i<errarrsize; i++) {
1420 MonoObject *sock=mono_array_get(*err_socks, MonoObject *, i);
1422 if(_wapi_FD_ISSET(Socket_to_SOCKET(sock), &errfds)) {
1423 mono_array_set(socks, MonoObject *, count, sock);
1431 static MonoObject* int_to_object (MonoDomain *domain, int val)
1433 return mono_value_box (domain, mono_get_int32_class (), &val);
1437 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1443 int valsize=sizeof(val);
1444 struct linger linger;
1445 int lingersize=sizeof(linger);
1447 int time_ms_size = sizeof (time_ms);
1450 int credsize = sizeof(cred);
1452 MonoDomain *domain=mono_domain_get();
1454 MonoClass *obj_class;
1455 MonoClassField *field;
1457 MONO_ARCH_SAVE_REGS;
1461 ret=convert_sockopt_level_and_name(level, name, &system_level,
1464 *error = WSAENOPROTOOPT;
1468 /* No need to deal with MulticastOption names here, because
1469 * you cant getsockopt AddMembership or DropMembership (the
1470 * int getsockopt will error, causing an exception)
1473 case SocketOptionName_Linger:
1474 case SocketOptionName_DontLinger:
1475 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1479 case SocketOptionName_SendTimeout:
1480 case SocketOptionName_ReceiveTimeout:
1481 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1485 case SocketOptionName_PeerCred:
1486 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1492 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1496 if(ret==SOCKET_ERROR) {
1497 *error = WSAGetLastError ();
1502 case SocketOptionName_Linger:
1503 /* build a System.Net.Sockets.LingerOption */
1504 obj_class=mono_class_from_name(system_assembly,
1505 "System.Net.Sockets",
1507 obj=mono_object_new(domain, obj_class);
1509 /* Locate and set the fields "bool enabled" and "int
1512 field=mono_class_get_field_from_name(obj_class, "enabled");
1513 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1515 field=mono_class_get_field_from_name(obj_class, "seconds");
1516 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1520 case SocketOptionName_DontLinger:
1521 /* construct a bool int in val - true if linger is off */
1522 obj = int_to_object (domain, !linger.l_onoff);
1525 case SocketOptionName_SendTimeout:
1526 case SocketOptionName_ReceiveTimeout:
1527 obj = int_to_object (domain, time_ms);
1531 case SocketOptionName_PeerCred:
1533 /* build a Mono.Posix.PeerCred+PeerCredData if
1536 static MonoImage *mono_posix_image = NULL;
1537 MonoPeerCredData *cred_data;
1539 if (mono_posix_image == NULL) {
1540 mono_posix_image=mono_image_loaded ("Mono.Posix");
1541 if (!mono_posix_image) {
1542 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1544 *error = WSAENOPROTOOPT;
1547 mono_posix_image = mono_assembly_get_image (sa);
1552 obj_class = mono_class_from_name(mono_posix_image,
1554 "PeerCred/PeerCredData");
1555 obj = mono_object_new(domain, obj_class);
1556 cred_data = (MonoPeerCredData *)obj;
1557 cred_data->pid = cred.pid;
1558 cred_data->uid = cred.uid;
1559 cred_data->gid = cred.gid;
1565 obj = int_to_object (domain, val);
1571 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1579 MONO_ARCH_SAVE_REGS;
1583 ret=convert_sockopt_level_and_name(level, name, &system_level,
1586 *error = WSAENOPROTOOPT;
1590 valsize=mono_array_length(*byte_val);
1591 buf=mono_array_addr(*byte_val, guchar, 0);
1593 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1594 if(ret==SOCKET_ERROR) {
1595 *error = WSAGetLastError ();
1599 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1600 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1602 struct in_addr inaddr;
1603 MonoClassField *field;
1605 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "address");
1607 /* No idea why .net uses a 64bit type to hold a 32bit value...
1609 * Internal value of IPAddess is in Network Order, there is no need
1610 * to call htonl here.
1612 inaddr.s_addr=(guint32)*(guint64 *)(((char *)ipaddr)+field->offset);
1619 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1621 struct in6_addr in6addr;
1622 MonoClassField *field;
1626 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "_numbers");
1627 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1629 /* Solaris has only the 8 bit version. */
1631 for(i=0; i<8; i++) {
1632 guint16 s = mono_array_get (data, guint16, i);
1633 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1634 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1638 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1642 #endif /* AF_INET6 */
1644 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)
1655 #ifdef HAVE_SOL_IPV6
1656 sol_ipv6 = SOL_IPV6;
1659 struct protoent *pent;
1660 pent = getprotobyname ("ipv6");
1661 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
1669 struct protoent *pent;
1670 pent = getprotobyname ("ip");
1671 sol_ip = (pent != NULL) ? pent->p_proto : 0;
1674 #endif /* AF_INET6 */
1676 MONO_ARCH_SAVE_REGS;
1678 ret=convert_sockopt_level_and_name(level, name, &system_level,
1681 *error = WSAENOPROTOOPT;
1685 /* Only one of obj_val, byte_val or int_val has data */
1687 MonoClassField *field;
1688 struct linger linger;
1692 case SocketOptionName_DontLinger:
1695 valsize=sizeof(linger);
1696 ret = _wapi_setsockopt (sock, system_level,
1697 system_name, &linger, valsize);
1700 case SocketOptionName_Linger:
1701 /* Dig out "bool enabled" and "int seconds"
1704 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
1705 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
1706 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
1707 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
1709 valsize=sizeof(linger);
1710 ret = _wapi_setsockopt (sock, system_level,
1711 system_name, &linger, valsize);
1713 case SocketOptionName_AddMembership:
1714 case SocketOptionName_DropMembership:
1715 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1717 MonoObject *address = NULL;
1720 if(system_level == sol_ipv6) {
1721 struct ipv6_mreq mreq6;
1726 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1727 address = *(gpointer *)(((char *)obj_val) + field->offset);
1730 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
1733 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
1734 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
1736 ret = _wapi_setsockopt (sock, system_level,
1737 system_name, &mreq6,
1739 } else if(system_level == sol_ip)
1740 #endif /* AF_INET6 */
1742 #ifdef HAVE_STRUCT_IP_MREQN
1743 struct ip_mreqn mreq = {{0}};
1745 struct ip_mreq mreq = {{0}};
1746 #endif /* HAVE_STRUCT_IP_MREQN */
1748 /* pain! MulticastOption holds two IPAddress
1749 * members, so I have to dig the value out of
1752 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
1753 address = *(gpointer *)(((char *)obj_val) + field->offset);
1755 /* address might not be defined and if so, set the address to ADDR_ANY.
1758 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
1761 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
1762 address = *(gpointer *)(((char *)obj_val) + field->offset);
1764 #ifdef HAVE_STRUCT_IP_MREQN
1766 mreq.imr_address = ipaddress_to_struct_in_addr (address);
1770 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
1772 #endif /* HAVE_STRUCT_IP_MREQN */
1774 ret = _wapi_setsockopt (sock, system_level,
1780 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
1782 /* Cause an exception to be thrown */
1786 } else if (byte_val!=NULL) {
1787 int valsize=mono_array_length(byte_val);
1788 guchar *buf=mono_array_addr(byte_val, guchar, 0);
1790 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
1791 if(ret==SOCKET_ERROR) {
1792 *error = WSAGetLastError ();
1796 /* ReceiveTimeout/SendTimeout get here */
1797 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
1800 if(ret==SOCKET_ERROR) {
1801 *error = WSAGetLastError ();
1805 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
1811 MONO_ARCH_SAVE_REGS;
1815 /* Currently, the values for how (recv=0, send=1, both=2) match
1818 ret = _wapi_shutdown (sock, how);
1819 if(ret==SOCKET_ERROR) {
1820 *error = WSAGetLastError ();
1825 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
1827 MonoArray *output, gint32 *error)
1829 gulong output_bytes = 0;
1830 gchar *i_buffer, *o_buffer;
1834 MONO_ARCH_SAVE_REGS;
1838 if (code == FIONBIO) {
1839 /* Invalid command. Must use Socket.Blocking */
1843 if (input == NULL) {
1847 i_buffer = mono_array_addr (input, gchar, 0);
1848 i_len = mono_array_length (input);
1851 if (output == NULL) {
1855 o_buffer = mono_array_addr (output, gchar, 0);
1856 o_len = mono_array_length (output);
1859 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
1860 if (ret == SOCKET_ERROR) {
1861 *error = WSAGetLastError ();
1865 return (gint) output_bytes;
1869 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
1870 MonoArray **h_aliases,
1871 MonoArray **h_addr_list)
1873 MonoDomain *domain = mono_domain_get ();
1876 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
1880 *h_name=mono_string_new(domain, he->h_name);
1883 while(he->h_aliases[i]!=NULL) {
1887 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
1889 while(he->h_aliases[i]!=NULL) {
1892 alias=mono_string_new(domain, he->h_aliases[i]);
1893 mono_array_set(*h_aliases, MonoString *, i, alias);
1898 while(he->h_addr_list[i]!=NULL) {
1902 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
1904 while(he->h_addr_list[i]!=NULL) {
1905 MonoString *addr_string;
1908 g_snprintf(addr, 16, "%u.%u.%u.%u",
1909 (unsigned char)he->h_addr_list[i][0],
1910 (unsigned char)he->h_addr_list[i][1],
1911 (unsigned char)he->h_addr_list[i][2],
1912 (unsigned char)he->h_addr_list[i][3]);
1914 addr_string=mono_string_new(domain, addr);
1915 mono_array_set(*h_addr_list, MonoString *, i, addr_string);
1923 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
1924 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
1926 MonoDomain *domain = mono_domain_get ();
1927 int i, host_count, host_index, family_hint;
1929 family_hint = get_family_hint ();
1931 if(he1 == NULL && he2 == NULL) {
1936 * Check if address length and family are correct
1938 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
1942 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
1947 * Get the aliases and host name from he1 or he2 whichever is
1948 * not null, if he1 is not null then take aliases from he1
1950 if (he1 != NULL && (family_hint == PF_UNSPEC ||
1951 family_hint == PF_INET)) {
1952 *h_name=mono_string_new (domain, he1->h_name);
1955 while(he1->h_aliases[i]!=NULL) {
1959 *h_aliases=mono_array_new (domain, mono_get_string_class (),
1962 while(he1->h_aliases[i]!=NULL) {
1965 alias=mono_string_new (domain, he1->h_aliases[i]);
1966 mono_array_set (*h_aliases, MonoString *, i, alias);
1969 } else if (family_hint == PF_UNSPEC || family_hint == PF_INET6) {
1970 *h_name=mono_string_new (domain, he2->h_name);
1973 while(he2->h_aliases [i] != NULL) {
1977 *h_aliases=mono_array_new (domain, mono_get_string_class (),
1980 while(he2->h_aliases[i]!=NULL) {
1983 alias=mono_string_new (domain, he2->h_aliases[i]);
1984 mono_array_set (*h_aliases, MonoString *, i, alias);
1990 * Count the number of addresses in he1 + he2
1993 if (he1 != NULL && (family_hint == PF_UNSPEC ||
1994 family_hint == PF_INET)) {
1996 while(he1->h_addr_list[i]!=NULL) {
2002 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2003 family_hint == PF_INET6)) {
2005 while(he2->h_addr_list[i]!=NULL) {
2014 *h_addr_list=mono_array_new (domain, mono_get_string_class (),
2019 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2020 family_hint == PF_INET6)) {
2022 while(he2->h_addr_list[i] != NULL) {
2023 MonoString *addr_string;
2026 inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2029 addr_string = mono_string_new (domain, addr);
2030 mono_array_set (*h_addr_list, MonoString *, host_index,
2037 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2038 family_hint == PF_INET)) {
2040 while(he1->h_addr_list[i] != NULL) {
2041 MonoString *addr_string;
2044 inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2047 addr_string=mono_string_new (domain, addr);
2048 mono_array_set (*h_addr_list, MonoString *, host_index,
2059 #if defined(AF_INET6)
2061 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2062 MonoArray **h_aliases,
2063 MonoArray **h_addr_list)
2066 struct addrinfo *ai = NULL;
2068 MonoDomain *domain = mono_domain_get ();
2070 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2071 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2078 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2079 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2081 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2082 MonoString *addr_string;
2085 gint32 buffer_size = 0;
2087 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2093 buffer = g_malloc0(buffer_size);
2095 if(ai->ai_family == PF_INET) {
2096 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, buffer_size);
2098 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, buffer_size);
2105 } while(ret == 0 && errno == ENOSPC);
2108 addr_string=mono_string_new(domain, buffer);
2111 addr_string=mono_string_new(domain, "");
2114 mono_array_set(*h_addr_list, MonoString *, i, addr_string);
2116 if(!i && ai->ai_canonname != NULL) {
2117 *h_name=mono_string_new(domain, ai->ai_canonname);
2132 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2134 #if !defined(HAVE_GETHOSTBYNAME2_R)
2135 struct addrinfo *info = NULL, hints;
2138 MONO_ARCH_SAVE_REGS;
2140 hostname=mono_string_to_utf8 (host);
2142 memset(&hints, 0, sizeof(hints));
2143 hints.ai_family = get_family_hint ();
2144 hints.ai_socktype = SOCK_STREAM;
2145 hints.ai_flags = AI_CANONNAME;
2147 if (getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2153 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list));
2155 struct hostent he1,*hp1, he2, *hp2;
2156 int buffer_size1, buffer_size2;
2157 char *buffer1, *buffer2;
2159 gboolean return_value;
2162 MONO_ARCH_SAVE_REGS;
2164 hostname=mono_string_to_utf8 (host);
2168 buffer1 = g_malloc0(buffer_size1);
2169 buffer2 = g_malloc0(buffer_size2);
2171 while (gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2172 &hp1, &herr) == ERANGE) {
2174 buffer1 = g_realloc(buffer1, buffer_size1);
2179 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2180 buffer_size2, &hp2, &herr) == ERANGE) {
2182 buffer2 = g_realloc(buffer2, buffer_size2);
2188 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2195 return(return_value);
2196 #endif /* HAVE_GETHOSTBYNAME2_R */
2198 #else /* AF_INET6 */
2199 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2204 MONO_ARCH_SAVE_REGS;
2206 hostname=mono_string_to_utf8(host);
2208 he = _wapi_gethostbyname (hostname);
2215 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list));
2217 #endif /* AF_INET6 */
2219 #ifndef HAVE_INET_PTON
2221 inet_pton (int family, const char *address, void *inaddrp)
2223 if (family == AF_INET) {
2224 #ifdef HAVE_INET_ATON
2225 struct in_addr inaddr;
2227 if (!inet_aton (address, &inaddr))
2230 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2233 /* assume the system has inet_addr(), if it doesn't
2234 have that we're pretty much screwed... */
2237 if (!strcmp (address, "255.255.255.255")) {
2238 /* special-case hack */
2239 inaddr = 0xffffffff;
2241 inaddr = inet_addr (address);
2243 #define INADDR_NONE ((in_addr_t) -1)
2245 if (inaddr == INADDR_NONE)
2249 memcpy (inaddrp, &inaddr, sizeof (guint32));
2251 #endif /* HAVE_INET_ATON */
2256 #endif /* !HAVE_INET_PTON */
2258 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2263 struct sockaddr_in saddr;
2264 struct sockaddr_in6 saddr6;
2265 struct addrinfo *info = NULL, hints;
2267 char hostname[1024] = {0};
2269 struct in_addr inaddr;
2273 MONO_ARCH_SAVE_REGS;
2275 address = mono_string_to_utf8 (addr);
2278 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2279 /* Maybe an ipv6 address */
2280 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2286 saddr6.sin6_family = AF_INET6;
2291 saddr.sin_family = AF_INET;
2295 if(family == AF_INET) {
2296 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2297 hostname, sizeof(hostname), NULL, 0,
2298 NI_NAMEREQD) != 0) {
2301 } else if(family == AF_INET6) {
2302 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2303 hostname, sizeof(hostname), NULL, 0,
2304 NI_NAMEREQD) != 0) {
2309 memset (&hints, 0, sizeof(hints));
2310 hints.ai_family = get_family_hint ();
2311 hints.ai_socktype = SOCK_STREAM;
2312 hints.ai_flags = AI_CANONNAME;
2314 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
2318 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list));
2320 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
2326 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
2330 return(hostent_to_IPHostEntry (he, h_name, h_aliases, h_addr_list));
2334 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
2336 guchar hostname[256];
2339 MONO_ARCH_SAVE_REGS;
2341 ret = gethostname (hostname, sizeof (hostname));
2346 *h_name=mono_string_new(mono_domain_get (), hostname);
2351 void mono_network_init(void)
2356 err=WSAStartup(MAKEWORD(2,0), &wsadata);
2358 g_error(G_GNUC_PRETTY_FUNCTION ": Couldn't initialise networking");
2363 g_message(G_GNUC_PRETTY_FUNCTION ": Using socket library: %s", wsadata.szDescription);
2364 g_message(G_GNUC_PRETTY_FUNCTION ": Socket system status: %s", wsadata.szSystemStatus);
2368 void mono_network_cleanup(void)