cbd8392bfff8b212061ed54ff745752c96b808e0
[oweals/gnunet.git] / src / util / 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 /**
22  * @file util/resolver_api.c
23  * @brief resolver for writing a tool
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_resolver_service.h"
31 #include "gnunet_server_lib.h"
32 #include "resolver.h"
33
34
35 /**
36  * FIXME.
37  */
38 struct GetAddressContext
39 {
40
41   /**
42    * FIXME.
43    */
44   GNUNET_RESOLVER_AddressCallback callback;
45
46   /**
47    * Closure for "callback".
48    */
49   void *cls;
50
51   /**
52    * FIXME.
53    */
54   struct GNUNET_RESOLVER_GetMessage *msg;
55
56   /**
57    * FIXME.
58    */
59   struct GNUNET_CLIENT_Connection *client;
60
61   /**
62    * FIXME.
63    */
64   struct GNUNET_TIME_Absolute timeout;
65 };
66
67
68 /**
69  * Possible hostnames for "loopback".
70  */
71 static const char *loopback[] = {
72   "localhost",
73   "ip6-localnet",
74   NULL
75 };
76
77
78 /**
79  * Check that the resolver service runs on localhost
80  * (or equivalent).
81  */
82 static void
83 check_config (const struct GNUNET_CONFIGURATION_Handle *cfg)
84 {
85   char *hostname;
86   unsigned int i;
87   struct in_addr v4;
88   struct in6_addr v6;
89
90   if (GNUNET_OK !=
91       GNUNET_CONFIGURATION_get_value_string (cfg,
92                                              "resolver",
93                                              "HOSTNAME",
94                                              &hostname))
95     {
96       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
97                   _("Must specify `%s' for `%s' in configuration!\n"),
98                   "HOSTNAME",
99                   "resolver");
100       GNUNET_assert (0);
101     }
102   if ( (0 == inet_pton (AF_INET,
103                         hostname,
104                         &v4)) ||
105        (0 == inet_pton (AF_INET6,
106                         hostname,
107                         &v6)) )
108     {
109       GNUNET_free (hostname);
110       return;
111     }
112   i = 0;
113   while (loopback[i] != NULL)
114     if (0 == strcmp (loopback[i++], hostname))
115       {
116         GNUNET_free (hostname); 
117         return;
118       }
119   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
120               _("Must specify `%s' for `%s' in configuration!\n"),
121               "localhost",
122               "resolver");
123   GNUNET_free (hostname); 
124   GNUNET_assert (0); 
125 }
126
127
128 /**
129  * Convert IP address to string without DNS resolution.
130  *
131  * @param sa the address 
132  * @param salen number of bytes in sa
133  * @return address as a string, NULL on error
134  */
135 static char *
136 no_resolve (const struct sockaddr *sa, socklen_t salen)
137 {
138   char *ret;
139   char inet4[INET_ADDRSTRLEN];
140   char inet6[INET6_ADDRSTRLEN];
141
142   if (salen < sizeof (struct sockaddr))
143     return NULL;
144   switch (sa->sa_family)
145     {
146     case AF_INET:
147       if (salen != sizeof (struct sockaddr_in))
148         return NULL;
149       inet_ntop (AF_INET,
150                  &((struct sockaddr_in *) sa)->sin_addr,
151                  inet4, INET_ADDRSTRLEN);
152       ret = GNUNET_strdup (inet4);
153       break;
154     case AF_INET6:
155       if (salen != sizeof (struct sockaddr_in6))
156         return NULL;
157       inet_ntop (AF_INET6,
158                  &((struct sockaddr_in6 *) sa)->sin6_addr,
159                  inet6, INET6_ADDRSTRLEN);
160       ret = GNUNET_strdup (inet6);
161       break;
162     default:
163       ret = NULL;
164       break;
165     }
166   return ret;
167 }
168
169
170 /**
171  * FIXME
172  *
173  * @param cls FIXME
174  * @param msg FIXME
175  */
176 static void
177 handle_address_response (void *cls, const struct GNUNET_MessageHeader *msg)
178 {
179   struct GetAddressContext *gac = cls;
180   uint16_t size;
181   const struct sockaddr *sa;
182   socklen_t salen;
183
184
185   if (msg == NULL)
186     {
187       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
188                   _("Timeout trying to resolve hostname.\n"));
189       gac->callback (gac->cls, NULL, 0);
190       GNUNET_CLIENT_disconnect (gac->client);
191       GNUNET_free (gac);
192       return;
193     }
194   if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
195     {
196       GNUNET_break (0);
197       gac->callback (gac->cls, NULL, 0);
198       GNUNET_CLIENT_disconnect (gac->client);
199       GNUNET_free (gac);
200       return;
201     }
202
203   size = ntohs (msg->size);
204   if (size == sizeof (struct GNUNET_MessageHeader))
205     {
206 #if DEBUG_RESOLVER
207       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208                   _("Received end message resolving hostname.\n"));
209 #endif
210       gac->callback (gac->cls, NULL, 0);
211       GNUNET_CLIENT_disconnect (gac->client);
212       GNUNET_free (gac);
213       return;
214     }
215   sa = (const struct sockaddr *) &msg[1];
216   salen = size - sizeof (struct GNUNET_MessageHeader);
217   if (salen < sizeof (struct sockaddr))
218     {
219       GNUNET_break (0);
220       gac->callback (gac->cls, NULL, 0);
221       GNUNET_CLIENT_disconnect (gac->client);
222       GNUNET_free (gac);
223       return;
224     }
225 #if DEBUG_RESOLVER
226   {
227     char *ips = no_resolve (sa, salen);
228     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), ips);
229     GNUNET_free (ips);
230   }
231 #endif
232   gac->callback (gac->cls, sa, salen);
233   GNUNET_CLIENT_receive (gac->client,
234                          &handle_address_response,
235                          gac,
236                          GNUNET_TIME_absolute_get_remaining (gac->timeout));
237 }
238
239
240 /**
241  * FIXME
242  *
243  * @param cls FIXME
244  * @param size number of bytes available in buf
245  * @param buf target buffer, NULL on error
246  * @return number of bytes written to buf
247  */
248 static size_t
249 transmit_get_ip (void *cls, size_t size, void *buf)
250 {
251   struct GetAddressContext *actx = cls;
252   uint16_t ms;
253
254   if (buf == NULL)
255     {
256       /* timeout / error */
257       GNUNET_free (actx->msg);
258       actx->callback (actx->cls, NULL, 0);
259       GNUNET_CLIENT_disconnect (actx->client);
260       GNUNET_free (actx);
261       return 0;
262     }
263   ms = ntohs (actx->msg->header.size);
264   GNUNET_assert (size >= ms);
265   memcpy (buf, actx->msg, ms);
266   GNUNET_free (actx->msg);
267   actx->msg = NULL;
268   GNUNET_CLIENT_receive (actx->client,
269                          &handle_address_response,
270                          actx,
271                          GNUNET_TIME_absolute_get_remaining (actx->timeout));
272   return ms;
273 }
274
275
276
277 /**
278  * Convert a string to one or more IP addresses.
279  *
280  * @param sched scheduler to use
281  * @param cfg configuration to use
282  * @param hostname the hostname to resolve
283  * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
284  * @param callback function to call with addresses
285  * @param callback_cls closure for callback
286  * @param timeout how long to try resolving
287  */
288 void
289 GNUNET_RESOLVER_ip_get (struct GNUNET_SCHEDULER_Handle *sched,
290                         const struct GNUNET_CONFIGURATION_Handle *cfg,
291                         const char *hostname,
292                         int domain,
293                         struct GNUNET_TIME_Relative timeout,
294                         GNUNET_RESOLVER_AddressCallback callback, 
295                         void *callback_cls)
296 {
297   struct GNUNET_CLIENT_Connection *client;
298   struct GNUNET_RESOLVER_GetMessage *msg;
299   struct GetAddressContext *actx;
300   size_t slen;
301   unsigned int i;
302   struct sockaddr_in v4;
303   struct sockaddr_in6 v6;
304
305   memset (&v4, 0, sizeof(v4));
306   v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);  
307   v4.sin_family = AF_INET;
308 #if HAVE_SOCKADDR_IN_SIN_LEN
309   v4.sin_len = sizeof(v4);
310 #endif
311   memset (&v6, 0, sizeof(v6)); 
312   v6.sin6_family = AF_INET6;
313 #if HAVE_SOCKADDR_IN_SIN_LEN
314   v6.sin6_len = sizeof(v6);
315 #endif
316   /* first, check if this is a numeric address */
317   if ( ( (domain == AF_UNSPEC) || (domain == AF_INET) ) && 
318        (0 == inet_pton (AF_INET,
319                         hostname,
320                         &v4.sin_addr)) )
321     {
322       callback (callback_cls,
323                 (const struct sockaddr*) &v4,
324                 sizeof(v4));
325       if ( (domain == AF_UNSPEC) && 
326            (0 == inet_pton (AF_INET6,
327                             hostname,
328                             &v6.sin6_addr)) )
329         {
330           /* this can happen on some systems IF "hostname" is "localhost" */
331           callback (callback_cls,
332                     (const struct sockaddr*) &v6,
333                     sizeof(v6));
334         }
335       callback (callback_cls, NULL, 0);      
336       return;
337     }
338   if ( ( (domain == AF_UNSPEC) ||(domain == AF_INET) ) && 
339        (0 == inet_pton (AF_INET6,
340                         hostname,
341                         &v6.sin6_addr)) )
342     {
343       callback (callback_cls,
344                 (const struct sockaddr*) &v6,
345                 sizeof(v6));
346       callback (callback_cls, NULL, 0);
347       return;
348     }
349   check_config (cfg);
350   /* then, check if this is a loopback address */
351   i = 0;
352   while (loopback[i] != NULL)
353     if (0 == strcmp (loopback[i++], hostname))
354       {
355         v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);  
356         v6.sin6_addr = in6addr_loopback;
357         switch (domain)
358           {
359           case AF_INET:
360             callback (callback_cls, 
361                       (const struct sockaddr*) &v4,
362                       sizeof(v4));
363             break;
364           case AF_INET6:
365             callback (callback_cls, 
366                       (const struct sockaddr*) &v6,
367                       sizeof(v6));
368             break;
369           case AF_UNSPEC:
370             callback (callback_cls, 
371                       (const struct sockaddr*) &v6,
372                       sizeof(v6));
373             callback (callback_cls, 
374                       (const struct sockaddr*) &v4,
375                       sizeof(v4));
376             break;
377           }
378         callback (callback_cls, NULL, 0);
379         return;
380       }
381   slen = strlen (hostname) + 1;
382   if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
383       GNUNET_SERVER_MAX_MESSAGE_SIZE)
384     {
385       GNUNET_break (0);
386       callback (callback_cls, NULL, 0);
387       return;
388     }
389   client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
390   if (client == NULL)
391     {
392       callback (callback_cls, NULL, 0);
393       return;
394     }
395   msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
396   msg->header.size =
397     htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
398   msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
399   msg->direction = htonl (GNUNET_NO);
400   msg->domain = htonl (domain);
401   memcpy (&msg[1], hostname, slen);
402   actx = GNUNET_malloc (sizeof (struct GetAddressContext));
403   actx->callback = callback;
404   actx->cls = callback_cls;
405   actx->client = client;
406   actx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
407   actx->msg = msg;
408
409 #if DEBUG_RESOLVER
410   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411               _("Resolver requests DNS resolution of hostname `%s'.\n"),
412               hostname);
413 #endif
414   if (NULL ==
415       GNUNET_CLIENT_notify_transmit_ready (client,
416                                            slen +
417                                            sizeof (struct
418                                                    GNUNET_RESOLVER_GetMessage),
419                                            timeout, &transmit_get_ip, actx))
420     {
421       GNUNET_free (msg);
422       GNUNET_free (actx);
423       callback (callback_cls, NULL, 0);
424       GNUNET_CLIENT_disconnect (client);
425       return;
426     }
427 }
428
429
430 /**
431  * FIXME.
432  */
433 struct GetHostnameContext
434 {
435
436   /**
437    * FIXME.
438    */
439   GNUNET_RESOLVER_HostnameCallback callback;
440   
441   /**
442    * FIXME.
443    */
444   void *cls;
445   
446   /**
447    * FIXME.
448    */ 
449   struct GNUNET_RESOLVER_GetMessage *msg;
450   
451   /**
452    * FIXME.
453    */  
454   struct GNUNET_CLIENT_Connection *client;
455   
456   /**
457    * FIXME.
458    */ 
459   struct GNUNET_TIME_Absolute timeout;
460 };
461
462
463 /**
464  * FIXME.
465  *
466  * @param cls FIXME
467  * @param msg FIXME
468  */
469 static void
470 handle_hostname_response (void *cls, const struct GNUNET_MessageHeader *msg)
471 {
472   struct GetHostnameContext *ghc = cls;
473   uint16_t size;
474   const char *hostname;
475
476   if (msg == NULL)
477     {
478       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
479                   _("Timeout trying to resolve IP address.\n"));
480       ghc->callback (ghc->cls, NULL);
481       GNUNET_CLIENT_disconnect (ghc->client);
482       GNUNET_free (ghc);
483       return;
484     }
485   size = ntohs (msg->size);
486   if (size == sizeof (struct GNUNET_MessageHeader))
487     {
488 #if DEBUG_RESOLVER
489       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490                   _("Received end message resolving IP address.\n"));
491 #endif
492       ghc->callback (ghc->cls, NULL);
493       GNUNET_CLIENT_disconnect (ghc->client);
494       GNUNET_free (ghc);
495       return;
496     }
497   hostname = (const char *) &msg[1];
498   if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
499     {
500       GNUNET_break (0);
501       ghc->callback (ghc->cls, NULL);
502       GNUNET_CLIENT_disconnect (ghc->client);
503       GNUNET_free (ghc);
504       return;
505     }
506 #if DEBUG_RESOLVER
507   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508               _("Resolver returns `%s'.\n"), hostname);
509 #endif
510   ghc->callback (ghc->cls, hostname);
511   GNUNET_CLIENT_receive (ghc->client,
512                          &handle_hostname_response,
513                          ghc,
514                          GNUNET_TIME_absolute_get_remaining (ghc->timeout));
515 }
516
517
518 /**
519  * FIXME
520  *
521  * @param cls FIXME
522  * @param size number of bytes available in buf
523  * @param buf target buffer, NULL on error
524  * @return number of bytes written to buf
525  */
526 static size_t
527 transmit_get_hostname (void *cls, size_t size, void *buf)
528 {
529   struct GetHostnameContext *hctx = cls;
530   uint16_t msize;
531
532   if (buf == NULL)
533     {
534       GNUNET_free (hctx->msg);
535       hctx->callback (hctx->cls, NULL);
536       GNUNET_CLIENT_disconnect (hctx->client);
537       GNUNET_free (hctx);
538       return 0;
539     }
540   msize = ntohs (hctx->msg->header.size);
541   GNUNET_assert (size >= msize);
542   memcpy (buf, hctx->msg, msize);
543   GNUNET_free (hctx->msg);
544   hctx->msg = NULL;
545   GNUNET_CLIENT_receive (hctx->client,
546                          &handle_hostname_response,
547                          hctx,
548                          GNUNET_TIME_absolute_get_remaining (hctx->timeout));
549   return msize;
550 }
551
552
553
554
555 /**
556  * Get an IP address as a string.
557  *
558  * @param sched scheduler to use
559  * @param cfg configuration to use
560  * @param sa host address
561  * @param salen length of host address
562  * @param do_resolve use GNUNET_NO to return numeric hostname
563  * @param timeout how long to try resolving
564  * @param callback function to call with hostnames
565  * @param cls closure for callback
566  */
567 void
568 GNUNET_RESOLVER_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
569                               const struct GNUNET_CONFIGURATION_Handle *cfg,
570                               const struct sockaddr *sa,
571                               socklen_t salen,
572                               int do_resolve,
573                               struct GNUNET_TIME_Relative timeout,
574                               GNUNET_RESOLVER_HostnameCallback callback,
575                               void *cls)
576 {
577   char *result;
578   struct GNUNET_CLIENT_Connection *client;
579   struct GNUNET_RESOLVER_GetMessage *msg;
580   struct GetHostnameContext *hctx;
581
582   check_config (cfg);
583   if (GNUNET_NO == do_resolve)
584     {
585       result = no_resolve (sa, salen);
586 #if DEBUG_RESOLVER
587       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588                   _("Resolver returns `%s'.\n"), result);
589 #endif
590       callback (cls, result);
591       if (result != NULL)
592         {
593           GNUNET_free (result);
594           callback (cls, NULL);
595         }
596       return;
597     }
598   if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
599       GNUNET_SERVER_MAX_MESSAGE_SIZE)
600     {
601       GNUNET_break (0);
602       callback (cls, NULL);
603       return;
604     }
605   client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
606   if (client == NULL)
607     {
608       callback (cls, NULL);
609       return;
610     }
611   msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
612   msg->header.size =
613     htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
614   msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
615   msg->direction = htonl (GNUNET_YES);
616   msg->domain = htonl (sa->sa_family);
617   memcpy (&msg[1], sa, salen);
618 #if DEBUG_RESOLVER
619   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
620               _("Resolver requests DNS resolution of IP address.\n"));
621 #endif
622   hctx = GNUNET_malloc (sizeof (struct GetHostnameContext));
623   hctx->callback = callback;
624   hctx->cls = cls;
625   hctx->client = client;
626   hctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
627   hctx->msg = msg;
628   if (NULL ==
629       GNUNET_CLIENT_notify_transmit_ready (client,
630                                            sizeof (struct
631                                                    GNUNET_RESOLVER_GetMessage)
632                                            + salen, timeout,
633                                            &transmit_get_hostname, hctx))
634     {
635       GNUNET_free (msg);
636       callback (cls, NULL);
637       GNUNET_CLIENT_disconnect (client);
638       GNUNET_free (hctx);
639     }
640 }
641
642 /**
643  * Maximum supported length of hostname
644  */
645 #define MAX_HOSTNAME 1024
646
647
648 /**
649  * Resolve our hostname to an IP address.
650  *
651  * @param sched scheduler to use
652  * @param cfg configuration to use
653  * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
654  * @param callback function to call with addresses
655  * @param cls closure for callback
656  * @param timeout how long to try resolving
657  */
658 void
659 GNUNET_RESOLVER_hostname_resolve (struct GNUNET_SCHEDULER_Handle *sched,
660                                   const struct GNUNET_CONFIGURATION_Handle *cfg,
661                                   int domain,
662                                   struct GNUNET_TIME_Relative timeout,
663                                   GNUNET_RESOLVER_AddressCallback callback,
664                                   void *cls)
665 {
666   char hostname[MAX_HOSTNAME];
667
668   check_config (cfg);
669   if (0 != gethostname (hostname, sizeof (hostname) - 1))
670     {
671       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
672                            GNUNET_ERROR_TYPE_BULK, "gethostname");
673       callback (cls, NULL, 0);
674       return;
675     }
676 #if DEBUG_RESOLVER
677   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678               _("Resolving our hostname `%s'\n"), hostname);
679 #endif
680   GNUNET_RESOLVER_ip_get (sched,
681                           cfg, hostname, domain, timeout, callback, cls);
682 }
683
684
685
686
687 /* end of resolver_api.c */