GNUnet  0.10.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 561 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().

562 {
563  struct GNUNET_DNSSTUB_Context *ctx;
564 
565  if (0 == num_sockets)
566  {
567  GNUNET_break(0);
568  return NULL;
569  }
570  ctx = GNUNET_new(struct GNUNET_DNSSTUB_Context);
571  ctx->num_sockets = num_sockets;
572  ctx->sockets =
575  return ctx;
576 }
#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:145
#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:125
Handle to the stub resolver.
Definition: dnsstub.c:121
struct GNUNET_TIME_Relative retry_freq
How frequently do we retry requests?
Definition: dnsstub.c:140
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 588 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().

590 {
591  struct DnsServer *ds;
592  struct in_addr i4;
593  struct in6_addr i6;
594 
595  ds = GNUNET_new(struct DnsServer);
596  if (1 == inet_pton(AF_INET, dns_ip, &i4))
597  {
598  struct sockaddr_in *s4 = (struct sockaddr_in *)&ds->ss;
599 
600  s4->sin_family = AF_INET;
601  s4->sin_port = htons(53);
602  s4->sin_addr = i4;
603 #if HAVE_SOCKADDR_IN_SIN_LEN
604  s4->sin_len = (u_char)sizeof(struct sockaddr_in);
605 #endif
606  }
607  else if (1 == inet_pton(AF_INET6, dns_ip, &i6))
608  {
609  struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&ds->ss;
610 
611  s6->sin6_family = AF_INET6;
612  s6->sin6_port = htons(53);
613  s6->sin6_addr = i6;
614 #if HAVE_SOCKADDR_IN_SIN_LEN
615  s6->sin6_len = (u_char)sizeof(struct sockaddr_in6);
616 #endif
617  }
618  else
619  {
621  "Malformed IP address `%s' for DNS server\n",
622  dns_ip);
623  GNUNET_free(ds);
624  return GNUNET_SYSERR;
625  }
627  return GNUNET_OK;
628 }
struct DnsServer * dns_head
DLL of DNS resolvers we use.
Definition: dnsstub.c:130
#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:100
struct DnsServer * dns_tail
DLL of DNS resolvers we use.
Definition: dnsstub.c:135
#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 640 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().

642 {
643  struct DnsServer *ds;
644 
645  ds = GNUNET_new(struct DnsServer);
646  switch (sa->sa_family)
647  {
648  case AF_INET:
649  GNUNET_memcpy(&ds->ss, sa, sizeof(struct sockaddr_in));
650  break;
651 
652  case AF_INET6:
653  GNUNET_memcpy(&ds->ss, sa, sizeof(struct sockaddr_in6));
654  break;
655 
656  default:
657  GNUNET_break(0);
658  GNUNET_free(ds);
659  return GNUNET_SYSERR;
660  }
662  return GNUNET_OK;
663 }
struct DnsServer * dns_head
DLL of DNS resolvers we use.
Definition: dnsstub.c:130
#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:114
#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:100
struct DnsServer * dns_tail
DLL of DNS resolvers we use.
Definition: dnsstub.c:135
#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 674 of file dnsstub.c.

References GNUNET_DNSSTUB_Context::retry_freq.

676 {
677  ctx->retry_freq = retry_freq;
678 }
struct GNUNET_TIME_Relative retry_freq
How frequently do we retry requests?
Definition: dnsstub.c:140

◆ GNUNET_DNSSTUB_stop()

void GNUNET_DNSSTUB_stop ( struct GNUNET_DNSSTUB_Context ctx)

Cleanup DNSSTUB resolver.

Parameters
ctxstub resolver to clean up

Definition at line 687 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().

688 {
689  struct DnsServer *ds;
690 
691  while (NULL != (ds = ctx->dns_head))
692  {
694  GNUNET_free(ds);
695  }
696  for (unsigned int i = 0; i < ctx->num_sockets; i++)
697  cleanup_rs(&ctx->sockets[i]);
698  GNUNET_free(ctx->sockets);
699  GNUNET_free(ctx);
700 }
#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:130
unsigned int num_sockets
Length of sockets array.
Definition: dnsstub.c:145
DNS Server used for resolution.
Definition: dnsstub.c:100
struct DnsServer * dns_tail
DLL of DNS resolvers we use.
Definition: dnsstub.c:135
struct GNUNET_DNSSTUB_RequestSocket * sockets
Array of all open sockets for DNS requests.
Definition: dnsstub.c:125
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:155
#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 501 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().

506 {
507  struct GNUNET_DNSSTUB_RequestSocket *rs;
508 
509  if (NULL == ctx->dns_head)
510  {
512  "No DNS server configured for resolution\n");
513  return NULL;
514  }
515  if (NULL == (rs = get_request_socket(ctx)))
516  {
518  "No request socket available for DNS resolution\n");
519  return NULL;
520  }
521  rs->ds_pos = ctx->dns_head;
522  rs->rc = rc;
523  rs->rc_cls = rc_cls;
525  rs->request_len = request_len;
527  return rs;
528 }
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:130
static void transmit_query(void *cls)
Task to (re)transmit the DNS query, possibly repeatedly until we succeed.
Definition: dnsstub.c:429
GNUNET_DNSSTUB_ResultCallback rc
Function to call with result.
Definition: dnsstub.c:58
void * request
Query we sent to addr.
Definition: dnsstub.c:88
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
size_t request_len
Number of bytes in request.
Definition: dnsstub.c:93
struct DnsServer * ds_pos
Next address we sent the DNS request to.
Definition: dnsstub.c:78
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:1264
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:241
void * rc_cls
Closure for rc.
Definition: dnsstub.c:63
#define GNUNET_log(kind,...)
struct GNUNET_SCHEDULER_Task * retry_task
Task for retrying transmission of the query.
Definition: dnsstub.c:73
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 537 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().

538 {
539  rs->rc = NULL;
540  if (NULL != rs->retry_task)
541  {
543  rs->retry_task = NULL;
544  }
545  if (NULL != rs->read_task)
546  {
548  rs->read_task = NULL;
549  }
550 }
struct GNUNET_SCHEDULER_Task * read_task
Task for reading from dnsout4 and dnsout6.
Definition: dnsstub.c:68
GNUNET_DNSSTUB_ResultCallback rc
Function to call with result.
Definition: dnsstub.c:58
struct GNUNET_SCHEDULER_Task * retry_task
Task for retrying transmission of the query.
Definition: dnsstub.c:73
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
Here is the call graph for this function:
Here is the caller graph for this function: