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 {
38  struct Request *next;
39 
43  struct Request *prev;
44 
49 
53  void *raw;
54 
58  size_t raw_len;
59 
63  char *hostname;
64 
68  time_t time;
69 
73  int issue_num;
74 
78  uint16_t id;
79 };
80 
81 
85 static struct GNUNET_DNSSTUB_Context *ctx;
86 
90 static unsigned int pending;
91 
95 static unsigned int lookups;
96 
100 static unsigned int failures;
101 
105 static unsigned int records;
106 
110 static struct Request *req_head;
111 
115 static struct Request *req_tail;
116 
120 static struct GNUNET_SCHEDULER_Task *t;
121 
125 #define THRESH 20
126 
131 #define TIME_THRESH 10
132 
136 #define MAX_RETRIES 5
137 
138 
145 static void
147  struct GNUNET_DNSPARSER_Record *rec)
148 {
149  char buf[INET6_ADDRSTRLEN];
150 
151  records++;
152  switch (rec->type)
153  {
155  fprintf(stdout,
156  "%s A %s\n",
157  req->hostname,
158  inet_ntop(AF_INET,
159  rec->data.raw.data,
160  buf,
161  sizeof(buf)));
162  break;
163 
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;
173 
175  fprintf(stdout,
176  "%s NS %s\n",
177  req->hostname,
178  rec->data.hostname);
179  break;
180 
182  fprintf(stdout,
183  "%s CNAME %s\n",
184  req->hostname,
185  rec->data.hostname);
186  break;
187 
189  fprintf(stdout,
190  "%s MX %u %s\n",
191  req->hostname,
192  (unsigned int)rec->data.mx->preference,
193  rec->data.mx->mxhost);
194  break;
195 
197  fprintf(stdout,
198  "%s SOA %s %s %u %u %u %u %u\n",
199  req->hostname,
200  rec->data.soa->mname,
201  rec->data.soa->rname,
202  (unsigned int)rec->data.soa->serial,
203  (unsigned int)rec->data.soa->refresh,
204  (unsigned int)rec->data.soa->retry,
205  (unsigned int)rec->data.soa->expire,
206  (unsigned int)rec->data.soa->minimum_ttl);
207  break;
208 
210  fprintf(stdout,
211  "%s SRV %s %u %u %u\n",
212  req->hostname,
213  rec->data.srv->target,
214  rec->data.srv->priority,
215  rec->data.srv->weight,
216  rec->data.srv->port);
217  break;
218 
220  fprintf(stdout,
221  "%s PTR %s\n",
222  req->hostname,
223  rec->data.hostname);
224  break;
225 
227  fprintf(stdout,
228  "%s TXT %.*s\n",
229  req->hostname,
230  (int)rec->data.raw.data_len,
231  (char *)rec->data.raw.data);
232  break;
233 
235  fprintf(stdout,
236  "%s DNAME %s\n",
237  req->hostname,
238  rec->data.hostname);
239  break;
240 
241  /* obscure records */
253 
254  /* DNSSEC */
263 
264  /* DNSSEC payload */
270 
271  /* obsolete records */
275  {
276  char *base32;
277 
279  rec->data.raw.data_len);
280  fprintf(stdout,
281  "%s (%u) %s\n",
282  req->hostname,
283  rec->type,
284  base32);
285  GNUNET_free(base32);
286  }
287  break;
288 
289  default:
290  fprintf(stderr,
291  "Unsupported type %u\n",
292  (unsigned int)rec->type);
293  break;
294  }
295 }
296 
297 
305 static void
306 process_result(void *cls,
307  const struct GNUNET_TUN_DnsHeader *dns,
308  size_t dns_len)
309 {
310  struct Request *req = cls;
311  struct GNUNET_DNSPARSER_Packet *p;
312 
313  if (NULL == dns)
314  {
315  /* stub gave up */
316  pending--;
318  "Stub gave up on DNS reply for `%s'\n",
319  req->hostname);
321  req_tail,
322  req);
323  if (req->issue_num > MAX_RETRIES)
324  {
325  failures++;
326  GNUNET_free(req->hostname);
327  GNUNET_free(req->raw);
328  GNUNET_free(req);
329  return;
330  }
332  req_tail,
333  req);
334  req->rs = NULL;
335  return;
336  }
337  if (req->id != dns->id)
338  return;
339  pending--;
341  req->rs = NULL;
343  req_tail,
344  req);
345  p = GNUNET_DNSPARSER_parse((const char *)dns,
346  dns_len);
347  if (NULL == p)
348  {
350  "Failed to parse DNS reply for `%s'\n",
351  req->hostname);
352  if (req->issue_num > MAX_RETRIES)
353  {
354  failures++;
355  GNUNET_free(req->hostname);
356  GNUNET_free(req->raw);
357  GNUNET_free(req);
358  return;
359  }
361  req_tail,
362  req);
363  return;
364  }
365  for (unsigned int i = 0; i < p->num_answers; i++)
366  {
367  struct GNUNET_DNSPARSER_Record *rs = &p->answers[i];
368 
369  process_record(req,
370  rs);
371  }
372  for (unsigned int i = 0; i < p->num_authority_records; i++)
373  {
375 
376  process_record(req,
377  rs);
378  }
379  for (unsigned int i = 0; i < p->num_additional_records; i++)
380  {
382 
383  process_record(req,
384  rs);
385  }
387  GNUNET_free(req->hostname);
388  GNUNET_free(req->raw);
389  GNUNET_free(req);
390 }
391 
392 
402 static int
403 submit_req(struct Request *req)
404 {
405  static struct timeval last_request;
406  struct timeval now;
407 
408  if (NULL != req->rs)
409  return GNUNET_NO; /* already submitted */
410  gettimeofday(&now,
411  NULL);
412  if ((((now.tv_sec - last_request.tv_sec) == 0) &&
413  ((now.tv_usec - last_request.tv_usec) < TIME_THRESH)) ||
414  (pending >= THRESH))
415  return GNUNET_SYSERR;
416  GNUNET_assert(NULL == req->rs);
417  req->rs = GNUNET_DNSSTUB_resolve(ctx,
418  req->raw,
419  req->raw_len,
421  req);
422  GNUNET_assert(NULL != req->rs);
423  req->issue_num++;
424  last_request = now;
425  lookups++;
426  pending++;
427  req->time = time(NULL);
428  return GNUNET_OK;
429 }
430 
431 
437 static void
438 process_queue(void *cls)
439 {
440  (void)cls;
441  t = NULL;
442  for (struct Request *req = req_head;
443  NULL != req;
444  req = req->next)
445  {
446  if (GNUNET_SYSERR == submit_req(req))
447  break;
448  }
449  if (NULL != req_head)
451  &process_queue,
452  NULL);
453  else
455 }
456 
457 
463 static void
464 do_shutdown(void *cls)
465 {
466  (void)cls;
467  if (NULL != t)
468  {
470  t = NULL;
471  }
472  GNUNET_DNSSTUB_stop(ctx);
473  ctx = NULL;
474 }
475 
476 
483 static void
484 run(void *cls)
485 {
486  (void)cls;
487 
489  NULL);
491  NULL);
492 }
493 
494 
500 static void
501 queue(const char *hostname)
502 {
503  struct GNUNET_DNSPARSER_Packet p;
504  struct GNUNET_DNSPARSER_Query q;
505  struct Request *req;
506  char *raw;
507  size_t raw_size;
508  int ret;
509 
510  if (GNUNET_OK !=
511  GNUNET_DNSPARSER_check_name(hostname))
512  {
514  "Refusing invalid hostname `%s'\n",
515  hostname);
516  return;
517  }
518  q.name = (char *)hostname;
521 
522  memset(&p,
523  0,
524  sizeof(p));
525  p.num_queries = 1;
526  p.queries = &q;
528  UINT16_MAX);
529  ret = GNUNET_DNSPARSER_pack(&p,
530  UINT16_MAX,
531  &raw,
532  &raw_size);
533  if (GNUNET_OK != ret)
534  {
535  if (GNUNET_NO == ret)
536  GNUNET_free(raw);
538  "Failed to pack query for hostname `%s'\n",
539  hostname);
540  return;
541  }
542 
543  req = GNUNET_new(struct Request);
544  req->hostname = strdup(hostname);
545  req->raw = raw;
546  req->raw_len = raw_size;
547  req->id = p.id;
549  req_tail,
550  req);
551 }
552 
553 
561 int
562 main(int argc,
563  char **argv)
564 {
565  char hn[256];
566 
567  if (2 != argc)
568  {
569  fprintf(stderr,
570  "Missing required configuration argument\n");
571  return -1;
572  }
573  ctx = GNUNET_DNSSTUB_start(256);
574  if (NULL == ctx)
575  {
576  fprintf(stderr,
577  "Failed to initialize GNUnet DNS STUB\n");
578  return 1;
579  }
580  if (GNUNET_OK !=
582  argv[1]))
583  {
584  fprintf(stderr,
585  "Failed to use `%s' for DNS resolver\n",
586  argv[1]);
587  return 1;
588  }
589 
590  while (NULL !=
591  fgets(hn,
592  sizeof(hn),
593  stdin))
594  {
595  if (strlen(hn) > 0)
596  hn[strlen(hn) - 1] = '\0'; /* eat newline */
597  queue(hn);
598  }
600  NULL);
601  fprintf(stderr,
602  "Did %u lookups, found %u records, %u lookups failed, %u pending on shutdown\n",
603  lookups,
604  records,
605  failures,
606  pending);
607  return 0;
608 }
#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:1284
#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:687
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:1256
#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:588
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:707
void GNUNET_DNSPARSER_free_packet(struct GNUNET_DNSPARSER_Packet *p)
Free memory taken by a packet.
Definition: dnsparser.c:853
#define GNUNET_NO
Definition: gnunet_common.h:78
#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.
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:517
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:1237
#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:656
#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:561
#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:1264
#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:121
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:501
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:76
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:79
#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:537
#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:131
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:921
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:44
#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:956
time_t time
When did we last issue this request?