2004-01-19 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / metadata / socket-io.c
index c6e38a0808f051c751b11728c27fc770b3553627..96c8d46154569ef797567d969fc78754000d9df1 100644 (file)
@@ -11,6 +11,9 @@
 
 #include <glib.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/appdomain.h>
 
 #include <sys/time.h> 
+
+#ifdef HAVE_NETDB_H
 #include <netdb.h>
+#endif
+#ifdef HAVE_SYS_FILIO_H
+#include <sys/filio.h>     /* defines FIONBIO and FIONREAD */
+#endif
+#ifdef HAVE_SYS_SOCKIO_H
+#include <sys/sockio.h>    /* defines SIOCATMARK */
+#endif
+#ifdef HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
+
+#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
 
@@ -62,9 +87,11 @@ static gint32 convert_family(MonoAddressFamily mono_family)
                family=AF_INET;
                break;
                
+#ifdef AF_IPX
        case AddressFamily_Ipx:
                family=AF_IPX;
                break;
+#endif
                
        case AddressFamily_Sna:
                family=AF_SNA;
@@ -78,9 +105,11 @@ static gint32 convert_family(MonoAddressFamily mono_family)
                family=AF_APPLETALK;
                break;
                
+#ifdef AF_INET6
        case AddressFamily_InterNetworkV6:
                family=AF_INET6;
                break;
+#endif
 #ifdef AF_IRDA 
        case AddressFamily_Irda:
                family=AF_IRDA;
@@ -110,9 +139,11 @@ static MonoAddressFamily convert_to_mono_family(guint16 af_family)
                family=AddressFamily_InterNetwork;
                break;
                
+#ifdef AF_IPX
        case AF_IPX:
                family=AddressFamily_Ipx;
                break;
+#endif
                
        case AF_SNA:
                family=AddressFamily_Sna;
@@ -126,9 +157,11 @@ static MonoAddressFamily convert_to_mono_family(guint16 af_family)
                family=AddressFamily_AppleTalk;
                break;
                
+#ifdef AF_INET6
        case AF_INET6:
                family=AddressFamily_InterNetworkV6;
                break;
+#endif
                
 #ifdef AF_IRDA 
        case AF_IRDA:
@@ -184,6 +217,7 @@ static gint32 convert_proto(MonoProtocolType mono_proto)
        
        switch(mono_proto) {
        case ProtocolType_IP:
+       case ProtocolType_IPv6:
        case ProtocolType_Icmp:
        case ProtocolType_Igmp:
        case ProtocolType_Ggp:
@@ -206,6 +240,7 @@ static gint32 convert_proto(MonoProtocolType mono_proto)
                break;
                
        default:
+               break;
        }
 
        return(proto);
@@ -314,15 +349,21 @@ static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
                case SocketOptionName_IPOptions:
                        *system_name = IP_OPTIONS;
                        break;
+#ifdef IP_HDRINCL
                case SocketOptionName_HeaderIncluded:
                        *system_name = IP_HDRINCL;
                        break;
+#endif
+#ifdef IP_TOS
                case SocketOptionName_TypeOfService:
                        *system_name = IP_TOS;
                        break;
+#endif
+#ifdef IP_TTL
                case SocketOptionName_IpTimeToLive:
                        *system_name = IP_TTL;
                        break;
+#endif
                case SocketOptionName_MulticastInterface:
                        *system_name = IP_MULTICAST_IF;
                        break;
@@ -356,6 +397,68 @@ static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
                        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:
+                       *system_name = IPV6_PKTINFO;
+                       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
@@ -369,7 +472,7 @@ static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
                                struct protoent *pent;
                                
                                pent = getprotobyname ("TCP");
-                               proto = pent ? pent->p_proto : 0 /* is 0 a good default value?? */;
+                               proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
                                cached = 1;
                        }
                        
@@ -420,11 +523,41 @@ static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
 
 #define STASH_SYS_ASS(this) \
        if(system_assembly == NULL) { \
-               system_assembly=this->vtable->klass->image; \
+               system_assembly=mono_image_loaded ("System"); \
        }
 
 static MonoImage *system_assembly=NULL;
 
