GNUnet  0.10.x
Data Structures | Macros | Functions | Variables
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 36 of file gnunet-service-resolver.c.

Referenced by resolve_and_cache().

◆ MAX_CACHE

#define MAX_CACHE   1024

Maximum number of hostnames we cache results for.

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

Referenced by handle_resolve_result().

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 209 of file gnunet-service-resolver.c.

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

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

210 {
211  struct RecordListEntry *pos;
212 
213  while (NULL != (pos = rc->records_head))
214  {
217  GNUNET_free (pos->record);
218  GNUNET_free (pos);
219  }
222  cache_size--;
223  GNUNET_free (rc);
224 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static unsigned int cache_size
How many entries do we have in cache_head DLL?
struct RecordListEntry * records_head
head of a double linked list containing the lookup results
void GNUNET_DNSPARSER_free_record(struct GNUNET_DNSPARSER_Record *r)
Free the given DNS record.
Definition: dnsparser.c:180
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
struct RecordListEntry * records_tail
tail of a double linked list containing the lookup results
struct GNUNET_DNSPARSER_Record * record
Cached data.
static struct ResolveCache * cache_tail
Tail of the linked list of cached DNS lookup results.
static struct ResolveCache * cache_head
Start of the linked list of cached DNS lookup results.
char * hostname
Which hostname is this cache for?
Entry in list of cached DNS records for a hostname.
#define GNUNET_free(ptr)
Wrapper around free.
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 233 of file gnunet-service-resolver.c.

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

Referenced by shutdown_task().

234 {
235  struct RecordListEntry *pos;
236 
237  while (NULL != (pos = rc->records_head))
238  {
241  GNUNET_free (pos->record);
242  GNUNET_free (pos);
243  }
246  cache_size--;
247  GNUNET_free (rc);
248 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static unsigned int cache_size
How many entries do we have in cache_head DLL?
static struct ResolveCache * hosts_tail
Tail of the linked list of DNS lookup results from /etc/hosts.
struct RecordListEntry * records_head
head of a double linked list containing the lookup results
void GNUNET_DNSPARSER_free_record(struct GNUNET_DNSPARSER_Record *r)
Free the given DNS record.
Definition: dnsparser.c:180
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
struct RecordListEntry * records_tail
tail of a double linked list containing the lookup results
struct GNUNET_DNSPARSER_Record * record
Cached data.
static struct ResolveCache * hosts_head
Head of the linked list of DNS lookup results from /etc/hosts.
char * hostname
Which hostname is this cache for?
Entry in list of cached DNS records for a hostname.
#define GNUNET_free(ptr)
Wrapper around free.
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 257 of file gnunet-service-resolver.c.

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

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

258 {
260  if (NULL != al->resolve_handle)
261  {
263  al->resolve_handle = NULL;
264  }
265  if (NULL != al->timeout_task)
266  {
268  al->timeout_task = NULL;
269  }
271  GNUNET_free (al);
272 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct GNUNET_SCHEDULER_Task * timeout_task
handle for the resolution timeout task
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
static struct ActiveLookup * lookup_tail
Tail of the linked list of active DNS lookups.
static struct ActiveLookup * lookup_head
Start of the linked list of active DNS lookups.
void GNUNET_DNSSTUB_resolve_cancel(struct GNUNET_DNSSTUB_RequestSocket *rs)
Cancel DNS resolution.
Definition: dnsstub.c:571
struct GNUNET_DNSSTUB_RequestSocket * resolve_handle
handle for cancelling a request
char * hostname
Which hostname are we resolving?
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965
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 285 of file gnunet-service-resolver.c.

References GNUNET_strndup.

Referenced by lookup_dns_servers().

286 {
287  if (0 == strncmp (line, "nameserver ", strlen ("nameserver ")))
288  return GNUNET_strndup (line + strlen ("nameserver "),
289  line_len - strlen ("nameserver "));
290  return NULL;
291 }
static char * line
Desired phone line (string to be converted to a hash).
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
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 304 of file gnunet-service-resolver.c.

References GNUNET_strndup.

Referenced by lookup_dns_servers().

305 {
306  if (0 == strncmp (line, "search ", strlen ("search ")))
307  return GNUNET_strndup (line + strlen ("search "),
308  line_len - strlen ("search "));
309  return NULL;
310 }
static char * line
Desired phone line (string to be converted to a hash).
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
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
server_addrs[out]a list of null-terminated server address strings
Returns
the number of server addresses in , -1 on error

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

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().

321 {
322  struct GNUNET_DISK_FileHandle *fh;
323  struct GNUNET_DISK_MapHandle *mh;
324  off_t bytes_read;
325  const char *buf;
326  size_t read_offset;
327  unsigned int num_dns_servers;
328 
329  fh = GNUNET_DISK_file_open ("/etc/resolv.conf",
332  if (NULL == fh)
333  {
335  "Could not open /etc/resolv.conf. "
336  "DNS resolution will not be possible.\n");
337  return -1;
338  }
339  if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read))
340  {
342  "Could not determine size of /etc/resolv.conf. "
343  "DNS resolution will not be possible.\n");
345  return -1;
346  }
347  if (((unsigned long long) bytes_read) > (unsigned long long) SIZE_MAX)
348  {
350  "/etc/resolv.conf file too large to mmap. "
351  "DNS resolution will not be possible.\n");
353  return -1;
354  }
355  buf = GNUNET_DISK_file_map (fh,
356  &mh,
358  (size_t) bytes_read);
359  *server_addrs = NULL;
360  read_offset = 0;
361  num_dns_servers = 0;
362  while (read_offset < (size_t) bytes_read)
363  {
364  const char *newline;
365  size_t line_len;
366  char *dns_server;
367 
368  newline = strchr (buf + read_offset, '\n');
369  if (NULL == newline)
370  break;
371  line_len = newline - buf - read_offset;
372  dns_server = extract_dns_server (buf + read_offset, line_len);
373  if (NULL != dns_server)
374  {
375  GNUNET_array_append (*server_addrs, num_dns_servers, dns_server);
376  }
377  else if (NULL == my_domain)
378  {
379  my_domain = extract_search_domain (buf + read_offset, line_len);
380  }
381  read_offset += line_len + 1;
382  }
385  return (int) num_dns_servers;
386 }
Open the file for reading.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1817
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
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 "...
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:2020
Nobody is allowed to do anything to the file.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define SIZE_MAX
Definition: platform.h:250
Read-only memory map.
static char buf[2048]
static int fh
Handle to the unique file.
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&#39;s search domain.
int GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
Unmap a file.
Definition: disk.c:2100
#define GNUNET_array_append(arr, size, element)
Append an element to a list (growing the list by one).
int GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
Definition: disk.c:208
#define GNUNET_log(kind,...)
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:1673
Handle used to access files (and pipes).
Handle for a memory-mapping operation.
Definition: disk.c:1985
static char * my_domain
My domain, to be appended to the hostname to get a FQDN.
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 396 of file gnunet-service-resolver.c.

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

