Revert "[w32handle] Remove use of w32handle for File, Console, Pipe and Socket (...
[mono.git] / mono / metadata / w32socket-unix.c
index 4b3d863e7462966aab904acaa4b56d3c38e2b0e1..5816b142b051e09e6c784e93518bbd9241dbd643 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * w32socket-unix.c: Unix specific socket code.
+/**
+ * \file
+ * Unix specific socket code.
  *
  * Copyright 2016 Microsoft
  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
@@ -11,6 +12,7 @@
 #include <pthread.h>
 #include <string.h>
 #include <stdlib.h>
+#include <sys/types.h>
 #include <sys/socket.h>
 #ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
@@ -26,7 +28,6 @@
 #endif
 #include <errno.h>
 #include <fcntl.h>
-#include <sys/types.h>
 #ifdef HAVE_SYS_UIO_H
 #include <sys/uio.h>
 #endif
 #ifdef HAVE_SYS_SENDFILE_H
 #include <sys/sendfile.h>
 #endif
+#include <sys/stat.h>
 
 #include "w32socket.h"
 #include "w32socket-internals.h"
+#include "w32error.h"
 #include "w32handle.h"
 #include "utils/mono-logger-internals.h"
 #include "utils/mono-poll.h"
@@ -606,7 +609,7 @@ mono_w32socket_transmit_file (SOCKET sock, gpointer file_handle, TRANSMIT_FILE_B
        }
 
        if ((flags & TF_DISCONNECT) == TF_DISCONNECT)
-               CloseHandle (handle);
+               mono_w32handle_close (handle);
 
        return TRUE;
 }
@@ -1123,9 +1126,16 @@ mono_w32socket_ioctl (SOCKET sock, gint32 command, gchar *input, gint inputlen,
        return 0;
 }
 
+gboolean
+mono_w32socket_close (SOCKET sock)
+{
+       return mono_w32handle_close (GINT_TO_POINTER (sock));
+}
+
 gint
 mono_w32socket_set_blocking (SOCKET socket, gboolean blocking)
 {
+#ifdef O_NONBLOCK
        gint ret;
        gpointer handle;
 
@@ -1135,14 +1145,12 @@ mono_w32socket_set_blocking (SOCKET socket, gboolean blocking)
                return SOCKET_ERROR;
        }
 
-#ifdef O_NONBLOCK
        /* This works better than ioctl(...FIONBIO...)
         * on Linux (it causes connect to return
         * EINPROGRESS, but the ioctl doesn't seem to) */
        ret = fcntl (socket, F_GETFL, 0);
        if (ret != -1)
                ret = fcntl (socket, F_SETFL, blocking ? (ret & (~O_NONBLOCK)) : (ret | (O_NONBLOCK)));
-#endif /* O_NONBLOCK */
 
        if (ret == -1) {
                gint errnum = errno;
@@ -1152,6 +1160,10 @@ mono_w32socket_set_blocking (SOCKET socket, gboolean blocking)
        }
 
        return 0;
+#else
+       mono_w32socket_set_last_error (ERROR_NOT_SUPPORTED);
+       return SOCKET_ERROR;
+#endif /* O_NONBLOCK */
 }
 
 gint
@@ -1196,13 +1208,13 @@ mono_w32socket_get_available (SOCKET socket, guint64 *amount)
 void
 mono_w32socket_set_last_error (gint32 error)
 {
-       SetLastError (error);
+       mono_w32error_set_last (error);
 }
 
 gint32
 mono_w32socket_get_last_error (void)
 {
-       return GetLastError ();
+       return mono_w32error_get_last ();
 }
 
 gint32
@@ -1310,14 +1322,21 @@ mono_w32socket_convert_error (gint error)
        case ENETDOWN: return WSAENETDOWN;
 #endif
        case ENODEV: return WSAENETDOWN;
+#ifdef EPROTOTYPE
+       case EPROTOTYPE: return WSAEPROTOTYPE;
+#endif
+#ifdef ENXIO
+       case ENXIO: return WSAENXIO;
+#endif
        default:
                g_error ("%s: no translation into winsock error for (%d) \"%s\"", __func__, error, g_strerror (error));
        }
 }
 
 gboolean
-ves_icall_System_Net_Sockets_Socket_SupportPortReuse (MonoProtocolType proto)
+ves_icall_System_Net_Sockets_Socket_SupportPortReuse (MonoProtocolType proto, MonoError *error)
 {
+       error_init (error);
 #if defined (SO_REUSEPORT)
        return TRUE;
 #else