Fix perf_crypto_rsa.c after various changes
[oweals/gnunet.git] / src / util / resolver_api.c
index 3469739e1f98a7ef769af6016f86dceaef7c0703..0488d6a3c1227c2fa3e7e4054ec8647d3bf875ed 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009-2015 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2015 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
@@ -14,8 +14,8 @@
 
      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.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -118,7 +118,7 @@ struct GNUNET_RESOLVER_RequestHandle
   GNUNET_RESOLVER_HostnameCallback name_callback;
 
   /**
-   * Closure for the respective "callback".
+   * Closure for the callbacks.
    */
   void *cls;
 
@@ -131,7 +131,7 @@ struct GNUNET_RESOLVER_RequestHandle
    * Task handle for making reply callbacks in numeric lookups
    * asynchronous, and for timeout handling.
    */
-  struct GNUNET_SCHEDULER_Task * task;
+  struct GNUNET_SCHEDULER_Task *task;
 
   /**
    * Desired address family.
@@ -171,8 +171,11 @@ struct GNUNET_RESOLVER_RequestHandle
 /**
  * Check that the resolver service runs on localhost
  * (or equivalent).
+ *
+ * @return #GNUNET_OK if the resolver is properly configured,
+ *         #GNUNET_SYSERR otherwise.
  */
-static void
+static int
 check_config ()
 {
   char *hostname;
@@ -197,32 +200,32 @@ check_config ()
                                              "HOSTNAME",
                                              &hostname))
   {
-    LOG (GNUNET_ERROR_TYPE_ERROR,
-         _("Must specify `%s' for `%s' in configuration!\n"),
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         _("Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"),
          "HOSTNAME",
          "resolver");
-    GNUNET_assert (0);
+    return GNUNET_SYSERR;
   }
-  if ((1 != inet_pton (AF_INET, hostname, &v4)) ||
-      (1 != inet_pton (AF_INET6, hostname, &v6)))
+  if ((1 == inet_pton (AF_INET, hostname, &v4)) ||
+      (1 == inet_pton (AF_INET6, hostname, &v6)))
   {
     GNUNET_free (hostname);
-    return;
+    return GNUNET_OK;
   }
   i = 0;
   while (NULL != loopback[i])
     if (0 == strcasecmp (loopback[i++], hostname))
     {
       GNUNET_free (hostname);
-      return;
+      return GNUNET_OK;
     }
-  LOG (GNUNET_ERROR_TYPE_ERROR,
-       _("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"),
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       _("Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"),
        "localhost",
        "HOSTNAME",
        "resolver");
   GNUNET_free (hostname);
-  GNUNET_assert (0);
+  return GNUNET_SYSERR;
 }
 
 
@@ -237,7 +240,7 @@ GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
   GNUNET_assert (NULL != cfg);
   backoff = GNUNET_TIME_UNIT_MILLISECONDS;
   resolver_cfg = cfg;
-  check_config ();
+  (void) check_config ();
 }
 
 
