Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / util / resolver_api.c
index 9be7014c2d1ce2662f19e829b5a49e2805d3c18f..f33c31f1c8ccedf328f4526ba6096088b004a594 100644 (file)
@@ -29,9 +29,9 @@
 #include "gnunet_resolver_service.h"
 #include "resolver.h"
 
-#define LOG(kind,...) GNUNET_log_from (kind, "resolver-api", __VA_ARGS__)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-resolver-api", __VA_ARGS__)
 
-#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "resolver-api", syscall)
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-resolver-api", syscall)
 
 /**
  * Maximum supported length for a hostname
@@ -249,8 +249,16 @@ GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
 void
 GNUNET_RESOLVER_disconnect ()
 {
-  GNUNET_assert (NULL == req_head);
-  GNUNET_assert (NULL == req_tail);
+  struct GNUNET_RESOLVER_RequestHandle *rh;
+
+  while (NULL != (rh = req_head))
+  {
+    GNUNET_assert (GNUNET_SYSERR == rh->was_transmitted);
+    GNUNET_CONTAINER_DLL_remove (req_head,
+                                req_tail,
+                                rh);
+    GNUNET_free (rh);
+  }
   if (NULL != mq)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -271,6 +279,42 @@ GNUNET_RESOLVER_disconnect ()
 }
 
 
+/**
+ * Task executed on system shutdown.
+ */
+static void
+shutdown_task (void *cls)
+{
+  s_task = NULL;
+  GNUNET_RESOLVER_disconnect ();
+  backoff = GNUNET_TIME_UNIT_MILLISECONDS;
+}
+
+
+/**
+ * Consider disconnecting if we have no further requests pending.
+ */
+static void
+check_disconnect ()
+{
+  struct GNUNET_RESOLVER_RequestHandle *rh;
+
+  for (rh = req_head; NULL != rh; rh = rh->next)
+    if (GNUNET_SYSERR != rh->was_transmitted)
+      return;
+  if (NULL != r_task)
+  {
+    GNUNET_SCHEDULER_cancel (r_task);
+    r_task = NULL;
+  }
+  if (NULL != s_task)
+    return;
+  s_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
+                                        &shutdown_task,
+                                        NULL);
+}
+
+
 /**
  * Convert IP address to string without DNS resolution.
  *
@@ -337,25 +381,14 @@ static void
 mq_error_handler (void *cls,
                   enum GNUNET_MQ_Error error)
 {
-  GNUNET_break (0);
   GNUNET_MQ_destroy (mq);
   mq = NULL;
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "MQ error, reconnecting\n");
   reconnect ();
 }
 
 
-/**
- * Task executed on system shutdown.
- */
-static void
-shutdown_task (void *cls)
-{
-  s_task = NULL;
-  GNUNET_RESOLVER_disconnect ();
-  backoff = GNUNET_TIME_UNIT_MILLISECONDS;
-}
-
-
 /**
  * Process pending requests to the resolver.
  */
@@ -388,7 +421,7 @@ process_requests ()
                              GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
   msg->direction = htonl (rh->direction);
   msg->af = htonl (rh->af);
-  memcpy (&msg[1],
+  GNUNET_memcpy (&msg[1],
           &rh[1],
           rh->data_len);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -600,7 +633,9 @@ numeric_resolution (void *cls)
   }
   if ( ( (rh->af == AF_UNSPEC) ||
          (rh->af == AF_INET6) ) &&
-       (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr) ) )
+       (1 == inet_pton (AF_INET6,
+                       hostname,
+                       &v6.sin6_addr) ) )
   {
     rh->addr_callback (rh->cls,
                        (const struct sockaddr *) &v6,
@@ -672,6 +707,9 @@ loopback_resolution (void *cls)
   rh->addr_callback (rh->cls,
                      NULL,
                      0);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Finished resolving hostname `%s'.\n",
+       (const char *) &rh[1]);
   GNUNET_free (rh);
 }
 
@@ -684,12 +722,12 @@ loopback_resolution (void *cls)
 static void
 reconnect_task (void *cls)
 {
-  GNUNET_MQ_hd_var_size (response,
-                         GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
-                         struct GNUNET_MessageHeader);
   struct GNUNET_MQ_MessageHandler handlers[] = {
-     make_response_handler (NULL),
-     GNUNET_MQ_handler_end ()
+    GNUNET_MQ_hd_var_size (response,
+                           GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
+                           struct GNUNET_MessageHeader,
+                           NULL),
+    GNUNET_MQ_handler_end ()
   };
 
   r_task = NULL;
@@ -697,7 +735,7 @@ reconnect_task (void *cls)
     return;                     /* no work pending */
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Trying to connect to DNS service\n");
-  mq = GNUNET_CLIENT_connecT (resolver_cfg,
+  mq = GNUNET_CLIENT_connect (resolver_cfg,
                               "resolver",
                               handlers,
                               &mq_error_handler,
@@ -741,6 +779,7 @@ reconnect ()
                                   req_tail,
                                   rh);
       GNUNET_free (rh);
+      check_disconnect ();
       break;
     default:
       GNUNET_assert (0);
@@ -842,13 +881,16 @@ GNUNET_RESOLVER_ip_get (const char *hostname,
     GNUNET_break (0);
     return NULL;
   }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Trying to resolve hostname `%s'.\n",
+       hostname);
   rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
   rh->af = af;
   rh->addr_callback = callback;
   rh->cls = callback_cls;
-  memcpy (&rh[1],
-          hostname,
-          slen);
+  GNUNET_memcpy (&rh[1],
+                hostname,
+                slen);
   rh->data_len = slen;
   rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
   rh->direction = GNUNET_NO;
@@ -984,7 +1026,7 @@ GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
   rh->cls = cls;
   rh->af = sa->sa_family;
   rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
-  memcpy (&rh[1],
+  GNUNET_memcpy (&rh[1],
           ip,
           ip_len);
   rh->data_len = ip_len;
@@ -1134,6 +1176,10 @@ GNUNET_RESOLVER_hostname_resolve (int af,
 void
 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
 {
+  if (GNUNET_NO == rh->direction)
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "Asked to cancel request to resolve hostname `%s'.\n",
+        (const char *) &rh[1]);
   if (NULL != rh->task)
   {
     GNUNET_SCHEDULER_cancel (rh->task);
@@ -1146,10 +1192,12 @@ GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
                                    req_tail,
                                    rh);
     GNUNET_free (rh);
+    check_disconnect ();
     return;
   }
   GNUNET_assert (GNUNET_YES == rh->was_transmitted);
   rh->was_transmitted = GNUNET_SYSERR;  /* mark as cancelled */
+  check_disconnect ();
 }