wrong error number
[mono.git] / mono / metadata / socket-io.c
index 613d1450ff23313ef42660165d585b458a4e5a2b..1ef49278ea93a38c9a4cc681dd1b9c1664f91ab2 100644 (file)
 #include <unistd.h>
 #include <errno.h>
 
+#ifndef PLATFORM_WIN32
+#ifdef HAVE_AIO_H
+#include <aio.h>
+#define USE_AIO        1
+#elif defined(HAVE_SYS_AIO_H)
+#include <sys/aio.h>
+#define USE_AIO 1
+#else
+#undef USE_AIO
+#endif
+#endif
+
 #include <mono/metadata/object.h>
 #include <mono/io-layer/io-layer.h>
 #include <mono/metadata/socket-io.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/threads.h>
+/* FIXME change this code to not mess so much with the internals */
+#include <mono/metadata/class-internals.h>
 
 #include <sys/time.h> 
 
@@ -316,6 +330,11 @@ static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
                case SocketOptionName_Type:
                        *system_name = SO_TYPE;
                        break;
+#ifdef SO_PEERCRED
+               case SocketOptionName_PeerCred:
+                       *system_name = SO_PEERCRED;
+                       break;
+#endif
                case SocketOptionName_ExclusiveAddressUse:
                case SocketOptionName_UseLoopback:
                case SocketOptionName_MaxConnections:
@@ -460,7 +479,7 @@ static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
                        return(-1);
                }
 
-               break;  /// SocketOptionLevel_IPv6
+               break;  /* SocketOptionLevel_IPv6 */
 #endif
                
        case SocketOptionLevel_Tcp:
@@ -755,7 +774,7 @@ static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
        field=mono_class_get_field_from_name(sockaddr_class, "data");
 
        /* Make sure there is space for the family and size bytes */
-       data=mono_array_new(domain, mono_defaults.byte_class, sa_size+2);
+       data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
 
        /* The data buffer is laid out as follows:
         * byte 0 is the address family
@@ -779,7 +798,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((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
+                       mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
                }
                
                mono_array_set(data, guint8, 2, (port>>8) & 0xff);
@@ -788,8 +807,8 @@ static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
                mono_array_set(data, guint8, 5, (address>>16) & 0xff);
                mono_array_set(data, guint8, 6, (address>>8) & 0xff);
                mono_array_set(data, guint8, 7, (address) & 0xff);
-               
-               *(MonoArray **)(((char *)sockaddr_obj) + field->offset)=data;
+       
+               mono_field_set_value (sockaddr_obj, field, data);
 
                return(sockaddr_obj);
 #ifdef AF_INET6
@@ -800,7 +819,7 @@ static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
                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_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
                }
 
                mono_array_set(data, guint8, 2, (port>>8) & 0xff);
@@ -819,7 +838,7 @@ static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
                mono_array_set(data, guint8, 27,
                               (sa_in->sin6_scope_id >> 24) & 0xff);
 
-               *(MonoArray **)(((char *)sockaddr_obj) + field->offset)=data;
+               mono_field_set_value (sockaddr_obj, field, data);
 
                return(sockaddr_obj);
 #endif
@@ -831,7 +850,7 @@ static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
                        mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
                }
                
-               *(MonoArray **)(((char *)sockaddr_obj) + field->offset) = data;
+               mono_field_set_value (sockaddr_obj, field, data);
 
                return sockaddr_obj;
 #endif
@@ -843,7 +862,7 @@ static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
 
 extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
 {
-       gchar sa[32];   /// sockaddr in not big enough for sockaddr_in6
+       gchar sa[32];   /* sockaddr in not big enough for sockaddr_in6 */
        int salen;
        int ret;
        
@@ -869,7 +888,7 @@ extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SO
 
 extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
 {
-       gchar sa[32];   /// sockaddr in not big enough for sockaddr_in6
+       gchar sa[32];   /* sockaddr in not big enough for sockaddr_in6 */
        int salen;
        int ret;
        
@@ -917,7 +936,7 @@ static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
         */
        len = mono_array_length (data);
        if (len < 2) {
-               mono_raise_exception (mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
+               mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
        }
        
        family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
@@ -1141,7 +1160,15 @@ gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoAr
                return(0);
        }
 
-       *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
+       /* If we didn't get a socket size, then we're probably a
+        * connected connection-oriented socket and the stack hasn't
+        * returned the remote address. All we can do is return null.
+        */
+       if ( sa_size != 0 )
+               *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
+       else
+               *sockaddr=NULL;
+
        g_free(sa);
        
        return(ret);
@@ -1399,14 +1426,7 @@ void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **read_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;
+       return mono_value_box (domain, mono_get_int32_class (), &val);
 }
 
 
@@ -1421,6 +1441,10 @@ void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET soc
        int lingersize=sizeof(linger);
        struct timeval tv;
        int tvsize=sizeof(tv);
