added function GNUNET_RESOLVER_local_hostname_get to resolve local hostname
[oweals/gnunet.git] / src / util / test_resolver_api.c
1 /*
2  This file is part of GNUnet.
3  (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20 /**
21  * @file resolver/test_resolver_api.c
22  * @brief testcase for resolver_api.c
23  */
24 #include "platform.h"
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"
31 #include "resolver.h"
32
33 #define VERBOSE GNUNET_NO
34
35 /** 
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/
39  */
40 #define ROOTSERVER_NAME "a.root-servers.net"
41 #define ROOTSERVER_IP   "198.41.0.4"
42
43 static void
44 check_hostname(void *cls, const struct sockaddr *sa, socklen_t salen)
45 {
46   int *ok = cls;
47
48   if (salen == 0)
49     {
50       (*ok) &= ~8;
51       return;
52     }
53   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Got IP address `%s' for our host.\n"),
54       GNUNET_a2s(sa, salen));
55 }
56
57 static void
58 check_localhost_num(void *cls, const char *hostname)
59 {
60   int *ok = cls;
61   if (hostname == NULL)
62     return;
63   if (0 == strcmp(hostname, "127.0.0.1"))
64     {
65 #if DEBUG_RESOLVER
66       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67           "Received correct hostname `%s'.\n", hostname);
68 #endif
69       (*ok) &= ~4;
70     }
71   else
72     {
73 #if DEBUG_RESOLVER
74       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75           "Received invalid hostname `%s'.\n", hostname);
76 #endif
77       GNUNET_break(0);
78     }
79 }
80
81 static void
82 check_localhost(void *cls, const char *hostname)
83 {
84   int *ok = cls;
85   if (hostname == NULL)
86     return;
87   if (0 == strcmp(hostname, "localhost"))
88     {
89 #if DEBUG_RESOLVER
90       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91           "Received correct hostname `%s'.\n", hostname);
92 #endif
93       (*ok) &= ~2;
94     }
95   else
96     {
97 #if DEBUG_RESOLVER
98       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99           "Received invalid hostname `%s'.\n", hostname);
100 #endif
101       GNUNET_break(0);
102     }
103 }
104
105 static void
106 check_127(void *cls, const struct sockaddr *sa, socklen_t salen)
107 {
108   int *ok = cls;
109   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
110
111   if (sa == NULL)
112     return;
113   GNUNET_assert(sizeof(struct sockaddr_in) == salen);
114   if (sai->sin_addr.s_addr == htonl(INADDR_LOOPBACK))
115     {
116 #if DEBUG_RESOLVER
117       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
118 #endif
119       (*ok) &= ~1;
120     }
121   else
122     {
123 #if DEBUG_RESOLVER
124       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
125 #endif
126       GNUNET_break(0);
127     }
128 }
129
130 static void
131 check_local_hostname(void *cls, const char *hostname)
132 {
133   int result = 0;
134
135   char own_hostname[GNUNET_OS_get_hostname_max_length() + 1];
136 #if DEBUG_RESOLVER
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);
141 #endif
142
143   result = gethostname (own_hostname, sizeof (own_hostname) - 1);
144
145   if ( 0 != result )
146   {
147     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148         "Could not resolve local hostname\n", own_hostname);
149   }
150   GNUNET_assert( 0 == result);
151
152   result = strcmp(hostname, own_hostname);
153   if ( 0 != result )
154   {
155     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156         "Local resolved and resolver resolved hostnames are not equal\n", own_hostname);
157   }
158   GNUNET_assert( 0 == result);
159 }
160
161
162 static void
163 check_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen)
164 {
165   int *ok = cls;
166   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
167
168   if (sa == NULL)
169     return;
170   GNUNET_assert(sizeof(struct sockaddr_in) == salen);
171
172   if (0 == strcmp(inet_ntoa(sai->sin_addr), ROOTSERVER_IP))
173     {
174 #if DEBUG_RESOLVER
175       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct rootserver ip address.\n");
176 #endif
177       (*ok) &= ~1;
178     }
179   else
180     {
181 #if DEBUG_RESOLVER
182       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect rootserver ip address.\n");
183 #endif
184       GNUNET_break(0);
185     }
186 }
187
188 static void
189 check_rootserver_name(void *cls, const char *hostname)
190 {
191   int *ok = cls;
192   if (hostname == NULL)
193     return;
194
195   if (0 == strcmp(hostname, ROOTSERVER_NAME))
196     {
197 #if DEBUG_RESOLVER
198       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199           "Received correct rootserver hostname `%s'.\n", hostname);
200 #endif
201       (*ok) &= ~2;
202     }
203   else
204     {
205 #if DEBUG_RESOLVER
206       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207           "Received invalid rootserver hostname `%s'.\n", hostname);
208 #endif
209       GNUNET_break(0);
210     }
211 }
212
213 static void
214 run(void *cls, struct GNUNET_SCHEDULER_Handle *sched, char * const *args,
215     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
216 {
217   struct sockaddr_in sa;
218   struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply(
219       GNUNET_TIME_UNIT_MILLISECONDS, 2500);
220   int count_ips = 0;
221
222   memset(&sa, 0, sizeof(sa));
223   sa.sin_family = AF_INET;
224   sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
225   GNUNET_RESOLVER_ip_get(sched, cfg, "localhost", AF_INET, timeout, &check_127,
226       cls);
227   GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
228       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
229   GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
230       sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
231   GNUNET_RESOLVER_hostname_resolve(sched, cfg, AF_UNSPEC, timeout,
232       &check_hostname, cls);
233
234   /*
235    * Looking up our own hostname
236    */
237   GNUNET_RESOLVER_local_hostname_get(sched, cfg, &check_local_hostname, cls);
238
239
240   /*
241    * Testing non-local DNS resolution
242    * DNS rootserver to test: a.root-servers.net - 198.41.0.4
243    */
244
245   char const * rootserver_name = ROOTSERVER_NAME;
246   struct hostent *rootserver;
247
248   rootserver = gethostbyname(rootserver_name);
249   if (rootserver == NULL)
250     {
251       /* Error: resolving ip addresses does not work */
252 #if DEBUG_RESOLVER
253       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
254           _("gethostbyname() could not lookup IP address: %s\n"),
255           hstrerror (h_errno));
256 #endif
257       GNUNET_break(0);
258       return;
259     }
260
261   /* Counting returned IP addresses */
262   while (rootserver->h_addr_list[count_ips] != NULL)
263     count_ips++;
264   if (count_ips > 1)
265     {
266 #if DEBUG_RESOLVER
267       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received range for root name server, but a root name server has only 1 IP\n");
268 #endif
269       GNUNET_break(0);
270     }
271
272   /* Comparing to resolved address to the address the root name server should have */
273   if (strcmp(inet_ntoa(*(struct in_addr *) rootserver->h_addr_list[0]),
274       ROOTSERVER_IP) != 0)
275     {
276 #if DEBUG_RESOLVER
277       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received and IP for root name server differ\n");
278 #endif
279       GNUNET_break(0);
280     }
281 #if DEBUG_RESOLVER
282   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own forward name resolution is working\n");
283 #endif
284
285   /* Resolve the same using GNUNET */
286   GNUNET_RESOLVER_ip_get(sched, cfg, ROOTSERVER_NAME, AF_INET, timeout,
287       &check_rootserver_ip, cls);
288
289   /*
290    * Success: forward lookups work as expected
291    * Next step: reverse lookups
292    */
293
294   struct in_addr rootserver_addr;
295   rootserver->h_name = "";
296   if (1 != inet_pton(AF_INET, ROOTSERVER_IP, &rootserver_addr))
297     {
298 #if DEBUG_RESOLVER
299       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not transform root name server IP address\n");
300 #endif
301       GNUNET_break(0);
302     }
303
304   rootserver
305       = gethostbyaddr(&rootserver_addr, sizeof(rootserver_addr), AF_INET);
306   if (rootserver == NULL)
307     {
308       /* Error: resolving IP addresses does not work */
309 #if DEBUG_RESOLVER
310       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
311           _("gethostbyaddr() could not lookup hostname: %s\n"),
312           hstrerror (h_errno));
313 #endif
314       GNUNET_break(0);
315     }
316   else
317   {
318     if (0 != strcmp(rootserver->h_name, ROOTSERVER_NAME))
319     {
320 #if DEBUG_RESOLVER
321       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received hostname and hostname for root name server differ\n");
322 #endif
323       GNUNET_break(0);
324     }
325   }
326
327 #if DEBUG_RESOLVER
328   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own reverse name resolution is working\n");
329 #endif
330   /* Resolve the same using GNUNET */
331
332   memset(&sa, 0, sizeof(sa));
333   sa.sin_family = AF_INET;
334   inet_aton(ROOTSERVER_IP, &sa.sin_addr);
335
336   GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
337       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls);
338 }
339
340 static int
341 check()
342 {
343   int ok = 1 + 2 + 4 + 8;
344   char *fn;
345   char *pfx;
346   pid_t pid;
347   char * const argv[] =
348     { "test-resolver-api", "-c", "test_resolver_api_data.conf",
349 #if VERBOSE
350         "-L", "DEBUG",
351 #endif
352         NULL };
353   struct GNUNET_GETOPT_CommandLineOption options[] =
354     { GNUNET_GETOPT_OPTION_END };
355   pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
356   GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
357   GNUNET_free(pfx);
358   pid = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver",
359 #if VERBOSE
360       "-L", "DEBUG",
361 #endif
362       "-c", "test_resolver_api_data.conf", NULL);
363   GNUNET_free(fn);
364   GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *))
365       - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok));
366   if (0 != PLIBC_KILL(pid, SIGTERM))
367     {
368       GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
369       ok = 1;
370     }
371   GNUNET_OS_process_wait(pid);
372   if (ok != 0)
373     fprintf(stderr, "Missed some resolutions: %u\n", ok);
374   return ok;
375 }
376
377 int
378 main(int argc, char *argv[])
379 {
380   int ret;
381
382   GNUNET_log_setup("test-resolver-api",
383 #if VERBOSE
384       "DEBUG",
385 #else
386       "WARNING",
387 #endif
388       NULL);
389   ret = check();
390
391   return ret;
392 }
393
394 /* end of test_resolver_api.c */