Fixed some debug messages
[mono.git] / mono / metadata / socket-io.c
index 87a756d39dd8e10a49e15193557b8eca7c3c81c4..5aa4dc65ead158d81a5bd6c3253d563aca968aa2 100644 (file)
@@ -8,7 +8,11 @@
  */
 
 #include <config.h>
+
 #include <glib.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
 
 #include <mono/metadata/object.h>
 #include <mono/io-layer/io-layer.h>
 #include <mono/metadata/exception.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
+
+
 #undef DEBUG
 
 static gint32 convert_family(MonoAddressFamily mono_family)
@@ -201,13 +218,227 @@ static gint32 convert_proto(MonoProtocolType mono_proto)
                break;
                
        default:
+               break;
        }
 
        return(proto);
 }
 
+static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
+                                            MonoSocketOptionName mono_name,
+                                            int *system_level,
+                                            int *system_name)
+{
+       switch (mono_level) {
+       case SocketOptionLevel_Socket:
+               *system_level = SOL_SOCKET;
+               
+               switch(mono_name) {
+               case SocketOptionName_DontLinger:
+                       /* This is SO_LINGER, because the setsockopt
+                        * internal call maps DontLinger to SO_LINGER
+                        * with l_onoff=0
+                        */
+                       *system_name = SO_LINGER;
+                       break;
+               case SocketOptionName_Debug:
+                       *system_name = SO_DEBUG;
+                       break;
+#ifdef SO_ACCEPTCONN
+               case SocketOptionName_AcceptConnection:
+                       *system_name = SO_ACCEPTCONN;
+                       break;
+#endif
+               case SocketOptionName_ReuseAddress:
+                       *system_name = SO_REUSEADDR;
+                       break;
+               case SocketOptionName_KeepAlive:
+                       *system_name = SO_KEEPALIVE;
+                       break;
+               case SocketOptionName_DontRoute:
+                       *system_name = SO_DONTROUTE;
+                       break;
+               case SocketOptionName_Broadcast:
+                       *system_name = SO_BROADCAST;
+                       break;
+               case SocketOptionName_Linger:
+                       *system_name = SO_LINGER;
+                       break;
+               case SocketOptionName_OutOfBandInline:
+                       *system_name = SO_OOBINLINE;
+                       break;
+               case SocketOptionName_SendBuffer:
+                       *system_name = SO_SNDBUF;
+                       break;
+               case SocketOptionName_ReceiveBuffer:
+                       *system_name = SO_RCVBUF;
+                       break;
+               case SocketOptionName_SendLowWater:
+                       *system_name = SO_SNDLOWAT;
+                       break;
+               case SocketOptionName_ReceiveLowWater:
+                       *system_name = SO_RCVLOWAT;
+                       break;
+               case SocketOptionName_SendTimeout:
+                       *system_name = SO_SNDTIMEO;
+                       break;
+               case SocketOptionName_ReceiveTimeout:
+                       *system_name = SO_RCVTIMEO;
+                       break;
+               case SocketOptionName_Error:
+                       *system_name = SO_ERROR;
+                       break;
+               case SocketOptionName_Type:
+                       *system_name = SO_TYPE;
+                       break;
+               case SocketOptionName_ExclusiveAddressUse:
+               case SocketOptionName_UseLoopback:
+               case SocketOptionName_MaxConnections:
+                       /* Can't figure out how to map these, so fall
+                        * through
+                        */
+               default:
+                       g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
+                       return(-1);
+               }
+               break;
+               
+       case SocketOptionLevel_IP:
+#ifdef HAVE_SOL_IP
+               *system_level = SOL_IP;
+#else
+               if (1) {
+                       static int cached = 0;
+                       static int proto;
+                       
+                       if (!cached) {
+                               struct protoent *pent;
+                               
+                               pent = getprotobyname ("IP");
+                               proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
+                               cached = 1;
+                       }
+                       
+                       *system_level = proto;
+               }
+#endif /* HAVE_SOL_IP */
+               
+               switch(mono_name) {
+               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;
+               case SocketOptionName_MulticastTimeToLive:
+                       *system_name = IP_MULTICAST_TTL;
+                       break;
+               case SocketOptionName_MulticastLoopback:
+                       *system_name = IP_MULTICAST_LOOP;
+                       break;
+               case SocketOptionName_AddMembership:
+                       *system_name = IP_ADD_MEMBERSHIP;
+                       break;
+               case SocketOptionName_DropMembership:
+                       *system_name = IP_DROP_MEMBERSHIP;
+                       break;
+#ifdef HAVE_IP_PKTINFO
+               case SocketOptionName_PacketInformation:
+                       *system_name = IP_PKTINFO;
+                       break;
+#endif /* HAVE_IP_PKTINFO */
+               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 IP level", mono_name);
+                       return(-1);
+               }
+               break;
+               
+       case SocketOptionLevel_Tcp:
+#ifdef HAVE_SOL_TCP
+               *system_level = SOL_TCP;
+#else
+               if (1) {
+                       static int cached = 0;
+                       static int proto;
+                       
+                       if (!cached) {
+                               struct protoent *pent;
+                               
+                               pent = getprotobyname ("TCP");
+                               proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
+                               cached = 1;
+                       }
+                       
+                       *system_level = proto;
+               }
+#endif /* HAVE_SOL_TCP */
+               
+               switch(mono_name) {
+               case SocketOptionName_NoDelay:
+                       *system_name = TCP_NODELAY;
+                       break;
+#if 0
+                       /* The documentation is talking complete
+                        * bollocks here: rfc-1222 is titled
+                        * 'Advancing the NSFNET Routing Architecture'
+                        * and doesn't mention either of the words
+                        * "expedite" or "urgent".
+                        */
+               case SocketOptionName_BsdUrgent:
+               case SocketOptionName_Expedited:
+#endif
+               default:
+                       g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
+                       return(-1);
+               }
+               break;
+               
+       case SocketOptionLevel_Udp:
+               g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
+
+               switch(mono_name) {
+               case SocketOptionName_NoChecksum:
+               case SocketOptionName_ChecksumCoverage:
+               default:
+                       g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
+                       return(-1);
+               }
+               return(-1);
+               break;
+
+       default:
+               g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
+               return(-1);
+       }
+
+       return(0);
+}
+
 #define STASH_SYS_ASS(this) \
