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 GNUNET_NETWORK_socket_sendto (ret,
290 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
291 _("Failed to send DNS request to %s\n"),
292 GNUNET_a2s (sa, sa_len));
298 * Perform DNS resolution using our default IP from init.
300 * @param ctx stub resolver to use
301 * @param request DNS request to transmit
302 * @param request_len number of bytes in msg
303 * @param rc function to call with result
304 * @param rc_cls closure for 'rc'
305 * @return socket used for the request, NULL on error
307 struct GNUNET_DNSSTUB_RequestSocket *
308 GNUNET_DNSSTUB_resolve2 (struct GNUNET_DNSSTUB_Context *ctx,
311 GNUNET_DNSSTUB_ResultCallback rc,
315 struct sockaddr_in v4;
316 struct sockaddr_in6 v6;
319 struct GNUNET_NETWORK_Handle *dnsout;
320 struct GNUNET_DNSSTUB_RequestSocket *rs;
322 memset (&v4, 0, sizeof (v4));
323 memset (&v6, 0, sizeof (v6));
324 if (1 == inet_pton (AF_INET, ctx->dns_exit, &v4.sin_addr))
327 v4.sin_family = AF_INET;
328 v4.sin_port = htons (53);
329 #if HAVE_SOCKADDR_IN_SIN_LEN
330 v4.sin_len = (u_char) salen;
332 sa = (struct sockaddr *) &v4;
335 else if (1 == inet_pton (AF_INET6, ctx->dns_exit, &v6.sin6_addr))
338 v6.sin6_family = AF_INET6;
339 v6.sin6_port = htons (53);
340 #if HAVE_SOCKADDR_IN_SIN_LEN
341 v6.sin6_len = (u_char) salen;
343 sa = (struct sockaddr *) &v6;
351 if (NULL == (rs = get_request_socket (ctx, af)))
353 if (NULL != rs->dnsout4)
354 dnsout = rs->dnsout4;
356 dnsout = rs->dnsout6;
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 _("Configured DNS exit `%s' is not working / valid.\n"),
371 GNUNET_NETWORK_socket_sendto (dnsout,
373 request_len, sa, salen))
374 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
375 _("Failed to send DNS request to %s\n"),
376 GNUNET_a2s (sa, salen));
377 rs->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
385 * Actually do the reading of a DNS packet from our UDP socket and see
386 * if we have a valid, matching, pending request.
388 * @param rs request socket with callback details
389 * @param dnsout socket to read from
390 * @return GNUNET_OK on success, GNUNET_NO on drop, GNUNET_SYSERR on IO-errors (closed socket)
393 do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
394 struct GNUNET_NETWORK_Handle *dnsout)
396 struct sockaddr_storage addr;
398 struct GNUNET_TUN_DnsHeader *dns;
403 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
405 /* conservative choice: */
409 /* port the code above? */
414 unsigned char buf[len] GNUNET_ALIGN;
416 addrlen = sizeof (addr);
417 memset (&addr, 0, sizeof (addr));
418 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
420 (struct sockaddr*) &addr, &addrlen);
423 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
424 GNUNET_NETWORK_socket_close (dnsout);
425 return GNUNET_SYSERR;
427 if (sizeof (struct GNUNET_TUN_DnsHeader) > r)
429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
430 _("Received DNS response that is too small (%u bytes)"),
434 dns = (struct GNUNET_TUN_DnsHeader *) buf;
435 if ( (addrlen != rs->addrlen) ||
436 (0 != memcmp (&rs->addr,
439 (0 == GNUNET_TIME_absolute_get_remaining (rs->timeout).rel_value) )
452 * Read a DNS response from the (unhindered) UDP-Socket
454 * @param cls socket to read from
455 * @param tc scheduler context (must be shutdown or read ready)
458 read_response (void *cls,
459 const struct GNUNET_SCHEDULER_TaskContext *tc)
461 struct GNUNET_DNSSTUB_RequestSocket *rs = cls;
462 struct GNUNET_NETWORK_FDSet *rset;
464 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
465 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
467 /* timeout or shutdown */
471 /* read and process ready sockets */
472 if ((NULL != rs->dnsout4) &&
473 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout4)) &&
474 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout4)))
476 if ((NULL != rs->dnsout6) &&
477 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout6)) &&
478 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout6)))
481 /* re-schedule read task */
482 rset = GNUNET_NETWORK_fdset_create ();
483 if (NULL != rs->dnsout4)
484 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
485 if (NULL != rs->dnsout6)
486 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
487 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
488 GNUNET_TIME_absolute_get_remaining (rs->timeout),
492 GNUNET_NETWORK_fdset_destroy (rset);
497 * Cancel DNS resolution.
499 * @param rs resolution to cancel
502 GNUNET_DNSSTUB_resolve_cancel (struct GNUNET_DNSSTUB_RequestSocket *rs)
509 * Start a DNS stub resolver.
511 * @param dns_ip target IP address to use
512 * @return NULL on error
514 struct GNUNET_DNSSTUB_Context *
515 GNUNET_DNSSTUB_start (const char *dns_ip)
517 struct GNUNET_DNSSTUB_Context *ctx;
519 ctx = GNUNET_malloc (sizeof (struct GNUNET_DNSSTUB_Context));
521 ctx->dns_exit = GNUNET_strdup (dns_ip);
527 * Cleanup DNSSTUB resolver.
529 * @param ctx stub resolver to clean up
532 GNUNET_DNSSTUB_stop (struct GNUNET_DNSSTUB_Context *ctx)
536 for (i=0;i<DNS_SOCKET_MAX;i++)
537 cleanup_rs (&ctx->sockets[i]);
538 if (NULL != ctx->dns_exit)
540 GNUNET_free (ctx->dns_exit);
541 ctx->dns_exit = NULL;
547 /* end of dnsstub.c */