2 * socket-io.c: Socket IO internal calls
5 * Dick Porter (dick@ximian.com)
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * (C) 2001 Ximian, Inc.
9 * Copyright (c) 2005-2006 Novell, Inc. (http://www.novell.com)
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/ioctl.h>
25 #include <netinet/in.h>
26 #include <netinet/tcp.h>
28 #include <arpa/inet.h>
30 #include <mono/metadata/object.h>
31 #include <mono/io-layer/io-layer.h>
32 #include <mono/metadata/socket-io.h>
33 #include <mono/metadata/exception.h>
34 #include <mono/metadata/assembly.h>
35 #include <mono/metadata/appdomain.h>
36 #include <mono/metadata/threads.h>
37 #include <mono/metadata/threads-types.h>
38 #include <mono/utils/mono-poll.h>
39 /* FIXME change this code to not mess so much with the internals */
40 #include <mono/metadata/class-internals.h>
41 #include <mono/metadata/threadpool-internals.h>
42 #include <mono/metadata/domain-internals.h>
44 #ifdef HAVE_SYS_TIME_H
47 #ifdef HAVE_SYS_IOCTL_H
48 #include <sys/ioctl.h>
57 #ifdef HAVE_SYS_FILIO_H
58 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
60 #ifdef HAVE_SYS_SOCKIO_H
61 #include <sys/sockio.h> /* defines SIOCATMARK */
67 #include "mono/io-layer/socket-wrappers.h"
70 /* This is a kludge to make this file build under cygwin:
71 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
72 * prototypes for some but not all required functions (notably
73 * inet_ntop() is missing), but the libws2_32 library is missing the
74 * actual implementations of these functions.
81 static gint32 convert_family(MonoAddressFamily mono_family)
86 case AddressFamily_Unknown:
87 case AddressFamily_ImpLink:
88 case AddressFamily_Pup:
89 case AddressFamily_Chaos:
90 case AddressFamily_Iso:
91 case AddressFamily_Ecma:
92 case AddressFamily_DataKit:
93 case AddressFamily_Ccitt:
94 case AddressFamily_DataLink:
95 case AddressFamily_Lat:
96 case AddressFamily_HyperChannel:
97 case AddressFamily_NetBios:
98 case AddressFamily_VoiceView:
99 case AddressFamily_FireFox:
100 case AddressFamily_Banyan:
101 case AddressFamily_Atm:
102 case AddressFamily_Cluster:
103 case AddressFamily_Ieee12844:
104 case AddressFamily_NetworkDesigners:
105 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
108 case AddressFamily_Unspecified:
112 case AddressFamily_Unix:
116 case AddressFamily_InterNetwork:
120 case AddressFamily_Ipx:
126 case AddressFamily_Sna:
130 case AddressFamily_DecNet:
134 case AddressFamily_AppleTalk:
138 case AddressFamily_InterNetworkV6:
143 case AddressFamily_Irda:
149 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
155 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
157 MonoAddressFamily family=AddressFamily_Unknown;
161 family=AddressFamily_Unspecified;
165 family=AddressFamily_Unix;
169 family=AddressFamily_InterNetwork;
174 family=AddressFamily_Ipx;
179 family=AddressFamily_Sna;
183 family=AddressFamily_DecNet;
187 family=AddressFamily_AppleTalk;
192 family=AddressFamily_InterNetworkV6;
198 family=AddressFamily_Irda;
202 g_warning("unknown address family 0x%x", af_family);
208 static gint32 convert_type(MonoSocketType mono_type)
213 case SocketType_Stream:
217 case SocketType_Dgram:
229 case SocketType_Seqpacket:
233 case SocketType_Unknown:
234 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
238 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
244 static gint32 convert_proto(MonoProtocolType mono_proto)
249 case ProtocolType_IP:
250 case ProtocolType_IPv6:
251 case ProtocolType_Icmp:
252 case ProtocolType_Igmp:
253 case ProtocolType_Ggp:
254 case ProtocolType_Tcp:
255 case ProtocolType_Pup:
256 case ProtocolType_Udp:
257 case ProtocolType_Idp:
258 /* These protocols are known (on my system at least) */
262 case ProtocolType_ND:
263 case ProtocolType_Raw:
264 case ProtocolType_Ipx:
265 case ProtocolType_Spx:
266 case ProtocolType_SpxII:
267 case ProtocolType_Unknown:
268 /* These protocols arent */
269 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
279 /* Convert MonoSocketFlags */
280 static gint32 convert_socketflags (gint32 sflags)
285 /* SocketFlags.None */
288 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
289 SocketFlags_DontRoute | SocketFlags_Partial))
290 /* Contains invalid flag values */
293 if (sflags & SocketFlags_OutOfBand)
295 if (sflags & SocketFlags_Peek)
297 if (sflags & SocketFlags_DontRoute)
298 flags |= MSG_DONTROUTE;
300 /* Ignore Partial - see bug 349688. Don't return -1, because
301 * according to the comment in that bug ms runtime doesn't for
302 * UDP sockets (this means we will silently ignore it for TCP
305 if (sflags & SocketFlags_Partial)
312 if (sflags & SocketFlags_MaxIOVectorLength)
313 /* FIXME: Don't know what to do for MaxIOVectorLength query */
316 return (flags ? flags : -1);
321 * 0 on success (mapped mono_level and mono_name to system_level and system_name
323 * -2 on non-fatal error (ie, must ignore)
325 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
326 MonoSocketOptionName mono_name,
330 switch (mono_level) {
331 case SocketOptionLevel_Socket:
332 *system_level = SOL_SOCKET;
335 case SocketOptionName_DontLinger:
336 /* This is SO_LINGER, because the setsockopt
337 * internal call maps DontLinger to SO_LINGER
340 *system_name = SO_LINGER;
342 case SocketOptionName_Debug:
343 *system_name = SO_DEBUG;
346 case SocketOptionName_AcceptConnection:
347 *system_name = SO_ACCEPTCONN;
350 case SocketOptionName_ReuseAddress:
351 *system_name = SO_REUSEADDR;
353 case SocketOptionName_KeepAlive:
354 *system_name = SO_KEEPALIVE;
356 case SocketOptionName_DontRoute:
357 *system_name = SO_DONTROUTE;
359 case SocketOptionName_Broadcast:
360 *system_name = SO_BROADCAST;
362 case SocketOptionName_Linger:
363 *system_name = SO_LINGER;
365 case SocketOptionName_OutOfBandInline:
366 *system_name = SO_OOBINLINE;
368 case SocketOptionName_SendBuffer:
369 *system_name = SO_SNDBUF;
371 case SocketOptionName_ReceiveBuffer:
372 *system_name = SO_RCVBUF;
374 case SocketOptionName_SendLowWater:
375 *system_name = SO_SNDLOWAT;
377 case SocketOptionName_ReceiveLowWater:
378 *system_name = SO_RCVLOWAT;
380 case SocketOptionName_SendTimeout:
381 *system_name = SO_SNDTIMEO;
383 case SocketOptionName_ReceiveTimeout:
384 *system_name = SO_RCVTIMEO;
386 case SocketOptionName_Error:
387 *system_name = SO_ERROR;
389 case SocketOptionName_Type:
390 *system_name = SO_TYPE;
393 case SocketOptionName_PeerCred:
394 *system_name = SO_PEERCRED;
397 case SocketOptionName_ExclusiveAddressUse:
398 case SocketOptionName_UseLoopback:
399 case SocketOptionName_MaxConnections:
400 /* Can't figure out how to map these, so fall
404 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
409 case SocketOptionLevel_IP:
411 *system_level = SOL_IP;
414 static int cached = 0;
418 struct protoent *pent;
420 pent = getprotobyname ("IP");
421 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
425 *system_level = proto;
427 #endif /* HAVE_SOL_IP */
430 case SocketOptionName_IPOptions:
431 *system_name = IP_OPTIONS;
434 case SocketOptionName_HeaderIncluded:
435 *system_name = IP_HDRINCL;
439 case SocketOptionName_TypeOfService:
440 *system_name = IP_TOS;
444 case SocketOptionName_IpTimeToLive:
445 *system_name = IP_TTL;
448 case SocketOptionName_MulticastInterface:
449 *system_name = IP_MULTICAST_IF;
451 case SocketOptionName_MulticastTimeToLive:
452 *system_name = IP_MULTICAST_TTL;
454 case SocketOptionName_MulticastLoopback:
455 *system_name = IP_MULTICAST_LOOP;
457 case SocketOptionName_AddMembership:
458 *system_name = IP_ADD_MEMBERSHIP;
460 case SocketOptionName_DropMembership:
461 *system_name = IP_DROP_MEMBERSHIP;
463 #ifdef HAVE_IP_PKTINFO
464 case SocketOptionName_PacketInformation:
465 *system_name = IP_PKTINFO;
467 #endif /* HAVE_IP_PKTINFO */
469 case SocketOptionName_DontFragment:
470 #ifdef HAVE_IP_DONTFRAGMENT
471 *system_name = IP_DONTFRAGMENT;
473 #elif defined HAVE_IP_MTU_DISCOVER
474 /* Not quite the same */
475 *system_name = IP_MTU_DISCOVER;
478 /* If the flag is not available on this system, we can ignore this error */
480 #endif /* HAVE_IP_DONTFRAGMENT */
481 case SocketOptionName_AddSourceMembership:
482 case SocketOptionName_DropSourceMembership:
483 case SocketOptionName_BlockSource:
484 case SocketOptionName_UnblockSource:
485 /* Can't figure out how to map these, so fall
489 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
495 case SocketOptionLevel_IPv6:
497 *system_level = SOL_IPV6;
500 static int cached = 0;
504 struct protoent *pent;
506 pent = getprotobyname ("IPV6");
507 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
511 *system_level = proto;
513 #endif /* HAVE_SOL_IPV6 */
516 case SocketOptionName_IpTimeToLive:
517 *system_name = IPV6_UNICAST_HOPS;
519 case SocketOptionName_MulticastInterface:
520 *system_name = IPV6_MULTICAST_IF;
522 case SocketOptionName_MulticastTimeToLive:
523 *system_name = IPV6_MULTICAST_HOPS;
525 case SocketOptionName_MulticastLoopback:
526 *system_name = IPV6_MULTICAST_LOOP;
528 case SocketOptionName_AddMembership:
529 *system_name = IPV6_JOIN_GROUP;
531 case SocketOptionName_DropMembership:
532 *system_name = IPV6_LEAVE_GROUP;
534 case SocketOptionName_PacketInformation:
535 #ifdef HAVE_IPV6_PKTINFO
536 *system_name = IPV6_PKTINFO;
539 case SocketOptionName_HeaderIncluded:
540 case SocketOptionName_IPOptions:
541 case SocketOptionName_TypeOfService:
542 case SocketOptionName_DontFragment:
543 case SocketOptionName_AddSourceMembership:
544 case SocketOptionName_DropSourceMembership:
545 case SocketOptionName_BlockSource:
546 case SocketOptionName_UnblockSource:
547 /* Can't figure out how to map these, so fall
551 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
555 break; /* SocketOptionLevel_IPv6 */
558 case SocketOptionLevel_Tcp:
560 *system_level = SOL_TCP;
563 static int cached = 0;
567 struct protoent *pent;
569 pent = getprotobyname ("TCP");
570 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
574 *system_level = proto;
576 #endif /* HAVE_SOL_TCP */
579 case SocketOptionName_NoDelay:
580 *system_name = TCP_NODELAY;
583 /* The documentation is talking complete
584 * bollocks here: rfc-1222 is titled
585 * 'Advancing the NSFNET Routing Architecture'
586 * and doesn't mention either of the words
587 * "expedite" or "urgent".
589 case SocketOptionName_BsdUrgent:
590 case SocketOptionName_Expedited:
593 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
598 case SocketOptionLevel_Udp:
599 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
602 case SocketOptionName_NoChecksum:
603 case SocketOptionName_ChecksumCoverage:
605 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
612 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
619 static MonoImage *get_socket_assembly (void)
621 static const char *version = NULL;
622 static gboolean moonlight;
623 static MonoImage *socket_assembly = NULL;
625 if (version == NULL) {
626 version = mono_get_runtime_info ()->framework_version;
627 moonlight = !strcmp (version, "2.1");
630 if (socket_assembly == NULL) {
632 socket_assembly = mono_image_loaded ("System.Net");
633 if (!socket_assembly) {
634 MonoAssembly *sa = mono_assembly_open ("System.Net.dll", NULL);
637 g_assert_not_reached ();
639 socket_assembly = mono_assembly_get_image (sa);
643 socket_assembly = mono_image_loaded ("System");
644 if (!socket_assembly) {
645 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
648 g_assert_not_reached ();
650 socket_assembly = mono_assembly_get_image (sa);
656 return(socket_assembly);
660 static gint32 get_family_hint(void)
662 MonoDomain *domain = mono_domain_get ();
664 if (!domain->inet_family_hint) {
665 MonoClass *socket_class;
666 MonoClassField *ipv6_field, *ipv4_field;
667 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
670 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
671 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
672 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
673 vtable = mono_class_vtable (mono_domain_get (), socket_class);
674 mono_runtime_class_init (vtable);
676 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
677 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
679 mono_domain_lock (domain);
680 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
681 domain->inet_family_hint = 1;
682 } else if (ipv4_enabled == 1) {
683 domain->inet_family_hint = 2;
685 domain->inet_family_hint = 3;
687 mono_domain_unlock (domain);
689 switch (domain->inet_family_hint) {
690 case 1: return PF_UNSPEC;
691 case 2: return PF_INET;
692 case 3: return PF_INET6;
699 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
710 sock_family=convert_family(family);
711 if(sock_family==-1) {
712 *error = WSAEAFNOSUPPORT;
716 sock_proto=convert_proto(proto);
718 *error = WSAEPROTONOSUPPORT;
722 sock_type=convert_type(type);
724 *error = WSAESOCKTNOSUPPORT;
728 sock = _wapi_socket (sock_family, sock_type, sock_proto,
729 NULL, 0, WSA_FLAG_OVERLAPPED);
731 if(sock==INVALID_SOCKET) {
732 *error = WSAGetLastError ();
736 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
737 return (GUINT_TO_POINTER (sock));
741 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
742 return (GUINT_TO_POINTER (sock));
746 return(GUINT_TO_POINTER (sock));
749 /* FIXME: the SOCKET parameter (here and in other functions in this
750 * file) is really an IntPtr which needs to be converted to a guint32.
752 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
758 g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
763 /* Clear any pending work item from this socket if the underlying
764 * polling system does not notify when the socket is closed */
765 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
769 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
774 g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
777 return(WSAGetLastError());
780 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
790 #if defined(PLATFORM_MACOSX)
792 socklen_t optlen = sizeof (amount);
793 ret=getsockopt (sock, SOL_SOCKET, SO_NREAD, &amount, &optlen);
796 ret=ioctlsocket(sock, FIONREAD, &amount);
798 if(ret==SOCKET_ERROR) {
799 *error = WSAGetLastError ();
806 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
817 * block == TRUE/FALSE means we will block/not block.
818 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
822 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
823 if(ret==SOCKET_ERROR) {
824 *error = WSAGetLastError ();
828 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
838 #ifdef PLATFORM_WIN32
839 /* Several applications are getting stuck during shutdown on Windows
840 * when an accept call is on a background thread.
844 MonoThread* curthread = mono_thread_current ();
852 FD_SET(sock, &readfds);
854 timeout.tv_usec = 1000;
855 selectret = select (0, &readfds, NULL, NULL, &timeout);
858 if (curthread->state & ThreadState_StopRequested)
865 newsock = _wapi_accept (sock, NULL, 0);
866 if(newsock==INVALID_SOCKET) {
867 *error = WSAGetLastError ();
871 return(GUINT_TO_POINTER (newsock));
874 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
884 ret = _wapi_listen (sock, backlog);
885 if(ret==SOCKET_ERROR) {
886 *error = WSAGetLastError ();
890 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
891 int sa_size, gint32 *error)
893 MonoDomain *domain = mono_domain_get ();
894 MonoObject *sockaddr_obj;
895 MonoClass *sockaddr_class;
896 MonoClassField *field;
898 MonoAddressFamily family;
900 /* Build a System.Net.SocketAddress object instance */
901 sockaddr_class=mono_class_from_name(get_socket_assembly (), "System.Net", "SocketAddress");
902 sockaddr_obj=mono_object_new(domain, sockaddr_class);
904 /* Locate the SocketAddress data buffer in the object */
905 field=mono_class_get_field_from_name(sockaddr_class, "data");
907 /* Make sure there is space for the family and size bytes */
909 if (saddr->sa_family == AF_UNIX) {
910 /* sa_len includes the entire sockaddr size, so we don't need the
911 * N bytes (sizeof (unsigned short)) of the family. */
912 data=mono_array_new(domain, mono_get_byte_class (), sa_size);
916 /* May be the +2 here is too conservative, as sa_len returns
917 * the length of the entire sockaddr_in/in6, including
918 * sizeof (unsigned short) of the family */
919 data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
922 /* The data buffer is laid out as follows:
923 * bytes 0 and 1 are the address family
924 * bytes 2 and 3 are the port info
925 * the rest is the address info
928 family=convert_to_mono_family(saddr->sa_family);
929 if(family==AddressFamily_Unknown) {
930 *error = WSAEAFNOSUPPORT;
934 mono_array_set(data, guint8, 0, family & 0x0FF);
935 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
937 if(saddr->sa_family==AF_INET) {
938 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
939 guint16 port=ntohs(sa_in->sin_port);
940 guint32 address=ntohl(sa_in->sin_addr.s_addr);
943 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
946 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
947 mono_array_set(data, guint8, 3, (port) & 0xff);
948 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
949 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
950 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
951 mono_array_set(data, guint8, 7, (address) & 0xff);
953 mono_field_set_value (sockaddr_obj, field, data);
955 return(sockaddr_obj);
957 } else if (saddr->sa_family == AF_INET6) {
958 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
961 guint16 port=ntohs(sa_in->sin6_port);
964 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
967 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
968 mono_array_set(data, guint8, 3, (port) & 0xff);
970 for(i=0; i<16; i++) {
971 mono_array_set(data, guint8, 8+i,
972 sa_in->sin6_addr.s6_addr[i]);
975 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
976 mono_array_set(data, guint8, 25,
977 (sa_in->sin6_scope_id >> 8) & 0xff);
978 mono_array_set(data, guint8, 26,
979 (sa_in->sin6_scope_id >> 16) & 0xff);
980 mono_array_set(data, guint8, 27,
981 (sa_in->sin6_scope_id >> 24) & 0xff);
983 mono_field_set_value (sockaddr_obj, field, data);
985 return(sockaddr_obj);
988 } else if (saddr->sa_family == AF_UNIX) {
991 for (i = 0; i < sa_size; i++) {
992 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
995 mono_field_set_value (sockaddr_obj, field, data);
1000 *error = WSAEAFNOSUPPORT;
1005 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
1007 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1011 MONO_ARCH_SAVE_REGS;
1016 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1018 if(ret==SOCKET_ERROR) {
1019 *error = WSAGetLastError ();
1024 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));
1027 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1031 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
1033 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1037 MONO_ARCH_SAVE_REGS;
1042 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1044 if(ret==SOCKET_ERROR) {
1045 *error = WSAGetLastError ();
1050 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));
1053 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1057 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1061 MonoClassField *field;
1066 /* Dig the SocketAddress data buffer out of the object */
1067 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1068 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1070 /* The data buffer is laid out as follows:
1071 * byte 0 is the address family low byte
1072 * byte 1 is the address family high byte
1074 * bytes 2 and 3 are the port info
1075 * the rest is the address info
1077 * the rest is the file name
1079 len = mono_array_length (data);
1081 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1084 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1085 if (family == AF_INET) {
1086 struct sockaddr_in *sa;
1091 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1094 sa = g_new0 (struct sockaddr_in, 1);
1095 port = (mono_array_get (data, guint8, 2) << 8) +
1096 mono_array_get (data, guint8, 3);
1097 address = (mono_array_get (data, guint8, 4) << 24) +
1098 (mono_array_get (data, guint8, 5) << 16 ) +
1099 (mono_array_get (data, guint8, 6) << 8) +
1100 mono_array_get (data, guint8, 7);
1102 sa->sin_family = family;
1103 sa->sin_addr.s_addr = htonl (address);
1104 sa->sin_port = htons (port);
1106 *sa_size = sizeof(struct sockaddr_in);
1107 return((struct sockaddr *)sa);
1110 } else if (family == AF_INET6) {
1111 struct sockaddr_in6 *sa;
1117 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1120 sa = g_new0 (struct sockaddr_in6, 1);
1121 port = mono_array_get (data, guint8, 3) +
1122 (mono_array_get (data, guint8, 2) << 8);
1123 scopeid = mono_array_get (data, guint8, 24) +
1124 (mono_array_get (data, guint8, 25) << 8) +
1125 (mono_array_get (data, guint8, 26) << 16) +
1126 (mono_array_get (data, guint8, 27) << 24);
1128 sa->sin6_family = family;
1129 sa->sin6_port = htons (port);
1130 sa->sin6_scope_id = scopeid;
1132 for(i=0; i<16; i++) {
1133 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1136 *sa_size = sizeof(struct sockaddr_in6);
1137 return((struct sockaddr *)sa);
1139 #ifdef HAVE_SYS_UN_H
1140 } else if (family == AF_UNIX) {
1141 struct sockaddr_un *sock_un;
1144 /* Need a byte for the '\0' terminator/prefix, and the first
1145 * two bytes hold the SocketAddress family
1147 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1148 mono_raise_exception (mono_get_exception_index_out_of_range ());
1151 sock_un = g_new0 (struct sockaddr_un, 1);
1153 sock_un->sun_family = family;
1154 for (i = 0; i < len - 2; i++) {
1155 sock_un->sun_path [i] = mono_array_get (data, guint8,
1161 return (struct sockaddr *)sock_un;
1164 *error = WSAEAFNOSUPPORT;
1169 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1171 struct sockaddr *sa;
1175 MONO_ARCH_SAVE_REGS;
1179 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1185 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));
1188 ret = _wapi_bind (sock, sa, sa_size);
1189 if(ret==SOCKET_ERROR) {
1190 *error = WSAGetLastError ();
1203 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1204 gint timeout, gint32 *error)
1206 MonoThread *thread = NULL;
1212 MONO_ARCH_SAVE_REGS;
1214 pfds = g_new0 (mono_pollfd, 1);
1215 pfds[0].fd = GPOINTER_TO_INT (sock);
1216 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1217 (mode == SelectModeWrite) ? MONO_POLLOUT :
1218 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1220 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1221 start = time (NULL);
1225 ret = mono_poll (pfds, 1, timeout);
1226 if (timeout > 0 && ret < 0) {
1228 int sec = time (NULL) - start;
1230 timeout -= sec * 1000;
1238 if (ret == -1 && errno == EINTR) {
1241 if (thread == NULL) {
1242 thread = mono_thread_current ();
1245 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1251 /* Suspend requested? */
1252 mono_thread_interruption_checkpoint ();
1256 } while (ret == -1 && errno == EINTR);
1259 #ifdef PLATFORM_WIN32
1260 *error = WSAGetLastError ();
1262 *error = errno_to_WSA (errno, __func__);
1277 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1279 struct sockaddr *sa;
1283 MONO_ARCH_SAVE_REGS;
1287 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1293 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));
1296 ret = _wapi_connect (sock, sa, sa_size);
1297 if(ret==SOCKET_ERROR) {
1298 *error = WSAGetLastError ();
1304 /* These #defines from mswsock.h from wine. Defining them here allows
1305 * us to build this file on a mingw box that doesn't know the magic
1306 * numbers, but still run on a newer windows box that does.
1308 #ifndef WSAID_DISCONNECTEX
1309 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1310 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1313 #ifndef WSAID_TRANSMITFILE
1314 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1315 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1318 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1321 glong output_bytes = 0;
1322 GUID disco_guid = WSAID_DISCONNECTEX;
1323 GUID trans_guid = WSAID_TRANSMITFILE;
1324 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1325 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1328 MONO_ARCH_SAVE_REGS;
1333 g_message("%s: disconnecting from socket %p (reuse %d)", __func__,
1337 /* I _think_ the extension function pointers need to be looked
1338 * up for each socket. FIXME: check the best way to store
1339 * pointers to functions in managed objects that still works
1340 * on 64bit platforms.
1342 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1343 (void *)&disco_guid, sizeof(GUID),
1344 (void *)&_wapi_disconnectex, sizeof(void *),
1345 &output_bytes, NULL, NULL);
1347 /* make sure that WSAIoctl didn't put crap in the
1350 _wapi_disconnectex = NULL;
1352 /* Look up the TransmitFile extension function pointer
1353 * instead of calling TransmitFile() directly, because
1354 * apparently "Several of the extension functions have
1355 * been available since WinSock 1.1 and are exported
1356 * from MSWsock.dll, however it's not advisable to
1357 * link directly to this dll as this ties you to the
1358 * Microsoft WinSock provider. A provider neutral way
1359 * of accessing these extension functions is to load
1360 * them dynamically via WSAIoctl using the
1361 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1362 * should, theoretically, allow you to access these
1363 * functions from any provider that supports them..."
1364 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1366 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1367 (void *)&trans_guid, sizeof(GUID),
1368 (void *)&_wapi_transmitfile, sizeof(void *),
1369 &output_bytes, NULL, NULL);
1371 _wapi_transmitfile = NULL;
1375 if (_wapi_disconnectex != NULL) {
1376 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1377 } else if (_wapi_transmitfile != NULL) {
1378 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1379 TF_DISCONNECT | TF_REUSE_SOCKET);
1381 *error = ERROR_NOT_SUPPORTED;
1385 if (bret == FALSE) {
1386 *error = WSAGetLastError ();
1390 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1397 MONO_ARCH_SAVE_REGS;
1401 alen = mono_array_length (buffer);
1402 if (offset > alen - count) {
1406 buf=mono_array_addr(buffer, guchar, offset);
1408 recvflags = convert_socketflags (flags);
1409 if (recvflags == -1) {
1410 *error = WSAEOPNOTSUPP;
1414 ret = _wapi_recv (sock, buf, count, recvflags);
1415 if(ret==SOCKET_ERROR) {
1416 *error = WSAGetLastError ();
1423 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1428 DWORD recvflags = 0;
1430 MONO_ARCH_SAVE_REGS;
1434 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1435 count = mono_array_length (buffers);
1437 recvflags = convert_socketflags (flags);
1438 if (recvflags == -1) {
1439 *error = WSAEOPNOTSUPP;
1443 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1444 if (ret == SOCKET_ERROR) {
1445 *error = WSAGetLastError ();
1452 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1458 struct sockaddr *sa;
1461 MONO_ARCH_SAVE_REGS;
1465 alen = mono_array_length (buffer);
1466 if (offset > alen - count) {
1470 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1475 buf=mono_array_addr(buffer, guchar, offset);
1477 recvflags = convert_socketflags (flags);
1478 if (recvflags == -1) {
1479 *error = WSAEOPNOTSUPP;
1483 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1484 if(ret==SOCKET_ERROR) {
1486 *error = WSAGetLastError ();
1490 /* If we didn't get a socket size, then we're probably a
1491 * connected connection-oriented socket and the stack hasn't
1492 * returned the remote address. All we can do is return null.
1495 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1504 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1511 MONO_ARCH_SAVE_REGS;
1515 alen = mono_array_length (buffer);
1516 if (offset > alen - count) {
1521 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1524 buf=mono_array_addr(buffer, guchar, offset);
1527 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1530 sendflags = convert_socketflags (flags);
1531 if (sendflags == -1) {
1532 *error = WSAEOPNOTSUPP;
1536 ret = _wapi_send (sock, buf, count, sendflags);
1537 if(ret==SOCKET_ERROR) {
1538 *error = WSAGetLastError ();
1545 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1550 DWORD sendflags = 0;
1552 MONO_ARCH_SAVE_REGS;
1556 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1557 count = mono_array_length (buffers);
1559 sendflags = convert_socketflags (flags);
1560 if (sendflags == -1) {
1561 *error = WSAEOPNOTSUPP;
1565 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1566 if (ret == SOCKET_ERROR) {
1567 *error = WSAGetLastError ();
1574 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1580 struct sockaddr *sa;
1583 MONO_ARCH_SAVE_REGS;
1587 alen = mono_array_length (buffer);
1588 if (offset > alen - count) {
1592 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1598 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1601 buf=mono_array_addr(buffer, guchar, offset);
1604 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1607 sendflags = convert_socketflags (flags);
1608 if (sendflags == -1) {
1609 *error = WSAEOPNOTSUPP;
1613 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1614 if(ret==SOCKET_ERROR) {
1615 *error = WSAGetLastError ();
1623 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1626 MonoClassField *field;
1628 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1629 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1634 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1635 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1637 MonoThread *thread = NULL;
1644 MonoClass *sock_arr_class;
1647 mono_array_size_t socks_size;
1649 MONO_ARCH_SAVE_REGS;
1651 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1652 count = mono_array_length (*sockets);
1653 nfds = count - 3; /* NULL separators */
1654 pfds = g_new0 (mono_pollfd, nfds);
1656 for (i = 0; i < count; i++) {
1657 obj = mono_array_get (*sockets, MonoObject *, i);
1664 /* The socket array was bogus */
1670 pfds [idx].fd = Socket_to_SOCKET (obj);
1671 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1675 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1676 start = time (NULL);
1679 ret = mono_poll (pfds, nfds, timeout);
1680 if (timeout > 0 && ret < 0) {
1682 int sec = time (NULL) - start;
1684 timeout -= sec * 1000;
1690 if (ret == -1 && errno == EINTR) {
1693 thread = mono_thread_current ();
1695 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1702 /* Suspend requested? */
1703 mono_thread_interruption_checkpoint ();
1707 } while (ret == -1 && errno == EINTR);
1710 #ifdef PLATFORM_WIN32
1711 *error = WSAGetLastError ();
1713 *error = errno_to_WSA (errno, __func__);
1725 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1726 socks_size = ((mono_array_size_t)ret) + 3; /* space for the NULL delimiters */
1727 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1730 for (i = 0; i < count && ret > 0; i++) {
1733 obj = mono_array_get (*sockets, MonoObject *, i);
1740 pfd = &pfds [i - mode];
1741 if (pfd->revents == 0)
1745 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1746 mono_array_setref (socks, idx++, obj);
1747 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1748 mono_array_setref (socks, idx++, obj);
1749 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1750 mono_array_setref (socks, idx++, obj);
1758 static MonoObject* int_to_object (MonoDomain *domain, int val)
1760 return mono_value_box (domain, mono_get_int32_class (), &val);
1764 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1770 socklen_t valsize=sizeof(val);
1771 struct linger linger;
1772 socklen_t lingersize=sizeof(linger);
1774 socklen_t time_ms_size = sizeof (time_ms);
1777 socklen_t credsize = sizeof(cred);
1779 MonoDomain *domain=mono_domain_get();
1781 MonoClass *obj_class;
1782 MonoClassField *field;
1784 MONO_ARCH_SAVE_REGS;
1788 ret=convert_sockopt_level_and_name(level, name, &system_level,
1791 *error = WSAENOPROTOOPT;
1795 *obj_val = int_to_object (domain, 0);
1799 /* No need to deal with MulticastOption names here, because
1800 * you cant getsockopt AddMembership or DropMembership (the
1801 * int getsockopt will error, causing an exception)
1804 case SocketOptionName_Linger:
1805 case SocketOptionName_DontLinger:
1806 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1810 case SocketOptionName_SendTimeout:
1811 case SocketOptionName_ReceiveTimeout:
1812 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1816 case SocketOptionName_PeerCred:
1817 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1823 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1827 if(ret==SOCKET_ERROR) {
1828 *error = WSAGetLastError ();
1833 case SocketOptionName_Linger:
1834 /* build a System.Net.Sockets.LingerOption */
1835 obj_class=mono_class_from_name(get_socket_assembly (),
1836 "System.Net.Sockets",
1838 obj=mono_object_new(domain, obj_class);
1840 /* Locate and set the fields "bool enabled" and "int
1843 field=mono_class_get_field_from_name(obj_class, "enabled");
1844 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1846 field=mono_class_get_field_from_name(obj_class, "seconds");
1847 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1851 case SocketOptionName_DontLinger:
1852 /* construct a bool int in val - true if linger is off */
1853 obj = int_to_object (domain, !linger.l_onoff);
1856 case SocketOptionName_SendTimeout:
1857 case SocketOptionName_ReceiveTimeout:
1858 obj = int_to_object (domain, time_ms);
1862 case SocketOptionName_PeerCred:
1864 /* build a Mono.Posix.PeerCred+PeerCredData if
1867 static MonoImage *mono_posix_image = NULL;
1868 MonoPeerCredData *cred_data;
1870 if (mono_posix_image == NULL) {
1871 mono_posix_image=mono_image_loaded ("Mono.Posix");
1872 if (!mono_posix_image) {
1873 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1875 *error = WSAENOPROTOOPT;
1878 mono_posix_image = mono_assembly_get_image (sa);
1883 obj_class = mono_class_from_name(mono_posix_image,
1886 obj = mono_object_new(domain, obj_class);
1887 cred_data = (MonoPeerCredData *)obj;
1888 cred_data->pid = cred.pid;
1889 cred_data->uid = cred.uid;
1890 cred_data->gid = cred.gid;
1896 obj = int_to_object (domain, val);
1902 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1910 MONO_ARCH_SAVE_REGS;
1914 ret=convert_sockopt_level_and_name(level, name, &system_level,
1917 *error = WSAENOPROTOOPT;
1923 valsize=mono_array_length(*byte_val);
1924 buf=mono_array_addr(*byte_val, guchar, 0);
1926 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1927 if(ret==SOCKET_ERROR) {
1928 *error = WSAGetLastError ();
1932 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1933 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1935 struct in_addr inaddr;
1936 MonoClassField *field;
1938 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
1940 /* No idea why .net uses a 64bit type to hold a 32bit value...
1942 * Internal value of IPAddess is in little-endian order
1944 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
1951 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1953 struct in6_addr in6addr;
1954 MonoClassField *field;
1958 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
1959 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1961 /* Solaris has only the 8 bit version. */
1963 for(i=0; i<8; i++) {
1964 guint16 s = mono_array_get (data, guint16, i);
1965 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1966 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1970 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1974 #endif /* AF_INET6 */
1976 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)
1987 #ifdef HAVE_SOL_IPV6
1988 sol_ipv6 = SOL_IPV6;
1991 struct protoent *pent;
1992 pent = getprotobyname ("ipv6");
1993 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2001 struct protoent *pent;
2002 pent = getprotobyname ("ip");
2003 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2006 #endif /* AF_INET6 */
2008 MONO_ARCH_SAVE_REGS;
2010 ret=convert_sockopt_level_and_name(level, name, &system_level,
2013 *error = WSAENOPROTOOPT;
2020 /* Only one of obj_val, byte_val or int_val has data */
2022 MonoClassField *field;
2023 struct linger linger;
2027 case SocketOptionName_DontLinger:
2030 valsize=sizeof(linger);
2031 ret = _wapi_setsockopt (sock, system_level,
2032 system_name, &linger, valsize);
2035 case SocketOptionName_Linger:
2036 /* Dig out "bool enabled" and "int seconds"
2039 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2040 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2041 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2042 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2044 valsize=sizeof(linger);
2045 ret = _wapi_setsockopt (sock, system_level,
2046 system_name, &linger, valsize);
2048 case SocketOptionName_AddMembership:
2049 case SocketOptionName_DropMembership:
2050 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2052 MonoObject *address = NULL;
2055 if(system_level == sol_ipv6) {
2056 struct ipv6_mreq mreq6;
2061 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2062 address = *(gpointer *)(((char *)obj_val) + field->offset);
2065 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2068 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2069 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2071 ret = _wapi_setsockopt (sock, system_level,
2072 system_name, &mreq6,
2074 } else if(system_level == sol_ip)
2075 #endif /* AF_INET6 */
2077 #ifdef HAVE_STRUCT_IP_MREQN
2078 struct ip_mreqn mreq = {{0}};
2080 struct ip_mreq mreq = {{0}};
2081 #endif /* HAVE_STRUCT_IP_MREQN */
2083 /* pain! MulticastOption holds two IPAddress
2084 * members, so I have to dig the value out of
2087 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2088 address = *(gpointer *)(((char *)obj_val) + field->offset);
2090 /* address might not be defined and if so, set the address to ADDR_ANY.
2093 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2096 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2097 address = *(gpointer *)(((char *)obj_val) + field->offset);
2099 #ifdef HAVE_STRUCT_IP_MREQN
2101 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2105 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2107 #endif /* HAVE_STRUCT_IP_MREQN */
2109 ret = _wapi_setsockopt (sock, system_level,
2115 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2117 /* Cause an exception to be thrown */
2121 } else if (byte_val!=NULL) {
2122 int valsize=mono_array_length(byte_val);
2123 guchar *buf=mono_array_addr(byte_val, guchar, 0);
2125 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2126 if(ret==SOCKET_ERROR) {
2127 *error = WSAGetLastError ();
2131 /* ReceiveTimeout/SendTimeout get here */
2133 case SocketOptionName_DontFragment:
2134 #ifdef HAVE_IP_MTU_DISCOVER
2135 /* Fiddle with the value slightly if we're
2139 int_val = IP_PMTUDISC_DO;
2145 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2149 if(ret==SOCKET_ERROR) {
2150 *error = WSAGetLastError ();
2154 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2160 MONO_ARCH_SAVE_REGS;
2164 /* Currently, the values for how (recv=0, send=1, both=2) match
2167 ret = _wapi_shutdown (sock, how);
2168 if(ret==SOCKET_ERROR) {
2169 *error = WSAGetLastError ();
2174 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2176 MonoArray *output, gint32 *error)
2178 glong output_bytes = 0;
2179 gchar *i_buffer, *o_buffer;
2183 MONO_ARCH_SAVE_REGS;
2187 if (code == FIONBIO) {
2188 /* Invalid command. Must use Socket.Blocking */
2192 if (input == NULL) {
2196 i_buffer = mono_array_addr (input, gchar, 0);
2197 i_len = mono_array_length (input);
2200 if (output == NULL) {
2204 o_buffer = mono_array_addr (output, gchar, 0);
2205 o_len = mono_array_length (output);
2208 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2209 if (ret == SOCKET_ERROR) {
2210 *error = WSAGetLastError ();
2214 return (gint) output_bytes;
2217 #ifdef HAVE_SIOCGIFCONF
2219 is_loopback (int family, void *ad)
2221 char *ptr = (char *) ad;
2223 if (family == AF_INET) {
2224 return (ptr [0] == 127);
2228 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2235 get_local_ips (int family, int *nips)
2237 int addr_size, offset, fd, i, count;
2238 int max_ifaces = 50; /* 50 interfaces should be enough... */
2241 struct ifreq iflags;
2242 char *result, *tmp_ptr;
2243 gboolean ignore_loopback = FALSE;
2246 if (family == AF_INET) {
2247 addr_size = sizeof (struct in_addr);
2248 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2250 } else if (family == AF_INET6) {
2251 addr_size = sizeof (struct in6_addr);
2252 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2258 fd = socket (family, SOCK_STREAM, 0);
2260 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2261 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2262 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2264 g_free (ifc.ifc_buf);
2268 count = ifc.ifc_len / sizeof (struct ifreq);
2271 g_free (ifc.ifc_buf);
2276 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2277 strcpy (iflags.ifr_name, ifr->ifr_name);
2278 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2282 if ((iflags.ifr_flags & IFF_UP) == 0) {
2283 ifr->ifr_name [0] = '\0';
2287 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2288 ignore_loopback = TRUE;
2293 result = g_malloc (addr_size * count);
2295 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2296 if (ifr->ifr_name [0] == '\0') {
2301 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2306 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2307 tmp_ptr += addr_size;
2310 g_free (ifc.ifc_buf);
2315 get_local_ips (int family, int *nips)
2321 #endif /* HAVE_SIOCGIFCONF */
2324 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2325 MonoArray **h_aliases,
2326 MonoArray **h_addr_list,
2327 gboolean add_local_ips)
2329 MonoDomain *domain = mono_domain_get ();
2331 struct in_addr *local_in = NULL;
2334 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2338 *h_name=mono_string_new(domain, he->h_name);
2341 while(he->h_aliases[i]!=NULL) {
2345 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2347 while(he->h_aliases[i]!=NULL) {
2350 alias=mono_string_new(domain, he->h_aliases[i]);
2351 mono_array_setref (*h_aliases, i, alias);
2355 if (add_local_ips) {
2356 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2358 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2359 for (i = 0; i < nlocal_in; i++) {
2360 MonoString *addr_string;
2361 char addr [16], *ptr;
2363 ptr = (char *) &local_in [i];
2364 g_snprintf(addr, 16, "%u.%u.%u.%u",
2365 (unsigned char) ptr [0],
2366 (unsigned char) ptr [1],
2367 (unsigned char) ptr [2],
2368 (unsigned char) ptr [3]);
2370 addr_string = mono_string_new (domain, addr);
2371 mono_array_setref (*h_addr_list, i, addr_string);
2379 if (nlocal_in == 0) {
2381 while (he->h_addr_list[i]!=NULL) {
2385 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2387 while(he->h_addr_list[i]!=NULL) {
2388 MonoString *addr_string;
2391 g_snprintf(addr, 16, "%u.%u.%u.%u",
2392 (unsigned char)he->h_addr_list[i][0],
2393 (unsigned char)he->h_addr_list[i][1],
2394 (unsigned char)he->h_addr_list[i][2],
2395 (unsigned char)he->h_addr_list[i][3]);
2397 addr_string=mono_string_new(domain, addr);
2398 mono_array_setref (*h_addr_list, i, addr_string);
2406 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2407 MonoArray **h_aliases,
2408 MonoArray **h_addr_list)
2410 MonoDomain *domain = mono_domain_get ();
2412 *h_name=mono_string_new(domain, addr);
2413 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2414 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2415 mono_array_setref (*h_addr_list, 0, *h_name);
2421 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2422 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2423 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2425 MonoDomain *domain = mono_domain_get ();
2426 int i, host_count, host_index, family_hint;
2427 struct in_addr *local_in = NULL;
2429 struct in6_addr *local_in6 = NULL;
2431 gboolean from_local = FALSE;
2433 family_hint = get_family_hint ();
2435 if(he1 == NULL && he2 == NULL) {
2440 * Check if address length and family are correct
2442 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2446 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2451 * Get the aliases and host name from he1 or he2 whichever is
2452 * not null, if he1 is not null then take aliases from he1
2454 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2455 family_hint == PF_INET)) {
2456 *h_name=mono_string_new (domain, he1->h_name);
2459 while(he1->h_aliases[i]!=NULL) {
2463 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2466 while(he1->h_aliases[i]!=NULL) {
2469 alias=mono_string_new (domain, he1->h_aliases[i]);
2470 mono_array_setref (*h_aliases, i, alias);
2473 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2474 family_hint == PF_INET6)) {
2475 *h_name=mono_string_new (domain, he2->h_name);
2478 while(he2->h_aliases [i] != NULL) {
2482 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2485 while(he2->h_aliases[i]!=NULL) {
2488 alias=mono_string_new (domain, he2->h_aliases[i]);
2489 mono_array_setref (*h_aliases, i, alias);
2497 * Count the number of addresses in he1 + he2
2500 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2501 family_hint == PF_INET)) {
2503 while(he1->h_addr_list[i]!=NULL) {
2509 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2510 family_hint == PF_INET6)) {
2512 while(he2->h_addr_list[i]!=NULL) {
2522 if (add_local_ips) {
2523 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2524 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2526 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2527 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2529 if (nlocal_in || nlocal_in6) {
2531 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2532 nlocal_in + nlocal_in6);
2536 for (n = 0; n < nlocal_in6; n++) {
2537 MonoString *addr_string;
2539 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2541 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2544 addr_string = mono_string_new (domain, addr);
2545 mono_array_setref (*h_addr_list, host_index, addr_string);
2553 for (n = 0; n < nlocal_in; n++) {
2554 MonoString *addr_string;
2556 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2558 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2561 addr_string = mono_string_new (domain, addr);
2562 mono_array_setref (*h_addr_list, host_index, addr_string);
2576 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2578 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2579 family_hint == PF_INET6)) {
2581 while(he2->h_addr_list[i] != NULL) {
2582 MonoString *addr_string;
2584 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2586 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2590 addr_string = mono_string_new (domain, addr);
2591 mono_array_setref (*h_addr_list, host_index, addr_string);
2598 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2599 family_hint == PF_INET)) {
2601 while(he1->h_addr_list[i] != NULL) {
2602 MonoString *addr_string;
2604 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2606 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2610 addr_string=mono_string_new (domain, addr);
2611 mono_array_setref (*h_addr_list, host_index, addr_string);
2622 #if defined(AF_INET6)
2624 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2625 MonoArray **h_aliases,
2626 MonoArray **h_addr_list,
2627 gboolean add_local_ips)
2630 struct addrinfo *ai = NULL;
2631 struct in_addr *local_in = NULL;
2633 struct in6_addr *local_in6 = NULL;
2637 MonoDomain *domain = mono_domain_get ();
2640 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2641 if (add_local_ips) {
2642 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2643 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2644 if (nlocal_in || nlocal_in6) {
2645 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2647 MonoString *addr_string;
2651 for (i = 0; i < nlocal_in; i++) {
2652 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2653 addr_string = mono_string_new (domain, addr);
2654 mono_array_setref (*h_addr_list, addr_index, addr_string);
2660 MonoString *addr_string;
2665 for (i = 0; i < nlocal_in6; i++) {
2666 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2668 addr_string = mono_string_new (domain, addr);
2669 mono_array_setref (*h_addr_list, addr_index, addr_string);
2678 freeaddrinfo (info);
2687 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2688 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2694 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2696 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2697 MonoString *addr_string;
2699 char buffer [48]; /* Max. size for IPv6 */
2701 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2705 if(ai->ai_family == PF_INET) {
2706 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2708 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2712 addr_string=mono_string_new(domain, buffer);
2714 addr_string=mono_string_new(domain, "");
2717 mono_array_setref (*h_addr_list, addr_index, addr_string);
2720 if (ai->ai_canonname != NULL) {
2721 *h_name=mono_string_new(domain, ai->ai_canonname);
2723 *h_name=mono_string_new(domain, buffer);
2739 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2741 gboolean add_local_ips = FALSE;
2742 #ifdef HAVE_SIOCGIFCONF
2743 gchar this_hostname [256];
2745 #if !defined(HAVE_GETHOSTBYNAME2_R)
2746 struct addrinfo *info = NULL, hints;
2749 MONO_ARCH_SAVE_REGS;
2751 hostname=mono_string_to_utf8 (host);
2752 #ifdef HAVE_SIOCGIFCONF
2753 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2754 if (!strcmp (hostname, this_hostname))
2755 add_local_ips = TRUE;
2759 memset(&hints, 0, sizeof(hints));
2760 hints.ai_family = get_family_hint ();
2761 hints.ai_socktype = SOCK_STREAM;
2762 hints.ai_flags = AI_CANONNAME;
2764 if (getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2770 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2772 struct hostent he1,*hp1, he2, *hp2;
2773 int buffer_size1, buffer_size2;
2774 char *buffer1, *buffer2;
2776 gboolean return_value;
2779 MONO_ARCH_SAVE_REGS;
2781 hostname=mono_string_to_utf8 (host);
2783 #ifdef HAVE_SIOCGIFCONF
2784 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2785 if (!strcmp (hostname, this_hostname))
2786 add_local_ips = TRUE;
2792 buffer1 = g_malloc0(buffer_size1);
2793 buffer2 = g_malloc0(buffer_size2);
2795 while (gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2796 &hp1, &herr) == ERANGE) {
2798 buffer1 = g_realloc(buffer1, buffer_size1);
2803 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2804 buffer_size2, &hp2, &herr) == ERANGE) {
2806 buffer2 = g_realloc(buffer2, buffer_size2);
2812 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2813 h_addr_list, add_local_ips);
2819 return(return_value);
2820 #endif /* HAVE_GETHOSTBYNAME2_R */
2822 #else /* AF_INET6 */
2823 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2827 gboolean add_local_ips = FALSE;
2828 #ifdef HAVE_SIOCGIFCONF
2829 gchar this_hostname [256];
2832 MONO_ARCH_SAVE_REGS;
2834 hostname=mono_string_to_utf8(host);
2835 #ifdef HAVE_SIOCGIFCONF
2836 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2837 if (!strcmp (hostname, this_hostname))
2838 add_local_ips = TRUE;
2842 he = _wapi_gethostbyname (hostname);
2849 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2851 #endif /* AF_INET6 */
2853 #ifndef HAVE_INET_PTON
2855 inet_pton (int family, const char *address, void *inaddrp)
2857 if (family == AF_INET) {
2858 #ifdef HAVE_INET_ATON
2859 struct in_addr inaddr;
2861 if (!inet_aton (address, &inaddr))
2864 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2867 /* assume the system has inet_addr(), if it doesn't
2868 have that we're pretty much screwed... */
2871 if (!strcmp (address, "255.255.255.255")) {
2872 /* special-case hack */
2873 inaddr = 0xffffffff;
2875 inaddr = inet_addr (address);
2877 #define INADDR_NONE ((in_addr_t) -1)
2879 if (inaddr == INADDR_NONE)
2883 memcpy (inaddrp, &inaddr, sizeof (guint32));
2885 #endif /* HAVE_INET_ATON */
2890 #endif /* !HAVE_INET_PTON */
2892 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2898 struct sockaddr_in saddr;
2899 struct sockaddr_in6 saddr6;
2900 struct addrinfo *info = NULL, hints;
2902 char hostname[1024] = {0};
2905 struct in_addr inaddr;
2910 v1 = mono_framework_version () == 1;
2912 address = mono_string_to_utf8 (addr);
2915 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2916 /* Maybe an ipv6 address */
2917 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2923 saddr6.sin6_family = AF_INET6;
2928 saddr.sin_family = AF_INET;
2933 flags = NI_NAMEREQD;
2936 if(family == AF_INET) {
2937 #if HAVE_SOCKADDR_IN_SIN_LEN
2938 saddr.sin_len = sizeof (saddr);
2940 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2941 hostname, sizeof(hostname), NULL, 0,
2945 } else if(family == AF_INET6) {
2946 #if HAVE_SOCKADDR_IN6_SIN_LEN
2947 saddr6.sin6_len = sizeof (saddr6);
2949 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2950 hostname, sizeof(hostname), NULL, 0,
2956 memset (&hints, 0, sizeof(hints));
2957 hints.ai_family = get_family_hint ();
2958 hints.ai_socktype = SOCK_STREAM;
2959 hints.ai_flags = AI_CANONNAME;
2961 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
2965 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
2967 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
2972 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
2976 ret = ipaddr_to_IPHostEntry (address, h_name,
2977 h_aliases, h_addr_list);
2980 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
2981 h_addr_list, FALSE);
2989 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
2991 gchar hostname[256];
2994 MONO_ARCH_SAVE_REGS;
2996 ret = gethostname (hostname, sizeof (hostname));
3001 *h_name=mono_string_new(mono_domain_get (), hostname);
3006 void mono_network_init(void)
3011 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3013 g_error(G_GNUC_PRETTY_FUNCTION ": Couldn't initialise networking");
3018 g_message(G_GNUC_PRETTY_FUNCTION ": Using socket library: %s", wsadata.szDescription);
3019 g_message(G_GNUC_PRETTY_FUNCTION ": Socket system status: %s", wsadata.szSystemStatus);
3023 void mono_network_cleanup(void)