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 rr request 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;
468 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret,
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
473 _("Could not bind to any port: %s\n"),
475 GNUNET_NETWORK_socket_close (ret);
483 * We're done with some request, finish processing.
485 * @param rr request send to the network or just clean up.
488 request_done (struct RequestRecord *rr)
490 struct GNUNET_MessageHeader *hdr;
492 uint16_t source_port;
493 uint16_t destination_port;
495 GNUNET_array_grow (rr->client_wait_list,
496 rr->client_wait_list_length,
498 if (RP_RESPONSE_MONITOR != rr->phase)
500 /* no response, drop */
505 /* send response via hijacker */
506 reply_len = sizeof (struct GNUNET_MessageHeader);
507 reply_len += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
508 switch (rr->src_addr.ss_family)
511 reply_len += sizeof (struct GNUNET_TUN_IPv4Header);
514 reply_len += sizeof (struct GNUNET_TUN_IPv6Header);
521 reply_len += sizeof (struct GNUNET_TUN_UdpHeader);
522 reply_len += rr->payload_length;
523 if (reply_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
525 /* response too big, drop */
526 GNUNET_break (0); /* how can this be? */
533 struct GNUNET_TUN_IPv4Header ip4;
534 struct GNUNET_TUN_IPv6Header ip6;
536 /* first, GNUnet message header */
537 hdr = (struct GNUNET_MessageHeader*) buf;
538 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
539 hdr->size = htons ((uint16_t) reply_len);
540 off = sizeof (struct GNUNET_MessageHeader);
542 /* first, TUN header */
544 struct GNUNET_TUN_Layer2PacketHeader tun;
546 tun.flags = htons (0);
547 if (rr->src_addr.ss_family == AF_INET)
548 tun.proto = htons (ETH_P_IPV4);
550 tun.proto = htons (ETH_P_IPV6);
551 memcpy (&buf[off], &tun, sizeof (struct GNUNET_TUN_Layer2PacketHeader));
552 off += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
556 switch (rr->src_addr.ss_family)
560 struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr;
561 struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr;
563 source_port = dst->sin_port;
564 destination_port = src->sin_port;
565 GNUNET_TUN_initialize_ipv4_header (&ip4,
567 reply_len - off - sizeof (struct GNUNET_TUN_IPv4Header),
570 memcpy (&buf[off], &ip4, sizeof (ip4));
576 struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr;
577 struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr;
579 source_port = dst->sin6_port;
580 destination_port = src->sin6_port;
581 GNUNET_TUN_initialize_ipv6_header (&ip6,
583 reply_len - sizeof (struct GNUNET_TUN_IPv6Header),
586 memcpy (&buf[off], &ip6, sizeof (ip6));
596 struct GNUNET_TUN_UdpHeader udp;
598 udp.source_port = source_port;
599 udp.destination_port = destination_port;
600 udp.len = htons (reply_len - off);
601 if (AF_INET == rr->src_addr.ss_family)
602 GNUNET_TUN_calculate_udp4_checksum (&ip4,
607 GNUNET_TUN_calculate_udp6_checksum (&ip6,
611 memcpy (&buf[off], &udp, sizeof (udp));
615 /* now DNS payload */
617 memcpy (&buf[off], rr->payload, rr->payload_length);
618 off += rr->payload_length;
620 /* final checks & sending */
621 GNUNET_assert (off == reply_len);
622 GNUNET_HELPER_send (hijacker,
626 GNUNET_STATISTICS_update (stats,
627 gettext_noop ("# DNS requests answered via TUN interface"),
630 /* clean up, we're done */
636 * Show the payload of the given request record to the client
637 * (and wait for a response).
639 * @param rr request to send to client
640 * @param client client to send the response to
643 send_request_to_client (struct RequestRecord *rr,
644 struct GNUNET_SERVER_Client *client)
646 char buf[sizeof (struct GNUNET_DNS_Request) + rr->payload_length];
647 struct GNUNET_DNS_Request *req;
649 if (sizeof (buf) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
655 req = (struct GNUNET_DNS_Request*) buf;
656 req->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
657 req->header.size = htons (sizeof (buf));
658 req->reserved = htonl (0);
659 req->request_id = rr->request_id;
660 memcpy (&req[1], rr->payload, rr->payload_length);
661 GNUNET_SERVER_notification_context_unicast (nc,
669 * Read a DNS response from the (unhindered) UDP-Socket
671 * @param cls socket to read from
672 * @param tc scheduler context (must be shutdown or read ready)
675 read_response (void *cls,
676 const struct GNUNET_SCHEDULER_TaskContext *tc);
680 * Get a socket of the specified address family to send out a
681 * UDP DNS request to the Internet.
683 * @param af desired address family
684 * @return NULL on error (given AF not "supported")
686 static struct GNUNET_NETWORK_Handle *
687 get_request_socket (int af)
689 struct RequestSocket *rs;
690 struct GNUNET_NETWORK_FDSet *rset;
691 static struct GNUNET_NETWORK_Handle *ret;
693 rs = &sockets[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
695 rs->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
699 if (NULL == rs->dnsout4)
700 rs->dnsout4 = open_socket (AF_INET);
704 if (NULL == rs->dnsout6)
705 rs->dnsout6 = open_socket (AF_INET6);
711 if (GNUNET_SCHEDULER_NO_TASK != rs->read_task)
713 GNUNET_SCHEDULER_cancel (rs->read_task);
714 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
716 if ( (NULL == rs->dnsout4) &&
717 (NULL == rs->dnsout6) )
719 rset = GNUNET_NETWORK_fdset_create ();
720 if (NULL != rs->dnsout4)
721 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
722 if (NULL != rs->dnsout6)
723 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
724 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
725 GNUNET_SCHEDULER_NO_TASK,
730 GNUNET_NETWORK_fdset_destroy (rset);
736 * A client has completed its processing for this
739 * @param rr request to process further
742 next_phase (struct RequestRecord *rr)
744 struct ClientRecord *cr;
749 if (rr->phase == RP_DROP)
755 for (j=0;j<rr->client_wait_list_length;j++)
757 if (NULL != rr->client_wait_list[j])
765 send_request_to_client (rr, rr->client_wait_list[nz]->client);
768 /* done with current phase, advance! */
772 rr->phase = RP_REQUEST_MONITOR;
773 for (cr = clients_head; NULL != cr; cr = cr->next)
775 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR))
776 GNUNET_array_append (rr->client_wait_list,
777 rr->client_wait_list_length,
782 case RP_REQUEST_MONITOR:
783 rr->phase = RP_QUERY;
784 for (cr = clients_head; NULL != cr; cr = cr->next)
786 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION))
787 GNUNET_array_append (rr->client_wait_list,
788 rr->client_wait_list_length,
794 switch (rr->dst_addr.ss_family)
797 salen = sizeof (struct sockaddr_in);
800 salen = sizeof (struct sockaddr_in6);
806 rr->phase = RP_INTERNET_DNS;
807 rr->dnsout = get_request_socket (rr->dst_addr.ss_family);
808 if (NULL == rr->dnsout)
810 GNUNET_STATISTICS_update (stats,
811 gettext_noop ("# DNS exit failed (failed to open socket)"),
816 GNUNET_NETWORK_socket_sendto (rr->dnsout,
819 (struct sockaddr*) &rr->dst_addr,
821 rr->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
823 case RP_INTERNET_DNS:
824 rr->phase = RP_MODIFY;
825 for (cr = clients_head; NULL != cr; cr = cr->next)
827 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
828 GNUNET_array_append (rr->client_wait_list,
829 rr->client_wait_list_length,
835 rr->phase = RP_RESPONSE_MONITOR;
836 for (cr = clients_head; NULL != cr; cr = cr->next)
838 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
839 GNUNET_array_append (rr->client_wait_list,
840 rr->client_wait_list_length,
845 case RP_RESPONSE_MONITOR:
860 * A client disconnected, clean up after it.
863 * @param client handle of client that disconnected
866 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
868 struct ClientRecord *cr;
869 struct RequestRecord *rr;
873 for (cr = clients_head; NULL != cr; cr = cr->next)
875 if (cr->client == client)
877 GNUNET_SERVER_client_drop (client);
878 GNUNET_CONTAINER_DLL_remove (clients_head,
881 for (i=0;i<UINT16_MAX;i++)
884 if (0 == rr->client_wait_list_length)
885 continue; /* not in use */
886 for (j=0;j<rr->client_wait_list_length;j++)
888 if (rr->client_wait_list[j] == cr)
890 rr->client_wait_list[j] = NULL;
903 * We got a reply from DNS for a request of a MESH tunnel. Send it
904 * via the tunnel (after changing the request ID back).
906 * @param cls the 'struct TunnelState'
907 * @param size number of bytes available in buf
908 * @param buf where to copy the reply
909 * @return number of bytes written to buf
912 transmit_reply_to_mesh (void *cls,
916 struct TunnelState *ts = cls;
920 struct GNUNET_MessageHeader hdr;
921 struct GNUNET_TUN_DnsHeader dns;
924 GNUNET_assert (ts->reply != NULL);
927 ret = sizeof (struct GNUNET_MessageHeader) + ts->reply_length;
928 GNUNET_assert (ret <= size);
929 hdr.size = htons (ret);
930 hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET);
931 memcpy (&dns, ts->reply, sizeof (dns));
932 dns.id = ts->original_id;
934 memcpy (&cbuf[off], &hdr, sizeof (hdr));
936 memcpy (&cbuf[off], &dns, sizeof (dns));
938 memcpy (&cbuf[off], &ts->reply[sizeof (dns)], ts->reply_length - sizeof (dns));
939 off += ts->reply_length - sizeof (dns);
940 GNUNET_free (ts->reply);
942 ts->reply_length = 0;
943 GNUNET_assert (ret == off);
949 * Actually do the reading of a DNS packet from our UDP socket and see
950 * if we have a valid, matching, pending request.
952 * @param dnsout socket to read from
953 * @param GNUNET_OK on success, GNUNET_NO on drop, GNUNET_SYSERR on IO-errors (closed socket)
956 do_dns_read (struct GNUNET_NETWORK_Handle *dnsout)
958 struct sockaddr_storage addr;
960 struct GNUNET_TUN_DnsHeader *dns;
961 struct RequestRecord *rr;
962 struct TunnelState *ts;
967 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
969 /* conservative choice: */
973 /* port the code above? */
978 unsigned char buf[len];
980 addrlen = sizeof (addr);
981 memset (&addr, 0, sizeof (addr));
982 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
984 (struct sockaddr*) &addr, &addrlen);
987 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
988 GNUNET_NETWORK_socket_close (dnsout);
989 return GNUNET_SYSERR;
991 if (sizeof (struct GNUNET_TUN_DnsHeader) > r)
993 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
994 _("Received DNS response that is too small (%u bytes)"),
998 dns = (struct GNUNET_TUN_DnsHeader *) buf;
999 /* Handle case that this is a reply to a request from a MESH DNS tunnel */
1000 ts = tunnels[dns->id];
1001 if ( (NULL == ts) ||
1002 (ts->dnsout != dnsout) ||
1003 (addrlen != ts->addrlen) ||
1004 (0 != memcmp (&ts->addr,
1007 (0 == GNUNET_TIME_absolute_get_remaining (ts->timeout).rel_value) )
1008 ts = NULL; /* DNS responder address missmatch */
1011 tunnels[dns->id] = NULL;
1012 GNUNET_free_non_null (ts->reply);
1013 ts->reply = GNUNET_malloc (r);
1014 ts->reply_length = r;
1015 memcpy (ts->reply, dns, r);
1017 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
1018 ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
1020 GNUNET_TIME_UNIT_FOREVER_REL,
1022 sizeof (struct GNUNET_MessageHeader) + r,
1023 &transmit_reply_to_mesh,
1026 /* Handle case that this is a reply to a local request (intercepted from TUN interface) */
1027 rr = &requests[dns->id];
1028 if ( (rr->phase != RP_INTERNET_DNS) ||
1029 (rr->dnsout != dnsout) ||
1030 (0 != memcmp (&rr->dst_addr,
1033 (0 == GNUNET_TIME_absolute_get_remaining (rr->timeout).rel_value) )
1037 /* unexpected / bogus reply */
1038 GNUNET_STATISTICS_update (stats,
1039 gettext_noop ("# External DNS response discarded (no matching request)"),
1044 GNUNET_free_non_null (rr->payload);
1045 rr->payload = GNUNET_malloc (r);
1046 memcpy (rr->payload, buf, r);
1047 rr->payload_length = r;
1055 * Read a DNS response from the (unhindered) UDP-Socket
1057 * @param cls socket to read from
1058 * @param tc scheduler context (must be shutdown or read ready)
1061 read_response (void *cls,
1062 const struct GNUNET_SCHEDULER_TaskContext *tc)
1064 struct RequestSocket *rs = cls;
1065 struct GNUNET_NETWORK_FDSet *rset;
1067 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
1068 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1070 /* timeout or shutdown */
1074 /* read and process ready sockets */
1075 if ((NULL != rs->dnsout4) &&
1076 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout4)) &&
1077 (GNUNET_SYSERR == do_dns_read (rs->dnsout4)))
1079 if ((NULL != rs->dnsout6) &&
1080 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout6)) &&
1081 (GNUNET_SYSERR == do_dns_read (rs->dnsout6)))
1084 /* re-schedule read task */
1085 rset = GNUNET_NETWORK_fdset_create ();
1086 if (NULL != rs->dnsout4)
1087 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
1088 if (NULL != rs->dnsout6)
1089 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
1090 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1091 GNUNET_SCHEDULER_NO_TASK,
1092 GNUNET_TIME_absolute_get_remaining (rs->timeout),
1095 &read_response, rs);
1096 GNUNET_NETWORK_fdset_destroy (rset);
1101 * We got a new client. Make sure all new DNS requests pass by its desk.
1104 * @param client the new client
1105 * @param message the init message (unused)
1108 handle_client_init (void *cls GNUNET_UNUSED,
1109 struct GNUNET_SERVER_Client *client,
1110 const struct GNUNET_MessageHeader *message)
1112 struct ClientRecord *cr;
1113 const struct GNUNET_DNS_Register *reg = (const struct GNUNET_DNS_Register*) message;
1115 cr = GNUNET_malloc (sizeof (struct ClientRecord));
1116 cr->client = client;
1117 cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
1118 GNUNET_SERVER_client_keep (client);
1119 GNUNET_CONTAINER_DLL_insert (clients_head,
1122 GNUNET_SERVER_notification_context_add (nc, client);
1123 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1128 * We got a response from a client.
1131 * @param client the client
1132 * @param message the response
1135 handle_client_response (void *cls GNUNET_UNUSED,
1136 struct GNUNET_SERVER_Client *client,
1137 const struct GNUNET_MessageHeader *message)
1139 const struct GNUNET_DNS_Response *resp;
1140 struct RequestRecord *rr;
1145 msize = ntohs (message->size);
1146 if (msize < sizeof (struct GNUNET_DNS_Response))
1149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1152 resp = (const struct GNUNET_DNS_Response*) message;
1153 off = (uint16_t) resp->request_id;
1154 rr = &requests[off];
1155 if (rr->request_id != resp->request_id)
1157 GNUNET_STATISTICS_update (stats,
1158 gettext_noop ("# Client response discarded (no matching request)"),
1160 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1163 for (i=0;i<rr->client_wait_list_length;i++)
1165 if (NULL == rr->client_wait_list[i])
1167 if (rr->client_wait_list[i]->client != client)
1169 rr->client_wait_list[i] = NULL;
1170 switch (ntohl (resp->drop_flag))
1173 rr->phase = RP_DROP;
1175 case 1: /* no change */
1177 case 2: /* update */
1178 msize -= sizeof (struct GNUNET_DNS_Response);
1179 if ( (sizeof (struct GNUNET_TUN_DnsHeader) > msize) ||
1180 (RP_REQUEST_MONITOR == rr->phase) ||
1181 (RP_RESPONSE_MONITOR == rr->phase) )
1184 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1188 GNUNET_free_non_null (rr->payload);
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 _("Changing DNS reply according to client specifications\n"));
1193 rr->payload = GNUNET_malloc (msize);
1194 rr->payload_length = msize;
1195 memcpy (rr->payload, &resp[1], msize);
1196 if (rr->phase == RP_QUERY)
1198 /* clear wait list, we're moving to MODIFY phase next */
1199 GNUNET_array_grow (rr->client_wait_list,
1200 rr->client_wait_list_length,
1206 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1209 /* odd, client was not on our list for the request, that ought
1212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1217 * Functions with this signature are called whenever a complete
1218 * message is received by the tokenizer from the DNS hijack process.
1220 * @param cls closure
1221 * @param client identification of the client
1222 * @param message the actual message, a DNS request we should handle
1225 process_helper_messages (void *cls GNUNET_UNUSED, void *client,
1226 const struct GNUNET_MessageHeader *message)
1229 const struct GNUNET_TUN_Layer2PacketHeader *tun;
1230 const struct GNUNET_TUN_IPv4Header *ip4;
1231 const struct GNUNET_TUN_IPv6Header *ip6;
1232 const struct GNUNET_TUN_UdpHeader *udp;
1233 const struct GNUNET_TUN_DnsHeader *dns;
1234 struct RequestRecord *rr;
1235 struct sockaddr_in *srca4;
1236 struct sockaddr_in6 *srca6;
1237 struct sockaddr_in *dsta4;
1238 struct sockaddr_in6 *dsta6;
1240 msize = ntohs (message->size);
1241 if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_TUN_IPv4Header))
1243 /* non-IP packet received on TUN!? */
1247 msize -= sizeof (struct GNUNET_MessageHeader);
1248 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
1249 msize -= sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1250 switch (ntohs (tun->proto))
1253 ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1];
1254 if ( (msize < sizeof (struct GNUNET_TUN_IPv4Header)) ||
1255 (ip4->version != 4) ||
1256 (ip4->header_length != sizeof (struct GNUNET_TUN_IPv4Header) / 4) ||
1257 (ntohs(ip4->total_length) != msize) ||
1258 (ip4->protocol != IPPROTO_UDP) )
1260 /* non-IP/UDP packet received on TUN (or with options) */
1261 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1262 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
1265 udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1];
1266 msize -= sizeof (struct GNUNET_TUN_IPv4Header);
1269 ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
1270 if ( (msize < sizeof (struct GNUNET_TUN_IPv6Header)) ||
1271 (ip6->version != 6) ||
1272 (ntohs (ip6->payload_length) != msize) ||
1273 (ip6->next_header != IPPROTO_UDP) )
1275 /* non-IP/UDP packet received on TUN (or with extensions) */
1276 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1277 _("Received malformed IPv6-UDP packet on TUN interface.\n"));
1280 udp = (const struct GNUNET_TUN_UdpHeader*) &ip6[1];
1281 msize -= sizeof (struct GNUNET_TUN_IPv6Header);
1284 /* non-IP packet received on TUN!? */
1285 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1286 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"),
1287 (unsigned int) msize,
1288 ntohs (tun->proto));
1291 if (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader))
1293 /* non-DNS packet received on TUN, ignore */
1294 GNUNET_STATISTICS_update (stats,
1295 gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
1299 msize -= sizeof (struct GNUNET_TUN_UdpHeader);
1300 dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1];
1301 rr = &requests[dns->id];
1303 /* clean up from previous request */
1304 GNUNET_free_non_null (rr->payload);
1306 GNUNET_array_grow (rr->client_wait_list,
1307 rr->client_wait_list_length,
1310 /* setup new request */
1311 rr->phase = RP_INIT;
1312 switch (ntohs (tun->proto))
1316 srca4 = (struct sockaddr_in*) &rr->src_addr;
1317 dsta4 = (struct sockaddr_in*) &rr->dst_addr;
1318 memset (srca4, 0, sizeof (struct sockaddr_in));
1319 memset (dsta4, 0, sizeof (struct sockaddr_in));
1320 srca4->sin_family = AF_INET;
1321 dsta4->sin_family = AF_INET;
1322 srca4->sin_addr = ip4->source_address;
1323 dsta4->sin_addr = ip4->destination_address;
1324 srca4->sin_port = udp->source_port;
1325 dsta4->sin_port = udp->destination_port;
1326 #if HAVE_SOCKADDR_IN_SIN_LEN
1327 srca4->sin_len = sizeof (struct sockaddr_in))
1328 dsta4->sin_len = sizeof (struct sockaddr_in);
1334 srca6 = (struct sockaddr_in6*) &rr->src_addr;
1335 dsta6 = (struct sockaddr_in6*) &rr->dst_addr;
1336 memset (srca6, 0, sizeof (struct sockaddr_in6));
1337 memset (dsta6, 0, sizeof (struct sockaddr_in6));
1338 srca6->sin6_family = AF_INET6;
1339 dsta6->sin6_family = AF_INET6;
1340 srca6->sin6_addr = ip6->source_address;
1341 dsta6->sin6_addr = ip6->destination_address;
1342 srca6->sin6_port = udp->source_port;
1343 dsta6->sin6_port = udp->destination_port;
1344 #if HAVE_SOCKADDR_IN_SIN_LEN
1345 srca6->sin6_len = sizeof (struct sockaddr_in6);
1346 dsta6->sin6_len = sizeof (struct sockaddr_in6);
1353 rr->payload = GNUNET_malloc (msize);
1354 rr->payload_length = msize;
1355 memcpy (rr->payload, dns, msize);
1356 rr->request_id = dns->id | (request_id_gen << 16);
1359 GNUNET_STATISTICS_update (stats,
1360 gettext_noop ("# DNS requests received via TUN interface"),
1362 /* start request processing state machine */
1368 * Process a request via mesh to perform a DNS query.
1370 * @param cls closure, NULL
1371 * @param tunnel connection to the other end
1372 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1373 * @param sender who sent the message
1374 * @param message the actual message
1375 * @param atsi performance data for the connection
1376 * @return GNUNET_OK to keep the connection open,
1377 * GNUNET_SYSERR to close it (signal serious error)
1380 receive_dns_request (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1382 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1383 const struct GNUNET_MessageHeader *message,
1384 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1386 struct TunnelState *ts = *tunnel_ctx;
1387 const struct GNUNET_TUN_DnsHeader *dns;
1388 size_t mlen = ntohs (message->size);
1389 size_t dlen = mlen - sizeof (struct GNUNET_MessageHeader);
1391 struct GNUNET_TUN_DnsHeader *dout;
1392 struct sockaddr_in v4;
1393 struct sockaddr_in6 v6;
1394 struct sockaddr *so;
1397 if (dlen < sizeof (struct GNUNET_TUN_DnsHeader))
1399 GNUNET_break_op (0);
1400 return GNUNET_SYSERR;
1402 dns = (const struct GNUNET_TUN_DnsHeader *) &message[1];
1403 ts->original_id = dns->id;
1404 if (tunnels[ts->my_id] == ts)
1405 tunnels[ts->my_id] = NULL;
1406 ts->my_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1408 tunnels[ts->my_id] = ts;
1409 memcpy (buf, dns, dlen);
1410 dout = (struct GNUNET_TUN_DnsHeader*) buf;
1411 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 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);
1437 if (NULL == ts->dnsout)
1439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1440 _("Configured DNS exit `%s' is not working / valid.\n"),
1442 return GNUNET_SYSERR;
1447 ts->addrlen = salen;
1448 GNUNET_NETWORK_socket_sendto (ts->dnsout,
1449 buf, dlen, so, salen);
1450 ts->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
1456 * Callback from GNUNET_MESH for new tunnels.
1458 * @param cls closure
1459 * @param tunnel new handle to the tunnel
1460 * @param initiator peer that started the tunnel
1461 * @param ats performance information for the tunnel
1462 * @return initial tunnel context for the tunnel
1465 accept_dns_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1466 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
1467 const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
1469 struct TunnelState *ts = GNUNET_malloc (sizeof (struct TunnelState));
1471 GNUNET_STATISTICS_update (stats,
1472 gettext_noop ("# Inbound MESH tunnels created"),
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1475 "Received inbound tunnel from `%s'\n",
1476 GNUNET_i2s (initiator));
1477 ts->tunnel = tunnel;
1483 * Function called by mesh whenever an inbound tunnel is destroyed.
1484 * Should clean up any associated state.
1486 * @param cls closure (set from GNUNET_MESH_connect)
1487 * @param tunnel connection to the other end (henceforth invalid)
1488 * @param tunnel_ctx place where local state associated
1489 * with the tunnel is stored
1492 destroy_dns_tunnel (void *cls GNUNET_UNUSED,
1493 const struct GNUNET_MESH_Tunnel *tunnel,
1496 struct TunnelState *ts = tunnel_ctx;
1498 if (tunnels[ts->my_id] == ts)
1499 tunnels[ts->my_id] = NULL;
1501 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
1502 GNUNET_free_non_null (ts->reply);
1508 * @param cls closure
1509 * @param server the initialized server
1510 * @param cfg_ configuration to use
1513 run (void *cls, struct GNUNET_SERVER_Handle *server,
1514 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1516 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1517 /* callback, cls, type, size */
1518 {&handle_client_init, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
1519 sizeof (struct GNUNET_DNS_Register)},
1520 {&handle_client_response, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 0},
1528 struct in_addr dns_exit4;
1529 struct in6_addr dns_exit6;
1532 stats = GNUNET_STATISTICS_create ("dns", cfg);
1533 nc = GNUNET_SERVER_notification_context_create (server, 1);
1534 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1537 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT")) &&
1539 GNUNET_CONFIGURATION_get_value_string (cfg, "dns",
1542 ( (1 != inet_pton (AF_INET, dns_exit, &dns_exit4)) &&
1543 (1 != inet_pton (AF_INET6, dns_exit, &dns_exit6)) ) ) )
1544 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1545 _("Configured to provide DNS exit, but no valid DNS server configured!\n"));
1547 helper_argv[0] = GNUNET_strdup ("gnunet-dns");
1548 if (GNUNET_SYSERR ==
1549 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name))
1551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1552 "No entry 'IFNAME' in configuration!\n");
1553 GNUNET_SCHEDULER_shutdown ();
1556 helper_argv[1] = ifc_name;
1557 if ( (GNUNET_SYSERR ==
1558 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6ADDR",
1561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1562 "No entry 'IPV6ADDR' in configuration!\n");
1563 GNUNET_SCHEDULER_shutdown ();
1566 helper_argv[2] = ipv6addr;
1567 if (GNUNET_SYSERR ==
1568 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6PREFIX",
1571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1572 "No entry 'IPV6PREFIX' in configuration!\n");
1573 GNUNET_SCHEDULER_shutdown ();
1576 helper_argv[3] = ipv6prefix;
1578 if (GNUNET_SYSERR ==
1579 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4ADDR",
1582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1583 "No entry 'IPV4ADDR' in configuration!\n");
1584 GNUNET_SCHEDULER_shutdown ();
1587 helper_argv[4] = ipv4addr;
1588 if (GNUNET_SYSERR ==
1589 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK",
1592 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1593 "No entry 'IPV4MASK' in configuration!\n");
1594 GNUNET_SCHEDULER_shutdown ();
1597 helper_argv[5] = ipv4mask;
1598 helper_argv[6] = NULL;
1600 if (NULL != dns_exit)
1602 static struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1603 {&receive_dns_request, GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET, 0},
1606 static GNUNET_MESH_ApplicationType mesh_types[] = {
1607 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1608 GNUNET_APPLICATION_TYPE_END
1610 mesh = GNUNET_MESH_connect (cfg,
1613 &destroy_dns_tunnel,
1617 hijacker = GNUNET_HELPER_start ("gnunet-helper-dns",
1619 &process_helper_messages,
1621 GNUNET_SERVER_add_handlers (server, handlers);
1622 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
1627 * The main function for the dns service.
1629 * @param argc number of arguments from the command line
1630 * @param argv command line arguments
1631 * @return 0 ok, 1 on error
1634 main (int argc, char *const *argv)
1636 return (GNUNET_OK ==
1637 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1638 &run, NULL)) ? 0 : 1;
1642 /* end of gnunet-service-dns.c */