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_SERVER_Client *client;
133 * Flags for the client.
135 enum GNUNET_DNS_Flags flags;
141 * Entry we keep for each active request.
147 * List of clients that still need to see this request (each entry
148 * is set to NULL when the client is done).
150 struct ClientRecord **client_wait_list;
153 * Payload of the UDP packet (the UDP payload), can be either query
154 * or already the response.
159 * Socket we are using to transmit this request (must match if we receive
162 struct GNUNET_DNSSTUB_RequestSocket *rs;
165 * Source address of the original request (for sending response).
167 struct sockaddr_storage src_addr;
170 * Destination address of the original request (for potential use as exit).
172 struct sockaddr_storage dst_addr;
175 * ID of this request, also basis for hashing. Lowest 16 bit will
176 * be our message ID when doing a global DNS request and our index
177 * into the 'requests' array.
182 * Number of bytes in payload.
184 size_t payload_length;
187 * Length of the client wait list.
189 unsigned int client_wait_list_length;
192 * In which phase this this request?
194 enum RequestPhase phase;
200 * Global return value from 'main'.
202 static int global_ret;
205 * The configuration to use
207 static const struct GNUNET_CONFIGURATION_Handle *cfg;
212 static struct GNUNET_STATISTICS_Handle *stats;
215 * Handle to DNS hijacker helper process ("gnunet-helper-dns").
217 static struct GNUNET_HELPER_Handle *hijacker;
220 * Command-line arguments we are giving to the hijacker process.
222 static char *helper_argv[7];
225 * Head of DLL of clients we consult.
227 static struct ClientRecord *clients_head;
230 * Tail of DLL of clients we consult.
232 static struct ClientRecord *clients_tail;
235 * Our notification context.
237 static struct GNUNET_SERVER_NotificationContext *nc;
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.
279 cleanup_task (void *cls GNUNET_UNUSED,
280 const struct GNUNET_SCHEDULER_TaskContext *tc)
284 if (NULL != hijacker)
286 GNUNET_HELPER_stop (hijacker, GNUNET_NO);
290 GNUNET_free_non_null (helper_argv[i]);
291 for (i=0;i<=UINT16_MAX;i++)
292 cleanup_rr (&requests[i]);
293 GNUNET_SERVER_notification_context_destroy (nc);
297 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
302 GNUNET_DNSSTUB_stop (dnsstub);
309 * We're done with some request, finish processing.
311 * @param rr request send to the network or just clean up.
314 request_done (struct RequestRecord *rr)
316 struct GNUNET_MessageHeader *hdr;
318 uint16_t source_port;
319 uint16_t destination_port;
321 GNUNET_array_grow (rr->client_wait_list,
322 rr->client_wait_list_length,
324 if (RP_RESPONSE_MONITOR != rr->phase)
326 /* no response, drop */
327 LOG (GNUNET_ERROR_TYPE_DEBUG,
328 "Got no response for request %llu, dropping\n",
329 (unsigned long long) rr->request_id);
334 LOG (GNUNET_ERROR_TYPE_DEBUG,
335 "Transmitting response for request %llu\n",
336 (unsigned long long) rr->request_id);
337 /* send response via hijacker */
338 reply_len = sizeof (struct GNUNET_MessageHeader);
339 reply_len += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
340 switch (rr->src_addr.ss_family)
343 reply_len += sizeof (struct GNUNET_TUN_IPv4Header);
346 reply_len += sizeof (struct GNUNET_TUN_IPv6Header);
353 reply_len += sizeof (struct GNUNET_TUN_UdpHeader);
354 reply_len += rr->payload_length;
355 if (reply_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
357 /* response too big, drop */
358 GNUNET_break (0); /* how can this be? */
363 char buf[reply_len] GNUNET_ALIGN;
365 struct GNUNET_TUN_IPv4Header ip4;
366 struct GNUNET_TUN_IPv6Header ip6;
368 /* first, GNUnet message header */
369 hdr = (struct GNUNET_MessageHeader*) buf;
370 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
371 hdr->size = htons ((uint16_t) reply_len);
372 off = sizeof (struct GNUNET_MessageHeader);
374 /* first, TUN header */
376 struct GNUNET_TUN_Layer2PacketHeader tun;
378 tun.flags = htons (0);
379 if (rr->src_addr.ss_family == AF_INET)
380 tun.proto = htons (ETH_P_IPV4);
382 tun.proto = htons (ETH_P_IPV6);
383 memcpy (&buf[off], &tun, sizeof (struct GNUNET_TUN_Layer2PacketHeader));
384 off += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
388 switch (rr->src_addr.ss_family)
392 struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr;
393 struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr;
395 source_port = dst->sin_port;
396 destination_port = src->sin_port;
397 GNUNET_TUN_initialize_ipv4_header (&ip4,
399 reply_len - off - sizeof (struct GNUNET_TUN_IPv4Header),
402 memcpy (&buf[off], &ip4, sizeof (ip4));
408 struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr;
409 struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr;
411 source_port = dst->sin6_port;
412 destination_port = src->sin6_port;
413 GNUNET_TUN_initialize_ipv6_header (&ip6,
415 reply_len - sizeof (struct GNUNET_TUN_IPv6Header),
418 memcpy (&buf[off], &ip6, sizeof (ip6));
428 struct GNUNET_TUN_UdpHeader udp;
430 udp.source_port = source_port;
431 udp.destination_port = destination_port;
432 udp.len = htons (reply_len - off);
433 if (AF_INET == rr->src_addr.ss_family)
434 GNUNET_TUN_calculate_udp4_checksum (&ip4,
439 GNUNET_TUN_calculate_udp6_checksum (&ip6,
443 memcpy (&buf[off], &udp, sizeof (udp));
447 /* now DNS payload */
449 memcpy (&buf[off], rr->payload, rr->payload_length);
450 off += rr->payload_length;
452 /* final checks & sending */
453 GNUNET_assert (off == reply_len);
454 (void) GNUNET_HELPER_send (hijacker,
458 GNUNET_STATISTICS_update (stats,
459 gettext_noop ("# DNS requests answered via TUN interface"),
462 /* clean up, we're done */
468 * Show the payload of the given request record to the client
469 * (and wait for a response).
471 * @param rr request to send to client
472 * @param client client to send the response to
475 send_request_to_client (struct RequestRecord *rr,
476 struct GNUNET_SERVER_Client *client)
478 char buf[sizeof (struct GNUNET_DNS_Request) + rr->payload_length] GNUNET_ALIGN;
479 struct GNUNET_DNS_Request *req;
481 if (sizeof (buf) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
487 LOG (GNUNET_ERROR_TYPE_DEBUG,
488 "Sending information about request %llu to local client\n",
489 (unsigned long long) rr->request_id);
490 req = (struct GNUNET_DNS_Request*) buf;
491 req->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
492 req->header.size = htons (sizeof (buf));
493 req->reserved = htonl (0);
494 req->request_id = rr->request_id;
495 memcpy (&req[1], rr->payload, rr->payload_length);
496 GNUNET_SERVER_notification_context_unicast (nc,
505 * Callback called from DNSSTUB resolver when a resolution
509 * @param rs the socket that received the response
510 * @param dns the response itself
511 * @param r number of bytes in dns
514 process_dns_result (void *cls,
515 struct GNUNET_DNSSTUB_RequestSocket *rs,
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;
534 if (rr->phase == RP_DROP)
540 for (j=0;j<rr->client_wait_list_length;j++)
542 if (NULL != rr->client_wait_list[j])
550 send_request_to_client (rr, rr->client_wait_list[nz]->client);
553 /* done with current phase, advance! */
554 LOG (GNUNET_ERROR_TYPE_DEBUG,
555 "Request %llu now in phase %d\n",
561 rr->phase = RP_REQUEST_MONITOR;
562 for (cr = clients_head; NULL != cr; cr = cr->next)
564 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR))
565 GNUNET_array_append (rr->client_wait_list,
566 rr->client_wait_list_length,
571 case RP_REQUEST_MONITOR:
572 rr->phase = RP_QUERY;
573 for (cr = clients_head; NULL != cr; cr = cr->next)
575 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION))
576 GNUNET_array_append (rr->client_wait_list,
577 rr->client_wait_list_length,
583 switch (rr->dst_addr.ss_family)
586 salen = sizeof (struct sockaddr_in);
589 salen = sizeof (struct sockaddr_in6);
595 rr->phase = RP_INTERNET_DNS;
596 rr->rs = GNUNET_DNSSTUB_resolve (dnsstub,
597 (struct sockaddr*) &rr->dst_addr,
605 GNUNET_STATISTICS_update (stats,
606 gettext_noop ("# DNS exit failed (failed to open socket)"),
612 case RP_INTERNET_DNS:
613 rr->phase = RP_MODIFY;
614 for (cr = clients_head; NULL != cr; cr = cr->next)
616 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
617 GNUNET_array_append (rr->client_wait_list,
618 rr->client_wait_list_length,
624 rr->phase = RP_RESPONSE_MONITOR;
625 for (cr = clients_head; NULL != cr; cr = cr->next)
627 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
628 GNUNET_array_append (rr->client_wait_list,
629 rr->client_wait_list_length,
634 case RP_RESPONSE_MONITOR:
649 * A client disconnected, clean up after it.
652 * @param client handle of client that disconnected
655 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
657 struct ClientRecord *cr;
658 struct RequestRecord *rr;
662 for (cr = clients_head; NULL != cr; cr = cr->next)
664 if (cr->client == client)
666 GNUNET_SERVER_client_drop (client);
667 GNUNET_CONTAINER_DLL_remove (clients_head,
670 for (i=0;i<UINT16_MAX;i++)
673 if (0 == rr->client_wait_list_length)
674 continue; /* not in use */
675 for (j=0;j<rr->client_wait_list_length;j++)
677 if (rr->client_wait_list[j] == cr)
679 rr->client_wait_list[j] = NULL;
692 * Callback called from DNSSTUB resolver when a resolution
696 * @param rs the socket that received the response
697 * @param dns the response itself
698 * @param r number of bytes in dns
701 process_dns_result (void *cls,
702 struct GNUNET_DNSSTUB_RequestSocket *rs,
703 const struct GNUNET_TUN_DnsHeader *dns,
706 struct RequestRecord *rr;
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "Processing DNS result from stub resolver\n");
710 GNUNET_assert (NULL == cls);
711 rr = &requests[dns->id];
712 if ( (rr->phase != RP_INTERNET_DNS) ||
715 /* unexpected / bogus reply */
716 GNUNET_STATISTICS_update (stats,
717 gettext_noop ("# External DNS response discarded (no matching request)"),
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Received DNS reply that does not match any pending request. Dropping.\n");
723 LOG (GNUNET_ERROR_TYPE_DEBUG,
724 "Got a response from the stub resolver for DNS request %llu intercepted locally!\n",
725 (unsigned long long) rr->request_id);
726 GNUNET_free_non_null (rr->payload);
727 rr->payload = GNUNET_malloc (r);
728 memcpy (rr->payload, dns, r);
729 rr->payload_length = r;
735 * We got a new client. Make sure all new DNS requests pass by its desk.
738 * @param client the new client
739 * @param message the init message (unused)
742 handle_client_init (void *cls GNUNET_UNUSED,
743 struct GNUNET_SERVER_Client *client,
744 const struct GNUNET_MessageHeader *message)
746 struct ClientRecord *cr;
747 const struct GNUNET_DNS_Register *reg = (const struct GNUNET_DNS_Register*) message;
749 cr = GNUNET_new (struct ClientRecord);
751 cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
752 GNUNET_SERVER_client_keep (client);
753 GNUNET_CONTAINER_DLL_insert (clients_head,
756 GNUNET_SERVER_notification_context_add (nc, client);
757 GNUNET_SERVER_receive_done (client, GNUNET_OK);
762 * We got a response from a client.
765 * @param client the client
766 * @param message the response
769 handle_client_response (void *cls GNUNET_UNUSED,
770 struct GNUNET_SERVER_Client *client,
771 const struct GNUNET_MessageHeader *message)
773 const struct GNUNET_DNS_Response *resp;
774 struct RequestRecord *rr;
779 msize = ntohs (message->size);
780 if (msize < sizeof (struct GNUNET_DNS_Response))
783 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
786 resp = (const struct GNUNET_DNS_Response*) message;
787 off = (uint16_t) resp->request_id;
789 LOG (GNUNET_ERROR_TYPE_DEBUG,
790 "Received DNS response with ID %llu from local client!\n",
791 (unsigned long long) resp->request_id);
792 if (rr->request_id != resp->request_id)
794 GNUNET_STATISTICS_update (stats,
795 gettext_noop ("# Client response discarded (no matching request)"),
797 GNUNET_SERVER_receive_done (client, GNUNET_OK);
800 for (i=0;i<rr->client_wait_list_length;i++)
802 if (NULL == rr->client_wait_list[i])
804 if (rr->client_wait_list[i]->client != client)
806 rr->client_wait_list[i] = NULL;
807 switch (ntohl (resp->drop_flag))
812 case 1: /* no change */
815 msize -= sizeof (struct GNUNET_DNS_Response);
816 if ( (sizeof (struct GNUNET_TUN_DnsHeader) > msize) ||
817 (RP_REQUEST_MONITOR == rr->phase) ||
818 (RP_RESPONSE_MONITOR == rr->phase) )
821 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
825 GNUNET_free_non_null (rr->payload);
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
827 "Changing DNS reply according to client specifications\n");
828 rr->payload = GNUNET_malloc (msize);
829 rr->payload_length = msize;
830 memcpy (rr->payload, &resp[1], msize);
831 if (rr->phase == RP_QUERY)
833 /* clear wait list, we're moving to MODIFY phase next */
834 GNUNET_array_grow (rr->client_wait_list,
835 rr->client_wait_list_length,
838 /* if query changed to answer, move past DNS resolution phase... */
839 if ( (RP_QUERY == rr->phase) &&
840 (rr->payload_length > sizeof (struct GNUNET_TUN_DnsHeader)) &&
841 ((struct GNUNET_TUN_DnsFlags*)&(((struct GNUNET_TUN_DnsHeader*) rr->payload)->flags))->query_or_response == 1)
843 rr->phase = RP_INTERNET_DNS;
844 GNUNET_array_grow (rr->client_wait_list,
845 rr->client_wait_list_length,
851 GNUNET_SERVER_receive_done (client, GNUNET_OK);
854 /* odd, client was not on our list for the request, that ought
857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
862 * Functions with this signature are called whenever a complete
863 * message is received by the tokenizer from the DNS hijack process.
866 * @param client identification of the client
867 * @param message the actual message, a DNS request we should handle
870 process_helper_messages (void *cls GNUNET_UNUSED, void *client,
871 const struct GNUNET_MessageHeader *message)
874 const struct GNUNET_TUN_Layer2PacketHeader *tun;
875 const struct GNUNET_TUN_IPv4Header *ip4;
876 const struct GNUNET_TUN_IPv6Header *ip6;
877 const struct GNUNET_TUN_UdpHeader *udp;
878 const struct GNUNET_TUN_DnsHeader *dns;
879 struct RequestRecord *rr;
880 struct sockaddr_in *srca4;
881 struct sockaddr_in6 *srca6;
882 struct sockaddr_in *dsta4;
883 struct sockaddr_in6 *dsta6;
885 LOG (GNUNET_ERROR_TYPE_DEBUG,
886 "Intercepted message via DNS hijacker\n");
887 msize = ntohs (message->size);
888 if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_TUN_IPv4Header))
890 /* non-IP packet received on TUN!? */
894 msize -= sizeof (struct GNUNET_MessageHeader);
895 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
896 msize -= sizeof (struct GNUNET_TUN_Layer2PacketHeader);
897 switch (ntohs (tun->proto))
900 ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1];
901 ip6 = NULL; /* make compiler happy */
902 if ( (msize < sizeof (struct GNUNET_TUN_IPv4Header)) ||
903 (ip4->version != 4) ||
904 (ip4->header_length != sizeof (struct GNUNET_TUN_IPv4Header) / 4) ||
905 (ntohs(ip4->total_length) != msize) ||
906 (ip4->protocol != IPPROTO_UDP) )
908 /* non-IP/UDP packet received on TUN (or with options) */
909 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
910 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
913 udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1];
914 msize -= sizeof (struct GNUNET_TUN_IPv4Header);
917 ip4 = NULL; /* make compiler happy */
918 ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
919 if ( (msize < sizeof (struct GNUNET_TUN_IPv6Header)) ||
920 (ip6->version != 6) ||
921 (ntohs (ip6->payload_length) != msize) ||
922 (ip6->next_header != IPPROTO_UDP) )
924 /* non-IP/UDP packet received on TUN (or with extensions) */
925 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
926 _("Received malformed IPv6-UDP packet on TUN interface.\n"));
929 udp = (const struct GNUNET_TUN_UdpHeader*) &ip6[1];
930 msize -= sizeof (struct GNUNET_TUN_IPv6Header);
933 /* non-IP packet received on TUN!? */
934 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
935 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"),
936 (unsigned int) msize,
940 if ( (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader)) ||
941 (DNS_PORT != ntohs (udp->destination_port)) )
943 /* non-DNS packet received on TUN, ignore */
944 GNUNET_STATISTICS_update (stats,
945 gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
949 msize -= sizeof (struct GNUNET_TUN_UdpHeader);
950 dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1];
951 rr = &requests[dns->id];
953 /* clean up from previous request */
954 GNUNET_free_non_null (rr->payload);
956 GNUNET_array_grow (rr->client_wait_list,
957 rr->client_wait_list_length,
960 /* setup new request */
962 switch (ntohs (tun->proto))
966 srca4 = (struct sockaddr_in*) &rr->src_addr;
967 dsta4 = (struct sockaddr_in*) &rr->dst_addr;
968 memset (srca4, 0, sizeof (struct sockaddr_in));
969 memset (dsta4, 0, sizeof (struct sockaddr_in));
970 srca4->sin_family = AF_INET;
971 dsta4->sin_family = AF_INET;
972 srca4->sin_addr = ip4->source_address;
973 dsta4->sin_addr = ip4->destination_address;
974 srca4->sin_port = udp->source_port;
975 dsta4->sin_port = udp->destination_port;
976 #if HAVE_SOCKADDR_IN_SIN_LEN
977 srca4->sin_len = sizeof (struct sockaddr_in);
978 dsta4->sin_len = sizeof (struct sockaddr_in);
984 srca6 = (struct sockaddr_in6*) &rr->src_addr;
985 dsta6 = (struct sockaddr_in6*) &rr->dst_addr;
986 memset (srca6, 0, sizeof (struct sockaddr_in6));
987 memset (dsta6, 0, sizeof (struct sockaddr_in6));
988 srca6->sin6_family = AF_INET6;
989 dsta6->sin6_family = AF_INET6;
990 srca6->sin6_addr = ip6->source_address;
991 dsta6->sin6_addr = ip6->destination_address;
992 srca6->sin6_port = udp->source_port;
993 dsta6->sin6_port = udp->destination_port;
994 #if HAVE_SOCKADDR_IN_SIN_LEN
995 srca6->sin6_len = sizeof (struct sockaddr_in6);
996 dsta6->sin6_len = sizeof (struct sockaddr_in6);
1003 rr->payload = GNUNET_malloc (msize);
1004 rr->payload_length = msize;
1005 memcpy (rr->payload, dns, msize);
1006 rr->request_id = dns->id | (request_id_gen << 16);
1008 LOG (GNUNET_ERROR_TYPE_DEBUG,
1009 "Creating new DNS request %llu\n",
1010 (unsigned long long) rr->request_id);
1011 GNUNET_STATISTICS_update (stats,
1012 gettext_noop ("# DNS requests received via TUN interface"),
1014 /* start request processing state machine */
1021 * @param cls closure
1022 * @param server the initialized server
1023 * @param cfg_ configuration to use
1026 run (void *cls, struct GNUNET_SERVER_Handle *server,
1027 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1029 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1030 /* callback, cls, type, size */
1031 {&handle_client_init, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
1032 sizeof (struct GNUNET_DNS_Register)},
1033 {&handle_client_response, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 0},
1041 struct in_addr dns_exit4;
1042 struct in6_addr dns_exit6;
1047 stats = GNUNET_STATISTICS_create ("dns", cfg);
1048 nc = GNUNET_SERVER_notification_context_create (server, 1);
1049 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1053 if ( ( (GNUNET_OK !=
1054 GNUNET_CONFIGURATION_get_value_string (cfg,
1058 ( (1 != inet_pton (AF_INET, dns_exit, &dns_exit4)) &&
1059 (1 != inet_pton (AF_INET6, dns_exit, &dns_exit6)) ) ) )
1061 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1064 _("need a valid IPv4 or IPv6 address\n"));
1065 GNUNET_free_non_null (dns_exit);
1068 dnsstub = GNUNET_DNSSTUB_start (dns_exit);
1069 GNUNET_free_non_null (dns_exit);
1070 GNUNET_SERVER_add_handlers (server,
1072 GNUNET_SERVER_disconnect_notify (server,
1075 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
1077 GNUNET_OS_check_helper_binary (binary,
1079 NULL)) // TODO: once we have a windows-testcase, add test parameters here
1081 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1082 _("`%s' must be installed SUID, will not run DNS interceptor\n"),
1085 GNUNET_free (binary);
1088 GNUNET_free (binary);
1090 helper_argv[0] = GNUNET_strdup ("gnunet-dns");
1091 if (GNUNET_SYSERR ==
1092 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name))
1094 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1095 "No entry 'IFNAME' in configuration!\n");
1096 GNUNET_SCHEDULER_shutdown ();
1099 helper_argv[1] = ifc_name;
1100 if ( (GNUNET_SYSERR ==
1101 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6ADDR",
1104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1105 "No entry 'IPV6ADDR' in configuration!\n");
1106 GNUNET_SCHEDULER_shutdown ();
1109 helper_argv[2] = ipv6addr;
1110 if (GNUNET_SYSERR ==
1111 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6PREFIX",
1114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1115 "No entry 'IPV6PREFIX' in configuration!\n");
1116 GNUNET_SCHEDULER_shutdown ();
1119 helper_argv[3] = ipv6prefix;
1121 if (GNUNET_SYSERR ==
1122 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4ADDR",
1125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1126 "No entry 'IPV4ADDR' in configuration!\n");
1127 GNUNET_SCHEDULER_shutdown ();
1130 helper_argv[4] = ipv4addr;
1131 if (GNUNET_SYSERR ==
1132 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK",
1135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1136 "No entry 'IPV4MASK' in configuration!\n");
1137 GNUNET_SCHEDULER_shutdown ();
1140 helper_argv[5] = ipv4mask;
1141 helper_argv[6] = NULL;
1142 hijacker = GNUNET_HELPER_start (GNUNET_NO,
1143 "gnunet-helper-dns",
1145 &process_helper_messages,
1151 * The main function for the dns service.
1153 * @param argc number of arguments from the command line
1154 * @param argv command line arguments
1155 * @return 0 ok, 1 on error
1158 main (int argc, char *const *argv)
1160 /* make use of SGID capabilities on POSIX */
1161 /* FIXME: this might need a port on systems without 'getresgid' */
1167 if (-1 == getresgid (&rgid, &egid, &sgid))
1170 "getresgid failed: %s\n",
1173 else if (sgid != rgid)
1175 if (-1 == setregid (sgid, sgid))
1176 fprintf (stderr, "setregid failed: %s\n", strerror (errno));
1179 return (GNUNET_OK ==
1180 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1181 &run, NULL)) ? global_ret : 1;
1185 /* end of gnunet-service-dns.c */