+
+#ifdef AF_INET6
+static gint32 get_family_hint(void)
+{
+       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
+
 static MonoException *get_socket_exception(guint32 error_code)
 {
        /* Don't cache this exception, because we need the object
@@ -445,7 +578,7 @@ static MonoException *get_socket_exception(guint32 error_code)
        return(ex);
 }
 
-SOCKET 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)
 {
        SOCKET sock;
        gint32 sock_family;
@@ -454,6 +587,8 @@ SOCKET ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gin
        int ret;
        int true=1;
        
+       MONO_ARCH_SAVE_REGS;
+
        STASH_SYS_ASS(this);
        
        sock_family=convert_family(family);
@@ -480,7 +615,19 @@ SOCKET ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gin
                return(NULL);
        }
 
-       /* .net seems to set this by default */
+       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
+
+       /* .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));
        if(ret==SOCKET_ERROR) {
                closesocket(sock);
@@ -488,17 +635,31 @@ SOCKET ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gin
                return(NULL);
        }
        
-       return(sock);
+       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)
 {
+       MONO_ARCH_SAVE_REGS;
+
+#ifdef DEBUG
+       g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
+#endif
+
        closesocket(sock);
 }
 
 gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
 {
+       MONO_ARCH_SAVE_REGS;
+
+#ifdef DEBUG
        g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
+#endif
+
        return(WSAGetLastError());
 }
 
@@ -506,6 +667,8 @@ gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock)
 {
        int ret, amount;
        
+       MONO_ARCH_SAVE_REGS;
+
        ret=ioctlsocket(sock, FIONREAD, &amount);
        if(ret==SOCKET_ERROR) {
                mono_raise_exception(get_socket_exception(WSAGetLastError()));
@@ -520,23 +683,27 @@ void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
 {
        int ret;
        
+       MONO_ARCH_SAVE_REGS;
+
        ret=ioctlsocket(sock, FIONBIO, &block);
        if(ret==SOCKET_ERROR) {
                mono_raise_exception(get_socket_exception(WSAGetLastError()));
        }
 }
 
-SOCKET ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock)
+gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock)
 {
        SOCKET newsock;
        
+       MONO_ARCH_SAVE_REGS;
+
        newsock=accept(sock, NULL, 0);
        if(newsock==INVALID_SOCKET) {
                mono_raise_exception(get_socket_exception(WSAGetLastError()));
                return(NULL);
        }
        
-       return(newsock);
+       return(GUINT_TO_POINTER (newsock));
 }
 
 void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
@@ -544,6 +711,8 @@ void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
 {
        int ret;
        
+       MONO_ARCH_SAVE_REGS;
+
        ret=listen(sock, backlog);
        if(ret==SOCKET_ERROR) {
                mono_raise_exception(get_socket_exception(WSAGetLastError()));
@@ -583,8 +752,8 @@ static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
                return(NULL);
        }
 
-       mono_array_set(data, guint8, 0, family);
-       mono_array_set(data, guint8, 1, sa_size+2);
+       mono_array_set(data, guint8, 0, family & 0x0FF);
+       mono_array_set(data, guint8, 1, ((family << 8) & 0x0FFFF));
        
        if(saddr->sa_family==AF_INET) {
                struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
@@ -605,6 +774,49 @@ static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
                *(MonoArray **)(((char *)sockaddr_obj) + field->offset)=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_defaults.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);
+
+               *(MonoArray **)(((char *)sockaddr_obj) + field->offset)=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]);
+               }
+               
+               *(MonoArray **)(((char *)sockaddr_obj) + field->offset) = data;
+
+               return sockaddr_obj;
+#endif
        } else {
                mono_raise_exception(get_socket_exception(WSAEAFNOSUPPORT));
                return(NULL);
@@ -613,42 +825,46 @@ static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
 
 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock)
 {
-       struct sockaddr sa;
+       gchar sa[32];   /// sockaddr in not big enough for sockaddr_in6
        int salen;
        int ret;
        
-       salen=sizeof(struct sockaddr);
-       ret=getsockname(sock, &sa, &salen);
+       MONO_ARCH_SAVE_REGS;
+
+       salen=sizeof(sa);
+       ret=getsockname(sock, (struct sockaddr *)sa, &salen);
        
        if(ret==SOCKET_ERROR) {
                mono_raise_exception(get_socket_exception(WSAGetLastError()));
        }
        
 #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));
+       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));
 }
 
 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock)
 {
-       struct sockaddr sa;
+       gchar sa[32];   /// sockaddr in not big enough for sockaddr_in6
        int salen;
        int ret;
        
-       salen=sizeof(struct sockaddr);
-       ret=getpeername(sock, &sa, &salen);
+       MONO_ARCH_SAVE_REGS;
+
+       salen=sizeof(sa);
+       ret=getpeername(sock, (struct sockaddr *)sa, &salen);
        
        if(ret==SOCKET_ERROR) {
                mono_raise_exception(get_socket_exception(WSAGetLastError()));
        }
        
 #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));
+       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));
 }
 
 static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
@@ -664,17 +880,20 @@ static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
        data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
 
        /* The data buffer is laid out as follows:
-        * byte 0 is the address family
-        * byte 1 is the buffer length
-        * bytes 2 and 3 are the port info
-        * the rest is the address info
+        * byte 0 is the address family low byte
+        * byte 1 is the address family high byte
+        * INET:
+        *      bytes 2 and 3 are the port info
+        *      the rest is the address info
+        * UNIX:
+        *      the rest is the file name
         */
-       len=mono_array_get(data, guint8, 1);
-       if((len<2) || (mono_array_length(data)!=len)) {
-               mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
+       len = mono_array_length (data);
+       if (len < 2) {
+               mono_raise_exception (mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
        }
        
-       family=convert_family(mono_array_get(data, guint8, 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) +
@@ -690,6 +909,45 @@ static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
 
                *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);
+               int i;
+
+               if (len - 2 > MONO_SIZEOF_SUNPATH)
+                       mono_raise_exception (mono_get_exception_index_out_of_range ());
+
+               sock_un->sun_family = family;
+               for (i = 0; i < len - 2; i++)
+                       sock_un->sun_path [i] = mono_array_get (data, guint8,
+                                                               i + 2);
+               sock_un->sun_path [len - 2] = '\0';
+               *sa_size = sizeof (struct sockaddr_un);
+
+               return (struct sockaddr *)sock_un;
+#endif
        } else {
                mono_raise_exception(get_socket_exception(WSAEAFNOSUPPORT));
                return(0);
@@ -702,10 +960,12 @@ extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoO
        int sa_size;
        int ret;
        
+       MONO_ARCH_SAVE_REGS;
+
        sa=create_sockaddr_from_object(sockaddr, &sa_size);
 
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION ": binding to %s port %d", inet_ntoa(sa.sin_addr), port);
+       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);
@@ -722,10 +982,12 @@ extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, Mo
        int sa_size;
        int ret;
        
+       MONO_ARCH_SAVE_REGS;
+
        sa=create_sockaddr_from_object(sockaddr, &sa_size);
        
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION ": connecting to %s port %d", inet_ntoa(sa.sin_addr), port);
+       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);
@@ -743,6 +1005,8 @@ gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArr
        gint32 alen;
        int recvflags=0;
        
