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)
11 * This file has been re-licensed under the MIT License:
12 * http://opensource.org/licenses/MIT
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
18 #ifndef DISABLE_SOCKETS
20 #if defined(__APPLE__) || defined(__FreeBSD__)
21 #define __APPLE_USE_RFC_3542
30 #include <sys/socket.h>
31 #ifdef HAVE_SYS_IOCTL_H
32 #include <sys/ioctl.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
39 #include <arpa/inet.h>
46 #include <sys/types.h>
48 #include <mono/metadata/object.h>
49 #include <mono/io-layer/io-layer.h>
50 #include <mono/metadata/socket-io.h>
51 #include <mono/metadata/exception.h>
52 #include <mono/metadata/assembly.h>
53 #include <mono/metadata/appdomain.h>
54 #include <mono/metadata/file-io.h>
55 #include <mono/metadata/threads.h>
56 #include <mono/metadata/threads-types.h>
57 #include <mono/metadata/threadpool-io.h>
58 #include <mono/utils/mono-poll.h>
59 /* FIXME change this code to not mess so much with the internals */
60 #include <mono/metadata/class-internals.h>
61 #include <mono/metadata/domain-internals.h>
62 #include <mono/utils/mono-threads.h>
63 #include <mono/utils/mono-memory-model.h>
64 #include <mono/utils/networking.h>
65 #include <mono/metadata/w32handle.h>
68 #ifdef HAVE_SYS_TIME_H
71 #ifdef HAVE_SYS_IOCTL_H
72 #include <sys/ioctl.h>
81 #ifdef HAVE_SYS_FILIO_H
82 #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
84 #ifdef HAVE_SYS_SOCKIO_H
85 #include <sys/sockio.h> /* defines SIOCATMARK */
91 #ifdef HAVE_GETIFADDRS
92 // <net/if.h> must be included before <ifaddrs.h>
96 #if defined(_MSC_VER) && G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
100 #include "mono/io-layer/socket-wrappers.h"
102 #include "mono/metadata/socket-io-windows-internals.h"
105 #define LOGDEBUG(...)
106 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
109 abort_syscall (gpointer data
)
111 mono_thread_info_abort_socket_syscall_for_close ((MonoNativeThreadId
) (gsize
) data
);
115 convert_family (MonoAddressFamily mono_family
)
117 switch (mono_family
) {
118 case AddressFamily_Unknown
:
119 case AddressFamily_ImpLink
:
120 case AddressFamily_Pup
:
121 case AddressFamily_Chaos
:
122 case AddressFamily_Iso
:
123 case AddressFamily_Ecma
:
124 case AddressFamily_DataKit
:
125 case AddressFamily_Ccitt
:
126 case AddressFamily_DataLink
:
127 case AddressFamily_Lat
:
128 case AddressFamily_HyperChannel
:
129 case AddressFamily_NetBios
:
130 case AddressFamily_VoiceView
:
131 case AddressFamily_FireFox
:
132 case AddressFamily_Banyan
:
133 case AddressFamily_Atm
:
134 case AddressFamily_Cluster
:
135 case AddressFamily_Ieee12844
:
136 case AddressFamily_NetworkDesigners
:
137 g_warning ("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family
);
139 case AddressFamily_Unspecified
:
141 case AddressFamily_Unix
:
143 case AddressFamily_InterNetwork
:
145 case AddressFamily_AppleTalk
:
147 case AddressFamily_InterNetworkV6
:
149 case AddressFamily_DecNet
:
155 case AddressFamily_Ipx
:
161 case AddressFamily_Sna
:
167 case AddressFamily_Irda
:
174 g_warning ("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family
);
179 static MonoAddressFamily
180 convert_to_mono_family (guint16 af_family
)
184 return AddressFamily_Unspecified
;
186 return AddressFamily_Unix
;
188 return AddressFamily_InterNetwork
;
191 return AddressFamily_Ipx
;
195 return AddressFamily_Sna
;
199 return AddressFamily_DecNet
;
202 return AddressFamily_AppleTalk
;
204 return AddressFamily_InterNetworkV6
;
207 return AddressFamily_Irda
;
210 g_warning ("unknown address family 0x%x", af_family
);
211 return AddressFamily_Unknown
;
216 convert_type (MonoSocketType mono_type
)
219 case SocketType_Stream
:
221 case SocketType_Dgram
:
231 case SocketType_Seqpacket
:
232 return SOCK_SEQPACKET
;
233 case SocketType_Unknown
:
234 g_warning ("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type
);
237 g_warning ("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type
);
243 convert_proto (MonoProtocolType mono_proto
)
245 switch (mono_proto
) {
246 case ProtocolType_IP
:
247 case ProtocolType_IPv6
:
248 case ProtocolType_Icmp
:
249 case ProtocolType_Igmp
:
250 case ProtocolType_Ggp
:
251 case ProtocolType_Tcp
:
252 case ProtocolType_Pup
:
253 case ProtocolType_Udp
:
254 case ProtocolType_Idp
:
255 /* These protocols are known (on my system at least) */
257 case ProtocolType_ND
:
258 case ProtocolType_Raw
:
259 case ProtocolType_Ipx
:
260 case ProtocolType_Spx
:
261 case ProtocolType_SpxII
:
262 case ProtocolType_Unknown
:
263 /* These protocols arent */
264 g_warning ("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto
);
271 /* Convert MonoSocketFlags */
273 convert_socketflags (gint32 sflags
)
278 /* SocketFlags.None */
281 if (sflags
& ~(SocketFlags_OutOfBand
| SocketFlags_MaxIOVectorLength
| SocketFlags_Peek
|
282 SocketFlags_DontRoute
| SocketFlags_Partial
))
283 /* Contains invalid flag values */
286 if (sflags
& SocketFlags_OutOfBand
)
288 if (sflags
& SocketFlags_Peek
)
290 if (sflags
& SocketFlags_DontRoute
)
291 flags
|= MSG_DONTROUTE
;
293 /* Ignore Partial - see bug 349688. Don't return -1, because
294 * according to the comment in that bug ms runtime doesn't for
295 * UDP sockets (this means we will silently ignore it for TCP
299 if (sflags
& SocketFlags_Partial
)
303 /* Don't do anything for MaxIOVectorLength */
304 if (sflags
& SocketFlags_MaxIOVectorLength
)
312 * 0 on success (mapped mono_level and mono_name to system_level and system_name
314 * -2 on non-fatal error (ie, must ignore)
317 convert_sockopt_level_and_name (MonoSocketOptionLevel mono_level
, MonoSocketOptionName mono_name
, int *system_level
, int *system_name
)
319 switch (mono_level
) {
320 case SocketOptionLevel_Socket
:
321 *system_level
= SOL_SOCKET
;
324 case SocketOptionName_DontLinger
:
325 /* This is SO_LINGER, because the setsockopt
326 * internal call maps DontLinger to SO_LINGER
329 *system_name
= SO_LINGER
;
331 case SocketOptionName_Debug
:
332 *system_name
= SO_DEBUG
;
335 case SocketOptionName_AcceptConnection
:
336 *system_name
= SO_ACCEPTCONN
;
339 case SocketOptionName_ReuseAddress
:
340 *system_name
= SO_REUSEADDR
;
342 case SocketOptionName_KeepAlive
:
343 *system_name
= SO_KEEPALIVE
;
345 case SocketOptionName_DontRoute
:
346 *system_name
= SO_DONTROUTE
;
348 case SocketOptionName_Broadcast
:
349 *system_name
= SO_BROADCAST
;
351 case SocketOptionName_Linger
:
352 *system_name
= SO_LINGER
;
354 case SocketOptionName_OutOfBandInline
:
355 *system_name
= SO_OOBINLINE
;
357 case SocketOptionName_SendBuffer
:
358 *system_name
= SO_SNDBUF
;
360 case SocketOptionName_ReceiveBuffer
:
361 *system_name
= SO_RCVBUF
;
363 case SocketOptionName_SendLowWater
:
364 *system_name
= SO_SNDLOWAT
;
366 case SocketOptionName_ReceiveLowWater
:
367 *system_name
= SO_RCVLOWAT
;
369 case SocketOptionName_SendTimeout
:
370 *system_name
= SO_SNDTIMEO
;
372 case SocketOptionName_ReceiveTimeout
:
373 *system_name
= SO_RCVTIMEO
;
375 case SocketOptionName_Error
:
376 *system_name
= SO_ERROR
;
378 case SocketOptionName_Type
:
379 *system_name
= SO_TYPE
;
382 case SocketOptionName_PeerCred
:
383 *system_name
= SO_PEERCRED
;
386 case SocketOptionName_ExclusiveAddressUse
:
387 #ifdef SO_EXCLUSIVEADDRUSE
388 *system_name
= SO_EXCLUSIVEADDRUSE
;
391 case SocketOptionName_UseLoopback
:
392 #ifdef SO_USELOOPBACK
393 *system_name
= SO_USELOOPBACK
;
396 case SocketOptionName_MaxConnections
:
398 *system_name
= SO_MAXCONN
;
400 #elif defined(SOMAXCONN)
401 *system_name
= SOMAXCONN
;
405 g_warning ("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name
);
410 case SocketOptionLevel_IP
:
411 *system_level
= mono_networking_get_ip_protocol ();
414 case SocketOptionName_IPOptions
:
415 *system_name
= IP_OPTIONS
;
418 case SocketOptionName_HeaderIncluded
:
419 *system_name
= IP_HDRINCL
;
423 case SocketOptionName_TypeOfService
:
424 *system_name
= IP_TOS
;
428 case SocketOptionName_IpTimeToLive
:
429 *system_name
= IP_TTL
;
432 case SocketOptionName_MulticastInterface
:
433 *system_name
= IP_MULTICAST_IF
;
435 case SocketOptionName_MulticastTimeToLive
:
436 *system_name
= IP_MULTICAST_TTL
;
438 case SocketOptionName_MulticastLoopback
:
439 *system_name
= IP_MULTICAST_LOOP
;
441 case SocketOptionName_AddMembership
:
442 *system_name
= IP_ADD_MEMBERSHIP
;
444 case SocketOptionName_DropMembership
:
445 *system_name
= IP_DROP_MEMBERSHIP
;
447 #ifdef HAVE_IP_PKTINFO
448 case SocketOptionName_PacketInformation
:
449 *system_name
= IP_PKTINFO
;
451 #endif /* HAVE_IP_PKTINFO */
453 case SocketOptionName_DontFragment
:
454 #ifdef HAVE_IP_DONTFRAGMENT
455 *system_name
= IP_DONTFRAGMENT
;
457 #elif defined HAVE_IP_MTU_DISCOVER
458 /* Not quite the same */
459 *system_name
= IP_MTU_DISCOVER
;
462 /* If the flag is not available on this system, we can ignore this error */
464 #endif /* HAVE_IP_DONTFRAGMENT */
465 case SocketOptionName_AddSourceMembership
:
466 case SocketOptionName_DropSourceMembership
:
467 case SocketOptionName_BlockSource
:
468 case SocketOptionName_UnblockSource
:
469 /* Can't figure out how to map these, so fall
473 g_warning ("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name
);
478 case SocketOptionLevel_IPv6
:
479 *system_level
= mono_networking_get_ipv6_protocol ();
482 case SocketOptionName_IpTimeToLive
:
483 case SocketOptionName_HopLimit
:
484 *system_name
= IPV6_UNICAST_HOPS
;
486 case SocketOptionName_MulticastInterface
:
487 *system_name
= IPV6_MULTICAST_IF
;
489 case SocketOptionName_MulticastTimeToLive
:
490 *system_name
= IPV6_MULTICAST_HOPS
;
492 case SocketOptionName_MulticastLoopback
:
493 *system_name
= IPV6_MULTICAST_LOOP
;
495 case SocketOptionName_AddMembership
:
496 *system_name
= IPV6_JOIN_GROUP
;
498 case SocketOptionName_DropMembership
:
499 *system_name
= IPV6_LEAVE_GROUP
;
501 case SocketOptionName_IPv6Only
:
503 *system_name
= IPV6_V6ONLY
;
508 case SocketOptionName_PacketInformation
:
509 #ifdef HAVE_IPV6_PKTINFO
510 *system_name
= IPV6_PKTINFO
;
513 case SocketOptionName_HeaderIncluded
:
514 case SocketOptionName_IPOptions
:
515 case SocketOptionName_TypeOfService
:
516 case SocketOptionName_DontFragment
:
517 case SocketOptionName_AddSourceMembership
:
518 case SocketOptionName_DropSourceMembership
:
519 case SocketOptionName_BlockSource
:
520 case SocketOptionName_UnblockSource
:
521 /* Can't figure out how to map these, so fall
525 g_warning ("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name
);
528 break; /* SocketOptionLevel_IPv6 */
530 case SocketOptionLevel_Tcp
:
531 *system_level
= mono_networking_get_tcp_protocol ();
534 case SocketOptionName_NoDelay
:
535 *system_name
= TCP_NODELAY
;
538 /* The documentation is talking complete
539 * bollocks here: rfc-1222 is titled
540 * 'Advancing the NSFNET Routing Architecture'
541 * and doesn't mention either of the words
542 * "expedite" or "urgent".
544 case SocketOptionName_BsdUrgent
:
545 case SocketOptionName_Expedited
:
548 g_warning ("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name
);
553 case SocketOptionLevel_Udp
:
554 g_warning ("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level
);
557 case SocketOptionName_NoChecksum
:
558 case SocketOptionName_ChecksumCoverage
:
560 g_warning ("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name
);
567 g_warning ("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level
);
575 get_socket_assembly (void)
577 MonoDomain
*domain
= mono_domain_get ();
579 if (domain
->socket_assembly
== NULL
) {
580 MonoImage
*socket_assembly
;
582 socket_assembly
= mono_image_loaded ("System");
583 if (!socket_assembly
) {
584 MonoAssembly
*sa
= mono_assembly_open ("System.dll", NULL
);
587 g_assert_not_reached ();
589 socket_assembly
= mono_assembly_get_image (sa
);
592 mono_atomic_store_release (&domain
->socket_assembly
, socket_assembly
);
595 return domain
->socket_assembly
;
599 ves_icall_System_Net_Sockets_Socket_Socket_internal (MonoObject
*this_obj
, gint32 family
, gint32 type
, gint32 proto
, gint32
*werror
)
608 sock_family
= convert_family ((MonoAddressFamily
)family
);
609 if (sock_family
== -1) {
610 *werror
= WSAEAFNOSUPPORT
;
614 sock_proto
= convert_proto ((MonoProtocolType
)proto
);
615 if (sock_proto
== -1) {
616 *werror
= WSAEPROTONOSUPPORT
;
620 sock_type
= convert_type ((MonoSocketType
)type
);
621 if (sock_type
== -1) {
622 *werror
= WSAESOCKTNOSUPPORT
;
626 sock
= _wapi_socket (sock_family
, sock_type
, sock_proto
,
627 NULL
, 0, WSA_FLAG_OVERLAPPED
);
629 if (sock
== INVALID_SOCKET
) {
630 *werror
= WSAGetLastError ();
634 return GUINT_TO_POINTER (sock
);
637 /* FIXME: the SOCKET parameter (here and in other functions in this
638 * file) is really an IntPtr which needs to be converted to a guint32.
641 ves_icall_System_Net_Sockets_Socket_Close_internal (SOCKET sock
, gint32
*werror
)
643 LOGDEBUG (g_message ("%s: closing 0x%x", __func__
, sock
));
647 /* Clear any pending work item from this socket if the underlying
648 * polling system does not notify when the socket is closed */
649 mono_threadpool_io_remove_socket (GPOINTER_TO_INT (sock
));
657 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal (void)
659 LOGDEBUG (g_message("%s: returning %d", __func__
, WSAGetLastError ()));
661 return WSAGetLastError ();
665 ves_icall_System_Net_Sockets_Socket_Available_internal (SOCKET sock
, gint32
*werror
)
672 /* FIXME: this might require amount to be unsigned long. */
673 ret
= ioctlsocket (sock
, FIONREAD
, &amount
);
674 if (ret
== SOCKET_ERROR
) {
675 *werror
= WSAGetLastError ();
683 ves_icall_System_Net_Sockets_Socket_Blocking_internal (SOCKET sock
, gboolean block
, gint32
*werror
)
690 * block == TRUE/FALSE means we will block/not block.
691 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
695 ret
= ioctlsocket (sock
, FIONBIO
, (gulong
*)&block
);
696 if (ret
== SOCKET_ERROR
)
697 *werror
= WSAGetLastError ();
701 ves_icall_System_Net_Sockets_Socket_Accept_internal (SOCKET sock
, gint32
*werror
, gboolean blocking
)
703 gboolean interrupted
;
708 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
718 MonoInternalThread
*curthread
= mono_thread_internal_current ();
719 curthread
->interrupt_on_stop
= (gpointer
)TRUE
;
720 newsock
= alertable_accept (sock
, NULL
, 0, blocking
);
721 curthread
->interrupt_on_stop
= (gpointer
)FALSE
;
724 newsock
= _wapi_accept (sock
, NULL
, 0);
729 if (newsock
== INVALID_SOCKET
)
730 *werror
= WSAGetLastError ();
732 mono_thread_info_uninstall_interrupt (&interrupted
);
739 return GUINT_TO_POINTER (newsock
);
743 ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock
, guint32 backlog
, gint32
*werror
)
751 ret
= _wapi_listen (sock
, backlog
);
755 if (ret
== SOCKET_ERROR
)
756 *werror
= WSAGetLastError ();
759 // Check whether it's ::ffff::0:0.
761 is_ipv4_mapped_any (const struct in6_addr
*addr
)
765 for (i
= 0; i
< 10; i
++) {
766 if (addr
->s6_addr
[i
])
769 if ((addr
->s6_addr
[10] != 0xff) || (addr
->s6_addr
[11] != 0xff))
771 for (i
= 12; i
< 16; i
++) {
772 if (addr
->s6_addr
[i
])
779 create_object_from_sockaddr (struct sockaddr
*saddr
, int sa_size
, gint32
*werror
, MonoError
*error
)
781 MonoDomain
*domain
= mono_domain_get ();
782 MonoObject
*sockaddr_obj
;
784 MonoAddressFamily family
;
786 mono_error_init (error
);
788 /* Build a System.Net.SocketAddress object instance */
789 if (!domain
->sockaddr_class
)
790 domain
->sockaddr_class
= mono_class_load_from_name (get_socket_assembly (), "System.Net", "SocketAddress");
791 sockaddr_obj
= mono_object_new_checked (domain
, domain
->sockaddr_class
, error
);
792 return_val_if_nok (error
, NULL
);
794 /* Locate the SocketAddress data buffer in the object */
795 if (!domain
->sockaddr_data_field
) {
796 domain
->sockaddr_data_field
= mono_class_get_field_from_name (domain
->sockaddr_class
, "m_Buffer");
797 g_assert (domain
->sockaddr_data_field
);
800 /* Locate the SocketAddress data buffer length in the object */
801 if (!domain
->sockaddr_data_length_field
) {
802 domain
->sockaddr_data_length_field
= mono_class_get_field_from_name (domain
->sockaddr_class
, "m_Size");
803 g_assert (domain
->sockaddr_data_length_field
);
806 /* May be the +2 here is too conservative, as sa_len returns
807 * the length of the entire sockaddr_in/in6, including
808 * sizeof (unsigned short) of the family */
809 /* We can't really avoid the +2 as all code below depends on this size - INCLUDING unix domain sockets.*/
810 data
= mono_array_new_cached (domain
, mono_get_byte_class (), sa_size
+ 2, error
);
811 return_val_if_nok (error
, NULL
);
813 /* The data buffer is laid out as follows:
814 * bytes 0 and 1 are the address family
815 * bytes 2 and 3 are the port info
816 * the rest is the address info
819 family
= convert_to_mono_family (saddr
->sa_family
);
820 if (family
== AddressFamily_Unknown
) {
821 *werror
= WSAEAFNOSUPPORT
;
825 mono_array_set (data
, guint8
, 0, family
& 0x0FF);
826 mono_array_set (data
, guint8
, 1, (family
>> 8) & 0x0FF);
828 if (saddr
->sa_family
== AF_INET
) {
829 struct sockaddr_in
*sa_in
= (struct sockaddr_in
*)saddr
;
830 guint16 port
= ntohs (sa_in
->sin_port
);
831 guint32 address
= ntohl (sa_in
->sin_addr
.s_addr
);
834 if (sa_size
< buffer_size
) {
835 mono_error_set_exception_instance (error
, mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
839 mono_array_set (data
, guint8
, 2, (port
>>8) & 0xff);
840 mono_array_set (data
, guint8
, 3, (port
) & 0xff);
841 mono_array_set (data
, guint8
, 4, (address
>>24) & 0xff);
842 mono_array_set (data
, guint8
, 5, (address
>>16) & 0xff);
843 mono_array_set (data
, guint8
, 6, (address
>>8) & 0xff);
844 mono_array_set (data
, guint8
, 7, (address
) & 0xff);
846 mono_field_set_value (sockaddr_obj
, domain
->sockaddr_data_field
, data
);
847 mono_field_set_value (sockaddr_obj
, domain
->sockaddr_data_length_field
, &buffer_size
);
850 } else if (saddr
->sa_family
== AF_INET6
) {
851 struct sockaddr_in6
*sa_in
= (struct sockaddr_in6
*)saddr
;
853 int buffer_size
= 28;
855 guint16 port
= ntohs (sa_in
->sin6_port
);
857 if (sa_size
< buffer_size
) {
858 mono_error_set_exception_instance (error
, mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
862 mono_array_set (data
, guint8
, 2, (port
>>8) & 0xff);
863 mono_array_set (data
, guint8
, 3, (port
) & 0xff);
865 if (is_ipv4_mapped_any (&sa_in
->sin6_addr
)) {
866 // Map ::ffff:0:0 to :: (bug #5502)
867 for (i
= 0; i
< 16; i
++)
868 mono_array_set (data
, guint8
, 8 + i
, 0);
870 for (i
= 0; i
< 16; i
++) {
871 mono_array_set (data
, guint8
, 8 + i
,
872 sa_in
->sin6_addr
.s6_addr
[i
]);
876 mono_array_set (data
, guint8
, 24, sa_in
->sin6_scope_id
& 0xff);
877 mono_array_set (data
, guint8
, 25,
878 (sa_in
->sin6_scope_id
>> 8) & 0xff);
879 mono_array_set (data
, guint8
, 26,
880 (sa_in
->sin6_scope_id
>> 16) & 0xff);
881 mono_array_set (data
, guint8
, 27,
882 (sa_in
->sin6_scope_id
>> 24) & 0xff);
884 mono_field_set_value (sockaddr_obj
, domain
->sockaddr_data_field
, data
);
885 mono_field_set_value (sockaddr_obj
, domain
->sockaddr_data_length_field
, &buffer_size
);
890 else if (saddr
->sa_family
== AF_UNIX
) {
892 int buffer_size
= sa_size
+ 2;
894 for (i
= 0; i
< sa_size
; i
++)
895 mono_array_set (data
, guint8
, i
+ 2, saddr
->sa_data
[i
]);
897 mono_field_set_value (sockaddr_obj
, domain
->sockaddr_data_field
, data
);
898 mono_field_set_value (sockaddr_obj
, domain
->sockaddr_data_length_field
, &buffer_size
);
904 *werror
= WSAEAFNOSUPPORT
;
910 get_sockaddr_size (int family
)
915 if (family
== AF_INET
) {
916 size
= sizeof (struct sockaddr_in
);
917 } else if (family
== AF_INET6
) {
918 size
= sizeof (struct sockaddr_in6
);
921 else if (family
== AF_UNIX
) {
922 size
= sizeof (struct sockaddr_un
);
929 ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal (SOCKET sock
, gint32 af
, gint32
*werror
)
939 salen
= get_sockaddr_size (convert_family ((MonoAddressFamily
)af
));
941 *werror
= WSAEAFNOSUPPORT
;
944 sa
= (salen
<= 128) ? (gchar
*)alloca (salen
) : (gchar
*)g_malloc0 (salen
);
948 ret
= _wapi_getsockname (sock
, (struct sockaddr
*)sa
, &salen
);
952 if (ret
== SOCKET_ERROR
) {
953 *werror
= WSAGetLastError ();
959 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
)));
961 result
= create_object_from_sockaddr ((struct sockaddr
*)sa
, salen
, werror
, &error
);
964 if (!mono_error_ok (&error
))
965 mono_error_set_pending_exception (&error
);
970 ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal (SOCKET sock
, gint32 af
, gint32
*werror
)
980 salen
= get_sockaddr_size (convert_family ((MonoAddressFamily
)af
));
982 *werror
= WSAEAFNOSUPPORT
;
985 sa
= (salen
<= 128) ? (gchar
*)alloca (salen
) : (gchar
*)g_malloc0 (salen
);
986 /* Note: linux returns just 2 for AF_UNIX. Always. */
990 ret
= _wapi_getpeername (sock
, (struct sockaddr
*)sa
, &salen
);
994 if (ret
== SOCKET_ERROR
) {
995 *werror
= WSAGetLastError ();
1001 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
)));
1003 result
= create_object_from_sockaddr ((struct sockaddr
*)sa
, salen
, werror
, &error
);
1006 if (!mono_error_ok (&error
))
1007 mono_error_set_pending_exception (&error
);
1011 static struct sockaddr
*
1012 create_sockaddr_from_object (MonoObject
*saddr_obj
, socklen_t
*sa_size
, gint32
*werror
, MonoError
*error
)
1014 MonoDomain
*domain
= mono_domain_get ();
1019 mono_error_init (error
);
1021 if (!domain
->sockaddr_class
)
1022 domain
->sockaddr_class
= mono_class_load_from_name (get_socket_assembly (), "System.Net", "SocketAddress");
1024 /* Locate the SocketAddress data buffer in the object */
1025 if (!domain
->sockaddr_data_field
) {
1026 domain
->sockaddr_data_field
= mono_class_get_field_from_name (domain
->sockaddr_class
, "m_Buffer");
1027 g_assert (domain
->sockaddr_data_field
);
1030 /* Locate the SocketAddress data buffer length in the object */
1031 if (!domain
->sockaddr_data_length_field
) {
1032 domain
->sockaddr_data_length_field
= mono_class_get_field_from_name (domain
->sockaddr_class
, "m_Size");
1033 g_assert (domain
->sockaddr_data_length_field
);
1036 data
= *(MonoArray
**)(((char *)saddr_obj
) + domain
->sockaddr_data_field
->offset
);
1038 /* The data buffer is laid out as follows:
1039 * byte 0 is the address family low byte
1040 * byte 1 is the address family high byte
1042 * bytes 2 and 3 are the port info
1043 * the rest is the address info
1045 * the rest is the file name
1047 len
= *(int *)(((char *)saddr_obj
) + domain
->sockaddr_data_length_field
->offset
);
1048 g_assert (len
>= 2);
1050 family
= convert_family ((MonoAddressFamily
)(mono_array_get (data
, guint8
, 0) + (mono_array_get (data
, guint8
, 1) << 8)));
1051 if (family
== AF_INET
) {
1052 struct sockaddr_in
*sa
;
1057 mono_error_set_exception_instance (error
, mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1061 sa
= g_new0 (struct sockaddr_in
, 1);
1062 port
= (mono_array_get (data
, guint8
, 2) << 8) +
1063 mono_array_get (data
, guint8
, 3);
1064 address
= (mono_array_get (data
, guint8
, 4) << 24) +
1065 (mono_array_get (data
, guint8
, 5) << 16 ) +
1066 (mono_array_get (data
, guint8
, 6) << 8) +
1067 mono_array_get (data
, guint8
, 7);
1069 sa
->sin_family
= family
;
1070 sa
->sin_addr
.s_addr
= htonl (address
);
1071 sa
->sin_port
= htons (port
);
1073 *sa_size
= sizeof (struct sockaddr_in
);
1074 return (struct sockaddr
*)sa
;
1075 } else if (family
== AF_INET6
) {
1076 struct sockaddr_in6
*sa
;
1082 mono_error_set_exception_instance (error
, mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1086 sa
= g_new0 (struct sockaddr_in6
, 1);
1087 port
= mono_array_get (data
, guint8
, 3) +
1088 (mono_array_get (data
, guint8
, 2) << 8);
1089 scopeid
= mono_array_get (data
, guint8
, 24) +
1090 (mono_array_get (data
, guint8
, 25) << 8) +
1091 (mono_array_get (data
, guint8
, 26) << 16) +
1092 (mono_array_get (data
, guint8
, 27) << 24);
1094 sa
->sin6_family
= family
;
1095 sa
->sin6_port
= htons (port
);
1096 sa
->sin6_scope_id
= scopeid
;
1098 for (i
= 0; i
< 16; i
++)
1099 sa
->sin6_addr
.s6_addr
[i
] = mono_array_get (data
, guint8
, 8 + i
);
1101 *sa_size
= sizeof (struct sockaddr_in6
);
1102 return (struct sockaddr
*)sa
;
1104 #ifdef HAVE_SYS_UN_H
1105 else if (family
== AF_UNIX
) {
1106 struct sockaddr_un
*sock_un
;
1109 /* Need a byte for the '\0' terminator/prefix, and the first
1110 * two bytes hold the SocketAddress family
1112 if (len
- 2 >= sizeof (sock_un
->sun_path
)) {
1113 mono_error_set_exception_instance (error
, mono_get_exception_index_out_of_range ());
1117 sock_un
= g_new0 (struct sockaddr_un
, 1);
1119 sock_un
->sun_family
= family
;
1120 for (i
= 0; i
< len
- 2; i
++)
1121 sock_un
->sun_path
[i
] = mono_array_get (data
, guint8
, i
+ 2);
1124 return (struct sockaddr
*)sock_un
;
1128 *werror
= WSAEAFNOSUPPORT
;
1134 ves_icall_System_Net_Sockets_Socket_Bind_internal (SOCKET sock
, MonoObject
*sockaddr
, gint32
*werror
)
1137 struct sockaddr
*sa
;
1143 sa
= create_sockaddr_from_object (sockaddr
, &sa_size
, werror
, &error
);
1146 if (!mono_error_ok (&error
)) {
1147 mono_error_set_pending_exception (&error
);
1151 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
)));
1153 ret
= _wapi_bind (sock
, sa
, sa_size
);
1155 if (ret
== SOCKET_ERROR
)
1156 *werror
= WSAGetLastError ();
1168 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock
, gint mode
,
1169 gint timeout
, gint32
*werror
)
1171 MonoInternalThread
*thread
= mono_thread_internal_current ();
1174 gboolean interrupted
;
1179 pfds
= g_new0 (mono_pollfd
, 1);
1180 pfds
->fd
= GPOINTER_TO_INT (sock
);
1183 case SelectModeRead
:
1184 pfds
->events
= MONO_POLLIN
;
1186 case SelectModeWrite
:
1187 pfds
->events
= MONO_POLLOUT
;
1190 pfds
->events
= MONO_POLLERR
| MONO_POLLHUP
| MONO_POLLNVAL
;
1194 timeout
= (timeout
>= 0) ? (timeout
/ 1000) : -1;
1195 start
= time (NULL
);
1198 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
1207 ret
= mono_poll (pfds
, 1, timeout
);
1211 mono_thread_info_uninstall_interrupt (&interrupted
);
1218 if (timeout
> 0 && ret
< 0) {
1220 int sec
= time (NULL
) - start
;
1222 timeout
-= sec
* 1000;
1230 if (ret
== -1 && errno
== EINTR
) {
1231 if (mono_thread_test_state (thread
, (MonoThreadState
)(ThreadState_AbortRequested
| ThreadState_StopRequested
))) {
1236 /* Suspend requested? */
1237 mono_thread_interruption_checkpoint ();
1241 } while (ret
== -1 && errno
== EINTR
);
1245 *werror
= (errno
> 0 && errno
< WSABASEERR
) ? errno
+ WSABASEERR
: errno
;
1247 *werror
= errno_to_WSA (errno
, __func__
);
1258 ves_icall_System_Net_Sockets_Socket_Connect_internal (SOCKET sock
, MonoObject
*sockaddr
, gint32
*werror
, gboolean blocking
)
1261 struct sockaddr
*sa
;
1264 gboolean interrupted
;
1268 sa
= create_sockaddr_from_object (sockaddr
, &sa_size
, werror
, &error
);
1271 if (!mono_error_ok (&error
)) {
1272 mono_error_set_pending_exception (&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 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
1287 ret
= alertable_connect (sock
, sa
, sa_size
, blocking
);
1289 ret
= _wapi_connect (sock
, sa
, sa_size
);
1294 if (ret
== SOCKET_ERROR
)
1295 *werror
= WSAGetLastError ();
1297 mono_thread_info_uninstall_interrupt (&interrupted
);
1304 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
1305 /* These #defines from mswsock.h from wine. Defining them here allows
1306 * us to build this file on a mingw box that doesn't know the magic
1307 * numbers, but still run on a newer windows box that does.
1309 #ifndef WSAID_DISCONNECTEX
1310 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1311 typedef BOOL (WINAPI
*LPFN_DISCONNECTEX
)(SOCKET
, LPOVERLAPPED
, DWORD
, DWORD
);
1314 #ifndef WSAID_TRANSMITFILE
1315 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1316 typedef BOOL (WINAPI
*LPFN_TRANSMITFILE
)(SOCKET
, HANDLE
, DWORD
, DWORD
, LPOVERLAPPED
, LPTRANSMIT_FILE_BUFFERS
, DWORD
);
1320 ves_icall_System_Net_Sockets_Socket_Disconnect_internal (SOCKET sock
, MonoBoolean reuse
, gint32
*werror
)
1323 glong output_bytes
= 0;
1324 GUID disco_guid
= WSAID_DISCONNECTEX
;
1325 GUID trans_guid
= WSAID_TRANSMITFILE
;
1326 LPFN_DISCONNECTEX _wapi_disconnectex
= NULL
;
1327 LPFN_TRANSMITFILE _wapi_transmitfile
= NULL
;
1328 gboolean interrupted
;
1332 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__
, sock
, reuse
));
1336 /* I _think_ the extension function pointers need to be looked
1337 * up for each socket. FIXME: check the best way to store
1338 * pointers to functions in managed objects that still works
1339 * on 64bit platforms.
1341 ret
= WSAIoctl (sock
, SIO_GET_EXTENSION_FUNCTION_POINTER
, (gchar
*)&disco_guid
, sizeof (GUID
),
1342 (gchar
*)&_wapi_disconnectex
, sizeof (void *), &output_bytes
, NULL
, NULL
);
1347 /* make sure that WSAIoctl didn't put crap in the
1350 _wapi_disconnectex
= NULL
;
1355 * Use the SIO_GET_EXTENSION_FUNCTION_POINTER to
1356 * determine the address of the disconnect method without
1357 * taking a hard dependency on a single provider
1359 * For an explanation of why this is done, you can read
1360 * the article at http://www.codeproject.com/internet/jbsocketserver3.asp
1362 ret
= WSAIoctl (sock
, SIO_GET_EXTENSION_FUNCTION_POINTER
, (gchar
*)&trans_guid
, sizeof(GUID
),
1363 (gchar
*)&_wapi_transmitfile
, sizeof(void *), &output_bytes
, NULL
, NULL
);
1368 _wapi_transmitfile
= NULL
;
1371 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
1379 if (_wapi_disconnectex
!= NULL
) {
1380 if (!_wapi_disconnectex (sock
, NULL
, reuse
? TF_REUSE_SOCKET
: 0, 0))
1381 *werror
= WSAGetLastError ();
1382 } else if (_wapi_transmitfile
!= NULL
) {
1383 if (!_wapi_transmitfile (sock
, NULL
, 0, 0, NULL
, NULL
, TF_DISCONNECT
| (reuse
? TF_REUSE_SOCKET
: 0)))
1384 *werror
= WSAGetLastError ();
1386 *werror
= ERROR_NOT_SUPPORTED
;
1391 mono_thread_info_uninstall_interrupt (&interrupted
);
1395 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
1398 ves_icall_System_Net_Sockets_Socket_Receive_internal (SOCKET sock
, MonoArray
*buffer
, gint32 offset
, gint32 count
, gint32 flags
, gint32
*werror
, gboolean blocking
)
1404 gboolean interrupted
;
1405 MonoInternalThread
* curthread G_GNUC_UNUSED
= mono_thread_internal_current ();
1409 alen
= mono_array_length (buffer
);
1410 if (offset
> alen
- count
)
1413 buf
= mono_array_addr (buffer
, guchar
, offset
);
1415 recvflags
= convert_socketflags (flags
);
1416 if (recvflags
== -1) {
1417 *werror
= WSAEOPNOTSUPP
;
1421 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
1429 curthread
->interrupt_on_stop
= (gpointer
)TRUE
;
1430 ret
= alertable_recv (sock
, buf
, count
, recvflags
, blocking
);
1431 curthread
->interrupt_on_stop
= (gpointer
)FALSE
;
1434 ret
= _wapi_recv (sock
, buf
, count
, recvflags
);
1439 if (ret
== SOCKET_ERROR
)
1440 *werror
= WSAGetLastError ();
1442 mono_thread_info_uninstall_interrupt (&interrupted
);
1453 ves_icall_System_Net_Sockets_Socket_Receive_array_internal (SOCKET sock
, MonoArray
*buffers
, gint32 flags
, gint32
*werror
, gboolean blocking
)
1456 gboolean interrupted
;
1459 DWORD recvflags
= 0;
1463 wsabufs
= mono_array_addr (buffers
, WSABUF
, 0);
1464 count
= mono_array_length (buffers
);
1466 recvflags
= convert_socketflags (flags
);
1467 if (recvflags
== -1) {
1468 *werror
= WSAEOPNOTSUPP
;
1472 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
1481 ret
= alertable_WSARecv (sock
, wsabufs
, count
, &recv
, &recvflags
, NULL
, NULL
, blocking
);
1483 ret
= WSARecv (sock
, wsabufs
, count
, &recv
, &recvflags
, NULL
, NULL
);
1488 if (ret
== SOCKET_ERROR
)
1489 *werror
= WSAGetLastError ();
1491 mono_thread_info_uninstall_interrupt (&interrupted
);
1502 ves_icall_System_Net_Sockets_Socket_ReceiveFrom_internal (SOCKET sock
, MonoArray
*buffer
, gint32 offset
, gint32 count
, gint32 flags
, MonoObject
**sockaddr
, gint32
*werror
, gboolean blocking
)
1509 struct sockaddr
*sa
;
1511 gboolean interrupted
;
1515 alen
= mono_array_length (buffer
);
1516 if (offset
> alen
- count
)
1519 sa
= create_sockaddr_from_object (*sockaddr
, &sa_size
, werror
, &error
);
1522 if (!mono_error_ok (&error
)) {
1523 mono_error_set_pending_exception (&error
);
1527 buf
= mono_array_addr (buffer
, guchar
, offset
);
1529 recvflags
= convert_socketflags (flags
);
1530 if (recvflags
== -1) {
1531 *werror
= WSAEOPNOTSUPP
;
1535 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
1545 ret
= alertable_recvfrom (sock
, buf
, count
, recvflags
, sa
, &sa_size
, blocking
);
1547 ret
= _wapi_recvfrom (sock
, buf
, count
, recvflags
, sa
, &sa_size
);
1552 if (ret
== SOCKET_ERROR
)
1553 *werror
= WSAGetLastError ();
1555 mono_thread_info_uninstall_interrupt (&interrupted
);
1565 /* If we didn't get a socket size, then we're probably a
1566 * connected connection-oriented socket and the stack hasn't
1567 * returned the remote address. All we can do is return null.
1570 *sockaddr
= create_object_from_sockaddr (sa
, sa_size
, werror
, &error
);
1571 if (!mono_error_ok (&error
)) {
1572 mono_error_set_pending_exception (&error
);
1586 ves_icall_System_Net_Sockets_Socket_Send_internal (SOCKET sock
, MonoArray
*buffer
, gint32 offset
, gint32 count
, gint32 flags
, gint32
*werror
, gboolean blocking
)
1592 gboolean interrupted
;
1596 alen
= mono_array_length (buffer
);
1597 if (offset
> alen
- count
)
1600 LOGDEBUG (g_message("%s: alen: %d", __func__
, alen
));
1602 buf
= mono_array_addr (buffer
, guchar
, offset
);
1604 LOGDEBUG (g_message("%s: Sending %d bytes", __func__
, count
));
1606 sendflags
= convert_socketflags (flags
);
1607 if (sendflags
== -1) {
1608 *werror
= WSAEOPNOTSUPP
;
1612 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
1621 ret
= alertable_send (sock
, buf
, count
, sendflags
, blocking
);
1623 ret
= _wapi_send (sock
, buf
, count
, sendflags
);
1628 if (ret
== SOCKET_ERROR
)
1629 *werror
= WSAGetLastError ();
1631 mono_thread_info_uninstall_interrupt (&interrupted
);
1642 ves_icall_System_Net_Sockets_Socket_Send_array_internal (SOCKET sock
, MonoArray
*buffers
, gint32 flags
, gint32
*werror
, gboolean blocking
)
1647 DWORD sendflags
= 0;
1648 gboolean interrupted
;
1652 wsabufs
= mono_array_addr (buffers
, WSABUF
, 0);
1653 count
= mono_array_length (buffers
);
1655 sendflags
= convert_socketflags (flags
);
1656 if (sendflags
== -1) {
1657 *werror
= WSAEOPNOTSUPP
;
1661 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
1670 ret
= alertable_WSASend (sock
, wsabufs
, count
, &sent
, sendflags
, NULL
, NULL
, blocking
);
1672 ret
= WSASend (sock
, wsabufs
, count
, &sent
, sendflags
, NULL
, NULL
);
1677 if (ret
== SOCKET_ERROR
)
1678 *werror
= WSAGetLastError ();
1680 mono_thread_info_uninstall_interrupt (&interrupted
);
1691 ves_icall_System_Net_Sockets_Socket_SendTo_internal (SOCKET sock
, MonoArray
*buffer
, gint32 offset
, gint32 count
, gint32 flags
, MonoObject
*sockaddr
, gint32
*werror
, gboolean blocking
)
1698 struct sockaddr
*sa
;
1700 gboolean interrupted
;
1704 alen
= mono_array_length (buffer
);
1705 if (offset
> alen
- count
) {
1709 sa
= create_sockaddr_from_object(sockaddr
, &sa_size
, werror
, &error
);
1712 if (!mono_error_ok (&error
)) {
1713 mono_error_set_pending_exception (&error
);
1717 LOGDEBUG (g_message ("%s: alen: %d", __func__
, alen
));
1719 buf
= mono_array_addr (buffer
, guchar
, offset
);
1721 LOGDEBUG (g_message("%s: Sending %d bytes", __func__
, count
));
1723 sendflags
= convert_socketflags (flags
);
1724 if (sendflags
== -1) {
1726 *werror
= WSAEOPNOTSUPP
;
1730 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
1740 ret
= alertable_sendto (sock
, buf
, count
, sendflags
, sa
, sa_size
, blocking
);
1742 ret
= _wapi_sendto (sock
, buf
, count
, sendflags
, sa
, sa_size
);
1747 if (ret
== SOCKET_ERROR
)
1748 *werror
= WSAGetLastError ();
1750 mono_thread_info_uninstall_interrupt (&interrupted
);
1763 Socket_to_SOCKET (MonoObject
*sockobj
)
1765 MonoSafeHandle
*safe_handle
;
1766 MonoClassField
*field
;
1768 field
= mono_class_get_field_from_name (sockobj
->vtable
->klass
, "m_Handle");
1769 safe_handle
= ((MonoSafeHandle
*)(*(gpointer
*)(((char *)sockobj
) + field
->offset
)));
1771 if (safe_handle
== NULL
)
1774 return (SOCKET
)safe_handle
->handle
;
1777 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1780 ves_icall_System_Net_Sockets_Socket_Select_internal (MonoArray
**sockets
, gint32 timeout
, gint32
*werror
)
1783 MonoInternalThread
*thread
= mono_thread_internal_current ();
1790 MonoClass
*sock_arr_class
;
1793 uintptr_t socks_size
;
1794 gboolean interrupted
;
1798 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1799 count
= mono_array_length (*sockets
);
1800 nfds
= count
- 3; /* NULL separators */
1801 pfds
= g_new0 (mono_pollfd
, nfds
);
1803 for (i
= 0; i
< count
; i
++) {
1804 obj
= mono_array_get (*sockets
, MonoObject
*, i
);
1811 /* The socket array was bogus */
1813 *werror
= WSAEFAULT
;
1817 pfds
[idx
].fd
= Socket_to_SOCKET (obj
);
1818 pfds
[idx
].events
= (mode
== 0) ? MONO_POLLIN
: (mode
== 1) ? MONO_POLLOUT
: POLL_ERRORS
;
1822 timeout
= (timeout
>= 0) ? (timeout
/ 1000) : -1;
1823 start
= time (NULL
);
1825 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
1834 ret
= mono_poll (pfds
, nfds
, timeout
);
1838 mono_thread_info_uninstall_interrupt (&interrupted
);
1845 if (timeout
> 0 && ret
< 0) {
1847 int sec
= time (NULL
) - start
;
1849 timeout
-= sec
* 1000;
1855 if (ret
== -1 && errno
== EINTR
) {
1856 if (mono_thread_test_state (thread
, (MonoThreadState
)(ThreadState_AbortRequested
| ThreadState_StopRequested
))) {
1862 /* Suspend requested? */
1863 mono_thread_interruption_checkpoint ();
1867 } while (ret
== -1 && errno
== EINTR
);
1871 *werror
= (errno
> 0 && errno
< WSABASEERR
) ? errno
+ WSABASEERR
: errno
;
1873 *werror
= errno_to_WSA (errno
, __func__
);
1885 sock_arr_class
= ((MonoObject
*)*sockets
)->vtable
->klass
;
1886 socks_size
= ((uintptr_t)ret
) + 3; /* space for the NULL delimiters */
1887 socks
= mono_array_new_full_checked (mono_domain_get (), sock_arr_class
, &socks_size
, NULL
, &error
);
1888 if (!mono_error_ok (&error
)) {
1889 mono_error_set_pending_exception (&error
);
1895 for (i
= 0; i
< count
&& ret
> 0; i
++) {
1898 obj
= mono_array_get (*sockets
, MonoObject
*, i
);
1905 pfd
= &pfds
[i
- mode
];
1906 if (pfd
->revents
== 0)
1910 if (mode
== 0 && (pfd
->revents
& (MONO_POLLIN
| POLL_ERRORS
)) != 0) {
1911 mono_array_setref (socks
, idx
++, obj
);
1912 } else if (mode
== 1 && (pfd
->revents
& (MONO_POLLOUT
| POLL_ERRORS
)) != 0) {
1913 mono_array_setref (socks
, idx
++, obj
);
1914 } else if ((pfd
->revents
& POLL_ERRORS
) != 0) {
1915 mono_array_setref (socks
, idx
++, obj
);
1924 int_to_object (MonoDomain
*domain
, int val
, MonoError
*error
)
1926 return mono_value_box_checked (domain
, mono_get_int32_class (), &val
, error
);
1930 ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal (SOCKET sock
, gint32 level
, gint32 name
, MonoObject
**obj_val
, gint32
*werror
)
1932 int system_level
= 0;
1933 int system_name
= 0;
1936 socklen_t valsize
= sizeof (val
);
1937 struct linger linger
;
1938 socklen_t lingersize
= sizeof (linger
);
1940 socklen_t time_ms_size
= sizeof (time_ms
);
1942 # if defined(__OpenBSD__)
1943 struct sockpeercred cred
;
1947 socklen_t credsize
= sizeof (cred
);
1950 MonoDomain
*domain
= mono_domain_get ();
1952 MonoClass
*obj_class
;
1953 MonoClassField
*field
;
1957 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
1958 if (level
== SocketOptionLevel_Socket
&& name
== SocketOptionName_ExclusiveAddressUse
) {
1959 system_level
= SOL_SOCKET
;
1960 system_name
= SO_REUSEADDR
;
1965 ret
= convert_sockopt_level_and_name ((MonoSocketOptionLevel
)level
, (MonoSocketOptionName
)name
, &system_level
, &system_name
);
1969 *werror
= WSAENOPROTOOPT
;
1973 *obj_val
= int_to_object (domain
, 0, &error
);
1974 mono_error_set_pending_exception (&error
);
1980 /* No need to deal with MulticastOption names here, because
1981 * you cant getsockopt AddMembership or DropMembership (the
1982 * int getsockopt will error, causing an exception)
1985 case SocketOptionName_Linger
:
1986 case SocketOptionName_DontLinger
:
1987 ret
= _wapi_getsockopt (sock
, system_level
, system_name
, &linger
, &lingersize
);
1990 case SocketOptionName_SendTimeout
:
1991 case SocketOptionName_ReceiveTimeout
:
1992 ret
= _wapi_getsockopt (sock
, system_level
, system_name
, (char *)&time_ms
, &time_ms_size
);
1996 case SocketOptionName_PeerCred
:
1997 ret
= _wapi_getsockopt (sock
, system_level
, system_name
, &cred
, &credsize
);
2002 ret
= _wapi_getsockopt (sock
, system_level
, system_name
, &val
, &valsize
);
2007 if (ret
== SOCKET_ERROR
) {
2008 *werror
= WSAGetLastError ();
2013 case SocketOptionName_Linger
:
2014 /* build a System.Net.Sockets.LingerOption */
2015 obj_class
= mono_class_load_from_name (get_socket_assembly (),
2016 "System.Net.Sockets",
2018 obj
= mono_object_new_checked (domain
, obj_class
, &error
);
2019 if (!mono_error_ok (&error
)) {
2020 mono_error_set_pending_exception (&error
);
2024 /* Locate and set the fields "bool enabled" and "int
2027 field
= mono_class_get_field_from_name(obj_class
, "enabled");
2028 *(guint8
*)(((char *)obj
)+field
->offset
) = linger
.l_onoff
;
2030 field
= mono_class_get_field_from_name(obj_class
, "lingerTime");
2031 *(guint32
*)(((char *)obj
)+field
->offset
)=linger
.l_linger
;
2033 case SocketOptionName_DontLinger
:
2034 /* construct a bool int in val - true if linger is off */
2035 obj
= int_to_object (domain
, !linger
.l_onoff
, &error
);
2036 mono_error_set_pending_exception (&error
);
2038 case SocketOptionName_SendTimeout
:
2039 case SocketOptionName_ReceiveTimeout
:
2040 obj
= int_to_object (domain
, time_ms
, &error
);
2041 mono_error_set_pending_exception (&error
);
2045 case SocketOptionName_PeerCred
: {
2047 * build a Mono.Posix.PeerCred+PeerCredData if
2050 static MonoImage
*mono_posix_image
= NULL
;
2051 MonoPeerCredData
*cred_data
;
2053 if (mono_posix_image
== NULL
) {
2054 mono_posix_image
= mono_image_loaded ("Mono.Posix");
2055 if (!mono_posix_image
) {
2056 MonoAssembly
*sa
= mono_assembly_open ("Mono.Posix.dll", NULL
);
2058 *werror
= WSAENOPROTOOPT
;
2061 mono_posix_image
= mono_assembly_get_image (sa
);
2066 obj_class
= mono_class_load_from_name (mono_posix_image
,
2069 obj
= mono_object_new_checked (domain
, obj_class
, &error
);
2070 if (!mono_error_ok (&error
)) {
2071 mono_error_set_pending_exception (&error
);
2074 cred_data
= (MonoPeerCredData
*)obj
;
2075 cred_data
->pid
= cred
.pid
;
2076 cred_data
->uid
= cred
.uid
;
2077 cred_data
->gid
= cred
.gid
;
2083 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2084 if (level
== SocketOptionLevel_Socket
&& name
== SocketOptionName_ExclusiveAddressUse
)
2087 obj
= int_to_object (domain
, val
, &error
);
2088 mono_error_set_pending_exception (&error
);
2095 ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal (SOCKET sock
, gint32 level
, gint32 name
, MonoArray
**byte_val
, gint32
*werror
)
2097 int system_level
= 0;
2098 int system_name
= 0;
2105 ret
= convert_sockopt_level_and_name((MonoSocketOptionLevel
)level
, (MonoSocketOptionName
)name
, &system_level
,
2108 *werror
= WSAENOPROTOOPT
;
2114 valsize
= mono_array_length (*byte_val
);
2115 buf
= mono_array_addr (*byte_val
, guchar
, 0);
2119 ret
= _wapi_getsockopt (sock
, system_level
, system_name
, buf
, &valsize
);
2123 if (ret
== SOCKET_ERROR
)
2124 *werror
= WSAGetLastError ();
2127 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2128 static struct in_addr
2129 ipaddress_to_struct_in_addr (MonoObject
*ipaddr
)
2131 struct in_addr inaddr
;
2132 MonoClassField
*field
;
2134 field
= mono_class_get_field_from_name (ipaddr
->vtable
->klass
, "m_Address");
2136 /* No idea why .net uses a 64bit type to hold a 32bit value...
2138 * Internal value of IPAddess is in little-endian order
2140 inaddr
.s_addr
= GUINT_FROM_LE ((guint32
)*(guint64
*)(((char *)ipaddr
) + field
->offset
));
2145 static struct in6_addr
2146 ipaddress_to_struct_in6_addr (MonoObject
*ipaddr
)
2148 struct in6_addr in6addr
;
2149 MonoClassField
*field
;
2153 field
= mono_class_get_field_from_name (ipaddr
->vtable
->klass
, "m_Numbers");
2155 data
= *(MonoArray
**)(((char *)ipaddr
) + field
->offset
);
2157 for (i
= 0; i
< 8; i
++) {
2158 const guint16 s
= GUINT16_TO_BE (mono_array_get (data
, guint16
, i
));
2160 /* Solaris/MacOS have only the 8 bit version. */
2162 in6addr
.s6_addr
[2 * i
+ 1] = (s
>> 8) & 0xff;
2163 in6addr
.s6_addr
[2 * i
] = s
& 0xff;
2165 in6addr
.s6_addr16
[i
] = s
;
2172 #if defined(__APPLE__) || defined(__FreeBSD__)
2175 get_local_interface_id (int family
)
2177 #if !defined(HAVE_GETIFADDRS) || !defined(HAVE_IF_NAMETOINDEX)
2180 struct ifaddrs
*ifap
= NULL
, *ptr
;
2183 if (getifaddrs (&ifap
))
2186 for (ptr
= ifap
; ptr
; ptr
= ptr
->ifa_next
) {
2187 if (!ptr
->ifa_addr
|| !ptr
->ifa_name
)
2189 if (ptr
->ifa_addr
->sa_family
!= family
)
2191 if ((ptr
->ifa_flags
& IFF_LOOPBACK
) != 0)
2193 if ((ptr
->ifa_flags
& IFF_MULTICAST
) == 0)
2196 idx
= if_nametoindex (ptr
->ifa_name
);
2205 #endif /* defined(__APPLE__) || defined(__FreeBSD__) */
2208 ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal (SOCKET sock
, gint32 level
, gint32 name
, MonoObject
*obj_val
, MonoArray
*byte_val
, gint32 int_val
, gint32
*werror
)
2210 struct linger linger
;
2211 int system_level
= 0;
2212 int system_name
= 0;
2219 sol_ipv6
= mono_networking_get_ipv6_protocol ();
2220 sol_ip
= mono_networking_get_ip_protocol ();
2222 ret
= convert_sockopt_level_and_name ((MonoSocketOptionLevel
)level
, (MonoSocketOptionName
)name
, &system_level
,
2225 #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
2226 if (level
== SocketOptionLevel_Socket
&& name
== SocketOptionName_ExclusiveAddressUse
) {
2227 system_name
= SO_REUSEADDR
;
2228 int_val
= int_val
? 0 : 1;
2234 *werror
= WSAENOPROTOOPT
;
2240 /* Only one of obj_val, byte_val or int_val has data */
2242 MonoClassField
*field
;
2246 case SocketOptionName_Linger
:
2247 /* Dig out "bool enabled" and "int lingerTime"
2250 field
= mono_class_get_field_from_name (obj_val
->vtable
->klass
, "enabled");
2251 linger
.l_onoff
= *(guint8
*)(((char *)obj_val
) + field
->offset
);
2252 field
= mono_class_get_field_from_name (obj_val
->vtable
->klass
, "lingerTime");
2253 linger
.l_linger
= *(guint32
*)(((char *)obj_val
) + field
->offset
);
2255 valsize
= sizeof (linger
);
2256 ret
= _wapi_setsockopt (sock
, system_level
,
2257 system_name
, &linger
, valsize
);
2259 case SocketOptionName_AddMembership
:
2260 case SocketOptionName_DropMembership
:
2261 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2263 MonoObject
*address
= NULL
;
2265 if (system_level
== sol_ipv6
) {
2266 struct ipv6_mreq mreq6
;
2271 field
= mono_class_get_field_from_name (obj_val
->vtable
->klass
, "m_Group");
2273 address
= *(MonoObject
**)(((char *)obj_val
) + field
->offset
);
2276 mreq6
.ipv6mr_multiaddr
= ipaddress_to_struct_in6_addr (address
);
2278 field
= mono_class_get_field_from_name (obj_val
->vtable
->klass
, "m_Interface");
2279 mreq6
.ipv6mr_interface
= *(guint64
*)(((char *)obj_val
) + field
->offset
);
2281 #if defined(__APPLE__) || defined(__FreeBSD__)
2285 * Mac OS Lion doesn't allow ipv6mr_interface = 0.
2287 * Tests on Windows and Linux show that the multicast group is only
2288 * joined on one NIC when interface = 0, so we simply use the interface
2289 * id from the first non-loopback interface (this is also what
2290 * Dns.GetHostName (string.Empty) would return).
2292 if (!mreq6
.ipv6mr_interface
)
2293 mreq6
.ipv6mr_interface
= get_local_interface_id (AF_INET6
);
2296 ret
= _wapi_setsockopt (sock
, system_level
,
2297 system_name
, &mreq6
,
2299 } else if (system_level
== sol_ip
) {
2300 #ifdef HAVE_STRUCT_IP_MREQN
2301 struct ip_mreqn mreq
= {{0}};
2303 struct ip_mreq mreq
= {{0}};
2304 #endif /* HAVE_STRUCT_IP_MREQN */
2307 * pain! MulticastOption holds two IPAddress
2308 * members, so I have to dig the value out of
2311 field
= mono_class_get_field_from_name (obj_val
->vtable
->klass
, "group");
2312 address
= *(MonoObject
**)(((char *)obj_val
) + field
->offset
);
2314 /* address might not be defined and if so, set the address to ADDR_ANY.
2317 mreq
.imr_multiaddr
= ipaddress_to_struct_in_addr (address
);
2319 field
= mono_class_get_field_from_name (obj_val
->vtable
->klass
, "localAddress");
2320 address
= *(MonoObject
**)(((char *)obj_val
) + field
->offset
);
2322 #ifdef HAVE_STRUCT_IP_MREQN
2324 mreq
.imr_address
= ipaddress_to_struct_in_addr (address
);
2326 field
= mono_class_get_field_from_name (obj_val
->vtable
->klass
, "ifIndex");
2327 mreq
.imr_ifindex
= *(gint32
*)(((char *)obj_val
) + field
->offset
);
2330 mreq
.imr_interface
= ipaddress_to_struct_in_addr (address
);
2331 #endif /* HAVE_STRUCT_IP_MREQN */
2333 ret
= _wapi_setsockopt (sock
, system_level
,
2339 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2341 /* Cause an exception to be thrown */
2342 *werror
= WSAEINVAL
;
2345 } else if (byte_val
!=NULL
) {
2346 int valsize
= mono_array_length (byte_val
);
2347 guchar
*buf
= mono_array_addr (byte_val
, guchar
, 0);
2350 case SocketOptionName_DontLinger
:
2352 linger
.l_onoff
= (*buf
) ? 0 : 1;
2353 linger
.l_linger
= 0;
2354 ret
= _wapi_setsockopt (sock
, system_level
, system_name
, &linger
, sizeof (linger
));
2356 *werror
= WSAEINVAL
;
2360 ret
= _wapi_setsockopt (sock
, system_level
, system_name
, buf
, valsize
);
2364 /* ReceiveTimeout/SendTimeout get here */
2366 case SocketOptionName_DontLinger
:
2367 linger
.l_onoff
= !int_val
;
2368 linger
.l_linger
= 0;
2369 ret
= _wapi_setsockopt (sock
, system_level
, system_name
, &linger
, sizeof (linger
));
2371 case SocketOptionName_MulticastInterface
:
2373 #ifdef HAVE_STRUCT_IP_MREQN
2374 int_val
= GUINT32_FROM_BE (int_val
);
2375 if ((int_val
& 0xff000000) == 0) {
2376 /* int_val is interface index */
2377 struct ip_mreqn mreq
= {{0}};
2378 mreq
.imr_ifindex
= int_val
;
2379 ret
= _wapi_setsockopt (sock
, system_level
, system_name
, (char *) &mreq
, sizeof (mreq
));
2382 int_val
= GUINT32_TO_BE (int_val
);
2383 #endif /* HAVE_STRUCT_IP_MREQN */
2384 #endif /* HOST_WIN32 */
2385 /* int_val is in_addr */
2386 ret
= _wapi_setsockopt (sock
, system_level
, system_name
, (char *) &int_val
, sizeof (int_val
));
2388 case SocketOptionName_DontFragment
:
2389 #ifdef HAVE_IP_MTU_DISCOVER
2390 /* Fiddle with the value slightly if we're
2394 int_val
= IP_PMTUDISC_DO
;
2399 ret
= _wapi_setsockopt (sock
, system_level
, system_name
, (char *) &int_val
, sizeof (int_val
));
2403 if (ret
== SOCKET_ERROR
)
2404 *werror
= WSAGetLastError ();
2408 ves_icall_System_Net_Sockets_Socket_Shutdown_internal (SOCKET sock
, gint32 how
, gint32
*werror
)
2411 gboolean interrupted
;
2415 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
2423 /* Currently, the values for how (recv=0, send=1, both=2) match the BSD API */
2424 ret
= _wapi_shutdown (sock
, how
);
2428 if (ret
== SOCKET_ERROR
)
2429 *werror
= WSAGetLastError ();
2431 mono_thread_info_uninstall_interrupt (&interrupted
);
2439 ves_icall_System_Net_Sockets_Socket_IOControl_internal (SOCKET sock
, gint32 code
, MonoArray
*input
, MonoArray
*output
, gint32
*werror
)
2441 glong output_bytes
= 0;
2442 gchar
*i_buffer
, *o_buffer
;
2448 if ((guint32
)code
== FIONBIO
)
2449 /* Invalid command. Must use Socket.Blocking */
2452 if (input
== NULL
) {
2456 i_buffer
= mono_array_addr (input
, gchar
, 0);
2457 i_len
= mono_array_length (input
);
2460 if (output
== NULL
) {
2464 o_buffer
= mono_array_addr (output
, gchar
, 0);
2465 o_len
= mono_array_length (output
);
2470 ret
= WSAIoctl (sock
, code
, i_buffer
, i_len
, o_buffer
, o_len
, &output_bytes
, NULL
, NULL
);
2474 if (ret
== SOCKET_ERROR
) {
2475 *werror
= WSAGetLastError ();
2479 return (gint
)output_bytes
;
2483 addrinfo_to_IPHostEntry (MonoAddressInfo
*info
, MonoString
**h_name
, MonoArray
**h_aliases
, MonoArray
**h_addr_list
, gboolean add_local_ips
, MonoError
*error
)
2486 MonoAddressEntry
*ai
= NULL
;
2487 struct in_addr
*local_in
= NULL
;
2489 struct in6_addr
*local_in6
= NULL
;
2492 MonoDomain
*domain
= mono_domain_get ();
2494 mono_error_init (error
);
2496 *h_aliases
= mono_array_new_checked (domain
, mono_get_string_class (), 0, error
);
2497 return_val_if_nok (error
, FALSE
);
2498 if (add_local_ips
) {
2499 local_in
= (struct in_addr
*) mono_get_local_interfaces (AF_INET
, &nlocal_in
);
2500 local_in6
= (struct in6_addr
*) mono_get_local_interfaces (AF_INET6
, &nlocal_in6
);
2501 if (nlocal_in
|| nlocal_in6
) {
2502 char addr
[INET6_ADDRSTRLEN
];
2503 *h_addr_list
= mono_array_new_checked (domain
, mono_get_string_class (), nlocal_in
+ nlocal_in6
, error
);
2508 MonoString
*addr_string
;
2511 for (i
= 0; i
< nlocal_in
; i
++) {
2513 mono_address_init (&maddr
, AF_INET
, &local_in
[i
]);
2514 if (mono_networking_addr_to_str (&maddr
, addr
, sizeof (addr
))) {
2515 addr_string
= mono_string_new (domain
, addr
);
2516 mono_array_setref (*h_addr_list
, addr_index
, addr_string
);
2523 MonoString
*addr_string
;
2526 for (i
= 0; i
< nlocal_in6
; i
++) {
2528 mono_address_init (&maddr
, AF_INET6
, &local_in6
[i
]);
2529 if (mono_networking_addr_to_str (&maddr
, addr
, sizeof (addr
))) {
2530 addr_string
= mono_string_new (domain
, addr
);
2531 mono_array_setref (*h_addr_list
, addr_index
, addr_string
);
2541 mono_free_address_info (info
);
2542 return is_ok (error
);;
2549 for (count
= 0, ai
= info
->entries
; ai
!= NULL
; ai
= ai
->next
) {
2550 if (ai
->family
!= AF_INET
&& ai
->family
!= AF_INET6
)
2555 *h_addr_list
= mono_array_new_checked (domain
, mono_get_string_class (), count
, error
);
2559 for (ai
= info
->entries
, i
= 0; ai
!= NULL
; ai
= ai
->next
) {
2561 MonoString
*addr_string
;
2562 char buffer
[INET6_ADDRSTRLEN
]; /* Max. size for IPv6 */
2564 if ((ai
->family
!= PF_INET
) && (ai
->family
!= PF_INET6
))
2567 mono_address_init (&maddr
, ai
->family
, &ai
->address
);
2568 if (mono_networking_addr_to_str (&maddr
, buffer
, sizeof (buffer
)))
2569 addr_string
= mono_string_new (domain
, buffer
);
2571 addr_string
= mono_string_new (domain
, "");
2573 mono_array_setref (*h_addr_list
, addr_index
, addr_string
);
2577 if (ai
->canonical_name
!= NULL
) {
2578 *h_name
= mono_string_new (domain
, ai
->canonical_name
);
2580 *h_name
= mono_string_new (domain
, buffer
);
2589 mono_free_address_info (info
);
2591 return is_ok (error
);
2595 ves_icall_System_Net_Dns_GetHostByName_internal (MonoString
*host
, MonoString
**h_name
, MonoArray
**h_aliases
, MonoArray
**h_addr_list
, gint32 hint
)
2598 gboolean add_local_ips
= FALSE
, add_info_ok
= TRUE
;
2599 gchar this_hostname
[256];
2600 MonoAddressInfo
*info
= NULL
;
2602 char *hostname
= mono_string_to_utf8_checked (host
, &error
);
2603 if (mono_error_set_pending_exception (&error
))
2606 if (*hostname
== '\0') {
2607 add_local_ips
= TRUE
;
2611 if (!add_local_ips
&& gethostname (this_hostname
, sizeof (this_hostname
)) != -1) {
2612 if (!strcmp (hostname
, this_hostname
)) {
2613 add_local_ips
= TRUE
;
2619 // Win32 APIs already returns local interface addresses for empty hostname ("")
2620 // so we never want to add them manually.
2621 add_local_ips
= FALSE
;
2622 if (mono_get_address_info(hostname
, 0, MONO_HINT_CANONICAL_NAME
| hint
, &info
))
2623 add_info_ok
= FALSE
;
2625 if (*hostname
&& mono_get_address_info (hostname
, 0, MONO_HINT_CANONICAL_NAME
| hint
, &info
))
2626 add_info_ok
= FALSE
;
2632 MonoBoolean result
= addrinfo_to_IPHostEntry (info
, h_name
, h_aliases
, h_addr_list
, add_local_ips
, &error
);
2633 mono_error_set_pending_exception (&error
);
2640 ves_icall_System_Net_Dns_GetHostByAddr_internal (MonoString
*addr
, MonoString
**h_name
, MonoArray
**h_aliases
, MonoArray
**h_addr_list
, gint32 hint
)
2643 struct sockaddr_in saddr
;
2644 struct sockaddr_in6 saddr6
;
2645 MonoAddressInfo
*info
= NULL
;
2648 gchar hostname
[NI_MAXHOST
] = { 0 };
2651 address
= mono_string_to_utf8_checked (addr
, &error
);
2652 if (mono_error_set_pending_exception (&error
))
2655 if (inet_pton (AF_INET
, address
, &saddr
.sin_addr
) == 1) {
2657 saddr
.sin_family
= AF_INET
;
2658 } else if (inet_pton (AF_INET6
, address
, &saddr6
.sin6_addr
) == 1) {
2660 saddr6
.sin6_family
= AF_INET6
;
2672 #if HAVE_SOCKADDR_IN_SIN_LEN
2673 saddr
.sin_len
= sizeof (saddr
);
2675 ret
= getnameinfo ((struct sockaddr
*)&saddr
, sizeof (saddr
), hostname
, sizeof (hostname
), NULL
, 0, 0) == 0;
2679 #if HAVE_SOCKADDR_IN6_SIN_LEN
2680 saddr6
.sin6_len
= sizeof (saddr6
);
2682 ret
= getnameinfo ((struct sockaddr
*)&saddr6
, sizeof (saddr6
), hostname
, sizeof (hostname
), NULL
, 0, 0) == 0;
2686 g_assert_not_reached ();
2694 if (mono_get_address_info (hostname
, 0, hint
| MONO_HINT_CANONICAL_NAME
| MONO_HINT_CONFIGURED_ONLY
, &info
) != 0)
2697 MonoBoolean result
= addrinfo_to_IPHostEntry (info
, h_name
, h_aliases
, h_addr_list
, FALSE
, &error
);
2698 mono_error_set_pending_exception (&error
);
2703 ves_icall_System_Net_Dns_GetHostName_internal (MonoString
**h_name
)
2705 gchar hostname
[NI_MAXHOST
] = { 0 };
2708 ret
= gethostname (hostname
, sizeof (hostname
));
2712 *h_name
= mono_string_new (mono_domain_get (), hostname
);
2717 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
2719 ves_icall_System_Net_Sockets_Socket_SendFile_internal (SOCKET sock
, MonoString
*filename
, MonoArray
*pre_buffer
, MonoArray
*post_buffer
, gint flags
, gint32
*werror
, gboolean blocking
)
2723 gboolean interrupted
;
2724 TRANSMIT_FILE_BUFFERS buffers
;
2726 if (filename
== NULL
)
2729 /* FIXME: replace file by a proper fd that we can call open and close on, as they are interruptible */
2731 file
= ves_icall_System_IO_MonoIO_Open (filename
, FileMode_Open
, FileAccess_Read
, FileShare_Read
, 0, werror
);
2733 if (file
== INVALID_HANDLE_VALUE
) {
2734 SetLastError (*werror
);
2738 memset (&buffers
, 0, sizeof (buffers
));
2739 if (pre_buffer
!= NULL
) {
2740 buffers
.Head
= mono_array_addr (pre_buffer
, guchar
, 0);
2741 buffers
.HeadLength
= mono_array_length (pre_buffer
);
2743 if (post_buffer
!= NULL
) {
2744 buffers
.Tail
= mono_array_addr (post_buffer
, guchar
, 0);
2745 buffers
.TailLength
= mono_array_length (post_buffer
);
2748 mono_thread_info_install_interrupt (abort_syscall
, (gpointer
) (gsize
) mono_native_thread_id_get (), &interrupted
);
2751 SetLastError (WSAEINTR
);
2758 ret
= alertable_TransmitFile (sock
, file
, 0, 0, NULL
, &buffers
, flags
, blocking
);
2760 ret
= TransmitFile (sock
, file
, 0, 0, NULL
, &buffers
, flags
);
2766 *werror
= WSAGetLastError ();
2768 mono_thread_info_uninstall_interrupt (&interrupted
);
2786 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
2789 ves_icall_System_Net_Sockets_Socket_SupportPortReuse (MonoProtocolType proto
)
2791 #if defined (SO_REUSEPORT) || defined (HOST_WIN32)
2795 /* Linux always supports double binding for UDP, even on older kernels. */
2796 if (proto
== ProtocolType_Udp
)
2804 mono_network_init (void)
2806 mono_networking_init ();
2810 mono_network_cleanup (void)
2812 _wapi_cleanup_networking ();
2813 mono_networking_shutdown ();
2817 icall_cancel_blocking_socket_operation (MonoThread
*thread
)
2819 MonoInternalThread
*internal
;
2821 internal
= thread
->internal_thread
;
2822 g_assert (internal
);
2824 mono_thread_info_abort_socket_syscall_for_close (MONO_UINT_TO_NATIVE_THREAD_ID (internal
->tid
));
2827 #endif /* #ifndef DISABLE_SOCKETS */