2 This file is part of GNUnet.
3 Copyright (C) 2009-2016 GNUnet e.V.
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 3, or (at your
8 option) any later version.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/resolver_api.c
23 * @brief resolver for writing a tool
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_resolver_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "util-resolver-api", __VA_ARGS__)
34 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-resolver-api", syscall)
37 * Maximum supported length for a hostname
39 #define MAX_HOSTNAME 1024
43 * Possible hostnames for "loopback".
45 static const char *loopback[] = {
55 static const struct GNUNET_CONFIGURATION_Handle *resolver_cfg;
58 * Our connection to the resolver service, created on-demand, but then
59 * persists until error or shutdown.
61 static struct GNUNET_MQ_Handle *mq;
64 * Head of DLL of requests.
66 static struct GNUNET_RESOLVER_RequestHandle *req_head;
69 * Tail of DLL of requests.
71 static struct GNUNET_RESOLVER_RequestHandle *req_tail;
74 * How long should we wait to reconnect?
76 static struct GNUNET_TIME_Relative backoff;
79 * Task for reconnecting.
81 static struct GNUNET_SCHEDULER_Task *r_task;
84 * Task ID of shutdown task; only present while we have a
85 * connection to the resolver service.
87 static struct GNUNET_SCHEDULER_Task *s_task;
91 * Handle to a request given to the resolver. Can be used to cancel
92 * the request prior to the timeout or successful execution. Also
93 * used to track our internal state for the request.
95 struct GNUNET_RESOLVER_RequestHandle
99 * Next entry in DLL of requests.
101 struct GNUNET_RESOLVER_RequestHandle *next;
104 * Previous entry in DLL of requests.
106 struct GNUNET_RESOLVER_RequestHandle *prev;
109 * Callback if this is an name resolution request,
112 GNUNET_RESOLVER_AddressCallback addr_callback;
115 * Callback if this is a reverse lookup request,
118 GNUNET_RESOLVER_HostnameCallback name_callback;
121 * Closure for the callbacks.
126 * When should this request time out?
128 struct GNUNET_TIME_Absolute timeout;
131 * Task handle for making reply callbacks in numeric lookups
132 * asynchronous, and for timeout handling.
134 struct GNUNET_SCHEDULER_Task *task;
137 * Desired address family.
142 * Has this request been transmitted to the service?
143 * #GNUNET_YES if transmitted
144 * #GNUNET_YES if not transmitted
145 * #GNUNET_SYSERR when request was canceled
150 * Did we add this request to the queue?
155 * Desired direction (IP to name or name to IP)
160 * #GNUNET_YES if a response was received
162 int received_response;
165 * Length of the data that follows this struct.
172 * Check that the resolver service runs on localhost
175 * @return #GNUNET_OK if the resolver is properly configured,
176 * #GNUNET_SYSERR otherwise.
182 struct sockaddr_in v4;
183 struct sockaddr_in6 v6;
185 memset (&v4, 0, sizeof (v4));
186 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
187 v4.sin_family = AF_INET;
188 #if HAVE_SOCKADDR_IN_SIN_LEN
189 v4.sin_len = sizeof (v4);
191 memset (&v6, 0, sizeof (v6));
192 v6.sin6_family = AF_INET6;
193 #if HAVE_SOCKADDR_IN_SIN_LEN
194 v6.sin6_len = sizeof (v6);
197 GNUNET_CONFIGURATION_get_value_string (resolver_cfg,
202 LOG (GNUNET_ERROR_TYPE_INFO,
203 _("Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"),
206 return GNUNET_SYSERR;
208 if ( (1 == inet_pton (AF_INET, hostname, &v4)) ||
209 (1 == inet_pton (AF_INET6, hostname, &v6)) )
211 GNUNET_free (hostname);
214 for (unsigned int i = 0;
217 if (0 == strcasecmp (loopback[i], hostname))
219 GNUNET_free (hostname);
222 LOG (GNUNET_ERROR_TYPE_INFO,
223 _("Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"),
227 GNUNET_free (hostname);
228 return GNUNET_SYSERR;
233 * Create the connection to the resolver service.
235 * @param cfg configuration to use
238 GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
240 GNUNET_assert (NULL != cfg);
241 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
247 * Destroy the connection to the resolver service.
250 GNUNET_RESOLVER_disconnect ()
252 struct GNUNET_RESOLVER_RequestHandle *rh;
254 while (NULL != (rh = req_head))
256 GNUNET_assert (GNUNET_SYSERR == rh->was_transmitted);
257 GNUNET_CONTAINER_DLL_remove (req_head,
264 LOG (GNUNET_ERROR_TYPE_DEBUG,
265 "Disconnecting from DNS service\n");
266 GNUNET_MQ_destroy (mq);
271 GNUNET_SCHEDULER_cancel (r_task);
276 GNUNET_SCHEDULER_cancel (s_task);
283 * Task executed on system shutdown.
286 shutdown_task (void *cls)
289 GNUNET_RESOLVER_disconnect ();
290 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
295 * Consider disconnecting if we have no further requests pending.
300 for (struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
303 if (GNUNET_SYSERR != rh->was_transmitted)
307 GNUNET_SCHEDULER_cancel (r_task);
312 s_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
319 * Convert IP address to string without DNS resolution.
321 * @param af address family
322 * @param ip the address
323 * @param ip_len number of bytes in @a ip
324 * @return address as a string, NULL on error
331 char buf[INET6_ADDRSTRLEN];
336 if (ip_len != sizeof (struct in_addr))
344 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
350 if (ip_len != sizeof (struct in6_addr))
358 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
367 return GNUNET_strdup (buf);
372 * Adjust exponential back-off and reconnect to the service.
379 * Generic error handler, called with the appropriate error code and
380 * the same closure specified at the creation of the message queue.
381 * Not every message queue implementation supports an error handler.
384 * @param error error code
387 mq_error_handler (void *cls,
388 enum GNUNET_MQ_Error error)
390 GNUNET_MQ_destroy (mq);
392 LOG (GNUNET_ERROR_TYPE_DEBUG,
393 "MQ error, reconnecting\n");
399 * Process pending requests to the resolver.
404 struct GNUNET_RESOLVER_GetMessage *msg;
405 struct GNUNET_MQ_Envelope *env;
406 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
415 /* nothing to do, release socket really soon if there is nothing
416 * else happening... */
418 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
423 if (GNUNET_NO != rh->was_transmitted)
424 return; /* waiting for reply */
425 env = GNUNET_MQ_msg_extra (msg,
427 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
428 msg->direction = htonl (rh->direction);
429 msg->af = htonl (rh->af);
430 GNUNET_memcpy (&msg[1],
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
434 "Transmitting DNS resolution request to DNS service\n");
437 rh->was_transmitted = GNUNET_YES;
442 * Check validity of response with a hostname for a DNS lookup.
445 * @param msg message with the hostname
448 check_response (void *cls,
449 const struct GNUNET_MessageHeader *msg)
451 /* implemented in #handle_response() for now */
457 * Check validity of response with a hostname for a DNS lookup.
458 * NOTE: right now rather messy, might want to use different
459 * message types for different response formats in the future.
462 * @param msg message with the response
465 handle_response (void *cls,
466 const struct GNUNET_MessageHeader *msg)
468 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
472 GNUNET_assert (NULL != rh);
473 size = ntohs (msg->size);
474 if (size == sizeof (struct GNUNET_MessageHeader))
476 LOG (GNUNET_ERROR_TYPE_DEBUG,
477 "Received empty response from DNS service\n");
478 /* message contains not data, just header; end of replies */
479 /* check if request was canceled */
480 if (GNUNET_SYSERR != rh->was_transmitted)
482 /* no reverse lookup was successful, return IP as string */
483 if (NULL != rh->name_callback)
485 if (GNUNET_NO == rh->received_response)
487 nret = no_resolve (rh->af,
490 rh->name_callback (rh->cls, nret);
493 /* finally, make termination call */
494 rh->name_callback (rh->cls,
497 if (NULL != rh->addr_callback)
498 rh->addr_callback (rh->cls,
502 rh->was_transmitted = GNUNET_NO;
503 GNUNET_RESOLVER_request_cancel (rh);
507 /* return reverse lookup results to caller */
508 if (NULL != rh->name_callback)
510 const char *hostname;
512 hostname = (const char *) &msg[1];
513 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
516 if (GNUNET_SYSERR != rh->was_transmitted)
517 rh->name_callback (rh->cls,
519 rh->was_transmitted = GNUNET_NO;
520 GNUNET_RESOLVER_request_cancel (rh);
521 GNUNET_MQ_destroy (mq);
526 LOG (GNUNET_ERROR_TYPE_DEBUG,
527 "Resolver returns `%s' for IP `%s'.\n",
529 GNUNET_a2s ((const void *) &rh[1],
531 if (rh->was_transmitted != GNUNET_SYSERR)
532 rh->name_callback (rh->cls,
534 rh->received_response = GNUNET_YES;
536 /* return lookup results to caller */
537 if (NULL != rh->addr_callback)
539 struct sockaddr_in v4;
540 struct sockaddr_in6 v6;
541 const struct sockaddr *sa;
547 ip_len = size - sizeof (struct GNUNET_MessageHeader);
548 if (ip_len == sizeof (struct in_addr))
550 memset (&v4, 0, sizeof (v4));
551 v4.sin_family = AF_INET;
552 v4.sin_addr = *(struct in_addr*) ip;
553 #if HAVE_SOCKADDR_IN_SIN_LEN
554 v4.sin_len = sizeof (v4);
557 sa = (const struct sockaddr *) &v4;
559 else if (ip_len == sizeof (struct in6_addr))
561 memset (&v6, 0, sizeof (v6));
562 v6.sin6_family = AF_INET6;
563 v6.sin6_addr = *(struct in6_addr*) ip;
564 #if HAVE_SOCKADDR_IN_SIN_LEN
565 v6.sin6_len = sizeof (v6);
568 sa = (const struct sockaddr *) &v6;
573 if (GNUNET_SYSERR != rh->was_transmitted)
574 rh->addr_callback (rh->cls,
577 rh->was_transmitted = GNUNET_NO;
578 GNUNET_RESOLVER_request_cancel (rh);
579 GNUNET_MQ_destroy (mq);
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
585 "Received IP from DNS service\n");
586 if (GNUNET_SYSERR != rh->was_transmitted)
587 rh->addr_callback (rh->cls,
595 * We've been asked to lookup the address for a hostname and were
596 * given a valid numeric string. Perform the callbacks for the
599 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
602 numeric_resolution (void *cls)
604 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
605 struct sockaddr_in v4;
606 struct sockaddr_in6 v6;
607 const char *hostname;
610 memset (&v4, 0, sizeof (v4));
611 v4.sin_family = AF_INET;
612 #if HAVE_SOCKADDR_IN_SIN_LEN
613 v4.sin_len = sizeof (v4);
615 memset (&v6, 0, sizeof (v6));
616 v6.sin6_family = AF_INET6;
617 #if HAVE_SOCKADDR_IN_SIN_LEN
618 v6.sin6_len = sizeof (v6);
620 hostname = (const char *) &rh[1];
621 if ( ( (rh->af == AF_UNSPEC) ||
622 (rh->af == AF_INET) ) &&
623 (1 == inet_pton (AF_INET,
627 rh->addr_callback (rh->cls,
628 (const struct sockaddr *) &v4,
630 if ( (rh->af == AF_UNSPEC) &&
631 (1 == inet_pton (AF_INET6,
635 /* this can happen on some systems IF "hostname" is "localhost" */
636 rh->addr_callback (rh->cls,
637 (const struct sockaddr *) &v6,
640 rh->addr_callback (rh->cls,
646 if ( ( (rh->af == AF_UNSPEC) ||
647 (rh->af == AF_INET6) ) &&
648 (1 == inet_pton (AF_INET6,
652 rh->addr_callback (rh->cls,
653 (const struct sockaddr *) &v6,
655 rh->addr_callback (rh->cls,
661 /* why are we here? this task should not have been scheduled! */
668 * We've been asked to lookup the address for a hostname and were
669 * given a variant of "loopback". Perform the callbacks for the
670 * respective loopback numeric addresses.
672 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
675 loopback_resolution (void *cls)
677 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
678 struct sockaddr_in v4;
679 struct sockaddr_in6 v6;
682 memset (&v4, 0, sizeof (v4));
683 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
684 v4.sin_family = AF_INET;
685 #if HAVE_SOCKADDR_IN_SIN_LEN
686 v4.sin_len = sizeof (v4);
688 memset (&v6, 0, sizeof (v6));
689 v6.sin6_family = AF_INET6;
690 #if HAVE_SOCKADDR_IN_SIN_LEN
691 v6.sin6_len = sizeof (v6);
693 v6.sin6_addr = in6addr_loopback;
697 rh->addr_callback (rh->cls,
698 (const struct sockaddr *) &v4,
702 rh->addr_callback (rh->cls,
703 (const struct sockaddr *) &v6,
707 rh->addr_callback (rh->cls,
708 (const struct sockaddr *) &v6,
710 rh->addr_callback (rh->cls,
711 (const struct sockaddr *) &v4,
719 rh->addr_callback (rh->cls,
722 LOG (GNUNET_ERROR_TYPE_DEBUG,
723 "Finished resolving hostname `%s'.\n",
724 (const char *) &rh[1]);
730 * Now try to reconnect to the resolver service.
735 reconnect_task (void *cls)
737 struct GNUNET_MQ_MessageHandler handlers[] = {
738 GNUNET_MQ_hd_var_size (response,
739 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
740 struct GNUNET_MessageHeader,
742 GNUNET_MQ_handler_end ()
746 if (NULL == req_head)
747 return; /* no work pending */
748 LOG (GNUNET_ERROR_TYPE_DEBUG,
749 "Trying to connect to DNS service\n");
750 mq = GNUNET_CLIENT_connect (resolver_cfg,
757 LOG (GNUNET_ERROR_TYPE_DEBUG,
758 "Failed to connect, will try again later\n");
767 * Adjust exponential back-off and reconnect to the service.
772 struct GNUNET_RESOLVER_RequestHandle *rh;
776 GNUNET_assert (NULL == mq);
777 if (NULL != (rh = req_head))
779 switch (rh->was_transmitted)
782 /* nothing more to do */
785 /* disconnected, transmit again! */
786 rh->was_transmitted = GNUNET_NO;
789 /* request was cancelled, remove entirely */
790 GNUNET_CONTAINER_DLL_remove (req_head,
801 LOG (GNUNET_ERROR_TYPE_DEBUG,
802 "Will try to connect to DNS service in %s\n",
803 GNUNET_STRINGS_relative_time_to_string (backoff,
805 GNUNET_assert (NULL != resolver_cfg);
806 r_task = GNUNET_SCHEDULER_add_delayed (backoff,
809 backoff = GNUNET_TIME_STD_BACKOFF (backoff);
814 * A DNS resolution timed out. Notify the application.
816 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
819 handle_lookup_timeout (void *cls)
821 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
824 if (GNUNET_NO == rh->direction)
826 LOG (GNUNET_ERROR_TYPE_INFO,
827 _("Timeout trying to resolve hostname `%s'.\n"),
828 (const char *) &rh[1]);
829 if (NULL != rh->addr_callback)
830 rh->addr_callback (rh->cls,
836 #if !defined(GNUNET_CULL_LOGGING)
837 char buf[INET6_ADDRSTRLEN];
839 LOG (GNUNET_ERROR_TYPE_INFO,
840 _("Timeout trying to resolve IP address `%s'.\n"),
842 (const void *) &rh[1],
846 if (GNUNET_NO == rh->received_response)
850 nret = no_resolve (rh->af,
853 if (NULL != rh->name_callback)
854 rh->name_callback (rh->cls, nret);
857 /* finally, make termination call */
858 if (NULL != rh->name_callback)
859 rh->name_callback (rh->cls,
862 rh->was_transmitted = GNUNET_NO;
863 GNUNET_RESOLVER_request_cancel (rh);
869 * Convert a string to one or more IP addresses.
871 * @param hostname the hostname to resolve
872 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
873 * @param callback function to call with addresses
874 * @param callback_cls closure for @a callback
875 * @param timeout how long to try resolving
876 * @return handle that can be used to cancel the request, NULL on error
878 struct GNUNET_RESOLVER_RequestHandle *
879 GNUNET_RESOLVER_ip_get (const char *hostname,
881 struct GNUNET_TIME_Relative timeout,
882 GNUNET_RESOLVER_AddressCallback callback,
885 struct GNUNET_RESOLVER_RequestHandle *rh;
890 slen = strlen (hostname) + 1;
891 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
892 GNUNET_MAX_MESSAGE_SIZE)
897 LOG (GNUNET_ERROR_TYPE_DEBUG,
898 "Trying to resolve hostname `%s'.\n",
900 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
902 rh->addr_callback = callback;
903 rh->cls = callback_cls;
904 GNUNET_memcpy (&rh[1],
908 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
909 rh->direction = GNUNET_NO;
910 /* first, check if this is a numeric address */
911 if ( ( (1 == inet_pton (AF_INET,
915 (af == AF_UNSPEC) ) ) ||
916 ( (1 == inet_pton (AF_INET6,
919 ( (af == AF_INET6) ||
920 (af == AF_UNSPEC)) ) )
922 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution,
926 /* then, check if this is a loopback address */
927 for (unsigned int i = 0;
930 if (0 == strcasecmp (loopback[i],
933 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution,
937 if (GNUNET_OK != check_config ())
942 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
943 &handle_lookup_timeout,
945 GNUNET_CONTAINER_DLL_insert_tail (req_head,
948 rh->was_queued = GNUNET_YES;
951 GNUNET_SCHEDULER_cancel (s_task);
960 * We've been asked to convert an address to a string without
961 * a reverse lookup, either because the client asked for it
962 * or because the DNS lookup hit a timeout. Do the numeric
963 * conversion and invoke the callback.
965 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
968 numeric_reverse (void *cls)
970 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
974 result = no_resolve (rh->af,
977 LOG (GNUNET_ERROR_TYPE_DEBUG,
978 "Resolver returns `%s'.\n",
982 rh->name_callback (rh->cls,
984 GNUNET_free (result);
986 rh->name_callback (rh->cls,
988 if (NULL != rh->task)
990 GNUNET_SCHEDULER_cancel (rh->task);
998 * Get an IP address as a string.
1000 * @param sa host address
1001 * @param salen length of host address in @a sa
1002 * @param do_resolve use #GNUNET_NO to return numeric hostname
1003 * @param timeout how long to try resolving
1004 * @param callback function to call with hostnames
1005 * last callback is NULL when finished
1006 * @param cls closure for @a callback
1007 * @return handle that can be used to cancel the request
1009 struct GNUNET_RESOLVER_RequestHandle *
1010 GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
1013 struct GNUNET_TIME_Relative timeout,
1014 GNUNET_RESOLVER_HostnameCallback callback,
1017 struct GNUNET_RESOLVER_RequestHandle *rh;
1021 if (GNUNET_OK != check_config ())
1023 LOG (GNUNET_ERROR_TYPE_ERROR,
1024 _("Resolver not configured correctly.\n"));
1028 switch (sa->sa_family)
1031 GNUNET_assert (salen == sizeof (struct sockaddr_in));
1032 ip_len = sizeof (struct in_addr);
1033 ip = &((const struct sockaddr_in*)sa)->sin_addr;
1036 GNUNET_assert (salen == sizeof (struct sockaddr_in6));
1037 ip_len = sizeof (struct in6_addr);
1038 ip = &((const struct sockaddr_in6*)sa)->sin6_addr;
1044 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
1045 rh->name_callback = callback;
1047 rh->af = sa->sa_family;
1048 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1049 GNUNET_memcpy (&rh[1],
1052 rh->data_len = ip_len;
1053 rh->direction = GNUNET_YES;
1054 rh->received_response = GNUNET_NO;
1055 if (GNUNET_NO == do_resolve)
1057 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse,
1061 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
1062 &handle_lookup_timeout,
1064 GNUNET_CONTAINER_DLL_insert_tail (req_head,
1067 rh->was_queued = GNUNET_YES;
1070 GNUNET_SCHEDULER_cancel (s_task);
1073 process_requests ();
1079 * Get local fully qualified af name
1084 GNUNET_RESOLVER_local_fqdn_get ()
1086 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1088 if (0 != gethostname (hostname,
1089 sizeof (hostname) - 1))
1091 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1095 LOG (GNUNET_ERROR_TYPE_DEBUG,
1096 "Resolving our FQDN `%s'\n",
1098 #if HAVE_GETADDRINFO
1100 struct addrinfo *ai;
1104 if (0 != (ret = getaddrinfo (hostname,
1109 LOG (GNUNET_ERROR_TYPE_ERROR,
1110 _("Could not resolve our FQDN: %s\n"),
1111 gai_strerror (ret));
1114 if (NULL != ai->ai_canonname)
1115 rval = GNUNET_strdup (ai->ai_canonname);
1117 rval = GNUNET_strdup (hostname);
1121 #elif HAVE_GETHOSTBYNAME2
1123 struct hostent *host;
1125 host = gethostbyname2 (hostname,
1128 host = gethostbyname2 (hostname,
1132 LOG (GNUNET_ERROR_TYPE_ERROR,
1133 _("Could not resolve our FQDN: %s\n"),
1134 hstrerror (h_errno));
1137 return GNUNET_strdup (host->h_name);
1139 #elif HAVE_GETHOSTBYNAME
1141 struct hostent *host;
1143 host = gethostbyname (hostname);
1146 LOG (GNUNET_ERROR_TYPE_ERROR,
1147 _("Could not resolve our FQDN: %s\n"),
1148 hstrerror (h_errno));
1151 return GNUNET_strdup (host->h_name);
1154 /* fallback: just hope name is already FQDN */
1155 return GNUNET_strdup (hostname);
1161 * Looking our own hostname.
1163 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
1164 * @param timeout how long to try resolving
1165 * @param callback function to call with addresses
1166 * @param cls closure for @a callback
1167 * @return handle that can be used to cancel the request, NULL on error
1169 struct GNUNET_RESOLVER_RequestHandle *
1170 GNUNET_RESOLVER_hostname_resolve (int af,
1171 struct GNUNET_TIME_Relative timeout,
1172 GNUNET_RESOLVER_AddressCallback callback,
1175 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1177 if (0 != gethostname (hostname, sizeof (hostname) - 1))
1179 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "Resolving our hostname `%s'\n",
1186 return GNUNET_RESOLVER_ip_get (hostname,
1195 * Cancel a request that is still pending with the resolver.
1196 * Note that a client MUST NOT cancel a request that has
1197 * been completed (i.e, the callback has been called to
1198 * signal timeout or the final result).
1200 * @param rh handle of request to cancel
1203 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
1205 if (GNUNET_NO == rh->direction)
1206 LOG (GNUNET_ERROR_TYPE_DEBUG,
1207 "Asked to cancel request to resolve hostname `%s'.\n",
1208 (const char *) &rh[1]);
1209 if (NULL != rh->task)
1211 GNUNET_SCHEDULER_cancel (rh->task);
1214 if (GNUNET_NO == rh->was_transmitted)
1216 if (GNUNET_YES == rh->was_queued)
1217 GNUNET_CONTAINER_DLL_remove (req_head,
1221 check_disconnect ();
1224 GNUNET_assert (GNUNET_YES == rh->was_transmitted);
1225 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
1226 check_disconnect ();
1230 /* end of resolver_api.c */