GNUnet  0.20.0
gnunet-service-resolver.c File Reference

code to do DNS resolution More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_statistics_service.h"
#include "resolver.h"
Include dependency graph for gnunet-service-resolver.c:

Go to the source code of this file.

Data Structures

struct  RecordListEntry
 Entry in list of cached DNS records for a hostname. More...
 
struct  ResolveCache
 A cached DNS lookup result. More...
 
struct  ActiveLookup
 Information about pending lookups. More...
 

Macros

#define DNS_TIMEOUT   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 How long do we wait for DNS answers? More...
 
#define MAX_CACHE   1024
 Maximum number of hostnames we cache results for. More...
 

Functions

static void free_cache_entry (struct ResolveCache *rc)
 Remove entry from cache. More...
 
static void free_hosts_entry (struct ResolveCache *rc)
 Remove entry from cache. More...
 
static void free_active_lookup (struct ActiveLookup *al)
 Release resources associated with al. More...
 
static char * extract_dns_server (const char *line, size_t line_len)
 Find out if the configuration file line contains a string starting with "nameserver ", and if so, return a copy of the nameserver's IP. More...
 
static char * extract_search_domain (const char *line, size_t line_len)
 Find out if the configuration file line contains a string starting with "search ", and if so, return a copy of the machine's search domain. More...
 
static int lookup_dns_servers (char ***server_addrs)
 Reads the list of nameservers from /etc/resolve.conf. More...
 
static char * make_reverse_hostname (const void *ip, int af)
 Compute name to use for DNS reverse lookups from ip. More...
 