+#ifdef SO_PEERCRED
+       struct ucred cred;
+       int credsize = sizeof(cred);
+#endif
        MonoDomain *domain=mono_domain_get();
        MonoObject *obj;
        MonoClass *obj_class;
@@ -1454,6 +1478,13 @@ void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET soc
                           &tvsize);
                break;
 
+#ifdef SO_PEERCRED
+       case SocketOptionName_PeerCred: 
+               ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
+                                       &credsize);
+               break;
+#endif
+
        default:
                ret = _wapi_getsockopt (sock, system_level, system_name, &val,
                               &valsize);
@@ -1493,6 +1524,32 @@ void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET soc
                obj = int_to_object (domain, (tv.tv_sec * 1000) + (tv.tv_usec / 1000));
                break;
 
+#ifdef SO_PEERCRED
+       case SocketOptionName_PeerCred: 
+       {
+               /* build a Mono.Posix.PeerCred+PeerCredData if
+                * possible
+                */
+               MonoImage *mono_posix_image = mono_image_loaded ("Mono.Posix");
+               MonoPeerCredData *cred_data;
+               
+               if (mono_posix_image == NULL) {
+                       *error = WSAENOPROTOOPT;
+                       return;
+               }
+               
+               obj_class = mono_class_from_name(mono_posix_image,
+                                                "Mono.Posix",
+                                                "PeerCred/PeerCredData");
+               obj = mono_object_new(domain, obj_class);
+               cred_data = (MonoPeerCredData *)obj;
+               cred_data->pid = cred.pid;
+               cred_data->uid = cred.uid;
+               cred_data->gid = cred.gid;
+               break;
+       }
+#endif
+
        default:
                obj = int_to_object (domain, val);
        }
@@ -1827,7 +1884,7 @@ static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
                i++;
        }
        
-       *h_aliases=mono_array_new(domain, mono_defaults.string_class, i);
+       *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
        i=0;
        while(he->h_aliases[i]!=NULL) {
                MonoString *alias;
@@ -1842,7 +1899,7 @@ static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
                i++;
        }
        
-       *h_addr_list=mono_array_new(domain, mono_defaults.string_class, i);
+       *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
        i=0;
        while(he->h_addr_list[i]!=NULL) {
                MonoString *addr_string;
@@ -1899,7 +1956,7 @@ static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2,
                        i++;
                }
 
-               *h_aliases=mono_array_new (domain, mono_defaults.string_class,
+               *h_aliases=mono_array_new (domain, mono_get_string_class (),
                                           i);
                i=0;
                while(he1->h_aliases[i]!=NULL) {
@@ -1917,7 +1974,7 @@ static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2,
                        i++;
                }
 
-               *h_aliases=mono_array_new (domain, mono_defaults.string_class,
+               *h_aliases=mono_array_new (domain, mono_get_string_class (),
                                           i);
                i=0;
                while(he2->h_aliases[i]!=NULL) {
@@ -1954,7 +2011,7 @@ static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2,
        /*
         * Fills the array
         */
-       *h_addr_list=mono_array_new (domain, mono_defaults.string_class,
+       *h_addr_list=mono_array_new (domain, mono_get_string_class (),
                                     host_count);
 
        host_index = 0;
@@ -2018,8 +2075,8 @@ addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
                count++;
        }
 
-       *h_aliases=mono_array_new(domain, mono_defaults.string_class, 0);
-       *h_addr_list=mono_array_new(domain, mono_defaults.string_class, count);
+       *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
+       *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
 
        for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
                MonoString *addr_string;
@@ -2056,7 +2113,7 @@ addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
 
                mono_array_set(*h_addr_list, MonoString *, i, addr_string);
 
-               if(!i) {
+               if(!i && ai->ai_canonname != NULL) {
                        *h_name=mono_string_new(domain, ai->ai_canonname);
                }
 
@@ -2117,17 +2174,23 @@ MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, Mo
                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);
+       if (hp1 == NULL)
+       {
+               while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
+                                       buffer_size2, &hp2, &herr) == ERANGE) {
+                       buffer_size2 *= 2;
+                       buffer2 = g_realloc(buffer2, buffer_size2);
+               }
        }
+       else
+               hp2 = NULL;
 
        return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
                                               h_addr_list);
 
        g_free(buffer1);
        g_free(buffer2);
+       g_free(hostname);
 
        return(return_value);
 #endif /* HAVE_GETHOSTBYNAME2_R */
@@ -2287,7 +2350,7 @@ extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_
 
 
 /* Async interface */
-#if defined(PLATFORM_WIN32) || !defined(HAVE_AIO_H)
+#ifndef USE_AIO
 void
 ves_icall_System_Net_Sockets_Socket_AsyncReceive (MonoSocketAsyncResult *ares, gint *error)
 {
@@ -2369,7 +2432,7 @@ ves_icall_System_Net_Sockets_Socket_AsyncSend (MonoSocketAsyncResult *ares, gint
                wsa_overlapped_callback (0, byteswritten, ares);
        }
 }
-#endif
+#endif /* USE_AIO */
 
 void mono_network_init(void)
 {