GNUnet  0.11.x
Typedefs | Functions
DNS Stub library

Helper library to send DNS requests to DNS resolver. More...

Typedefs

typedef void(* GNUNET_DNSSTUB_ResultCallback) (void *cls, const struct GNUNET_TUN_DnsHeader *dns, size_t dns_len)
 Function called with the result of a DNS resolution. More...
 

Functions

struct GNUNET_DNSSTUB_ContextGNUNET_DNSSTUB_start (unsigned int num_sockets)
 Start a DNS stub resolver. More...
 
int GNUNET_DNSSTUB_add_dns_ip (struct GNUNET_DNSSTUB_Context *ctx, const char *dns_ip)
 Add nameserver for use by the DNSSTUB. More...
 
int GNUNET_DNSSTUB_add_dns_sa (struct GNUNET_DNSSTUB_Context *ctx, const struct sockaddr *sa)
 Add nameserver for use by the DNSSTUB. More...
 
void GNUNET_DNSSTUB_set_retry (struct GNUNET_DNSSTUB_Context *ctx, struct GNUNET_TIME_Relative retry_freq)
 How long should we try requests before timing out? Only effective for requests issued after this call. More...
 
void GNUNET_DNSSTUB_stop (struct GNUNET_DNSSTUB_Context *ctx)
 Cleanup DNSSTUB resolver. More...
 
struct GNUNET_DNSSTUB_RequestSocketGNUNET_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. More...
 
void GNUNET_DNSSTUB_resolve_cancel (struct GNUNET_DNSSTUB_RequestSocket *rs)
 Cancel DNS resolution. More...
 

Detailed Description

Helper library to send DNS requests to DNS resolver.

Typedef Documentation

◆ GNUNET_DNSSTUB_ResultCallback

typedef void(* GNUNET_DNSSTUB_ResultCallback) (void *cls, const struct GNUNET_TUN_DnsHeader *dns, size_t dns_len)

Function called with the result of a DNS resolution.

Once this function is called, the resolution request is automatically cancelled / cleaned up. In particular, the function will only be called once.

Parameters
clsclosure
dnsdns response, NULL on hard error (i.e. timeout)
dns_lennumber of bytes in dns

Definition at line 115 of file gnunet_dnsstub_lib.h.

Function Documentation

◆ GNUNET_DNSSTUB_start()

struct GNUNET_DNSSTUB_Context* GNUNET_DNSSTUB_start ( unsigned int  num_sockets)

Start a DNS stub resolver.

Parameters
num_socketshow many sockets should we open in parallel for DNS queries for this stub?
Returns
NULL on error

Definition at line 564 of file dnsstub.c.

References GNUNET_DNSSTUB_RequestSocket::ctx, DNS_RETRANSMIT_DELAY, GNUNET_break, GNUNET_new, GNUNET_new_array, GNUNET_DNSSTUB_Context::num_sockets, GNUNET_DNSSTUB_Context::retry_freq, and GNUNET_DNSSTUB_Context::sockets.

Referenced by advertise_dns_exit(), init_cb(), main(), recursive_gns2dns_resolution(), and run().

565 {
566  struct GNUNET_DNSSTUB_Context *ctx;
567 
568  if (0 == num_sockets)
569  {
570  GNUNET_break (0);
571  return NULL;
572  }
573  ctx = GNUNET_new (struct GNUNET_DNSSTUB_Context);
574  ctx->num_sockets = num_sockets;
575  ctx->sockets =
578  return ctx;
579 }
#define DNS_RETRANSMIT_DELAY
Timeout for retrying DNS queries.
Definition: dnsstub.c:31
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
unsigned int num_sockets
Length of sockets array.
Definition: dnsstub.c:148
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
struct GNUNET_DNSSTUB_RequestSocket * sockets
Array of all open sockets for DNS requests.
Definition: dnsstub.c:128
Handle to the stub resolver.
Definition: dnsstub.c:123
struct GNUNET_TIME_Relative retry_freq
How frequently do we retry requests?
Definition: dnsstub.c:143
UDP socket we are using for sending DNS requests to the Internet.
Definition: dnsstub.c:44
Here is the caller graph for this function:

