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);
434 GNUNET_MESH_disconnect(mesh);
441 * Open source port for sending DNS requests
443 * @param af AF_INET or AF_INET6
444 * @return GNUNET_OK on success
446 static struct GNUNET_NETWORK_Handle *
449 struct sockaddr_in a4;
450 struct sockaddr_in6 a6;
453 struct GNUNET_NETWORK_Handle *ret;
455 ret = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, 0);
461 memset (&a4, 0, alen = sizeof (struct sockaddr_in));
462 sa = (struct sockaddr *) &a4;
465 memset (&a6, 0, alen = sizeof (struct sockaddr_in6));
466 sa = (struct sockaddr *) &a6;
470 GNUNET_NETWORK_socket_close (ret);
474 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret,
478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
479 _("Could not bind to any port: %s\n"),
481 GNUNET_NETWORK_socket_close (ret);
489 * We're done with some request, finish processing.
491 * @param rr request send to the network or just clean up.
494 request_done (struct RequestRecord *rr)
496 struct GNUNET_MessageHeader *hdr;
498 uint16_t source_port;
499 uint16_t destination_port;
501 GNUNET_array_grow (rr->client_wait_list,
502 rr->client_wait_list_length,
504 if (RP_RESPONSE_MONITOR != rr->phase)
506 /* no response, drop */
511 /* send response via hijacker */
512 reply_len = sizeof (struct GNUNET_MessageHeader);
513 reply_len += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
514 switch (rr->src_addr.ss_family)
517 reply_len += sizeof (struct GNUNET_TUN_IPv4Header);
520 reply_len += sizeof (struct GNUNET_TUN_IPv6Header);
527 reply_len += sizeof (struct GNUNET_TUN_UdpHeader);
528 reply_len += rr->payload_length;
529 if (reply_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
531 /* response too big, drop */
532 GNUNET_break (0); /* how can this be? */
539 struct GNUNET_TUN_IPv4Header ip4;
540 struct GNUNET_TUN_IPv6Header ip6;
542 /* first, GNUnet message header */
543 hdr = (struct GNUNET_MessageHeader*) buf;
544 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
545 hdr->size = htons ((uint16_t) reply_len);
546 off = sizeof (struct GNUNET_MessageHeader);
548 /* first, TUN header */
550 struct GNUNET_TUN_Layer2PacketHeader tun;
552 tun.flags = htons (0);
553 if (rr->src_addr.ss_family == AF_INET)
554 tun.proto = htons (ETH_P_IPV4);
556 tun.proto = htons (ETH_P_IPV6);
557 memcpy (&buf[off], &tun, sizeof (struct GNUNET_TUN_Layer2PacketHeader));
558 off += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
562 switch (rr->src_addr.ss_family)
566 struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr;
567 struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr;
569 source_port = dst->sin_port;
570 destination_port = src->sin_port;
571 GNUNET_TUN_initialize_ipv4_header (&ip4,
573 reply_len - off - sizeof (struct GNUNET_TUN_IPv4Header),
576 memcpy (&buf[off], &ip4, sizeof (ip4));
582 struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr;
583 struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr;
585 source_port = dst->sin6_port;
586 destination_port = src->sin6_port;
587 GNUNET_TUN_initialize_ipv6_header (&ip6,
589 reply_len - sizeof (struct GNUNET_TUN_IPv6Header),
592 memcpy (&buf[off], &ip6, sizeof (ip6));
602 struct GNUNET_TUN_UdpHeader udp;
604 udp.source_port = source_port;
605 udp.destination_port = destination_port;
606 udp.len = htons (reply_len - off);
607 if (AF_INET == rr->src_addr.ss_family)
608 GNUNET_TUN_calculate_udp4_checksum (&ip4,
613 GNUNET_TUN_calculate_udp6_checksum (&ip6,
617 memcpy (&buf[off], &udp, sizeof (udp));
621 /* now DNS payload */
623 memcpy (&buf[off], rr->payload, rr->payload_length);
624 off += rr->payload_length;
626 /* final checks & sending */
627 GNUNET_assert (off == reply_len);
628 GNUNET_HELPER_send (hijacker,
632 GNUNET_STATISTICS_update (stats,
633 gettext_noop ("# DNS requests answered via TUN interface"),
636 /* clean up, we're done */
642 * Show the payload of the given request record to the client
643 * (and wait for a response).
645 * @param rr request to send to client
646 * @param client client to send the response to
649 send_request_to_client (struct RequestRecord *rr,
650 struct GNUNET_SERVER_Client *client)
652 char buf[sizeof (struct GNUNET_DNS_Request) + rr->payload_length];
653 struct GNUNET_DNS_Request *req;
655 if (sizeof (buf) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
661 req = (struct GNUNET_DNS_Request*) buf;
662 req->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
663 req->header.size = htons (sizeof (buf));
664 req->reserved = htonl (0);
665 req->request_id = rr->request_id;
666 memcpy (&req[1], rr->payload, rr->payload_length);
667 GNUNET_SERVER_notification_context_unicast (nc,
675 * Read a DNS response from the (unhindered) UDP-Socket
677 * @param cls socket to read from
678 * @param tc scheduler context (must be shutdown or read ready)
681 read_response (void *cls,
682 const struct GNUNET_SCHEDULER_TaskContext *tc);
686 * Get a socket of the specified address family to send out a
687 * UDP DNS request to the Internet.
689 * @param af desired address family
690 * @return NULL on error (given AF not "supported")
692 static struct GNUNET_NETWORK_Handle *
693 get_request_socket (int af)
695 struct RequestSocket *rs;
696 struct GNUNET_NETWORK_FDSet *rset;
697 static struct GNUNET_NETWORK_Handle *ret;
699 rs = &sockets[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
701 rs->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
705 if (NULL == rs->dnsout4)
706 rs->dnsout4 = open_socket (AF_INET);
710 if (NULL == rs->dnsout6)
711 rs->dnsout6 = open_socket (AF_INET6);
717 if (GNUNET_SCHEDULER_NO_TASK != rs->read_task)
719 GNUNET_SCHEDULER_cancel (rs->read_task);
720 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
722 if ( (NULL == rs->dnsout4) &&
723 (NULL == rs->dnsout6) )
725 rset = GNUNET_NETWORK_fdset_create ();
726 if (NULL != rs->dnsout4)
727 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
728 if (NULL != rs->dnsout6)
729 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
730 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
731 GNUNET_SCHEDULER_NO_TASK,
736 GNUNET_NETWORK_fdset_destroy (rset);
742 * A client has completed its processing for this
745 * @param rr request to process further
748 next_phase (struct RequestRecord *rr)
750 struct ClientRecord *cr;
755 if (rr->phase == RP_DROP)
761 for (j=0;j<rr->client_wait_list_length;j++)
763 if (NULL != rr->client_wait_list[j])
771 send_request_to_client (rr, rr->client_wait_list[nz]->client);
774 /* done with current phase, advance! */
778 rr->phase = RP_REQUEST_MONITOR;
779 for (cr = clients_head; NULL != cr; cr = cr->next)
781 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR))
782 GNUNET_array_append (rr->client_wait_list,
783 rr->client_wait_list_length,
788 case RP_REQUEST_MONITOR:
789 rr->phase = RP_QUERY;
790 for (cr = clients_head; NULL != cr; cr = cr->next)
792 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION))
793 GNUNET_array_append (rr->client_wait_list,
794 rr->client_wait_list_length,
800 switch (rr->dst_addr.ss_family)
803 salen = sizeof (struct sockaddr_in);
806 salen = sizeof (struct sockaddr_in6);
812 rr->phase = RP_INTERNET_DNS;
813 rr->dnsout = get_request_socket (rr->dst_addr.ss_family);
814 if (NULL == rr->dnsout)
816 GNUNET_STATISTICS_update (stats,
817 gettext_noop ("# DNS exit failed (failed to open socket)"),
822 GNUNET_NETWORK_socket_sendto (rr->dnsout,
825 (struct sockaddr*) &rr->dst_addr,
827 rr->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
829 case RP_INTERNET_DNS:
830 rr->phase = RP_MODIFY;
831 for (cr = clients_head; NULL != cr; cr = cr->next)
833 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
834 GNUNET_array_append (rr->client_wait_list,
835 rr->client_wait_list_length,
841 rr->phase = RP_RESPONSE_MONITOR;
842 for (cr = clients_head; NULL != cr; cr = cr->next)
844 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
845 GNUNET_array_append (rr->client_wait_list,
846 rr->client_wait_list_length,
851 case RP_RESPONSE_MONITOR:
866 * A client disconnected, clean up after it.
869 * @param client handle of client that disconnected
872 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
874 struct ClientRecord *cr;
875 struct RequestRecord *rr;
879 for (cr = clients_head; NULL != cr; cr = cr->next)
881 if (cr->client == client)
883 GNUNET_SERVER_client_drop (client);
884 GNUNET_CONTAINER_DLL_remove (clients_head,
887 for (i=0;i<UINT16_MAX;i++)
890 if (0 == rr->client_wait_list_length)
891 continue; /* not in use */
892 for (j=0;j<rr->client_wait_list_length;j++)
894 if (rr->client_wait_list[j] == cr)
896 rr->client_wait_list[j] = NULL;
909 * We got a reply from DNS for a request of a MESH tunnel. Send it
910 * via the tunnel (after changing the request ID back).
912 * @param cls the 'struct TunnelState'
913 * @param size number of bytes available in buf
914 * @param buf where to copy the reply
915 * @return number of bytes written to buf
918 transmit_reply_to_mesh (void *cls,
922 struct TunnelState *ts = cls;
926 struct GNUNET_MessageHeader hdr;
927 struct GNUNET_TUN_DnsHeader dns;
930 GNUNET_assert (ts->reply != NULL);
933 ret = sizeof (struct GNUNET_MessageHeader) + ts->reply_length;
934 GNUNET_assert (ret <= size);
935 hdr.size = htons (ret);
936 hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET);
937 memcpy (&dns, ts->reply, sizeof (dns));
938 dns.id = ts->original_id;
940 memcpy (&cbuf[off], &hdr, sizeof (hdr));
942 memcpy (&cbuf[off], &dns, sizeof (dns));
944 memcpy (&cbuf[off], &ts->reply[sizeof (dns)], ts->reply_length - sizeof (dns));
945 off += ts->reply_length - sizeof (dns);
946 GNUNET_free (ts->reply);
948 ts->reply_length = 0;
949 GNUNET_assert (ret == off);
955 * Actually do the reading of a DNS packet from our UDP socket and see
956 * if we have a valid, matching, pending request.
958 * @param dnsout socket to read from
959 * @return GNUNET_OK on success, GNUNET_NO on drop, GNUNET_SYSERR on IO-errors (closed socket)
962 do_dns_read (struct GNUNET_NETWORK_Handle *dnsout)
964 struct sockaddr_storage addr;
966 struct GNUNET_TUN_DnsHeader *dns;
967 struct RequestRecord *rr;
968 struct TunnelState *ts;
973 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
975 /* conservative choice: */
979 /* port the code above? */
984 unsigned char buf[len];
986 addrlen = sizeof (addr);
987 memset (&addr, 0, sizeof (addr));
988 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
990 (struct sockaddr*) &addr, &addrlen);
993 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
994 GNUNET_NETWORK_socket_close (dnsout);
995 return GNUNET_SYSERR;
997 if (sizeof (struct GNUNET_TUN_DnsHeader) > r)
999 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1000 _("Received DNS response that is too small (%u bytes)"),
1004 dns = (struct GNUNET_TUN_DnsHeader *) buf;
1005 /* Handle case that this is a reply to a request from a MESH DNS tunnel */
1006 ts = tunnels[dns->id];
1007 if ( (NULL == ts) ||
1008 (ts->dnsout != dnsout) ||
1009 (addrlen != ts->addrlen) ||
1010 (0 != memcmp (&ts->addr,
1013 (0 == GNUNET_TIME_absolute_get_remaining (ts->timeout).rel_value) )
1014 ts = NULL; /* DNS responder address missmatch */
1017 tunnels[dns->id] = NULL;
1018 GNUNET_free_non_null (ts->reply);
1019 ts->reply = GNUNET_malloc (r);
1020 ts->reply_length = r;
1021 memcpy (ts->reply, dns, r);
1023 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
1024 ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
1026 GNUNET_TIME_UNIT_FOREVER_REL,
1028 sizeof (struct GNUNET_MessageHeader) + r,
1029 &transmit_reply_to_mesh,
1032 /* Handle case that this is a reply to a local request (intercepted from TUN interface) */
1033 rr = &requests[dns->id];
1034 if ( (rr->phase != RP_INTERNET_DNS) ||
1035 (rr->dnsout != dnsout) ||
1036 (0 != memcmp (&rr->dst_addr,
1039 (0 == GNUNET_TIME_absolute_get_remaining (rr->timeout).rel_value) )
1043 /* unexpected / bogus reply */
1044 GNUNET_STATISTICS_update (stats,
1045 gettext_noop ("# External DNS response discarded (no matching request)"),
1050 GNUNET_free_non_null (rr->payload);
1051 rr->payload = GNUNET_malloc (r);
1052 memcpy (rr->payload, buf, r);
1053 rr->payload_length = r;
1061 * Read a DNS response from the (unhindered) UDP-Socket
1063 * @param cls socket to read from
1064 * @param tc scheduler context (must be shutdown or read ready)
1067 read_response (void *cls,
1068 const struct GNUNET_SCHEDULER_TaskContext *tc)
1070 struct RequestSocket *rs = cls;
1071 struct GNUNET_NETWORK_FDSet *rset;
1073 rs->read_task = GNUNET_SCHEDULER_NO_TASK;
1074 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1076 /* timeout or shutdown */
1080 /* read and process ready sockets */
1081 if ((NULL != rs->dnsout4) &&
1082 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout4)) &&
1083 (GNUNET_SYSERR == do_dns_read (rs->dnsout4)))
1085 if ((NULL != rs->dnsout6) &&
1086 (GNUNET_NETWORK_fdset_isset (tc->read_ready, rs->dnsout6)) &&
1087 (GNUNET_SYSERR == do_dns_read (rs->dnsout6)))
1090 /* re-schedule read task */
1091 rset = GNUNET_NETWORK_fdset_create ();
1092 if (NULL != rs->dnsout4)
1093 GNUNET_NETWORK_fdset_set (rset, rs->dnsout4);
1094 if (NULL != rs->dnsout6)
1095 GNUNET_NETWORK_fdset_set (rset, rs->dnsout6);
1096 rs->read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1097 GNUNET_SCHEDULER_NO_TASK,
1098 GNUNET_TIME_absolute_get_remaining (rs->timeout),
1101 &read_response, rs);
1102 GNUNET_NETWORK_fdset_destroy (rset);
1107 * We got a new client. Make sure all new DNS requests pass by its desk.
1110 * @param client the new client
1111 * @param message the init message (unused)
1114 handle_client_init (void *cls GNUNET_UNUSED,
1115 struct GNUNET_SERVER_Client *client,
1116 const struct GNUNET_MessageHeader *message)
1118 struct ClientRecord *cr;
1119 const struct GNUNET_DNS_Register *reg = (const struct GNUNET_DNS_Register*) message;
1121 cr = GNUNET_malloc (sizeof (struct ClientRecord));
1122 cr->client = client;
1123 cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
1124 GNUNET_SERVER_client_keep (client);
1125 GNUNET_CONTAINER_DLL_insert (clients_head,
1128 GNUNET_SERVER_notification_context_add (nc, client);
1129 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1134 * We got a response from a client.
1137 * @param client the client
1138 * @param message the response
1141 handle_client_response (void *cls GNUNET_UNUSED,
1142 struct GNUNET_SERVER_Client *client,
1143 const struct GNUNET_MessageHeader *message)
1145 const struct GNUNET_DNS_Response *resp;
1146 struct RequestRecord *rr;
1151 msize = ntohs (message->size);
1152 if (msize < sizeof (struct GNUNET_DNS_Response))
1155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1158 resp = (const struct GNUNET_DNS_Response*) message;
1159 off = (uint16_t) resp->request_id;
1160 rr = &requests[off];
1161 if (rr->request_id != resp->request_id)
1163 GNUNET_STATISTICS_update (stats,
1164 gettext_noop ("# Client response discarded (no matching request)"),
1166 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1169 for (i=0;i<rr->client_wait_list_length;i++)
1171 if (NULL == rr->client_wait_list[i])
1173 if (rr->client_wait_list[i]->client != client)
1175 rr->client_wait_list[i] = NULL;
1176 switch (ntohl (resp->drop_flag))
1179 rr->phase = RP_DROP;
1181 case 1: /* no change */
1183 case 2: /* update */
1184 msize -= sizeof (struct GNUNET_DNS_Response);
1185 if ( (sizeof (struct GNUNET_TUN_DnsHeader) > msize) ||
1186 (RP_REQUEST_MONITOR == rr->phase) ||
1187 (RP_RESPONSE_MONITOR == rr->phase) )
1190 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1194 GNUNET_free_non_null (rr->payload);
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 _("Changing DNS reply according to client specifications\n"));
1199 rr->payload = GNUNET_malloc (msize);
1200 rr->payload_length = msize;
1201 memcpy (rr->payload, &resp[1], msize);
1202 if (rr->phase == RP_QUERY)
1204 /* clear wait list, we're moving to MODIFY phase next */
1205 GNUNET_array_grow (rr->client_wait_list,
1206 rr->client_wait_list_length,
1212 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1215 /* odd, client was not on our list for the request, that ought
1218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1223 * Functions with this signature are called whenever a complete
1224 * message is received by the tokenizer from the DNS hijack process.
1226 * @param cls closure
1227 * @param client identification of the client
1228 * @param message the actual message, a DNS request we should handle
1231 process_helper_messages (void *cls GNUNET_UNUSED, void *client,
1232 const struct GNUNET_MessageHeader *message)
1235 const struct GNUNET_TUN_Layer2PacketHeader *tun;
1236 const struct GNUNET_TUN_IPv4Header *ip4;
1237 const struct GNUNET_TUN_IPv6Header *ip6;
1238 const struct GNUNET_TUN_UdpHeader *udp;
1239 const struct GNUNET_TUN_DnsHeader *dns;
1240 struct RequestRecord *rr;
1241 struct sockaddr_in *srca4;
1242 struct sockaddr_in6 *srca6;
1243 struct sockaddr_in *dsta4;
1244 struct sockaddr_in6 *dsta6;
1246 msize = ntohs (message->size);
1247 if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_TUN_IPv4Header))
1249 /* non-IP packet received on TUN!? */
1253 msize -= sizeof (struct GNUNET_MessageHeader);
1254 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
1255 msize -= sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1256 switch (ntohs (tun->proto))
1259 ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1];
1260 if ( (msize < sizeof (struct GNUNET_TUN_IPv4Header)) ||
1261 (ip4->version != 4) ||
1262 (ip4->header_length != sizeof (struct GNUNET_TUN_IPv4Header) / 4) ||
1263 (ntohs(ip4->total_length) != msize) ||
1264 (ip4->protocol != IPPROTO_UDP) )
1266 /* non-IP/UDP packet received on TUN (or with options) */
1267 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1268 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
1271 udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1];
1272 msize -= sizeof (struct GNUNET_TUN_IPv4Header);
1275 ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
1276 if ( (msize < sizeof (struct GNUNET_TUN_IPv6Header)) ||
1277 (ip6->version != 6) ||
1278 (ntohs (ip6->payload_length) != msize) ||
1279 (ip6->next_header != IPPROTO_UDP) )
1281 /* non-IP/UDP packet received on TUN (or with extensions) */
1282 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1283 _("Received malformed IPv6-UDP packet on TUN interface.\n"));
1286 udp = (const struct GNUNET_TUN_UdpHeader*) &ip6[1];
1287 msize -= sizeof (struct GNUNET_TUN_IPv6Header);
1290 /* non-IP packet received on TUN!? */
1291 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1292 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"),
1293 (unsigned int) msize,
1294 ntohs (tun->proto));
1297 if (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader))
1299 /* non-DNS packet received on TUN, ignore */
1300 GNUNET_STATISTICS_update (stats,
1301 gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
1305 msize -= sizeof (struct GNUNET_TUN_UdpHeader);
1306 dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1];
1307 rr = &requests[dns->id];
1309 /* clean up from previous request */
1310 GNUNET_free_non_null (rr->payload);
1312 GNUNET_array_grow (rr->client_wait_list,
1313 rr->client_wait_list_length,
1316 /* setup new request */
1317 rr->phase = RP_INIT;
1318 switch (ntohs (tun->proto))
1322 srca4 = (struct sockaddr_in*) &rr->src_addr;
1323 dsta4 = (struct sockaddr_in*) &rr->dst_addr;
1324 memset (srca4, 0, sizeof (struct sockaddr_in));
1325 memset (dsta4, 0, sizeof (struct sockaddr_in));
1326 srca4->sin_family = AF_INET;
1327 dsta4->sin_family = AF_INET;
1328 srca4->sin_addr = ip4->source_address;
1329 dsta4->sin_addr = ip4->destination_address;
1330 srca4->sin_port = udp->source_port;
1331 dsta4->sin_port = udp->destination_port;
1332 #if HAVE_SOCKADDR_IN_SIN_LEN
1333 srca4->sin_len = sizeof (struct sockaddr_in);
1334 dsta4->sin_len = sizeof (struct sockaddr_in);
1340 srca6 = (struct sockaddr_in6*) &rr->src_addr;
1341 dsta6 = (struct sockaddr_in6*) &rr->dst_addr;
1342 memset (srca6, 0, sizeof (struct sockaddr_in6));
1343 memset (dsta6, 0, sizeof (struct sockaddr_in6));
1344 srca6->sin6_family = AF_INET6;
1345 dsta6->sin6_family = AF_INET6;
1346 srca6->sin6_addr = ip6->source_address;
1347 dsta6->sin6_addr = ip6->destination_address;
1348 srca6->sin6_port = udp->source_port;
1349 dsta6->sin6_port = udp->destination_port;
1350 #if HAVE_SOCKADDR_IN_SIN_LEN
1351 srca6->sin6_len = sizeof (struct sockaddr_in6);
1352 dsta6->sin6_len = sizeof (struct sockaddr_in6);
1359 rr->payload = GNUNET_malloc (msize);
1360 rr->payload_length = msize;
1361 memcpy (rr->payload, dns, msize);
1362 rr->request_id = dns->id | (request_id_gen << 16);
1365 GNUNET_STATISTICS_update (stats,
1366 gettext_noop ("# DNS requests received via TUN interface"),
1368 /* start request processing state machine */
1374 * Process a request via mesh to perform a DNS query.
1376 * @param cls closure, NULL
1377 * @param tunnel connection to the other end
1378 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1379 * @param sender who sent the message
1380 * @param message the actual message
1381 * @param atsi performance data for the connection
1382 * @return GNUNET_OK to keep the connection open,
1383 * GNUNET_SYSERR to close it (signal serious error)
1386 receive_dns_request (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1388 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1389 const struct GNUNET_MessageHeader *message,
1390 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1392 struct TunnelState *ts = *tunnel_ctx;
1393 const struct GNUNET_TUN_DnsHeader *dns;
1394 size_t mlen = ntohs (message->size);
1395 size_t dlen = mlen - sizeof (struct GNUNET_MessageHeader);
1397 struct GNUNET_TUN_DnsHeader *dout;
1398 struct sockaddr_in v4;
1399 struct sockaddr_in6 v6;
1400 struct sockaddr *so;
1403 if (dlen < sizeof (struct GNUNET_TUN_DnsHeader))
1405 GNUNET_break_op (0);
1406 return GNUNET_SYSERR;
1408 dns = (const struct GNUNET_TUN_DnsHeader *) &message[1];
1409 ts->original_id = dns->id;
1410 if (tunnels[ts->my_id] == ts)
1411 tunnels[ts->my_id] = NULL;
1412 ts->my_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1414 tunnels[ts->my_id] = ts;
1415 memcpy (buf, dns, dlen);
1416 dout = (struct GNUNET_TUN_DnsHeader*) buf;
1417 dout->id = ts->my_id;
1418 memset (&v4, 0, sizeof (v4));
1419 memset (&v6, 0, sizeof (v6));
1420 if (1 == inet_pton (AF_INET, dns_exit, &v4.sin_addr))
1422 salen = sizeof (v4);
1423 v4.sin_family = AF_INET;
1424 v4.sin_port = htons (53);
1425 #if HAVE_SOCKADDR_IN_SIN_LEN
1426 v4.sin_len = (u_char) salen;
1428 so = (struct sockaddr *) &v4;
1429 ts->dnsout = get_request_socket (AF_INET);
1431 else if (1 == inet_pton (AF_INET6, dns_exit, &v6.sin6_addr))
1433 salen = sizeof (v6);
1434 v6.sin6_family = AF_INET6;
1435 v6.sin6_port = htons (53);
1436 #if HAVE_SOCKADDR_IN_SIN_LEN
1437 v6.sin6_len = (u_char) salen;
1439 so = (struct sockaddr *) &v6;
1440 ts->dnsout = get_request_socket (AF_INET6);
1445 return GNUNET_SYSERR;
1447 if (NULL == ts->dnsout)
1449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1450 _("Configured DNS exit `%s' is not working / valid.\n"),
1452 return GNUNET_SYSERR;
1457 ts->addrlen = salen;
1458 GNUNET_NETWORK_socket_sendto (ts->dnsout,
1459 buf, dlen, so, salen);
1460 ts->timeout = GNUNET_TIME_relative_to_absolute (REQUEST_TIMEOUT);
1466 * Callback from GNUNET_MESH for new tunnels.
1468 * @param cls closure
1469 * @param tunnel new handle to the tunnel
1470 * @param initiator peer that started the tunnel
1471 * @param ats performance information for the tunnel
1472 * @return initial tunnel context for the tunnel
1475 accept_dns_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1476 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
1477 const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
1479 struct TunnelState *ts = GNUNET_malloc (sizeof (struct TunnelState));
1481 GNUNET_STATISTICS_update (stats,
1482 gettext_noop ("# Inbound MESH tunnels created"),
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1485 "Received inbound tunnel from `%s'\n",
1486 GNUNET_i2s (initiator));
1487 ts->tunnel = tunnel;
1493 * Function called by mesh whenever an inbound tunnel is destroyed.
1494 * Should clean up any associated state.
1496 * @param cls closure (set from GNUNET_MESH_connect)
1497 * @param tunnel connection to the other end (henceforth invalid)
1498 * @param tunnel_ctx place where local state associated
1499 * with the tunnel is stored
1502 destroy_dns_tunnel (void *cls GNUNET_UNUSED,
1503 const struct GNUNET_MESH_Tunnel *tunnel,
1506 struct TunnelState *ts = tunnel_ctx;
1508 if (tunnels[ts->my_id] == ts)
1509 tunnels[ts->my_id] = NULL;
1511 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
1512 GNUNET_free_non_null (ts->reply);
1518 * @param cls closure
1519 * @param server the initialized server
1520 * @param cfg_ configuration to use
1523 run (void *cls, struct GNUNET_SERVER_Handle *server,
1524 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1526 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1527 /* callback, cls, type, size */
1528 {&handle_client_init, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
1529 sizeof (struct GNUNET_DNS_Register)},
1530 {&handle_client_response, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 0},
1538 struct in_addr dns_exit4;
1539 struct in6_addr dns_exit6;
1542 stats = GNUNET_STATISTICS_create ("dns", cfg);
1543 nc = GNUNET_SERVER_notification_context_create (server, 1);
1544 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1547 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT")) &&
1549 GNUNET_CONFIGURATION_get_value_string (cfg, "dns",
1552 ( (1 != inet_pton (AF_INET, dns_exit, &dns_exit4)) &&
1553 (1 != inet_pton (AF_INET6, dns_exit, &dns_exit6)) ) ) )
1555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1556 _("Configured to provide DNS exit, but no valid DNS server configured!\n"));
1557 GNUNET_free_non_null (dns_exit);
1561 helper_argv[0] = GNUNET_strdup ("gnunet-dns");
1562 if (GNUNET_SYSERR ==
1563 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name))
1565 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1566 "No entry 'IFNAME' in configuration!\n");
1567 GNUNET_SCHEDULER_shutdown ();
1570 helper_argv[1] = ifc_name;
1571 if ( (GNUNET_SYSERR ==
1572 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6ADDR",
1575 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1576 "No entry 'IPV6ADDR' in configuration!\n");
1577 GNUNET_SCHEDULER_shutdown ();
1580 helper_argv[2] = ipv6addr;
1581 if (GNUNET_SYSERR ==
1582 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6PREFIX",
1585 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1586 "No entry 'IPV6PREFIX' in configuration!\n");
1587 GNUNET_SCHEDULER_shutdown ();
1590 helper_argv[3] = ipv6prefix;
1592 if (GNUNET_SYSERR ==
1593 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4ADDR",
1596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1597 "No entry 'IPV4ADDR' in configuration!\n");
1598 GNUNET_SCHEDULER_shutdown ();
1601 helper_argv[4] = ipv4addr;
1602 if (GNUNET_SYSERR ==
1603 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK",
1606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1607 "No entry 'IPV4MASK' in configuration!\n");
1608 GNUNET_SCHEDULER_shutdown ();
1611 helper_argv[5] = ipv4mask;
1612 helper_argv[6] = NULL;
1614 if (NULL != dns_exit)
1616 static struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1617 {&receive_dns_request, GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET, 0},
1620 static GNUNET_MESH_ApplicationType mesh_types[] = {
1621 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1622 GNUNET_APPLICATION_TYPE_END
1624 mesh = GNUNET_MESH_connect (cfg,
1627 &destroy_dns_tunnel,
1631 hijacker = GNUNET_HELPER_start ("gnunet-helper-dns",
1633 &process_helper_messages,
1635 GNUNET_SERVER_add_handlers (server, handlers);
1636 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
1641 * The main function for the dns service.
1643 * @param argc number of arguments from the command line
1644 * @param argv command line arguments
1645 * @return 0 ok, 1 on error
1648 main (int argc, char *const *argv)
1650 return (GNUNET_OK ==
1651 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1652 &run, NULL)) ? 0 : 1;
1656 /* end of gnunet-service-dns.c */