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>
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__) */
91 * Some older versions of libc provide IPV6 support without defining the AI_ADDRCONFIG
92 * flag for getaddrinfo.
95 #define AI_ADDRCONFIG 0
98 static gint32
convert_family(MonoAddressFamily mono_family
)
102 switch(mono_family
) {
103 case AddressFamily_Unknown
:
104 case AddressFamily_ImpLink
:
105 case AddressFamily_Pup
:
106 case AddressFamily_Chaos
:
107 case AddressFamily_Iso
:
108 case AddressFamily_Ecma
:
109 case AddressFamily_DataKit
:
110 case AddressFamily_Ccitt
:
111 case AddressFamily_DataLink
:
112 case AddressFamily_Lat
:
113 case AddressFamily_HyperChannel
:
114 case AddressFamily_NetBios
:
115 case AddressFamily_VoiceView
:
116 case AddressFamily_FireFox
:
117 case AddressFamily_Banyan
:
118 case AddressFamily_Atm
:
119 case AddressFamily_Cluster
:
120 case AddressFamily_Ieee12844
:
121 case AddressFamily_NetworkDesigners
:
122 g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family
);
125 case AddressFamily_Unspecified
:
129 case AddressFamily_Unix
:
133 case AddressFamily_InterNetwork
:
137 case AddressFamily_Ipx
:
143 case AddressFamily_Sna
:
149 case AddressFamily_DecNet
:
155 case AddressFamily_AppleTalk
:
159 case AddressFamily_InterNetworkV6
:
164 case AddressFamily_Irda
:
170 g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family
);
176 static MonoAddressFamily
convert_to_mono_family(guint16 af_family
)
178 MonoAddressFamily family
=AddressFamily_Unknown
;
182 family
=AddressFamily_Unspecified
;
186 family
=AddressFamily_Unix
;
190 family
=AddressFamily_InterNetwork
;
195 family
=AddressFamily_Ipx
;
201 family
=AddressFamily_Sna
;
207 family
=AddressFamily_DecNet
;
212 family
=AddressFamily_AppleTalk
;
217 family
=AddressFamily_InterNetworkV6
;
223 family
=AddressFamily_Irda
;
227 g_warning("unknown address family 0x%x", af_family
);
233 static gint32
convert_type(MonoSocketType mono_type
)
238 case SocketType_Stream
:
242 case SocketType_Dgram
:
256 case SocketType_Seqpacket
:
260 case SocketType_Unknown
:
261 g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type
);
265 g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type
);
271 static gint32
convert_proto(MonoProtocolType mono_proto
)
276 case ProtocolType_IP
:
277 case ProtocolType_IPv6
:
278 case ProtocolType_Icmp
:
279 case ProtocolType_Igmp
:
280 case ProtocolType_Ggp
:
281 case ProtocolType_Tcp
:
282 case ProtocolType_Pup
:
283 case ProtocolType_Udp
:
284 case ProtocolType_Idp
:
285 /* These protocols are known (on my system at least) */
289 case ProtocolType_ND
:
290 case ProtocolType_Raw
:
291 case ProtocolType_Ipx
:
292 case ProtocolType_Spx
:
293 case ProtocolType_SpxII
:
294 case ProtocolType_Unknown
:
295 /* These protocols arent */
296 g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto
);
306 /* Convert MonoSocketFlags */
307 static gint32
convert_socketflags (gint32 sflags
)
312 /* SocketFlags.None */
315 if (sflags
& ~(SocketFlags_OutOfBand
| SocketFlags_MaxIOVectorLength
| SocketFlags_Peek
|
316 SocketFlags_DontRoute
| SocketFlags_Partial
))
317 /* Contains invalid flag values */
320 if (sflags
& SocketFlags_OutOfBand
)
322 if (sflags
& SocketFlags_Peek
)
324 if (sflags
& SocketFlags_DontRoute
)
325 flags
|= MSG_DONTROUTE
;
327 /* Ignore Partial - see bug 349688. Don't return -1, because
328 * according to the comment in that bug ms runtime doesn't for
329 * UDP sockets (this means we will silently ignore it for TCP
333 if (sflags
& SocketFlags_Partial
)
337 /* Don't do anything for MaxIOVectorLength */
338 if (sflags
& SocketFlags_MaxIOVectorLength
)
346 * 0 on success (mapped mono_level and mono_name to system_level and system_name
348 * -2 on non-fatal error (ie, must ignore)
350 static gint32
convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level
,
351 MonoSocketOptionName mono_name
,
355 switch (mono_level
) {
356 case SocketOptionLevel_Socket
:
357 *system_level
= SOL_SOCKET
;
360 case SocketOptionName_DontLinger
:
361 /* This is SO_LINGER, because the setsockopt
362 * internal call maps DontLinger to SO_LINGER
365 *system_name
= SO_LINGER
;
367 case SocketOptionName_Debug
:
368 *system_name
= SO_DEBUG
;
371 case SocketOptionName_AcceptConnection
:
372 *system_name
= SO_ACCEPTCONN
;
375 case SocketOptionName_ReuseAddress
:
376 *system_name
= SO_REUSEADDR
;
378 case SocketOptionName_KeepAlive
:
379 *system_name
= SO_KEEPALIVE
;
381 case SocketOptionName_DontRoute
:
382 *system_name
= SO_DONTROUTE
;
384 case SocketOptionName_Broadcast
:
385 *system_name
= SO_BROADCAST
;
387 case SocketOptionName_Linger
:
388 *system_name
= SO_LINGER
;
390 case SocketOptionName_OutOfBandInline
:
391 *system_name
= SO_OOBINLINE
;
393 case SocketOptionName_SendBuffer
:
394 *system_name
= SO_SNDBUF
;
396 case SocketOptionName_ReceiveBuffer
:
397 *system_name
= SO_RCVBUF
;
399 case SocketOptionName_SendLowWater
:
400 *system_name
= SO_SNDLOWAT
;
402 case SocketOptionName_ReceiveLowWater
:
403 *system_name
= SO_RCVLOWAT
;
405 case SocketOptionName_SendTimeout
:
406 *system_name
= SO_SNDTIMEO
;
408 case SocketOptionName_ReceiveTimeout
:
409 *system_name
= SO_RCVTIMEO
;
411 case SocketOptionName_Error
:
412 *system_name
= SO_ERROR
;
414 case SocketOptionName_Type
:
415 *system_name
= SO_TYPE
;
418 case SocketOptionName_PeerCred
:
419 *system_name
= SO_PEERCRED
;
422 case SocketOptionName_ExclusiveAddressUse
:
423 #ifdef SO_EXCLUSIVEADDRUSE
424 *system_name
= SO_EXCLUSIVEADDRUSE
;
427 case SocketOptionName_UseLoopback
:
428 #ifdef SO_USELOOPBACK
429 *system_name
= SO_USELOOPBACK
;
432 case SocketOptionName_MaxConnections
:
434 *system_name
= SO_MAXCONN
;
436 #elif defined(SOMAXCONN)
437 *system_name
= SOMAXCONN
;
441 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name
);
446 case SocketOptionLevel_IP
:
448 *system_level
= SOL_IP
;
451 static int cached
= 0;
455 struct protoent
*pent
;
457 pent
= getprotobyname ("IP");
458 proto
= pent
? pent
->p_proto
: 0 /* 0 a good default value?? */;
462 *system_level
= proto
;
464 #endif /* HAVE_SOL_IP */
467 case SocketOptionName_IPOptions
:
468 *system_name
= IP_OPTIONS
;
471 case SocketOptionName_HeaderIncluded
:
472 *system_name
= IP_HDRINCL
;
476 case SocketOptionName_TypeOfService
:
477 *system_name
= IP_TOS
;
481 case SocketOptionName_IpTimeToLive
:
482 *system_name
= IP_TTL
;
485 case SocketOptionName_MulticastInterface
:
486 *system_name
= IP_MULTICAST_IF
;
488 case SocketOptionName_MulticastTimeToLive
:
489 *system_name
= IP_MULTICAST_TTL
;
491 case SocketOptionName_MulticastLoopback
:
492 *system_name
= IP_MULTICAST_LOOP
;
494 case SocketOptionName_AddMembership
:
495 *system_name
= IP_ADD_MEMBERSHIP
;
497 case SocketOptionName_DropMembership
:
498 *system_name
= IP_DROP_MEMBERSHIP
;
500 #ifdef HAVE_IP_PKTINFO
501 case SocketOptionName_PacketInformation
:
502 *system_name
= IP_PKTINFO
;
504 #endif /* HAVE_IP_PKTINFO */
506 case SocketOptionName_DontFragment
:
507 #ifdef HAVE_IP_DONTFRAGMENT
508 *system_name
= IP_DONTFRAGMENT
;
510 #elif defined HAVE_IP_MTU_DISCOVER
511 /* Not quite the same */
512 *system_name
= IP_MTU_DISCOVER
;
515 /* If the flag is not available on this system, we can ignore this error */
517 #endif /* HAVE_IP_DONTFRAGMENT */
518 case SocketOptionName_AddSourceMembership
:
519 case SocketOptionName_DropSourceMembership
:
520 case SocketOptionName_BlockSource
:
521 case SocketOptionName_UnblockSource
:
522 /* Can't figure out how to map these, so fall
526 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name
);
532 case SocketOptionLevel_IPv6
:
534 *system_level
= SOL_IPV6
;
537 static int cached
= 0;
541 struct protoent
*pent
;
543 pent
= getprotobyname ("IPV6");
544 proto
= pent
? pent
->p_proto
: 41 /* 41 a good default value?? */;
548 *system_level
= proto
;
550 #endif /* HAVE_SOL_IPV6 */
553 case SocketOptionName_IpTimeToLive
:
554 *system_name
= IPV6_UNICAST_HOPS
;
556 case SocketOptionName_MulticastInterface
:
557 *system_name
= IPV6_MULTICAST_IF
;
559 case SocketOptionName_MulticastTimeToLive
:
560 *system_name
= IPV6_MULTICAST_HOPS
;
562 case SocketOptionName_MulticastLoopback
:
563 *system_name
= IPV6_MULTICAST_LOOP
;
565 case SocketOptionName_AddMembership
:
566 *system_name
= IPV6_JOIN_GROUP
;
568 case SocketOptionName_DropMembership
:
569 *system_name
= IPV6_LEAVE_GROUP
;
571 case SocketOptionName_PacketInformation
:
572 #ifdef HAVE_IPV6_PKTINFO
573 *system_name
= IPV6_PKTINFO
;
576 case SocketOptionName_HeaderIncluded
:
577 case SocketOptionName_IPOptions
:
578 case SocketOptionName_TypeOfService
:
579 case SocketOptionName_DontFragment
:
580 case SocketOptionName_AddSourceMembership
:
581 case SocketOptionName_DropSourceMembership
:
582 case SocketOptionName_BlockSource
:
583 case SocketOptionName_UnblockSource
:
584 /* Can't figure out how to map these, so fall
588 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name
);
592 break; /* SocketOptionLevel_IPv6 */
595 case SocketOptionLevel_Tcp
:
597 *system_level
= SOL_TCP
;
600 static int cached
= 0;
604 struct protoent
*pent
;
606 pent
= getprotobyname ("TCP");
607 proto
= pent
? pent
->p_proto
: 6 /* is 6 a good default value?? */;
611 *system_level
= proto
;
613 #endif /* HAVE_SOL_TCP */
616 case SocketOptionName_NoDelay
:
617 *system_name
= TCP_NODELAY
;
620 /* The documentation is talking complete
621 * bollocks here: rfc-1222 is titled
622 * 'Advancing the NSFNET Routing Architecture'
623 * and doesn't mention either of the words
624 * "expedite" or "urgent".
626 case SocketOptionName_BsdUrgent
:
627 case SocketOptionName_Expedited
:
630 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name
);
635 case SocketOptionLevel_Udp
:
636 g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level
);
639 case SocketOptionName_NoChecksum
:
640 case SocketOptionName_ChecksumCoverage
:
642 g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name
);
649 g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level
);
656 static MonoImage
*get_socket_assembly (void)
658 static const char *version
= NULL
;
659 static gboolean moonlight
;
660 static MonoImage
*socket_assembly
= NULL
;
662 if (version
== NULL
) {
663 version
= mono_get_runtime_info ()->framework_version
;
664 moonlight
= !strcmp (version
, "2.1");
667 if (socket_assembly
== NULL
) {
669 socket_assembly
= mono_image_loaded ("System.Net");
670 if (!socket_assembly
) {
671 MonoAssembly
*sa
= mono_assembly_open ("System.Net.dll", NULL
);
674 g_assert_not_reached ();
676 socket_assembly
= mono_assembly_get_image (sa
);
680 socket_assembly
= mono_image_loaded ("System");
681 if (!socket_assembly
) {
682 MonoAssembly
*sa
= mono_assembly_open ("System.dll", NULL
);
685 g_assert_not_reached ();
687 socket_assembly
= mono_assembly_get_image (sa
);
693 return(socket_assembly
);
697 static gint32
get_family_hint(void)
699 MonoDomain
*domain
= mono_domain_get ();
701 if (!domain
->inet_family_hint
) {
702 MonoClass
*socket_class
;
703 MonoClassField
*ipv6_field
, *ipv4_field
;
704 gint32 ipv6_enabled
= -1, ipv4_enabled
= -1;
707 socket_class
= mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
708 ipv4_field
= mono_class_get_field_from_name (socket_class
, "ipv4Supported");
709 ipv6_field
= mono_class_get_field_from_name (socket_class
, "ipv6Supported");
710 vtable
= mono_class_vtable (mono_domain_get (), socket_class
);
712 mono_runtime_class_init (vtable
);
714 mono_field_static_get_value (vtable
, ipv4_field
, &ipv4_enabled
);
715 mono_field_static_get_value (vtable
, ipv6_field
, &ipv6_enabled
);
717 mono_domain_lock (domain
);
718 if (ipv4_enabled
== 1 && ipv6_enabled
== 1) {
719 domain
->inet_family_hint
= 1;
720 } else if (ipv4_enabled
== 1) {
721 domain
->inet_family_hint
= 2;
723 domain
->inet_family_hint
= 3;
725 mono_domain_unlock (domain
);
727 switch (domain
->inet_family_hint
) {
728 case 1: return PF_UNSPEC
;
729 case 2: return PF_INET
;
730 case 3: return PF_INET6
;
737 gpointer
ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject
*this, gint32 family
, gint32 type
, gint32 proto
, gint32
*error
)
748 sock_family
=convert_family(family
);
749 if(sock_family
==-1) {
750 *error
= WSAEAFNOSUPPORT
;
754 sock_proto
=convert_proto(proto
);
756 *error
= WSAEPROTONOSUPPORT
;
760 sock_type
=convert_type(type
);
762 *error
= WSAESOCKTNOSUPPORT
;
766 sock
= _wapi_socket (sock_family
, sock_type
, sock_proto
,
767 NULL
, 0, WSA_FLAG_OVERLAPPED
);
769 if(sock
==INVALID_SOCKET
) {
770 *error
= WSAGetLastError ();
774 if (sock_family
== AF_INET
&& sock_type
== SOCK_DGRAM
) {
775 return (GUINT_TO_POINTER (sock
));
779 if (sock_family
== AF_INET6
&& sock_type
== SOCK_DGRAM
) {
780 return (GUINT_TO_POINTER (sock
));
784 return(GUINT_TO_POINTER (sock
));
787 /* FIXME: the SOCKET parameter (here and in other functions in this
788 * file) is really an IntPtr which needs to be converted to a guint32.
790 void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock
,
795 LOGDEBUG (g_message ("%s: closing 0x%x", __func__
, sock
));
799 /* Clear any pending work item from this socket if the underlying
800 * polling system does not notify when the socket is closed */
801 mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock
));
805 gint32
ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
809 LOGDEBUG (g_message("%s: returning %d", __func__
, WSAGetLastError()));
811 return(WSAGetLastError());
814 gint32
ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock
,
824 ret
=ioctlsocket(sock
, FIONREAD
, &amount
);
825 if(ret
==SOCKET_ERROR
) {
826 *error
= WSAGetLastError ();
833 void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock
,
844 * block == TRUE/FALSE means we will block/not block.
845 * But the ioctlsocket call takes TRUE/FALSE for non-block/block
849 ret
= ioctlsocket (sock
, FIONBIO
, (gulong
*) &block
);
850 if(ret
==SOCKET_ERROR
) {
851 *error
= WSAGetLastError ();
855 gpointer
ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock
,
866 MonoInternalThread
* curthread
= mono_thread_internal_current ();
867 curthread
->interrupt_on_stop
= (gpointer
)TRUE
;
868 newsock
= _wapi_accept (sock
, NULL
, 0);
869 curthread
->interrupt_on_stop
= (gpointer
)FALSE
;
872 newsock
= _wapi_accept (sock
, NULL
, 0);
874 if(newsock
==INVALID_SOCKET
) {
875 *error
= WSAGetLastError ();
879 return(GUINT_TO_POINTER (newsock
));
882 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock
,
892 ret
= _wapi_listen (sock
, backlog
);
893 if(ret
==SOCKET_ERROR
) {
894 *error
= WSAGetLastError ();
898 static MonoObject
*create_object_from_sockaddr(struct sockaddr
*saddr
,
899 int sa_size
, gint32
*error
)
901 MonoDomain
*domain
= mono_domain_get ();
902 MonoObject
*sockaddr_obj
;
903 MonoClass
*sockaddr_class
;
904 MonoClassField
*field
;
906 MonoAddressFamily family
;
908 /* Build a System.Net.SocketAddress object instance */
909 sockaddr_class
=mono_class_from_name_cached (get_socket_assembly (), "System.Net", "SocketAddress");
910 sockaddr_obj
=mono_object_new(domain
, sockaddr_class
);
912 /* Locate the SocketAddress data buffer in the object */
913 field
=mono_class_get_field_from_name_cached (sockaddr_class
, "data");
915 /* Make sure there is space for the family and size bytes */
917 if (saddr
->sa_family
== AF_UNIX
) {
918 /* sa_len includes the entire sockaddr size, so we don't need the
919 * N bytes (sizeof (unsigned short)) of the family. */
920 data
=mono_array_new_cached(domain
, mono_get_byte_class (), sa_size
);
924 /* May be the +2 here is too conservative, as sa_len returns
925 * the length of the entire sockaddr_in/in6, including
926 * sizeof (unsigned short) of the family */
927 data
=mono_array_new_cached(domain
, mono_get_byte_class (), sa_size
+2);
930 /* The data buffer is laid out as follows:
931 * bytes 0 and 1 are the address family
932 * bytes 2 and 3 are the port info
933 * the rest is the address info
936 family
=convert_to_mono_family(saddr
->sa_family
);
937 if(family
==AddressFamily_Unknown
) {
938 *error
= WSAEAFNOSUPPORT
;
942 mono_array_set(data
, guint8
, 0, family
& 0x0FF);
943 mono_array_set(data
, guint8
, 1, (family
>> 8) & 0x0FF);
945 if(saddr
->sa_family
==AF_INET
) {
946 struct sockaddr_in
*sa_in
=(struct sockaddr_in
*)saddr
;
947 guint16 port
=ntohs(sa_in
->sin_port
);
948 guint32 address
=ntohl(sa_in
->sin_addr
.s_addr
);
951 mono_raise_exception((MonoException
*)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
954 mono_array_set(data
, guint8
, 2, (port
>>8) & 0xff);
955 mono_array_set(data
, guint8
, 3, (port
) & 0xff);
956 mono_array_set(data
, guint8
, 4, (address
>>24) & 0xff);
957 mono_array_set(data
, guint8
, 5, (address
>>16) & 0xff);
958 mono_array_set(data
, guint8
, 6, (address
>>8) & 0xff);
959 mono_array_set(data
, guint8
, 7, (address
) & 0xff);
961 mono_field_set_value (sockaddr_obj
, field
, data
);
963 return(sockaddr_obj
);
965 } else if (saddr
->sa_family
== AF_INET6
) {
966 struct sockaddr_in6
*sa_in
=(struct sockaddr_in6
*)saddr
;
969 guint16 port
=ntohs(sa_in
->sin6_port
);
972 mono_raise_exception((MonoException
*)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
975 mono_array_set(data
, guint8
, 2, (port
>>8) & 0xff);
976 mono_array_set(data
, guint8
, 3, (port
) & 0xff);
978 for(i
=0; i
<16; i
++) {
979 mono_array_set(data
, guint8
, 8+i
,
980 sa_in
->sin6_addr
.s6_addr
[i
]);
983 mono_array_set(data
, guint8
, 24, sa_in
->sin6_scope_id
& 0xff);
984 mono_array_set(data
, guint8
, 25,
985 (sa_in
->sin6_scope_id
>> 8) & 0xff);
986 mono_array_set(data
, guint8
, 26,
987 (sa_in
->sin6_scope_id
>> 16) & 0xff);
988 mono_array_set(data
, guint8
, 27,
989 (sa_in
->sin6_scope_id
>> 24) & 0xff);
991 mono_field_set_value (sockaddr_obj
, field
, data
);
993 return(sockaddr_obj
);
996 } else if (saddr
->sa_family
== AF_UNIX
) {
999 for (i
= 0; i
< sa_size
; i
++) {
1000 mono_array_set (data
, guint8
, i
+2, saddr
->sa_data
[i
]);
1003 mono_field_set_value (sockaddr_obj
, field
, data
);
1005 return sockaddr_obj
;
1008 *error
= WSAEAFNOSUPPORT
;
1013 extern MonoObject
*ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock
, gint32
*error
)
1015 gchar sa
[32]; /* sockaddr in not big enough for sockaddr_in6 */
1019 MONO_ARCH_SAVE_REGS
;
1024 ret
= _wapi_getsockname (sock
, (struct sockaddr
*)sa
, &salen
);
1026 if(ret
==SOCKET_ERROR
) {
1027 *error
= WSAGetLastError ();
1031 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
)));
1033 return(create_object_from_sockaddr((struct sockaddr
*)sa
, salen
,
1037 extern MonoObject
*ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock
, gint32
*error
)
1039 gchar sa
[32]; /* sockaddr in not big enough for sockaddr_in6 */
1043 MONO_ARCH_SAVE_REGS
;
1048 ret
= _wapi_getpeername (sock
, (struct sockaddr
*)sa
, &salen
);
1050 if(ret
==SOCKET_ERROR
) {
1051 *error
= WSAGetLastError ();
1055 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
)));
1057 return(create_object_from_sockaddr((struct sockaddr
*)sa
, salen
,
1061 static struct sockaddr
*create_sockaddr_from_object(MonoObject
*saddr_obj
,
1065 MonoClassField
*field
;
1070 /* Dig the SocketAddress data buffer out of the object */
1071 field
=mono_class_get_field_from_name(saddr_obj
->vtable
->klass
, "data");
1072 data
=*(MonoArray
**)(((char *)saddr_obj
) + field
->offset
);
1074 /* The data buffer is laid out as follows:
1075 * byte 0 is the address family low byte
1076 * byte 1 is the address family high byte
1078 * bytes 2 and 3 are the port info
1079 * the rest is the address info
1081 * the rest is the file name
1083 len
= mono_array_length (data
);
1085 mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
1088 family
= convert_family (mono_array_get (data
, guint8
, 0) + (mono_array_get (data
, guint8
, 1) << 8));
1089 if (family
== AF_INET
) {
1090 struct sockaddr_in
*sa
;
1095 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1098 sa
= g_new0 (struct sockaddr_in
, 1);
1099 port
= (mono_array_get (data
, guint8
, 2) << 8) +
1100 mono_array_get (data
, guint8
, 3);
1101 address
= (mono_array_get (data
, guint8
, 4) << 24) +
1102 (mono_array_get (data
, guint8
, 5) << 16 ) +
1103 (mono_array_get (data
, guint8
, 6) << 8) +
1104 mono_array_get (data
, guint8
, 7);
1106 sa
->sin_family
= family
;
1107 sa
->sin_addr
.s_addr
= htonl (address
);
1108 sa
->sin_port
= htons (port
);
1110 *sa_size
= sizeof(struct sockaddr_in
);
1111 return((struct sockaddr
*)sa
);
1114 } else if (family
== AF_INET6
) {
1115 struct sockaddr_in6
*sa
;
1121 mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
1124 sa
= g_new0 (struct sockaddr_in6
, 1);
1125 port
= mono_array_get (data
, guint8
, 3) +
1126 (mono_array_get (data
, guint8
, 2) << 8);
1127 scopeid
= mono_array_get (data
, guint8
, 24) +
1128 (mono_array_get (data
, guint8
, 25) << 8) +
1129 (mono_array_get (data
, guint8
, 26) << 16) +
1130 (mono_array_get (data
, guint8
, 27) << 24);
1132 sa
->sin6_family
= family
;
1133 sa
->sin6_port
= htons (port
);
1134 sa
->sin6_scope_id
= scopeid
;
1136 for(i
=0; i
<16; i
++) {
1137 sa
->sin6_addr
.s6_addr
[i
] = mono_array_get (data
, guint8
, 8+i
);
1140 *sa_size
= sizeof(struct sockaddr_in6
);
1141 return((struct sockaddr
*)sa
);
1143 #ifdef HAVE_SYS_UN_H
1144 } else if (family
== AF_UNIX
) {
1145 struct sockaddr_un
*sock_un
;
1148 /* Need a byte for the '\0' terminator/prefix, and the first
1149 * two bytes hold the SocketAddress family
1151 if (len
- 2 >= MONO_SIZEOF_SUNPATH
) {
1152 mono_raise_exception (mono_get_exception_index_out_of_range ());
1155 sock_un
= g_new0 (struct sockaddr_un
, 1);
1157 sock_un
->sun_family
= family
;
1158 for (i
= 0; i
< len
- 2; i
++) {
1159 sock_un
->sun_path
[i
] = mono_array_get (data
, guint8
,
1165 return (struct sockaddr
*)sock_un
;
1168 *error
= WSAEAFNOSUPPORT
;
1173 extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock
, MonoObject
*sockaddr
, gint32
*error
)
1175 struct sockaddr
*sa
;
1179 MONO_ARCH_SAVE_REGS
;
1183 sa
=create_sockaddr_from_object(sockaddr
, &sa_size
, error
);
1188 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
)));
1190 ret
= _wapi_bind (sock
, sa
, sa_size
);
1191 if(ret
==SOCKET_ERROR
) {
1192 *error
= WSAGetLastError ();
1205 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock
, gint mode
,
1206 gint timeout
, gint32
*error
)
1208 MonoInternalThread
*thread
= NULL
;
1214 MONO_ARCH_SAVE_REGS
;
1216 pfds
= g_new0 (mono_pollfd
, 1);
1217 pfds
[0].fd
= GPOINTER_TO_INT (sock
);
1218 pfds
[0].events
= (mode
== SelectModeRead
) ? MONO_POLLIN
:
1219 (mode
== SelectModeWrite
) ? MONO_POLLOUT
:
1220 (MONO_POLLERR
| MONO_POLLHUP
| MONO_POLLNVAL
);
1222 timeout
= (timeout
>= 0) ? (timeout
/ 1000) : -1;
1223 start
= time (NULL
);
1227 ret
= mono_poll (pfds
, 1, timeout
);
1228 if (timeout
> 0 && ret
< 0) {
1230 int sec
= time (NULL
) - start
;
1232 timeout
-= sec
* 1000;
1240 if (ret
== -1 && errno
== EINTR
) {
1243 if (thread
== NULL
) {
1244 thread
= mono_thread_internal_current ();
1247 leave
= mono_thread_test_state (thread
, ThreadState_AbortRequested
| ThreadState_StopRequested
);
1253 /* Suspend requested? */
1254 mono_thread_interruption_checkpoint ();
1258 } while (ret
== -1 && errno
== EINTR
);
1262 *error
= WSAGetLastError ();
1264 *error
= errno_to_WSA (errno
, __func__
);
1279 extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock
, MonoObject
*sockaddr
, gint32
*error
)
1281 struct sockaddr
*sa
;
1285 MONO_ARCH_SAVE_REGS
;
1289 sa
=create_sockaddr_from_object(sockaddr
, &sa_size
, error
);
1294 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
)));
1296 ret
= _wapi_connect (sock
, sa
, sa_size
);
1297 if(ret
==SOCKET_ERROR
) {
1298 *error
= WSAGetLastError ();
1304 /* These #defines from mswsock.h from wine. Defining them here allows
1305 * us to build this file on a mingw box that doesn't know the magic
1306 * numbers, but still run on a newer windows box that does.
1308 #ifndef WSAID_DISCONNECTEX
1309 #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
1310 typedef BOOL (WINAPI
*LPFN_DISCONNECTEX
)(SOCKET
, LPOVERLAPPED
, DWORD
, DWORD
);
1313 #ifndef WSAID_TRANSMITFILE
1314 #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1315 typedef BOOL (WINAPI
*LPFN_TRANSMITFILE
)(SOCKET
, HANDLE
, DWORD
, DWORD
, LPOVERLAPPED
, LPTRANSMIT_FILE_BUFFERS
, DWORD
);
1318 extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock
, MonoBoolean reuse
, gint32
*error
)
1321 glong output_bytes
= 0;
1322 GUID disco_guid
= WSAID_DISCONNECTEX
;
1323 GUID trans_guid
= WSAID_TRANSMITFILE
;
1324 LPFN_DISCONNECTEX _wapi_disconnectex
= NULL
;
1325 LPFN_TRANSMITFILE _wapi_transmitfile
= NULL
;
1328 MONO_ARCH_SAVE_REGS
;
1332 LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__
, sock
, reuse
));
1334 /* I _think_ the extension function pointers need to be looked
1335 * up for each socket. FIXME: check the best way to store
1336 * pointers to functions in managed objects that still works
1337 * on 64bit platforms.
1339 ret
= WSAIoctl (sock
, SIO_GET_EXTENSION_FUNCTION_POINTER
,
1340 (void *)&disco_guid
, sizeof(GUID
),
1341 (void *)&_wapi_disconnectex
, sizeof(void *),
1342 &output_bytes
, NULL
, NULL
);
1344 /* make sure that WSAIoctl didn't put crap in the
1347 _wapi_disconnectex
= NULL
;
1349 /* Look up the TransmitFile extension function pointer
1350 * instead of calling TransmitFile() directly, because
1351 * apparently "Several of the extension functions have
1352 * been available since WinSock 1.1 and are exported
1353 * from MSWsock.dll, however it's not advisable to
1354 * link directly to this dll as this ties you to the
1355 * Microsoft WinSock provider. A provider neutral way
1356 * of accessing these extension functions is to load
1357 * them dynamically via WSAIoctl using the
1358 * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
1359 * should, theoretically, allow you to access these
1360 * functions from any provider that supports them..."
1361 * (http://www.codeproject.com/internet/jbsocketserver3.asp)
1363 ret
= WSAIoctl (sock
, SIO_GET_EXTENSION_FUNCTION_POINTER
,
1364 (void *)&trans_guid
, sizeof(GUID
),
1365 (void *)&_wapi_transmitfile
, sizeof(void *),
1366 &output_bytes
, NULL
, NULL
);
1368 _wapi_transmitfile
= NULL
;
1372 if (_wapi_disconnectex
!= NULL
) {
1373 bret
= _wapi_disconnectex (sock
, NULL
, TF_REUSE_SOCKET
, 0);
1374 } else if (_wapi_transmitfile
!= NULL
) {
1375 bret
= _wapi_transmitfile (sock
, NULL
, 0, 0, NULL
, NULL
,
1376 TF_DISCONNECT
| TF_REUSE_SOCKET
);
1378 *error
= ERROR_NOT_SUPPORTED
;
1382 if (bret
== FALSE
) {
1383 *error
= WSAGetLastError ();
1387 gint32
ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock
, MonoArray
*buffer
, gint32 offset
, gint32 count
, gint32 flags
, gint32
*error
)
1394 MONO_ARCH_SAVE_REGS
;
1398 alen
= mono_array_length (buffer
);
1399 if (offset
> alen
- count
) {
1403 buf
=mono_array_addr(buffer
, guchar
, offset
);
1405 recvflags
= convert_socketflags (flags
);
1406 if (recvflags
== -1) {
1407 *error
= WSAEOPNOTSUPP
;
1413 MonoInternalThread
* curthread
= mono_thread_internal_current ();
1414 curthread
->interrupt_on_stop
= (gpointer
)TRUE
;
1415 ret
= _wapi_recv (sock
, buf
, count
, recvflags
);
1416 curthread
->interrupt_on_stop
= (gpointer
)FALSE
;
1419 ret
= _wapi_recv (sock
, buf
, count
, recvflags
);
1422 if(ret
==SOCKET_ERROR
) {
1423 *error
= WSAGetLastError ();
1430 gint32
ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock
, MonoArray
*buffers
, gint32 flags
, gint32
*error
)
1435 DWORD recvflags
= 0;
1437 MONO_ARCH_SAVE_REGS
;
1441 wsabufs
= mono_array_addr (buffers
, WSABUF
, 0);
1442 count
= mono_array_length (buffers
);
1444 recvflags
= convert_socketflags (flags
);
1445 if (recvflags
== -1) {
1446 *error
= WSAEOPNOTSUPP
;
1450 ret
= WSARecv (sock
, wsabufs
, count
, &recv
, &recvflags
, NULL
, NULL
);
1451 if (ret
== SOCKET_ERROR
) {
1452 *error
= WSAGetLastError ();
1459 gint32
ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock
, MonoArray
*buffer
, gint32 offset
, gint32 count
, gint32 flags
, MonoObject
**sockaddr
, gint32
*error
)
1465 struct sockaddr
*sa
;
1468 MONO_ARCH_SAVE_REGS
;
1472 alen
= mono_array_length (buffer
);
1473 if (offset
> alen
- count
) {
1477 sa
=create_sockaddr_from_object(*sockaddr
, &sa_size
, error
);
1482 buf
=mono_array_addr(buffer
, guchar
, offset
);
1484 recvflags
= convert_socketflags (flags
);
1485 if (recvflags
== -1) {
1486 *error
= WSAEOPNOTSUPP
;
1490 ret
= _wapi_recvfrom (sock
, buf
, count
, recvflags
, sa
, &sa_size
);
1491 if(ret
==SOCKET_ERROR
) {
1493 *error
= WSAGetLastError ();
1497 /* If we didn't get a socket size, then we're probably a
1498 * connected connection-oriented socket and the stack hasn't
1499 * returned the remote address. All we can do is return null.
1502 *sockaddr
=create_object_from_sockaddr(sa
, sa_size
, error
);
1511 gint32
ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock
, MonoArray
*buffer
, gint32 offset
, gint32 count
, gint32 flags
, gint32
*error
)
1518 MONO_ARCH_SAVE_REGS
;
1522 alen
= mono_array_length (buffer
);
1523 if (offset
> alen
- count
) {
1527 LOGDEBUG (g_message("%s: alen: %d", __func__
, alen
));
1529 buf
=mono_array_addr(buffer
, guchar
, offset
);
1531 LOGDEBUG (g_message("%s: Sending %d bytes", __func__
, count
));
1533 sendflags
= convert_socketflags (flags
);
1534 if (sendflags
== -1) {
1535 *error
= WSAEOPNOTSUPP
;
1539 ret
= _wapi_send (sock
, buf
, count
, sendflags
);
1540 if(ret
==SOCKET_ERROR
) {
1541 *error
= WSAGetLastError ();
1548 gint32
ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock
, MonoArray
*buffers
, gint32 flags
, gint32
*error
)
1553 DWORD sendflags
= 0;
1555 MONO_ARCH_SAVE_REGS
;
1559 wsabufs
= mono_array_addr (buffers
, WSABUF
, 0);
1560 count
= mono_array_length (buffers
);
1562 sendflags
= convert_socketflags (flags
);
1563 if (sendflags
== -1) {
1564 *error
= WSAEOPNOTSUPP
;
1568 ret
= WSASend (sock
, wsabufs
, count
, &sent
, sendflags
, NULL
, NULL
);
1569 if (ret
== SOCKET_ERROR
) {
1570 *error
= WSAGetLastError ();
1577 gint32
ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock
, MonoArray
*buffer
, gint32 offset
, gint32 count
, gint32 flags
, MonoObject
*sockaddr
, gint32
*error
)
1583 struct sockaddr
*sa
;
1586 MONO_ARCH_SAVE_REGS
;
1590 alen
= mono_array_length (buffer
);
1591 if (offset
> alen
- count
) {
1595 sa
=create_sockaddr_from_object(sockaddr
, &sa_size
, error
);
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 *error
= WSAEOPNOTSUPP
;
1612 ret
= _wapi_sendto (sock
, buf
, count
, sendflags
, sa
, sa_size
);
1613 if(ret
==SOCKET_ERROR
) {
1614 *error
= WSAGetLastError ();
1622 static SOCKET
Socket_to_SOCKET(MonoObject
*sockobj
)
1625 MonoClassField
*field
;
1627 field
=mono_class_get_field_from_name(sockobj
->vtable
->klass
, "socket");
1628 sock
=GPOINTER_TO_INT (*(gpointer
*)(((char *)sockobj
)+field
->offset
));
1633 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
1634 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray
**sockets
, gint32 timeout
, gint32
*error
)
1636 MonoInternalThread
*thread
= NULL
;
1643 MonoClass
*sock_arr_class
;
1646 uintptr_t socks_size
;
1648 MONO_ARCH_SAVE_REGS
;
1650 /* *sockets -> READ, null, WRITE, null, ERROR, null */
1651 count
= mono_array_length (*sockets
);
1652 nfds
= count
- 3; /* NULL separators */
1653 pfds
= g_new0 (mono_pollfd
, nfds
);
1655 for (i
= 0; i
< count
; i
++) {
1656 obj
= mono_array_get (*sockets
, MonoObject
*, i
);
1663 /* The socket array was bogus */
1669 pfds
[idx
].fd
= Socket_to_SOCKET (obj
);
1670 pfds
[idx
].events
= (mode
== 0) ? MONO_POLLIN
: (mode
== 1) ? MONO_POLLOUT
: POLL_ERRORS
;
1674 timeout
= (timeout
>= 0) ? (timeout
/ 1000) : -1;
1675 start
= time (NULL
);
1678 ret
= mono_poll (pfds
, nfds
, timeout
);
1679 if (timeout
> 0 && ret
< 0) {
1681 int sec
= time (NULL
) - start
;
1683 timeout
-= sec
* 1000;
1689 if (ret
== -1 && errno
== EINTR
) {
1692 thread
= mono_thread_internal_current ();
1694 leave
= mono_thread_test_state (thread
, ThreadState_AbortRequested
| ThreadState_StopRequested
);
1701 /* Suspend requested? */
1702 mono_thread_interruption_checkpoint ();
1706 } while (ret
== -1 && errno
== EINTR
);
1710 *error
= WSAGetLastError ();
1712 *error
= errno_to_WSA (errno
, __func__
);
1724 sock_arr_class
= ((MonoObject
*)*sockets
)->vtable
->klass
;
1725 socks_size
= ((uintptr_t)ret
) + 3; /* space for the NULL delimiters */
1726 socks
= mono_array_new_full (mono_domain_get (), sock_arr_class
, &socks_size
, NULL
);
1729 for (i
= 0; i
< count
&& ret
> 0; i
++) {
1732 obj
= mono_array_get (*sockets
, MonoObject
*, i
);
1739 pfd
= &pfds
[i
- mode
];
1740 if (pfd
->revents
== 0)
1744 if (mode
== 0 && (pfd
->revents
& (MONO_POLLIN
| POLL_ERRORS
)) != 0) {
1745 mono_array_setref (socks
, idx
++, obj
);
1746 } else if (mode
== 1 && (pfd
->revents
& (MONO_POLLOUT
| POLL_ERRORS
)) != 0) {
1747 mono_array_setref (socks
, idx
++, obj
);
1748 } else if ((pfd
->revents
& POLL_ERRORS
) != 0) {
1749 mono_array_setref (socks
, idx
++, obj
);
1757 static MonoObject
* int_to_object (MonoDomain
*domain
, int val
)
1759 return mono_value_box (domain
, mono_get_int32_class (), &val
);
1763 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock
, gint32 level
, gint32 name
, MonoObject
**obj_val
, gint32
*error
)
1769 socklen_t valsize
=sizeof(val
);
1770 struct linger linger
;
1771 socklen_t lingersize
=sizeof(linger
);
1773 socklen_t time_ms_size
= sizeof (time_ms
);
1776 socklen_t credsize
= sizeof(cred
);
1778 MonoDomain
*domain
=mono_domain_get();
1780 MonoClass
*obj_class
;
1781 MonoClassField
*field
;
1783 MONO_ARCH_SAVE_REGS
;
1787 ret
=convert_sockopt_level_and_name(level
, name
, &system_level
,
1790 *error
= WSAENOPROTOOPT
;
1794 *obj_val
= int_to_object (domain
, 0);
1798 /* No need to deal with MulticastOption names here, because
1799 * you cant getsockopt AddMembership or DropMembership (the
1800 * int getsockopt will error, causing an exception)
1803 case SocketOptionName_Linger
:
1804 case SocketOptionName_DontLinger
:
1805 ret
= _wapi_getsockopt(sock
, system_level
, system_name
, &linger
,
1809 case SocketOptionName_SendTimeout
:
1810 case SocketOptionName_ReceiveTimeout
:
1811 ret
= _wapi_getsockopt (sock
, system_level
, system_name
, (char *) &time_ms
, &time_ms_size
);
1815 case SocketOptionName_PeerCred
:
1816 ret
= _wapi_getsockopt (sock
, system_level
, system_name
, &cred
,
1822 ret
= _wapi_getsockopt (sock
, system_level
, system_name
, &val
,
1826 if(ret
==SOCKET_ERROR
) {
1827 *error
= WSAGetLastError ();
1832 case SocketOptionName_Linger
:
1833 /* build a System.Net.Sockets.LingerOption */
1834 obj_class
=mono_class_from_name(get_socket_assembly (),
1835 "System.Net.Sockets",
1837 obj
=mono_object_new(domain
, obj_class
);
1839 /* Locate and set the fields "bool enabled" and "int
1842 field
=mono_class_get_field_from_name(obj_class
, "enabled");
1843 *(guint8
*)(((char *)obj
)+field
->offset
)=linger
.l_onoff
;
1845 field
=mono_class_get_field_from_name(obj_class
, "seconds");
1846 *(guint32
*)(((char *)obj
)+field
->offset
)=linger
.l_linger
;
1850 case SocketOptionName_DontLinger
:
1851 /* construct a bool int in val - true if linger is off */
1852 obj
= int_to_object (domain
, !linger
.l_onoff
);
1855 case SocketOptionName_SendTimeout
:
1856 case SocketOptionName_ReceiveTimeout
:
1857 obj
= int_to_object (domain
, time_ms
);
1861 case SocketOptionName_PeerCred
:
1863 /* build a Mono.Posix.PeerCred+PeerCredData if
1866 static MonoImage
*mono_posix_image
= NULL
;
1867 MonoPeerCredData
*cred_data
;
1869 if (mono_posix_image
== NULL
) {
1870 mono_posix_image
=mono_image_loaded ("Mono.Posix");
1871 if (!mono_posix_image
) {
1872 MonoAssembly
*sa
= mono_assembly_open ("Mono.Posix.dll", NULL
);
1874 *error
= WSAENOPROTOOPT
;
1877 mono_posix_image
= mono_assembly_get_image (sa
);
1882 obj_class
= mono_class_from_name(mono_posix_image
,
1885 obj
= mono_object_new(domain
, obj_class
);
1886 cred_data
= (MonoPeerCredData
*)obj
;
1887 cred_data
->pid
= cred
.pid
;
1888 cred_data
->uid
= cred
.uid
;
1889 cred_data
->gid
= cred
.gid
;
1895 obj
= int_to_object (domain
, val
);
1901 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock
, gint32 level
, gint32 name
, MonoArray
**byte_val
, gint32
*error
)
1909 MONO_ARCH_SAVE_REGS
;
1913 ret
=convert_sockopt_level_and_name(level
, name
, &system_level
,
1916 *error
= WSAENOPROTOOPT
;
1922 valsize
=mono_array_length(*byte_val
);
1923 buf
=mono_array_addr(*byte_val
, guchar
, 0);
1925 ret
= _wapi_getsockopt (sock
, system_level
, system_name
, buf
, &valsize
);
1926 if(ret
==SOCKET_ERROR
) {
1927 *error
= WSAGetLastError ();
1931 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
1932 static struct in_addr
ipaddress_to_struct_in_addr(MonoObject
*ipaddr
)
1934 struct in_addr inaddr
;
1935 MonoClassField
*field
;
1937 field
=mono_class_get_field_from_name(ipaddr
->vtable
->klass
, "m_Address");
1939 /* No idea why .net uses a 64bit type to hold a 32bit value...
1941 * Internal value of IPAddess is in little-endian order
1943 inaddr
.s_addr
=GUINT_FROM_LE ((guint32
)*(guint64
*)(((char *)ipaddr
)+field
->offset
));
1949 static struct in6_addr
ipaddress_to_struct_in6_addr(MonoObject
*ipaddr
)
1951 struct in6_addr in6addr
;
1952 MonoClassField
*field
;
1956 field
=mono_class_get_field_from_name(ipaddr
->vtable
->klass
, "m_Numbers");
1957 data
=*(MonoArray
**)(((char *)ipaddr
) + field
->offset
);
1959 /* Solaris has only the 8 bit version. */
1961 for(i
=0; i
<8; i
++) {
1962 guint16 s
= mono_array_get (data
, guint16
, i
);
1963 in6addr
.s6_addr
[2 * i
] = (s
>> 8) & 0xff;
1964 in6addr
.s6_addr
[2 * i
+ 1] = s
& 0xff;
1968 in6addr
.s6_addr16
[i
] = mono_array_get (data
, guint16
, i
);
1972 #endif /* AF_INET6 */
1975 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
)
1977 struct linger linger
;
1987 #ifdef HAVE_SOL_IPV6
1988 sol_ipv6
= SOL_IPV6
;
1991 struct protoent
*pent
;
1992 pent
= getprotobyname ("ipv6");
1993 sol_ipv6
= (pent
!= NULL
) ? pent
->p_proto
: 41;
2001 struct protoent
*pent
;
2002 pent
= getprotobyname ("ip");
2003 sol_ip
= (pent
!= NULL
) ? pent
->p_proto
: 0;
2006 #endif /* AF_INET6 */
2008 MONO_ARCH_SAVE_REGS
;
2010 ret
=convert_sockopt_level_and_name(level
, name
, &system_level
,
2013 *error
= WSAENOPROTOOPT
;
2020 /* Only one of obj_val, byte_val or int_val has data */
2022 MonoClassField
*field
;
2026 case SocketOptionName_Linger
:
2027 /* Dig out "bool enabled" and "int seconds"
2030 field
=mono_class_get_field_from_name(obj_val
->vtable
->klass
, "enabled");
2031 linger
.l_onoff
=*(guint8
*)(((char *)obj_val
)+field
->offset
);
2032 field
=mono_class_get_field_from_name(obj_val
->vtable
->klass
, "seconds");
2033 linger
.l_linger
=*(guint32
*)(((char *)obj_val
)+field
->offset
);
2035 valsize
=sizeof(linger
);
2036 ret
= _wapi_setsockopt (sock
, system_level
,
2037 system_name
, &linger
, valsize
);
2039 case SocketOptionName_AddMembership
:
2040 case SocketOptionName_DropMembership
:
2041 #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
2043 MonoObject
*address
= NULL
;
2046 if(system_level
== sol_ipv6
) {
2047 struct ipv6_mreq mreq6
;
2052 field
= mono_class_get_field_from_name (obj_val
->vtable
->klass
, "group");
2053 address
= *(gpointer
*)(((char *)obj_val
) + field
->offset
);
2056 mreq6
.ipv6mr_multiaddr
= ipaddress_to_struct_in6_addr (address
);
2059 field
=mono_class_get_field_from_name(obj_val
->vtable
->klass
, "ifIndex");
2060 mreq6
.ipv6mr_interface
=*(guint64
*)(((char *)obj_val
)+field
->offset
);
2062 ret
= _wapi_setsockopt (sock
, system_level
,
2063 system_name
, &mreq6
,
2065 } else if(system_level
== sol_ip
)
2066 #endif /* AF_INET6 */
2068 #ifdef HAVE_STRUCT_IP_MREQN
2069 struct ip_mreqn mreq
= {{0}};
2071 struct ip_mreq mreq
= {{0}};
2072 #endif /* HAVE_STRUCT_IP_MREQN */
2074 /* pain! MulticastOption holds two IPAddress
2075 * members, so I have to dig the value out of
2078 field
= mono_class_get_field_from_name (obj_val
->vtable
->klass
, "group");
2079 address
= *(gpointer
*)(((char *)obj_val
) + field
->offset
);
2081 /* address might not be defined and if so, set the address to ADDR_ANY.
2084 mreq
.imr_multiaddr
= ipaddress_to_struct_in_addr (address
);
2087 field
= mono_class_get_field_from_name (obj_val
->vtable
->klass
, "local");
2088 address
= *(gpointer
*)(((char *)obj_val
) + field
->offset
);
2090 #ifdef HAVE_STRUCT_IP_MREQN
2092 mreq
.imr_address
= ipaddress_to_struct_in_addr (address
);
2096 mreq
.imr_interface
= ipaddress_to_struct_in_addr (address
);
2098 #endif /* HAVE_STRUCT_IP_MREQN */
2100 ret
= _wapi_setsockopt (sock
, system_level
,
2106 #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
2108 /* Cause an exception to be thrown */
2112 } else if (byte_val
!=NULL
) {
2113 int valsize
= mono_array_length (byte_val
);
2114 guchar
*buf
= mono_array_addr (byte_val
, guchar
, 0);
2117 case SocketOptionName_DontLinger
:
2119 linger
.l_onoff
= (*buf
) ? 0 : 1;
2120 linger
.l_linger
= 0;
2121 ret
= _wapi_setsockopt (sock
, system_level
, system_name
, &linger
, sizeof (linger
));
2127 ret
= _wapi_setsockopt (sock
, system_level
, system_name
, buf
, valsize
);
2131 /* ReceiveTimeout/SendTimeout get here */
2133 case SocketOptionName_DontLinger
:
2134 linger
.l_onoff
= !int_val
;
2135 linger
.l_linger
= 0;
2136 ret
= _wapi_setsockopt (sock
, system_level
, system_name
, &linger
, sizeof (linger
));
2138 case SocketOptionName_DontFragment
:
2139 #ifdef HAVE_IP_MTU_DISCOVER
2140 /* Fiddle with the value slightly if we're
2144 int_val
= IP_PMTUDISC_DO
;
2150 ret
= _wapi_setsockopt (sock
, system_level
, system_name
, (char *) &int_val
, sizeof (int_val
));
2154 if(ret
==SOCKET_ERROR
) {
2155 *error
= WSAGetLastError ();
2159 void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock
,
2165 MONO_ARCH_SAVE_REGS
;
2169 /* Currently, the values for how (recv=0, send=1, both=2) match
2172 ret
= _wapi_shutdown (sock
, how
);
2173 if(ret
==SOCKET_ERROR
) {
2174 *error
= WSAGetLastError ();
2179 ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock
, gint32 code
,
2181 MonoArray
*output
, gint32
*error
)
2183 glong output_bytes
= 0;
2184 gchar
*i_buffer
, *o_buffer
;
2188 MONO_ARCH_SAVE_REGS
;
2192 if (code
== FIONBIO
) {
2193 /* Invalid command. Must use Socket.Blocking */
2197 if (input
== NULL
) {
2201 i_buffer
= mono_array_addr (input
, gchar
, 0);
2202 i_len
= mono_array_length (input
);
2205 if (output
== NULL
) {
2209 o_buffer
= mono_array_addr (output
, gchar
, 0);
2210 o_len
= mono_array_length (output
);
2213 ret
= WSAIoctl (sock
, code
, i_buffer
, i_len
, o_buffer
, o_len
, &output_bytes
, NULL
, NULL
);
2214 if (ret
== SOCKET_ERROR
) {
2215 *error
= WSAGetLastError ();
2219 return (gint
) output_bytes
;
2222 #ifdef HAVE_SIOCGIFCONF
2224 is_loopback (int family
, void *ad
)
2226 char *ptr
= (char *) ad
;
2228 if (family
== AF_INET
) {
2229 return (ptr
[0] == 127);
2233 return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr
*) ptr
));
2240 get_local_ips (int family
, int *nips
)
2242 int addr_size
, offset
, fd
, i
, count
;
2243 int max_ifaces
= 50; /* 50 interfaces should be enough... */
2246 struct ifreq iflags
;
2247 char *result
, *tmp_ptr
;
2248 gboolean ignore_loopback
= FALSE
;
2251 if (family
== AF_INET
) {
2252 addr_size
= sizeof (struct in_addr
);
2253 offset
= G_STRUCT_OFFSET (struct sockaddr_in
, sin_addr
);
2255 } else if (family
== AF_INET6
) {
2256 addr_size
= sizeof (struct in6_addr
);
2257 offset
= G_STRUCT_OFFSET (struct sockaddr_in6
, sin6_addr
);
2263 fd
= socket (family
, SOCK_STREAM
, 0);
2265 ifc
.ifc_len
= max_ifaces
* sizeof (struct ifreq
);
2266 ifc
.ifc_buf
= g_malloc (ifc
.ifc_len
);
2267 if (ioctl (fd
, SIOCGIFCONF
, &ifc
) < 0) {
2269 g_free (ifc
.ifc_buf
);
2273 count
= ifc
.ifc_len
/ sizeof (struct ifreq
);
2276 g_free (ifc
.ifc_buf
);
2281 for (i
= 0, ifr
= ifc
.ifc_req
; i
< *nips
; i
++, ifr
++) {
2282 strcpy (iflags
.ifr_name
, ifr
->ifr_name
);
2283 if (ioctl (fd
, SIOCGIFFLAGS
, &iflags
) < 0) {
2287 if ((iflags
.ifr_flags
& IFF_UP
) == 0) {
2288 ifr
->ifr_name
[0] = '\0';
2292 if ((iflags
.ifr_flags
& IFF_LOOPBACK
) == 0) {
2293 ignore_loopback
= TRUE
;
2298 result
= g_malloc (addr_size
* count
);
2300 for (i
= 0, ifr
= ifc
.ifc_req
; i
< count
; i
++, ifr
++) {
2301 if (ifr
->ifr_name
[0] == '\0') {
2306 if (ignore_loopback
&& is_loopback (family
, ((char *) &ifr
->ifr_addr
) + offset
)) {
2311 memcpy (tmp_ptr
, ((char *) &ifr
->ifr_addr
) + offset
, addr_size
);
2312 tmp_ptr
+= addr_size
;
2315 g_free (ifc
.ifc_buf
);
2320 get_local_ips (int family
, int *nips
)
2326 #endif /* HAVE_SIOCGIFCONF */
2329 static gboolean
hostent_to_IPHostEntry(struct hostent
*he
, MonoString
**h_name
,
2330 MonoArray
**h_aliases
,
2331 MonoArray
**h_addr_list
,
2332 gboolean add_local_ips
)
2334 MonoDomain
*domain
= mono_domain_get ();
2336 struct in_addr
*local_in
= NULL
;
2340 if(he
->h_length
!=4 || he
->h_addrtype
!=AF_INET
) {
2344 *h_name
=mono_string_new(domain
, he
->h_name
);
2346 while(he
->h_aliases
[i
]!=NULL
) {
2350 *h_aliases
=mono_array_new(domain
, mono_get_string_class (), i
);
2352 while(he
->h_aliases
[i
]!=NULL
) {
2355 alias
=mono_string_new(domain
, he
->h_aliases
[i
]);
2356 mono_array_setref (*h_aliases
, i
, alias
);
2359 } else if (!add_local_ips
) {
2363 if (add_local_ips
) {
2364 local_in
= (struct in_addr
*) get_local_ips (AF_INET
, &nlocal_in
);
2366 *h_addr_list
= mono_array_new(domain
, mono_get_string_class (), nlocal_in
);
2367 for (i
= 0; i
< nlocal_in
; i
++) {
2368 MonoString
*addr_string
;
2369 char addr
[16], *ptr
;
2371 ptr
= (char *) &local_in
[i
];
2372 g_snprintf(addr
, 16, "%u.%u.%u.%u",
2373 (unsigned char) ptr
[0],
2374 (unsigned char) ptr
[1],
2375 (unsigned char) ptr
[2],
2376 (unsigned char) ptr
[3]);
2378 addr_string
= mono_string_new (domain
, addr
);
2379 mono_array_setref (*h_addr_list
, i
, addr_string
);
2384 } else if (he
== NULL
) {
2385 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2386 *h_addr_list
= mono_array_new(domain
, mono_get_string_class (), 1);
2387 mono_array_setref (*h_addr_list
, 0, mono_string_new (domain
, "127.0.0.1"));
2392 if (nlocal_in
== 0 && he
!= NULL
) {
2394 while (he
->h_addr_list
[i
]!=NULL
) {
2398 *h_addr_list
=mono_array_new(domain
, mono_get_string_class (), i
);
2400 while(he
->h_addr_list
[i
]!=NULL
) {
2401 MonoString
*addr_string
;
2404 g_snprintf(addr
, 16, "%u.%u.%u.%u",
2405 (unsigned char)he
->h_addr_list
[i
][0],
2406 (unsigned char)he
->h_addr_list
[i
][1],
2407 (unsigned char)he
->h_addr_list
[i
][2],
2408 (unsigned char)he
->h_addr_list
[i
][3]);
2410 addr_string
=mono_string_new(domain
, addr
);
2411 mono_array_setref (*h_addr_list
, i
, addr_string
);
2419 static gboolean
ipaddr_to_IPHostEntry(const char *addr
, MonoString
**h_name
,
2420 MonoArray
**h_aliases
,
2421 MonoArray
**h_addr_list
)
2423 MonoDomain
*domain
= mono_domain_get ();
2425 *h_name
=mono_string_new(domain
, addr
);
2426 *h_aliases
=mono_array_new(domain
, mono_get_string_class (), 0);
2427 *h_addr_list
=mono_array_new(domain
, mono_get_string_class (), 1);
2428 mono_array_setref (*h_addr_list
, 0, *h_name
);
2434 #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
2435 static gboolean
hostent_to_IPHostEntry2(struct hostent
*he1
,struct hostent
*he2
, MonoString
**h_name
,
2436 MonoArray
**h_aliases
, MonoArray
**h_addr_list
, gboolean add_local_ips
)
2438 MonoDomain
*domain
= mono_domain_get ();
2439 int i
, host_count
, host_index
, family_hint
;
2440 struct in_addr
*local_in
= NULL
;
2442 struct in6_addr
*local_in6
= NULL
;
2444 gboolean from_local
= FALSE
;
2446 family_hint
= get_family_hint ();
2449 * Check if address length and family are correct
2451 if (he1
!= NULL
&& (he1
->h_length
!=4 || he1
->h_addrtype
!=AF_INET
)) {
2455 if (he2
!= NULL
&& (he2
->h_length
!=16 || he2
->h_addrtype
!=AF_INET6
)) {
2460 * Get the aliases and host name from he1 or he2 whichever is
2461 * not null, if he1 is not null then take aliases from he1
2463 if (he1
!= NULL
&& (family_hint
== PF_UNSPEC
||
2464 family_hint
== PF_INET
)) {
2465 *h_name
=mono_string_new (domain
, he1
->h_name
);
2468 while(he1
->h_aliases
[i
]!=NULL
) {
2472 *h_aliases
=mono_array_new (domain
, mono_get_string_class (),
2475 while(he1
->h_aliases
[i
]!=NULL
) {
2478 alias
=mono_string_new (domain
, he1
->h_aliases
[i
]);
2479 mono_array_setref (*h_aliases
, i
, alias
);
2482 } else if (he2
!= NULL
&& (family_hint
== PF_UNSPEC
||
2483 family_hint
== PF_INET6
)) {
2484 *h_name
=mono_string_new (domain
, he2
->h_name
);
2487 while(he2
->h_aliases
[i
] != NULL
) {
2491 *h_aliases
=mono_array_new (domain
, mono_get_string_class (),
2494 while(he2
->h_aliases
[i
]!=NULL
) {
2497 alias
=mono_string_new (domain
, he2
->h_aliases
[i
]);
2498 mono_array_setref (*h_aliases
, i
, alias
);
2501 } else if (!add_local_ips
) {
2506 * Count the number of addresses in he1 + he2
2509 if (he1
!= NULL
&& (family_hint
== PF_UNSPEC
||
2510 family_hint
== PF_INET
)) {
2512 while(he1
->h_addr_list
[i
]!=NULL
) {
2518 if (he2
!= NULL
&& (family_hint
== PF_UNSPEC
||
2519 family_hint
== PF_INET6
)) {
2521 while(he2
->h_addr_list
[i
]!=NULL
) {
2531 if (add_local_ips
) {
2532 if (family_hint
== PF_UNSPEC
|| family_hint
== PF_INET
)
2533 local_in
= (struct in_addr
*) get_local_ips (AF_INET
, &nlocal_in
);
2535 if (family_hint
== PF_UNSPEC
|| family_hint
== PF_INET6
)
2536 local_in6
= (struct in6_addr
*) get_local_ips (AF_INET6
, &nlocal_in6
);
2538 if (nlocal_in
|| nlocal_in6
) {
2540 *h_addr_list
= mono_array_new (domain
, mono_get_string_class (),
2541 nlocal_in
+ nlocal_in6
);
2545 for (n
= 0; n
< nlocal_in6
; n
++) {
2546 MonoString
*addr_string
;
2548 char addr
[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
2550 ret
= inet_ntop (AF_INET6
, &local_in6
[n
], addr
, sizeof(addr
));
2553 addr_string
= mono_string_new (domain
, addr
);
2554 mono_array_setref (*h_addr_list
, host_index
, addr_string
);
2562 for (n
= 0; n
< nlocal_in
; n
++) {
2563 MonoString
*addr_string
;
2565 char addr
[16]; /* INET_ADDRSTRLEN == 16 */
2567 ret
= inet_ntop (AF_INET
, &local_in
[n
], addr
, sizeof(addr
));
2570 addr_string
= mono_string_new (domain
, addr
);
2571 mono_array_setref (*h_addr_list
, host_index
, addr_string
);
2579 } else if (he1
== NULL
&& he2
== NULL
) {
2580 /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
2581 *h_addr_list
= mono_array_new(domain
, mono_get_string_class (), 1);
2582 mono_array_setref (*h_addr_list
, 0, mono_string_new (domain
, "127.0.0.1"));
2592 *h_addr_list
=mono_array_new (domain
, mono_get_string_class (), host_count
);
2594 if (he2
!= NULL
&& (family_hint
== PF_UNSPEC
||
2595 family_hint
== PF_INET6
)) {
2597 while(he2
->h_addr_list
[i
] != NULL
) {
2598 MonoString
*addr_string
;
2600 char addr
[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
2602 ret
= inet_ntop (AF_INET6
, he2
->h_addr_list
[i
], addr
,
2606 addr_string
= mono_string_new (domain
, addr
);
2607 mono_array_setref (*h_addr_list
, host_index
, addr_string
);
2614 if (he1
!= NULL
&& (family_hint
== PF_UNSPEC
||
2615 family_hint
== PF_INET
)) {
2617 while(he1
->h_addr_list
[i
] != NULL
) {
2618 MonoString
*addr_string
;
2620 char addr
[16]; /* INET_ADDRSTRLEN == 16 */
2622 ret
= inet_ntop (AF_INET
, he1
->h_addr_list
[i
], addr
,
2626 addr_string
=mono_string_new (domain
, addr
);
2627 mono_array_setref (*h_addr_list
, host_index
, addr_string
);
2638 #if defined(AF_INET6)
2640 addrinfo_to_IPHostEntry(struct addrinfo
*info
, MonoString
**h_name
,
2641 MonoArray
**h_aliases
,
2642 MonoArray
**h_addr_list
,
2643 gboolean add_local_ips
)
2646 struct addrinfo
*ai
= NULL
;
2647 struct in_addr
*local_in
= NULL
;
2649 struct in6_addr
*local_in6
= NULL
;
2653 MonoDomain
*domain
= mono_domain_get ();
2656 *h_aliases
=mono_array_new(domain
, mono_get_string_class (), 0);
2657 if (add_local_ips
) {
2658 local_in
= (struct in_addr
*) get_local_ips (AF_INET
, &nlocal_in
);
2659 local_in6
= (struct in6_addr
*) get_local_ips (AF_INET6
, &nlocal_in6
);
2660 if (nlocal_in
|| nlocal_in6
) {
2661 *h_addr_list
=mono_array_new(domain
, mono_get_string_class (), nlocal_in
+ nlocal_in6
);
2663 MonoString
*addr_string
;
2667 for (i
= 0; i
< nlocal_in
; i
++) {
2668 inet_ntop (AF_INET
, &local_in
[i
], addr
, sizeof (addr
));
2669 addr_string
= mono_string_new (domain
, addr
);
2670 mono_array_setref (*h_addr_list
, addr_index
, addr_string
);
2676 MonoString
*addr_string
;
2681 for (i
= 0; i
< nlocal_in6
; i
++) {
2682 ret
= inet_ntop (AF_INET6
, &local_in6
[i
], addr
, sizeof (addr
));
2684 addr_string
= mono_string_new (domain
, addr
);
2685 mono_array_setref (*h_addr_list
, addr_index
, addr_string
);
2694 freeaddrinfo (info
);
2703 for (count
=0, ai
=info
; ai
!=NULL
; ai
=ai
->ai_next
) {
2704 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
2710 *h_addr_list
=mono_array_new(domain
, mono_get_string_class (), count
);
2712 for (ai
=info
, i
=0; ai
!=NULL
; ai
=ai
->ai_next
) {
2713 MonoString
*addr_string
;
2715 char buffer
[48]; /* Max. size for IPv6 */
2717 if((ai
->ai_family
!= PF_INET
) && (ai
->ai_family
!= PF_INET6
)) {
2721 if(ai
->ai_family
== PF_INET
) {
2722 ret
= inet_ntop(ai
->ai_family
, (void*)&(((struct sockaddr_in
*)ai
->ai_addr
)->sin_addr
), buffer
, 16);
2724 ret
= inet_ntop(ai
->ai_family
, (void*)&(((struct sockaddr_in6
*)ai
->ai_addr
)->sin6_addr
), buffer
, 48);
2728 addr_string
=mono_string_new(domain
, buffer
);
2730 addr_string
=mono_string_new(domain
, "");
2733 mono_array_setref (*h_addr_list
, addr_index
, addr_string
);
2736 if (ai
->ai_canonname
!= NULL
) {
2737 *h_name
=mono_string_new(domain
, ai
->ai_canonname
);
2739 *h_name
=mono_string_new(domain
, buffer
);
2755 MonoBoolean
ves_icall_System_Net_Dns_GetHostByName_internal(MonoString
*host
, MonoString
**h_name
, MonoArray
**h_aliases
, MonoArray
**h_addr_list
)
2757 gboolean add_local_ips
= FALSE
;
2758 #ifdef HAVE_SIOCGIFCONF
2759 gchar this_hostname
[256];
2761 #if !defined(HAVE_GETHOSTBYNAME2_R)
2762 struct addrinfo
*info
= NULL
, hints
;
2765 MONO_ARCH_SAVE_REGS
;
2767 hostname
=mono_string_to_utf8 (host
);
2768 if (*hostname
== '\0')
2769 add_local_ips
= TRUE
;
2770 #ifdef HAVE_SIOCGIFCONF
2771 if (!add_local_ips
&& gethostname (this_hostname
, sizeof (this_hostname
)) != -1) {
2772 if (!strcmp (hostname
, this_hostname
))
2773 add_local_ips
= TRUE
;
2777 memset(&hints
, 0, sizeof(hints
));
2778 hints
.ai_family
= get_family_hint ();
2779 hints
.ai_socktype
= SOCK_STREAM
;
2780 hints
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
2782 if (*hostname
&& getaddrinfo(hostname
, NULL
, &hints
, &info
) == -1) {
2788 return(addrinfo_to_IPHostEntry(info
, h_name
, h_aliases
, h_addr_list
, add_local_ips
));
2790 struct hostent he1
,*hp1
, he2
, *hp2
;
2791 int buffer_size1
, buffer_size2
;
2792 char *buffer1
, *buffer2
;
2794 gboolean return_value
;
2797 MONO_ARCH_SAVE_REGS
;
2799 hostname
=mono_string_to_utf8 (host
);
2800 if (*hostname
== '\0')
2801 add_local_ips
= TRUE
;
2803 #ifdef HAVE_SIOCGIFCONF
2804 if (!add_local_ips
&& gethostname (this_hostname
, sizeof (this_hostname
)) != -1) {
2805 if (!strcmp (hostname
, this_hostname
))
2806 add_local_ips
= TRUE
;
2812 buffer1
= g_malloc0(buffer_size1
);
2813 buffer2
= g_malloc0(buffer_size2
);
2817 while (*hostname
&& gethostbyname2_r(hostname
, AF_INET
, &he1
, buffer1
, buffer_size1
,
2818 &hp1
, &herr
) == ERANGE
) {
2820 buffer1
= g_realloc(buffer1
, buffer_size1
);
2823 if (*hostname
&& hp1
== NULL
)
2825 while (gethostbyname2_r(hostname
, AF_INET6
, &he2
, buffer2
,
2826 buffer_size2
, &hp2
, &herr
) == ERANGE
) {
2828 buffer2
= g_realloc(buffer2
, buffer_size2
);
2832 return_value
= hostent_to_IPHostEntry2(hp1
, hp2
, h_name
, h_aliases
,
2833 h_addr_list
, add_local_ips
);
2839 return(return_value
);
2840 #endif /* HAVE_GETHOSTBYNAME2_R */
2842 #else /* AF_INET6 */
2843 MonoBoolean
ves_icall_System_Net_Dns_GetHostByName_internal(MonoString
*host
, MonoString
**h_name
, MonoArray
**h_aliases
, MonoArray
**h_addr_list
)
2847 gboolean add_local_ips
= FALSE
;
2848 #ifdef HAVE_SIOCGIFCONF
2849 gchar this_hostname
[256];
2852 MONO_ARCH_SAVE_REGS
;
2854 hostname
=mono_string_to_utf8(host
);
2855 if (*hostname
== '\0')
2856 add_local_ips
= TRUE
;
2857 #ifdef HAVE_SIOCGIFCONF
2858 if (!add_local_ips
&& gethostname (this_hostname
, sizeof (this_hostname
)) != -1) {
2859 if (!strcmp (hostname
, this_hostname
))
2860 add_local_ips
= TRUE
;
2867 he
= _wapi_gethostbyname (hostname
);
2869 he
= _wapi_gethostbyname (hostname
);
2873 if (*hostname
&& he
==NULL
)
2876 return(hostent_to_IPHostEntry(he
, h_name
, h_aliases
, h_addr_list
, add_local_ips
));
2878 #endif /* AF_INET6 */
2880 #ifndef HAVE_INET_PTON
2882 inet_pton (int family
, const char *address
, void *inaddrp
)
2884 if (family
== AF_INET
) {
2885 #ifdef HAVE_INET_ATON
2886 struct in_addr inaddr
;
2888 if (!inet_aton (address
, &inaddr
))
2891 memcpy (inaddrp
, &inaddr
, sizeof (struct in_addr
));
2894 /* assume the system has inet_addr(), if it doesn't
2895 have that we're pretty much screwed... */
2898 if (!strcmp (address
, "255.255.255.255")) {
2899 /* special-case hack */
2900 inaddr
= 0xffffffff;
2902 inaddr
= inet_addr (address
);
2904 #define INADDR_NONE ((in_addr_t) -1)
2906 if (inaddr
== INADDR_NONE
)
2910 memcpy (inaddrp
, &inaddr
, sizeof (guint32
));
2912 #endif /* HAVE_INET_ATON */
2917 #endif /* !HAVE_INET_PTON */
2919 extern MonoBoolean
ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString
*addr
, MonoString
**h_name
, MonoArray
**h_aliases
, MonoArray
**h_addr_list
)
2924 struct sockaddr_in saddr
;
2925 struct sockaddr_in6 saddr6
;
2926 struct addrinfo
*info
= NULL
, hints
;
2928 char hostname
[1024] = {0};
2931 struct in_addr inaddr
;
2936 address
= mono_string_to_utf8 (addr
);
2939 if (inet_pton (AF_INET
, address
, &saddr
.sin_addr
) <= 0) {
2940 /* Maybe an ipv6 address */
2941 if (inet_pton (AF_INET6
, address
, &saddr6
.sin6_addr
) <= 0) {
2947 saddr6
.sin6_family
= AF_INET6
;
2952 saddr
.sin_family
= AF_INET
;
2956 if(family
== AF_INET
) {
2957 #if HAVE_SOCKADDR_IN_SIN_LEN
2958 saddr
.sin_len
= sizeof (saddr
);
2960 if(getnameinfo ((struct sockaddr
*)&saddr
, sizeof(saddr
),
2961 hostname
, sizeof(hostname
), NULL
, 0,
2965 } else if(family
== AF_INET6
) {
2966 #if HAVE_SOCKADDR_IN6_SIN_LEN
2967 saddr6
.sin6_len
= sizeof (saddr6
);
2969 if(getnameinfo ((struct sockaddr
*)&saddr6
, sizeof(saddr6
),
2970 hostname
, sizeof(hostname
), NULL
, 0,
2976 memset (&hints
, 0, sizeof(hints
));
2977 hints
.ai_family
= get_family_hint ();
2978 hints
.ai_socktype
= SOCK_STREAM
;
2979 hints
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
2981 if( getaddrinfo (hostname
, NULL
, &hints
, &info
) == -1 ) {
2985 return(addrinfo_to_IPHostEntry (info
, h_name
, h_aliases
, h_addr_list
, FALSE
));
2987 if (inet_pton (AF_INET
, address
, &inaddr
) <= 0) {
2992 if ((he
= gethostbyaddr ((char *) &inaddr
, sizeof (inaddr
), AF_INET
)) == NULL
) {
2993 ret
= ipaddr_to_IPHostEntry (address
, h_name
, h_aliases
, h_addr_list
);
2995 ret
= hostent_to_IPHostEntry (he
, h_name
, h_aliases
,
2996 h_addr_list
, FALSE
);
3004 extern MonoBoolean
ves_icall_System_Net_Dns_GetHostName_internal(MonoString
**h_name
)
3006 gchar hostname
[256];
3009 MONO_ARCH_SAVE_REGS
;
3011 ret
= gethostname (hostname
, sizeof (hostname
));
3016 *h_name
=mono_string_new(mono_domain_get (), hostname
);
3022 ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock
, MonoString
*filename
, MonoArray
*pre_buffer
, MonoArray
*post_buffer
, gint flags
)
3026 TRANSMIT_FILE_BUFFERS buffers
;
3028 MONO_ARCH_SAVE_REGS
;
3030 if (filename
== NULL
)
3033 file
= ves_icall_System_IO_MonoIO_Open (filename
, FileMode_Open
, FileAccess_Read
, FileShare_Read
, 0, &error
);
3034 if (file
== INVALID_HANDLE_VALUE
) {
3035 SetLastError (error
);
3039 memset (&buffers
, 0, sizeof (buffers
));
3040 if (pre_buffer
!= NULL
) {
3041 buffers
.Head
= mono_array_addr (pre_buffer
, guchar
, 0);
3042 buffers
.HeadLength
= mono_array_length (pre_buffer
);
3044 if (post_buffer
!= NULL
) {
3045 buffers
.Tail
= mono_array_addr (post_buffer
, guchar
, 0);
3046 buffers
.TailLength
= mono_array_length (post_buffer
);
3049 if (!TransmitFile (sock
, file
, 0, 0, NULL
, &buffers
, flags
)) {
3058 void mono_network_init(void)
3063 err
=WSAStartup(MAKEWORD(2,0), &wsadata
);
3065 g_error("%s: Couldn't initialise networking", __func__
);
3069 LOGDEBUG (g_message("%s: Using socket library: %s", __func__
, wsadata
.szDescription
));
3070 LOGDEBUG (g_message("%s: Socket system status: %s", __func__
, wsadata
.szSystemStatus
));
3073 void mono_network_cleanup(void)