static int send_reply (struct GNUNET_DNSPARSER_Record *record, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
 Send DNS record back to our client. More...
 
static void send_end_msg (uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
 Send message to client that we transmitted all responses for client_request_id. More...
 
static int remove_expired (struct ResolveCache *rc)
 Remove expired entries from rc. More...
 
static void process_get (const char *hostname, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
 Process DNS request for hostname with request ID request_id from client demanding records of type record_type. More...
 
static int try_cache (const char *hostname, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
 Get an IP address as a string (works for both IPv4 and IPv6). More...
 
static int pack (const char *hostname, uint16_t type, uint16_t dns_id, char **packet_buf, size_t *packet_size)
 Create DNS query for hostname of type type with DNS request ID dns_id. More...
 
static void cache_answers (const char *name, struct GNUNET_DNSPARSER_Record *records, unsigned int num_records)
 
static void handle_resolve_result (void *cls, const struct GNUNET_TUN_DnsHeader *dns, size_t dns_len)
 We got a result from DNS. More...
 
static void handle_resolve_timeout (void *cls)
 We encountered a timeout trying to perform a DNS lookup. More...
 
static int resolve_and_cache (const char *hostname, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
 Initiate an active lookup, then cache the result and try to then complete the resolution. More...
 
static int check_get (void *cls, const struct GNUNET_RESOLVER_GetMessage *get)
 Verify well-formedness of GET-message. More...
 
static void handle_get (void *cls, const struct GNUNET_RESOLVER_GetMessage *msg)
 Handle GET-message. More...
 
static void shutdown_task (void *cls)
 Service is shutting down, clean up. More...
 
static void add_host (const char *hostname, uint16_t rec_type, const void *data, size_t data_size)
 Add information about a host from /etc/hosts to our cache. More...
 
static void extract_hosts (const char *line, size_t line_len)
 Extract host information from a line in /etc/hosts. More...
 
static void load_etc_hosts (void)
 Reads the list of hosts from /etc/hosts. More...
 
static void init_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_SERVICE_Handle *sh)
 Service is starting, initialize everything. More...
 
static void * connect_cb (void *cls, struct GNUNET_SERVICE_Client *c, struct GNUNET_MQ_Handle *mq)
 Callback called when a client connects to the service. More...
 
static void disconnect_cb (void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
 Callback called when a client disconnected from the service. More...
 
 GNUNET_SERVICE_MAIN ("resolver", GNUNET_SERVICE_OPTION_NONE, &init_cb, &connect_cb, &disconnect_cb, NULL, GNUNET_MQ_hd_var_size(get, GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST, struct GNUNET_RESOLVER_GetMessage, NULL), GNUNET_MQ_handler_end())
 Define "main" method using service macro. More...
 

Variables

static struct ResolveCachecache_head
 Start of the linked list of cached DNS lookup results. More...
 
static struct ResolveCachecache_tail
 Tail of the linked list of cached DNS lookup results. More...
 
static struct ResolveCachehosts_head
 Head of the linked list of DNS lookup results from /etc/hosts. More...
 
static struct ResolveCachehosts_tail
 Tail of the linked list of DNS lookup results from /etc/hosts. More...
 
static struct ActiveLookuplookup_head
 Start of the linked list of active DNS lookups. More...
 
static struct ActiveLookuplookup_tail
 Tail of the linked list of active DNS lookups. More...
 
static struct GNUNET_DNSSTUB_Contextdnsstub_ctx
 context of dnsstub library More...
 
static char * my_domain
 My domain, to be appended to the hostname to get a FQDN. More...
 
static unsigned int cache_size
 How many entries do we have in cache_head DLL? More...
 

Detailed Description

code to do DNS resolution

Author
Christian Grothoff

Definition in file gnunet-service-resolver.c.

Macro Definition Documentation

◆ DNS_TIMEOUT

How long do we wait for DNS answers?

Definition at line 37 of file gnunet-service-resolver.c.

◆ MAX_CACHE

#define MAX_CACHE   1024

Maximum number of hostnames we cache results for.

Definition at line 42 of file gnunet-service-resolver.c.

Function Documentation

◆ free_cache_entry()

static void free_cache_entry ( struct ResolveCache rc)
static

Remove entry from cache.

Parameters
rcentry to free

Definition at line 210 of file gnunet-service-resolver.c.

211 {
212  struct RecordListEntry *pos;
213 
214  while (NULL != (pos = rc->records_head))
215  {
218  GNUNET_free (pos->record);
219  GNUNET_free (pos);
220  }
221  GNUNET_free (rc->hostname);
223  cache_size--;
224  GNUNET_free (rc);
225 }
static struct ResolveCache * cache_head
Start of the linked list of cached DNS lookup results.
static unsigned int cache_size
How many entries do we have in cache_head DLL?
static struct ResolveCache * cache_tail
Tail of the linked list of cached DNS lookup results.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
void GNUNET_DNSPARSER_free_record(struct GNUNET_DNSPARSER_Record *r)
Free the given DNS record.
Definition: dnsparser.c:169
#define GNUNET_free(ptr)
Wrapper around free.
Entry in list of cached DNS records for a hostname.
struct GNUNET_DNSPARSER_Record * record
Cached data.
struct RecordListEntry * records_tail
tail of a double linked list containing the lookup results
struct RecordListEntry * records_head
head of a double linked list containing the lookup results
char * hostname
Which hostname is this cache for?

References cache_head, cache_size, cache_tail, GNUNET_CONTAINER_DLL_remove, GNUNET_DNSPARSER_free_record(), GNUNET_free, ResolveCache::hostname, RecordListEntry::record, ResolveCache::records_head, and ResolveCache::records_tail.

Referenced by handle_resolve_result(), remove_expired(), and shutdown_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_hosts_entry()

static void free_hosts_entry ( struct ResolveCache rc)
static

Remove entry from cache.

Parameters
rcentry to free

Definition at line 234 of file gnunet-service-resolver.c.

235 {
236  struct RecordListEntry *pos;
237 
238  while (NULL != (pos = rc->records_head))
239  {
242  GNUNET_free (pos->record);
243  GNUNET_free (pos);
244  }
245  GNUNET_free (rc->hostname);
247  cache_size--;
248  GNUNET_free (rc);
249 }
static struct ResolveCache * hosts_head
Head of the linked list of DNS lookup results from /etc/hosts.
static struct ResolveCache * hosts_tail
Tail of the linked list of DNS lookup results from /etc/hosts.

References cache_size, GNUNET_CONTAINER_DLL_remove, GNUNET_DNSPARSER_free_record(), GNUNET_free, ResolveCache::hostname, hosts_head, hosts_tail, RecordListEntry::record, ResolveCache::records_head, and ResolveCache::records_tail.

Referenced by shutdown_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_active_lookup()

static void free_active_lookup ( struct ActiveLookup al)
static

Release resources associated with al.

Parameters
alan active lookup

Definition at line 258 of file gnunet-service-resolver.c.

259 {
261  if (NULL != al->resolve_handle)
262  {
264  al->resolve_handle = NULL;
265  }
266  if (NULL != al->timeout_task)
267  {
269  al->timeout_task = NULL;
270  }
271  GNUNET_free (al->hostname);
272  GNUNET_free (al);
273 }
static struct ActiveLookup * lookup_head
Start of the linked list of active DNS lookups.
static struct ActiveLookup * lookup_tail
Tail of the linked list of active DNS lookups.
void GNUNET_DNSSTUB_resolve_cancel(struct GNUNET_DNSSTUB_RequestSocket *rs)
Cancel DNS resolution.
Definition: dnsstub.c:562
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
char * hostname
Which hostname are we resolving?
struct GNUNET_SCHEDULER_Task * timeout_task
handle for the resolution timeout task
struct GNUNET_DNSSTUB_RequestSocket * resolve_handle
handle for cancelling a request

References GNUNET_CONTAINER_DLL_remove, GNUNET_DNSSTUB_resolve_cancel(), GNUNET_free, GNUNET_SCHEDULER_cancel(), ActiveLookup::hostname, lookup_head, lookup_tail, ActiveLookup::resolve_handle, and ActiveLookup::timeout_task.

Referenced by disconnect_cb(), handle_resolve_result(), handle_resolve_timeout(), and shutdown_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ extract_dns_server()

static char* extract_dns_server ( const char *  line,
size_t  line_len 
)
static

Find out if the configuration file line contains a string starting with "nameserver ", and if so, return a copy of the nameserver's IP.

Parameters
lineline to parse
line_lennumber of characters in line
Returns
NULL if no nameserver is configured in this line

Definition at line 286 of file gnunet-service-resolver.c.

287 {
288  if (0 == strncmp (line, "nameserver ", strlen ("nameserver ")))
289  return GNUNET_strndup (line + strlen ("nameserver "),
290  line_len - strlen ("nameserver "));
291  return NULL;
292 }
static char * line
Desired phone line (string to be converted to a hash).
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.

References GNUNET_strndup, and line.

Referenced by lookup_dns_servers().

Here is the caller graph for this function:

◆ extract_search_domain()

static char* extract_search_domain ( const char *  line,
size_t  line_len 
)
static

Find out if the configuration file line contains a string starting with "search ", and if so, return a copy of the machine's search domain.

Parameters
lineline to parse
line_lennumber of characters in line
Returns
NULL if no nameserver is configured in this line

Definition at line 305 of file gnunet-service-resolver.c.

306 {
307  if (0 == strncmp (line, "search ", strlen ("search ")))
308  return GNUNET_strndup (line + strlen ("search "),
309  line_len - strlen ("search "));
310  return NULL;
311 }

References GNUNET_strndup, and line.

Referenced by lookup_dns_servers().

Here is the caller graph for this function:

◆ lookup_dns_servers()

static int lookup_dns_servers ( char ***  server_addrs)
static

Reads the list of nameservers from /etc/resolve.conf.

Parameters
[out]server_addrsa list of null-terminated server address strings
Returns
the number of server addresses in server_addrs, -1 on error

Definition at line 321 of file gnunet-service-resolver.c.

322 {
323  struct GNUNET_DISK_FileHandle *fh;
324  struct GNUNET_DISK_MapHandle *mh;
325  off_t bytes_read;
326  const char *buf;
327  size_t read_offset;
328  unsigned int num_dns_servers;
329 
330  fh = GNUNET_DISK_file_open ("/etc/resolv.conf",
333  if (NULL == fh)
334  {
336  "Could not open /etc/resolv.conf. "
337  "DNS resolution will not be possible.\n");
338  return -1;
339  }
340  if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read))
341  {
343  "Could not determine size of /etc/resolv.conf. "
344  "DNS resolution will not be possible.\n");
346  return -1;
347  }
348  if (((unsigned long long) bytes_read) > (unsigned long long) SIZE_MAX)
349  {
351  "/etc/resolv.conf file too large to mmap. "
352  "DNS resolution will not be possible.\n");
354  return -1;
355  }
357  &mh,
359  (size_t) bytes_read);
360  *server_addrs = NULL;
361  read_offset = 0;
362  num_dns_servers = 0;
363  while (read_offset < (size_t) bytes_read)
364  {
365  const char *newline;
366  size_t line_len;
367  char *dns_server;
368 
369  newline = strchr (buf + read_offset, '\n');
370  if (NULL == newline)
371  break;
372  line_len = newline - buf - read_offset;
373  dns_server = extract_dns_server (buf + read_offset, line_len);
374  if (NULL != dns_server)
375  {
376  GNUNET_array_append (*server_addrs, num_dns_servers, dns_server);
377  }
378  else if (NULL == my_domain)
379  {
380  my_domain = extract_search_domain (buf + read_offset, line_len);
381  }
382  read_offset += line_len + 1;
383  }
386  return (int) num_dns_servers;
387 }
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
static char * my_domain
My domain, to be appended to the hostname to get a FQDN.
static char * extract_dns_server(const char *line, size_t line_len)
Find out if the configuration file line contains a string starting with "nameserver ",...
static char * extract_search_domain(const char *line, size_t line_len)
Find out if the configuration file line contains a string starting with "search ",...
static char buf[2048]
static struct GNUNET_DISK_FileHandle * fh
File handle to STDIN, for reading restart/quit commands.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1237
void * GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m, enum GNUNET_DISK_MapType access, size_t len)
Map a file into memory.
Definition: disk.c:1380
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1308
enum GNUNET_GenericReturnValue GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
Definition: disk.c:192
enum GNUNET_GenericReturnValue GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
Unmap a file.
Definition: disk.c:1411
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_DISK_MAP_TYPE_READ
Read-only memory map.
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define SIZE_MAX
Definition: platform.h:208
Handle used to access files (and pipes).
Handle for a memory-mapping operation.
Definition: disk.c:1361

References buf, extract_dns_server(), extract_search_domain(), fh, GNUNET_array_append, GNUNET_DISK_file_close(), GNUNET_DISK_file_handle_size(), GNUNET_DISK_file_map(), GNUNET_DISK_file_open(), GNUNET_DISK_file_unmap(), GNUNET_DISK_MAP_TYPE_READ, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, mh, my_domain, and SIZE_MAX.

Referenced by init_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_reverse_hostname()

