tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / network.c
index 968870defb42ecd9c33ad27ce171ecc5fdf4a7ce..90f8c8640ad5f70ff9f7a59090ea6a6649c9344c 100644 (file)
@@ -1,21 +1,21 @@
 /*
      This file is part of GNUnet.
-     (C) 2009-2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2013 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -28,9 +28,9 @@
 #include "gnunet_util_lib.h"
 #include "disk.h"
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
-#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
-#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-network", __VA_ARGS__)
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-network", syscall, filename)
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-network", syscall)
 
 #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
 
@@ -83,24 +83,64 @@ struct GNUNET_NETWORK_Handle
 int
 GNUNET_NETWORK_test_pf (int pf)
 {
+  static int cache_v4 = -1;
+  static int cache_v6 = -1;
+  static int cache_un = -1;
   int s;
+  int ret;
 
+  switch (pf)
+  {
+  case PF_INET:
+    if (-1 != cache_v4)
+      return cache_v4;
+    break;
+  case PF_INET6:
+    if (-1 != cache_v6)
+      return cache_v6;
+    break;
+#ifdef PF_UNIX
+  case PF_UNIX:
+    if (-1 != cache_un)
+      return cache_un;
+    break;
+#endif
+  }
   s = socket (pf, SOCK_STREAM, 0);
   if (-1 == s)
   {
-    if (EAFNOSUPPORT == errno)
-      return GNUNET_NO;
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-               "Failed to create test socket: %s\n",
-               STRERROR (errno));
-    return GNUNET_SYSERR;
+    if (EAFNOSUPPORT != errno)
+    {
+      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
+                          "socket");
+      return GNUNET_SYSERR;
+    }
+    ret = GNUNET_NO;
   }
+  else
+  {
 #if WINDOWS
-  closesocket (s);
+    closesocket (s);
 #else
-  close (s);
+    close (s);
 #endif
-  return GNUNET_OK;
+    ret = GNUNET_OK;
+  }
+  switch (pf)
+  {
+  case PF_INET:
+    cache_v4 = ret;
+    break;
+  case PF_INET6:
+    cache_v6 = ret;
+    break;
+#ifdef PF_UNIX
+  case PF_UNIX:
+    cache_un = ret;
+    break;
+#endif
+  }
+  return ret;
 }
 
 
@@ -128,27 +168,80 @@ GNUNET_NETWORK_shorten_unixpath (char *unixpath)
   if (slen < upm)
     return unixpath; /* no shortening required */
   GNUNET_CRYPTO_hash (unixpath, slen, &sh);
-  while (16 +
-        strlen (unixpath) >= upm)
+  while (16 + strlen (unixpath) >= upm)
   {
     if (NULL == (end = strrchr (unixpath, '/')))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 _("Unable to shorten unix path `%s' while keeping name unique\n"),
-                 unixpath);
+                  _("Unable to shorten unix path `%s' while keeping name unique\n"),
+                  unixpath);
       GNUNET_free (unixpath);
       return NULL;
     }
     *end = '\0';
   }
   GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
-  strncat (unixpath, (char*) ae.encoding, 16);
+  ae.encoding[16] = '\0';
+  strcat (unixpath, (char *) ae.encoding);
   return unixpath;
 }
 
 
