make resolver test pass even if primary DNS is down
authorChristian Grothoff <christian@grothoff.org>
Mon, 20 Jun 2011 08:50:35 +0000 (08:50 +0000)
committerChristian Grothoff <christian@grothoff.org>
Mon, 20 Jun 2011 08:50:35 +0000 (08:50 +0000)
src/util/connection.c
src/util/gnunet-service-resolver.c
src/util/resolver_api.c
src/util/server.c
src/util/test_resolver_api.c
src/util/test_resolver_api_data.conf

index a29237de67c14f0e0e40fa3bc566eaf6c79c708a..80f2cae7f8b33d11e50a556b419aa5be91a45d6f 100644 (file)
@@ -1606,7 +1606,7 @@ SCHEDULE_WRITE:
   GNUNET_assert ( (sock->nth.notify_ready != NULL) || (have > 0) );
   if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
     sock->write_task =
-      GNUNET_SCHEDULER_add_write_net ((have > 0
+      GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready == NULL
                                      ? GNUNET_TIME_UNIT_FOREVER_REL 
                                      : GNUNET_TIME_absolute_get_remaining (sock->nth.transmit_timeout),
                                      sock->sock, 
index ee2cfb3b837408bb8a5865825a7d63d0c9fad946..bffe2ce2610ac5ed75bb607ee58971793ae410ed 100644 (file)
@@ -23,8 +23,6 @@
  * @brief code to do DNS resolution
  * @author Christian Grothoff
  */
-
-#include <stdlib.h>
 #include "platform.h"
 #include "gnunet_disk_lib.h"
 #include "gnunet_getopt_lib.h"
@@ -440,6 +438,7 @@ handle_get (void *cls,
   uint16_t msize;
   const struct GNUNET_RESOLVER_GetMessage *msg;
   const char *hostname;
+  const struct sockaddr *sa;
   uint16_t size;
   int direction;
   int domain;
@@ -473,11 +472,51 @@ handle_get (void *cls,
     }
   else
     {
-#if DEBUG_RESOLVER
+#if DEBUG_RESOLVER      
+      char buf[INET6_ADDRSTRLEN] buf;
+#endif
+      sa = (const struct sockaddr*) &msg[1];
+      if (size < sizeof (struct sockaddr_in))
+       {
+         GNUNET_break (0);
+         GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+         return;
+       }
+      switch (sa->sa_family)
+       {
+       case AF_INET:
+         if (size != sizeof (struct sockaddr_in))
+           {
+             GNUNET_break (0);
+             GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+             return;
+           }
+#if DEBUG_RESOLVER      
+         inet_ntop (AF_INET, sa, buf, size);
+#endif
+         break;
+       case AF_INET6:
+         if (size != sizeof (struct sockaddr_in6))
+           {
+             GNUNET_break (0);
+             GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+             return;
+           }
+#if DEBUG_RESOLVER      
+         inet_ntop (AF_INET6, sa, buf, size);
+#endif
+         break;
+       default:
+         GNUNET_break (0);
+         GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+         return;
+       }      
+#if DEBUG_RESOLVER      
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  _("Resolver asked to look up IP address.\n"));
+                  _("Resolver asked to look up IP address `%s'.\n"),
+                 buf);
 #endif
-      get_ip_as_string (client, (const struct sockaddr *) &msg[1], size);
+      get_ip_as_string (client, sa, size);
     }
 }
 
index 5c0973f8644056c792287b27f7a61aa01911cdab..6127badeb4b52aacd6c6fed93be6f822cec8b90e 100644 (file)
@@ -33,7 +33,6 @@
 #include "gnunet_server_lib.h"
 #include "resolver.h"
 
-
 /**
  * Maximum supported length for a hostname
  */
@@ -239,6 +238,10 @@ GNUNET_RESOLVER_disconnect ()
   GNUNET_assert (NULL == req_tail);
   if (NULL != client)
     {
+#if DEBUG_RESOLVER
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Disconnecting from DNS service\n");
+#endif
       GNUNET_CLIENT_disconnect (client, GNUNET_NO);
       client = NULL;
     }
@@ -339,6 +342,10 @@ handle_response (void *cls,
   const struct sockaddr *sa;
   socklen_t salen;
 
+#if DEBUG_RESOLVER
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Receiving response from DNS service\n");
+#endif
   if (msg == NULL)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
@@ -437,8 +444,9 @@ handle_response (void *cls,
       {
        char *ips = no_resolve (sa, salen);
        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                   "Resolver returns `%s' for `%s'.\n", ips,
-                   rh->hostname);
+                   "Resolver returns `%s' for `%s'.\n", 
+                   ips,
+                   (const char*) &rh[1]);
        GNUNET_free (ips);
       }
 #endif
@@ -601,7 +609,10 @@ process_requests ()
   msg->direction = htonl (rh->direction);
   msg->domain = htonl (rh->domain);
   memcpy (&msg[1], &rh[1], rh->data_len);
