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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file util/resolver_api.c
21 * @brief resolver for writing a tool
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_resolver_service.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util-resolver-api", __VA_ARGS__)
32 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-resolver-api", syscall)
35 * Maximum supported length for a hostname
37 #define MAX_HOSTNAME 1024
41 * Possible hostnames for "loopback".
43 static const char *loopback[] = {
53 static const struct GNUNET_CONFIGURATION_Handle *resolver_cfg;
56 * Our connection to the resolver service, created on-demand, but then
57 * persists until error or shutdown.
59 static struct GNUNET_MQ_Handle *mq;
62 * Head of DLL of requests.
64 static struct GNUNET_RESOLVER_RequestHandle *req_head;
67 * Tail of DLL of requests.
69 static struct GNUNET_RESOLVER_RequestHandle *req_tail;
72 * How long should we wait to reconnect?
74 static struct GNUNET_TIME_Relative backoff;
77 * Task for reconnecting.
79 static struct GNUNET_SCHEDULER_Task *r_task;
82 * Task ID of shutdown task; only present while we have a
83 * connection to the resolver service.
85 static struct GNUNET_SCHEDULER_Task *s_task;
89 * Handle to a request given to the resolver. Can be used to cancel
90 * the request prior to the timeout or successful execution. Also
91 * used to track our internal state for the request.
93 struct GNUNET_RESOLVER_RequestHandle
97 * Next entry in DLL of requests.
99 struct GNUNET_RESOLVER_RequestHandle *next;
102 * Previous entry in DLL of requests.
104 struct GNUNET_RESOLVER_RequestHandle *prev;
107 * Callback if this is an name resolution request,
110 GNUNET_RESOLVER_AddressCallback addr_callback;
113 * Callback if this is a reverse lookup request,
116 GNUNET_RESOLVER_HostnameCallback name_callback;
119 * Closure for the callbacks.
124 * When should this request time out?
126 struct GNUNET_TIME_Absolute timeout;
129 * Task handle for making reply callbacks in numeric lookups
130 * asynchronous, and for timeout handling.
132 struct GNUNET_SCHEDULER_Task *task;
135 * Desired address family.
140 * Has this request been transmitted to the service?
141 * #GNUNET_YES if transmitted
142 * #GNUNET_YES if not transmitted
143 * #GNUNET_SYSERR when request was canceled
148 * Did we add this request to the queue?
153 * Desired direction (IP to name or name to IP)
158 * #GNUNET_YES if a response was received
160 int received_response;
163 * Length of the data that follows this struct.
170 * Check that the resolver service runs on localhost
173 * @return #GNUNET_OK if the resolver is properly configured,
174 * #GNUNET_SYSERR otherwise.
180 struct sockaddr_in v4;
181 struct sockaddr_in6 v6;
183 memset (&v4, 0, sizeof (v4));
184 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
185 v4.sin_family = AF_INET;
186 #if HAVE_SOCKADDR_IN_SIN_LEN
187 v4.sin_len = sizeof (v4);
189 memset (&v6, 0, sizeof (v6));
190 v6.sin6_family = AF_INET6;
191 #if HAVE_SOCKADDR_IN_SIN_LEN
192 v6.sin6_len = sizeof (v6);
195 GNUNET_CONFIGURATION_get_value_string (resolver_cfg,
200 LOG (GNUNET_ERROR_TYPE_INFO,
201 _("Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"),
204 return GNUNET_SYSERR;
206 if ( (1 == inet_pton (AF_INET, hostname, &v4)) ||
207 (1 == inet_pton (AF_INET6, hostname, &v6)) )
209 GNUNET_free (hostname);
212 for (unsigned int i = 0;
215 if (0 == strcasecmp (loopback[i],
218 GNUNET_free (hostname);
221 LOG (GNUNET_ERROR_TYPE_INFO,
222 _("Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"),
226 GNUNET_free (hostname);
227 return GNUNET_SYSERR;
232 * Create the connection to the resolver service.
234 * @param cfg configuration to use
237 GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
239 GNUNET_assert (NULL != cfg);
240 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
246 * Destroy the connection to the resolver service.
249 GNUNET_RESOLVER_disconnect ()
251 struct GNUNET_RESOLVER_RequestHandle *rh;
253 while (NULL != (rh = req_head))
255 GNUNET_assert (GNUNET_SYSERR == rh->was_transmitted);
256 GNUNET_CONTAINER_DLL_remove (req_head,
263 LOG (GNUNET_ERROR_TYPE_DEBUG,
264 "Disconnecting from DNS service\n");
265 GNUNET_MQ_destroy (mq);
270 GNUNET_SCHEDULER_cancel (r_task);
275 GNUNET_SCHEDULER_cancel (s_task);
282 * Task executed on system shutdown.
285 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)
391 GNUNET_MQ_destroy (mq);
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 "MQ error %d, reconnecting\n",
401 * Process pending requests to the resolver.
406 struct GNUNET_RESOLVER_GetMessage *msg;
407 struct GNUNET_MQ_Envelope *env;
408 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
417 /* nothing to do, release socket really soon if there is nothing
418 * else happening... */
421 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
426 if (GNUNET_NO != rh->was_transmitted)
427 return; /* waiting for reply */
428 env = GNUNET_MQ_msg_extra (msg,
430 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
431 msg->direction = htonl (rh->direction);
432 msg->af = htonl (rh->af);
433 GNUNET_memcpy (&msg[1],
436 LOG (GNUNET_ERROR_TYPE_DEBUG,
437 "Transmitting DNS resolution request to DNS service\n");
440 rh->was_transmitted = GNUNET_YES;
445 * Check validity of response with a hostname for a DNS lookup.
448 * @param msg message with the hostname
451 check_response (void *cls,
452 const struct GNUNET_MessageHeader *msg)
457 /* implemented in #handle_response() for now */
463 * Check validity of response with a hostname for a DNS lookup.
464 * NOTE: right now rather messy, might want to use different
465 * message types for different response formats in the future.
468 * @param msg message with the response
471 handle_response (void *cls,
472 const struct GNUNET_MessageHeader *msg)
474 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
481 /* Resolver service sent extra replies to query (after terminator)? Bad! */
483 GNUNET_MQ_destroy (mq);
488 size = ntohs (msg->size);
489 if (size == sizeof (struct GNUNET_MessageHeader))
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 "Received empty response from DNS service\n");
493 /* message contains not data, just header; end of replies */
494 /* check if request was canceled */
495 if (GNUNET_SYSERR != rh->was_transmitted)
497 /* no reverse lookup was successful, return IP as string */
498 if (NULL != rh->name_callback)
500 if (GNUNET_NO == rh->received_response)
502 nret = no_resolve (rh->af,
505 rh->name_callback (rh->cls, nret);
508 /* finally, make termination call */
509 if (GNUNET_SYSERR != rh->was_transmitted)
510 rh->name_callback (rh->cls,
513 if ( (NULL != rh->addr_callback) &&
514 (GNUNET_SYSERR != rh->was_transmitted) )
515 rh->addr_callback (rh->cls,
519 rh->was_transmitted = GNUNET_NO;
520 GNUNET_RESOLVER_request_cancel (rh);
524 /* return reverse lookup results to caller */
525 if (NULL != rh->name_callback)
527 const char *hostname;
529 hostname = (const char *) &msg[1];
530 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
533 if (GNUNET_SYSERR != rh->was_transmitted)
534 rh->name_callback (rh->cls,
536 rh->was_transmitted = GNUNET_NO;
537 GNUNET_RESOLVER_request_cancel (rh);
538 GNUNET_MQ_destroy (mq);
543 LOG (GNUNET_ERROR_TYPE_DEBUG,
544 "Resolver returns `%s' for IP `%s'.\n",
546 GNUNET_a2s ((const void *) &rh[1],
548 if (rh->was_transmitted != GNUNET_SYSERR)
549 rh->name_callback (rh->cls,
551 rh->received_response = GNUNET_YES;
553 /* return lookup results to caller */
554 if (NULL != rh->addr_callback)
556 struct sockaddr_in v4;
557 struct sockaddr_in6 v6;
558 const struct sockaddr *sa;
564 ip_len = size - sizeof (struct GNUNET_MessageHeader);
565 if (ip_len == sizeof (struct in_addr))
567 memset (&v4, 0, sizeof (v4));
568 v4.sin_family = AF_INET;
569 v4.sin_addr = *(struct in_addr*) ip;
570 #if HAVE_SOCKADDR_IN_SIN_LEN
571 v4.sin_len = sizeof (v4);
574 sa = (const struct sockaddr *) &v4;
576 else if (ip_len == sizeof (struct in6_addr))
578 memset (&v6, 0, sizeof (v6));
579 v6.sin6_family = AF_INET6;
580 v6.sin6_addr = *(struct in6_addr*) ip;
581 #if HAVE_SOCKADDR_IN_SIN_LEN
582 v6.sin6_len = sizeof (v6);
585 sa = (const struct sockaddr *) &v6;
590 if (GNUNET_SYSERR != rh->was_transmitted)
591 rh->addr_callback (rh->cls,
594 rh->was_transmitted = GNUNET_NO;
595 GNUNET_RESOLVER_request_cancel (rh);
596 GNUNET_MQ_destroy (mq);
601 LOG (GNUNET_ERROR_TYPE_DEBUG,
602 "Received IP from DNS service\n");
603 if (GNUNET_SYSERR != rh->was_transmitted)
604 rh->addr_callback (rh->cls,
612 * We've been asked to lookup the address for a hostname and were
613 * given a valid numeric string. Perform the callbacks for the
616 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
619 numeric_resolution (void *cls)
621 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
622 struct sockaddr_in v4;
623 struct sockaddr_in6 v6;
624 const char *hostname;
627 memset (&v4, 0, sizeof (v4));
628 v4.sin_family = AF_INET;
629 #if HAVE_SOCKADDR_IN_SIN_LEN
630 v4.sin_len = sizeof (v4);
632 memset (&v6, 0, sizeof (v6));
633 v6.sin6_family = AF_INET6;
634 #if HAVE_SOCKADDR_IN_SIN_LEN
635 v6.sin6_len = sizeof (v6);
637 hostname = (const char *) &rh[1];
638 if ( ( (rh->af == AF_UNSPEC) ||
639 (rh->af == AF_INET) ) &&
640 (1 == inet_pton (AF_INET,
644 rh->addr_callback (rh->cls,
645 (const struct sockaddr *) &v4,
647 if ( (rh->af == AF_UNSPEC) &&
648 (GNUNET_SYSERR != rh->was_transmitted) &&
649 (1 == inet_pton (AF_INET6,
653 /* this can happen on some systems IF "hostname" is "localhost" */
654 rh->addr_callback (rh->cls,
655 (const struct sockaddr *) &v6,
658 if (GNUNET_SYSERR != rh->was_transmitted)
659 rh->addr_callback (rh->cls,
665 if ( ( (rh->af == AF_UNSPEC) ||
666 (rh->af == AF_INET6) ) &&
667 (1 == inet_pton (AF_INET6,
671 rh->addr_callback (rh->cls,
672 (const struct sockaddr *) &v6,
674 if (GNUNET_SYSERR != rh->was_transmitted)
675 rh->addr_callback (rh->cls,
681 /* why are we here? this task should not have been scheduled! */
688 * We've been asked to lookup the address for a hostname and were
689 * given a variant of "loopback". Perform the callbacks for the
690 * respective loopback numeric addresses.
692 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
695 loopback_resolution (void *cls)
697 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
698 struct sockaddr_in v4;
699 struct sockaddr_in6 v6;
702 memset (&v4, 0, sizeof (v4));
703 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
704 v4.sin_family = AF_INET;
705 #if HAVE_SOCKADDR_IN_SIN_LEN
706 v4.sin_len = sizeof (v4);
708 memset (&v6, 0, sizeof (v6));
709 v6.sin6_family = AF_INET6;
710 #if HAVE_SOCKADDR_IN_SIN_LEN
711 v6.sin6_len = sizeof (v6);
713 v6.sin6_addr = in6addr_loopback;
717 rh->addr_callback (rh->cls,
718 (const struct sockaddr *) &v4,
722 rh->addr_callback (rh->cls,
723 (const struct sockaddr *) &v6,
727 rh->addr_callback (rh->cls,
728 (const struct sockaddr *) &v6,
730 rh->addr_callback (rh->cls,
731 (const struct sockaddr *) &v4,
739 if (GNUNET_SYSERR != rh->was_transmitted)
740 rh->addr_callback (rh->cls,
743 LOG (GNUNET_ERROR_TYPE_DEBUG,
744 "Finished resolving hostname `%s'.\n",
745 (const char *) &rh[1]);
751 * Now try to reconnect to the resolver service.
756 reconnect_task (void *cls)
758 struct GNUNET_MQ_MessageHandler handlers[] = {
759 GNUNET_MQ_hd_var_size (response,
760 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
761 struct GNUNET_MessageHeader,
763 GNUNET_MQ_handler_end ()
768 if (NULL == req_head)
769 return; /* no work pending */
770 LOG (GNUNET_ERROR_TYPE_DEBUG,
771 "Trying to connect to DNS service\n");
772 mq = GNUNET_CLIENT_connect (resolver_cfg,
779 LOG (GNUNET_ERROR_TYPE_DEBUG,
780 "Failed to connect, will try again later\n");
789 * Adjust exponential back-off and reconnect to the service.
794 struct GNUNET_RESOLVER_RequestHandle *rh;
798 GNUNET_assert (NULL == mq);
799 if (NULL != (rh = req_head))
801 switch (rh->was_transmitted)
804 /* nothing more to do */
807 /* disconnected, transmit again! */
808 rh->was_transmitted = GNUNET_NO;
811 /* request was cancelled, remove entirely */
812 GNUNET_CONTAINER_DLL_remove (req_head,
823 LOG (GNUNET_ERROR_TYPE_DEBUG,
824 "Will try to connect to DNS service in %s\n",
825 GNUNET_STRINGS_relative_time_to_string (backoff,
827 GNUNET_assert (NULL != resolver_cfg);
828 r_task = GNUNET_SCHEDULER_add_delayed (backoff,
831 backoff = GNUNET_TIME_STD_BACKOFF (backoff);
836 * A DNS resolution timed out. Notify the application.
838 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
841 handle_lookup_timeout (void *cls)
843 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
846 if (GNUNET_NO == rh->direction)
848 LOG (GNUNET_ERROR_TYPE_INFO,
849 _("Timeout trying to resolve hostname `%s'.\n"),
850 (const char *) &rh[1]);
851 if (NULL != rh->addr_callback)
852 rh->addr_callback (rh->cls,
858 #if !defined(GNUNET_CULL_LOGGING)
859 char buf[INET6_ADDRSTRLEN];
861 LOG (GNUNET_ERROR_TYPE_INFO,
862 _("Timeout trying to resolve IP address `%s'.\n"),
864 (const void *) &rh[1],
868 if (GNUNET_NO == rh->received_response)
872 nret = no_resolve (rh->af,
875 if (NULL != rh->name_callback)
876 rh->name_callback (rh->cls, nret);
879 /* finally, make termination call */
880 if (NULL != rh->name_callback)
881 rh->name_callback (rh->cls,
884 rh->was_transmitted = GNUNET_NO;
885 GNUNET_RESOLVER_request_cancel (rh);
891 * Convert a string to one or more IP addresses.
893 * @param hostname the hostname to resolve
894 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
895 * @param callback function to call with addresses
896 * @param callback_cls closure for @a callback
897 * @param timeout how long to try resolving
898 * @return handle that can be used to cancel the request, NULL on error
900 struct GNUNET_RESOLVER_RequestHandle *
901 GNUNET_RESOLVER_ip_get (const char *hostname,
903 struct GNUNET_TIME_Relative timeout,
904 GNUNET_RESOLVER_AddressCallback callback,
907 struct GNUNET_RESOLVER_RequestHandle *rh;
912 slen = strlen (hostname) + 1;
913 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
914 GNUNET_MAX_MESSAGE_SIZE)
919 LOG (GNUNET_ERROR_TYPE_DEBUG,
920 "Trying to resolve hostname `%s'.\n",
922 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
924 rh->addr_callback = callback;
925 rh->cls = callback_cls;
926 GNUNET_memcpy (&rh[1],
930 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
931 rh->direction = GNUNET_NO;
932 /* first, check if this is a numeric address */
933 if ( ( (1 == inet_pton (AF_INET,
937 (af == AF_UNSPEC) ) ) ||
938 ( (1 == inet_pton (AF_INET6,
941 ( (af == AF_INET6) ||
942 (af == AF_UNSPEC)) ) )
944 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution,
948 /* then, check if this is a loopback address */
949 for (unsigned int i = 0;
952 if (0 == strcasecmp (loopback[i],
955 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution,
959 if (GNUNET_OK != check_config ())
964 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
965 &handle_lookup_timeout,
967 GNUNET_CONTAINER_DLL_insert_tail (req_head,
970 rh->was_queued = GNUNET_YES;
973 GNUNET_SCHEDULER_cancel (s_task);
982 * We've been asked to convert an address to a string without
983 * a reverse lookup, either because the client asked for it
984 * or because the DNS lookup hit a timeout. Do the numeric
985 * conversion and invoke the callback.
987 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
990 numeric_reverse (void *cls)
992 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
996 result = no_resolve (rh->af,
999 LOG (GNUNET_ERROR_TYPE_DEBUG,
1000 "Resolver returns `%s'.\n",
1004 rh->name_callback (rh->cls,
1006 GNUNET_free (result);
1008 rh->name_callback (rh->cls,
1010 if (NULL != rh->task)
1012 GNUNET_SCHEDULER_cancel (rh->task);
1020 * Get an IP address as a string.
1022 * @param sa host address
1023 * @param salen length of host address in @a sa
1024 * @param do_resolve use #GNUNET_NO to return numeric hostname
1025 * @param timeout how long to try resolving
1026 * @param callback function to call with hostnames
1027 * last callback is NULL when finished
1028 * @param cls closure for @a callback
1029 * @return handle that can be used to cancel the request
1031 struct GNUNET_RESOLVER_RequestHandle *
1032 GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
1035 struct GNUNET_TIME_Relative timeout,
1036 GNUNET_RESOLVER_HostnameCallback callback,
1039 struct GNUNET_RESOLVER_RequestHandle *rh;
1043 if (GNUNET_OK != check_config ())
1045 LOG (GNUNET_ERROR_TYPE_ERROR,
1046 _("Resolver not configured correctly.\n"));
1050 switch (sa->sa_family)
1053 GNUNET_assert (salen == sizeof (struct sockaddr_in));
1054 ip_len = sizeof (struct in_addr);
1055 ip = &((const struct sockaddr_in*)sa)->sin_addr;
1058 GNUNET_assert (salen == sizeof (struct sockaddr_in6));
1059 ip_len = sizeof (struct in6_addr);
1060 ip = &((const struct sockaddr_in6*)sa)->sin6_addr;
1066 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
1067 rh->name_callback = callback;
1069 rh->af = sa->sa_family;
1070 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1071 GNUNET_memcpy (&rh[1],
1074 rh->data_len = ip_len;
1075 rh->direction = GNUNET_YES;
1076 rh->received_response = GNUNET_NO;
1077 if (GNUNET_NO == do_resolve)
1079 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse,
1083 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
1084 &handle_lookup_timeout,
1086 GNUNET_CONTAINER_DLL_insert_tail (req_head,
1089 rh->was_queued = GNUNET_YES;
1092 GNUNET_SCHEDULER_cancel (s_task);
1095 process_requests ();
1101 * Get local fully qualified af name
1106 GNUNET_RESOLVER_local_fqdn_get ()
1108 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1110 if (0 != gethostname (hostname,
1111 sizeof (hostname) - 1))
1113 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1117 LOG (GNUNET_ERROR_TYPE_DEBUG,
1118 "Resolving our FQDN `%s'\n",
1120 #if HAVE_GETADDRINFO
1122 struct addrinfo *ai;
1126 if (0 != (ret = getaddrinfo (hostname,
1131 LOG (GNUNET_ERROR_TYPE_ERROR,
1132 _("Could not resolve our FQDN: %s\n"),
1133 gai_strerror (ret));
1136 if (NULL != ai->ai_canonname)
1137 rval = GNUNET_strdup (ai->ai_canonname);
1139 rval = GNUNET_strdup (hostname);
1143 #elif HAVE_GETHOSTBYNAME2
1145 struct hostent *host;
1147 host = gethostbyname2 (hostname,
1150 host = gethostbyname2 (hostname,
1154 LOG (GNUNET_ERROR_TYPE_ERROR,
1155 _("Could not resolve our FQDN: %s\n"),
1156 hstrerror (h_errno));
1159 return GNUNET_strdup (host->h_name);
1161 #elif HAVE_GETHOSTBYNAME
1163 struct hostent *host;
1165 host = gethostbyname (hostname);
1168 LOG (GNUNET_ERROR_TYPE_ERROR,
1169 _("Could not resolve our FQDN: %s\n"),
1170 hstrerror (h_errno));
1173 return GNUNET_strdup (host->h_name);
1176 /* fallback: just hope name is already FQDN */
1177 return GNUNET_strdup (hostname);
1183 * Looking our own hostname.
1185 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
1186 * @param timeout how long to try resolving
1187 * @param callback function to call with addresses
1188 * @param cls closure for @a callback
1189 * @return handle that can be used to cancel the request, NULL on error
1191 struct GNUNET_RESOLVER_RequestHandle *
1192 GNUNET_RESOLVER_hostname_resolve (int af,
1193 struct GNUNET_TIME_Relative timeout,
1194 GNUNET_RESOLVER_AddressCallback callback,
1197 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1199 if (0 != gethostname (hostname, sizeof (hostname) - 1))
1201 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1205 LOG (GNUNET_ERROR_TYPE_DEBUG,
1206 "Resolving our hostname `%s'\n",
1208 return GNUNET_RESOLVER_ip_get (hostname,
1217 * Cancel a request that is still pending with the resolver.
1218 * Note that a client MUST NOT cancel a request that has
1219 * been completed (i.e, the callback has been called to
1220 * signal timeout or the final result).
1222 * @param rh handle of request to cancel
1225 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
1227 if (GNUNET_NO == rh->direction)
1228 LOG (GNUNET_ERROR_TYPE_DEBUG,
1229 "Asked to cancel request to resolve hostname `%s'.\n",
1230 (const char *) &rh[1]);
1231 if (NULL != rh->task)
1233 GNUNET_SCHEDULER_cancel (rh->task);
1236 if (GNUNET_NO == rh->was_transmitted)
1238 if (GNUNET_YES == rh->was_queued)
1239 GNUNET_CONTAINER_DLL_remove (req_head,
1243 check_disconnect ();
1246 GNUNET_assert (GNUNET_YES == rh->was_transmitted);
1247 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
1248 check_disconnect ();
1252 /* end of resolver_api.c */