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