-eliminating duplicate code
[oweals/gnunet.git] / src / util / network.c
index 180adc16412aa701d55de0228cb8d9327f79c9fb..9371ae6f6b58678c6e980c0b7c09325ed0ff1399 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009 Christian Grothoff (and other contributing authors)
+     (C) 2009, 2012 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -35,7 +35,6 @@
 
 #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
 
-#define DEBUG_W32_CYCLES GNUNET_EXTRA_LOGGING
 
 #ifndef INVALID_SOCKET
 #define INVALID_SOCKET -1
@@ -46,7 +45,6 @@ struct GNUNET_NETWORK_Handle
 {
 #ifndef MINGW
   int fd;
-
 #else
   SOCKET fd;
 #endif
@@ -194,6 +192,61 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
 }
 
 
+/**
+ * Perform proper canonical initialization for a network handle.
+ * Set it to non-blocking, make it non-inheritable to child
+ * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
+ * stream socket) and check that it is smaller than FS_SETSIZE.
+ *
+ * @param h socket to initialize
+ * @param af address family of the socket
+ * @param type socket type
+ * @return GNUNET_OK on success, GNUNET_SYSERR if initialization
+ *         failed and the handle was destroyed
+ */
+static int
+initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
+                          int af, int type)
+{
+  h->af = af;
+  if (h->fd == INVALID_SOCKET)
+  {
+#ifdef MINGW
+    SetErrnoFromWinsockError (WSAGetLastError ());
+#endif
+    GNUNET_free (h);
+    return GNUNET_SYSERR;
+  }
+#ifndef MINGW
+  if (h->fd >= FD_SETSIZE)
+  {
+    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
+    errno = EMFILE;
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_OK != socket_set_inheritable (h))
+    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
+                  "socket_set_inheritable");
+#endif
+  if (GNUNET_SYSERR == socket_set_blocking (h, GNUNET_NO))
+  {
+    GNUNET_break (0);
+    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
+    return GNUNET_SYSERR;
+  }
+#ifdef DARWIN
+  socket_set_nosigpipe (h);
+#endif
+  if ( (type == SOCK_STREAM) 
+#ifdef AF_UNIX
+       && (af != AF_UNIX)
+#endif
+       )
+    socket_set_nodelay (h);
+  return GNUNET_OK;
+}
+
+
 /**
  * accept a new connection on a socket
  *
@@ -221,49 +274,10 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
   }
 #endif
   ret->fd = accept (desc->fd, address, address_len);
-  if (address != NULL)
-    ret->af = address->sa_family;
-  else
-    ret->af = desc->af;
-  if (ret->fd == INVALID_SOCKET)
-  {
-#ifdef MINGW
-    SetErrnoFromWinsockError (WSAGetLastError ());
-#endif
-    GNUNET_free (ret);
-    return NULL;
-  }
-#ifndef MINGW
-  if (ret->fd >= FD_SETSIZE)
-  {
-    GNUNET_break (0 == close (ret->fd));
-    GNUNET_free (ret);
-    errno = EMFILE;
-    return NULL;
-  }
-#endif
-  if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
-
-  {
-
-    /* we might want to treat this one as fatal... */
-    GNUNET_break (0);
-    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
+  if (GNUNET_OK != initialize_network_handle (ret,
+                                             (NULL != address) ? address->sa_family : desc->af,
+                                             SOCK_STREAM))
     return NULL;
-  }
-
-#ifndef MINGW
-  if (GNUNET_OK != socket_set_inheritable (ret))
-    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                  "socket_set_inheritable");
-#endif
-#ifdef DARWIN
-  socket_set_nosigpipe (ret);
-#endif
-#ifdef AF_UNIX
-  if (ret->af != AF_UNIX)
-#endif
-    socket_set_nodelay (ret);
   return ret;
 }
 
@@ -337,19 +351,13 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
 #ifdef MINGW
   DWORD error = 0;
 
-#if DEBUG_NETWORK
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", "Closing 0x%x\n",
-       desc->fd);
-#endif
   SetLastError (0);
   ret = closesocket (desc->fd);
   error = WSAGetLastError ();
   SetErrnoFromWinsockError (error);
-#if DEBUG_NETWORK
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Closed 0x%x, closesocket() returned %d, GLE is %u\n", desc->fd, ret,
        error);
-#endif
 #else
   ret = close (desc->fd);
 #endif