-       if(system_assembly==NULL) { \
+       if(system_assembly == NULL) { \
                system_assembly=this->vtable->klass->image; \
        }
 
@@ -233,7 +464,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;
@@ -242,6 +473,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);
@@ -276,17 +509,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());
 }
 
@@ -294,6 +541,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()));
@@ -303,17 +552,32 @@ gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock)
        return(amount);
 }
 
-SOCKET ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock)
+void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
+                                                          gboolean block)
+{
+       int ret;
+       
+       MONO_ARCH_SAVE_REGS;
+
+       ret=ioctlsocket(sock, FIONBIO, &block);
+       if(ret==SOCKET_ERROR) {
+               mono_raise_exception(get_socket_exception(WSAGetLastError()));
+       }
+}
+
+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,
@@ -321,6 +585,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()));
@@ -369,7 +635,7 @@ static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
                guint32 address=ntohl(sa_in->sin_addr.s_addr);
                
                if(sa_size<8) {
-                       mono_raise_exception(mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
+                       mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
                }
                
                mono_array_set(data, guint8, 2, (port>>8) & 0xff);
@@ -394,6 +660,8 @@ extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SO
        int salen;
        int ret;
        
+       MONO_ARCH_SAVE_REGS;
+
        salen=sizeof(struct sockaddr);
        ret=getsockname(sock, &sa, &salen);
        
@@ -402,7 +670,7 @@ extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SO
        }
        
 #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));
@@ -414,6 +682,8 @@ extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(S
        int salen;
        int ret;
        
+       MONO_ARCH_SAVE_REGS;
+
        salen=sizeof(struct sockaddr);
        ret=getpeername(sock, &sa, &salen);
        
@@ -422,7 +692,7 @@ extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(S
        }
        
 #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));
@@ -448,7 +718,7 @@ static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
         */
        len=mono_array_get(data, guint8, 1);
        if((len<2) || (mono_array_length(data)!=len)) {
-               mono_raise_exception(mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
+               mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
        }
        
        family=convert_family(mono_array_get(data, guint8, 0));
@@ -479,10 +749,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);
@@ -499,10 +771,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);
@@ -520,6 +794,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);
@@ -544,6 +820,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);
@@ -554,13 +832,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);
 }
@@ -572,6 +851,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);
@@ -604,6 +885,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);
@@ -631,6 +914,373 @@ gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArra
        return(ret);
 }
 