Referenced by handle_get().

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

References GNUNET_RESOLVER_ResponseMessage::client_id, GNUNET_DNSPARSER_RawRecord::data, GNUNET_DNSPARSER_Record::data, GNUNET_DNSPARSER_RawRecord::data_len, 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, GNUNET_DNSPARSER_Record::hostname, msg, payload, GNUNET_DNSPARSER_Record::raw, and GNUNET_DNSPARSER_Record::type.

Referenced by try_cache().

463 {
465  struct GNUNET_MQ_Envelope *env;
466  const void *payload;
467  size_t payload_len;
468 
469  switch (record->type)
470  {
472  if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
473  return GNUNET_NO;
474  payload = record->data.hostname;
475  payload_len = strlen (record->data.hostname) + 1;
476  break;
478  if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
479  return GNUNET_NO;
480  payload = record->data.hostname;
481  payload_len = strlen (record->data.hostname) + 1;
482  break;
484  if ((GNUNET_DNSPARSER_TYPE_A != record_type) &&
485  (GNUNET_DNSPARSER_TYPE_ALL != record_type))
486  return GNUNET_NO;
487  payload = record->data.raw.data;
488  payload_len = record->data.raw.data_len;
489  break;
491  if ((GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
492  (GNUNET_DNSPARSER_TYPE_ALL != record_type))
493  return GNUNET_NO;
494  payload = record->data.raw.data;
495  payload_len = record->data.raw.data_len;
496  break;
497  default:
499  "Cannot handle DNS response type %u: not supported here\n",
500  record->type);
501  return GNUNET_NO;
502  }
503  env = GNUNET_MQ_msg_extra (msg,
504  payload_len,
506  msg->client_id = client_request_id;
507  GNUNET_memcpy (&msg[1], payload, payload_len);
509  return GNUNET_YES;
510 }
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
#define GNUNET_DNSPARSER_TYPE_CNAME
struct GNUNET_MQ_Handle * GNUNET_SERVICE_client_get_mq(struct GNUNET_SERVICE_Client *c)
Obtain the message queue of c.
Definition: service.c:2734
#define GNUNET_NO
Definition: gnunet_common.h:81
uint32_t client_id
identifies the request this message responds to.
Definition: resolver.h:84
#define GNUNET_DNSPARSER_TYPE_PTR
#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:52
char * hostname
For NS, CNAME and PTR records, this is the uncompressed 0-terminated hostname.
#define GNUNET_memcpy(dst, src, n)
#define GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE
Response to a DNS resolution request.
size_t data_len
Number of bytes in data.
void * data
Binary record data.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static unsigned long long payload
How much data are we currently storing in the database?
#define GNUNET_DNSPARSER_TYPE_AAAA
#define GNUNET_log(kind,...)
#define GNUNET_YES
Definition: gnunet_common.h:80
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:353
union GNUNET_DNSPARSER_Record::@27 data
Payload of the record (which one of these is valid depends on the &#39;type&#39;).
#define GNUNET_DNSPARSER_TYPE_ALL
struct GNUNET_DNSPARSER_RawRecord raw
Raw data for all other types.
#define GNUNET_DNSPARSER_TYPE_A
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 521 of file gnunet-service-resolver.c.

References GNUNET_RESOLVER_ResponseMessage::client_id, 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().

522 {
524  struct GNUNET_MQ_Envelope *env;
525 
526  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending END message\n");
528  msg->client_id = client_request_id;
530 }
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Handle * GNUNET_SERVICE_client_get_mq(struct GNUNET_SERVICE_Client *c)
Obtain the message queue of c.
Definition: service.c:2734
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
uint32_t client_id
identifies the request this message responds to.
Definition: resolver.h:84
#define GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE
Response to a DNS resolution request.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_log(kind,...)
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:353
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 541 of file gnunet-service-resolver.c.

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, hostname, RecordListEntry::next, process_get(), ResolveCache::records_head, and ResolveCache::records_tail.

Referenced by try_cache().

542 {
544  struct RecordListEntry *n;
545 
546  for (struct RecordListEntry *pos = rc->records_head; NULL != pos; pos = n)
547  {
548  n = pos->next;
549  if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
550  {
552  GNUNET_DNSPARSER_free_record (pos->record);
553  GNUNET_free (pos->record);
554  GNUNET_free (pos);
555  }
556  }
557  if (NULL == rc->records_head)
558  {
559  free_cache_entry (rc);
560  return GNUNET_YES;
561  }
562  return GNUNET_NO;
563 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct RecordListEntry * records_head
head of a double linked list containing the lookup results
void GNUNET_DNSPARSER_free_record(struct GNUNET_DNSPARSER_Record *r)
Free the given DNS record.
Definition: dnsparser.c:180
#define GNUNET_NO
Definition: gnunet_common.h:81
struct RecordListEntry * next
This is a doubly linked list.
uint64_t abs_value_us
The actual value.
struct RecordListEntry * records_tail
tail of a double linked list containing the lookup results
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
static void free_cache_entry(struct ResolveCache *rc)
Remove entry from cache.
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:80
Entry in list of cached DNS records for a hostname.
#define GNUNET_free(ptr)
Wrapper around free.
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 936 of file gnunet-service-resolver.c.

References GNUNET_break, GNUNET_NO, GNUNET_OK, GNUNET_SERVICE_client_drop(), GNUNET_snprintf(), my_domain, resolve_and_cache(), send_end_msg(), and try_cache().

Referenced by handle_get(), remove_expired(), and try_cache().

940 {
941  char fqdn[255];
942 
943  if (GNUNET_NO != try_cache (hostname, record_type, client_request_id, client))
944  return;
945  if ((NULL != my_domain) && (NULL == strchr (hostname, (unsigned char) '.')) &&
946  (strlen (hostname) + strlen (my_domain) <= 253))
947  {
948  GNUNET_snprintf (fqdn, sizeof (fqdn), "%s.%s", hostname, my_domain);
949  }
950  else if (strlen (hostname) < 255)
951  {
952  GNUNET_snprintf (fqdn, sizeof (fqdn), "%s", hostname);
953  }
954  else
955  {
956  GNUNET_break (0);
958  return;
959  }
960  if (GNUNET_NO == try_cache (fqdn, record_type, client_request_id, client))
961  {
962  if (GNUNET_OK !=
963  resolve_and_cache (fqdn, record_type, client_request_id, client))
964  {
965  send_end_msg (client_request_id, client);
966  }
967  }
968 }
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
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 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.
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2618
static char * hostname
Our hostname; we give this to all the peers we start.
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 char * my_domain
My domain, to be appended to the hostname to get a FQDN.
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 594 of file gnunet-service-resolver.c.

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

Referenced by handle_resolve_result(), and process_get().

598 {
599  struct ResolveCache *pos;
600  struct ResolveCache *next;
601  int found;
602  int in_hosts;
603 
604  in_hosts = GNUNET_NO;
605  for (pos = hosts_head; NULL != pos; pos = pos->next)
606  if (0 == strcmp (pos->hostname, hostname))
607  {
608  in_hosts = GNUNET_YES;
609  break;
610  }
611  if (NULL == pos)
612  {
613  next = cache_head;
614  for (pos = next; NULL != pos; pos = next)
615  {
616  next = pos->next;
617  if (GNUNET_YES == remove_expired (pos))
618  continue;
619  if (0 == strcmp (pos->hostname, hostname))
620  break;
621  }
622  }
623  if (NULL == pos)
624  {
625  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cache entry for '%s'\n", hostname);
626  return GNUNET_NO;
627  }
628  if ((GNUNET_NO == in_hosts) && (cache_head != pos))
629  {
630  /* move result to head to achieve LRU for cache eviction */
633  }
634  found = GNUNET_NO;
635  for (struct RecordListEntry *rle = pos->records_head; NULL != rle;
636  rle = rle->next)
637  {
638  const struct GNUNET_DNSPARSER_Record *record = rle->record;
639 
641  "Found cache entry for '%s', record type '%u'\n",
642  hostname,
643  record_type);
644  if ((GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
645  (GNUNET_DNSPARSER_TYPE_CNAME != record_type) && (GNUNET_NO == found))
646  {
647  const char *hostname = record->data.hostname;
648 
649  process_get (hostname, record_type, client_request_id, client);
650  return GNUNET_YES; /* counts as a cache "hit" */
651  }
652  found |= send_reply (rle->record, record_type, client_request_id, client);
653  }
654  if (GNUNET_NO == found)
655  return GNUNET_NO; /* had records, but none matched! */
656  send_end_msg (client_request_id, client);
657  return GNUNET_YES;
658 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
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.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
Definition: w32nsp.c:83
struct RecordListEntry * records_head
head of a double linked list containing the lookup results
#define GNUNET_DNSPARSER_TYPE_CNAME
#define GNUNET_NO
Definition: gnunet_common.h:81
struct ResolveCache * next
This is a doubly linked list.
struct RecordListEntry * next
This is a doubly linked list.
A cached DNS lookup result.
char * hostname
For NS, CNAME and PTR records, this is the uncompressed 0-terminated hostname.
static struct ResolveCache * hosts_head
Head of the linked list of DNS lookup results from /etc/hosts.
static int remove_expired(struct ResolveCache *rc)
Remove expired entries from rc.
static struct ResolveCache * cache_tail
Tail of the linked list of cached DNS lookup results.
A DNS response record.
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...
static struct ResolveCache * cache_head
Start of the linked list of cached DNS lookup results.
static char * hostname
Our hostname; we give this to all the peers we start.
#define GNUNET_log(kind,...)
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.
char * hostname
Which hostname is this cache for?
#define GNUNET_YES
Definition: gnunet_common.h:80
union GNUNET_DNSPARSER_Record::@27 data
Payload of the record (which one of these is valid depends on the &#39;type&#39;).
Entry in list of cached DNS records for a hostname.
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
packet_buf[out]where to write the request packet
packet_size[out]set to size of packet_buf on success
Returns
GNUNET_OK on success

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

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, 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().

678 {
679  struct GNUNET_DNSPARSER_Query query;
680  struct GNUNET_DNSPARSER_Packet packet;
681 
682  query.name = (char *) hostname;
683  query.type = type;
684  query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
685  memset (&packet, 0, sizeof (packet));
686  packet.num_queries = 1;
687  packet.queries = &query;
688  packet.id = htons (dns_id);
689  packet.flags.recursion_desired = 1;
690  if (GNUNET_OK !=
691  GNUNET_DNSPARSER_pack (&packet, UINT16_MAX, packet_buf, packet_size))
692  {
694  "Failed to pack query for hostname `%s'\n",
695  hostname);
696  packet_buf = NULL;
697  return GNUNET_SYSERR;
698  }
699  return GNUNET_OK;
700 }
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
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#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&#39;r...
static char * hostname
Our hostname; we give this to all the peers we start.
Easy-to-process, parsed version of a DNS packet.
#define GNUNET_log(kind,...)
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
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 703 of file gnunet-service-resolver.c.

References cache_size, GNUNET_CONTAINER_DLL_insert, GNUNET_DNSPARSER_duplicate_record(), GNUNET_new, GNUNET_strdup, ResolveCache::hostname, ResolveCache::next, RecordListEntry::record, ResolveCache::records_head, and ResolveCache::records_tail.

Referenced by handle_resolve_result().

706 {
707  struct ResolveCache *rc;
709  struct RecordListEntry *rle;
710 
711  for (unsigned int i = 0; i != num_records; i++)
712  {
713  record = &records[i];
714 
715  for (rc = cache_head; NULL != rc; rc = rc->next)
716  if (0 == strcasecmp (rc->hostname, name))
717  break;
718  if (NULL == rc)
719  {
720  rc = GNUNET_new (struct ResolveCache);
721  rc->hostname = GNUNET_strdup (name);
723  cache_size++;
724  }
725  /* TODO: ought to check first if we have this exact record
726  already in the cache! */
727  rle = GNUNET_new (struct RecordListEntry);
730  }
731 }
static unsigned int cache_size
How many entries do we have in cache_head DLL?
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
struct RecordListEntry * records_head
head of a double linked list containing the lookup results
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct ResolveCache * next
This is a doubly linked list.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
A cached DNS lookup result.
struct RecordListEntry * records_tail
tail of a double linked list containing the lookup results
struct GNUNET_DNSPARSER_Record * record
Cached data.
static struct ResolveCache * cache_tail
Tail of the linked list of cached DNS lookup results.
A DNS response record.
struct GNUNET_DNSPARSER_Record * GNUNET_DNSPARSER_duplicate_record(const struct GNUNET_DNSPARSER_Record *r)
Duplicate (deep-copy) the given DNS record.
Definition: dnsparser.c:743
static struct ResolveCache * cache_head
Start of the linked list of cached DNS lookup results.
const char * name
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
char * hostname
Which hostname is this cache for?
Entry in list of cached DNS records for a hostname.
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 742 of file gnunet-service-resolver.c.

References GNUNET_DNSPARSER_Packet::additional_records, GNUNET_DNSPARSER_Packet::answers, GNUNET_DNSPARSER_Packet::authority_records, cache_answers(), cache_size, ActiveLookup::client, ActiveLookup::client_request_id, ActiveLookup::did_aaaa, ActiveLookup::dns_id, 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().

745 {
746  struct ActiveLookup *al = cls;
747  struct GNUNET_DNSPARSER_Packet *parsed;
748 
749  parsed = GNUNET_DNSPARSER_parse ((const char *) dns, dns_len);
750  if (NULL == parsed)
751  {
753  "Failed to parse DNS reply (hostname %s, request ID %u)\n",
754  al->hostname,
755  al->dns_id);
756  return;
757  }
758  if (al->dns_id != ntohs (parsed->id))
759  {
761  "Request ID in DNS reply does not match\n");
763  return;
764  }
765  if (0 == parsed->num_answers + parsed->num_authority_records +
766  parsed->num_additional_records)
767  {
769  "DNS reply (hostname %s, request ID %u) contains no answers\n",
770  al->hostname,
771  (unsigned int) al->client_request_id);
772  /* resume by trying again from cache */
773  if (GNUNET_NO == try_cache (al->hostname,
774  al->record_type,
775  al->client_request_id,
776  al->client))
777  /* cache failed, tell client we could not get an answer */
778  {
780  }
782  free_active_lookup (al);
783  return;
784  }
785  /* LRU-based cache eviction: we remove from tail */
786  while (cache_size > MAX_CACHE)
788 
790  "Got reply for hostname %s and request ID %u\n",
791  al->hostname,
792  (unsigned int) al->client_request_id);
793  /* add to cache */
794  cache_answers (al->hostname, parsed->answers, parsed->num_answers);
795  cache_answers (al->hostname,
796  parsed->authority_records,
797  parsed->num_authority_records);
798  cache_answers (al->hostname,
799  parsed->additional_records,
800  parsed->num_additional_records);
801 
802  /* see if we need to do the 2nd request for AAAA records */
803  if ((GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
804  (GNUNET_NO == al->did_aaaa))
805  {
806  char *packet_buf;
807  size_t packet_size;
808  uint16_t dns_id;
809 
811  UINT16_MAX);
812  if (GNUNET_OK == pack (al->hostname,
814  dns_id,
815  &packet_buf,
816  &packet_size))
817  {
818  al->did_aaaa = GNUNET_YES;
819  al->dns_id = dns_id;
822  packet_buf,
823  packet_size,
825  al);
826  GNUNET_free (packet_buf);
828  return;
829  }
830  }
831 
832  /* resume by trying again from cache */
833  if (GNUNET_NO == try_cache (al->hostname,
834  al->record_type,
835  al->client_request_id,
836  al->client))
837  /* cache failed, tell client we could not get an answer */
838  {
840  }
841  free_active_lookup (al);
843 }
struct GNUNET_DNSPARSER_Record * answers
Array of all answers in the packet, must contain "num_answers" entries.
static unsigned int cache_size
How many entries do we have in cache_head DLL?
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.
struct GNUNET_SERVICE_Client * client
The client that queried the records contained in this cache entry.
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
static struct GNUNET_DNSSTUB_Context * dnsstub_ctx
context of dnsstub library
uint16_t id
DNS ID (to match replies to requests).
void GNUNET_DNSPARSER_free_packet(struct GNUNET_DNSPARSER_Packet *p)
Free memory taken by a packet.
Definition: dnsparser.c:854
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static void handle_resolve_result(void *cls, const struct GNUNET_TUN_DnsHeader *dns, size_t dns_len)
We got a result from DNS.
struct GNUNET_DNSPARSER_Record * additional_records
Array of all additional answers in the packet, must contain "num_additional_records" entries...
unsigned int num_answers
Number of answers in the packet, should be 0 for queries.
uint16_t dns_id
Unique DNS request ID of a client if a query for this hostname/record_type is currently pending...
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).
uint32_t client_request_id
Unique request ID of a client if a query for this hostname/record_type is currently pending...
uint16_t record_type
type of queried DNS record
unsigned int num_additional_records
Number of additional records in the packet, should be 0 for queries.
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:662
int did_aaaa
If record_type is GNUNET_DNSPARSER_TYPE_ALL, did we go again for the AAAA records yet...
Randomness for IVs etc.
static struct ResolveCache * cache_tail
Tail of the linked list of cached DNS lookup results.
Information about pending lookups.
struct GNUNET_DNSPARSER_Record * authority_records
Array of all authority records in the packet, must contain "num_authority_records" entries...
static void cache_answers(const char *name, struct GNUNET_DNSPARSER_Record *records, unsigned int num_records)
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:533
unsigned int num_authority_records
Number of authoritative answers in the packet, should be 0 for queries.
static void free_cache_entry(struct ResolveCache *rc)
Remove entry from cache.
#define GNUNET_DNSPARSER_TYPE_AAAA
void GNUNET_DNSSTUB_resolve_cancel(struct GNUNET_DNSSTUB_RequestSocket *rs)
Cancel DNS resolution.
Definition: dnsstub.c:571
Easy-to-process, parsed version of a DNS packet.
struct GNUNET_DNSSTUB_RequestSocket * resolve_handle
handle for cancelling a request
#define GNUNET_log(kind,...)
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.
#define GNUNET_YES
Definition: gnunet_common.h:80
static void free_active_lookup(struct ActiveLookup *al)
Release resources associated with al.
#define GNUNET_DNSPARSER_TYPE_ALL
char * hostname
Which hostname are we resolving?
#define MAX_CACHE
Maximum number of hostnames we cache results for.
#define GNUNET_free(ptr)
Wrapper around free.
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 853 of file gnunet-service-resolver.c.

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().

