GNUnet  0.10.x
gnunet-zonewalk.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2018 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
26 #include "platform.h"
27 #include <gnunet_util_lib.h>
28 #include <gnunet_dnsstub_lib.h>
29 #include <gnunet_dnsparser_lib.h>
30 
34 struct Request
35 {
39  struct Request *next;
40 
44  struct Request *prev;
45 
50 
54  void *raw;
55 
59  size_t raw_len;
60 
64  char *hostname;
65 
69  time_t time;
70 
74  int issue_num;
75 
79  uint16_t id;
80 };
81 
82 
86 static struct GNUNET_DNSSTUB_Context *ctx;
87 
91 static unsigned int pending;
92 
96 static unsigned int lookups;
97 
101 static unsigned int failures;
102 
106 static unsigned int records;
107 
111 static struct Request *req_head;
112 
116 static struct Request *req_tail;
117 
121 static struct GNUNET_SCHEDULER_Task *t;
122 
126 #define THRESH 20
127 
132 #define TIME_THRESH 10
133 
137 #define MAX_RETRIES 5
138 
139 
146 static void
147 process_record (struct Request *req,
148  struct GNUNET_DNSPARSER_Record *rec)
149 {
150  char buf[INET6_ADDRSTRLEN];
151 
152  records++;
153  switch (rec->type)
154  {
156  fprintf (stdout,
157  "%s A %s\n",
158  req->hostname,
159  inet_ntop (AF_INET,
160  rec->data.raw.data,
161  buf,
162  sizeof (buf)));
163  break;
165  fprintf (stdout,
166  "%s AAAA %s\n",
167  req->hostname,
168  inet_ntop (AF_INET6,
169  rec->data.raw.data,
170  buf,
171  sizeof (buf)));
172  break;
174  fprintf (stdout,
175  "%s NS %s\n",
176  req->hostname,
177  rec->data.hostname);
178  break;
180  fprintf (stdout,
181  "%s CNAME %s\n",
182  req->hostname,
183  rec->data.hostname);
184  break;
186  fprintf (stdout,
187  "%s MX %u %s\n",
188  req->hostname,
189  (unsigned int) rec->data.mx->preference,
190  rec->data.mx->mxhost);
191  break;
193  fprintf (stdout,
194  "%s SOA %s %s %u %u %u %u %u\n",
195  req->hostname,
196  rec->data.soa->mname,
197  rec->data.soa->rname,
198  (unsigned int) rec->data.soa->serial,
199  (unsigned int) rec->data.soa->refresh,
200  (unsigned int) rec->data.soa->retry,
201  (unsigned int) rec->data.soa->expire,
202  (unsigned int) rec->data.soa->minimum_ttl);
203  break;
205  fprintf (stdout,
206  "%s SRV %s %u %u %u\n",
207  req->hostname,
208  rec->data.srv->target,
209  rec->data.srv->priority,
210  rec->data.srv->weight,
211  rec->data.srv->port);
212  break;
214  fprintf (stdout,
215  "%s PTR %s\n",
216  req->hostname,
217  rec->data.hostname);
218  break;
220  fprintf (stdout,
221  "%s TXT %.*s\n",
222  req->hostname,
223  (int) rec->data.raw.data_len,
224  (char *) rec->data.raw.data);
225  break;
227  fprintf (stdout,
228  "%s DNAME %s\n",
229  req->hostname,
230  rec->data.hostname);
231  break;
232 
233  /* obscure records */
245 
246  /* DNSSEC */
255 
256  /* DNSSEC payload */
262 
263  /* obsolete records */
267  {
268  char *base32;
269 
271  rec->data.raw.data_len);
272  fprintf (stdout,
273  "%s (%u) %s\n",
274  req->hostname,
275  rec->type,
276  base32);
277  GNUNET_free (base32);
278  }
279  break;
280  default:
281  fprintf (stderr,
282  "Unsupported type %u\n",
283  (unsigned int) rec->type);
284  break;
285  }
286 }
287 
288 
296 static void
297 process_result (void *cls,
298  const struct GNUNET_TUN_DnsHeader *dns,
299  size_t dns_len)
300 {
301  struct Request *req = cls;
302  struct GNUNET_DNSPARSER_Packet *p;
303 
304  if (NULL == dns)
305  {
306  /* stub gave up */
307  pending--;
309  "Stub gave up on DNS reply for `%s'\n",
310  req->hostname);
311  GNUNET_CONTAINER_DLL_remove (req_head,
312  req_tail,
313  req);
314  if (req->issue_num > MAX_RETRIES)
315  {
316  failures++;
317  GNUNET_free (req->hostname);
318  GNUNET_free (req->raw);
319  GNUNET_free (req);
320  return;
321  }
323  req_tail,
324  req);
325  req->rs = NULL;
326  return;
327  }
328  if (req->id != dns->id)
329  return;
330  pending--;
332  req->rs = NULL;
333  GNUNET_CONTAINER_DLL_remove (req_head,
334  req_tail,
335  req);
336  p = GNUNET_DNSPARSER_parse ((const char *) dns,
337  dns_len);
338  if (NULL == p)
339  {
341  "Failed to parse DNS reply for `%s'\n",
342  req->hostname);
343  if (req->issue_num > MAX_RETRIES)
344  {
345  failures++;
346  GNUNET_free (req->hostname);
347  GNUNET_free (req->raw);
348  GNUNET_free (req);
349  return;
350  }
352  req_tail,
353  req);
354  return;
355  }
356  for (unsigned int i=0;i<p->num_answers;i++)
357  {
358  struct GNUNET_DNSPARSER_Record *rs = &p->answers[i];
359 
360  process_record (req,
361  rs);
362  }
363  for (unsigned int i=0;i<p->num_authority_records;i++)
364  {
366 
367  process_record (req,
368  rs);
369  }
370  for (unsigned int i=0;i<p->num_additional_records;i++)
371  {
373 
374  process_record (req,
375  rs);
376  }
378  GNUNET_free (req->hostname);
379  GNUNET_free (req->raw);
380  GNUNET_free (req);
381 }
382 
383 
393 static int
394 submit_req (struct Request *req)
395 {
396  static struct timeval last_request;
397  struct timeval now;
398 
399  if (NULL != req->rs)
400  return GNUNET_NO; /* already submitted */
401  gettimeofday (&now,
402  NULL);
403  if ( ( ( (now.tv_sec - last_request.tv_sec) == 0) &&
404  ( (now.tv_usec - last_request.tv_usec) < TIME_THRESH) ) ||
405  (pending >= THRESH) )
406  return GNUNET_SYSERR;
407  GNUNET_assert (NULL == req->rs);
408  req->rs = GNUNET_DNSSTUB_resolve (ctx,
409  req->raw,
410  req->raw_len,
412  req);
413  GNUNET_assert (NULL != req->rs);
414  req->issue_num++;
415  last_request = now;
416  lookups++;
417  pending++;
418  req->time = time (NULL);
419  return GNUNET_OK;
420 }
421 
422 
428 static void
429 process_queue(void *cls)
430 {
431  (void) cls;
432  t = NULL;
433  for (struct Request *req = req_head;
434  NULL != req;
435  req = req->next)
436  {
437  if (GNUNET_SYSERR == submit_req (req))
438  break;
439  }
440  if (NULL != req_head)
442  &process_queue,
443  NULL);
444  else
446 }
447 
448 
454 static void
455 do_shutdown (void *cls)
456 {
457  (void) cls;
458  if (NULL != t)
459  {
461  t = NULL;
462  }
463  GNUNET_DNSSTUB_stop (ctx);
464  ctx = NULL;
465 }
466 
467 
474 static void
475 run (void *cls)
476 {
477  (void) cls;
478 
480  NULL);
482  NULL);
483 }
484 
485 
491 static void
492 queue (const char *hostname)
493 {
494  struct GNUNET_DNSPARSER_Packet p;
495  struct GNUNET_DNSPARSER_Query q;
496  struct Request *req;
497  char *raw;
498  size_t raw_size;
499  int ret;
500 
501  if (GNUNET_OK !=
502  GNUNET_DNSPARSER_check_name (hostname))
503  {
505  "Refusing invalid hostname `%s'\n",
506  hostname);
507  return;
508  }
509  q.name = (char *) hostname;
512 
513  memset (&p,
514  0,
515  sizeof (p));
516  p.num_queries = 1;
517  p.queries = &q;
519  UINT16_MAX);
520  ret = GNUNET_DNSPARSER_pack (&p,
521  UINT16_MAX,
522  &raw,
523  &raw_size);
524  if (GNUNET_OK != ret)
525  {
526  if (GNUNET_NO == ret)
527  GNUNET_free (raw);
529  "Failed to pack query for hostname `%s'\n",
530  hostname);
531  return;
532  }
533 
534  req = GNUNET_new (struct Request);
535  req->hostname = strdup (hostname);
536  req->raw = raw;
537  req->raw_len = raw_size;
538  req->id = p.id;
540  req_tail,
541  req);
542 }
543 
544 
552 int
553 main (int argc,
554  char **argv)
555 {
556  char hn[256];
557 
558  if (2 != argc)
559  {
560  fprintf (stderr,
561  "Missing required configuration argument\n");
562  return -1;
563  }
564  ctx = GNUNET_DNSSTUB_start (256);
565  if (NULL == ctx)
566  {
567  fprintf (stderr,
568  "Failed to initialize GNUnet DNS STUB\n");
569  return 1;
570  }
571  if (GNUNET_OK !=
573  argv[1]))
574  {
575  fprintf (stderr,
576  "Failed to use `%s' for DNS resolver\n",
577  argv[1]);
578  return 1;
579  }
580 
581  while (NULL !=
582  fgets (hn,
583  sizeof (hn),
584  stdin))
585  {
586  if (strlen(hn) > 0)
587  hn[strlen(hn)-1] = '\0'; /* eat newline */
588  queue (hn);
589  }
591  NULL);
592  fprintf (stderr,
593  "Did %u lookups, found %u records, %u lookups failed, %u pending on shutdown\n",
594  lookups,
595  records,
596  failures,
597  pending);
598  return 0;
599 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static void queue(const char *hostname)
Add hostname to the list of requests to be made.
struct GNUNET_DNSPARSER_Record * answers
Array of all answers in the packet, must contain "num_answers" entries.
static unsigned int lookups
Number of lookups we performed overall.
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
#define GNUNET_DNSPARSER_TYPE_SIG
#define GNUNET_DNSPARSER_TYPE_AFSDB
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
uint32_t retry
Time interval that should elapse before a failed refresh should be retried.
uint32_t refresh
Time interval before the zone should be refreshed.
int main(int argc, char **argv)
Call with IP address of resolver to query.
static void do_shutdown(void *cls)
Clean up and terminate the process.
static void process_result(void *cls, const struct GNUNET_TUN_DnsHeader *dns, size_t dns_len)
Function called with the result of a DNS resolution.
char * mname
The domainname of the name server that was the original or primary source of data for this zone...
char * hostname
Hostname we are resolving.
#define GNUNET_DNSPARSER_TYPE_KX
#define GNUNET_DNSPARSER_TYPE_TKEY
#define GNUNET_DNSPARSER_TYPE_CNAME
char * mxhost
Name of the mail server.
struct GNUNET_DNSPARSER_SrvRecord * srv
SRV data for SRV records.
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
#define GNUNET_DNSPARSER_TYPE_SSHFP
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_DNSSTUB_stop(struct GNUNET_DNSSTUB_Context *ctx)
Cleanup DNSSTUB resolver.
Definition: dnsstub.c:731
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_DNSPARSER_TYPE_APL
uint16_t id
random 16-bit DNS query identifier.
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
uint16_t id
DNS ID (to match replies to requests).
void GNUNET_SCHEDULER_run(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Initialize and run scheduler.
Definition: scheduler.c:716
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
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static struct GNUNET_SCHEDULER_Task * t
Main task.
struct GNUNET_DNSPARSER_Record * additional_records
Array of all additional answers in the packet, must contain "num_additional_records" entries...
#define GNUNET_DNSPARSER_TYPE_LOC
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_DNSPARSER_TYPE_NSEC3PARAM
#define GNUNET_DNSPARSER_TYPE_MX
unsigned int num_answers
Number of answers in the packet, should be 0 for queries.
uint16_t id
Unique identifier for the request/response.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
#define GNUNET_DNSPARSER_TYPE_HIP
uint32_t serial
The version number of the original copy of the zone.
static void process_queue(void *cls)
Process as many requests as possible from the queue.
#define GNUNET_DNSPARSER_TYPE_PTR
static unsigned int records
Number of records we found.
uint16_t dns_traffic_class
See GNUNET_TUN_DNS_CLASS_*.
uint32_t minimum_ttl
The bit minimum TTL field that should be exported with any RR from this zone.
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
#define GNUNET_DNSPARSER_TYPE_SOA
char * hostname
For NS, CNAME and PTR records, this is the uncompressed 0-terminated hostname.
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
struct GNUNET_DNSPARSER_SoaRecord * soa
SOA data for SOA records.
unsigned int num_additional_records
Number of additional records in the packet, should be 0 for queries.
#define GNUNET_DNSPARSER_TYPE_RP
static int submit_req(struct Request *req)
Submit a request to DNS unless we need to slow down because we are at the rate limit.
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
#define TIME_THRESH
TIME_THRESH is in usecs.
uint16_t port
TCP or UDP port of the service.
struct GNUNET_DNSSTUB_Context * GNUNET_DNSSTUB_start(unsigned int num_sockets)
Start a DNS stub resolver.
Definition: dnsstub.c:595
#define GNUNET_DNSPARSER_TYPE_TA
#define GNUNET_DNSPARSER_TYPE_DNAME
static void run(void *cls)
Process requests from the queue, then if the queue is not empty, try again.
char * target
Hostname offering the service.
#define THRESH
Maximum number of queries pending at the same time.
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
#define MAX_RETRIES
How often do we retry a query before giving up for good?
struct GNUNET_CONTAINER_HeapNode * hn
Requests are kept in a heap while waiting to be resolved.
#define GNUNET_DNSPARSER_TYPE_DS
char * rname
A domainname which specifies the mailbox of the person responsible for this zone. ...
Randomness for IVs etc.
static char buf[2048]
A DNS response record.
struct GNUNET_DNSPARSER_Record * authority_records
Array of all authority records in the packet, must contain "num_authority_records" entries...
Handle to the stub resolver.
Definition: dnsstub.c:125
uint32_t expire
Time value that specifies the upper limit on the time interval that can elapse before the zone is no ...
struct GNUNET_DNSPARSER_Query * queries
Array of all queries in the packet, must contain "num_queries" entries.
#define GNUNET_TIME_UNIT_MILLISECONDS
One millisecond.
#define GNUNET_DNSPARSER_TYPE_RRSIG
static struct GNUNET_REVOCATION_Query * q
Handle for revocation query.
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
struct Request * prev
Requests are kept in a DLL.
#define GNUNET_DNSPARSER_TYPE_CDNSKEY
#define GNUNET_DNSPARSER_TYPE_IPSECKEY
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
size_t data_len
Number of bytes in data.
void * data
Binary record data.
int issue_num
How often did we issue this query?
#define GNUNET_DNSPARSER_TYPE_TXT
int GNUNET_DNSPARSER_check_name(const char *name)
Check if a hostname in UTF-8 format can be coded into valid IDNA.
Definition: dnsparser.c:86
#define GNUNET_DNSPARSER_TYPE_NAPTR
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
unsigned int num_authority_records
Number of authoritative answers in the packet, should be 0 for queries.
static unsigned int pending
The number of queries that are outstanding.
#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...
#define GNUNET_DNSPARSER_TYPE_OPENPGPKEY
#define GNUNET_DNSPARSER_TYPE_URI
#define GNUNET_DNSPARSER_TYPE_TLSA
Request we should make.
struct GNUNET_DNSSTUB_RequestSocket * rs
Socket used to make the request, NULL if not active.
void * raw
Raw DNS query.
struct GNUNET_DNSPARSER_MxRecord * mx
MX data for MX records.
#define GNUNET_DNSPARSER_TYPE_KEY
#define GNUNET_DNSPARSER_TYPE_AAAA
void GNUNET_DNSSTUB_resolve_cancel(struct GNUNET_DNSSTUB_RequestSocket *rs)
Cancel DNS resolution.
Definition: dnsstub.c:571
#define GNUNET_DNSPARSER_TYPE_NS
#define GNUNET_DNSPARSER_TYPE_NSEC
Easy-to-process, parsed version of a DNS packet.
static unsigned int failures
Number of lookups that failed.
#define GNUNET_DNSPARSER_TYPE_CDS
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
struct Request * next
Requests are kept in a DLL.
#define GNUNET_DNSPARSER_TYPE_NSEC3
uint16_t priority
Preference for this entry (lower value is higher preference).
static struct Request * req_tail
Tail of DLL of all requests to perform.
char * name
Name of the record that the query is for (0-terminated).
unsigned int num_queries
Number of queries in the packet.
uint16_t weight
Relative weight for records with the same priority.
#define GNUNET_DNSPARSER_TYPE_DHCID
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:988
union GNUNET_DNSPARSER_Record::@27 data
Payload of the record (which one of these is valid depends on the &#39;type&#39;).
size_t raw_len
Number of bytes in raw.
UDP socket we are using for sending DNS requests to the Internet.
Definition: dnsstub.c:43
#define GNUNET_DNSPARSER_TYPE_SRV
static void process_record(struct Request *req, struct GNUNET_DNSPARSER_Record *rec)
We received rec for req.
uint16_t preference
Preference for this entry (lower value is higher preference).
#define GNUNET_DNSPARSER_TYPE_TSIG
struct GNUNET_DNSPARSER_RawRecord raw
Raw data for all other types.
#define GNUNET_DNSPARSER_TYPE_A
static struct Request * req_head
Head of DLL of all requests to perform.
#define GNUNET_DNSPARSER_TYPE_CERT
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_DNSPARSER_TYPE_DNSKEY
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965
time_t time
When did we last issue this request?