Merge remote-tracking branch 'origin/master' into credentials
[oweals/gnunet.git] / src / util / network.c
index 489966426286991830fab146352f2290d7ed2428..66a468e455a8f314eb5c56bb488d497f63d0681d 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (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
@@ -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
 
@@ -166,6 +166,12 @@ GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
   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));
@@ -195,7 +201,7 @@ GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
 
 
 #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
 
 
@@ -512,11 +518,10 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
     /* 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)
@@ -540,7 +545,7 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
     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;
@@ -574,13 +579,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,
@@ -766,7 +770,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)
@@ -780,9 +784,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;
 
@@ -791,7 +795,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
 }
 
@@ -1204,7 +1208,7 @@ GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
     GNUNET_array_grow (to->handles,
                        to->handles_size,
                        from->handles_pos * 2);
-  memcpy (to->handles,
+  GNUNET_memcpy (to->handles,
           from->handles,
           from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
   to->handles_pos = from->handles_pos;
@@ -1693,6 +1697,64 @@ 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));
+  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
 /**