/*
* socket-io.c: Socket IO internal calls
*
- * Author:
+ * Authors:
* Dick Porter (dick@ximian.com)
+ * Gonzalo Paniagua Javier (gonzalo@ximian.com)
*
* (C) 2001 Ximian, Inc.
+ * Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
*/
#include <config.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
+#include <errno.h>
#include <mono/metadata/object.h>
#include <mono/io-layer/io-layer.h>
#include <mono/metadata/socket-io.h>
#include <mono/metadata/exception.h>
+#include <mono/metadata/assembly.h>
#include <mono/metadata/appdomain.h>
+#include <mono/metadata/threads.h>
+#include <mono/metadata/threads-types.h>
+#include <mono/utils/mono-poll.h>
+/* FIXME change this code to not mess so much with the internals */
+#include <mono/metadata/class-internals.h>
+#include <mono/metadata/threadpool-internals.h>
#include <sys/time.h>
+#ifdef HAVE_SYS_IOCTL_H
+#include <sys/ioctl.h>
+#endif
+#ifdef HAVE_NET_IF_H
+#include <net/if.h>
+#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#include <sys/un.h>
#endif
+#include "mono/io-layer/socket-wrappers.h"
+#ifdef PLATFORM_WIN32
+/* This is a kludge to make this file build under cygwin:
+ * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
+ * prototypes for some but not all required functions (notably
+ * inet_ntop() is missing), but the libws2_32 library is missing the
+ * actual implementations of these functions.
+ */
+#undef AF_INET6
+#endif
#undef DEBUG
family=AF_INET;
break;
-#ifdef AF_IPX
case AddressFamily_Ipx:
+#ifdef AF_IPX
family=AF_IPX;
- break;
#endif
+ break;
case AddressFamily_Sna:
family=AF_SNA;
family=AF_APPLETALK;
break;
-#ifdef AF_INET6
case AddressFamily_InterNetworkV6:
+#ifdef AF_INET6
family=AF_INET6;
- break;
#endif
-#ifdef AF_IRDA
+ break;
case AddressFamily_Irda:
+#ifdef AF_IRDA
family=AF_IRDA;
- break;
#endif
+ break;
default:
g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
}
switch(mono_proto) {
case ProtocolType_IP:
+ case ProtocolType_IPv6:
case ProtocolType_Icmp:
case ProtocolType_Igmp:
case ProtocolType_Ggp:
case SocketOptionName_Type:
*system_name = SO_TYPE;
break;
+#ifdef SO_PEERCRED
+ case SocketOptionName_PeerCred:
+ *system_name = SO_PEERCRED;
+ break;
+#endif
case SocketOptionName_ExclusiveAddressUse:
case SocketOptionName_UseLoopback:
case SocketOptionName_MaxConnections:
return(-1);
}
break;
+
+#ifdef AF_INET6
+ case SocketOptionLevel_IPv6:
+#ifdef HAVE_SOL_IPV6
+ *system_level = SOL_IPV6;
+#else
+ if (1) {
+ static int cached = 0;
+ static int proto;
+
+ if (!cached) {
+ struct protoent *pent;
+
+ pent = getprotobyname ("IPV6");
+ proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
+ cached = 1;
+ }
+
+ *system_level = proto;
+ }
+#endif /* HAVE_SOL_IPV6 */
+
+ switch(mono_name) {
+ case SocketOptionName_IpTimeToLive:
+ *system_name = IPV6_UNICAST_HOPS;
+ break;
+ case SocketOptionName_MulticastInterface:
+ *system_name = IPV6_MULTICAST_IF;
+ break;
+ case SocketOptionName_MulticastTimeToLive:
+ *system_name = IPV6_MULTICAST_HOPS;
+ break;
+ case SocketOptionName_MulticastLoopback:
+ *system_name = IPV6_MULTICAST_LOOP;
+ break;
+ case SocketOptionName_AddMembership:
+ *system_name = IPV6_JOIN_GROUP;
+ break;
+ case SocketOptionName_DropMembership:
+ *system_name = IPV6_LEAVE_GROUP;
+ break;
+ case SocketOptionName_PacketInformation:
+#ifdef HAVE_IPV6_PKTINFO
+ *system_name = IPV6_PKTINFO;
+#endif
+ break;
+ case SocketOptionName_HeaderIncluded:
+ case SocketOptionName_IPOptions:
+ case SocketOptionName_TypeOfService:
+ case SocketOptionName_DontFragment:
+ case SocketOptionName_AddSourceMembership:
+ case SocketOptionName_DropSourceMembership:
+ case SocketOptionName_BlockSource:
+ case SocketOptionName_UnblockSource:
+ /* Can't figure out how to map these, so fall
+ * through
+ */
+ default:
+ g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
+ return(-1);
+ }
+
+ break; /* SocketOptionLevel_IPv6 */
+#endif
case SocketOptionLevel_Tcp:
#ifdef HAVE_SOL_TCP
#define STASH_SYS_ASS(this) \
if(system_assembly == NULL) { \
- system_assembly=this->vtable->klass->image; \
+ system_assembly=mono_image_loaded ("System"); \
+ if (!system_assembly) { \
+ MonoAssembly *sa = mono_assembly_open ("System.dll", NULL); \
+ if (!sa) g_assert_not_reached (); \
+ else {system_assembly = mono_assembly_get_image (sa);} \
+ } \
}
static MonoImage *system_assembly=NULL;
-static MonoException *get_socket_exception(guint32 error_code)
+
+#ifdef AF_INET6
+static gint32 get_family_hint(void)
{
- /* Don't cache this exception, because we need the object
- * constructor to set up the message from the sockets error code.
- */
- MonoException *ex;
-
- /* This is a bit of a kludge. The SocketException 0-arg
- * constructor calls WSAGetLastError() to find the error code
- * to use. Until we can init objects with parameters, this
- * will have to do.
- */
- WSASetLastError(error_code);
-
- ex=(MonoException *)mono_exception_from_name(system_assembly,
- "System.Net.Sockets",
- "SocketException");
- return(ex);
+ MonoClass *socket_class;
+ MonoClassField *ipv6_field, *ipv4_field;
+ gint32 ipv6_enabled = -1, ipv4_enabled = -1;
+ MonoVTable *vtable;
+
+ socket_class = mono_class_from_name (system_assembly,
+ "System.Net.Sockets", "Socket");
+ ipv4_field = mono_class_get_field_from_name (socket_class,
+ "ipv4Supported");
+ ipv6_field = mono_class_get_field_from_name (socket_class,
+ "ipv6Supported");
+ vtable = mono_class_vtable (mono_domain_get (), socket_class);
+
+ mono_field_static_get_value(vtable, ipv4_field, &ipv4_enabled);
+ mono_field_static_get_value(vtable, ipv6_field, &ipv6_enabled);
+
+ if(ipv4_enabled == 1 && ipv6_enabled == 1) {
+ return(PF_UNSPEC);
+ } else if(ipv4_enabled == 1) {
+ return(PF_INET);
+ } else {
+ return(PF_INET6);
+ }
}
+#endif
-gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto)
+gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
{
SOCKET sock;
gint32 sock_family;
gint32 sock_proto;
gint32 sock_type;
- int ret;
- int true=1;
MONO_ARCH_SAVE_REGS;
STASH_SYS_ASS(this);
+ *error = 0;
+
sock_family=convert_family(family);
if(sock_family==-1) {
- mono_raise_exception(get_socket_exception(WSAEAFNOSUPPORT));
+ *error = WSAEAFNOSUPPORT;
return(NULL);
}
sock_proto=convert_proto(proto);
if(sock_proto==-1) {
- mono_raise_exception(get_socket_exception(WSAEPROTONOSUPPORT));
+ *error = WSAEPROTONOSUPPORT;
return(NULL);
}
sock_type=convert_type(type);
if(sock_type==-1) {
- mono_raise_exception(get_socket_exception(WSAESOCKTNOSUPPORT));
+ *error = WSAESOCKTNOSUPPORT;
return(NULL);
}
- sock=socket(sock_family, sock_type, sock_proto);
+ sock = _wapi_socket (sock_family, sock_type, sock_proto,
+ NULL, 0, WSA_FLAG_OVERLAPPED);
+
if(sock==INVALID_SOCKET) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
return(NULL);
}
- if (sock_family == AF_INET && sock_type == SOCK_DGRAM)
+ if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
+ return (GUINT_TO_POINTER (sock));
+ }
+
+#ifdef AF_INET6
+ if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
return (GUINT_TO_POINTER (sock));
+ }
+#endif
+#ifndef PLATFORM_WIN32
/* .net seems to set this by default for SOCK_STREAM,
- * not for SOCK_DGRAM (see bug #36322) */
- ret=setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(true));
+ * not for SOCK_DGRAM (see bug #36322)
+ *
+ * It seems winsock has a rather different idea of what
+ * SO_REUSEADDR means. If it's set, then a new socket can be
+ * bound over an existing listening socket. There's a new
+ * windows-specific option called SO_EXCLUSIVEADDRUSE but
+ * using that means the socket MUST be closed properly, or a
+ * denial of service can occur. Luckily for us, winsock
+ * behaves as though any other system would when SO_REUSEADDR
+ * is true, so we don't need to do anything else here. See
+ * bug 53992.
+ */
+ {
+ int ret, true = 1;
+
+ ret = _wapi_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof (true));
if(ret==SOCKET_ERROR) {
+ *error = WSAGetLastError ();
+
closesocket(sock);
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
return(NULL);
}
+ }
+#endif
return(GUINT_TO_POINTER (sock));
}
/* FIXME: the SOCKET parameter (here and in other functions in this
* file) is really an IntPtr which needs to be converted to a guint32.
*/
-void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock)
+void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
+ gint32 *error)
{
MONO_ARCH_SAVE_REGS;
g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
#endif
+ *error = 0;
+
+ /* Clear any pending work item from this socket if the underlying
+ * polling system does not notify when the socket is closed */
+ mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
closesocket(sock);
}
return(WSAGetLastError());
}
-gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock)
+gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
+ gint32 *error)
{
- int ret, amount;
+ int ret;
+ gulong amount;
MONO_ARCH_SAVE_REGS;
+ *error = 0;
+
ret=ioctlsocket(sock, FIONREAD, &amount);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
return(0);
}
}
void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
- gboolean block)
+ gboolean block,
+ gint32 *error)
{
int ret;
MONO_ARCH_SAVE_REGS;
- ret=ioctlsocket(sock, FIONBIO, &block);
+ *error = 0;
+
+ /*
+ * block == TRUE/FALSE means we will block/not block.
+ * But the ioctlsocket call takes TRUE/FALSE for non-block/block
+ */
+ block = !block;
+
+ ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
}
}
-gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock)
+gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
+ gint32 *error)
{
SOCKET newsock;
MONO_ARCH_SAVE_REGS;
- newsock=accept(sock, NULL, 0);
+ *error = 0;
+
+ newsock = _wapi_accept (sock, NULL, 0);
if(newsock==INVALID_SOCKET) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
return(NULL);
}
}
void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
- guint32 backlog)
+ guint32 backlog,
+ gint32 *error)
{
int ret;
MONO_ARCH_SAVE_REGS;
- ret=listen(sock, backlog);
+ *error = 0;
+
+ ret = _wapi_listen (sock, backlog);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
}
}
static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
- int sa_size)
+ int sa_size, gint32 *error)
{
MonoDomain *domain = mono_domain_get ();
MonoObject *sockaddr_obj;
field=mono_class_get_field_from_name(sockaddr_class, "data");
/* Make sure there is space for the family and size bytes */
- data=mono_array_new(domain, mono_defaults.byte_class, sa_size+2);
+ data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
/* The data buffer is laid out as follows:
* byte 0 is the address family
family=convert_to_mono_family(saddr->sa_family);
if(family==AddressFamily_Unknown) {
- mono_raise_exception(get_socket_exception(WSAEAFNOSUPPORT));
+ *error = WSAEAFNOSUPPORT;
return(NULL);
}
guint32 address=ntohl(sa_in->sin_addr.s_addr);
if(sa_size<8) {
- mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
+ mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
}
mono_array_set(data, guint8, 2, (port>>8) & 0xff);
mono_array_set(data, guint8, 5, (address>>16) & 0xff);
mono_array_set(data, guint8, 6, (address>>8) & 0xff);
mono_array_set(data, guint8, 7, (address) & 0xff);
-
- *(MonoArray **)(((char *)sockaddr_obj) + field->offset)=data;
+
+ mono_field_set_value (sockaddr_obj, field, data);
+
+ return(sockaddr_obj);
+#ifdef AF_INET6
+ } else if (saddr->sa_family == AF_INET6) {
+ struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
+ int i;
+
+ guint16 port=ntohs(sa_in->sin6_port);
+
+ if(sa_size<28) {
+ mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
+ }
+
+ mono_array_set(data, guint8, 2, (port>>8) & 0xff);
+ mono_array_set(data, guint8, 3, (port) & 0xff);
+
+ for(i=0; i<16; i++) {
+ mono_array_set(data, guint8, 8+i,
+ sa_in->sin6_addr.s6_addr[i]);
+ }
+
+ mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
+ mono_array_set(data, guint8, 25,
+ (sa_in->sin6_scope_id >> 8) & 0xff);
+ mono_array_set(data, guint8, 26,
+ (sa_in->sin6_scope_id >> 16) & 0xff);
+ mono_array_set(data, guint8, 27,
+ (sa_in->sin6_scope_id >> 24) & 0xff);
+
+ mono_field_set_value (sockaddr_obj, field, data);
return(sockaddr_obj);
+#endif
#ifdef HAVE_SYS_UN_H
} else if (saddr->sa_family == AF_UNIX) {
int i;
- for (i = 0; i < sa_size; i++)
- mono_array_set (data, guint8, i + 2, saddr->sa_data [i]);
+ for (i = 0; i < sa_size; i++) {
+ mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
+ }
- *(MonoArray **)(((char *)sockaddr_obj) + field->offset) = data;
+ mono_field_set_value (sockaddr_obj, field, data);
return sockaddr_obj;
#endif
} else {
- mono_raise_exception(get_socket_exception(WSAEAFNOSUPPORT));
+ *error = WSAEAFNOSUPPORT;
return(NULL);
}
}
-extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock)
+extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
{
- struct sockaddr sa;
+ gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
int salen;
int ret;
MONO_ARCH_SAVE_REGS;
- salen=sizeof(struct sockaddr);
- ret=getsockname(sock, &sa, &salen);
+ *error = 0;
+
+ salen=sizeof(sa);
+ ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
+ return(NULL);
}
#ifdef DEBUG
g_message(G_GNUC_PRETTY_FUNCTION ": bound to %s port %d", inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr), ntohs(((struct sockaddr_in *)&sa)->sin_port));
#endif
- return(create_object_from_sockaddr(&sa, salen));
+ return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
+ error));
}
-extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock)
+extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
{
- struct sockaddr sa;
+ gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
int salen;
int ret;
MONO_ARCH_SAVE_REGS;
- salen=sizeof(struct sockaddr);
- ret=getpeername(sock, &sa, &salen);
+ *error = 0;
+
+ salen=sizeof(sa);
+ ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
+ return(NULL);
}
#ifdef DEBUG
g_message(G_GNUC_PRETTY_FUNCTION ": connected to %s port %d", inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr), ntohs(((struct sockaddr_in *)&sa)->sin_port));
#endif
- return(create_object_from_sockaddr(&sa, salen));
+ return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
+ error));
}
static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
- int *sa_size)
+ int *sa_size,
+ gint32 *error)
{
MonoClassField *field;
MonoArray *data;
*/
len = mono_array_length (data);
if (len < 2) {
- mono_raise_exception (mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
+ mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
}
- family = convert_family (mono_array_get (data, guint16, 0));
+ family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
if(family==AF_INET) {
struct sockaddr_in *sa=g_new0(struct sockaddr_in, 1);
guint16 port=(mono_array_get(data, guint8, 2) << 8) +
*sa_size=sizeof(struct sockaddr_in);
return((struct sockaddr *)sa);
+
+#ifdef AF_INET6
+ } else if (family == AF_INET6) {
+ struct sockaddr_in6 *sa=g_new0(struct sockaddr_in6, 1);
+ int i;
+
+ guint16 port = mono_array_get(data, guint8, 3) + (mono_array_get(data, guint8, 2) << 8);
+ guint32 scopeid = mono_array_get(data, guint8, 24) +
+ (mono_array_get(data, guint8, 25)<<8) +
+ (mono_array_get(data, guint8, 26)<<16) +
+ (mono_array_get(data, guint8, 27)<<24);
+
+ sa->sin6_family=family;
+ sa->sin6_port=htons(port);
+ sa->sin6_scope_id = scopeid;
+
+ for(i=0; i<16; i++)
+ sa->sin6_addr.s6_addr[i] = mono_array_get(data, guint8, 8+i);
+
+ *sa_size=sizeof(struct sockaddr_in6);
+ return((struct sockaddr *)sa);
+#endif
#ifdef HAVE_SYS_UN_H
} else if (family == AF_UNIX) {
struct sockaddr_un *sock_un = g_new0 (struct sockaddr_un, 1);
return (struct sockaddr *)sock_un;
#endif
} else {
- mono_raise_exception(get_socket_exception(WSAEAFNOSUPPORT));
+ *error = WSAEAFNOSUPPORT;
return(0);
}
}
-extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr)
+extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
{
struct sockaddr *sa;
int sa_size;
MONO_ARCH_SAVE_REGS;
- sa=create_sockaddr_from_object(sockaddr, &sa_size);
+ *error = 0;
+
+ sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
+ if (*error != 0) {
+ return;
+ }
#ifdef DEBUG
g_message(G_GNUC_PRETTY_FUNCTION ": binding to %s port %d", inet_ntoa(((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port));
#endif
- ret=bind(sock, sa, sa_size);
- g_free(sa);
-
+
+ ret = _wapi_bind (sock, sa, sa_size);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
}
+
+ g_free(sa);
+}
+
+enum {
+ SelectModeRead,
+ SelectModeWrite,
+ SelectModeError
+};
+
+MonoBoolean
+ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
+ gint timeout, gint32 *error)
+{
+ fd_set fds;
+ int ret = 0;
+ struct timeval tv;
+ struct timeval *tvptr;
+ div_t divvy;
+ time_t start;
+
+ MONO_ARCH_SAVE_REGS;
+
+ start = time (NULL);
+ do {
+ *error = 0;
+ FD_ZERO (&fds);
+ _wapi_FD_SET (sock, &fds);
+ if (timeout >= 0) {
+ divvy = div (timeout, 1000000);
+ tv.tv_sec = divvy.quot;
+ tv.tv_usec = divvy.rem;
+ tvptr = &tv;
+ } else {
+ tvptr = NULL;
+ }
+
+ if (mode == SelectModeRead) {
+ ret = _wapi_select (0, &fds, NULL, NULL, tvptr);
+ } else if (mode == SelectModeWrite) {
+ ret = _wapi_select (0, NULL, &fds, NULL, tvptr);
+ } else if (mode == SelectModeError) {
+ ret = _wapi_select (0, NULL, NULL, &fds, tvptr);
+ } else {
+ g_assert_not_reached ();
+ }
+
+ if (timeout > 0 && ret < 0) {
+ int err = errno;
+ int sec = time (NULL) - start;
+
+ timeout -= sec * 1000;
+ if (timeout < 0)
+ timeout = 0;
+ errno = err;
+ }
+
+ } while ((ret == SOCKET_ERROR) && (*error == WSAGetLastError ()) == WSAEINTR);
+
+ return (ret != SOCKET_ERROR && _wapi_FD_ISSET (sock, &fds));
}
-extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr)
+extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
{
struct sockaddr *sa;
int sa_size;
MONO_ARCH_SAVE_REGS;
- sa=create_sockaddr_from_object(sockaddr, &sa_size);
+ *error = 0;
+
+ sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
+ if (*error != 0) {
+ return;
+ }
#ifdef DEBUG
g_message(G_GNUC_PRETTY_FUNCTION ": connecting to %s port %d", inet_ntoa(((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port));
#endif
- ret=connect(sock, sa, sa_size);
- g_free(sa);
-
+ ret = _wapi_connect (sock, sa, sa_size);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
}
+
+ g_free(sa);
}
-gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags)
+gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
{
int ret;
guchar *buf;
MONO_ARCH_SAVE_REGS;
+ *error = 0;
+
alen=mono_array_length(buffer);
if(offset+count>alen) {
return(0);
buf=mono_array_addr(buffer, guchar, offset);
- ret=recv(sock, buf, count, recvflags);
+ ret = _wapi_recv (sock, buf, count, recvflags);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
+ return(0);
}
-
+
return(ret);
}
-gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr)
+gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
{
int ret;
guchar *buf;
MONO_ARCH_SAVE_REGS;
+ *error = 0;
+
alen=mono_array_length(buffer);
if(offset+count>alen) {
return(0);
}
- sa=create_sockaddr_from_object(*sockaddr, &sa_size);
+ sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
+ if (*error != 0) {
+ return(0);
+ }
buf=mono_array_addr(buffer, guchar, offset);
- ret=recvfrom(sock, buf, count, recvflags, sa, &sa_size);
-
+ ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
if(ret==SOCKET_ERROR) {
g_free(sa);
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
+ return(0);
}
- *sockaddr=create_object_from_sockaddr(sa, sa_size);
+ /* If we didn't get a socket size, then we're probably a
+ * connected connection-oriented socket and the stack hasn't
+ * returned the remote address. All we can do is return null.
+ */
+ if ( sa_size != 0 )
+ *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
+ else
+ *sockaddr=NULL;
+
g_free(sa);
return(ret);
}
-gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags)
+gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
{
int ret;
guchar *buf;
MONO_ARCH_SAVE_REGS;
+ *error = 0;
+
alen=mono_array_length(buffer);
if(offset+count>alen) {
return(0);
g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
#endif
- ret=send(sock, buf, count, sendflags);
+ ret = _wapi_send (sock, buf, count, sendflags);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
+ return(0);
}
-
+
return(ret);
}
-gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr)
+gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
{
int ret;
guchar *buf;
MONO_ARCH_SAVE_REGS;
+ *error = 0;
+
alen=mono_array_length(buffer);
if(offset+count>alen) {
return(0);
}
- sa=create_sockaddr_from_object(sockaddr, &sa_size);
+ sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
+ if(*error != 0) {
+ return(0);
+ }
#ifdef DEBUG
g_message(G_GNUC_PRETTY_FUNCTION ": alen: %d", alen);
g_message(G_GNUC_PRETTY_FUNCTION ": Sending %d bytes", count);
#endif
- ret=sendto(sock, buf, count, sendflags, sa, sa_size);
- g_free(sa);
-
+ ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
}
+
+ g_free(sa);
return(ret);
}
return(sock);
}
-void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **read_socks, MonoArray **write_socks, MonoArray **err_socks, gint32 timeout)
+#define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
+void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
{
- fd_set readfds, writefds, errfds;
- fd_set *readptr = NULL, *writeptr = NULL, *errptr = NULL;
- struct timeval tv;
- div_t divvy;
+ MonoThread *thread = NULL;
+ MonoObject *obj;
+ mono_pollfd *pfds;
+ int nfds, idx;
int ret;
- int readarrsize = 0, writearrsize = 0, errarrsize = 0;
- MonoDomain *domain=mono_domain_get();
+ int i, count;
+ int mode;
MonoClass *sock_arr_class;
MonoArray *socks;
- int count;
- int i;
- SOCKET handle;
+ time_t start;
MONO_ARCH_SAVE_REGS;
- if (*read_socks)
- readarrsize=mono_array_length(*read_socks);
+ /* *sockets -> READ, null, WRITE, null, ERROR, null */
+ count = mono_array_length (*sockets);
+ nfds = count - 3; /* NULL separators */
+ pfds = g_new0 (mono_pollfd, nfds);
+ mode = idx = 0;
+ for (i = 0; i < count; i++) {
+ obj = mono_array_get (*sockets, MonoObject *, i);
+ if (obj == NULL) {
+ mode++;
+ continue;
+ }
- if(readarrsize>FD_SETSIZE) {
- mono_raise_exception(get_socket_exception(WSAEFAULT));
- return;
+ pfds [idx].fd = GPOINTER_TO_INT (Socket_to_SOCKET (obj));
+ pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
+ idx++;
}
-
- if (readarrsize) {
- readptr = &readfds;
- FD_ZERO(&readfds);
- for(i=0; i<readarrsize; i++) {
- handle = Socket_to_SOCKET(mono_array_get(*read_socks, MonoObject *, i));
- FD_SET(handle, &readfds);
+
+ timeout = (timeout >= 0) ? (timeout / 1000) : -1;
+ start = time (NULL);
+ do {
+ *error = 0;
+ ret = mono_poll (pfds, nfds, timeout);
+ if (timeout > 0 && ret < 0) {
+ int err = errno;
+ int sec = time (NULL) - start;
+
+ timeout -= sec * 1000;
+ if (timeout < 0)
+ timeout = 0;
+ errno = err;
}
- }
-
- if (*write_socks)
- writearrsize=mono_array_length(*write_socks);
- if(writearrsize>FD_SETSIZE) {
- mono_raise_exception(get_socket_exception(WSAEFAULT));
- return;
- }
-
- if (writearrsize) {
- writeptr = &writefds;
- FD_ZERO(&writefds);
- for(i=0; i<writearrsize; i++) {
- handle = Socket_to_SOCKET(mono_array_get(*write_socks, MonoObject *, i));
- FD_SET(handle, &writefds);
+ if (ret == -1 && errno == EINTR) {
+ int leave = 0;
+ if (thread == NULL)
+ thread = mono_thread_current ();
+
+ mono_monitor_enter (thread->synch_lock);
+ leave = ((thread->state & ThreadState_AbortRequested) != 0 ||
+ (thread->state & ThreadState_StopRequested) != 0);
+ mono_monitor_exit (thread->synch_lock);
+ if (leave != 0) {
+ g_free (pfds);
+ *sockets = NULL;
+ return;
+ } else {
+ /* Suspend requested? */
+ mono_thread_interruption_checkpoint ();
+ }
+ errno = EINTR;
}
- }
+ } while (ret == -1 && errno == EINTR);
- if (*err_socks)
- errarrsize=mono_array_length(*err_socks);
-
- if(errarrsize>FD_SETSIZE) {
- mono_raise_exception(get_socket_exception(WSAEFAULT));
+ if (ret == -1) {
+#ifdef PLATFORM_WIN32
+ *error = WSAGetLastError ();
+#else
+ *error = errno_to_WSA (errno, __func__);
+#endif
+ g_free (pfds);
return;
}
-
- if (errarrsize) {
- errptr = &errfds;
- FD_ZERO(&errfds);
- for(i=0; i<errarrsize; i++) {
- handle = Socket_to_SOCKET(mono_array_get(*err_socks, MonoObject *, i));
- FD_SET(handle, &errfds);
- }
- }
- /* Negative timeout meaning block until ready is only
- * specified in Poll, not Select
- */
- if(timeout>=0) {
- divvy=div(timeout, 1000000);
- tv.tv_sec=divvy.quot;
- tv.tv_usec=divvy.rem;
-
- ret=select(0, readptr, writeptr, errptr, &tv);
- } else {
- ret=select(0, readptr, writeptr, errptr, NULL);
- }
-
- if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ if (ret == 0) {
+ g_free (pfds);
+ *sockets = NULL;
return;
}
- if (readarrsize) {
- sock_arr_class=((MonoObject *)*read_socks)->vtable->klass;
-
- count=0;
- for(i=0; i<readarrsize; i++) {
- if(FD_ISSET(Socket_to_SOCKET(mono_array_get(*read_socks, MonoObject *, i)), &readfds)) {
- count++;
- }
- }
- socks=mono_array_new_full(domain, sock_arr_class, &count, NULL);
- count=0;
- for(i=0; i<readarrsize; i++) {
- MonoObject *sock=mono_array_get(*read_socks, MonoObject *, i);
-
- if(FD_ISSET(Socket_to_SOCKET(sock), &readfds)) {
- mono_array_set(socks, MonoObject *, count, sock);
- count++;
- }
+ sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
+ ret += 3; /* space for the NULL delimiters */
+ socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &ret, NULL);
+ ret -= 3;
+ mode = idx = 0;
+ for (i = 0; i < count && ret > 0; i++) {
+ mono_pollfd *pfd;
+
+ obj = mono_array_get (*sockets, MonoObject *, i);
+ if (obj == NULL) {
+ mode++;
+ idx++;
+ continue;
}
- *read_socks=socks;
- } else {
- *read_socks = NULL;
- }
- if (writearrsize) {
- sock_arr_class=((MonoObject *)*write_socks)->vtable->klass;
- count=0;
- for(i=0; i<writearrsize; i++) {
- if(FD_ISSET(Socket_to_SOCKET(mono_array_get(*write_socks, MonoObject *, i)), &writefds)) {
- count++;
- }
- }
- socks=mono_array_new_full(domain, sock_arr_class, &count, NULL);
- count=0;
- for(i=0; i<writearrsize; i++) {
- MonoObject *sock=mono_array_get(*write_socks, MonoObject *, i);
-
- if(FD_ISSET(Socket_to_SOCKET(sock), &writefds)) {
- mono_array_set(socks, MonoObject *, count, sock);
- count++;
- }
+ pfd = &pfds [i - mode];
+ if (pfd->revents == 0)
+ continue;
+
+ ret--;
+ if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
+ mono_array_set (socks, MonoObject *, idx++, obj);
+ } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
+ mono_array_set (socks, MonoObject *, idx++, obj);
+ } else if ((pfd->revents & POLL_ERRORS) != 0) {
+ mono_array_set (socks, MonoObject *, idx++, obj);
}
- *write_socks=socks;
- } else {
- *write_socks = NULL;
}
- if (errarrsize) {
- sock_arr_class=((MonoObject *)*err_socks)->vtable->klass;
- count=0;
- for(i=0; i<errarrsize; i++) {
- if(FD_ISSET(Socket_to_SOCKET(mono_array_get(*err_socks, MonoObject *, i)), &errfds)) {
- count++;
- }
- }
- socks=mono_array_new_full(domain, sock_arr_class, &count, NULL);
- count=0;
- for(i=0; i<errarrsize; i++) {
- MonoObject *sock=mono_array_get(*err_socks, MonoObject *, i);
-
- if(FD_ISSET(Socket_to_SOCKET(sock), &errfds)) {
- mono_array_set(socks, MonoObject *, count, sock);
- count++;
- }
- }
- *err_socks=socks;
- }
+ *sockets = socks;
+ g_free (pfds);
+}
+
+static MonoObject* int_to_object (MonoDomain *domain, int val)
+{
+ return mono_value_box (domain, mono_get_int32_class (), &val);
}
-void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val)
+
+void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
{
int system_level;
int system_name;
int valsize=sizeof(val);
struct linger linger;
int lingersize=sizeof(linger);
+ int time_ms = 0;
+ int time_ms_size = sizeof (time_ms);
+#ifdef SO_PEERCRED
+ struct ucred cred;
+ int credsize = sizeof(cred);
+#endif
MonoDomain *domain=mono_domain_get();
MonoObject *obj;
MonoClass *obj_class;
MONO_ARCH_SAVE_REGS;
+ *error = 0;
+
ret=convert_sockopt_level_and_name(level, name, &system_level,
&system_name);
if(ret==-1) {
- mono_raise_exception(get_socket_exception(WSAENOPROTOOPT));
+ *error = WSAENOPROTOOPT;
return;
}
switch(name) {
case SocketOptionName_Linger:
case SocketOptionName_DontLinger:
- ret=getsockopt(sock, system_level, system_name, &linger,
+ ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
&lingersize);
break;
+ case SocketOptionName_SendTimeout:
+ case SocketOptionName_ReceiveTimeout:
+ ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
+ break;
+
+#ifdef SO_PEERCRED
+ case SocketOptionName_PeerCred:
+ ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
+ &credsize);
+ break;
+#endif
+
default:
- ret=getsockopt(sock, system_level, system_name, &val,
+ ret = _wapi_getsockopt (sock, system_level, system_name, &val,
&valsize);
}
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
return;
}
case SocketOptionName_DontLinger:
/* construct a bool int in val - true if linger is off */
- val=!linger.l_onoff;
+ obj = int_to_object (domain, !linger.l_onoff);
+ break;
- /* fall through */
+ case SocketOptionName_SendTimeout:
+ case SocketOptionName_ReceiveTimeout:
+ obj = int_to_object (domain, time_ms);
+ break;
+
+#ifdef SO_PEERCRED
+ case SocketOptionName_PeerCred:
+ {
+ /* build a Mono.Posix.PeerCred+PeerCredData if
+ * possible
+ */
+ static MonoImage *mono_posix_image = NULL;
+ MonoPeerCredData *cred_data;
- default:
- /* construct an Int32 object to hold val */
- obj=mono_object_new(domain, mono_defaults.int32_class);
+ if (mono_posix_image == NULL) {
+ mono_posix_image=mono_image_loaded ("Mono.Posix");
+ if (!mono_posix_image) {
+ MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
+ if (!sa) {
+ *error = WSAENOPROTOOPT;
+ return;
+ } else {
+ mono_posix_image = mono_assembly_get_image (sa);
+ }
+ }
+ }
- /* Locate and set the "value" field */
- field=mono_class_get_field_from_name(mono_defaults.int32_class,
- "value");
- *(gint32 *)(((char *)obj)+field->offset)=val;
+ obj_class = mono_class_from_name(mono_posix_image,
+ "Mono.Posix",
+ "PeerCred/PeerCredData");
+ obj = mono_object_new(domain, obj_class);
+ cred_data = (MonoPeerCredData *)obj;
+ cred_data->pid = cred.pid;
+ cred_data->uid = cred.uid;
+ cred_data->gid = cred.gid;
+ break;
+ }
+#endif
+
+ default:
+ obj = int_to_object (domain, val);
}
*obj_val=obj;
}
-void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val)
+void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
{
int system_level;
int system_name;
MONO_ARCH_SAVE_REGS;
+ *error = 0;
+
ret=convert_sockopt_level_and_name(level, name, &system_level,
&system_name);
if(ret==-1) {
- mono_raise_exception(get_socket_exception(WSAENOPROTOOPT));
+ *error = WSAENOPROTOOPT;
return;
}
valsize=mono_array_length(*byte_val);
buf=mono_array_addr(*byte_val, guchar, 0);
- ret=getsockopt(sock, system_level, system_name, buf, &valsize);
+ ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
}
}
+#if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
{
struct in_addr inaddr;
MonoClassField *field;
- field=mono_class_get_field_from_name(ipaddr->vtable->klass, "address");
+ field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
/* No idea why .net uses a 64bit type to hold a 32bit value...
*
return(inaddr);
}
+#endif
-void ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal(SOCKET sock, gint32 level, gint32 name, MonoObject *obj_val, MonoArray *byte_val, gint32 int_val)
+#ifdef AF_INET6
+static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
+{
+ struct in6_addr in6addr;
+ MonoClassField *field;
+ MonoArray *data;
+ int i;
+
+ field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
+ data=*(MonoArray **)(((char *)ipaddr) + field->offset);
+
+/* Solaris has only the 8 bit version. */
+#ifndef s6_addr16
+ for(i=0; i<8; i++) {
+ guint16 s = mono_array_get (data, guint16, i);
+ in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
+ in6addr.s6_addr[2 * i + 1] = s & 0xff;
+ }
+#else
+ for(i=0; i<8; i++)
+ in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
+#endif
+ return(in6addr);
+}
+#endif /* AF_INET6 */
+
+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)
{
int system_level;
int system_name;
int ret;
+#ifdef AF_INET6
+ int sol_ip;
+ int sol_ipv6;
+
+ *error = 0;
+
+#ifdef HAVE_SOL_IPV6
+ sol_ipv6 = SOL_IPV6;
+#else
+ {
+ struct protoent *pent;
+ pent = getprotobyname ("ipv6");
+ sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
+ }
+#endif
+
+#ifdef HAVE_SOL_IP
+ sol_ip = SOL_IP;
+#else
+ {
+ struct protoent *pent;
+ pent = getprotobyname ("ip");
+ sol_ip = (pent != NULL) ? pent->p_proto : 0;
+ }
+#endif
+#endif /* AF_INET6 */
MONO_ARCH_SAVE_REGS;
ret=convert_sockopt_level_and_name(level, name, &system_level,
&system_name);
if(ret==-1) {
- mono_raise_exception(get_socket_exception(WSAENOPROTOOPT));
+ *error = WSAENOPROTOOPT;
return;
}
linger.l_onoff=0;
linger.l_linger=0;
valsize=sizeof(linger);
- ret=setsockopt(sock, system_level, system_name,
- &linger, valsize);
+ ret = _wapi_setsockopt (sock, system_level,
+ system_name, &linger, valsize);
break;
case SocketOptionName_Linger:
linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
valsize=sizeof(linger);
- ret=setsockopt(sock, system_level, system_name,
- &linger, valsize);
+ ret = _wapi_setsockopt (sock, system_level,
+ system_name, &linger, valsize);
break;
case SocketOptionName_AddMembership:
case SocketOptionName_DropMembership:
+#if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
{
MonoObject *address = NULL;
+#ifdef AF_INET6
+ if(system_level == sol_ipv6) {
+ struct ipv6_mreq mreq6;
+
+ /*
+ * Get group address
+ */
+ field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
+ address = *(gpointer *)(((char *)obj_val) + field->offset);
+
+ if(address) {
+ mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
+ }
+
+ field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
+ mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
+
+ ret = _wapi_setsockopt (sock, system_level,
+ system_name, &mreq6,
+ sizeof (mreq6));
+ } else if(system_level == sol_ip)
+#endif /* AF_INET6 */
+ {
#ifdef HAVE_STRUCT_IP_MREQN
- struct ip_mreqn mreq = {{0}};
+ struct ip_mreqn mreq = {{0}};
#else
- struct ip_mreq mreq = {{0}};
+ struct ip_mreq mreq = {{0}};
#endif /* HAVE_STRUCT_IP_MREQN */
- /* pain! MulticastOption holds two IPAddress
- * members, so I have to dig the value out of
- * those :-(
- */
- field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
- address = *(gpointer *)(((char *)obj_val) + field->offset);
-
- /* address might not be defined and if so, set the address to ADDR_ANY.
- */
- if(address)
- mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
-
- field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
- address = *(gpointer *)(((char *)obj_val) + field->offset);
+ /* pain! MulticastOption holds two IPAddress
+ * members, so I have to dig the value out of
+ * those :-(
+ */
+ field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
+ address = *(gpointer *)(((char *)obj_val) + field->offset);
+
+ /* address might not be defined and if so, set the address to ADDR_ANY.
+ */
+ if(address) {
+ mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);
+ }
+
+ field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
+ address = *(gpointer *)(((char *)obj_val) + field->offset);
#ifdef HAVE_STRUCT_IP_MREQN
- if(address)
- mreq.imr_address = ipaddress_to_struct_in_addr (address);
+ if(address) {
+ mreq.imr_address = ipaddress_to_struct_in_addr (address);
+ }
#else
- if(address)
- mreq.imr_interface = ipaddress_to_struct_in_addr (address);
+ if(address) {
+ mreq.imr_interface = ipaddress_to_struct_in_addr (address);
+ }
#endif /* HAVE_STRUCT_IP_MREQN */
- ret = setsockopt (sock, system_level, system_name,
- &mreq, sizeof (mreq));
+ ret = _wapi_setsockopt (sock, system_level,
+ system_name, &mreq,
+ sizeof (mreq));
+ }
break;
}
+#endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
default:
- /* Throw an exception */
- mono_raise_exception(get_socket_exception(WSAEINVAL));
+ /* Cause an exception to be thrown */
+ *error = WSAEINVAL;
+ return;
}
} else if (byte_val!=NULL) {
int valsize=mono_array_length(byte_val);
guchar *buf=mono_array_addr(byte_val, guchar, 0);
- ret=setsockopt(sock, system_level, system_name, buf, valsize);
+ ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
+ return;
}
} else {
- ret=setsockopt(sock, system_level, system_name, &int_val,
- sizeof(int_val));
+ /* ReceiveTimeout/SendTimeout get here */
+ ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
}
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
}
}
void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
- gint32 how)
+ gint32 how,
+ gint32 *error)
{
int ret;
MONO_ARCH_SAVE_REGS;
+ *error = 0;
+
/* Currently, the values for how (recv=0, send=1, both=2) match
* the BSD API
*/
- ret=shutdown(sock, how);
+ ret = _wapi_shutdown (sock, how);
if(ret==SOCKET_ERROR) {
- mono_raise_exception(get_socket_exception(WSAGetLastError()));
+ *error = WSAGetLastError ();
+ }
+}
+
+gint
+ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
+ MonoArray *input,
+ MonoArray *output, gint32 *error)
+{
+ gulong output_bytes = 0;
+ gchar *i_buffer, *o_buffer;
+ gint i_len, o_len;
+ gint ret;
+
+ MONO_ARCH_SAVE_REGS;
+
+ *error = 0;
+
+ if (code == FIONBIO) {
+ /* Invalid command. Must use Socket.Blocking */
+ return -1;
+ }
+
+ if (input == NULL) {
+ i_buffer = NULL;
+ i_len = 0;
+ } else {
+ i_buffer = mono_array_addr (input, gchar, 0);
+ i_len = mono_array_length (input);
+ }
+
+ if (output == NULL) {
+ o_buffer = NULL;
+ o_len = 0;
+ } else {
+ o_buffer = mono_array_addr (output, gchar, 0);
+ o_len = mono_array_length (output);
+ }
+
+ ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
+ if (ret == SOCKET_ERROR) {
+ *error = WSAGetLastError ();
+ return(-1);
+ }
+
+ return (gint) output_bytes;
+}
+
+#ifdef HAVE_SIOCGIFCONF
+static gboolean
+is_loopback (int family, void *ad)
+{
+ char *ptr = (char *) ad;
+
+ if (family == AF_INET) {
+ return (ptr [0] == 127);
+ }
+#ifdef AF_INET6
+ else {
+ return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
+ }
+#endif
+ return FALSE;
+}
+
+static void *
+get_local_ips (int family, int *nips)
+{
+ int addr_size, offset, fd, i, count;
+ int max_ifaces = 50; /* 50 interfaces should be enough... */
+ struct ifconf ifc;
+ struct ifreq *ifr;
+ struct ifreq iflags;
+ char *result, *tmp_ptr;
+ gboolean ignore_loopback = FALSE;
+
+ *nips = 0;
+ if (family == AF_INET) {
+ addr_size = sizeof (struct in_addr);
+ offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
+#ifdef AF_INET6
+ } else if (family == AF_INET6) {
+ addr_size = sizeof (struct in6_addr);
+ offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
+#endif
+ } else {
+ return NULL;
+ }
+
+ fd = socket (family, SOCK_STREAM, 0);
+
+ ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
+ ifc.ifc_buf = g_malloc (ifc.ifc_len);
+ if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
+ close (fd);
+ g_free (ifc.ifc_buf);
+ return NULL;
}
+
+ count = ifc.ifc_len / sizeof (struct ifreq);
+ *nips = count;
+ if (count == 0) {
+ g_free (ifc.ifc_buf);
+ close (fd);
+ return NULL;
+ }
+
+ for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
+ strcpy (iflags.ifr_name, ifr->ifr_name);
+ if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
+ continue;
+ }
+
+ if ((iflags.ifr_flags & IFF_UP) == 0) {
+ ifr->ifr_name [0] = '\0';
+ continue;
+ }
+
+ if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
+ ignore_loopback = TRUE;
+ }
+ }
+
+ close (fd);
+ result = g_malloc (addr_size * count);
+ tmp_ptr = result;
+ for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
+ if (ifr->ifr_name [0] == '\0') {
+ (*nips)--;
+ continue;
+ }
+
+ if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
+ (*nips)--;
+ continue;
+ }
+
+ memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
+ tmp_ptr += addr_size;
+ }
+
+ g_free (ifc.ifc_buf);
+ return result;
}
+#else
+static void *
+get_local_ips (int family, int *nips)
+{
+ *nips = 0;
+ return NULL;
+}
+
+#endif /* HAVE_SIOCGIFCONF */
+#ifndef AF_INET6
static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
MonoArray **h_aliases,
- MonoArray **h_addr_list)
+ MonoArray **h_addr_list,
+ gboolean add_local_ips)
{
MonoDomain *domain = mono_domain_get ();
int i;
+ struct in_addr *local_in = NULL;
+ int nlocal_in = 0;
if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
return(FALSE);
i++;
}
- *h_aliases=mono_array_new(domain, mono_defaults.string_class, i);
+ *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
i=0;
while(he->h_aliases[i]!=NULL) {
MonoString *alias;
i++;
}
- i=0;
- while(he->h_addr_list[i]!=NULL) {
- i++;
+ if (add_local_ips) {
+ local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
+ if (nlocal_in) {
+ *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
+ for (i = 0; i < nlocal_in; i++) {
+ MonoString *addr_string;
+ char addr [16], *ptr;
+
+ ptr = (char *) &local_in [i];
+ g_snprintf(addr, 16, "%u.%u.%u.%u",
+ (unsigned char) ptr [0],
+ (unsigned char) ptr [1],
+ (unsigned char) ptr [2],
+ (unsigned char) ptr [3]);
+
+ addr_string = mono_string_new (domain, addr);
+ mono_array_set (*h_addr_list, MonoString *, i, addr_string);
+ i++;
+ }
+
+ g_free (local_in);
+ }
}
- *h_addr_list=mono_array_new(domain, mono_defaults.string_class, i);
- i=0;
- while(he->h_addr_list[i]!=NULL) {
+ if (nlocal_in == 0) {
+ i = 0;
+ while (he->h_addr_list[i]!=NULL) {
+ i++;
+ }
+
+ *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
+ i=0;
+ while(he->h_addr_list[i]!=NULL) {
+ MonoString *addr_string;
+ char addr[16];
+
+ g_snprintf(addr, 16, "%u.%u.%u.%u",
+ (unsigned char)he->h_addr_list[i][0],
+ (unsigned char)he->h_addr_list[i][1],
+ (unsigned char)he->h_addr_list[i][2],
+ (unsigned char)he->h_addr_list[i][3]);
+
+ addr_string=mono_string_new(domain, addr);
+ mono_array_set(*h_addr_list, MonoString *, i, addr_string);
+ i++;
+ }
+ }
+
+ return(TRUE);
+}
+#endif
+
+#if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
+static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
+ MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
+{
+ MonoDomain *domain = mono_domain_get ();
+ int i, host_count, host_index, family_hint;
+ struct in_addr *local_in = NULL;
+ int nlocal_in = 0;
+ struct in6_addr *local_in6 = NULL;
+ int nlocal_in6 = 0;
+ gboolean from_local = FALSE;
+
+ family_hint = get_family_hint ();
+
+ if(he1 == NULL && he2 == NULL) {
+ return(FALSE);
+ }
+
+ /*
+ * Check if address length and family are correct
+ */
+ if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
+ return(FALSE);
+ }
+
+ if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
+ return(FALSE);
+ }
+
+ /*
+ * Get the aliases and host name from he1 or he2 whichever is
+ * not null, if he1 is not null then take aliases from he1
+ */
+ if (he1 != NULL && (family_hint == PF_UNSPEC ||
+ family_hint == PF_INET)) {
+ *h_name=mono_string_new (domain, he1->h_name);
+
+ i=0;
+ while(he1->h_aliases[i]!=NULL) {
+ i++;
+ }
+
+ *h_aliases=mono_array_new (domain, mono_get_string_class (),
+ i);
+ i=0;
+ while(he1->h_aliases[i]!=NULL) {
+ MonoString *alias;
+
+ alias=mono_string_new (domain, he1->h_aliases[i]);
+ mono_array_set (*h_aliases, MonoString *, i, alias);
+ i++;
+ }
+ } else if (family_hint == PF_UNSPEC || family_hint == PF_INET6) {
+ *h_name=mono_string_new (domain, he2->h_name);
+
+ i=0;
+ while(he2->h_aliases [i] != NULL) {
+ i++;
+ }
+
+ *h_aliases=mono_array_new (domain, mono_get_string_class (),
+ i);
+ i=0;
+ while(he2->h_aliases[i]!=NULL) {
+ MonoString *alias;
+
+ alias=mono_string_new (domain, he2->h_aliases[i]);
+ mono_array_set (*h_aliases, MonoString *, i, alias);
+ i++;
+ }
+ }
+
+ /*
+ * Count the number of addresses in he1 + he2
+ */
+ host_count = 0;
+ if (he1 != NULL && (family_hint == PF_UNSPEC ||
+ family_hint == PF_INET)) {
+ i=0;
+ while(he1->h_addr_list[i]!=NULL) {
+ i++;
+ host_count++;
+ }
+ }
+
+ if (he2 != NULL && (family_hint == PF_UNSPEC ||
+ family_hint == PF_INET6)) {
+ i=0;
+ while(he2->h_addr_list[i]!=NULL) {
+ i++;
+ host_count++;
+ }
+ }
+
+ /*
+ * Fills the array
+ */
+ host_index = 0;
+ if (add_local_ips) {
+ if (family_hint == PF_UNSPEC || family_hint == PF_INET)
+ local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
+
+ if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
+ local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
+
+ if (nlocal_in || nlocal_in6) {
+ from_local = TRUE;
+ *h_addr_list = mono_array_new (domain, mono_get_string_class (),
+ nlocal_in + nlocal_in6);
+
+ if (nlocal_in6) {
+ int n;
+ for (n = 0; n < nlocal_in6; n++) {
+ MonoString *addr_string;
+ char addr[46]; /* INET6_ADDRSTRLEN == 46 */
+
+ inet_ntop (AF_INET6, &local_in6 [n], addr,
+ sizeof(addr));
+
+ addr_string = mono_string_new (domain, addr);
+ mono_array_set (*h_addr_list, MonoString *, host_index,
+ addr_string);
+ host_index++;
+ }
+ }
+
+ if (nlocal_in) {
+ int n;
+ for (n = 0; n < nlocal_in; n++) {
+ MonoString *addr_string;
+ char addr[16]; /* INET_ADDRSTRLEN == 16 */
+
+ inet_ntop (AF_INET, &local_in [n], addr,
+ sizeof(addr));
+
+ addr_string = mono_string_new (domain, addr);
+ mono_array_set (*h_addr_list, MonoString *, host_index,
+ addr_string);
+ host_index++;
+ }
+ }
+ g_free (local_in);
+ g_free (local_in6);
+ return TRUE;
+ }
+
+ g_free (local_in);
+ g_free (local_in6);
+ }
+
+ *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
+
+ if (he2 != NULL && (family_hint == PF_UNSPEC ||
+ family_hint == PF_INET6)) {
+ i = 0;
+ while(he2->h_addr_list[i] != NULL) {
+ MonoString *addr_string;
+ char addr[46]; /* INET6_ADDRSTRLEN == 46 */
+
+ inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
+ sizeof(addr));
+
+ addr_string = mono_string_new (domain, addr);
+ mono_array_set (*h_addr_list, MonoString *, host_index,
+ addr_string);
+ i++;
+ host_index++;
+ }
+ }
+
+ if (he1 != NULL && (family_hint == PF_UNSPEC ||
+ family_hint == PF_INET)) {
+ i=0;
+ while(he1->h_addr_list[i] != NULL) {
+ MonoString *addr_string;
+ char addr[16]; /* INET_ADDRSTRLEN == 16 */
+
+ inet_ntop (AF_INET, he1->h_addr_list[i], addr,
+ sizeof(addr));
+
+ addr_string=mono_string_new (domain, addr);
+ mono_array_set (*h_addr_list, MonoString *, host_index,
+ addr_string);
+ i++;
+ host_index++;
+ }
+ }
+
+ return(TRUE);
+}
+#endif
+
+#if defined(AF_INET6)
+static gboolean
+addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
+ MonoArray **h_aliases,
+ MonoArray **h_addr_list,
+ gboolean add_local_ips)
+{
+ gint32 count, i;
+ struct addrinfo *ai = NULL;
+ struct in_addr *local_in = NULL;
+ int nlocal_in = 0;
+ struct in6_addr *local_in6 = NULL;
+ int nlocal_in6 = 0;
+ int addr_index;
+
+ MonoDomain *domain = mono_domain_get ();
+
+ addr_index = 0;
+ *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
+ if (add_local_ips) {
+ local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
+ local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in);
+ if (nlocal_in || nlocal_in6) {
+ *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
+ if (nlocal_in) {
+ MonoString *addr_string;
+ char addr [16];
+ int i;
+
+ for (i = 0; i < nlocal_in; i++) {
+ inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
+ addr_string = mono_string_new (domain, addr);
+ mono_array_set (*h_addr_list, MonoString *, addr_index, addr_string);
+ addr_index++;
+ }
+ }
+
+ if (nlocal_in6) {
+ MonoString *addr_string;
+ char addr [46];
+ int i;
+
+ for (i = 0; i < nlocal_in6; i++) {
+ inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
+ addr_string = mono_string_new (domain, addr);
+ mono_array_set (*h_addr_list, MonoString *, addr_index, addr_string);
+ addr_index++;
+ }
+ }
+
+ g_free (local_in);
+ g_free (local_in6);
+ if (info) {
+ freeaddrinfo (info);
+ }
+ return TRUE;
+ }
+
+ g_free (local_in);
+ g_free (local_in6);
+ }
+
+ for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
+ if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
+ continue;
+
+ count++;
+ }
+
+ *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
+
+ for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
MonoString *addr_string;
- char addr[16];
-
- g_snprintf(addr, 16, "%u.%u.%u.%u",
- (unsigned char)he->h_addr_list[i][0],
- (unsigned char)he->h_addr_list[i][1],
- (unsigned char)he->h_addr_list[i][2],
- (unsigned char)he->h_addr_list[i][3]);
-
- addr_string=mono_string_new(domain, addr);
- mono_array_set(*h_addr_list, MonoString *, i, addr_string);
- i++;
+ const char *ret;
+ char buffer [46]; /* Max. size for IPv6 */
+
+ if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
+ continue;
+ }
+
+ if(ai->ai_family == PF_INET) {
+ ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
+ } else {
+ ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 46);
+ }
+
+ if(ret) {
+ addr_string=mono_string_new(domain, buffer);
+ } else {
+ addr_string=mono_string_new(domain, "");
+ }
+
+ mono_array_set(*h_addr_list, MonoString *, addr_index, addr_string);
+
+ if(!i && ai->ai_canonname != NULL) {
+ *h_name=mono_string_new(domain, ai->ai_canonname);
+ }
+
+ addr_index++;
+ }
+
+ if(info) {
+ freeaddrinfo(info);
}
return(TRUE);
}
+#endif
-extern MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
+#ifdef AF_INET6
+MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
{
+ gboolean add_local_ips = FALSE;
+#ifdef HAVE_SIOCGIFCONF
+ guchar this_hostname [256];
+#endif
+#if !defined(HAVE_GETHOSTBYNAME2_R)
+ struct addrinfo *info = NULL, hints;
+ char *hostname;
+
+ MONO_ARCH_SAVE_REGS;
+
+ hostname=mono_string_to_utf8 (host);
+#ifdef HAVE_SIOCGIFCONF
+ if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
+ if (!strcmp (hostname, this_hostname))
+ add_local_ips = TRUE;
+ }
+#endif
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = get_family_hint ();
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_CANONNAME;
+
+ if (getaddrinfo(hostname, NULL, &hints, &info) == -1) {
+ return(FALSE);
+ }
+
+ g_free(hostname);
+
+ return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
+#else
+ struct hostent he1,*hp1, he2, *hp2;
+ int buffer_size1, buffer_size2;
+ char *buffer1, *buffer2;
+ int herr;
+ gboolean return_value;
char *hostname;
+
+ MONO_ARCH_SAVE_REGS;
+
+ hostname=mono_string_to_utf8 (host);
+
+#ifdef HAVE_SIOCGIFCONF
+ if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
+ if (!strcmp (hostname, this_hostname))
+ add_local_ips = TRUE;
+ }
+#endif
+
+ buffer_size1 = 512;
+ buffer_size2 = 512;
+ buffer1 = g_malloc0(buffer_size1);
+ buffer2 = g_malloc0(buffer_size2);
+
+ while (gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
+ &hp1, &herr) == ERANGE) {
+ buffer_size1 *= 2;
+ buffer1 = g_realloc(buffer1, buffer_size1);
+ }
+
+ if (hp1 == NULL)
+ {
+ while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
+ buffer_size2, &hp2, &herr) == ERANGE) {
+ buffer_size2 *= 2;
+ buffer2 = g_realloc(buffer2, buffer_size2);
+ }
+ }
+ else
+ hp2 = NULL;
+
+ return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
+ h_addr_list, add_local_ips);
+
+ g_free(buffer1);
+ g_free(buffer2);
+ g_free(hostname);
+
+ return(return_value);
+#endif /* HAVE_GETHOSTBYNAME2_R */
+}
+#else /* AF_INET6 */
+MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
+{
struct hostent *he;
+ char *hostname;
+ gboolean add_local_ips = FALSE;
+#ifdef HAVE_SIOCGIFCONF
+ guchar this_hostname [256];
+#endif
MONO_ARCH_SAVE_REGS;
hostname=mono_string_to_utf8(host);
- he=gethostbyname(hostname);
+#ifdef HAVE_SIOCGIFCONF
+ if (gethostname (this_hostname, sizeof (this_hostname)) != -1) {
+ if (!strcmp (hostname, this_hostname))
+ add_local_ips = TRUE;
+ }
+#endif
+
+ he = _wapi_gethostbyname (hostname);
g_free(hostname);
-
+
if(he==NULL) {
return(FALSE);
}
- return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list));
+ return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
}
+#endif /* AF_INET6 */
#ifndef HAVE_INET_PTON
static int
extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
{
+ char *address;
+
+#ifdef AF_INET6
+ struct sockaddr_in saddr;
+ struct sockaddr_in6 saddr6;
+ struct addrinfo *info = NULL, hints;
+ gint32 family;
+ char hostname[1024] = {0};
+#else
struct in_addr inaddr;
struct hostent *he;
- char *address;
-
+#endif
+
MONO_ARCH_SAVE_REGS;
address = mono_string_to_utf8 (addr);
+
+#ifdef AF_INET6
+ if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
+ /* Maybe an ipv6 address */
+ if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
+ g_free (address);
+ return FALSE;
+ }
+ else {
+ family = AF_INET6;
+ saddr6.sin6_family = AF_INET6;
+ }
+ }
+ else {
+ family = AF_INET;
+ saddr.sin_family = AF_INET;
+ }
+ g_free(address);
+
+ if(family == AF_INET) {
+ if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
+ hostname, sizeof(hostname), NULL, 0,
+ NI_NAMEREQD) != 0) {
+ return(FALSE);
+ }
+ } else if(family == AF_INET6) {
+ if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
+ hostname, sizeof(hostname), NULL, 0,
+ NI_NAMEREQD) != 0) {
+ return(FALSE);
+ }
+ }
+
+ memset (&hints, 0, sizeof(hints));
+ hints.ai_family = get_family_hint ();
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_CANONNAME;
+
+ if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
+ return(FALSE);
+ }
+
+ return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
+#else
if (inet_pton (AF_INET, address, &inaddr) <= 0) {
g_free (address);
- return FALSE;
+ return(FALSE);
}
-
g_free (address);
- if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL)
- return FALSE;
-
- return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list));
+
+ if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
+ return(FALSE);
+ }
+
+ return(hostent_to_IPHostEntry (he, h_name, h_aliases, h_addr_list, FALSE));
+#endif
}
extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
MONO_ARCH_SAVE_REGS;
- ret=gethostname (hostname, sizeof(hostname));
+ ret = gethostname (hostname, sizeof (hostname));
if(ret==-1) {
return(FALSE);
}
return(TRUE);
}
-
void mono_network_init(void)
{
WSADATA wsadata;
{
WSACleanup();
}
+