◆ GNUNET_DNSSTUB_add_dns_ip()

int GNUNET_DNSSTUB_add_dns_ip ( struct GNUNET_DNSSTUB_Context ctx,
const char *  dns_ip 
)

Add nameserver for use by the DNSSTUB.

We will use all provided nameservers for resolution (round-robin).

Parameters
ctxresolver context to modify
dns_iptarget IP address to use (as string)
Returns
GNUNET_OK on success

Definition at line 591 of file dnsstub.c.

References GNUNET_DNSSTUB_Context::dns_head, GNUNET_DNSSTUB_Context::dns_tail, ds, GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_new, GNUNET_OK, and GNUNET_SYSERR.

Referenced by advertise_dns_exit(), init_cb(), main(), recursive_gns2dns_resolution(), and run().

593 {
594  struct DnsServer *ds;
595  struct in_addr i4;
596  struct in6_addr i6;
597 
598  ds = GNUNET_new (struct DnsServer);
599  if (1 == inet_pton (AF_INET, dns_ip, &i4))
600  {
601  struct sockaddr_in *s4 = (struct sockaddr_in *) &ds->ss;
602 
603  s4->sin_family = AF_INET;
604  s4->sin_port = htons (53);
605  s4->sin_addr = i4;
606 #if HAVE_SOCKADDR_IN_SIN_LEN
607  s4->sin_len = (u_char) sizeof(struct sockaddr_in);
608 #endif
609  }
610  else if (1 == inet_pton (AF_INET6, dns_ip, &i6))
611  {
612  struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &ds->ss;
613 
614  s6->sin6_family = AF_INET6;
615  s6->sin6_port = htons (53);
616  s6->sin6_addr = i6;
617 #if HAVE_SOCKADDR_IN_SIN_LEN
618  s6->sin6_len = (u_char) sizeof(struct sockaddr_in6);
619 #endif
620  }
621  else
622  {
624  "Malformed IP address `%s' for DNS server\n",
625  dns_ip);
626  GNUNET_free (ds);
627  return GNUNET_SYSERR;
628  }
630  return GNUNET_OK;
631 }
struct DnsServer * dns_head
DLL of DNS resolvers we use.
Definition: dnsstub.c:133
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
static char * dns_ip
IP of DNS server.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
DNS Server used for resolution.
Definition: dnsstub.c:101
struct DnsServer * dns_tail
DLL of DNS resolvers we use.
Definition: dnsstub.c:138
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define GNUNET_log(kind,...)
static struct GNUNET_FS_DirScanner * ds
Handle to the directory scanner (for recursive insertions).
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ GNUNET_DNSSTUB_add_dns_sa()

int GNUNET_DNSSTUB_add_dns_sa ( struct GNUNET_DNSSTUB_Context ctx,
const struct sockaddr *  sa 
)

Add nameserver for use by the DNSSTUB.

We will use all provided nameservers for resolution (round-robin).

Parameters
ctxresolver context to modify
sasocket address of DNS resolver to use
Returns
GNUNET_OK on success

Definition at line 643 of file dnsstub.c.

References GNUNET_DNSSTUB_Context::dns_head, GNUNET_DNSSTUB_Context::dns_tail, ds, GNUNET_break, GNUNET_CONTAINER_DLL_insert, GNUNET_free, GNUNET_memcpy, GNUNET_new, GNUNET_OK, GNUNET_SYSERR, and DnsServer::ss.

Referenced by handle_gns2dns_ip(), and handle_gns2dns_result().

