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,
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 _("Could not bind to any port: %s\n"),
178 GNUNET_NETWORK_socket_close (ret);
186 * Read a DNS response from the (unhindered) UDP-Socket
188 * @param cls socket to read from
189 * @param tc scheduler context (must be shutdown or read ready)
192 read_response (void *cls,
193 const struct GNUNET_SCHEDULER_TaskContext *tc);
197 * Get a socket of the specified address family to send out a
198 * UDP DNS request to the Internet.
200 * @param ctx the DNSSTUB context
201 * @param af desired address family
202 * @return NULL on error (given AF not "supported")
204 static struct GNUNET_DNSSTUB_RequestSocket *
205 get_request_socket (struct GNUNET_DNSSTUB_Context *ctx,
208 struct GNUNET_DNSSTUB_RequestSocket *rs;
209 struct GNUNET_NETWORK_FDSet *rset;
211 rs = &ctx->sockets[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
213 rs->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
217 if (NULL == rs->dnsout4)
218 rs->dnsout4 = open_socket (AF_INET);
221 if (NULL == rs->dnsout6)
222 rs->dnsout6 = open_socket (AF_INET6);
227 if (GNUNET_SCHEDULER_NO_TASK != rs->read_task)
229 GNUNET_SCHEDULER_cancel (rs->read_task);
230 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
232 if ( (NULL == rs->dnsout4) &&
233 (NULL == rs->dnsout6) )
235 rset = GNUNET_NETWORK_fdset_create ();
236 if (NULL != rs->dnsout4)
237 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
238 if (NULL != rs->dnsout6)
239 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
240 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
245 GNUNET_NETWORK_fdset_destroy (rset);
251 * Perform DNS resolution.
253 * @param ctx stub resolver to use
254 * @param sa the socket address
255 * @param sa_len the socket length
256 * @param request DNS request to transmit
257 * @param request_len number of bytes in msg
258 * @param rc function to call with result
259 * @param rc_cls closure for 'rc'
260 * @return socket used for the request, NULL on error
262 struct GNUNET_DNSSTUB_RequestSocket *
263 GNUNET_DNSSTUB_resolve (struct GNUNET_DNSSTUB_Context *ctx,
264 const struct sockaddr *sa,
268 GNUNET_DNSSTUB_ResultCallback rc,
271 struct GNUNET_DNSSTUB_RequestSocket *rs;
272 struct GNUNET_NETWORK_Handle *ret;
276 if (NULL == (rs = get_request_socket (ctx, af)))
278 if (NULL != rs->dnsout4)
282 GNUNET_assert (NULL != ret);
286 rs->addrlen = sa_len;
290 GNUNET_NETWORK_socket_sendto (ret,
295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
296 _("Failed to send DNS request to %s\n"),
297 GNUNET_a2s (sa, sa_len));
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 _("Sent DNS request to %s\n"),
301 GNUNET_a2s (sa, sa_len));
307 * Perform DNS resolution using our default IP from init.
309 * @param ctx stub resolver to use
310 * @param request DNS request to transmit
311 * @param request_len number of bytes in msg
312 * @param rc function to call with result
313 * @param rc_cls closure for 'rc'
314 * @return socket used for the request, NULL on error
316 struct GNUNET_DNSSTUB_RequestSocket *
317 GNUNET_DNSSTUB_resolve2 (struct GNUNET_DNSSTUB_Context *ctx,
320 GNUNET_DNSSTUB_ResultCallback rc,
324 struct sockaddr_in v4;
325 struct sockaddr_in6 v6;
328 struct GNUNET_NETWORK_Handle *dnsout;
329 struct GNUNET_DNSSTUB_RequestSocket *rs;
331 memset (&v4, 0, sizeof (v4));
332 memset (&v6, 0, sizeof (v6));
333 if (1 == inet_pton (AF_INET, ctx->dns_exit, &v4.sin_addr))
336 v4.sin_family = AF_INET;
337 v4.sin_port = htons (53);
338 #if HAVE_SOCKADDR_IN_SIN_LEN
339 v4.sin_len = (u_char) salen;
341 sa = (struct sockaddr *) &v4;
344 else if (1 == inet_pton (AF_INET6, ctx->dns_exit, &v6.sin6_addr))
347 v6.sin6_family = AF_INET6;
348 v6.sin6_port = htons (53);
349 #if HAVE_SOCKADDR_IN_SIN_LEN
350 v6.sin6_len = (u_char) salen;
352 sa = (struct sockaddr *) &v6;
360 if (NULL == (rs = get_request_socket (ctx, af)))
362 if (NULL != rs->dnsout4)
363 dnsout = rs->dnsout4;
365 dnsout = rs->dnsout6;
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 _("Configured DNS exit `%s' is not working / valid.\n"),
380 GNUNET_NETWORK_socket_sendto (dnsout,
382 request_len, sa, salen))
383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
384 _("Failed to send DNS request to %s\n"),
385 GNUNET_a2s (sa, salen));
386 rs->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
394 * Actually do the reading of a DNS packet from our UDP socket and see
395 * if we have a valid, matching, pending request.
397 * @param rs request socket with callback details
398 * @param dnsout socket to read from
399 * @return GNUNET_OK on success, GNUNET_NO on drop, GNUNET_SYSERR on IO-errors (closed socket)
402 do_dns_read (struct GNUNET_DNSSTUB_RequestSocket *rs,
403 struct GNUNET_NETWORK_Handle *dnsout)
405 struct sockaddr_storage addr;
407 struct GNUNET_TUN_DnsHeader *dns;
412 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
414 /* conservative choice: */
418 /* port the code above? */
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Receiving %d byte DNS reply\n",
425 unsigned char buf[len] GNUNET_ALIGN;
427 addrlen = sizeof (addr);
428 memset (&addr, 0, sizeof (addr));
429 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
431 (struct sockaddr*) &addr, &addrlen);
434 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
435 GNUNET_NETWORK_socket_close (dnsout);
436 return GNUNET_SYSERR;
438 if (sizeof (struct GNUNET_TUN_DnsHeader) > r)
440 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
441 _("Received DNS response that is too small (%u bytes)"),
445 dns = (struct GNUNET_TUN_DnsHeader *) buf;
446 if ( (addrlen != rs->addrlen) ||
447 (0 != memcmp (&rs->addr,
450 (0 == GNUNET_TIME_absolute_get_remaining (rs->timeout).rel_value_us) )
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Request timeout or invalid sender address; ignoring reply\n");
467 * Read a DNS response from the (unhindered) UDP-Socket
469 * @param cls socket to read from
470 * @param tc scheduler context (must be shutdown or read ready)
473 read_response (void *cls,
474 const struct GNUNET_SCHEDULER_TaskContext *tc)
476 struct GNUNET_DNSSTUB_RequestSocket *rs = cls;
477 struct GNUNET_NETWORK_FDSet *rset;
479 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
480 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
482 /* timeout or shutdown */
486 /* read and process ready sockets */
487 if ((NULL != rs->dnsout4) &&
488 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout4)) &&
489 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout4)))
491 if ((NULL != rs->dnsout6) &&
492 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout6)) &&
493 (GNUNET_SYSERR == do_dns_read (rs, rs->dnsout6)))
496 /* re-schedule read task */
497 rset = GNUNET_NETWORK_fdset_create ();
498 if (NULL != rs->dnsout4)
499 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
500 if (NULL != rs->dnsout6)
501 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
502 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
503 GNUNET_TIME_absolute_get_remaining (rs->timeout),
507 GNUNET_NETWORK_fdset_destroy (rset);
512 * Cancel DNS resolution.
514 * @param rs resolution to cancel
517 GNUNET_DNSSTUB_resolve_cancel (struct GNUNET_DNSSTUB_RequestSocket *rs)
524 * Start a DNS stub resolver.
526 * @param dns_ip target IP address to use
527 * @return NULL on error
529 struct GNUNET_DNSSTUB_Context *
530 GNUNET_DNSSTUB_start (const char *dns_ip)
532 struct GNUNET_DNSSTUB_Context *ctx;
534 ctx = GNUNET_malloc (sizeof (struct GNUNET_DNSSTUB_Context));
536 ctx->dns_exit = GNUNET_strdup (dns_ip);
542 * Cleanup DNSSTUB resolver.
544 * @param ctx stub resolver to clean up
547 GNUNET_DNSSTUB_stop (struct GNUNET_DNSSTUB_Context *ctx)
551 for (i=0;i<DNS_SOCKET_MAX;i++)
552 cleanup_rs (&ctx->sockets[i]);
553 if (NULL != ctx->dns_exit)
555 GNUNET_free (ctx->dns_exit);
556 ctx->dns_exit = NULL;
562 /* end of dnsstub.c */