2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @brief DNS stub resolver which sends DNS requests to an actual resolver
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_dnsstub_lib.h"
30 * Timeout for an external (Internet-DNS) DNS resolution
32 #define REQUEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
35 * How many DNS sockets do we open at most at the same time?
36 * (technical socket maximum is this number x2 for IPv4+IPv6)
38 #define DNS_SOCKET_MAX 128
42 * UDP socket we are using for sending DNS requests to the Internet.
44 struct GNUNET_DNSSTUB_RequestSocket
48 * UDP socket we use for this request for IPv4
50 struct GNUNET_NETWORK_Handle *dnsout4;
53 * UDP socket we use for this request for IPv6
55 struct GNUNET_NETWORK_Handle *dnsout6;
58 * Function to call with result.
60 GNUNET_DNSSTUB_ResultCallback rc;
68 * Task for reading from dnsout4 and dnsout6.
70 GNUNET_SCHEDULER_TaskIdentifier read_task;
73 * When should this request time out?
75 struct GNUNET_TIME_Absolute timeout;
78 * Address we sent the DNS request to.
80 struct sockaddr_storage addr;
83 * Number of bytes in 'addr'.
91 * Handle to the stub resolver.
93 struct GNUNET_DNSSTUB_Context
97 * Array of all open sockets for DNS requests.
99 struct GNUNET_DNSSTUB_RequestSocket sockets[DNS_SOCKET_MAX];
102 * IP address to use for the DNS server if we are a DNS exit service
103 * (for VPN via mesh); otherwise NULL.
111 * We're done with a GNUNET_DNSSTUB_RequestSocket, close it for now.
113 * @param rs request socket to clean up
116 cleanup_rs (struct GNUNET_DNSSTUB_RequestSocket *rs)
118 if (NULL != rs->dnsout4)
120 GNUNET_NETWORK_socket_close (rs->dnsout4);
123 if (NULL != rs->dnsout6)
125 GNUNET_NETWORK_socket_close (rs->dnsout6);
128 if (GNUNET_SCHEDULER_NO_TASK != rs->read_task)
130 GNUNET_SCHEDULER_cancel (rs->read_task);
131 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
137 * Open source port for sending DNS requests
139 * @param af AF_INET or AF_INET6
140 * @return GNUNET_OK on success
142 static struct GNUNET_NETWORK_Handle *
145 struct sockaddr_in a4;
146 struct sockaddr_in6 a6;
149 struct GNUNET_NETWORK_Handle *ret;
151 ret = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, 0);
157 memset (&a4, 0, alen = sizeof (struct sockaddr_in));
158 sa = (struct sockaddr *) &a4;
161 memset (&a6, 0, alen = sizeof (struct sockaddr_in6));
162 sa = (struct sockaddr *) &a6;
166 GNUNET_NETWORK_socket_close (ret);
170 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret,
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 _("Could not bind to any port: %s\n"),
177 GNUNET_NETWORK_socket_close (ret);
185 * Read a DNS response from the (unhindered) UDP-Socket
187 * @param cls socket to read from
188 * @param tc scheduler context (must be shutdown or read ready)
191 read_response (void *cls,
192 const struct GNUNET_SCHEDULER_TaskContext *tc);
196 * Get a socket of the specified address family to send out a
197 * UDP DNS request to the Internet.
199 * @param ctx the DNSSTUB context
200 * @param af desired address family
201 * @return NULL on error (given AF not "supported")
203 static struct GNUNET_DNSSTUB_RequestSocket *
204 get_request_socket (struct GNUNET_DNSSTUB_Context *ctx,
207 struct GNUNET_DNSSTUB_RequestSocket *rs;
208 struct GNUNET_NETWORK_FDSet *rset;
210 rs = &ctx->sockets[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
212 rs->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
216 if (NULL == rs->dnsout4)
217 rs->dnsout4 = open_socket (AF_INET);
220 if (NULL == rs->dnsout6)
221 rs->dnsout6 = open_socket (AF_INET6);
226 if (GNUNET_SCHEDULER_NO_TASK != rs->read_task)
228 GNUNET_SCHEDULER_cancel (rs->read_task);
229 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
231 if ( (NULL == rs->dnsout4) &&
232 (NULL == rs->dnsout6) )
234 rset = GNUNET_NETWORK_fdset_create ();
235 if (NULL != rs->dnsout4)
236 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
237 if (NULL != rs->dnsout6)
238 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
239 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
244 GNUNET_NETWORK_fdset_destroy (rset);
250 * Perform DNS resolution.
252 * @param ctx stub resolver to use
253 * @param sa the socket address
254 * @param sa_len the socket length
255 * @param request DNS request to transmit
256 * @param request_len number of bytes in msg
257 * @param rc function to call with result
258 * @param rc_cls closure for 'rc'
259 * @return socket used for the request, NULL on error
261 struct GNUNET_DNSSTUB_RequestSocket *
262 GNUNET_DNSSTUB_resolve (struct GNUNET_DNSSTUB_Context *ctx,
263 const struct sockaddr *sa,
267 GNUNET_DNSSTUB_ResultCallback rc,
270 struct GNUNET_DNSSTUB_RequestSocket *rs;
271 struct GNUNET_NETWORK_Handle *ret;
275 if (NULL == (rs = get_request_socket (ctx, af)))
277 if (NULL != rs->dnsout4)
281 GNUNET_assert (NULL != ret);
285 rs->addrlen = sa_len;
289 GNUNET_NETWORK_socket_sendto (ret,
294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
295 _("Failed to send DNS request to %s\n"),
296 GNUNET_a2s (sa, sa_len));
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 _("Sent DNS request to %s\n"),
300 GNUNET_a2s (sa, sa_len));
306 * Perform DNS resolution using our default IP from init.
308 * @param ctx stub resolver to use
309 * @param request DNS request to transmit
310 * @param request_len number of bytes in msg
311 * @param rc function to call with result
312 * @param rc_cls closure for 'rc'
313 * @return socket used for the request, NULL on error
315 struct GNUNET_DNSSTUB_RequestSocket *
316 GNUNET_DNSSTUB_resolve2 (struct GNUNET_DNSSTUB_Context *ctx,
319 GNUNET_DNSSTUB_ResultCallback rc,
323 struct sockaddr_in v4;
324 struct sockaddr_in6 v6;
327 struct GNUNET_NETWORK_Handle *dnsout;
328 struct GNUNET_DNSSTUB_RequestSocket *rs;
330 memset (&v4, 0, sizeof (v4));
331 memset (&v6, 0, sizeof (v6));
332 if (1 == inet_pton (AF_INET, ctx->dns_exit, &v4.sin_addr))
335 v4.sin_family = AF_INET;
336 v4.sin_port = htons (53);
337 #if HAVE_SOCKADDR_IN_SIN_LEN
338 v4.sin_len = (u_char) salen;
340 sa = (struct sockaddr *) &v4;
343 else if (1 == inet_pton (AF_INET6, ctx->dns_exit, &v6.sin6_addr))
346 v6.sin6_family = AF_INET6;
347 v6.sin6_port = htons (53);
348 #if HAVE_SOCKADDR_IN_SIN_LEN
349 v6.sin6_len = (u_char) salen;
351 sa = (struct sockaddr *) &v6;
359 if (NULL == (rs = get_request_socket (ctx, af)))
361 if (NULL != rs->dnsout4)
362 dnsout = rs->dnsout4;
364 dnsout = rs->dnsout6;
367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
368 _("Configured DNS exit `%s' is not working / valid.\n"),
379 GNUNET_NETWORK_socket_sendto (dnsout,
381 request_len, sa, salen))
382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
383 _("Failed to send DNS request to %s\n"),
384 GNUNET_a2s (sa, salen));
385 rs->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
393 * Actually do the reading of a DNS packet from our UDP socket and see
394 * if we have a valid, matching, pending request.
396 * @param rs request socket with callback details
397 * @param dnsout socket to read from
398 * @return GNUNET_OK on success, GNUNET_NO on drop, GNUNET_SYSERR on IO-errors (closed socket)
401 do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
402 struct GNUNET_NETWORK_Handle *dnsout)
404 struct sockaddr_storage addr;
406 struct GNUNET_TUN_DnsHeader *dns;
411 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
413 /* conservative choice: */
417 /* port the code above? */
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Receiving %d byte DNS reply\n",
424 unsigned char buf[len] GNUNET_ALIGN;
426 addrlen = sizeof (addr);
427 memset (&addr, 0, sizeof (addr));
428 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
430 (struct sockaddr*) &addr, &addrlen);
433 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
434 GNUNET_NETWORK_socket_close (dnsout);
435 return GNUNET_SYSERR;
437 if (sizeof (struct GNUNET_TUN_DnsHeader) > r)
439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
440 _("Received DNS response that is too small (%u bytes)"),
444 dns = (struct GNUNET_TUN_DnsHeader *) buf;
445 if ( (addrlen != rs->addrlen) ||
446 (0 != memcmp (&rs->addr,
449 (0 == GNUNET_TIME_absolute_get_remaining (rs->timeout).rel_value) )
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Request timeout or invalid sender address; ignoring reply\n");
466 * Read a DNS response from the (unhindered) UDP-Socket
468 * @param cls socket to read from
469 * @param tc scheduler context (must be shutdown or read ready)
472 read_response (void *cls,
473 const struct GNUNET_SCHEDULER_TaskContext *tc)
475 struct GNUNET_DNSSTUB_RequestSocket *rs = cls;
476 struct GNUNET_NETWORK_FDSet *rset;
478 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
479 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
481 /* timeout or shutdown */
485 /* read and process ready sockets */
486 if ((NULL != rs->dnsout4) &&
487 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout4)) &&
488 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout4)))
490 if ((NULL != rs->dnsout6) &&
491 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout6)) &&
492 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout6)))
495 /* re-schedule read task */
496 rset = GNUNET_NETWORK_fdset_create ();
497 if (NULL != rs->dnsout4)
498 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
499 if (NULL != rs->dnsout6)
500 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
501 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
502 GNUNET_TIME_absolute_get_remaining (rs->timeout),
506 GNUNET_NETWORK_fdset_destroy (rset);
511 * Cancel DNS resolution.
513 * @param rs resolution to cancel
516 GNUNET_DNSSTUB_resolve_cancel (struct GNUNET_DNSSTUB_RequestSocket *rs)
523 * Start a DNS stub resolver.
525 * @param dns_ip target IP address to use
526 * @return NULL on error
528 struct GNUNET_DNSSTUB_Context *
529 GNUNET_DNSSTUB_start (const char *dns_ip)
531 struct GNUNET_DNSSTUB_Context *ctx;
533 ctx = GNUNET_malloc (sizeof (struct GNUNET_DNSSTUB_Context));
535 ctx->dns_exit = GNUNET_strdup (dns_ip);
541 * Cleanup DNSSTUB resolver.
543 * @param ctx stub resolver to clean up
546 GNUNET_DNSSTUB_stop (struct GNUNET_DNSSTUB_Context *ctx)
550 for (i=0;i<DNS_SOCKET_MAX;i++)
551 cleanup_rs (&ctx->sockets[i]);
552 if (NULL != ctx->dns_exit)
554 GNUNET_free (ctx->dns_exit);
555 ctx->dns_exit = NULL;
561 /* end of dnsstub.c */