+#ifndef WINDOWS
+/**
+ * If services crash, they can leave a unix domain socket file on the
+ * disk. This needs to be manually removed, because otherwise both
+ * bind() and connect() for the respective address will fail.  In this
+ * function, we test if such a left-over file exists, and if so,
+ * remove it (unless there is a listening service at the address).
+ *
+ * @param un unix domain socket address to check
+ */
+void
+GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
+{
+  int s;
+  int eno;
+  struct stat sbuf;
+  int ret;
+
+  s = socket (AF_UNIX, SOCK_STREAM, 0);
+  if (-1 == s)
+  {
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
+                         "Failed to open AF_UNIX socket");
+    return;
+  }
+  ret = connect (s,
+                 (struct sockaddr *) un,
+                 sizeof (struct sockaddr_un));
+  eno = errno;
+  GNUNET_break (0 == close (s));
+  if (0 == ret)
+    return; /* another process is listening, do not remove! */
+  if (ECONNREFUSED != eno)
+    return; /* some other error, likely "no such file or directory" -- all well */
+  /* should unlink, but sanity checks first */
+  if (0 != stat (un->sun_path,
+                 &sbuf))
+    return; /* failed to 'stat', likely does not exist after all */
+  if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
+    return; /* refuse to unlink anything except sockets */
+  /* finally, really unlink */
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Removing left-over `%s' from previous exeuction\n",
+              un->sun_path);
+  if (0 != unlink (un->sun_path))
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                              "unlink",
+                              un->sun_path);
+}
+#endif
+
+
+
 #ifndef FD_COPY
-#define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
+#define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof (fd_set)); } while (0)
 #endif
 
 
@@ -186,7 +279,6 @@ GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
   int flags = fcntl (fd->fd, F_GETFL);
 
   if (flags == -1)
-
   {
     LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
                   "fcntl");
@@ -258,7 +350,7 @@ socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
 
   if (0 !=
       setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
-                 (const void *) &abs_value,
+                  (const void *) &abs_value,
                   sizeof (abs_value)))
     LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
 }
@@ -289,8 +381,10 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
   const char *abs_value = "1";
 
   if (0 !=
-      setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY,
-                 (const void *) abs_value,
+      setsockopt (h->fd,
+                  IPPROTO_TCP,
+                  TCP_NODELAY,
+                  (const void *) abs_value,
                   sizeof (abs_value)))
     LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
                   "setsockopt");
@@ -312,9 +406,11 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
  */
 static int
 initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
-                          int af,
+                           int af,
                            int type)
 {
+  int eno;
+
   h->af = af;
   h->type = type;
   if (h->fd == INVALID_SOCKET)
@@ -322,7 +418,9 @@ initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
 #ifdef MINGW
     SetErrnoFromWinsockError (WSAGetLastError ());
 #endif
+    eno = errno;
     GNUNET_free (h);
+    errno = eno;
     return GNUNET_SYSERR;
   }
 #ifndef MINGW
@@ -339,8 +437,10 @@ initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
 
   if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
   {
+    eno = errno;
     GNUNET_break (0);
     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
+    errno = eno;
     return GNUNET_SYSERR;
   }
 #ifdef DARWIN
@@ -350,7 +450,7 @@ initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
 #ifdef AF_UNIX
        && (af != AF_UNIX)
 #endif
-       )
+      )
     socket_set_nodelay (h);
   return GNUNET_OK;
 }
@@ -367,9 +467,10 @@ initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
 struct GNUNET_NETWORK_Handle *
 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
                               struct sockaddr *address,
-                             socklen_t *address_len)
+                              socklen_t *address_len)
 {
   struct GNUNET_NETWORK_Handle *ret;
+  int eno;
 
   ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
 #if DEBUG_NETWORK
@@ -381,9 +482,9 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
                            (struct sockaddr *) &name,
                            &namelen);
 
-    if (gsn == 0)
+    if (0 == gsn)
       LOG (GNUNET_ERROR_TYPE_DEBUG,
-          "Accepting connection on `%s'\n",
+           "Accepting connection on `%s'\n",
            GNUNET_a2s ((const struct sockaddr *) &name,
                        namelen));
   }
@@ -393,14 +494,18 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
                     address_len);
   if (-1 == ret->fd)
   {
+    eno = errno;
     GNUNET_free (ret);
+    errno = eno;
     return NULL;
   }
   if (GNUNET_OK !=
       initialize_network_handle (ret,
                                  (NULL != address) ? address->sa_family : desc->af,
                                  SOCK_STREAM))
+  {
     return NULL;
+  }
   return ret;
 }
 
@@ -426,15 +531,19 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
     const int on = 1;
 
     if (AF_INET6 == desc->af)
-      if (setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY,
-                     (const void *) &on,
-                     sizeof (on)))
+      if (setsockopt (desc->fd,
+                      IPPROTO_IPV6,
+                      IPV6_V6ONLY,
+                      (const void *) &on,
+                      sizeof (on)))
         LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
                       "setsockopt");
   }
 #endif
 #endif
 #ifndef WINDOWS
+  if (AF_UNIX == address->sa_family)
+    GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
   {
     const int on = 1;
 
@@ -447,24 +556,24 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
       LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
                     "setsockopt");
   }