@@ -377,13 +385,20 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
  * @return NULL on error (including not supported on target platform)
  */
 struct GNUNET_NETWORK_Handle *
-GNUNET_NETWORK_socket_box_native (int fd)
+GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
 {
+  struct GNUNET_NETWORK_Handle *ret;
 #if MINGW
-  return NULL;
+  unsigned long i;
+  DWORD d;
+  /* FIXME: Find a better call to check that FD is valid */
+  if (WSAIoctl (fd, FIONBIO, (void *) &i, sizeof (i), NULL, 0, &d, NULL, NULL) != 0)
+    return NULL;                /* invalid FD */
+  ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
+  ret->fd = fd;
+  ret->af = AF_UNSPEC;
+  return ret;
 #else
-  struct GNUNET_NETWORK_Handle *ret;
-
   if (fcntl (fd, F_GETFD) < 0)
     return NULL;                /* invalid FD */
   ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
@@ -678,49 +693,10 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
   struct GNUNET_NETWORK_Handle *ret;
 
   ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
-  ret->af = domain;
   ret->fd = socket (domain, type, protocol);
-  if (INVALID_SOCKET == ret->fd)
-  {
-#ifdef MINGW
-    SetErrnoFromWinsockError (WSAGetLastError ());
-#endif
-    GNUNET_free (ret);
-    return NULL;
-  }
-
-#ifndef MINGW
-  if (ret->fd >= FD_SETSIZE)
-  {
-    GNUNET_break (0 == close (ret->fd));
-    GNUNET_free (ret);
-    errno = EMFILE;
+  if (GNUNET_OK !=
+      initialize_network_handle (ret, domain, type))
     return NULL;
-  }
-
-#endif
-  if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
-  {
-    /* we might want to treat this one as fatal... */
-    GNUNET_break (0);
-    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
-    return NULL;
-  }
-
-#ifndef MINGW
-  if (GNUNET_OK != socket_set_inheritable (ret))
-    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                  "socket_set_inheritable");
-#endif
-#ifdef DARWIN
-  socket_set_nosigpipe (ret);
-#endif
-  if ((type == SOCK_STREAM)
-#ifdef AF_UNIX
-      && (domain != AF_UNIX)
-#endif
-      )
-    socket_set_nodelay (ret);
   return ret;
 }
 
@@ -1294,37 +1270,32 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
       if (fh->type == GNUNET_PIPE)
       {
         /* Read zero bytes to check the status of the pipe */
-#if DEBUG_NETWORK
         LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading 0 bytes from the pipe 0x%x\n",
              fh->h);
-#endif
         if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
         {
           DWORD error_code = GetLastError ();
 
           if (error_code == ERROR_IO_PENDING)
           {
-#if DEBUG_NETWORK
             LOG (GNUNET_ERROR_TYPE_DEBUG,
                  "Adding the pipe's 0x%x overlapped event to the array as %d\n",
                  fh->h, nhandles);
-#endif
             handle_array[nhandles++] = fh->oOverlapRead->hEvent;
             readArray[readPipes++] = fh;
           }
-          /*
-           * else
-           * {
-           * SetErrnoFromWinError (error_code);
-           * }
-           */
+          else
+          {
+            LOG (GNUNET_ERROR_TYPE_DEBUG,
+                 "Read failed, adding the read ready event to the array as %d\n", nhandles);
+            handle_array[nhandles++] = hEventReadReady;
+            readArray[readPipes++] = fh;
+          }
         }
         else
         {
-#if DEBUG_NETWORK
           LOG (GNUNET_ERROR_TYPE_DEBUG,
                "Adding the read ready event to the array as %d\n", nhandles);
-#endif
           handle_array[nhandles++] = hEventReadReady;
           readArray[readPipes++] = fh;
         }
@@ -1339,10 +1310,8 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
   }
   if (wfds && write_handles)
   {
-#if DEBUG_NETWORK
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Adding the write ready event to the array as %d\n", nhandles);
-#endif
     handle_array[nhandles++] = hEventPipeWrite;
     writePipePos = nhandles;
   }
