add keywords from metadata for files
[oweals/gnunet.git] / src / util / test_resolver_api.c
index c577222e16abafc21666974ee0faa303e6f29816..b897a6c13c4aa6127f6121f80af44f4fc0b83f94 100644 (file)
@@ -4,7 +4,7 @@
 
  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
@@ -94,11 +94,9 @@ check_localhost(void *cls, const char *hostname)
     }
   else
     {
     }
   else
     {
-#if DEBUG_RESOLVER
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-          "Received invalid hostname `%s'.\n", hostname);
-#endif
-      GNUNET_break(0);
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n", 
+                 hostname);
     }
 }
 
     }
 }
 
@@ -128,37 +126,45 @@ check_127(void *cls, const struct sockaddr *sa, socklen_t salen)
 }
 
 static void
 }
 
 static void
-check_local_hostname(void *cls, const char *hostname)
+check_local_fqdn(void *cls, const char *gnunet_fqdn)
 {
   int result = 0;
 
 {
   int result = 0;
 
-  char own_hostname[GNUNET_OS_get_hostname_max_length() + 1];
+  struct hostent *host;
+  char hostname[GNUNET_OS_get_hostname_max_length() + 1];
+
+  if (0 != gethostname (hostname, sizeof (hostname) - 1))
+    {
+      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
+                           GNUNET_ERROR_TYPE_BULK, "gethostname");
+      return;
+    }
 #if DEBUG_RESOLVER
 #if DEBUG_RESOLVER
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-          "Hostname resolved here is `%s'.\n", own_hostname);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-          "Hostname resolved using resolver is `%s'.\n", hostname);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              _("Resolving our FQDN `%s'\n"), hostname);
 #endif
 #endif
-
-  result = gethostname (own_hostname, sizeof (own_hostname) - 1);
-
-  if ( 0 != result )
+  host = gethostbyname ( hostname );
+  if ( NULL == host)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-        "Could not resolve local hostname\n", own_hostname);
+                _("Could not resolve our FQDN : %s %u\n"),
+                hstrerror (h_errno), h_errno);
+    return;
   }
   }
-  GNUNET_assert( 0 == result);
 
 
-  result = strcmp(hostname, own_hostname);
+  GNUNET_assert( 0 != host);
+
+  result = strcmp(host->h_name, gnunet_fqdn);
   if ( 0 != result )
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   if ( 0 != result )
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-        "Local resolved and resolver resolved hostnames are not equal\n", own_hostname);
+        "Local resolved and resolver resolved fqdns are not equal\n");
   }
   GNUNET_assert( 0 == result);
 }
 
 
   }
   GNUNET_assert( 0 == result);
 }
 
 
+
 static void
 check_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen)
 {
 static void
 check_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen)
 {
@@ -211,38 +217,42 @@ check_rootserver_name(void *cls, const char *hostname)
 }
 
 static void
 }
 
 static void