+       MONO_ARCH_SAVE_REGS;
+
        alen=mono_array_length(buffer);
        if(offset+count>alen) {
                return(0);
@@ -767,6 +1031,8 @@ gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoAr
        struct sockaddr *sa;
        int sa_size;
        
+       MONO_ARCH_SAVE_REGS;
+
        alen=mono_array_length(buffer);
        if(offset+count>alen) {
                return(0);
@@ -777,13 +1043,14 @@ gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoAr
        buf=mono_array_addr(buffer, guchar, offset);
        
        ret=recvfrom(sock, buf, count, recvflags, sa, &sa_size);
-       g_free(sa);
        
        if(ret==SOCKET_ERROR) {
+               g_free(sa);
                mono_raise_exception(get_socket_exception(WSAGetLastError()));
        }
 
        *sockaddr=create_object_from_sockaddr(sa, sa_size);
+       g_free(sa);
        
        return(ret);
 }
@@ -795,6 +1062,8 @@ gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray
        gint32 alen;
        int sendflags=0;
        
+       MONO_ARCH_SAVE_REGS;
+
        alen=mono_array_length(buffer);
        if(offset+count>alen) {
                return(0);
@@ -827,6 +1096,8 @@ gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArra
        struct sockaddr *sa;
        int sa_size;
        
+       MONO_ARCH_SAVE_REGS;
+
        alen=mono_array_length(buffer);
        if(offset+count>alen) {
                return(0);
@@ -854,13 +1125,13 @@ gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArra
        return(ret);
 }
 
-static SOCKET Socket_to_SOCKET(MonoObject *socket)
+static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
 {
        SOCKET sock;
        MonoClassField *field;
        
-       field=mono_class_get_field_from_name(socket->vtable->klass, "socket");
-       sock=*(SOCKET *)(((char *)socket)+field->offset);
+       field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
+       sock=*(SOCKET *)(((char *)sockobj)+field->offset);
 
        return(sock);
 }
@@ -868,124 +1139,175 @@ static SOCKET Socket_to_SOCKET(MonoObject *socket)
 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **read_socks, MonoArray **write_socks, MonoArray **err_socks, gint32 timeout)
 {
        fd_set readfds, writefds, errfds;
+       fd_set *readptr = NULL, *writeptr = NULL, *errptr = NULL;
        struct timeval tv;
        div_t divvy;
        int ret;
-       int readarrsize, writearrsize, errarrsize;
+       int readarrsize = 0, writearrsize = 0, errarrsize = 0;
        MonoDomain *domain=mono_domain_get();
        MonoClass *sock_arr_class;
        MonoArray *socks;
        int count;
        int i;
+       SOCKET handle;
        
-       readarrsize=mono_array_length(*read_socks);
+       MONO_ARCH_SAVE_REGS;
+
+       if (*read_socks)
+               readarrsize=mono_array_length(*read_socks);
+
        if(readarrsize>FD_SETSIZE) {
                mono_raise_exception(get_socket_exception(WSAEFAULT));
                return;
        }
        
-       FD_ZERO(&readfds);
-       for(i=0; i<readarrsize; i++) {
-               FD_SET(Socket_to_SOCKET(mono_array_get(*read_socks, MonoObject *, i)), &readfds);
+       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);
+               }
        }
        
