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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file dns/gnunet-service-dns.c
21 * @brief service to intercept and modify DNS queries (and replies) of this system
22 * @author Christian Grothoff
24 * For "secure" interaction with the legacy DNS system, we permit
25 * replies only to arrive within a 5s window (and they must match
26 * ports, IPs and request IDs). Furthermore, we let the OS pick a
27 * source port, opening up to 128 sockets per address family (IPv4 or
28 * IPv6). Those sockets are closed if they are not in use for 5s
29 * (which means they will be freshly randomized afterwards). For new
30 * requests, we pick a random slot in the array with 128 socket slots
31 * (and re-use an existing socket if the slot is still in use). Thus
32 * each request will be given one of 128 random source ports, and the
33 * 128 random source ports will also change "often" (less often if the
34 * system is very busy, each time if we are mostly idle). At the same
35 * time, the system will never use more than 256 UDP sockets.
38 #include "gnunet_util_lib.h"
39 #include "gnunet_applications.h"
40 #include "gnunet_constants.h"
41 #include "gnunet_protocols.h"
42 #include "gnunet_signatures.h"
44 #include "gnunet_dns_service.h"
45 #include "gnunet_dnsparser_lib.h"
46 #include "gnunet_dnsstub_lib.h"
47 #include "gnunet_statistics_service.h"
48 #include "gnunet_tun_lib.h"
57 * Generic logging shorthand
59 #define LOG(kind, ...) \
60 GNUNET_log_from (kind, "dns", __VA_ARGS__);
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_SERVICE_Client *client;
131 * Message queue to talk to @a client.
133 struct GNUNET_MQ_Handle *mq;
136 * Flags for the client.
138 enum GNUNET_DNS_Flags flags;
144 * Entry we keep for each active request.
150 * List of clients that still need to see this request (each entry
151 * is set to NULL when the client is done).
153 struct ClientRecord **client_wait_list;
156 * Payload of the UDP packet (the UDP payload), can be either query
157 * or already the response.
162 * Socket we are using to transmit this request (must match if we receive
165 struct GNUNET_DNSSTUB_RequestSocket *rs;
168 * Source address of the original request (for sending response).
170 struct sockaddr_storage src_addr;
173 * Destination address of the original request (for potential use as exit).
175 struct sockaddr_storage dst_addr;
178 * ID of this request, also basis for hashing. Lowest 16 bit will
179 * be our message ID when doing a global DNS request and our index
180 * into the 'requests' array.
185 * Number of bytes in payload.
187 size_t payload_length;
190 * Length of the @e client_wait_list.
192 unsigned int client_wait_list_length;
195 * In which phase this this request?
197 enum RequestPhase phase;
203 * Global return value from 'main'.
205 static int global_ret;
208 * The configuration to use
210 static const struct GNUNET_CONFIGURATION_Handle *cfg;
215 static struct GNUNET_STATISTICS_Handle *stats;
218 * Handle to DNS hijacker helper process ("gnunet-helper-dns").
220 static struct GNUNET_HELPER_Handle *hijacker;
223 * Command-line arguments we are giving to the hijacker process.
225 static char *helper_argv[8];
228 * Head of DLL of clients we consult.
230 static struct ClientRecord *clients_head;
233 * Tail of DLL of clients we consult.
235 static struct ClientRecord *clients_tail;
238 * Array of all open requests.
240 static struct RequestRecord requests[UINT16_MAX + 1];
243 * Generator for unique request IDs.
245 static uint64_t request_id_gen;
248 * Handle to the DNS Stub resolver.
250 static struct GNUNET_DNSSTUB_Context *dnsstub;
254 * We're done processing a DNS request, free associated memory.
256 * @param rr request to clean up
259 cleanup_rr (struct RequestRecord *rr)
261 GNUNET_free_non_null (rr->payload);
263 rr->payload_length = 0;
264 GNUNET_array_grow (rr->client_wait_list,
265 rr->client_wait_list_length,
271 * Task run during shutdown.
276 cleanup_task (void *cls GNUNET_UNUSED)
278 if (NULL != hijacker)
280 GNUNET_HELPER_stop (hijacker, GNUNET_NO);
283 for (unsigned int i=0;i<8;i++)
284 GNUNET_free_non_null (helper_argv[i]);
285 for (unsigned int i=0;i<=UINT16_MAX;i++)
286 cleanup_rr (&requests[i]);
289 GNUNET_STATISTICS_destroy (stats,
295 GNUNET_DNSSTUB_stop (dnsstub);
302 * We're done with some request, finish processing.
304 * @param rr request send to the network or just clean up.
307 request_done (struct RequestRecord *rr)
309 struct GNUNET_MessageHeader *hdr;
311 uint16_t source_port;
312 uint16_t destination_port;
314 GNUNET_array_grow (rr->client_wait_list,
315 rr->client_wait_list_length,
317 if (RP_RESPONSE_MONITOR != rr->phase)
319 /* no response, drop */
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
321 "Got no response for request %llu, dropping\n",
322 (unsigned long long) rr->request_id);
327 LOG (GNUNET_ERROR_TYPE_DEBUG,
328 "Transmitting response for request %llu\n",
329 (unsigned long long) rr->request_id);
330 /* send response via hijacker */
331 reply_len = sizeof (struct GNUNET_MessageHeader);
332 reply_len += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
333 switch (rr->src_addr.ss_family)
336 reply_len += sizeof (struct GNUNET_TUN_IPv4Header);
339 reply_len += sizeof (struct GNUNET_TUN_IPv6Header);
346 reply_len += sizeof (struct GNUNET_TUN_UdpHeader);
347 reply_len += rr->payload_length;
348 if (reply_len >= GNUNET_MAX_MESSAGE_SIZE)
350 /* response too big, drop */
351 GNUNET_break (0); /* how can this be? */
356 char buf[reply_len] GNUNET_ALIGN;
358 struct GNUNET_TUN_IPv4Header ip4;
359 struct GNUNET_TUN_IPv6Header ip6;
361 /* first, GNUnet message header */
362 hdr = (struct GNUNET_MessageHeader*) buf;
363 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
364 hdr->size = htons ((uint16_t) reply_len);
365 off = sizeof (struct GNUNET_MessageHeader);
367 /* first, TUN header */
369 struct GNUNET_TUN_Layer2PacketHeader tun;
371 tun.flags = htons (0);
372 if (rr->src_addr.ss_family == AF_INET)
373 tun.proto = htons (ETH_P_IPV4);
375 tun.proto = htons (ETH_P_IPV6);
376 GNUNET_memcpy (&buf[off],
378 sizeof (struct GNUNET_TUN_Layer2PacketHeader));
379 off += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
383 switch (rr->src_addr.ss_family)
387 struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr;
388 struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr;
390 source_port = dst->sin_port;
391 destination_port = src->sin_port;
392 GNUNET_TUN_initialize_ipv4_header (&ip4,
394 reply_len - off - sizeof (struct GNUNET_TUN_IPv4Header),
397 GNUNET_memcpy (&buf[off],
405 struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr;
406 struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr;
408 source_port = dst->sin6_port;
409 destination_port = src->sin6_port;
410 GNUNET_TUN_initialize_ipv6_header (&ip6,
412 reply_len - off - sizeof (struct GNUNET_TUN_IPv6Header),
415 GNUNET_memcpy (&buf[off],
427 struct GNUNET_TUN_UdpHeader udp;
429 udp.source_port = source_port;
430 udp.destination_port = destination_port;
431 udp.len = htons (reply_len - off);
432 if (AF_INET == rr->src_addr.ss_family)
433 GNUNET_TUN_calculate_udp4_checksum (&ip4,
438 GNUNET_TUN_calculate_udp6_checksum (&ip6,
442 GNUNET_memcpy (&buf[off],
448 /* now DNS payload */
450 GNUNET_memcpy (&buf[off], rr->payload, rr->payload_length);
451 off += rr->payload_length;
453 /* final checks & sending */
454 GNUNET_assert (off == reply_len);
455 (void) GNUNET_HELPER_send (hijacker,
459 GNUNET_STATISTICS_update (stats,
460 gettext_noop ("# DNS requests answered via TUN interface"),
463 /* clean up, we're done */
469 * Show the payload of the given request record to the client
470 * (and wait for a response).
472 * @param rr request to send to client
473 * @param cr client to send the response to
476 send_request_to_client (struct RequestRecord *rr,
477 struct ClientRecord *cr)
479 struct GNUNET_MQ_Envelope *env;
480 struct GNUNET_DNS_Request *req;
482 if (sizeof (struct GNUNET_DNS_Request) + rr->payload_length >= GNUNET_MAX_MESSAGE_SIZE)
488 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 "Sending information about request %llu to local client\n",
490 (unsigned long long) rr->request_id);
491 env = GNUNET_MQ_msg_extra (req,
493 GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
494 req->reserved = htonl (0);
495 req->request_id = rr->request_id;
496 GNUNET_memcpy (&req[1],
499 GNUNET_MQ_send (cr->mq,
505 * Callback called from DNSSTUB resolver when a resolution
509 * @param dns the response itself
510 * @param r number of bytes in dns
513 process_dns_result (void *cls,
514 const struct GNUNET_TUN_DnsHeader *dns,
519 * A client has completed its processing for this
522 * @param rr request to process further
525 next_phase (struct RequestRecord *rr)
527 struct ClientRecord *cr;
530 if (rr->phase == RP_DROP)
536 for (unsigned int j=0;j<rr->client_wait_list_length;j++)
538 if (NULL != rr->client_wait_list[j])
546 send_request_to_client (rr,
547 rr->client_wait_list[nz]);
550 /* done with current phase, advance! */
551 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 "Request %llu now in phase %d\n",
558 rr->phase = RP_REQUEST_MONITOR;
559 for (cr = clients_head; NULL != cr; cr = cr->next)
561 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR))
562 GNUNET_array_append (rr->client_wait_list,
563 rr->client_wait_list_length,
568 case RP_REQUEST_MONITOR:
569 rr->phase = RP_QUERY;
570 for (cr = clients_head; NULL != cr; cr = cr->next)
572 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION))
573 GNUNET_array_append (rr->client_wait_list,
574 rr->client_wait_list_length,
581 /* TODO: optionally, use this to forward DNS requests to the
582 *original* DNS server instead of the one we have configured...
583 (but then we need to create a fresh dnsstub for each request
584 *and* manage the timeout) */
585 switch (rr->dst_addr.ss_family)
588 salen = sizeof (struct sockaddr_in);
589 sa = (const struct sockaddr *) &rr->dst_addr;
592 salen = sizeof (struct sockaddr_in6);
593 sa = (const struct sockaddr *) &rr->dst_addr;
599 rr->phase = RP_INTERNET_DNS;
600 rr->rs = GNUNET_DNSSTUB_resolve (dnsstub,
607 GNUNET_STATISTICS_update (stats,
608 gettext_noop ("# DNS exit failed (failed to open socket)"),
615 case RP_INTERNET_DNS:
616 rr->phase = RP_MODIFY;
617 for (cr = clients_head; NULL != cr; cr = cr->next)
619 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
620 GNUNET_array_append (rr->client_wait_list,
621 rr->client_wait_list_length,
627 rr->phase = RP_RESPONSE_MONITOR;
628 for (cr = clients_head; NULL != cr; cr = cr->next)
630 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
631 GNUNET_array_append (rr->client_wait_list,
632 rr->client_wait_list_length,
637 case RP_RESPONSE_MONITOR:
652 * A client connected, setup our data structures.
655 * @param client handle of client that connected
656 * @param mq message queue to talk to @a client
657 * @return our `struct ClientRecord`
660 client_connect_cb (void *cls,
661 struct GNUNET_SERVICE_Client *client,
662 struct GNUNET_MQ_Handle *mq)
664 struct ClientRecord *cr = cls;
666 cr = GNUNET_new (struct ClientRecord);
669 GNUNET_CONTAINER_DLL_insert (clients_head,
677 * A client disconnected, clean up after it.
680 * @param client handle of client that disconnected
681 * @param app_ctx our `struct ClientRecord`
684 client_disconnect_cb (void *cls,
685 struct GNUNET_SERVICE_Client *client,
688 struct ClientRecord *cr = app_ctx;
689 struct RequestRecord *rr;
691 GNUNET_CONTAINER_DLL_remove (clients_head,
694 for (unsigned int i=0;i<UINT16_MAX;i++)
697 if (0 == rr->client_wait_list_length)
698 continue; /* not in use */
699 for (unsigned int j=0;j<rr->client_wait_list_length;j++)
701 if (rr->client_wait_list[j] == cr)
703 rr->client_wait_list[j] = NULL;
713 * Callback called from DNSSTUB resolver when a resolution
717 * @param dns the response itself
718 * @param r number of bytes in dns
721 process_dns_result (void *cls,
722 const struct GNUNET_TUN_DnsHeader *dns,
725 struct RequestRecord *rr;
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Processing DNS result from stub resolver\n");
729 GNUNET_assert (NULL == cls);
733 rr = &requests[dns->id];
734 if (rr->phase != RP_INTERNET_DNS)
736 /* unexpected / bogus reply */
737 GNUNET_STATISTICS_update (stats,
738 gettext_noop ("# External DNS response discarded (no matching request)"),
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 "Received DNS reply that does not match any pending request. Dropping.\n");
744 LOG (GNUNET_ERROR_TYPE_DEBUG,
745 "Got a response from the stub resolver for DNS request %llu intercepted locally!\n",
746 (unsigned long long) rr->request_id);
747 GNUNET_free_non_null (rr->payload);
748 rr->payload = GNUNET_malloc (r);
749 GNUNET_memcpy (rr->payload,
752 rr->payload_length = r;
758 * We got a new client. Make sure all new DNS requests pass by its desk.
760 * @param cls the client
761 * @param reg the init message
764 handle_client_init (void *cls,
765 const struct GNUNET_DNS_Register *reg)
767 struct ClientRecord *cr = cls;
769 cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
770 GNUNET_SERVICE_client_continue (cr->client);
775 * Check a response from a client.
777 * @param cls the client
778 * @param resp the response
779 * @return #GNUNET_OK (always fine)
782 check_client_response (void *cls,
783 const struct GNUNET_DNS_Response *resp)
785 return GNUNET_OK; /* any payload is acceptable */
790 * Handle a response from a client.
792 * @param cls the client
793 * @param resp the response
796 handle_client_response (void *cls,
797 const struct GNUNET_DNS_Response *resp)
799 struct ClientRecord *cr = cls;
800 struct RequestRecord *rr;
804 msize = ntohs (resp->header.size);
805 off = (uint16_t) resp->request_id;
807 LOG (GNUNET_ERROR_TYPE_DEBUG,
808 "Received DNS response with ID %llu from local client!\n",
809 (unsigned long long) resp->request_id);
810 if (rr->request_id != resp->request_id)
812 GNUNET_STATISTICS_update (stats,
813 gettext_noop ("# Client response discarded (no matching request)"),
816 GNUNET_SERVICE_client_continue (cr->client);
819 for (unsigned int i=0;i<rr->client_wait_list_length;i++)
821 if (NULL == rr->client_wait_list[i])
823 if (rr->client_wait_list[i] != cr)
825 rr->client_wait_list[i] = NULL;
826 switch (ntohl (resp->drop_flag))
831 case 1: /* no change */
834 msize -= sizeof (struct GNUNET_DNS_Response);
835 if ( (sizeof (struct GNUNET_TUN_DnsHeader) > msize) ||
836 (RP_REQUEST_MONITOR == rr->phase) ||
837 (RP_RESPONSE_MONITOR == rr->phase) )
840 GNUNET_SERVICE_client_drop (cr->client);
844 GNUNET_free_non_null (rr->payload);
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 "Changing DNS reply according to client specifications\n");
847 rr->payload = GNUNET_malloc (msize);
848 rr->payload_length = msize;
849 GNUNET_memcpy (rr->payload, &resp[1], msize);
850 if (rr->phase == RP_QUERY)
852 /* clear wait list, we're moving to MODIFY phase next */
853 GNUNET_array_grow (rr->client_wait_list,
854 rr->client_wait_list_length,
857 /* if query changed to answer, move past DNS resolution phase... */
858 if ( (RP_QUERY == rr->phase) &&
859 (rr->payload_length > sizeof (struct GNUNET_TUN_DnsHeader)) &&
860 ((struct GNUNET_TUN_DnsFlags*)&(((struct GNUNET_TUN_DnsHeader*) rr->payload)->flags))->query_or_response == 1)
862 rr->phase = RP_INTERNET_DNS;
863 GNUNET_array_grow (rr->client_wait_list,
864 rr->client_wait_list_length,
870 GNUNET_SERVICE_client_continue (cr->client);
873 /* odd, client was not on our list for the request, that ought
876 GNUNET_SERVICE_client_drop (cr->client);
881 * Functions with this signature are called whenever a complete
882 * message is received by the tokenizer from the DNS hijack process.
885 * @param message the actual message, a DNS request we should handle
888 process_helper_messages (void *cls,
889 const struct GNUNET_MessageHeader *message)
892 const struct GNUNET_TUN_Layer2PacketHeader *tun;
893 const struct GNUNET_TUN_IPv4Header *ip4;
894 const struct GNUNET_TUN_IPv6Header *ip6;
895 const struct GNUNET_TUN_UdpHeader *udp;
896 const struct GNUNET_TUN_DnsHeader *dns;
897 struct RequestRecord *rr;
898 struct sockaddr_in *srca4;
899 struct sockaddr_in6 *srca6;
900 struct sockaddr_in *dsta4;
901 struct sockaddr_in6 *dsta6;
903 LOG (GNUNET_ERROR_TYPE_DEBUG,
904 "Intercepted message via DNS hijacker\n");
905 msize = ntohs (message->size);
906 if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_TUN_IPv4Header))
908 /* non-IP packet received on TUN!? */
912 msize -= sizeof (struct GNUNET_MessageHeader);
913 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
914 msize -= sizeof (struct GNUNET_TUN_Layer2PacketHeader);
915 switch (ntohs (tun->proto))
918 ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1];
919 ip6 = NULL; /* make compiler happy */
920 if ( (msize < sizeof (struct GNUNET_TUN_IPv4Header)) ||
921 (ip4->version != 4) ||
922 (ip4->header_length != sizeof (struct GNUNET_TUN_IPv4Header) / 4) ||
923 (ntohs(ip4->total_length) != msize) ||
924 (ip4->protocol != IPPROTO_UDP) )
926 /* non-IP/UDP packet received on TUN (or with options) */
927 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
928 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
931 udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1];
932 msize -= sizeof (struct GNUNET_TUN_IPv4Header);
935 ip4 = NULL; /* make compiler happy */
936 ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
937 if ( (msize < sizeof (struct GNUNET_TUN_IPv6Header)) ||
938 (ip6->version != 6) ||
939 (ntohs (ip6->payload_length) != msize - sizeof (struct GNUNET_TUN_IPv6Header)) ||
940 (ip6->next_header != IPPROTO_UDP) )
942 /* non-IP/UDP packet received on TUN (or with extensions) */
943 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
944 _("Received malformed IPv6-UDP packet on TUN interface.\n"));
947 udp = (const struct GNUNET_TUN_UdpHeader *) &ip6[1];
948 msize -= sizeof (struct GNUNET_TUN_IPv6Header);
951 /* non-IP packet received on TUN!? */
952 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
953 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"),
954 (unsigned int) msize,
958 if ( (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader)) ||
959 (DNS_PORT != ntohs (udp->destination_port)) )
961 /* non-DNS packet received on TUN, ignore */
962 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
963 _("DNS interceptor got non-DNS packet (dropped)\n"));
964 GNUNET_STATISTICS_update (stats,
965 gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
969 msize -= sizeof (struct GNUNET_TUN_UdpHeader);
970 dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1];
971 rr = &requests[dns->id];
973 /* clean up from previous request */
974 GNUNET_free_non_null (rr->payload);
976 GNUNET_array_grow (rr->client_wait_list,
977 rr->client_wait_list_length,
980 /* setup new request */
982 switch (ntohs (tun->proto))
986 srca4 = (struct sockaddr_in*) &rr->src_addr;
987 dsta4 = (struct sockaddr_in*) &rr->dst_addr;
988 memset (srca4, 0, sizeof (struct sockaddr_in));
989 memset (dsta4, 0, sizeof (struct sockaddr_in));
990 srca4->sin_family = AF_INET;
991 dsta4->sin_family = AF_INET;
992 srca4->sin_addr = ip4->source_address;
993 dsta4->sin_addr = ip4->destination_address;
994 srca4->sin_port = udp->source_port;
995 dsta4->sin_port = udp->destination_port;
996 #if HAVE_SOCKADDR_IN_SIN_LEN
997 srca4->sin_len = sizeof (struct sockaddr_in);
998 dsta4->sin_len = sizeof (struct sockaddr_in);
1004 srca6 = (struct sockaddr_in6*) &rr->src_addr;
1005 dsta6 = (struct sockaddr_in6*) &rr->dst_addr;
1006 memset (srca6, 0, sizeof (struct sockaddr_in6));
1007 memset (dsta6, 0, sizeof (struct sockaddr_in6));
1008 srca6->sin6_family = AF_INET6;
1009 dsta6->sin6_family = AF_INET6;
1010 srca6->sin6_addr = ip6->source_address;
1011 dsta6->sin6_addr = ip6->destination_address;
1012 srca6->sin6_port = udp->source_port;
1013 dsta6->sin6_port = udp->destination_port;
1014 #if HAVE_SOCKADDR_IN_SIN_LEN
1015 srca6->sin6_len = sizeof (struct sockaddr_in6);
1016 dsta6->sin6_len = sizeof (struct sockaddr_in6);
1023 rr->payload = GNUNET_malloc (msize);
1024 rr->payload_length = msize;
1025 GNUNET_memcpy (rr->payload, dns, msize);
1026 rr->request_id = dns->id | (request_id_gen << 16);
1028 LOG (GNUNET_ERROR_TYPE_DEBUG,
1029 "Creating new DNS request %llu\n",
1030 (unsigned long long) rr->request_id);
1031 GNUNET_STATISTICS_update (stats,
1032 gettext_noop ("# DNS requests received via TUN interface"),
1034 /* start request processing state machine */
1041 * @param cls closure
1042 * @param cfg_ configuration to use
1043 * @param service the initialized service
1047 const struct GNUNET_CONFIGURATION_Handle *cfg_,
1048 struct GNUNET_SERVICE_Handle *service)
1060 stats = GNUNET_STATISTICS_create ("dns", cfg);
1061 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
1063 dnsstub = GNUNET_DNSSTUB_start (128);
1064 /* TODO: support multiple DNS_EXIT servers being configured */
1065 /* TODO: see above TODO on using DNS server from original packet.
1066 Not sure which is best... */
1069 GNUNET_CONFIGURATION_get_value_string (cfg,
1074 GNUNET_DNSSTUB_add_dns_ip (dnsstub,
1077 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1080 _("need a valid IPv4 or IPv6 address\n"));
1081 GNUNET_free_non_null (dns_exit);
1083 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
1085 GNUNET_OS_check_helper_binary (binary,
1087 NULL)) // TODO: once we have a windows-testcase, add test parameters here
1089 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1090 _("`%s' must be installed SUID, will not run DNS interceptor\n"),
1093 GNUNET_free (binary);
1096 GNUNET_free (binary);
1098 helper_argv[0] = GNUNET_strdup ("gnunet-dns");
1099 if (GNUNET_SYSERR ==
1100 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name))
1102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1103 "No entry 'IFNAME' in configuration!\n");
1104 GNUNET_SCHEDULER_shutdown ();
1107 helper_argv[1] = ifc_name;
1108 if ( (GNUNET_SYSERR ==
1109 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6ADDR",
1112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1113 "No entry 'IPV6ADDR' in configuration!\n");
1114 GNUNET_SCHEDULER_shutdown ();
1117 helper_argv[2] = ipv6addr;
1118 if (GNUNET_SYSERR ==
1119 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6PREFIX",
1122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1123 "No entry 'IPV6PREFIX' in configuration!\n");
1124 GNUNET_SCHEDULER_shutdown ();
1127 helper_argv[3] = ipv6prefix;
1129 if (GNUNET_SYSERR ==
1130 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4ADDR",
1133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1134 "No entry 'IPV4ADDR' in configuration!\n");
1135 GNUNET_SCHEDULER_shutdown ();
1138 helper_argv[4] = ipv4addr;
1139 if (GNUNET_SYSERR ==
1140 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK",
1143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1144 "No entry 'IPV4MASK' in configuration!\n");
1145 GNUNET_SCHEDULER_shutdown ();
1148 helper_argv[5] = ipv4mask;
1150 nortsetup = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dns",
1151 "SKIP_ROUTING_SETUP");
1152 if (GNUNET_YES == nortsetup)
1153 helper_argv[6] = GNUNET_strdup("1");
1155 helper_argv[6] = GNUNET_strdup("0");
1157 helper_argv[7] = NULL;
1158 hijacker = GNUNET_HELPER_start (GNUNET_NO,
1159 "gnunet-helper-dns",
1161 &process_helper_messages,
1167 * Define "main" method using service macro.
1171 GNUNET_SERVICE_OPTION_NONE,
1174 &client_disconnect_cb,
1176 GNUNET_MQ_hd_fixed_size (client_init,
1177 GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
1178 struct GNUNET_DNS_Register,
1180 GNUNET_MQ_hd_var_size (client_response,
1181 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE,
1182 struct GNUNET_DNS_Response,
1184 GNUNET_MQ_handler_end ());
1187 /* FIXME: this might need a port on systems without 'getresgid' */
1190 * Enable use of SGID capabilities on POSIX
1192 void __attribute__ ((constructor))
1199 if (-1 == getresgid (&rgid,
1204 "getresgid failed: %s\n",
1207 else if (sgid != rgid)
1209 if (-1 == setregid (sgid,
1212 "setregid failed: %s\n",
1219 /* end of gnunet-service-dns.c */