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/>.
20 * @brief DNS stub resolver which sends DNS requests to an actual resolver
21 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
27 * Timeout for retrying DNS queries.
29 #define DNS_RETRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
33 * DNS Server used for resolution.
39 * UDP socket we are using for sending DNS requests to the Internet.
41 struct GNUNET_DNSSTUB_RequestSocket
45 * UDP socket we use for this request for IPv4
47 struct GNUNET_NETWORK_Handle *dnsout4;
50 * UDP socket we use for this request for IPv6
52 struct GNUNET_NETWORK_Handle *dnsout6;
55 * Function to call with result.
57 GNUNET_DNSSTUB_ResultCallback rc;
65 * Task for reading from dnsout4 and dnsout6.
67 struct GNUNET_SCHEDULER_Task *read_task;
70 * Task for retrying transmission of the query.
72 struct GNUNET_SCHEDULER_Task *retry_task;
75 * Next address we sent the DNS request to.
77 struct DnsServer *ds_pos;
80 * Context this request executes in.
82 struct GNUNET_DNSSTUB_Context *ctx;
85 * Query we sent to @e addr.
90 * Number of bytes in @a request.
98 * 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
127 * Array of all open sockets for DNS requests.
129 struct GNUNET_DNSSTUB_RequestSocket *sockets;
132 * DLL of DNS resolvers we use.
134 struct DnsServer *dns_head;
137 * DLL of DNS resolvers we use.
139 struct DnsServer *dns_tail;
142 * How frequently do we retry requests?
144 struct GNUNET_TIME_Relative retry_freq;
147 * Length of @e sockets array.
149 unsigned int num_sockets;
155 * We're done with a `struct GNUNET_DNSSTUB_RequestSocket`, close it for now.
157 * @param rs request socket to clean up
160 cleanup_rs (struct GNUNET_DNSSTUB_RequestSocket *rs)
162 if (NULL != rs->dnsout4)
164 GNUNET_NETWORK_socket_close (rs->dnsout4);
167 if (NULL != rs->dnsout6)
169 GNUNET_NETWORK_socket_close (rs->dnsout6);
172 if (NULL != rs->read_task)
174 GNUNET_SCHEDULER_cancel (rs->read_task);
175 rs->read_task = NULL;
177 if (NULL != rs->retry_task)
179 GNUNET_SCHEDULER_cancel (rs->retry_task);
180 rs->retry_task = NULL;
182 if (NULL != rs->request)
184 GNUNET_free (rs->request);
191 * Open source port for sending DNS requests
193 * @param af AF_INET or AF_INET6
194 * @return #GNUNET_OK on success
196 static struct GNUNET_NETWORK_Handle *
199 struct sockaddr_in a4;
200 struct sockaddr_in6 a6;
203 struct GNUNET_NETWORK_Handle *ret;
205 ret = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, 0);
211 memset (&a4, 0, alen = sizeof (struct sockaddr_in));
212 sa = (struct sockaddr *) &a4;
215 memset (&a6, 0, alen = sizeof (struct sockaddr_in6));
216 sa = (struct sockaddr *) &a6;
220 GNUNET_NETWORK_socket_close (ret);
224 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret,
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
229 _("Could not bind to any port: %s\n"),
231 GNUNET_NETWORK_socket_close (ret);
239 * Get a socket of the specified address family to send out a
240 * UDP DNS request to the Internet.
242 * @param ctx the DNSSTUB context
243 * @return NULL on error
245 static struct GNUNET_DNSSTUB_RequestSocket *
246 get_request_socket (struct GNUNET_DNSSTUB_Context *ctx)
248 struct GNUNET_DNSSTUB_RequestSocket *rs;
250 for (unsigned int i=0;i<256;i++)
252 rs = &ctx->sockets[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
259 /* signal "failure" */
265 if (NULL != rs->read_task)
267 GNUNET_SCHEDULER_cancel (rs->read_task);
268 rs->read_task = NULL;
270 if (NULL != rs->retry_task)
272 GNUNET_SCHEDULER_cancel (rs->retry_task);
273 rs->retry_task = NULL;
275 if (NULL != rs->request)
277 GNUNET_free (rs->request);
286 * Actually do the reading of a DNS packet from our UDP socket and see
287 * if we have a valid, matching, pending request.
289 * @param rs request socket with callback details
290 * @param dnsout socket to read from
291 * @return #GNUNET_OK on success, #GNUNET_NO on drop, #GNUNET_SYSERR on IO-errors (closed socket)
294 do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
295 struct GNUNET_NETWORK_Handle *dnsout)
297 struct GNUNET_DNSSTUB_Context *ctx = rs->ctx;
302 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout),
306 /* conservative choice: */
310 /* port the code above? */
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Receiving %d byte DNS reply\n",
317 unsigned char buf[len] GNUNET_ALIGN;
319 struct sockaddr_storage addr;
321 struct GNUNET_TUN_DnsHeader *dns;
323 addrlen = sizeof (addr);
327 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
330 (struct sockaddr*) &addr,
334 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
336 GNUNET_NETWORK_socket_close (dnsout);
337 return GNUNET_SYSERR;
340 for (struct DnsServer *ds = ctx->dns_head; NULL != ds; ds = ds->next)
342 if (0 == memcmp (&addr,
344 GNUNET_MIN (sizeof (struct sockaddr_storage),
351 if (GNUNET_NO == found)
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Received DNS response from server we never asked (ignored)");
357 if (sizeof (struct GNUNET_TUN_DnsHeader) > (size_t) r)
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 _("Received DNS response that is too small (%u bytes)"),
364 dns = (struct GNUNET_TUN_DnsHeader *) buf;
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "Request timeout or cancelled; ignoring reply\n");
380 * Read a DNS response from the (unhindered) UDP-Socket
382 * @param cls socket to read from
385 read_response (void *cls);
389 * Schedule #read_response() task for @a rs.
391 * @param rs request to schedule read operation for
394 schedule_read (struct GNUNET_DNSSTUB_RequestSocket *rs)
396 struct GNUNET_NETWORK_FDSet *rset;
398 if (NULL != rs->read_task)
399 GNUNET_SCHEDULER_cancel (rs->read_task);
400 rset = GNUNET_NETWORK_fdset_create ();
401 if (NULL != rs->dnsout4)
402 GNUNET_NETWORK_fdset_set (rset,
404 if (NULL != rs->dnsout6)
405 GNUNET_NETWORK_fdset_set (rset,
407 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
408 GNUNET_TIME_UNIT_FOREVER_REL,
413 GNUNET_NETWORK_fdset_destroy (rset);
418 * Read a DNS response from the (unhindered) UDP-Socket
420 * @param cls `struct GNUNET_DNSSTUB_RequestSocket` to read from
423 read_response (void *cls)
425 struct GNUNET_DNSSTUB_RequestSocket *rs = cls;
426 const struct GNUNET_SCHEDULER_TaskContext *tc;
428 rs->read_task = NULL;
429 tc = GNUNET_SCHEDULER_get_task_context ();
430 /* read and process ready sockets */
431 if ( (NULL != rs->dnsout4) &&
432 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
438 if ( (NULL != rs->dnsout6) &&
439 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
445 /* re-schedule read task */
451 * Task to (re)transmit the DNS query, possibly repeatedly until
454 * @param cls our `struct GNUNET_DNSSTUB_RequestSocket *`
457 transmit_query (void *cls)
459 struct GNUNET_DNSSTUB_RequestSocket *rs = cls;
460 struct GNUNET_DNSSTUB_Context *ctx = rs->ctx;
461 const struct sockaddr *sa;
463 struct DnsServer *ds;
464 struct GNUNET_NETWORK_Handle *dnsout;
466 rs->retry_task = GNUNET_SCHEDULER_add_delayed (ctx->retry_freq,
470 rs->ds_pos = ds->next;
471 if (NULL == rs->ds_pos)
472 rs->ds_pos = ctx->dns_head;
473 GNUNET_assert (NULL != ds);
475 switch (ds->ss.ss_family)
478 if (NULL == rs->dnsout4)
479 rs->dnsout4 = open_socket (AF_INET);
480 dnsout = rs->dnsout4;
481 sa = (const struct sockaddr *) &ds->ss;
482 salen = sizeof (struct sockaddr_in);
485 if (NULL == rs->dnsout6)
486 rs->dnsout6 = open_socket (AF_INET6);
487 dnsout = rs->dnsout6;
488 sa = (const struct sockaddr *) &ds->ss;
489 salen = sizeof (struct sockaddr_in6);
496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
497 "Unable to use configure DNS server, skipping\n");
501 GNUNET_NETWORK_socket_sendto (dnsout,
506 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
507 _("Failed to send DNS request to %s: %s\n"),
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 _("Sent DNS request to %s\n"),
521 * Perform DNS resolution using our default IP from init.
523 * @param ctx stub resolver to use
524 * @param request DNS request to transmit
525 * @param request_len number of bytes in msg
526 * @param rc function to call with result
527 * @param rc_cls closure for 'rc'
528 * @return socket used for the request, NULL on error
530 struct GNUNET_DNSSTUB_RequestSocket *
531 GNUNET_DNSSTUB_resolve (struct GNUNET_DNSSTUB_Context *ctx,
534 GNUNET_DNSSTUB_ResultCallback rc,
537 struct GNUNET_DNSSTUB_RequestSocket *rs;
539 if (NULL == ctx->dns_head)
541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
542 "No DNS server configured for resolution\n");
545 if (NULL == (rs = get_request_socket (ctx)))
547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
548 "No request socket available for DNS resolution\n");
551 rs->ds_pos = ctx->dns_head;
554 rs->request = GNUNET_memdup (request,
556 rs->request_len = request_len;
557 rs->retry_task = GNUNET_SCHEDULER_add_now (&transmit_query,
564 * Cancel DNS resolution.
566 * @param rs resolution to cancel
569 GNUNET_DNSSTUB_resolve_cancel (struct GNUNET_DNSSTUB_RequestSocket *rs)
572 if (NULL != rs->retry_task)
574 GNUNET_SCHEDULER_cancel (rs->retry_task);
575 rs->retry_task = NULL;
577 if (NULL != rs->read_task)
579 GNUNET_SCHEDULER_cancel (rs->read_task);
580 rs->read_task = NULL;
586 * Start a DNS stub resolver.
588 * @param num_sockets how many sockets should we open
589 * in parallel for DNS queries for this stub?
590 * @return NULL on error
592 struct GNUNET_DNSSTUB_Context *
593 GNUNET_DNSSTUB_start (unsigned int num_sockets)
595 struct GNUNET_DNSSTUB_Context *ctx;
597 if (0 == num_sockets)
602 ctx = GNUNET_new (struct GNUNET_DNSSTUB_Context);
603 ctx->num_sockets = num_sockets;
604 ctx->sockets = GNUNET_new_array (num_sockets,
605 struct GNUNET_DNSSTUB_RequestSocket);
606 ctx->retry_freq = DNS_RETRANSMIT_DELAY;
612 * Add nameserver for use by the DNSSTUB. We will use
613 * all provided nameservers for resolution (round-robin).
615 * @param ctx resolver context to modify
616 * @param dns_ip target IP address to use (as string)
617 * @return #GNUNET_OK on success
620 GNUNET_DNSSTUB_add_dns_ip (struct GNUNET_DNSSTUB_Context *ctx,
623 struct DnsServer *ds;
627 ds = GNUNET_new (struct DnsServer);
628 if (1 == inet_pton (AF_INET,
632 struct sockaddr_in *s4 = (struct sockaddr_in *) &ds->ss;
634 s4->sin_family = AF_INET;
635 s4->sin_port = htons (53);
637 #if HAVE_SOCKADDR_IN_SIN_LEN
638 s4->sin_len = (u_char) sizeof (struct sockaddr_in);
641 else if (1 == inet_pton (AF_INET6,
645 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &ds->ss;
647 s6->sin6_family = AF_INET6;
648 s6->sin6_port = htons (53);
650 #if HAVE_SOCKADDR_IN_SIN_LEN
651 s6->sin6_len = (u_char) sizeof (struct sockaddr_in6);
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Malformed IP address `%s' for DNS server\n",
660 return GNUNET_SYSERR;
662 GNUNET_CONTAINER_DLL_insert (ctx->dns_head,
670 * Add nameserver for use by the DNSSTUB. We will use
671 * all provided nameservers for resolution (round-robin).
673 * @param ctx resolver context to modify
674 * @param sa socket address of DNS resolver to use
675 * @return #GNUNET_OK on success
678 GNUNET_DNSSTUB_add_dns_sa (struct GNUNET_DNSSTUB_Context *ctx,
679 const struct sockaddr *sa)
681 struct DnsServer *ds;
683 ds = GNUNET_new (struct DnsServer);
684 switch (sa->sa_family)
687 GNUNET_memcpy (&ds->ss,
689 sizeof (struct sockaddr_in));
692 GNUNET_memcpy (&ds->ss,
694 sizeof (struct sockaddr_in6));
699 return GNUNET_SYSERR;
701 GNUNET_CONTAINER_DLL_insert (ctx->dns_head,
709 * How long should we try requests before timing out?
710 * Only effective for requests issued after this call.
712 * @param ctx resolver context to modify
713 * @param retry_freq how long to wait between retries
716 GNUNET_DNSSTUB_set_retry (struct GNUNET_DNSSTUB_Context *ctx,
717 struct GNUNET_TIME_Relative retry_freq)
719 ctx->retry_freq = retry_freq;
724 * Cleanup DNSSTUB resolver.
726 * @param ctx stub resolver to clean up
729 GNUNET_DNSSTUB_stop (struct GNUNET_DNSSTUB_Context *ctx)
731 struct DnsServer *ds;
733 while (NULL != (ds = ctx->dns_head))
735 GNUNET_CONTAINER_DLL_remove (ctx->dns_head,
740 for (unsigned int i=0;i<ctx->num_sockets;i++)
741 cleanup_rs (&ctx->sockets[i]);
742 GNUNET_free (ctx->sockets);
747 /* end of dnsstub.c */