-       writearrsize=mono_array_length(*write_socks);
+       if (*write_socks)
+               writearrsize=mono_array_length(*write_socks);
+
        if(writearrsize>FD_SETSIZE) {
                mono_raise_exception(get_socket_exception(WSAEFAULT));
                return;
        }
        
-       FD_ZERO(&writefds);
-       for(i=0; i<writearrsize; i++) {
-               FD_SET(Socket_to_SOCKET(mono_array_get(*write_socks, MonoObject *, i)), &writefds);
+       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);
+               }
        }
        
-       errarrsize=mono_array_length(*err_socks);
+       if (*err_socks)
+               errarrsize=mono_array_length(*err_socks);
+
        if(errarrsize>FD_SETSIZE) {
                mono_raise_exception(get_socket_exception(WSAEFAULT));
                return;
        }
        
-       FD_ZERO(&errfds);
-       for(i=0; i<errarrsize; i++) {
-               FD_SET(Socket_to_SOCKET(mono_array_get(*err_socks, MonoObject *, i)), &errfds);
+       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*1000000;
+
+       divvy = div (timeout, 1000000);
        
-               ret=select(0, &readfds, &writefds, &errfds, &tv);
-       } else {
-               ret=select(0, &readfds, &writefds, &errfds, NULL);
-       }
+       do {
+               if(timeout>=0) {
+                       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);
+               }
+       } while ((ret==SOCKET_ERROR) && (WSAGetLastError() == WSAEINTR));
        
        if(ret==SOCKET_ERROR) {
                mono_raise_exception(get_socket_exception(WSAGetLastError()));
                return;
        }
 