static char* make_reverse_hostname ( const void *  ip,
int  af 
)
static

Compute name to use for DNS reverse lookups from ip.

Parameters
ipIP address to resolve, in binary format, network byte order
afaddress family of ip, AF_INET or AF_INET6

Definition at line 397 of file gnunet-service-resolver.c.

398 {
399  char *buf = GNUNET_new_array (80, char);
400  int pos = 0;
401 
402  if (AF_INET == af)
403  {
404  struct in_addr *addr = (struct in_addr *) ip;
405  uint32_t ip_int = addr->s_addr;
406 
407  for (int i = 3; i >= 0; i--)
408  {
409  int n =
410  GNUNET_snprintf (buf + pos, 80 - pos, "%u.", ((uint8_t *) &ip_int)[i]);
411  if (n < 0)
412  {
413  GNUNET_free (buf);
414  return NULL;
415  }
416  pos += n;
417  }
418  pos += GNUNET_snprintf (buf + pos, 80 - pos, "in-addr.arpa");
419  }
420  else if (AF_INET6 == af)
421  {
422  struct in6_addr *addr = (struct in6_addr *) ip;
423  for (int i = 15; i >= 0; i--)
424  {
425  int n =
426  GNUNET_snprintf (buf + pos, 80 - pos, "%x.", addr->s6_addr[i] & 0xf);
427  if (n < 0)
428  {
429  GNUNET_free (buf);
430  return NULL;
431  }
432  pos += n;
433  n = GNUNET_snprintf (buf + pos, 80 - pos, "%x.", addr->s6_addr[i] >> 4);
434  if (n < 0)
435  {
436  GNUNET_free (buf);
437  return NULL;
438  }
439  pos += n;
440  }
441  pos += GNUNET_snprintf (buf + pos, 80 - pos, "ip6.arpa");
442  }
443  buf[pos] = '\0';
444  return buf;
445 }
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.

References buf, GNUNET_free, GNUNET_new_array, and GNUNET_snprintf().

Referenced by handle_get().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_reply()

static int send_reply ( struct GNUNET_DNSPARSER_Record record,
uint16_t  record_type,
uint32_t  client_request_id,
struct GNUNET_SERVICE_Client client 
)
static

Send DNS record back to our client.

Parameters
recordinformation to transmit
record_typerequested record type from client
client_request_idto which request are we responding
clientwhere to send record
Returns
GNUNET_YES if we sent a reply, GNUNET_NO if the record type is not understood or does not match record_type

Definition at line 460 of file gnunet-service-resolver.c.

464 {
466  struct GNUNET_MQ_Envelope *env;
467  const void *payload;
468  size_t payload_len;
469 
470  switch (record->type)
471  {
473  if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
474  return GNUNET_NO;
475  payload = record->data.hostname;
476  payload_len = strlen (record->data.hostname) + 1;
477  break;
478 
480  if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
481  return GNUNET_NO;
482  payload = record->data.hostname;
483  payload_len = strlen (record->data.hostname) + 1;
484  break;
485 
487  if ((GNUNET_DNSPARSER_TYPE_A != record_type) &&
488  (GNUNET_DNSPARSER_TYPE_ALL != record_type))
489  return GNUNET_NO;
490  payload = record->data.raw.data;
491  payload_len = record->data.raw.data_len;
492  break;
493 
495  if ((GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
496  (GNUNET_DNSPARSER_TYPE_ALL != record_type))
497  return GNUNET_NO;
498  payload = record->data.raw.data;
499  payload_len = record->data.raw.data_len;
500  break;
501 
502  default:
504  "Cannot handle DNS response type %u: not supported here\n",
505  record->type);
506  return GNUNET_NO;
507  }
509  payload_len,
511  msg->client_id = client_request_id;
512  GNUNET_memcpy (&msg[1], payload, payload_len);
514  return GNUNET_YES;
515 }
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
static unsigned long long payload
How much data are we currently storing in the database?
#define GNUNET_DNSPARSER_TYPE_ALL
#define GNUNET_DNSPARSER_TYPE_A
#define GNUNET_DNSPARSER_TYPE_PTR
#define GNUNET_DNSPARSER_TYPE_CNAME
#define GNUNET_DNSPARSER_TYPE_AAAA
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_ERROR_TYPE_DEBUG
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:304
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
Definition: gnunet_mq_lib.h:63
#define GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE
Response to a DNS resolution request.
struct GNUNET_MQ_Handle * GNUNET_SERVICE_client_get_mq(struct GNUNET_SERVICE_Client *c)
Obtain the message queue of c.
Definition: service.c:2443

References env, GNUNET_DNSPARSER_TYPE_A, GNUNET_DNSPARSER_TYPE_AAAA, GNUNET_DNSPARSER_TYPE_ALL, GNUNET_DNSPARSER_TYPE_CNAME, GNUNET_DNSPARSER_TYPE_PTR, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE, GNUNET_MQ_msg_extra, GNUNET_MQ_send(), GNUNET_NO, GNUNET_SERVICE_client_get_mq(), GNUNET_YES, msg, payload, and record().

Referenced by try_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_end_msg()

static void send_end_msg ( uint32_t  client_request_id,
struct GNUNET_SERVICE_Client client 
)
static

Send message to client that we transmitted all responses for client_request_id.

Parameters
client_request_idto which request are we responding
clientwhere to send record

Definition at line 526 of file gnunet-service-resolver.c.

527 {
529  struct GNUNET_MQ_Envelope *env;
530 
531  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending END message\n");
533  msg->client_id = client_request_id;
535 }
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:78

References env, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE, GNUNET_MQ_msg, GNUNET_MQ_send(), GNUNET_SERVICE_client_get_mq(), and msg.

Referenced by handle_resolve_result(), handle_resolve_timeout(), process_get(), and try_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove_expired()

static int remove_expired ( struct ResolveCache rc)
static

Remove expired entries from rc.

Parameters
rcentry in resolver cache
Returns
GNUNET_YES if rc was completely expired GNUNET_NO if some entries are left

Definition at line 546 of file gnunet-service-resolver.c.

547 {
549  struct RecordListEntry *n;
550 
551  for (struct RecordListEntry *pos = rc->records_head; NULL != pos; pos = n)
552  {
553  n = pos->next;
554  if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
555  {
557  GNUNET_DNSPARSER_free_record (pos->record);
558  GNUNET_free (pos->record);
559  GNUNET_free (pos);
560  }
561  }
562  if (NULL == rc->records_head)
563  {
564  free_cache_entry (rc);
565  return GNUNET_YES;
566  }
567  return GNUNET_NO;
568 }
static void free_cache_entry(struct ResolveCache *rc)
Remove entry from cache.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
struct RecordListEntry * next
This is a doubly linked list.

References GNUNET_TIME_Absolute::abs_value_us, free_cache_entry(), GNUNET_CONTAINER_DLL_remove, GNUNET_DNSPARSER_free_record(), GNUNET_free, GNUNET_NO, GNUNET_TIME_absolute_get(), GNUNET_YES, RecordListEntry::next, ResolveCache::records_head, and ResolveCache::records_tail.

Referenced by try_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_get()

static void process_get ( const char *  hostname,
uint16_t  record_type,
uint32_t  client_request_id,
struct GNUNET_SERVICE_Client client 
)
static

Process DNS request for hostname with request ID request_id from client demanding records of type record_type.

Process DNS request for hostname with request ID client_request_id from client demanding records of type record_type.

