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 rootservers 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,
54 _("Got IP address `%s' for our host.\n"),
55 GNUNET_a2s (sa, salen));
60 check_localhost_num (void *cls, const char *hostname)
65 if (0 == strcmp (hostname, "127.0.0.1"))
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
69 "Received correct hostname `%s'.\n", hostname);
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
77 "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);
100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
101 "Received invalid hostname `%s'.\n", hostname);
108 check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
111 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
115 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
116 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
133 check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
136 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
140 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
142 if ( 0 == strcmp(inet_ntoa(sai->sin_addr),ROOTSERVER_IP))
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct rootserver ip address.\n");
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect rootserver ip address.\n");
159 check_rootserver_name (void *cls, const char *hostname)
162 if (hostname == NULL)
165 if (0 == strcmp (hostname, ROOTSERVER_NAME))
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Received correct rootserver hostname `%s'.\n", hostname);
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "Received invalid rootserver hostname `%s'.\n", hostname);
185 struct GNUNET_SCHEDULER_Handle *sched,
187 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
189 struct sockaddr_in sa;
190 struct GNUNET_TIME_Relative timeout =
191 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
193 memset (&sa, 0, sizeof (sa));
194 sa.sin_family = AF_INET;
195 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
196 GNUNET_RESOLVER_ip_get (sched,
198 "localhost", AF_INET, timeout, &check_127, cls);
199 GNUNET_RESOLVER_hostname_get (sched,
201 (const struct sockaddr *) &sa,
202 sizeof (struct sockaddr),
203 GNUNET_YES, timeout, &check_localhost, cls);
204 GNUNET_RESOLVER_hostname_get (sched,
206 (const struct sockaddr *) &sa,
207 sizeof (struct sockaddr),
209 timeout, &check_localhost_num, cls);
210 GNUNET_RESOLVER_hostname_resolve (sched,
212 AF_UNSPEC, timeout, &check_hostname, cls);
213 // Testing non-local dns resolution
214 // DNS Rootserver to test: a.root-servers.net - 198.41.0.4
216 char const * rootserver_name = ROOTSERVER_NAME;
218 struct hostent *rootserver;
220 rootserver = gethostbyname(rootserver_name);
221 if (rootserver == NULL)
223 // Error: resolving ip addresses does not work
225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
226 _("gethostbyname() could not lookup IP address: %s\n"),
227 hstrerror (h_errno));
232 // Counting returned ip addresses
234 while (rootserver->h_addr_list[count_ips]!=NULL)
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ip range for root name server, but a root nameserver has only 1 ip\n");
246 // Comparing to resolved address to the address the root nameserver should have
247 if ( strcmp(inet_ntoa( *(struct in_addr *) rootserver->h_addr_list[0]),ROOTSERVER_IP) !=0)
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ip and ip for root name server differ\n");
256 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own forward name resolution is working\n");
259 // Resolve the same using GNUNET
260 GNUNET_RESOLVER_ip_get (sched, cfg, ROOTSERVER_NAME, AF_INET, timeout, &check_rootserver_ip, cls);
262 // Success: forward lookups work as exptected
265 // Next step: reverse lookups
267 struct in_addr rootserver_addr;
268 rootserver->h_name="";
269 if ( 1 != inet_pton(AF_INET, ROOTSERVER_IP, &rootserver_addr))
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not transform root nameserver ip addressr\n");
277 rootserver = gethostbyaddr(&rootserver_addr, sizeof(rootserver_addr), AF_INET);
278 if (rootserver == NULL)
280 // Error: resolving ip addresses does not work
285 case HOST_NOT_FOUND: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: HOST_NOT_FOUND\n");break;
286 case NO_ADDRESS: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: NO_ADDRESS\n");break;
287 case NO_RECOVERY: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: NO_RECOVERY\n");break;
288 case TRY_AGAIN: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: TRY_AGAIN\n");break;
294 if ( 0 != strcmp( rootserver->h_name,ROOTSERVER_NAME))
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received hostname and hostname for root name server differ\n");
303 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own reverse name resolution is working\n");
305 // Resolve the same using GNUNET
307 memset (&sa, 0, sizeof (sa));
308 sa.sin_family = AF_INET;
309 inet_aton(ROOTSERVER_IP, &sa.sin_addr);
311 GNUNET_RESOLVER_hostname_get (sched,
313 (const struct sockaddr *) &sa,
314 sizeof (struct sockaddr),
316 timeout, &check_rootserver_name, cls);
324 int ok = 1 + 2 + 4 + 8;
328 char *const argv[] = { "test-resolver-api",
330 "test_resolver_api_data.conf",
336 struct GNUNET_GETOPT_CommandLineOption options[] = {
337 GNUNET_GETOPT_OPTION_END
339 pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
340 GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver",
344 pid = GNUNET_OS_start_process (NULL, NULL, fn,
345 "gnunet-service-resolver",
349 "-c", "test_resolver_api_data.conf", NULL);
351 GNUNET_assert (GNUNET_OK == GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
352 argv, "test-resolver-api", "nohelp",
353 options, &run, &ok));
354 if (0 != PLIBC_KILL (pid, SIGTERM))
356 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
359 GNUNET_OS_process_wait (pid);
361 fprintf (stderr, "Missed some resolutions: %u\n", ok);
366 main (int argc, char *argv[])
370 GNUNET_log_setup ("test-resolver-api",
382 /* end of test_resolver_api.c */