-run(void *cls, struct GNUNET_SCHEDULER_Handle *sched, char * const *args,
+run(void *cls, char * const *args,
     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
+  int *ok = cls;
   struct sockaddr_in sa;
   struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply(
       GNUNET_TIME_UNIT_MILLISECONDS, 2500);
   int count_ips = 0;
   struct sockaddr_in sa;
   struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply(
       GNUNET_TIME_UNIT_MILLISECONDS, 2500);
   int count_ips = 0;
+  char * own_fqdn;
 
   memset(&sa, 0, sizeof(sa));
   sa.sin_family = AF_INET;
   sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
 
   memset(&sa, 0, sizeof(sa));
   sa.sin_family = AF_INET;
   sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-  GNUNET_RESOLVER_ip_get(sched, cfg, "localhost", AF_INET, timeout, &check_127,
+  GNUNET_RESOLVER_ip_get(cfg, "localhost", AF_INET, timeout, &check_127,
       cls);
       cls);
-  GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
+  GNUNET_RESOLVER_hostname_get(cfg, (const struct sockaddr *) &sa,
       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
-  GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
+  GNUNET_RESOLVER_hostname_get(cfg, (const struct sockaddr *) &sa,
       sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
       sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
-  GNUNET_RESOLVER_hostname_resolve(sched, cfg, AF_UNSPEC, timeout,
+  GNUNET_RESOLVER_hostname_resolve(cfg, AF_UNSPEC, timeout,
       &check_hostname, cls);
 
       &check_hostname, cls);
 
+
   /*
   /*
-   * Looking up our own hostname
+   * Looking up our own fqdn
    */
    */
-  GNUNET_RESOLVER_local_hostname_get(sched, cfg, &check_local_hostname, cls);
-
+  own_fqdn = GNUNET_RESOLVER_local_fqdn_get();
+  check_local_fqdn( NULL, own_fqdn);
+  GNUNET_free_non_null (own_fqdn);
 
   /*
    * Testing non-local DNS resolution
    * DNS rootserver to test: a.root-servers.net - 198.41.0.4
    */
 
 
   /*
    * Testing non-local DNS resolution
    * DNS rootserver to test: a.root-servers.net - 198.41.0.4
    */
 
-  char const * rootserver_name = ROOTSERVER_NAME;
+  const char * rootserver_name = ROOTSERVER_NAME;
   struct hostent *rootserver;
 
   rootserver = gethostbyname(rootserver_name);
   struct hostent *rootserver;
 
   rootserver = gethostbyname(rootserver_name);
@@ -254,7 +264,9 @@ run(void *cls, struct GNUNET_SCHEDULER_Handle *sched, char * const *args,
           _("gethostbyname() could not lookup IP address: %s\n"),
           hstrerror (h_errno));
 #endif
           _("gethostbyname() could not lookup IP address: %s\n"),
           hstrerror (h_errno));
 #endif
-      GNUNET_break(0);
+      fprintf (stderr,
+              "System seems to be off-line, will not run all DNS tests\n");
+      *ok = 0; /* mark test as passing anyway */
       return;
     }
 
       return;
     }
 
@@ -283,7 +295,7 @@ run(void *cls, struct GNUNET_SCHEDULER_Handle *sched, char * const *args,
 #endif
 
   /* Resolve the same using GNUNET */
 #endif
 
   /* Resolve the same using GNUNET */
-  GNUNET_RESOLVER_ip_get(sched, cfg, ROOTSERVER_NAME, AF_INET, timeout,
+  GNUNET_RESOLVER_ip_get(cfg, ROOTSERVER_NAME, AF_INET, timeout,
       &check_rootserver_ip, cls);
 
   /*
       &check_rootserver_ip, cls);
 
   /*
@@ -331,9 +343,12 @@ run(void *cls, struct GNUNET_SCHEDULER_Handle *sched, char * const *args,
 
   memset(&sa, 0, sizeof(sa));
   sa.sin_family = AF_INET;
 
   memset(&sa, 0, sizeof(sa));
   sa.sin_family = AF_INET;
+#ifndef MINGW
   inet_aton(ROOTSERVER_IP, &sa.sin_addr);
   inet_aton(ROOTSERVER_IP, &sa.sin_addr);
-
-  GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
+#else
+  sa.sin_addr.S_un.S_addr = inet_addr(ROOTSERVER_IP);
+#endif
+  GNUNET_RESOLVER_hostname_get(cfg, (const struct sockaddr *) &sa,
       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls);
 }
 
       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls);
 }
 
@@ -343,7 +358,7 @@ check()
   int ok = 1 + 2 + 4 + 8;
   char *fn;
   char *pfx;
   int ok = 1 + 2 + 4 + 8;
   char *fn;
   char *pfx;
-  pid_t pid;
+  struct GNUNET_OS_Process *proc;
   char * const argv[] =
     { "test-resolver-api", "-c", "test_resolver_api_data.conf",
 #if VERBOSE
   char * const argv[] =
     { "test-resolver-api", "-c", "test_resolver_api_data.conf",
 #if VERBOSE
@@ -355,20 +370,23 @@ check()
   pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
   GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
   GNUNET_free(pfx);
   pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
   GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
   GNUNET_free(pfx);
-  pid = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver",
+  proc = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver",
 #if VERBOSE
       "-L", "DEBUG",
 #endif
       "-c", "test_resolver_api_data.conf", NULL);
 #if VERBOSE
       "-L", "DEBUG",
 #endif
       "-c", "test_resolver_api_data.conf", NULL);
+  GNUNET_assert (NULL != proc);
   GNUNET_free(fn);
   GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *))
       - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok));
   GNUNET_free(fn);
   GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *))
       - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok));
-  if (0 != PLIBC_KILL(pid, SIGTERM))
+  if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
     {
       GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
       ok = 1;
     }
     {
       GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
       ok = 1;
     }
-  GNUNET_OS_process_wait(pid);
+  GNUNET_OS_process_wait (proc);
+  GNUNET_OS_process_close (proc);
+  proc = NULL;
   if (ok != 0)
     fprintf(stderr, "Missed some resolutions: %u\n", ok);
   return ok;
   if (ok != 0)
     fprintf(stderr, "Missed some resolutions: %u\n", ok);
   return ok;