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 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_signatures.h"
31 #include "gnunet_dns_service.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_tun_lib.h"
37 * Phases each request goes through.
42 * Request has just been received.
47 * Showing the request to all monitor clients. If
48 * client list is empty, will enter QUERY phase.
53 * Showing the request to PRE-RESOLUTION clients to find an answer.
54 * If client list is empty, will trigger global DNS request.
59 * Global Internet query is now pending.
64 * Client (or global DNS request) has resulted in a response.
65 * Forward to all POST-RESOLUTION clients. If client list is empty,
66 * will enter RESPONSE_MONITOR phase.
71 * Showing the request to all monitor clients. If
72 * client list is empty, give the result to the hijacker (and be done).
77 * Some client has told us to drop the request.
84 * Entry we keep for each client.
89 * Kept in doubly-linked list.
91 struct ClientRecord *next;
94 * Kept in doubly-linked list.
96 struct ClientRecord *prev;
99 * Handle to the client.
101 struct GNUNET_SERVER_Client *client;
104 * Flags for the client.
106 enum GNUNET_DNS_Flags flags;
112 * Entry we keep for each active request.
118 * List of clients that still need to see this request (each entry
119 * is set to NULL when the client is done).
121 struct ClientRecord **client_wait_list;
124 * Payload of the UDP packet (the UDP payload), can be either query
125 * or already the response.
130 * Source address of the original request (for sending response).
132 struct sockaddr_storage src_addr;
135 * Destination address of the original request (for potential use as exit).
137 struct sockaddr_storage dst_addr;
140 * ID of this request, also basis for hashing. Lowest 16 bit will
141 * be our message ID when doing a global DNS request and our index
142 * into the 'requests' array.
147 * Number of bytes in payload.
149 size_t payload_length;
152 * Length of the client wait list.
154 unsigned int client_wait_list_length;
157 * In which phase this this request?
159 enum RequestPhase phase;
165 * The IPv4 UDP-Socket through which DNS-Resolves will be sent if they are not to be
166 * sent through gnunet. The port of this socket will not be hijacked.
168 static struct GNUNET_NETWORK_Handle *dnsout4;
171 * The IPv6 UDP-Socket through which DNS-Resolves will be sent if they are not to be
172 * sent through gnunet. The port of this socket will not be hijacked.
174 static struct GNUNET_NETWORK_Handle *dnsout6;
177 * Task for reading from dnsout4.
179 static GNUNET_SCHEDULER_TaskIdentifier read4_task;
182 * Task for reading from dnsout6.
184 static GNUNET_SCHEDULER_TaskIdentifier read6_task;
187 * The port bound to the socket dnsout (and/or dnsout6). We always (try) to bind
188 * both sockets to the same port.
190 static uint16_t dnsoutport;
193 * The configuration to use
195 static const struct GNUNET_CONFIGURATION_Handle *cfg;
200 static struct GNUNET_STATISTICS_Handle *stats;
203 * Handle to DNS hijacker helper process ("gnunet-helper-dns").
205 static struct GNUNET_HELPER_Handle *hijacker;
208 * Command-line arguments we are giving to the hijacker process.
210 static char *helper_argv[8];
213 * Head of DLL of clients we consult.
215 static struct ClientRecord *clients_head;
218 * Tail of DLL of clients we consult.
220 static struct ClientRecord *clients_tail;
223 * Our notification context.
225 static struct GNUNET_SERVER_NotificationContext *nc;
228 * Array of all open requests.
230 static struct RequestRecord requests[UINT16_MAX + 1];
233 * Generator for unique request IDs.
235 static uint64_t request_id_gen;
239 * We're done processing a DNS request, free associated memory.
241 * @param rr request to clean up
244 cleanup_rr (struct RequestRecord *rr)
246 GNUNET_free_non_null (rr->payload);
248 rr->payload_length = 0;
249 GNUNET_array_grow (rr->client_wait_list,
250 rr->client_wait_list_length,
256 * Task run during shutdown.
262 cleanup_task (void *cls GNUNET_UNUSED,
263 const struct GNUNET_SCHEDULER_TaskContext *tc)
267 GNUNET_HELPER_stop (hijacker);
270 GNUNET_free_non_null (helper_argv[i]);
273 GNUNET_NETWORK_socket_close (dnsout4);
276 if (GNUNET_SCHEDULER_NO_TASK != read4_task)
278 GNUNET_SCHEDULER_cancel (read4_task);
279 read4_task = GNUNET_SCHEDULER_NO_TASK;
283 GNUNET_NETWORK_socket_close (dnsout6);
286 if (GNUNET_SCHEDULER_NO_TASK != read6_task)
288 GNUNET_SCHEDULER_cancel (read6_task);
289 read6_task = GNUNET_SCHEDULER_NO_TASK;
291 for (i=0;i<65536;i++)
292 cleanup_rr (&requests[i]);
293 GNUNET_SERVER_notification_context_destroy (nc);
297 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
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;
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 */
326 /* send response via hijacker */
327 reply_len = sizeof (struct GNUNET_MessageHeader);
328 reply_len += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
329 switch (rr->src_addr.ss_family)
332 reply_len += sizeof (struct GNUNET_TUN_IPv4Header);
335 reply_len += sizeof (struct GNUNET_TUN_IPv6Header);
342 reply_len += sizeof (struct GNUNET_TUN_UdpHeader);
343 reply_len += rr->payload_length;
344 if (reply_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
346 /* response too big, drop */
347 GNUNET_break (0); /* how can this be? */
354 uint32_t udp_crc_sum;
356 /* first, GNUnet message header */
357 hdr = (struct GNUNET_MessageHeader*) buf;
358 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
359 hdr->size = htons ((uint16_t) reply_len);
360 off = sizeof (struct GNUNET_MessageHeader);
362 /* first, TUN header */
364 struct GNUNET_TUN_Layer2PacketHeader tun;
366 tun.flags = htons (0);
367 if (rr->src_addr.ss_family == AF_INET)
368 tun.proto = htons (ETH_P_IPV4);
370 tun.proto = htons (ETH_P_IPV6);
371 memcpy (&buf[off], &tun, sizeof (struct GNUNET_TUN_Layer2PacketHeader));
372 off += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
377 switch (rr->src_addr.ss_family)
381 struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr;
382 struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr;
383 struct GNUNET_TUN_IPv4Header ip;
387 GNUNET_TUN_initialize_ipv4_header (&ip,
389 reply_len - off - sizeof (struct GNUNET_TUN_IPv4Header),
395 udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum,
397 sizeof (struct in_addr) * 2);
401 tmp = htons (IPPROTO_UDP);
402 udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum,
405 tmp = htons (rr->payload_length + sizeof (struct GNUNET_TUN_UdpHeader));
406 udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum,
410 memcpy (&buf[off], &ip, sizeof (ip));
416 struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr;
417 struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr;
418 struct GNUNET_TUN_IPv6Header ip;
420 spt = dst->sin6_port;
421 dpt = src->sin6_port;
422 GNUNET_TUN_initialize_ipv6_header (&ip,
424 reply_len - sizeof (struct GNUNET_TUN_IPv6Header),
430 tmp = htons (rr->payload_length + sizeof (struct GNUNET_TUN_UdpHeader));
431 udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum,
434 tmp = htons (IPPROTO_UDP);
435 udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum,
439 memcpy (&buf[off], &ip, sizeof (ip));
449 struct GNUNET_TUN_UdpHeader udp;
453 udp.len = htons (reply_len - off);
455 udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum,
458 udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum,
461 udp.crc = GNUNET_CRYPTO_crc16_finish (udp_crc_sum);
462 memcpy (&buf[off], &udp, sizeof (udp));
465 /* now DNS payload */
467 memcpy (&buf[off], rr->payload, rr->payload_length);
468 off += rr->payload_length;
470 /* final checks & sending */
471 GNUNET_assert (off == reply_len);
472 GNUNET_HELPER_send (hijacker,
476 GNUNET_STATISTICS_update (stats,
477 gettext_noop ("# DNS requests answered via TUN interface"),
480 /* clean up, we're done */
486 * Show the payload of the given request record to the client
487 * (and wait for a response).
489 * @param rr request to send to client
490 * @param client client to send the response to
493 send_request_to_client (struct RequestRecord *rr,
494 struct GNUNET_SERVER_Client *client)
496 char buf[sizeof (struct GNUNET_DNS_Request) + rr->payload_length];
497 struct GNUNET_DNS_Request *req;
499 if (sizeof (buf) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
505 req = (struct GNUNET_DNS_Request*) buf;
506 req->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
507 req->header.size = htons (sizeof (buf));
508 req->reserved = htonl (0);
509 req->request_id = rr->request_id;
510 memcpy (&req[1], rr->payload, rr->payload_length);
511 GNUNET_SERVER_notification_context_unicast (nc,
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 struct GNUNET_NETWORK_Handle *dnsout;
533 if (rr->phase == RP_DROP)
539 for (j=0;j<rr->client_wait_list_length;j++)
541 if (NULL != rr->client_wait_list[j])
549 send_request_to_client (rr, rr->client_wait_list[nz]->client);
552 /* done with current phase, advance! */
556 rr->phase = RP_REQUEST_MONITOR;
557 for (cr = clients_head; NULL != cr; cr = cr->next)
559 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR))
560 GNUNET_array_append (rr->client_wait_list,
561 rr->client_wait_list_length,
566 case RP_REQUEST_MONITOR:
567 rr->phase = RP_QUERY;
568 for (cr = clients_head; NULL != cr; cr = cr->next)
570 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION))
571 GNUNET_array_append (rr->client_wait_list,
572 rr->client_wait_list_length,
578 rr->phase = RP_INTERNET_DNS;
579 switch (rr->dst_addr.ss_family)
583 salen = sizeof (struct GNUNET_TUN_IPv4Header);
587 salen = sizeof (struct GNUNET_TUN_IPv6Header);
596 GNUNET_STATISTICS_update (stats,
597 gettext_noop ("# DNS exit failed (address family not supported)"),
602 GNUNET_NETWORK_socket_sendto (dnsout,
605 (struct sockaddr*) &rr->dst_addr,
608 case RP_INTERNET_DNS:
609 rr->phase = RP_MODIFY;
610 for (cr = clients_head; NULL != cr; cr = cr->next)
612 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
613 GNUNET_array_append (rr->client_wait_list,
614 rr->client_wait_list_length,
620 rr->phase = RP_RESPONSE_MONITOR;
621 for (cr = clients_head; NULL != cr; cr = cr->next)
623 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
624 GNUNET_array_append (rr->client_wait_list,
625 rr->client_wait_list_length,
630 case RP_RESPONSE_MONITOR:
645 * A client disconnected, clean up after it.
648 * @param client handle of client that disconnected
651 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
653 struct ClientRecord *cr;
654 struct RequestRecord *rr;
658 for (cr = clients_head; NULL != cr; cr = cr->next)
660 if (cr->client == client)
662 GNUNET_SERVER_client_drop (client);
663 GNUNET_CONTAINER_DLL_remove (clients_head,
666 for (i=0;i<UINT16_MAX;i++)
669 if (0 == rr->client_wait_list_length)
670 continue; /* not in use */
671 for (j=0;j<rr->client_wait_list_length;j++)
673 if (rr->client_wait_list[j] == cr)
675 rr->client_wait_list[j] = NULL;
688 * Read a DNS response from the (unhindered) UDP-Socket
690 * @param cls socket to read from
691 * @param tc scheduler context (must be shutdown or read ready)
694 read_response (void *cls,
695 const struct GNUNET_SCHEDULER_TaskContext *tc)
697 struct GNUNET_NETWORK_Handle *dnsout = cls;
698 struct sockaddr_in addr4;
699 struct sockaddr_in6 addr6;
700 struct sockaddr *addr;
701 struct GNUNET_TUN_DnsHeader *dns;
703 struct RequestRecord *rr;
707 if (dnsout == dnsout4)
709 addrlen = sizeof (struct sockaddr_in);
710 addr = (struct sockaddr* ) &addr4;
711 read4_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
718 addrlen = sizeof (struct sockaddr_in6);
719 addr = (struct sockaddr* ) &addr6;
720 read6_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
725 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
729 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
731 /* conservative choice: */
735 /* port the code above? */
740 unsigned char buf[len];
742 memset (addr, 0, addrlen);
743 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
748 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
751 if (sizeof (struct GNUNET_TUN_DnsHeader) > r)
753 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
754 _("Received DNS response that is too small (%u bytes)"),
758 dns = (struct GNUNET_TUN_DnsHeader *) buf;
759 rr = &requests[dns->id];
760 if (rr->phase != RP_INTERNET_DNS)
762 /* unexpected / bogus reply */
763 GNUNET_STATISTICS_update (stats,
764 gettext_noop ("# External DNS response discarded (no matching request)"),
768 GNUNET_free_non_null (rr->payload);
769 rr->payload = GNUNET_malloc (len);
770 memcpy (rr->payload, buf, len);
771 rr->payload_length = len;
778 * Open source port for sending DNS request on IPv4.
780 * @return GNUNET_OK on success
785 struct sockaddr_in addr;
788 dnsout4 = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
790 return GNUNET_SYSERR;
792 memset (&addr, 0, sizeof (struct sockaddr_in));
793 addr.sin_family = AF_INET;
794 int err = GNUNET_NETWORK_socket_bind (dnsout4,
795 (struct sockaddr *) &addr,
796 sizeof (struct sockaddr_in));
798 if (err != GNUNET_OK)
800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
801 _("Could not bind to any port: %s\n"),
803 GNUNET_NETWORK_socket_close (dnsout4);
805 return GNUNET_SYSERR;
808 /* Read the port we bound to */
809 addrlen = sizeof (struct sockaddr_in);
810 if (0 != getsockname (GNUNET_NETWORK_get_fd (dnsout4),
811 (struct sockaddr *) &addr,
814 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
815 _("Could not determine port I got: %s\n"),
817 GNUNET_NETWORK_socket_close (dnsout4);
819 return GNUNET_SYSERR;
821 dnsoutport = htons (addr.sin_port);
823 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
824 _("GNUnet DNS will exit on source port %u\n"),
825 (unsigned int) dnsoutport);
826 read4_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
828 &read_response, dnsout4);
834 * Open source port for sending DNS request on IPv6. Should be
835 * called AFTER open_port4.
837 * @return GNUNET_OK on success
842 struct sockaddr_in6 addr;
845 dnsout6 = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_DGRAM, 0);
848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
849 _("Could not create IPv6 socket: %s\n"),
851 return GNUNET_SYSERR;
853 memset (&addr, 0, sizeof (struct sockaddr_in6));
854 addr.sin6_family = AF_INET6;
855 addr.sin6_port = htons (dnsoutport);
856 int err = GNUNET_NETWORK_socket_bind (dnsout6,
857 (struct sockaddr *) &addr,
858 sizeof (struct sockaddr_in6));
860 if (err != GNUNET_OK)
862 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
863 _("Could not bind to port %u: %s\n"),
864 (unsigned int) dnsoutport,
866 GNUNET_NETWORK_socket_close (dnsout6);
868 return GNUNET_SYSERR;
872 addrlen = sizeof (struct sockaddr_in6);
873 if (0 != getsockname (GNUNET_NETWORK_get_fd (dnsout6),
874 (struct sockaddr *) &addr,
877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
878 _("Could not determine port I got: %s\n"),
880 GNUNET_NETWORK_socket_close (dnsout6);
882 return GNUNET_SYSERR;
885 dnsoutport = htons (addr.sin6_port);
886 read6_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
888 &read_response, dnsout6);
894 * We got a new client. Make sure all new DNS requests pass by its desk.
897 * @param client the new client
898 * @param message the init message (unused)
901 handle_client_init (void *cls GNUNET_UNUSED,
902 struct GNUNET_SERVER_Client *client,
903 const struct GNUNET_MessageHeader *message)
905 struct ClientRecord *cr;
906 const struct GNUNET_DNS_Register *reg = (const struct GNUNET_DNS_Register*) message;
908 cr = GNUNET_malloc (sizeof (struct ClientRecord));
910 cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
911 GNUNET_SERVER_client_keep (client);
912 GNUNET_CONTAINER_DLL_insert (clients_head,
915 GNUNET_SERVER_notification_context_add (nc, client);
916 GNUNET_SERVER_receive_done (client, GNUNET_OK);
921 * We got a response from a client.
924 * @param client the client
925 * @param message the response
928 handle_client_response (void *cls GNUNET_UNUSED,
929 struct GNUNET_SERVER_Client *client,
930 const struct GNUNET_MessageHeader *message)
932 const struct GNUNET_DNS_Response *resp;
933 struct RequestRecord *rr;
938 msize = ntohs (message->size);
939 if (msize < sizeof (struct GNUNET_DNS_Response))
942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
945 resp = (const struct GNUNET_DNS_Response*) message;
946 off = (uint16_t) resp->request_id;
948 if (rr->request_id != resp->request_id)
950 GNUNET_STATISTICS_update (stats,
951 gettext_noop ("# Client response discarded (no matching request)"),
953 GNUNET_SERVER_receive_done (client, GNUNET_OK);
956 for (i=0;i<rr->client_wait_list_length;i++)
958 if (NULL == rr->client_wait_list[i])
960 if (rr->client_wait_list[i]->client != client)
962 rr->client_wait_list[i] = NULL;
963 switch (ntohl (resp->drop_flag))
968 case 1: /* no change */
971 msize -= sizeof (struct GNUNET_DNS_Response);
972 if ( (sizeof (struct GNUNET_TUN_DnsHeader) > msize) ||
973 (RP_REQUEST_MONITOR == rr->phase) ||
974 (RP_RESPONSE_MONITOR == rr->phase) )
977 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
981 GNUNET_free_non_null (rr->payload);
983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
984 _("Changing DNS reply according to client specifications\n"));
986 rr->payload = GNUNET_malloc (msize);
987 rr->payload_length = msize;
988 memcpy (rr->payload, &resp[1], msize);
989 if (rr->phase == RP_QUERY)
991 /* clear wait list, we're moving to MODIFY phase next */
992 GNUNET_array_grow (rr->client_wait_list,
993 rr->client_wait_list_length,
999 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1002 /* odd, client was not on our list for the request, that ought
1005 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1010 * Functions with this signature are called whenever a complete
1011 * message is received by the tokenizer from the DNS hijack process.
1013 * @param cls closure
1014 * @param client identification of the client
1015 * @param message the actual message, a DNS request we should handle
1018 process_helper_messages (void *cls GNUNET_UNUSED, void *client,
1019 const struct GNUNET_MessageHeader *message)
1022 const struct GNUNET_TUN_Layer2PacketHeader *tun;
1023 const struct GNUNET_TUN_IPv4Header *ip4;
1024 const struct GNUNET_TUN_IPv6Header *ip6;
1025 const struct GNUNET_TUN_UdpHeader *udp;
1026 const struct GNUNET_TUN_DnsHeader *dns;
1027 struct RequestRecord *rr;
1028 struct sockaddr_in *srca4;
1029 struct sockaddr_in6 *srca6;
1030 struct sockaddr_in *dsta4;
1031 struct sockaddr_in6 *dsta6;
1033 msize = ntohs (message->size);
1034 if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_TUN_IPv4Header))
1036 /* non-IP packet received on TUN!? */
1040 msize -= sizeof (struct GNUNET_MessageHeader);
1041 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
1042 msize -= sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1043 switch (ntohs (tun->proto))
1046 ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1];
1047 if ( (msize < sizeof (struct GNUNET_TUN_IPv4Header)) ||
1048 (ip4->version != 4) ||
1049 (ip4->header_length != sizeof (struct GNUNET_TUN_IPv4Header) / 4) ||
1050 (ntohs(ip4->total_length) != msize) ||
1051 (ip4->protocol != IPPROTO_UDP) )
1053 /* non-IP/UDP packet received on TUN (or with options) */
1054 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1055 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
1058 udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1];
1059 msize -= sizeof (struct GNUNET_TUN_IPv4Header);
1062 ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
1063 if ( (msize < sizeof (struct GNUNET_TUN_IPv6Header)) ||
1064 (ip6->version != 6) ||
1065 (ntohs (ip6->payload_length) != msize) ||
1066 (ip6->next_header != IPPROTO_UDP) )
1068 /* non-IP/UDP packet received on TUN (or with extensions) */
1069 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1070 _("Received malformed IPv6-UDP packet on TUN interface.\n"));
1073 udp = (const struct GNUNET_TUN_UdpHeader*) &ip6[1];
1074 msize -= sizeof (struct GNUNET_TUN_IPv6Header);
1077 /* non-IP packet received on TUN!? */
1078 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1079 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"),
1080 (unsigned int) msize,
1081 ntohs (tun->proto));
1084 if (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader))
1086 /* non-DNS packet received on TUN, ignore */
1087 GNUNET_STATISTICS_update (stats,
1088 gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
1092 msize -= sizeof (struct GNUNET_TUN_UdpHeader);
1093 dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1];
1094 rr = &requests[dns->id];
1096 /* clean up from previous request */
1097 GNUNET_free_non_null (rr->payload);
1099 GNUNET_array_grow (rr->client_wait_list,
1100 rr->client_wait_list_length,
1103 /* setup new request */
1104 rr->phase = RP_INIT;
1105 if (ip4->version == 4)
1107 srca4 = (struct sockaddr_in*) &rr->src_addr;
1108 dsta4 = (struct sockaddr_in*) &rr->dst_addr;
1109 memset (srca4, 0, sizeof (struct sockaddr_in));
1110 memset (dsta4, 0, sizeof (struct sockaddr_in));
1111 srca4->sin_family = AF_INET;
1112 dsta4->sin_family = AF_INET;
1113 srca4->sin_addr = ip4->source_address;
1114 dsta4->sin_addr = ip4->destination_address;
1115 srca4->sin_port = udp->spt;
1116 dsta4->sin_port = udp->dpt;
1117 #if HAVE_SOCKADDR_IN_SIN_LEN
1118 srca4->sin_len = sizeof (sizeof (struct sockaddr_in));
1119 dsta4->sin_len = sizeof (sizeof (struct sockaddr_in));
1124 srca6 = (struct sockaddr_in6*) &rr->src_addr;
1125 dsta6 = (struct sockaddr_in6*) &rr->dst_addr;
1126 memset (srca6, 0, sizeof (struct sockaddr_in6));
1127 memset (dsta6, 0, sizeof (struct sockaddr_in6));
1128 srca6->sin6_family = AF_INET6;
1129 dsta6->sin6_family = AF_INET6;
1130 srca6->sin6_addr = ip6->source_address;
1131 dsta6->sin6_addr = ip6->destination_address;
1132 srca6->sin6_port = udp->spt;
1133 dsta6->sin6_port = udp->dpt;
1134 #if HAVE_SOCKADDR_IN_SIN_LEN
1135 srca6->sin6_len = sizeof (sizeof (struct sockaddr_in6));
1136 dsta6->sin6_len = sizeof (sizeof (struct sockaddr_in6));
1139 rr->payload = GNUNET_malloc (msize);
1140 rr->payload_length = msize;
1141 memcpy (rr->payload, dns, msize);
1142 rr->request_id = dns->id | (request_id_gen << 16);
1145 GNUNET_STATISTICS_update (stats,
1146 gettext_noop ("# DNS requests received via TUN interface"),
1148 /* start request processing state machine */
1154 * @param cls closure
1155 * @param server the initialized server
1156 * @param cfg_ configuration to use
1159 run (void *cls, struct GNUNET_SERVER_Handle *server,
1160 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1162 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1163 /* callback, cls, type, size */
1164 {&handle_client_init, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
1165 sizeof (struct GNUNET_DNS_Register)},
1166 {&handle_client_response, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 0},
1177 stats = GNUNET_STATISTICS_create ("dns", cfg);
1178 nc = GNUNET_SERVER_notification_context_create (server, 1);
1179 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1182 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1184 if ( (GNUNET_OK != open_port4 ()) &&
1185 (GNUNET_OK != open_port6 ()) )
1187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1188 _("Failed to open any port to provide DNS exit\n"));
1189 GNUNET_SCHEDULER_shutdown ();
1194 helper_argv[0] = GNUNET_strdup ("gnunet-dns");
1195 if (GNUNET_SYSERR ==
1196 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name))
1198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1199 "No entry 'IFNAME' in configuration!\n");
1200 GNUNET_SCHEDULER_shutdown ();
1203 helper_argv[1] = ifc_name;
1204 if ( (GNUNET_SYSERR ==
1205 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6ADDR",
1208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1209 "No entry 'IPV6ADDR' in configuration!\n");
1210 GNUNET_SCHEDULER_shutdown ();
1213 helper_argv[2] = ipv6addr;
1214 if (GNUNET_SYSERR ==
1215 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6PREFIX",
1218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1219 "No entry 'IPV6PREFIX' in configuration!\n");
1220 GNUNET_SCHEDULER_shutdown ();
1223 helper_argv[3] = ipv6prefix;
1225 if (GNUNET_SYSERR ==
1226 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4ADDR",
1229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1230 "No entry 'IPV4ADDR' in configuration!\n");
1231 GNUNET_SCHEDULER_shutdown ();
1234 helper_argv[4] = ipv4addr;
1235 if (GNUNET_SYSERR ==
1236 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK",
1239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1240 "No entry 'IPV4MASK' in configuration!\n");
1241 GNUNET_SCHEDULER_shutdown ();
1244 helper_argv[5] = ipv4mask;
1245 GNUNET_snprintf (port_s,
1248 (unsigned int) dnsoutport);
1249 helper_argv[6] = GNUNET_strdup (port_s);
1250 helper_argv[7] = NULL;
1251 hijacker = GNUNET_HELPER_start ("gnunet-helper-dns",
1253 &process_helper_messages,
1255 GNUNET_SERVER_add_handlers (server, handlers);
1256 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
1261 * The main function for the dns service.
1263 * @param argc number of arguments from the command line
1264 * @param argv command line arguments
1265 * @return 0 ok, 1 on error
1268 main (int argc, char *const *argv)
1270 return (GNUNET_OK ==
1271 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1272 &run, NULL)) ? 0 : 1;
1276 /* end of gnunet-service-dns_new.c */