-       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 (readarrsize) {
+               sock_arr_class=((MonoObject *)*read_socks)->vtable->klass;
                
-               if(FD_ISSET(Socket_to_SOCKET(sock), &readfds)) {
-                       mono_array_set(socks, MonoObject *, count, sock);
-                       count++;
+               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++;
+                       }
+               }
+               *read_socks=socks;
+       } else {
+               *read_socks = NULL;
        }
-       *read_socks=socks;
 
-       count=0;
-       for(i=0; i<writearrsize; i++) {
-               if(FD_ISSET(Socket_to_SOCKET(mono_array_get(*write_socks, MonoObject *, i)), &writefds)) {
-                       count++;
+       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++;
+               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++;
+                       }
                }
+               *write_socks=socks;
+       } else {
+               *write_socks = NULL;
        }
-       *write_socks=socks;
 
-       count=0;
-       for(i=0; i<errarrsize; i++) {
-               if(FD_ISSET(Socket_to_SOCKET(mono_array_get(*err_socks, MonoObject *, i)), &errfds)) {
-                       count++;
+       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++;
+               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;
        }
-       *err_socks=socks;
 }
 
+static MonoObject* int_to_object (MonoDomain *domain, int val)
+{
+       /* construct an Int32 object to hold val */
+       MonoObject* obj = mono_object_new(domain, mono_defaults.int32_class);
+
+       /* Locate and set the "value" field */
+       MonoClassField *field = mono_class_get_field_from_name(mono_defaults.int32_class,
+                                            "value");
+       *(gint32 *)(((char *)obj)+field->offset)=val;
+    return obj;
+}
+
+
 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val)
 {
        int system_level;
@@ -995,11 +1317,15 @@ void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET soc
        int valsize=sizeof(val);
        struct linger linger;
        int lingersize=sizeof(linger);
+       struct timeval tv;
+       int tvsize=sizeof(tv);
        MonoDomain *domain=mono_domain_get();
        MonoObject *obj;
        MonoClass *obj_class;
        MonoClassField *field;
        
+       MONO_ARCH_SAVE_REGS;
+
        ret=convert_sockopt_level_and_name(level, name, &system_level,
                                           &system_name);
        if(ret==-1) {
@@ -1018,6 +1344,12 @@ void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET soc
                               &lingersize);
                break;
                
+       case SocketOptionName_SendTimeout:
+       case SocketOptionName_ReceiveTimeout:
+               ret=getsockopt(sock, system_level, system_name, &tv,
+                          &tvsize);
+               break;
+
        default:
                ret=getsockopt(sock, system_level, system_name, &val,
                               &valsize);
@@ -1049,18 +1381,16 @@ void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET soc
                
        case SocketOptionName_DontLinger:
                /* construct a bool int in val - true if linger is off */
-               val=!linger.l_onoff;
-               
-               /* fall through */
+               obj = int_to_object (domain, !linger.l_onoff);
+               break;
                
+       case SocketOptionName_SendTimeout:
+       case SocketOptionName_ReceiveTimeout:
+               obj = int_to_object (domain, (tv.tv_sec * 1000) + (tv.tv_usec / 1000));
+               break;
+
        default:
-               /* construct an Int32 object to hold val */
-               obj=mono_object_new(domain, mono_defaults.int32_class);
-               
-               /* 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 = int_to_object (domain, val);
        }
 
        *obj_val=obj;
@@ -1074,6 +1404,8 @@ void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET soc
        guchar *buf;
        int valsize;
        
+       MONO_ARCH_SAVE_REGS;
+
        ret=convert_sockopt_level_and_name(level, name, &system_level,
                                           &system_name);
        if(ret==-1) {
@@ -1093,23 +1425,77 @@ void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET soc
 static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
 {
        struct in_addr inaddr;
-       guint64 addr;
        MonoClassField *field;
        
        field=mono_class_get_field_from_name(ipaddr->vtable->klass, "address");
-       addr=*(guint64 *)(((char *)ipaddr)+field->offset);
 
-       /* No idea why .net uses a 64bit type to hold a 32bit value */
-       inaddr.s_addr=htonl((guint32)addr);
+       /* No idea why .net uses a 64bit type to hold a 32bit value...
+        *
+        * Internal value of IPAddess is in Network Order, there is no need
+        * to call htonl here.
+        */
+       inaddr.s_addr=(guint32)*(guint64 *)(((char *)ipaddr)+field->offset);
        
        return(inaddr);
 }
 
