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 rh->name_callback (rh->cls,
506 if (NULL != rh->addr_callback)
507 rh->addr_callback (rh->cls,
511 rh->was_transmitted = GNUNET_NO;
512 GNUNET_RESOLVER_request_cancel (rh);
516 /* return reverse lookup results to caller */
517 if (NULL != rh->name_callback)
519 const char *hostname;
521 hostname = (const char *) &msg[1];
522 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
525 if (GNUNET_SYSERR != rh->was_transmitted)
526 rh->name_callback (rh->cls,
528 rh->was_transmitted = GNUNET_NO;
529 GNUNET_RESOLVER_request_cancel (rh);
530 GNUNET_MQ_destroy (mq);
535 LOG (GNUNET_ERROR_TYPE_DEBUG,
536 "Resolver returns `%s' for IP `%s'.\n",
538 GNUNET_a2s ((const void *) &rh[1],
540 if (rh->was_transmitted != GNUNET_SYSERR)
541 rh->name_callback (rh->cls,
543 rh->received_response = GNUNET_YES;
545 /* return lookup results to caller */
546 if (NULL != rh->addr_callback)
548 struct sockaddr_in v4;
549 struct sockaddr_in6 v6;
550 const struct sockaddr *sa;
556 ip_len = size - sizeof (struct GNUNET_MessageHeader);
557 if (ip_len == sizeof (struct in_addr))
559 memset (&v4, 0, sizeof (v4));
560 v4.sin_family = AF_INET;
561 v4.sin_addr = *(struct in_addr*) ip;
562 #if HAVE_SOCKADDR_IN_SIN_LEN
563 v4.sin_len = sizeof (v4);
566 sa = (const struct sockaddr *) &v4;
568 else if (ip_len == sizeof (struct in6_addr))
570 memset (&v6, 0, sizeof (v6));
571 v6.sin6_family = AF_INET6;
572 v6.sin6_addr = *(struct in6_addr*) ip;
573 #if HAVE_SOCKADDR_IN_SIN_LEN
574 v6.sin6_len = sizeof (v6);
577 sa = (const struct sockaddr *) &v6;
582 if (GNUNET_SYSERR != rh->was_transmitted)
583 rh->addr_callback (rh->cls,
586 rh->was_transmitted = GNUNET_NO;
587 GNUNET_RESOLVER_request_cancel (rh);
588 GNUNET_MQ_destroy (mq);
593 LOG (GNUNET_ERROR_TYPE_DEBUG,
594 "Received IP from DNS service\n");
595 if (GNUNET_SYSERR != rh->was_transmitted)
596 rh->addr_callback (rh->cls,
604 * We've been asked to lookup the address for a hostname and were
605 * given a valid numeric string. Perform the callbacks for the
608 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
611 numeric_resolution (void *cls)
613 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
614 struct sockaddr_in v4;
615 struct sockaddr_in6 v6;
616 const char *hostname;
619 memset (&v4, 0, sizeof (v4));
620 v4.sin_family = AF_INET;
621 #if HAVE_SOCKADDR_IN_SIN_LEN
622 v4.sin_len = sizeof (v4);
624 memset (&v6, 0, sizeof (v6));
625 v6.sin6_family = AF_INET6;
626 #if HAVE_SOCKADDR_IN_SIN_LEN
627 v6.sin6_len = sizeof (v6);
629 hostname = (const char *) &rh[1];
630 if ( ( (rh->af == AF_UNSPEC) ||
631 (rh->af == AF_INET) ) &&
632 (1 == inet_pton (AF_INET,
636 rh->addr_callback (rh->cls,
637 (const struct sockaddr *) &v4,
639 if ( (rh->af == AF_UNSPEC) &&
640 (1 == inet_pton (AF_INET6,
644 /* this can happen on some systems IF "hostname" is "localhost" */
645 rh->addr_callback (rh->cls,
646 (const struct sockaddr *) &v6,
649 rh->addr_callback (rh->cls,
655 if ( ( (rh->af == AF_UNSPEC) ||
656 (rh->af == AF_INET6) ) &&
657 (1 == inet_pton (AF_INET6,
661 rh->addr_callback (rh->cls,
662 (const struct sockaddr *) &v6,
664 rh->addr_callback (rh->cls,
670 /* why are we here? this task should not have been scheduled! */
677 * We've been asked to lookup the address for a hostname and were
678 * given a variant of "loopback". Perform the callbacks for the
679 * respective loopback numeric addresses.
681 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
684 loopback_resolution (void *cls)
686 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
687 struct sockaddr_in v4;
688 struct sockaddr_in6 v6;
691 memset (&v4, 0, sizeof (v4));
692 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
693 v4.sin_family = AF_INET;
694 #if HAVE_SOCKADDR_IN_SIN_LEN
695 v4.sin_len = sizeof (v4);
697 memset (&v6, 0, sizeof (v6));
698 v6.sin6_family = AF_INET6;
699 #if HAVE_SOCKADDR_IN_SIN_LEN
700 v6.sin6_len = sizeof (v6);
702 v6.sin6_addr = in6addr_loopback;
706 rh->addr_callback (rh->cls,
707 (const struct sockaddr *) &v4,
711 rh->addr_callback (rh->cls,
712 (const struct sockaddr *) &v6,
716 rh->addr_callback (rh->cls,
717 (const struct sockaddr *) &v6,
719 rh->addr_callback (rh->cls,
720 (const struct sockaddr *) &v4,
728 rh->addr_callback (rh->cls,
731 LOG (GNUNET_ERROR_TYPE_DEBUG,
732 "Finished resolving hostname `%s'.\n",
733 (const char *) &rh[1]);
739 * Now try to reconnect to the resolver service.
744 reconnect_task (void *cls)
746 struct GNUNET_MQ_MessageHandler handlers[] = {
747 GNUNET_MQ_hd_var_size (response,
748 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
749 struct GNUNET_MessageHeader,
751 GNUNET_MQ_handler_end ()
756 if (NULL == req_head)
757 return; /* no work pending */
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "Trying to connect to DNS service\n");
760 mq = GNUNET_CLIENT_connect (resolver_cfg,
767 LOG (GNUNET_ERROR_TYPE_DEBUG,
768 "Failed to connect, will try again later\n");
777 * Adjust exponential back-off and reconnect to the service.
782 struct GNUNET_RESOLVER_RequestHandle *rh;
786 GNUNET_assert (NULL == mq);
787 if (NULL != (rh = req_head))
789 switch (rh->was_transmitted)
792 /* nothing more to do */
795 /* disconnected, transmit again! */
796 rh->was_transmitted = GNUNET_NO;
799 /* request was cancelled, remove entirely */
800 GNUNET_CONTAINER_DLL_remove (req_head,
811 LOG (GNUNET_ERROR_TYPE_DEBUG,
812 "Will try to connect to DNS service in %s\n",
813 GNUNET_STRINGS_relative_time_to_string (backoff,
815 GNUNET_assert (NULL != resolver_cfg);
816 r_task = GNUNET_SCHEDULER_add_delayed (backoff,
819 backoff = GNUNET_TIME_STD_BACKOFF (backoff);
824 * A DNS resolution timed out. Notify the application.
826 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
829 handle_lookup_timeout (void *cls)
831 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
834 if (GNUNET_NO == rh->direction)
836 LOG (GNUNET_ERROR_TYPE_INFO,
837 _("Timeout trying to resolve hostname `%s'.\n"),
838 (const char *) &rh[1]);
839 if (NULL != rh->addr_callback)
840 rh->addr_callback (rh->cls,
846 #if !defined(GNUNET_CULL_LOGGING)
847 char buf[INET6_ADDRSTRLEN];
849 LOG (GNUNET_ERROR_TYPE_INFO,
850 _("Timeout trying to resolve IP address `%s'.\n"),
852 (const void *) &rh[1],
856 if (GNUNET_NO == rh->received_response)
860 nret = no_resolve (rh->af,
863 if (NULL != rh->name_callback)
864 rh->name_callback (rh->cls, nret);
867 /* finally, make termination call */
868 if (NULL != rh->name_callback)
869 rh->name_callback (rh->cls,
872 rh->was_transmitted = GNUNET_NO;
873 GNUNET_RESOLVER_request_cancel (rh);
879 * Convert a string to one or more IP addresses.
881 * @param hostname the hostname to resolve
882 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
883 * @param callback function to call with addresses
884 * @param callback_cls closure for @a callback
885 * @param timeout how long to try resolving
886 * @return handle that can be used to cancel the request, NULL on error
888 struct GNUNET_RESOLVER_RequestHandle *
889 GNUNET_RESOLVER_ip_get (const char *hostname,
891 struct GNUNET_TIME_Relative timeout,
892 GNUNET_RESOLVER_AddressCallback callback,
895 struct GNUNET_RESOLVER_RequestHandle *rh;
900 slen = strlen (hostname) + 1;
901 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
902 GNUNET_MAX_MESSAGE_SIZE)
907 LOG (GNUNET_ERROR_TYPE_DEBUG,
908 "Trying to resolve hostname `%s'.\n",
910 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
912 rh->addr_callback = callback;
913 rh->cls = callback_cls;
914 GNUNET_memcpy (&rh[1],
918 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
919 rh->direction = GNUNET_NO;
920 /* first, check if this is a numeric address */
921 if ( ( (1 == inet_pton (AF_INET,
925 (af == AF_UNSPEC) ) ) ||
926 ( (1 == inet_pton (AF_INET6,
929 ( (af == AF_INET6) ||
930 (af == AF_UNSPEC)) ) )
932 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution,
936 /* then, check if this is a loopback address */
937 for (unsigned int i = 0;
940 if (0 == strcasecmp (loopback[i],
943 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution,
947 if (GNUNET_OK != check_config ())
952 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
953 &handle_lookup_timeout,
955 GNUNET_CONTAINER_DLL_insert_tail (req_head,
958 rh->was_queued = GNUNET_YES;
961 GNUNET_SCHEDULER_cancel (s_task);
970 * We've been asked to convert an address to a string without
971 * a reverse lookup, either because the client asked for it
972 * or because the DNS lookup hit a timeout. Do the numeric
973 * conversion and invoke the callback.
975 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
978 numeric_reverse (void *cls)
980 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
984 result = no_resolve (rh->af,
987 LOG (GNUNET_ERROR_TYPE_DEBUG,
988 "Resolver returns `%s'.\n",
992 rh->name_callback (rh->cls,
994 GNUNET_free (result);
996 rh->name_callback (rh->cls,
998 if (NULL != rh->task)
1000 GNUNET_SCHEDULER_cancel (rh->task);
1008 * Get an IP address as a string.
1010 * @param sa host address
1011 * @param salen length of host address in @a sa
1012 * @param do_resolve use #GNUNET_NO to return numeric hostname
1013 * @param timeout how long to try resolving
1014 * @param callback function to call with hostnames
1015 * last callback is NULL when finished
1016 * @param cls closure for @a callback
1017 * @return handle that can be used to cancel the request
1019 struct GNUNET_RESOLVER_RequestHandle *
1020 GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
1023 struct GNUNET_TIME_Relative timeout,
1024 GNUNET_RESOLVER_HostnameCallback callback,
1027 struct GNUNET_RESOLVER_RequestHandle *rh;
1031 if (GNUNET_OK != check_config ())
1033 LOG (GNUNET_ERROR_TYPE_ERROR,
1034 _("Resolver not configured correctly.\n"));
1038 switch (sa->sa_family)
1041 GNUNET_assert (salen == sizeof (struct sockaddr_in));
1042 ip_len = sizeof (struct in_addr);
1043 ip = &((const struct sockaddr_in*)sa)->sin_addr;
1046 GNUNET_assert (salen == sizeof (struct sockaddr_in6));
1047 ip_len = sizeof (struct in6_addr);
1048 ip = &((const struct sockaddr_in6*)sa)->sin6_addr;
1054 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
1055 rh->name_callback = callback;
1057 rh->af = sa->sa_family;
1058 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1059 GNUNET_memcpy (&rh[1],
1062 rh->data_len = ip_len;
1063 rh->direction = GNUNET_YES;
1064 rh->received_response = GNUNET_NO;
1065 if (GNUNET_NO == do_resolve)
1067 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse,
1071 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
1072 &handle_lookup_timeout,
1074 GNUNET_CONTAINER_DLL_insert_tail (req_head,
1077 rh->was_queued = GNUNET_YES;
1080 GNUNET_SCHEDULER_cancel (s_task);
1083 process_requests ();
1089 * Get local fully qualified af name
1094 GNUNET_RESOLVER_local_fqdn_get ()
1096 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1098 if (0 != gethostname (hostname,
1099 sizeof (hostname) - 1))
1101 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1105 LOG (GNUNET_ERROR_TYPE_DEBUG,
1106 "Resolving our FQDN `%s'\n",
1108 #if HAVE_GETADDRINFO
1110 struct addrinfo *ai;
1114 if (0 != (ret = getaddrinfo (hostname,
1119 LOG (GNUNET_ERROR_TYPE_ERROR,
1120 _("Could not resolve our FQDN: %s\n"),
1121 gai_strerror (ret));
1124 if (NULL != ai->ai_canonname)
1125 rval = GNUNET_strdup (ai->ai_canonname);
1127 rval = GNUNET_strdup (hostname);
1131 #elif HAVE_GETHOSTBYNAME2
1133 struct hostent *host;
1135 host = gethostbyname2 (hostname,
1138 host = gethostbyname2 (hostname,
1142 LOG (GNUNET_ERROR_TYPE_ERROR,
1143 _("Could not resolve our FQDN: %s\n"),
1144 hstrerror (h_errno));
1147 return GNUNET_strdup (host->h_name);
1149 #elif HAVE_GETHOSTBYNAME
1151 struct hostent *host;
1153 host = gethostbyname (hostname);
1156 LOG (GNUNET_ERROR_TYPE_ERROR,
1157 _("Could not resolve our FQDN: %s\n"),
1158 hstrerror (h_errno));
1161 return GNUNET_strdup (host->h_name);
1164 /* fallback: just hope name is already FQDN */
1165 return GNUNET_strdup (hostname);
1171 * Looking our own hostname.
1173 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
1174 * @param timeout how long to try resolving
1175 * @param callback function to call with addresses
1176 * @param cls closure for @a callback
1177 * @return handle that can be used to cancel the request, NULL on error
1179 struct GNUNET_RESOLVER_RequestHandle *
1180 GNUNET_RESOLVER_hostname_resolve (int af,
1181 struct GNUNET_TIME_Relative timeout,
1182 GNUNET_RESOLVER_AddressCallback callback,
1185 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1187 if (0 != gethostname (hostname, sizeof (hostname) - 1))
1189 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1193 LOG (GNUNET_ERROR_TYPE_DEBUG,
1194 "Resolving our hostname `%s'\n",
1196 return GNUNET_RESOLVER_ip_get (hostname,
1205 * Cancel a request that is still pending with the resolver.
1206 * Note that a client MUST NOT cancel a request that has
1207 * been completed (i.e, the callback has been called to
1208 * signal timeout or the final result).
1210 * @param rh handle of request to cancel
1213 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
1215 if (GNUNET_NO == rh->direction)
1216 LOG (GNUNET_ERROR_TYPE_DEBUG,
1217 "Asked to cancel request to resolve hostname `%s'.\n",
1218 (const char *) &rh[1]);
1219 if (NULL != rh->task)
1221 GNUNET_SCHEDULER_cancel (rh->task);
1224 if (GNUNET_NO == rh->was_transmitted)
1226 if (GNUNET_YES == rh->was_queued)
1227 GNUNET_CONTAINER_DLL_remove (req_head,
1231 check_disconnect ();
1234 GNUNET_assert (GNUNET_YES == rh->was_transmitted);
1235 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
1236 check_disconnect ();
1240 /* end of resolver_api.c */