Parameters
hostnameDNS name to resolve
record_typedesired record type
client_request_idclient's request ID
clientwho should get the result?

Definition at line 947 of file gnunet-service-resolver.c.

951 {
952  char fqdn[255];
953 
954  if (GNUNET_NO != try_cache (hostname, record_type, client_request_id, client))
955  return;
956  if ((NULL != my_domain) && (NULL == strchr (hostname, (unsigned char) '.')) &&
957  (strlen (hostname) + strlen (my_domain) <= 253))
958  {
959  GNUNET_snprintf (fqdn, sizeof(fqdn), "%s.%s", hostname, my_domain);
960  }
961  else if (strlen (hostname) < 255)
962  {
963  GNUNET_snprintf (fqdn, sizeof(fqdn), "%s", hostname);
964  }
965  else
966  {
967  GNUNET_break (0);
969  return;
970  }
971  if (GNUNET_NO == try_cache (fqdn, record_type, client_request_id, client))
972  {
973  if (GNUNET_OK !=
974  resolve_and_cache (fqdn, record_type, client_request_id, client))
975  {
976  send_end_msg (client_request_id, client);
977  }
978  }
979 }
static void send_end_msg(uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
Send message to client that we transmitted all responses for client_request_id.
static int resolve_and_cache(const char *hostname, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
Initiate an active lookup, then cache the result and try to then complete the resolution.
static int try_cache(const char *hostname, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
Get an IP address as a string (works for both IPv4 and IPv6).
static char * hostname
Our hostname; we give this to all the peers we start.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2330

References ActiveLookup::client, ActiveLookup::client_request_id, GNUNET_break, GNUNET_NO, GNUNET_OK, GNUNET_SERVICE_client_drop(), GNUNET_snprintf(), hostname, my_domain, ActiveLookup::record_type, resolve_and_cache(), send_end_msg(), and try_cache().

Referenced by handle_get(), and try_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_cache()

static int try_cache ( const char *  hostname,
uint16_t  record_type,
uint32_t  client_request_id,
struct GNUNET_SERVICE_Client client 
)
static

Get an IP address as a string (works for both IPv4 and IPv6).

Note that the resolution happens asynchronously and that the first call may not immediately result in the FQN (but instead in a human-readable IP address).

Parameters
hostnamewhat hostname was to be resolved
record_typewhat type of record was requested
client_request_idunique identification of the client's request
clienthandle to the client making the request (for sending the reply)

Definition at line 599 of file gnunet-service-resolver.c.

603 {
604  struct ResolveCache *pos;
605  struct ResolveCache *next;
606  int found;
607  int in_hosts;
608 
609  in_hosts = GNUNET_NO;
610  for (pos = hosts_head; NULL != pos; pos = pos->next)
611  if (0 == strcmp (pos->hostname, hostname))
612  {
613  in_hosts = GNUNET_YES;
614  break;
615  }
616  if (NULL == pos)
617  {
618  next = cache_head;
619  for (pos = next; NULL != pos; pos = next)
620  {
621  next = pos->next;
622  if (GNUNET_YES == remove_expired (pos))
623  continue;
624  if (0 == strcmp (pos->hostname, hostname))
625  break;
626  }
627  }
628  if (NULL == pos)
629  {
630  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cache entry for '%s'\n", hostname);
631  return GNUNET_NO;
632  }
633  if ((GNUNET_NO == in_hosts) && (cache_head != pos))
634  {
635  /* move result to head to achieve LRU for cache eviction */
638  }
639  found = GNUNET_NO;
640  for (struct RecordListEntry *rle = pos->records_head; NULL != rle;
641  rle = rle->next)
642  {
643  const struct GNUNET_DNSPARSER_Record *record = rle->record;
644 
646  "Checking cache entry for '%s', record is for '%s'\n",
647  hostname,
648  record->name);
649  if ((GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
650  (GNUNET_DNSPARSER_TYPE_CNAME != record_type) && (GNUNET_NO == found))
651  {
652  const char *hostname = record->data.hostname;
653 
654  process_get (hostname, record_type, client_request_id, client);
655  return GNUNET_YES; /* counts as a cache "hit" */
656  }
657  if (0 == strcmp (record->name, hostname))
658  found |= send_reply (rle->record, record_type, client_request_id, client);
659  }
660  if (GNUNET_NO == found)
661  return GNUNET_NO; /* had records, but none matched! */
662  send_end_msg (client_request_id, client);
663  return GNUNET_YES;
664 }
static int send_reply(struct GNUNET_DNSPARSER_Record *record, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
Send DNS record back to our client.
static int remove_expired(struct ResolveCache *rc)
Remove expired entries from rc.
static void process_get(const char *hostname, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
Process DNS request for hostname with request ID request_id from client demanding records of type rec...
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
A DNS response record.
A cached DNS lookup result.
struct ResolveCache * next
This is a doubly linked list.

References cache_head, cache_tail, GNUNET_CONTAINER_DLL_insert, GNUNET_CONTAINER_DLL_remove, GNUNET_DNSPARSER_TYPE_CNAME, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_NO, GNUNET_YES, hostname, ResolveCache::hostname, hosts_head, RecordListEntry::next, ResolveCache::next, process_get(), record(), ResolveCache::records_head, remove_expired(), send_end_msg(), and send_reply().

Referenced by handle_resolve_result(), and process_get().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pack()

static int pack ( const char *  hostname,
uint16_t  type,
uint16_t  dns_id,
char **  packet_buf,
size_t *  packet_size 
)
static

Create DNS query for hostname of type type with DNS request ID dns_id.

Parameters
hostnameDNS name to query
typerequested DNS record type
dns_idwhat should be the DNS request ID
[out]packet_bufwhere to write the request packet
[out]packet_sizeset to size of packet_buf on success
Returns
GNUNET_OK on success

Definition at line 679 of file gnunet-service-resolver.c.

684 {
685  struct GNUNET_DNSPARSER_Query query;
686  struct GNUNET_DNSPARSER_Packet packet;
687 
688  query.name = (char *) hostname;
689  query.type = type;
690  query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
691  memset (&packet, 0, sizeof(packet));
692  packet.num_queries = 1;
693  packet.queries = &query;
694  packet.id = htons (dns_id);
695  packet.flags.recursion_desired = 1;
696  if (GNUNET_OK !=
697  GNUNET_DNSPARSER_pack (&packet, UINT16_MAX, packet_buf, packet_size))
698  {
700  "Failed to pack query for hostname `%s'\n",
701  hostname);
702  packet_buf = NULL;
703  return GNUNET_SYSERR;
704  }
705  return GNUNET_OK;
706 }
int GNUNET_DNSPARSER_pack(const struct GNUNET_DNSPARSER_Packet *p, uint16_t max, char **buf, size_t *buf_length)
Given a DNS packet p, generate the corresponding UDP payload.
Definition: dnsparser.c:1259
@ GNUNET_SYSERR
#define GNUNET_TUN_DNS_CLASS_INTERNET
A few common DNS classes (ok, only one is common, but I list a couple more to make it clear what we'r...
Easy-to-process, parsed version of a DNS packet.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

References GNUNET_DNSPARSER_Query::dns_traffic_class, GNUNET_DNSPARSER_Packet::flags, GNUNET_DNSPARSER_pack(), GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, GNUNET_SYSERR, GNUNET_TUN_DNS_CLASS_INTERNET, hostname, GNUNET_DNSPARSER_Packet::id, GNUNET_DNSPARSER_Query::name, GNUNET_DNSPARSER_Packet::num_queries, GNUNET_DNSPARSER_Packet::queries, GNUNET_TUN_DnsFlags::recursion_desired, GNUNET_DNSPARSER_Query::type, and type.

Referenced by handle_resolve_result(), and resolve_and_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cache_answers()

static void cache_answers ( const char *  name,
struct GNUNET_DNSPARSER_Record records,
unsigned int  num_records 
)
static

Definition at line 710 of file gnunet-service-resolver.c.

713 {
714  struct ResolveCache *rc;
716  struct RecordListEntry *rle;
717 
718  for (unsigned int i = 0; i != num_records; i++)
719  {
720  record = &records[i];
721 
722  for (rc = cache_head; NULL != rc; rc = rc->next)
723  if (0 == strcasecmp (rc->hostname, name))
724  break;
725  if (NULL == rc)
726  {
727  rc = GNUNET_new (struct ResolveCache);
728  rc->hostname = GNUNET_strdup (name);
730  "Caching record for name %s under %s\n",
731  record->name, name);
733  cache_size++;
734  }
735  /* TODO: ought to check first if we have this exact record
736  already in the cache! */
737  rle = GNUNET_new (struct RecordListEntry);
740  }
741 }
static unsigned int records
Number of records we found.
struct GNUNET_DNSPARSER_Record * GNUNET_DNSPARSER_duplicate_record(const struct GNUNET_DNSPARSER_Record *r)
Duplicate (deep-copy) the given DNS record.
Definition: dnsparser.c:737
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
const char * name

References cache_head, cache_size, cache_tail, GNUNET_CONTAINER_DLL_insert, GNUNET_DNSPARSER_duplicate_record(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_new, GNUNET_strdup, ResolveCache::hostname, name, ResolveCache::next, record(), RecordListEntry::record, records, ResolveCache::records_head, and ResolveCache::records_tail.

Referenced by handle_resolve_result().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_resolve_result()

static void handle_resolve_result ( void *  cls,
const struct GNUNET_TUN_DnsHeader dns,
size_t  dns_len 
)
static

We got a result from DNS.

Add it to the cache and see if we can make our client happy...

Parameters
clsthe struct ActiveLookup
dnsthe DNS response
dns_lennumber of bytes in dns

Definition at line 753 of file gnunet-service-resolver.c.

756 {
757  struct ActiveLookup *al = cls;
758  struct GNUNET_DNSPARSER_Packet *parsed;
759 
760  parsed = GNUNET_DNSPARSER_parse ((const char *) dns, dns_len);
761  if (NULL == parsed)
762  {
764  "Failed to parse DNS reply (hostname %s, request ID %u)\n",
765  al->hostname,
766  al->dns_id);
767  return;
768  }
769  if (al->dns_id != ntohs (parsed->id))
770  {
772  "Request ID in DNS reply does not match\n");
774  return;
775  }
776  if (0 == parsed->num_answers + parsed->num_authority_records
777  + parsed->num_additional_records)
778  {
780  "DNS reply (hostname %s, request ID %u) contains no answers\n",
781  al->hostname,
782  (unsigned int) al->client_request_id);
783  /* resume by trying again from cache */
784  if (GNUNET_NO == try_cache (al->hostname,
785  al->record_type,
786  al->client_request_id,
787  al->client))
788  /* cache failed, tell client we could not get an answer */
789  {
791  }
793  free_active_lookup (al);
794  return;
795  }
796  /* LRU-based cache eviction: we remove from tail */
797  while (cache_size > MAX_CACHE)
799 
801  "Got reply for hostname %s and request ID %u\n",
802  al->hostname,
803  (unsigned int) al->client_request_id);
804  /* add to cache */
805  cache_answers (al->hostname, parsed->answers, parsed->num_answers);
806  cache_answers (al->hostname,
807  parsed->authority_records,
808  parsed->num_authority_records);
809  cache_answers (al->hostname,
810  parsed->additional_records,
811  parsed->num_additional_records);
812 
813  /* see if we need to do the 2nd request for AAAA records */
814  if ((GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
815  (GNUNET_NO == al->did_aaaa))
816  {
817  char *packet_buf;
818  size_t packet_size;
819  uint16_t dns_id;
820 
822  UINT16_MAX);
823  if (GNUNET_OK == pack (al->hostname,
825  dns_id,
826  &packet_buf,
827  &packet_size))
828  {
829  al->did_aaaa = GNUNET_YES;
830  al->dns_id = dns_id;
833  packet_buf,
834  packet_size,
836  al);
837  GNUNET_free (packet_buf);
839  return;
840  }
841  }
842 
843  /* resume by trying again from cache */
844  if (GNUNET_NO == try_cache (al->hostname,
845  al->record_type,
846  al->client_request_id,
847  al->client))
848  /* cache failed, tell client we could not get an answer */
849  {
851  }
852  free_active_lookup (al);
854 }
#define MAX_CACHE
Maximum number of hostnames we cache results for.
static void cache_answers(const char *name, struct GNUNET_DNSPARSER_Record *records, unsigned int num_records)
static int pack(const char *hostname, uint16_t type, uint16_t dns_id, char **packet_buf, size_t *packet_size)
Create DNS query for hostname of type type with DNS request ID dns_id.
static void free_active_lookup(struct ActiveLookup *al)
Release resources associated with al.
static void handle_resolve_result(void *cls, const struct GNUNET_TUN_DnsHeader *dns, size_t dns_len)
We got a result from DNS.
static struct GNUNET_DNSSTUB_Context * dnsstub_ctx
context of dnsstub library
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
@ GNUNET_CRYPTO_QUALITY_NONCE
Randomness for IVs etc.
void GNUNET_DNSPARSER_free_packet(struct GNUNET_DNSPARSER_Packet *p)
Free memory taken by a packet.
Definition: dnsparser.c:854
struct GNUNET_DNSPARSER_Packet * GNUNET_DNSPARSER_parse(const char *udp_payload, size_t udp_payload_length)
Parse a UDP payload of a DNS packet in to a nice struct for further processing and manipulation.
Definition: dnsparser.c:656
struct GNUNET_DNSSTUB_RequestSocket * GNUNET_DNSSTUB_resolve(struct GNUNET_DNSSTUB_Context *ctx, const void *request, size_t request_len, GNUNET_DNSSTUB_ResultCallback rc, void *rc_cls)
Perform DNS resolution using our default IP from init.
Definition: dnsstub.c:526
Information about pending lookups.
struct GNUNET_SERVICE_Client * client
The client that queried the records contained in this cache entry.
uint16_t record_type
type of queried DNS record
uint16_t dns_id
Unique DNS request ID of a client if a query for this hostname/record_type is currently pending,...
uint32_t client_request_id
Unique request ID of a client if a query for this hostname/record_type is currently pending,...
int did_aaaa
If record_type is GNUNET_DNSPARSER_TYPE_ALL, did we go again for the AAAA records yet?
unsigned int num_answers
Number of answers in the packet, should be 0 for queries.
struct GNUNET_DNSPARSER_Record * answers
Array of all answers in the packet, must contain "num_answers" entries.
unsigned int num_additional_records
Number of additional records in the packet, should be 0 for queries.
struct GNUNET_DNSPARSER_Record * additional_records
Array of all additional answers in the packet, must contain "num_additional_records" entries.
struct GNUNET_DNSPARSER_Record * authority_records
Array of all authority records in the packet, must contain "num_authority_records" entries.
unsigned int num_authority_records
Number of authoritative answers in the packet, should be 0 for queries.
uint16_t id
DNS ID (to match replies to requests).

