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
17 #define __APPLE_USE_RFC_3542
28 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
35 #include <arpa/inet.h>
38 #include <mono/metadata/object.h>
39 #include <mono/io-layer/io-layer.h>
40 #include <mono/metadata/socket-io.h>
41 #include <mono/metadata/exception.h>
42 #include <mono/metadata/assembly.h>
43 #include <mono/metadata/appdomain.h>
44 #include <mono/metadata/file-io.h>
45 #include <mono/metadata/threads.h>
46 #include <mono/metadata/threads-types.h>
47 #include <mono/utils/mono-poll.h>
48 /* FIXME change this code to not mess so much with the internals */
49 #include <mono/metadata/class-internals.h>
50 #include <mono/metadata/threadpool-internals.h>
51 #include <mono/metadata/domain-internals.h>
53 #ifdef HAVE_SYS_TIME_H
56 #ifdef HAVE_SYS_IOCTL_H
57 #include <sys/ioctl.h>
66 #ifdef HAVE_SYS_FILIO_H
67 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
69 #ifdef HAVE_SYS_SOCKIO_H
70 #include <sys/sockio.h> /* defines SIOCATMARK */
76 #include "mono/io-layer/socket-wrappers.h"
78 #if defined(HOST_WIN32)
79 /* This is a kludge to make this file build under cygwin:
80 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
81 * prototypes for some but not all required functions (notably
82 * inet_ntop() is missing), but the libws2_32 library is missing the
83 * actual implementations of these functions.
88 #ifdef PLATFORM_ANDROID
89 // not yet actually implemented...
94 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
97 * Some older versions of libc provide IPV6 support without defining the AI_ADDRCONFIG
98 * flag for getaddrinfo.
100 #ifndef AI_ADDRCONFIG
101 #define AI_ADDRCONFIG 0
104 static gint32 convert_family(MonoAddressFamily mono_family)
108 switch(mono_family) {
109 case AddressFamily_Unknown:
110 case AddressFamily_ImpLink:
111 case AddressFamily_Pup:
112 case AddressFamily_Chaos:
113 case AddressFamily_Iso:
114 case AddressFamily_Ecma:
115 case AddressFamily_DataKit:
116 case AddressFamily_Ccitt:
117 case AddressFamily_DataLink:
118 case AddressFamily_Lat:
119 case AddressFamily_HyperChannel:
120 case AddressFamily_NetBios:
121 case AddressFamily_VoiceView:
122 case AddressFamily_FireFox:
123 case AddressFamily_Banyan:
124 case AddressFamily_Atm:
125 case AddressFamily_Cluster:
126 case AddressFamily_Ieee12844:
127 case AddressFamily_NetworkDesigners:
128 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
131 case AddressFamily_Unspecified:
135 case AddressFamily_Unix:
139 case AddressFamily_InterNetwork:
143 case AddressFamily_Ipx:
149 case AddressFamily_Sna:
155 case AddressFamily_DecNet:
161 case AddressFamily_AppleTalk:
165 case AddressFamily_InterNetworkV6:
170 case AddressFamily_Irda:
176 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
182 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
184 MonoAddressFamily family=AddressFamily_Unknown;
188 family=AddressFamily_Unspecified;
192 family=AddressFamily_Unix;
196 family=AddressFamily_InterNetwork;
201 family=AddressFamily_Ipx;
207 family=AddressFamily_Sna;
213 family=AddressFamily_DecNet;
218 family=AddressFamily_AppleTalk;
223 family=AddressFamily_InterNetworkV6;
229 family=AddressFamily_Irda;
233 g_warning("unknown address family 0x%x", af_family);
239 static gint32 convert_type(MonoSocketType mono_type)
244 case SocketType_Stream:
248 case SocketType_Dgram:
262 case SocketType_Seqpacket:
266 case SocketType_Unknown:
267 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
271 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
277 static gint32 convert_proto(MonoProtocolType mono_proto)
282 case ProtocolType_IP:
283 case ProtocolType_IPv6:
284 case ProtocolType_Icmp:
285 case ProtocolType_Igmp:
286 case ProtocolType_Ggp:
287 case ProtocolType_Tcp:
288 case ProtocolType_Pup:
289 case ProtocolType_Udp:
290 case ProtocolType_Idp:
291 /* These protocols are known (on my system at least) */
295 case ProtocolType_ND:
296 case ProtocolType_Raw:
297 case ProtocolType_Ipx:
298 case ProtocolType_Spx:
299 case ProtocolType_SpxII:
300 case ProtocolType_Unknown:
301 /* These protocols arent */
302 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
312 /* Convert MonoSocketFlags */
313 static gint32 convert_socketflags (gint32 sflags)
318 /* SocketFlags.None */
321 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
322 SocketFlags_DontRoute | SocketFlags_Partial))
323 /* Contains invalid flag values */
326 if (sflags & SocketFlags_OutOfBand)
328 if (sflags & SocketFlags_Peek)
330 if (sflags & SocketFlags_DontRoute)
331 flags |= MSG_DONTROUTE;
333 /* Ignore Partial - see bug 349688. Don't return -1, because
334 * according to the comment in that bug ms runtime doesn't for
335 * UDP sockets (this means we will silently ignore it for TCP
339 if (sflags & SocketFlags_Partial)
343 /* Don't do anything for MaxIOVectorLength */
344 if (sflags & SocketFlags_MaxIOVectorLength)
352 * 0 on success (mapped mono_level and mono_name to system_level and system_name
354 * -2 on non-fatal error (ie, must ignore)
356 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
357 MonoSocketOptionName mono_name,
361 switch (mono_level) {
362 case SocketOptionLevel_Socket:
363 *system_level = SOL_SOCKET;
366 case SocketOptionName_DontLinger:
367 /* This is SO_LINGER, because the setsockopt
368 * internal call maps DontLinger to SO_LINGER
371 *system_name = SO_LINGER;
373 case SocketOptionName_Debug:
374 *system_name = SO_DEBUG;
377 case SocketOptionName_AcceptConnection:
378 *system_name = SO_ACCEPTCONN;
381 case SocketOptionName_ReuseAddress:
382 *system_name = SO_REUSEADDR;
384 case SocketOptionName_KeepAlive:
385 *system_name = SO_KEEPALIVE;
387 case SocketOptionName_DontRoute:
388 *system_name = SO_DONTROUTE;
390 case SocketOptionName_Broadcast:
391 *system_name = SO_BROADCAST;
393 case SocketOptionName_Linger:
394 *system_name = SO_LINGER;
396 case SocketOptionName_OutOfBandInline:
397 *system_name = SO_OOBINLINE;
399 case SocketOptionName_SendBuffer:
400 *system_name = SO_SNDBUF;
402 case SocketOptionName_ReceiveBuffer:
403 *system_name = SO_RCVBUF;
405 case SocketOptionName_SendLowWater:
406 *system_name = SO_SNDLOWAT;
408 case SocketOptionName_ReceiveLowWater:
409 *system_name = SO_RCVLOWAT;
411 case SocketOptionName_SendTimeout:
412 *system_name = SO_SNDTIMEO;
414 case SocketOptionName_ReceiveTimeout:
415 *system_name = SO_RCVTIMEO;
417 case SocketOptionName_Error:
418 *system_name = SO_ERROR;
420 case SocketOptionName_Type:
421 *system_name = SO_TYPE;
424 case SocketOptionName_PeerCred:
425 *system_name = SO_PEERCRED;
428 case SocketOptionName_ExclusiveAddressUse:
429 #ifdef SO_EXCLUSIVEADDRUSE
430 *system_name = SO_EXCLUSIVEADDRUSE;
433 case SocketOptionName_UseLoopback:
434 #ifdef SO_USELOOPBACK
435 *system_name = SO_USELOOPBACK;
438 case SocketOptionName_MaxConnections:
440 *system_name = SO_MAXCONN;
442 #elif defined(SOMAXCONN)
443 *system_name = SOMAXCONN;
447 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
452 case SocketOptionLevel_IP:
454 *system_level = SOL_IP;
457 static int cached = 0;
461 struct protoent *pent;
463 pent = getprotobyname ("IP");
464 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
468 *system_level = proto;
470 #endif /* HAVE_SOL_IP */
473 case SocketOptionName_IPOptions:
474 *system_name = IP_OPTIONS;
477 case SocketOptionName_HeaderIncluded:
478 *system_name = IP_HDRINCL;
482 case SocketOptionName_TypeOfService:
483 *system_name = IP_TOS;
487 case SocketOptionName_IpTimeToLive:
488 *system_name = IP_TTL;
491 case SocketOptionName_MulticastInterface:
492 *system_name = IP_MULTICAST_IF;
494 case SocketOptionName_MulticastTimeToLive:
495 *system_name = IP_MULTICAST_TTL;
497 case SocketOptionName_MulticastLoopback:
498 *system_name = IP_MULTICAST_LOOP;
500 case SocketOptionName_AddMembership:
501 *system_name = IP_ADD_MEMBERSHIP;
503 case SocketOptionName_DropMembership:
504 *system_name = IP_DROP_MEMBERSHIP;
506 #ifdef HAVE_IP_PKTINFO
507 case SocketOptionName_PacketInformation:
508 *system_name = IP_PKTINFO;
510 #endif /* HAVE_IP_PKTINFO */
512 case SocketOptionName_DontFragment:
513 #ifdef HAVE_IP_DONTFRAGMENT
514 *system_name = IP_DONTFRAGMENT;
516 #elif defined HAVE_IP_MTU_DISCOVER
517 /* Not quite the same */
518 *system_name = IP_MTU_DISCOVER;
521 /* If the flag is not available on this system, we can ignore this error */
523 #endif /* HAVE_IP_DONTFRAGMENT */
524 case SocketOptionName_AddSourceMembership:
525 case SocketOptionName_DropSourceMembership:
526 case SocketOptionName_BlockSource:
527 case SocketOptionName_UnblockSource:
528 /* Can't figure out how to map these, so fall
532 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
538 case SocketOptionLevel_IPv6:
540 *system_level = SOL_IPV6;
543 static int cached = 0;
547 struct protoent *pent;
549 pent = getprotobyname ("IPV6");
550 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
554 *system_level = proto;
556 #endif /* HAVE_SOL_IPV6 */
559 case SocketOptionName_IpTimeToLive:
560 case SocketOptionName_HopLimit:
561 *system_name = IPV6_UNICAST_HOPS;
563 case SocketOptionName_MulticastInterface:
564 *system_name = IPV6_MULTICAST_IF;
566 case SocketOptionName_MulticastTimeToLive:
567 *system_name = IPV6_MULTICAST_HOPS;
569 case SocketOptionName_MulticastLoopback:
570 *system_name = IPV6_MULTICAST_LOOP;
572 case SocketOptionName_AddMembership:
573 *system_name = IPV6_JOIN_GROUP;
575 case SocketOptionName_DropMembership:
576 *system_name = IPV6_LEAVE_GROUP;
578 case SocketOptionName_PacketInformation:
579 #ifdef HAVE_IPV6_PKTINFO
580 *system_name = IPV6_PKTINFO;
583 case SocketOptionName_HeaderIncluded:
584 case SocketOptionName_IPOptions:
585 case SocketOptionName_TypeOfService:
586 case SocketOptionName_DontFragment:
587 case SocketOptionName_AddSourceMembership:
588 case SocketOptionName_DropSourceMembership:
589 case SocketOptionName_BlockSource:
590 case SocketOptionName_UnblockSource:
591 /* Can't figure out how to map these, so fall
595 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
599 break; /* SocketOptionLevel_IPv6 */
602 case SocketOptionLevel_Tcp:
604 *system_level = SOL_TCP;
607 static int cached = 0;
611 struct protoent *pent;
613 pent = getprotobyname ("TCP");
614 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
618 *system_level = proto;
620 #endif /* HAVE_SOL_TCP */
623 case SocketOptionName_NoDelay:
624 *system_name = TCP_NODELAY;
627 /* The documentation is talking complete
628 * bollocks here: rfc-1222 is titled
629 * 'Advancing the NSFNET Routing Architecture'
630 * and doesn't mention either of the words
631 * "expedite" or "urgent".
633 case SocketOptionName_BsdUrgent:
634 case SocketOptionName_Expedited:
637 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
642 case SocketOptionLevel_Udp:
643 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
646 case SocketOptionName_NoChecksum:
647 case SocketOptionName_ChecksumCoverage:
649 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
656 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
663 static MonoImage *get_socket_assembly (void)
665 static const char *version = NULL;
666 static gboolean moonlight;
667 MonoDomain *domain = mono_domain_get ();
669 if (version == NULL) {
670 version = mono_get_runtime_info ()->framework_version;
671 moonlight = !strcmp (version, "2.1");
674 if (domain->socket_assembly == NULL) {
675 MonoImage *socket_assembly;
678 socket_assembly = mono_image_loaded ("System.Net");
679 if (!socket_assembly) {
680 MonoAssembly *sa = mono_assembly_open ("System.Net.dll", NULL);
683 g_assert_not_reached ();
685 socket_assembly = mono_assembly_get_image (sa);
689 socket_assembly = mono_image_loaded ("System");
690 if (!socket_assembly) {
691 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
694 g_assert_not_reached ();
696 socket_assembly = mono_assembly_get_image (sa);
701 domain->socket_assembly = socket_assembly;
704 return domain->socket_assembly;
708 static gint32 get_family_hint(void)
710 MonoDomain *domain = mono_domain_get ();
712 if (!domain->inet_family_hint) {
713 MonoClass *socket_class;
714 MonoClassField *ipv6_field, *ipv4_field;
715 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
718 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
719 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
720 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
721 vtable = mono_class_vtable (mono_domain_get (), socket_class);
723 mono_runtime_class_init (vtable);
725 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
726 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
728 mono_domain_lock (domain);
729 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
730 domain->inet_family_hint = 1;
731 } else if (ipv4_enabled == 1) {
732 domain->inet_family_hint = 2;
734 domain->inet_family_hint = 3;
736 mono_domain_unlock (domain);
738 switch (domain->inet_family_hint) {
739 case 1: return PF_UNSPEC;
740 case 2: return PF_INET;
741 case 3: return PF_INET6;
748 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
759 sock_family=convert_family(family);
760 if(sock_family==-1) {
761 *error = WSAEAFNOSUPPORT;
765 sock_proto=convert_proto(proto);
767 *error = WSAEPROTONOSUPPORT;
771 sock_type=convert_type(type);
773 *error = WSAESOCKTNOSUPPORT;
777 sock = _wapi_socket (sock_family, sock_type, sock_proto,
778 NULL, 0, WSA_FLAG_OVERLAPPED);
780 if(sock==INVALID_SOCKET) {
781 *error = WSAGetLastError ();
785 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
786 return (GUINT_TO_POINTER (sock));
790 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
791 return (GUINT_TO_POINTER (sock));
795 return(GUINT_TO_POINTER (sock));
798 /* FIXME: the SOCKET parameter (here and in other functions in this
799 * file) is really an IntPtr which needs to be converted to a guint32.
801 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
806 LOGDEBUG (g_message ("%s: closing 0x%x", __func__, sock));
810 /* Clear any pending work item from this socket if the underlying
811 * polling system does not notify when the socket is closed */
812 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
816 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
820 LOGDEBUG (g_message("%s: returning %d", __func__, WSAGetLastError()));
822 return(WSAGetLastError());
825 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
835 ret=ioctlsocket(sock, FIONREAD, &amount);
836 if(ret==SOCKET_ERROR) {
837 *error = WSAGetLastError ();
844 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
855 * block == TRUE/FALSE means we will block/not block.
856 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
860 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
861 if(ret==SOCKET_ERROR) {
862 *error = WSAGetLastError ();
866 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
877 MonoInternalThread* curthread = mono_thread_internal_current ();
878 curthread->interrupt_on_stop = (gpointer)TRUE;
879 newsock = _wapi_accept (sock, NULL, 0);
880 curthread->interrupt_on_stop = (gpointer)FALSE;
883 newsock = _wapi_accept (sock, NULL, 0);
885 if(newsock==INVALID_SOCKET) {
886 *error = WSAGetLastError ();
890 return(GUINT_TO_POINTER (newsock));
893 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
903 ret = _wapi_listen (sock, backlog);
904 if(ret==SOCKET_ERROR) {
905 *error = WSAGetLastError ();
909 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
910 int sa_size, gint32 *error)
912 MonoDomain *domain = mono_domain_get ();
913 MonoObject *sockaddr_obj;
915 MonoAddressFamily family;
917 /* Build a System.Net.SocketAddress object instance */
918 if (!domain->sockaddr_class) {
919 domain->sockaddr_class=mono_class_from_name (get_socket_assembly (), "System.Net", "SocketAddress");
920 g_assert (domain->sockaddr_class);
922 sockaddr_obj=mono_object_new(domain, domain->sockaddr_class);
924 /* Locate the SocketAddress data buffer in the object */
925 if (!domain->sockaddr_data_field) {
926 domain->sockaddr_data_field=mono_class_get_field_from_name (domain->sockaddr_class, "data");
927 g_assert (domain->sockaddr_data_field);
930 /* Make sure there is space for the family and size bytes */
932 if (saddr->sa_family == AF_UNIX) {
933 /* sa_len includes the entire sockaddr size, so we don't need the
934 * N bytes (sizeof (unsigned short)) of the family. */
935 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size);
939 /* May be the +2 here is too conservative, as sa_len returns
940 * the length of the entire sockaddr_in/in6, including
941 * sizeof (unsigned short) of the family */
942 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
945 /* The data buffer is laid out as follows:
946 * bytes 0 and 1 are the address family
947 * bytes 2 and 3 are the port info
948 * the rest is the address info
951 family=convert_to_mono_family(saddr->sa_family);
952 if(family==AddressFamily_Unknown) {
953 *error = WSAEAFNOSUPPORT;
957 mono_array_set(data, guint8, 0, family & 0x0FF);
958 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
960 if(saddr->sa_family==AF_INET) {
961 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
962 guint16 port=ntohs(sa_in->sin_port);
963 guint32 address=ntohl(sa_in->sin_addr.s_addr);
966 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
969 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
970 mono_array_set(data, guint8, 3, (port) & 0xff);
971 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
972 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
973 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
974 mono_array_set(data, guint8, 7, (address) & 0xff);
976 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
978 return(sockaddr_obj);
980 } else if (saddr->sa_family == AF_INET6) {
981 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
984 guint16 port=ntohs(sa_in->sin6_port);
987 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
990 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
991 mono_array_set(data, guint8, 3, (port) & 0xff);
993 for(i=0; i<16; i++) {
994 mono_array_set(data, guint8, 8+i,
995 sa_in->sin6_addr.s6_addr[i]);
998 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
999 mono_array_set(data, guint8, 25,
1000 (sa_in->sin6_scope_id >> 8) & 0xff);
1001 mono_array_set(data, guint8, 26,
1002 (sa_in->sin6_scope_id >> 16) & 0xff);
1003 mono_array_set(data, guint8, 27,
1004 (sa_in->sin6_scope_id >> 24) & 0xff);
1006 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1008 return(sockaddr_obj);
1010 #ifdef HAVE_SYS_UN_H
1011 } else if (saddr->sa_family == AF_UNIX) {
1014 for (i = 0; i < sa_size; i++) {
1015 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
1018 mono_field_set_value (sockaddr_obj, domain->sockaddr_data_field, data);
1020 return sockaddr_obj;
1023 *error = WSAEAFNOSUPPORT;
1029 get_sockaddr_size (int family)
1034 if (family == AF_INET) {
1035 size = sizeof (struct sockaddr_in);
1037 } else if (family == AF_INET6) {
1038 size = sizeof (struct sockaddr_in6);
1040 #ifdef HAVE_SYS_UN_H
1041 } else if (family == AF_UNIX) {
1042 size = sizeof (struct sockaddr_un);
1048 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1055 MONO_ARCH_SAVE_REGS;
1059 salen = get_sockaddr_size (convert_family (af));
1061 *error = WSAEAFNOSUPPORT;
1064 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1065 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1067 if(ret==SOCKET_ERROR) {
1068 *error = WSAGetLastError ();
1074 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)));
1076 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1082 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 af, gint32 *error)
1089 MONO_ARCH_SAVE_REGS;
1093 salen = get_sockaddr_size (convert_family (af));
1095 *error = WSAEAFNOSUPPORT;
1098 sa = (salen <= 128) ? alloca (salen) : g_malloc0 (salen);
1099 /* Note: linux returns just 2 for AF_UNIX. Always. */
1100 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1101 if(ret==SOCKET_ERROR) {
1102 *error = WSAGetLastError ();
1108 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)));
1110 result = create_object_from_sockaddr((struct sockaddr *)sa, salen, error);
1116 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1120 MonoClassField *field;
1125 /* Dig the SocketAddress data buffer out of the object */
1126 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1127 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1129 /* The data buffer is laid out as follows:
1130 * byte 0 is the address family low byte
1131 * byte 1 is the address family high byte
1133 * bytes 2 and 3 are the port info
1134 * the rest is the address info
1136 * the rest is the file name
1138 len = mono_array_length (data);
1140 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1143 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1144 if (family == AF_INET) {
1145 struct sockaddr_in *sa;
1150 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1153 sa = g_new0 (struct sockaddr_in, 1);
1154 port = (mono_array_get (data, guint8, 2) << 8) +
1155 mono_array_get (data, guint8, 3);
1156 address = (mono_array_get (data, guint8, 4) << 24) +
1157 (mono_array_get (data, guint8, 5) << 16 ) +
1158 (mono_array_get (data, guint8, 6) << 8) +
1159 mono_array_get (data, guint8, 7);
1161 sa->sin_family = family;
1162 sa->sin_addr.s_addr = htonl (address);
1163 sa->sin_port = htons (port);
1165 *sa_size = sizeof(struct sockaddr_in);
1166 return((struct sockaddr *)sa);
1169 } else if (family == AF_INET6) {
1170 struct sockaddr_in6 *sa;
1176 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1179 sa = g_new0 (struct sockaddr_in6, 1);
1180 port = mono_array_get (data, guint8, 3) +
1181 (mono_array_get (data, guint8, 2) << 8);
1182 scopeid = mono_array_get (data, guint8, 24) +
1183 (mono_array_get (data, guint8, 25) << 8) +
1184 (mono_array_get (data, guint8, 26) << 16) +
1185 (mono_array_get (data, guint8, 27) << 24);
1187 sa->sin6_family = family;
1188 sa->sin6_port = htons (port);
1189 sa->sin6_scope_id = scopeid;
1191 for(i=0; i<16; i++) {
1192 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1195 *sa_size = sizeof(struct sockaddr_in6);
1196 return((struct sockaddr *)sa);
1198 #ifdef HAVE_SYS_UN_H
1199 } else if (family == AF_UNIX) {
1200 struct sockaddr_un *sock_un;
1203 /* Need a byte for the '\0' terminator/prefix, and the first
1204 * two bytes hold the SocketAddress family
1206 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1207 mono_raise_exception (mono_get_exception_index_out_of_range ());
1210 sock_un = g_new0 (struct sockaddr_un, 1);
1212 sock_un->sun_family = family;
1213 for (i = 0; i < len - 2; i++) {
1214 sock_un->sun_path [i] = mono_array_get (data, guint8,
1220 return (struct sockaddr *)sock_un;
1223 *error = WSAEAFNOSUPPORT;
1228 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1230 struct sockaddr *sa;
1234 MONO_ARCH_SAVE_REGS;
1238 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1243 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)));
1245 ret = _wapi_bind (sock, sa, sa_size);
1246 if(ret==SOCKET_ERROR) {
1247 *error = WSAGetLastError ();
1260 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1261 gint timeout, gint32 *error)
1263 MonoInternalThread *thread = NULL;
1269 MONO_ARCH_SAVE_REGS;
1271 pfds = g_new0 (mono_pollfd, 1);
1272 pfds[0].fd = GPOINTER_TO_INT (sock);
1273 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1274 (mode == SelectModeWrite) ? MONO_POLLOUT :
1275 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1277 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1278 start = time (NULL);
1282 ret = mono_poll (pfds, 1, timeout);
1283 if (timeout > 0 && ret < 0) {
1285 int sec = time (NULL) - start;
1287 timeout -= sec * 1000;
1295 if (ret == -1 && errno == EINTR) {
1298 if (thread == NULL) {
1299 thread = mono_thread_internal_current ();
1302 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1308 /* Suspend requested? */
1309 mono_thread_interruption_checkpoint ();
1313 } while (ret == -1 && errno == EINTR);
1317 *error = WSAGetLastError ();
1319 *error = errno_to_WSA (errno, __func__);
1334 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1336 struct sockaddr *sa;
1340 MONO_ARCH_SAVE_REGS;
1344 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1349 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)));
1351 ret = _wapi_connect (sock, sa, sa_size);
1352 if(ret==SOCKET_ERROR) {
1353 *error = WSAGetLastError ();
1359 /* These #defines from mswsock.h from wine. Defining them here allows
1360 * us to build this file on a mingw box that doesn't know the magic
1361 * numbers, but still run on a newer windows box that does.
1363 #ifndef WSAID_DISCONNECTEX
1364 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1365 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1368 #ifndef WSAID_TRANSMITFILE
1369 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1370 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1373 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1376 glong output_bytes = 0;
1377 GUID disco_guid = WSAID_DISCONNECTEX;
1378 GUID trans_guid = WSAID_TRANSMITFILE;
1379 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1380 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1383 MONO_ARCH_SAVE_REGS;
1387 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1389 /* I _think_ the extension function pointers need to be looked
1390 * up for each socket. FIXME: check the best way to store
1391 * pointers to functions in managed objects that still works
1392 * on 64bit platforms.
1394 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1395 (void *)&disco_guid, sizeof(GUID),
1396 (void *)&_wapi_disconnectex, sizeof(void *),
1397 &output_bytes, NULL, NULL);
1399 /* make sure that WSAIoctl didn't put crap in the
1402 _wapi_disconnectex = NULL;
1404 /* Look up the TransmitFile extension function pointer
1405 * instead of calling TransmitFile() directly, because
1406 * apparently "Several of the extension functions have
1407 * been available since WinSock 1.1 and are exported
1408 * from MSWsock.dll, however it's not advisable to
1409 * link directly to this dll as this ties you to the
1410 * Microsoft WinSock provider. A provider neutral way
1411 * of accessing these extension functions is to load
1412 * them dynamically via WSAIoctl using the
1413 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1414 * should, theoretically, allow you to access these
1415 * functions from any provider that supports them..."
1416 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1418 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1419 (void *)&trans_guid, sizeof(GUID),
1420 (void *)&_wapi_transmitfile, sizeof(void *),
1421 &output_bytes, NULL, NULL);
1423 _wapi_transmitfile = NULL;
1427 if (_wapi_disconnectex != NULL) {
1428 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1429 } else if (_wapi_transmitfile != NULL) {
1430 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1431 TF_DISCONNECT | TF_REUSE_SOCKET);
1433 *error = ERROR_NOT_SUPPORTED;
1437 if (bret == FALSE) {
1438 *error = WSAGetLastError ();
1442 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1449 MONO_ARCH_SAVE_REGS;
1453 alen = mono_array_length (buffer);
1454 if (offset > alen - count) {
1458 buf=mono_array_addr(buffer, guchar, offset);
1460 recvflags = convert_socketflags (flags);
1461 if (recvflags == -1) {
1462 *error = WSAEOPNOTSUPP;
1468 MonoInternalThread* curthread = mono_thread_internal_current ();
1469 curthread->interrupt_on_stop = (gpointer)TRUE;
1470 ret = _wapi_recv (sock, buf, count, recvflags);
1471 curthread->interrupt_on_stop = (gpointer)FALSE;
1474 ret = _wapi_recv (sock, buf, count, recvflags);
1477 if(ret==SOCKET_ERROR) {
1478 *error = WSAGetLastError ();
1485 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1490 DWORD recvflags = 0;
1492 MONO_ARCH_SAVE_REGS;
1496 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1497 count = mono_array_length (buffers);
1499 recvflags = convert_socketflags (flags);
1500 if (recvflags == -1) {
1501 *error = WSAEOPNOTSUPP;
1505 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1506 if (ret == SOCKET_ERROR) {
1507 *error = WSAGetLastError ();
1514 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1520 struct sockaddr *sa;
1523 MONO_ARCH_SAVE_REGS;
1527 alen = mono_array_length (buffer);
1528 if (offset > alen - count) {
1532 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1537 buf=mono_array_addr(buffer, guchar, offset);
1539 recvflags = convert_socketflags (flags);
1540 if (recvflags == -1) {
1541 *error = WSAEOPNOTSUPP;
1545 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1546 if(ret==SOCKET_ERROR) {
1548 *error = WSAGetLastError ();
1552 /* If we didn't get a socket size, then we're probably a
1553 * connected connection-oriented socket and the stack hasn't
1554 * returned the remote address. All we can do is return null.
1557 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1566 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1573 MONO_ARCH_SAVE_REGS;
1577 alen = mono_array_length (buffer);
1578 if (offset > alen - count) {
1582 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1584 buf=mono_array_addr(buffer, guchar, offset);
1586 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1588 sendflags = convert_socketflags (flags);
1589 if (sendflags == -1) {
1590 *error = WSAEOPNOTSUPP;
1594 ret = _wapi_send (sock, buf, count, sendflags);
1595 if(ret==SOCKET_ERROR) {
1596 *error = WSAGetLastError ();
1603 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1608 DWORD sendflags = 0;
1610 MONO_ARCH_SAVE_REGS;
1614 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1615 count = mono_array_length (buffers);
1617 sendflags = convert_socketflags (flags);
1618 if (sendflags == -1) {
1619 *error = WSAEOPNOTSUPP;
1623 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1624 if (ret == SOCKET_ERROR) {
1625 *error = WSAGetLastError ();
1632 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1638 struct sockaddr *sa;
1641 MONO_ARCH_SAVE_REGS;
1645 alen = mono_array_length (buffer);
1646 if (offset > alen - count) {
1650 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1655 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1657 buf=mono_array_addr(buffer, guchar, offset);
1659 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1661 sendflags = convert_socketflags (flags);
1662 if (sendflags == -1) {
1663 *error = WSAEOPNOTSUPP;
1667 ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
1668 if(ret==SOCKET_ERROR) {
1669 *error = WSAGetLastError ();
1677 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1680 MonoClassField *field;
1682 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1683 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1688 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1689 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1691 MonoInternalThread *thread = NULL;
1698 MonoClass *sock_arr_class;
1701 uintptr_t socks_size;
1703 MONO_ARCH_SAVE_REGS;
1705 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1706 count = mono_array_length (*sockets);
1707 nfds = count - 3; /* NULL separators */
1708 pfds = g_new0 (mono_pollfd, nfds);
1710 for (i = 0; i < count; i++) {
1711 obj = mono_array_get (*sockets, MonoObject *, i);
1718 /* The socket array was bogus */
1724 pfds [idx].fd = Socket_to_SOCKET (obj);
1725 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1729 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1730 start = time (NULL);
1733 ret = mono_poll (pfds, nfds, timeout);
1734 if (timeout > 0 && ret < 0) {
1736 int sec = time (NULL) - start;
1738 timeout -= sec * 1000;
1744 if (ret == -1 && errno == EINTR) {
1747 thread = mono_thread_internal_current ();
1749 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1756 /* Suspend requested? */
1757 mono_thread_interruption_checkpoint ();
1761 } while (ret == -1 && errno == EINTR);
1765 *error = WSAGetLastError ();
1767 *error = errno_to_WSA (errno, __func__);
1779 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1780 socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
1781 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1784 for (i = 0; i < count && ret > 0; i++) {
1787 obj = mono_array_get (*sockets, MonoObject *, i);
1794 pfd = &pfds [i - mode];
1795 if (pfd->revents == 0)
1799 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1800 mono_array_setref (socks, idx++, obj);
1801 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1802 mono_array_setref (socks, idx++, obj);
1803 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1804 mono_array_setref (socks, idx++, obj);
1812 static MonoObject* int_to_object (MonoDomain *domain, int val)
1814 return mono_value_box (domain, mono_get_int32_class (), &val);
1818 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1824 socklen_t valsize=sizeof(val);
1825 struct linger linger;
1826 socklen_t lingersize=sizeof(linger);
1828 socklen_t time_ms_size = sizeof (time_ms);
1830 # if defined(__OpenBSD__)
1831 struct sockpeercred cred;
1835 socklen_t credsize = sizeof(cred);
1837 MonoDomain *domain=mono_domain_get();
1839 MonoClass *obj_class;
1840 MonoClassField *field;
1842 MONO_ARCH_SAVE_REGS;
1846 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1847 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
1848 system_level = SOL_SOCKET;
1849 system_name = SO_REUSEADDR;
1855 ret = convert_sockopt_level_and_name (level, name, &system_level, &system_name);
1859 *error = WSAENOPROTOOPT;
1863 *obj_val = int_to_object (domain, 0);
1867 /* No need to deal with MulticastOption names here, because
1868 * you cant getsockopt AddMembership or DropMembership (the
1869 * int getsockopt will error, causing an exception)
1872 case SocketOptionName_Linger:
1873 case SocketOptionName_DontLinger:
1874 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1878 case SocketOptionName_SendTimeout:
1879 case SocketOptionName_ReceiveTimeout:
1880 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1884 case SocketOptionName_PeerCred:
1885 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1891 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1895 if(ret==SOCKET_ERROR) {
1896 *error = WSAGetLastError ();
1901 case SocketOptionName_Linger:
1902 /* build a System.Net.Sockets.LingerOption */
1903 obj_class=mono_class_from_name(get_socket_assembly (),
1904 "System.Net.Sockets",
1906 obj=mono_object_new(domain, obj_class);
1908 /* Locate and set the fields "bool enabled" and "int
1911 field=mono_class_get_field_from_name(obj_class, "enabled");
1912 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1914 field=mono_class_get_field_from_name(obj_class, "seconds");
1915 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1919 case SocketOptionName_DontLinger:
1920 /* construct a bool int in val - true if linger is off */
1921 obj = int_to_object (domain, !linger.l_onoff);
1924 case SocketOptionName_SendTimeout:
1925 case SocketOptionName_ReceiveTimeout:
1926 obj = int_to_object (domain, time_ms);
1930 case SocketOptionName_PeerCred:
1932 /* build a Mono.Posix.PeerCred+PeerCredData if
1935 static MonoImage *mono_posix_image = NULL;
1936 MonoPeerCredData *cred_data;
1938 if (mono_posix_image == NULL) {
1939 mono_posix_image=mono_image_loaded ("Mono.Posix");
1940 if (!mono_posix_image) {
1941 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1943 *error = WSAENOPROTOOPT;
1946 mono_posix_image = mono_assembly_get_image (sa);
1951 obj_class = mono_class_from_name(mono_posix_image,
1954 obj = mono_object_new(domain, obj_class);
1955 cred_data = (MonoPeerCredData *)obj;
1956 cred_data->pid = cred.pid;
1957 cred_data->uid = cred.uid;
1958 cred_data->gid = cred.gid;
1964 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1965 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse)
1968 obj = int_to_object (domain, val);
1973 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1981 MONO_ARCH_SAVE_REGS;
1985 ret=convert_sockopt_level_and_name(level, name, &system_level,
1988 *error = WSAENOPROTOOPT;
1994 valsize=mono_array_length(*byte_val);
1995 buf=mono_array_addr(*byte_val, guchar, 0);
1997 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1998 if(ret==SOCKET_ERROR) {
1999 *error = WSAGetLastError ();
2003 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2004 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
2006 struct in_addr inaddr;
2007 MonoClassField *field;
2009 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
2011 /* No idea why .net uses a 64bit type to hold a 32bit value...
2013 * Internal value of IPAddess is in little-endian order
2015 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
2021 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
2023 struct in6_addr in6addr;
2024 MonoClassField *field;
2028 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
2029 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
2031 /* Solaris has only the 8 bit version. */
2033 for(i=0; i<8; i++) {
2034 guint16 s = mono_array_get (data, guint16, i);
2035 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
2036 in6addr.s6_addr[2 * i + 1] = s & 0xff;
2040 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
2044 #endif /* AF_INET6 */
2047 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)
2049 struct linger linger;
2059 #ifdef HAVE_SOL_IPV6
2060 sol_ipv6 = SOL_IPV6;
2063 struct protoent *pent;
2064 pent = getprotobyname ("ipv6");
2065 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
2073 struct protoent *pent;
2074 pent = getprotobyname ("ip");
2075 sol_ip = (pent != NULL) ? pent->p_proto : 0;
2078 #endif /* AF_INET6 */
2080 MONO_ARCH_SAVE_REGS;
2082 ret=convert_sockopt_level_and_name(level, name, &system_level,
2085 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2086 if (level == SocketOptionLevel_Socket && name == SocketOptionName_ExclusiveAddressUse) {
2087 system_name = SO_REUSEADDR;
2088 int_val = int_val ? 0 : 1;
2094 *error = WSAENOPROTOOPT;
2101 /* Only one of obj_val, byte_val or int_val has data */
2103 MonoClassField *field;
2107 case SocketOptionName_Linger:
2108 /* Dig out "bool enabled" and "int seconds"
2111 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2112 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2113 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2114 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2116 valsize=sizeof(linger);
2117 ret = _wapi_setsockopt (sock, system_level,
2118 system_name, &linger, valsize);
2120 case SocketOptionName_AddMembership:
2121 case SocketOptionName_DropMembership:
2122 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2124 MonoObject *address = NULL;
2127 if(system_level == sol_ipv6) {
2128 struct ipv6_mreq mreq6;
2133 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2134 address = *(gpointer *)(((char *)obj_val) + field->offset);
2137 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2140 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2141 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2143 ret = _wapi_setsockopt (sock, system_level,
2144 system_name, &mreq6,
2146 } else if(system_level == sol_ip)
2147 #endif /* AF_INET6 */
2149 #ifdef HAVE_STRUCT_IP_MREQN
2150 struct ip_mreqn mreq = {{0}};
2152 struct ip_mreq mreq = {{0}};
2153 #endif /* HAVE_STRUCT_IP_MREQN */
2155 /* pain! MulticastOption holds two IPAddress
2156 * members, so I have to dig the value out of
2159 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2160 address = *(gpointer *)(((char *)obj_val) + field->offset);
2162 /* address might not be defined and if so, set the address to ADDR_ANY.
2165 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2168 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2169 address = *(gpointer *)(((char *)obj_val) + field->offset);
2171 #ifdef HAVE_STRUCT_IP_MREQN
2173 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2177 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2179 #endif /* HAVE_STRUCT_IP_MREQN */
2181 ret = _wapi_setsockopt (sock, system_level,
2187 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2189 /* Cause an exception to be thrown */
2193 } else if (byte_val!=NULL) {
2194 int valsize = mono_array_length (byte_val);
2195 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2198 case SocketOptionName_DontLinger:
2200 linger.l_onoff = (*buf) ? 0 : 1;
2201 linger.l_linger = 0;
2202 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2208 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2212 /* ReceiveTimeout/SendTimeout get here */
2214 case SocketOptionName_DontLinger:
2215 linger.l_onoff = !int_val;
2216 linger.l_linger = 0;
2217 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2219 case SocketOptionName_DontFragment:
2220 #ifdef HAVE_IP_MTU_DISCOVER
2221 /* Fiddle with the value slightly if we're
2225 int_val = IP_PMTUDISC_DO;
2231 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2235 if(ret==SOCKET_ERROR) {
2236 *error = WSAGetLastError ();
2240 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2246 MONO_ARCH_SAVE_REGS;
2250 /* Currently, the values for how (recv=0, send=1, both=2) match
2253 ret = _wapi_shutdown (sock, how);
2254 if(ret==SOCKET_ERROR) {
2255 *error = WSAGetLastError ();
2260 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2262 MonoArray *output, gint32 *error)
2264 glong output_bytes = 0;
2265 gchar *i_buffer, *o_buffer;
2269 MONO_ARCH_SAVE_REGS;
2273 if (code == FIONBIO) {
2274 /* Invalid command. Must use Socket.Blocking */
2278 if (input == NULL) {
2282 i_buffer = mono_array_addr (input, gchar, 0);
2283 i_len = mono_array_length (input);
2286 if (output == NULL) {
2290 o_buffer = mono_array_addr (output, gchar, 0);
2291 o_len = mono_array_length (output);
2294 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2295 if (ret == SOCKET_ERROR) {
2296 *error = WSAGetLastError ();
2300 return (gint) output_bytes;
2303 #ifdef HAVE_SIOCGIFCONF
2305 is_loopback (int family, void *ad)
2307 char *ptr = (char *) ad;
2309 if (family == AF_INET) {
2310 return (ptr [0] == 127);
2314 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2321 get_local_ips (int family, int *nips)
2323 int addr_size, offset, fd, i, count;
2324 int max_ifaces = 50; /* 50 interfaces should be enough... */
2327 struct ifreq iflags;
2328 char *result, *tmp_ptr;
2329 gboolean ignore_loopback = FALSE;
2332 if (family == AF_INET) {
2333 addr_size = sizeof (struct in_addr);
2334 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2336 } else if (family == AF_INET6) {
2337 addr_size = sizeof (struct in6_addr);
2338 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2344 fd = socket (family, SOCK_STREAM, 0);
2346 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2347 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2348 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2350 g_free (ifc.ifc_buf);
2354 count = ifc.ifc_len / sizeof (struct ifreq);
2357 g_free (ifc.ifc_buf);
2362 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2363 strcpy (iflags.ifr_name, ifr->ifr_name);
2364 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2368 if ((iflags.ifr_flags & IFF_UP) == 0) {
2369 ifr->ifr_name [0] = '\0';
2373 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2374 ignore_loopback = TRUE;
2379 result = g_malloc (addr_size * count);
2381 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2382 if (ifr->ifr_name [0] == '\0') {
2387 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2392 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2393 tmp_ptr += addr_size;
2396 g_free (ifc.ifc_buf);
2401 get_local_ips (int family, int *nips)
2407 #endif /* HAVE_SIOCGIFCONF */
2410 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2411 MonoArray **h_aliases,
2412 MonoArray **h_addr_list,
2413 gboolean add_local_ips)
2415 MonoDomain *domain = mono_domain_get ();
2417 struct in_addr *local_in = NULL;
2421 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2425 *h_name=mono_string_new(domain, he->h_name);
2427 while(he->h_aliases[i]!=NULL) {
2431 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2433 while(he->h_aliases[i]!=NULL) {
2436 alias=mono_string_new(domain, he->h_aliases[i]);
2437 mono_array_setref (*h_aliases, i, alias);
2440 } else if (!add_local_ips) {
2444 if (add_local_ips) {
2445 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2447 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2448 for (i = 0; i < nlocal_in; i++) {
2449 MonoString *addr_string;
2450 char addr [16], *ptr;
2452 ptr = (char *) &local_in [i];
2453 g_snprintf(addr, 16, "%u.%u.%u.%u",
2454 (unsigned char) ptr [0],
2455 (unsigned char) ptr [1],
2456 (unsigned char) ptr [2],
2457 (unsigned char) ptr [3]);
2459 addr_string = mono_string_new (domain, addr);
2460 mono_array_setref (*h_addr_list, i, addr_string);
2465 } else if (he == NULL) {
2466 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2467 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2468 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2473 if (nlocal_in == 0 && he != NULL) {
2475 while (he->h_addr_list[i]!=NULL) {
2479 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2481 while(he->h_addr_list[i]!=NULL) {
2482 MonoString *addr_string;
2485 g_snprintf(addr, 16, "%u.%u.%u.%u",
2486 (unsigned char)he->h_addr_list[i][0],
2487 (unsigned char)he->h_addr_list[i][1],
2488 (unsigned char)he->h_addr_list[i][2],
2489 (unsigned char)he->h_addr_list[i][3]);
2491 addr_string=mono_string_new(domain, addr);
2492 mono_array_setref (*h_addr_list, i, addr_string);
2500 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2501 MonoArray **h_aliases,
2502 MonoArray **h_addr_list)
2504 MonoDomain *domain = mono_domain_get ();
2506 *h_name=mono_string_new(domain, addr);
2507 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2508 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2509 mono_array_setref (*h_addr_list, 0, *h_name);
2515 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2516 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2517 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2519 MonoDomain *domain = mono_domain_get ();
2520 int i, host_count, host_index, family_hint;
2521 struct in_addr *local_in = NULL;
2523 struct in6_addr *local_in6 = NULL;
2525 gboolean from_local = FALSE;
2527 family_hint = get_family_hint ();
2530 * Check if address length and family are correct
2532 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2536 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2541 * Get the aliases and host name from he1 or he2 whichever is
2542 * not null, if he1 is not null then take aliases from he1
2544 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2545 family_hint == PF_INET)) {
2546 *h_name=mono_string_new (domain, he1->h_name);
2549 while(he1->h_aliases[i]!=NULL) {
2553 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2556 while(he1->h_aliases[i]!=NULL) {
2559 alias=mono_string_new (domain, he1->h_aliases[i]);
2560 mono_array_setref (*h_aliases, i, alias);
2563 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2564 family_hint == PF_INET6)) {
2565 *h_name=mono_string_new (domain, he2->h_name);
2568 while(he2->h_aliases [i] != NULL) {
2572 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2575 while(he2->h_aliases[i]!=NULL) {
2578 alias=mono_string_new (domain, he2->h_aliases[i]);
2579 mono_array_setref (*h_aliases, i, alias);
2582 } else if (!add_local_ips) {
2587 * Count the number of addresses in he1 + he2
2590 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2591 family_hint == PF_INET)) {
2593 while(he1->h_addr_list[i]!=NULL) {
2599 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2600 family_hint == PF_INET6)) {
2602 while(he2->h_addr_list[i]!=NULL) {
2612 if (add_local_ips) {
2613 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2614 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2616 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2617 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2619 if (nlocal_in || nlocal_in6) {
2621 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2622 nlocal_in + nlocal_in6);
2626 for (n = 0; n < nlocal_in6; n++) {
2627 MonoString *addr_string;
2629 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2631 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2634 addr_string = mono_string_new (domain, addr);
2635 mono_array_setref (*h_addr_list, host_index, addr_string);
2643 for (n = 0; n < nlocal_in; n++) {
2644 MonoString *addr_string;
2646 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2648 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2651 addr_string = mono_string_new (domain, addr);
2652 mono_array_setref (*h_addr_list, host_index, addr_string);
2660 } else if (he1 == NULL && he2 == NULL) {
2661 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2662 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2663 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2673 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2675 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2676 family_hint == PF_INET6)) {
2678 while(he2->h_addr_list[i] != NULL) {
2679 MonoString *addr_string;
2681 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2683 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2687 addr_string = mono_string_new (domain, addr);
2688 mono_array_setref (*h_addr_list, host_index, addr_string);
2695 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2696 family_hint == PF_INET)) {
2698 while(he1->h_addr_list[i] != NULL) {
2699 MonoString *addr_string;
2701 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2703 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2707 addr_string=mono_string_new (domain, addr);
2708 mono_array_setref (*h_addr_list, host_index, addr_string);
2719 #if defined(AF_INET6)
2721 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2722 MonoArray **h_aliases,
2723 MonoArray **h_addr_list,
2724 gboolean add_local_ips)
2727 struct addrinfo *ai = NULL;
2728 struct in_addr *local_in = NULL;
2730 struct in6_addr *local_in6 = NULL;
2734 MonoDomain *domain = mono_domain_get ();
2737 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2738 if (add_local_ips) {
2739 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2740 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2741 if (nlocal_in || nlocal_in6) {
2742 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2744 MonoString *addr_string;
2748 for (i = 0; i < nlocal_in; i++) {
2749 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2750 addr_string = mono_string_new (domain, addr);
2751 mono_array_setref (*h_addr_list, addr_index, addr_string);
2757 MonoString *addr_string;
2762 for (i = 0; i < nlocal_in6; i++) {
2763 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2765 addr_string = mono_string_new (domain, addr);
2766 mono_array_setref (*h_addr_list, addr_index, addr_string);
2775 freeaddrinfo (info);
2784 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2785 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2791 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2793 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2794 MonoString *addr_string;
2796 char buffer [48]; /* Max. size for IPv6 */
2798 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2802 if(ai->ai_family == PF_INET) {
2803 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2805 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2809 addr_string=mono_string_new(domain, buffer);
2811 addr_string=mono_string_new(domain, "");
2814 mono_array_setref (*h_addr_list, addr_index, addr_string);
2818 if (ai->ai_canonname != NULL) {
2819 *h_name=mono_string_new(domain, ai->ai_canonname);
2821 *h_name=mono_string_new(domain, buffer);
2837 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2839 gboolean add_local_ips = FALSE;
2840 #ifdef HAVE_SIOCGIFCONF
2841 gchar this_hostname [256];
2843 #if !defined(HAVE_GETHOSTBYNAME2_R)
2844 struct addrinfo *info = NULL, hints;
2847 MONO_ARCH_SAVE_REGS;
2849 hostname=mono_string_to_utf8 (host);
2850 if (*hostname == '\0') {
2851 add_local_ips = TRUE;
2854 #ifdef HAVE_SIOCGIFCONF
2855 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2856 if (!strcmp (hostname, this_hostname)) {
2857 add_local_ips = TRUE;
2863 memset(&hints, 0, sizeof(hints));
2864 hints.ai_family = get_family_hint ();
2865 hints.ai_socktype = SOCK_STREAM;
2866 hints.ai_flags = AI_CANONNAME;
2868 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2874 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2876 struct hostent he1,*hp1, he2, *hp2;
2877 int buffer_size1, buffer_size2;
2878 char *buffer1, *buffer2;
2880 gboolean return_value;
2883 MONO_ARCH_SAVE_REGS;
2885 hostname=mono_string_to_utf8 (host);
2886 if (*hostname == '\0')
2887 add_local_ips = TRUE;
2889 #ifdef HAVE_SIOCGIFCONF
2890 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2891 if (!strcmp (hostname, this_hostname))
2892 add_local_ips = TRUE;
2898 buffer1 = g_malloc0(buffer_size1);
2899 buffer2 = g_malloc0(buffer_size2);
2903 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2904 &hp1, &herr) == ERANGE) {
2906 buffer1 = g_realloc(buffer1, buffer_size1);
2909 if (*hostname && hp1 == NULL)
2911 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2912 buffer_size2, &hp2, &herr) == ERANGE) {
2914 buffer2 = g_realloc(buffer2, buffer_size2);
2918 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2919 h_addr_list, add_local_ips);
2925 return(return_value);
2926 #endif /* HAVE_GETHOSTBYNAME2_R */
2928 #else /* AF_INET6 */
2929 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2933 gboolean add_local_ips = FALSE;
2934 #ifdef HAVE_SIOCGIFCONF
2935 gchar this_hostname [256];
2938 MONO_ARCH_SAVE_REGS;
2940 hostname=mono_string_to_utf8(host);
2941 if (*hostname == '\0')
2942 add_local_ips = TRUE;
2943 #ifdef HAVE_SIOCGIFCONF
2944 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2945 if (!strcmp (hostname, this_hostname))
2946 add_local_ips = TRUE;
2953 he = _wapi_gethostbyname (hostname);
2955 he = _wapi_gethostbyname (hostname);
2959 if (*hostname && he==NULL)
2962 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2964 #endif /* AF_INET6 */
2966 #ifndef HAVE_INET_PTON
2968 inet_pton (int family, const char *address, void *inaddrp)
2970 if (family == AF_INET) {
2971 #ifdef HAVE_INET_ATON
2972 struct in_addr inaddr;
2974 if (!inet_aton (address, &inaddr))
2977 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2980 /* assume the system has inet_addr(), if it doesn't
2981 have that we're pretty much screwed... */
2984 if (!strcmp (address, "255.255.255.255")) {
2985 /* special-case hack */
2986 inaddr = 0xffffffff;
2988 inaddr = inet_addr (address);
2990 #define INADDR_NONE ((in_addr_t) -1)
2992 if (inaddr == INADDR_NONE)
2996 memcpy (inaddrp, &inaddr, sizeof (guint32));
2998 #endif /* HAVE_INET_ATON */
3003 #endif /* !HAVE_INET_PTON */
3005 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
3010 struct sockaddr_in saddr;
3011 struct sockaddr_in6 saddr6;
3012 struct addrinfo *info = NULL, hints;
3014 char hostname[1024] = {0};
3017 struct in_addr inaddr;
3022 address = mono_string_to_utf8 (addr);
3025 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
3026 /* Maybe an ipv6 address */
3027 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
3033 saddr6.sin6_family = AF_INET6;
3038 saddr.sin_family = AF_INET;
3042 if(family == AF_INET) {
3043 #if HAVE_SOCKADDR_IN_SIN_LEN
3044 saddr.sin_len = sizeof (saddr);
3046 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
3047 hostname, sizeof(hostname), NULL, 0,
3051 } else if(family == AF_INET6) {
3052 #if HAVE_SOCKADDR_IN6_SIN_LEN
3053 saddr6.sin6_len = sizeof (saddr6);
3055 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
3056 hostname, sizeof(hostname), NULL, 0,
3062 memset (&hints, 0, sizeof(hints));
3063 hints.ai_family = get_family_hint ();
3064 hints.ai_socktype = SOCK_STREAM;
3065 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
3067 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
3071 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
3073 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
3078 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
3079 ret = ipaddr_to_IPHostEntry (address, h_name, h_aliases, h_addr_list);
3081 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
3082 h_addr_list, FALSE);
3090 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
3092 gchar hostname[256];
3095 MONO_ARCH_SAVE_REGS;
3097 ret = gethostname (hostname, sizeof (hostname));
3102 *h_name=mono_string_new(mono_domain_get (), hostname);
3108 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
3112 TRANSMIT_FILE_BUFFERS buffers;
3114 MONO_ARCH_SAVE_REGS;
3116 if (filename == NULL)
3119 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
3120 if (file == INVALID_HANDLE_VALUE) {
3121 SetLastError (error);
3125 memset (&buffers, 0, sizeof (buffers));
3126 if (pre_buffer != NULL) {
3127 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
3128 buffers.HeadLength = mono_array_length (pre_buffer);
3130 if (post_buffer != NULL) {
3131 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
3132 buffers.TailLength = mono_array_length (post_buffer);
3135 if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
3144 void mono_network_init(void)
3149 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3151 g_error("%s: Couldn't initialise networking", __func__);
3155 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3156 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3159 void mono_network_cleanup(void)
3165 #endif /* #ifndef DISABLE_SOCKETS */