645 {
646  struct DnsServer *ds;
647 
648  ds = GNUNET_new (struct DnsServer);
649  switch (sa->sa_family)
650  {
651  case AF_INET:
652  GNUNET_memcpy (&ds->ss, sa, sizeof(struct sockaddr_in));
653  break;
654 
655  case AF_INET6:
656  GNUNET_memcpy (&ds->ss, sa, sizeof(struct sockaddr_in6));
657  break;
658 
659  default:
660  GNUNET_break (0);
661  GNUNET_free (ds);
662  return GNUNET_SYSERR;
663  }
665  return GNUNET_OK;
666 }
struct DnsServer * dns_head
DLL of DNS resolvers we use.
Definition: dnsstub.c:133
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct sockaddr_storage ss
IP address of the DNS resolver.
Definition: dnsstub.c:116
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
DNS Server used for resolution.
Definition: dnsstub.c:101
struct DnsServer * dns_tail
DLL of DNS resolvers we use.
Definition: dnsstub.c:138
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static struct GNUNET_FS_DirScanner * ds
Handle to the directory scanner (for recursive insertions).
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ GNUNET_DNSSTUB_set_retry()

void GNUNET_DNSSTUB_set_retry ( struct GNUNET_DNSSTUB_Context ctx,
struct GNUNET_TIME_Relative  retry_freq 
)

How long should we try requests before timing out? Only effective for requests issued after this call.

Parameters
ctxresolver context to modify
retry_frequhow long to wait between retries
ctxresolver context to modify
retry_freqhow long to wait between retries

Definition at line 677 of file dnsstub.c.

References GNUNET_DNSSTUB_Context::retry_freq.

679 {
680  ctx->retry_freq = retry_freq;
681 }
struct GNUNET_TIME_Relative retry_freq
How frequently do we retry requests?
Definition: dnsstub.c:143

◆ GNUNET_DNSSTUB_stop()

void GNUNET_DNSSTUB_stop ( struct GNUNET_DNSSTUB_Context ctx)

Cleanup DNSSTUB resolver.

Parameters
ctxstub resolver to clean up

Definition at line 690 of file dnsstub.c.

References cleanup_rs(), GNUNET_DNSSTUB_Context::dns_head, GNUNET_DNSSTUB_Context::dns_tail, ds, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_DNSSTUB_Context::num_sockets, and GNUNET_DNSSTUB_Context::sockets.

Referenced by cleanup(), cleanup_task(), do_shutdown(), GNS_resolver_lookup_cancel(), run(), and shutdown_task().

