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 2, 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));
58 check_localhost_num(void *cls, const char *hostname)
63 if (0 == strcmp(hostname, "127.0.0.1"))
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67 "Received correct hostname `%s'.\n", hostname);
74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75 "Received invalid hostname `%s'.\n", hostname);
82 check_localhost(void *cls, const char *hostname)
87 if (0 == strcmp(hostname, "localhost"))
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 "Received correct hostname `%s'.\n", hostname);
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99 "Received invalid hostname `%s'.\n", hostname);
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_hostname(void *cls, const char *hostname)
135 char own_hostname[GNUNET_OS_get_hostname_max_length() + 1];
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 "Hostname resolved here is `%s'.\n", own_hostname);
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140 "Hostname resolved using resolver is `%s'.\n", hostname);
143 result = gethostname (own_hostname, sizeof (own_hostname) - 1);
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148 "Could not resolve local hostname\n", own_hostname);
150 GNUNET_assert( 0 == result);
152 result = strcmp(hostname, own_hostname);
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156 "Local resolved and resolver resolved hostnames are not equal\n", own_hostname);
158 GNUNET_assert( 0 == result);
163 check_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen)
166 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
170 GNUNET_assert(sizeof(struct sockaddr_in) == salen);
172 if (0 == strcmp(inet_ntoa(sai->sin_addr), ROOTSERVER_IP))
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct rootserver ip address.\n");
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect rootserver ip address.\n");
189 check_rootserver_name(void *cls, const char *hostname)
192 if (hostname == NULL)
195 if (0 == strcmp(hostname, ROOTSERVER_NAME))
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "Received correct rootserver hostname `%s'.\n", hostname);
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Received invalid rootserver hostname `%s'.\n", hostname);
214 run(void *cls, struct GNUNET_SCHEDULER_Handle *sched, char * const *args,
215 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
217 struct sockaddr_in sa;
218 struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply(
219 GNUNET_TIME_UNIT_MILLISECONDS, 2500);
223 memset(&sa, 0, sizeof(sa));
224 sa.sin_family = AF_INET;
225 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
226 GNUNET_RESOLVER_ip_get(sched, cfg, "localhost", AF_INET, timeout, &check_127,
228 GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
229 sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
230 GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
231 sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
232 GNUNET_RESOLVER_hostname_resolve(sched, cfg, AF_UNSPEC, timeout,
233 &check_hostname, cls);
236 * Looking up our own hostname
238 own_hostname = GNUNET_RESOLVER_local_hostname_get();
239 check_local_hostname( NULL, own_hostname);
240 GNUNET_free (own_hostname);
243 * Testing non-local DNS resolution
244 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
247 char const * rootserver_name = ROOTSERVER_NAME;
248 struct hostent *rootserver;
250 rootserver = gethostbyname(rootserver_name);
251 if (rootserver == NULL)
253 /* Error: resolving ip addresses does not work */
255 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
256 _("gethostbyname() could not lookup IP address: %s\n"),
257 hstrerror (h_errno));
263 /* Counting returned IP addresses */
264 while (rootserver->h_addr_list[count_ips] != NULL)
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received range for root name server, but a root name server has only 1 IP\n");
274 /* Comparing to resolved address to the address the root name server should have */
275 if (strcmp(inet_ntoa(*(struct in_addr *) rootserver->h_addr_list[0]),
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received and IP for root name server differ\n");
284 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own forward name resolution is working\n");
287 /* Resolve the same using GNUNET */
288 GNUNET_RESOLVER_ip_get(sched, cfg, ROOTSERVER_NAME, AF_INET, timeout,
289 &check_rootserver_ip, cls);
292 * Success: forward lookups work as expected
293 * Next step: reverse lookups
296 struct in_addr rootserver_addr;
297 rootserver->h_name = "";
298 if (1 != inet_pton(AF_INET, ROOTSERVER_IP, &rootserver_addr))
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not transform root name server IP address\n");
307 = gethostbyaddr(&rootserver_addr, sizeof(rootserver_addr), AF_INET);
308 if (rootserver == NULL)
310 /* Error: resolving IP addresses does not work */
312 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
313 _("gethostbyaddr() could not lookup hostname: %s\n"),
314 hstrerror (h_errno));
320 if (0 != strcmp(rootserver->h_name, ROOTSERVER_NAME))
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received hostname and hostname for root name server differ\n");
330 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own reverse name resolution is working\n");
332 /* Resolve the same using GNUNET */
334 memset(&sa, 0, sizeof(sa));
335 sa.sin_family = AF_INET;
336 sa.sin_addr.S_un.S_addr = inet_addr(ROOTSERVER_IP);
338 GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
339 sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls);
345 int ok = 1 + 2 + 4 + 8;
349 char * const argv[] =
350 { "test-resolver-api", "-c", "test_resolver_api_data.conf",
355 struct GNUNET_GETOPT_CommandLineOption options[] =
356 { GNUNET_GETOPT_OPTION_END };
357 pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
358 GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
360 pid = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver",
364 "-c", "test_resolver_api_data.conf", NULL);
366 GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *))
367 - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok));
368 if (0 != PLIBC_KILL(pid, SIGTERM))
370 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
373 GNUNET_OS_process_wait(pid);
375 fprintf(stderr, "Missed some resolutions: %u\n", ok);
380 main(int argc, char *argv[])
384 GNUNET_log_setup("test-resolver-api",
396 /* end of test_resolver_api.c */