854 {
855  struct ActiveLookup *al = cls;
856 
857  al->timeout_task = NULL;
858  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DNS lookup timeout!\n");
860  free_active_lookup (al);
861 }
struct GNUNET_SERVICE_Client * client
The client that queried the records contained in this cache entry.
struct GNUNET_SCHEDULER_Task * timeout_task
handle for the resolution timeout task
uint32_t client_request_id
Unique request ID of a client if a query for this hostname/record_type is currently pending...
Information about pending lookups.
#define GNUNET_log(kind,...)
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 void free_active_lookup(struct ActiveLookup *al)
Release resources associated with al.
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 875 of file gnunet-service-resolver.c.

References ActiveLookup::client, ActiveLookup::client_request_id, ActiveLookup::dns_id, DNS_TIMEOUT, 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(), ActiveLookup::hostname, pack(), ActiveLookup::record_type, ActiveLookup::resolve_handle, ActiveLookup::timeout_task, and type.

Referenced by process_get().

879 {
880  char *packet_buf;
881  size_t packet_size;
882  struct ActiveLookup *al;
883  uint16_t dns_id;
884  uint16_t type;
885 
886  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "resolve_and_cache `%s'\n", hostname);
888  UINT16_MAX);
889 
892  else
893  type = record_type;
894  if (GNUNET_OK != pack (hostname, type, dns_id, &packet_buf, &packet_size))
895  {
897  "Failed to pack query for hostname `%s'\n",
898  hostname);
899  return GNUNET_SYSERR;
900  }
901 
902  al = GNUNET_new (struct ActiveLookup);
904  al->record_type = record_type;
906  al->dns_id = dns_id;
907  al->client = client;
908  al->timeout_task =
911  packet_buf,
912  packet_size,
914  al);
915  GNUNET_free (packet_buf);
918  "Resolving %s, client_request_id = %u, dns_id = %u\n",
919  hostname,
920  (unsigned int) client_request_id,
921  (unsigned int) dns_id);
922  return GNUNET_OK;
923 }
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
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.
struct GNUNET_SERVICE_Client * client
The client that queried the records contained in this cache entry.
struct GNUNET_SCHEDULER_Task * timeout_task
handle for the resolution timeout task
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
static struct GNUNET_DNSSTUB_Context * dnsstub_ctx
context of dnsstub library
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static void handle_resolve_result(void *cls, const struct GNUNET_TUN_DnsHeader *dns, size_t dns_len)
We got a result from DNS.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define DNS_TIMEOUT
How long do we wait for DNS answers?
uint16_t dns_id
Unique DNS request ID of a client if a query for this hostname/record_type is currently pending...
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:1246
uint32_t client_request_id
Unique request ID of a client if a query for this hostname/record_type is currently pending...
static struct ActiveLookup * lookup_tail
Tail of the linked list of active DNS lookups.
uint16_t record_type
type of queried DNS record
Randomness for IVs etc.
static void handle_resolve_timeout(void *cls)
We encountered a timeout trying to perform a DNS lookup.
Information about pending lookups.
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:533
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static struct ActiveLookup * lookup_head
Start of the linked list of active DNS lookups.
static char * hostname
Our hostname; we give this to all the peers we start.
struct GNUNET_DNSSTUB_RequestSocket * resolve_handle
handle for cancelling a request
#define GNUNET_log(kind,...)
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
#define GNUNET_DNSPARSER_TYPE_ALL
#define GNUNET_DNSPARSER_TYPE_A
char * hostname
Which hostname are we resolving?
#define GNUNET_free(ptr)
Wrapper around free.
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 979 of file gnunet-service-resolver.c.

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

