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