-remove find() forking, we pretty much should not need this anymore, and it confused...
[oweals/gnunet.git] / src / util / resolver_api.c
index 9d5ae6c0a9b02ccf46dc69c9278acdb3d6f63195..46ac52586c752c1bb8baff3f9728d04779a52578 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
 /*
      This file is part of GNUnet.
-     (C) 2009, 2011 Christian Grothoff (and other contributing authors)
+     (C) 2009-2013 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
 
      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 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
  * @author Christian Grothoff
  */
 #include "platform.h"
  * @author Christian Grothoff
  */
 #include "platform.h"
-#include "gnunet_getopt_lib.h"
-#include "gnunet_os_lib.h"
-#include "gnunet_client_lib.h"
-#include "gnunet_container_lib.h"
+#include "gnunet_util_lib.h"
 #include "gnunet_protocols.h"
 #include "gnunet_resolver_service.h"
 #include "gnunet_protocols.h"
 #include "gnunet_resolver_service.h"
-#include "gnunet_server_lib.h"
 #include "resolver.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "resolver-api", __VA_ARGS__)
 #include "resolver.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "resolver-api", __VA_ARGS__)
@@ -210,15 +206,14 @@ check_config ()
     return;
   }
   i = 0;
     return;
   }
   i = 0;
-  while (loopback[i] != NULL)
+  while (NULL != loopback[i])
     if (0 == strcasecmp (loopback[i++], hostname))
     {
       GNUNET_free (hostname);
       return;
     }
   LOG (GNUNET_ERROR_TYPE_ERROR,
     if (0 == strcasecmp (loopback[i++], hostname))
     {
       GNUNET_free (hostname);
       return;
     }
   LOG (GNUNET_ERROR_TYPE_ERROR,
-       _
-       ("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"),
+       _("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"),
        "localhost", "HOSTNAME", "resolver");
   GNUNET_free (hostname);
   GNUNET_assert (0);
        "localhost", "HOSTNAME", "resolver");
   GNUNET_free (hostname);
   GNUNET_assert (0);
