2 * socket-io.c: Socket IO internal calls
5 * Dick Porter (dick@ximian.com)
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
22 #include <sys/types.h>
23 #ifndef PLATFORM_WIN32
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
29 #include <arpa/inet.h>
32 #include <mono/metadata/object.h>
33 #include <mono/io-layer/io-layer.h>
34 #include <mono/metadata/socket-io.h>
35 #include <mono/metadata/exception.h>
36 #include <mono/metadata/assembly.h>
37 #include <mono/metadata/appdomain.h>
38 #include <mono/metadata/threads.h>
39 #include <mono/metadata/threads-types.h>
40 #include <mono/utils/mono-poll.h>
41 /* FIXME change this code to not mess so much with the internals */
42 #include <mono/metadata/class-internals.h>
43 #include <mono/metadata/threadpool-internals.h>
44 #include <mono/metadata/domain-internals.h>
46 #ifdef HAVE_SYS_TIME_H
49 #ifdef HAVE_SYS_IOCTL_H
50 #include <sys/ioctl.h>
59 #ifdef HAVE_SYS_FILIO_H
60 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
62 #ifdef HAVE_SYS_SOCKIO_H
63 #include <sys/sockio.h> /* defines SIOCATMARK */
69 #include "mono/io-layer/socket-wrappers.h"
72 /* This is a kludge to make this file build under cygwin:
73 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
74 * prototypes for some but not all required functions (notably
75 * inet_ntop() is missing), but the libws2_32 library is missing the
76 * actual implementations of these functions.
81 #ifdef PLATFORM_ANDROID
82 // not yet actually implemented...
88 static gint32 convert_family(MonoAddressFamily mono_family)
93 case AddressFamily_Unknown:
94 case AddressFamily_ImpLink:
95 case AddressFamily_Pup:
96 case AddressFamily_Chaos:
97 case AddressFamily_Iso:
98 case AddressFamily_Ecma:
99 case AddressFamily_DataKit:
100 case AddressFamily_Ccitt:
101 case AddressFamily_DataLink:
102 case AddressFamily_Lat:
103 case AddressFamily_HyperChannel:
104 case AddressFamily_NetBios:
105 case AddressFamily_VoiceView:
106 case AddressFamily_FireFox:
107 case AddressFamily_Banyan:
108 case AddressFamily_Atm:
109 case AddressFamily_Cluster:
110 case AddressFamily_Ieee12844:
111 case AddressFamily_NetworkDesigners:
112 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
115 case AddressFamily_Unspecified:
119 case AddressFamily_Unix:
123 case AddressFamily_InterNetwork:
127 case AddressFamily_Ipx:
133 case AddressFamily_Sna:
137 case AddressFamily_DecNet:
141 case AddressFamily_AppleTalk:
145 case AddressFamily_InterNetworkV6:
150 case AddressFamily_Irda:
156 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
162 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
164 MonoAddressFamily family=AddressFamily_Unknown;
168 family=AddressFamily_Unspecified;
172 family=AddressFamily_Unix;
176 family=AddressFamily_InterNetwork;
181 family=AddressFamily_Ipx;
186 family=AddressFamily_Sna;
190 family=AddressFamily_DecNet;
194 family=AddressFamily_AppleTalk;
199 family=AddressFamily_InterNetworkV6;
205 family=AddressFamily_Irda;
209 g_warning("unknown address family 0x%x", af_family);
215 static gint32 convert_type(MonoSocketType mono_type)
220 case SocketType_Stream:
224 case SocketType_Dgram:
236 case SocketType_Seqpacket:
240 case SocketType_Unknown:
241 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
245 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
251 static gint32 convert_proto(MonoProtocolType mono_proto)
256 case ProtocolType_IP:
257 case ProtocolType_IPv6:
258 case ProtocolType_Icmp:
259 case ProtocolType_Igmp:
260 case ProtocolType_Ggp:
261 case ProtocolType_Tcp:
262 case ProtocolType_Pup:
263 case ProtocolType_Udp:
264 case ProtocolType_Idp:
265 /* These protocols are known (on my system at least) */
269 case ProtocolType_ND:
270 case ProtocolType_Raw:
271 case ProtocolType_Ipx:
272 case ProtocolType_Spx:
273 case ProtocolType_SpxII:
274 case ProtocolType_Unknown:
275 /* These protocols arent */
276 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
286 /* Convert MonoSocketFlags */
287 static gint32 convert_socketflags (gint32 sflags)
292 /* SocketFlags.None */
295 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
296 SocketFlags_DontRoute | SocketFlags_Partial))
297 /* Contains invalid flag values */
300 if (sflags & SocketFlags_OutOfBand)
302 if (sflags & SocketFlags_Peek)
304 if (sflags & SocketFlags_DontRoute)
305 flags |= MSG_DONTROUTE;
307 /* Ignore Partial - see bug 349688. Don't return -1, because
308 * according to the comment in that bug ms runtime doesn't for
309 * UDP sockets (this means we will silently ignore it for TCP
312 if (sflags & SocketFlags_Partial)
319 if (sflags & SocketFlags_MaxIOVectorLength)
320 /* FIXME: Don't know what to do for MaxIOVectorLength query */
323 return (flags ? flags : -1);
328 * 0 on success (mapped mono_level and mono_name to system_level and system_name
330 * -2 on non-fatal error (ie, must ignore)
332 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
333 MonoSocketOptionName mono_name,
337 switch (mono_level) {
338 case SocketOptionLevel_Socket:
339 *system_level = SOL_SOCKET;
342 case SocketOptionName_DontLinger:
343 /* This is SO_LINGER, because the setsockopt
344 * internal call maps DontLinger to SO_LINGER
347 *system_name = SO_LINGER;
349 case SocketOptionName_Debug:
350 *system_name = SO_DEBUG;
353 case SocketOptionName_AcceptConnection:
354 *system_name = SO_ACCEPTCONN;
357 case SocketOptionName_ReuseAddress:
358 *system_name = SO_REUSEADDR;
360 case SocketOptionName_KeepAlive:
361 *system_name = SO_KEEPALIVE;
363 case SocketOptionName_DontRoute:
364 *system_name = SO_DONTROUTE;
366 case SocketOptionName_Broadcast:
367 *system_name = SO_BROADCAST;
369 case SocketOptionName_Linger:
370 *system_name = SO_LINGER;
372 case SocketOptionName_OutOfBandInline:
373 *system_name = SO_OOBINLINE;
375 case SocketOptionName_SendBuffer:
376 *system_name = SO_SNDBUF;
378 case SocketOptionName_ReceiveBuffer:
379 *system_name = SO_RCVBUF;
381 case SocketOptionName_SendLowWater:
382 *system_name = SO_SNDLOWAT;
384 case SocketOptionName_ReceiveLowWater:
385 *system_name = SO_RCVLOWAT;
387 case SocketOptionName_SendTimeout:
388 *system_name = SO_SNDTIMEO;
390 case SocketOptionName_ReceiveTimeout:
391 *system_name = SO_RCVTIMEO;
393 case SocketOptionName_Error:
394 *system_name = SO_ERROR;
396 case SocketOptionName_Type:
397 *system_name = SO_TYPE;
400 case SocketOptionName_PeerCred:
401 *system_name = SO_PEERCRED;
404 case SocketOptionName_ExclusiveAddressUse:
405 case SocketOptionName_UseLoopback:
406 case SocketOptionName_MaxConnections:
407 /* Can't figure out how to map these, so fall
411 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
416 case SocketOptionLevel_IP:
418 *system_level = SOL_IP;
421 static int cached = 0;
425 struct protoent *pent;
427 pent = getprotobyname ("IP");
428 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
432 *system_level = proto;
434 #endif /* HAVE_SOL_IP */
437 case SocketOptionName_IPOptions:
438 *system_name = IP_OPTIONS;
441 case SocketOptionName_HeaderIncluded:
442 *system_name = IP_HDRINCL;
446 case SocketOptionName_TypeOfService:
447 *system_name = IP_TOS;
451 case SocketOptionName_IpTimeToLive:
452 *system_name = IP_TTL;
455 case SocketOptionName_MulticastInterface:
456 *system_name = IP_MULTICAST_IF;
458 case SocketOptionName_MulticastTimeToLive:
459 *system_name = IP_MULTICAST_TTL;
461 case SocketOptionName_MulticastLoopback:
462 *system_name = IP_MULTICAST_LOOP;
464 case SocketOptionName_AddMembership:
465 *system_name = IP_ADD_MEMBERSHIP;
467 case SocketOptionName_DropMembership:
468 *system_name = IP_DROP_MEMBERSHIP;
470 #ifdef HAVE_IP_PKTINFO
471 case SocketOptionName_PacketInformation:
472 *system_name = IP_PKTINFO;
474 #endif /* HAVE_IP_PKTINFO */
476 case SocketOptionName_DontFragment:
477 #ifdef HAVE_IP_DONTFRAGMENT
478 *system_name = IP_DONTFRAGMENT;
480 #elif defined HAVE_IP_MTU_DISCOVER
481 /* Not quite the same */
482 *system_name = IP_MTU_DISCOVER;
485 /* If the flag is not available on this system, we can ignore this error */
487 #endif /* HAVE_IP_DONTFRAGMENT */
488 case SocketOptionName_AddSourceMembership:
489 case SocketOptionName_DropSourceMembership:
490 case SocketOptionName_BlockSource:
491 case SocketOptionName_UnblockSource:
492 /* Can't figure out how to map these, so fall
496 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
502 case SocketOptionLevel_IPv6:
504 *system_level = SOL_IPV6;
507 static int cached = 0;
511 struct protoent *pent;
513 pent = getprotobyname ("IPV6");
514 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
518 *system_level = proto;
520 #endif /* HAVE_SOL_IPV6 */
523 case SocketOptionName_IpTimeToLive:
524 *system_name = IPV6_UNICAST_HOPS;
526 case SocketOptionName_MulticastInterface:
527 *system_name = IPV6_MULTICAST_IF;
529 case SocketOptionName_MulticastTimeToLive:
530 *system_name = IPV6_MULTICAST_HOPS;
532 case SocketOptionName_MulticastLoopback:
533 *system_name = IPV6_MULTICAST_LOOP;
535 case SocketOptionName_AddMembership:
536 *system_name = IPV6_JOIN_GROUP;
538 case SocketOptionName_DropMembership:
539 *system_name = IPV6_LEAVE_GROUP;
541 case SocketOptionName_PacketInformation:
542 #ifdef HAVE_IPV6_PKTINFO
543 *system_name = IPV6_PKTINFO;
546 case SocketOptionName_HeaderIncluded:
547 case SocketOptionName_IPOptions:
548 case SocketOptionName_TypeOfService:
549 case SocketOptionName_DontFragment:
550 case SocketOptionName_AddSourceMembership:
551 case SocketOptionName_DropSourceMembership:
552 case SocketOptionName_BlockSource:
553 case SocketOptionName_UnblockSource:
554 /* Can't figure out how to map these, so fall
558 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
562 break; /* SocketOptionLevel_IPv6 */
565 case SocketOptionLevel_Tcp:
567 *system_level = SOL_TCP;
570 static int cached = 0;
574 struct protoent *pent;
576 pent = getprotobyname ("TCP");
577 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
581 *system_level = proto;
583 #endif /* HAVE_SOL_TCP */
586 case SocketOptionName_NoDelay:
587 *system_name = TCP_NODELAY;
590 /* The documentation is talking complete
591 * bollocks here: rfc-1222 is titled
592 * 'Advancing the NSFNET Routing Architecture'
593 * and doesn't mention either of the words
594 * "expedite" or "urgent".
596 case SocketOptionName_BsdUrgent:
597 case SocketOptionName_Expedited:
600 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
605 case SocketOptionLevel_Udp:
606 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
609 case SocketOptionName_NoChecksum:
610 case SocketOptionName_ChecksumCoverage:
612 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
619 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
626 static MonoImage *get_socket_assembly (void)
628 static const char *version = NULL;
629 static gboolean moonlight;
630 static MonoImage *socket_assembly = NULL;
632 if (version == NULL) {
633 version = mono_get_runtime_info ()->framework_version;
634 moonlight = !strcmp (version, "2.1");
637 if (socket_assembly == NULL) {
639 socket_assembly = mono_image_loaded ("System.Net");
640 if (!socket_assembly) {
641 MonoAssembly *sa = mono_assembly_open ("System.Net.dll", NULL);
644 g_assert_not_reached ();
646 socket_assembly = mono_assembly_get_image (sa);
650 socket_assembly = mono_image_loaded ("System");
651 if (!socket_assembly) {
652 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
655 g_assert_not_reached ();
657 socket_assembly = mono_assembly_get_image (sa);
663 return(socket_assembly);
667 static gint32 get_family_hint(void)
669 MonoDomain *domain = mono_domain_get ();
671 if (!domain->inet_family_hint) {
672 MonoClass *socket_class;
673 MonoClassField *ipv6_field, *ipv4_field;
674 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
677 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
678 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
679 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
680 vtable = mono_class_vtable (mono_domain_get (), socket_class);
681 mono_runtime_class_init (vtable);
683 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
684 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
686 mono_domain_lock (domain);
687 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
688 domain->inet_family_hint = 1;
689 } else if (ipv4_enabled == 1) {
690 domain->inet_family_hint = 2;
692 domain->inet_family_hint = 3;
694 mono_domain_unlock (domain);
696 switch (domain->inet_family_hint) {
697 case 1: return PF_UNSPEC;
698 case 2: return PF_INET;
699 case 3: return PF_INET6;
706 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
717 sock_family=convert_family(family);
718 if(sock_family==-1) {
719 *error = WSAEAFNOSUPPORT;
723 sock_proto=convert_proto(proto);
725 *error = WSAEPROTONOSUPPORT;
729 sock_type=convert_type(type);
731 *error = WSAESOCKTNOSUPPORT;
735 sock = _wapi_socket (sock_family, sock_type, sock_proto,
736 NULL, 0, WSA_FLAG_OVERLAPPED);
738 if(sock==INVALID_SOCKET) {
739 *error = WSAGetLastError ();
743 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
744 return (GUINT_TO_POINTER (sock));
748 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
749 return (GUINT_TO_POINTER (sock));
753 return(GUINT_TO_POINTER (sock));
756 /* FIXME: the SOCKET parameter (here and in other functions in this
757 * file) is really an IntPtr which needs to be converted to a guint32.
759 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
765 g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
770 /* Clear any pending work item from this socket if the underlying
771 * polling system does not notify when the socket is closed */
772 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
776 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
781 g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
784 return(WSAGetLastError());
787 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
797 #if defined(PLATFORM_MACOSX)
799 socklen_t optlen = sizeof (amount);
800 ret=getsockopt (sock, SOL_SOCKET, SO_NREAD, &amount, &optlen);
803 ret=ioctlsocket(sock, FIONREAD, &amount);
805 if(ret==SOCKET_ERROR) {
806 *error = WSAGetLastError ();
813 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
824 * block == TRUE/FALSE means we will block/not block.
825 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
829 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
830 if(ret==SOCKET_ERROR) {
831 *error = WSAGetLastError ();
835 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
845 #ifdef PLATFORM_WIN32
846 /* Several applications are getting stuck during shutdown on Windows
847 * when an accept call is on a background thread.
851 MonoThread* curthread = mono_thread_current ();
859 FD_SET(sock, &readfds);
861 timeout.tv_usec = 1000;
862 selectret = select (0, &readfds, NULL, NULL, &timeout);
865 if (curthread->state & ThreadState_StopRequested)
872 newsock = _wapi_accept (sock, NULL, 0);
873 if(newsock==INVALID_SOCKET) {
874 *error = WSAGetLastError ();
878 return(GUINT_TO_POINTER (newsock));
881 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
891 ret = _wapi_listen (sock, backlog);
892 if(ret==SOCKET_ERROR) {
893 *error = WSAGetLastError ();
897 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
898 int sa_size, gint32 *error)
900 MonoDomain *domain = mono_domain_get ();
901 MonoObject *sockaddr_obj;
902 MonoClass *sockaddr_class;
903 MonoClassField *field;
905 MonoAddressFamily family;
907 /* Build a System.Net.SocketAddress object instance */
908 sockaddr_class=mono_class_from_name(get_socket_assembly (), "System.Net", "SocketAddress");
909 sockaddr_obj=mono_object_new(domain, sockaddr_class);
911 /* Locate the SocketAddress data buffer in the object */
912 field=mono_class_get_field_from_name(sockaddr_class, "data");
914 /* Make sure there is space for the family and size bytes */
916 if (saddr->sa_family == AF_UNIX) {
917 /* sa_len includes the entire sockaddr size, so we don't need the
918 * N bytes (sizeof (unsigned short)) of the family. */
919 data=mono_array_new(domain, mono_get_byte_class (), sa_size);
923 /* May be the +2 here is too conservative, as sa_len returns
924 * the length of the entire sockaddr_in/in6, including
925 * sizeof (unsigned short) of the family */
926 data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
929 /* The data buffer is laid out as follows:
930 * bytes 0 and 1 are the address family
931 * bytes 2 and 3 are the port info
932 * the rest is the address info
935 family=convert_to_mono_family(saddr->sa_family);
936 if(family==AddressFamily_Unknown) {
937 *error = WSAEAFNOSUPPORT;
941 mono_array_set(data, guint8, 0, family & 0x0FF);
942 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
944 if(saddr->sa_family==AF_INET) {
945 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
946 guint16 port=ntohs(sa_in->sin_port);
947 guint32 address=ntohl(sa_in->sin_addr.s_addr);
950 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
953 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
954 mono_array_set(data, guint8, 3, (port) & 0xff);
955 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
956 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
957 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
958 mono_array_set(data, guint8, 7, (address) & 0xff);
960 mono_field_set_value (sockaddr_obj, field, data);
962 return(sockaddr_obj);
964 } else if (saddr->sa_family == AF_INET6) {
965 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
968 guint16 port=ntohs(sa_in->sin6_port);
971 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
974 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
975 mono_array_set(data, guint8, 3, (port) & 0xff);
977 for(i=0; i<16; i++) {
978 mono_array_set(data, guint8, 8+i,
979 sa_in->sin6_addr.s6_addr[i]);
982 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
983 mono_array_set(data, guint8, 25,
984 (sa_in->sin6_scope_id >> 8) & 0xff);
985 mono_array_set(data, guint8, 26,
986 (sa_in->sin6_scope_id >> 16) & 0xff);
987 mono_array_set(data, guint8, 27,
988 (sa_in->sin6_scope_id >> 24) & 0xff);
990 mono_field_set_value (sockaddr_obj, field, data);
992 return(sockaddr_obj);
995 } else if (saddr->sa_family == AF_UNIX) {
998 for (i = 0; i < sa_size; i++) {
999 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
1002 mono_field_set_value (sockaddr_obj, field, data);
1004 return sockaddr_obj;
1007 *error = WSAEAFNOSUPPORT;
1012 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
1014 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1018 MONO_ARCH_SAVE_REGS;
1023 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1025 if(ret==SOCKET_ERROR) {
1026 *error = WSAGetLastError ();
1031 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));
1034 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1038 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
1040 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1044 MONO_ARCH_SAVE_REGS;
1049 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1051 if(ret==SOCKET_ERROR) {
1052 *error = WSAGetLastError ();
1057 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));
1060 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1064 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1068 MonoClassField *field;
1073 /* Dig the SocketAddress data buffer out of the object */
1074 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1075 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1077 /* The data buffer is laid out as follows:
1078 * byte 0 is the address family low byte
1079 * byte 1 is the address family high byte
1081 * bytes 2 and 3 are the port info
1082 * the rest is the address info
1084 * the rest is the file name
1086 len = mono_array_length (data);
1088 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1091 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1092 if (family == AF_INET) {
1093 struct sockaddr_in *sa;
1098 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1101 sa = g_new0 (struct sockaddr_in, 1);
1102 port = (mono_array_get (data, guint8, 2) << 8) +
1103 mono_array_get (data, guint8, 3);
1104 address = (mono_array_get (data, guint8, 4) << 24) +
1105 (mono_array_get (data, guint8, 5) << 16 ) +
1106 (mono_array_get (data, guint8, 6) << 8) +
1107 mono_array_get (data, guint8, 7);
1109 sa->sin_family = family;
1110 sa->sin_addr.s_addr = htonl (address);
1111 sa->sin_port = htons (port);
1113 *sa_size = sizeof(struct sockaddr_in);
1114 return((struct sockaddr *)sa);
1117 } else if (family == AF_INET6) {
1118 struct sockaddr_in6 *sa;
1124 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1127 sa = g_new0 (struct sockaddr_in6, 1);
1128 port = mono_array_get (data, guint8, 3) +
1129 (mono_array_get (data, guint8, 2) << 8);
1130 scopeid = mono_array_get (data, guint8, 24) +
1131 (mono_array_get (data, guint8, 25) << 8) +
1132 (mono_array_get (data, guint8, 26) << 16) +
1133 (mono_array_get (data, guint8, 27) << 24);
1135 sa->sin6_family = family;
1136 sa->sin6_port = htons (port);
1137 sa->sin6_scope_id = scopeid;
1139 for(i=0; i<16; i++) {
1140 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1143 *sa_size = sizeof(struct sockaddr_in6);
1144 return((struct sockaddr *)sa);
1146 #ifdef HAVE_SYS_UN_H
1147 } else if (family == AF_UNIX) {
1148 struct sockaddr_un *sock_un;
1151 /* Need a byte for the '\0' terminator/prefix, and the first
1152 * two bytes hold the SocketAddress family
1154 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1155 mono_raise_exception (mono_get_exception_index_out_of_range ());
1158 sock_un = g_new0 (struct sockaddr_un, 1);
1160 sock_un->sun_family = family;
1161 for (i = 0; i < len - 2; i++) {
1162 sock_un->sun_path [i] = mono_array_get (data, guint8,
1168 return (struct sockaddr *)sock_un;
1171 *error = WSAEAFNOSUPPORT;
1176 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1178 struct sockaddr *sa;
1182 MONO_ARCH_SAVE_REGS;
1186 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1192 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));
1195 ret = _wapi_bind (sock, sa, sa_size);
1196 if(ret==SOCKET_ERROR) {
1197 *error = WSAGetLastError ();
1210 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1211 gint timeout, gint32 *error)
1213 MonoThread *thread = NULL;
1219 MONO_ARCH_SAVE_REGS;
1221 pfds = g_new0 (mono_pollfd, 1);
1222 pfds[0].fd = GPOINTER_TO_INT (sock);
1223 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1224 (mode == SelectModeWrite) ? MONO_POLLOUT :
1225 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1227 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1228 start = time (NULL);
1232 ret = mono_poll (pfds, 1, timeout);
1233 if (timeout > 0 && ret < 0) {
1235 int sec = time (NULL) - start;
1237 timeout -= sec * 1000;
1245 if (ret == -1 && errno == EINTR) {
1248 if (thread == NULL) {
1249 thread = mono_thread_current ();
1252 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1258 /* Suspend requested? */
1259 mono_thread_interruption_checkpoint ();
1263 } while (ret == -1 && errno == EINTR);
1266 #ifdef PLATFORM_WIN32
1267 *error = WSAGetLastError ();
1269 *error = errno_to_WSA (errno, __func__);
1284 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1286 struct sockaddr *sa;
1290 MONO_ARCH_SAVE_REGS;
1294 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1300 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));
1303 ret = _wapi_connect (sock, sa, sa_size);
1304 if(ret==SOCKET_ERROR) {
1305 *error = WSAGetLastError ();
1311 /* These #defines from mswsock.h from wine. Defining them here allows
1312 * us to build this file on a mingw box that doesn't know the magic
1313 * numbers, but still run on a newer windows box that does.
1315 #ifndef WSAID_DISCONNECTEX
1316 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1317 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1320 #ifndef WSAID_TRANSMITFILE
1321 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1322 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1325 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1328 glong output_bytes = 0;
1329 GUID disco_guid = WSAID_DISCONNECTEX;
1330 GUID trans_guid = WSAID_TRANSMITFILE;
1331 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1332 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1335 MONO_ARCH_SAVE_REGS;
1340 g_message("%s: disconnecting from socket %p (reuse %d)", __func__,
1344 /* I _think_ the extension function pointers need to be looked
1345 * up for each socket. FIXME: check the best way to store
1346 * pointers to functions in managed objects that still works
1347 * on 64bit platforms.
1349 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1350 (void *)&disco_guid, sizeof(GUID),
1351 (void *)&_wapi_disconnectex, sizeof(void *),
1352 &output_bytes, NULL, NULL);
1354 /* make sure that WSAIoctl didn't put crap in the
1357 _wapi_disconnectex = NULL;
1359 /* Look up the TransmitFile extension function pointer
1360 * instead of calling TransmitFile() directly, because
1361 * apparently "Several of the extension functions have
1362 * been available since WinSock 1.1 and are exported
1363 * from MSWsock.dll, however it's not advisable to
1364 * link directly to this dll as this ties you to the
1365 * Microsoft WinSock provider. A provider neutral way
1366 * of accessing these extension functions is to load
1367 * them dynamically via WSAIoctl using the
1368 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1369 * should, theoretically, allow you to access these
1370 * functions from any provider that supports them..."
1371 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1373 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1374 (void *)&trans_guid, sizeof(GUID),
1375 (void *)&_wapi_transmitfile, sizeof(void *),
1376 &output_bytes, NULL, NULL);
1378 _wapi_transmitfile = NULL;
1382 if (_wapi_disconnectex != NULL) {
1383 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1384 } else if (_wapi_transmitfile != NULL) {
1385 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1386 TF_DISCONNECT | TF_REUSE_SOCKET);
1388 *error = ERROR_NOT_SUPPORTED;
1392 if (bret == FALSE) {
1393 *error = WSAGetLastError ();
1397 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1404 MONO_ARCH_SAVE_REGS;
1408 alen = mono_array_length (buffer);
1409 if (offset > alen - count) {
1413 buf=mono_array_addr(buffer, guchar, offset);
1415 recvflags = convert_socketflags (flags);
1416 if (recvflags == -1) {
1417 *error = WSAEOPNOTSUPP;
1421 ret = _wapi_recv (sock, buf, count, recvflags);
1422 if(ret==SOCKET_ERROR) {
1423 *error = WSAGetLastError ();
1430 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1435 DWORD recvflags = 0;
1437 MONO_ARCH_SAVE_REGS;
1441 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1442 count = mono_array_length (buffers);
1444 recvflags = convert_socketflags (flags);
1445 if (recvflags == -1) {
1446 *error = WSAEOPNOTSUPP;
1450 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1451 if (ret == SOCKET_ERROR) {
1452 *error = WSAGetLastError ();
1459 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1465 struct sockaddr *sa;
1468 MONO_ARCH_SAVE_REGS;
1472 alen = mono_array_length (buffer);
1473 if (offset > alen - count) {
1477 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1482 buf=mono_array_addr(buffer, guchar, offset);
1484 recvflags = convert_socketflags (flags);
1485 if (recvflags == -1) {
1486 *error = WSAEOPNOTSUPP;
1490 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1491 if(ret==SOCKET_ERROR) {
1493 *error = WSAGetLastError ();
1497 /* If we didn't get a socket size, then we're probably a
1498 * connected connection-oriented socket and the stack hasn't
1499 * returned the remote address. All we can do is return null.
1502 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1511 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1518 MONO_ARCH_SAVE_REGS;
1522 alen = mono_array_length (buffer);
1523 if (offset > alen - count) {
1528 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1531 buf=mono_array_addr(buffer, guchar, offset);
1534 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1537 sendflags = convert_socketflags (flags);
1538 if (sendflags == -1) {
1539 *error = WSAEOPNOTSUPP;
1543 ret = _wapi_send (sock, buf, count, sendflags);
1544 if(ret==SOCKET_ERROR) {
1545 *error = WSAGetLastError ();
1552 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1557 DWORD sendflags = 0;
1559 MONO_ARCH_SAVE_REGS;
1563 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1564 count = mono_array_length (buffers);
1566 sendflags = convert_socketflags (flags);
1567 if (sendflags == -1) {
1568 *error = WSAEOPNOTSUPP;
1572 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1573 if (ret == SOCKET_ERROR) {
1574 *error = WSAGetLastError ();
1581 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1587 struct sockaddr *sa;
1590 MONO_ARCH_SAVE_REGS;
1594 alen = mono_array_length (buffer);
1595 if (offset > alen - count) {
1599 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1605 g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
1608 buf=mono_array_addr(buffer, guchar, offset);
1611 g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
1614 sendflags = convert_socketflags (flags);
1615 if (sendflags == -1) {
1616 *error = WSAEOPNOTSUPP;
1620 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1621 if(ret==SOCKET_ERROR) {
1622 *error = WSAGetLastError ();
1630 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1633 MonoClassField *field;
1635 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1636 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1641 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1642 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1644 MonoThread *thread = NULL;
1651 MonoClass *sock_arr_class;
1654 mono_array_size_t socks_size;
1656 MONO_ARCH_SAVE_REGS;
1658 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1659 count = mono_array_length (*sockets);
1660 nfds = count - 3; /* NULL separators */
1661 pfds = g_new0 (mono_pollfd, nfds);
1663 for (i = 0; i < count; i++) {
1664 obj = mono_array_get (*sockets, MonoObject *, i);
1671 /* The socket array was bogus */
1677 pfds [idx].fd = Socket_to_SOCKET (obj);
1678 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1682 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1683 start = time (NULL);
1686 ret = mono_poll (pfds, nfds, timeout);
1687 if (timeout > 0 && ret < 0) {
1689 int sec = time (NULL) - start;
1691 timeout -= sec * 1000;
1697 if (ret == -1 && errno == EINTR) {
1700 thread = mono_thread_current ();
1702 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1709 /* Suspend requested? */
1710 mono_thread_interruption_checkpoint ();
1714 } while (ret == -1 && errno == EINTR);
1717 #ifdef PLATFORM_WIN32
1718 *error = WSAGetLastError ();
1720 *error = errno_to_WSA (errno, __func__);
1732 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1733 socks_size = ((mono_array_size_t)ret) + 3; /* space for the NULL delimiters */
1734 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1737 for (i = 0; i < count && ret > 0; i++) {
1740 obj = mono_array_get (*sockets, MonoObject *, i);
1747 pfd = &pfds [i - mode];
1748 if (pfd->revents == 0)
1752 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1753 mono_array_setref (socks, idx++, obj);
1754 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1755 mono_array_setref (socks, idx++, obj);
1756 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1757 mono_array_setref (socks, idx++, obj);
1765 static MonoObject* int_to_object (MonoDomain *domain, int val)
1767 return mono_value_box (domain, mono_get_int32_class (), &val);
1771 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1777 socklen_t valsize=sizeof(val);
1778 struct linger linger;
1779 socklen_t lingersize=sizeof(linger);
1781 socklen_t time_ms_size = sizeof (time_ms);
1784 socklen_t credsize = sizeof(cred);
1786 MonoDomain *domain=mono_domain_get();
1788 MonoClass *obj_class;
1789 MonoClassField *field;
1791 MONO_ARCH_SAVE_REGS;
1795 ret=convert_sockopt_level_and_name(level, name, &system_level,
1798 *error = WSAENOPROTOOPT;
1802 *obj_val = int_to_object (domain, 0);
1806 /* No need to deal with MulticastOption names here, because
1807 * you cant getsockopt AddMembership or DropMembership (the
1808 * int getsockopt will error, causing an exception)
1811 case SocketOptionName_Linger:
1812 case SocketOptionName_DontLinger:
1813 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1817 case SocketOptionName_SendTimeout:
1818 case SocketOptionName_ReceiveTimeout:
1819 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1823 case SocketOptionName_PeerCred:
1824 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1830 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1834 if(ret==SOCKET_ERROR) {
1835 *error = WSAGetLastError ();
1840 case SocketOptionName_Linger:
1841 /* build a System.Net.Sockets.LingerOption */
1842 obj_class=mono_class_from_name(get_socket_assembly (),
1843 "System.Net.Sockets",
1845 obj=mono_object_new(domain, obj_class);
1847 /* Locate and set the fields "bool enabled" and "int
1850 field=mono_class_get_field_from_name(obj_class, "enabled");
1851 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1853 field=mono_class_get_field_from_name(obj_class, "seconds");
1854 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1858 case SocketOptionName_DontLinger:
1859 /* construct a bool int in val - true if linger is off */
1860 obj = int_to_object (domain, !linger.l_onoff);
1863 case SocketOptionName_SendTimeout:
1864 case SocketOptionName_ReceiveTimeout:
1865 obj = int_to_object (domain, time_ms);
1869 case SocketOptionName_PeerCred:
1871 /* build a Mono.Posix.PeerCred+PeerCredData if
1874 static MonoImage *mono_posix_image = NULL;
1875 MonoPeerCredData *cred_data;
1877 if (mono_posix_image == NULL) {
1878 mono_posix_image=mono_image_loaded ("Mono.Posix");
1879 if (!mono_posix_image) {
1880 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1882 *error = WSAENOPROTOOPT;
1885 mono_posix_image = mono_assembly_get_image (sa);
1890 obj_class = mono_class_from_name(mono_posix_image,
1893 obj = mono_object_new(domain, obj_class);
1894 cred_data = (MonoPeerCredData *)obj;
1895 cred_data->pid = cred.pid;
1896 cred_data->uid = cred.uid;
1897 cred_data->gid = cred.gid;
1903 obj = int_to_object (domain, val);
1909 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1917 MONO_ARCH_SAVE_REGS;
1921 ret=convert_sockopt_level_and_name(level, name, &system_level,
1924 *error = WSAENOPROTOOPT;
1930 valsize=mono_array_length(*byte_val);
1931 buf=mono_array_addr(*byte_val, guchar, 0);
1933 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1934 if(ret==SOCKET_ERROR) {
1935 *error = WSAGetLastError ();
1939 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1940 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1942 struct in_addr inaddr;
1943 MonoClassField *field;
1945 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
1947 /* No idea why .net uses a 64bit type to hold a 32bit value...
1949 * Internal value of IPAddess is in little-endian order
1951 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
1958 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1960 struct in6_addr in6addr;
1961 MonoClassField *field;
1965 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
1966 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1968 /* Solaris has only the 8 bit version. */
1970 for(i=0; i<8; i++) {
1971 guint16 s = mono_array_get (data, guint16, i);
1972 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1973 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1977 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1981 #endif /* AF_INET6 */
1983 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)
1994 #ifdef HAVE_SOL_IPV6
1995 sol_ipv6 = SOL_IPV6;
1998 struct protoent *pent;
1999 pent = getprotobyname ("ipv6");
2000 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2008 struct protoent *pent;
2009 pent = getprotobyname ("ip");
2010 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2013 #endif /* AF_INET6 */
2015 MONO_ARCH_SAVE_REGS;
2017 ret=convert_sockopt_level_and_name(level, name, &system_level,
2020 *error = WSAENOPROTOOPT;
2027 /* Only one of obj_val, byte_val or int_val has data */
2029 MonoClassField *field;
2030 struct linger linger;
2034 case SocketOptionName_DontLinger:
2037 valsize=sizeof(linger);
2038 ret = _wapi_setsockopt (sock, system_level,
2039 system_name, &linger, valsize);
2042 case SocketOptionName_Linger:
2043 /* Dig out "bool enabled" and "int seconds"
2046 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2047 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2048 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2049 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2051 valsize=sizeof(linger);
2052 ret = _wapi_setsockopt (sock, system_level,
2053 system_name, &linger, valsize);
2055 case SocketOptionName_AddMembership:
2056 case SocketOptionName_DropMembership:
2057 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2059 MonoObject *address = NULL;
2062 if(system_level == sol_ipv6) {
2063 struct ipv6_mreq mreq6;
2068 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2069 address = *(gpointer *)(((char *)obj_val) + field->offset);
2072 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2075 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2076 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2078 ret = _wapi_setsockopt (sock, system_level,
2079 system_name, &mreq6,
2081 } else if(system_level == sol_ip)
2082 #endif /* AF_INET6 */
2084 #ifdef HAVE_STRUCT_IP_MREQN
2085 struct ip_mreqn mreq = {{0}};
2087 struct ip_mreq mreq = {{0}};
2088 #endif /* HAVE_STRUCT_IP_MREQN */
2090 /* pain! MulticastOption holds two IPAddress
2091 * members, so I have to dig the value out of
2094 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2095 address = *(gpointer *)(((char *)obj_val) + field->offset);
2097 /* address might not be defined and if so, set the address to ADDR_ANY.
2100 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2103 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2104 address = *(gpointer *)(((char *)obj_val) + field->offset);
2106 #ifdef HAVE_STRUCT_IP_MREQN
2108 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2112 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2114 #endif /* HAVE_STRUCT_IP_MREQN */
2116 ret = _wapi_setsockopt (sock, system_level,
2122 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2124 /* Cause an exception to be thrown */
2128 } else if (byte_val!=NULL) {
2129 int valsize=mono_array_length(byte_val);
2130 guchar *buf=mono_array_addr(byte_val, guchar, 0);
2132 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2133 if(ret==SOCKET_ERROR) {
2134 *error = WSAGetLastError ();
2138 /* ReceiveTimeout/SendTimeout get here */
2140 case SocketOptionName_DontFragment:
2141 #ifdef HAVE_IP_MTU_DISCOVER
2142 /* Fiddle with the value slightly if we're
2146 int_val = IP_PMTUDISC_DO;
2152 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2156 if(ret==SOCKET_ERROR) {
2157 *error = WSAGetLastError ();
2161 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2167 MONO_ARCH_SAVE_REGS;
2171 /* Currently, the values for how (recv=0, send=1, both=2) match
2174 ret = _wapi_shutdown (sock, how);
2175 if(ret==SOCKET_ERROR) {
2176 *error = WSAGetLastError ();
2181 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2183 MonoArray *output, gint32 *error)
2185 glong output_bytes = 0;
2186 gchar *i_buffer, *o_buffer;
2190 MONO_ARCH_SAVE_REGS;
2194 if (code == FIONBIO) {
2195 /* Invalid command. Must use Socket.Blocking */
2199 if (input == NULL) {
2203 i_buffer = mono_array_addr (input, gchar, 0);
2204 i_len = mono_array_length (input);
2207 if (output == NULL) {
2211 o_buffer = mono_array_addr (output, gchar, 0);
2212 o_len = mono_array_length (output);
2215 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2216 if (ret == SOCKET_ERROR) {
2217 *error = WSAGetLastError ();
2221 return (gint) output_bytes;
2224 #ifdef HAVE_SIOCGIFCONF
2226 is_loopback (int family, void *ad)
2228 char *ptr = (char *) ad;
2230 if (family == AF_INET) {
2231 return (ptr [0] == 127);
2235 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2242 get_local_ips (int family, int *nips)
2244 int addr_size, offset, fd, i, count;
2245 int max_ifaces = 50; /* 50 interfaces should be enough... */
2248 struct ifreq iflags;
2249 char *result, *tmp_ptr;
2250 gboolean ignore_loopback = FALSE;
2253 if (family == AF_INET) {
2254 addr_size = sizeof (struct in_addr);
2255 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2257 } else if (family == AF_INET6) {
2258 addr_size = sizeof (struct in6_addr);
2259 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2265 fd = socket (family, SOCK_STREAM, 0);
2267 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2268 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2269 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2271 g_free (ifc.ifc_buf);
2275 count = ifc.ifc_len / sizeof (struct ifreq);
2278 g_free (ifc.ifc_buf);
2283 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2284 strcpy (iflags.ifr_name, ifr->ifr_name);
2285 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2289 if ((iflags.ifr_flags & IFF_UP) == 0) {
2290 ifr->ifr_name [0] = '\0';
2294 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2295 ignore_loopback = TRUE;
2300 result = g_malloc (addr_size * count);
2302 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2303 if (ifr->ifr_name [0] == '\0') {
2308 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2313 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2314 tmp_ptr += addr_size;
2317 g_free (ifc.ifc_buf);
2322 get_local_ips (int family, int *nips)
2328 #endif /* HAVE_SIOCGIFCONF */
2331 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2332 MonoArray **h_aliases,
2333 MonoArray **h_addr_list,
2334 gboolean add_local_ips)
2336 MonoDomain *domain = mono_domain_get ();
2338 struct in_addr *local_in = NULL;
2341 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2345 *h_name=mono_string_new(domain, he->h_name);
2348 while(he->h_aliases[i]!=NULL) {
2352 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2354 while(he->h_aliases[i]!=NULL) {
2357 alias=mono_string_new(domain, he->h_aliases[i]);
2358 mono_array_setref (*h_aliases, i, alias);
2362 if (add_local_ips) {
2363 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2365 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2366 for (i = 0; i < nlocal_in; i++) {
2367 MonoString *addr_string;
2368 char addr [16], *ptr;
2370 ptr = (char *) &local_in [i];
2371 g_snprintf(addr, 16, "%u.%u.%u.%u",
2372 (unsigned char) ptr [0],
2373 (unsigned char) ptr [1],
2374 (unsigned char) ptr [2],
2375 (unsigned char) ptr [3]);
2377 addr_string = mono_string_new (domain, addr);
2378 mono_array_setref (*h_addr_list, i, addr_string);
2386 if (nlocal_in == 0) {
2388 while (he->h_addr_list[i]!=NULL) {
2392 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2394 while(he->h_addr_list[i]!=NULL) {
2395 MonoString *addr_string;
2398 g_snprintf(addr, 16, "%u.%u.%u.%u",
2399 (unsigned char)he->h_addr_list[i][0],
2400 (unsigned char)he->h_addr_list[i][1],
2401 (unsigned char)he->h_addr_list[i][2],
2402 (unsigned char)he->h_addr_list[i][3]);
2404 addr_string=mono_string_new(domain, addr);
2405 mono_array_setref (*h_addr_list, i, addr_string);
2413 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2414 MonoArray **h_aliases,
2415 MonoArray **h_addr_list)
2417 MonoDomain *domain = mono_domain_get ();
2419 *h_name=mono_string_new(domain, addr);
2420 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2421 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2422 mono_array_setref (*h_addr_list, 0, *h_name);
2428 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2429 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2430 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2432 MonoDomain *domain = mono_domain_get ();
2433 int i, host_count, host_index, family_hint;
2434 struct in_addr *local_in = NULL;
2436 struct in6_addr *local_in6 = NULL;
2438 gboolean from_local = FALSE;
2440 family_hint = get_family_hint ();
2442 if(he1 == NULL && he2 == NULL) {
2447 * Check if address length and family are correct
2449 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2453 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2458 * Get the aliases and host name from he1 or he2 whichever is
2459 * not null, if he1 is not null then take aliases from he1
2461 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2462 family_hint == PF_INET)) {
2463 *h_name=mono_string_new (domain, he1->h_name);
2466 while(he1->h_aliases[i]!=NULL) {
2470 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2473 while(he1->h_aliases[i]!=NULL) {
2476 alias=mono_string_new (domain, he1->h_aliases[i]);
2477 mono_array_setref (*h_aliases, i, alias);
2480 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2481 family_hint == PF_INET6)) {
2482 *h_name=mono_string_new (domain, he2->h_name);
2485 while(he2->h_aliases [i] != NULL) {
2489 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2492 while(he2->h_aliases[i]!=NULL) {
2495 alias=mono_string_new (domain, he2->h_aliases[i]);
2496 mono_array_setref (*h_aliases, i, alias);
2504 * Count the number of addresses in he1 + he2
2507 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2508 family_hint == PF_INET)) {
2510 while(he1->h_addr_list[i]!=NULL) {
2516 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2517 family_hint == PF_INET6)) {
2519 while(he2->h_addr_list[i]!=NULL) {
2529 if (add_local_ips) {
2530 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2531 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2533 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2534 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2536 if (nlocal_in || nlocal_in6) {
2538 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2539 nlocal_in + nlocal_in6);
2543 for (n = 0; n < nlocal_in6; n++) {
2544 MonoString *addr_string;
2546 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2548 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2551 addr_string = mono_string_new (domain, addr);
2552 mono_array_setref (*h_addr_list, host_index, addr_string);
2560 for (n = 0; n < nlocal_in; n++) {
2561 MonoString *addr_string;
2563 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2565 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2568 addr_string = mono_string_new (domain, addr);
2569 mono_array_setref (*h_addr_list, host_index, addr_string);
2583 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2585 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2586 family_hint == PF_INET6)) {
2588 while(he2->h_addr_list[i] != NULL) {
2589 MonoString *addr_string;
2591 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2593 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2597 addr_string = mono_string_new (domain, addr);
2598 mono_array_setref (*h_addr_list, host_index, addr_string);
2605 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2606 family_hint == PF_INET)) {
2608 while(he1->h_addr_list[i] != NULL) {
2609 MonoString *addr_string;
2611 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2613 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2617 addr_string=mono_string_new (domain, addr);
2618 mono_array_setref (*h_addr_list, host_index, addr_string);
2629 #if defined(AF_INET6)
2631 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2632 MonoArray **h_aliases,
2633 MonoArray **h_addr_list,
2634 gboolean add_local_ips)
2637 struct addrinfo *ai = NULL;
2638 struct in_addr *local_in = NULL;
2640 struct in6_addr *local_in6 = NULL;
2644 MonoDomain *domain = mono_domain_get ();
2647 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2648 if (add_local_ips) {
2649 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2650 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2651 if (nlocal_in || nlocal_in6) {
2652 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2654 MonoString *addr_string;
2658 for (i = 0; i < nlocal_in; i++) {
2659 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2660 addr_string = mono_string_new (domain, addr);
2661 mono_array_setref (*h_addr_list, addr_index, addr_string);
2667 MonoString *addr_string;
2672 for (i = 0; i < nlocal_in6; i++) {
2673 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2675 addr_string = mono_string_new (domain, addr);
2676 mono_array_setref (*h_addr_list, addr_index, addr_string);
2685 freeaddrinfo (info);
2694 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2695 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2701 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2703 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2704 MonoString *addr_string;
2706 char buffer [48]; /* Max. size for IPv6 */
2708 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2712 if(ai->ai_family == PF_INET) {
2713 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2715 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2719 addr_string=mono_string_new(domain, buffer);
2721 addr_string=mono_string_new(domain, "");
2724 mono_array_setref (*h_addr_list, addr_index, addr_string);
2727 if (ai->ai_canonname != NULL) {
2728 *h_name=mono_string_new(domain, ai->ai_canonname);
2730 *h_name=mono_string_new(domain, buffer);
2746 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2748 gboolean add_local_ips = FALSE;
2749 #ifdef HAVE_SIOCGIFCONF
2750 gchar this_hostname [256];
2752 #if !defined(HAVE_GETHOSTBYNAME2_R)
2753 struct addrinfo *info = NULL, hints;
2756 MONO_ARCH_SAVE_REGS;
2758 hostname=mono_string_to_utf8 (host);
2759 #ifdef HAVE_SIOCGIFCONF
2760 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2761 if (!strcmp (hostname, this_hostname))
2762 add_local_ips = TRUE;
2766 memset(&hints, 0, sizeof(hints));
2767 hints.ai_family = get_family_hint ();
2768 hints.ai_socktype = SOCK_STREAM;
2769 hints.ai_flags = AI_CANONNAME;
2771 if (getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2777 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2779 struct hostent he1,*hp1, he2, *hp2;
2780 int buffer_size1, buffer_size2;
2781 char *buffer1, *buffer2;
2783 gboolean return_value;
2786 MONO_ARCH_SAVE_REGS;
2788 hostname=mono_string_to_utf8 (host);
2790 #ifdef HAVE_SIOCGIFCONF
2791 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2792 if (!strcmp (hostname, this_hostname))
2793 add_local_ips = TRUE;
2799 buffer1 = g_malloc0(buffer_size1);
2800 buffer2 = g_malloc0(buffer_size2);
2802 while (gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2803 &hp1, &herr) == ERANGE) {
2805 buffer1 = g_realloc(buffer1, buffer_size1);
2810 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2811 buffer_size2, &hp2, &herr) == ERANGE) {
2813 buffer2 = g_realloc(buffer2, buffer_size2);
2819 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2820 h_addr_list, add_local_ips);
2826 return(return_value);
2827 #endif /* HAVE_GETHOSTBYNAME2_R */
2829 #else /* AF_INET6 */
2830 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2834 gboolean add_local_ips = FALSE;
2835 #ifdef HAVE_SIOCGIFCONF
2836 gchar this_hostname [256];
2839 MONO_ARCH_SAVE_REGS;
2841 hostname=mono_string_to_utf8(host);
2842 #ifdef HAVE_SIOCGIFCONF
2843 if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2844 if (!strcmp (hostname, this_hostname))
2845 add_local_ips = TRUE;
2849 he = _wapi_gethostbyname (hostname);
2856 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2858 #endif /* AF_INET6 */
2860 #ifndef HAVE_INET_PTON
2862 inet_pton (int family, const char *address, void *inaddrp)
2864 if (family == AF_INET) {
2865 #ifdef HAVE_INET_ATON
2866 struct in_addr inaddr;
2868 if (!inet_aton (address, &inaddr))
2871 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2874 /* assume the system has inet_addr(), if it doesn't
2875 have that we're pretty much screwed... */
2878 if (!strcmp (address, "255.255.255.255")) {
2879 /* special-case hack */
2880 inaddr = 0xffffffff;
2882 inaddr = inet_addr (address);
2884 #define INADDR_NONE ((in_addr_t) -1)
2886 if (inaddr == INADDR_NONE)
2890 memcpy (inaddrp, &inaddr, sizeof (guint32));
2892 #endif /* HAVE_INET_ATON */
2897 #endif /* !HAVE_INET_PTON */
2899 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2905 struct sockaddr_in saddr;
2906 struct sockaddr_in6 saddr6;
2907 struct addrinfo *info = NULL, hints;
2909 char hostname[1024] = {0};
2912 struct in_addr inaddr;
2917 v1 = mono_framework_version () == 1;
2919 address = mono_string_to_utf8 (addr);
2922 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2923 /* Maybe an ipv6 address */
2924 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2930 saddr6.sin6_family = AF_INET6;
2935 saddr.sin_family = AF_INET;
2940 flags = NI_NAMEREQD;
2943 if(family == AF_INET) {
2944 #if HAVE_SOCKADDR_IN_SIN_LEN
2945 saddr.sin_len = sizeof (saddr);
2947 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2948 hostname, sizeof(hostname), NULL, 0,
2952 } else if(family == AF_INET6) {
2953 #if HAVE_SOCKADDR_IN6_SIN_LEN
2954 saddr6.sin6_len = sizeof (saddr6);
2956 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2957 hostname, sizeof(hostname), NULL, 0,
2963 memset (&hints, 0, sizeof(hints));
2964 hints.ai_family = get_family_hint ();
2965 hints.ai_socktype = SOCK_STREAM;
2966 hints.ai_flags = AI_CANONNAME;
2968 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
2972 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
2974 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
2979 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
2983 ret = ipaddr_to_IPHostEntry (address, h_name,
2984 h_aliases, h_addr_list);
2987 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
2988 h_addr_list, FALSE);
2996 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
2998 gchar hostname[256];
3001 MONO_ARCH_SAVE_REGS;
3003 ret = gethostname (hostname, sizeof (hostname));
3008 *h_name=mono_string_new(mono_domain_get (), hostname);
3013 void mono_network_init(void)
3018 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3020 g_error(G_GNUC_PRETTY_FUNCTION ": Couldn't initialise networking");
3025 g_message(G_GNUC_PRETTY_FUNCTION ": Using socket library: %s", wsadata.szDescription);
3026 g_message(G_GNUC_PRETTY_FUNCTION ": Socket system status: %s", wsadata.szSystemStatus);
3030 void mono_network_cleanup(void)