From dca2e627584720657437f519f7d12a5334acc2cc Mon Sep 17 00:00:00 2001 From: Matthias Wachs Date: Mon, 12 Apr 2010 10:07:22 +0000 Subject: [PATCH] added function GNUNET_RESOLVER_local_hostname_get to resolve local hostname --- src/util/resolver_api.c | 34 ++++++++++++++++++++++++++++++- src/util/test_resolver_api.c | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/util/resolver_api.c b/src/util/resolver_api.c index a35f680d3..96396bfcc 100644 --- a/src/util/resolver_api.c +++ b/src/util/resolver_api.c @@ -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 diff --git a/src/util/test_resolver_api.c b/src/util/test_resolver_api.c index 3b41477bd..c577222e1 100644 --- a/src/util/test_resolver_api.c +++ b/src/util/test_resolver_api.c @@ -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 -- 2.25.1