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_dnsparser_lib.h"
48 #include "gnunet_dnsstub_lib.h"
49 #include "gnunet_mesh_service.h"
50 #include "gnunet_statistics_service.h"
51 #include "gnunet_tun_lib.h"
55 * Phases each request goes through.
60 * Request has just been received.
65 * Showing the request to all monitor clients. If
66 * client list is empty, will enter QUERY phase.
71 * Showing the request to PRE-RESOLUTION clients to find an answer.
72 * If client list is empty, will trigger global DNS request.
77 * Global Internet query is now pending.
82 * Client (or global DNS request) has resulted in a response.
83 * Forward to all POST-RESOLUTION clients. If client list is empty,
84 * will enter RESPONSE_MONITOR phase.
89 * Showing the request to all monitor clients. If
90 * client list is empty, give the result to the hijacker (and be done).
95 * Some client has told us to drop the request.
102 * Entry we keep for each client.
107 * Kept in doubly-linked list.
109 struct ClientRecord *next;
112 * Kept in doubly-linked list.
114 struct ClientRecord *prev;
117 * Handle to the client.
119 struct GNUNET_SERVER_Client *client;
122 * Flags for the client.
124 enum GNUNET_DNS_Flags flags;
130 * Entry we keep for each active request.
136 * List of clients that still need to see this request (each entry
137 * is set to NULL when the client is done).
139 struct ClientRecord **client_wait_list;
142 * Payload of the UDP packet (the UDP payload), can be either query
143 * or already the response.
148 * Socket we are using to transmit this request (must match if we receive
151 struct GNUNET_DNSSTUB_RequestSocket *rs;
154 * Source address of the original request (for sending response).
156 struct sockaddr_storage src_addr;
159 * Destination address of the original request (for potential use as exit).
161 struct sockaddr_storage dst_addr;
164 * ID of this request, also basis for hashing. Lowest 16 bit will
165 * be our message ID when doing a global DNS request and our index
166 * into the 'requests' array.
171 * Number of bytes in payload.
173 size_t payload_length;
176 * Length of the client wait list.
178 unsigned int client_wait_list_length;
181 * In which phase this this request?
183 enum RequestPhase phase;
189 * State we keep for each DNS tunnel that terminates at this node.
195 * Associated MESH tunnel.
197 struct GNUNET_MESH_Tunnel *tunnel;
200 * Active request for sending a reply.
202 struct GNUNET_MESH_TransmitHandle *th;
205 * DNS reply ready for transmission.
210 * Socket we are using to transmit this request (must match if we receive
213 struct GNUNET_DNSSTUB_RequestSocket *rs;
216 * Number of bytes in 'reply'.
221 * Original DNS request ID as used by the client.
223 uint16_t original_id;
226 * DNS request ID that we used for forwarding.
233 * Global return value from 'main'.
235 static int global_ret;
238 * The configuration to use
240 static const struct GNUNET_CONFIGURATION_Handle *cfg;
245 static struct GNUNET_STATISTICS_Handle *stats;
248 * Handle to DNS hijacker helper process ("gnunet-helper-dns").
250 static struct GNUNET_HELPER_Handle *hijacker;
253 * Command-line arguments we are giving to the hijacker process.
255 static char *helper_argv[7];
258 * Head of DLL of clients we consult.
260 static struct ClientRecord *clients_head;
263 * Tail of DLL of clients we consult.
265 static struct ClientRecord *clients_tail;
268 * Our notification context.
270 static struct GNUNET_SERVER_NotificationContext *nc;
273 * Array of all open requests from tunnels.
275 static struct TunnelState *tunnels[UINT16_MAX + 1];
278 * Array of all open requests.
280 static struct RequestRecord requests[UINT16_MAX + 1];
283 * Generator for unique request IDs.
285 static uint64_t request_id_gen;
288 * Handle to the MESH service (for receiving DNS queries), or NULL
289 * if we are not a DNS exit.
291 static struct GNUNET_MESH_Handle *mesh;
294 * Handle to the DNS Stub resolver.
296 static struct GNUNET_DNSSTUB_Context *dnsstub;
300 * We're done processing a DNS request, free associated memory.
302 * @param rr request to clean up
305 cleanup_rr (struct RequestRecord *rr)
307 GNUNET_free_non_null (rr->payload);
309 rr->payload_length = 0;
310 GNUNET_array_grow (rr->client_wait_list,
311 rr->client_wait_list_length,
317 * Task run during shutdown.
323 cleanup_task (void *cls GNUNET_UNUSED,
324 const struct GNUNET_SCHEDULER_TaskContext *tc)
328 GNUNET_HELPER_stop (hijacker);
331 GNUNET_free_non_null (helper_argv[i]);
332 for (i=0;i<=UINT16_MAX;i++)
333 cleanup_rr (&requests[i]);
334 GNUNET_SERVER_notification_context_destroy (nc);
338 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
343 GNUNET_DNSSTUB_stop (dnsstub);
348 GNUNET_MESH_disconnect(mesh);
355 * We're done with some request, finish processing.
357 * @param rr request send to the network or just clean up.
360 request_done (struct RequestRecord *rr)
362 struct GNUNET_MessageHeader *hdr;
364 uint16_t source_port;
365 uint16_t destination_port;
367 GNUNET_array_grow (rr->client_wait_list,
368 rr->client_wait_list_length,
370 if (RP_RESPONSE_MONITOR != rr->phase)
372 /* no response, drop */
377 /* send response via hijacker */
378 reply_len = sizeof (struct GNUNET_MessageHeader);
379 reply_len += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
380 switch (rr->src_addr.ss_family)
383 reply_len += sizeof (struct GNUNET_TUN_IPv4Header);
386 reply_len += sizeof (struct GNUNET_TUN_IPv6Header);
393 reply_len += sizeof (struct GNUNET_TUN_UdpHeader);
394 reply_len += rr->payload_length;
395 if (reply_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
397 /* response too big, drop */
398 GNUNET_break (0); /* how can this be? */
403 char buf[reply_len] GNUNET_ALIGN;
405 struct GNUNET_TUN_IPv4Header ip4;
406 struct GNUNET_TUN_IPv6Header ip6;
408 /* first, GNUnet message header */
409 hdr = (struct GNUNET_MessageHeader*) buf;
410 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
411 hdr->size = htons ((uint16_t) reply_len);
412 off = sizeof (struct GNUNET_MessageHeader);
414 /* first, TUN header */
416 struct GNUNET_TUN_Layer2PacketHeader tun;
418 tun.flags = htons (0);
419 if (rr->src_addr.ss_family == AF_INET)
420 tun.proto = htons (ETH_P_IPV4);
422 tun.proto = htons (ETH_P_IPV6);
423 memcpy (&buf[off], &tun, sizeof (struct GNUNET_TUN_Layer2PacketHeader));
424 off += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
428 switch (rr->src_addr.ss_family)
432 struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr;
433 struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr;
435 source_port = dst->sin_port;
436 destination_port = src->sin_port;
437 GNUNET_TUN_initialize_ipv4_header (&ip4,
439 reply_len - off - sizeof (struct GNUNET_TUN_IPv4Header),
442 memcpy (&buf[off], &ip4, sizeof (ip4));
448 struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr;
449 struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr;
451 source_port = dst->sin6_port;
452 destination_port = src->sin6_port;
453 GNUNET_TUN_initialize_ipv6_header (&ip6,
455 reply_len - sizeof (struct GNUNET_TUN_IPv6Header),
458 memcpy (&buf[off], &ip6, sizeof (ip6));
468 struct GNUNET_TUN_UdpHeader udp;
470 udp.source_port = source_port;
471 udp.destination_port = destination_port;
472 udp.len = htons (reply_len - off);
473 if (AF_INET == rr->src_addr.ss_family)
474 GNUNET_TUN_calculate_udp4_checksum (&ip4,
479 GNUNET_TUN_calculate_udp6_checksum (&ip6,
483 memcpy (&buf[off], &udp, sizeof (udp));
487 /* now DNS payload */
489 memcpy (&buf[off], rr->payload, rr->payload_length);
490 off += rr->payload_length;
492 /* final checks & sending */
493 GNUNET_assert (off == reply_len);
494 (void) GNUNET_HELPER_send (hijacker,
498 GNUNET_STATISTICS_update (stats,
499 gettext_noop ("# DNS requests answered via TUN interface"),
502 /* clean up, we're done */
508 * Show the payload of the given request record to the client
509 * (and wait for a response).
511 * @param rr request to send to client
512 * @param client client to send the response to
515 send_request_to_client (struct RequestRecord *rr,
516 struct GNUNET_SERVER_Client *client)
518 char buf[sizeof (struct GNUNET_DNS_Request) + rr->payload_length] GNUNET_ALIGN;
519 struct GNUNET_DNS_Request *req;
521 if (sizeof (buf) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
527 req = (struct GNUNET_DNS_Request*) buf;
528 req->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
529 req->header.size = htons (sizeof (buf));
530 req->reserved = htonl (0);
531 req->request_id = rr->request_id;
532 memcpy (&req[1], rr->payload, rr->payload_length);
533 GNUNET_SERVER_notification_context_unicast (nc,
542 * Callback called from DNSSTUB resolver when a resolution
546 * @param rs the socket that received the response
547 * @param dns the response itself
548 * @param r number of bytes in dns
551 process_dns_result (void *cls,
552 struct GNUNET_DNSSTUB_RequestSocket *rs,
553 const struct GNUNET_TUN_DnsHeader *dns,
558 * A client has completed its processing for this
561 * @param rr request to process further
564 next_phase (struct RequestRecord *rr)
566 struct ClientRecord *cr;
571 if (rr->phase == RP_DROP)
577 for (j=0;j<rr->client_wait_list_length;j++)
579 if (NULL != rr->client_wait_list[j])
587 send_request_to_client (rr, rr->client_wait_list[nz]->client);
590 /* done with current phase, advance! */
594 rr->phase = RP_REQUEST_MONITOR;
595 for (cr = clients_head; NULL != cr; cr = cr->next)
597 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR))
598 GNUNET_array_append (rr->client_wait_list,
599 rr->client_wait_list_length,
604 case RP_REQUEST_MONITOR:
605 rr->phase = RP_QUERY;
606 for (cr = clients_head; NULL != cr; cr = cr->next)
608 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION))
609 GNUNET_array_append (rr->client_wait_list,
610 rr->client_wait_list_length,
616 switch (rr->dst_addr.ss_family)
619 salen = sizeof (struct sockaddr_in);
622 salen = sizeof (struct sockaddr_in6);
628 rr->phase = RP_INTERNET_DNS;
629 rr->rs = GNUNET_DNSSTUB_resolve (dnsstub,
630 (struct sockaddr*) &rr->dst_addr,
638 GNUNET_STATISTICS_update (stats,
639 gettext_noop ("# DNS exit failed (failed to open socket)"),
645 case RP_INTERNET_DNS:
646 rr->phase = RP_MODIFY;
647 for (cr = clients_head; NULL != cr; cr = cr->next)
649 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
650 GNUNET_array_append (rr->client_wait_list,
651 rr->client_wait_list_length,
657 rr->phase = RP_RESPONSE_MONITOR;
658 for (cr = clients_head; NULL != cr; cr = cr->next)
660 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
661 GNUNET_array_append (rr->client_wait_list,
662 rr->client_wait_list_length,
667 case RP_RESPONSE_MONITOR:
682 * A client disconnected, clean up after it.
685 * @param client handle of client that disconnected
688 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
690 struct ClientRecord *cr;
691 struct RequestRecord *rr;
695 for (cr = clients_head; NULL != cr; cr = cr->next)
697 if (cr->client == client)
699 GNUNET_SERVER_client_drop (client);
700 GNUNET_CONTAINER_DLL_remove (clients_head,
703 for (i=0;i<UINT16_MAX;i++)
706 if (0 == rr->client_wait_list_length)
707 continue; /* not in use */
708 for (j=0;j<rr->client_wait_list_length;j++)
710 if (rr->client_wait_list[j] == cr)
712 rr->client_wait_list[j] = NULL;
725 * We got a reply from DNS for a request of a MESH tunnel. Send it
726 * via the tunnel (after changing the request ID back).
728 * @param cls the 'struct TunnelState'
729 * @param size number of bytes available in buf
730 * @param buf where to copy the reply
731 * @return number of bytes written to buf
734 transmit_reply_to_mesh (void *cls,
738 struct TunnelState *ts = cls;
742 struct GNUNET_MessageHeader hdr;
743 struct GNUNET_TUN_DnsHeader dns;
746 GNUNET_assert (ts->reply != NULL);
749 ret = sizeof (struct GNUNET_MessageHeader) + ts->reply_length;
750 GNUNET_assert (ret <= size);
751 hdr.size = htons (ret);
752 hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET);
753 memcpy (&dns, ts->reply, sizeof (dns));
754 dns.id = ts->original_id;
756 memcpy (&cbuf[off], &hdr, sizeof (hdr));
758 memcpy (&cbuf[off], &dns, sizeof (dns));
760 memcpy (&cbuf[off], &ts->reply[sizeof (dns)], ts->reply_length - sizeof (dns));
761 off += ts->reply_length - sizeof (dns);
762 GNUNET_free (ts->reply);
764 ts->reply_length = 0;
765 GNUNET_assert (ret == off);
772 * Callback called from DNSSTUB resolver when a resolution
776 * @param rs the socket that received the response
777 * @param dns the response itself
778 * @param r number of bytes in dns
781 process_dns_result (void *cls,
782 struct GNUNET_DNSSTUB_RequestSocket *rs,
783 const struct GNUNET_TUN_DnsHeader *dns,
786 struct RequestRecord *rr;
787 struct TunnelState *ts;
789 GNUNET_assert (NULL == cls);
790 /* Handle case that this is a reply to a request from a MESH DNS tunnel */
791 ts = tunnels[dns->id];
794 ts = NULL; /* DNS responder address missmatch */
797 tunnels[dns->id] = NULL;
798 GNUNET_free_non_null (ts->reply);
799 ts->reply = GNUNET_malloc (r);
800 ts->reply_length = r;
801 memcpy (ts->reply, dns, r);
803 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
804 ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
806 GNUNET_TIME_UNIT_FOREVER_REL,
808 sizeof (struct GNUNET_MessageHeader) + r,
809 &transmit_reply_to_mesh,
812 /* Handle case that this is a reply to a local request (intercepted from TUN interface) */
813 rr = &requests[dns->id];
814 if ( (rr->phase != RP_INTERNET_DNS) ||
819 /* unexpected / bogus reply */
820 GNUNET_STATISTICS_update (stats,
821 gettext_noop ("# External DNS response discarded (no matching request)"),
826 GNUNET_free_non_null (rr->payload);
827 rr->payload = GNUNET_malloc (r);
828 memcpy (rr->payload, dns, r);
829 rr->payload_length = r;
835 * We got a new client. Make sure all new DNS requests pass by its desk.
838 * @param client the new client
839 * @param message the init message (unused)
842 handle_client_init (void *cls GNUNET_UNUSED,
843 struct GNUNET_SERVER_Client *client,
844 const struct GNUNET_MessageHeader *message)
846 struct ClientRecord *cr;
847 const struct GNUNET_DNS_Register *reg = (const struct GNUNET_DNS_Register*) message;
849 cr = GNUNET_malloc (sizeof (struct ClientRecord));
851 cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
852 GNUNET_SERVER_client_keep (client);
853 GNUNET_CONTAINER_DLL_insert (clients_head,
856 GNUNET_SERVER_notification_context_add (nc, client);
857 GNUNET_SERVER_receive_done (client, GNUNET_OK);
862 * We got a response from a client.
865 * @param client the client
866 * @param message the response
869 handle_client_response (void *cls GNUNET_UNUSED,
870 struct GNUNET_SERVER_Client *client,
871 const struct GNUNET_MessageHeader *message)
873 const struct GNUNET_DNS_Response *resp;
874 struct RequestRecord *rr;
879 msize = ntohs (message->size);
880 if (msize < sizeof (struct GNUNET_DNS_Response))
883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
886 resp = (const struct GNUNET_DNS_Response*) message;
887 off = (uint16_t) resp->request_id;
889 if (rr->request_id != resp->request_id)
891 GNUNET_STATISTICS_update (stats,
892 gettext_noop ("# Client response discarded (no matching request)"),
894 GNUNET_SERVER_receive_done (client, GNUNET_OK);
897 for (i=0;i<rr->client_wait_list_length;i++)
899 if (NULL == rr->client_wait_list[i])
901 if (rr->client_wait_list[i]->client != client)
903 rr->client_wait_list[i] = NULL;
904 switch (ntohl (resp->drop_flag))
909 case 1: /* no change */
912 msize -= sizeof (struct GNUNET_DNS_Response);
913 if ( (sizeof (struct GNUNET_TUN_DnsHeader) > msize) ||
914 (RP_REQUEST_MONITOR == rr->phase) ||
915 (RP_RESPONSE_MONITOR == rr->phase) )
918 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
922 GNUNET_free_non_null (rr->payload);
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
924 "Changing DNS reply according to client specifications\n");
925 rr->payload = GNUNET_malloc (msize);
926 rr->payload_length = msize;
927 memcpy (rr->payload, &resp[1], msize);
928 if (rr->phase == RP_QUERY)
930 /* clear wait list, we're moving to MODIFY phase next */
931 GNUNET_array_grow (rr->client_wait_list,
932 rr->client_wait_list_length,
935 /* if query changed to answer, move past DNS resolution phase... */
936 if ( (RP_QUERY == rr->phase) &&
937 (rr->payload_length > sizeof (struct GNUNET_TUN_DnsHeader)) &&
938 ((struct GNUNET_DNSPARSER_Flags*)&(((struct GNUNET_TUN_DnsHeader*) rr->payload)->flags))->query_or_response == 1)
940 rr->phase = RP_INTERNET_DNS;
941 GNUNET_array_grow (rr->client_wait_list,
942 rr->client_wait_list_length,
948 GNUNET_SERVER_receive_done (client, GNUNET_OK);
951 /* odd, client was not on our list for the request, that ought
954 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
959 * Functions with this signature are called whenever a complete
960 * message is received by the tokenizer from the DNS hijack process.
963 * @param client identification of the client
964 * @param message the actual message, a DNS request we should handle
967 process_helper_messages (void *cls GNUNET_UNUSED, void *client,
968 const struct GNUNET_MessageHeader *message)
971 const struct GNUNET_TUN_Layer2PacketHeader *tun;
972 const struct GNUNET_TUN_IPv4Header *ip4;
973 const struct GNUNET_TUN_IPv6Header *ip6;
974 const struct GNUNET_TUN_UdpHeader *udp;
975 const struct GNUNET_TUN_DnsHeader *dns;
976 struct RequestRecord *rr;
977 struct sockaddr_in *srca4;
978 struct sockaddr_in6 *srca6;
979 struct sockaddr_in *dsta4;
980 struct sockaddr_in6 *dsta6;
982 msize = ntohs (message->size);
983 if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_TUN_IPv4Header))
985 /* non-IP packet received on TUN!? */
989 msize -= sizeof (struct GNUNET_MessageHeader);
990 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
991 msize -= sizeof (struct GNUNET_TUN_Layer2PacketHeader);
992 switch (ntohs (tun->proto))
995 ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1];
996 ip6 = NULL; /* make compiler happy */
997 if ( (msize < sizeof (struct GNUNET_TUN_IPv4Header)) ||
998 (ip4->version != 4) ||
999 (ip4->header_length != sizeof (struct GNUNET_TUN_IPv4Header) / 4) ||
1000 (ntohs(ip4->total_length) != msize) ||
1001 (ip4->protocol != IPPROTO_UDP) )
1003 /* non-IP/UDP packet received on TUN (or with options) */
1004 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1005 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
1008 udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1];
1009 msize -= sizeof (struct GNUNET_TUN_IPv4Header);
1012 ip4 = NULL; /* make compiler happy */
1013 ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
1014 if ( (msize < sizeof (struct GNUNET_TUN_IPv6Header)) ||
1015 (ip6->version != 6) ||
1016 (ntohs (ip6->payload_length) != msize) ||
1017 (ip6->next_header != IPPROTO_UDP) )
1019 /* non-IP/UDP packet received on TUN (or with extensions) */
1020 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1021 _("Received malformed IPv6-UDP packet on TUN interface.\n"));
1024 udp = (const struct GNUNET_TUN_UdpHeader*) &ip6[1];
1025 msize -= sizeof (struct GNUNET_TUN_IPv6Header);
1028 /* non-IP packet received on TUN!? */
1029 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1030 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"),
1031 (unsigned int) msize,
1032 ntohs (tun->proto));
1035 if (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader))
1037 /* non-DNS packet received on TUN, ignore */
1038 GNUNET_STATISTICS_update (stats,
1039 gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
1043 msize -= sizeof (struct GNUNET_TUN_UdpHeader);
1044 dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1];
1045 rr = &requests[dns->id];
1047 /* clean up from previous request */
1048 GNUNET_free_non_null (rr->payload);
1050 GNUNET_array_grow (rr->client_wait_list,
1051 rr->client_wait_list_length,
1054 /* setup new request */
1055 rr->phase = RP_INIT;
1056 switch (ntohs (tun->proto))
1060 srca4 = (struct sockaddr_in*) &rr->src_addr;
1061 dsta4 = (struct sockaddr_in*) &rr->dst_addr;
1062 memset (srca4, 0, sizeof (struct sockaddr_in));
1063 memset (dsta4, 0, sizeof (struct sockaddr_in));
1064 srca4->sin_family = AF_INET;
1065 dsta4->sin_family = AF_INET;
1066 srca4->sin_addr = ip4->source_address;
1067 dsta4->sin_addr = ip4->destination_address;
1068 srca4->sin_port = udp->source_port;
1069 dsta4->sin_port = udp->destination_port;
1070 #if HAVE_SOCKADDR_IN_SIN_LEN
1071 srca4->sin_len = sizeof (struct sockaddr_in);
1072 dsta4->sin_len = sizeof (struct sockaddr_in);
1078 srca6 = (struct sockaddr_in6*) &rr->src_addr;
1079 dsta6 = (struct sockaddr_in6*) &rr->dst_addr;
1080 memset (srca6, 0, sizeof (struct sockaddr_in6));
1081 memset (dsta6, 0, sizeof (struct sockaddr_in6));
1082 srca6->sin6_family = AF_INET6;
1083 dsta6->sin6_family = AF_INET6;
1084 srca6->sin6_addr = ip6->source_address;
1085 dsta6->sin6_addr = ip6->destination_address;
1086 srca6->sin6_port = udp->source_port;
1087 dsta6->sin6_port = udp->destination_port;
1088 #if HAVE_SOCKADDR_IN_SIN_LEN
1089 srca6->sin6_len = sizeof (struct sockaddr_in6);
1090 dsta6->sin6_len = sizeof (struct sockaddr_in6);
1097 rr->payload = GNUNET_malloc (msize);
1098 rr->payload_length = msize;
1099 memcpy (rr->payload, dns, msize);
1100 rr->request_id = dns->id | (request_id_gen << 16);
1103 GNUNET_STATISTICS_update (stats,
1104 gettext_noop ("# DNS requests received via TUN interface"),
1106 /* start request processing state machine */
1113 * Process a request via mesh to perform a DNS query.
1115 * @param cls closure, NULL
1116 * @param tunnel connection to the other end
1117 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1118 * @param sender who sent the message
1119 * @param message the actual message
1120 * @param atsi performance data for the connection
1121 * @return GNUNET_OK to keep the connection open,
1122 * GNUNET_SYSERR to close it (signal serious error)
1125 receive_dns_request (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1127 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1128 const struct GNUNET_MessageHeader *message,
1129 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1131 struct TunnelState *ts = *tunnel_ctx;
1132 const struct GNUNET_TUN_DnsHeader *dns;
1133 size_t mlen = ntohs (message->size);
1134 size_t dlen = mlen - sizeof (struct GNUNET_MessageHeader);
1135 char buf[dlen] GNUNET_ALIGN;
1136 struct GNUNET_TUN_DnsHeader *dout;
1138 if (dlen < sizeof (struct GNUNET_TUN_DnsHeader))
1140 GNUNET_break_op (0);
1141 return GNUNET_SYSERR;
1143 dns = (const struct GNUNET_TUN_DnsHeader *) &message[1];
1144 ts->original_id = dns->id;
1145 if (tunnels[ts->my_id] == ts)
1146 tunnels[ts->my_id] = NULL;
1147 ts->my_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1149 tunnels[ts->my_id] = ts;
1150 memcpy (buf, dns, dlen);
1151 dout = (struct GNUNET_TUN_DnsHeader *) buf;
1152 dout->id = ts->my_id;
1153 ts->rs = GNUNET_DNSSTUB_resolve2 (dnsstub,
1155 &process_dns_result,
1158 return GNUNET_SYSERR;
1164 * Callback from GNUNET_MESH for new tunnels.
1166 * @param cls closure
1167 * @param tunnel new handle to the tunnel
1168 * @param initiator peer that started the tunnel
1169 * @param ats performance information for the tunnel
1170 * @return initial tunnel context for the tunnel
1173 accept_dns_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1174 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
1175 const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
1177 struct TunnelState *ts = GNUNET_malloc (sizeof (struct TunnelState));
1179 GNUNET_STATISTICS_update (stats,
1180 gettext_noop ("# Inbound MESH tunnels created"),
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "Received inbound tunnel from `%s'\n",
1184 GNUNET_i2s (initiator));
1185 ts->tunnel = tunnel;
1191 * Function called by mesh whenever an inbound tunnel is destroyed.
1192 * Should clean up any associated state.
1194 * @param cls closure (set from GNUNET_MESH_connect)
1195 * @param tunnel connection to the other end (henceforth invalid)
1196 * @param tunnel_ctx place where local state associated
1197 * with the tunnel is stored
1200 destroy_dns_tunnel (void *cls GNUNET_UNUSED,
1201 const struct GNUNET_MESH_Tunnel *tunnel,
1204 struct TunnelState *ts = tunnel_ctx;
1206 if (tunnels[ts->my_id] == ts)
1207 tunnels[ts->my_id] = NULL;
1209 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
1210 GNUNET_free_non_null (ts->reply);
1216 * @param cls closure
1217 * @param server the initialized server
1218 * @param cfg_ configuration to use
1221 run (void *cls, struct GNUNET_SERVER_Handle *server,
1222 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1224 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1225 /* callback, cls, type, size */
1226 {&handle_client_init, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
1227 sizeof (struct GNUNET_DNS_Register)},
1228 {&handle_client_response, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 0},
1236 struct in_addr dns_exit4;
1237 struct in6_addr dns_exit6;
1242 GNUNET_OS_check_helper_binary ("gnunet-helper-dns"))
1244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1245 _("`%s' must be installed SUID, refusing to run\n"),
1246 "gnunet-helper-dns");
1251 stats = GNUNET_STATISTICS_create ("dns", cfg);
1252 nc = GNUNET_SERVER_notification_context_create (server, 1);
1253 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1257 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT")) &&
1259 GNUNET_CONFIGURATION_get_value_string (cfg, "dns",
1262 ( (1 != inet_pton (AF_INET, dns_exit, &dns_exit4)) &&
1263 (1 != inet_pton (AF_INET6, dns_exit, &dns_exit6)) ) ) )
1265 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "dns", "DNS_EXIT",
1266 _("need a valid IPv4 or IPv6 address\n"));
1267 GNUNET_free_non_null (dns_exit);
1270 dnsstub = GNUNET_DNSSTUB_start (dns_exit);
1271 helper_argv[0] = GNUNET_strdup ("gnunet-dns");
1272 if (GNUNET_SYSERR ==
1273 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name))
1275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1276 "No entry 'IFNAME' in configuration!\n");
1277 GNUNET_SCHEDULER_shutdown ();
1280 helper_argv[1] = ifc_name;
1281 if ( (GNUNET_SYSERR ==
1282 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6ADDR",
1285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1286 "No entry 'IPV6ADDR' in configuration!\n");
1287 GNUNET_SCHEDULER_shutdown ();
1290 helper_argv[2] = ipv6addr;
1291 if (GNUNET_SYSERR ==
1292 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6PREFIX",
1295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1296 "No entry 'IPV6PREFIX' in configuration!\n");
1297 GNUNET_SCHEDULER_shutdown ();
1300 helper_argv[3] = ipv6prefix;
1302 if (GNUNET_SYSERR ==
1303 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4ADDR",
1306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1307 "No entry 'IPV4ADDR' in configuration!\n");
1308 GNUNET_SCHEDULER_shutdown ();
1311 helper_argv[4] = ipv4addr;
1312 if (GNUNET_SYSERR ==
1313 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK",
1316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1317 "No entry 'IPV4MASK' in configuration!\n");
1318 GNUNET_SCHEDULER_shutdown ();
1321 helper_argv[5] = ipv4mask;
1322 helper_argv[6] = NULL;
1324 if (NULL != dns_exit)
1326 static struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1327 {&receive_dns_request, GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET, 0},
1330 static GNUNET_MESH_ApplicationType mesh_types[] = {
1331 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1332 GNUNET_APPLICATION_TYPE_END
1334 mesh = GNUNET_MESH_connect (cfg,
1337 &destroy_dns_tunnel,
1341 hijacker = GNUNET_HELPER_start (GNUNET_NO,
1342 "gnunet-helper-dns",
1344 &process_helper_messages,
1346 GNUNET_SERVER_add_handlers (server, handlers);
1347 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
1352 * The main function for the dns service.
1354 * @param argc number of arguments from the command line
1355 * @param argv command line arguments
1356 * @return 0 ok, 1 on error
1359 main (int argc, char *const *argv)
1361 /* make use of SGID capabilities on POSIX */
1362 /* FIXME: this might need a port on systems without 'getresgid' */
1368 if (-1 == getresgid (&rgid, &egid, &sgid))
1371 "getresgid failed: %s\n",
1374 else if (sgid != rgid)
1376 if (-1 == setregid (sgid, sgid))
1377 fprintf (stderr, "setregid failed: %s\n", strerror (errno));
1380 return (GNUNET_OK ==
1381 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1382 &run, NULL)) ? global_ret : 1;
1386 /* end of gnunet-service-dns.c */