+static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
+{
+       SOCKET sock;
+       MonoClassField *field;
+       
+       field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
+       sock=*(SOCKET *)(((char *)sockobj)+field->offset);
+
+       return(sock);
+}
+
+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;
+       struct timeval tv;
+       div_t divvy;
+       int ret;
+       int readarrsize, writearrsize, errarrsize;
+       MonoDomain *domain=mono_domain_get();
+       MonoClass *sock_arr_class;
+       MonoArray *socks;
+       int count;
+       int i;
+       
+       MONO_ARCH_SAVE_REGS;
+
+       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);
+       }
+       
+       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);
+       }
+       
+       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);
+       }
+
+       /* 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;
+       
+               ret=select(0, &readfds, &writefds, &errfds, &tv);
+       } else {
+               ret=select(0, &readfds, &writefds, &errfds, NULL);
+       }
+       
+       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(FD_ISSET(Socket_to_SOCKET(sock), &readfds)) {
+                       mono_array_set(socks, MonoObject *, count, sock);
+                       count++;
+               }
+       }
+       *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++;
+               }
+       }
+       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;
+
+       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;
+}
+
+void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val)
+{
+       int system_level;
+       int system_name;
+       int ret;
+       int val;
+       int valsize=sizeof(val);
+       struct linger linger;
+       int lingersize=sizeof(linger);
+       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) {
+               mono_raise_exception(get_socket_exception(WSAENOPROTOOPT));
+               return;
+       }
+       
+       /* No need to deal with MulticastOption names here, because
+        * you cant getsockopt AddMembership or DropMembership (the
+        * int getsockopt will error, causing an exception)
+        */
+       switch(name) {
+       case SocketOptionName_Linger:
+       case SocketOptionName_DontLinger:
+               ret=getsockopt(sock, system_level, system_name, &linger,
+                              &lingersize);
+               break;
+               
+       default:
+               ret=getsockopt(sock, system_level, system_name, &val,
+                              &valsize);
+       }
+       
+       if(ret==SOCKET_ERROR) {
+               mono_raise_exception(get_socket_exception(WSAGetLastError()));
+               return;
+       }
+       
+       switch(name) {
+       case SocketOptionName_Linger:
+               /* build a System.Net.Sockets.LingerOption */
+               obj_class=mono_class_from_name(system_assembly,
+                                              "System.Net.Sockets",
+                                              "LingerOption");
+               obj=mono_object_new(domain, obj_class);
+               
+               /* Locate and set the fields "bool enabled" and "int
+                * seconds"
+                */
+               field=mono_class_get_field_from_name(obj_class, "enabled");
+               *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
+
+               field=mono_class_get_field_from_name(obj_class, "seconds");
+               *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
+               
+               break;
+               
+       case SocketOptionName_DontLinger:
+               /* construct a bool int in val - true if linger is off */
+               val=!linger.l_onoff;
+               
+               /* fall through */
+               
+       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_val=obj;
+}
+
+void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val)
+{
+       int system_level;
+       int system_name;
+       int ret;
+       guchar *buf;
+       int valsize;
+       
+       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));
+               return;
+       }
+
+       valsize=mono_array_length(*byte_val);
+       buf=mono_array_addr(*byte_val, guchar, 0);
+       
+       ret=getsockopt(sock, system_level, system_name, buf, &valsize);
+       if(ret==SOCKET_ERROR) {
+               mono_raise_exception(get_socket_exception(WSAGetLastError()));
+       }
+}
+
+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);
+       
+       return(inaddr);
+}
+
+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;
+
+       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));
+               return;
+       }
+
+       /* Only one of obj_val, byte_val or int_val has data */
+       if(obj_val!=NULL) {
+               MonoClassField *field;
+               struct linger linger;
+               int valsize;
+               
+               switch(name) {
+               case SocketOptionName_DontLinger:
+                       linger.l_onoff=0;
+                       linger.l_linger=0;
+                       valsize=sizeof(linger);
+                       ret=setsockopt(sock, system_level, system_name,
+                                      &linger, valsize);
+                       break;
+                       
+               case SocketOptionName_Linger:
+                       /* Dig out "bool enabled" and "int seconds"
+                        * fields
+                        */
+                       field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
+                       linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
+                       field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
+                       linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
+                       
+                       valsize=sizeof(linger);
+                       ret=setsockopt(sock, system_level, system_name,
+                                      &linger, valsize);
+                       break;
+               case SocketOptionName_AddMembership:
+               case SocketOptionName_DropMembership:
+               {
+#ifdef HAVE_STRUCT_IP_MREQN
+                       struct ip_mreqn mreq;
+#else
+                       struct ip_mreq mreq;
+#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");
+#ifdef HAVE_STRUCT_IP_MREQN
+                       mreq.imr_address = ipaddress_to_struct_in_addr (*(gpointer *)(((char *)obj_val) +
+                                                                                     field->offset));
+#else
+                       mreq.imr_interface = ipaddress_to_struct_in_addr (*(gpointer *)(((char *)obj_val) +
+                                                                                       field->offset));
+#endif /* HAVE_STRUCT_IP_MREQN */
+                       
+                       ret = setsockopt (sock, system_level, system_name,
+                                         &mreq, sizeof (mreq));
+                       break;
+               }
+               default:
+                       /* Throw an exception */
+                       mono_raise_exception(get_socket_exception(WSAEINVAL));
+               }
+       } 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);
+               if(ret==SOCKET_ERROR) {
+                       mono_raise_exception(get_socket_exception(WSAGetLastError()));
+               }
+       } else {
+               ret=setsockopt(sock, system_level, system_name, &int_val,
+                              sizeof(int_val));
+       }
+
+       if(ret==SOCKET_ERROR) {
+               mono_raise_exception(get_socket_exception(WSAGetLastError()));
+       }
+}
+
+void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
+                                                          gint32 how)
+{
+       int ret;
+       
+       MONO_ARCH_SAVE_REGS;
+
+       /* Currently, the values for how (recv=0, send=1, both=2) match
+        * the BSD API
+        */
+       ret=shutdown(sock, how);
+       if(ret==SOCKET_ERROR) {
+               mono_raise_exception(get_socket_exception(WSAGetLastError()));
+       }
+}
+
 static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
                                       MonoArray **h_aliases,
                                       MonoArray **h_addr_list)