References GNUNET_DNSPARSER_Packet::additional_records, GNUNET_DNSPARSER_Packet::answers, GNUNET_DNSPARSER_Packet::authority_records, cache_answers(), cache_size, cache_tail, ActiveLookup::client, ActiveLookup::client_request_id, ActiveLookup::did_aaaa, ActiveLookup::dns_id, dnsstub_ctx, free_active_lookup(), free_cache_entry(), GNUNET_CRYPTO_QUALITY_NONCE, GNUNET_CRYPTO_random_u32(), GNUNET_DNSPARSER_free_packet(), GNUNET_DNSPARSER_parse(), GNUNET_DNSPARSER_TYPE_AAAA, GNUNET_DNSPARSER_TYPE_ALL, GNUNET_DNSSTUB_resolve(), GNUNET_DNSSTUB_resolve_cancel(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_YES, ActiveLookup::hostname, GNUNET_DNSPARSER_Packet::id, MAX_CACHE, GNUNET_DNSPARSER_Packet::num_additional_records, GNUNET_DNSPARSER_Packet::num_answers, GNUNET_DNSPARSER_Packet::num_authority_records, pack(), ActiveLookup::record_type, ActiveLookup::resolve_handle, send_end_msg(), and try_cache().

Referenced by resolve_and_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_resolve_timeout()

static void handle_resolve_timeout ( void *  cls)
static

We encountered a timeout trying to perform a DNS lookup.

Parameters
clsa struct ActiveLookup

Definition at line 864 of file gnunet-service-resolver.c.

865 {
866  struct ActiveLookup *al = cls;
867 
868  al->timeout_task = NULL;
869  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DNS lookup timeout!\n");
871  free_active_lookup (al);
872 }

References ActiveLookup::client, ActiveLookup::client_request_id, free_active_lookup(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, send_end_msg(), and ActiveLookup::timeout_task.

Referenced by resolve_and_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resolve_and_cache()

static int resolve_and_cache ( const char *  hostname,
uint16_t  record_type,
uint32_t  client_request_id,
struct GNUNET_SERVICE_Client client 
)
static

Initiate an active lookup, then cache the result and try to then complete the resolution.

Parameters
hostnameDNS name to resolve
record_typerecord type to locate
client_request_idclient request ID
clienthandle to the client
Returns
GNUNET_OK if the DNS query is now pending

Definition at line 886 of file gnunet-service-resolver.c.

890 {
891  char *packet_buf;
892  size_t packet_size;
893  struct ActiveLookup *al;
894  uint16_t dns_id;
895  uint16_t type;
896 
897  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "resolve_and_cache `%s'\n", hostname);
899  UINT16_MAX);
900 
903  else
904  type = record_type;
905  if (GNUNET_OK != pack (hostname, type, dns_id, &packet_buf, &packet_size))
906  {
908  "Failed to pack query for hostname `%s'\n",
909  hostname);
910  return GNUNET_SYSERR;
911  }
912 
913  al = GNUNET_new (struct ActiveLookup);
915  al->record_type = record_type;
917  al->dns_id = dns_id;
918  al->client = client;
919  al->timeout_task =
922  packet_buf,
923  packet_size,
925  al);
926  GNUNET_free (packet_buf);
929  "Resolving %s, client_request_id = %u, dns_id = %u\n",
930  hostname,
931  (unsigned int) client_request_id,
932  (unsigned int) dns_id);
933  return GNUNET_OK;
934 }
#define DNS_TIMEOUT
How long do we wait for DNS answers?
static void handle_resolve_timeout(void *cls)
We encountered a timeout trying to perform a DNS lookup.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1272