691 {
692  struct DnsServer *ds;
693 
694  while (NULL != (ds = ctx->dns_head))
695  {
697  GNUNET_free (ds);
698  }
699  for (unsigned int i = 0; i < ctx->num_sockets; i++)
700  cleanup_rs (&ctx->sockets[i]);
701  GNUNET_free (ctx->sockets);
702  GNUNET_free (ctx);
703 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct DnsServer * dns_head
DLL of DNS resolvers we use.
Definition: dnsstub.c:133
unsigned int num_sockets
Length of sockets array.
Definition: dnsstub.c:148
DNS Server used for resolution.
Definition: dnsstub.c:101
struct DnsServer * dns_tail
DLL of DNS resolvers we use.
Definition: dnsstub.c:138
struct GNUNET_DNSSTUB_RequestSocket * sockets
Array of all open sockets for DNS requests.
Definition: dnsstub.c:128
static struct GNUNET_FS_DirScanner * ds
Handle to the directory scanner (for recursive insertions).
static void cleanup_rs(struct GNUNET_DNSSTUB_RequestSocket *rs)
We&#39;re done with a struct GNUNET_DNSSTUB_RequestSocket, close it for now.
Definition: dnsstub.c:158
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DNSSTUB_resolve()

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.

Parameters
ctxstub resolver to use
requestDNS request to transmit
request_lennumber of bytes in msg
rcfunction to call with result (once)
rc_clsclosure for rc
Returns
socket used for the request, NULL on error
Parameters
ctxstub resolver to use
requestDNS request to transmit
request_lennumber of bytes in msg
rcfunction to call with result
rc_clsclosure for 'rc'
Returns
socket used for the request, NULL on error

Definition at line 504 of file dnsstub.c.

References GNUNET_DNSSTUB_Context::dns_head, GNUNET_DNSSTUB_RequestSocket::ds_pos, get_request_socket(), GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_memdup, GNUNET_SCHEDULER_add_now(), GNUNET_DNSSTUB_RequestSocket::rc, GNUNET_DNSSTUB_RequestSocket::rc_cls, GNUNET_DNSSTUB_RequestSocket::request, GNUNET_DNSSTUB_RequestSocket::request_len, GNUNET_DNSSTUB_RequestSocket::retry_task, and transmit_query().

Referenced by handle_dns_request(), handle_resolve_result(), next_phase(), process_queue(), recursive_dns_resolution(), resolve_and_cache(), result_processor(), and submit_req().

509 {
510  struct GNUNET_DNSSTUB_RequestSocket *rs;
511 
512  if (NULL == ctx->dns_head)
513  {
515  "No DNS server configured for resolution\n");
516  return NULL;
517  }
518  if (NULL == (rs = get_request_socket (ctx)))
519  {
521  "No request socket available for DNS resolution\n");
522  return NULL;
523  }
524  rs->ds_pos = ctx->dns_head;
525  rs->rc = rc;
526  rs->rc_cls = rc_cls;
528  rs->request_len = request_len;
530  return rs;
531 }
static struct GNUNET_VPN_RedirectionRequest * request
Opaque redirection request handle.
Definition: gnunet-vpn.c:41
struct DnsServer * dns_head
DLL of DNS resolvers we use.
Definition: dnsstub.c:133
static void transmit_query(void *cls)
Task to (re)transmit the DNS query, possibly repeatedly until we succeed.
Definition: dnsstub.c:432
GNUNET_DNSSTUB_ResultCallback rc
Function to call with result.
Definition: dnsstub.c:59
void * request
Query we sent to addr.
Definition: dnsstub.c:89
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
size_t request_len
Number of bytes in request.
Definition: dnsstub.c:94
struct DnsServer * ds_pos
Next address we sent the DNS request to.
Definition: dnsstub.c:79
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1280
static struct GNUNET_DNSSTUB_RequestSocket * get_request_socket(struct GNUNET_DNSSTUB_Context *ctx)
Get a socket of the specified address family to send out a UDP DNS request to the Internet...
Definition: dnsstub.c:244
void * rc_cls
Closure for rc.
Definition: dnsstub.c:64
#define GNUNET_log(kind,...)
struct GNUNET_SCHEDULER_Task * retry_task
Task for retrying transmission of the query.
Definition: dnsstub.c:74
UDP socket we are using for sending DNS requests to the Internet.
Definition: dnsstub.c:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DNSSTUB_resolve_cancel()

void GNUNET_DNSSTUB_resolve_cancel ( struct GNUNET_DNSSTUB_RequestSocket rs)

Cancel DNS resolution.

Parameters
rsresolution to cancel

Definition at line 540 of file dnsstub.c.

References GNUNET_SCHEDULER_cancel(), GNUNET_DNSSTUB_RequestSocket::rc, GNUNET_DNSSTUB_RequestSocket::read_task, and GNUNET_DNSSTUB_RequestSocket::retry_task.

Referenced by dns_result_parser(), dns_result_processor(), do_timeout(), free_active_lookup(), GNS_resolver_lookup_cancel(), handle_resolve_result(), and process_result().

541 {
542  rs->rc = NULL;
543  if (NULL != rs->retry_task)
544  {
546  rs->retry_task = NULL;
547  }
548  if (NULL != rs->read_task)
549  {
551  rs->read_task = NULL;
552  }
553 }
struct GNUNET_SCHEDULER_Task * read_task
Task for reading from dnsout4 and dnsout6.
Definition: dnsstub.c:69
GNUNET_DNSSTUB_ResultCallback rc
Function to call with result.
Definition: dnsstub.c:59
struct GNUNET_SCHEDULER_Task * retry_task
Task for retrying transmission of the query.
Definition: dnsstub.c:74
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:966
Here is the call graph for this function:
Here is the caller graph for this function: