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;
184 GNUNET_CONFIGURATION_have_value (resolver_cfg,
188 memset (&v4, 0, sizeof (v4));
189 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
190 v4.sin_family = AF_INET;
191 #if HAVE_SOCKADDR_IN_SIN_LEN
192 v4.sin_len = sizeof (v4);
194 memset (&v6, 0, sizeof (v6));
195 v6.sin6_family = AF_INET6;
196 #if HAVE_SOCKADDR_IN_SIN_LEN
197 v6.sin6_len = sizeof (v6);
200 GNUNET_CONFIGURATION_get_value_string (resolver_cfg,
205 LOG (GNUNET_ERROR_TYPE_INFO,
206 _("Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"),
209 return GNUNET_SYSERR;
211 if ( (1 == inet_pton (AF_INET, hostname, &v4)) ||
212 (1 == inet_pton (AF_INET6, hostname, &v6)) )
214 GNUNET_free (hostname);
217 for (unsigned int i = 0;
220 if (0 == strcasecmp (loopback[i],
223 GNUNET_free (hostname);
226 LOG (GNUNET_ERROR_TYPE_INFO,
227 _("Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"),
231 GNUNET_free (hostname);
232 return GNUNET_SYSERR;
237 * Create the connection to the resolver service.
239 * @param cfg configuration to use
242 GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
244 GNUNET_assert (NULL != cfg);
245 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
251 * Destroy the connection to the resolver service.
254 GNUNET_RESOLVER_disconnect ()
256 struct GNUNET_RESOLVER_RequestHandle *rh;
258 while (NULL != (rh = req_head))
260 GNUNET_assert (GNUNET_SYSERR == rh->was_transmitted);
261 GNUNET_CONTAINER_DLL_remove (req_head,
268 LOG (GNUNET_ERROR_TYPE_DEBUG,
269 "Disconnecting from DNS service\n");
270 GNUNET_MQ_destroy (mq);
275 GNUNET_SCHEDULER_cancel (r_task);
280 GNUNET_SCHEDULER_cancel (s_task);
287 * Task executed on system shutdown.
290 shutdown_task (void *cls)
294 GNUNET_RESOLVER_disconnect ();
295 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
300 * Consider disconnecting if we have no further requests pending.
305 for (struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
308 if (GNUNET_SYSERR != rh->was_transmitted)
312 GNUNET_SCHEDULER_cancel (r_task);
317 s_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
324 * Convert IP address to string without DNS resolution.
326 * @param af address family
327 * @param ip the address
328 * @param ip_len number of bytes in @a ip
329 * @return address as a string, NULL on error
336 char buf[INET6_ADDRSTRLEN];
341 if (ip_len != sizeof (struct in_addr))
349 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
355 if (ip_len != sizeof (struct in6_addr))
363 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
372 return GNUNET_strdup (buf);
377 * Adjust exponential back-off and reconnect to the service.
384 * Generic error handler, called with the appropriate error code and
385 * the same closure specified at the creation of the message queue.
386 * Not every message queue implementation supports an error handler.
389 * @param error error code
392 mq_error_handler (void *cls,
393 enum GNUNET_MQ_Error error)
396 GNUNET_MQ_destroy (mq);
398 LOG (GNUNET_ERROR_TYPE_DEBUG,
399 "MQ error %d, reconnecting\n",
406 * Process pending requests to the resolver.
411 struct GNUNET_RESOLVER_GetMessage *msg;
412 struct GNUNET_MQ_Envelope *env;
413 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
422 /* nothing to do, release socket really soon if there is nothing
423 * else happening... */
426 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
431 if (GNUNET_NO != rh->was_transmitted)
432 return; /* waiting for reply */
433 env = GNUNET_MQ_msg_extra (msg,
435 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
436 msg->direction = htonl (rh->direction);
437 msg->af = htonl (rh->af);
438 GNUNET_memcpy (&msg[1],
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "Transmitting DNS resolution request to DNS service\n");
445 rh->was_transmitted = GNUNET_YES;
450 * Check validity of response with a hostname for a DNS lookup.
453 * @param msg message with the hostname
456 check_response (void *cls,
457 const struct GNUNET_MessageHeader *msg)
462 /* implemented in #handle_response() for now */
468 * Check validity of response with a hostname for a DNS lookup.
469 * NOTE: right now rather messy, might want to use different
470 * message types for different response formats in the future.
473 * @param msg message with the response
476 handle_response (void *cls,
477 const struct GNUNET_MessageHeader *msg)
479 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
486 /* Resolver service sent extra replies to query (after terminator)? Bad! */
488 GNUNET_MQ_destroy (mq);
493 size = ntohs (msg->size);
494 if (size == sizeof (struct GNUNET_MessageHeader))
496 LOG (GNUNET_ERROR_TYPE_DEBUG,
497 "Received empty response from DNS service\n");
498 /* message contains not data, just header; end of replies */
499 /* check if request was canceled */
500 if (GNUNET_SYSERR != rh->was_transmitted)
502 /* no reverse lookup was successful, return IP as string */
503 if (NULL != rh->name_callback)
505 if (GNUNET_NO == rh->received_response)
507 nret = no_resolve (rh->af,
510 rh->name_callback (rh->cls, nret);
513 /* finally, make termination call */
514 if (GNUNET_SYSERR != rh->was_transmitted)
515 rh->name_callback (rh->cls,
518 if ( (NULL != rh->addr_callback) &&
519 (GNUNET_SYSERR != rh->was_transmitted) )
520 rh->addr_callback (rh->cls,
524 rh->was_transmitted = GNUNET_NO;
525 GNUNET_RESOLVER_request_cancel (rh);
529 /* return reverse lookup results to caller */
530 if (NULL != rh->name_callback)
532 const char *hostname;
534 hostname = (const char *) &msg[1];
535 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
538 if (GNUNET_SYSERR != rh->was_transmitted)
539 rh->name_callback (rh->cls,
541 rh->was_transmitted = GNUNET_NO;
542 GNUNET_RESOLVER_request_cancel (rh);
543 GNUNET_MQ_destroy (mq);
548 LOG (GNUNET_ERROR_TYPE_DEBUG,
549 "Resolver returns `%s' for IP `%s'.\n",
551 GNUNET_a2s ((const void *) &rh[1],
553 if (rh->was_transmitted != GNUNET_SYSERR)
554 rh->name_callback (rh->cls,
556 rh->received_response = GNUNET_YES;
558 /* return lookup results to caller */
559 if (NULL != rh->addr_callback)
561 struct sockaddr_in v4;
562 struct sockaddr_in6 v6;
563 const struct sockaddr *sa;
569 ip_len = size - sizeof (struct GNUNET_MessageHeader);
570 if (ip_len == sizeof (struct in_addr))
572 memset (&v4, 0, sizeof (v4));
573 v4.sin_family = AF_INET;
574 v4.sin_addr = *(struct in_addr*) ip;
575 #if HAVE_SOCKADDR_IN_SIN_LEN
576 v4.sin_len = sizeof (v4);
579 sa = (const struct sockaddr *) &v4;
581 else if (ip_len == sizeof (struct in6_addr))
583 memset (&v6, 0, sizeof (v6));
584 v6.sin6_family = AF_INET6;
585 v6.sin6_addr = *(struct in6_addr*) ip;
586 #if HAVE_SOCKADDR_IN_SIN_LEN
587 v6.sin6_len = sizeof (v6);
590 sa = (const struct sockaddr *) &v6;
595 if (GNUNET_SYSERR != rh->was_transmitted)
596 rh->addr_callback (rh->cls,
599 rh->was_transmitted = GNUNET_NO;
600 GNUNET_RESOLVER_request_cancel (rh);
601 GNUNET_MQ_destroy (mq);
606 LOG (GNUNET_ERROR_TYPE_DEBUG,
607 "Received IP from DNS service\n");
608 if (GNUNET_SYSERR != rh->was_transmitted)
609 rh->addr_callback (rh->cls,
617 * We've been asked to lookup the address for a hostname and were
618 * given a valid numeric string. Perform the callbacks for the
621 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
624 numeric_resolution (void *cls)
626 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
627 struct sockaddr_in v4;
628 struct sockaddr_in6 v6;
629 const char *hostname;
632 memset (&v4, 0, sizeof (v4));
633 v4.sin_family = AF_INET;
634 #if HAVE_SOCKADDR_IN_SIN_LEN
635 v4.sin_len = sizeof (v4);
637 memset (&v6, 0, sizeof (v6));
638 v6.sin6_family = AF_INET6;
639 #if HAVE_SOCKADDR_IN_SIN_LEN
640 v6.sin6_len = sizeof (v6);
642 hostname = (const char *) &rh[1];
643 if ( ( (rh->af == AF_UNSPEC) ||
644 (rh->af == AF_INET) ) &&
645 (1 == inet_pton (AF_INET,
649 rh->addr_callback (rh->cls,
650 (const struct sockaddr *) &v4,
652 if ( (rh->af == AF_UNSPEC) &&
653 (GNUNET_SYSERR != rh->was_transmitted) &&
654 (1 == inet_pton (AF_INET6,
658 /* this can happen on some systems IF "hostname" is "localhost" */
659 rh->addr_callback (rh->cls,
660 (const struct sockaddr *) &v6,
663 if (GNUNET_SYSERR != rh->was_transmitted)
664 rh->addr_callback (rh->cls,
670 if ( ( (rh->af == AF_UNSPEC) ||
671 (rh->af == AF_INET6) ) &&
672 (1 == inet_pton (AF_INET6,
676 rh->addr_callback (rh->cls,
677 (const struct sockaddr *) &v6,
679 if (GNUNET_SYSERR != rh->was_transmitted)
680 rh->addr_callback (rh->cls,
686 /* why are we here? this task should not have been scheduled! */
693 * We've been asked to lookup the address for a hostname and were
694 * given a variant of "loopback". Perform the callbacks for the
695 * respective loopback numeric addresses.
697 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
700 loopback_resolution (void *cls)
702 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
703 struct sockaddr_in v4;
704 struct sockaddr_in6 v6;
707 memset (&v4, 0, sizeof (v4));
708 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
709 v4.sin_family = AF_INET;
710 #if HAVE_SOCKADDR_IN_SIN_LEN
711 v4.sin_len = sizeof (v4);
713 memset (&v6, 0, sizeof (v6));
714 v6.sin6_family = AF_INET6;
715 #if HAVE_SOCKADDR_IN_SIN_LEN
716 v6.sin6_len = sizeof (v6);
718 v6.sin6_addr = in6addr_loopback;
722 rh->addr_callback (rh->cls,
723 (const struct sockaddr *) &v4,
727 rh->addr_callback (rh->cls,
728 (const struct sockaddr *) &v6,
732 rh->addr_callback (rh->cls,
733 (const struct sockaddr *) &v6,
735 rh->addr_callback (rh->cls,
736 (const struct sockaddr *) &v4,
744 if (GNUNET_SYSERR != rh->was_transmitted)
745 rh->addr_callback (rh->cls,
748 LOG (GNUNET_ERROR_TYPE_DEBUG,
749 "Finished resolving hostname `%s'.\n",
750 (const char *) &rh[1]);
756 * Now try to reconnect to the resolver service.
761 reconnect_task (void *cls)
763 struct GNUNET_MQ_MessageHandler handlers[] = {
764 GNUNET_MQ_hd_var_size (response,
765 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
766 struct GNUNET_MessageHeader,
768 GNUNET_MQ_handler_end ()
773 if (NULL == req_head)
774 return; /* no work pending */
775 LOG (GNUNET_ERROR_TYPE_DEBUG,
776 "Trying to connect to DNS service\n");
777 mq = GNUNET_CLIENT_connect (resolver_cfg,
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
785 "Failed to connect, will try again later\n");
794 * Adjust exponential back-off and reconnect to the service.
799 struct GNUNET_RESOLVER_RequestHandle *rh;
803 GNUNET_assert (NULL == mq);
804 if (NULL != (rh = req_head))
806 switch (rh->was_transmitted)
809 /* nothing more to do */
812 /* disconnected, transmit again! */
813 rh->was_transmitted = GNUNET_NO;
816 /* request was cancelled, remove entirely */
817 GNUNET_CONTAINER_DLL_remove (req_head,
828 LOG (GNUNET_ERROR_TYPE_DEBUG,
829 "Will try to connect to DNS service in %s\n",
830 GNUNET_STRINGS_relative_time_to_string (backoff,
832 GNUNET_assert (NULL != resolver_cfg);
833 r_task = GNUNET_SCHEDULER_add_delayed (backoff,
836 backoff = GNUNET_TIME_STD_BACKOFF (backoff);
841 * A DNS resolution timed out. Notify the application.
843 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
846 handle_lookup_timeout (void *cls)
848 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
851 if (GNUNET_NO == rh->direction)
853 LOG (GNUNET_ERROR_TYPE_INFO,
854 _("Timeout trying to resolve hostname `%s'.\n"),
855 (const char *) &rh[1]);
856 if (NULL != rh->addr_callback)
857 rh->addr_callback (rh->cls,
863 #if !defined(GNUNET_CULL_LOGGING)
864 char buf[INET6_ADDRSTRLEN];
866 LOG (GNUNET_ERROR_TYPE_INFO,
867 _("Timeout trying to resolve IP address `%s'.\n"),
869 (const void *) &rh[1],
873 if (GNUNET_NO == rh->received_response)
877 nret = no_resolve (rh->af,
880 if (NULL != rh->name_callback)
881 rh->name_callback (rh->cls, nret);
884 /* finally, make termination call */
885 if (NULL != rh->name_callback)
886 rh->name_callback (rh->cls,
889 rh->was_transmitted = GNUNET_NO;
890 GNUNET_RESOLVER_request_cancel (rh);
896 * Convert a string to one or more IP addresses.
898 * @param hostname the hostname to resolve
899 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
900 * @param callback function to call with addresses
901 * @param callback_cls closure for @a callback
902 * @param timeout how long to try resolving
903 * @return handle that can be used to cancel the request, NULL on error
905 struct GNUNET_RESOLVER_RequestHandle *
906 GNUNET_RESOLVER_ip_get (const char *hostname,
908 struct GNUNET_TIME_Relative timeout,
909 GNUNET_RESOLVER_AddressCallback callback,
912 struct GNUNET_RESOLVER_RequestHandle *rh;
917 slen = strlen (hostname) + 1;
918 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >=
919 GNUNET_MAX_MESSAGE_SIZE)
924 LOG (GNUNET_ERROR_TYPE_DEBUG,
925 "Trying to resolve hostname `%s'.\n",
927 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
929 rh->addr_callback = callback;
930 rh->cls = callback_cls;
931 GNUNET_memcpy (&rh[1],
935 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
936 rh->direction = GNUNET_NO;
937 /* first, check if this is a numeric address */
938 if ( ( (1 == inet_pton (AF_INET,
942 (af == AF_UNSPEC) ) ) ||
943 ( (1 == inet_pton (AF_INET6,
946 ( (af == AF_INET6) ||
947 (af == AF_UNSPEC)) ) )
949 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution,
953 /* then, check if this is a loopback address */
954 for (unsigned int i = 0;
957 if (0 == strcasecmp (loopback[i],
960 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution,
964 if (GNUNET_OK != check_config ())
969 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
970 &handle_lookup_timeout,
972 GNUNET_CONTAINER_DLL_insert_tail (req_head,
975 rh->was_queued = GNUNET_YES;
978 GNUNET_SCHEDULER_cancel (s_task);
987 * We've been asked to convert an address to a string without
988 * a reverse lookup, either because the client asked for it
989 * or because the DNS lookup hit a timeout. Do the numeric
990 * conversion and invoke the callback.
992 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
995 numeric_reverse (void *cls)
997 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
1001 result = no_resolve (rh->af,
1004 LOG (GNUNET_ERROR_TYPE_DEBUG,
1005 "Resolver returns `%s'.\n",
1009 rh->name_callback (rh->cls,
1011 GNUNET_free (result);
1013 rh->name_callback (rh->cls,
1015 if (NULL != rh->task)
1017 GNUNET_SCHEDULER_cancel (rh->task);
1025 * Get an IP address as a string.
1027 * @param sa host address
1028 * @param salen length of host address in @a sa
1029 * @param do_resolve use #GNUNET_NO to return numeric hostname
1030 * @param timeout how long to try resolving
1031 * @param callback function to call with hostnames
1032 * last callback is NULL when finished
1033 * @param cls closure for @a callback
1034 * @return handle that can be used to cancel the request
1036 struct GNUNET_RESOLVER_RequestHandle *
1037 GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
1040 struct GNUNET_TIME_Relative timeout,
1041 GNUNET_RESOLVER_HostnameCallback callback,
1044 struct GNUNET_RESOLVER_RequestHandle *rh;
1048 if (GNUNET_OK != check_config ())
1050 LOG (GNUNET_ERROR_TYPE_ERROR,
1051 _("Resolver not configured correctly.\n"));
1055 switch (sa->sa_family)
1058 GNUNET_assert (salen == sizeof (struct sockaddr_in));
1059 ip_len = sizeof (struct in_addr);
1060 ip = &((const struct sockaddr_in*)sa)->sin_addr;
1063 GNUNET_assert (salen == sizeof (struct sockaddr_in6));
1064 ip_len = sizeof (struct in6_addr);
1065 ip = &((const struct sockaddr_in6*)sa)->sin6_addr;
1071 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
1072 rh->name_callback = callback;
1074 rh->af = sa->sa_family;
1075 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1076 GNUNET_memcpy (&rh[1],
1079 rh->data_len = ip_len;
1080 rh->direction = GNUNET_YES;
1081 rh->received_response = GNUNET_NO;
1082 if (GNUNET_NO == do_resolve)
1084 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse,
1088 rh->task = GNUNET_SCHEDULER_add_delayed (timeout,
1089 &handle_lookup_timeout,
1091 GNUNET_CONTAINER_DLL_insert_tail (req_head,
1094 rh->was_queued = GNUNET_YES;
1097 GNUNET_SCHEDULER_cancel (s_task);
1100 process_requests ();
1106 * Get local fully qualified af name
1111 GNUNET_RESOLVER_local_fqdn_get ()
1113 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1115 if (0 != gethostname (hostname,
1116 sizeof (hostname) - 1))
1118 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1122 LOG (GNUNET_ERROR_TYPE_DEBUG,
1123 "Resolving our FQDN `%s'\n",
1125 #if HAVE_GETADDRINFO
1127 struct addrinfo *ai;
1131 if (0 != (ret = getaddrinfo (hostname,
1136 LOG (GNUNET_ERROR_TYPE_ERROR,
1137 _("Could not resolve our FQDN: %s\n"),
1138 gai_strerror (ret));
1141 if (NULL != ai->ai_canonname)
1142 rval = GNUNET_strdup (ai->ai_canonname);
1144 rval = GNUNET_strdup (hostname);
1148 #elif HAVE_GETHOSTBYNAME2
1150 struct hostent *host;
1152 host = gethostbyname2 (hostname,
1155 host = gethostbyname2 (hostname,
1159 LOG (GNUNET_ERROR_TYPE_ERROR,
1160 _("Could not resolve our FQDN: %s\n"),
1161 hstrerror (h_errno));
1164 return GNUNET_strdup (host->h_name);
1166 #elif HAVE_GETHOSTBYNAME
1168 struct hostent *host;
1170 host = gethostbyname (hostname);
1173 LOG (GNUNET_ERROR_TYPE_ERROR,
1174 _("Could not resolve our FQDN: %s\n"),
1175 hstrerror (h_errno));
1178 return GNUNET_strdup (host->h_name);
1181 /* fallback: just hope name is already FQDN */
1182 return GNUNET_strdup (hostname);
1188 * Looking our own hostname.
1190 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
1191 * @param timeout how long to try resolving
1192 * @param callback function to call with addresses
1193 * @param cls closure for @a callback
1194 * @return handle that can be used to cancel the request, NULL on error
1196 struct GNUNET_RESOLVER_RequestHandle *
1197 GNUNET_RESOLVER_hostname_resolve (int af,
1198 struct GNUNET_TIME_Relative timeout,
1199 GNUNET_RESOLVER_AddressCallback callback,
1202 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
1204 if (0 != gethostname (hostname, sizeof (hostname) - 1))
1206 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1210 LOG (GNUNET_ERROR_TYPE_DEBUG,
1211 "Resolving our hostname `%s'\n",
1213 return GNUNET_RESOLVER_ip_get (hostname,
1222 * Cancel a request that is still pending with the resolver.
1223 * Note that a client MUST NOT cancel a request that has
1224 * been completed (i.e, the callback has been called to
1225 * signal timeout or the final result).
1227 * @param rh handle of request to cancel
1230 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
1232 if (GNUNET_NO == rh->direction)
1233 LOG (GNUNET_ERROR_TYPE_DEBUG,
1234 "Asked to cancel request to resolve hostname `%s'.\n",
1235 (const char *) &rh[1]);
1236 if (NULL != rh->task)
1238 GNUNET_SCHEDULER_cancel (rh->task);
1241 if (GNUNET_NO == rh->was_transmitted)
1243 if (GNUNET_YES == rh->was_queued)
1244 GNUNET_CONTAINER_DLL_remove (req_head,
1248 check_disconnect ();
1251 GNUNET_assert (GNUNET_YES == rh->was_transmitted);
1252 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
1253 check_disconnect ();
1257 /* end of resolver_api.c */