@@ -250,18 +245,16 @@ GNUNET_RESOLVER_disconnect ()
   GNUNET_assert (NULL == req_tail);
   if (NULL != client)
   {
   GNUNET_assert (NULL == req_tail);
   if (NULL != client)
   {
-#if DEBUG_RESOLVER
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from DNS service\n");
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from DNS service\n");
-#endif
-    GNUNET_CLIENT_disconnect (client, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (client);
     client = NULL;
   }
     client = NULL;
   }
-  if (r_task != GNUNET_SCHEDULER_NO_TASK)
+  if (GNUNET_SCHEDULER_NO_TASK != r_task)
   {
     GNUNET_SCHEDULER_cancel (r_task);
     r_task = GNUNET_SCHEDULER_NO_TASK;
   }
   {
     GNUNET_SCHEDULER_cancel (r_task);
     r_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  if (s_task != GNUNET_SCHEDULER_NO_TASK)
+  if (GNUNET_SCHEDULER_NO_TASK != s_task)
   {
     GNUNET_SCHEDULER_cancel (s_task);
     s_task = GNUNET_SCHEDULER_NO_TASK;
   {
     GNUNET_SCHEDULER_cancel (s_task);
     s_task = GNUNET_SCHEDULER_NO_TASK;
@@ -339,10 +332,9 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
   struct GNUNET_RESOLVER_RequestHandle *rh = cls;
   uint16_t size;
 
   struct GNUNET_RESOLVER_RequestHandle *rh = cls;
   uint16_t size;
 
-#if DEBUG_RESOLVER
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving response from DNS service\n");
-#endif
-  if (msg == NULL)
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Receiving response from DNS service\n");
+  if (NULL == msg)
   {
     char buf[INET6_ADDRSTRLEN];
 
   {
     char buf[INET6_ADDRSTRLEN];
 
@@ -355,16 +347,16 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
            _("Timeout trying to resolve hostname `%s'.\n"),
            (const char *) &rh[1]);
     /* check if request was canceled */
            _("Timeout trying to resolve hostname `%s'.\n"),
            (const char *) &rh[1]);
     /* check if request was canceled */
-    if (rh->was_transmitted != GNUNET_SYSERR)
+    if (GNUNET_SYSERR != rh->was_transmitted)
     {
       if (NULL != rh->name_callback)
       {
         /* no reverse lookup was successful, return ip as string */
         if (rh->received_response == GNUNET_NO)
     {
       if (NULL != rh->name_callback)
       {
         /* no reverse lookup was successful, return ip as string */
         if (rh->received_response == GNUNET_NO)
-          rh->name_callback (rh->cls,
-                             no_resolve (rh->af,
-                                        &rh[1],
-                                         rh->data_len));
+        {
+          rh->name_callback (rh->cls, no_resolve (rh->af, &rh[1], rh->data_len));
+          rh->name_callback (rh->cls, NULL);
+        }
         /* at least one reverse lookup was successful */
         else
           rh->name_callback (rh->cls, NULL);
         /* at least one reverse lookup was successful */
         else
           rh->name_callback (rh->cls, NULL);
@@ -374,7 +366,7 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
     }
     GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
     GNUNET_free (rh);
     }
     GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
     GNUNET_free (rh);
-    GNUNET_CLIENT_disconnect (client, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (client);
     client = NULL;
     reconnect ();
     return;
     client = NULL;
     reconnect ();
     return;
@@ -382,7 +374,7 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
   if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
   {
     GNUNET_break (0);
   if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
   {
     GNUNET_break (0);
-    GNUNET_CLIENT_disconnect (client, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (client);
     client = NULL;
     reconnect ();
     return;
     client = NULL;
     reconnect ();
     return;
@@ -417,15 +409,13 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
         rh->name_callback (rh->cls, NULL);
       GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
       GNUNET_free (rh);
         rh->name_callback (rh->cls, NULL);
       GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
       GNUNET_free (rh);
-      GNUNET_CLIENT_disconnect (client, GNUNET_NO);
+      GNUNET_CLIENT_disconnect (client);
       client = NULL;
       reconnect ();
       return;
     }
       client = NULL;
       reconnect ();
       return;
     }
-#if DEBUG_RESOLVER
-    LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s' for IP `%s'.\n"),
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolver returns `%s' for IP `%s'.\n",
          hostname, GNUNET_a2s ((const void *) &rh[1], rh->data_len));
          hostname, GNUNET_a2s ((const void *) &rh[1], rh->data_len));
-#endif
     if (rh->was_transmitted != GNUNET_SYSERR)
       rh->name_callback (rh->cls, hostname);
     rh->received_response = GNUNET_YES;
     if (rh->was_transmitted != GNUNET_SYSERR)
       rh->name_callback (rh->cls, hostname);
     rh->received_response = GNUNET_YES;
@@ -473,7 +463,7 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
         rh->addr_callback (rh->cls, NULL, 0);
       GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
       GNUNET_free (rh);
         rh->addr_callback (rh->cls, NULL, 0);
       GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
       GNUNET_free (rh);
-      GNUNET_CLIENT_disconnect (client, GNUNET_NO);
+      GNUNET_CLIENT_disconnect (client);
       client = NULL;
       reconnect ();
       return;
       client = NULL;
       reconnect ();
       return;
@@ -592,10 +582,12 @@ loopback_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Task executed on system shutdown.
  */
 static void
  * Task executed on system shutdown.
  */
 static void
-shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls,
+              const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   s_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_RESOLVER_disconnect ();
 {
   s_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_RESOLVER_disconnect ();
+  backoff = GNUNET_TIME_UNIT_MILLISECONDS;
 }
 
 
 }
 
 
@@ -606,7 +598,7 @@ static void
 process_requests ()
 {
   struct GNUNET_RESOLVER_GetMessage *msg;
 process_requests ()
 {
   struct GNUNET_RESOLVER_GetMessage *msg;
-  char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
+  char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
   struct GNUNET_RESOLVER_RequestHandle *rh;
 
   if (NULL == client)
   struct GNUNET_RESOLVER_RequestHandle *rh;
 
   if (NULL == client)
@@ -633,18 +625,17 @@ process_requests ()
   msg->direction = htonl (rh->direction);
   msg->af = htonl (rh->af);
   memcpy (&msg[1], &rh[1], rh->data_len);
   msg->direction = htonl (rh->direction);
   msg->af = htonl (rh->af);
   memcpy (&msg[1], &rh[1], rh->data_len);
-#if DEBUG_RESOLVER
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Transmitting DNS resolution request to DNS service\n");
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Transmitting DNS resolution request to DNS service\n");
-#endif
   if (GNUNET_OK !=
       GNUNET_CLIENT_transmit_and_get_response (client, &msg->header,
                                                GNUNET_TIME_absolute_get_remaining
                                                (rh->timeout), GNUNET_YES,
                                                &handle_response, rh))
   {
   if (GNUNET_OK !=
       GNUNET_CLIENT_transmit_and_get_response (client, &msg->header,
                                                GNUNET_TIME_absolute_get_remaining
                                                (rh->timeout), GNUNET_YES,
                                                &handle_response, rh))
   {
-    GNUNET_CLIENT_disconnect (client, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (client);
     client = NULL;
     client = NULL;
+    GNUNET_break (0);
     reconnect ();
     return;
   }
     reconnect ();
     return;
   }
@@ -666,13 +657,13 @@ reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     return;                     /* no work pending */
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
     return;                     /* no work pending */
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
-#if DEBUG_RESOLVER
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to DNS service\n");
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Trying to connect to DNS service\n");
   client = GNUNET_CLIENT_connect ("resolver", resolver_cfg);
   if (NULL == client)
   {
   client = GNUNET_CLIENT_connect ("resolver", resolver_cfg);
   if (NULL == client)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect, will try again later\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "Failed to connect, will try again later\n");
     reconnect ();
     return;
   }
     reconnect ();
     return;
   }
@@ -712,14 +703,12 @@ reconnect ()
       break;
     }
   }
       break;
     }
   }
-#if DEBUG_RESOLVER
   LOG (GNUNET_ERROR_TYPE_DEBUG,
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Will try to connect to DNS service in %llu ms\n",
-       (unsigned long long) backoff.rel_value);
-#endif
+       "Will try to connect to DNS service in %s\n",
+       GNUNET_STRINGS_relative_time_to_string (backoff, GNUNET_YES));
   GNUNET_assert (NULL != resolver_cfg);
   r_task = GNUNET_SCHEDULER_add_delayed (backoff, &reconnect_task, NULL);
   GNUNET_assert (NULL != resolver_cfg);
   r_task = GNUNET_SCHEDULER_add_delayed (backoff, &reconnect_task, NULL);
-  backoff = GNUNET_TIME_relative_multiply (backoff, 2);
+  backoff = GNUNET_TIME_STD_BACKOFF (backoff);
 }
 
 
 }
 
 
