adding basic declarations for datastore implementation
[oweals/gnunet.git] / src / resolver / gnunet-service-resolver.c
1 /*
2      This file is part of GNUnet.
3      (C) 2007, 2008, 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 resolver/gnunet-service-resolver.c
23  * @brief code to do DNS resolution
24  * @author Christian Grothoff
25  */
26
27 #include <stdlib.h>
28 #include "platform.h"
29 #include "gnunet_disk_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_service_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_strings_lib.h"
35 #include "gnunet_time_lib.h"
36 #include "resolver.h"
37
38
39 struct IPCache
40 {
41   struct IPCache *next;
42   char *addr;
43   struct sockaddr *sa;
44   struct GNUNET_TIME_Absolute last_refresh;
45   struct GNUNET_TIME_Absolute last_request;
46   unsigned int salen;
47 };
48
49
50 static struct IPCache *head;
51
52
53
54
55 #if HAVE_GETNAMEINFO
56 static void
57 getnameinfo_resolve (struct IPCache *cache)
58 {
59   char hostname[256];
60
61   if (0 == getnameinfo (cache->sa, cache->salen, hostname, 255, NULL, 0, 0))
62     cache->addr = GNUNET_strdup (hostname);
63 }
64 #endif
65
66
67 #if HAVE_GETHOSTBYADDR
68 static void
69 gethostbyaddr_resolve (struct IPCache *cache)
70 {
71   struct hostent *ent;
72
73   switch (cache->sa->sa_family)
74     {
75     case AF_INET:
76       ent = gethostbyaddr (&((struct sockaddr_in *) cache->sa)->sin_addr,
77                            sizeof (struct in_addr), AF_INET);
78       break;
79     case AF_INET6:
80       ent = gethostbyaddr (&((struct sockaddr_in6 *) cache->sa)->sin6_addr,
81                            sizeof (struct in6_addr), AF_INET6);
82       break;
83     default:
84       ent = NULL;
85     }
86   if (ent != NULL)
87     cache->addr = GNUNET_strdup (ent->h_name);
88 }
89 #endif
90
91
92 static void
93 cache_resolve (struct IPCache *cache)
94 {
95 #if HAVE_GETNAMEINFO
96   if (cache->addr == NULL)
97     getnameinfo_resolve (cache);
98 #endif
99 #if HAVE_GETHOSTBYADDR
100   if (cache->addr == NULL)
101     gethostbyaddr_resolve (cache);
102 #endif
103 }
104
105
106
107 /**
108  * Get an IP address as a string (works for both IPv4 and IPv6).  Note
109  * that the resolution happens asynchronously and that the first call
110  * may not immediately result in the FQN (but instead in a
111  * human-readable IP address).
112  *
113  * @param sa should be of type "struct sockaddr*"
114  */
115 static void
116 get_ip_as_string (struct GNUNET_SERVER_Client *client,
117                   const struct sockaddr *sav, socklen_t salen)
118 {
119   struct IPCache *cache;
120   struct IPCache *prev;
121   struct GNUNET_TIME_Absolute now;
122   struct GNUNET_SERVER_TransmitContext *tc;
123
124   if (salen < sizeof (struct sockaddr))
125     {
126       GNUNET_break (0);
127       return;
128     }
129   now = GNUNET_TIME_absolute_get ();
130   cache = head;
131   prev = NULL;
132   while ((cache != NULL) &&
133          ((cache->salen != salen) || (0 != memcmp (cache->sa, sav, salen))))
134     {
135       if (GNUNET_TIME_absolute_get_duration (cache->last_request).value <
136           60 * 60 * 1000)
137         {
138           if (prev != NULL)
139             {
140               prev->next = cache->next;
141               GNUNET_free_non_null (cache->addr);
142               GNUNET_free (cache->sa);
143               GNUNET_free (cache);
144               cache = prev->next;
145             }
146           else
147             {
148               head = cache->next;
149               GNUNET_free_non_null (cache->addr);
150               GNUNET_free (cache->sa);
151               GNUNET_free (cache);
152               cache = head;
153             }
154           continue;
155         }
156       prev = cache;
157       cache = cache->next;
158     }
159   if (cache != NULL)
160     {
161       cache->last_request = now;
162       if (GNUNET_TIME_absolute_get_duration (cache->last_request).value <
163           60 * 60 * 1000)
164         {
165           GNUNET_free_non_null (cache->addr);
166           cache->addr = NULL;
167           cache->salen = 0;
168           cache_resolve (cache);
169         }
170     }
171   else
172     {
173       cache = GNUNET_malloc (sizeof (struct IPCache));
174       cache->next = head;
175       cache->salen = salen;
176       cache->sa = GNUNET_malloc (salen);
177       memcpy (cache->sa, sav, salen);
178       cache->last_request = GNUNET_TIME_absolute_get ();
179       cache->last_refresh = GNUNET_TIME_absolute_get ();
180       cache->addr = NULL;
181       cache_resolve (cache);
182       head = cache;
183     }
184   tc = GNUNET_SERVER_transmit_context_create (client);
185   if (cache->addr != NULL)
186     GNUNET_SERVER_transmit_context_append (tc,
187                                            cache->addr,
188                                            strlen (cache->addr) + 1,
189                                            GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
190   GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
191                                          GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
192   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
193 }
194
195
196 #if HAVE_GETADDRINFO
197 static int
198 getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc,
199                      const char *hostname, int domain)
200 {
201   int s;
202   struct addrinfo hints;
203   struct addrinfo *result;
204   struct addrinfo *pos;
205
206   memset (&hints, 0, sizeof (struct addrinfo));
207 // FIXME in PlibC
208 #ifndef MINGW
209   hints.ai_family = domain;
210 #else
211   hints.ai_family = AF_INET;
212 #endif
213   hints.ai_socktype = SOCK_STREAM;      /* go for TCP */
214
215   if (0 != (s = getaddrinfo (hostname, NULL, &hints, &result)))
216     {
217       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
218                   _("Could not resolve `%s' (%s): %s\n"), hostname,
219                   (domain ==
220                    AF_INET) ? "IPv4" : ((domain ==
221                                          AF_INET6) ? "IPv6" : "any"),
222                   gai_strerror (s));
223       if ((s == EAI_BADFLAGS) || (s == EAI_MEMORY) ||
224 #ifndef MINGW
225           (s == EAI_SYSTEM)
226 #else
227           // FIXME NILS
228           1
229 #endif
230       )
231         return GNUNET_NO;       /* other function may still succeed */
232       return GNUNET_SYSERR;
233     }
234   if (result == NULL)
235     return GNUNET_SYSERR;
236   pos = result;
237   while (pos != NULL)
238     {
239       GNUNET_SERVER_transmit_context_append (tc,
240                                              result->ai_addr,
241                                              result->ai_addrlen,
242                                              GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
243       pos = pos->ai_next;
244     }
245   freeaddrinfo (result);
246   return GNUNET_OK;
247 }
248 #endif
249
250 #if HAVE_GETHOSTBYNAME2
251 static int
252 gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc,
253                         const char *hostname, int domain)
254 {
255   struct hostent *hp;
256   struct sockaddr_in a4;
257   struct sockaddr_in6 a6;
258   int ret1;
259   int ret2;
260
261   if (domain == AF_UNSPEC)
262     {
263       ret1 = gethostbyname2_resolve (tc, hostname, AF_INET);
264       ret2 = gethostbyname2_resolve (tc, hostname, AF_INET6);
265       if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK))
266         return GNUNET_OK;
267       if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR))
268         return GNUNET_SYSERR;
269       return GNUNET_NO;
270     }
271   hp = gethostbyname2 (hostname, domain);
272   if (hp == NULL)
273     {
274       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
275                   _("Could not find IP of host `%s': %s\n"),
276                   hostname, hstrerror (h_errno));
277       return GNUNET_SYSERR;
278     }
279   GNUNET_assert (hp->h_addrtype == domain);
280   if (domain == AF_INET)
281     {
282       GNUNET_assert (hp->h_length == sizeof (struct in_addr));
283       memset (&a4, 0, sizeof (a4));
284       a4.sin_family = AF_INET;
285       memcpy (&a4.sin_addr, hp->h_addr_list[0], hp->h_length);
286       GNUNET_SERVER_transmit_context_append (tc,
287                                              &a4,
288                                              sizeof (a4),
289                                              GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
290     }
291   else
292     {
293       GNUNET_assert (hp->h_length == sizeof (struct in6_addr));
294       memset (&a6, 0, sizeof (a6));
295       a6.sin6_family = AF_INET6;
296       memcpy (&a6.sin6_addr, hp->h_addr_list[0], hp->h_length);
297       GNUNET_SERVER_transmit_context_append (tc,
298                                              &a6,
299                                              sizeof (a6),
300                                              GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
301     }
302   return GNUNET_OK;
303 }
304 #endif
305
306 #if HAVE_GETHOSTBYNAME
307 static int
308 gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc,
309                        const char *hostname)
310 {
311   struct hostent *hp;
312   struct sockaddr_in addr;
313
314   hp = GETHOSTBYNAME (hostname);
315   if (hp == NULL)
316     {
317       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
318                   _("Could not find IP of host `%s': %s\n"),
319                   hostname, hstrerror (h_errno));
320       return GNUNET_SYSERR;
321     }
322   if (hp->h_addrtype != AF_INET)
323     {
324       GNUNET_break (0);
325       return GNUNET_SYSERR;
326     }
327   GNUNET_assert (hp->h_length == sizeof (struct in_addr));
328   memset (&addr, 0, sizeof (addr));
329   addr.sin_family = AF_INET;
330   memcpy (&addr.sin_addr, hp->h_addr_list[0], hp->h_length);
331   GNUNET_SERVER_transmit_context_append (tc,
332                                          &addr,
333                                          sizeof (addr),
334                                          GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
335   return GNUNET_OK;
336 }
337 #endif
338
339
340 /**
341  * Convert a string to an IP address.
342  *
343  * @param client where to send the IP address
344  * @param hostname the hostname to resolve
345  * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
346  */
347 static void
348 get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
349                       const char *hostname, int domain)
350 {
351   int ret;
352   struct GNUNET_SERVER_TransmitContext *tc;
353
354   tc = GNUNET_SERVER_transmit_context_create (client);
355   ret = GNUNET_NO;
356 #if HAVE_GETADDRINFO
357   if (ret == GNUNET_NO)
358     ret = getaddrinfo_resolve (tc, hostname, domain);
359 #endif
360 #if HAVE_GETHOSTBYNAME2
361   if (ret == GNUNET_NO)
362     ret = gethostbyname2_resolve (tc, hostname, domain);
363 #endif
364 #if HAVE_GETHOSTBYNAME
365   if ((ret == GNUNET_NO) && ((domain == AF_UNSPEC) || (domain == PF_INET)))
366     gethostbyname_resolve (tc, hostname);
367 #endif
368   GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
369                                          GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
370   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
371 }
372
373
374 /**
375  * Handle GET-message.
376  *
377  * @param cls closure
378  * @param client identification of the client
379  * @param message the actual message
380  */
381 static void
382 handle_get (void *cls,
383             struct GNUNET_SERVER_Client *client,
384             const struct GNUNET_MessageHeader *message)
385 {
386   uint16_t msize;
387   const struct GNUNET_RESOLVER_GetMessage *msg;
388   const char *hostname;
389   uint16_t size;
390   int direction;
391   int domain;
392
393   msize = ntohs (message->size);
394   if (msize < sizeof (struct GNUNET_RESOLVER_GetMessage))
395     {
396       GNUNET_break (0);
397       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
398       return;
399     }
400   msg = (const struct GNUNET_RESOLVER_GetMessage *) message;
401   size = msize - sizeof (struct GNUNET_RESOLVER_GetMessage);
402   direction = ntohl (msg->direction);
403   domain = ntohl (msg->domain);
404   if (direction == GNUNET_NO)
405     {
406       /* IP from hostname */
407       hostname = (const char *) &msg[1];
408       if (hostname[size - 1] != '\0')
409         {
410           GNUNET_break (0);
411           GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
412           return;
413         }
414 #if DEBUG_RESOLVER
415       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416                   _("Resolver asked to look up `%s'.\n"), hostname);
417 #endif
418       get_ip_from_hostname (client, hostname, domain);
419     }
420   else
421     {
422 #if DEBUG_RESOLVER
423       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424                   _("Resolver asked to look up IP address.\n"));
425 #endif
426       get_ip_as_string (client, (const struct sockaddr *) &msg[1], size);
427     }
428 }
429
430
431 /**
432  * List of handlers for the messages understood by this
433  * service.
434  */
435 static struct GNUNET_SERVER_MessageHandler handlers[] = {
436   {&handle_get, NULL, GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST, 0},
437   {NULL, NULL, 0, 0}
438 };
439
440
441 /**
442  * Process resolver requests.
443  *
444  * @param cls closure
445  * @param sched scheduler to use
446  * @param server the initialized server
447  * @param cfg configuration to use
448  */
449 static void
450 run (void *cls,
451      struct GNUNET_SCHEDULER_Handle *sched,
452      struct GNUNET_SERVER_Handle *server,
453      struct GNUNET_CONFIGURATION_Handle *cfg)
454 {
455   GNUNET_SERVER_add_handlers (server, handlers);
456 }
457
458
459 /**
460  * The main function for the resolver service.
461  *
462  * @param argc number of arguments from the command line
463  * @param argv command line arguments
464  * @return 0 ok, 1 on error
465  */
466 int
467 main (int argc, char *const *argv)
468 {
469   int ret;
470   struct IPCache *pos;
471
472   ret = (GNUNET_OK ==
473          GNUNET_SERVICE_run (argc,
474                              argv,
475                              "resolver", &run, NULL, NULL, NULL)) ? 0 : 1;
476
477   while (head != NULL)
478     {
479       pos = head->next;
480       GNUNET_free_non_null (head->addr);
481       GNUNET_free (head->sa);
482       GNUNET_free (head);
483       head = pos;
484     }
485   return ret;
486 }
487
488 /* end of gnunet-service-resolver.c */