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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 * ID of the last request we sent to the service
76 static uint32_t last_request_id;
79 * How long should we wait to reconnect?
81 static struct GNUNET_TIME_Relative backoff;
84 * Task for reconnecting.
86 static struct GNUNET_SCHEDULER_Task *r_task;
89 * Task ID of shutdown task; only present while we have a
90 * connection to the resolver service.
92 static struct GNUNET_SCHEDULER_Task *s_task;
96 * Handle to a request given to the resolver. Can be used to cancel
97 * the request prior to the timeout or successful execution. Also
98 * used to track our internal state for the request.
100 struct GNUNET_RESOLVER_RequestHandle
104 * Next entry in DLL of requests.
106 struct GNUNET_RESOLVER_RequestHandle *next;
109 * Previous entry in DLL of requests.
111 struct GNUNET_RESOLVER_RequestHandle *prev;
114 * Callback if this is an name resolution request,
117 GNUNET_RESOLVER_AddressCallback addr_callback;
120 * Callback if this is a reverse lookup request,
123 GNUNET_RESOLVER_HostnameCallback name_callback;
126 * Closure for the callbacks.
131 * When should this request time out?
133 struct GNUNET_TIME_Absolute timeout;
136 * Task handle for making reply callbacks in numeric lookups
137 * asynchronous, and for timeout handling.
139 struct GNUNET_SCHEDULER_Task *task;
142 * Desired address family.
147 * Identifies the request. The response will contain this id.
152 * Has this request been transmitted to the service?
153 * #GNUNET_YES if transmitted
154 * #GNUNET_YES if not transmitted
155 * #GNUNET_SYSERR when request was canceled
160 * Did we add this request to the queue?
165 * Desired direction (IP to name or name to IP)
170 * #GNUNET_YES if a response was received
172 int received_response;
175 * Length of the data that follows this struct.
182 * Check that the resolver service runs on localhost
185 * @return #GNUNET_OK if the resolver is properly configured,
186 * #GNUNET_SYSERR otherwise.
192 struct sockaddr_in v4;
193 struct sockaddr_in6 v6;
196 GNUNET_CONFIGURATION_have_value (resolver_cfg,
200 memset (&v4, 0, sizeof (v4));
201 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
202 v4.sin_family = AF_INET;
203 #if HAVE_SOCKADDR_IN_SIN_LEN
204 v4.sin_len = sizeof (v4);
206 memset (&v6, 0, sizeof (v6));
207 v6.sin6_family = AF_INET6;
208 #if HAVE_SOCKADDR_IN_SIN_LEN
209 v6.sin6_len = sizeof (v6);
212 GNUNET_CONFIGURATION_get_value_string (resolver_cfg,
217 LOG (GNUNET_ERROR_TYPE_INFO,
218 _("Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"),
221 return GNUNET_SYSERR;
223 if ( (1 == inet_pton (AF_INET, hostname, &v4)) ||
224 (1 == inet_pton (AF_INET6, hostname, &v6)) )
226 GNUNET_free (hostname);
229 for (unsigned int i = 0;
232 if (0 == strcasecmp (loopback[i],
235 GNUNET_free (hostname);
238 LOG (GNUNET_ERROR_TYPE_INFO,
239 _("Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"),
243 GNUNET_free (hostname);
244 return GNUNET_SYSERR;
249 * Create the connection to the resolver service.
251 * @param cfg configuration to use
254 GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
256 GNUNET_assert (NULL != cfg);
257 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
263 * Destroy the connection to the resolver service.
266 GNUNET_RESOLVER_disconnect ()
268 struct GNUNET_RESOLVER_RequestHandle *rh;
270 while (NULL != (rh = req_head))
272 GNUNET_assert (GNUNET_SYSERR == rh->was_transmitted);
273 GNUNET_CONTAINER_DLL_remove (req_head,
280 LOG (GNUNET_ERROR_TYPE_DEBUG,
281 "Disconnecting from DNS service\n");
282 GNUNET_MQ_destroy (mq);
287 GNUNET_SCHEDULER_cancel (r_task);
292 GNUNET_SCHEDULER_cancel (s_task);
299 * Task executed on system shutdown.
302 shutdown_task (void *cls)
306 GNUNET_RESOLVER_disconnect ();
307 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
312 * Consider disconnecting if we have no further requests pending.
317 for (struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
320 if (GNUNET_SYSERR != rh->was_transmitted)
324 GNUNET_SCHEDULER_cancel (r_task);
329 s_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
336 * Convert IP address to string without DNS resolution.
338 * @param af address family
339 * @param ip the address
340 * @param ip_len number of bytes in @a ip
341 * @return address as a string, NULL on error
348 char buf[INET6_ADDRSTRLEN];
353 if (ip_len != sizeof (struct in_addr))
361 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
367 if (ip_len != sizeof (struct in6_addr))
375 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
384 return GNUNET_strdup (buf);
389 * Adjust exponential back-off and reconnect to the service.
396 * Generic error handler, called with the appropriate error code and
397 * the same closure specified at the creation of the message queue.
398 * Not every message queue implementation supports an error handler.
401 * @param error error code
404 mq_error_handler (void *cls,
405 enum GNUNET_MQ_Error error)
408 GNUNET_MQ_destroy (mq);
410 LOG (GNUNET_ERROR_TYPE_DEBUG,
411 "MQ error %d, reconnecting\n",
418 * Process pending requests to the resolver.
423 struct GNUNET_RESOLVER_GetMessage *msg;
424 struct GNUNET_MQ_Envelope *env;
425 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
434 /* nothing to do, release socket really soon if there is nothing
435 * else happening... */
438 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
443 if (GNUNET_NO != rh->was_transmitted)
444 return; /* waiting for reply */
445 env = GNUNET_MQ_msg_extra (msg,
447 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
448 msg->direction = htonl (rh->direction);
449 msg->af = htonl (rh->af);
450 msg->client_id = rh->id;
451 GNUNET_memcpy (&msg[1],
454 LOG (GNUNET_ERROR_TYPE_DEBUG,
455 "Transmitting DNS resolution request (ID %u) to DNS service\n",
459 rh->was_transmitted = GNUNET_YES;
464 * Check validity of response with a hostname for a DNS lookup.
467 * @param msg message with the hostname
470 check_response (void *cls,
471 const struct GNUNET_RESOLVER_ResponseMessage *msg)
476 /* implemented in #handle_response() for now */
482 * Check validity of response with a hostname for a DNS lookup.
483 * NOTE: right now rather messy, might want to use different
484 * message types for different response formats in the future.
487 * @param msg message with the response
490 handle_response (void *cls,
491 const struct GNUNET_RESOLVER_ResponseMessage *msg)
493 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
496 uint32_t client_request_id = msg->client_id;
498 for (; rh != NULL; rh = rh->next)
500 if (rh->id == client_request_id)
507 /* Resolver service sent extra replies to query (after terminator)? Bad! */
509 GNUNET_MQ_destroy (mq);
514 size = ntohs (msg->header.size);
515 if (size == sizeof (struct GNUNET_RESOLVER_ResponseMessage))
517 LOG (GNUNET_ERROR_TYPE_DEBUG,
518 "Received empty response from DNS service\n");
519 /* message contains not data, just header; end of replies */
520 /* check if request was canceled */
521 if (GNUNET_SYSERR != rh->was_transmitted)
523 /* no reverse lookup was successful, return IP as string */
524 if (NULL != rh->name_callback)
526 if (GNUNET_NO == rh->received_response)
528 nret = no_resolve (rh->af,
531 rh->name_callback (rh->cls, nret);
534 /* finally, make termination call */
535 if (GNUNET_SYSERR != rh->was_transmitted)
536 rh->name_callback (rh->cls,
539 if ( (NULL != rh->addr_callback) &&
540 (GNUNET_SYSERR != rh->was_transmitted) )
541 rh->addr_callback (rh->cls,
545 rh->was_transmitted = GNUNET_NO;
546 GNUNET_RESOLVER_request_cancel (rh);
550 /* return reverse lookup results to caller */
551 if (NULL != rh->name_callback)
553 const char *hostname;
555 hostname = (const char *) &msg[1];
556 if (hostname[size - sizeof (struct GNUNET_RESOLVER_ResponseMessage) - 1] != '\0')
559 if (GNUNET_SYSERR != rh->was_transmitted)
560 rh->name_callback (rh->cls,
562 rh->was_transmitted = GNUNET_NO;
563 GNUNET_RESOLVER_request_cancel (rh);
564 GNUNET_MQ_destroy (mq);
569 LOG (GNUNET_ERROR_TYPE_DEBUG,
570 "Resolver returns `%s' for IP `%s'.\n",
572 GNUNET_a2s ((const void *) &rh[1],
574 if (rh->was_transmitted != GNUNET_SYSERR)
575 rh->name_callback (rh->cls,
577 rh->received_response = GNUNET_YES;
579 /* return lookup results to caller */
580 if (NULL != rh->addr_callback)
582 struct sockaddr_in v4;
583 struct sockaddr_in6 v6;
584 const struct sockaddr *sa;
590 ip_len = size - sizeof (struct GNUNET_RESOLVER_ResponseMessage);
591 if (ip_len == sizeof (struct in_addr))
593 memset (&v4, 0, sizeof (v4));
594 v4.sin_family = AF_INET;
595 v4.sin_addr = *(struct in_addr*) ip;
596 #if HAVE_SOCKADDR_IN_SIN_LEN
597 v4.sin_len = sizeof (v4);
600 sa = (const struct sockaddr *) &v4;
602 else if (ip_len == sizeof (struct in6_addr))
604 memset (&v6, 0, sizeof (v6));
605 v6.sin6_family = AF_INET6;
606 v6.sin6_addr = *(struct in6_addr*) ip;
607 #if HAVE_SOCKADDR_IN_SIN_LEN
608 v6.sin6_len = sizeof (v6);
611 sa = (const struct sockaddr *) &v6;
616 if (GNUNET_SYSERR != rh->was_transmitted)
617 rh->addr_callback (rh->cls,
620 rh->was_transmitted = GNUNET_NO;
621 GNUNET_RESOLVER_request_cancel (rh);
622 GNUNET_MQ_destroy (mq);
627 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 "Received IP from DNS service\n");
629 if (GNUNET_SYSERR != rh->was_transmitted)
630 rh->addr_callback (rh->cls,
638 * We've been asked to lookup the address for a hostname and were
639 * given a valid numeric string. Perform the callbacks for the
642 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
645 numeric_resolution (void *cls)
647 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
648 struct sockaddr_in v4;
649 struct sockaddr_in6 v6;
650 const char *hostname;
653 memset (&v4, 0, sizeof (v4));
654 v4.sin_family = AF_INET;
655 #if HAVE_SOCKADDR_IN_SIN_LEN
656 v4.sin_len = sizeof (v4);
658 memset (&v6, 0, sizeof (v6));
659 v6.sin6_family = AF_INET6;
660 #if HAVE_SOCKADDR_IN_SIN_LEN
661 v6.sin6_len = sizeof (v6);
663 hostname = (const char *) &rh[1];
664 if ( ( (rh->af == AF_UNSPEC) ||
665 (rh->af == AF_INET) ) &&
666 (1 == inet_pton (AF_INET,
670 rh->addr_callback (rh->cls,
671 (const struct sockaddr *) &v4,
673 if ( (rh->af == AF_UNSPEC) &&
674 (GNUNET_SYSERR != rh->was_transmitted) &&
675 (1 == inet_pton (AF_INET6,
679 /* this can happen on some systems IF "hostname" is "localhost" */
680 rh->addr_callback (rh->cls,
681 (const struct sockaddr *) &v6,
684 if (GNUNET_SYSERR != rh->was_transmitted)
685 rh->addr_callback (rh->cls,
691 if ( ( (rh->af == AF_UNSPEC) ||
692 (rh->af == AF_INET6) ) &&
693 (1 == inet_pton (AF_INET6,
697 rh->addr_callback (rh->cls,
698 (const struct sockaddr *) &v6,
700 if (GNUNET_SYSERR != rh->was_transmitted)
701 rh->addr_callback (rh->cls,
707 /* why are we here? this task should not have been scheduled! */
714 * We've been asked to lookup the address for a hostname and were
715 * given a variant of "loopback". Perform the callbacks for the
716 * respective loopback numeric addresses.
718 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
721 loopback_resolution (void *cls)
723 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
724 struct sockaddr_in v4;
725 struct sockaddr_in6 v6;
728 memset (&v4, 0, sizeof (v4));
729 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
730 v4.sin_family = AF_INET;
731 #if HAVE_SOCKADDR_IN_SIN_LEN
732 v4.sin_len = sizeof (v4);
734 memset (&v6, 0, sizeof (v6));
735 v6.sin6_family = AF_INET6;
736 #if HAVE_SOCKADDR_IN_SIN_LEN
737 v6.sin6_len = sizeof (v6);
739 v6.sin6_addr = in6addr_loopback;
743 rh->addr_callback (rh->cls,
744 (const struct sockaddr *) &v4,
748 rh->addr_callback (rh->cls,
749 (const struct sockaddr *) &v6,
753 rh->addr_callback (rh->cls,
754 (const struct sockaddr *) &v6,
756 rh->addr_callback (rh->cls,
757 (const struct sockaddr *) &v4,
765 if (GNUNET_SYSERR != rh->was_transmitted)
766 rh->addr_callback (rh->cls,
769 LOG (GNUNET_ERROR_TYPE_DEBUG,
770 "Finished resolving hostname `%s'.\n",
771 (const char *) &rh[1]);
777 * Now try to reconnect to the resolver service.
782 reconnect_task (void *cls)
784 struct GNUNET_MQ_MessageHandler handlers[] = {
785 GNUNET_MQ_hd_var_size (response,
786 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
787 struct GNUNET_RESOLVER_ResponseMessage,
789 GNUNET_MQ_handler_end ()
794 if (NULL == req_head)
795 return; /* no work pending */
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "Trying to connect to DNS service\n");
798 mq = GNUNET_CLIENT_connect (resolver_cfg,
805 LOG (GNUNET_ERROR_TYPE_DEBUG,
806 "Failed to connect, will try again later\n");
815 * Adjust exponential back-off and reconnect to the service.
820 struct GNUNET_RESOLVER_RequestHandle *rh;
824 GNUNET_assert (NULL == mq);
825 if (NULL != (rh = req_head))
827 switch (rh->was_transmitted)
830 /* nothing more to do */
833 /* disconnected, transmit again! */
834 rh->was_transmitted = GNUNET_NO;
837 /* request was cancelled, remove entirely */
838 GNUNET_CONTAINER_DLL_remove (req_head,
849 LOG (GNUNET_ERROR_TYPE_DEBUG,
850 "Will try to connect to DNS service in %s\n",
851 GNUNET_STRINGS_relative_time_to_string (backoff,
853 GNUNET_assert (NULL != resolver_cfg);
854 r_task = GNUNET_SCHEDULER_add_delayed (backoff,
857 backoff = GNUNET_TIME_STD_BACKOFF (backoff);
862 * A DNS resolution timed out. Notify the application.
864 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
867 handle_lookup_timeout (void *cls)
869 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
872 if (GNUNET_NO == rh->direction)
874 LOG (GNUNET_ERROR_TYPE_INFO,
875 _("Timeout trying to resolve hostname `%s'.\n"),
876 (const char *) &rh[1]);
877 if (NULL != rh->addr_callback)
878 rh->addr_callback (rh->cls,
884 #if !defined(GNUNET_CULL_LOGGING)
885 char buf[INET6_ADDRSTRLEN];
887 LOG (GNUNET_ERROR_TYPE_INFO,
888 _("Timeout trying to resolve IP address `%s'.\n"),
890 (const void *) &rh[1],
894 if (GNUNET_NO == rh->received_response)
898 nret = no_resolve (rh->af,
901 if (NULL != rh->name_callback)
902 rh->name_callback (rh->cls, nret);
905 /* finally, make termination call */
906 if (NULL != rh->name_callback)
907 rh->name_callback (rh->cls,
910 rh->was_transmitted = GNUNET_NO;
911 GNUNET_RESOLVER_request_cancel (rh);
917 * Convert a string to one or more IP addresses.
919 * @param hostname the hostname to resolve
920 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
921 * @param callback function to call with addresses
922 * @param callback_cls closure for @a callback
923 * @param timeout how long to try resolving
924 * @return handle that can be used to cancel the request, NULL on error
926 struct GNUNET_RESOLVER_RequestHandle *
927 GNUNET_RESOLVER_ip_get (const char *hostname,
929 struct GNUNET_TIME_Relative timeout,
930 GNUNET_RESOLVER_AddressCallback callback,
933 struct GNUNET_RESOLVER_RequestHandle *rh;
938 slen = strlen (hostname) + 1;
939 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
940 GNUNET_MAX_MESSAGE_SIZE)
945 LOG (GNUNET_ERROR_TYPE_DEBUG,
946 "Trying to resolve hostname `%s'.\n",
948 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
950 rh->id = ++last_request_id;
951 rh->addr_callback = callback;
952 rh->cls = callback_cls;
953 GNUNET_memcpy (&rh[1],
957 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
958 rh->direction = GNUNET_NO;
959 /* first, check if this is a numeric address */
960 if ( ( (1 == inet_pton (AF_INET,
964 (af == AF_UNSPEC) ) ) ||
965 ( (1 == inet_pton (AF_INET6,
968 ( (af == AF_INET6) ||
969 (af == AF_UNSPEC)) ) )
971 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution,
975 /* then, check if this is a loopback address */
976 for (unsigned int i = 0;
979 if (0 == strcasecmp (loopback[i],
982 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution,
986 if (GNUNET_OK != check_config ())
991 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
992 &handle_lookup_timeout,
994 GNUNET_CONTAINER_DLL_insert_tail (req_head,
997 rh->was_queued = GNUNET_YES;
1000 GNUNET_SCHEDULER_cancel (s_task);
1003 process_requests ();
1009 * We've been asked to convert an address to a string without
1010 * a reverse lookup, either because the client asked for it
1011 * or because the DNS lookup hit a timeout. Do the numeric
1012 * conversion and invoke the callback.
1014 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
1017 numeric_reverse (void *cls)
1019 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
1023 result = no_resolve (rh->af,
1026 LOG (GNUNET_ERROR_TYPE_DEBUG,
1027 "Resolver returns `%s'.\n",
1031 rh->name_callback (rh->cls,
1033 GNUNET_free (result);
1035 rh->name_callback (rh->cls,
1037 if (NULL != rh->task)
1039 GNUNET_SCHEDULER_cancel (rh->task);
1047 * Get an IP address as a string.
1049 * @param sa host address
1050 * @param salen length of host address in @a sa
1051 * @param do_resolve use #GNUNET_NO to return numeric hostname
1052 * @param timeout how long to try resolving
1053 * @param callback function to call with hostnames
1054 * last callback is NULL when finished
1055 * @param cls closure for @a callback
1056 * @return handle that can be used to cancel the request
1058 struct GNUNET_RESOLVER_RequestHandle *
1059 GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
1062 struct GNUNET_TIME_Relative timeout,
1063 GNUNET_RESOLVER_HostnameCallback callback,
1066 struct GNUNET_RESOLVER_RequestHandle *rh;
1070 if (GNUNET_OK != check_config ())
1072 LOG (GNUNET_ERROR_TYPE_ERROR,
1073 _("Resolver not configured correctly.\n"));
1077 switch (sa->sa_family)
1080 GNUNET_assert (salen == sizeof (struct sockaddr_in));
1081 ip_len = sizeof (struct in_addr);
1082 ip = &((const struct sockaddr_in*)sa)->sin_addr;
1085 GNUNET_assert (salen == sizeof (struct sockaddr_in6));
1086 ip_len = sizeof (struct in6_addr);
1087 ip = &((const struct sockaddr_in6*)sa)->sin6_addr;
1093 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
1094 rh->name_callback = callback;
1096 rh->af = sa->sa_family;
1097 rh->id = ++last_request_id;
1098 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1099 GNUNET_memcpy (&rh[1],
1102 rh->data_len = ip_len;
1103 rh->direction = GNUNET_YES;
1104 rh->received_response = GNUNET_NO;
1105 if (GNUNET_NO == do_resolve)
1107 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse,
1111 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
1112 &handle_lookup_timeout,
1114 GNUNET_CONTAINER_DLL_insert_tail (req_head,
1117 rh->was_queued = GNUNET_YES;
1120 GNUNET_SCHEDULER_cancel (s_task);
1123 process_requests ();
1129 * Get local fully qualified af name
1134 GNUNET_RESOLVER_local_fqdn_get ()
1136 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1138 if (0 != gethostname (hostname,
1139 sizeof (hostname) - 1))
1141 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1145 LOG (GNUNET_ERROR_TYPE_DEBUG,
1146 "Resolving our FQDN `%s'\n",
1148 #if HAVE_GETADDRINFO
1150 struct addrinfo *ai;
1154 if (0 != (ret = getaddrinfo (hostname,
1159 LOG (GNUNET_ERROR_TYPE_ERROR,
1160 _("Could not resolve our FQDN: %s\n"),
1161 gai_strerror (ret));
1164 if (NULL != ai->ai_canonname)
1165 rval = GNUNET_strdup (ai->ai_canonname);
1167 rval = GNUNET_strdup (hostname);
1171 #elif HAVE_GETHOSTBYNAME2
1173 struct hostent *host;
1175 host = gethostbyname2 (hostname,
1178 host = gethostbyname2 (hostname,
1182 LOG (GNUNET_ERROR_TYPE_ERROR,
1183 _("Could not resolve our FQDN: %s\n"),
1184 hstrerror (h_errno));
1187 return GNUNET_strdup (host->h_name);
1189 #elif HAVE_GETHOSTBYNAME
1191 struct hostent *host;
1193 host = gethostbyname (hostname);
1196 LOG (GNUNET_ERROR_TYPE_ERROR,
1197 _("Could not resolve our FQDN: %s\n"),
1198 hstrerror (h_errno));
1201 return GNUNET_strdup (host->h_name);
1204 /* fallback: just hope name is already FQDN */
1205 return GNUNET_strdup (hostname);
1211 * Looking our own hostname.
1213 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
1214 * @param timeout how long to try resolving
1215 * @param callback function to call with addresses
1216 * @param cls closure for @a callback
1217 * @return handle that can be used to cancel the request, NULL on error
1219 struct GNUNET_RESOLVER_RequestHandle *
1220 GNUNET_RESOLVER_hostname_resolve (int af,
1221 struct GNUNET_TIME_Relative timeout,
1222 GNUNET_RESOLVER_AddressCallback callback,
1225 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1227 if (0 != gethostname (hostname, sizeof (hostname) - 1))
1229 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1233 LOG (GNUNET_ERROR_TYPE_DEBUG,
1234 "Resolving our hostname `%s'\n",
1236 return GNUNET_RESOLVER_ip_get (hostname,
1245 * Cancel a request that is still pending with the resolver.
1246 * Note that a client MUST NOT cancel a request that has
1247 * been completed (i.e, the callback has been called to
1248 * signal timeout or the final result).
1250 * @param rh handle of request to cancel
1253 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
1255 if (GNUNET_NO == rh->direction)
1256 LOG (GNUNET_ERROR_TYPE_DEBUG,
1257 "Asked to cancel request to resolve hostname `%s'.\n",
1258 (const char *) &rh[1]);
1259 if (NULL != rh->task)
1261 GNUNET_SCHEDULER_cancel (rh->task);
1264 if (GNUNET_NO == rh->was_transmitted)
1266 if (GNUNET_YES == rh->was_queued)
1267 GNUNET_CONTAINER_DLL_remove (req_head,
1271 check_disconnect ();
1274 GNUNET_assert (GNUNET_YES == rh->was_transmitted);
1275 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
1276 check_disconnect ();
1280 /* end of resolver_api.c */