References ActiveLookup::client, ActiveLookup::client_request_id, ActiveLookup::dns_id, DNS_TIMEOUT, dnsstub_ctx, GNUNET_CONTAINER_DLL_insert, GNUNET_CRYPTO_QUALITY_NONCE, GNUNET_CRYPTO_random_u32(), GNUNET_DNSPARSER_TYPE_A, GNUNET_DNSPARSER_TYPE_ALL, GNUNET_DNSSTUB_resolve(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_new, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), GNUNET_strdup, GNUNET_SYSERR, handle_resolve_result(), handle_resolve_timeout(), hostname, ActiveLookup::hostname, lookup_head, lookup_tail, pack(), ActiveLookup::record_type, ActiveLookup::resolve_handle, ActiveLookup::timeout_task, and type.

Referenced by process_get().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_get()

static int check_get ( void *  cls,
const struct GNUNET_RESOLVER_GetMessage get 
)
static

Verify well-formedness of GET-message.

Parameters
clsclosure, unused
getthe actual message
Returns
GNUNET_OK if get is well-formed

Definition at line 990 of file gnunet-service-resolver.c.

991 {
992  uint16_t size;
993  int direction;
994  int af;
995 
996  (void) cls;
997  size = ntohs (get->header.size) - sizeof(*get);
998  direction = ntohl (get->direction);
999  if (GNUNET_NO == direction)
1000  {
1002  return GNUNET_OK;
1003  }
1004  af = ntohl (get->af);
1005  switch (af)
1006  {
1007  case AF_INET:
1008  if (size != sizeof(struct in_addr))
1009  {
1010  GNUNET_break (0);
1011  return GNUNET_SYSERR;
1012  }
1013  break;
1014 
1015  case AF_INET6:
1016  if (size != sizeof(struct in6_addr))
1017  {
1018  GNUNET_break (0);
1019  return GNUNET_SYSERR;
1020  }
1021  break;
1022 
1023  default:
1024  GNUNET_break (0);
1025  return GNUNET_SYSERR;
1026  }
1027  return GNUNET_OK;
1028 }
enum GNUNET_ABD_AlgoDirectionFlags direction
API enum, filled and passed for collect/verify.
Definition: gnunet-abd.c:172
static int get
Get DID Documement for DID Flag.
Definition: gnunet-did.c:66
#define GNUNET_MQ_check_zero_termination(m)
Insert code for a "check_" function that verifies that a given variable-length message received over ...
static unsigned int size
Size of the "table".
Definition: peer.c:68

References direction, get, GNUNET_break, GNUNET_MQ_check_zero_termination, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, and size.

◆ handle_get()

static void handle_get ( void *  cls,
const struct GNUNET_RESOLVER_GetMessage msg 
)
static

Handle GET-message.

Parameters
clsidentification of the client
msgthe actual message

Definition at line 1038 of file gnunet-service-resolver.c.

1039 {
1040  struct GNUNET_SERVICE_Client *client = cls;
1041  int direction;
1042  int af;
1043  uint32_t client_request_id;
1044  char *hostname;
1045 
1046  direction = ntohl (msg->direction);
1047  af = ntohl (msg->af);
1048  client_request_id = msg->client_id;
1050  if (GNUNET_NO == direction)
1051  {
1052  /* IP from hostname */
1053  hostname = GNUNET_strdup ((const char *) &msg[1]);
1055  "Client asks to resolve `%s'\n",
1056  hostname);
1057  switch (af)
1058  {
1059  case AF_UNSPEC: {
1062  client_request_id,
1063  client);
1064  break;
1065  }
1066 
1067  case AF_INET: {
1070  client_request_id,
1071  client);
1072  break;
1073  }
1074 
1075  case AF_INET6: {
1078  client_request_id,
1079  client);
1080  break;
1081  }
1082 
1083  default: {
1084  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got invalid af: %d\n", af);
1085  GNUNET_assert (0);
1086  }
1087  }
1088  }
1089  else
1090  {
1091  /* hostname from IP */
1092  hostname = make_reverse_hostname (&msg[1], af);
1095  client_request_id,
1096  client);
1097  }
1099 }
static char * make_reverse_hostname(const void *ip, int af)
Compute name to use for DNS reverse lookups from ip.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2249
Handle to a client that is connected to a service.
Definition: service.c:252

