[runtime] Use a guint64 for device/inode instead of dev_t/ino_t since ino_t is define...
[mono.git] / mono / io-layer / sockets.c
index 817b8478105edd703c9c2663eec19c47c58b1c53..60429573c07f8f6e66f4b37aa6d0a80aa8428b74 100644 (file)
 
 #include <netinet/in.h>
 #include <netinet/tcp.h>
-#include <netdb.h>
 #include <arpa/inet.h>
 #ifdef HAVE_SYS_SENDFILE_H
 #include <sys/sendfile.h>
 #endif
+#ifdef HAVE_NETDB_H
+#include <netdb.h>
+#endif
 
 #if 0
 #define DEBUG(...) g_message(__VA_ARGS__)
@@ -335,7 +337,8 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                                                          (gpointer *)&socket_handle);
                                if (ok == FALSE) {
                                        /* ECONNRESET means the socket was closed by another thread */
-                                       if (errnum != WSAECONNRESET)
+                                       /* Async close on mac raises ECONNABORTED. */
+                                       if (errnum != WSAECONNRESET && errnum != WSAENETDOWN)
                                                g_warning ("%s: error looking up socket handle %p (error %d)", __func__, handle, errnum);
                                } else {
                                        socket_handle->saved_error = errnum;
@@ -345,7 +348,7 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                }
 
                fds.fd = fd;
-               fds.events = POLLOUT;
+               fds.events = MONO_POLLOUT;
                while (mono_poll (&fds, 1, -1) == -1 &&
                       !_wapi_thread_cur_apc_pending ()) {
                        if (errno != EINTR) {
@@ -777,6 +780,10 @@ int _wapi_setsockopt(guint32 fd, int level, int optname,
        gpointer handle = GUINT_TO_POINTER (fd);
        int ret;
        const void *tmp_val;
+#if defined (__linux__)
+       /* This has its address taken so it cannot be moved to the if block which uses it */
+       int bufsize = 0;
+#endif
        struct timeval tv;
        
        if (startup_count == 0) {
@@ -804,7 +811,7 @@ int _wapi_setsockopt(guint32 fd, int level, int optname,
                 * buffer sizes "to allow space for bookkeeping
                 * overhead."
                 */
-               int bufsize = *((int *) optval);
+               bufsize = *((int *) optval);
 
                bufsize /= 2;
                tmp_val = &bufsize;
@@ -901,6 +908,7 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
        if (fd == -1 && domain == AF_INET && type == SOCK_RAW &&
            protocol == 0) {
                /* Retry with protocol == 4 (see bug #54565) */
+               // https://bugzilla.novell.com/show_bug.cgi?id=MONO54565
                socket_handle.protocol = 4;
                fd = socket (AF_INET, SOCK_RAW, 4);
        }
@@ -926,6 +934,7 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
 
        /* .net seems to set this by default for SOCK_STREAM, not for
         * SOCK_DGRAM (see bug #36322)
+        * https://bugzilla.novell.com/show_bug.cgi?id=MONO36322
         *
         * It seems winsock has a rather different idea of what
         * SO_REUSEADDR means.  If it's set, then a new socket can be
@@ -936,6 +945,7 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
         * behaves as though any other system would when SO_REUSEADDR
         * is true, so we don't need to do anything else here.  See
         * bug 53992.
+        * https://bugzilla.novell.com/show_bug.cgi?id=MONO53992
         */
        {
                int ret, true = 1;
@@ -972,45 +982,6 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
        return(fd);
 }
 
-struct hostent *_wapi_gethostbyname(const char *hostname)
-{
-       struct hostent *he;
-       
-       if (startup_count == 0) {
-               WSASetLastError (WSANOTINITIALISED);
-               return(NULL);
-       }
-
-       he = gethostbyname (hostname);
-       if (he == NULL) {
-               DEBUG ("%s: gethostbyname error: %s", __func__,
-                          strerror (h_errno));
-
-               switch(h_errno) {
-               case HOST_NOT_FOUND:
-                       WSASetLastError (WSAHOST_NOT_FOUND);
-                       break;
-#if NO_ADDRESS != NO_DATA
-               case NO_ADDRESS:
-#endif
-               case NO_DATA:
-                       WSASetLastError (WSANO_DATA);
-                       break;
-               case NO_RECOVERY:
-                       WSASetLastError (WSANO_RECOVERY);
-                       break;
-               case TRY_AGAIN:
-                       WSASetLastError (WSATRY_AGAIN);
-                       break;
-               default:
-                       g_warning ("%s: Need to translate %d into winsock error", __func__, h_errno);
-                       break;
-               }
-       }
-       
-       return(he);
-}
-
 static gboolean socket_disconnect (guint32 fd)
 {
        struct _WapiHandle_socket *socket_handle;
@@ -1290,13 +1261,13 @@ WSAIoctl (guint32 fd, gint32 command,
                        keepalivetime /= 1000;
                        if (keepalivetime == 0 || rem >= 500)
                                keepalivetime++;
-                       ret = setsockopt (fd, SOL_TCP, TCP_KEEPIDLE, &keepalivetime, sizeof (uint32_t));
+                       ret = setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &keepalivetime, sizeof (uint32_t));
                        if (ret == 0) {
                                rem = keepaliveinterval % 1000;
                                keepaliveinterval /= 1000;
                                if (keepaliveinterval == 0 || rem >= 500)
                                        keepaliveinterval++;
-                               ret = setsockopt (fd, SOL_TCP, TCP_KEEPINTVL, &keepaliveinterval, sizeof (uint32_t));
+                               ret = setsockopt (fd, IPPROTO_TCP, TCP_KEEPINTVL, &keepaliveinterval, sizeof (uint32_t));
                        }
                        if (ret != 0) {
                                gint errnum = errno;