@@ -670,7 +1320,7 @@ static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
                MonoString *addr_string;
                char addr[16];
                
-               snprintf(addr, 16, "%u.%u.%u.%u",
+               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],
@@ -684,14 +1334,16 @@ static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
        return(TRUE);
 }
 
-extern gboolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
+extern MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
 {
        char *hostname;
        struct hostent *he;
        
+       MONO_ARCH_SAVE_REGS;
+
        hostname=mono_string_to_utf8(host);
        he=gethostbyname(hostname);
-       free(hostname);
+       g_free(hostname);
        
        if(he==NULL) {
                return(FALSE);
@@ -700,27 +1352,84 @@ extern gboolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host
        return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list));
 }
 
-extern gboolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
+#ifndef HAVE_INET_PTON
+static int
+inet_pton (int family, const char *address, void *inaddrp)
 {
-       char *address;
-       guint32 inaddr;
+       if (family == AF_INET) {
+#ifdef HAVE_INET_ATON
+               struct in_addr inaddr;
+               
+               if (!inet_aton (address, &inaddr))
+                       return 0;
+               
+               memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
+               return 1;
+#else
+               /* assume the system has inet_addr(), if it doesn't
+                  have that we're pretty much screwed... */
+               guint32 inaddr;
+               
+               if (!strcmp (address, "255.255.255.255")) {
+                       /* special-case hack */
+                       inaddr = 0xffffffff;
+               } else {
+                       inaddr = inet_addr (address);
+#ifndef INADDR_NONE
+#define INADDR_NONE ((in_addr_t) -1)
+#endif
+                       if (inaddr == INADDR_NONE)
+                               return 0;
+               }
+               
+               memcpy (inaddrp, &inaddr, sizeof (guint32));
+               return 1;
+#endif /* HAVE_INET_ATON */
+       }
+       
+       return -1;
+}
+#endif /* !HAVE_INET_PTON */
+
+extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
+{
+       struct in_addr inaddr;
        struct hostent *he;
+       char *address;
        
-       address=mono_string_to_utf8(addr);
-       inaddr=inet_addr(address);
-       free(address);
-       if(inaddr==INADDR_NONE) {
-               return(FALSE);
+       MONO_ARCH_SAVE_REGS;
+
+       address = mono_string_to_utf8 (addr);
+       if (inet_pton (AF_INET, address, &inaddr) <= 0) {
+               g_free (address);
+               return FALSE;
        }
        
-       he=gethostbyaddr(&inaddr, sizeof(inaddr), AF_INET);
-       if(he==NULL) {
+       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));
+}
+
+extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
+{
+       guchar hostname[256];
+       int ret;
+       
+       MONO_ARCH_SAVE_REGS;
+
+       ret=gethostname (hostname, sizeof(hostname));
+       if(ret==-1) {
                return(FALSE);
        }
+       
+       *h_name=mono_string_new(mono_domain_get (), hostname);
 
-       return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list));
+       return(TRUE);
 }
 
+
 void mono_network_init(void)
 {
        WSADATA wsadata;