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
35 // Using dns rootservers to check gnunet's resolver service
36 // a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years
37 // For more information have a look at IANA's website http://www.root-servers.org/
38 #define ROOTSERVER_NAME "a.root-servers.net"
39 #define ROOTSERVER_IP "198.41.0.4"
42 check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen)
51 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
52 _("Got IP address `%s' for our host.\n"),
53 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_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
134 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
138 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
140 if ( 0 == strcmp(inet_ntoa(sai->sin_addr),ROOTSERVER_IP))
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct rootserver ip address.\n");
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect rootserver ip address.\n");
157 check_rootserver_name (void *cls, const char *hostname)
160 if (hostname == NULL)
163 if (0 == strcmp (hostname, ROOTSERVER_NAME))
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167 "Received correct rootserver hostname `%s'.\n", hostname);
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Received invalid rootserver hostname `%s'.\n", hostname);
183 struct GNUNET_SCHEDULER_Handle *sched,
185 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
187 struct sockaddr_in sa;
188 struct GNUNET_TIME_Relative timeout =
189 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
191 memset (&sa, 0, sizeof (sa));
192 sa.sin_family = AF_INET;
193 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
194 GNUNET_RESOLVER_ip_get (sched,
196 "localhost", AF_INET, timeout, &check_127, cls);
197 GNUNET_RESOLVER_hostname_get (sched,
199 (const struct sockaddr *) &sa,
200 sizeof (struct sockaddr),
201 GNUNET_YES, timeout, &check_localhost, cls);
202 GNUNET_RESOLVER_hostname_get (sched,
204 (const struct sockaddr *) &sa,
205 sizeof (struct sockaddr),
207 timeout, &check_localhost_num, cls);
208 GNUNET_RESOLVER_hostname_resolve (sched,
210 AF_UNSPEC, timeout, &check_hostname, cls);
211 // Testing non-local dns resolution
212 // DNS Rootserver to test: a.root-servers.net - 198.41.0.4
214 char const * rootserver_name = ROOTSERVER_NAME;
216 struct hostent *rootserver;
218 rootserver = gethostbyname(rootserver_name);
219 if (rootserver == NULL)
221 // Error: resolving ip addresses does not work
226 case HOST_NOT_FOUND: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyname() could not lookup ip address: HOST_NOT_FOUND\n");break;
227 case NO_ADDRESS: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyname() could not lookup ip address: NO_ADDRESS\n");break;
228 case NO_RECOVERY: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyname() could not lookup ip address: NO_RECOVERY\n");break;
229 case TRY_AGAIN: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyname() could not lookup ip address: TRY_AGAIN\n");break;
236 // Counting returned ip addresses
238 while (rootserver->h_addr_list[count_ips]!=NULL)
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ip range for root name server, but a root nameserver has only 1 ip\n");
250 // Comparing to resolved address to the address the root nameserver should have
251 if ( strcmp(inet_ntoa( *(struct in_addr *) rootserver->h_addr_list[0]),ROOTSERVER_IP) !=0)
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ip and ip for root name server differ\n");
260 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own forward name resolution is working\n");
263 // Resolve the same using GNUNET
264 GNUNET_RESOLVER_ip_get (sched, cfg, ROOTSERVER_NAME, AF_INET, timeout, &check_rootserver_ip, cls);
266 // Success: forward lookups work as exptected
269 // Next step: reverse lookups
271 struct in_addr rootserver_addr;
272 rootserver->h_name="";
273 if ( 1 != inet_pton(AF_INET, ROOTSERVER_IP, &rootserver_addr))
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not transform root nameserver ip addressr\n");
281 rootserver = gethostbyaddr(&rootserver_addr, sizeof(rootserver_addr), AF_INET);
282 if (rootserver == NULL)
284 // Error: resolving ip addresses does not work
289 case HOST_NOT_FOUND: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: HOST_NOT_FOUND\n");break;
290 case NO_ADDRESS: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: NO_ADDRESS\n");break;
291 case NO_RECOVERY: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: NO_RECOVERY\n");break;
292 case TRY_AGAIN: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: TRY_AGAIN\n");break;
298 if ( 0 != strcmp( rootserver->h_name,ROOTSERVER_NAME))
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received hostname and hostname for root name server differ\n");
307 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own reverse name resolution is working\n");
309 // Resolve the same using GNUNET
311 memset (&sa, 0, sizeof (sa));
312 sa.sin_family = AF_INET;
313 inet_aton(ROOTSERVER_IP, &sa.sin_addr);
315 GNUNET_RESOLVER_hostname_get (sched,
317 (const struct sockaddr *) &sa,
318 sizeof (struct sockaddr),
320 timeout, &check_rootserver_name, cls);
322 // Success: reverse lookups work as exptected
329 int ok = 1 + 2 + 4 + 8;
333 char *const argv[] = { "test-resolver-api",
335 "test_resolver_api_data.conf",
341 struct GNUNET_GETOPT_CommandLineOption options[] = {
342 GNUNET_GETOPT_OPTION_END
344 pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
345 GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver",
349 pid = GNUNET_OS_start_process (NULL, NULL, fn,
350 "gnunet-service-resolver",
354 "-c", "test_resolver_api_data.conf", NULL);
356 GNUNET_assert (GNUNET_OK == GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
357 argv, "test-resolver-api", "nohelp",
358 options, &run, &ok));
359 if (0 != PLIBC_KILL (pid, SIGTERM))
361 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
364 GNUNET_OS_process_wait (pid);
366 fprintf (stderr, "Missed some resolutions: %u\n", ok);
371 main (int argc, char *argv[])
375 GNUNET_log_setup ("test-resolver-api",
387 /* end of test_resolver_api.c */