2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_statistics_service.h"
50 #include "gnunet_tun_lib.h"
59 * Generic logging shorthand
61 #define LOG(kind, ...) \
62 GNUNET_log_from (kind, "dns", __VA_ARGS__);
66 * Phases each request goes through.
71 * Request has just been received.
76 * Showing the request to all monitor clients. If
77 * client list is empty, will enter QUERY phase.
82 * Showing the request to PRE-RESOLUTION clients to find an answer.
83 * If client list is empty, will trigger global DNS request.
88 * Global Internet query is now pending.
93 * Client (or global DNS request) has resulted in a response.
94 * Forward to all POST-RESOLUTION clients. If client list is empty,
95 * will enter RESPONSE_MONITOR phase.
100 * Showing the request to all monitor clients. If
101 * client list is empty, give the result to the hijacker (and be done).
106 * Some client has told us to drop the request.
113 * Entry we keep for each client.
118 * Kept in doubly-linked list.
120 struct ClientRecord *next;
123 * Kept in doubly-linked list.
125 struct ClientRecord *prev;
128 * Handle to the client.
130 struct GNUNET_SERVICE_Client *client;
133 * Message queue to talk to @a client.
135 struct GNUNET_MQ_Handle *mq;
138 * Flags for the client.
140 enum GNUNET_DNS_Flags flags;
146 * Entry we keep for each active request.
152 * List of clients that still need to see this request (each entry
153 * is set to NULL when the client is done).
155 struct ClientRecord **client_wait_list;
158 * Payload of the UDP packet (the UDP payload), can be either query
159 * or already the response.
164 * Socket we are using to transmit this request (must match if we receive
167 struct GNUNET_DNSSTUB_RequestSocket *rs;
170 * Source address of the original request (for sending response).
172 struct sockaddr_storage src_addr;
175 * Destination address of the original request (for potential use as exit).
177 struct sockaddr_storage dst_addr;
180 * ID of this request, also basis for hashing. Lowest 16 bit will
181 * be our message ID when doing a global DNS request and our index
182 * into the 'requests' array.
187 * Number of bytes in payload.
189 size_t payload_length;
192 * Length of the @e client_wait_list.
194 unsigned int client_wait_list_length;
197 * In which phase this this request?
199 enum RequestPhase phase;
205 * Global return value from 'main'.
207 static int global_ret;
210 * The configuration to use
212 static const struct GNUNET_CONFIGURATION_Handle *cfg;
217 static struct GNUNET_STATISTICS_Handle *stats;
220 * Handle to DNS hijacker helper process ("gnunet-helper-dns").
222 static struct GNUNET_HELPER_Handle *hijacker;
225 * Command-line arguments we are giving to the hijacker process.
227 static char *helper_argv[8];
230 * Head of DLL of clients we consult.
232 static struct ClientRecord *clients_head;
235 * Tail of DLL of clients we consult.
237 static struct ClientRecord *clients_tail;
240 * Array of all open requests.
242 static struct RequestRecord requests[UINT16_MAX + 1];
245 * Generator for unique request IDs.
247 static uint64_t request_id_gen;
250 * Handle to the DNS Stub resolver.
252 static struct GNUNET_DNSSTUB_Context *dnsstub;
256 * We're done processing a DNS request, free associated memory.
258 * @param rr request to clean up
261 cleanup_rr (struct RequestRecord *rr)
263 GNUNET_free_non_null (rr->payload);
265 rr->payload_length = 0;
266 GNUNET_array_grow (rr->client_wait_list,
267 rr->client_wait_list_length,
273 * Task run during shutdown.
278 cleanup_task (void *cls GNUNET_UNUSED)
280 if (NULL != hijacker)
282 GNUNET_HELPER_stop (hijacker, GNUNET_NO);
285 for (unsigned int i=0;i<8;i++)
286 GNUNET_free_non_null (helper_argv[i]);
287 for (unsigned int i=0;i<=UINT16_MAX;i++)
288 cleanup_rr (&requests[i]);
291 GNUNET_STATISTICS_destroy (stats,
297 GNUNET_DNSSTUB_stop (dnsstub);
304 * We're done with some request, finish processing.
306 * @param rr request send to the network or just clean up.
309 request_done (struct RequestRecord *rr)
311 struct GNUNET_MessageHeader *hdr;
313 uint16_t source_port;
314 uint16_t destination_port;
316 GNUNET_array_grow (rr->client_wait_list,
317 rr->client_wait_list_length,
319 if (RP_RESPONSE_MONITOR != rr->phase)
321 /* no response, drop */
322 LOG (GNUNET_ERROR_TYPE_DEBUG,
323 "Got no response for request %llu, dropping\n",
324 (unsigned long long) rr->request_id);
329 LOG (GNUNET_ERROR_TYPE_DEBUG,
330 "Transmitting response for request %llu\n",
331 (unsigned long long) rr->request_id);
332 /* send response via hijacker */
333 reply_len = sizeof (struct GNUNET_MessageHeader);
334 reply_len += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
335 switch (rr->src_addr.ss_family)
338 reply_len += sizeof (struct GNUNET_TUN_IPv4Header);
341 reply_len += sizeof (struct GNUNET_TUN_IPv6Header);
348 reply_len += sizeof (struct GNUNET_TUN_UdpHeader);
349 reply_len += rr->payload_length;
350 if (reply_len >= GNUNET_MAX_MESSAGE_SIZE)
352 /* response too big, drop */
353 GNUNET_break (0); /* how can this be? */
358 char buf[reply_len] GNUNET_ALIGN;
360 struct GNUNET_TUN_IPv4Header ip4;
361 struct GNUNET_TUN_IPv6Header ip6;
363 /* first, GNUnet message header */
364 hdr = (struct GNUNET_MessageHeader*) buf;
365 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
366 hdr->size = htons ((uint16_t) reply_len);
367 off = sizeof (struct GNUNET_MessageHeader);
369 /* first, TUN header */
371 struct GNUNET_TUN_Layer2PacketHeader tun;
373 tun.flags = htons (0);
374 if (rr->src_addr.ss_family == AF_INET)
375 tun.proto = htons (ETH_P_IPV4);
377 tun.proto = htons (ETH_P_IPV6);
378 GNUNET_memcpy (&buf[off],
380 sizeof (struct GNUNET_TUN_Layer2PacketHeader));
381 off += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
385 switch (rr->src_addr.ss_family)
389 struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr;
390 struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr;
392 source_port = dst->sin_port;
393 destination_port = src->sin_port;
394 GNUNET_TUN_initialize_ipv4_header (&ip4,
396 reply_len - off - sizeof (struct GNUNET_TUN_IPv4Header),
399 GNUNET_memcpy (&buf[off],
407 struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr;
408 struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr;
410 source_port = dst->sin6_port;
411 destination_port = src->sin6_port;
412 GNUNET_TUN_initialize_ipv6_header (&ip6,
414 reply_len - off - sizeof (struct GNUNET_TUN_IPv6Header),
417 GNUNET_memcpy (&buf[off],
429 struct GNUNET_TUN_UdpHeader udp;
431 udp.source_port = source_port;
432 udp.destination_port = destination_port;
433 udp.len = htons (reply_len - off);
434 if (AF_INET == rr->src_addr.ss_family)
435 GNUNET_TUN_calculate_udp4_checksum (&ip4,
440 GNUNET_TUN_calculate_udp6_checksum (&ip6,
444 GNUNET_memcpy (&buf[off],
450 /* now DNS payload */
452 GNUNET_memcpy (&buf[off], rr->payload, rr->payload_length);
453 off += rr->payload_length;
455 /* final checks & sending */
456 GNUNET_assert (off == reply_len);
457 (void) GNUNET_HELPER_send (hijacker,
461 GNUNET_STATISTICS_update (stats,
462 gettext_noop ("# DNS requests answered via TUN interface"),
465 /* clean up, we're done */
471 * Show the payload of the given request record to the client
472 * (and wait for a response).
474 * @param rr request to send to client
475 * @param cr client to send the response to
478 send_request_to_client (struct RequestRecord *rr,
479 struct ClientRecord *cr)
481 struct GNUNET_MQ_Envelope *env;
482 struct GNUNET_DNS_Request *req;
484 if (sizeof (struct GNUNET_DNS_Request) + rr->payload_length >= GNUNET_MAX_MESSAGE_SIZE)
490 LOG (GNUNET_ERROR_TYPE_DEBUG,
491 "Sending information about request %llu to local client\n",
492 (unsigned long long) rr->request_id);
493 env = GNUNET_MQ_msg_extra (req,
495 GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
496 req->reserved = htonl (0);
497 req->request_id = rr->request_id;
498 GNUNET_memcpy (&req[1],
501 GNUNET_MQ_send (cr->mq,
507 * Callback called from DNSSTUB resolver when a resolution
511 * @param dns the response itself
512 * @param r number of bytes in dns
515 process_dns_result (void *cls,
516 const struct GNUNET_TUN_DnsHeader *dns,
521 * A client has completed its processing for this
524 * @param rr request to process further
527 next_phase (struct RequestRecord *rr)
529 struct ClientRecord *cr;
532 if (rr->phase == RP_DROP)
538 for (unsigned int j=0;j<rr->client_wait_list_length;j++)
540 if (NULL != rr->client_wait_list[j])
548 send_request_to_client (rr,
549 rr->client_wait_list[nz]);
552 /* done with current phase, advance! */
553 LOG (GNUNET_ERROR_TYPE_DEBUG,
554 "Request %llu now in phase %d\n",
560 rr->phase = RP_REQUEST_MONITOR;
561 for (cr = clients_head; NULL != cr; cr = cr->next)
563 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR))
564 GNUNET_array_append (rr->client_wait_list,
565 rr->client_wait_list_length,
570 case RP_REQUEST_MONITOR:
571 rr->phase = RP_QUERY;
572 for (cr = clients_head; NULL != cr; cr = cr->next)
574 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION))
575 GNUNET_array_append (rr->client_wait_list,
576 rr->client_wait_list_length,
583 /* TODO: optionally, use this to forward DNS requests to the
584 *original* DNS server instead of the one we have configured...
585 (but then we need to create a fresh dnsstub for each request
586 *and* manage the timeout) */
587 switch (rr->dst_addr.ss_family)
590 salen = sizeof (struct sockaddr_in);
591 sa = (const struct sockaddr *) &rr->dst_addr;
594 salen = sizeof (struct sockaddr_in6);
595 sa = (const struct sockaddr *) &rr->dst_addr;
601 rr->phase = RP_INTERNET_DNS;
602 rr->rs = GNUNET_DNSSTUB_resolve (dnsstub,
609 GNUNET_STATISTICS_update (stats,
610 gettext_noop ("# DNS exit failed (failed to open socket)"),
617 case RP_INTERNET_DNS:
618 rr->phase = RP_MODIFY;
619 for (cr = clients_head; NULL != cr; cr = cr->next)
621 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
622 GNUNET_array_append (rr->client_wait_list,
623 rr->client_wait_list_length,
629 rr->phase = RP_RESPONSE_MONITOR;
630 for (cr = clients_head; NULL != cr; cr = cr->next)
632 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
633 GNUNET_array_append (rr->client_wait_list,
634 rr->client_wait_list_length,
639 case RP_RESPONSE_MONITOR:
654 * A client connected, setup our data structures.
657 * @param client handle of client that connected
658 * @param mq message queue to talk to @a client
659 * @return our `struct ClientRecord`
662 client_connect_cb (void *cls,
663 struct GNUNET_SERVICE_Client *client,
664 struct GNUNET_MQ_Handle *mq)
666 struct ClientRecord *cr = cls;
668 cr = GNUNET_new (struct ClientRecord);
671 GNUNET_CONTAINER_DLL_insert (clients_head,
679 * A client disconnected, clean up after it.
682 * @param client handle of client that disconnected
683 * @param app_ctx our `struct ClientRecord`
686 client_disconnect_cb (void *cls,
687 struct GNUNET_SERVICE_Client *client,
690 struct ClientRecord *cr = app_ctx;
691 struct RequestRecord *rr;
693 GNUNET_CONTAINER_DLL_remove (clients_head,
696 for (unsigned int i=0;i<UINT16_MAX;i++)
699 if (0 == rr->client_wait_list_length)
700 continue; /* not in use */
701 for (unsigned int j=0;j<rr->client_wait_list_length;j++)
703 if (rr->client_wait_list[j] == cr)
705 rr->client_wait_list[j] = NULL;
715 * Callback called from DNSSTUB resolver when a resolution
719 * @param dns the response itself
720 * @param r number of bytes in dns
723 process_dns_result (void *cls,
724 const struct GNUNET_TUN_DnsHeader *dns,
727 struct RequestRecord *rr;
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Processing DNS result from stub resolver\n");
731 GNUNET_assert (NULL == cls);
735 rr = &requests[dns->id];
736 if (rr->phase != RP_INTERNET_DNS)
738 /* unexpected / bogus reply */
739 GNUNET_STATISTICS_update (stats,
740 gettext_noop ("# External DNS response discarded (no matching request)"),
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 "Received DNS reply that does not match any pending request. Dropping.\n");
746 LOG (GNUNET_ERROR_TYPE_DEBUG,
747 "Got a response from the stub resolver for DNS request %llu intercepted locally!\n",
748 (unsigned long long) rr->request_id);
749 GNUNET_free_non_null (rr->payload);
750 rr->payload = GNUNET_malloc (r);
751 GNUNET_memcpy (rr->payload,
754 rr->payload_length = r;
760 * We got a new client. Make sure all new DNS requests pass by its desk.
762 * @param cls the client
763 * @param reg the init message
766 handle_client_init (void *cls,
767 const struct GNUNET_DNS_Register *reg)
769 struct ClientRecord *cr = cls;
771 cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
772 GNUNET_SERVICE_client_continue (cr->client);
777 * Check a response from a client.
779 * @param cls the client
780 * @param resp the response
781 * @return #GNUNET_OK (always fine)
784 check_client_response (void *cls,
785 const struct GNUNET_DNS_Response *resp)
787 return GNUNET_OK; /* any payload is acceptable */
792 * Handle a response from a client.
794 * @param cls the client
795 * @param resp the response
798 handle_client_response (void *cls,
799 const struct GNUNET_DNS_Response *resp)
801 struct ClientRecord *cr = cls;
802 struct RequestRecord *rr;
806 msize = ntohs (resp->header.size);
807 off = (uint16_t) resp->request_id;
809 LOG (GNUNET_ERROR_TYPE_DEBUG,
810 "Received DNS response with ID %llu from local client!\n",
811 (unsigned long long) resp->request_id);
812 if (rr->request_id != resp->request_id)
814 GNUNET_STATISTICS_update (stats,
815 gettext_noop ("# Client response discarded (no matching request)"),
818 GNUNET_SERVICE_client_continue (cr->client);
821 for (unsigned int i=0;i<rr->client_wait_list_length;i++)
823 if (NULL == rr->client_wait_list[i])
825 if (rr->client_wait_list[i] != cr)
827 rr->client_wait_list[i] = NULL;
828 switch (ntohl (resp->drop_flag))
833 case 1: /* no change */
836 msize -= sizeof (struct GNUNET_DNS_Response);
837 if ( (sizeof (struct GNUNET_TUN_DnsHeader) > msize) ||
838 (RP_REQUEST_MONITOR == rr->phase) ||
839 (RP_RESPONSE_MONITOR == rr->phase) )
842 GNUNET_SERVICE_client_drop (cr->client);
846 GNUNET_free_non_null (rr->payload);
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Changing DNS reply according to client specifications\n");
849 rr->payload = GNUNET_malloc (msize);
850 rr->payload_length = msize;
851 GNUNET_memcpy (rr->payload, &resp[1], msize);
852 if (rr->phase == RP_QUERY)
854 /* clear wait list, we're moving to MODIFY phase next */
855 GNUNET_array_grow (rr->client_wait_list,
856 rr->client_wait_list_length,
859 /* if query changed to answer, move past DNS resolution phase... */
860 if ( (RP_QUERY == rr->phase) &&
861 (rr->payload_length > sizeof (struct GNUNET_TUN_DnsHeader)) &&
862 ((struct GNUNET_TUN_DnsFlags*)&(((struct GNUNET_TUN_DnsHeader*) rr->payload)->flags))->query_or_response == 1)
864 rr->phase = RP_INTERNET_DNS;
865 GNUNET_array_grow (rr->client_wait_list,
866 rr->client_wait_list_length,
872 GNUNET_SERVICE_client_continue (cr->client);
875 /* odd, client was not on our list for the request, that ought
878 GNUNET_SERVICE_client_drop (cr->client);
883 * Functions with this signature are called whenever a complete
884 * message is received by the tokenizer from the DNS hijack process.
887 * @param message the actual message, a DNS request we should handle
890 process_helper_messages (void *cls,
891 const struct GNUNET_MessageHeader *message)
894 const struct GNUNET_TUN_Layer2PacketHeader *tun;
895 const struct GNUNET_TUN_IPv4Header *ip4;
896 const struct GNUNET_TUN_IPv6Header *ip6;
897 const struct GNUNET_TUN_UdpHeader *udp;
898 const struct GNUNET_TUN_DnsHeader *dns;
899 struct RequestRecord *rr;
900 struct sockaddr_in *srca4;
901 struct sockaddr_in6 *srca6;
902 struct sockaddr_in *dsta4;
903 struct sockaddr_in6 *dsta6;
905 LOG (GNUNET_ERROR_TYPE_DEBUG,
906 "Intercepted message via DNS hijacker\n");
907 msize = ntohs (message->size);
908 if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_TUN_IPv4Header))
910 /* non-IP packet received on TUN!? */
914 msize -= sizeof (struct GNUNET_MessageHeader);
915 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
916 msize -= sizeof (struct GNUNET_TUN_Layer2PacketHeader);
917 switch (ntohs (tun->proto))
920 ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1];
921 ip6 = NULL; /* make compiler happy */
922 if ( (msize < sizeof (struct GNUNET_TUN_IPv4Header)) ||
923 (ip4->version != 4) ||
924 (ip4->header_length != sizeof (struct GNUNET_TUN_IPv4Header) / 4) ||
925 (ntohs(ip4->total_length) != msize) ||
926 (ip4->protocol != IPPROTO_UDP) )
928 /* non-IP/UDP packet received on TUN (or with options) */
929 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
930 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
933 udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1];
934 msize -= sizeof (struct GNUNET_TUN_IPv4Header);
937 ip4 = NULL; /* make compiler happy */
938 ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
939 if ( (msize < sizeof (struct GNUNET_TUN_IPv6Header)) ||
940 (ip6->version != 6) ||
941 (ntohs (ip6->payload_length) != msize - sizeof (struct GNUNET_TUN_IPv6Header)) ||
942 (ip6->next_header != IPPROTO_UDP) )
944 /* non-IP/UDP packet received on TUN (or with extensions) */
945 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
946 _("Received malformed IPv6-UDP packet on TUN interface.\n"));
949 udp = (const struct GNUNET_TUN_UdpHeader *) &ip6[1];
950 msize -= sizeof (struct GNUNET_TUN_IPv6Header);
953 /* non-IP packet received on TUN!? */
954 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
955 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"),
956 (unsigned int) msize,
960 if ( (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader)) ||
961 (DNS_PORT != ntohs (udp->destination_port)) )
963 /* non-DNS packet received on TUN, ignore */
964 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
965 _("DNS interceptor got non-DNS packet (dropped)\n"));
966 GNUNET_STATISTICS_update (stats,
967 gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
971 msize -= sizeof (struct GNUNET_TUN_UdpHeader);
972 dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1];
973 rr = &requests[dns->id];
975 /* clean up from previous request */
976 GNUNET_free_non_null (rr->payload);
978 GNUNET_array_grow (rr->client_wait_list,
979 rr->client_wait_list_length,
982 /* setup new request */
984 switch (ntohs (tun->proto))
988 srca4 = (struct sockaddr_in*) &rr->src_addr;
989 dsta4 = (struct sockaddr_in*) &rr->dst_addr;
990 memset (srca4, 0, sizeof (struct sockaddr_in));
991 memset (dsta4, 0, sizeof (struct sockaddr_in));
992 srca4->sin_family = AF_INET;
993 dsta4->sin_family = AF_INET;
994 srca4->sin_addr = ip4->source_address;
995 dsta4->sin_addr = ip4->destination_address;
996 srca4->sin_port = udp->source_port;
997 dsta4->sin_port = udp->destination_port;
998 #if HAVE_SOCKADDR_IN_SIN_LEN
999 srca4->sin_len = sizeof (struct sockaddr_in);
1000 dsta4->sin_len = sizeof (struct sockaddr_in);
1006 srca6 = (struct sockaddr_in6*) &rr->src_addr;
1007 dsta6 = (struct sockaddr_in6*) &rr->dst_addr;
1008 memset (srca6, 0, sizeof (struct sockaddr_in6));
1009 memset (dsta6, 0, sizeof (struct sockaddr_in6));
1010 srca6->sin6_family = AF_INET6;
1011 dsta6->sin6_family = AF_INET6;
1012 srca6->sin6_addr = ip6->source_address;
1013 dsta6->sin6_addr = ip6->destination_address;
1014 srca6->sin6_port = udp->source_port;
1015 dsta6->sin6_port = udp->destination_port;
1016 #if HAVE_SOCKADDR_IN_SIN_LEN
1017 srca6->sin6_len = sizeof (struct sockaddr_in6);
1018 dsta6->sin6_len = sizeof (struct sockaddr_in6);
1025 rr->payload = GNUNET_malloc (msize);
1026 rr->payload_length = msize;
1027 GNUNET_memcpy (rr->payload, dns, msize);
1028 rr->request_id = dns->id | (request_id_gen << 16);
1030 LOG (GNUNET_ERROR_TYPE_DEBUG,
1031 "Creating new DNS request %llu\n",
1032 (unsigned long long) rr->request_id);
1033 GNUNET_STATISTICS_update (stats,
1034 gettext_noop ("# DNS requests received via TUN interface"),
1036 /* start request processing state machine */
1043 * @param cls closure
1044 * @param cfg_ configuration to use
1045 * @param service the initialized service
1049 const struct GNUNET_CONFIGURATION_Handle *cfg_,
1050 struct GNUNET_SERVICE_Handle *service)
1062 stats = GNUNET_STATISTICS_create ("dns", cfg);
1063 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
1065 dnsstub = GNUNET_DNSSTUB_start (128);
1066 /* TODO: support multiple DNS_EXIT servers being configured */
1067 /* TODO: see above TODO on using DNS server from original packet.
1068 Not sure which is best... */
1071 GNUNET_CONFIGURATION_get_value_string (cfg,
1076 GNUNET_DNSSTUB_add_dns_ip (dnsstub,
1079 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1082 _("need a valid IPv4 or IPv6 address\n"));
1083 GNUNET_free_non_null (dns_exit);
1085 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
1087 GNUNET_OS_check_helper_binary (binary,
1089 NULL)) // TODO: once we have a windows-testcase, add test parameters here
1091 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1092 _("`%s' must be installed SUID, will not run DNS interceptor\n"),
1095 GNUNET_free (binary);
1098 GNUNET_free (binary);
1100 helper_argv[0] = GNUNET_strdup ("gnunet-dns");
1101 if (GNUNET_SYSERR ==
1102 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name))
1104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1105 "No entry 'IFNAME' in configuration!\n");
1106 GNUNET_SCHEDULER_shutdown ();
1109 helper_argv[1] = ifc_name;
1110 if ( (GNUNET_SYSERR ==
1111 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6ADDR",
1114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1115 "No entry 'IPV6ADDR' in configuration!\n");
1116 GNUNET_SCHEDULER_shutdown ();
1119 helper_argv[2] = ipv6addr;
1120 if (GNUNET_SYSERR ==
1121 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6PREFIX",
1124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1125 "No entry 'IPV6PREFIX' in configuration!\n");
1126 GNUNET_SCHEDULER_shutdown ();
1129 helper_argv[3] = ipv6prefix;
1131 if (GNUNET_SYSERR ==
1132 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4ADDR",
1135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1136 "No entry 'IPV4ADDR' in configuration!\n");
1137 GNUNET_SCHEDULER_shutdown ();
1140 helper_argv[4] = ipv4addr;
1141 if (GNUNET_SYSERR ==
1142 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK",
1145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1146 "No entry 'IPV4MASK' in configuration!\n");
1147 GNUNET_SCHEDULER_shutdown ();
1150 helper_argv[5] = ipv4mask;
1152 nortsetup = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dns",
1153 "SKIP_ROUTING_SETUP");
1154 if (GNUNET_YES == nortsetup)
1155 helper_argv[6] = GNUNET_strdup("1");
1157 helper_argv[6] = GNUNET_strdup("0");
1159 helper_argv[7] = NULL;
1160 hijacker = GNUNET_HELPER_start (GNUNET_NO,
1161 "gnunet-helper-dns",
1163 &process_helper_messages,
1169 * Define "main" method using service macro.
1173 GNUNET_SERVICE_OPTION_NONE,
1176 &client_disconnect_cb,
1178 GNUNET_MQ_hd_fixed_size (client_init,
1179 GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
1180 struct GNUNET_DNS_Register,
1182 GNUNET_MQ_hd_var_size (client_response,
1183 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE,
1184 struct GNUNET_DNS_Response,
1186 GNUNET_MQ_handler_end ());
1189 /* FIXME: this might need a port on systems without 'getresgid' */
1192 * Enable use of SGID capabilities on POSIX
1194 void __attribute__ ((constructor))
1201 if (-1 == getresgid (&rgid,
1206 "getresgid failed: %s\n",
1209 else if (sgid != rgid)
1211 if (-1 == setregid (sgid,
1214 "setregid failed: %s\n",
1221 /* end of gnunet-service-dns.c */