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;
1025 get_sockaddr_size (int family)
1030 if (family == AF_INET) {
1031 size = sizeof (struct sockaddr_in);
1033 } else if (family == AF_INET6) {
1034 size = sizeof (struct sockaddr_in6);
1036 #ifdef HAVE_SYS_UN_H
1037 } else if (family == AF_UNIX) {
1038 size = sizeof (struct sockaddr_un);
1044 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1051 MONO_ARCH_SAVE_REGS;
1055 salen = get_sockaddr_size (convert_family (af));
1057 *error = WSAEAFNOSUPPORT;
1060 sa = g_malloc0 (salen);
1061 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1063 if(ret==SOCKET_ERROR) {
1064 *error = WSAGetLastError ();
1069 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)));
1071 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1076 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1083 MONO_ARCH_SAVE_REGS;
1087 salen = get_sockaddr_size (convert_family (af));
1089 *error = WSAEAFNOSUPPORT;
1092 sa = g_malloc0 (salen);
1093 /* Note: linux returns just 2 for AF_UNIX. Always. */
1094 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1095 if(ret==SOCKET_ERROR) {
1096 *error = WSAGetLastError ();
1101 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)));
1103 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1108 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1112 MonoClassField *field;
1117 /* Dig the SocketAddress data buffer out of the object */
1118 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1119 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1121 /* The data buffer is laid out as follows:
1122 * byte 0 is the address family low byte
1123 * byte 1 is the address family high byte
1125 * bytes 2 and 3 are the port info
1126 * the rest is the address info
1128 * the rest is the file name
1130 len = mono_array_length (data);
1132 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1135 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1136 if (family == AF_INET) {
1137 struct sockaddr_in *sa;
1142 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1145 sa = g_new0 (struct sockaddr_in, 1);
1146 port = (mono_array_get (data, guint8, 2) << 8) +
1147 mono_array_get (data, guint8, 3);
1148 address = (mono_array_get (data, guint8, 4) << 24) +
1149 (mono_array_get (data, guint8, 5) << 16 ) +
1150 (mono_array_get (data, guint8, 6) << 8) +
1151 mono_array_get (data, guint8, 7);
1153 sa->sin_family = family;
1154 sa->sin_addr.s_addr = htonl (address);
1155 sa->sin_port = htons (port);
1157 *sa_size = sizeof(struct sockaddr_in);
1158 return((struct sockaddr *)sa);
1161 } else if (family == AF_INET6) {
1162 struct sockaddr_in6 *sa;
1168 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1171 sa = g_new0 (struct sockaddr_in6, 1);
1172 port = mono_array_get (data, guint8, 3) +
1173 (mono_array_get (data, guint8, 2) << 8);
1174 scopeid = mono_array_get (data, guint8, 24) +
1175 (mono_array_get (data, guint8, 25) << 8) +
1176 (mono_array_get (data, guint8, 26) << 16) +
1177 (mono_array_get (data, guint8, 27) << 24);
1179 sa->sin6_family = family;
1180 sa->sin6_port = htons (port);
1181 sa->sin6_scope_id = scopeid;
1183 for(i=0; i<16; i++) {
1184 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1187 *sa_size = sizeof(struct sockaddr_in6);
1188 return((struct sockaddr *)sa);
1190 #ifdef HAVE_SYS_UN_H
1191 } else if (family == AF_UNIX) {
1192 struct sockaddr_un *sock_un;
1195 /* Need a byte for the '\0' terminator/prefix, and the first
1196 * two bytes hold the SocketAddress family
1198 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1199 mono_raise_exception (mono_get_exception_index_out_of_range ());
1202 sock_un = g_new0 (struct sockaddr_un, 1);
1204 sock_un->sun_family = family;
1205 for (i = 0; i < len - 2; i++) {
1206 sock_un->sun_path [i] = mono_array_get (data, guint8,
1212 return (struct sockaddr *)sock_un;
1215 *error = WSAEAFNOSUPPORT;
1220 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1222 struct sockaddr *sa;
1226 MONO_ARCH_SAVE_REGS;
1230 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1235 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)));
1237 ret = _wapi_bind (sock, sa, sa_size);
1238 if(ret==SOCKET_ERROR) {
1239 *error = WSAGetLastError ();
1252 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1253 gint timeout, gint32 *error)
1255 MonoInternalThread *thread = NULL;
1261 MONO_ARCH_SAVE_REGS;
1263 pfds = g_new0 (mono_pollfd, 1);
1264 pfds[0].fd = GPOINTER_TO_INT (sock);
1265 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1266 (mode == SelectModeWrite) ? MONO_POLLOUT :
1267 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1269 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1270 start = time (NULL);
1274 ret = mono_poll (pfds, 1, timeout);
1275 if (timeout > 0 && ret < 0) {
1277 int sec = time (NULL) - start;
1279 timeout -= sec * 1000;
1287 if (ret == -1 && errno == EINTR) {
1290 if (thread == NULL) {
1291 thread = mono_thread_internal_current ();
1294 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1300 /* Suspend requested? */
1301 mono_thread_interruption_checkpoint ();
1305 } while (ret == -1 && errno == EINTR);
1309 *error = WSAGetLastError ();
1311 *error = errno_to_WSA (errno, __func__);
1326 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1328 struct sockaddr *sa;
1332 MONO_ARCH_SAVE_REGS;
1336 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1341 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)));
1343 ret = _wapi_connect (sock, sa, sa_size);
1344 if(ret==SOCKET_ERROR) {
1345 *error = WSAGetLastError ();
1351 /* These #defines from mswsock.h from wine. Defining them here allows
1352 * us to build this file on a mingw box that doesn't know the magic
1353 * numbers, but still run on a newer windows box that does.
1355 #ifndef WSAID_DISCONNECTEX
1356 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1357 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1360 #ifndef WSAID_TRANSMITFILE
1361 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1362 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1365 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1368 glong output_bytes = 0;
1369 GUID disco_guid = WSAID_DISCONNECTEX;
1370 GUID trans_guid = WSAID_TRANSMITFILE;
1371 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1372 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1375 MONO_ARCH_SAVE_REGS;
1379 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1381 /* I _think_ the extension function pointers need to be looked
1382 * up for each socket. FIXME: check the best way to store
1383 * pointers to functions in managed objects that still works
1384 * on 64bit platforms.
1386 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1387 (void *)&disco_guid, sizeof(GUID),
1388 (void *)&_wapi_disconnectex, sizeof(void *),
1389 &output_bytes, NULL, NULL);
1391 /* make sure that WSAIoctl didn't put crap in the
1394 _wapi_disconnectex = NULL;
1396 /* Look up the TransmitFile extension function pointer
1397 * instead of calling TransmitFile() directly, because
1398 * apparently "Several of the extension functions have
1399 * been available since WinSock 1.1 and are exported
1400 * from MSWsock.dll, however it's not advisable to
1401 * link directly to this dll as this ties you to the
1402 * Microsoft WinSock provider. A provider neutral way
1403 * of accessing these extension functions is to load
1404 * them dynamically via WSAIoctl using the
1405 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1406 * should, theoretically, allow you to access these
1407 * functions from any provider that supports them..."
1408 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1410 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1411 (void *)&trans_guid, sizeof(GUID),
1412 (void *)&_wapi_transmitfile, sizeof(void *),
1413 &output_bytes, NULL, NULL);
1415 _wapi_transmitfile = NULL;
1419 if (_wapi_disconnectex != NULL) {
1420 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1421 } else if (_wapi_transmitfile != NULL) {
1422 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1423 TF_DISCONNECT | TF_REUSE_SOCKET);
1425 *error = ERROR_NOT_SUPPORTED;
1429 if (bret == FALSE) {
1430 *error = WSAGetLastError ();
1434 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1441 MONO_ARCH_SAVE_REGS;
1445 alen = mono_array_length (buffer);
1446 if (offset > alen - count) {
1450 buf=mono_array_addr(buffer, guchar, offset);
1452 recvflags = convert_socketflags (flags);
1453 if (recvflags == -1) {
1454 *error = WSAEOPNOTSUPP;
1460 MonoInternalThread* curthread = mono_thread_internal_current ();
1461 curthread->interrupt_on_stop = (gpointer)TRUE;
1462 ret = _wapi_recv (sock, buf, count, recvflags);
1463 curthread->interrupt_on_stop = (gpointer)FALSE;
1466 ret = _wapi_recv (sock, buf, count, recvflags);
1469 if(ret==SOCKET_ERROR) {
1470 *error = WSAGetLastError ();
1477 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1482 DWORD recvflags = 0;
1484 MONO_ARCH_SAVE_REGS;
1488 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1489 count = mono_array_length (buffers);
1491 recvflags = convert_socketflags (flags);
1492 if (recvflags == -1) {
1493 *error = WSAEOPNOTSUPP;
1497 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1498 if (ret == SOCKET_ERROR) {
1499 *error = WSAGetLastError ();
1506 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1512 struct sockaddr *sa;
1515 MONO_ARCH_SAVE_REGS;
1519 alen = mono_array_length (buffer);
1520 if (offset > alen - count) {
1524 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1529 buf=mono_array_addr(buffer, guchar, offset);
1531 recvflags = convert_socketflags (flags);
1532 if (recvflags == -1) {
1533 *error = WSAEOPNOTSUPP;
1537 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1538 if(ret==SOCKET_ERROR) {
1540 *error = WSAGetLastError ();
1544 /* If we didn't get a socket size, then we're probably a
1545 * connected connection-oriented socket and the stack hasn't
1546 * returned the remote address. All we can do is return null.
1549 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1558 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1565 MONO_ARCH_SAVE_REGS;
1569 alen = mono_array_length (buffer);
1570 if (offset > alen - count) {
1574 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1576 buf=mono_array_addr(buffer, guchar, offset);
1578 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1580 sendflags = convert_socketflags (flags);
1581 if (sendflags == -1) {
1582 *error = WSAEOPNOTSUPP;
1586 ret = _wapi_send (sock, buf, count, sendflags);
1587 if(ret==SOCKET_ERROR) {
1588 *error = WSAGetLastError ();
1595 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1600 DWORD sendflags = 0;
1602 MONO_ARCH_SAVE_REGS;
1606 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1607 count = mono_array_length (buffers);
1609 sendflags = convert_socketflags (flags);
1610 if (sendflags == -1) {
1611 *error = WSAEOPNOTSUPP;
1615 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1616 if (ret == SOCKET_ERROR) {
1617 *error = WSAGetLastError ();
1624 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1630 struct sockaddr *sa;
1633 MONO_ARCH_SAVE_REGS;
1637 alen = mono_array_length (buffer);
1638 if (offset > alen - count) {
1642 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1647 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1649 buf=mono_array_addr(buffer, guchar, offset);
1651 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1653 sendflags = convert_socketflags (flags);
1654 if (sendflags == -1) {
1655 *error = WSAEOPNOTSUPP;
1659 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1660 if(ret==SOCKET_ERROR) {
1661 *error = WSAGetLastError ();
1669 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1672 MonoClassField *field;
1674 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1675 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1680 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1681 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1683 MonoInternalThread *thread = NULL;
1690 MonoClass *sock_arr_class;
1693 uintptr_t socks_size;
1695 MONO_ARCH_SAVE_REGS;
1697 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1698 count = mono_array_length (*sockets);
1699 nfds = count - 3; /* NULL separators */
1700 pfds = g_new0 (mono_pollfd, nfds);
1702 for (i = 0; i < count; i++) {
1703 obj = mono_array_get (*sockets, MonoObject *, i);
1710 /* The socket array was bogus */
1716 pfds [idx].fd = Socket_to_SOCKET (obj);
1717 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1721 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1722 start = time (NULL);
1725 ret = mono_poll (pfds, nfds, timeout);
1726 if (timeout > 0 && ret < 0) {
1728 int sec = time (NULL) - start;
1730 timeout -= sec * 1000;
1736 if (ret == -1 && errno == EINTR) {
1739 thread = mono_thread_internal_current ();
1741 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1748 /* Suspend requested? */
1749 mono_thread_interruption_checkpoint ();
1753 } while (ret == -1 && errno == EINTR);
1757 *error = WSAGetLastError ();
1759 *error = errno_to_WSA (errno, __func__);
1771 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1772 socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
1773 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1776 for (i = 0; i < count && ret > 0; i++) {
1779 obj = mono_array_get (*sockets, MonoObject *, i);
1786 pfd = &pfds [i - mode];
1787 if (pfd->revents == 0)
1791 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1792 mono_array_setref (socks, idx++, obj);
1793 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1794 mono_array_setref (socks, idx++, obj);
1795 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1796 mono_array_setref (socks, idx++, obj);
1804 static MonoObject* int_to_object (MonoDomain *domain, int val)
1806 return mono_value_box (domain, mono_get_int32_class (), &val);
1810 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1816 socklen_t valsize=sizeof(val);
1817 struct linger linger;
1818 socklen_t lingersize=sizeof(linger);
1820 socklen_t time_ms_size = sizeof (time_ms);
1822 # if defined(__OpenBSD__)
1823 struct sockpeercred cred;
1827 socklen_t credsize = sizeof(cred);
1829 MonoDomain *domain=mono_domain_get();
1831 MonoClass *obj_class;
1832 MonoClassField *field;
1834 MONO_ARCH_SAVE_REGS;
1838 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1839 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
1840 system_level = SOL_SOCKET;
1841 system_name = SO_REUSEADDR;
1847 ret = convert_sockopt_level_and_name (level, name, &system_level, &system_name);
1851 *error = WSAENOPROTOOPT;
1855 *obj_val = int_to_object (domain, 0);
1859 /* No need to deal with MulticastOption names here, because
1860 * you cant getsockopt AddMembership or DropMembership (the
1861 * int getsockopt will error, causing an exception)
1864 case SocketOptionName_Linger:
1865 case SocketOptionName_DontLinger:
1866 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1870 case SocketOptionName_SendTimeout:
1871 case SocketOptionName_ReceiveTimeout:
1872 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1876 case SocketOptionName_PeerCred:
1877 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1883 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1887 if(ret==SOCKET_ERROR) {
1888 *error = WSAGetLastError ();
1893 case SocketOptionName_Linger:
1894 /* build a System.Net.Sockets.LingerOption */
1895 obj_class=mono_class_from_name(get_socket_assembly (),
1896 "System.Net.Sockets",
1898 obj=mono_object_new(domain, obj_class);
1900 /* Locate and set the fields "bool enabled" and "int
1903 field=mono_class_get_field_from_name(obj_class, "enabled");
1904 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1906 field=mono_class_get_field_from_name(obj_class, "seconds");
1907 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1911 case SocketOptionName_DontLinger:
1912 /* construct a bool int in val - true if linger is off */
1913 obj = int_to_object (domain, !linger.l_onoff);
1916 case SocketOptionName_SendTimeout:
1917 case SocketOptionName_ReceiveTimeout:
1918 obj = int_to_object (domain, time_ms);
1922 case SocketOptionName_PeerCred:
1924 /* build a Mono.Posix.PeerCred+PeerCredData if
1927 static MonoImage *mono_posix_image = NULL;
1928 MonoPeerCredData *cred_data;
1930 if (mono_posix_image == NULL) {
1931 mono_posix_image=mono_image_loaded ("Mono.Posix");
1932 if (!mono_posix_image) {
1933 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1935 *error = WSAENOPROTOOPT;
1938 mono_posix_image = mono_assembly_get_image (sa);
1943 obj_class = mono_class_from_name(mono_posix_image,
1946 obj = mono_object_new(domain, obj_class);
1947 cred_data = (MonoPeerCredData *)obj;
1948 cred_data->pid = cred.pid;
1949 cred_data->uid = cred.uid;
1950 cred_data->gid = cred.gid;
1956 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1957 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse)
1960 obj = int_to_object (domain, val);
1965 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1973 MONO_ARCH_SAVE_REGS;
1977 ret=convert_sockopt_level_and_name(level, name, &system_level,
1980 *error = WSAENOPROTOOPT;
1986 valsize=mono_array_length(*byte_val);
1987 buf=mono_array_addr(*byte_val, guchar, 0);
1989 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1990 if(ret==SOCKET_ERROR) {
1991 *error = WSAGetLastError ();
1995 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1996 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1998 struct in_addr inaddr;
1999 MonoClassField *field;
2001 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
2003 /* No idea why .net uses a 64bit type to hold a 32bit value...
2005 * Internal value of IPAddess is in little-endian order
2007 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
2013 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
2015 struct in6_addr in6addr;
2016 MonoClassField *field;
2020 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
2021 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
2023 /* Solaris has only the 8 bit version. */
2025 for(i=0; i<8; i++) {
2026 guint16 s = mono_array_get (data, guint16, i);
2027 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
2028 in6addr.s6_addr[2 * i + 1] = s & 0xff;
2032 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
2036 #endif /* AF_INET6 */
2039 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)
2041 struct linger linger;
2051 #ifdef HAVE_SOL_IPV6
2052 sol_ipv6 = SOL_IPV6;
2055 struct protoent *pent;
2056 pent = getprotobyname ("ipv6");
2057 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2065 struct protoent *pent;
2066 pent = getprotobyname ("ip");
2067 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2070 #endif /* AF_INET6 */
2072 MONO_ARCH_SAVE_REGS;
2074 ret=convert_sockopt_level_and_name(level, name, &system_level,
2077 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2078 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
2079 system_name = SO_REUSEADDR;
2080 int_val = int_val ? 0 : 1;
2086 *error = WSAENOPROTOOPT;
2093 /* Only one of obj_val, byte_val or int_val has data */
2095 MonoClassField *field;
2099 case SocketOptionName_Linger:
2100 /* Dig out "bool enabled" and "int seconds"
2103 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2104 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2105 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2106 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2108 valsize=sizeof(linger);
2109 ret = _wapi_setsockopt (sock, system_level,
2110 system_name, &linger, valsize);
2112 case SocketOptionName_AddMembership:
2113 case SocketOptionName_DropMembership:
2114 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2116 MonoObject *address = NULL;
2119 if(system_level == sol_ipv6) {
2120 struct ipv6_mreq mreq6;
2125 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2126 address = *(gpointer *)(((char *)obj_val) + field->offset);
2129 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2132 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2133 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2135 ret = _wapi_setsockopt (sock, system_level,
2136 system_name, &mreq6,
2138 } else if(system_level == sol_ip)
2139 #endif /* AF_INET6 */
2141 #ifdef HAVE_STRUCT_IP_MREQN
2142 struct ip_mreqn mreq = {{0}};
2144 struct ip_mreq mreq = {{0}};
2145 #endif /* HAVE_STRUCT_IP_MREQN */
2147 /* pain! MulticastOption holds two IPAddress
2148 * members, so I have to dig the value out of
2151 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2152 address = *(gpointer *)(((char *)obj_val) + field->offset);
2154 /* address might not be defined and if so, set the address to ADDR_ANY.
2157 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2160 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2161 address = *(gpointer *)(((char *)obj_val) + field->offset);
2163 #ifdef HAVE_STRUCT_IP_MREQN
2165 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2169 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2171 #endif /* HAVE_STRUCT_IP_MREQN */
2173 ret = _wapi_setsockopt (sock, system_level,
2179 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2181 /* Cause an exception to be thrown */
2185 } else if (byte_val!=NULL) {
2186 int valsize = mono_array_length (byte_val);
2187 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2190 case SocketOptionName_DontLinger:
2192 linger.l_onoff = (*buf) ? 0 : 1;
2193 linger.l_linger = 0;
2194 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2200 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2204 /* ReceiveTimeout/SendTimeout get here */
2206 case SocketOptionName_DontLinger:
2207 linger.l_onoff = !int_val;
2208 linger.l_linger = 0;
2209 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2211 case SocketOptionName_DontFragment:
2212 #ifdef HAVE_IP_MTU_DISCOVER
2213 /* Fiddle with the value slightly if we're
2217 int_val = IP_PMTUDISC_DO;
2223 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2227 if(ret==SOCKET_ERROR) {
2228 *error = WSAGetLastError ();
2232 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2238 MONO_ARCH_SAVE_REGS;
2242 /* Currently, the values for how (recv=0, send=1, both=2) match
2245 ret = _wapi_shutdown (sock, how);
2246 if(ret==SOCKET_ERROR) {
2247 *error = WSAGetLastError ();
2252 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2254 MonoArray *output, gint32 *error)
2256 glong output_bytes = 0;
2257 gchar *i_buffer, *o_buffer;
2261 MONO_ARCH_SAVE_REGS;
2265 if (code == FIONBIO) {
2266 /* Invalid command. Must use Socket.Blocking */
2270 if (input == NULL) {
2274 i_buffer = mono_array_addr (input, gchar, 0);
2275 i_len = mono_array_length (input);
2278 if (output == NULL) {
2282 o_buffer = mono_array_addr (output, gchar, 0);
2283 o_len = mono_array_length (output);
2286 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2287 if (ret == SOCKET_ERROR) {
2288 *error = WSAGetLastError ();
2292 return (gint) output_bytes;
2295 #ifdef HAVE_SIOCGIFCONF
2297 is_loopback (int family, void *ad)
2299 char *ptr = (char *) ad;
2301 if (family == AF_INET) {
2302 return (ptr [0] == 127);
2306 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2313 get_local_ips (int family, int *nips)
2315 int addr_size, offset, fd, i, count;
2316 int max_ifaces = 50; /* 50 interfaces should be enough... */
2319 struct ifreq iflags;
2320 char *result, *tmp_ptr;
2321 gboolean ignore_loopback = FALSE;
2324 if (family == AF_INET) {
2325 addr_size = sizeof (struct in_addr);
2326 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2328 } else if (family == AF_INET6) {
2329 addr_size = sizeof (struct in6_addr);
2330 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2336 fd = socket (family, SOCK_STREAM, 0);
2338 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2339 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2340 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2342 g_free (ifc.ifc_buf);
2346 count = ifc.ifc_len / sizeof (struct ifreq);
2349 g_free (ifc.ifc_buf);
2354 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2355 strcpy (iflags.ifr_name, ifr->ifr_name);
2356 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2360 if ((iflags.ifr_flags & IFF_UP) == 0) {
2361 ifr->ifr_name [0] = '\0';
2365 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2366 ignore_loopback = TRUE;
2371 result = g_malloc (addr_size * count);
2373 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2374 if (ifr->ifr_name [0] == '\0') {
2379 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2384 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2385 tmp_ptr += addr_size;
2388 g_free (ifc.ifc_buf);
2393 get_local_ips (int family, int *nips)
2399 #endif /* HAVE_SIOCGIFCONF */
2402 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2403 MonoArray **h_aliases,
2404 MonoArray **h_addr_list,
2405 gboolean add_local_ips)
2407 MonoDomain *domain = mono_domain_get ();
2409 struct in_addr *local_in = NULL;
2413 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2417 *h_name=mono_string_new(domain, he->h_name);
2419 while(he->h_aliases[i]!=NULL) {
2423 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2425 while(he->h_aliases[i]!=NULL) {
2428 alias=mono_string_new(domain, he->h_aliases[i]);
2429 mono_array_setref (*h_aliases, i, alias);
2432 } else if (!add_local_ips) {
2436 if (add_local_ips) {
2437 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2439 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2440 for (i = 0; i < nlocal_in; i++) {
2441 MonoString *addr_string;
2442 char addr [16], *ptr;
2444 ptr = (char *) &local_in [i];
2445 g_snprintf(addr, 16, "%u.%u.%u.%u",
2446 (unsigned char) ptr [0],
2447 (unsigned char) ptr [1],
2448 (unsigned char) ptr [2],
2449 (unsigned char) ptr [3]);
2451 addr_string = mono_string_new (domain, addr);
2452 mono_array_setref (*h_addr_list, i, addr_string);
2457 } else if (he == NULL) {
2458 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2459 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2460 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2465 if (nlocal_in == 0 && he != NULL) {
2467 while (he->h_addr_list[i]!=NULL) {
2471 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2473 while(he->h_addr_list[i]!=NULL) {
2474 MonoString *addr_string;
2477 g_snprintf(addr, 16, "%u.%u.%u.%u",
2478 (unsigned char)he->h_addr_list[i][0],
2479 (unsigned char)he->h_addr_list[i][1],
2480 (unsigned char)he->h_addr_list[i][2],
2481 (unsigned char)he->h_addr_list[i][3]);
2483 addr_string=mono_string_new(domain, addr);
2484 mono_array_setref (*h_addr_list, i, addr_string);
2492 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2493 MonoArray **h_aliases,
2494 MonoArray **h_addr_list)
2496 MonoDomain *domain = mono_domain_get ();
2498 *h_name=mono_string_new(domain, addr);
2499 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2500 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2501 mono_array_setref (*h_addr_list, 0, *h_name);
2507 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2508 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2509 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2511 MonoDomain *domain = mono_domain_get ();
2512 int i, host_count, host_index, family_hint;
2513 struct in_addr *local_in = NULL;
2515 struct in6_addr *local_in6 = NULL;
2517 gboolean from_local = FALSE;
2519 family_hint = get_family_hint ();
2522 * Check if address length and family are correct
2524 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2528 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2533 * Get the aliases and host name from he1 or he2 whichever is
2534 * not null, if he1 is not null then take aliases from he1
2536 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2537 family_hint == PF_INET)) {
2538 *h_name=mono_string_new (domain, he1->h_name);
2541 while(he1->h_aliases[i]!=NULL) {
2545 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2548 while(he1->h_aliases[i]!=NULL) {
2551 alias=mono_string_new (domain, he1->h_aliases[i]);
2552 mono_array_setref (*h_aliases, i, alias);
2555 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2556 family_hint == PF_INET6)) {
2557 *h_name=mono_string_new (domain, he2->h_name);
2560 while(he2->h_aliases [i] != NULL) {
2564 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2567 while(he2->h_aliases[i]!=NULL) {
2570 alias=mono_string_new (domain, he2->h_aliases[i]);
2571 mono_array_setref (*h_aliases, i, alias);
2574 } else if (!add_local_ips) {
2579 * Count the number of addresses in he1 + he2
2582 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2583 family_hint == PF_INET)) {
2585 while(he1->h_addr_list[i]!=NULL) {
2591 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2592 family_hint == PF_INET6)) {
2594 while(he2->h_addr_list[i]!=NULL) {
2604 if (add_local_ips) {
2605 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2606 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2608 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2609 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2611 if (nlocal_in || nlocal_in6) {
2613 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2614 nlocal_in + nlocal_in6);
2618 for (n = 0; n < nlocal_in6; n++) {
2619 MonoString *addr_string;
2621 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2623 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2626 addr_string = mono_string_new (domain, addr);
2627 mono_array_setref (*h_addr_list, host_index, addr_string);
2635 for (n = 0; n < nlocal_in; n++) {
2636 MonoString *addr_string;
2638 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2640 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2643 addr_string = mono_string_new (domain, addr);
2644 mono_array_setref (*h_addr_list, host_index, addr_string);
2652 } else if (he1 == NULL && he2 == NULL) {
2653 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2654 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2655 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2665 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2667 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2668 family_hint == PF_INET6)) {
2670 while(he2->h_addr_list[i] != NULL) {
2671 MonoString *addr_string;
2673 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2675 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2679 addr_string = mono_string_new (domain, addr);
2680 mono_array_setref (*h_addr_list, host_index, addr_string);
2687 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2688 family_hint == PF_INET)) {
2690 while(he1->h_addr_list[i] != NULL) {
2691 MonoString *addr_string;
2693 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2695 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2699 addr_string=mono_string_new (domain, addr);
2700 mono_array_setref (*h_addr_list, host_index, addr_string);
2711 #if defined(AF_INET6)
2713 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2714 MonoArray **h_aliases,
2715 MonoArray **h_addr_list,
2716 gboolean add_local_ips)
2719 struct addrinfo *ai = NULL;
2720 struct in_addr *local_in = NULL;
2722 struct in6_addr *local_in6 = NULL;
2726 MonoDomain *domain = mono_domain_get ();
2729 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2730 if (add_local_ips) {
2731 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2732 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2733 if (nlocal_in || nlocal_in6) {
2734 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2736 MonoString *addr_string;
2740 for (i = 0; i < nlocal_in; i++) {
2741 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2742 addr_string = mono_string_new (domain, addr);
2743 mono_array_setref (*h_addr_list, addr_index, addr_string);
2749 MonoString *addr_string;
2754 for (i = 0; i < nlocal_in6; i++) {
2755 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2757 addr_string = mono_string_new (domain, addr);
2758 mono_array_setref (*h_addr_list, addr_index, addr_string);
2767 freeaddrinfo (info);
2776 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2777 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2783 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2785 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2786 MonoString *addr_string;
2788 char buffer [48]; /* Max. size for IPv6 */
2790 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2794 if(ai->ai_family == PF_INET) {
2795 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2797 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2801 addr_string=mono_string_new(domain, buffer);
2803 addr_string=mono_string_new(domain, "");
2806 mono_array_setref (*h_addr_list, addr_index, addr_string);
2809 if (ai->ai_canonname != NULL) {
2810 *h_name=mono_string_new(domain, ai->ai_canonname);
2812 *h_name=mono_string_new(domain, buffer);
2828 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2830 gboolean add_local_ips = FALSE;
2831 #ifdef HAVE_SIOCGIFCONF
2832 gchar this_hostname [256];
2834 #if !defined(HAVE_GETHOSTBYNAME2_R)
2835 struct addrinfo *info = NULL, hints;
2838 MONO_ARCH_SAVE_REGS;
2840 hostname=mono_string_to_utf8 (host);
2841 if (*hostname == '\0')
2842 add_local_ips = TRUE;
2843 #ifdef HAVE_SIOCGIFCONF
2844 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2845 if (!strcmp (hostname, this_hostname))
2846 add_local_ips = TRUE;
2850 memset(&hints, 0, sizeof(hints));
2851 hints.ai_family = get_family_hint ();
2852 hints.ai_socktype = SOCK_STREAM;
2853 hints.ai_flags = AI_CANONNAME;
2855 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2861 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2863 struct hostent he1,*hp1, he2, *hp2;
2864 int buffer_size1, buffer_size2;
2865 char *buffer1, *buffer2;
2867 gboolean return_value;
2870 MONO_ARCH_SAVE_REGS;
2872 hostname=mono_string_to_utf8 (host);
2873 if (*hostname == '\0')
2874 add_local_ips = TRUE;
2876 #ifdef HAVE_SIOCGIFCONF
2877 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2878 if (!strcmp (hostname, this_hostname))
2879 add_local_ips = TRUE;
2885 buffer1 = g_malloc0(buffer_size1);
2886 buffer2 = g_malloc0(buffer_size2);
2890 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2891 &hp1, &herr) == ERANGE) {
2893 buffer1 = g_realloc(buffer1, buffer_size1);
2896 if (*hostname && hp1 == NULL)
2898 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2899 buffer_size2, &hp2, &herr) == ERANGE) {
2901 buffer2 = g_realloc(buffer2, buffer_size2);
2905 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2906 h_addr_list, add_local_ips);
2912 return(return_value);
2913 #endif /* HAVE_GETHOSTBYNAME2_R */
2915 #else /* AF_INET6 */
2916 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2920 gboolean add_local_ips = FALSE;
2921 #ifdef HAVE_SIOCGIFCONF
2922 gchar this_hostname [256];
2925 MONO_ARCH_SAVE_REGS;
2927 hostname=mono_string_to_utf8(host);
2928 if (*hostname == '\0')
2929 add_local_ips = TRUE;
2930 #ifdef HAVE_SIOCGIFCONF
2931 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2932 if (!strcmp (hostname, this_hostname))
2933 add_local_ips = TRUE;
2940 he = _wapi_gethostbyname (hostname);
2942 he = _wapi_gethostbyname (hostname);
2946 if (*hostname && he==NULL)
2949 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2951 #endif /* AF_INET6 */
2953 #ifndef HAVE_INET_PTON
2955 inet_pton (int family, const char *address, void *inaddrp)
2957 if (family == AF_INET) {
2958 #ifdef HAVE_INET_ATON
2959 struct in_addr inaddr;
2961 if (!inet_aton (address, &inaddr))
2964 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2967 /* assume the system has inet_addr(), if it doesn't
2968 have that we're pretty much screwed... */
2971 if (!strcmp (address, "255.255.255.255")) {
2972 /* special-case hack */
2973 inaddr = 0xffffffff;
2975 inaddr = inet_addr (address);
2977 #define INADDR_NONE ((in_addr_t) -1)
2979 if (inaddr == INADDR_NONE)
2983 memcpy (inaddrp, &inaddr, sizeof (guint32));
2985 #endif /* HAVE_INET_ATON */
2990 #endif /* !HAVE_INET_PTON */
2992 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2997 struct sockaddr_in saddr;
2998 struct sockaddr_in6 saddr6;
2999 struct addrinfo *info = NULL, hints;
3001 char hostname[1024] = {0};
3004 struct in_addr inaddr;
3009 address = mono_string_to_utf8 (addr);
3012 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
3013 /* Maybe an ipv6 address */
3014 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
3020 saddr6.sin6_family = AF_INET6;
3025 saddr.sin_family = AF_INET;
3029 if(family == AF_INET) {
3030 #if HAVE_SOCKADDR_IN_SIN_LEN
3031 saddr.sin_len = sizeof (saddr);
3033 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
3034 hostname, sizeof(hostname), NULL, 0,
3038 } else if(family == AF_INET6) {
3039 #if HAVE_SOCKADDR_IN6_SIN_LEN
3040 saddr6.sin6_len = sizeof (saddr6);
3042 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
3043 hostname, sizeof(hostname), NULL, 0,
3049 memset (&hints, 0, sizeof(hints));
3050 hints.ai_family = get_family_hint ();
3051 hints.ai_socktype = SOCK_STREAM;
3052 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
3054 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
3058 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
3060 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
3065 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
3066 ret = ipaddr_to_IPHostEntry (address, h_name, h_aliases, h_addr_list);
3068 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
3069 h_addr_list, FALSE);
3077 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
3079 gchar hostname[256];
3082 MONO_ARCH_SAVE_REGS;
3084 ret = gethostname (hostname, sizeof (hostname));
3089 *h_name=mono_string_new(mono_domain_get (), hostname);
3095 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
3099 TRANSMIT_FILE_BUFFERS buffers;
3101 MONO_ARCH_SAVE_REGS;
3103 if (filename == NULL)
3106 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
3107 if (file == INVALID_HANDLE_VALUE) {
3108 SetLastError (error);
3112 memset (&buffers, 0, sizeof (buffers));
3113 if (pre_buffer != NULL) {
3114 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
3115 buffers.HeadLength = mono_array_length (pre_buffer);
3117 if (post_buffer != NULL) {
3118 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
3119 buffers.TailLength = mono_array_length (post_buffer);
3122 if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
3131 void mono_network_init(void)
3136 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3138 g_error("%s: Couldn't initialise networking", __func__);
3142 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3143 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3146 void mono_network_cleanup(void)
3152 #endif /* #ifndef DISABLE_SOCKETS */