2 This file is part of GNUnet.
3 Copyright (C) 2012, 2018 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @brief DNS stub resolver which sends DNS requests to an actual resolver
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
29 * Timeout for retrying DNS queries.
31 #define DNS_RETRANSMIT_DELAY \
32 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
36 * DNS Server used for resolution.
42 * UDP socket we are using for sending DNS requests to the Internet.
44 struct GNUNET_DNSSTUB_RequestSocket
47 * UDP socket we use for this request for IPv4
49 struct GNUNET_NETWORK_Handle *dnsout4;
52 * UDP socket we use for this request for IPv6
54 struct GNUNET_NETWORK_Handle *dnsout6;
57 * Function to call with result.
59 GNUNET_DNSSTUB_ResultCallback rc;
67 * Task for reading from dnsout4 and dnsout6.
69 struct GNUNET_SCHEDULER_Task *read_task;
72 * Task for retrying transmission of the query.
74 struct GNUNET_SCHEDULER_Task *retry_task;
77 * Next address we sent the DNS request to.
79 struct DnsServer *ds_pos;
82 * Context this request executes in.
84 struct GNUNET_DNSSTUB_Context *ctx;
87 * Query we sent to @e addr.
92 * Number of bytes in @a request.
99 * DNS Server used for resolution.
106 struct DnsServer *next;
111 struct DnsServer *prev;
114 * IP address of the DNS resolver.
116 struct sockaddr_storage ss;
121 * Handle to the stub resolver.
123 struct GNUNET_DNSSTUB_Context
126 * Array of all open sockets for DNS requests.
128 struct GNUNET_DNSSTUB_RequestSocket *sockets;
131 * DLL of DNS resolvers we use.
133 struct DnsServer *dns_head;
136 * DLL of DNS resolvers we use.
138 struct DnsServer *dns_tail;
141 * How frequently do we retry requests?
143 struct GNUNET_TIME_Relative retry_freq;
146 * Length of @e sockets array.
148 unsigned int num_sockets;
153 * We're done with a `struct GNUNET_DNSSTUB_RequestSocket`, close it for now.
155 * @param rs request socket to clean up
158 cleanup_rs (struct GNUNET_DNSSTUB_RequestSocket *rs)
160 if (NULL != rs->dnsout4)
162 GNUNET_NETWORK_socket_close (rs->dnsout4);
165 if (NULL != rs->dnsout6)
167 GNUNET_NETWORK_socket_close (rs->dnsout6);
170 if (NULL != rs->read_task)
172 GNUNET_SCHEDULER_cancel (rs->read_task);
173 rs->read_task = NULL;
175 if (NULL != rs->retry_task)
177 GNUNET_SCHEDULER_cancel (rs->retry_task);
178 rs->retry_task = NULL;
180 if (NULL != rs->request)
182 GNUNET_free (rs->request);
189 * Open source port for sending DNS requests
191 * @param af AF_INET or AF_INET6
192 * @return #GNUNET_OK on success
194 static struct GNUNET_NETWORK_Handle *
197 struct sockaddr_in a4;
198 struct sockaddr_in6 a6;
201 struct GNUNET_NETWORK_Handle *ret;
203 ret = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, 0);
209 memset (&a4, 0, alen = sizeof(struct sockaddr_in));
210 sa = (struct sockaddr *) &a4;
214 memset (&a6, 0, alen = sizeof(struct sockaddr_in6));
215 sa = (struct sockaddr *) &a6;
220 GNUNET_NETWORK_socket_close (ret);
224 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret, sa, alen))
226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227 _ ("Could not bind to any port: %s\n"),
229 GNUNET_NETWORK_socket_close (ret);
237 * Get a socket of the specified address family to send out a
238 * UDP DNS request to the Internet.
240 * @param ctx the DNSSTUB context
241 * @return NULL on error
243 static struct GNUNET_DNSSTUB_RequestSocket *
244 get_request_socket (struct GNUNET_DNSSTUB_Context *ctx)
246 struct GNUNET_DNSSTUB_RequestSocket *rs;
248 for (unsigned int i = 0; i < 256; i++)
250 rs = &ctx->sockets[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
257 /* signal "failure" */
258 rs->rc (rs->rc_cls, NULL, 0);
261 if (NULL != rs->read_task)
263 GNUNET_SCHEDULER_cancel (rs->read_task);
264 rs->read_task = NULL;
266 if (NULL != rs->retry_task)
268 GNUNET_SCHEDULER_cancel (rs->retry_task);
269 rs->retry_task = NULL;
271 if (NULL != rs->request)
273 GNUNET_free (rs->request);
282 * Actually do the reading of a DNS packet from our UDP socket and see
283 * if we have a valid, matching, pending request.
285 * @param rs request socket with callback details
286 * @param dnsout socket to read from
287 * @return #GNUNET_OK on success, #GNUNET_NO on drop, #GNUNET_SYSERR on IO-errors (closed socket)
290 do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
291 struct GNUNET_NETWORK_Handle *dnsout)
293 struct GNUNET_DNSSTUB_Context *ctx = rs->ctx;
297 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
299 /* conservative choice: */
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving %d byte DNS reply\n", len);
305 unsigned char buf[len] GNUNET_ALIGN;
307 struct sockaddr_storage addr;
309 struct GNUNET_TUN_DnsHeader *dns;
311 addrlen = sizeof(addr);
312 memset (&addr, 0, sizeof(addr));
313 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
316 (struct sockaddr *) &addr,
320 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
321 GNUNET_NETWORK_socket_close (dnsout);
322 return GNUNET_SYSERR;
325 for (struct DnsServer *ds = ctx->dns_head; NULL != ds; ds = ds->next)
327 if (0 == memcmp (&addr,
329 GNUNET_MIN (sizeof(struct sockaddr_storage), addrlen)))
335 if (GNUNET_NO == found)
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Received DNS response from server we never asked (ignored)");
341 if (sizeof(struct GNUNET_TUN_DnsHeader) > (size_t) r)
343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
344 _ ("Received DNS response that is too small (%u bytes)"),
348 dns = (struct GNUNET_TUN_DnsHeader *) buf;
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Request timeout or cancelled; ignoring reply\n");
355 rs->rc (rs->rc_cls, dns, r);
362 * Read a DNS response from the (unhindered) UDP-Socket
364 * @param cls socket to read from
367 read_response (void *cls);
371 * Schedule #read_response() task for @a rs.
373 * @param rs request to schedule read operation for
376 schedule_read (struct GNUNET_DNSSTUB_RequestSocket *rs)
378 struct GNUNET_NETWORK_FDSet *rset;
380 if (NULL != rs->read_task)
381 GNUNET_SCHEDULER_cancel (rs->read_task);
382 rset = GNUNET_NETWORK_fdset_create ();
383 if (NULL != rs->dnsout4)
384 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
385 if (NULL != rs->dnsout6)
386 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
388 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
389 GNUNET_TIME_UNIT_FOREVER_REL,
394 GNUNET_NETWORK_fdset_destroy (rset);
399 * Read a DNS response from the (unhindered) UDP-Socket
401 * @param cls `struct GNUNET_DNSSTUB_RequestSocket` to read from
404 read_response (void *cls)
406 struct GNUNET_DNSSTUB_RequestSocket *rs = cls;
407 const struct GNUNET_SCHEDULER_TaskContext *tc;
409 rs->read_task = NULL;
410 tc = GNUNET_SCHEDULER_get_task_context ();
411 /* read and process ready sockets */
412 if ((NULL != rs->dnsout4) &&
413 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout4)) &&
414 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout4)))
416 if ((NULL != rs->dnsout6) &&
417 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout6)) &&
418 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout6)))
420 /* re-schedule read task */
426 * Task to (re)transmit the DNS query, possibly repeatedly until
429 * @param cls our `struct GNUNET_DNSSTUB_RequestSocket *`
432 transmit_query (void *cls)
434 struct GNUNET_DNSSTUB_RequestSocket *rs = cls;
435 struct GNUNET_DNSSTUB_Context *ctx = rs->ctx;
436 const struct sockaddr *sa;
438 struct DnsServer *ds;
439 struct GNUNET_NETWORK_Handle *dnsout;
442 GNUNET_SCHEDULER_add_delayed (ctx->retry_freq, &transmit_query, rs);
444 rs->ds_pos = ds->next;
445 if (NULL == rs->ds_pos)
446 rs->ds_pos = ctx->dns_head;
447 GNUNET_assert (NULL != ds);
449 switch (ds->ss.ss_family)
452 if (NULL == rs->dnsout4)
453 rs->dnsout4 = open_socket (AF_INET);
454 dnsout = rs->dnsout4;
455 sa = (const struct sockaddr *) &ds->ss;
456 salen = sizeof(struct sockaddr_in);
460 if (NULL == rs->dnsout6)
461 rs->dnsout6 = open_socket (AF_INET6);
462 dnsout = rs->dnsout6;
463 sa = (const struct sockaddr *) &ds->ss;
464 salen = sizeof(struct sockaddr_in6);
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
473 "Unable to use configure DNS server, skipping\n");
476 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_sendto (dnsout,
481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
482 _ ("Failed to send DNS request to %s: %s\n"),
483 GNUNET_a2s (sa, salen),
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 _ ("Sent DNS request to %s\n"),
488 GNUNET_a2s (sa, salen));
494 * Perform DNS resolution using our default IP from init.
496 * @param ctx stub resolver to use
497 * @param request DNS request to transmit
498 * @param request_len number of bytes in msg
499 * @param rc function to call with result
500 * @param rc_cls closure for 'rc'
501 * @return socket used for the request, NULL on error
503 struct GNUNET_DNSSTUB_RequestSocket *
504 GNUNET_DNSSTUB_resolve (struct GNUNET_DNSSTUB_Context *ctx,
507 GNUNET_DNSSTUB_ResultCallback rc,
510 struct GNUNET_DNSSTUB_RequestSocket *rs;
512 if (NULL == ctx->dns_head)
514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
515 "No DNS server configured for resolution\n");
518 if (NULL == (rs = get_request_socket (ctx)))
520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
521 "No request socket available for DNS resolution\n");
524 rs->ds_pos = ctx->dns_head;
527 rs->request = GNUNET_memdup (request, request_len);
528 rs->request_len = request_len;
529 rs->retry_task = GNUNET_SCHEDULER_add_now (&transmit_query, rs);
535 * Cancel DNS resolution.
537 * @param rs resolution to cancel
540 GNUNET_DNSSTUB_resolve_cancel (struct GNUNET_DNSSTUB_RequestSocket *rs)
543 if (NULL != rs->retry_task)
545 GNUNET_SCHEDULER_cancel (rs->retry_task);
546 rs->retry_task = NULL;
548 if (NULL != rs->read_task)
550 GNUNET_SCHEDULER_cancel (rs->read_task);
551 rs->read_task = NULL;
557 * Start a DNS stub resolver.
559 * @param num_sockets how many sockets should we open
560 * in parallel for DNS queries for this stub?
561 * @return NULL on error
563 struct GNUNET_DNSSTUB_Context *
564 GNUNET_DNSSTUB_start (unsigned int num_sockets)
566 struct GNUNET_DNSSTUB_Context *ctx;
568 if (0 == num_sockets)
573 ctx = GNUNET_new (struct GNUNET_DNSSTUB_Context);
574 ctx->num_sockets = num_sockets;
576 GNUNET_new_array (num_sockets, struct GNUNET_DNSSTUB_RequestSocket);
577 ctx->retry_freq = DNS_RETRANSMIT_DELAY;
583 * Add nameserver for use by the DNSSTUB. We will use
584 * all provided nameservers for resolution (round-robin).
586 * @param ctx resolver context to modify
587 * @param dns_ip target IP address to use (as string)
588 * @return #GNUNET_OK on success
591 GNUNET_DNSSTUB_add_dns_ip (struct GNUNET_DNSSTUB_Context *ctx,
594 struct DnsServer *ds;
598 ds = GNUNET_new (struct DnsServer);
599 if (1 == inet_pton (AF_INET, dns_ip, &i4))
601 struct sockaddr_in *s4 = (struct sockaddr_in *) &ds->ss;
603 s4->sin_family = AF_INET;
604 s4->sin_port = htons (53);
606 #if HAVE_SOCKADDR_IN_SIN_LEN
607 s4->sin_len = (u_char) sizeof(struct sockaddr_in);
610 else if (1 == inet_pton (AF_INET6, dns_ip, &i6))
612 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &ds->ss;
614 s6->sin6_family = AF_INET6;
615 s6->sin6_port = htons (53);
617 #if HAVE_SOCKADDR_IN_SIN_LEN
618 s6->sin6_len = (u_char) sizeof(struct sockaddr_in6);
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624 "Malformed IP address `%s' for DNS server\n",
627 return GNUNET_SYSERR;
629 GNUNET_CONTAINER_DLL_insert (ctx->dns_head, ctx->dns_tail, ds);
635 * Add nameserver for use by the DNSSTUB. We will use
636 * all provided nameservers for resolution (round-robin).
638 * @param ctx resolver context to modify
639 * @param sa socket address of DNS resolver to use
640 * @return #GNUNET_OK on success
643 GNUNET_DNSSTUB_add_dns_sa (struct GNUNET_DNSSTUB_Context *ctx,
644 const struct sockaddr *sa)
646 struct DnsServer *ds;
648 ds = GNUNET_new (struct DnsServer);
649 switch (sa->sa_family)
652 GNUNET_memcpy (&ds->ss, sa, sizeof(struct sockaddr_in));
656 GNUNET_memcpy (&ds->ss, sa, sizeof(struct sockaddr_in6));
662 return GNUNET_SYSERR;
664 GNUNET_CONTAINER_DLL_insert (ctx->dns_head, ctx->dns_tail, ds);
670 * How long should we try requests before timing out?
671 * Only effective for requests issued after this call.
673 * @param ctx resolver context to modify
674 * @param retry_freq how long to wait between retries
677 GNUNET_DNSSTUB_set_retry (struct GNUNET_DNSSTUB_Context *ctx,
678 struct GNUNET_TIME_Relative retry_freq)
680 ctx->retry_freq = retry_freq;
685 * Cleanup DNSSTUB resolver.
687 * @param ctx stub resolver to clean up
690 GNUNET_DNSSTUB_stop (struct GNUNET_DNSSTUB_Context *ctx)
692 struct DnsServer *ds;
694 while (NULL != (ds = ctx->dns_head))
696 GNUNET_CONTAINER_DLL_remove (ctx->dns_head, ctx->dns_tail, ds);
699 for (unsigned int i = 0; i < ctx->num_sockets; i++)
700 cleanup_rs (&ctx->sockets[i]);
701 GNUNET_free (ctx->sockets);
706 /* end of dnsstub.c */