+#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, "_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)
 {
        int system_level;
        int system_name;
        int ret;
+#ifdef AF_INET6
+       int sol_ip;
+       int sol_ipv6;
+
+#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);
@@ -1146,32 +1532,74 @@ void ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal(SOCKET sock, g
                        ret=setsockopt(sock, system_level, system_name,
                                       &linger, valsize);
                        break;
-#ifdef HAVE_IP_ADD_MEMBERSHIP
                case SocketOptionName_AddMembership:
-#endif
-#ifdef HAVE_IP_DROP_MEMBERSHIP
                case SocketOptionName_DropMembership:
-#endif
-#if defined (HAVE_IP_ADD_MEMBERSHIP) || defined (HAVE_IP_DROP_MEMBERSHIP)
+#if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
                {
-                       struct ip_mreqn 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 = 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}};
+#else
+                               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");
-                       mreq.imr_multiaddr = ipaddress_to_struct_in_addr (*(gpointer *)(((char *)obj_val) +
-                                                                                       field->offset));
-                       field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
-                       mreq.imr_address = ipaddress_to_struct_in_addr (*(gpointer *)(((char *)obj_val) +
-                                                                                     field->offset));
-                       valsize = sizeof (mreq);
-                       ret = setsockopt (sock, system_level, system_name,
-                                         &mreq, valsize);
+                               /* 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);
+                               }
+#else
+                               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));
+                       }
                        break;
                }
-#endif /* HAVE_IP_[ADD,DROP]_MEMBERSHIP */
+#endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
                default:
                        /* Throw an exception */
                        mono_raise_exception(get_socket_exception(WSAEINVAL));
@@ -1185,8 +1613,19 @@ void ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal(SOCKET sock, g
                        mono_raise_exception(get_socket_exception(WSAGetLastError()));
                }
        } else {
-               ret=setsockopt(sock, system_level, system_name, &int_val,
+               switch(name) {
+                       case SocketOptionName_SendTimeout:
+                       case SocketOptionName_ReceiveTimeout: {
+                               struct timeval tv;
+                               tv.tv_sec = int_val / 1000;
+                               tv.tv_usec = (int_val % 1000) * 1000;
+                               ret=setsockopt(sock, system_level, system_name, &tv, sizeof (tv));
+                               break;
+                       }
+                       default:
+                               ret=setsockopt(sock, system_level, system_name, &int_val,
                               sizeof(int_val));
+               }
        }
 
        if(ret==SOCKET_ERROR) {
@@ -1199,6 +1638,8 @@ void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
 {
        int ret;
        
+       MONO_ARCH_SAVE_REGS;
+
        /* Currently, the values for how (recv=0, send=1, both=2) match
         * the BSD API
         */
@@ -1208,6 +1649,7 @@ void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
        }
 }
 
+#ifndef AF_INET6
 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
                                       MonoArray **h_aliases,
                                       MonoArray **h_addr_list)
@@ -1260,22 +1702,297 @@ static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
 
        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)
