2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file resolver/test_resolver_api.c
22 * @brief testcase for resolver_api.c
25 #include "gnunet_common.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_resolver_service.h"
33 #define VERBOSE GNUNET_NO
36 * Using DNS root servers to check gnunet's resolver service
37 * a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years
38 * For more information have a look at IANA's website http://www.root-servers.org/
40 #define ROOTSERVER_NAME "a.root-servers.net"
41 #define ROOTSERVER_IP "198.41.0.4"
44 check_hostname(void *cls, const struct sockaddr *sa, socklen_t salen)
53 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Got IP address `%s' for our host.\n"),
54 GNUNET_a2s(sa, salen));
59 check_localhost_num(void *cls, const char *hostname)
64 if (0 == strcmp(hostname, "127.0.0.1"))
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
68 "Received correct hostname `%s'.\n", hostname);
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76 "Received invalid hostname `%s'.\n", hostname);
84 check_localhost(void *cls, const char *hostname)
89 if (0 == strcmp(hostname, "localhost"))
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93 "Received correct hostname `%s'.\n", hostname);
99 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
100 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
106 check_127(void *cls, const struct sockaddr *sa, socklen_t salen)
109 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
113 GNUNET_assert(sizeof(struct sockaddr_in) == salen);
114 if (sai->sin_addr.s_addr == htonl(INADDR_LOOPBACK))
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
131 check_local_fqdn(void *cls, const char *gnunet_fqdn)
135 struct hostent *host;
136 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
138 if (0 != gethostname (hostname, sizeof (hostname) - 1))
140 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
141 GNUNET_ERROR_TYPE_BULK, "gethostname");
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 _("Resolving our FQDN `%s'\n"), hostname);
148 host = gethostbyname ( hostname );
151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152 _("Could not resolve our FQDN : %s %u\n"),
153 hstrerror (h_errno), h_errno);
157 GNUNET_assert( 0 != host);
159 result = strcmp(host->h_name, gnunet_fqdn);
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163 "Local resolved and resolver resolved fqdns are not equal\n");
165 GNUNET_assert( 0 == result);
171 check_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen)
174 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
178 GNUNET_assert(sizeof(struct sockaddr_in) == salen);
180 if (0 == strcmp(inet_ntoa(sai->sin_addr), ROOTSERVER_IP))
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct rootserver ip address.\n");
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect rootserver ip address.\n");
197 check_rootserver_name(void *cls, const char *hostname)
200 if (hostname == NULL)
203 if (0 == strcmp(hostname, ROOTSERVER_NAME))
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Received correct rootserver hostname `%s'.\n", hostname);
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Received invalid rootserver hostname `%s'.\n", hostname);
222 run(void *cls, char * const *args,
223 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
226 struct sockaddr_in sa;
227 struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply(
228 GNUNET_TIME_UNIT_SECONDS, 30);
232 memset(&sa, 0, sizeof(sa));
233 sa.sin_family = AF_INET;
234 #if HAVE_SOCKADDR_IN_SIN_LEN
235 sa.sin_len = (u_char) sizeof (sa);
237 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
240 * Looking up our own fqdn
242 own_fqdn = GNUNET_RESOLVER_local_fqdn_get();
243 check_local_fqdn( NULL, own_fqdn);
244 GNUNET_free_non_null (own_fqdn);
247 * Testing non-local DNS resolution
248 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
250 const char * rootserver_name = ROOTSERVER_NAME;
251 struct hostent *rootserver;
253 rootserver = gethostbyname(rootserver_name);
254 if (rootserver == NULL)
256 /* Error: resolving ip addresses does not work */
258 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
259 _("gethostbyname() could not lookup IP address: %s\n"),
260 hstrerror (h_errno));
263 "System seems to be off-line, will not run all DNS tests\n");
264 *ok = 0; /* mark test as passing anyway */
268 /* Counting returned IP addresses */
269 while (rootserver->h_addr_list[count_ips] != NULL)
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received range for root name server, but a root name server has only 1 IP\n");
279 /* Comparing to resolved address to the address the root name server should have */
280 if (strcmp(inet_ntoa(*(struct in_addr *) rootserver->h_addr_list[0]),
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received and IP for root name server differ\n");
289 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own forward name resolution is working\n");
292 /* Resolve the same using GNUNET */
293 GNUNET_RESOLVER_ip_get(ROOTSERVER_NAME, AF_INET, timeout,
294 &check_rootserver_ip, cls);
297 * Success: forward lookups work as expected
298 * Next step: reverse lookups
301 struct in_addr rootserver_addr;
302 rootserver->h_name = "";
303 if (1 != inet_pton(AF_INET, ROOTSERVER_IP, &rootserver_addr))
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not transform root name server IP address\n");
312 = gethostbyaddr(&rootserver_addr, sizeof(rootserver_addr), AF_INET);
313 if (rootserver == NULL)
315 /* Error: resolving IP addresses does not work */
317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
318 _("gethostbyaddr() could not lookup hostname: %s\n"),
319 hstrerror (h_errno));
325 if (0 != strcmp(rootserver->h_name, ROOTSERVER_NAME))
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received hostname and hostname for root name server differ\n");
335 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
336 "System's own reverse name resolution is working\n");
339 /* Resolve the same using GNUNET */
340 memset(&sa, 0, sizeof(sa));
341 sa.sin_family = AF_INET;
342 #if HAVE_SOCKADDR_IN_SIN_LEN
343 sa.sin_len = (u_char) sizeof (sa);
346 inet_aton(ROOTSERVER_IP, &sa.sin_addr);
348 sa.sin_addr.S_un.S_addr = inet_addr(ROOTSERVER_IP);
350 GNUNET_RESOLVER_hostname_get((const struct sockaddr *) &sa,
351 sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls);
353 memset(&sa, 0, sizeof(sa));
354 sa.sin_family = AF_INET;
355 #if HAVE_SOCKADDR_IN_SIN_LEN
356 sa.sin_len = (u_char) sizeof (sa);
358 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
360 GNUNET_RESOLVER_ip_get("localhost", AF_INET, timeout, &check_127,
362 fprintf (stderr, "Trying to get hostname for 127.0.0.1\n");
363 GNUNET_RESOLVER_hostname_get((const struct sockaddr *) &sa,
364 sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
366 GNUNET_RESOLVER_hostname_get((const struct sockaddr *) &sa,
367 sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
368 GNUNET_RESOLVER_hostname_resolve(AF_UNSPEC, timeout,
369 &check_hostname, cls);
376 int ok = 1 + 2 + 4 + 8;
379 struct GNUNET_OS_Process *proc;
380 char * const argv[] =
381 { "test-resolver-api", "-c", "test_resolver_api_data.conf",
386 struct GNUNET_GETOPT_CommandLineOption options[] =
387 { GNUNET_GETOPT_OPTION_END };
388 pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
389 GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
391 proc = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver",
395 "-c", "test_resolver_api_data.conf", NULL);
396 GNUNET_assert (NULL != proc);
398 GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *))
399 - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok));
400 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
402 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
405 GNUNET_OS_process_wait (proc);
406 GNUNET_OS_process_close (proc);
409 fprintf(stderr, "Missed some resolutions: %u\n", ok);
414 main(int argc, char *argv[])
418 GNUNET_log_setup("test-resolver-api",
430 /* end of test_resolver_api.c */