References direction, GNUNET_assert, GNUNET_DNSPARSER_TYPE_A, GNUNET_DNSPARSER_TYPE_AAAA, GNUNET_DNSPARSER_TYPE_ALL, GNUNET_DNSPARSER_TYPE_PTR, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_NO, GNUNET_SERVICE_client_continue(), GNUNET_strdup, hostname, make_reverse_hostname(), msg, and process_get().

Here is the call graph for this function:

◆ shutdown_task()

static void shutdown_task ( void *  cls)
static

Service is shutting down, clean up.

Parameters
clsNULL, unused

Definition at line 1108 of file gnunet-service-resolver.c.

1109 {
1110  (void) cls;
1111 
1112  while (NULL != lookup_head)
1114  while (NULL != cache_head)
1116  while (NULL != hosts_head)
1120 }
static void free_hosts_entry(struct ResolveCache *rc)
Remove entry from cache.
void GNUNET_DNSSTUB_stop(struct GNUNET_DNSSTUB_Context *ctx)
Cleanup DNSSTUB resolver.
Definition: dnsstub.c:705

References cache_head, dnsstub_ctx, free_active_lookup(), free_cache_entry(), free_hosts_entry(), GNUNET_DNSSTUB_stop(), GNUNET_free, hosts_head, lookup_head, and my_domain.

Referenced by init_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_host()

static void add_host ( const char *  hostname,
uint16_t  rec_type,
const void *  data,
size_t  data_size 
)
static

Add information about a host from /etc/hosts to our cache.

Parameters
hostnamethe name of the host
rec_typeDNS record type to use
datapayload
data_sizenumber of bytes in data

Definition at line 1133 of file gnunet-service-resolver.c.

1137 {
1138  struct ResolveCache *rc;
1139  struct RecordListEntry *rle;
1140  struct GNUNET_DNSPARSER_Record *rec;
1141 
1142  rec = GNUNET_malloc (sizeof(struct GNUNET_DNSPARSER_Record));
1144  rec->type = rec_type;
1146  rec->name = GNUNET_strdup (hostname);
1148  rec->data.raw.data_len = data_size;
1149  rle = GNUNET_new (struct RecordListEntry);
1150  rle->record = rec;
1151  rc = GNUNET_new (struct ResolveCache);
1155 }
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
uint32_t data
The data value.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
void * data
Binary record data.
size_t data_len
Number of bytes in data.
uint16_t dns_traffic_class
See GNUNET_TUN_DNS_CLASS_*.
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
struct GNUNET_TIME_Absolute expiration_time
When does the record expire?
union GNUNET_DNSPARSER_Record::@24 data
Payload of the record (which one of these is valid depends on the 'type').
char * name
Name of the record that the query is for (0-terminated).
struct GNUNET_DNSPARSER_RawRecord raw
Raw data for all other types.

References GNUNET_DNSPARSER_RawRecord::data, GNUNET_DNSPARSER_Record::data, data, GNUNET_DNSPARSER_RawRecord::data_len, data_size, GNUNET_DNSPARSER_Record::dns_traffic_class, GNUNET_DNSPARSER_Record::expiration_time, GNUNET_CONTAINER_DLL_insert, GNUNET_malloc, GNUNET_memdup, GNUNET_new, GNUNET_strdup, GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TUN_DNS_CLASS_INTERNET, hostname, ResolveCache::hostname, hosts_head, hosts_tail, GNUNET_DNSPARSER_Record::name, GNUNET_DNSPARSER_Record::raw, RecordListEntry::record, ResolveCache::records_head, ResolveCache::records_tail, and GNUNET_DNSPARSER_Record::type.

Referenced by extract_hosts().

Here is the caller graph for this function:

◆ extract_hosts()

static void extract_hosts ( const char *  line,
size_t  line_len 
)
static

Extract host information from a line in /etc/hosts.

Parameters
linethe line to parse
line_lennumber of bytes in line

Definition at line 1165 of file gnunet-service-resolver.c.

1166 {
1167  const char *c;
1168  struct in_addr v4;
1169  struct in6_addr v6;
1170  char *tbuf;
1171  char *tok;
1172 
1173  /* ignore everything after '#' */
1174  c = memrchr (line, (unsigned char) '#', line_len);
1175  if (NULL != c)
1176  line_len = c - line;
1177  /* ignore leading whitespace */
1178  while ((0 < line_len) && isspace ((unsigned char) *line))
1179  {
1180  line++;
1181  line_len--;
1182  }
1183  tbuf = GNUNET_strndup (line, line_len);
1184  tok = strtok (tbuf, " \t");
1185  if (NULL == tok)
1186  {
1187  GNUNET_free (tbuf);
1188  return;
1189  }
1190  if (1 == inet_pton (AF_INET, tok, &v4))
1191  {
1192  while (NULL != (tok = strtok (NULL, " \t")))
1193  add_host (tok, GNUNET_DNSPARSER_TYPE_A, &v4, sizeof(struct in_addr));
1194  }
1195  else if (1 == inet_pton (AF_INET6, tok, &v6))
1196  {
1197  while (NULL != (tok = strtok (NULL, " \t")))
1198  add_host (tok, GNUNET_DNSPARSER_TYPE_AAAA, &v6, sizeof(struct in6_addr));
1199  }
1200  GNUNET_free (tbuf);
1201 }
#define memrchr(s, c, n)
Definition: compat.h:49
static void add_host(const char *hostname, uint16_t rec_type, const void *data, size_t data_size)
Add information about a host from /etc/hosts to our cache.

References add_host(), GNUNET_DNSPARSER_TYPE_A, GNUNET_DNSPARSER_TYPE_AAAA, GNUNET_free, GNUNET_strndup, line, and memrchr.

Referenced by load_etc_hosts().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_etc_hosts()

static void load_etc_hosts ( void  )
static

Reads the list of hosts from /etc/hosts.

Definition at line 1208 of file gnunet-service-resolver.c.

1209 {
1210  struct GNUNET_DISK_FileHandle *fh;
1211  struct GNUNET_DISK_MapHandle *mh;
1212  off_t bytes_read;
1213  const char *buf;
1214  size_t read_offset;
1215 
1216  fh = GNUNET_DISK_file_open ("/etc/hosts",
1219  if (NULL == fh)
1220  {
1221  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to open /etc/hosts");
1222  return;
1223  }
1224  if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read))
1225  {
1227  "Could not determine size of /etc/hosts. "
1228  "DNS resolution will not be possible.\n");
1230  return;
1231  }
1232  if (((unsigned long long) bytes_read) > (unsigned long long) SIZE_MAX)
1233  {
1235  "/etc/hosts file too large to mmap. "
1236  "DNS resolution will not be possible.\n");
1238  return;
1239  }
1241  &mh,
1243  (size_t) bytes_read);
1244  read_offset = 0;
1245  while (read_offset < (size_t) bytes_read)
1246  {
1247  const char *newline;
1248  size_t line_len;
1249 
1250  newline = strchr (buf + read_offset, '\n');
1251  if (NULL == newline)
1252  break;
1253  line_len = newline - buf - read_offset;
1254  extract_hosts (buf + read_offset, line_len);
1255  read_offset += line_len + 1;
1256  }
1259 }
static void extract_hosts(const char *line, size_t line_len)
Extract host information from a line in /etc/hosts.
@ GNUNET_ERROR_TYPE_INFO

