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)
14 #ifndef DISABLE_SOCKETS
24 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
31 #include <arpa/inet.h>
34 #include <mono/metadata/object.h>
35 #include <mono/io-layer/io-layer.h>
36 #include <mono/metadata/socket-io.h>
37 #include <mono/metadata/exception.h>
38 #include <mono/metadata/assembly.h>
39 #include <mono/metadata/appdomain.h>
40 #include <mono/metadata/file-io.h>
41 #include <mono/metadata/threads.h>
42 #include <mono/metadata/threads-types.h>
43 #include <mono/utils/mono-poll.h>
44 /* FIXME change this code to not mess so much with the internals */
45 #include <mono/metadata/class-internals.h>
46 #include <mono/metadata/threadpool-internals.h>
47 #include <mono/metadata/domain-internals.h>
49 #ifdef HAVE_SYS_TIME_H
52 #ifdef HAVE_SYS_IOCTL_H
53 #include <sys/ioctl.h>
62 #ifdef HAVE_SYS_FILIO_H
63 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
65 #ifdef HAVE_SYS_SOCKIO_H
66 #include <sys/sockio.h> /* defines SIOCATMARK */
72 #include "mono/io-layer/socket-wrappers.h"
74 #if defined(HOST_WIN32)
75 /* This is a kludge to make this file build under cygwin:
76 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
77 * prototypes for some but not all required functions (notably
78 * inet_ntop() is missing), but the libws2_32 library is missing the
79 * actual implementations of these functions.
84 #ifdef PLATFORM_ANDROID
85 // not yet actually implemented...
90 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
93 * Some older versions of libc provide IPV6 support without defining the AI_ADDRCONFIG
94 * flag for getaddrinfo.
97 #define AI_ADDRCONFIG 0
100 static gint32 convert_family(MonoAddressFamily mono_family)
104 switch(mono_family) {
105 case AddressFamily_Unknown:
106 case AddressFamily_ImpLink:
107 case AddressFamily_Pup:
108 case AddressFamily_Chaos:
109 case AddressFamily_Iso:
110 case AddressFamily_Ecma:
111 case AddressFamily_DataKit:
112 case AddressFamily_Ccitt:
113 case AddressFamily_DataLink:
114 case AddressFamily_Lat:
115 case AddressFamily_HyperChannel:
116 case AddressFamily_NetBios:
117 case AddressFamily_VoiceView:
118 case AddressFamily_FireFox:
119 case AddressFamily_Banyan:
120 case AddressFamily_Atm:
121 case AddressFamily_Cluster:
122 case AddressFamily_Ieee12844:
123 case AddressFamily_NetworkDesigners:
124 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
127 case AddressFamily_Unspecified:
131 case AddressFamily_Unix:
135 case AddressFamily_InterNetwork:
139 case AddressFamily_Ipx:
145 case AddressFamily_Sna:
151 case AddressFamily_DecNet:
157 case AddressFamily_AppleTalk:
161 case AddressFamily_InterNetworkV6:
166 case AddressFamily_Irda:
172 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
178 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
180 MonoAddressFamily family=AddressFamily_Unknown;
184 family=AddressFamily_Unspecified;
188 family=AddressFamily_Unix;
192 family=AddressFamily_InterNetwork;
197 family=AddressFamily_Ipx;
203 family=AddressFamily_Sna;
209 family=AddressFamily_DecNet;
214 family=AddressFamily_AppleTalk;
219 family=AddressFamily_InterNetworkV6;
225 family=AddressFamily_Irda;
229 g_warning("unknown address family 0x%x", af_family);
235 static gint32 convert_type(MonoSocketType mono_type)
240 case SocketType_Stream:
244 case SocketType_Dgram:
258 case SocketType_Seqpacket:
262 case SocketType_Unknown:
263 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
267 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
273 static gint32 convert_proto(MonoProtocolType mono_proto)
278 case ProtocolType_IP:
279 case ProtocolType_IPv6:
280 case ProtocolType_Icmp:
281 case ProtocolType_Igmp:
282 case ProtocolType_Ggp:
283 case ProtocolType_Tcp:
284 case ProtocolType_Pup:
285 case ProtocolType_Udp:
286 case ProtocolType_Idp:
287 /* These protocols are known (on my system at least) */
291 case ProtocolType_ND:
292 case ProtocolType_Raw:
293 case ProtocolType_Ipx:
294 case ProtocolType_Spx:
295 case ProtocolType_SpxII:
296 case ProtocolType_Unknown:
297 /* These protocols arent */
298 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
308 /* Convert MonoSocketFlags */
309 static gint32 convert_socketflags (gint32 sflags)
314 /* SocketFlags.None */
317 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
318 SocketFlags_DontRoute | SocketFlags_Partial))
319 /* Contains invalid flag values */
322 if (sflags & SocketFlags_OutOfBand)
324 if (sflags & SocketFlags_Peek)
326 if (sflags & SocketFlags_DontRoute)
327 flags |= MSG_DONTROUTE;
329 /* Ignore Partial - see bug 349688. Don't return -1, because
330 * according to the comment in that bug ms runtime doesn't for
331 * UDP sockets (this means we will silently ignore it for TCP
335 if (sflags & SocketFlags_Partial)
339 /* Don't do anything for MaxIOVectorLength */
340 if (sflags & SocketFlags_MaxIOVectorLength)
348 * 0 on success (mapped mono_level and mono_name to system_level and system_name
350 * -2 on non-fatal error (ie, must ignore)
352 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
353 MonoSocketOptionName mono_name,
357 switch (mono_level) {
358 case SocketOptionLevel_Socket:
359 *system_level = SOL_SOCKET;
362 case SocketOptionName_DontLinger:
363 /* This is SO_LINGER, because the setsockopt
364 * internal call maps DontLinger to SO_LINGER
367 *system_name = SO_LINGER;
369 case SocketOptionName_Debug:
370 *system_name = SO_DEBUG;
373 case SocketOptionName_AcceptConnection:
374 *system_name = SO_ACCEPTCONN;
377 case SocketOptionName_ReuseAddress:
378 *system_name = SO_REUSEADDR;
380 case SocketOptionName_KeepAlive:
381 *system_name = SO_KEEPALIVE;
383 case SocketOptionName_DontRoute:
384 *system_name = SO_DONTROUTE;
386 case SocketOptionName_Broadcast:
387 *system_name = SO_BROADCAST;
389 case SocketOptionName_Linger:
390 *system_name = SO_LINGER;
392 case SocketOptionName_OutOfBandInline:
393 *system_name = SO_OOBINLINE;
395 case SocketOptionName_SendBuffer:
396 *system_name = SO_SNDBUF;
398 case SocketOptionName_ReceiveBuffer:
399 *system_name = SO_RCVBUF;
401 case SocketOptionName_SendLowWater:
402 *system_name = SO_SNDLOWAT;
404 case SocketOptionName_ReceiveLowWater:
405 *system_name = SO_RCVLOWAT;
407 case SocketOptionName_SendTimeout:
408 *system_name = SO_SNDTIMEO;
410 case SocketOptionName_ReceiveTimeout:
411 *system_name = SO_RCVTIMEO;
413 case SocketOptionName_Error:
414 *system_name = SO_ERROR;
416 case SocketOptionName_Type:
417 *system_name = SO_TYPE;
420 case SocketOptionName_PeerCred:
421 *system_name = SO_PEERCRED;
424 case SocketOptionName_ExclusiveAddressUse:
425 #ifdef SO_EXCLUSIVEADDRUSE
426 *system_name = SO_EXCLUSIVEADDRUSE;
429 case SocketOptionName_UseLoopback:
430 #ifdef SO_USELOOPBACK
431 *system_name = SO_USELOOPBACK;
434 case SocketOptionName_MaxConnections:
436 *system_name = SO_MAXCONN;
438 #elif defined(SOMAXCONN)
439 *system_name = SOMAXCONN;
443 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
448 case SocketOptionLevel_IP:
450 *system_level = SOL_IP;
453 static int cached = 0;
457 struct protoent *pent;
459 pent = getprotobyname ("IP");
460 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
464 *system_level = proto;
466 #endif /* HAVE_SOL_IP */
469 case SocketOptionName_IPOptions:
470 *system_name = IP_OPTIONS;
473 case SocketOptionName_HeaderIncluded:
474 *system_name = IP_HDRINCL;
478 case SocketOptionName_TypeOfService:
479 *system_name = IP_TOS;
483 case SocketOptionName_IpTimeToLive:
484 *system_name = IP_TTL;
487 case SocketOptionName_MulticastInterface:
488 *system_name = IP_MULTICAST_IF;
490 case SocketOptionName_MulticastTimeToLive:
491 *system_name = IP_MULTICAST_TTL;
493 case SocketOptionName_MulticastLoopback:
494 *system_name = IP_MULTICAST_LOOP;
496 case SocketOptionName_AddMembership:
497 *system_name = IP_ADD_MEMBERSHIP;
499 case SocketOptionName_DropMembership:
500 *system_name = IP_DROP_MEMBERSHIP;
502 #ifdef HAVE_IP_PKTINFO
503 case SocketOptionName_PacketInformation:
504 *system_name = IP_PKTINFO;
506 #endif /* HAVE_IP_PKTINFO */
508 case SocketOptionName_DontFragment:
509 #ifdef HAVE_IP_DONTFRAGMENT
510 *system_name = IP_DONTFRAGMENT;
512 #elif defined HAVE_IP_MTU_DISCOVER
513 /* Not quite the same */
514 *system_name = IP_MTU_DISCOVER;
517 /* If the flag is not available on this system, we can ignore this error */
519 #endif /* HAVE_IP_DONTFRAGMENT */
520 case SocketOptionName_AddSourceMembership:
521 case SocketOptionName_DropSourceMembership:
522 case SocketOptionName_BlockSource:
523 case SocketOptionName_UnblockSource:
524 /* Can't figure out how to map these, so fall
528 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
534 case SocketOptionLevel_IPv6:
536 *system_level = SOL_IPV6;
539 static int cached = 0;
543 struct protoent *pent;
545 pent = getprotobyname ("IPV6");
546 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
550 *system_level = proto;
552 #endif /* HAVE_SOL_IPV6 */
555 case SocketOptionName_IpTimeToLive:
556 case SocketOptionName_HopLimit:
557 *system_name = IPV6_UNICAST_HOPS;
559 case SocketOptionName_MulticastInterface:
560 *system_name = IPV6_MULTICAST_IF;
562 case SocketOptionName_MulticastTimeToLive:
563 *system_name = IPV6_MULTICAST_HOPS;
565 case SocketOptionName_MulticastLoopback:
566 *system_name = IPV6_MULTICAST_LOOP;
568 case SocketOptionName_AddMembership:
569 *system_name = IPV6_JOIN_GROUP;
571 case SocketOptionName_DropMembership:
572 *system_name = IPV6_LEAVE_GROUP;
574 case SocketOptionName_PacketInformation:
575 #ifdef HAVE_IPV6_PKTINFO
576 *system_name = IPV6_PKTINFO;
579 case SocketOptionName_HeaderIncluded:
580 case SocketOptionName_IPOptions:
581 case SocketOptionName_TypeOfService:
582 case SocketOptionName_DontFragment:
583 case SocketOptionName_AddSourceMembership:
584 case SocketOptionName_DropSourceMembership:
585 case SocketOptionName_BlockSource:
586 case SocketOptionName_UnblockSource:
587 /* Can't figure out how to map these, so fall
591 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
595 break; /* SocketOptionLevel_IPv6 */
598 case SocketOptionLevel_Tcp:
600 *system_level = SOL_TCP;
603 static int cached = 0;
607 struct protoent *pent;
609 pent = getprotobyname ("TCP");
610 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
614 *system_level = proto;
616 #endif /* HAVE_SOL_TCP */
619 case SocketOptionName_NoDelay:
620 *system_name = TCP_NODELAY;
623 /* The documentation is talking complete
624 * bollocks here: rfc-1222 is titled
625 * 'Advancing the NSFNET Routing Architecture'
626 * and doesn't mention either of the words
627 * "expedite" or "urgent".
629 case SocketOptionName_BsdUrgent:
630 case SocketOptionName_Expedited:
633 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
638 case SocketOptionLevel_Udp:
639 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
642 case SocketOptionName_NoChecksum:
643 case SocketOptionName_ChecksumCoverage:
645 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
652 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
659 static MonoImage *get_socket_assembly (void)
661 static const char *version = NULL;
662 static gboolean moonlight;
663 MonoDomain *domain = mono_domain_get ();
665 if (version == NULL) {
666 version = mono_get_runtime_info ()->framework_version;
667 moonlight = !strcmp (version, "2.1");
670 if (domain->socket_assembly == NULL) {
671 MonoImage *socket_assembly;
674 socket_assembly = mono_image_loaded ("System.Net");
675 if (!socket_assembly) {
676 MonoAssembly *sa = mono_assembly_open ("System.Net.dll", NULL);
679 g_assert_not_reached ();
681 socket_assembly = mono_assembly_get_image (sa);
685 socket_assembly = mono_image_loaded ("System");
686 if (!socket_assembly) {
687 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
690 g_assert_not_reached ();
692 socket_assembly = mono_assembly_get_image (sa);
697 domain->socket_assembly = socket_assembly;
700 return domain->socket_assembly;
704 static gint32 get_family_hint(void)
706 MonoDomain *domain = mono_domain_get ();
708 if (!domain->inet_family_hint) {
709 MonoClass *socket_class;
710 MonoClassField *ipv6_field, *ipv4_field;
711 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
714 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
715 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
716 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
717 vtable = mono_class_vtable (mono_domain_get (), socket_class);
719 mono_runtime_class_init (vtable);
721 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
722 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
724 mono_domain_lock (domain);
725 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
726 domain->inet_family_hint = 1;
727 } else if (ipv4_enabled == 1) {
728 domain->inet_family_hint = 2;
730 domain->inet_family_hint = 3;
732 mono_domain_unlock (domain);
734 switch (domain->inet_family_hint) {
735 case 1: return PF_UNSPEC;
736 case 2: return PF_INET;
737 case 3: return PF_INET6;
744 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
755 sock_family=convert_family(family);
756 if(sock_family==-1) {
757 *error = WSAEAFNOSUPPORT;
761 sock_proto=convert_proto(proto);
763 *error = WSAEPROTONOSUPPORT;
767 sock_type=convert_type(type);
769 *error = WSAESOCKTNOSUPPORT;
773 sock = _wapi_socket (sock_family, sock_type, sock_proto,
774 NULL, 0, WSA_FLAG_OVERLAPPED);
776 if(sock==INVALID_SOCKET) {
777 *error = WSAGetLastError ();
781 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
782 return (GUINT_TO_POINTER (sock));
786 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
787 return (GUINT_TO_POINTER (sock));
791 return(GUINT_TO_POINTER (sock));
794 /* FIXME: the SOCKET parameter (here and in other functions in this
795 * file) is really an IntPtr which needs to be converted to a guint32.
797 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
802 LOGDEBUG (g_message ("%s: closing 0x%x", __func__, sock));
806 /* Clear any pending work item from this socket if the underlying
807 * polling system does not notify when the socket is closed */
808 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
812 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
816 LOGDEBUG (g_message("%s: returning %d", __func__, WSAGetLastError()));
818 return(WSAGetLastError());
821 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
831 ret=ioctlsocket(sock, FIONREAD, &amount);
832 if(ret==SOCKET_ERROR) {
833 *error = WSAGetLastError ();
840 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
851 * block == TRUE/FALSE means we will block/not block.
852 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
856 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
857 if(ret==SOCKET_ERROR) {
858 *error = WSAGetLastError ();
862 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
873 MonoInternalThread* curthread = mono_thread_internal_current ();
874 curthread->interrupt_on_stop = (gpointer)TRUE;
875 newsock = _wapi_accept (sock, NULL, 0);
876 curthread->interrupt_on_stop = (gpointer)FALSE;
879 newsock = _wapi_accept (sock, NULL, 0);
881 if(newsock==INVALID_SOCKET) {
882 *error = WSAGetLastError ();
886 return(GUINT_TO_POINTER (newsock));
889 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
899 ret = _wapi_listen (sock, backlog);
900 if(ret==SOCKET_ERROR) {
901 *error = WSAGetLastError ();
905 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
906 int sa_size, gint32 *error)
908 MonoDomain *domain = mono_domain_get ();
909 MonoObject *sockaddr_obj;
911 MonoAddressFamily family;
913 /* Build a System.Net.SocketAddress object instance */
914 if (!domain->sockaddr_class) {
915 domain->sockaddr_class=mono_class_from_name (get_socket_assembly (), "System.Net", "SocketAddress");
916 g_assert (domain->sockaddr_class);
918 sockaddr_obj=mono_object_new(domain, domain->sockaddr_class);
920 /* Locate the SocketAddress data buffer in the object */
921 if (!domain->sockaddr_data_field) {
922 domain->sockaddr_data_field=mono_class_get_field_from_name (domain->sockaddr_class, "data");
923 g_assert (domain->sockaddr_data_field);
926 /* Make sure there is space for the family and size bytes */
928 if (saddr->sa_family == AF_UNIX) {
929 /* sa_len includes the entire sockaddr size, so we don't need the
930 * N bytes (sizeof (unsigned short)) of the family. */
931 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size);
935 /* May be the +2 here is too conservative, as sa_len returns
936 * the length of the entire sockaddr_in/in6, including
937 * sizeof (unsigned short) of the family */
938 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
941 /* The data buffer is laid out as follows:
942 * bytes 0 and 1 are the address family
943 * bytes 2 and 3 are the port info
944 * the rest is the address info
947 family=convert_to_mono_family(saddr->sa_family);
948 if(family==AddressFamily_Unknown) {
949 *error = WSAEAFNOSUPPORT;
953 mono_array_set(data, guint8, 0, family & 0x0FF);
954 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
956 if(saddr->sa_family==AF_INET) {
957 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
958 guint16 port=ntohs(sa_in->sin_port);
959 guint32 address=ntohl(sa_in->sin_addr.s_addr);
962 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
965 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
966 mono_array_set(data, guint8, 3, (port) & 0xff);
967 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
968 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
969 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
970 mono_array_set(data, guint8, 7, (address) & 0xff);
972 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
974 return(sockaddr_obj);
976 } else if (saddr->sa_family == AF_INET6) {
977 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
980 guint16 port=ntohs(sa_in->sin6_port);
983 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
986 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
987 mono_array_set(data, guint8, 3, (port) & 0xff);
989 for(i=0; i<16; i++) {
990 mono_array_set(data, guint8, 8+i,
991 sa_in->sin6_addr.s6_addr[i]);
994 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
995 mono_array_set(data, guint8, 25,
996 (sa_in->sin6_scope_id >> 8) & 0xff);
997 mono_array_set(data, guint8, 26,
998 (sa_in->sin6_scope_id >> 16) & 0xff);
999 mono_array_set(data, guint8, 27,
1000 (sa_in->sin6_scope_id >> 24) & 0xff);
1002 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1004 return(sockaddr_obj);
1006 #ifdef HAVE_SYS_UN_H
1007 } else if (saddr->sa_family == AF_UNIX) {
1010 for (i = 0; i < sa_size; i++) {
1011 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
1014 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1016 return sockaddr_obj;
1019 *error = WSAEAFNOSUPPORT;
1024 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
1026 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1030 MONO_ARCH_SAVE_REGS;
1035 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1037 if(ret==SOCKET_ERROR) {
1038 *error = WSAGetLastError ();
1042 LOGDEBUG (g_message("%s: bound to %s port %d", __func__, inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr), ntohs(((struct sockaddr_in *)&sa)->sin_port)));
1044 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1048 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
1050 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1054 MONO_ARCH_SAVE_REGS;
1059 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1061 if(ret==SOCKET_ERROR) {
1062 *error = WSAGetLastError ();
1066 LOGDEBUG (g_message("%s: connected to %s port %d", __func__, inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr), ntohs(((struct sockaddr_in *)&sa)->sin_port)));
1068 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1072 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1076 MonoClassField *field;
1081 /* Dig the SocketAddress data buffer out of the object */
1082 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1083 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1085 /* The data buffer is laid out as follows:
1086 * byte 0 is the address family low byte
1087 * byte 1 is the address family high byte
1089 * bytes 2 and 3 are the port info
1090 * the rest is the address info
1092 * the rest is the file name
1094 len = mono_array_length (data);
1096 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1099 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1100 if (family == AF_INET) {
1101 struct sockaddr_in *sa;
1106 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1109 sa = g_new0 (struct sockaddr_in, 1);
1110 port = (mono_array_get (data, guint8, 2) << 8) +
1111 mono_array_get (data, guint8, 3);
1112 address = (mono_array_get (data, guint8, 4) << 24) +
1113 (mono_array_get (data, guint8, 5) << 16 ) +
1114 (mono_array_get (data, guint8, 6) << 8) +
1115 mono_array_get (data, guint8, 7);
1117 sa->sin_family = family;
1118 sa->sin_addr.s_addr = htonl (address);
1119 sa->sin_port = htons (port);
1121 *sa_size = sizeof(struct sockaddr_in);
1122 return((struct sockaddr *)sa);
1125 } else if (family == AF_INET6) {
1126 struct sockaddr_in6 *sa;
1132 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1135 sa = g_new0 (struct sockaddr_in6, 1);
1136 port = mono_array_get (data, guint8, 3) +
1137 (mono_array_get (data, guint8, 2) << 8);
1138 scopeid = mono_array_get (data, guint8, 24) +
1139 (mono_array_get (data, guint8, 25) << 8) +
1140 (mono_array_get (data, guint8, 26) << 16) +
1141 (mono_array_get (data, guint8, 27) << 24);
1143 sa->sin6_family = family;
1144 sa->sin6_port = htons (port);
1145 sa->sin6_scope_id = scopeid;
1147 for(i=0; i<16; i++) {
1148 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1151 *sa_size = sizeof(struct sockaddr_in6);
1152 return((struct sockaddr *)sa);
1154 #ifdef HAVE_SYS_UN_H
1155 } else if (family == AF_UNIX) {
1156 struct sockaddr_un *sock_un;
1159 /* Need a byte for the '\0' terminator/prefix, and the first
1160 * two bytes hold the SocketAddress family
1162 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1163 mono_raise_exception (mono_get_exception_index_out_of_range ());
1166 sock_un = g_new0 (struct sockaddr_un, 1);
1168 sock_un->sun_family = family;
1169 for (i = 0; i < len - 2; i++) {
1170 sock_un->sun_path [i] = mono_array_get (data, guint8,
1176 return (struct sockaddr *)sock_un;
1179 *error = WSAEAFNOSUPPORT;
1184 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1186 struct sockaddr *sa;
1190 MONO_ARCH_SAVE_REGS;
1194 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1199 LOGDEBUG (g_message("%s: binding to %s port %d", __func__, inet_ntoa(((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port)));
1201 ret = _wapi_bind (sock, sa, sa_size);
1202 if(ret==SOCKET_ERROR) {
1203 *error = WSAGetLastError ();
1216 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1217 gint timeout, gint32 *error)
1219 MonoInternalThread *thread = NULL;
1225 MONO_ARCH_SAVE_REGS;
1227 pfds = g_new0 (mono_pollfd, 1);
1228 pfds[0].fd = GPOINTER_TO_INT (sock);
1229 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1230 (mode == SelectModeWrite) ? MONO_POLLOUT :
1231 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1233 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1234 start = time (NULL);
1238 ret = mono_poll (pfds, 1, timeout);
1239 if (timeout > 0 && ret < 0) {
1241 int sec = time (NULL) - start;
1243 timeout -= sec * 1000;
1251 if (ret == -1 && errno == EINTR) {
1254 if (thread == NULL) {
1255 thread = mono_thread_internal_current ();
1258 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1264 /* Suspend requested? */
1265 mono_thread_interruption_checkpoint ();
1269 } while (ret == -1 && errno == EINTR);
1273 *error = WSAGetLastError ();
1275 *error = errno_to_WSA (errno, __func__);
1290 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1292 struct sockaddr *sa;
1296 MONO_ARCH_SAVE_REGS;
1300 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1305 LOGDEBUG (g_message("%s: connecting to %s port %d", __func__, inet_ntoa(((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port)));
1307 ret = _wapi_connect (sock, sa, sa_size);
1308 if(ret==SOCKET_ERROR) {
1309 *error = WSAGetLastError ();
1315 /* These #defines from mswsock.h from wine. Defining them here allows
1316 * us to build this file on a mingw box that doesn't know the magic
1317 * numbers, but still run on a newer windows box that does.
1319 #ifndef WSAID_DISCONNECTEX
1320 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1321 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1324 #ifndef WSAID_TRANSMITFILE
1325 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1326 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1329 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1332 glong output_bytes = 0;
1333 GUID disco_guid = WSAID_DISCONNECTEX;
1334 GUID trans_guid = WSAID_TRANSMITFILE;
1335 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1336 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1339 MONO_ARCH_SAVE_REGS;
1343 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1345 /* I _think_ the extension function pointers need to be looked
1346 * up for each socket. FIXME: check the best way to store
1347 * pointers to functions in managed objects that still works
1348 * on 64bit platforms.
1350 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1351 (void *)&disco_guid, sizeof(GUID),
1352 (void *)&_wapi_disconnectex, sizeof(void *),
1353 &output_bytes, NULL, NULL);
1355 /* make sure that WSAIoctl didn't put crap in the
1358 _wapi_disconnectex = NULL;
1360 /* Look up the TransmitFile extension function pointer
1361 * instead of calling TransmitFile() directly, because
1362 * apparently "Several of the extension functions have
1363 * been available since WinSock 1.1 and are exported
1364 * from MSWsock.dll, however it's not advisable to
1365 * link directly to this dll as this ties you to the
1366 * Microsoft WinSock provider. A provider neutral way
1367 * of accessing these extension functions is to load
1368 * them dynamically via WSAIoctl using the
1369 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1370 * should, theoretically, allow you to access these
1371 * functions from any provider that supports them..."
1372 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1374 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1375 (void *)&trans_guid, sizeof(GUID),
1376 (void *)&_wapi_transmitfile, sizeof(void *),
1377 &output_bytes, NULL, NULL);
1379 _wapi_transmitfile = NULL;
1383 if (_wapi_disconnectex != NULL) {
1384 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1385 } else if (_wapi_transmitfile != NULL) {
1386 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1387 TF_DISCONNECT | TF_REUSE_SOCKET);
1389 *error = ERROR_NOT_SUPPORTED;
1393 if (bret == FALSE) {
1394 *error = WSAGetLastError ();
1398 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1405 MONO_ARCH_SAVE_REGS;
1409 alen = mono_array_length (buffer);
1410 if (offset > alen - count) {
1414 buf=mono_array_addr(buffer, guchar, offset);
1416 recvflags = convert_socketflags (flags);
1417 if (recvflags == -1) {
1418 *error = WSAEOPNOTSUPP;
1424 MonoInternalThread* curthread = mono_thread_internal_current ();
1425 curthread->interrupt_on_stop = (gpointer)TRUE;
1426 ret = _wapi_recv (sock, buf, count, recvflags);
1427 curthread->interrupt_on_stop = (gpointer)FALSE;
1430 ret = _wapi_recv (sock, buf, count, recvflags);
1433 if(ret==SOCKET_ERROR) {
1434 *error = WSAGetLastError ();
1441 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1446 DWORD recvflags = 0;
1448 MONO_ARCH_SAVE_REGS;
1452 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1453 count = mono_array_length (buffers);
1455 recvflags = convert_socketflags (flags);
1456 if (recvflags == -1) {
1457 *error = WSAEOPNOTSUPP;
1461 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1462 if (ret == SOCKET_ERROR) {
1463 *error = WSAGetLastError ();
1470 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1476 struct sockaddr *sa;
1479 MONO_ARCH_SAVE_REGS;
1483 alen = mono_array_length (buffer);
1484 if (offset > alen - count) {
1488 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1493 buf=mono_array_addr(buffer, guchar, offset);
1495 recvflags = convert_socketflags (flags);
1496 if (recvflags == -1) {
1497 *error = WSAEOPNOTSUPP;
1501 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1502 if(ret==SOCKET_ERROR) {
1504 *error = WSAGetLastError ();
1508 /* If we didn't get a socket size, then we're probably a
1509 * connected connection-oriented socket and the stack hasn't
1510 * returned the remote address. All we can do is return null.
1513 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1522 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1529 MONO_ARCH_SAVE_REGS;
1533 alen = mono_array_length (buffer);
1534 if (offset > alen - count) {
1538 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1540 buf=mono_array_addr(buffer, guchar, offset);
1542 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1544 sendflags = convert_socketflags (flags);
1545 if (sendflags == -1) {
1546 *error = WSAEOPNOTSUPP;
1550 ret = _wapi_send (sock, buf, count, sendflags);
1551 if(ret==SOCKET_ERROR) {
1552 *error = WSAGetLastError ();
1559 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1564 DWORD sendflags = 0;
1566 MONO_ARCH_SAVE_REGS;
1570 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1571 count = mono_array_length (buffers);
1573 sendflags = convert_socketflags (flags);
1574 if (sendflags == -1) {
1575 *error = WSAEOPNOTSUPP;
1579 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1580 if (ret == SOCKET_ERROR) {
1581 *error = WSAGetLastError ();
1588 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1594 struct sockaddr *sa;
1597 MONO_ARCH_SAVE_REGS;
1601 alen = mono_array_length (buffer);
1602 if (offset > alen - count) {
1606 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1611 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1613 buf=mono_array_addr(buffer, guchar, offset);
1615 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1617 sendflags = convert_socketflags (flags);
1618 if (sendflags == -1) {
1619 *error = WSAEOPNOTSUPP;
1623 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1624 if(ret==SOCKET_ERROR) {
1625 *error = WSAGetLastError ();
1633 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1636 MonoClassField *field;
1638 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1639 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1644 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1645 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1647 MonoInternalThread *thread = NULL;
1654 MonoClass *sock_arr_class;
1657 uintptr_t socks_size;
1659 MONO_ARCH_SAVE_REGS;
1661 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1662 count = mono_array_length (*sockets);
1663 nfds = count - 3; /* NULL separators */
1664 pfds = g_new0 (mono_pollfd, nfds);
1666 for (i = 0; i < count; i++) {
1667 obj = mono_array_get (*sockets, MonoObject *, i);
1674 /* The socket array was bogus */
1680 pfds [idx].fd = Socket_to_SOCKET (obj);
1681 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1685 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1686 start = time (NULL);
1689 ret = mono_poll (pfds, nfds, timeout);
1690 if (timeout > 0 && ret < 0) {
1692 int sec = time (NULL) - start;
1694 timeout -= sec * 1000;
1700 if (ret == -1 && errno == EINTR) {
1703 thread = mono_thread_internal_current ();
1705 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1712 /* Suspend requested? */
1713 mono_thread_interruption_checkpoint ();
1717 } while (ret == -1 && errno == EINTR);
1721 *error = WSAGetLastError ();
1723 *error = errno_to_WSA (errno, __func__);
1735 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1736 socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
1737 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1740 for (i = 0; i < count && ret > 0; i++) {
1743 obj = mono_array_get (*sockets, MonoObject *, i);
1750 pfd = &pfds [i - mode];
1751 if (pfd->revents == 0)
1755 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1756 mono_array_setref (socks, idx++, obj);
1757 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1758 mono_array_setref (socks, idx++, obj);
1759 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1760 mono_array_setref (socks, idx++, obj);
1768 static MonoObject* int_to_object (MonoDomain *domain, int val)
1770 return mono_value_box (domain, mono_get_int32_class (), &val);
1774 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1780 socklen_t valsize=sizeof(val);
1781 struct linger linger;
1782 socklen_t lingersize=sizeof(linger);
1784 socklen_t time_ms_size = sizeof (time_ms);
1786 # if defined(__OpenBSD__)
1787 struct sockpeercred cred;
1791 socklen_t credsize = sizeof(cred);
1793 MonoDomain *domain=mono_domain_get();
1795 MonoClass *obj_class;
1796 MonoClassField *field;
1798 MONO_ARCH_SAVE_REGS;
1802 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1803 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
1804 system_level = SOL_SOCKET;
1805 system_name = SO_REUSEADDR;
1811 ret = convert_sockopt_level_and_name (level, name, &system_level, &system_name);
1815 *error = WSAENOPROTOOPT;
1819 *obj_val = int_to_object (domain, 0);
1823 /* No need to deal with MulticastOption names here, because
1824 * you cant getsockopt AddMembership or DropMembership (the
1825 * int getsockopt will error, causing an exception)
1828 case SocketOptionName_Linger:
1829 case SocketOptionName_DontLinger:
1830 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1834 case SocketOptionName_SendTimeout:
1835 case SocketOptionName_ReceiveTimeout:
1836 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1840 case SocketOptionName_PeerCred:
1841 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1847 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1851 if(ret==SOCKET_ERROR) {
1852 *error = WSAGetLastError ();
1857 case SocketOptionName_Linger:
1858 /* build a System.Net.Sockets.LingerOption */
1859 obj_class=mono_class_from_name(get_socket_assembly (),
1860 "System.Net.Sockets",
1862 obj=mono_object_new(domain, obj_class);
1864 /* Locate and set the fields "bool enabled" and "int
1867 field=mono_class_get_field_from_name(obj_class, "enabled");
1868 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1870 field=mono_class_get_field_from_name(obj_class, "seconds");
1871 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1875 case SocketOptionName_DontLinger:
1876 /* construct a bool int in val - true if linger is off */
1877 obj = int_to_object (domain, !linger.l_onoff);
1880 case SocketOptionName_SendTimeout:
1881 case SocketOptionName_ReceiveTimeout:
1882 obj = int_to_object (domain, time_ms);
1886 case SocketOptionName_PeerCred:
1888 /* build a Mono.Posix.PeerCred+PeerCredData if
1891 static MonoImage *mono_posix_image = NULL;
1892 MonoPeerCredData *cred_data;
1894 if (mono_posix_image == NULL) {
1895 mono_posix_image=mono_image_loaded ("Mono.Posix");
1896 if (!mono_posix_image) {
1897 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1899 *error = WSAENOPROTOOPT;
1902 mono_posix_image = mono_assembly_get_image (sa);
1907 obj_class = mono_class_from_name(mono_posix_image,
1910 obj = mono_object_new(domain, obj_class);
1911 cred_data = (MonoPeerCredData *)obj;
1912 cred_data->pid = cred.pid;
1913 cred_data->uid = cred.uid;
1914 cred_data->gid = cred.gid;
1920 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1921 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse)
1924 obj = int_to_object (domain, val);
1929 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1937 MONO_ARCH_SAVE_REGS;
1941 ret=convert_sockopt_level_and_name(level, name, &system_level,
1944 *error = WSAENOPROTOOPT;
1950 valsize=mono_array_length(*byte_val);
1951 buf=mono_array_addr(*byte_val, guchar, 0);
1953 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1954 if(ret==SOCKET_ERROR) {
1955 *error = WSAGetLastError ();
1959 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1960 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1962 struct in_addr inaddr;
1963 MonoClassField *field;
1965 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
1967 /* No idea why .net uses a 64bit type to hold a 32bit value...
1969 * Internal value of IPAddess is in little-endian order
1971 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
1977 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1979 struct in6_addr in6addr;
1980 MonoClassField *field;
1984 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
1985 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1987 /* Solaris has only the 8 bit version. */
1989 for(i=0; i<8; i++) {
1990 guint16 s = mono_array_get (data, guint16, i);
1991 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1992 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1996 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
2000 #endif /* AF_INET6 */
2003 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)
2005 struct linger linger;
2015 #ifdef HAVE_SOL_IPV6
2016 sol_ipv6 = SOL_IPV6;
2019 struct protoent *pent;
2020 pent = getprotobyname ("ipv6");
2021 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2029 struct protoent *pent;
2030 pent = getprotobyname ("ip");
2031 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2034 #endif /* AF_INET6 */
2036 MONO_ARCH_SAVE_REGS;
2038 ret=convert_sockopt_level_and_name(level, name, &system_level,
2041 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2042 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
2043 system_name = SO_REUSEADDR;
2044 int_val = int_val ? 0 : 1;
2050 *error = WSAENOPROTOOPT;
2057 /* Only one of obj_val, byte_val or int_val has data */
2059 MonoClassField *field;
2063 case SocketOptionName_Linger:
2064 /* Dig out "bool enabled" and "int seconds"
2067 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2068 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2069 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2070 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2072 valsize=sizeof(linger);
2073 ret = _wapi_setsockopt (sock, system_level,
2074 system_name, &linger, valsize);
2076 case SocketOptionName_AddMembership:
2077 case SocketOptionName_DropMembership:
2078 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2080 MonoObject *address = NULL;
2083 if(system_level == sol_ipv6) {
2084 struct ipv6_mreq mreq6;
2089 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2090 address = *(gpointer *)(((char *)obj_val) + field->offset);
2093 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2096 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2097 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2099 ret = _wapi_setsockopt (sock, system_level,
2100 system_name, &mreq6,
2102 } else if(system_level == sol_ip)
2103 #endif /* AF_INET6 */
2105 #ifdef HAVE_STRUCT_IP_MREQN
2106 struct ip_mreqn mreq = {{0}};
2108 struct ip_mreq mreq = {{0}};
2109 #endif /* HAVE_STRUCT_IP_MREQN */
2111 /* pain! MulticastOption holds two IPAddress
2112 * members, so I have to dig the value out of
2115 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2116 address = *(gpointer *)(((char *)obj_val) + field->offset);
2118 /* address might not be defined and if so, set the address to ADDR_ANY.
2121 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2124 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2125 address = *(gpointer *)(((char *)obj_val) + field->offset);
2127 #ifdef HAVE_STRUCT_IP_MREQN
2129 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2133 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2135 #endif /* HAVE_STRUCT_IP_MREQN */
2137 ret = _wapi_setsockopt (sock, system_level,
2143 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2145 /* Cause an exception to be thrown */
2149 } else if (byte_val!=NULL) {
2150 int valsize = mono_array_length (byte_val);
2151 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2154 case SocketOptionName_DontLinger:
2156 linger.l_onoff = (*buf) ? 0 : 1;
2157 linger.l_linger = 0;
2158 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2164 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2168 /* ReceiveTimeout/SendTimeout get here */
2170 case SocketOptionName_DontLinger:
2171 linger.l_onoff = !int_val;
2172 linger.l_linger = 0;
2173 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2175 case SocketOptionName_DontFragment:
2176 #ifdef HAVE_IP_MTU_DISCOVER
2177 /* Fiddle with the value slightly if we're
2181 int_val = IP_PMTUDISC_DO;
2187 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2191 if(ret==SOCKET_ERROR) {
2192 *error = WSAGetLastError ();
2196 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2202 MONO_ARCH_SAVE_REGS;
2206 /* Currently, the values for how (recv=0, send=1, both=2) match
2209 ret = _wapi_shutdown (sock, how);
2210 if(ret==SOCKET_ERROR) {
2211 *error = WSAGetLastError ();
2216 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2218 MonoArray *output, gint32 *error)
2220 glong output_bytes = 0;
2221 gchar *i_buffer, *o_buffer;
2225 MONO_ARCH_SAVE_REGS;
2229 if (code == FIONBIO) {
2230 /* Invalid command. Must use Socket.Blocking */
2234 if (input == NULL) {
2238 i_buffer = mono_array_addr (input, gchar, 0);
2239 i_len = mono_array_length (input);
2242 if (output == NULL) {
2246 o_buffer = mono_array_addr (output, gchar, 0);
2247 o_len = mono_array_length (output);
2250 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2251 if (ret == SOCKET_ERROR) {
2252 *error = WSAGetLastError ();
2256 return (gint) output_bytes;
2259 #ifdef HAVE_SIOCGIFCONF
2261 is_loopback (int family, void *ad)
2263 char *ptr = (char *) ad;
2265 if (family == AF_INET) {
2266 return (ptr [0] == 127);
2270 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2277 get_local_ips (int family, int *nips)
2279 int addr_size, offset, fd, i, count;
2280 int max_ifaces = 50; /* 50 interfaces should be enough... */
2283 struct ifreq iflags;
2284 char *result, *tmp_ptr;
2285 gboolean ignore_loopback = FALSE;
2288 if (family == AF_INET) {
2289 addr_size = sizeof (struct in_addr);
2290 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2292 } else if (family == AF_INET6) {
2293 addr_size = sizeof (struct in6_addr);
2294 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2300 fd = socket (family, SOCK_STREAM, 0);
2302 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2303 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2304 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2306 g_free (ifc.ifc_buf);
2310 count = ifc.ifc_len / sizeof (struct ifreq);
2313 g_free (ifc.ifc_buf);
2318 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2319 strcpy (iflags.ifr_name, ifr->ifr_name);
2320 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2324 if ((iflags.ifr_flags & IFF_UP) == 0) {
2325 ifr->ifr_name [0] = '\0';
2329 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2330 ignore_loopback = TRUE;
2335 result = g_malloc (addr_size * count);
2337 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2338 if (ifr->ifr_name [0] == '\0') {
2343 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2348 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2349 tmp_ptr += addr_size;
2352 g_free (ifc.ifc_buf);
2357 get_local_ips (int family, int *nips)
2363 #endif /* HAVE_SIOCGIFCONF */
2366 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2367 MonoArray **h_aliases,
2368 MonoArray **h_addr_list,
2369 gboolean add_local_ips)
2371 MonoDomain *domain = mono_domain_get ();
2373 struct in_addr *local_in = NULL;
2377 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2381 *h_name=mono_string_new(domain, he->h_name);
2383 while(he->h_aliases[i]!=NULL) {
2387 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2389 while(he->h_aliases[i]!=NULL) {
2392 alias=mono_string_new(domain, he->h_aliases[i]);
2393 mono_array_setref (*h_aliases, i, alias);
2396 } else if (!add_local_ips) {
2400 if (add_local_ips) {
2401 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2403 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2404 for (i = 0; i < nlocal_in; i++) {
2405 MonoString *addr_string;
2406 char addr [16], *ptr;
2408 ptr = (char *) &local_in [i];
2409 g_snprintf(addr, 16, "%u.%u.%u.%u",
2410 (unsigned char) ptr [0],
2411 (unsigned char) ptr [1],
2412 (unsigned char) ptr [2],
2413 (unsigned char) ptr [3]);
2415 addr_string = mono_string_new (domain, addr);
2416 mono_array_setref (*h_addr_list, i, addr_string);
2421 } else if (he == NULL) {
2422 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2423 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2424 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2429 if (nlocal_in == 0 && he != NULL) {
2431 while (he->h_addr_list[i]!=NULL) {
2435 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2437 while(he->h_addr_list[i]!=NULL) {
2438 MonoString *addr_string;
2441 g_snprintf(addr, 16, "%u.%u.%u.%u",
2442 (unsigned char)he->h_addr_list[i][0],
2443 (unsigned char)he->h_addr_list[i][1],
2444 (unsigned char)he->h_addr_list[i][2],
2445 (unsigned char)he->h_addr_list[i][3]);
2447 addr_string=mono_string_new(domain, addr);
2448 mono_array_setref (*h_addr_list, i, addr_string);
2456 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2457 MonoArray **h_aliases,
2458 MonoArray **h_addr_list)
2460 MonoDomain *domain = mono_domain_get ();
2462 *h_name=mono_string_new(domain, addr);
2463 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2464 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2465 mono_array_setref (*h_addr_list, 0, *h_name);
2471 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2472 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2473 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2475 MonoDomain *domain = mono_domain_get ();
2476 int i, host_count, host_index, family_hint;
2477 struct in_addr *local_in = NULL;
2479 struct in6_addr *local_in6 = NULL;
2481 gboolean from_local = FALSE;
2483 family_hint = get_family_hint ();
2486 * Check if address length and family are correct
2488 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2492 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2497 * Get the aliases and host name from he1 or he2 whichever is
2498 * not null, if he1 is not null then take aliases from he1
2500 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2501 family_hint == PF_INET)) {
2502 *h_name=mono_string_new (domain, he1->h_name);
2505 while(he1->h_aliases[i]!=NULL) {
2509 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2512 while(he1->h_aliases[i]!=NULL) {
2515 alias=mono_string_new (domain, he1->h_aliases[i]);
2516 mono_array_setref (*h_aliases, i, alias);
2519 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2520 family_hint == PF_INET6)) {
2521 *h_name=mono_string_new (domain, he2->h_name);
2524 while(he2->h_aliases [i] != NULL) {
2528 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2531 while(he2->h_aliases[i]!=NULL) {
2534 alias=mono_string_new (domain, he2->h_aliases[i]);
2535 mono_array_setref (*h_aliases, i, alias);
2538 } else if (!add_local_ips) {
2543 * Count the number of addresses in he1 + he2
2546 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2547 family_hint == PF_INET)) {
2549 while(he1->h_addr_list[i]!=NULL) {
2555 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2556 family_hint == PF_INET6)) {
2558 while(he2->h_addr_list[i]!=NULL) {
2568 if (add_local_ips) {
2569 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2570 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2572 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2573 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2575 if (nlocal_in || nlocal_in6) {
2577 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2578 nlocal_in + nlocal_in6);
2582 for (n = 0; n < nlocal_in6; n++) {
2583 MonoString *addr_string;
2585 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2587 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2590 addr_string = mono_string_new (domain, addr);
2591 mono_array_setref (*h_addr_list, host_index, addr_string);
2599 for (n = 0; n < nlocal_in; n++) {
2600 MonoString *addr_string;
2602 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2604 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2607 addr_string = mono_string_new (domain, addr);
2608 mono_array_setref (*h_addr_list, host_index, addr_string);
2616 } else if (he1 == NULL && he2 == NULL) {
2617 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2618 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2619 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2629 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2631 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2632 family_hint == PF_INET6)) {
2634 while(he2->h_addr_list[i] != NULL) {
2635 MonoString *addr_string;
2637 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2639 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2643 addr_string = mono_string_new (domain, addr);
2644 mono_array_setref (*h_addr_list, host_index, addr_string);
2651 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2652 family_hint == PF_INET)) {
2654 while(he1->h_addr_list[i] != NULL) {
2655 MonoString *addr_string;
2657 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2659 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2663 addr_string=mono_string_new (domain, addr);
2664 mono_array_setref (*h_addr_list, host_index, addr_string);
2675 #if defined(AF_INET6)
2677 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2678 MonoArray **h_aliases,
2679 MonoArray **h_addr_list,
2680 gboolean add_local_ips)
2683 struct addrinfo *ai = NULL;
2684 struct in_addr *local_in = NULL;
2686 struct in6_addr *local_in6 = NULL;
2690 MonoDomain *domain = mono_domain_get ();
2693 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2694 if (add_local_ips) {
2695 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2696 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2697 if (nlocal_in || nlocal_in6) {
2698 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2700 MonoString *addr_string;
2704 for (i = 0; i < nlocal_in; i++) {
2705 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2706 addr_string = mono_string_new (domain, addr);
2707 mono_array_setref (*h_addr_list, addr_index, addr_string);
2713 MonoString *addr_string;
2718 for (i = 0; i < nlocal_in6; i++) {
2719 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2721 addr_string = mono_string_new (domain, addr);
2722 mono_array_setref (*h_addr_list, addr_index, addr_string);
2731 freeaddrinfo (info);
2740 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2741 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2747 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2749 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2750 MonoString *addr_string;
2752 char buffer [48]; /* Max. size for IPv6 */
2754 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2758 if(ai->ai_family == PF_INET) {
2759 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2761 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2765 addr_string=mono_string_new(domain, buffer);
2767 addr_string=mono_string_new(domain, "");
2770 mono_array_setref (*h_addr_list, addr_index, addr_string);
2773 if (ai->ai_canonname != NULL) {
2774 *h_name=mono_string_new(domain, ai->ai_canonname);
2776 *h_name=mono_string_new(domain, buffer);
2792 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2794 gboolean add_local_ips = FALSE;
2795 #ifdef HAVE_SIOCGIFCONF
2796 gchar this_hostname [256];
2798 #if !defined(HAVE_GETHOSTBYNAME2_R)
2799 struct addrinfo *info = NULL, hints;
2802 MONO_ARCH_SAVE_REGS;
2804 hostname=mono_string_to_utf8 (host);
2805 if (*hostname == '\0')
2806 add_local_ips = TRUE;
2807 #ifdef HAVE_SIOCGIFCONF
2808 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2809 if (!strcmp (hostname, this_hostname))
2810 add_local_ips = TRUE;
2814 memset(&hints, 0, sizeof(hints));
2815 hints.ai_family = get_family_hint ();
2816 hints.ai_socktype = SOCK_STREAM;
2817 hints.ai_flags = AI_CANONNAME;
2819 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2825 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2827 struct hostent he1,*hp1, he2, *hp2;
2828 int buffer_size1, buffer_size2;
2829 char *buffer1, *buffer2;
2831 gboolean return_value;
2834 MONO_ARCH_SAVE_REGS;
2836 hostname=mono_string_to_utf8 (host);
2837 if (*hostname == '\0')
2838 add_local_ips = TRUE;
2840 #ifdef HAVE_SIOCGIFCONF
2841 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2842 if (!strcmp (hostname, this_hostname))
2843 add_local_ips = TRUE;
2849 buffer1 = g_malloc0(buffer_size1);
2850 buffer2 = g_malloc0(buffer_size2);
2854 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2855 &hp1, &herr) == ERANGE) {
2857 buffer1 = g_realloc(buffer1, buffer_size1);
2860 if (*hostname && hp1 == NULL)
2862 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2863 buffer_size2, &hp2, &herr) == ERANGE) {
2865 buffer2 = g_realloc(buffer2, buffer_size2);
2869 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2870 h_addr_list, add_local_ips);
2876 return(return_value);
2877 #endif /* HAVE_GETHOSTBYNAME2_R */
2879 #else /* AF_INET6 */
2880 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2884 gboolean add_local_ips = FALSE;
2885 #ifdef HAVE_SIOCGIFCONF
2886 gchar this_hostname [256];
2889 MONO_ARCH_SAVE_REGS;
2891 hostname=mono_string_to_utf8(host);
2892 if (*hostname == '\0')
2893 add_local_ips = TRUE;
2894 #ifdef HAVE_SIOCGIFCONF
2895 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2896 if (!strcmp (hostname, this_hostname))
2897 add_local_ips = TRUE;
2904 he = _wapi_gethostbyname (hostname);
2906 he = _wapi_gethostbyname (hostname);
2910 if (*hostname && he==NULL)
2913 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2915 #endif /* AF_INET6 */
2917 #ifndef HAVE_INET_PTON
2919 inet_pton (int family, const char *address, void *inaddrp)
2921 if (family == AF_INET) {
2922 #ifdef HAVE_INET_ATON
2923 struct in_addr inaddr;
2925 if (!inet_aton (address, &inaddr))
2928 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2931 /* assume the system has inet_addr(), if it doesn't
2932 have that we're pretty much screwed... */
2935 if (!strcmp (address, "255.255.255.255")) {
2936 /* special-case hack */
2937 inaddr = 0xffffffff;
2939 inaddr = inet_addr (address);
2941 #define INADDR_NONE ((in_addr_t) -1)
2943 if (inaddr == INADDR_NONE)
2947 memcpy (inaddrp, &inaddr, sizeof (guint32));
2949 #endif /* HAVE_INET_ATON */
2954 #endif /* !HAVE_INET_PTON */
2956 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2961 struct sockaddr_in saddr;
2962 struct sockaddr_in6 saddr6;
2963 struct addrinfo *info = NULL, hints;
2965 char hostname[1024] = {0};
2968 struct in_addr inaddr;
2973 address = mono_string_to_utf8 (addr);
2976 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2977 /* Maybe an ipv6 address */
2978 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2984 saddr6.sin6_family = AF_INET6;
2989 saddr.sin_family = AF_INET;
2993 if(family == AF_INET) {
2994 #if HAVE_SOCKADDR_IN_SIN_LEN
2995 saddr.sin_len = sizeof (saddr);
2997 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2998 hostname, sizeof(hostname), NULL, 0,
3002 } else if(family == AF_INET6) {
3003 #if HAVE_SOCKADDR_IN6_SIN_LEN
3004 saddr6.sin6_len = sizeof (saddr6);
3006 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
3007 hostname, sizeof(hostname), NULL, 0,
3013 memset (&hints, 0, sizeof(hints));
3014 hints.ai_family = get_family_hint ();
3015 hints.ai_socktype = SOCK_STREAM;
3016 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
3018 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
3022 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
3024 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
3029 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
3030 ret = ipaddr_to_IPHostEntry (address, h_name, h_aliases, h_addr_list);
3032 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
3033 h_addr_list, FALSE);
3041 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
3043 gchar hostname[256];
3046 MONO_ARCH_SAVE_REGS;
3048 ret = gethostname (hostname, sizeof (hostname));
3053 *h_name=mono_string_new(mono_domain_get (), hostname);
3059 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
3063 TRANSMIT_FILE_BUFFERS buffers;
3065 MONO_ARCH_SAVE_REGS;
3067 if (filename == NULL)
3070 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
3071 if (file == INVALID_HANDLE_VALUE) {
3072 SetLastError (error);
3076 memset (&buffers, 0, sizeof (buffers));
3077 if (pre_buffer != NULL) {
3078 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
3079 buffers.HeadLength = mono_array_length (pre_buffer);
3081 if (post_buffer != NULL) {
3082 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
3083 buffers.TailLength = mono_array_length (post_buffer);
3086 if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
3095 void mono_network_init(void)
3100 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3102 g_error("%s: Couldn't initialise networking", __func__);
3106 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3107 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3110 void mono_network_cleanup(void)
3116 #endif /* #ifndef DISABLE_SOCKETS */