added function GNUNET_RESOLVER_local_hostname_get to resolve local hostname
authorMatthias Wachs <wachs@net.in.tum.de>
Mon, 12 Apr 2010 10:07:22 +0000 (10:07 +0000)
committerMatthias Wachs <wachs@net.in.tum.de>
Mon, 12 Apr 2010 10:07:22 +0000 (10:07 +0000)
src/util/resolver_api.c
src/util/test_resolver_api.c

index a35f680d309ba6260d784a79c9c2e6fec07eec41..96396bfcccc7a61ade1c5c0cd65bc91cdecbe66f 100644 (file)
@@ -669,7 +669,39 @@ GNUNET_RESOLVER_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
 
 
 /**
- * Perform a reverse DNS lookup.
+ * Get local hostname
+ *
+ * @param sched scheduler to use
+ * @param cfg configuration to use
+ * @param callback function to call with addresses
+ * @param cls closure for callback
+ * @return handle that can be used to cancel the request, NULL on error
+ */
+void
+GNUNET_RESOLVER_local_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
+                                    const struct GNUNET_CONFIGURATION_Handle *cfg,
+                                    GNUNET_RESOLVER_HostnameCallback callback,
+                                    void *cls)
+{
+
+  char hostname[GNUNET_OS_get_hostname_max_length() + 1];
+
+  check_config (cfg);
+  if (0 != gethostname (hostname, sizeof (hostname) - 1))
+    {
+      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
+                           GNUNET_ERROR_TYPE_BULK, "gethostname");
+      return;
+    }
+#if DEBUG_RESOLVER
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              _("Resolving our hostname `%s'\n"), hostname);
+#endif
+  callback (cls, hostname);
+}
+
+/**
+ * Looking our own hostname.
  *
  * @param sched scheduler to use
  * @param cfg configuration to use
index 3b41477bd6357297ef1032e123f9ff9c9b65beae..c577222e16abafc21666974ee0faa303e6f29816 100644 (file)
@@ -127,6 +127,38 @@ check_127(void *cls, const struct sockaddr *sa, socklen_t salen)
     }
 }
 
+static void
+check_local_hostname(void *cls, const char *hostname)
+{
+  int result = 0;
+
+  char own_hostname[GNUNET_OS_get_hostname_max_length() + 1];
+#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);
+#endif
+
+  result = gethostname (own_hostname, sizeof (own_hostname) - 1);
+
+  if ( 0 != result )
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+        "Could not resolve local hostname\n", own_hostname);
+  }
+  GNUNET_assert( 0 == result);
+
+  result = strcmp(hostname, own_hostname);
+  if ( 0 != result )
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+        "Local resolved and resolver resolved hostnames are not equal\n", own_hostname);
+  }
+  GNUNET_assert( 0 == result);
+}
+
+
 static void
 check_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen)
 {
@@ -198,6 +230,13 @@ run(void *cls, struct GNUNET_SCHEDULER_Handle *sched, char * const *args,
       sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
   GNUNET_RESOLVER_hostname_resolve(sched, cfg, AF_UNSPEC, timeout,
       &check_hostname, cls);
+
+  /*
+   * Looking up our own hostname
+   */
+  GNUNET_RESOLVER_local_hostname_get(sched, cfg, &check_local_hostname, cls);
+
+
   /*
    * Testing non-local DNS resolution
    * DNS rootserver to test: a.root-servers.net - 198.41.0.4