980 {
981  uint16_t size;
982  int direction;
983  int af;
984 
985  (void) cls;
986  size = ntohs (get->header.size) - sizeof (*get);
987  direction = ntohl (get->direction);
988  if (GNUNET_NO == direction)
989  {
991  return GNUNET_OK;
992  }
993  af = ntohl (get->af);
994  switch (af)
995  {
996  case AF_INET:
997  if (size != sizeof (struct in_addr))
998  {
999  GNUNET_break (0);
1000  return GNUNET_SYSERR;
1001  }
1002  break;
1003  case AF_INET6:
1004  if (size != sizeof (struct in6_addr))
1005  {
1006  GNUNET_break (0);
1007  return GNUNET_SYSERR;
1008  }
1009  break;
1010  default:
1011  GNUNET_break (0);
1012  return GNUNET_SYSERR;
1013  }
1014  return GNUNET_OK;
1015 }
int32_t direction
GNUNET_YES to get hostname from IP, GNUNET_NO to get IP from hostname.
Definition: resolver.h:54
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST.
Definition: resolver.h:48
#define GNUNET_MQ_check_zero_termination(m)
Insert code for a "check_" function that verifies that a given variable-length message received over ...
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
int32_t af
Address family to use (AF_INET, AF_INET6 or AF_UNSPEC).
Definition: resolver.h:59

