paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / util / test_resolver_api.c
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009 GNUnet e.V.
4
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14
15  You should have received a copy of the GNU Affero General Public License
16  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /**
19  * @file resolver/test_resolver_api.c
20  * @brief testcase for resolver_api.c
21  */
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_resolver_service.h"
25 #include "resolver.h"
26
27
28 static int disable_rootserver_check;
29
30
31 /**
32  * Using DNS root servers to check gnunet's resolver service
33  * a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years
34  * For more information have a look at IANA's website http://www.root-servers.org/
35  */
36 #define ROOTSERVER_NAME "a.root-servers.net"
37 #define ROOTSERVER_IP   "198.41.0.4"
38
39
40 static void
41 check_hostname (void *cls,
42                 const struct sockaddr *sa,
43                 socklen_t salen)
44 {
45   int *ok = cls;
46
47   if (0 == salen)
48   {
49     (*ok) &= ~8;
50     return;
51   }
52   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
53               "Got IP address `%s' for our host.\n",
54               GNUNET_a2s (sa, salen));
55 }
56
57
58 static void
59 check_localhost_num (void *cls,
60                      const char *hostname)
61 {
62   int *ok = cls;
63
64   if (hostname == NULL)
65     return;
66   if (0 == strcmp (hostname, "127.0.0.1"))
67   {
68     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
69                 "Received correct hostname `%s'.\n",
70                 hostname);
71     (*ok) &= ~4;
72   }
73   else
74   {
75     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
76                 "Received invalid hostname `%s'.\n",
77                 hostname);
78     GNUNET_break (0);
79   }
80 }
81
82
83 static void
84 check_localhost (void *cls,
85                  const char *hostname)
86 {
87   int *ok = cls;
88
89   if (NULL == hostname)
90     return;
91   if (0 == strcmp (hostname, "localhost"))
92   {
93     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94                 "Received correct hostname `%s'.\n",
95                 hostname);
96     (*ok) &= ~2;
97   }
98   else
99   {
100     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
101                 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
102                 hostname);
103   }
104 }
105
106
107 static void
108 check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
109 {
110   int *ok = cls;
111   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
112
113   if (NULL == sa)
114     return;
115   GNUNET_assert (sizeof (struct sockaddr_in) == salen);
116   if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
117   {
118     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119                 "Received correct address.\n");
120     (*ok) &= ~1;
121   }
122   else
123   {
124     char buf[INET_ADDRSTRLEN];
125
126     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127                 "Received incorrect address `%s'.\n",
128                 inet_ntop (AF_INET,
129                            &sai->sin_addr,
130                            buf,
131                            sizeof (buf)));
132     GNUNET_break (0);
133   }
134 }
135
136
137 static void
138 check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
139 {
140   int *ok = cls;
141   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
142
143   if (NULL == sa)
144     return;
145   GNUNET_assert (sizeof (struct sockaddr_in) == salen);
146
147   if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
148   {
149     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150                 "Received correct rootserver ip address.\n");
151     (*ok) &= ~1;
152   }
153   else
154   {
155     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156                 "Received incorrect rootserver ip address.\n");
157     GNUNET_break (0);
158   }
159 }
160
161
162 static void
163 check_rootserver_name (void *cls,
164                        const char *hostname)
165 {
166   int *ok = cls;
167
168   if (NULL == hostname)
169     return;
170
171   if (0 == strcmp (hostname, ROOTSERVER_NAME))
172   {
173     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174                 "Received correct rootserver hostname `%s'.\n",
175                 hostname);
176     (*ok) &= ~2;
177   }
178   else
179   {
180     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
181                 "Received invalid rootserver hostname `%s', expected `%s'\n",
182                 hostname,
183                 ROOTSERVER_NAME);
184     GNUNET_break (disable_rootserver_check);
185   }
186 }
187
188
189 static void
190 run (void *cls, char *const *args, const char *cfgfile,
191      const struct GNUNET_CONFIGURATION_Handle *cfg)
192 {
193   int *ok = cls;
194   struct sockaddr_in sa;
195   struct GNUNET_TIME_Relative timeout =
196       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
197   int count_ips = 0;
198   char *own_fqdn;
199   const char *rootserver_name = ROOTSERVER_NAME;
200   struct hostent *rootserver;
201   struct in_addr rootserver_addr;
202
203   memset (&sa, 0, sizeof (sa));
204   sa.sin_family = AF_INET;
205 #if HAVE_SOCKADDR_IN_SIN_LEN
206   sa.sin_len = (u_char) sizeof (sa);
207 #endif
208   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
209
210   /*
211    * Looking up our own fqdn
212    */
213   own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
214   /* can't really check, only thing we can safely
215      compare against is our own identical logic... */
216   GNUNET_free_non_null (own_fqdn);
217
218   /*
219    * Testing non-local DNS resolution
220    * DNS rootserver to test: a.root-servers.net - 198.41.0.4
221    */
222
223   rootserver = gethostbyname (rootserver_name);
224   if (NULL == rootserver)
225   {
226     /* Error: resolving ip addresses does not work */
227     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228                 _("gethostbyname() could not lookup IP address: %s\n"),
229                 hstrerror (h_errno));
230     FPRINTF (stderr,
231              "%s",
232              "System seems to be off-line, will not run all DNS tests\n");
233     *ok = 0;                    /* mark test as passing anyway */
234     return;
235   }
236
237   /* Counting returned IP addresses */
238   while (NULL != rootserver->h_addr_list[count_ips])
239     count_ips++;
240   if (count_ips > 1)
241   {
242     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243                 "IP received range for root name server, but a root name server has only 1 IP\n");
244     GNUNET_break (0);
245   }
246
247   /* Comparing to resolved address to the address the root name server should have */
248   if (0 !=
249       strcmp (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
250               ROOTSERVER_IP))
251   {
252     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253                 "IP received and IP for root name server differ\n");
254     GNUNET_break (0);
255   }
256   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
257               "System's own forward name resolution is working\n");
258   /* Resolve the same using GNUNET */
259   GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
260                           &check_rootserver_ip, cls);
261
262   /*
263    * Success: forward lookups work as expected
264    * Next step: reverse lookups
265    */
266   if (1 != inet_pton (AF_INET,
267                       ROOTSERVER_IP,
268                       &rootserver_addr))
269   {
270     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271                 "Could not transform root name server IP address\n");
272     GNUNET_break (0);
273   }
274
275   rootserver =
276       gethostbyaddr ((const void *) &rootserver_addr,
277                      sizeof (rootserver_addr),
278                      AF_INET);
279   if (NULL == rootserver)
280   {
281     /* Error: resolving IP addresses does not work */
282     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
283                 "gethostbyaddr() could not lookup hostname: %s\n",
284                 hstrerror (h_errno));
285     disable_rootserver_check = GNUNET_YES;
286   }
287   else
288   {
289     if (0 != strcmp (rootserver->h_name,
290                      ROOTSERVER_NAME))
291     {
292       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
293                   "Received hostname and hostname for root name server differ\n");
294       disable_rootserver_check = GNUNET_YES;
295     }
296   }
297
298   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
299               "System's own reverse name resolution is working\n");
300   /* Resolve the same using GNUNET */
301   memset (&sa, 0, sizeof (sa));
302   sa.sin_family = AF_INET;
303 #if HAVE_SOCKADDR_IN_SIN_LEN
304   sa.sin_len = (u_char) sizeof (sa);
305 #endif
306 #ifndef MINGW
307   inet_aton (ROOTSERVER_IP, &sa.sin_addr);
308 #else
309   sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP);
310 #endif
311   GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
312                                 sizeof (struct sockaddr), GNUNET_YES, timeout,
313                                 &check_rootserver_name, cls);
314
315   memset (&sa, 0, sizeof (sa));
316   sa.sin_family = AF_INET;
317 #if HAVE_SOCKADDR_IN_SIN_LEN
318   sa.sin_len = (u_char) sizeof (sa);
319 #endif
320   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
321
322   GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
323   GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
324                                 sizeof (struct sockaddr), GNUNET_YES, timeout,
325                                 &check_localhost, cls);
326
327   GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
328                                 sizeof (struct sockaddr), GNUNET_NO, timeout,
329                                 &check_localhost_num, cls);
330   GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
331
332 }
333
334
335 int
336 main (int argc, char *argv[])
337 {
338   int ok = 1 + 2 + 4 + 8;
339   char *fn;
340   struct GNUNET_OS_Process *proc;
341   char *const argvx[] = {
342     "test-resolver-api", "-c", "test_resolver_api_data.conf", NULL
343   };
344   struct GNUNET_GETOPT_CommandLineOption options[] =
345       { GNUNET_GETOPT_OPTION_END };
346
347   GNUNET_log_setup ("test-resolver-api",
348                     "WARNING",
349                     NULL);
350   fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
351   proc = GNUNET_OS_start_process (GNUNET_YES,
352                                   GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
353                                   NULL, NULL, NULL,
354                                   fn,
355                                   "gnunet-service-resolver",
356                                   "-c", "test_resolver_api_data.conf", NULL);
357   GNUNET_assert (NULL != proc);
358   GNUNET_free (fn);
359   GNUNET_assert (GNUNET_OK ==
360                  GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
361                                      argvx, "test-resolver-api", "nohelp",
362                                      options, &run, &ok));
363   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
364   {
365     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
366     ok = 1;
367   }
368   GNUNET_OS_process_wait (proc);
369   GNUNET_OS_process_destroy (proc);
370   proc = NULL;
371   if (0 != ok)
372     FPRINTF (stderr, "Missed some resolutions: %u\n", ok);
373   return ok;
374 }
375
376
377 /* end of test_resolver_api.c */