@@ -771,7 +760,7 @@ GNUNET_RESOLVER_ip_get (const char *hostname, int af,
   }
   /* then, check if this is a loopback address */
   i = 0;
   }
   /* then, check if this is a loopback address */
   i = 0;
-  while (loopback[i] != NULL)
+  while (NULL != loopback[i])
     if (0 == strcasecmp (loopback[i++], hostname))
     {
       rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution, rh);
     if (0 == strcasecmp (loopback[i++], hostname))
     {
       rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution, rh);
@@ -803,9 +792,7 @@ numeric_reverse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   char *result;
 
   result = no_resolve (rh->af, &rh[1], rh->data_len);
   char *result;
 
   result = no_resolve (rh->af, &rh[1], rh->data_len);
-#if DEBUG_RESOLVER
-  LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), result);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolver returns `%s'.\n", result);
   if (result != NULL)
   {
     rh->name_callback (rh->cls, result);
   if (result != NULL)
   {
     rh->name_callback (rh->cls, result);
@@ -897,9 +884,7 @@ GNUNET_RESOLVER_local_fqdn_get ()
                   "gethostname");
     return NULL;
   }
                   "gethostname");
     return NULL;
   }
-#if DEBUG_RESOLVER
-  LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"), hostname);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Resolving our FQDN `%s'\n", hostname);
   host = gethostbyname (hostname);
   if (NULL == host)
   {
   host = gethostbyname (hostname);
   if (NULL == host)
   {
@@ -934,9 +919,9 @@ GNUNET_RESOLVER_hostname_resolve (int af,
                   "gethostname");
     return NULL;
   }
                   "gethostname");
     return NULL;
   }
-#if DEBUG_RESOLVER
-  LOG (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our hostname `%s'\n"), hostname);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Resolving our hostname `%s'\n",
+       hostname);
   return GNUNET_RESOLVER_ip_get (hostname, af, timeout, callback, cls);
 }
 
   return GNUNET_RESOLVER_ip_get (hostname, af, timeout, callback, cls);
 }