Handle absence of socket constants
[mono.git] / mono / metadata / socket-io.c
index a1856b151d946bddba59d4e7a359fe5eabddc50f..bc0b4e95f1084896d262e3b4f0c7964f25a99577 100644 (file)
@@ -20,7 +20,7 @@
 #include <errno.h>
 
 #include <sys/types.h>
-#ifndef PLATFORM_WIN32 
+#ifndef HOST_WIN32 
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <netinet/in.h>
@@ -35,6 +35,7 @@
 #include <mono/metadata/exception.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/appdomain.h>
+#include <mono/metadata/file-io.h>
 #include <mono/metadata/threads.h>
 #include <mono/metadata/threads-types.h>
 #include <mono/utils/mono-poll.h>
@@ -68,7 +69,7 @@
 
 #include "mono/io-layer/socket-wrappers.h"
 
-#ifdef PLATFORM_WIN32
+#ifdef HOST_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
 #define LOGDEBUG(...)  
 /* define LOGDEBUG(...) g_message(__VA_ARGS__)  */
 
+/* 
+ * Some older versions of libc provide IPV6 support without defining the AI_ADDRCONFIG
+ * flag for getaddrinfo.
+ */
+#ifndef AI_ADDRCONFIG
+#define AI_ADDRCONFIG 0
+#endif
+
 static gint32 convert_family(MonoAddressFamily mono_family)
 {
        gint32 family=-1;
@@ -132,11 +141,15 @@ static gint32 convert_family(MonoAddressFamily mono_family)
                break;
                
        case AddressFamily_Sna:
+#ifdef AF_SNA
                family=AF_SNA;
+#endif
                break;
                
        case AddressFamily_DecNet:
+#ifdef AF_DECnet
                family=AF_DECnet;
+#endif
                break;
                
        case AddressFamily_AppleTalk:
@@ -183,13 +196,17 @@ static MonoAddressFamily convert_to_mono_family(guint16 af_family)
                break;
 #endif
                
+#ifdef AF_SNA
        case AF_SNA:
                family=AddressFamily_Sna;
                break;
+#endif
                
+#ifdef AF_DECnet
        case AF_DECnet:
                family=AddressFamily_DecNet;
                break;
+#endif
                
        case AF_APPLETALK:
                family=AddressFamily_AppleTalk;
@@ -231,7 +248,9 @@ static gint32 convert_type(MonoSocketType mono_type)
                break;
 
        case SocketType_Rdm:
+#ifdef SOCK_RDM
                type=SOCK_RDM;
+#endif
                break;
 
        case SocketType_Seqpacket:
@@ -842,9 +861,9 @@ gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
        MONO_ARCH_SAVE_REGS;
 
        *error = 0;
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
        {
-               MonoThread* curthread = mono_thread_current ();
+               MonoInternalThread* curthread = mono_thread_internal_current ();
                curthread->interrupt_on_stop = (gpointer)TRUE;
                newsock = _wapi_accept (sock, NULL, 0);
                curthread->interrupt_on_stop = (gpointer)FALSE;
@@ -1186,7 +1205,7 @@ MonoBoolean
 ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
                                                   gint timeout, gint32 *error)
 {
-       MonoThread *thread = NULL;
+       MonoInternalThread *thread = NULL;
        mono_pollfd *pfds;
        int ret;
        time_t start;
@@ -1222,7 +1241,7 @@ ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
                        int leave = 0;
 
                        if (thread == NULL) {
-                               thread = mono_thread_current ();
+                               thread = mono_thread_internal_current ();
                        }
                        
                        leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
@@ -1239,7 +1258,7 @@ ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
                *error = WSAGetLastError ();
 #else
                *error = errno_to_WSA (errno, __func__);
@@ -1389,9 +1408,9 @@ gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArr
                return (0);
        }
 
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
        {
-               MonoThread* curthread = mono_thread_current ();
+               MonoInternalThread* curthread = mono_thread_internal_current ();
                curthread->interrupt_on_stop = (gpointer)TRUE;
                ret = _wapi_recv (sock, buf, count, recvflags);
                curthread->interrupt_on_stop = (gpointer)FALSE;
@@ -1614,7 +1633,7 @@ static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
 #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
 void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
 {
-       MonoThread *thread = NULL;
+       MonoInternalThread *thread = NULL;
        MonoObject *obj;
        mono_pollfd *pfds;
        int nfds, idx;
@@ -1624,7 +1643,7 @@ void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gi
        MonoClass *sock_arr_class;
        MonoArray *socks;
        time_t start;
-       mono_array_size_t socks_size;
+       uintptr_t socks_size;
        
        MONO_ARCH_SAVE_REGS;
 
@@ -1670,7 +1689,7 @@ void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gi
                if (ret == -1 && errno == EINTR) {
                        int leave = 0;
                        if (thread == NULL)
-                               thread = mono_thread_current ();
+                               thread = mono_thread_internal_current ();
 
                        leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
                        
@@ -1687,7 +1706,7 @@ void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gi
        } while (ret == -1 && errno == EINTR);
        
        if (ret == -1) {
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
                *error = WSAGetLastError ();
 #else
                *error = errno_to_WSA (errno, __func__);
@@ -1703,7 +1722,7 @@ void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gi
        }
 
        sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
-       socks_size = ((mono_array_size_t)ret) + 3; /* space for the NULL delimiters */
+       socks_size = ((uintptr_t)ret) + 3; /* space for the NULL delimiters */
        socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
 
        mode = idx = 0;
@@ -2896,7 +2915,6 @@ inet_pton (int family, const char *address, void *inaddrp)
 extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
 {
        char *address;
-       gboolean v1;
        
 #ifdef AF_INET6
        struct sockaddr_in saddr;
@@ -2911,8 +2929,6 @@ extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *a
        gboolean ret;
 #endif
 
-       v1 = mono_framework_version () == 1;
-
        address = mono_string_to_utf8 (addr);
 
 #ifdef AF_INET6
@@ -2933,10 +2949,6 @@ extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *a
        }
        g_free(address);
 
-       if (v1) {
-               flags = NI_NAMEREQD;
-       }
-       
        if(family == AF_INET) {
 #if HAVE_SOCKADDR_IN_SIN_LEN
                saddr.sin_len = sizeof (saddr);
@@ -2974,12 +2986,7 @@ extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *a
        }
 
        if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
-               if (v1) {
-                       ret = FALSE;
-               } else {
-                       ret = ipaddr_to_IPHostEntry (address, h_name,
-                                                    h_aliases, h_addr_list);
-               }
+               ret = ipaddr_to_IPHostEntry (address, h_name, h_aliases, h_addr_list);
        } else {
                ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
                                              h_addr_list, FALSE);
@@ -3007,6 +3014,43 @@ extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_
        return(TRUE);
 }
 
+gboolean
+ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
+{
+       HANDLE file;
+       gint32 error;
+       TRANSMIT_FILE_BUFFERS buffers;
+
+       MONO_ARCH_SAVE_REGS;
+
+       if (filename == NULL)
+               return FALSE;
+
+       file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
+       if (file == INVALID_HANDLE_VALUE) {
+               SetLastError (error);
+               return FALSE;
+       }
+
+       memset (&buffers, 0, sizeof (buffers));
+       if (pre_buffer != NULL) {
+               buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
+               buffers.HeadLength = mono_array_length (pre_buffer);
+       }
+       if (post_buffer != NULL) {
+               buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
+               buffers.TailLength = mono_array_length (post_buffer);
+       }
+
+       if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
+               CloseHandle (file);
+               return FALSE;
+       }
+
+       CloseHandle (file);
+       return TRUE;
+}
+
 void mono_network_init(void)
 {
        WSADATA wsadata;