2 This file is part of GNUnet.
3 Copyright (C) 2009-2018 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],
220 GNUNET_free (hostname);
223 LOG (GNUNET_ERROR_TYPE_INFO,
224 _("Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"),
228 GNUNET_free (hostname);
229 return GNUNET_SYSERR;
234 * Create the connection to the resolver service.
236 * @param cfg configuration to use
239 GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
241 GNUNET_assert (NULL != cfg);
242 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
248 * Destroy the connection to the resolver service.
251 GNUNET_RESOLVER_disconnect ()
253 struct GNUNET_RESOLVER_RequestHandle *rh;
255 while (NULL != (rh = req_head))
257 GNUNET_assert (GNUNET_SYSERR == rh->was_transmitted);
258 GNUNET_CONTAINER_DLL_remove (req_head,
265 LOG (GNUNET_ERROR_TYPE_DEBUG,
266 "Disconnecting from DNS service\n");
267 GNUNET_MQ_destroy (mq);
272 GNUNET_SCHEDULER_cancel (r_task);
277 GNUNET_SCHEDULER_cancel (s_task);
284 * Task executed on system shutdown.
287 shutdown_task (void *cls)
291 GNUNET_RESOLVER_disconnect ();
292 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
297 * Consider disconnecting if we have no further requests pending.
302 for (struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
305 if (GNUNET_SYSERR != rh->was_transmitted)
309 GNUNET_SCHEDULER_cancel (r_task);
314 s_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
321 * Convert IP address to string without DNS resolution.
323 * @param af address family
324 * @param ip the address
325 * @param ip_len number of bytes in @a ip
326 * @return address as a string, NULL on error
333 char buf[INET6_ADDRSTRLEN];
338 if (ip_len != sizeof (struct in_addr))
346 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
352 if (ip_len != sizeof (struct in6_addr))
360 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
369 return GNUNET_strdup (buf);
374 * Adjust exponential back-off and reconnect to the service.
381 * Generic error handler, called with the appropriate error code and
382 * the same closure specified at the creation of the message queue.
383 * Not every message queue implementation supports an error handler.
386 * @param error error code
389 mq_error_handler (void *cls,
390 enum GNUNET_MQ_Error error)
393 GNUNET_MQ_destroy (mq);
395 LOG (GNUNET_ERROR_TYPE_DEBUG,
396 "MQ error %d, reconnecting\n",
403 * Process pending requests to the resolver.
408 struct GNUNET_RESOLVER_GetMessage *msg;
409 struct GNUNET_MQ_Envelope *env;
410 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
419 /* nothing to do, release socket really soon if there is nothing
420 * else happening... */
423 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
428 if (GNUNET_NO != rh->was_transmitted)
429 return; /* waiting for reply */
430 env = GNUNET_MQ_msg_extra (msg,
432 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
433 msg->direction = htonl (rh->direction);
434 msg->af = htonl (rh->af);
435 GNUNET_memcpy (&msg[1],
438 LOG (GNUNET_ERROR_TYPE_DEBUG,
439 "Transmitting DNS resolution request to DNS service\n");
442 rh->was_transmitted = GNUNET_YES;
447 * Check validity of response with a hostname for a DNS lookup.
450 * @param msg message with the hostname
453 check_response (void *cls,
454 const struct GNUNET_MessageHeader *msg)
459 /* implemented in #handle_response() for now */
465 * Check validity of response with a hostname for a DNS lookup.
466 * NOTE: right now rather messy, might want to use different
467 * message types for different response formats in the future.
470 * @param msg message with the response
473 handle_response (void *cls,
474 const struct GNUNET_MessageHeader *msg)
476 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
481 GNUNET_assert (NULL != rh);
482 size = ntohs (msg->size);
483 if (size == sizeof (struct GNUNET_MessageHeader))
485 LOG (GNUNET_ERROR_TYPE_DEBUG,
486 "Received empty response from DNS service\n");
487 /* message contains not data, just header; end of replies */
488 /* check if request was canceled */
489 if (GNUNET_SYSERR != rh->was_transmitted)
491 /* no reverse lookup was successful, return IP as string */
492 if (NULL != rh->name_callback)
494 if (GNUNET_NO == rh->received_response)
496 nret = no_resolve (rh->af,
499 rh->name_callback (rh->cls, nret);
502 /* finally, make termination call */
503 if (GNUNET_SYSERR != rh->was_transmitted)
504 rh->name_callback (rh->cls,
507 if ( (NULL != rh->addr_callback) &&
508 (GNUNET_SYSERR != rh->was_transmitted) )
509 rh->addr_callback (rh->cls,
513 rh->was_transmitted = GNUNET_NO;
514 GNUNET_RESOLVER_request_cancel (rh);
518 /* return reverse lookup results to caller */
519 if (NULL != rh->name_callback)
521 const char *hostname;
523 hostname = (const char *) &msg[1];
524 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
527 if (GNUNET_SYSERR != rh->was_transmitted)
528 rh->name_callback (rh->cls,
530 rh->was_transmitted = GNUNET_NO;
531 GNUNET_RESOLVER_request_cancel (rh);
532 GNUNET_MQ_destroy (mq);
537 LOG (GNUNET_ERROR_TYPE_DEBUG,
538 "Resolver returns `%s' for IP `%s'.\n",
540 GNUNET_a2s ((const void *) &rh[1],
542 if (rh->was_transmitted != GNUNET_SYSERR)
543 rh->name_callback (rh->cls,
545 rh->received_response = GNUNET_YES;
547 /* return lookup results to caller */
548 if (NULL != rh->addr_callback)
550 struct sockaddr_in v4;
551 struct sockaddr_in6 v6;
552 const struct sockaddr *sa;
558 ip_len = size - sizeof (struct GNUNET_MessageHeader);
559 if (ip_len == sizeof (struct in_addr))
561 memset (&v4, 0, sizeof (v4));
562 v4.sin_family = AF_INET;
563 v4.sin_addr = *(struct in_addr*) ip;
564 #if HAVE_SOCKADDR_IN_SIN_LEN
565 v4.sin_len = sizeof (v4);
568 sa = (const struct sockaddr *) &v4;
570 else if (ip_len == sizeof (struct in6_addr))
572 memset (&v6, 0, sizeof (v6));
573 v6.sin6_family = AF_INET6;
574 v6.sin6_addr = *(struct in6_addr*) ip;
575 #if HAVE_SOCKADDR_IN_SIN_LEN
576 v6.sin6_len = sizeof (v6);
579 sa = (const struct sockaddr *) &v6;
584 if (GNUNET_SYSERR != rh->was_transmitted)
585 rh->addr_callback (rh->cls,
588 rh->was_transmitted = GNUNET_NO;
589 GNUNET_RESOLVER_request_cancel (rh);
590 GNUNET_MQ_destroy (mq);
595 LOG (GNUNET_ERROR_TYPE_DEBUG,
596 "Received IP from DNS service\n");
597 if (GNUNET_SYSERR != rh->was_transmitted)
598 rh->addr_callback (rh->cls,
606 * We've been asked to lookup the address for a hostname and were
607 * given a valid numeric string. Perform the callbacks for the
610 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
613 numeric_resolution (void *cls)
615 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
616 struct sockaddr_in v4;
617 struct sockaddr_in6 v6;
618 const char *hostname;
621 memset (&v4, 0, sizeof (v4));
622 v4.sin_family = AF_INET;
623 #if HAVE_SOCKADDR_IN_SIN_LEN
624 v4.sin_len = sizeof (v4);
626 memset (&v6, 0, sizeof (v6));
627 v6.sin6_family = AF_INET6;
628 #if HAVE_SOCKADDR_IN_SIN_LEN
629 v6.sin6_len = sizeof (v6);
631 hostname = (const char *) &rh[1];
632 if ( ( (rh->af == AF_UNSPEC) ||
633 (rh->af == AF_INET) ) &&
634 (1 == inet_pton (AF_INET,
638 rh->addr_callback (rh->cls,
639 (const struct sockaddr *) &v4,
641 if ( (rh->af == AF_UNSPEC) &&
642 (GNUNET_SYSERR != rh->was_transmitted) &&
643 (1 == inet_pton (AF_INET6,
647 /* this can happen on some systems IF "hostname" is "localhost" */
648 rh->addr_callback (rh->cls,
649 (const struct sockaddr *) &v6,
652 if (GNUNET_SYSERR != rh->was_transmitted)
653 rh->addr_callback (rh->cls,
659 if ( ( (rh->af == AF_UNSPEC) ||
660 (rh->af == AF_INET6) ) &&
661 (1 == inet_pton (AF_INET6,
665 rh->addr_callback (rh->cls,
666 (const struct sockaddr *) &v6,
668 if (GNUNET_SYSERR != rh->was_transmitted)
669 rh->addr_callback (rh->cls,
675 /* why are we here? this task should not have been scheduled! */
682 * We've been asked to lookup the address for a hostname and were
683 * given a variant of "loopback". Perform the callbacks for the
684 * respective loopback numeric addresses.
686 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
689 loopback_resolution (void *cls)
691 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
692 struct sockaddr_in v4;
693 struct sockaddr_in6 v6;
696 memset (&v4, 0, sizeof (v4));
697 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
698 v4.sin_family = AF_INET;
699 #if HAVE_SOCKADDR_IN_SIN_LEN
700 v4.sin_len = sizeof (v4);
702 memset (&v6, 0, sizeof (v6));
703 v6.sin6_family = AF_INET6;
704 #if HAVE_SOCKADDR_IN_SIN_LEN
705 v6.sin6_len = sizeof (v6);
707 v6.sin6_addr = in6addr_loopback;
711 rh->addr_callback (rh->cls,
712 (const struct sockaddr *) &v4,
716 rh->addr_callback (rh->cls,
717 (const struct sockaddr *) &v6,
721 rh->addr_callback (rh->cls,
722 (const struct sockaddr *) &v6,
724 rh->addr_callback (rh->cls,
725 (const struct sockaddr *) &v4,
733 if (GNUNET_SYSERR != rh->was_transmitted)
734 rh->addr_callback (rh->cls,
737 LOG (GNUNET_ERROR_TYPE_DEBUG,
738 "Finished resolving hostname `%s'.\n",
739 (const char *) &rh[1]);
745 * Now try to reconnect to the resolver service.
750 reconnect_task (void *cls)
752 struct GNUNET_MQ_MessageHandler handlers[] = {
753 GNUNET_MQ_hd_var_size (response,
754 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
755 struct GNUNET_MessageHeader,
757 GNUNET_MQ_handler_end ()
762 if (NULL == req_head)
763 return; /* no work pending */
764 LOG (GNUNET_ERROR_TYPE_DEBUG,
765 "Trying to connect to DNS service\n");
766 mq = GNUNET_CLIENT_connect (resolver_cfg,
773 LOG (GNUNET_ERROR_TYPE_DEBUG,
774 "Failed to connect, will try again later\n");
783 * Adjust exponential back-off and reconnect to the service.
788 struct GNUNET_RESOLVER_RequestHandle *rh;
792 GNUNET_assert (NULL == mq);
793 if (NULL != (rh = req_head))
795 switch (rh->was_transmitted)
798 /* nothing more to do */
801 /* disconnected, transmit again! */
802 rh->was_transmitted = GNUNET_NO;
805 /* request was cancelled, remove entirely */
806 GNUNET_CONTAINER_DLL_remove (req_head,
817 LOG (GNUNET_ERROR_TYPE_DEBUG,
818 "Will try to connect to DNS service in %s\n",
819 GNUNET_STRINGS_relative_time_to_string (backoff,
821 GNUNET_assert (NULL != resolver_cfg);
822 r_task = GNUNET_SCHEDULER_add_delayed (backoff,
825 backoff = GNUNET_TIME_STD_BACKOFF (backoff);
830 * A DNS resolution timed out. Notify the application.
832 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
835 handle_lookup_timeout (void *cls)
837 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
840 if (GNUNET_NO == rh->direction)
842 LOG (GNUNET_ERROR_TYPE_INFO,
843 _("Timeout trying to resolve hostname `%s'.\n"),
844 (const char *) &rh[1]);
845 if (NULL != rh->addr_callback)
846 rh->addr_callback (rh->cls,
852 #if !defined(GNUNET_CULL_LOGGING)
853 char buf[INET6_ADDRSTRLEN];
855 LOG (GNUNET_ERROR_TYPE_INFO,
856 _("Timeout trying to resolve IP address `%s'.\n"),
858 (const void *) &rh[1],
862 if (GNUNET_NO == rh->received_response)
866 nret = no_resolve (rh->af,
869 if (NULL != rh->name_callback)
870 rh->name_callback (rh->cls, nret);
873 /* finally, make termination call */
874 if (NULL != rh->name_callback)
875 rh->name_callback (rh->cls,
878 rh->was_transmitted = GNUNET_NO;
879 GNUNET_RESOLVER_request_cancel (rh);
885 * Convert a string to one or more IP addresses.
887 * @param hostname the hostname to resolve
888 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
889 * @param callback function to call with addresses
890 * @param callback_cls closure for @a callback
891 * @param timeout how long to try resolving
892 * @return handle that can be used to cancel the request, NULL on error
894 struct GNUNET_RESOLVER_RequestHandle *
895 GNUNET_RESOLVER_ip_get (const char *hostname,
897 struct GNUNET_TIME_Relative timeout,
898 GNUNET_RESOLVER_AddressCallback callback,
901 struct GNUNET_RESOLVER_RequestHandle *rh;
906 slen = strlen (hostname) + 1;
907 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
908 GNUNET_MAX_MESSAGE_SIZE)
913 LOG (GNUNET_ERROR_TYPE_DEBUG,
914 "Trying to resolve hostname `%s'.\n",
916 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
918 rh->addr_callback = callback;
919 rh->cls = callback_cls;
920 GNUNET_memcpy (&rh[1],
924 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
925 rh->direction = GNUNET_NO;
926 /* first, check if this is a numeric address */
927 if ( ( (1 == inet_pton (AF_INET,
931 (af == AF_UNSPEC) ) ) ||
932 ( (1 == inet_pton (AF_INET6,
935 ( (af == AF_INET6) ||
936 (af == AF_UNSPEC)) ) )
938 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution,
942 /* then, check if this is a loopback address */
943 for (unsigned int i = 0;
946 if (0 == strcasecmp (loopback[i],
949 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution,
953 if (GNUNET_OK != check_config ())
958 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
959 &handle_lookup_timeout,
961 GNUNET_CONTAINER_DLL_insert_tail (req_head,
964 rh->was_queued = GNUNET_YES;
967 GNUNET_SCHEDULER_cancel (s_task);
976 * We've been asked to convert an address to a string without
977 * a reverse lookup, either because the client asked for it
978 * or because the DNS lookup hit a timeout. Do the numeric
979 * conversion and invoke the callback.
981 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
984 numeric_reverse (void *cls)
986 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
990 result = no_resolve (rh->af,
993 LOG (GNUNET_ERROR_TYPE_DEBUG,
994 "Resolver returns `%s'.\n",
998 rh->name_callback (rh->cls,
1000 GNUNET_free (result);
1002 rh->name_callback (rh->cls,
1004 if (NULL != rh->task)
1006 GNUNET_SCHEDULER_cancel (rh->task);
1014 * Get an IP address as a string.
1016 * @param sa host address
1017 * @param salen length of host address in @a sa
1018 * @param do_resolve use #GNUNET_NO to return numeric hostname
1019 * @param timeout how long to try resolving
1020 * @param callback function to call with hostnames
1021 * last callback is NULL when finished
1022 * @param cls closure for @a callback
1023 * @return handle that can be used to cancel the request
1025 struct GNUNET_RESOLVER_RequestHandle *
1026 GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
1029 struct GNUNET_TIME_Relative timeout,
1030 GNUNET_RESOLVER_HostnameCallback callback,
1033 struct GNUNET_RESOLVER_RequestHandle *rh;
1037 if (GNUNET_OK != check_config ())
1039 LOG (GNUNET_ERROR_TYPE_ERROR,
1040 _("Resolver not configured correctly.\n"));
1044 switch (sa->sa_family)
1047 GNUNET_assert (salen == sizeof (struct sockaddr_in));
1048 ip_len = sizeof (struct in_addr);
1049 ip = &((const struct sockaddr_in*)sa)->sin_addr;
1052 GNUNET_assert (salen == sizeof (struct sockaddr_in6));
1053 ip_len = sizeof (struct in6_addr);
1054 ip = &((const struct sockaddr_in6*)sa)->sin6_addr;
1060 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
1061 rh->name_callback = callback;
1063 rh->af = sa->sa_family;
1064 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1065 GNUNET_memcpy (&rh[1],
1068 rh->data_len = ip_len;
1069 rh->direction = GNUNET_YES;
1070 rh->received_response = GNUNET_NO;
1071 if (GNUNET_NO == do_resolve)
1073 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse,
1077 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
1078 &handle_lookup_timeout,
1080 GNUNET_CONTAINER_DLL_insert_tail (req_head,
1083 rh->was_queued = GNUNET_YES;
1086 GNUNET_SCHEDULER_cancel (s_task);
1089 process_requests ();
1095 * Get local fully qualified af name
1100 GNUNET_RESOLVER_local_fqdn_get ()
1102 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1104 if (0 != gethostname (hostname,
1105 sizeof (hostname) - 1))
1107 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1111 LOG (GNUNET_ERROR_TYPE_DEBUG,
1112 "Resolving our FQDN `%s'\n",
1114 #if HAVE_GETADDRINFO
1116 struct addrinfo *ai;
1120 if (0 != (ret = getaddrinfo (hostname,
1125 LOG (GNUNET_ERROR_TYPE_ERROR,
1126 _("Could not resolve our FQDN: %s\n"),
1127 gai_strerror (ret));
1130 if (NULL != ai->ai_canonname)
1131 rval = GNUNET_strdup (ai->ai_canonname);
1133 rval = GNUNET_strdup (hostname);
1137 #elif HAVE_GETHOSTBYNAME2
1139 struct hostent *host;
1141 host = gethostbyname2 (hostname,
1144 host = gethostbyname2 (hostname,
1148 LOG (GNUNET_ERROR_TYPE_ERROR,
1149 _("Could not resolve our FQDN: %s\n"),
1150 hstrerror (h_errno));
1153 return GNUNET_strdup (host->h_name);
1155 #elif HAVE_GETHOSTBYNAME
1157 struct hostent *host;
1159 host = gethostbyname (hostname);
1162 LOG (GNUNET_ERROR_TYPE_ERROR,
1163 _("Could not resolve our FQDN: %s\n"),
1164 hstrerror (h_errno));
1167 return GNUNET_strdup (host->h_name);
1170 /* fallback: just hope name is already FQDN */
1171 return GNUNET_strdup (hostname);
1177 * Looking our own hostname.
1179 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
1180 * @param timeout how long to try resolving
1181 * @param callback function to call with addresses
1182 * @param cls closure for @a callback
1183 * @return handle that can be used to cancel the request, NULL on error
1185 struct GNUNET_RESOLVER_RequestHandle *
1186 GNUNET_RESOLVER_hostname_resolve (int af,
1187 struct GNUNET_TIME_Relative timeout,
1188 GNUNET_RESOLVER_AddressCallback callback,
1191 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1193 if (0 != gethostname (hostname, sizeof (hostname) - 1))
1195 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1199 LOG (GNUNET_ERROR_TYPE_DEBUG,
1200 "Resolving our hostname `%s'\n",
1202 return GNUNET_RESOLVER_ip_get (hostname,
1211 * Cancel a request that is still pending with the resolver.
1212 * Note that a client MUST NOT cancel a request that has
1213 * been completed (i.e, the callback has been called to
1214 * signal timeout or the final result).
1216 * @param rh handle of request to cancel
1219 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
1221 if (GNUNET_NO == rh->direction)
1222 LOG (GNUNET_ERROR_TYPE_DEBUG,
1223 "Asked to cancel request to resolve hostname `%s'.\n",
1224 (const char *) &rh[1]);
1225 if (NULL != rh->task)
1227 GNUNET_SCHEDULER_cancel (rh->task);
1230 if (GNUNET_NO == rh->was_transmitted)
1232 if (GNUNET_YES == rh->was_queued)
1233 GNUNET_CONTAINER_DLL_remove (req_head,
1237 check_disconnect ();
1240 GNUNET_assert (GNUNET_YES == rh->was_transmitted);
1241 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
1242 check_disconnect ();
1246 /* end of resolver_api.c */