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 * @file dns/gnunet-service-dns.c
23 * @brief service to intercept and modify DNS queries (and replies) of this system
24 * @author Christian Grothoff
26 * For "secure" interaction with the legacy DNS system, we permit
27 * replies only to arrive within a 5s window (and they must match
28 * ports, IPs and request IDs). Furthermore, we let the OS pick a
29 * source port, opening up to 128 sockets per address family (IPv4 or
30 * IPv6). Those sockets are closed if they are not in use for 5s
31 * (which means they will be freshly randomized afterwards). For new
32 * requests, we pick a random slot in the array with 128 socket slots
33 * (and re-use an existing socket if the slot is still in use). Thus
34 * each request will be given one of 128 random source ports, and the
35 * 128 random source ports will also change "often" (less often if the
36 * system is very busy, each time if we are mostly idle). At the same
37 * time, the system will never use more than 256 UDP sockets.
40 #include "gnunet_util_lib.h"
41 #include "gnunet_applications.h"
42 #include "gnunet_constants.h"
43 #include "gnunet_protocols.h"
44 #include "gnunet_signatures.h"
46 #include "gnunet_dns_service.h"
47 #include "gnunet_mesh_service.h"
48 #include "gnunet_statistics_service.h"
49 #include "gnunet_tun_lib.h"
53 * Timeout for an external (Internet-DNS) DNS resolution
55 #define REQUEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
58 * How many DNS sockets do we open at most at the same time?
59 * (technical socket maximum is this number x2 for IPv4+IPv6)
61 #define DNS_SOCKET_MAX 128
64 * Phases each request goes through.
69 * Request has just been received.
74 * Showing the request to all monitor clients. If
75 * client list is empty, will enter QUERY phase.
80 * Showing the request to PRE-RESOLUTION clients to find an answer.
81 * If client list is empty, will trigger global DNS request.
86 * Global Internet query is now pending.
91 * Client (or global DNS request) has resulted in a response.
92 * Forward to all POST-RESOLUTION clients. If client list is empty,
93 * will enter RESPONSE_MONITOR phase.
98 * Showing the request to all monitor clients. If
99 * client list is empty, give the result to the hijacker (and be done).
104 * Some client has told us to drop the request.
111 * Entry we keep for each client.
116 * Kept in doubly-linked list.
118 struct ClientRecord *next;
121 * Kept in doubly-linked list.
123 struct ClientRecord *prev;
126 * Handle to the client.
128 struct GNUNET_SERVER_Client *client;
131 * Flags for the client.
133 enum GNUNET_DNS_Flags flags;
139 * UDP socket we are using for sending DNS requests to the Internet.
145 * UDP socket we use for this request for IPv4
147 struct GNUNET_NETWORK_Handle *dnsout4;
150 * UDP socket we use for this request for IPv6
152 struct GNUNET_NETWORK_Handle *dnsout6;
155 * Task for reading from dnsout4 and dnsout6.
157 GNUNET_SCHEDULER_TaskIdentifier read_task;
160 * When should this socket be closed?
162 struct GNUNET_TIME_Absolute timeout;
167 * Entry we keep for each active request.
173 * List of clients that still need to see this request (each entry
174 * is set to NULL when the client is done).
176 struct ClientRecord **client_wait_list;
179 * Payload of the UDP packet (the UDP payload), can be either query
180 * or already the response.
185 * Socket we are using to transmit this request (must match if we receive
186 * a response). Must NOT be freed as part of this request record (as it
187 * might be shared with other requests).
189 struct GNUNET_NETWORK_Handle *dnsout;
192 * Source address of the original request (for sending response).
194 struct sockaddr_storage src_addr;
197 * Destination address of the original request (for potential use as exit).
199 struct sockaddr_storage dst_addr;
202 * When should this request time out?
204 struct GNUNET_TIME_Absolute timeout;
207 * ID of this request, also basis for hashing. Lowest 16 bit will
208 * be our message ID when doing a global DNS request and our index
209 * into the 'requests' array.
214 * Number of bytes in payload.
216 size_t payload_length;
219 * Length of the client wait list.
221 unsigned int client_wait_list_length;
224 * In which phase this this request?
226 enum RequestPhase phase;
232 * State we keep for each DNS tunnel that terminates at this node.
238 * Associated MESH tunnel.
240 struct GNUNET_MESH_Tunnel *tunnel;
243 * Active request for sending a reply.
245 struct GNUNET_MESH_TransmitHandle *th;
248 * DNS reply ready for transmission.
253 * Socket we are using to transmit this request (must match if we receive
254 * a response). Must NOT be freed as part of this request record (as it
255 * might be shared with other requests).
257 struct GNUNET_NETWORK_Handle *dnsout;
260 * Address we sent the DNS request to.
262 struct sockaddr_storage addr;
265 * When should this request time out?
267 struct GNUNET_TIME_Absolute timeout;
270 * Number of bytes in 'addr'.
275 * Number of bytes in 'reply'.
280 * Original DNS request ID as used by the client.
282 uint16_t original_id;
285 * DNS request ID that we used for forwarding.
292 * The configuration to use
294 static const struct GNUNET_CONFIGURATION_Handle *cfg;
299 static struct GNUNET_STATISTICS_Handle *stats;
302 * Handle to DNS hijacker helper process ("gnunet-helper-dns").
304 static struct GNUNET_HELPER_Handle *hijacker;
307 * Command-line arguments we are giving to the hijacker process.
309 static char *helper_argv[7];
312 * Head of DLL of clients we consult.
314 static struct ClientRecord *clients_head;
317 * Tail of DLL of clients we consult.
319 static struct ClientRecord *clients_tail;
322 * Our notification context.
324 static struct GNUNET_SERVER_NotificationContext *nc;
327 * Array of all open requests.
329 static struct RequestRecord requests[UINT16_MAX + 1];
332 * Array of all open requests from tunnels.
334 static struct TunnelState *tunnels[UINT16_MAX + 1];
337 * Array of all open sockets for DNS requests.
339 static struct RequestSocket sockets[DNS_SOCKET_MAX];
342 * Generator for unique request IDs.
344 static uint64_t request_id_gen;
347 * IP address to use for the DNS server if we are a DNS exit service
348 * (for VPN via mesh); otherwise NULL.
350 static char *dns_exit;
353 * Handle to the MESH service (for receiving DNS queries), or NULL
354 * if we are not a DNS exit.
356 static struct GNUNET_MESH_Handle *mesh;
360 * We're done with a RequestSocket, close it for now.
362 * @param rs request socket to clean up
365 cleanup_rs (struct RequestSocket *rs)
367 if (NULL != rs->dnsout4)
369 GNUNET_NETWORK_socket_close (rs->dnsout4);
372 if (NULL != rs->dnsout6)
374 GNUNET_NETWORK_socket_close (rs->dnsout6);
377 if (GNUNET_SCHEDULER_NO_TASK != rs->read_task)
379 GNUNET_SCHEDULER_cancel (rs->read_task);
380 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
386 * We're done processing a DNS request, free associated memory.
388 * @param rr request to clean up
391 cleanup_rr (struct RequestRecord *rr)
393 GNUNET_free_non_null (rr->payload);
395 rr->payload_length = 0;
396 GNUNET_array_grow (rr->client_wait_list,
397 rr->client_wait_list_length,
403 * Task run during shutdown.
409 cleanup_task (void *cls GNUNET_UNUSED,
410 const struct GNUNET_SCHEDULER_TaskContext *tc)
414 GNUNET_HELPER_stop (hijacker);
417 GNUNET_free_non_null (helper_argv[i]);
418 for (i=0;i<=UINT16_MAX;i++)
419 cleanup_rr (&requests[i]);
420 GNUNET_SERVER_notification_context_destroy (nc);
424 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
427 if (NULL != dns_exit)
429 GNUNET_free (dns_exit);
436 * Open source port for sending DNS requests
438 * @param af AF_INET or AF_INET6
439 * @return GNUNET_OK on success
441 static struct GNUNET_NETWORK_Handle *
444 struct sockaddr_in a4;
445 struct sockaddr_in6 a6;
448 struct GNUNET_NETWORK_Handle *ret;
450 ret = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, 0);
456 memset (&a4, 0, alen = sizeof (struct sockaddr_in));
457 sa = (struct sockaddr *) &a4;
460 memset (&a6, 0, alen = sizeof (struct sockaddr_in6));
461 sa = (struct sockaddr *) &a6;
465 GNUNET_NETWORK_socket_close (ret);
469 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret,
473 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
474 _("Could not bind to any port: %s\n"),
476 GNUNET_NETWORK_socket_close (ret);
484 * We're done with some request, finish processing.
486 * @param rr request send to the network or just clean up.
489 request_done (struct RequestRecord *rr)
491 struct GNUNET_MessageHeader *hdr;
493 uint16_t source_port;
494 uint16_t destination_port;
496 GNUNET_array_grow (rr->client_wait_list,
497 rr->client_wait_list_length,
499 if (RP_RESPONSE_MONITOR != rr->phase)
501 /* no response, drop */
506 /* send response via hijacker */
507 reply_len = sizeof (struct GNUNET_MessageHeader);
508 reply_len += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
509 switch (rr->src_addr.ss_family)
512 reply_len += sizeof (struct GNUNET_TUN_IPv4Header);
515 reply_len += sizeof (struct GNUNET_TUN_IPv6Header);
522 reply_len += sizeof (struct GNUNET_TUN_UdpHeader);
523 reply_len += rr->payload_length;
524 if (reply_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
526 /* response too big, drop */
527 GNUNET_break (0); /* how can this be? */
534 struct GNUNET_TUN_IPv4Header ip4;
535 struct GNUNET_TUN_IPv6Header ip6;
537 /* first, GNUnet message header */
538 hdr = (struct GNUNET_MessageHeader*) buf;
539 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
540 hdr->size = htons ((uint16_t) reply_len);
541 off = sizeof (struct GNUNET_MessageHeader);
543 /* first, TUN header */
545 struct GNUNET_TUN_Layer2PacketHeader tun;
547 tun.flags = htons (0);
548 if (rr->src_addr.ss_family == AF_INET)
549 tun.proto = htons (ETH_P_IPV4);
551 tun.proto = htons (ETH_P_IPV6);
552 memcpy (&buf[off], &tun, sizeof (struct GNUNET_TUN_Layer2PacketHeader));
553 off += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
557 switch (rr->src_addr.ss_family)
561 struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr;
562 struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr;
564 source_port = dst->sin_port;
565 destination_port = src->sin_port;
566 GNUNET_TUN_initialize_ipv4_header (&ip4,
568 reply_len - off - sizeof (struct GNUNET_TUN_IPv4Header),
571 memcpy (&buf[off], &ip4, sizeof (ip4));
577 struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr;
578 struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr;
580 source_port = dst->sin6_port;
581 destination_port = src->sin6_port;
582 GNUNET_TUN_initialize_ipv6_header (&ip6,
584 reply_len - sizeof (struct GNUNET_TUN_IPv6Header),
587 memcpy (&buf[off], &ip6, sizeof (ip6));
597 struct GNUNET_TUN_UdpHeader udp;
599 udp.source_port = source_port;
600 udp.destination_port = destination_port;
601 udp.len = htons (reply_len - off);
602 if (AF_INET == rr->src_addr.ss_family)
603 GNUNET_TUN_calculate_udp4_checksum (&ip4,
608 GNUNET_TUN_calculate_udp6_checksum (&ip6,
612 memcpy (&buf[off], &udp, sizeof (udp));
616 /* now DNS payload */
618 memcpy (&buf[off], rr->payload, rr->payload_length);
619 off += rr->payload_length;
621 /* final checks & sending */
622 GNUNET_assert (off == reply_len);
623 GNUNET_HELPER_send (hijacker,
627 GNUNET_STATISTICS_update (stats,
628 gettext_noop ("# DNS requests answered via TUN interface"),
631 /* clean up, we're done */
637 * Show the payload of the given request record to the client
638 * (and wait for a response).
640 * @param rr request to send to client
641 * @param client client to send the response to
644 send_request_to_client (struct RequestRecord *rr,
645 struct GNUNET_SERVER_Client *client)
647 char buf[sizeof (struct GNUNET_DNS_Request) + rr->payload_length];
648 struct GNUNET_DNS_Request *req;
650 if (sizeof (buf) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
656 req = (struct GNUNET_DNS_Request*) buf;
657 req->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
658 req->header.size = htons (sizeof (buf));
659 req->reserved = htonl (0);
660 req->request_id = rr->request_id;
661 memcpy (&req[1], rr->payload, rr->payload_length);
662 GNUNET_SERVER_notification_context_unicast (nc,
670 * Read a DNS response from the (unhindered) UDP-Socket
672 * @param cls socket to read from
673 * @param tc scheduler context (must be shutdown or read ready)
676 read_response (void *cls,
677 const struct GNUNET_SCHEDULER_TaskContext *tc);
681 * Get a socket of the specified address family to send out a
682 * UDP DNS request to the Internet.
684 * @param af desired address family
685 * @return NULL on error (given AF not "supported")
687 static struct GNUNET_NETWORK_Handle *
688 get_request_socket (int af)
690 struct RequestSocket *rs;
691 struct GNUNET_NETWORK_FDSet *rset;
692 static struct GNUNET_NETWORK_Handle *ret;
694 rs = &sockets[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
696 rs->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
700 if (NULL == rs->dnsout4)
701 rs->dnsout4 = open_socket (AF_INET);
705 if (NULL == rs->dnsout6)
706 rs->dnsout6 = open_socket (AF_INET6);
712 if (GNUNET_SCHEDULER_NO_TASK != rs->read_task)
714 GNUNET_SCHEDULER_cancel (rs->read_task);
715 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
717 if ( (NULL == rs->dnsout4) &&
718 (NULL == rs->dnsout6) )
720 rset = GNUNET_NETWORK_fdset_create ();
721 if (NULL != rs->dnsout4)
722 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
723 if (NULL != rs->dnsout6)
724 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
725 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
726 GNUNET_SCHEDULER_NO_TASK,
731 GNUNET_NETWORK_fdset_destroy (rset);
737 * A client has completed its processing for this
740 * @param rr request to process further
743 next_phase (struct RequestRecord *rr)
745 struct ClientRecord *cr;
750 if (rr->phase == RP_DROP)
756 for (j=0;j<rr->client_wait_list_length;j++)
758 if (NULL != rr->client_wait_list[j])
766 send_request_to_client (rr, rr->client_wait_list[nz]->client);
769 /* done with current phase, advance! */
773 rr->phase = RP_REQUEST_MONITOR;
774 for (cr = clients_head; NULL != cr; cr = cr->next)
776 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR))
777 GNUNET_array_append (rr->client_wait_list,
778 rr->client_wait_list_length,
783 case RP_REQUEST_MONITOR:
784 rr->phase = RP_QUERY;
785 for (cr = clients_head; NULL != cr; cr = cr->next)
787 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION))
788 GNUNET_array_append (rr->client_wait_list,
789 rr->client_wait_list_length,
795 switch (rr->dst_addr.ss_family)
798 salen = sizeof (struct sockaddr_in);
801 salen = sizeof (struct sockaddr_in6);
807 rr->phase = RP_INTERNET_DNS;
808 rr->dnsout = get_request_socket (rr->dst_addr.ss_family);
809 if (NULL == rr->dnsout)
811 GNUNET_STATISTICS_update (stats,
812 gettext_noop ("# DNS exit failed (failed to open socket)"),
817 GNUNET_NETWORK_socket_sendto (rr->dnsout,
820 (struct sockaddr*) &rr->dst_addr,
822 rr->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
824 case RP_INTERNET_DNS:
825 rr->phase = RP_MODIFY;
826 for (cr = clients_head; NULL != cr; cr = cr->next)
828 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
829 GNUNET_array_append (rr->client_wait_list,
830 rr->client_wait_list_length,
836 rr->phase = RP_RESPONSE_MONITOR;
837 for (cr = clients_head; NULL != cr; cr = cr->next)
839 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
840 GNUNET_array_append (rr->client_wait_list,
841 rr->client_wait_list_length,
846 case RP_RESPONSE_MONITOR:
861 * A client disconnected, clean up after it.
864 * @param client handle of client that disconnected
867 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
869 struct ClientRecord *cr;
870 struct RequestRecord *rr;
874 for (cr = clients_head; NULL != cr; cr = cr->next)
876 if (cr->client == client)
878 GNUNET_SERVER_client_drop (client);
879 GNUNET_CONTAINER_DLL_remove (clients_head,
882 for (i=0;i<UINT16_MAX;i++)
885 if (0 == rr->client_wait_list_length)
886 continue; /* not in use */
887 for (j=0;j<rr->client_wait_list_length;j++)
889 if (rr->client_wait_list[j] == cr)
891 rr->client_wait_list[j] = NULL;
904 * We got a reply from DNS for a request of a MESH tunnel. Send it
905 * via the tunnel (after changing the request ID back).
907 * @param cls the 'struct TunnelState'
908 * @param size number of bytes available in buf
909 * @param buf where to copy the reply
910 * @return number of bytes written to buf
913 transmit_reply_to_mesh (void *cls,
917 struct TunnelState *ts = cls;
921 struct GNUNET_MessageHeader hdr;
922 struct GNUNET_TUN_DnsHeader dns;
925 GNUNET_assert (ts->reply != NULL);
928 ret = sizeof (struct GNUNET_MessageHeader) + ts->reply_length;
929 GNUNET_assert (ret <= size);
930 hdr.size = htons (ret);
931 hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET);
932 memcpy (&dns, ts->reply, sizeof (dns));
933 dns.id = ts->original_id;
935 memcpy (&cbuf[off], &hdr, sizeof (hdr));
937 memcpy (&cbuf[off], &dns, sizeof (dns));
939 memcpy (&cbuf[off], &ts->reply[sizeof (dns)], ts->reply_length - sizeof (dns));
940 off += ts->reply_length - sizeof (dns);
941 GNUNET_free (ts->reply);
943 ts->reply_length = 0;
944 GNUNET_assert (ret == off);
950 * Actually do the reading of a DNS packet from our UDP socket and see
951 * if we have a valid, matching, pending request.
953 * @param dnsout socket to read from
954 * @return GNUNET_OK on success, GNUNET_NO on drop, GNUNET_SYSERR on IO-errors (closed socket)
957 do_dns_read (struct GNUNET_NETWORK_Handle *dnsout)
959 struct sockaddr_storage addr;
961 struct GNUNET_TUN_DnsHeader *dns;
962 struct RequestRecord *rr;
963 struct TunnelState *ts;
968 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
970 /* conservative choice: */
974 /* port the code above? */
979 unsigned char buf[len];
981 addrlen = sizeof (addr);
982 memset (&addr, 0, sizeof (addr));
983 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
985 (struct sockaddr*) &addr, &addrlen);
988 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
989 GNUNET_NETWORK_socket_close (dnsout);
990 return GNUNET_SYSERR;
992 if (sizeof (struct GNUNET_TUN_DnsHeader) > r)
994 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
995 _("Received DNS response that is too small (%u bytes)"),
999 dns = (struct GNUNET_TUN_DnsHeader *) buf;
1000 /* Handle case that this is a reply to a request from a MESH DNS tunnel */
1001 ts = tunnels[dns->id];
1002 if ( (NULL == ts) ||
1003 (ts->dnsout != dnsout) ||
1004 (addrlen != ts->addrlen) ||
1005 (0 != memcmp (&ts->addr,
1008 (0 == GNUNET_TIME_absolute_get_remaining (ts->timeout).rel_value) )
1009 ts = NULL; /* DNS responder address missmatch */
1012 tunnels[dns->id] = NULL;
1013 GNUNET_free_non_null (ts->reply);
1014 ts->reply = GNUNET_malloc (r);
1015 ts->reply_length = r;
1016 memcpy (ts->reply, dns, r);
1018 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
1019 ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
1021 GNUNET_TIME_UNIT_FOREVER_REL,
1023 sizeof (struct GNUNET_MessageHeader) + r,
1024 &transmit_reply_to_mesh,
1027 /* Handle case that this is a reply to a local request (intercepted from TUN interface) */
1028 rr = &requests[dns->id];
1029 if ( (rr->phase != RP_INTERNET_DNS) ||
1030 (rr->dnsout != dnsout) ||
1031 (0 != memcmp (&rr->dst_addr,
1034 (0 == GNUNET_TIME_absolute_get_remaining (rr->timeout).rel_value) )
1038 /* unexpected / bogus reply */
1039 GNUNET_STATISTICS_update (stats,
1040 gettext_noop ("# External DNS response discarded (no matching request)"),
1045 GNUNET_free_non_null (rr->payload);
1046 rr->payload = GNUNET_malloc (r);
1047 memcpy (rr->payload, buf, r);
1048 rr->payload_length = r;
1056 * Read a DNS response from the (unhindered) UDP-Socket
1058 * @param cls socket to read from
1059 * @param tc scheduler context (must be shutdown or read ready)
1062 read_response (void *cls,
1063 const struct GNUNET_SCHEDULER_TaskContext *tc)
1065 struct RequestSocket *rs = cls;
1066 struct GNUNET_NETWORK_FDSet *rset;
1068 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
1069 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1071 /* timeout or shutdown */
1075 /* read and process ready sockets */
1076 if ((NULL != rs->dnsout4) &&
1077 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout4)) &&
1078 (GNUNET_SYSERR == do_dns_read (rs->dnsout4)))
1080 if ((NULL != rs->dnsout6) &&
1081 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout6)) &&
1082 (GNUNET_SYSERR == do_dns_read (rs->dnsout6)))
1085 /* re-schedule read task */
1086 rset = GNUNET_NETWORK_fdset_create ();
1087 if (NULL != rs->dnsout4)
1088 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
1089 if (NULL != rs->dnsout6)
1090 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
1091 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1092 GNUNET_SCHEDULER_NO_TASK,
1093 GNUNET_TIME_absolute_get_remaining (rs->timeout),
1096 &read_response, rs);
1097 GNUNET_NETWORK_fdset_destroy (rset);
1102 * We got a new client. Make sure all new DNS requests pass by its desk.
1105 * @param client the new client
1106 * @param message the init message (unused)
1109 handle_client_init (void *cls GNUNET_UNUSED,
1110 struct GNUNET_SERVER_Client *client,
1111 const struct GNUNET_MessageHeader *message)
1113 struct ClientRecord *cr;
1114 const struct GNUNET_DNS_Register *reg = (const struct GNUNET_DNS_Register*) message;
1116 cr = GNUNET_malloc (sizeof (struct ClientRecord));
1117 cr->client = client;
1118 cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
1119 GNUNET_SERVER_client_keep (client);
1120 GNUNET_CONTAINER_DLL_insert (clients_head,
1123 GNUNET_SERVER_notification_context_add (nc, client);
1124 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1129 * We got a response from a client.
1132 * @param client the client
1133 * @param message the response
1136 handle_client_response (void *cls GNUNET_UNUSED,
1137 struct GNUNET_SERVER_Client *client,
1138 const struct GNUNET_MessageHeader *message)
1140 const struct GNUNET_DNS_Response *resp;
1141 struct RequestRecord *rr;
1146 msize = ntohs (message->size);
1147 if (msize < sizeof (struct GNUNET_DNS_Response))
1150 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1153 resp = (const struct GNUNET_DNS_Response*) message;
1154 off = (uint16_t) resp->request_id;
1155 rr = &requests[off];
1156 if (rr->request_id != resp->request_id)
1158 GNUNET_STATISTICS_update (stats,
1159 gettext_noop ("# Client response discarded (no matching request)"),
1161 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1164 for (i=0;i<rr->client_wait_list_length;i++)
1166 if (NULL == rr->client_wait_list[i])
1168 if (rr->client_wait_list[i]->client != client)
1170 rr->client_wait_list[i] = NULL;
1171 switch (ntohl (resp->drop_flag))
1174 rr->phase = RP_DROP;
1176 case 1: /* no change */
1178 case 2: /* update */
1179 msize -= sizeof (struct GNUNET_DNS_Response);
1180 if ( (sizeof (struct GNUNET_TUN_DnsHeader) > msize) ||
1181 (RP_REQUEST_MONITOR == rr->phase) ||
1182 (RP_RESPONSE_MONITOR == rr->phase) )
1185 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1189 GNUNET_free_non_null (rr->payload);
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1192 _("Changing DNS reply according to client specifications\n"));
1194 rr->payload = GNUNET_malloc (msize);
1195 rr->payload_length = msize;
1196 memcpy (rr->payload, &resp[1], msize);
1197 if (rr->phase == RP_QUERY)
1199 /* clear wait list, we're moving to MODIFY phase next */
1200 GNUNET_array_grow (rr->client_wait_list,
1201 rr->client_wait_list_length,
1207 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1210 /* odd, client was not on our list for the request, that ought
1213 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1218 * Functions with this signature are called whenever a complete
1219 * message is received by the tokenizer from the DNS hijack process.
1221 * @param cls closure
1222 * @param client identification of the client
1223 * @param message the actual message, a DNS request we should handle
1226 process_helper_messages (void *cls GNUNET_UNUSED, void *client,
1227 const struct GNUNET_MessageHeader *message)
1230 const struct GNUNET_TUN_Layer2PacketHeader *tun;
1231 const struct GNUNET_TUN_IPv4Header *ip4;
1232 const struct GNUNET_TUN_IPv6Header *ip6;
1233 const struct GNUNET_TUN_UdpHeader *udp;
1234 const struct GNUNET_TUN_DnsHeader *dns;
1235 struct RequestRecord *rr;
1236 struct sockaddr_in *srca4;
1237 struct sockaddr_in6 *srca6;
1238 struct sockaddr_in *dsta4;
1239 struct sockaddr_in6 *dsta6;
1241 msize = ntohs (message->size);
1242 if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_TUN_IPv4Header))
1244 /* non-IP packet received on TUN!? */
1248 msize -= sizeof (struct GNUNET_MessageHeader);
1249 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
1250 msize -= sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1251 switch (ntohs (tun->proto))
1254 ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1];
1255 if ( (msize < sizeof (struct GNUNET_TUN_IPv4Header)) ||
1256 (ip4->version != 4) ||
1257 (ip4->header_length != sizeof (struct GNUNET_TUN_IPv4Header) / 4) ||
1258 (ntohs(ip4->total_length) != msize) ||
1259 (ip4->protocol != IPPROTO_UDP) )
1261 /* non-IP/UDP packet received on TUN (or with options) */
1262 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1263 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
1266 udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1];
1267 msize -= sizeof (struct GNUNET_TUN_IPv4Header);
1270 ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
1271 if ( (msize < sizeof (struct GNUNET_TUN_IPv6Header)) ||
1272 (ip6->version != 6) ||
1273 (ntohs (ip6->payload_length) != msize) ||
1274 (ip6->next_header != IPPROTO_UDP) )
1276 /* non-IP/UDP packet received on TUN (or with extensions) */
1277 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1278 _("Received malformed IPv6-UDP packet on TUN interface.\n"));
1281 udp = (const struct GNUNET_TUN_UdpHeader*) &ip6[1];
1282 msize -= sizeof (struct GNUNET_TUN_IPv6Header);
1285 /* non-IP packet received on TUN!? */
1286 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1287 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"),
1288 (unsigned int) msize,
1289 ntohs (tun->proto));
1292 if (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader))
1294 /* non-DNS packet received on TUN, ignore */
1295 GNUNET_STATISTICS_update (stats,
1296 gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
1300 msize -= sizeof (struct GNUNET_TUN_UdpHeader);
1301 dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1];
1302 rr = &requests[dns->id];
1304 /* clean up from previous request */
1305 GNUNET_free_non_null (rr->payload);
1307 GNUNET_array_grow (rr->client_wait_list,
1308 rr->client_wait_list_length,
1311 /* setup new request */
1312 rr->phase = RP_INIT;
1313 switch (ntohs (tun->proto))
1317 srca4 = (struct sockaddr_in*) &rr->src_addr;
1318 dsta4 = (struct sockaddr_in*) &rr->dst_addr;
1319 memset (srca4, 0, sizeof (struct sockaddr_in));
1320 memset (dsta4, 0, sizeof (struct sockaddr_in));
1321 srca4->sin_family = AF_INET;
1322 dsta4->sin_family = AF_INET;
1323 srca4->sin_addr = ip4->source_address;
1324 dsta4->sin_addr = ip4->destination_address;
1325 srca4->sin_port = udp->source_port;
1326 dsta4->sin_port = udp->destination_port;
1327 #if HAVE_SOCKADDR_IN_SIN_LEN
1328 srca4->sin_len = sizeof (struct sockaddr_in);
1329 dsta4->sin_len = sizeof (struct sockaddr_in);
1335 srca6 = (struct sockaddr_in6*) &rr->src_addr;
1336 dsta6 = (struct sockaddr_in6*) &rr->dst_addr;
1337 memset (srca6, 0, sizeof (struct sockaddr_in6));
1338 memset (dsta6, 0, sizeof (struct sockaddr_in6));
1339 srca6->sin6_family = AF_INET6;
1340 dsta6->sin6_family = AF_INET6;
1341 srca6->sin6_addr = ip6->source_address;
1342 dsta6->sin6_addr = ip6->destination_address;
1343 srca6->sin6_port = udp->source_port;
1344 dsta6->sin6_port = udp->destination_port;
1345 #if HAVE_SOCKADDR_IN_SIN_LEN
1346 srca6->sin6_len = sizeof (struct sockaddr_in6);
1347 dsta6->sin6_len = sizeof (struct sockaddr_in6);
1354 rr->payload = GNUNET_malloc (msize);
1355 rr->payload_length = msize;
1356 memcpy (rr->payload, dns, msize);
1357 rr->request_id = dns->id | (request_id_gen << 16);
1360 GNUNET_STATISTICS_update (stats,
1361 gettext_noop ("# DNS requests received via TUN interface"),
1363 /* start request processing state machine */
1369 * Process a request via mesh to perform a DNS query.
1371 * @param cls closure, NULL
1372 * @param tunnel connection to the other end
1373 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1374 * @param sender who sent the message
1375 * @param message the actual message
1376 * @param atsi performance data for the connection
1377 * @return GNUNET_OK to keep the connection open,
1378 * GNUNET_SYSERR to close it (signal serious error)
1381 receive_dns_request (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1383 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1384 const struct GNUNET_MessageHeader *message,
1385 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1387 struct TunnelState *ts = *tunnel_ctx;
1388 const struct GNUNET_TUN_DnsHeader *dns;
1389 size_t mlen = ntohs (message->size);
1390 size_t dlen = mlen - sizeof (struct GNUNET_MessageHeader);
1392 struct GNUNET_TUN_DnsHeader *dout;
1393 struct sockaddr_in v4;
1394 struct sockaddr_in6 v6;
1395 struct sockaddr *so;
1398 if (dlen < sizeof (struct GNUNET_TUN_DnsHeader))
1400 GNUNET_break_op (0);
1401 return GNUNET_SYSERR;
1403 dns = (const struct GNUNET_TUN_DnsHeader *) &message[1];
1404 ts->original_id = dns->id;
1405 if (tunnels[ts->my_id] == ts)
1406 tunnels[ts->my_id] = NULL;
1407 ts->my_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1409 tunnels[ts->my_id] = ts;
1410 memcpy (buf, dns, dlen);
1411 dout = (struct GNUNET_TUN_DnsHeader*) buf;
1412 dout->id = ts->my_id;
1413 memset (&v4, 0, sizeof (v4));
1414 memset (&v6, 0, sizeof (v6));
1415 if (1 == inet_pton (AF_INET, dns_exit, &v4.sin_addr))
1417 salen = sizeof (v4);
1418 v4.sin_family = AF_INET;
1419 v4.sin_port = htons (53);
1420 #if HAVE_SOCKADDR_IN_SIN_LEN
1421 v4.sin_len = (u_char) salen;
1423 so = (struct sockaddr *) &v4;
1424 ts->dnsout = get_request_socket (AF_INET);
1426 else if (1 == inet_pton (AF_INET6, dns_exit, &v6.sin6_addr))
1428 salen = sizeof (v6);
1429 v6.sin6_family = AF_INET6;
1430 v6.sin6_port = htons (53);
1431 #if HAVE_SOCKADDR_IN_SIN_LEN
1432 v6.sin6_len = (u_char) salen;
1434 so = (struct sockaddr *) &v6;
1435 ts->dnsout = get_request_socket (AF_INET6);
1440 return GNUNET_SYSERR;
1442 if (NULL == ts->dnsout)
1444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1445 _("Configured DNS exit `%s' is not working / valid.\n"),
1447 return GNUNET_SYSERR;
1452 ts->addrlen = salen;
1453 GNUNET_NETWORK_socket_sendto (ts->dnsout,
1454 buf, dlen, so, salen);
1455 ts->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
1461 * Callback from GNUNET_MESH for new tunnels.
1463 * @param cls closure
1464 * @param tunnel new handle to the tunnel
1465 * @param initiator peer that started the tunnel
1466 * @param ats performance information for the tunnel
1467 * @return initial tunnel context for the tunnel
1470 accept_dns_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1471 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
1472 const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
1474 struct TunnelState *ts = GNUNET_malloc (sizeof (struct TunnelState));
1476 GNUNET_STATISTICS_update (stats,
1477 gettext_noop ("# Inbound MESH tunnels created"),
1479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1480 "Received inbound tunnel from `%s'\n",
1481 GNUNET_i2s (initiator));
1482 ts->tunnel = tunnel;
1488 * Function called by mesh whenever an inbound tunnel is destroyed.
1489 * Should clean up any associated state.
1491 * @param cls closure (set from GNUNET_MESH_connect)
1492 * @param tunnel connection to the other end (henceforth invalid)
1493 * @param tunnel_ctx place where local state associated
1494 * with the tunnel is stored
1497 destroy_dns_tunnel (void *cls GNUNET_UNUSED,
1498 const struct GNUNET_MESH_Tunnel *tunnel,
1501 struct TunnelState *ts = tunnel_ctx;
1503 if (tunnels[ts->my_id] == ts)
1504 tunnels[ts->my_id] = NULL;
1506 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
1507 GNUNET_free_non_null (ts->reply);
1513 * @param cls closure
1514 * @param server the initialized server
1515 * @param cfg_ configuration to use
1518 run (void *cls, struct GNUNET_SERVER_Handle *server,
1519 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1521 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1522 /* callback, cls, type, size */
1523 {&handle_client_init, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
1524 sizeof (struct GNUNET_DNS_Register)},
1525 {&handle_client_response, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 0},
1533 struct in_addr dns_exit4;
1534 struct in6_addr dns_exit6;
1537 stats = GNUNET_STATISTICS_create ("dns", cfg);
1538 nc = GNUNET_SERVER_notification_context_create (server, 1);
1539 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1542 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT")) &&
1544 GNUNET_CONFIGURATION_get_value_string (cfg, "dns",
1547 ( (1 != inet_pton (AF_INET, dns_exit, &dns_exit4)) &&
1548 (1 != inet_pton (AF_INET6, dns_exit, &dns_exit6)) ) ) )
1550 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1551 _("Configured to provide DNS exit, but no valid DNS server configured!\n"));
1552 GNUNET_free_non_null (dns_exit);
1556 helper_argv[0] = GNUNET_strdup ("gnunet-dns");
1557 if (GNUNET_SYSERR ==
1558 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name))
1560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1561 "No entry 'IFNAME' in configuration!\n");
1562 GNUNET_SCHEDULER_shutdown ();
1565 helper_argv[1] = ifc_name;
1566 if ( (GNUNET_SYSERR ==
1567 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6ADDR",
1570 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1571 "No entry 'IPV6ADDR' in configuration!\n");
1572 GNUNET_SCHEDULER_shutdown ();
1575 helper_argv[2] = ipv6addr;
1576 if (GNUNET_SYSERR ==
1577 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6PREFIX",
1580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1581 "No entry 'IPV6PREFIX' in configuration!\n");
1582 GNUNET_SCHEDULER_shutdown ();
1585 helper_argv[3] = ipv6prefix;
1587 if (GNUNET_SYSERR ==
1588 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4ADDR",
1591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1592 "No entry 'IPV4ADDR' in configuration!\n");
1593 GNUNET_SCHEDULER_shutdown ();
1596 helper_argv[4] = ipv4addr;
1597 if (GNUNET_SYSERR ==
1598 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK",
1601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1602 "No entry 'IPV4MASK' in configuration!\n");
1603 GNUNET_SCHEDULER_shutdown ();
1606 helper_argv[5] = ipv4mask;
1607 helper_argv[6] = NULL;
1609 if (NULL != dns_exit)
1611 static struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1612 {&receive_dns_request, GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET, 0},
1615 static GNUNET_MESH_ApplicationType mesh_types[] = {
1616 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1617 GNUNET_APPLICATION_TYPE_END
1619 mesh = GNUNET_MESH_connect (cfg,
1622 &destroy_dns_tunnel,
1626 hijacker = GNUNET_HELPER_start ("gnunet-helper-dns",
1628 &process_helper_messages,
1630 GNUNET_SERVER_add_handlers (server, handlers);
1631 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
1636 * The main function for the dns service.
1638 * @param argc number of arguments from the command line
1639 * @param argv command line arguments
1640 * @return 0 ok, 1 on error
1643 main (int argc, char *const *argv)
1645 return (GNUNET_OK ==
1646 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1647 &run, NULL)) ? 0 : 1;
1651 /* end of gnunet-service-dns.c */