-#endif
-#ifndef WINDOWS
   {
     /* set permissions of newly created non-abstract UNIX domain socket to
        "user-only"; applications can choose to relax this later */
     mode_t old_mask = 0; /* assigned to make compiler happy */
-    const struct sockaddr_un *un;
+    const struct sockaddr_un *un = (const struct sockaddr_un *) address;
     int not_abstract = 0;
 
     if ((AF_UNIX == address->sa_family)
-        && (NULL != (un = (const struct sockaddr_un *) address)->sun_path)
         && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */
       not_abstract = 1;
     if (not_abstract)
       old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
 #endif
 
-    ret = bind (desc->fd, address, address_len);
+    ret = bind (desc->fd,
+                address,
+                address_len);
+
 #ifndef WINDOWS
     if (not_abstract)
       (void) umask (old_mask);
@@ -474,11 +583,11 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
   if (SOCKET_ERROR == ret)
     SetErrnoFromWinsockError (WSAGetLastError ());
 #endif
-  if (ret != 0)
+  if (0 != ret)
     return GNUNET_SYSERR;
 #ifndef MINGW
   desc->addr = GNUNET_malloc (address_len);
-  memcpy (desc->addr, address, address_len);
+  GNUNET_memcpy (desc->addr, address, address_len);
   desc->addrlen = address_len;
 #endif
   return GNUNET_OK;
@@ -512,13 +621,12 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
   ret = close (desc->fd);
 #endif
 #ifndef WINDOWS
-  const struct sockaddr_un *un;
+  const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
 
   /* Cleanup the UNIX domain socket and its parent directories in case of non
      abstract sockets */
   if ( (AF_UNIX == desc->af) &&
        (NULL != desc->addr) &&
-       (NULL != (un = (const struct sockaddr_un *) desc->addr)->sun_path) &&
        ('\0' != un->sun_path[0]) )
   {
     char *dirname = GNUNET_strndup (un->sun_path,
@@ -527,8 +635,8 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
     if (0 != unlink (dirname))
     {
       LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
-                        "unlink",
-                        dirname);
+                         "unlink",
+                         dirname);
     }
     else
     {
@@ -704,7 +812,7 @@ GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
  * How much data is available to be read on this descriptor?
  *
  * @param desc socket
- * @returns #GNUNET_NO if no data is available, or on error!
+ * @returns #GNUNET_SYSERR if no data is available, or on error!
  */
 ssize_t
 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
@@ -718,9 +826,9 @@ GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
   error = ioctl (desc->fd,
                  FIONREAD,
                  &pending);
-  if (error == 0)
+  if (0 == error)
     return (ssize_t) pending;
-  return GNUNET_NO;
+  return GNUNET_SYSERR;
 #else
   u_long pending;
 
@@ -729,7 +837,7 @@ GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
                        &pending);
   if (error != SOCKET_ERROR)
     return (ssize_t) pending;
-  return GNUNET_NO;
+  return GNUNET_SYSERR;
 #endif
 }
 
@@ -931,14 +1039,13 @@ GNUNET_NETWORK_socket_create (int domain,
                               int protocol)
 {
   struct GNUNET_NETWORK_Handle *ret;
+  int fd;
 
-  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
-  ret->fd = socket (domain, type, protocol);
-  if (-1 == ret->fd)
-  {
-    GNUNET_free (ret);
+  fd = socket (domain, type, protocol);
+  if (-1 == fd)
     return NULL;
-  }
+  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
+  ret->fd = fd;
   if (GNUNET_OK !=
       initialize_network_handle (ret,
                                  domain,
@@ -962,10 +1069,10 @@ GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
 
   ret = shutdown (desc->fd, how);
 #ifdef MINGW
-  if (ret != 0)
+  if (0 != ret)
     SetErrnoFromWinsockError (WSAGetLastError ());
 #endif
-  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
+  return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
 }
 
 
@@ -1143,9 +1250,9 @@ GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
     GNUNET_array_grow (to->handles,
                        to->handles_size,
                        from->handles_pos * 2);
-  memcpy (to->handles,
-          from->handles,
-          from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
+  GNUNET_memcpy (to->handles,
+                 from->handles,
+                 from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
   to->handles_pos = from->handles_pos;
 #endif
 }
@@ -1158,7 +1265,7 @@ GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
  * @return POSIX file descriptor
  */
 int
-GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
+GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
 {
   return desc->fd;
 }
@@ -1171,7 +1278,7 @@ GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
  * @return sockaddr
  */
 struct sockaddr*
-GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc)
+GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
 {
   return desc->addr;
 }
@@ -1184,7 +1291,7 @@ GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc)
  * @return socklen_t for sockaddr
  */
 socklen_t
-GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc)
+GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
 {
   return desc->addrlen;
 }
@@ -1261,9 +1368,10 @@ GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
 #else
   int fd;
 
-  GNUNET_DISK_internal_file_handle_ (h,
-                                     &fd,
-                                     sizeof (int));
+  GNUNET_assert (GNUNET_OK ==
+                 GNUNET_DISK_internal_file_handle_ (h,
+                                                    &fd,
+                                                    sizeof (int)));
   FD_SET (fd,
           &fds->sds);
   fds->nsds = GNUNET_MAX (fd + 1,
@@ -1632,6 +1740,63 @@ initialize_select_thread ()
 
 #endif
 
+/**
+ * Test if the given @a port is available.
+ *
+ * @param ipproto transport protocol to test (i.e. IPPROTO_TCP)
+ * @param port port number to test
+ * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
+ */
+int
+GNUNET_NETWORK_test_port_free (int ipproto,
+                               uint16_t port)
+{
+  struct GNUNET_NETWORK_Handle *socket;
+  int bind_status;
+  int socktype;
+  char open_port_str[6];
+  struct addrinfo hint;
+  struct addrinfo *ret;
+  struct addrinfo *ai;
+
+  GNUNET_snprintf (open_port_str,
+                   sizeof (open_port_str),
+                   "%u",
+                   (unsigned int) port);
+  socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
+  ret = NULL;
+  memset (&hint, 0, sizeof (hint));
+  hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
+  hint.ai_socktype = socktype;
+  hint.ai_protocol = ipproto;
+  hint.ai_addrlen = 0;
+  hint.ai_addr = NULL;
+  hint.ai_canonname = NULL;
+  hint.ai_next = NULL;
+  hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
+  GNUNET_assert (0 == getaddrinfo (NULL,
+                                   open_port_str,
+                                   &hint,
+                                   &ret));
+  bind_status = GNUNET_NO;
+  for (ai = ret; NULL != ai; ai = ai->ai_next)
+  {
+    socket = GNUNET_NETWORK_socket_create (ai->ai_family,
+                                           ai->ai_socktype,
+                                           ai->ai_protocol);
+    if (NULL == socket)
+      continue;
+    bind_status = GNUNET_NETWORK_socket_bind (socket,
+                                              ai->ai_addr,
+                                              ai->ai_addrlen);
+    GNUNET_NETWORK_socket_close (socket);
+    if (GNUNET_OK != bind_status)
+      break;
+  }
+  freeaddrinfo (ret);
+  return bind_status;
+}
+
 
 #ifndef MINGW
 /**
@@ -1670,12 +1835,20 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
          _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
          "select");
   }
-  tv.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
-  tv.tv_usec =
-    (timeout.rel_value_us -
-     (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
+  if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned long long) LONG_MAX)
+  {
+    tv.tv_sec = LONG_MAX;
+    tv.tv_usec = 999999L;
+  }
+  else
+  {
+    tv.tv_sec = (long) (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
+    tv.tv_usec =
+      (timeout.rel_value_us -
+       (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
+  }
   return select (nfds,
-                (NULL != rfds) ? &rfds->sds : NULL,
+                 (NULL != rfds) ? &rfds->sds : NULL,
                  (NULL != wfds) ? &wfds->sds : NULL,
                  (NULL != efds) ? &efds->sds : NULL,
                  (timeout.rel_value_us ==
@@ -1694,7 +1867,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
  * @return #GNUNET_YES if the pipe is ready for reading
  */
 static int
-pipe_read_ready (struct GNUNET_DISK_FileHandle *fh)
+pipe_read_ready (const struct GNUNET_DISK_FileHandle *fh)
 {
   DWORD error;
   BOOL bret;
@@ -1726,7 +1899,7 @@ pipe_read_ready (struct GNUNET_DISK_FileHandle *fh)
  * @return #GNUNET_YES if the pipe is having an IO exception.
  */
 static int
-pipe_except_ready (struct GNUNET_DISK_FileHandle *fh)
+pipe_except_ready (const struct GNUNET_DISK_FileHandle *fh)
 {
   DWORD dwBytes;
 
@@ -1754,7 +1927,7 @@ check_handles_status (struct GNUNET_NETWORK_FDSet *fds,
                       int except,
                       HANDLE set_for_sure)
 {
-  struct GNUNET_DISK_FileHandle *fh;
+  const struct GNUNET_DISK_FileHandle *fh;
   unsigned int roff;
   unsigned int woff;
 
@@ -1778,7 +1951,7 @@ check_handles_status (struct GNUNET_NETWORK_FDSet *fds,
     }
     else
     {
-      if (WAIT_OBJECT_0 == WaitForSingleObject (fh, 0))
+      if (WAIT_OBJECT_0 == WaitForSingleObject (fh->h, 0))
         fds->handles[woff++] = fh;
     }
   }
@@ -1802,7 +1975,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
                               struct GNUNET_NETWORK_FDSet *efds,
                               const struct GNUNET_TIME_Relative timeout)
 {
-  struct GNUNET_DISK_FileHandle *fh;
+  const struct GNUNET_DISK_FileHandle *fh;
   int nfds;
   int handles;
   unsigned int i;
@@ -1912,7 +2085,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
          wfds ? awrite.fd_count : 0,
          aexcept.fd_count,
          GetLastError ());
-    GNUNET_abort ();
+    GNUNET_assert (0);
   }
 
   /* Check aexcept, if something is in there and we copied that
@@ -2045,7 +2218,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Adding the socket event to the array as %d\n",
-        nhandles);
+         nhandles);
     handle_array[nhandles++] = select_finished_event;
     if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
     {