@@ -1376,10 +1345,8 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
   {
     if (rfds)
     {
-#if DEBUG_NETWORK
       LOG (GNUNET_ERROR_TYPE_DEBUG,
            "Adding the socket read event to the array as %d\n", nhandles);
-#endif
       handle_array[nhandles++] = hEventRead;
       nSockEvents++;
       for (i = 0; i < rfds->sds.fd_count; i++)
@@ -1393,10 +1360,8 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
     {
       int wakeup = 0;
 
-#if DEBUG_NETWORK
       LOG (GNUNET_ERROR_TYPE_DEBUG,
            "Adding the socket write event to the array as %d\n", nhandles);
-#endif
       handle_array[nhandles++] = hEventWrite;
       nSockEvents++;
       for (i = 0; i < wfds->sds.fd_count; i++)
@@ -1406,10 +1371,8 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
 
         status = send (wfds->sds.fd_array[i], NULL, 0, 0);
         error = GetLastError ();
-#if DEBUG_NETWORK
         LOG (GNUNET_ERROR_TYPE_DEBUG,
              "pre-send to the socket %d returned %d (%u)\n", i, status, error);
-#endif
         if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
           wakeup = 1;
         WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
@@ -1421,10 +1384,8 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
     }
     if (efds)
     {
-#if DEBUG_NETWORK
       LOG (GNUNET_ERROR_TYPE_DEBUG,
            "Adding the socket error event to the array as %d\n", nhandles);
-#endif
       handle_array[nhandles++] = hEventException;
       nSockEvents++;
       for (i = 0; i < efds->sds.fd_count; i++)
@@ -1454,9 +1415,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
 #endif
 
   returnedpos = returncode - WAIT_OBJECT_0;
-#if DEBUG_NETWORK
   LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
-#endif
 
   /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
   /*
@@ -1477,22 +1436,16 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
       retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
       if (retcode == -1)
         retcode = 0;
-#if DEBUG_NETWORK
       LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
-#endif
     }
     /* FIXME: <= writePipePos? Really? */
     if ((writePipePos != -1) && (returnedpos <= writePipePos))
     {
       GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
       retcode += write_handles;
-#if DEBUG_NETWORK
       LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
-#endif
     }
-#if DEBUG_NETWORK
     LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
-#endif
     /* We have some pipes ready for read. */
     /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
 
@@ -1522,11 +1475,9 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
         bret =
             PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
         error = GetLastError ();
-#if DEBUG_NETWORK
         LOG (GNUNET_ERROR_TYPE_DEBUG,
              "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
              i, readArray[i]->h, bret, waitstatus, error);
-#endif
         if (bret == 0)
         {
           if (error != ERROR_BROKEN_PIPE)
@@ -1539,17 +1490,13 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
                                     readArray[i],
                                     sizeof (struct GNUNET_DISK_FileHandle));
         retcode++;
-#if DEBUG_NETWORK
         LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
              readArray[i], readArray[i]->h);
-#endif
       }
     }
     waitstatus = WaitForSingleObject (hEventWrite, 0);
-#if DEBUG_NETWORK
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Wait for the write event returned %d\n",
          waitstatus);
-#endif
     if (waitstatus == WAIT_OBJECT_0)
     {
       for (i = 0; i < wfds->sds.fd_count; i++)
@@ -1564,10 +1511,8 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
 
         status = send (wfds->sds.fd_array[i], NULL, 0, 0);
         error = GetLastError ();
-#if DEBUG_NETWORK
         LOG (GNUNET_ERROR_TYPE_DEBUG,
              "send to the socket %d returned %d (%u)\n", i, status, error);
-#endif
         if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) ||
             (status == -1 && gso_result == 0 && error == WSAENOTCONN &&
              so_error == WSAECONNREFUSED))
@@ -1604,9 +1549,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
         CancelIo (fh->h);
       }
     }
-#if DEBUG_NETWORK
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
-#endif
     GNUNET_NETWORK_fdset_zero (rfds);
     if (retcode != -1 && nhandles && (returnedpos < nhandles))
       GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
@@ -1619,9 +1562,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
       WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
       nsock++;
     }
-#if DEBUG_NETWORK
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
-#endif
     GNUNET_NETWORK_fdset_zero (wfds);
     if (retcode != -1 && nhandles && (returnedpos < nhandles))
       GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
@@ -1634,9 +1575,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
       WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
       nsock++;
     }
-#if DEBUG_NETWORK
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
-#endif
     GNUNET_NETWORK_fdset_zero (efds);
     if (retcode != -1 && nhandles && (returnedpos < nhandles))
       GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);