◆ 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 1025 of file gnunet-service-resolver.c.

References GNUNET_RESOLVER_GetMessage::af, GNUNET_RESOLVER_GetMessage::client_id, GNUNET_RESOLVER_GetMessage::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_non_null, GNUNET_log, GNUNET_NO, GNUNET_SERVICE_client_continue(), GNUNET_strdup, hostname, make_reverse_hostname(), and process_get().

1026 {
1027  struct GNUNET_SERVICE_Client *client = cls;
1028  int direction;
1029  int af;
1030  uint32_t client_request_id;
1031  char *hostname;
1032 
1033  direction = ntohl (msg->direction);
1034  af = ntohl (msg->af);
1035  client_request_id = msg->client_id;
1037  if (GNUNET_NO == direction)
1038  {
1039  /* IP from hostname */
1040  hostname = GNUNET_strdup ((const char *) &msg[1]);
1042  "Client asks to resolve `%s'\n",
1043  hostname);
1044  switch (af)
1045  {
1046  case AF_UNSPEC: {
1047  process_get (hostname,
1049  client_request_id,
1050  client);
1051  break;
1052  }
1053  case AF_INET: {
1054  process_get (hostname,
1056  client_request_id,
1057  client);
1058  break;
1059  }
1060  case AF_INET6: {
1061  process_get (hostname,
1063  client_request_id,
1064  client);
1065  break;
1066  }
1067  default: {
1068  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got invalid af: %d\n", af);
1069  GNUNET_assert (0);
1070  }
1071  }
1072  }
1073  else
1074  {
1075  /* hostname from IP */
1076  hostname = make_reverse_hostname (&msg[1], af);
1077  process_get (hostname,
1079  client_request_id,
1080  client);
1081  }
1082  GNUNET_free_non_null (hostname);
1083 }
static char * make_reverse_hostname(const void *ip, int af)
Compute name to use for DNS reverse lookups from ip.
int32_t direction
GNUNET_YES to get hostname from IP, GNUNET_NO to get IP from hostname.
Definition: resolver.h:54
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_DNSPARSER_TYPE_PTR
Handle to a client that is connected to a service.
Definition: service.c:249
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...
int32_t af
Address family to use (AF_INET, AF_INET6 or AF_UNSPEC).
Definition: resolver.h:59
static char * hostname
Our hostname; we give this to all the peers we start.
#define GNUNET_DNSPARSER_TYPE_AAAA
uint32_t client_id
identifies the request and is contained in the response message.
Definition: resolver.h:65
#define GNUNET_log(kind,...)
#define GNUNET_DNSPARSER_TYPE_ALL
#define GNUNET_DNSPARSER_TYPE_A
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2533
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 1092 of file gnunet-service-resolver.c.

