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

596 {
597  struct GNUNET_DNSSTUB_Context *ctx;
598 
599  if (0 == num_sockets)
600  {
601  GNUNET_break (0);
602  return NULL;
603  }
604  ctx = GNUNET_new (struct GNUNET_DNSSTUB_Context);
605  ctx->num_sockets = num_sockets;
609  return ctx;
610 }
#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:151
#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:131
Handle to the stub resolver.
Definition: dnsstub.c:125
struct GNUNET_TIME_Relative retry_freq
How frequently do we retry requests?
Definition: dnsstub.c:146
UDP socket we are using for sending DNS requests to the Internet.
Definition: dnsstub.c:43
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 622 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, GNUNET_SYSERR, and inet_pton().

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

624 {
625  struct DnsServer *ds;
626  struct in_addr i4;
627  struct in6_addr i6;
628 
629  ds = GNUNET_new (struct DnsServer);
630  if (1 == inet_pton (AF_INET,
631  dns_ip,
632  &i4))
633  {
634  struct sockaddr_in *s4 = (struct sockaddr_in *) &ds->ss;
635 
636  s4->sin_family = AF_INET;
637  s4->sin_port = htons (53);
638  s4->sin_addr = i4;
639 #if HAVE_SOCKADDR_IN_SIN_LEN
640  s4->sin_len = (u_char) sizeof (struct sockaddr_in);
641 #endif
642  }
643  else if (1 == inet_pton (AF_INET6,
644  dns_ip,
645  &i6))
646  {
647  struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &ds->ss;
648 
649  s6->sin6_family = AF_INET6;
650  s6->sin6_port = htons (53);
651  s6->sin6_addr = i6;
652 #if HAVE_SOCKADDR_IN_SIN_LEN
653  s6->sin6_len = (u_char) sizeof (struct sockaddr_in6);
654 #endif
655  }
656  else
657  {
659  "Malformed IP address `%s' for DNS server\n",
660  dns_ip);
661  GNUNET_free (ds);
662  return GNUNET_SYSERR;
663  }
665  ctx->dns_tail,
666  ds);
667  return GNUNET_OK;
668 }
struct DnsServer * dns_head
DLL of DNS resolvers we use.
Definition: dnsstub.c:136
#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:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
DNS Server used for resolution.
Definition: dnsstub.c:102
struct DnsServer * dns_tail
DLL of DNS resolvers we use.
Definition: dnsstub.c:141
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static int inet_pton(int af, const char *cp, struct in_addr *buf)
Convert IPv4 address from text to binary form.
#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 call graph for this function:
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 680 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().

682 {
683  struct DnsServer *ds;
684 
685  ds = GNUNET_new (struct DnsServer);
686  switch (sa->sa_family)
687  {
688  case AF_INET:
689  GNUNET_memcpy (&ds->ss,
690  sa,
691  sizeof (struct sockaddr_in));
692  break;
693  case AF_INET6:
694  GNUNET_memcpy (&ds->ss,
695  sa,
696  sizeof (struct sockaddr_in6));
697  break;
698  default:
699  GNUNET_break (0);
700  GNUNET_free (ds);
701  return GNUNET_SYSERR;
702  }
704  ctx->dns_tail,
705  ds);
706  return GNUNET_OK;
707 }
struct DnsServer * dns_head
DLL of DNS resolvers we use.
Definition: dnsstub.c:136
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#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.
struct sockaddr_storage ss
IP address of the DNS resolver.
Definition: dnsstub.c:118
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_memcpy(dst, src, n)
DNS Server used for resolution.
Definition: dnsstub.c:102
struct DnsServer * dns_tail
DLL of DNS resolvers we use.
Definition: dnsstub.c:141
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
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 718 of file dnsstub.c.

References GNUNET_DNSSTUB_Context::retry_freq.

720 {
721  ctx->retry_freq = retry_freq;
722 }
struct GNUNET_TIME_Relative retry_freq
How frequently do we retry requests?
Definition: dnsstub.c:146

◆ GNUNET_DNSSTUB_stop()

void GNUNET_DNSSTUB_stop ( struct GNUNET_DNSSTUB_Context ctx)

Cleanup DNSSTUB resolver.

Parameters
ctxstub resolver to clean up

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

732 {
733  struct DnsServer *ds;
734 
735  while (NULL != (ds = ctx->dns_head))
736  {
738  ctx->dns_tail,
739  ds);
740  GNUNET_free (ds);
741  }
742  for (unsigned int i=0;i<ctx->num_sockets;i++)
743  cleanup_rs (&ctx->sockets[i]);
744  GNUNET_free (ctx->sockets);
745  GNUNET_free (ctx);
746 }
#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:136
unsigned int num_sockets
Length of sockets array.
Definition: dnsstub.c:151
DNS Server used for resolution.
Definition: dnsstub.c:102
struct DnsServer * dns_tail
DLL of DNS resolvers we use.
Definition: dnsstub.c:141
struct GNUNET_DNSSTUB_RequestSocket * sockets
Array of all open sockets for DNS requests.
Definition: dnsstub.c:131
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:162
#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 533 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().

538 {
539  struct GNUNET_DNSSTUB_RequestSocket *rs;
540 
541  if (NULL == ctx->dns_head)
542  {
544  "No DNS server configured for resolution\n");
545  return NULL;
546  }
547  if (NULL == (rs = get_request_socket (ctx)))
548  {
550  "No request socket available for DNS resolution\n");
551  return NULL;
552  }
553  rs->ds_pos = ctx->dns_head;
554  rs->rc = rc;
555  rs->rc_cls = rc_cls;
557  request_len);
558  rs->request_len = request_len;
560  rs);
561  return rs;
562 }
struct DnsServer * dns_head
DLL of DNS resolvers we use.
Definition: dnsstub.c:136
static void transmit_query(void *cls)
Task to (re)transmit the DNS query, possibly repeatedly until we succeed.
Definition: dnsstub.c:459
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:1273
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:248
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:43
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 571 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().

572 {
573  rs->rc = NULL;
574  if (NULL != rs->retry_task)
575  {
577  rs->retry_task = NULL;
578  }
579  if (NULL != rs->read_task)
580  {
582  rs->read_task = NULL;
583  }
584 }
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:965
Here is the call graph for this function:
Here is the caller graph for this function: