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.
183 struct sockaddr_in v4;
184 struct sockaddr_in6 v6;
186 memset (&v4, 0, sizeof (v4));
187 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
188 v4.sin_family = AF_INET;
189 #if HAVE_SOCKADDR_IN_SIN_LEN
190 v4.sin_len = sizeof (v4);
192 memset (&v6, 0, sizeof (v6));
193 v6.sin6_family = AF_INET6;
194 #if HAVE_SOCKADDR_IN_SIN_LEN
195 v6.sin6_len = sizeof (v6);
198 GNUNET_CONFIGURATION_get_value_string (resolver_cfg,
203 LOG (GNUNET_ERROR_TYPE_INFO,
204 _("Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"),
207 return GNUNET_SYSERR;
209 if ((1 == inet_pton (AF_INET, hostname, &v4)) ||
210 (1 == inet_pton (AF_INET6, hostname, &v6)))
212 GNUNET_free (hostname);
216 while (NULL != loopback[i])
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 struct GNUNET_RESOLVER_RequestHandle *rh;
302 for (rh = req_head; NULL != rh; rh = rh->next)
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))
339 inet_ntop (AF_INET, ip, buf, sizeof (buf)))
341 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
347 if (ip_len != sizeof (struct in6_addr))
350 inet_ntop (AF_INET6, ip, buf, sizeof (buf)))
352 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
361 return GNUNET_strdup (buf);
366 * Adjust exponential back-off and reconnect to the service.
373 * Generic error handler, called with the appropriate error code and
374 * the same closure specified at the creation of the message queue.
375 * Not every message queue implementation supports an error handler.
378 * @param error error code
381 mq_error_handler (void *cls,
382 enum GNUNET_MQ_Error error)
384 GNUNET_MQ_destroy (mq);
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "MQ error, reconnecting\n");
393 * Process pending requests to the resolver.
398 struct GNUNET_RESOLVER_GetMessage *msg;
399 struct GNUNET_MQ_Envelope *env;
400 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
409 /* nothing to do, release socket really soon if there is nothing
410 * else happening... */
412 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
417 if (GNUNET_NO != rh->was_transmitted)
418 return; /* waiting for reply */
419 env = GNUNET_MQ_msg_extra (msg,
421 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
422 msg->direction = htonl (rh->direction);
423 msg->af = htonl (rh->af);
424 GNUNET_memcpy (&msg[1],
427 LOG (GNUNET_ERROR_TYPE_DEBUG,
428 "Transmitting DNS resolution request to DNS service\n");
431 rh->was_transmitted = GNUNET_YES;
436 * Check validity of response with a hostname for a DNS lookup.
439 * @param msg message with the hostname
442 check_response (void *cls,
443 const struct GNUNET_MessageHeader *msg)
445 /* implemented in #handle_response() for now */
451 * Check validity of response with a hostname for a DNS lookup.
452 * NOTE: right now rather messy, might want to use different
453 * message types for different response formats in the future.
456 * @param msg message with the response
459 handle_response (void *cls,
460 const struct GNUNET_MessageHeader *msg)
462 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
466 size = ntohs (msg->size);
467 if (size == sizeof (struct GNUNET_MessageHeader))
469 LOG (GNUNET_ERROR_TYPE_DEBUG,
470 "Received empty response from DNS service\n");
471 /* message contains not data, just header; end of replies */
472 /* check if request was canceled */
473 if (GNUNET_SYSERR != rh->was_transmitted)
475 /* no reverse lookup was successful, return IP as string */
476 if (NULL != rh->name_callback)
478 if (GNUNET_NO == rh->received_response)
480 nret = no_resolve (rh->af,
483 rh->name_callback (rh->cls, nret);
486 /* finally, make termination call */
487 rh->name_callback (rh->cls,
490 if (NULL != rh->addr_callback)
491 rh->addr_callback (rh->cls,
495 rh->was_transmitted = GNUNET_NO;
496 GNUNET_RESOLVER_request_cancel (rh);
500 /* return reverse lookup results to caller */
501 if (NULL != rh->name_callback)
503 const char *hostname;
505 hostname = (const char *) &msg[1];
506 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
509 if (GNUNET_SYSERR != rh->was_transmitted)
510 rh->name_callback (rh->cls,
512 rh->was_transmitted = GNUNET_NO;
513 GNUNET_RESOLVER_request_cancel (rh);
514 GNUNET_MQ_destroy (mq);
519 LOG (GNUNET_ERROR_TYPE_DEBUG,
520 "Resolver returns `%s' for IP `%s'.\n",
522 GNUNET_a2s ((const void *) &rh[1],
524 if (rh->was_transmitted != GNUNET_SYSERR)
525 rh->name_callback (rh->cls,
527 rh->received_response = GNUNET_YES;
529 /* return lookup results to caller */
530 if (NULL != rh->addr_callback)
532 struct sockaddr_in v4;
533 struct sockaddr_in6 v6;
534 const struct sockaddr *sa;
540 ip_len = size - sizeof (struct GNUNET_MessageHeader);
541 if (ip_len == sizeof (struct in_addr))
543 memset (&v4, 0, sizeof (v4));
544 v4.sin_family = AF_INET;
545 v4.sin_addr = *(struct in_addr*) ip;
546 #if HAVE_SOCKADDR_IN_SIN_LEN
547 v4.sin_len = sizeof (v4);
550 sa = (const struct sockaddr *) &v4;
552 else if (ip_len == sizeof (struct in6_addr))
554 memset (&v6, 0, sizeof (v6));
555 v6.sin6_family = AF_INET6;
556 v6.sin6_addr = *(struct in6_addr*) ip;
557 #if HAVE_SOCKADDR_IN_SIN_LEN
558 v6.sin6_len = sizeof (v6);
561 sa = (const struct sockaddr *) &v6;
566 if (GNUNET_SYSERR != rh->was_transmitted)
567 rh->addr_callback (rh->cls,
570 rh->was_transmitted = GNUNET_NO;
571 GNUNET_RESOLVER_request_cancel (rh);
572 GNUNET_MQ_destroy (mq);
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Received IP from DNS service\n");
579 if (GNUNET_SYSERR != rh->was_transmitted)
580 rh->addr_callback (rh->cls,
588 * We've been asked to lookup the address for a hostname and were
589 * given a valid numeric string. Perform the callbacks for the
592 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
595 numeric_resolution (void *cls)
597 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
598 struct sockaddr_in v4;
599 struct sockaddr_in6 v6;
600 const char *hostname;
603 memset (&v4, 0, sizeof (v4));
604 v4.sin_family = AF_INET;
605 #if HAVE_SOCKADDR_IN_SIN_LEN
606 v4.sin_len = sizeof (v4);
608 memset (&v6, 0, sizeof (v6));
609 v6.sin6_family = AF_INET6;
610 #if HAVE_SOCKADDR_IN_SIN_LEN
611 v6.sin6_len = sizeof (v6);
613 hostname = (const char *) &rh[1];
614 if (((rh->af == AF_UNSPEC) || (rh->af == AF_INET)) &&
615 (1 == inet_pton (AF_INET, hostname, &v4.sin_addr)))
617 rh->addr_callback (rh->cls,
618 (const struct sockaddr *) &v4,
620 if ((rh->af == AF_UNSPEC) &&
621 (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr)))
623 /* this can happen on some systems IF "hostname" is "localhost" */
624 rh->addr_callback (rh->cls,
625 (const struct sockaddr *) &v6,
628 rh->addr_callback (rh->cls,
634 if ( ( (rh->af == AF_UNSPEC) ||
635 (rh->af == AF_INET6) ) &&
636 (1 == inet_pton (AF_INET6,
640 rh->addr_callback (rh->cls,
641 (const struct sockaddr *) &v6,
643 rh->addr_callback (rh->cls,
649 /* why are we here? this task should not have been scheduled! */
656 * We've been asked to lookup the address for a hostname and were
657 * given a variant of "loopback". Perform the callbacks for the
658 * respective loopback numeric addresses.
660 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
663 loopback_resolution (void *cls)
665 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
666 struct sockaddr_in v4;
667 struct sockaddr_in6 v6;
670 memset (&v4, 0, sizeof (v4));
671 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
672 v4.sin_family = AF_INET;
673 #if HAVE_SOCKADDR_IN_SIN_LEN
674 v4.sin_len = sizeof (v4);
676 memset (&v6, 0, sizeof (v6));
677 v6.sin6_family = AF_INET6;
678 #if HAVE_SOCKADDR_IN_SIN_LEN
679 v6.sin6_len = sizeof (v6);
681 v6.sin6_addr = in6addr_loopback;
685 rh->addr_callback (rh->cls,
686 (const struct sockaddr *) &v4,
690 rh->addr_callback (rh->cls,
691 (const struct sockaddr *) &v6,
695 rh->addr_callback (rh->cls,
696 (const struct sockaddr *) &v6,
698 rh->addr_callback (rh->cls,
699 (const struct sockaddr *) &v4,
707 rh->addr_callback (rh->cls,
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "Finished resolving hostname `%s'.\n",
712 (const char *) &rh[1]);
718 * Now try to reconnect to the resolver service.
723 reconnect_task (void *cls)
725 struct GNUNET_MQ_MessageHandler handlers[] = {
726 GNUNET_MQ_hd_var_size (response,
727 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
728 struct GNUNET_MessageHeader,
730 GNUNET_MQ_handler_end ()
734 if (NULL == req_head)
735 return; /* no work pending */
736 LOG (GNUNET_ERROR_TYPE_DEBUG,
737 "Trying to connect to DNS service\n");
738 mq = GNUNET_CLIENT_connect (resolver_cfg,
745 LOG (GNUNET_ERROR_TYPE_DEBUG,
746 "Failed to connect, will try again later\n");
755 * Adjust exponential back-off and reconnect to the service.
760 struct GNUNET_RESOLVER_RequestHandle *rh;
764 GNUNET_assert (NULL == mq);
765 if (NULL != (rh = req_head))
767 switch (rh->was_transmitted)
770 /* nothing more to do */
773 /* disconnected, transmit again! */
774 rh->was_transmitted = GNUNET_NO;
777 /* request was cancelled, remove entirely */
778 GNUNET_CONTAINER_DLL_remove (req_head,
789 LOG (GNUNET_ERROR_TYPE_DEBUG,
790 "Will try to connect to DNS service in %s\n",
791 GNUNET_STRINGS_relative_time_to_string (backoff,
793 GNUNET_assert (NULL != resolver_cfg);
794 r_task = GNUNET_SCHEDULER_add_delayed (backoff,
797 backoff = GNUNET_TIME_STD_BACKOFF (backoff);
802 * A DNS resolution timed out. Notify the application.
804 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
807 handle_lookup_timeout (void *cls)
809 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
812 if (GNUNET_NO == rh->direction)
814 LOG (GNUNET_ERROR_TYPE_INFO,
815 _("Timeout trying to resolve hostname `%s'.\n"),
816 (const char *) &rh[1]);
817 if (NULL != rh->addr_callback)
818 rh->addr_callback (rh->cls,
824 char buf[INET6_ADDRSTRLEN];
826 LOG (GNUNET_ERROR_TYPE_INFO,
827 _("Timeout trying to resolve IP address `%s'.\n"),
829 (const void *) &rh[1],
832 if (GNUNET_NO == rh->received_response)
836 nret = no_resolve (rh->af,
839 if (NULL != rh->name_callback)
840 rh->name_callback (rh->cls, nret);
843 /* finally, make termination call */
844 if (NULL != rh->name_callback)
845 rh->name_callback (rh->cls,
848 rh->was_transmitted = GNUNET_NO;
849 GNUNET_RESOLVER_request_cancel (rh);
855 * Convert a string to one or more IP addresses.
857 * @param hostname the hostname to resolve
858 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
859 * @param callback function to call with addresses
860 * @param callback_cls closure for @a callback
861 * @param timeout how long to try resolving
862 * @return handle that can be used to cancel the request, NULL on error
864 struct GNUNET_RESOLVER_RequestHandle *
865 GNUNET_RESOLVER_ip_get (const char *hostname,
867 struct GNUNET_TIME_Relative timeout,
868 GNUNET_RESOLVER_AddressCallback callback,
871 struct GNUNET_RESOLVER_RequestHandle *rh;
877 slen = strlen (hostname) + 1;
878 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
879 GNUNET_MAX_MESSAGE_SIZE)
884 LOG (GNUNET_ERROR_TYPE_DEBUG,
885 "Trying to resolve hostname `%s'.\n",
887 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
889 rh->addr_callback = callback;
890 rh->cls = callback_cls;
891 GNUNET_memcpy (&rh[1],
895 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
896 rh->direction = GNUNET_NO;
897 /* first, check if this is a numeric address */
898 if (((1 == inet_pton (AF_INET, hostname, &v4)) &&
899 ((af == AF_INET) || (af == AF_UNSPEC))) ||
900 ((1 == inet_pton (AF_INET6, hostname, &v6)) &&
901 ((af == AF_INET6) || (af == AF_UNSPEC))))
903 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution,
907 /* then, check if this is a loopback address */
909 while (NULL != loopback[i])
910 if (0 == strcasecmp (loopback[i++],
913 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution,
917 if (GNUNET_OK != check_config ())
922 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
923 &handle_lookup_timeout,
925 GNUNET_CONTAINER_DLL_insert_tail (req_head,
928 rh->was_queued = GNUNET_YES;
931 GNUNET_SCHEDULER_cancel (s_task);
940 * We've been asked to convert an address to a string without
941 * a reverse lookup, either because the client asked for it
942 * or because the DNS lookup hit a timeout. Do the numeric
943 * conversion and invoke the callback.
945 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
948 numeric_reverse (void *cls)
950 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
954 result = no_resolve (rh->af,
957 LOG (GNUNET_ERROR_TYPE_DEBUG,
958 "Resolver returns `%s'.\n",
962 rh->name_callback (rh->cls,
964 GNUNET_free (result);
966 rh->name_callback (rh->cls,
968 if (NULL != rh->task)
970 GNUNET_SCHEDULER_cancel (rh->task);
978 * Get an IP address as a string.
980 * @param sa host address
981 * @param salen length of host address in @a sa
982 * @param do_resolve use #GNUNET_NO to return numeric hostname
983 * @param timeout how long to try resolving
984 * @param callback function to call with hostnames
985 * last callback is NULL when finished
986 * @param cls closure for @a callback
987 * @return handle that can be used to cancel the request
989 struct GNUNET_RESOLVER_RequestHandle *
990 GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
993 struct GNUNET_TIME_Relative timeout,
994 GNUNET_RESOLVER_HostnameCallback callback,
997 struct GNUNET_RESOLVER_RequestHandle *rh;
1001 if (GNUNET_OK != check_config ())
1003 LOG (GNUNET_ERROR_TYPE_ERROR,
1004 _("Resolver not configured correctly.\n"));
1008 switch (sa->sa_family)
1011 GNUNET_assert (salen == sizeof (struct sockaddr_in));
1012 ip_len = sizeof (struct in_addr);
1013 ip = &((const struct sockaddr_in*)sa)->sin_addr;
1016 GNUNET_assert (salen == sizeof (struct sockaddr_in6));
1017 ip_len = sizeof (struct in6_addr);
1018 ip = &((const struct sockaddr_in6*)sa)->sin6_addr;
1024 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
1025 rh->name_callback = callback;
1027 rh->af = sa->sa_family;
1028 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1029 GNUNET_memcpy (&rh[1],
1032 rh->data_len = ip_len;
1033 rh->direction = GNUNET_YES;
1034 rh->received_response = GNUNET_NO;
1035 if (GNUNET_NO == do_resolve)
1037 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse,
1041 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
1042 &handle_lookup_timeout,
1044 GNUNET_CONTAINER_DLL_insert_tail (req_head,
1047 rh->was_queued = GNUNET_YES;
1050 GNUNET_SCHEDULER_cancel (s_task);
1053 process_requests ();
1059 * Get local fully qualified af name
1064 GNUNET_RESOLVER_local_fqdn_get ()
1066 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1068 if (0 != gethostname (hostname, sizeof (hostname) - 1))
1070 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1074 LOG (GNUNET_ERROR_TYPE_DEBUG,
1075 "Resolving our FQDN `%s'\n",
1077 #if HAVE_GETADDRINFO
1079 struct addrinfo *ai;
1083 if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
1085 LOG (GNUNET_ERROR_TYPE_ERROR,
1086 _("Could not resolve our FQDN: %s\n"),
1087 gai_strerror (ret));
1090 if (NULL != ai->ai_canonname)
1091 rval = GNUNET_strdup (ai->ai_canonname);
1093 rval = GNUNET_strdup (hostname);
1097 #elif HAVE_GETHOSTBYNAME2
1099 struct hostent *host;
1101 host = gethostbyname2 (hostname, AF_INET);
1103 host = gethostbyname2 (hostname, AF_INET6);
1106 LOG (GNUNET_ERROR_TYPE_ERROR,
1107 _("Could not resolve our FQDN: %s\n"),
1108 hstrerror (h_errno));
1111 return GNUNET_strdup (host->h_name);
1113 #elif HAVE_GETHOSTBYNAME
1115 struct hostent *host;
1117 host = gethostbyname (hostname);
1120 LOG (GNUNET_ERROR_TYPE_ERROR,
1121 _("Could not resolve our FQDN: %s\n"),
1122 hstrerror (h_errno));
1125 return GNUNET_strdup (host->h_name);
1128 /* fallback: just hope name is already FQDN */
1129 return GNUNET_strdup (hostname);
1135 * Looking our own hostname.
1137 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
1138 * @param timeout how long to try resolving
1139 * @param callback function to call with addresses
1140 * @param cls closure for @a callback
1141 * @return handle that can be used to cancel the request, NULL on error
1143 struct GNUNET_RESOLVER_RequestHandle *
1144 GNUNET_RESOLVER_hostname_resolve (int af,
1145 struct GNUNET_TIME_Relative timeout,
1146 GNUNET_RESOLVER_AddressCallback callback,
1149 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1151 if (0 != gethostname (hostname, sizeof (hostname) - 1))
1153 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1157 LOG (GNUNET_ERROR_TYPE_DEBUG,
1158 "Resolving our hostname `%s'\n",
1160 return GNUNET_RESOLVER_ip_get (hostname,
1169 * Cancel a request that is still pending with the resolver.
1170 * Note that a client MUST NOT cancel a request that has
1171 * been completed (i.e, the callback has been called to
1172 * signal timeout or the final result).
1174 * @param rh handle of request to cancel
1177 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
1179 if (GNUNET_NO == rh->direction)
1180 LOG (GNUNET_ERROR_TYPE_DEBUG,
1181 "Asked to cancel request to resolve hostname `%s'.\n",
1182 (const char *) &rh[1]);
1183 if (NULL != rh->task)
1185 GNUNET_SCHEDULER_cancel (rh->task);
1188 if (GNUNET_NO == rh->was_transmitted)
1190 if (GNUNET_YES == rh->was_queued)
1191 GNUNET_CONTAINER_DLL_remove (req_head,
1195 check_disconnect ();
1198 GNUNET_assert (GNUNET_YES == rh->was_transmitted);
1199 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
1200 check_disconnect ();
1204 /* end of resolver_api.c */