References free_active_lookup(), free_cache_entry(), free_hosts_entry(), GNUNET_DNSSTUB_stop(), GNUNET_free_non_null, and my_domain.

Referenced by init_cb().

1093 {
1094  (void) cls;
1095 
1096  while (NULL != lookup_head)
1098  while (NULL != cache_head)
1100  while (NULL != hosts_head)
1104 }
void GNUNET_DNSSTUB_stop(struct GNUNET_DNSSTUB_Context *ctx)
Cleanup DNSSTUB resolver.
Definition: dnsstub.c:731
static struct GNUNET_DNSSTUB_Context * dnsstub_ctx
context of dnsstub library
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
static struct ResolveCache * hosts_head
Head of the linked list of DNS lookup results from /etc/hosts.
static void free_hosts_entry(struct ResolveCache *rc)
Remove entry from cache.
static struct ResolveCache * cache_head
Start of the linked list of cached DNS lookup results.
static struct ActiveLookup * lookup_head
Start of the linked list of active DNS lookups.
static void free_cache_entry(struct ResolveCache *rc)
Remove entry from cache.
static void free_active_lookup(struct ActiveLookup *al)
Release resources associated with al.
static char * my_domain
My domain, to be appended to the hostname to get a FQDN.
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 1117 of file gnunet-service-resolver.c.

