/*
This file is part of GNUnet.
- Copyright (C) 2009-2014 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2009-2015 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
/**
* Task for reconnecting.
*/
-static struct GNUNET_SCHEDULER_Task * r_task;
+static struct GNUNET_SCHEDULER_Task *r_task;
/**
* Task ID of shutdown task; only present while we have a
* connection to the resolver service.
*/
-static struct GNUNET_SCHEDULER_Task * s_task;
+static struct GNUNET_SCHEDULER_Task *s_task;
/**
GNUNET_RESOLVER_HostnameCallback name_callback;
/**
- * Closure for the respective "callback".
+ * Closure for the callbacks.
*/
void *cls;
* Task handle for making reply callbacks in numeric lookups
* asynchronous, and for timeout handling.
*/
- struct GNUNET_SCHEDULER_Task * task;
+ struct GNUNET_SCHEDULER_Task *task;
/**
* Desired address family.
/**
* Check that the resolver service runs on localhost
* (or equivalent).
+ *
+ * @return #GNUNET_OK if the resolver is properly configured,
+ * #GNUNET_SYSERR otherwise.
*/
-static void
+static int
check_config ()
{
char *hostname;
"HOSTNAME",
&hostname))
{
- LOG (GNUNET_ERROR_TYPE_ERROR,
- _("Must specify `%s' for `%s' in configuration!\n"),
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ _("Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"),
"HOSTNAME",
"resolver");
- GNUNET_assert (0);
+ return GNUNET_SYSERR;
}
- if ((1 != inet_pton (AF_INET, hostname, &v4)) ||
- (1 != inet_pton (AF_INET6, hostname, &v6)))
+ if ((1 == inet_pton (AF_INET, hostname, &v4)) ||
+ (1 == inet_pton (AF_INET6, hostname, &v6)))
{
GNUNET_free (hostname);
- return;
+ return GNUNET_OK;
}
i = 0;
while (NULL != loopback[i])
if (0 == strcasecmp (loopback[i++], hostname))
{
GNUNET_free (hostname);
- return;
+ return GNUNET_OK;
}
- LOG (GNUNET_ERROR_TYPE_ERROR,
- _("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"),
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ _("Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"),
"localhost",
"HOSTNAME",
"resolver");
GNUNET_free (hostname);
- GNUNET_assert (0);
+ return GNUNET_SYSERR;
}
GNUNET_assert (NULL != cfg);
backoff = GNUNET_TIME_UNIT_MILLISECONDS;
resolver_cfg = cfg;
- check_config ();
+ (void) check_config ();
}
size = ntohs (msg->size);
if (size == sizeof (struct GNUNET_MessageHeader))
{
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Received empty response from DNS service\n");
/* message contains not data, just header; end of replies */
/* check if request was canceled */
if (GNUNET_SYSERR != rh->was_transmitted)
{
+ /* no reverse lookup was successful, return IP as string */
if (NULL != rh->name_callback)
+ {
+ if (GNUNET_NO == rh->received_response)
+ {
+ nret = no_resolve (rh->af,
+ &rh[1],
+ rh->data_len);
+ rh->name_callback (rh->cls, nret);
+ GNUNET_free (nret);
+ }
+ /* finally, make termination call */
rh->name_callback (rh->cls,
NULL);
+ }
if (NULL != rh->addr_callback)
rh->addr_callback (rh->cls,
NULL,
reconnect ();
return;
}
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Received IP from DNS service\n");
if (GNUNET_SYSERR != rh->was_transmitted)
rh->addr_callback (rh->cls,
sa,
* numeric addresses.
*
* @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
- * @param tc unused scheduler context
*/
static void
-numeric_resolution (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+numeric_resolution (void *cls)
{
struct GNUNET_RESOLVER_RequestHandle *rh = cls;
struct sockaddr_in v4;
* respective loopback numeric addresses.
*
* @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
- * @param tc unused scheduler context
*/
static void
-loopback_resolution (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+loopback_resolution (void *cls)
{
struct GNUNET_RESOLVER_RequestHandle *rh = cls;
struct sockaddr_in v4;
rh->addr_callback (rh->cls,
(const struct sockaddr *) &v4,
sizeof (v4));
+
break;
default:
GNUNET_break (0);
* Task executed on system shutdown.
*/
static void
-shutdown_task (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
{
s_task = NULL;
GNUNET_RESOLVER_disconnect ();
/* nothing to do, release socket really soon if there is nothing
* else happening... */
s_task =
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
&shutdown_task,
NULL);
return;
* Now try to reconnect to the resolver service.
*
* @param cls NULL
- * @param tc scheduler context
*/
static void
-reconnect_task (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+reconnect_task (void *cls)
{
r_task = NULL;
if (NULL == req_head)
return; /* no work pending */
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Trying to connect to DNS service\n");
client = GNUNET_CLIENT_connect ("resolver",
break;
case GNUNET_SYSERR:
/* request was cancelled, remove entirely */
- GNUNET_CONTAINER_DLL_remove (req_head, req_tail, rh);
+ GNUNET_CONTAINER_DLL_remove (req_head,
+ req_tail,
+ rh);
GNUNET_free (rh);
break;
default:
* A DNS resolution timed out. Notify the application.
*
* @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
- * @param tc scheduler context
*/
static void
-handle_lookup_timeout (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+handle_lookup_timeout (void *cls)
{
struct GNUNET_RESOLVER_RequestHandle *rh = cls;
* @return handle that can be used to cancel the request, NULL on error
*/
struct GNUNET_RESOLVER_RequestHandle *
-GNUNET_RESOLVER_ip_get (const char *hostname, int af,
+GNUNET_RESOLVER_ip_get (const char *hostname,
+ int af,
struct GNUNET_TIME_Relative timeout,
GNUNET_RESOLVER_AddressCallback callback,
void *callback_cls)
* conversion and invoke the callback.
*
* @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
- * @param tc unused scheduler context
*/
static void
-numeric_reverse (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+numeric_reverse (void *cls)
{
struct GNUNET_RESOLVER_RequestHandle *rh = cls;
char *result;
size_t ip_len;
const void *ip;
- check_config ();
+ if (GNUNET_OK != check_config ())
+ {
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ _("Resolver not configured correctly.\n"));
+ return NULL;
+ }
+
switch (sa->sa_family)
{
case AF_INET:
+ GNUNET_assert (salen == sizeof (struct sockaddr_in));
ip_len = sizeof (struct in_addr);
ip = &((const struct sockaddr_in*)sa)->sin_addr;
break;
case AF_INET6:
+ GNUNET_assert (salen == sizeof (struct sockaddr_in6));
ip_len = sizeof (struct in6_addr);
ip = &((const struct sockaddr_in6*)sa)->sin6_addr;
break;
char *
GNUNET_RESOLVER_local_fqdn_get ()
{
- struct hostent *host;
char hostname[GNUNET_OS_get_hostname_max_length () + 1];
if (0 != gethostname (hostname, sizeof (hostname) - 1))
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Resolving our FQDN `%s'\n",
hostname);
- host = gethostbyname (hostname);
- if (NULL == host)
+#if HAVE_GETADDRINFO
{
- LOG (GNUNET_ERROR_TYPE_ERROR,
- _("Could not resolve our FQDN : %s\n"),
- hstrerror (h_errno));
- return NULL;
+ struct addrinfo *ai;
+ int ret;
+ char *rval;
+
+ if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
+ {
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ _("Could not resolve our FQDN: %s\n"),
+ gai_strerror (ret));
+ return NULL;
+ }
+ if (NULL != ai->ai_canonname)
+ rval = GNUNET_strdup (ai->ai_canonname);
+ else
+ rval = GNUNET_strdup (hostname);
+ freeaddrinfo (ai);
+ return rval;
}
- return GNUNET_strdup (host->h_name);
+#elif HAVE_GETHOSTBYNAME2
+ {
+ struct hostent *host;
+
+ host = gethostbyname2 (hostname, AF_INET);
+ if (NULL == host)
+ host = gethostbyname2 (hostname, AF_INET6);
+ if (NULL == host)
+ {
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ _("Could not resolve our FQDN: %s\n"),
+ hstrerror (h_errno));
+ return NULL;
+ }
+ return GNUNET_strdup (host->h_name);
+ }
+#elif HAVE_GETHOSTBYNAME
+ {
+ struct hostent *host;
+
+ host = gethostbyname (hostname);
+ if (NULL == host)
+ {
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ _("Could not resolve our FQDN: %s\n"),
+ hstrerror (h_errno));
+ return NULL;
+ }
+ return GNUNET_strdup (host->h_name);
+ }
+#else
+ /* fallback: just hope name is already FQDN */
+ return GNUNET_strdup (hostname);
+#endif
}