@@ -529,11 +532,9 @@ handle_response (void *cls,
  * numeric addresses.
  *
  * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
- * @param tc unused scheduler context
  */
 static void
-numeric_resolution (void *cls,
-                    const struct GNUNET_SCHEDULER_TaskContext *tc)
+numeric_resolution (void *cls)
 {
   struct GNUNET_RESOLVER_RequestHandle *rh = cls;
   struct sockaddr_in v4;
@@ -597,11 +598,9 @@ numeric_resolution (void *cls,
  * respective loopback numeric addresses.
  *
  * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
- * @param tc unused scheduler context
  */
 static void
-loopback_resolution (void *cls,
-                     const struct GNUNET_SCHEDULER_TaskContext *tc)
+loopback_resolution (void *cls)
 {
   struct GNUNET_RESOLVER_RequestHandle *rh = cls;
   struct sockaddr_in v4;
@@ -639,6 +638,7 @@ loopback_resolution (void *cls,
     rh->addr_callback (rh->cls,
                        (const struct sockaddr *) &v4,
                        sizeof (v4));
+
     break;
   default:
     GNUNET_break (0);
@@ -655,8 +655,7 @@ loopback_resolution (void *cls,
  * Task executed on system shutdown.
  */
 static void
-shutdown_task (void *cls,
-              const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
   s_task = NULL;
   GNUNET_RESOLVER_disconnect ();
@@ -685,7 +684,7 @@ process_requests ()
     /* nothing to do, release socket really soon if there is nothing
      * else happening... */
     s_task =
-        GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
+        GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                       &shutdown_task,
                                       NULL);
     return;
@@ -725,17 +724,13 @@ process_requests ()
  * Now try to reconnect to the resolver service.
  *
  * @param cls NULL
- * @param tc scheduler context
  */
 static void
-reconnect_task (void *cls,
-                const struct GNUNET_SCHEDULER_TaskContext *tc)
+reconnect_task (void *cls)
 {
   r_task = NULL;
   if (NULL == req_head)
     return;                     /* no work pending */
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Trying to connect to DNS service\n");
   client = GNUNET_CLIENT_connect ("resolver",
@@ -775,7 +770,9 @@ reconnect ()
       break;
     case GNUNET_SYSERR:
       /* request was cancelled, remove entirely */
-      GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
+      GNUNET_CONTAINER_DLL_remove (req_head,
+                                  req_tail,
+                                  rh);
       GNUNET_free (rh);
       break;
     default:
@@ -799,11 +796,9 @@ reconnect ()
  * A DNS resolution timed out. Notify the application.
  *
  * @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
- * @param tc scheduler context
  */
 static void
-handle_lookup_timeout (void *cls,
-                       const struct GNUNET_SCHEDULER_TaskContext *tc)
+handle_lookup_timeout (void *cls)
 {
   struct GNUNET_RESOLVER_RequestHandle *rh = cls;
 
@@ -899,11 +894,9 @@ GNUNET_RESOLVER_ip_get (const char *hostname,
  * conversion and invoke the callback.
  *
  * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
- * @param tc unused scheduler context
  */
 static void
-numeric_reverse (void *cls,
-                 const struct GNUNET_SCHEDULER_TaskContext *tc)
+numeric_reverse (void *cls)
 {
   struct GNUNET_RESOLVER_RequestHandle *rh = cls;
   char *result;
@@ -951,7 +944,13 @@ GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
   size_t ip_len;
   const void *ip;
 
-  check_config ();
+  if (GNUNET_OK != check_config ())
+  {
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         _("Resolver not configured correctly.\n"));
+    return NULL;
+  }
+
   switch (sa->sa_family)
   {
   case AF_INET:
@@ -1004,7 +1003,6 @@ GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
 char *
 GNUNET_RESOLVER_local_fqdn_get ()
 {
-  struct hostent *host;
   char hostname[GNUNET_OS_get_hostname_max_length () + 1];
 
   if (0 != gethostname (hostname, sizeof (hostname) - 1))
@@ -1016,15 +1014,60 @@ GNUNET_RESOLVER_local_fqdn_get ()
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Resolving our FQDN `%s'\n",
        hostname);
-  host = gethostbyname (hostname);
-  if (NULL == host)
+#if HAVE_GETADDRINFO
   {
-    LOG (GNUNET_ERROR_TYPE_ERROR,
-         _("Could not resolve our FQDN : %s\n"),
-         hstrerror (h_errno));
-    return NULL;
+    struct addrinfo *ai;
+    int ret;
+    char *rval;
+
+    if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR,
+           _("Could not resolve our FQDN: %s\n"),
+           gai_strerror (ret));
+      return NULL;
+    }
+    if (NULL != ai->ai_canonname)
+      rval = GNUNET_strdup (ai->ai_canonname);
+    else
+      rval = GNUNET_strdup (hostname);
+    freeaddrinfo (ai);
+    return rval;
+  }
+#elif HAVE_GETHOSTBYNAME2
+  {
+    struct hostent *host;
+
+    host = gethostbyname2 (hostname, AF_INET);
+    if (NULL == host)
+      host = gethostbyname2 (hostname, AF_INET6);
+    if (NULL == host)
+      {
+        LOG (GNUNET_ERROR_TYPE_ERROR,
+             _("Could not resolve our FQDN: %s\n"),
+             hstrerror (h_errno));
+        return NULL;
+      }
+    return GNUNET_strdup (host->h_name);
+  }
+#elif HAVE_GETHOSTBYNAME
+  {
+    struct hostent *host;
+
+    host = gethostbyname (hostname);
+    if (NULL == host)
+      {
+        LOG (GNUNET_ERROR_TYPE_ERROR,
+             _("Could not resolve our FQDN: %s\n"),
+             hstrerror (h_errno));
+        return NULL;
+      }
+    return GNUNET_strdup (host->h_name);
   }
-  return GNUNET_strdup (host->h_name);
+#else
+  /* fallback: just hope name is already FQDN */
+  return GNUNET_strdup (hostname);
+#endif
 }