References GNUNET_DNSPARSER_RawRecord::data, GNUNET_DNSPARSER_Record::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, ResolveCache::hostname, 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(), and testbed_run().

1121 {
1122  struct ResolveCache *rc;
1123  struct RecordListEntry *rle;
1124  struct GNUNET_DNSPARSER_Record *rec;
1125 
1126  rec = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_Record));
1128  rec->type = rec_type;
1130  rec->name = GNUNET_strdup (hostname);
1132  rec->data.raw.data_len = data_size;
1133  rle = GNUNET_new (struct RecordListEntry);
1134  rle->record = rec;
1135  rc = GNUNET_new (struct ResolveCache);
1139 }
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
static struct ResolveCache * hosts_tail
Tail of the linked list of DNS lookup results from /etc/hosts.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
struct RecordListEntry * records_head
head of a double linked list containing the lookup results
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
A cached DNS lookup result.
struct RecordListEntry * records_tail
tail of a double linked list containing the lookup results
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
struct GNUNET_DNSPARSER_Record * record
Cached data.
char * name
Name of the record that the query is for (0-terminated).
static struct ResolveCache * hosts_head
Head of the linked list of DNS lookup results from /etc/hosts.
A DNS response record.
size_t data_len
Number of bytes in data.
void * data
Binary record data.
#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&#39;r...
struct GNUNET_TIME_Absolute expiration_time
When does the record expire?
static char * hostname
Our hostname; we give this to all the peers we start.
uint16_t dns_traffic_class
See GNUNET_TUN_DNS_CLASS_*.
char * hostname
Which hostname is this cache for?
union GNUNET_DNSPARSER_Record::@27 data
Payload of the record (which one of these is valid depends on the &#39;type&#39;).
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
Entry in list of cached DNS records for a hostname.
struct GNUNET_DNSPARSER_RawRecord raw
Raw data for all other types.
#define GNUNET_malloc(size)
Wrapper around malloc.
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 1149 of file gnunet-service-resolver.c.

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

Referenced by load_etc_hosts().

1150 {
1151  const char *c;
1152  struct in_addr v4;
1153  struct in6_addr v6;
1154  char *tbuf;
1155  char *tok;
1156 
1157  /* ignore everything after '#' */
1158  c = memrchr (line, (unsigned char) '#', line_len);
1159  if (NULL != c)
1160  line_len = c - line;
1161  /* ignore leading whitespace */
1162  while ((0 < line_len) && isspace ((unsigned char) *line))
1163  {
1164  line++;
1165  line_len--;
1166  }
1167  tbuf = GNUNET_strndup (line, line_len);
1168  tok = strtok (tbuf, " \t");
1169  if (NULL == tok)
1170  {
1171  GNUNET_free (tbuf);
1172  return;
1173  }
1174  if (1 == inet_pton (AF_INET, tok, &v4))
1175  {
1176  while (NULL != (tok = strtok (NULL, " \t")))
1177  add_host (tok, GNUNET_DNSPARSER_TYPE_A, &v4, sizeof (struct in_addr));
1178  }
1179  else if (1 == inet_pton (AF_INET6, tok, &v6))
1180  {
1181  while (NULL != (tok = strtok (NULL, " \t")))
1182  add_host (tok, GNUNET_DNSPARSER_TYPE_AAAA, &v6, sizeof (struct in6_addr));
1183  }
1184  GNUNET_free (tbuf);
1185 }
#define memrchr(s, c, n)
Definition: compat.h:48
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.
static char * line
Desired phone line (string to be converted to a hash).
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
#define GNUNET_DNSPARSER_TYPE_AAAA
static int inet_pton(int af, const char *cp, struct in_addr *buf)
Convert IPv4 address from text to binary form.
#define GNUNET_DNSPARSER_TYPE_A
#define GNUNET_free(ptr)
Wrapper around free.
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 1192 of file gnunet-service-resolver.c.

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().

1193 {
1194  struct GNUNET_DISK_FileHandle *fh;
1195  struct GNUNET_DISK_MapHandle *mh;
1196  off_t bytes_read;
1197  const char *buf;
1198  size_t read_offset;
1199 
1200  fh = GNUNET_DISK_file_open ("/etc/hosts",
1203  if (NULL == fh)
1204  {
1205  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to open /etc/hosts");
1206  return;
1207  }
1208  if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read))
1209  {
1211  "Could not determin size of /etc/hosts. "
1212  "DNS resolution will not be possible.\n");
1214  return;
1215  }
1216  if (((unsigned long long) bytes_read) > (unsigned long long) SIZE_MAX)
1217  {
1219  "/etc/hosts file too large to mmap. "
1220  "DNS resolution will not be possible.\n");
1222  return;
1223  }
1224  buf = GNUNET_DISK_file_map (fh,
1225  &mh,
1227  (size_t) bytes_read);
1228  read_offset = 0;
1229  while (read_offset < (size_t) bytes_read)
1230  {
1231  const char *newline;
1232  size_t line_len;
1233 
1234  newline = strchr (buf + read_offset, '\n');
1235  if (NULL == newline)
1236  break;
1237  line_len = newline - buf - read_offset;
1238  extract_hosts (buf + read_offset, line_len);
1239  read_offset += line_len + 1;
1240  }
1243 }
Open the file for reading.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1817
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
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:2020
Nobody is allowed to do anything to the file.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define SIZE_MAX
Definition: platform.h:250
Read-only memory map.
static char buf[2048]
static int fh
Handle to the unique file.
int GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
Unmap a file.
Definition: disk.c:2100
int GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
Definition: disk.c:208
#define GNUNET_log(kind,...)
static void extract_hosts(const char *line, size_t line_len)
Extract host information from a line in /etc/hosts.
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:1673
Handle used to access files (and pipes).
Handle for a memory-mapping operation.
Definition: disk.c:1985
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 1254 of file gnunet-service-resolver.c.

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