-
+#if DEBUG_RESOLVER
+  GNUNET_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,
@@ -633,9 +644,15 @@ reconnect_task (void *cls,
     return; /* no work pending */
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
+#if DEBUG_RESOLVER
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Trying to connect to DNS service\n");
+#endif
   client = GNUNET_CLIENT_connect ("resolver", cfg);
   if (NULL == client)
     {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Failed to connect, will try again later\n");
       reconnect ();
       return;
     }
@@ -677,6 +694,11 @@ reconnect ()
          break;
        }
     }
+#if DEBUG_RESOLVER
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Will try to connect to DNS service in %llu ms\n",
+             (unsigned long long) backoff.rel_value);
+#endif
   r_task = GNUNET_SCHEDULER_add_delayed (backoff,
                                         &reconnect_task,
                                         NULL);
index d1d0b1aca2d4e635d845a15721ba4c92431db767..33a824e7c8065d9b648da84d59c22767f803df3f 100644 (file)
@@ -375,6 +375,22 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
                         (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
           eno = 0;
         }
+      else
+       {
+         if (port != 0)
+           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                       _
+                       ("`%s' failed for port %d (%s): address already in use\n"),
+                       "bind", port,
+                       (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
+         else if (serverAddr->sa_family == AF_UNIX)
+           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                       _
+                       ("`%s' failed for `%s': address already in use\n"),
+                       "bind", 
+                       ((const struct sockaddr_un*) serverAddr)->sun_path);
+
+       }
       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
       errno = eno;
       return NULL;
index d1c343f8263d01e9650ad5ce9f846625040f8abf..be631e36f259ac38b42251b4432790239a132aba 100644 (file)
@@ -54,6 +54,7 @@ check_hostname(void *cls, const struct sockaddr *sa, socklen_t salen)
       GNUNET_a2s(sa, salen));
 }
 
+
 static void
 check_localhost_num(void *cls, const char *hostname)
 {
@@ -78,6 +79,7 @@ check_localhost_num(void *cls, const char *hostname)
     }
 }
 
+
 static void
 check_localhost(void *cls, const char *hostname)
 {
@@ -223,7 +225,7 @@ run(void *cls, char * const *args,
   int *ok = cls;
   struct sockaddr_in sa;
   struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply(
-      GNUNET_TIME_UNIT_MILLISECONDS, 2500);
+      GNUNET_TIME_UNIT_SECONDS, 30);
   int count_ips = 0;
   char * own_fqdn;
 
@@ -233,15 +235,6 @@ run(void *cls, char * const *args,
   sa.sin_len = (u_char) sizeof (sa);
 #endif
   sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-  GNUNET_RESOLVER_ip_get("localhost", AF_INET, timeout, &check_127,
-      cls);
-  GNUNET_RESOLVER_hostname_get((const struct sockaddr *) &sa,
-      sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
-  GNUNET_RESOLVER_hostname_get((const struct sockaddr *) &sa,
-      sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
-  GNUNET_RESOLVER_hostname_resolve(AF_UNSPEC, timeout,
-      &check_hostname, cls);
-
 
   /*
    * Looking up our own fqdn
@@ -254,7 +247,6 @@ run(void *cls, char * const *args,
    * Testing non-local DNS resolution
    * DNS rootserver to test: a.root-servers.net - 198.41.0.4
    */
-
   const char * rootserver_name = ROOTSERVER_NAME;
   struct hostent *rootserver;
 
@@ -340,10 +332,11 @@ run(void *cls, char * const *args,
   }
 
 #if DEBUG_RESOLVER
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own reverse name resolution is working\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+             "System's own reverse name resolution is working\n");
 #endif
-  /* Resolve the same using GNUNET */
 
+  /* Resolve the same using GNUNET */
   memset(&sa, 0, sizeof(sa));
   sa.sin_family = AF_INET;
 #if HAVE_SOCKADDR_IN_SIN_LEN
@@ -356,6 +349,25 @@ run(void *cls, char * const *args,
 #endif
   GNUNET_RESOLVER_hostname_get((const struct sockaddr *) &sa,
       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls);
+
+  memset(&sa, 0, sizeof(sa));
+  sa.sin_family = AF_INET;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+  sa.sin_len = (u_char) sizeof (sa);
+#endif
+  sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+  GNUNET_RESOLVER_ip_get("localhost", AF_INET, timeout, &check_127,
+                        cls);
+  fprintf (stderr, "Trying to get hostname for 127.0.0.1\n");
+  GNUNET_RESOLVER_hostname_get((const struct sockaddr *) &sa,
+      sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
+
+  GNUNET_RESOLVER_hostname_get((const struct sockaddr *) &sa,
+      sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
+  GNUNET_RESOLVER_hostname_resolve(AF_UNSPEC, timeout,
+      &check_hostname, cls);
+
 }
 
 static int
index dde65683a14142ece989b721593792e28dbce636..f776c0bcddc4ca06ee338da161de29c877d21ac2 100644 (file)
@@ -4,6 +4,7 @@ SERVICEHOME = /tmp/test-gnunetd-statistics/
 [resolver]
 PORT = 22354
 HOSTNAME = localhost
+DEBUG = YES
 
 [dns]
 AUTOSTART = NO