References buf, extract_hosts(), fh, GNUNET_DISK_file_close(), GNUNET_DISK_file_handle_size(), GNUNET_DISK_file_map(), GNUNET_DISK_file_open(), GNUNET_DISK_file_unmap(), GNUNET_DISK_MAP_TYPE_READ, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_OK, mh, and SIZE_MAX.

Referenced by init_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_cb()

static void init_cb ( void *  cls,
const struct GNUNET_CONFIGURATION_Handle cfg,
struct GNUNET_SERVICE_Handle sh 
)
static

Service is starting, initialize everything.

Parameters
clsNULL, unused
cfgour configuration
shservice handle

Definition at line 1270 of file gnunet-service-resolver.c.

1273 {
1274  char **dns_servers;
1275  int num_dns_servers;
1276 
1277  (void) cfg;
1278  (void) sh;
1279  load_etc_hosts ();
1282  dns_servers = NULL;
1283  num_dns_servers = lookup_dns_servers (&dns_servers);
1284  if (0 >= num_dns_servers)
1285  {
1286  GNUNET_log (
1288  _ ("No DNS server available. DNS resolution will not be possible.\n"));
1289  return;
1290  }
1291  for (int i = 0; i < num_dns_servers; i++)
1292  {
1293  int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1295  "Adding DNS server '%s': %s\n",
1296  dns_servers[i],
1297  GNUNET_OK == result ? "success" : "failure");
1298  GNUNET_free (dns_servers[i]);
1299  }
1300  GNUNET_free (dns_servers);
1301 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct SolverHandle * sh
static int result
Global testing status.
static void shutdown_task(void *cls)
Service is shutting down, clean up.
static void load_etc_hosts(void)
Reads the list of hosts from /etc/hosts.
static int lookup_dns_servers(char ***server_addrs)
Reads the list of nameservers from /etc/resolve.conf.
int GNUNET_DNSSTUB_add_dns_ip(struct GNUNET_DNSSTUB_Context *ctx, const char *dns_ip)
Add nameserver for use by the DNSSTUB.
Definition: dnsstub.c:613
struct GNUNET_DNSSTUB_Context * GNUNET_DNSSTUB_start(unsigned int num_sockets)
Start a DNS stub resolver.
Definition: dnsstub.c:586
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1334
#define _(String)
GNU gettext support macro.
Definition: platform.h:178

References _, cfg, dnsstub_ctx, GNUNET_DNSSTUB_add_dns_ip(), GNUNET_DNSSTUB_start(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_SCHEDULER_add_shutdown(), load_etc_hosts(), lookup_dns_servers(), result, sh, and shutdown_task().

Here is the call graph for this function:

◆ connect_cb()

static void* connect_cb ( void *  cls,
struct GNUNET_SERVICE_Client c,
struct GNUNET_MQ_Handle mq 
)
static

Callback called when a client connects to the service.

Parameters
clsclosure for the service, unused
cthe new client that connected to the service
mqthe message queue used to send messages to the client
Returns
c

Definition at line 1313 of file gnunet-service-resolver.c.

1316 {
1317  (void) cls;
1318  (void) mq;
1319 
1320  return c;
1321 }
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5

References mq.

◆ disconnect_cb()

static void disconnect_cb ( void *  cls,
struct GNUNET_SERVICE_Client c,
void *  internal_cls 
)
static

Callback called when a client disconnected from the service.

Parameters
clsclosure for the service
cthe client that disconnected
internal_clsshould be equal to c

Definition at line 1332 of file gnunet-service-resolver.c.

1333 {
1334  struct ActiveLookup *n;
1335 
1336  (void) cls;
1337 
1338  GNUNET_assert (c == internal_cls);
1339  n = lookup_head;
1340  for (struct ActiveLookup *al = n; NULL != al; al = n)
1341  {
1342  n = al->next;
1343  if (al->client == c)
1344  free_active_lookup (al);
1345  }
1346 }
struct ActiveLookup * next
Stored in a DLL.

References free_active_lookup(), GNUNET_assert, lookup_head, and ActiveLookup::next.

Here is the call graph for this function:

◆ GNUNET_SERVICE_MAIN()

GNUNET_SERVICE_MAIN ( "resolver"  ,
GNUNET_SERVICE_OPTION_NONE  ,
init_cb,
connect_cb,
disconnect_cb,
NULL  ,
GNUNET_MQ_hd_var_size(get, GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST, struct GNUNET_RESOLVER_GetMessage, NULL)  ,
GNUNET_MQ_handler_end()   
)

Define "main" method using service macro.

Variable Documentation

◆ cache_head

struct ResolveCache* cache_head
static

Start of the linked list of cached DNS lookup results.

Definition at line 161 of file gnunet-service-resolver.c.

Referenced by cache_answers(), free_cache_entry(), shutdown_task(), and try_cache().

◆ cache_tail

struct ResolveCache* cache_tail
static

Tail of the linked list of cached DNS lookup results.

Definition at line 166 of file gnunet-service-resolver.c.

Referenced by cache_answers(), free_cache_entry(), handle_resolve_result(), and try_cache().

◆ hosts_head

struct ResolveCache* hosts_head
static

Head of the linked list of DNS lookup results from /etc/hosts.

Definition at line 171 of file gnunet-service-resolver.c.

Referenced by add_host(), free_hosts_entry(), shutdown_task(), and try_cache().

◆ hosts_tail

struct ResolveCache* hosts_tail
static

Tail of the linked list of DNS lookup results from /etc/hosts.

Definition at line 176 of file gnunet-service-resolver.c.

Referenced by add_host(), and free_hosts_entry().

◆ lookup_head

struct ActiveLookup* lookup_head
static

Start of the linked list of active DNS lookups.

Definition at line 181 of file gnunet-service-resolver.c.

Referenced by disconnect_cb(), free_active_lookup(), resolve_and_cache(), and shutdown_task().

◆ lookup_tail

struct ActiveLookup* lookup_tail
static

Tail of the linked list of active DNS lookups.

Definition at line 186 of file gnunet-service-resolver.c.

Referenced by free_active_lookup(), and resolve_and_cache().

◆ dnsstub_ctx

struct GNUNET_DNSSTUB_Context* dnsstub_ctx
static

context of dnsstub library

Definition at line 191 of file gnunet-service-resolver.c.

Referenced by handle_resolve_result(), init_cb(), resolve_and_cache(), and shutdown_task().

◆ my_domain

char* my_domain
static

My domain, to be appended to the hostname to get a FQDN.

Definition at line 196 of file gnunet-service-resolver.c.

Referenced by lookup_dns_servers(), process_get(), and shutdown_task().

◆ cache_size

unsigned int cache_size
static

How many entries do we have in cache_head DLL?

Definition at line 201 of file gnunet-service-resolver.c.

Referenced by cache_answers(), free_cache_entry(), free_hosts_entry(), and handle_resolve_result().