Referenced by disconnect_cb().

1257 {
1258  char **dns_servers;
1259  int num_dns_servers;
1260 
1261  (void) cfg;
1262  (void) sh;
1263  load_etc_hosts ();
1266  dns_servers = NULL;
1267  num_dns_servers = lookup_dns_servers (&dns_servers);
1268  if (0 >= num_dns_servers)
1269  {
1270  GNUNET_log (
1272  _ ("No DNS server available. DNS resolution will not be possible.\n"));
1273  return;
1274  }
1275  for (int i = 0; i < num_dns_servers; i++)
1276  {
1277  int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1279  "Adding DNS server '%s': %s\n",
1280  dns_servers[i],
1281  GNUNET_OK == result ? "success" : "failure");
1282  GNUNET_free (dns_servers[i]);
1283  }
1284  GNUNET_free_non_null (dns_servers);
1285 }
static void load_etc_hosts(void)
Reads the list of hosts from /etc/hosts.
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, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1293
static struct GNUNET_DNSSTUB_Context * dnsstub_ctx
context of dnsstub library
static void shutdown_task(void *cls)
Service is shutting down, clean up.
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:622
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
struct GNUNET_DNSSTUB_Context * GNUNET_DNSSTUB_start(unsigned int num_sockets)
Start a DNS stub resolver.
Definition: dnsstub.c:595
static int result
Global testing status.
#define GNUNET_log(kind,...)
static int lookup_dns_servers(char ***server_addrs)
Reads the list of nameservers from /etc/resolve.conf.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller 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 1297 of file gnunet-service-resolver.c.

Referenced by disconnect_cb().

1300 {
1301  (void) cls;
1302  (void) mq;
1303 
1304  return c;
1305 }
Here is the caller graph for this function:

◆ 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 1316 of file gnunet-service-resolver.c.

References __attribute__, connect_cb(), free_active_lookup(), GNUNET_assert, GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST, GNUNET_MQ_handler_end, GNUNET_MQ_hd_var_size, GNUNET_SERVICE_MAIN(), GNUNET_SERVICE_OPTION_NONE, init_cb(), lookup_head, and ActiveLookup::next.

1317 {
1318  struct ActiveLookup *n;
1319  (void) cls;
1320 
1321  GNUNET_assert (c == internal_cls);
1322  n = lookup_head;
1323  for (struct ActiveLookup *al = n; NULL != al; al = n)
1324  {
1325  n = al->next;
1326  if (al->client == c)
1327  free_active_lookup (al);
1328  }
1329 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Information about pending lookups.
static struct ActiveLookup * lookup_head
Start of the linked list of active DNS lookups.
struct ActiveLookup * next
Stored in a DLL.
static void free_active_lookup(struct ActiveLookup *al)
Release resources associated with al.
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.

Referenced by disconnect_cb().

Here is the caller graph for this function:

Variable Documentation

◆ cache_head

struct ResolveCache* cache_head
static

Start of the linked list of cached DNS lookup results.

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

Referenced by try_cache().

◆ cache_tail

struct ResolveCache* cache_tail
static

Tail of the linked list of cached DNS lookup results.

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

◆ hosts_head

struct ResolveCache* hosts_head
static

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

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

◆ hosts_tail

struct ResolveCache* hosts_tail
static

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

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

◆ lookup_head

struct ActiveLookup* lookup_head
static

Start of the linked list of active DNS lookups.

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

Referenced by disconnect_cb().

◆ lookup_tail

struct ActiveLookup* lookup_tail
static

Tail of the linked list of active DNS lookups.

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

◆ dnsstub_ctx

struct GNUNET_DNSSTUB_Context* dnsstub_ctx
static

context of dnsstub library

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

◆ my_domain

char* my_domain
static

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

Definition at line 195 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 200 of file gnunet-service-resolver.c.

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