merging configs
[oweals/gnunet.git] / src / util / network.c
index 491e4fffd390443577c014a6bdb604745f9a694f..69db7cd25af973c5792fddf7ce8b6c52121a50df 100644 (file)
@@ -66,6 +66,34 @@ struct GNUNET_NETWORK_Handle
 };
 
 
+/**
+ * Test if the given protocol family is supported by this system.
+ *
+ * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
+ * @return GNUNET_OK if the PF is supported
+ */
+int
+GNUNET_NETWORK_test_pf (int pf)
+{
+  int s;
+
+  s = socket (pf, SOCK_STREAM, 0);
+  if (-1 == s)
+  {
+    if (EAFNOSUPPORT == errno)
+      return GNUNET_NO;
+    fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
+    return GNUNET_SYSERR;
+  }
+#if WINDOWS
+  closesocket (s);
+#else
+  close (s);
+#endif
+  return GNUNET_OK;
+}
+
+
 /**
  * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
  * shorten it to an acceptable length while keeping it unique
@@ -868,6 +896,7 @@ void
 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
                           const struct GNUNET_NETWORK_FDSet *src)
 {
+#ifndef MINGW
   int nfds;
 
   for (nfds = src->nsds; nfds > 0; nfds--)
@@ -878,7 +907,18 @@ GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
       if (nfds + 1 > dst->nsds)
         dst->nsds = nfds + 1;
     }
-#ifdef MINGW
+#else
+  /* This is MinGW32-specific implementation that relies on the code that
+   * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
+   * that fd being added is not already in the set.
+   * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
+   */
+  int i;
+  for (i = 0; i < src->sds.fd_count; i++)
+    FD_SET (src->sds.fd_array[i], &dst->sds);
+  if (src->nsds > dst->nsds)
+    dst->nsds = src->nsds;
+
   GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
 #endif
 }
@@ -1410,10 +1450,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
 
   /* Copy all the writes to the except, so we can detect connect() errors */
   for (i = 0; i < awrite.fd_count; i++)
-  {
-      if (awrite.fd_array[i] != 0 && awrite.fd_array[i] != -1)
-          FD_SET (awrite.fd_array[i], &aexcept);
-  }
+    FD_SET (awrite.fd_array[i], &aexcept);
   if (aread.fd_count > 0 || awrite.fd_count > 0 || aexcept.fd_count > 0)
     selectret = select (1, (rfds != NULL) ? &aread : NULL,
         (wfds != NULL) ? &awrite : NULL, &aexcept, &select_timeout);
@@ -1432,10 +1469,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
      have checked that descriptors were in awrite originally before re-adding them from
      aexcept. Luckily, GNUnet never uses aexcept for anything, so this does not become a problem (yet). */
   for (i = 0; i < aexcept.fd_count; i++)
-  {
-    if (aexcept.fd_array[i] != 0 && aexcept.fd_array[i] != -1)
-      FD_SET (aexcept.fd_array[i], &awrite);
-  }
+    FD_SET (aexcept.fd_array[i], &awrite);
 
   /* If our select returned something or is a 0-timed request, then also check the pipes and get out of here! */
   /* Sadly, it means code duplication :( */
@@ -1711,10 +1745,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
      * but we don't use OOB data.
      */
     for (i = 0; i < awrite.fd_count; i++)
-    {
-      if (awrite.fd_array[i] != 0 && awrite.fd_array[i] != -1)
-        FD_SET (awrite.fd_array[i], &aexcept);
-    }
+      FD_SET (awrite.fd_array[i], &aexcept);
     ResetEvent (select_finished_event);
     SetEvent (select_standby_event);
   }
@@ -1758,10 +1789,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
     }
     /* Check aexcept, add its contents to awrite */
     for (i = 0; i < aexcept.fd_count; i++)
-    {
-      if (aexcept.fd_array[i] != 0 && aexcept.fd_array[i] != -1)
-        FD_SET (aexcept.fd_array[i], &awrite);
-    }
+      FD_SET (aexcept.fd_array[i], &awrite);
   }
 
   returnedpos = returncode - WAIT_OBJECT_0;