2 * socket-io.c: Socket IO internal calls
5 * Dick Porter (dick@ximian.com)
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
22 #include <sys/types.h>
23 #ifndef PLATFORM_WIN32
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
29 #include <arpa/inet.h>
32 #include <mono/metadata/object.h>
33 #include <mono/io-layer/io-layer.h>
34 #include <mono/metadata/socket-io.h>
35 #include <mono/metadata/exception.h>
36 #include <mono/metadata/assembly.h>
37 #include <mono/metadata/appdomain.h>
38 #include <mono/metadata/file-io.h>
39 #include <mono/metadata/threads.h>
40 #include <mono/metadata/threads-types.h>
41 #include <mono/utils/mono-poll.h>
42 /* FIXME change this code to not mess so much with the internals */
43 #include <mono/metadata/class-internals.h>
44 #include <mono/metadata/threadpool-internals.h>
45 #include <mono/metadata/domain-internals.h>
47 #ifdef HAVE_SYS_TIME_H
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
60 #ifdef HAVE_SYS_FILIO_H
61 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
63 #ifdef HAVE_SYS_SOCKIO_H
64 #include <sys/sockio.h> /* defines SIOCATMARK */
70 #include "mono/io-layer/socket-wrappers.h"
73 /* This is a kludge to make this file build under cygwin:
74 * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
75 * prototypes for some but not all required functions (notably
76 * inet_ntop() is missing), but the libws2_32 library is missing the
77 * actual implementations of these functions.
82 #ifdef PLATFORM_ANDROID
83 // not yet actually implemented...
88 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
90 static gint32 convert_family(MonoAddressFamily mono_family)
95 case AddressFamily_Unknown:
96 case AddressFamily_ImpLink:
97 case AddressFamily_Pup:
98 case AddressFamily_Chaos:
99 case AddressFamily_Iso:
100 case AddressFamily_Ecma:
101 case AddressFamily_DataKit:
102 case AddressFamily_Ccitt:
103 case AddressFamily_DataLink:
104 case AddressFamily_Lat:
105 case AddressFamily_HyperChannel:
106 case AddressFamily_NetBios:
107 case AddressFamily_VoiceView:
108 case AddressFamily_FireFox:
109 case AddressFamily_Banyan:
110 case AddressFamily_Atm:
111 case AddressFamily_Cluster:
112 case AddressFamily_Ieee12844:
113 case AddressFamily_NetworkDesigners:
114 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
117 case AddressFamily_Unspecified:
121 case AddressFamily_Unix:
125 case AddressFamily_InterNetwork:
129 case AddressFamily_Ipx:
135 case AddressFamily_Sna:
139 case AddressFamily_DecNet:
143 case AddressFamily_AppleTalk:
147 case AddressFamily_InterNetworkV6:
152 case AddressFamily_Irda:
158 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
164 static MonoAddressFamily convert_to_mono_family(guint16 af_family)
166 MonoAddressFamily family=AddressFamily_Unknown;
170 family=AddressFamily_Unspecified;
174 family=AddressFamily_Unix;
178 family=AddressFamily_InterNetwork;
183 family=AddressFamily_Ipx;
188 family=AddressFamily_Sna;
192 family=AddressFamily_DecNet;
196 family=AddressFamily_AppleTalk;
201 family=AddressFamily_InterNetworkV6;
207 family=AddressFamily_Irda;
211 g_warning("unknown address family 0x%x", af_family);
217 static gint32 convert_type(MonoSocketType mono_type)
222 case SocketType_Stream:
226 case SocketType_Dgram:
238 case SocketType_Seqpacket:
242 case SocketType_Unknown:
243 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
247 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
253 static gint32 convert_proto(MonoProtocolType mono_proto)
258 case ProtocolType_IP:
259 case ProtocolType_IPv6:
260 case ProtocolType_Icmp:
261 case ProtocolType_Igmp:
262 case ProtocolType_Ggp:
263 case ProtocolType_Tcp:
264 case ProtocolType_Pup:
265 case ProtocolType_Udp:
266 case ProtocolType_Idp:
267 /* These protocols are known (on my system at least) */
271 case ProtocolType_ND:
272 case ProtocolType_Raw:
273 case ProtocolType_Ipx:
274 case ProtocolType_Spx:
275 case ProtocolType_SpxII:
276 case ProtocolType_Unknown:
277 /* These protocols arent */
278 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
288 /* Convert MonoSocketFlags */
289 static gint32 convert_socketflags (gint32 sflags)
294 /* SocketFlags.None */
297 if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
298 SocketFlags_DontRoute | SocketFlags_Partial))
299 /* Contains invalid flag values */
302 if (sflags & SocketFlags_OutOfBand)
304 if (sflags & SocketFlags_Peek)
306 if (sflags & SocketFlags_DontRoute)
307 flags |= MSG_DONTROUTE;
309 /* Ignore Partial - see bug 349688. Don't return -1, because
310 * according to the comment in that bug ms runtime doesn't for
311 * UDP sockets (this means we will silently ignore it for TCP
315 if (sflags & SocketFlags_Partial)
319 /* Don't do anything for MaxIOVectorLength */
320 if (sflags & SocketFlags_MaxIOVectorLength)
328 * 0 on success (mapped mono_level and mono_name to system_level and system_name
330 * -2 on non-fatal error (ie, must ignore)
332 static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
333 MonoSocketOptionName mono_name,
337 switch (mono_level) {
338 case SocketOptionLevel_Socket:
339 *system_level = SOL_SOCKET;
342 case SocketOptionName_DontLinger:
343 /* This is SO_LINGER, because the setsockopt
344 * internal call maps DontLinger to SO_LINGER
347 *system_name = SO_LINGER;
349 case SocketOptionName_Debug:
350 *system_name = SO_DEBUG;
353 case SocketOptionName_AcceptConnection:
354 *system_name = SO_ACCEPTCONN;
357 case SocketOptionName_ReuseAddress:
358 *system_name = SO_REUSEADDR;
360 case SocketOptionName_KeepAlive:
361 *system_name = SO_KEEPALIVE;
363 case SocketOptionName_DontRoute:
364 *system_name = SO_DONTROUTE;
366 case SocketOptionName_Broadcast:
367 *system_name = SO_BROADCAST;
369 case SocketOptionName_Linger:
370 *system_name = SO_LINGER;
372 case SocketOptionName_OutOfBandInline:
373 *system_name = SO_OOBINLINE;
375 case SocketOptionName_SendBuffer:
376 *system_name = SO_SNDBUF;
378 case SocketOptionName_ReceiveBuffer:
379 *system_name = SO_RCVBUF;
381 case SocketOptionName_SendLowWater:
382 *system_name = SO_SNDLOWAT;
384 case SocketOptionName_ReceiveLowWater:
385 *system_name = SO_RCVLOWAT;
387 case SocketOptionName_SendTimeout:
388 *system_name = SO_SNDTIMEO;
390 case SocketOptionName_ReceiveTimeout:
391 *system_name = SO_RCVTIMEO;
393 case SocketOptionName_Error:
394 *system_name = SO_ERROR;
396 case SocketOptionName_Type:
397 *system_name = SO_TYPE;
400 case SocketOptionName_PeerCred:
401 *system_name = SO_PEERCRED;
404 case SocketOptionName_ExclusiveAddressUse:
405 #ifdef SO_EXCLUSIVEADDRUSE
406 *system_name = SO_EXCLUSIVEADDRUSE;
409 case SocketOptionName_UseLoopback:
410 #ifdef SO_USELOOPBACK
411 *system_name = SO_USELOOPBACK;
414 case SocketOptionName_MaxConnections:
416 *system_name = SO_MAXCONN;
418 #elif defined(SOMAXCONN)
419 *system_name = SOMAXCONN;
423 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
428 case SocketOptionLevel_IP:
430 *system_level = SOL_IP;
433 static int cached = 0;
437 struct protoent *pent;
439 pent = getprotobyname ("IP");
440 proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
444 *system_level = proto;
446 #endif /* HAVE_SOL_IP */
449 case SocketOptionName_IPOptions:
450 *system_name = IP_OPTIONS;
453 case SocketOptionName_HeaderIncluded:
454 *system_name = IP_HDRINCL;
458 case SocketOptionName_TypeOfService:
459 *system_name = IP_TOS;
463 case SocketOptionName_IpTimeToLive:
464 *system_name = IP_TTL;
467 case SocketOptionName_MulticastInterface:
468 *system_name = IP_MULTICAST_IF;
470 case SocketOptionName_MulticastTimeToLive:
471 *system_name = IP_MULTICAST_TTL;
473 case SocketOptionName_MulticastLoopback:
474 *system_name = IP_MULTICAST_LOOP;
476 case SocketOptionName_AddMembership:
477 *system_name = IP_ADD_MEMBERSHIP;
479 case SocketOptionName_DropMembership:
480 *system_name = IP_DROP_MEMBERSHIP;
482 #ifdef HAVE_IP_PKTINFO
483 case SocketOptionName_PacketInformation:
484 *system_name = IP_PKTINFO;
486 #endif /* HAVE_IP_PKTINFO */
488 case SocketOptionName_DontFragment:
489 #ifdef HAVE_IP_DONTFRAGMENT
490 *system_name = IP_DONTFRAGMENT;
492 #elif defined HAVE_IP_MTU_DISCOVER
493 /* Not quite the same */
494 *system_name = IP_MTU_DISCOVER;
497 /* If the flag is not available on this system, we can ignore this error */
499 #endif /* HAVE_IP_DONTFRAGMENT */
500 case SocketOptionName_AddSourceMembership:
501 case SocketOptionName_DropSourceMembership:
502 case SocketOptionName_BlockSource:
503 case SocketOptionName_UnblockSource:
504 /* Can't figure out how to map these, so fall
508 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
514 case SocketOptionLevel_IPv6:
516 *system_level = SOL_IPV6;
519 static int cached = 0;
523 struct protoent *pent;
525 pent = getprotobyname ("IPV6");
526 proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
530 *system_level = proto;
532 #endif /* HAVE_SOL_IPV6 */
535 case SocketOptionName_IpTimeToLive:
536 *system_name = IPV6_UNICAST_HOPS;
538 case SocketOptionName_MulticastInterface:
539 *system_name = IPV6_MULTICAST_IF;
541 case SocketOptionName_MulticastTimeToLive:
542 *system_name = IPV6_MULTICAST_HOPS;
544 case SocketOptionName_MulticastLoopback:
545 *system_name = IPV6_MULTICAST_LOOP;
547 case SocketOptionName_AddMembership:
548 *system_name = IPV6_JOIN_GROUP;
550 case SocketOptionName_DropMembership:
551 *system_name = IPV6_LEAVE_GROUP;
553 case SocketOptionName_PacketInformation:
554 #ifdef HAVE_IPV6_PKTINFO
555 *system_name = IPV6_PKTINFO;
558 case SocketOptionName_HeaderIncluded:
559 case SocketOptionName_IPOptions:
560 case SocketOptionName_TypeOfService:
561 case SocketOptionName_DontFragment:
562 case SocketOptionName_AddSourceMembership:
563 case SocketOptionName_DropSourceMembership:
564 case SocketOptionName_BlockSource:
565 case SocketOptionName_UnblockSource:
566 /* Can't figure out how to map these, so fall
570 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
574 break; /* SocketOptionLevel_IPv6 */
577 case SocketOptionLevel_Tcp:
579 *system_level = SOL_TCP;
582 static int cached = 0;
586 struct protoent *pent;
588 pent = getprotobyname ("TCP");
589 proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
593 *system_level = proto;
595 #endif /* HAVE_SOL_TCP */
598 case SocketOptionName_NoDelay:
599 *system_name = TCP_NODELAY;
602 /* The documentation is talking complete
603 * bollocks here: rfc-1222 is titled
604 * 'Advancing the NSFNET Routing Architecture'
605 * and doesn't mention either of the words
606 * "expedite" or "urgent".
608 case SocketOptionName_BsdUrgent:
609 case SocketOptionName_Expedited:
612 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
617 case SocketOptionLevel_Udp:
618 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
621 case SocketOptionName_NoChecksum:
622 case SocketOptionName_ChecksumCoverage:
624 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
631 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
638 static MonoImage *get_socket_assembly (void)
640 static const char *version = NULL;
641 static gboolean moonlight;
642 static MonoImage *socket_assembly = NULL;
644 if (version == NULL) {
645 version = mono_get_runtime_info ()->framework_version;
646 moonlight = !strcmp (version, "2.1");
649 if (socket_assembly == NULL) {
651 socket_assembly = mono_image_loaded ("System.Net");
652 if (!socket_assembly) {
653 MonoAssembly *sa = mono_assembly_open ("System.Net.dll", NULL);
656 g_assert_not_reached ();
658 socket_assembly = mono_assembly_get_image (sa);
662 socket_assembly = mono_image_loaded ("System");
663 if (!socket_assembly) {
664 MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
667 g_assert_not_reached ();
669 socket_assembly = mono_assembly_get_image (sa);
675 return(socket_assembly);
679 static gint32 get_family_hint(void)
681 MonoDomain *domain = mono_domain_get ();
683 if (!domain->inet_family_hint) {
684 MonoClass *socket_class;
685 MonoClassField *ipv6_field, *ipv4_field;
686 gint32 ipv6_enabled = -1, ipv4_enabled = -1;
689 socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
690 ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
691 ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
692 vtable = mono_class_vtable (mono_domain_get (), socket_class);
694 mono_runtime_class_init (vtable);
696 mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
697 mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
699 mono_domain_lock (domain);
700 if (ipv4_enabled == 1 && ipv6_enabled == 1) {
701 domain->inet_family_hint = 1;
702 } else if (ipv4_enabled == 1) {
703 domain->inet_family_hint = 2;
705 domain->inet_family_hint = 3;
707 mono_domain_unlock (domain);
709 switch (domain->inet_family_hint) {
710 case 1: return PF_UNSPEC;
711 case 2: return PF_INET;
712 case 3: return PF_INET6;
719 gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
730 sock_family=convert_family(family);
731 if(sock_family==-1) {
732 *error = WSAEAFNOSUPPORT;
736 sock_proto=convert_proto(proto);
738 *error = WSAEPROTONOSUPPORT;
742 sock_type=convert_type(type);
744 *error = WSAESOCKTNOSUPPORT;
748 sock = _wapi_socket (sock_family, sock_type, sock_proto,
749 NULL, 0, WSA_FLAG_OVERLAPPED);
751 if(sock==INVALID_SOCKET) {
752 *error = WSAGetLastError ();
756 if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
757 return (GUINT_TO_POINTER (sock));
761 if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
762 return (GUINT_TO_POINTER (sock));
766 return(GUINT_TO_POINTER (sock));
769 /* FIXME: the SOCKET parameter (here and in other functions in this
770 * file) is really an IntPtr which needs to be converted to a guint32.
772 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
777 LOGDEBUG (g_message ("%s: closing 0x%x", __func__, sock));
781 /* Clear any pending work item from this socket if the underlying
782 * polling system does not notify when the socket is closed */
783 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
787 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
791 LOGDEBUG (g_message("%s: returning %d", __func__, WSAGetLastError()));
793 return(WSAGetLastError());
796 gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
806 ret=ioctlsocket(sock, FIONREAD, &amount);
807 if(ret==SOCKET_ERROR) {
808 *error = WSAGetLastError ();
815 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
826 * block == TRUE/FALSE means we will block/not block.
827 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
831 ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
832 if(ret==SOCKET_ERROR) {
833 *error = WSAGetLastError ();
837 gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
846 #ifdef PLATFORM_WIN32
848 MonoInternalThread* curthread = mono_thread_internal_current ();
849 curthread->interrupt_on_stop = (gpointer)TRUE;
850 newsock = _wapi_accept (sock, NULL, 0);
851 curthread->interrupt_on_stop = (gpointer)FALSE;
854 newsock = _wapi_accept (sock, NULL, 0);
856 if(newsock==INVALID_SOCKET) {
857 *error = WSAGetLastError ();
861 return(GUINT_TO_POINTER (newsock));
864 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
874 ret = _wapi_listen (sock, backlog);
875 if(ret==SOCKET_ERROR) {
876 *error = WSAGetLastError ();
880 static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
881 int sa_size, gint32 *error)
883 MonoDomain *domain = mono_domain_get ();
884 MonoObject *sockaddr_obj;
885 MonoClass *sockaddr_class;
886 MonoClassField *field;
888 MonoAddressFamily family;
890 /* Build a System.Net.SocketAddress object instance */
891 sockaddr_class=mono_class_from_name_cached (get_socket_assembly (), "System.Net", "SocketAddress");
892 sockaddr_obj=mono_object_new(domain, sockaddr_class);
894 /* Locate the SocketAddress data buffer in the object */
895 field=mono_class_get_field_from_name_cached (sockaddr_class, "data");
897 /* Make sure there is space for the family and size bytes */
899 if (saddr->sa_family == AF_UNIX) {
900 /* sa_len includes the entire sockaddr size, so we don't need the
901 * N bytes (sizeof (unsigned short)) of the family. */
902 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size);
906 /* May be the +2 here is too conservative, as sa_len returns
907 * the length of the entire sockaddr_in/in6, including
908 * sizeof (unsigned short) of the family */
909 data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
912 /* The data buffer is laid out as follows:
913 * bytes 0 and 1 are the address family
914 * bytes 2 and 3 are the port info
915 * the rest is the address info
918 family=convert_to_mono_family(saddr->sa_family);
919 if(family==AddressFamily_Unknown) {
920 *error = WSAEAFNOSUPPORT;
924 mono_array_set(data, guint8, 0, family & 0x0FF);
925 mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
927 if(saddr->sa_family==AF_INET) {
928 struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
929 guint16 port=ntohs(sa_in->sin_port);
930 guint32 address=ntohl(sa_in->sin_addr.s_addr);
933 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
936 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
937 mono_array_set(data, guint8, 3, (port) & 0xff);
938 mono_array_set(data, guint8, 4, (address>>24) & 0xff);
939 mono_array_set(data, guint8, 5, (address>>16) & 0xff);
940 mono_array_set(data, guint8, 6, (address>>8) & 0xff);
941 mono_array_set(data, guint8, 7, (address) & 0xff);
943 mono_field_set_value (sockaddr_obj, field, data);
945 return(sockaddr_obj);
947 } else if (saddr->sa_family == AF_INET6) {
948 struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
951 guint16 port=ntohs(sa_in->sin6_port);
954 mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
957 mono_array_set(data, guint8, 2, (port>>8) & 0xff);
958 mono_array_set(data, guint8, 3, (port) & 0xff);
960 for(i=0; i<16; i++) {
961 mono_array_set(data, guint8, 8+i,
962 sa_in->sin6_addr.s6_addr[i]);
965 mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
966 mono_array_set(data, guint8, 25,
967 (sa_in->sin6_scope_id >> 8) & 0xff);
968 mono_array_set(data, guint8, 26,
969 (sa_in->sin6_scope_id >> 16) & 0xff);
970 mono_array_set(data, guint8, 27,
971 (sa_in->sin6_scope_id >> 24) & 0xff);
973 mono_field_set_value (sockaddr_obj, field, data);
975 return(sockaddr_obj);
978 } else if (saddr->sa_family == AF_UNIX) {
981 for (i = 0; i < sa_size; i++) {
982 mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
985 mono_field_set_value (sockaddr_obj, field, data);
990 *error = WSAEAFNOSUPPORT;
995 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
997 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1001 MONO_ARCH_SAVE_REGS;
1006 ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
1008 if(ret==SOCKET_ERROR) {
1009 *error = WSAGetLastError ();
1013 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)));
1015 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1019 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
1021 gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
1025 MONO_ARCH_SAVE_REGS;
1030 ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
1032 if(ret==SOCKET_ERROR) {
1033 *error = WSAGetLastError ();
1037 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)));
1039 return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
1043 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
1047 MonoClassField *field;
1052 /* Dig the SocketAddress data buffer out of the object */
1053 field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
1054 data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
1056 /* The data buffer is laid out as follows:
1057 * byte 0 is the address family low byte
1058 * byte 1 is the address family high byte
1060 * bytes 2 and 3 are the port info
1061 * the rest is the address info
1063 * the rest is the file name
1065 len = mono_array_length (data);
1067 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1070 family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
1071 if (family == AF_INET) {
1072 struct sockaddr_in *sa;
1077 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1080 sa = g_new0 (struct sockaddr_in, 1);
1081 port = (mono_array_get (data, guint8, 2) << 8) +
1082 mono_array_get (data, guint8, 3);
1083 address = (mono_array_get (data, guint8, 4) << 24) +
1084 (mono_array_get (data, guint8, 5) << 16 ) +
1085 (mono_array_get (data, guint8, 6) << 8) +
1086 mono_array_get (data, guint8, 7);
1088 sa->sin_family = family;
1089 sa->sin_addr.s_addr = htonl (address);
1090 sa->sin_port = htons (port);
1092 *sa_size = sizeof(struct sockaddr_in);
1093 return((struct sockaddr *)sa);
1096 } else if (family == AF_INET6) {
1097 struct sockaddr_in6 *sa;
1103 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1106 sa = g_new0 (struct sockaddr_in6, 1);
1107 port = mono_array_get (data, guint8, 3) +
1108 (mono_array_get (data, guint8, 2) << 8);
1109 scopeid = mono_array_get (data, guint8, 24) +
1110 (mono_array_get (data, guint8, 25) << 8) +
1111 (mono_array_get (data, guint8, 26) << 16) +
1112 (mono_array_get (data, guint8, 27) << 24);
1114 sa->sin6_family = family;
1115 sa->sin6_port = htons (port);
1116 sa->sin6_scope_id = scopeid;
1118 for(i=0; i<16; i++) {
1119 sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
1122 *sa_size = sizeof(struct sockaddr_in6);
1123 return((struct sockaddr *)sa);
1125 #ifdef HAVE_SYS_UN_H
1126 } else if (family == AF_UNIX) {
1127 struct sockaddr_un *sock_un;
1130 /* Need a byte for the '\0' terminator/prefix, and the first
1131 * two bytes hold the SocketAddress family
1133 if (len - 2 >= MONO_SIZEOF_SUNPATH) {
1134 mono_raise_exception (mono_get_exception_index_out_of_range ());
1137 sock_un = g_new0 (struct sockaddr_un, 1);
1139 sock_un->sun_family = family;
1140 for (i = 0; i < len - 2; i++) {
1141 sock_un->sun_path [i] = mono_array_get (data, guint8,
1147 return (struct sockaddr *)sock_un;
1150 *error = WSAEAFNOSUPPORT;
1155 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1157 struct sockaddr *sa;
1161 MONO_ARCH_SAVE_REGS;
1165 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1170 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)));
1172 ret = _wapi_bind (sock, sa, sa_size);
1173 if(ret==SOCKET_ERROR) {
1174 *error = WSAGetLastError ();
1187 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
1188 gint timeout, gint32 *error)
1190 MonoInternalThread *thread = NULL;
1196 MONO_ARCH_SAVE_REGS;
1198 pfds = g_new0 (mono_pollfd, 1);
1199 pfds[0].fd = GPOINTER_TO_INT (sock);
1200 pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
1201 (mode == SelectModeWrite) ? MONO_POLLOUT :
1202 (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
1204 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1205 start = time (NULL);
1209 ret = mono_poll (pfds, 1, timeout);
1210 if (timeout > 0 && ret < 0) {
1212 int sec = time (NULL) - start;
1214 timeout -= sec * 1000;
1222 if (ret == -1 && errno == EINTR) {
1225 if (thread == NULL) {
1226 thread = mono_thread_internal_current ();
1229 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1235 /* Suspend requested? */
1236 mono_thread_interruption_checkpoint ();
1240 } while (ret == -1 && errno == EINTR);
1243 #ifdef PLATFORM_WIN32
1244 *error = WSAGetLastError ();
1246 *error = errno_to_WSA (errno, __func__);
1261 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
1263 struct sockaddr *sa;
1267 MONO_ARCH_SAVE_REGS;
1271 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
1276 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)));
1278 ret = _wapi_connect (sock, sa, sa_size);
1279 if(ret==SOCKET_ERROR) {
1280 *error = WSAGetLastError ();
1286 /* These #defines from mswsock.h from wine. Defining them here allows
1287 * us to build this file on a mingw box that doesn't know the magic
1288 * numbers, but still run on a newer windows box that does.
1290 #ifndef WSAID_DISCONNECTEX
1291 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1292 typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
1295 #ifndef WSAID_TRANSMITFILE
1296 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1297 typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
1300 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
1303 glong output_bytes = 0;
1304 GUID disco_guid = WSAID_DISCONNECTEX;
1305 GUID trans_guid = WSAID_TRANSMITFILE;
1306 LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
1307 LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
1310 MONO_ARCH_SAVE_REGS;
1314 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
1316 /* I _think_ the extension function pointers need to be looked
1317 * up for each socket. FIXME: check the best way to store
1318 * pointers to functions in managed objects that still works
1319 * on 64bit platforms.
1321 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1322 (void *)&disco_guid, sizeof(GUID),
1323 (void *)&_wapi_disconnectex, sizeof(void *),
1324 &output_bytes, NULL, NULL);
1326 /* make sure that WSAIoctl didn't put crap in the
1329 _wapi_disconnectex = NULL;
1331 /* Look up the TransmitFile extension function pointer
1332 * instead of calling TransmitFile() directly, because
1333 * apparently "Several of the extension functions have
1334 * been available since WinSock 1.1 and are exported
1335 * from MSWsock.dll, however it's not advisable to
1336 * link directly to this dll as this ties you to the
1337 * Microsoft WinSock provider. A provider neutral way
1338 * of accessing these extension functions is to load
1339 * them dynamically via WSAIoctl using the
1340 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1341 * should, theoretically, allow you to access these
1342 * functions from any provider that supports them..."
1343 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1345 ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
1346 (void *)&trans_guid, sizeof(GUID),
1347 (void *)&_wapi_transmitfile, sizeof(void *),
1348 &output_bytes, NULL, NULL);
1350 _wapi_transmitfile = NULL;
1354 if (_wapi_disconnectex != NULL) {
1355 bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
1356 } else if (_wapi_transmitfile != NULL) {
1357 bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
1358 TF_DISCONNECT | TF_REUSE_SOCKET);
1360 *error = ERROR_NOT_SUPPORTED;
1364 if (bret == FALSE) {
1365 *error = WSAGetLastError ();
1369 gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1376 MONO_ARCH_SAVE_REGS;
1380 alen = mono_array_length (buffer);
1381 if (offset > alen - count) {
1385 buf=mono_array_addr(buffer, guchar, offset);
1387 recvflags = convert_socketflags (flags);
1388 if (recvflags == -1) {
1389 *error = WSAEOPNOTSUPP;
1393 #ifdef PLATFORM_WIN32
1395 MonoInternalThread* curthread = mono_thread_internal_current ();
1396 curthread->interrupt_on_stop = (gpointer)TRUE;
1397 ret = _wapi_recv (sock, buf, count, recvflags);
1398 curthread->interrupt_on_stop = (gpointer)FALSE;
1401 ret = _wapi_recv (sock, buf, count, recvflags);
1404 if(ret==SOCKET_ERROR) {
1405 *error = WSAGetLastError ();
1412 gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1417 DWORD recvflags = 0;
1419 MONO_ARCH_SAVE_REGS;
1423 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1424 count = mono_array_length (buffers);
1426 recvflags = convert_socketflags (flags);
1427 if (recvflags == -1) {
1428 *error = WSAEOPNOTSUPP;
1432 ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
1433 if (ret == SOCKET_ERROR) {
1434 *error = WSAGetLastError ();
1441 gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
1447 struct sockaddr *sa;
1450 MONO_ARCH_SAVE_REGS;
1454 alen = mono_array_length (buffer);
1455 if (offset > alen - count) {
1459 sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
1464 buf=mono_array_addr(buffer, guchar, offset);
1466 recvflags = convert_socketflags (flags);
1467 if (recvflags == -1) {
1468 *error = WSAEOPNOTSUPP;
1472 ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
1473 if(ret==SOCKET_ERROR) {
1475 *error = WSAGetLastError ();
1479 /* If we didn't get a socket size, then we're probably a
1480 * connected connection-oriented socket and the stack hasn't
1481 * returned the remote address. All we can do is return null.
1484 *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
1493 gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
1500 MONO_ARCH_SAVE_REGS;
1504 alen = mono_array_length (buffer);
1505 if (offset > alen - count) {
1509 LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
1511 buf=mono_array_addr(buffer, guchar, offset);
1513 LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
1515 sendflags = convert_socketflags (flags);
1516 if (sendflags == -1) {
1517 *error = WSAEOPNOTSUPP;
1521 ret = _wapi_send (sock, buf, count, sendflags);
1522 if(ret==SOCKET_ERROR) {
1523 *error = WSAGetLastError ();
1530 gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
1535 DWORD sendflags = 0;
1537 MONO_ARCH_SAVE_REGS;
1541 wsabufs = mono_array_addr (buffers, WSABUF, 0);
1542 count = mono_array_length (buffers);
1544 sendflags = convert_socketflags (flags);
1545 if (sendflags == -1) {
1546 *error = WSAEOPNOTSUPP;
1550 ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
1551 if (ret == SOCKET_ERROR) {
1552 *error = WSAGetLastError ();
1559 gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
1565 struct sockaddr *sa;
1568 MONO_ARCH_SAVE_REGS;
1572 alen = mono_array_length (buffer);
1573 if (offset > alen - count) {
1577 sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
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_sendto (sock, buf, count, sendflags, sa, sa_size);
1595 if(ret==SOCKET_ERROR) {
1596 *error = WSAGetLastError ();
1604 static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
1607 MonoClassField *field;
1609 field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
1610 sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
1615 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1616 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
1618 MonoInternalThread *thread = NULL;
1625 MonoClass *sock_arr_class;
1628 mono_array_size_t socks_size;
1630 MONO_ARCH_SAVE_REGS;
1632 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1633 count = mono_array_length (*sockets);
1634 nfds = count - 3; /* NULL separators */
1635 pfds = g_new0 (mono_pollfd, nfds);
1637 for (i = 0; i < count; i++) {
1638 obj = mono_array_get (*sockets, MonoObject *, i);
1645 /* The socket array was bogus */
1651 pfds [idx].fd = Socket_to_SOCKET (obj);
1652 pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
1656 timeout = (timeout >= 0) ? (timeout / 1000) : -1;
1657 start = time (NULL);
1660 ret = mono_poll (pfds, nfds, timeout);
1661 if (timeout > 0 && ret < 0) {
1663 int sec = time (NULL) - start;
1665 timeout -= sec * 1000;
1671 if (ret == -1 && errno == EINTR) {
1674 thread = mono_thread_internal_current ();
1676 leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
1683 /* Suspend requested? */
1684 mono_thread_interruption_checkpoint ();
1688 } while (ret == -1 && errno == EINTR);
1691 #ifdef PLATFORM_WIN32
1692 *error = WSAGetLastError ();
1694 *error = errno_to_WSA (errno, __func__);
1706 sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
1707 socks_size = ((mono_array_size_t)ret) + 3; /* space for the NULL delimiters */
1708 socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
1711 for (i = 0; i < count && ret > 0; i++) {
1714 obj = mono_array_get (*sockets, MonoObject *, i);
1721 pfd = &pfds [i - mode];
1722 if (pfd->revents == 0)
1726 if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
1727 mono_array_setref (socks, idx++, obj);
1728 } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
1729 mono_array_setref (socks, idx++, obj);
1730 } else if ((pfd->revents & POLL_ERRORS) != 0) {
1731 mono_array_setref (socks, idx++, obj);
1739 static MonoObject* int_to_object (MonoDomain *domain, int val)
1741 return mono_value_box (domain, mono_get_int32_class (), &val);
1745 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
1751 socklen_t valsize=sizeof(val);
1752 struct linger linger;
1753 socklen_t lingersize=sizeof(linger);
1755 socklen_t time_ms_size = sizeof (time_ms);
1758 socklen_t credsize = sizeof(cred);
1760 MonoDomain *domain=mono_domain_get();
1762 MonoClass *obj_class;
1763 MonoClassField *field;
1765 MONO_ARCH_SAVE_REGS;
1769 ret=convert_sockopt_level_and_name(level, name, &system_level,
1772 *error = WSAENOPROTOOPT;
1776 *obj_val = int_to_object (domain, 0);
1780 /* No need to deal with MulticastOption names here, because
1781 * you cant getsockopt AddMembership or DropMembership (the
1782 * int getsockopt will error, causing an exception)
1785 case SocketOptionName_Linger:
1786 case SocketOptionName_DontLinger:
1787 ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
1791 case SocketOptionName_SendTimeout:
1792 case SocketOptionName_ReceiveTimeout:
1793 ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
1797 case SocketOptionName_PeerCred:
1798 ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
1804 ret = _wapi_getsockopt (sock, system_level, system_name, &val,
1808 if(ret==SOCKET_ERROR) {
1809 *error = WSAGetLastError ();
1814 case SocketOptionName_Linger:
1815 /* build a System.Net.Sockets.LingerOption */
1816 obj_class=mono_class_from_name(get_socket_assembly (),
1817 "System.Net.Sockets",
1819 obj=mono_object_new(domain, obj_class);
1821 /* Locate and set the fields "bool enabled" and "int
1824 field=mono_class_get_field_from_name(obj_class, "enabled");
1825 *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
1827 field=mono_class_get_field_from_name(obj_class, "seconds");
1828 *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
1832 case SocketOptionName_DontLinger:
1833 /* construct a bool int in val - true if linger is off */
1834 obj = int_to_object (domain, !linger.l_onoff);
1837 case SocketOptionName_SendTimeout:
1838 case SocketOptionName_ReceiveTimeout:
1839 obj = int_to_object (domain, time_ms);
1843 case SocketOptionName_PeerCred:
1845 /* build a Mono.Posix.PeerCred+PeerCredData if
1848 static MonoImage *mono_posix_image = NULL;
1849 MonoPeerCredData *cred_data;
1851 if (mono_posix_image == NULL) {
1852 mono_posix_image=mono_image_loaded ("Mono.Posix");
1853 if (!mono_posix_image) {
1854 MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
1856 *error = WSAENOPROTOOPT;
1859 mono_posix_image = mono_assembly_get_image (sa);
1864 obj_class = mono_class_from_name(mono_posix_image,
1867 obj = mono_object_new(domain, obj_class);
1868 cred_data = (MonoPeerCredData *)obj;
1869 cred_data->pid = cred.pid;
1870 cred_data->uid = cred.uid;
1871 cred_data->gid = cred.gid;
1877 obj = int_to_object (domain, val);
1883 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
1891 MONO_ARCH_SAVE_REGS;
1895 ret=convert_sockopt_level_and_name(level, name, &system_level,
1898 *error = WSAENOPROTOOPT;
1904 valsize=mono_array_length(*byte_val);
1905 buf=mono_array_addr(*byte_val, guchar, 0);
1907 ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
1908 if(ret==SOCKET_ERROR) {
1909 *error = WSAGetLastError ();
1913 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1914 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
1916 struct in_addr inaddr;
1917 MonoClassField *field;
1919 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
1921 /* No idea why .net uses a 64bit type to hold a 32bit value...
1923 * Internal value of IPAddess is in little-endian order
1925 inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
1932 static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
1934 struct in6_addr in6addr;
1935 MonoClassField *field;
1939 field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
1940 data=*(MonoArray **)(((char *)ipaddr) + field->offset);
1942 /* Solaris has only the 8 bit version. */
1944 for(i=0; i<8; i++) {
1945 guint16 s = mono_array_get (data, guint16, i);
1946 in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
1947 in6addr.s6_addr[2 * i + 1] = s & 0xff;
1951 in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
1955 #endif /* AF_INET6 */
1957 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)
1959 struct linger linger;
1969 #ifdef HAVE_SOL_IPV6
1970 sol_ipv6 = SOL_IPV6;
1973 struct protoent *pent;
1974 pent = getprotobyname ("ipv6");
1975 sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
1983 struct protoent *pent;
1984 pent = getprotobyname ("ip");
1985 sol_ip = (pent != NULL) ? pent->p_proto : 0;
1988 #endif /* AF_INET6 */
1990 MONO_ARCH_SAVE_REGS;
1992 ret=convert_sockopt_level_and_name(level, name, &system_level,
1995 *error = WSAENOPROTOOPT;
2002 /* Only one of obj_val, byte_val or int_val has data */
2004 MonoClassField *field;
2008 case SocketOptionName_Linger:
2009 /* Dig out "bool enabled" and "int seconds"
2012 field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
2013 linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
2014 field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
2015 linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
2017 valsize=sizeof(linger);
2018 ret = _wapi_setsockopt (sock, system_level,
2019 system_name, &linger, valsize);
2021 case SocketOptionName_AddMembership:
2022 case SocketOptionName_DropMembership:
2023 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2025 MonoObject *address = NULL;
2028 if(system_level == sol_ipv6) {
2029 struct ipv6_mreq mreq6;
2034 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2035 address = *(gpointer *)(((char *)obj_val) + field->offset);
2038 mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
2041 field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
2042 mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
2044 ret = _wapi_setsockopt (sock, system_level,
2045 system_name, &mreq6,
2047 } else if(system_level == sol_ip)
2048 #endif /* AF_INET6 */
2050 #ifdef HAVE_STRUCT_IP_MREQN
2051 struct ip_mreqn mreq = {{0}};
2053 struct ip_mreq mreq = {{0}};
2054 #endif /* HAVE_STRUCT_IP_MREQN */
2056 /* pain! MulticastOption holds two IPAddress
2057 * members, so I have to dig the value out of
2060 field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
2061 address = *(gpointer *)(((char *)obj_val) + field->offset);
2063 /* address might not be defined and if so, set the address to ADDR_ANY.
2066 mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
2069 field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
2070 address = *(gpointer *)(((char *)obj_val) + field->offset);
2072 #ifdef HAVE_STRUCT_IP_MREQN
2074 mreq.imr_address = ipaddress_to_struct_in_addr (address);
2078 mreq.imr_interface = ipaddress_to_struct_in_addr (address);
2080 #endif /* HAVE_STRUCT_IP_MREQN */
2082 ret = _wapi_setsockopt (sock, system_level,
2088 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2090 /* Cause an exception to be thrown */
2094 } else if (byte_val!=NULL) {
2095 int valsize = mono_array_length (byte_val);
2096 guchar *buf = mono_array_addr (byte_val, guchar, 0);
2099 case SocketOptionName_DontLinger:
2101 linger.l_onoff = (*buf) ? 0 : 1;
2102 linger.l_linger = 0;
2103 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2109 ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
2113 /* ReceiveTimeout/SendTimeout get here */
2115 case SocketOptionName_DontLinger:
2116 linger.l_onoff = !int_val;
2117 linger.l_linger = 0;
2118 ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
2120 case SocketOptionName_DontFragment:
2121 #ifdef HAVE_IP_MTU_DISCOVER
2122 /* Fiddle with the value slightly if we're
2126 int_val = IP_PMTUDISC_DO;
2132 ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
2136 if(ret==SOCKET_ERROR) {
2137 *error = WSAGetLastError ();
2141 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
2147 MONO_ARCH_SAVE_REGS;
2151 /* Currently, the values for how (recv=0, send=1, both=2) match
2154 ret = _wapi_shutdown (sock, how);
2155 if(ret==SOCKET_ERROR) {
2156 *error = WSAGetLastError ();
2161 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
2163 MonoArray *output, gint32 *error)
2165 glong output_bytes = 0;
2166 gchar *i_buffer, *o_buffer;
2170 MONO_ARCH_SAVE_REGS;
2174 if (code == FIONBIO) {
2175 /* Invalid command. Must use Socket.Blocking */
2179 if (input == NULL) {
2183 i_buffer = mono_array_addr (input, gchar, 0);
2184 i_len = mono_array_length (input);
2187 if (output == NULL) {
2191 o_buffer = mono_array_addr (output, gchar, 0);
2192 o_len = mono_array_length (output);
2195 ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
2196 if (ret == SOCKET_ERROR) {
2197 *error = WSAGetLastError ();
2201 return (gint) output_bytes;
2204 #ifdef HAVE_SIOCGIFCONF
2206 is_loopback (int family, void *ad)
2208 char *ptr = (char *) ad;
2210 if (family == AF_INET) {
2211 return (ptr [0] == 127);
2215 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
2222 get_local_ips (int family, int *nips)
2224 int addr_size, offset, fd, i, count;
2225 int max_ifaces = 50; /* 50 interfaces should be enough... */
2228 struct ifreq iflags;
2229 char *result, *tmp_ptr;
2230 gboolean ignore_loopback = FALSE;
2233 if (family == AF_INET) {
2234 addr_size = sizeof (struct in_addr);
2235 offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
2237 } else if (family == AF_INET6) {
2238 addr_size = sizeof (struct in6_addr);
2239 offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
2245 fd = socket (family, SOCK_STREAM, 0);
2247 ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
2248 ifc.ifc_buf = g_malloc (ifc.ifc_len);
2249 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
2251 g_free (ifc.ifc_buf);
2255 count = ifc.ifc_len / sizeof (struct ifreq);
2258 g_free (ifc.ifc_buf);
2263 for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
2264 strcpy (iflags.ifr_name, ifr->ifr_name);
2265 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
2269 if ((iflags.ifr_flags & IFF_UP) == 0) {
2270 ifr->ifr_name [0] = '\0';
2274 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
2275 ignore_loopback = TRUE;
2280 result = g_malloc (addr_size * count);
2282 for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
2283 if (ifr->ifr_name [0] == '\0') {
2288 if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
2293 memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
2294 tmp_ptr += addr_size;
2297 g_free (ifc.ifc_buf);
2302 get_local_ips (int family, int *nips)
2308 #endif /* HAVE_SIOCGIFCONF */
2311 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
2312 MonoArray **h_aliases,
2313 MonoArray **h_addr_list,
2314 gboolean add_local_ips)
2316 MonoDomain *domain = mono_domain_get ();
2318 struct in_addr *local_in = NULL;
2322 if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
2326 *h_name=mono_string_new(domain, he->h_name);
2328 while(he->h_aliases[i]!=NULL) {
2332 *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
2334 while(he->h_aliases[i]!=NULL) {
2337 alias=mono_string_new(domain, he->h_aliases[i]);
2338 mono_array_setref (*h_aliases, i, alias);
2341 } else if (!add_local_ips) {
2345 if (add_local_ips) {
2346 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2348 *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
2349 for (i = 0; i < nlocal_in; i++) {
2350 MonoString *addr_string;
2351 char addr [16], *ptr;
2353 ptr = (char *) &local_in [i];
2354 g_snprintf(addr, 16, "%u.%u.%u.%u",
2355 (unsigned char) ptr [0],
2356 (unsigned char) ptr [1],
2357 (unsigned char) ptr [2],
2358 (unsigned char) ptr [3]);
2360 addr_string = mono_string_new (domain, addr);
2361 mono_array_setref (*h_addr_list, i, addr_string);
2366 } else if (he == NULL) {
2367 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2368 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2369 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2374 if (nlocal_in == 0 && he != NULL) {
2376 while (he->h_addr_list[i]!=NULL) {
2380 *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
2382 while(he->h_addr_list[i]!=NULL) {
2383 MonoString *addr_string;
2386 g_snprintf(addr, 16, "%u.%u.%u.%u",
2387 (unsigned char)he->h_addr_list[i][0],
2388 (unsigned char)he->h_addr_list[i][1],
2389 (unsigned char)he->h_addr_list[i][2],
2390 (unsigned char)he->h_addr_list[i][3]);
2392 addr_string=mono_string_new(domain, addr);
2393 mono_array_setref (*h_addr_list, i, addr_string);
2401 static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
2402 MonoArray **h_aliases,
2403 MonoArray **h_addr_list)
2405 MonoDomain *domain = mono_domain_get ();
2407 *h_name=mono_string_new(domain, addr);
2408 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2409 *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
2410 mono_array_setref (*h_addr_list, 0, *h_name);
2416 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2417 static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
2418 MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
2420 MonoDomain *domain = mono_domain_get ();
2421 int i, host_count, host_index, family_hint;
2422 struct in_addr *local_in = NULL;
2424 struct in6_addr *local_in6 = NULL;
2426 gboolean from_local = FALSE;
2428 family_hint = get_family_hint ();
2431 * Check if address length and family are correct
2433 if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
2437 if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
2442 * Get the aliases and host name from he1 or he2 whichever is
2443 * not null, if he1 is not null then take aliases from he1
2445 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2446 family_hint == PF_INET)) {
2447 *h_name=mono_string_new (domain, he1->h_name);
2450 while(he1->h_aliases[i]!=NULL) {
2454 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2457 while(he1->h_aliases[i]!=NULL) {
2460 alias=mono_string_new (domain, he1->h_aliases[i]);
2461 mono_array_setref (*h_aliases, i, alias);
2464 } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
2465 family_hint == PF_INET6)) {
2466 *h_name=mono_string_new (domain, he2->h_name);
2469 while(he2->h_aliases [i] != NULL) {
2473 *h_aliases=mono_array_new (domain, mono_get_string_class (),
2476 while(he2->h_aliases[i]!=NULL) {
2479 alias=mono_string_new (domain, he2->h_aliases[i]);
2480 mono_array_setref (*h_aliases, i, alias);
2483 } else if (!add_local_ips) {
2488 * Count the number of addresses in he1 + he2
2491 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2492 family_hint == PF_INET)) {
2494 while(he1->h_addr_list[i]!=NULL) {
2500 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2501 family_hint == PF_INET6)) {
2503 while(he2->h_addr_list[i]!=NULL) {
2513 if (add_local_ips) {
2514 if (family_hint == PF_UNSPEC || family_hint == PF_INET)
2515 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2517 if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
2518 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2520 if (nlocal_in || nlocal_in6) {
2522 *h_addr_list = mono_array_new (domain, mono_get_string_class (),
2523 nlocal_in + nlocal_in6);
2527 for (n = 0; n < nlocal_in6; n++) {
2528 MonoString *addr_string;
2530 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2532 ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
2535 addr_string = mono_string_new (domain, addr);
2536 mono_array_setref (*h_addr_list, host_index, addr_string);
2544 for (n = 0; n < nlocal_in; n++) {
2545 MonoString *addr_string;
2547 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2549 ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
2552 addr_string = mono_string_new (domain, addr);
2553 mono_array_setref (*h_addr_list, host_index, addr_string);
2561 } else if (he1 == NULL && he2 == NULL) {
2562 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2563 *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
2564 mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
2574 *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
2576 if (he2 != NULL && (family_hint == PF_UNSPEC ||
2577 family_hint == PF_INET6)) {
2579 while(he2->h_addr_list[i] != NULL) {
2580 MonoString *addr_string;
2582 char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2584 ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
2588 addr_string = mono_string_new (domain, addr);
2589 mono_array_setref (*h_addr_list, host_index, addr_string);
2596 if (he1 != NULL && (family_hint == PF_UNSPEC ||
2597 family_hint == PF_INET)) {
2599 while(he1->h_addr_list[i] != NULL) {
2600 MonoString *addr_string;
2602 char addr[16]; /* INET_ADDRSTRLEN == 16 */
2604 ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
2608 addr_string=mono_string_new (domain, addr);
2609 mono_array_setref (*h_addr_list, host_index, addr_string);
2620 #if defined(AF_INET6)
2622 addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
2623 MonoArray **h_aliases,
2624 MonoArray **h_addr_list,
2625 gboolean add_local_ips)
2628 struct addrinfo *ai = NULL;
2629 struct in_addr *local_in = NULL;
2631 struct in6_addr *local_in6 = NULL;
2635 MonoDomain *domain = mono_domain_get ();
2638 *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
2639 if (add_local_ips) {
2640 local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
2641 local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
2642 if (nlocal_in || nlocal_in6) {
2643 *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
2645 MonoString *addr_string;
2649 for (i = 0; i < nlocal_in; i++) {
2650 inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
2651 addr_string = mono_string_new (domain, addr);
2652 mono_array_setref (*h_addr_list, addr_index, addr_string);
2658 MonoString *addr_string;
2663 for (i = 0; i < nlocal_in6; i++) {
2664 ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
2666 addr_string = mono_string_new (domain, addr);
2667 mono_array_setref (*h_addr_list, addr_index, addr_string);
2676 freeaddrinfo (info);
2685 for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
2686 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2692 *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
2694 for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
2695 MonoString *addr_string;
2697 char buffer [48]; /* Max. size for IPv6 */
2699 if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
2703 if(ai->ai_family == PF_INET) {
2704 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
2706 ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
2710 addr_string=mono_string_new(domain, buffer);
2712 addr_string=mono_string_new(domain, "");
2715 mono_array_setref (*h_addr_list, addr_index, addr_string);
2718 if (ai->ai_canonname != NULL) {
2719 *h_name=mono_string_new(domain, ai->ai_canonname);
2721 *h_name=mono_string_new(domain, buffer);
2737 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2739 gboolean add_local_ips = FALSE;
2740 #ifdef HAVE_SIOCGIFCONF
2741 gchar this_hostname [256];
2743 #if !defined(HAVE_GETHOSTBYNAME2_R)
2744 struct addrinfo *info = NULL, hints;
2747 MONO_ARCH_SAVE_REGS;
2749 hostname=mono_string_to_utf8 (host);
2750 if (*hostname == '\0')
2751 add_local_ips = TRUE;
2752 #ifdef HAVE_SIOCGIFCONF
2753 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2754 if (!strcmp (hostname, this_hostname))
2755 add_local_ips = TRUE;
2759 memset(&hints, 0, sizeof(hints));
2760 hints.ai_family = get_family_hint ();
2761 hints.ai_socktype = SOCK_STREAM;
2762 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
2764 if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
2770 return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
2772 struct hostent he1,*hp1, he2, *hp2;
2773 int buffer_size1, buffer_size2;
2774 char *buffer1, *buffer2;
2776 gboolean return_value;
2779 MONO_ARCH_SAVE_REGS;
2781 hostname=mono_string_to_utf8 (host);
2782 if (*hostname == '\0')
2783 add_local_ips = TRUE;
2785 #ifdef HAVE_SIOCGIFCONF
2786 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2787 if (!strcmp (hostname, this_hostname))
2788 add_local_ips = TRUE;
2794 buffer1 = g_malloc0(buffer_size1);
2795 buffer2 = g_malloc0(buffer_size2);
2799 while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
2800 &hp1, &herr) == ERANGE) {
2802 buffer1 = g_realloc(buffer1, buffer_size1);
2805 if (*hostname && hp1 == NULL)
2807 while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
2808 buffer_size2, &hp2, &herr) == ERANGE) {
2810 buffer2 = g_realloc(buffer2, buffer_size2);
2814 return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
2815 h_addr_list, add_local_ips);
2821 return(return_value);
2822 #endif /* HAVE_GETHOSTBYNAME2_R */
2824 #else /* AF_INET6 */
2825 MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2829 gboolean add_local_ips = FALSE;
2830 #ifdef HAVE_SIOCGIFCONF
2831 gchar this_hostname [256];
2834 MONO_ARCH_SAVE_REGS;
2836 hostname=mono_string_to_utf8(host);
2837 if (*hostname == '\0')
2838 add_local_ips = TRUE;
2839 #ifdef HAVE_SIOCGIFCONF
2840 if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
2841 if (!strcmp (hostname, this_hostname))
2842 add_local_ips = TRUE;
2848 he = _wapi_gethostbyname (hostname);
2851 if (*hostname && he==NULL)
2854 return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
2856 #endif /* AF_INET6 */
2858 #ifndef HAVE_INET_PTON
2860 inet_pton (int family, const char *address, void *inaddrp)
2862 if (family == AF_INET) {
2863 #ifdef HAVE_INET_ATON
2864 struct in_addr inaddr;
2866 if (!inet_aton (address, &inaddr))
2869 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
2872 /* assume the system has inet_addr(), if it doesn't
2873 have that we're pretty much screwed... */
2876 if (!strcmp (address, "255.255.255.255")) {
2877 /* special-case hack */
2878 inaddr = 0xffffffff;
2880 inaddr = inet_addr (address);
2882 #define INADDR_NONE ((in_addr_t) -1)
2884 if (inaddr == INADDR_NONE)
2888 memcpy (inaddrp, &inaddr, sizeof (guint32));
2890 #endif /* HAVE_INET_ATON */
2895 #endif /* !HAVE_INET_PTON */
2897 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
2903 struct sockaddr_in saddr;
2904 struct sockaddr_in6 saddr6;
2905 struct addrinfo *info = NULL, hints;
2907 char hostname[1024] = {0};
2910 struct in_addr inaddr;
2915 v1 = mono_framework_version () == 1;
2917 address = mono_string_to_utf8 (addr);
2920 if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
2921 /* Maybe an ipv6 address */
2922 if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
2928 saddr6.sin6_family = AF_INET6;
2933 saddr.sin_family = AF_INET;
2938 flags = NI_NAMEREQD;
2941 if(family == AF_INET) {
2942 #if HAVE_SOCKADDR_IN_SIN_LEN
2943 saddr.sin_len = sizeof (saddr);
2945 if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
2946 hostname, sizeof(hostname), NULL, 0,
2950 } else if(family == AF_INET6) {
2951 #if HAVE_SOCKADDR_IN6_SIN_LEN
2952 saddr6.sin6_len = sizeof (saddr6);
2954 if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
2955 hostname, sizeof(hostname), NULL, 0,
2961 memset (&hints, 0, sizeof(hints));
2962 hints.ai_family = get_family_hint ();
2963 hints.ai_socktype = SOCK_STREAM;
2964 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
2966 if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
2970 return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
2972 if (inet_pton (AF_INET, address, &inaddr) <= 0) {
2977 if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
2981 ret = ipaddr_to_IPHostEntry (address, h_name,
2982 h_aliases, h_addr_list);
2985 ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
2986 h_addr_list, FALSE);
2994 extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
2996 gchar hostname[256];
2999 MONO_ARCH_SAVE_REGS;
3001 ret = gethostname (hostname, sizeof (hostname));
3006 *h_name=mono_string_new(mono_domain_get (), hostname);
3012 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, WapiTransmitFileFlags flags)
3016 WapiTransmitFileBuffers buffers;
3018 MONO_ARCH_SAVE_REGS;
3020 if (filename == NULL)
3023 file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
3024 if (file == INVALID_HANDLE_VALUE) {
3025 SetLastError (error);
3029 memset (&buffers, 0, sizeof (buffers));
3030 if (pre_buffer != NULL) {
3031 buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
3032 buffers.HeadLength = mono_array_length (pre_buffer);
3034 if (post_buffer != NULL) {
3035 buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
3036 buffers.TailLength = mono_array_length (post_buffer);
3039 if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
3048 void mono_network_init(void)
3053 err=WSAStartup(MAKEWORD(2,0), &wsadata);
3055 g_error("%s: Couldn't initialise networking", __func__);
3059 LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
3060 LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
3063 void mono_network_cleanup(void)