+{
+       MonoDomain *domain = mono_domain_get ();
+       int i, host_count, host_index, family_hint;
+
+       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_defaults.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_defaults.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
+        */
+       *h_addr_list=mono_array_new (domain, mono_defaults.string_class,
+                                    host_count);
+
+       host_index = 0;
+
+       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[40];
+
+                       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[17];
+
+                       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)
+{
+       gint32 count, i;
+       struct addrinfo *ai = NULL;
+
+       MonoDomain *domain = mono_domain_get ();
+
+       for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
+               if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
+                       continue;
+               }
+
+               count++;
+       }
+
+       *h_aliases=mono_array_new(domain, mono_defaults.string_class, 0);
+       *h_addr_list=mono_array_new(domain, mono_defaults.string_class, count);
+
+       for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
+               MonoString *addr_string;
+               const char *ret;
+               char *buffer;
+               gint32 buffer_size = 0;
+
+               if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
+                       continue;
+               }
+
+               buffer_size = 256;
+               do {
+                       buffer = g_malloc0(buffer_size);
+
+                       if(ai->ai_family == PF_INET) {
+                               ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, buffer_size);
+                       } else {
+                               ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, buffer_size);
+                       }
+
+                       if(ret == 0) {
+                               g_free(buffer);
+                               buffer_size += 256;
+                       }
+               } while(ret == 0 && errno == ENOSPC);
+
+               if(ret) {
+                       addr_string=mono_string_new(domain, buffer);
+                       g_free(buffer);
+               } else {
+                       addr_string=mono_string_new(domain, "");
+               }
+
+               mono_array_set(*h_addr_list, MonoString *, i, addr_string);
+
+               if(!i) {
+                       *h_name=mono_string_new(domain, ai->ai_canonname);
+               }
+
+               i++;
+       }
+
+       if(info) {
+               freeaddrinfo(info);
+       }
+
+       return(TRUE);
+}
+#endif
 
-extern gboolean 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)
 {
+#if !defined(HAVE_GETHOSTBYNAME2_R)
+       struct addrinfo *info = NULL, hints;
+       char *hostname;
+       
+       MONO_ARCH_SAVE_REGS;
+       
+       hostname=mono_string_to_utf8 (host);
+       
+       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));
+#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);
+
+       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);
+       }
+
+       while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
+                               buffer_size2, &hp2, &herr) == ERANGE) {
+               buffer_size2 *= 2;
+               buffer2 = g_realloc(buffer2, buffer_size2);
+       }
+
+       return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
+                                              h_addr_list);
+
+       g_free(buffer1);
+       g_free(buffer2);
+
+       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;
        
+       MONO_ARCH_SAVE_REGS;
+
        hostname=mono_string_to_utf8(host);
+
        he=gethostbyname(hostname);
-       free(hostname);
-       
+       g_free(hostname);
+
        if(he==NULL) {
                return(FALSE);
        }
 
        return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list));
 }
+#endif /* AF_INET6 */
 
 #ifndef HAVE_INET_PTON
 static int
@@ -1293,7 +2010,7 @@ inet_pton (int family, const char *address, void *inaddrp)
 #else
                /* assume the system has inet_addr(), if it doesn't
                   have that we're pretty much screwed... */
-               in_addr_t inaddr;
+               guint32 inaddr;
                
                if (!strcmp (address, "255.255.255.255")) {
                        /* special-case hack */
@@ -1307,34 +2024,109 @@ inet_pton (int family, const char *address, void *inaddrp)
                                return 0;
                }
                
-               memcpy (inaddrp, &inaddr, sizeof (in_addr_t));
+               memcpy (inaddrp, &inaddr, sizeof (guint32));
                return 1;
 #endif /* HAVE_INET_ATON */
        }
        
-       errno = EAFNOSUPPRT;
        return -1;
 }
 #endif /* !HAVE_INET_PTON */
 
-extern gboolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
+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));
+#else
        if (inet_pton (AF_INET, address, &inaddr) <= 0) {
-               free (address);
-               return FALSE;
+               g_free (address);
+               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));
+#endif
+}
+
+extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
+{
+       guchar hostname[256];
+       int ret;
        
-       if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL)
-               return FALSE;
+       MONO_ARCH_SAVE_REGS;
+
+       ret=gethostname (hostname, sizeof(hostname));
+       if(ret==-1) {
+               return(FALSE);
+       }
        
-       return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list));
+       *h_name=mono_string_new(mono_domain_get (), hostname);
+
+       return(TRUE);
 }
 
+
 void mono_network_init(void)
 {
        WSADATA wsadata;