From 1574e0f8c2a55363c024a95f440ba7be647efbe1 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 15 Jan 2012 20:41:40 +0000 Subject: -removing legacy dns/vpn/exit code and renaming -new versions to current --- src/dns/Makefile.am | 47 +- src/dns/dns.h | 93 +- src/dns/dns_api.c | 664 +++++---- src/dns/dns_api_new.c | 522 ------- src/dns/dns_new.h | 101 -- src/dns/dnsparser.c | 329 +---- src/dns/gnunet-dns-monitor.c | 2 +- src/dns/gnunet-dns-redirector.c | 2 +- src/dns/gnunet-helper-hijack-dns.c | 328 ----- src/dns/gnunet-service-dns.c | 2463 ++++++++++++++-------------------- src/dns/gnunet-service-dns_new.c | 1293 ------------------ src/include/Makefile.am | 1 - src/include/gnunet_dns_service-new.h | 186 --- src/include/gnunet_dns_service.h | 236 ++-- src/include/gnunet_dnsparser_lib.h | 93 -- src/vpn/Makefile.am | 25 +- src/vpn/gnunet-daemon-exit.c | 1501 --------------------- src/vpn/gnunet-daemon-vpn.c | 1808 ------------------------- src/vpn/test-conf.conf | 276 ---- 19 files changed, 1620 insertions(+), 8350 deletions(-) delete mode 100644 src/dns/dns_api_new.c delete mode 100644 src/dns/dns_new.h delete mode 100644 src/dns/gnunet-helper-hijack-dns.c delete mode 100644 src/dns/gnunet-service-dns_new.c delete mode 100644 src/include/gnunet_dns_service-new.h delete mode 100644 src/vpn/gnunet-daemon-exit.c delete mode 100644 src/vpn/gnunet-daemon-vpn.c delete mode 100644 src/vpn/test-conf.conf diff --git a/src/dns/Makefile.am b/src/dns/Makefile.am index 99e78d7d6..f8bdae94e 100644 --- a/src/dns/Makefile.am +++ b/src/dns/Makefile.am @@ -16,26 +16,23 @@ pkgcfg_DATA = \ dns.conf if LINUX -HIJACKBIN = gnunet-helper-hijack-dns gnunet-helper-dns +HIJACKBIN = gnunet-helper-dns install-exec-hook: - $(SUDO_BINARY) chown root:root $(bindir)/gnunet-helper-hijack-dns || true - $(SUDO_BINARY) chmod u+s $(bindir)/gnunet-helper-hijack-dns || true $(SUDO_BINARY) chown root $(bindir)/gnunet-helper-dns || true $(SUDO_BINARY) chgrp $(GNUNETDNS_GROUP) $(bindir)/gnunet-helper-dns || true $(SUDO_BINARY) chmod 4750 $(bindir)/gnunet-helper-dns || true - $(SUDO_BINARY) chgrp $(GNUNETDNS_GROUP) $(bindir)/gnunet-service-dns-new || true - $(SUDO_BINARY) chmod 2755 $(bindir)/gnunet-helper-dns || true + $(SUDO_BINARY) chgrp $(GNUNETDNS_GROUP) $(bindir)/gnunet-service-dns || true + $(SUDO_BINARY) chmod 2755 $(bindir)/gnunet-service-dns || true else install-exec-hook: endif lib_LTLIBRARIES = \ libgnunetdnsparser.la \ - libgnunetdnsnew.la \ libgnunetdns.la bin_PROGRAMS = \ - gnunet-service-dns gnunet-service-dns-new $(HIJACKBIN) + gnunet-service-dns $(HIJACKBIN) noinst_PROGRAMS = \ gnunet-dns-monitor gnunet-dns-redirector @@ -44,50 +41,35 @@ plugin_LTLIBRARIES = \ libgnunet_plugin_block_dns.la -gnunet_helper_hijack_dns_SOURCES = \ - gnunet-helper-hijack-dns.c - gnunet_helper_dns_SOURCES = \ gnunet-helper-dns.c -gnunet_service_dns_SOURCES = \ - gnunet-service-dns.c -gnunet_service_dns_LDADD = \ - $(top_builddir)/src/core/libgnunetcore.la \ - $(top_builddir)/src/statistics/libgnunetstatistics.la \ - $(top_builddir)/src/util/libgnunetutil.la \ - $(top_builddir)/src/dht/libgnunetdht.la \ - $(top_builddir)/src/mesh/libgnunetmesh.la \ - $(top_builddir)/src/dns/libgnunetdnsparser.la \ - $(GN_LIBINTL) -gnunet_service_dns_DEPENDENCIES = \ - libgnunetdnsparser.la gnunet_dns_monitor_SOURCES = \ gnunet-dns-monitor.c gnunet_dns_monitor_LDADD = \ $(top_builddir)/src/dns/libgnunetdnsparser.la \ - $(top_builddir)/src/dns/libgnunetdnsnew.la \ + $(top_builddir)/src/dns/libgnunetdns.la \ $(top_builddir)/src/util/libgnunetutil.la \ $(GN_LIBINTL) gnunet_dns_monitor_DEPENDENCIES = \ libgnunetdnsparser.la \ - libgnunetdnsnew.la + libgnunetdns.la gnunet_dns_redirector_SOURCES = \ gnunet-dns-redirector.c gnunet_dns_redirector_LDADD = \ $(top_builddir)/src/dns/libgnunetdnsparser.la \ - $(top_builddir)/src/dns/libgnunetdnsnew.la \ + $(top_builddir)/src/dns/libgnunetdns.la \ $(top_builddir)/src/util/libgnunetutil.la \ $(GN_LIBINTL) gnunet_dns_redirector_DEPENDENCIES = \ libgnunetdnsparser.la \ - libgnunetdnsnew.la + libgnunetdns.la -gnunet_service_dns_new_SOURCES = \ - gnunet-service-dns_new.c -gnunet_service_dns_new_LDADD = \ +gnunet_service_dns_SOURCES = \ + gnunet-service-dns.c +gnunet_service_dns_LDADD = \ $(top_builddir)/src/statistics/libgnunetstatistics.la \ $(top_builddir)/src/util/libgnunetutil.la \ $(GN_LIBINTL) @@ -106,13 +88,6 @@ libgnunetdns_la_LIBADD = \ libgnunetdns_la_LDFLAGS = \ $(GN_LIB_LDFLAGS) -libgnunetdnsnew_la_SOURCES = \ - dns_api_new.c dns_new.h -libgnunetdnsnew_la_LIBADD = \ - $(top_builddir)/src/util/libgnunetutil.la $(XLIB) -libgnunetdnsnew_la_LDFLAGS = \ - $(GN_LIB_LDFLAGS) - libgnunet_plugin_block_dns_la_SOURCES = \ plugin_block_dns.c libgnunet_plugin_block_dns_la_LIBADD = \ diff --git a/src/dns/dns.h b/src/dns/dns.h index dd0f55e16..29a9f937b 100644 --- a/src/dns/dns.h +++ b/src/dns/dns.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet - (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors) + (C) 2012 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -19,36 +19,83 @@ */ /** - * @file dns/dns.h + * @file dns/dns_new.h * @brief IPC messages between DNS API and DNS service - * @author Philipp Toelke * @author Christian Grothoff */ -#ifndef DNS_H -#define DNS_H +#ifndef DNS_NEW_H +#define DNS_NEW_H GNUNET_NETWORK_STRUCT_BEGIN -struct query_packet + +/** + * Message from client to DNS service to register itself. + */ +struct GNUNET_DNS_Register +{ + /** + * Header of type GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT + */ + struct GNUNET_MessageHeader header; + + /** + * NBO encoding of 'enum GNUNET_DNS_Flags' for the client. + */ + uint32_t flags; +}; + + +/** + * Message from DNS service to client: please handle a request. + */ +struct GNUNET_DNS_Request +{ + /** + * Header of type GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST + */ + struct GNUNET_MessageHeader header; + + /** + * Always zero. + */ + uint32_t reserved GNUNET_PACKED; + + /** + * Unique request ID. + */ + uint64_t request_id GNUNET_PACKED; + + /* followed by original DNS request (without UDP header) */ + +}; + + +/** + * Message from client to DNS service: here is my reply. + */ +struct GNUNET_DNS_Response { - struct GNUNET_MessageHeader hdr; - - /** - * The IP-Address this query was originally sent to - */ - char orig_to[16]; - /** - * The IP-Address this query was originally sent from - */ - char orig_from[16]; - char addrlen; - /** - * The UDP-Port this query was originally sent from - */ - uint16_t src_port GNUNET_PACKED; - - unsigned char data[1]; /* The DNS-Packet */ + /** + * Header of type GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE + */ + struct GNUNET_MessageHeader header; + + /** + * Zero to drop, 1 for no change (no payload), 2 for update (message has payload). + */ + uint32_t drop_flag GNUNET_PACKED; + + /** + * Unique request ID. + */ + uint64_t request_id GNUNET_PACKED; + + /* followed by original DNS request (without UDP header) */ + }; + + GNUNET_NETWORK_STRUCT_END #endif diff --git a/src/dns/dns_api.c b/src/dns/dns_api.c index b434fc1e7..dfce0af1b 100644 --- a/src/dns/dns_api.c +++ b/src/dns/dns_api.c @@ -1,342 +1,522 @@ /* - This file is part of GNUnet. - (C) 2010 Christian Grothoff - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. + This file is part of GNUnet + (C) 2012 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 2, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + */ - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. +/** + * @file dns/dns_api_new.c + * @brief API to access the DNS service. + * @author Christian Grothoff + */ +#include "platform.h" +#include "gnunet_dns_service.h" +#include "dns.h" - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. -*/ /** - * @file dns/dns_api.c - * @brief - * @author Philipp Toelke + * Reply to send to service. */ -#include -#include -#include -#include -#include -#include -#include -#include -#include +struct ReplyQueueEntry +{ + /** + * Kept in DLL. + */ + struct ReplyQueueEntry *next; -#include "gnunet_dns_service.h" -#include "dns.h" + /** + * Kept in DLL. + */ + struct ReplyQueueEntry *prev; + + /** + * Message to transmit, allocated at the end of this struct. + */ + const struct GNUNET_MessageHeader *msg; -struct query_packet_list +}; + + +/** + * Handle to identify an individual DNS request. + */ +struct GNUNET_DNS_RequestHandle { - struct query_packet_list *next; - struct query_packet_list *prev; - struct query_packet pkt; + + /** + * Handle to DNS API. + */ + struct GNUNET_DNS_Handle *dh; + + /** + * Stored in network byte order (as for us, it is just a random number). + */ + uint64_t request_id; + + /** + * Re-connect counter, to make sure we did not reconnect in the meantime. + */ + uint32_t generation; + }; +/** + * DNS handle + */ struct GNUNET_DNS_Handle { - struct query_packet_list *head; - struct query_packet_list *tail; + + /** + * Connection to DNS service, or NULL. + */ struct GNUNET_CLIENT_Connection *dns_connection; - unsigned char restart_hijack; + /** + * Handle to active transmission request, or NULL. + */ struct GNUNET_CLIENT_TransmitHandle *dns_transmit_handle; + /** + * Configuration to use. + */ const struct GNUNET_CONFIGURATION_Handle *cfg; - GNUNET_DNS_ResponseCallback process_answer_cb; + /** + * Function to call to get replies. + */ + GNUNET_DNS_RequestHandler rh; - void *process_answer_cb_cls; -}; + /** + * Closure for 'rh'. + */ + void *rh_cls; + /** + * Head of replies to transmit. + */ + struct ReplyQueueEntry *rq_head; -/** - * Callback called by notify_transmit_ready; sends dns-queries or rehijack-messages - * to the service-dns - * {{{ - */ -size_t -send_query (void *cls GNUNET_UNUSED, size_t size, void *buf) -{ - struct GNUNET_DNS_Handle *h = cls; + /** + * Tail of replies to transmit. + */ + struct ReplyQueueEntry *rq_tail; - size_t len; + /** + * Task to reconnect to the service. + */ + GNUNET_SCHEDULER_TaskIdentifier reconnect_task; - h->dns_transmit_handle = NULL; + /** + * Re-connect counter, to make sure we did not reconnect in the meantime. + */ + uint32_t generation; + + /** + * Flags for events we care about. + */ + enum GNUNET_DNS_Flags flags; - /* - * Send the rehijack-message + /** + * Did we start the receive loop yet? */ - if (h->restart_hijack == 1) - { - h->restart_hijack = 0; - /* - * The message is just a header - */ - GNUNET_assert (sizeof (struct GNUNET_MessageHeader) <= size); - struct GNUNET_MessageHeader *hdr = buf; - - len = sizeof (struct GNUNET_MessageHeader); - hdr->size = htons (len); - hdr->type = htons (GNUNET_MESSAGE_TYPE_REHIJACK); - } - else if (h->head != NULL) - { - struct query_packet_list *query = h->head; + int in_receive; + + /** + * Number of GNUNET_DNS_RequestHandles we have outstanding. Must be 0 before + * we can be disconnected. + */ + unsigned int pending_requests; +}; + - len = ntohs (query->pkt.hdr.size); +/** + * Add the given reply to our transmission queue and trigger sending if needed. + * + * @param dh handle with the connection + * @param qe reply to queue + */ +static void +queue_reply (struct GNUNET_DNS_Handle *dh, + struct ReplyQueueEntry *qe); - GNUNET_assert (len <= size); - memcpy (buf, &query->pkt.hdr, len); +/** + * Reconnect to the DNS service. + * + * @param cls handle with the connection to connect + * @param tc scheduler context (unused) + */ +static void +reconnect (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct GNUNET_DNS_Handle *dh = cls; + struct ReplyQueueEntry *qe; + struct GNUNET_DNS_Register *msg; - GNUNET_CONTAINER_DLL_remove (h->head, h->tail, query); + dh->reconnect_task = GNUNET_SCHEDULER_NO_TASK; + dh->dns_connection = GNUNET_CLIENT_connect ("dns", dh->cfg); + if (NULL == dh->dns_connection) + return; + dh->generation++; + qe = GNUNET_malloc (sizeof (struct ReplyQueueEntry) + + sizeof (struct GNUNET_DNS_Register)); + msg = (struct GNUNET_DNS_Register*) &qe[1]; + qe->msg = &msg->header; + msg->header.size = htons (sizeof (struct GNUNET_DNS_Register)); + msg->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT); + msg->flags = htonl (dh->flags); + queue_reply (dh, qe); +} - GNUNET_free (query); - } - else + +/** + * Disconnect from the DNS service. + * + * @param dh handle with the connection to disconnect + */ +static void +disconnect (struct GNUNET_DNS_Handle *dh) +{ + struct ReplyQueueEntry *qe; + + if (NULL != dh->dns_transmit_handle) { - GNUNET_break (0); - len = 0; + GNUNET_CLIENT_notify_transmit_ready_cancel (dh->dns_transmit_handle); + dh->dns_transmit_handle = NULL; } - - /* - * Check whether more data is to be sent - */ - if (h->head != NULL) + if (NULL != dh->dns_connection) { - h->dns_transmit_handle = - GNUNET_CLIENT_notify_transmit_ready (h->dns_connection, - ntohs (h->head->pkt.hdr.size), - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_YES, &send_query, h); + GNUNET_CLIENT_disconnect (dh->dns_connection, GNUNET_NO); + dh->dns_connection = NULL; } - else if (h->restart_hijack == 1) + while (NULL != (qe = dh->rq_head)) { - h->dns_transmit_handle = - GNUNET_CLIENT_notify_transmit_ready (h->dns_connection, - sizeof (struct - GNUNET_MessageHeader), - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_YES, &send_query, h); + GNUNET_CONTAINER_DLL_remove (dh->rq_head, + dh->rq_tail, + qe); + GNUNET_free (qe); } - - return len; + dh->in_receive = GNUNET_NO; } -/* }}} */ - - /** - * This receives packets from the service-dns and schedules process_answer to - * handle it + * This receives packets from the DNS service and calls the application to + * handle it. + * + * @param cls the struct GNUNET_DNS_Handle + * @param msg message from the service (request) */ static void -dns_answer_handler (void *cls, - const struct GNUNET_MessageHeader *msg) +request_handler (void *cls, + const struct GNUNET_MessageHeader *msg) { - struct GNUNET_DNS_Handle *h = cls; + struct GNUNET_DNS_Handle *dh = cls; + const struct GNUNET_DNS_Request *req; + struct GNUNET_DNS_RequestHandle *rh; + size_t payload_length; /* the service disconnected, reconnect after short wait */ if (msg == NULL) { - if (h->dns_transmit_handle != NULL) - GNUNET_CLIENT_notify_transmit_ready_cancel (h->dns_transmit_handle); - h->dns_transmit_handle = NULL; - GNUNET_CLIENT_disconnect (h->dns_connection, GNUNET_NO); - h->dns_connection = NULL; -#if 0 - h->conn_task = + disconnect (dh); + dh->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, - &connect_to_service_dns, h); -#endif + &reconnect, dh); return; } - - /* the service did something strange, reconnect immediately */ - if (msg->type != htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS)) + if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST) || + (ntohs (msg->size) < sizeof (struct GNUNET_DNS_Request)) ) { + /* the service did something strange, reconnect immediately */ GNUNET_break (0); - GNUNET_CLIENT_disconnect (h->dns_connection, GNUNET_NO); - h->dns_connection = NULL; -#if 0 - conn_task = GNUNET_SCHEDULER_add_now (&connect_to_service_dns, NULL); -#endif + disconnect (dh); + dh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, dh); return; } - h->process_answer_cb (h->process_answer_cb_cls, - (const struct answer_packet*) msg); - GNUNET_CLIENT_receive (h->dns_connection, &dns_answer_handler, h, + req = (const struct GNUNET_DNS_Request *) msg; + GNUNET_break (ntohl (req->reserved) == 0); + payload_length = ntohs (req->header.size) - sizeof (struct GNUNET_DNS_Request); + GNUNET_CLIENT_receive (dh->dns_connection, + &request_handler, dh, GNUNET_TIME_UNIT_FOREVER_REL); + + /* finally, pass request to callback for answers */ + rh = GNUNET_malloc (sizeof (struct GNUNET_DNS_RequestHandle)); + rh->dh =dh; + rh->request_id = req->request_id; + rh->generation = dh->generation; + dh->pending_requests++; + dh->rh (dh->rh_cls, + rh, + payload_length, + (const char*) &req[1]); } /** - * Connect to the service-dns + * Callback called by notify_transmit_ready; sends DNS replies + * to the DNS service. + * + * @param cls the struct GNUNET_DNS_Handle + * @param size number of bytes available in buf + * @param buf where to copy the message for transmission + * @return number of bytes copied to buf */ -struct GNUNET_DNS_Handle * -GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, - GNUNET_DNS_ResponseCallback cb, - void *cb_cls) +static size_t +send_response (void *cls, size_t size, void *buf) { - struct GNUNET_DNS_Handle *h; - - h = GNUNET_malloc (sizeof (struct GNUNET_DNS_Handle)); - h->cfg = cfg; - h->process_answer_cb = cb; - h->process_answer_cb_cls = cb_cls; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n"); - h->dns_connection = GNUNET_CLIENT_connect ("dns", h->cfg); - /* This would most likely be a misconfiguration */ - GNUNET_assert (NULL != h->dns_connection); - GNUNET_CLIENT_receive (h->dns_connection, - &dns_answer_handler, NULL, - GNUNET_TIME_UNIT_FOREVER_REL); - /* If a packet is already in the list, schedule to send it */ - if (h->dns_transmit_handle == NULL && h->head != NULL) - h->dns_transmit_handle = - GNUNET_CLIENT_notify_transmit_ready (h->dns_connection, - ntohs (h->head->pkt.hdr.size), - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_YES, &send_query, h); - else if (h->dns_transmit_handle == NULL && h->restart_hijack == 1) + struct GNUNET_DNS_Handle *dh = cls; + struct ReplyQueueEntry *qe; + size_t len; + + dh->dns_transmit_handle = NULL; + if (NULL == buf) + { + disconnect (dh); + dh->reconnect_task = + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, + &reconnect, dh); + return 0; + } + qe = dh->rq_head; + if (NULL == qe) + return 0; + len = ntohs (qe->msg->size); + if (len > size) + { + dh->dns_transmit_handle = + GNUNET_CLIENT_notify_transmit_ready (dh->dns_connection, + len, + GNUNET_TIME_UNIT_FOREVER_REL, + GNUNET_NO, + &send_response, dh); + return 0; + } + memcpy (buf, qe->msg, len); + GNUNET_CONTAINER_DLL_remove (dh->rq_head, + dh->rq_tail, + qe); + GNUNET_free (qe); + if (GNUNET_NO == dh->in_receive) { - h->dns_transmit_handle = - GNUNET_CLIENT_notify_transmit_ready (h->dns_connection, - sizeof (struct - GNUNET_MessageHeader), - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_YES, &send_query, NULL); + dh->in_receive = GNUNET_YES; + GNUNET_CLIENT_receive (dh->dns_connection, + &request_handler, dh, + GNUNET_TIME_UNIT_FOREVER_REL); } - return h; + if (NULL != (qe = dh->rq_head)) + { + dh->dns_transmit_handle = + GNUNET_CLIENT_notify_transmit_ready (dh->dns_connection, + ntohs (qe->msg->size), + GNUNET_TIME_UNIT_FOREVER_REL, + GNUNET_NO, + &send_response, dh); + } + return len; +} + + +/** + * Add the given reply to our transmission queue and trigger sending if needed. + * + * @param dh handle with the connection + * @param qe reply to queue + */ +static void +queue_reply (struct GNUNET_DNS_Handle *dh, + struct ReplyQueueEntry *qe) +{ + if (NULL == dh->dns_connection) + { + GNUNET_free (qe); + return; + } + GNUNET_CONTAINER_DLL_insert_tail (dh->rq_head, + dh->rq_tail, + qe); + if (NULL != dh->dns_transmit_handle) + return; + /* trigger sending */ + dh->dns_transmit_handle = + GNUNET_CLIENT_notify_transmit_ready (dh->dns_connection, + ntohs (dh->rq_head->msg->size), + GNUNET_TIME_UNIT_FOREVER_REL, + GNUNET_NO, + &send_response, dh); } +/** + * If a GNUNET_DNS_RequestHandler calls this function, the request is + * given to other clients or the global DNS for resolution. Once a + * global response has been obtained, the request handler is AGAIN + * called to give it a chance to observe and modify the response after + * the "normal" resolution. It is not legal for the request handler + * to call this function if a response is already present. + * + * @param rh request that should now be forwarded + */ void -GNUNET_DNS_restart_hijack (struct GNUNET_DNS_Handle *h) +GNUNET_DNS_request_forward (struct GNUNET_DNS_RequestHandle *rh) { - h->restart_hijack = 1; - if (NULL != h->dns_connection && h->dns_transmit_handle == NULL) - h->dns_transmit_handle = - GNUNET_CLIENT_notify_transmit_ready (h->dns_connection, - sizeof (struct - GNUNET_MessageHeader), - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_YES, &send_query, h); + struct ReplyQueueEntry *qe; + struct GNUNET_DNS_Response *resp; + + GNUNET_assert (0 < rh->dh->pending_requests--); + if (rh->generation != rh->dh->generation) + { + GNUNET_free (rh); + return; + } + qe = GNUNET_malloc (sizeof (struct ReplyQueueEntry) + + sizeof (struct GNUNET_DNS_Response)); + resp = (struct GNUNET_DNS_Response*) &qe[1]; + qe->msg = &resp->header; + resp->header.size = htons (sizeof (struct GNUNET_DNS_Response)); + resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); + resp->drop_flag = htonl (1); + resp->request_id = rh->request_id; + queue_reply (rh->dh, qe); + GNUNET_free (rh); } /** - * FIXME: we should not expost our internal structures like this. - * Just a quick initial hack. + * If a GNUNET_DNS_RequestHandler calls this function, the request is + * to be dropped and no response should be generated. + * + * @param rh request that should now be dropped */ -static void -queue_request (struct GNUNET_DNS_Handle *h, - struct query_packet_list *q) +void +GNUNET_DNS_request_drop (struct GNUNET_DNS_RequestHandle *rh) { - GNUNET_CONTAINER_DLL_insert_tail (h->head, h->tail, q); - if (h->dns_connection != NULL && h->dns_transmit_handle == NULL) - h->dns_transmit_handle = - GNUNET_CLIENT_notify_transmit_ready (h->dns_connection, ntohs(q->pkt.hdr.size), - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_YES, &send_query, - h); -} + struct ReplyQueueEntry *qe; + struct GNUNET_DNS_Response *resp; + GNUNET_assert (0 < rh->dh->pending_requests--); + if (rh->generation != rh->dh->generation) + { + GNUNET_free (rh); + return; + } + qe = GNUNET_malloc (sizeof (struct ReplyQueueEntry) + + sizeof (struct GNUNET_DNS_Response)); + resp = (struct GNUNET_DNS_Response*) &qe[1]; + qe->msg = &resp->header; + resp->header.size = htons (sizeof (struct GNUNET_DNS_Response)); + resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); + resp->request_id = rh->request_id; + resp->drop_flag = htonl (0); + queue_reply (rh->dh, qe); + GNUNET_free (rh); +} /** - * Process a DNS request sent to an IPv4 resolver. Pass it - * to the DNS service for resolution. + * If a GNUNET_DNS_RequestHandler calls this function, the request is + * supposed to be answered with the data provided to this call (with + * the modifications the function might have made). * - * @param h DNS handle - * @param dst_ip destination IPv4 address - * @param src_ip source IPv4 address (usually local machine) - * @param src_port source port (to be used for reply) - * @param udp_packet_len length of the UDP payload in bytes - * @param udp_packet UDP payload + * @param rh request that should now be answered + * @param reply_length size of reply (uint16_t to force sane size) + * @param reply reply data */ void -GNUNET_DNS_queue_request_v4 (struct GNUNET_DNS_Handle *h, - const struct in_addr *dst_ip, - const struct in_addr *src_ip, - uint16_t src_port, - size_t udp_packet_len, - const char *udp_packet) +GNUNET_DNS_request_answer (struct GNUNET_DNS_RequestHandle *rh, + uint16_t reply_length, + const char *reply) { - size_t len = sizeof (struct query_packet) + udp_packet_len - 1; - struct query_packet_list *query = - GNUNET_malloc (len + sizeof (struct query_packet_list) - - sizeof (struct query_packet)); - query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS); - query->pkt.hdr.size = htons (len); - memcpy (query->pkt.orig_to, dst_ip, 4); - memcpy (query->pkt.orig_from, src_ip, 4); - query->pkt.addrlen = 4; - query->pkt.src_port = htons (src_port); - memcpy (query->pkt.data, udp_packet, udp_packet_len); - queue_request (h, query); + struct ReplyQueueEntry *qe; + struct GNUNET_DNS_Response *resp; + + GNUNET_assert (0 < rh->dh->pending_requests--); + if (rh->generation != rh->dh->generation) + { + GNUNET_free (rh); + return; + } + if (reply_length + sizeof (struct GNUNET_DNS_Response) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) + { + GNUNET_break (0); + GNUNET_free (rh); + return; + } + qe = GNUNET_malloc (sizeof (struct ReplyQueueEntry) + + sizeof (struct GNUNET_DNS_Response) + reply_length); + resp = (struct GNUNET_DNS_Response*) &qe[1]; + qe->msg = &resp->header; + resp->header.size = htons (sizeof (struct GNUNET_DNS_Response) + reply_length); + resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); + resp->drop_flag = htonl (2); + resp->request_id = rh->request_id; + memcpy (&resp[1], reply, reply_length); + queue_reply (rh->dh, qe); + GNUNET_free (rh); } /** - * Process a DNS request sent to an IPv6 resolver. Pass it - * to the DNS service for resolution. + * Connect to the service-dns * - * @param h DNS handle - * @param dst_ip destination IPv6 address - * @param src_ip source IPv6 address (usually local machine) - * @param src_port source port (to be used for reply) - * @param udp_packet_len length of the UDP payload in bytes - * @param udp_packet UDP payload + * @param cfg configuration to use + * @param flags when to call rh + * @param rh function to call with DNS requests + * @param rh_cls closure to pass to rh + * @return DNS handle */ -void -GNUNET_DNS_queue_request_v6 (struct GNUNET_DNS_Handle *h, - const struct in6_addr *dst_ip, - const struct in6_addr *src_ip, - uint16_t src_port, - size_t udp_packet_len, - const char *udp_packet) +struct GNUNET_DNS_Handle * +GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, + enum GNUNET_DNS_Flags flags, + GNUNET_DNS_RequestHandler rh, + void *rh_cls) { - size_t len = - sizeof (struct query_packet) + udp_packet_len - 1; - struct query_packet_list *query = - GNUNET_malloc (len + sizeof (struct query_packet_list) - - sizeof (struct answer_packet)); - query->pkt.hdr.type = - htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS); - query->pkt.hdr.size = htons (len); - memcpy (query->pkt.orig_to, dst_ip, 16); - memcpy (query->pkt.orig_from, src_ip, 16); - query->pkt.addrlen = 16; - query->pkt.src_port = htons (src_port); - memcpy (query->pkt.data, udp_packet, - udp_packet_len); - queue_request (h, query); + struct GNUNET_DNS_Handle *dh; + + dh = GNUNET_malloc (sizeof (struct GNUNET_DNS_Handle)); + dh->cfg = cfg; + dh->flags = flags; + dh->rh = rh; + dh->rh_cls = rh_cls; + dh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, dh); + return dh; } +/** + * Disconnect from the DNS service. + * + * @param dh DNS handle + */ void -GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *h) +GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *dh) { - if (h->dns_connection != NULL) + if (GNUNET_SCHEDULER_NO_TASK != dh->reconnect_task) { - GNUNET_CLIENT_disconnect (h->dns_connection, GNUNET_NO); - h->dns_connection = NULL; + GNUNET_SCHEDULER_cancel (dh->reconnect_task); + dh->reconnect_task = GNUNET_SCHEDULER_NO_TASK; } - GNUNET_free (h); + disconnect (dh); + /* make sure client has no pending requests left over! */ + GNUNET_assert (0 == dh->pending_requests); + GNUNET_free (dh); } -/* end of dns_api.c */ +/* end of dns_api_new.c */ diff --git a/src/dns/dns_api_new.c b/src/dns/dns_api_new.c deleted file mode 100644 index 9e479fce7..000000000 --- a/src/dns/dns_api_new.c +++ /dev/null @@ -1,522 +0,0 @@ -/* - This file is part of GNUnet - (C) 2012 Christian Grothoff (and other contributing authors) - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 2, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - */ - -/** - * @file dns/dns_api_new.c - * @brief API to access the DNS service. - * @author Christian Grothoff - */ -#include "platform.h" -#include "gnunet_dns_service-new.h" -#include "dns_new.h" - - -/** - * Reply to send to service. - */ -struct ReplyQueueEntry -{ - /** - * Kept in DLL. - */ - struct ReplyQueueEntry *next; - - /** - * Kept in DLL. - */ - struct ReplyQueueEntry *prev; - - /** - * Message to transmit, allocated at the end of this struct. - */ - const struct GNUNET_MessageHeader *msg; - -}; - - -/** - * Handle to identify an individual DNS request. - */ -struct GNUNET_DNS_RequestHandle -{ - - /** - * Handle to DNS API. - */ - struct GNUNET_DNS_Handle *dh; - - /** - * Stored in network byte order (as for us, it is just a random number). - */ - uint64_t request_id; - - /** - * Re-connect counter, to make sure we did not reconnect in the meantime. - */ - uint32_t generation; - -}; - - -/** - * DNS handle - */ -struct GNUNET_DNS_Handle -{ - - /** - * Connection to DNS service, or NULL. - */ - struct GNUNET_CLIENT_Connection *dns_connection; - - /** - * Handle to active transmission request, or NULL. - */ - struct GNUNET_CLIENT_TransmitHandle *dns_transmit_handle; - - /** - * Configuration to use. - */ - const struct GNUNET_CONFIGURATION_Handle *cfg; - - /** - * Function to call to get replies. - */ - GNUNET_DNS_RequestHandler rh; - - /** - * Closure for 'rh'. - */ - void *rh_cls; - - /** - * Head of replies to transmit. - */ - struct ReplyQueueEntry *rq_head; - - /** - * Tail of replies to transmit. - */ - struct ReplyQueueEntry *rq_tail; - - /** - * Task to reconnect to the service. - */ - GNUNET_SCHEDULER_TaskIdentifier reconnect_task; - - /** - * Re-connect counter, to make sure we did not reconnect in the meantime. - */ - uint32_t generation; - - /** - * Flags for events we care about. - */ - enum GNUNET_DNS_Flags flags; - - /** - * Did we start the receive loop yet? - */ - int in_receive; - - /** - * Number of GNUNET_DNS_RequestHandles we have outstanding. Must be 0 before - * we can be disconnected. - */ - unsigned int pending_requests; -}; - - -/** - * Add the given reply to our transmission queue and trigger sending if needed. - * - * @param dh handle with the connection - * @param qe reply to queue - */ -static void -queue_reply (struct GNUNET_DNS_Handle *dh, - struct ReplyQueueEntry *qe); - - -/** - * Reconnect to the DNS service. - * - * @param cls handle with the connection to connect - * @param tc scheduler context (unused) - */ -static void -reconnect (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - struct GNUNET_DNS_Handle *dh = cls; - struct ReplyQueueEntry *qe; - struct GNUNET_DNS_Register *msg; - - dh->reconnect_task = GNUNET_SCHEDULER_NO_TASK; - dh->dns_connection = GNUNET_CLIENT_connect ("dns", dh->cfg); - if (NULL == dh->dns_connection) - return; - dh->generation++; - qe = GNUNET_malloc (sizeof (struct ReplyQueueEntry) + - sizeof (struct GNUNET_DNS_Register)); - msg = (struct GNUNET_DNS_Register*) &qe[1]; - qe->msg = &msg->header; - msg->header.size = htons (sizeof (struct GNUNET_DNS_Register)); - msg->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT); - msg->flags = htonl (dh->flags); - queue_reply (dh, qe); -} - - -/** - * Disconnect from the DNS service. - * - * @param dh handle with the connection to disconnect - */ -static void -disconnect (struct GNUNET_DNS_Handle *dh) -{ - struct ReplyQueueEntry *qe; - - if (NULL != dh->dns_transmit_handle) - { - GNUNET_CLIENT_notify_transmit_ready_cancel (dh->dns_transmit_handle); - dh->dns_transmit_handle = NULL; - } - if (NULL != dh->dns_connection) - { - GNUNET_CLIENT_disconnect (dh->dns_connection, GNUNET_NO); - dh->dns_connection = NULL; - } - while (NULL != (qe = dh->rq_head)) - { - GNUNET_CONTAINER_DLL_remove (dh->rq_head, - dh->rq_tail, - qe); - GNUNET_free (qe); - } - dh->in_receive = GNUNET_NO; -} - - -/** - * This receives packets from the DNS service and calls the application to - * handle it. - * - * @param cls the struct GNUNET_DNS_Handle - * @param msg message from the service (request) - */ -static void -request_handler (void *cls, - const struct GNUNET_MessageHeader *msg) -{ - struct GNUNET_DNS_Handle *dh = cls; - const struct GNUNET_DNS_Request *req; - struct GNUNET_DNS_RequestHandle *rh; - size_t payload_length; - - /* the service disconnected, reconnect after short wait */ - if (msg == NULL) - { - disconnect (dh); - dh->reconnect_task = - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, - &reconnect, dh); - return; - } - if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST) || - (ntohs (msg->size) < sizeof (struct GNUNET_DNS_Request)) ) - { - /* the service did something strange, reconnect immediately */ - GNUNET_break (0); - disconnect (dh); - dh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, dh); - return; - } - req = (const struct GNUNET_DNS_Request *) msg; - GNUNET_break (ntohl (req->reserved) == 0); - payload_length = ntohs (req->header.size) - sizeof (struct GNUNET_DNS_Request); - GNUNET_CLIENT_receive (dh->dns_connection, - &request_handler, dh, - GNUNET_TIME_UNIT_FOREVER_REL); - - /* finally, pass request to callback for answers */ - rh = GNUNET_malloc (sizeof (struct GNUNET_DNS_RequestHandle)); - rh->dh =dh; - rh->request_id = req->request_id; - rh->generation = dh->generation; - dh->pending_requests++; - dh->rh (dh->rh_cls, - rh, - payload_length, - (const char*) &req[1]); -} - - -/** - * Callback called by notify_transmit_ready; sends DNS replies - * to the DNS service. - * - * @param cls the struct GNUNET_DNS_Handle - * @param size number of bytes available in buf - * @param buf where to copy the message for transmission - * @return number of bytes copied to buf - */ -static size_t -send_response (void *cls, size_t size, void *buf) -{ - struct GNUNET_DNS_Handle *dh = cls; - struct ReplyQueueEntry *qe; - size_t len; - - dh->dns_transmit_handle = NULL; - if (NULL == buf) - { - disconnect (dh); - dh->reconnect_task = - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, - &reconnect, dh); - return 0; - } - qe = dh->rq_head; - if (NULL == qe) - return 0; - len = ntohs (qe->msg->size); - if (len > size) - { - dh->dns_transmit_handle = - GNUNET_CLIENT_notify_transmit_ready (dh->dns_connection, - len, - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_NO, - &send_response, dh); - return 0; - } - memcpy (buf, qe->msg, len); - GNUNET_CONTAINER_DLL_remove (dh->rq_head, - dh->rq_tail, - qe); - GNUNET_free (qe); - if (GNUNET_NO == dh->in_receive) - { - dh->in_receive = GNUNET_YES; - GNUNET_CLIENT_receive (dh->dns_connection, - &request_handler, dh, - GNUNET_TIME_UNIT_FOREVER_REL); - } - if (NULL != (qe = dh->rq_head)) - { - dh->dns_transmit_handle = - GNUNET_CLIENT_notify_transmit_ready (dh->dns_connection, - ntohs (qe->msg->size), - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_NO, - &send_response, dh); - } - return len; -} - - -/** - * Add the given reply to our transmission queue and trigger sending if needed. - * - * @param dh handle with the connection - * @param qe reply to queue - */ -static void -queue_reply (struct GNUNET_DNS_Handle *dh, - struct ReplyQueueEntry *qe) -{ - if (NULL == dh->dns_connection) - { - GNUNET_free (qe); - return; - } - GNUNET_CONTAINER_DLL_insert_tail (dh->rq_head, - dh->rq_tail, - qe); - if (NULL != dh->dns_transmit_handle) - return; - /* trigger sending */ - dh->dns_transmit_handle = - GNUNET_CLIENT_notify_transmit_ready (dh->dns_connection, - ntohs (dh->rq_head->msg->size), - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_NO, - &send_response, dh); -} - - -/** - * If a GNUNET_DNS_RequestHandler calls this function, the request is - * given to other clients or the global DNS for resolution. Once a - * global response has been obtained, the request handler is AGAIN - * called to give it a chance to observe and modify the response after - * the "normal" resolution. It is not legal for the request handler - * to call this function if a response is already present. - * - * @param rh request that should now be forwarded - */ -void -GNUNET_DNS_request_forward (struct GNUNET_DNS_RequestHandle *rh) -{ - struct ReplyQueueEntry *qe; - struct GNUNET_DNS_Response *resp; - - GNUNET_assert (0 < rh->dh->pending_requests--); - if (rh->generation != rh->dh->generation) - { - GNUNET_free (rh); - return; - } - qe = GNUNET_malloc (sizeof (struct ReplyQueueEntry) + - sizeof (struct GNUNET_DNS_Response)); - resp = (struct GNUNET_DNS_Response*) &qe[1]; - qe->msg = &resp->header; - resp->header.size = htons (sizeof (struct GNUNET_DNS_Response)); - resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); - resp->drop_flag = htonl (1); - resp->request_id = rh->request_id; - queue_reply (rh->dh, qe); - GNUNET_free (rh); -} - - -/** - * If a GNUNET_DNS_RequestHandler calls this function, the request is - * to be dropped and no response should be generated. - * - * @param rh request that should now be dropped - */ -void -GNUNET_DNS_request_drop (struct GNUNET_DNS_RequestHandle *rh) -{ - struct ReplyQueueEntry *qe; - struct GNUNET_DNS_Response *resp; - - GNUNET_assert (0 < rh->dh->pending_requests--); - if (rh->generation != rh->dh->generation) - { - GNUNET_free (rh); - return; - } - qe = GNUNET_malloc (sizeof (struct ReplyQueueEntry) + - sizeof (struct GNUNET_DNS_Response)); - resp = (struct GNUNET_DNS_Response*) &qe[1]; - qe->msg = &resp->header; - resp->header.size = htons (sizeof (struct GNUNET_DNS_Response)); - resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); - resp->request_id = rh->request_id; - resp->drop_flag = htonl (0); - queue_reply (rh->dh, qe); - GNUNET_free (rh); -} - - -/** - * If a GNUNET_DNS_RequestHandler calls this function, the request is - * supposed to be answered with the data provided to this call (with - * the modifications the function might have made). - * - * @param rh request that should now be answered - * @param reply_length size of reply (uint16_t to force sane size) - * @param reply reply data - */ -void -GNUNET_DNS_request_answer (struct GNUNET_DNS_RequestHandle *rh, - uint16_t reply_length, - const char *reply) -{ - struct ReplyQueueEntry *qe; - struct GNUNET_DNS_Response *resp; - - GNUNET_assert (0 < rh->dh->pending_requests--); - if (rh->generation != rh->dh->generation) - { - GNUNET_free (rh); - return; - } - if (reply_length + sizeof (struct GNUNET_DNS_Response) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) - { - GNUNET_break (0); - GNUNET_free (rh); - return; - } - qe = GNUNET_malloc (sizeof (struct ReplyQueueEntry) + - sizeof (struct GNUNET_DNS_Response) + reply_length); - resp = (struct GNUNET_DNS_Response*) &qe[1]; - qe->msg = &resp->header; - resp->header.size = htons (sizeof (struct GNUNET_DNS_Response) + reply_length); - resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); - resp->drop_flag = htonl (2); - resp->request_id = rh->request_id; - memcpy (&resp[1], reply, reply_length); - queue_reply (rh->dh, qe); - GNUNET_free (rh); -} - - -/** - * Connect to the service-dns - * - * @param cfg configuration to use - * @param flags when to call rh - * @param rh function to call with DNS requests - * @param rh_cls closure to pass to rh - * @return DNS handle - */ -struct GNUNET_DNS_Handle * -GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, - enum GNUNET_DNS_Flags flags, - GNUNET_DNS_RequestHandler rh, - void *rh_cls) -{ - struct GNUNET_DNS_Handle *dh; - - dh = GNUNET_malloc (sizeof (struct GNUNET_DNS_Handle)); - dh->cfg = cfg; - dh->flags = flags; - dh->rh = rh; - dh->rh_cls = rh_cls; - dh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, dh); - return dh; -} - - -/** - * Disconnect from the DNS service. - * - * @param dh DNS handle - */ -void -GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *dh) -{ - if (GNUNET_SCHEDULER_NO_TASK != dh->reconnect_task) - { - GNUNET_SCHEDULER_cancel (dh->reconnect_task); - dh->reconnect_task = GNUNET_SCHEDULER_NO_TASK; - } - disconnect (dh); - /* make sure client has no pending requests left over! */ - GNUNET_assert (0 == dh->pending_requests); - GNUNET_free (dh); -} - -/* end of dns_api_new.c */ diff --git a/src/dns/dns_new.h b/src/dns/dns_new.h deleted file mode 100644 index 29a9f937b..000000000 --- a/src/dns/dns_new.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - This file is part of GNUnet - (C) 2012 Christian Grothoff (and other contributing authors) - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 2, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - */ - -/** - * @file dns/dns_new.h - * @brief IPC messages between DNS API and DNS service - * @author Christian Grothoff - */ -#ifndef DNS_NEW_H -#define DNS_NEW_H - -GNUNET_NETWORK_STRUCT_BEGIN - - -/** - * Message from client to DNS service to register itself. - */ -struct GNUNET_DNS_Register -{ - /** - * Header of type GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT - */ - struct GNUNET_MessageHeader header; - - /** - * NBO encoding of 'enum GNUNET_DNS_Flags' for the client. - */ - uint32_t flags; -}; - - -/** - * Message from DNS service to client: please handle a request. - */ -struct GNUNET_DNS_Request -{ - /** - * Header of type GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST - */ - struct GNUNET_MessageHeader header; - - /** - * Always zero. - */ - uint32_t reserved GNUNET_PACKED; - - /** - * Unique request ID. - */ - uint64_t request_id GNUNET_PACKED; - - /* followed by original DNS request (without UDP header) */ - -}; - - -/** - * Message from client to DNS service: here is my reply. - */ -struct GNUNET_DNS_Response -{ - /** - * Header of type GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE - */ - struct GNUNET_MessageHeader header; - - /** - * Zero to drop, 1 for no change (no payload), 2 for update (message has payload). - */ - uint32_t drop_flag GNUNET_PACKED; - - /** - * Unique request ID. - */ - uint64_t request_id GNUNET_PACKED; - - /* followed by original DNS request (without UDP header) */ - -}; - - -GNUNET_NETWORK_STRUCT_END - -#endif diff --git a/src/dns/dnsparser.c b/src/dns/dnsparser.c index 2e9e73f1c..12253a2e7 100644 --- a/src/dns/dnsparser.c +++ b/src/dns/dnsparser.c @@ -793,331 +793,4 @@ GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p, return GNUNET_OK; } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/* legacy code follows */ - -/** - * Parse a name from DNS to a normal .-delimited, 0-terminated string. - * - * @param d The destination of the name. Should have at least 255 bytes allocated. - * @param src The DNS-Packet - * @param idx The offset inside the Packet from which on the name should be read - * @returns The offset of the first unparsed byte (the byte right behind the name) - */ -static unsigned int -parse_dns_name (char *d, const unsigned char *src, unsigned short idx) -{ /*{{{ */ - char *dest = d; - - int len = src[idx++]; - - while (len != 0) - { - if (len & 0xC0) - { /* Compressed name, offset in this and the next octet */ - unsigned short offset = ((len & 0x3F) << 8) | src[idx++]; - - parse_dns_name (dest, src, offset - 12); /* 12 for the Header of the DNS-Packet, idx starts at 0 which is 12 bytes from the start of the packet */ - return idx; - } - memcpy (dest, src + idx, len); - idx += len; - dest += len; - *dest = '.'; - dest++; - len = src[idx++]; - }; - *dest = 0; - - return idx; -} - -/*}}}*/ - -/** - * Parse a complete DNS-Record from raw DNS-data to a struct dns_record - * - * @param data The DNS-data - * @param dst Pointer to count pointers; individual pointers will be allocated - * @param count Number of records to parse - * @param idx The offset inside the Packet from which on the name should be read - * @returns The offset of the first unparsed byte (the byte right behind the last record) - */ -static unsigned short -parse_dns_record (unsigned char *data, /*{{{ */ - struct dns_record **dst, unsigned short count, - unsigned short idx) -{ - int i; - unsigned short _idx; - - for (i = 0; i < count; i++) - { - dst[i] = GNUNET_malloc (sizeof (struct dns_record)); - dst[i]->name = alloca (255); // see RFC1035, no name can be longer than this. - char *name = dst[i]->name; - - _idx = parse_dns_name (name, data, idx); - dst[i]->namelen = _idx - idx; - - dst[i]->name = GNUNET_malloc (dst[i]->namelen); - memcpy (dst[i]->name, name, dst[i]->namelen); - - idx = _idx; - - dst[i]->type = *((unsigned short *) (data + idx)); - idx += 2; - dst[i]->class = *((unsigned short *) (data + idx)); - idx += 2; - dst[i]->ttl = *((unsigned int *) (data + idx)); - idx += 4; - dst[i]->data_len = *((unsigned short *) (data + idx)); - idx += 2; - dst[i]->data = GNUNET_malloc (ntohs (dst[i]->data_len)); - memcpy (dst[i]->data, data + idx, ntohs (dst[i]->data_len)); - idx += ntohs (dst[i]->data_len); - } - return idx; -} /*}}} */ - -/** - * Parse a raw DNS-Packet into an usable struct - */ -struct dns_pkt_parsed * -parse_dns_packet (struct dns_pkt *pkt) -{ /*{{{ */ - struct dns_pkt_parsed *ppkt = GNUNET_malloc (sizeof (struct dns_pkt_parsed)); - - memcpy (&ppkt->s, &pkt->s, sizeof pkt->s); - - unsigned short qdcount = ntohs (ppkt->s.qdcount); - unsigned short ancount = ntohs (ppkt->s.ancount); - unsigned short nscount = ntohs (ppkt->s.nscount); - unsigned short arcount = ntohs (ppkt->s.arcount); - - ppkt->queries = GNUNET_malloc (qdcount * sizeof (struct dns_query *)); - ppkt->answers = GNUNET_malloc (ancount * sizeof (struct dns_record *)); - ppkt->nameservers = GNUNET_malloc (nscount * sizeof (struct dns_record *)); - ppkt->additional = GNUNET_malloc (arcount * sizeof (struct dns_record *)); - - unsigned short idx = 0, _idx; /* This keeps track how far we have parsed the data */ - - /* Parse the Query */ - int i; - - for (i = 0; i < qdcount; i++) - { /*{{{ */ - ppkt->queries[i] = GNUNET_malloc (sizeof (struct dns_query)); - char *name = alloca (255); /* see RFC1035, it can't be more than this. */ - - _idx = parse_dns_name (name, pkt->data, idx); - ppkt->queries[i]->namelen = _idx - idx; - idx = _idx; - - ppkt->queries[i]->name = GNUNET_malloc (ppkt->queries[i]->namelen); - memcpy (ppkt->queries[i]->name, name, ppkt->queries[i]->namelen); - - ppkt->queries[i]->qtype = *((unsigned short *) (pkt->data + idx)); - idx += 2; - ppkt->queries[i]->qclass = *((unsigned short *) (pkt->data + idx)); - idx += 2; - } - /*}}} */ - idx = parse_dns_record (pkt->data, ppkt->answers, ancount, idx); - idx = parse_dns_record (pkt->data, ppkt->nameservers, nscount, idx); - idx = parse_dns_record (pkt->data, ppkt->additional, arcount, idx); - return ppkt; -} /*}}} */ - -static void -unparse_dns_name (char *dest, char *src, size_t len) -{ - char *b = dest; - char cnt = 0; - - dest++; - while (*src != 0) - { - while (*src != '.' && *src != 0) - { - *dest = *src; - src++; - dest++; - cnt++; - } - *b = cnt; - cnt = 0; - b = dest; - dest++; - src++; - } - *b = 0; -} - -struct dns_pkt * -unparse_dns_packet (struct dns_pkt_parsed *ppkt) -{ - size_t size = sizeof (struct dns_pkt) - 1; - int i; - - for (i = 0; i < ntohs (ppkt->s.qdcount); i++) - size += ppkt->queries[i]->namelen + 1; - - for (i = 0; i < ntohs (ppkt->s.ancount); i++) - { - size += ppkt->answers[i]->namelen + 1; - size += ppkt->answers[i]->data_len; - } - for (i = 0; i < ntohs (ppkt->s.nscount); i++) - { - size += ppkt->nameservers[i]->namelen + 1; - size += ppkt->nameservers[i]->data_len; - } - for (i = 0; i < ntohs (ppkt->s.arcount); i++) - { - size += ppkt->additional[i]->namelen + 1; - size += ppkt->additional[i]->data_len; - } - - size += - 4 * ntohs (ppkt->s.qdcount) + 10 * (ntohs (ppkt->s.ancount) + - ntohs (ppkt->s.arcount) + - ntohs (ppkt->s.nscount)); - - struct dns_pkt *pkt = GNUNET_malloc (size); - char *pkt_c = (char *) pkt; - - memcpy (&pkt->s, &ppkt->s, sizeof ppkt->s); - size_t idx = sizeof ppkt->s; - - for (i = 0; i < ntohs (ppkt->s.qdcount); i++) - { - unparse_dns_name (&pkt_c[idx], ppkt->queries[i]->name, - ppkt->queries[i]->namelen); - idx += ppkt->queries[i]->namelen; - struct dns_query_line *d = (struct dns_query_line *) &pkt_c[idx]; - - d->class = ppkt->queries[i]->qclass; - d->type = ppkt->queries[i]->qtype; - idx += sizeof (struct dns_query_line); - } - - for (i = 0; i < ntohs (ppkt->s.ancount); i++) - { - unparse_dns_name (&pkt_c[idx], ppkt->answers[i]->name, - ppkt->answers[i]->namelen); - idx += ppkt->answers[i]->namelen; - struct dns_record_line *r = (struct dns_record_line *) &pkt_c[idx]; - - r->type = ppkt->answers[i]->type; - r->class = ppkt->answers[i]->class; - r->ttl = ppkt->answers[i]->ttl; - r->data_len = ppkt->answers[i]->data_len; - idx += sizeof (struct dns_record_line); - memcpy (&r->data, ppkt->answers[i]->data, ppkt->answers[i]->data_len); - idx += ppkt->answers[i]->data_len; - } - - for (i = 0; i < ntohs (ppkt->s.nscount); i++) - { - unparse_dns_name (&pkt_c[idx], ppkt->nameservers[i]->name, - ppkt->nameservers[i]->namelen); - idx += ppkt->nameservers[i]->namelen; - struct dns_record_line *r = (struct dns_record_line *) &pkt_c[idx]; - - r->type = ppkt->nameservers[i]->type; - r->class = ppkt->nameservers[i]->class; - r->ttl = ppkt->nameservers[i]->ttl; - r->data_len = ppkt->nameservers[i]->data_len; - idx += sizeof (struct dns_record_line); - memcpy (&r->data, ppkt->nameservers[i]->data, - ppkt->nameservers[i]->data_len); - idx += ppkt->nameservers[i]->data_len; - } - - for (i = 0; i < ntohs (ppkt->s.arcount); i++) - { - unparse_dns_name (&pkt_c[idx], ppkt->additional[i]->name, - ppkt->additional[i]->namelen); - idx += ppkt->additional[i]->namelen; - struct dns_record_line *r = (struct dns_record_line *) &pkt_c[idx]; - - r->type = ppkt->additional[i]->type; - r->class = ppkt->additional[i]->class; - r->ttl = ppkt->additional[i]->ttl; - r->data_len = ppkt->additional[i]->data_len; - idx += sizeof (struct dns_record_line); - memcpy (&r->data, ppkt->additional[i]->data, ppkt->additional[i]->data_len); - idx += ppkt->additional[i]->data_len; - } - - return pkt; -} - -void -free_parsed_dns_packet (struct dns_pkt_parsed *ppkt) -{ - unsigned short qdcount = ntohs (ppkt->s.qdcount); - unsigned short ancount = ntohs (ppkt->s.ancount); - unsigned short nscount = ntohs (ppkt->s.nscount); - unsigned short arcount = ntohs (ppkt->s.arcount); - - int i; - - for (i = 0; i < qdcount; i++) - { - GNUNET_free (ppkt->queries[i]->name); - GNUNET_free (ppkt->queries[i]); - } - GNUNET_free (ppkt->queries); - for (i = 0; i < ancount; i++) - { - GNUNET_free (ppkt->answers[i]->name); - GNUNET_free (ppkt->answers[i]->data); - GNUNET_free (ppkt->answers[i]); - } - GNUNET_free (ppkt->answers); - for (i = 0; i < nscount; i++) - { - GNUNET_free (ppkt->nameservers[i]->name); - GNUNET_free (ppkt->nameservers[i]->data); - GNUNET_free (ppkt->nameservers[i]); - } - GNUNET_free (ppkt->nameservers); - for (i = 0; i < arcount; i++) - { - GNUNET_free (ppkt->additional[i]->name); - GNUNET_free (ppkt->additional[i]->data); - GNUNET_free (ppkt->additional[i]); - } - GNUNET_free (ppkt->additional); - GNUNET_free (ppkt); -} +/* end of dnsparser.c */ diff --git a/src/dns/gnunet-dns-monitor.c b/src/dns/gnunet-dns-monitor.c index 9e86f9c2d..3c7f31985 100644 --- a/src/dns/gnunet-dns-monitor.c +++ b/src/dns/gnunet-dns-monitor.c @@ -26,7 +26,7 @@ #include "platform.h" #include "gnunet_util_lib.h" -#include "gnunet_dns_service-new.h" +#include "gnunet_dns_service.h" #include "gnunet_dnsparser_lib.h" /** diff --git a/src/dns/gnunet-dns-redirector.c b/src/dns/gnunet-dns-redirector.c index 4eabc8c79..41667ee9f 100644 --- a/src/dns/gnunet-dns-redirector.c +++ b/src/dns/gnunet-dns-redirector.c @@ -26,7 +26,7 @@ #include "platform.h" #include "gnunet_util_lib.h" -#include "gnunet_dns_service-new.h" +#include "gnunet_dns_service.h" #include "gnunet_dnsparser_lib.h" /** diff --git a/src/dns/gnunet-helper-hijack-dns.c b/src/dns/gnunet-helper-hijack-dns.c deleted file mode 100644 index 833bda8ad..000000000 --- a/src/dns/gnunet-helper-hijack-dns.c +++ /dev/null @@ -1,328 +0,0 @@ -/* - This file is part of GNUnet. - (C) 2010, 2011, 2012 Christian Grothoff - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - */ - -/** - * @file dns/gnunet-helper-hijack-dns.c - * @brief helper to install firewall rules to hijack all DNS traffic - * and send it to our virtual interface except for DNS traffic - * that originates on the specified port. - * @author Philipp Tölke - * @author Christian Grothoff - * - * This program alters the Linux firewall rules so that DNS traffic - * that ordinarily exits the system can be intercepted and managed by - * a virtual interface. In order to achieve this, DNS traffic is - * marked with the DNS_MARK given in below and re-routed to a custom - * table with the DNS_TABLE ID given below. Systems and - * administrators must take care to not cause conflicts with these - * values (it was deemed safest to hardcode them as passing these - * values as arguments might permit messing with arbitrary firewall - * rules, which would be dangerous). - * - * Note that having this binary SUID is only partially safe: it will - * allow redirecting (and intercepting / mangling) of all DNS traffic - * originating from this system by any user who can create a virtual - * interface (and this is again enabled by other GNUnet SUID - * binaries). Furthermore, even without the ability to create a - * tunnel interface, this code will make it possible to DoS all DNS - * traffic originating from the current system, simply by sending it - * to nowhere. - * - * Naturally, neither of these problems can be helped as this is the - * fundamental purpose of the binary. Certifying that this code is - * "safe" thus only means that it doesn't allow anything else (such - * as local priv. escalation, etc.). - * - * The following list of people have reviewed this code and considered - * it safe (within specifications) since the last modification (if you - * reviewed it, please have your name added to the list): - * - * - Christian Grothoff - */ -#include "platform.h" - -/** - * Name and full path of IPTABLES binary. - */ -#define SBIN_IPTABLES "/sbin/iptables" - -/** - * Name and full path of IPTABLES binary. - */ -#define SBIN_IP "/sbin/ip" - -/** - * Port for DNS traffic. - */ -#define DNS_PORT "53" - -/** - * Marker we set for our hijacked DNS traffic. We use GNUnet's - * port (2086) plus the DNS port (53) in HEX to make a 32-bit mark - * (which is hopefully long enough to not collide); so - * 0x08260035 = 136708149 (hopefully unique enough...). - */ -#define DNS_MARK "136708149" - -/** - * Table we use for our DNS rules. 0-255 is the range and - * 0, 253, 254 and 255 are already reserved. As this is about - * DNS and as "53" is likely (fingers crossed!) high enough to - * not usually conflict with a normal user's setup, we use 53 - * to give a hint that this has something to do with DNS. - */ -#define DNS_TABLE "53" - - -/** - * Run the given command and wait for it to complete. - * - * @param file name of the binary to run - * @param cmd command line arguments (as given to 'execv') - * @return 0 on success, 1 on any error - */ -static int -fork_and_exec (const char *file, - char *const cmd[]) -{ - int status; - pid_t pid; - pid_t ret; - - pid = fork (); - if (-1 == pid) - { - fprintf (stderr, - "fork failed: %s\n", - strerror (errno)); - return 1; - } - if (0 == pid) - { - /* we are the child process */ - (void) execv (file, cmd); - /* can only get here on error */ - fprintf (stderr, - "exec `%s' failed: %s\n", - file, - strerror (errno)); - _exit (1); - } - /* keep running waitpid as long as the only error we get is 'EINTR' */ - while ( (-1 == (ret = waitpid (pid, &status, 0))) && - (errno == EINTR) ); - if (-1 == ret) - { - fprintf (stderr, - "waitpid failed: %s\n", - strerror (errno)); - return 1; - } - if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status)))) - return 1; - /* child process completed and returned success, we're happy */ - return 0; -} - - -/** - * Main function of "gnunet-helper-hijack-dns". - * Use "-d" as the first argument to remove the firewall rules. - * The other arguments are the DNS source port to NOT affect - * by the rules, followed by the name of the virtual interface - * to redirect all of the remaining DNS traffic to. - * - * @param argc number of arguments - * @param argv ["-d"] PORT VTUN - * @return 0 on success, otherwise code indicating type of error: - * 1 wrong number of arguments - * 2 invalid port number - * 3 iptables not executable - * 4 ip not executable - * 8 failed to change routing table, cleanup successfull - * 16-31 failed to undo some changes to routing table - * 31-47 failed to fully change routing table and then might have failed to undo everything - */ -int -main (int argc, char *const*argv) -{ - int delete; - unsigned int port; - char *virt_dns; - char localport[6]; - int r; - - /* check command-line arguments */ - if (argc < 3) - { - fprintf (stderr, - "Syntax: gnunet-helper-hijack-dns [-d] PORT INTERFACENAME\n"); - return 1; - } - if (0 == strcmp (argv[1], "-d")) - delete = 1; - else - delete = 0; - if (argc != 3 + delete) - { - fprintf (stderr, - "Syntax: gnunet-helper-hijack-dns [-d] PORT INTERFACENAME\n"); - return 1; - } - port = atoi (argv[1 + delete]); - virt_dns = argv[2 + delete]; - if ( (port == 0) || (port >= 65536) ) - { - fprintf (stderr, - "Port `%u' is invalid\n", - port); - return 2; - } - /* verify that the binaries were care about are executable */ - if (0 != access (SBIN_IPTABLES, X_OK)) - { - fprintf (stderr, - "`%s' is not executable: %s\n", - SBIN_IPTABLES, - strerror (errno)); - return 3; - } - if (0 != access (SBIN_IP, X_OK)) - { - fprintf (stderr, - "`%s' is not executable: %s\n", - SBIN_IP, - strerror (errno)); - return 4; - } - - /* print port number to string for command-line use*/ - (void) snprintf (localport, - sizeof (localport), - "%u", - port); - - /* update routing tables -- this is why we are SUID! */ - if (! delete) - { - /* Forward everything from the given local port (with destination - to port 53, and only for UDP) without hijacking */ - { - char *const mangle_args[] = - { - "iptables", "-t", "mangle", "-I", "OUTPUT", "1", "-p", - "udp", "--sport", localport, "--dport", DNS_PORT, "-j", - "ACCEPT", NULL - }; - if (0 != fork_and_exec (SBIN_IPTABLES, mangle_args)) - goto cleanup_mangle_1; - } - /* Mark all of the other DNS traffic using our mark DNS_MARK */ - { - char *const mark_args[] = - { - "iptables", "-t", "mangle", "-I", "OUTPUT", DNS_TABLE, "-p", - "udp", "--dport", DNS_PORT, "-j", "MARK", "--set-mark", DNS_MARK, - NULL - }; - if (0 != fork_and_exec (SBIN_IPTABLES, mark_args)) - goto cleanup_mark_2; - } - /* Forward all marked DNS traffic to our DNS_TABLE */ - { - char *const forward_args[] = - { - "ip", "rule", "add", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL - }; - if (0 != fork_and_exec (SBIN_IP, forward_args)) - goto cleanup_forward_3; - } - /* Finally, add rule in our forwarding table to pass to our virtual interface */ - { - char *const route_args[] = - { - "ip", "route", "add", "default", "via", virt_dns, - "table", DNS_TABLE, NULL - }; - if (0 != fork_and_exec (SBIN_IP, route_args)) - goto cleanup_route_4; - } - } - else - { - r = 0; - /* delete or clean-up-on-error case */ -cleanup_route_4: - { - char *const route_clean_args[] = - { - "ip", "route", "del", "default", "via", virt_dns, - "table", DNS_TABLE, NULL - }; - if (0 != fork_and_exec (SBIN_IP, route_clean_args)) - r += 1; - } -cleanup_forward_3: - { - char *const forward_clean_args[] = - { - "ip", "rule", "del", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL - }; - if (0 != fork_and_exec (SBIN_IP, forward_clean_args)) - r += 2; - } -cleanup_mark_2: - { - char *const mark_clean_args[] = - { - "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp", - "--dport", DNS_PORT, "-j", "MARK", "--set-mark", DNS_MARK, NULL - }; - if (0 != fork_and_exec (SBIN_IPTABLES, mark_clean_args)) - r += 4; - } -cleanup_mangle_1: - { - char *const mangle_clean_args[] = - { - "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp", - "--sport", localport, "--dport", DNS_PORT, "-j", "ACCEPT", - NULL - }; - if (0 != fork_and_exec (SBIN_IPTABLES, mangle_clean_args)) - r += 8; - } - if (r != 0) - { - if (delete) - return 16 + r; /* failed to delete */ - return 32 + r; /* first failed to install, then also failed to clean up! */ - } - if (! delete) - { - /* got here via goto to clean up handler, failed to install, succeeded with clean up */ - return 8; - } - } - /* success ! */ - return 0; -} - -/* end of gnunet-helper-hijack-dns.c */ diff --git a/src/dns/gnunet-service-dns.c b/src/dns/gnunet-service-dns.c index c0e7e56ff..764ede782 100644 --- a/src/dns/gnunet-service-dns.c +++ b/src/dns/gnunet-service-dns.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2009 Christian Grothoff (and other contributing authors) + (C) 2012 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -20,119 +20,177 @@ /** * @file dns/gnunet-service-dns.c - * @author Philipp Toelke + * @author Christian Grothoff */ #include "platform.h" -#include "gnunet_getopt_lib.h" -#include "gnunet_service_lib.h" -#include -#include "gnunet_network_lib.h" -#include "gnunet_os_lib.h" -#include "gnunet_dns_service.h" -#include "gnunet_connection_lib.h" +#include "gnunet_util_lib.h" +#include "gnunet_constants.h" #include "gnunet_protocols.h" -#include "gnunet_applications.h" -#include "gnunet_container_lib.h" -#include "gnunet_dnsparser_lib.h" -#include "gnunet_dht_service.h" -#include "gnunet_block_lib.h" -#include "block_dns.h" -#include "gnunet_crypto_lib.h" -#include "gnunet_mesh_service.h" #include "gnunet_signatures.h" - #include "dns.h" +#include "gnunet_dns_service.h" +#include "gnunet_statistics_service.h" +#include "tcpip_tun.h" -/** - * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to - * some information needed to handle this query - * - * It currently allocates at least - * (1 + machine-width + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit - * = 17 MiB on 64 bit. - * = 11 MiB on 32 bit. - */ -static struct -{ - unsigned valid:1; - struct GNUNET_SERVER_Client *client; - struct GNUNET_MESH_Tunnel *tunnel; - char local_ip[16]; - char remote_ip[16]; - char addrlen; - uint16_t local_port; - char *name; - uint8_t namelen; - uint16_t qtype; -} query_states[UINT16_MAX + 1]; +#ifndef IPVERSION +#define IPVERSION 4 +#endif /** - * A struct used to give more than one value as - * closure to receive_dht + * Phases each request goes through. */ -struct receive_dht_cls +enum RequestPhase { - uint16_t id; - struct GNUNET_DHT_GetHandle *handle; -}; + /** + * Request has just been received. + */ + RP_INIT, -struct tunnel_notify_queue -{ - struct tunnel_notify_queue *next; - struct tunnel_notify_queue *prev; - void *cls; - size_t len; - GNUNET_CONNECTION_TransmitReadyNotify cb; -}; + /** + * Showing the request to all monitor clients. If + * client list is empty, will enter QUERY phase. + */ + RP_REQUEST_MONITOR, -struct tunnel_state -{ - struct tunnel_notify_queue *head, *tail; - struct GNUNET_MESH_TransmitHandle *th; + /** + * Showing the request to PRE-RESOLUTION clients to find an answer. + * If client list is empty, will trigger global DNS request. + */ + RP_QUERY, + + /** + * Global Internet query is now pending. + */ + RP_INTERNET_DNS, + + /** + * Client (or global DNS request) has resulted in a response. + * Forward to all POST-RESOLUTION clients. If client list is empty, + * will enter RESPONSE_MONITOR phase. + */ + RP_MODIFY, + + /** + * Showing the request to all monitor clients. If + * client list is empty, give the result to the hijacker (and be done). + */ + RP_RESPONSE_MONITOR, + + /** + * Some client has told us to drop the request. + */ + RP_DROP }; -struct answer_packet_list +/** + * Entry we keep for each client. + */ +struct ClientRecord { - struct answer_packet_list *next; - struct answer_packet_list *prev; + /** + * Kept in doubly-linked list. + */ + struct ClientRecord *next; + + /** + * Kept in doubly-linked list. + */ + struct ClientRecord *prev; + + /** + * Handle to the client. + */ struct GNUNET_SERVER_Client *client; - struct answer_packet pkt; + + /** + * Flags for the client. + */ + enum GNUNET_DNS_Flags flags; + }; -GNUNET_NETWORK_STRUCT_BEGIN -struct tunnel_cls + +/** + * Entry we keep for each active request. + */ +struct RequestRecord { - struct GNUNET_MESH_Tunnel *tunnel; - struct GNUNET_MessageHeader hdr; - struct dns_pkt dns; -}; -GNUNET_NETWORK_STRUCT_END -struct tunnel_cls *remote_pending[UINT16_MAX]; + /** + * List of clients that still need to see this request (each entry + * is set to NULL when the client is done). + */ + struct ClientRecord **client_wait_list; + + /** + * Payload of the UDP packet (the UDP payload), can be either query + * or already the response. + */ + char *payload; + + /** + * Source address of the original request (for sending response). + */ + struct sockaddr_storage src_addr; + + /** + * Destination address of the original request (for potential use as exit). + */ + struct sockaddr_storage dst_addr; + /** + * ID of this request, also basis for hashing. Lowest 16 bit will + * be our message ID when doing a global DNS request and our index + * into the 'requests' array. + */ + uint64_t request_id; -static struct GNUNET_MESH_Handle *mesh_handle; + /** + * Number of bytes in payload. + */ + size_t payload_length; + + /** + * Length of the client wait list. + */ + unsigned int client_wait_list_length; + + /** + * In which phase this this request? + */ + enum RequestPhase phase; + +}; -static struct GNUNET_CONNECTION_TransmitHandle *server_notify; /** - * The UDP-Socket through which DNS-Resolves will be sent if they are not to be + * The IPv4 UDP-Socket through which DNS-Resolves will be sent if they are not to be * sent through gnunet. The port of this socket will not be hijacked. */ -static struct GNUNET_NETWORK_Handle *dnsout; +static struct GNUNET_NETWORK_Handle *dnsout4; +/** + * The IPv6 UDP-Socket through which DNS-Resolves will be sent if they are not to be + * sent through gnunet. The port of this socket will not be hijacked. + */ static struct GNUNET_NETWORK_Handle *dnsout6; /** - * The port bound to the socket dnsout + * Task for reading from dnsout4. + */ +static GNUNET_SCHEDULER_TaskIdentifier read4_task; + +/** + * Task for reading from dnsout6. */ -static unsigned short dnsoutport; +static GNUNET_SCHEDULER_TaskIdentifier read6_task; /** - * A handle to the DHT-Service + * The port bound to the socket dnsout (and/or dnsout6). We always (try) to bind + * both sockets to the same port. */ -static struct GNUNET_DHT_Handle *dht; +static uint16_t dnsoutport; /** * The configuration to use @@ -140,1538 +198,975 @@ static struct GNUNET_DHT_Handle *dht; static const struct GNUNET_CONFIGURATION_Handle *cfg; /** - * A list of DNS-Responses that have to be sent to the requesting client + * Statistics. */ -static struct answer_packet_list *head; +static struct GNUNET_STATISTICS_Handle *stats; /** - * The tail of the list of DNS-responses + * Handle to DNS hijacker helper process ("gnunet-helper-dns"). */ -static struct answer_packet_list *tail; +static struct GNUNET_HELPER_Handle *hijacker; +/** + * Command-line arguments we are giving to the hijacker process. + */ +static char *helper_argv[8]; -static size_t -send_answer (void *cls, size_t size, void *buf); +/** + * Head of DLL of clients we consult. + */ +static struct ClientRecord *clients_head; -static void -client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) -{ - if (NULL == head) - return; +/** + * Tail of DLL of clients we consult. + */ +static struct ClientRecord *clients_tail; - if (head->client == client) - { - GNUNET_CONNECTION_notify_transmit_ready_cancel (server_notify); - server_notify = - GNUNET_SERVER_notify_transmit_ready (head->next->client, - ntohs (head->next->pkt.hdr.size), - GNUNET_TIME_UNIT_FOREVER_REL, - &send_answer, NULL); - } +/** + * Our notification context. + */ +static struct GNUNET_SERVER_NotificationContext *nc; - struct answer_packet_list *element = head; +/** + * Array of all open requests. + */ +static struct RequestRecord requests[UINT16_MAX + 1]; - while (element != NULL) - { - if (element->client == client) - { - GNUNET_SERVER_client_drop (client); - GNUNET_CONTAINER_DLL_remove (head, tail, element); - struct answer_packet_list *t = element; +/** + * Generator for unique request IDs. + */ +static uint64_t request_id_gen; - element = element->next; - GNUNET_free (t); - } - else - element = element->next; - } -} /** - * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port. + * We're done processing a DNS request, free associated memory. + * + * @param rr request to clean up */ static void -hijack (void *cls GNUNET_UNUSED, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - return; - - if (0 == dnsoutport) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Delaying the hijacking, port is still %d!\n", dnsoutport); - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL); - return; - } - - char port_s[6]; - char *virt_dns; - struct GNUNET_OS_Process *proc; - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'VIRTDNS' in configuration!\n"); - exit (1); - } - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport); - snprintf (port_s, 6, "%d", dnsoutport); - if (NULL != - (proc = - GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns", - "gnunet-hijack-dns", port_s, virt_dns, NULL))) - { - GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc)); - GNUNET_OS_process_close (proc); - } - GNUNET_free (virt_dns); -} - -static void * -new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, - const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED, - const struct GNUNET_ATS_Information *ats GNUNET_UNUSED) +cleanup_rr (struct RequestRecord *rr) { - struct tunnel_state *s = GNUNET_malloc (sizeof *s); - - s->head = NULL; - s->tail = NULL; - s->th = NULL; - return s; + GNUNET_free_non_null (rr->payload); + rr->payload = NULL; + rr->payload_length = 0; + GNUNET_array_grow (rr->client_wait_list, + rr->client_wait_list_length, + 0); } -static void -clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel, - void *tunnel_ctx) -{ - GNUNET_free (tunnel_ctx); -} /** - * Delete the hijacking-routes + * Task run during shutdown. + * + * @param cls unused + * @param tc unused */ static void -unhijack (unsigned short port) +cleanup_task (void *cls GNUNET_UNUSED, + const struct GNUNET_SCHEDULER_TaskContext *tc) { - char port_s[6]; - char *virt_dns; - struct GNUNET_OS_Process *proc; + unsigned int i; - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns)) + GNUNET_HELPER_stop (hijacker); + hijacker = NULL; + for (i=0;i<8;i++) + GNUNET_free_non_null (helper_argv[i]); + if (NULL != dnsout4) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'VIRTDNS' in configuration!\n"); - exit (1); + GNUNET_NETWORK_socket_close (dnsout4); + dnsout4 = NULL; } - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port); - snprintf (port_s, 6, "%d", port); - if (NULL != - (proc = - GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns", - "gnunet-hijack-dns", "-d", port_s, virt_dns, - NULL))) + if (GNUNET_SCHEDULER_NO_TASK != read4_task) { - GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc)); - GNUNET_OS_process_close (proc); + GNUNET_SCHEDULER_cancel (read4_task); + read4_task = GNUNET_SCHEDULER_NO_TASK; } - GNUNET_free (virt_dns); -} - -/** - * Send the DNS-Response to the client. Gets called via the notify_transmit_ready- - * system. - */ -static size_t -send_answer (void *cls, size_t size, void *buf) -{ - server_notify = NULL; - struct answer_packet_list *query = head; - size_t len = ntohs (query->pkt.hdr.size); - - GNUNET_assert (len <= size); - - memcpy (buf, &query->pkt.hdr, len); - - GNUNET_CONTAINER_DLL_remove (head, tail, query); - - /* When more data is to be sent, reschedule */ - if (head != NULL) - server_notify = - GNUNET_SERVER_notify_transmit_ready (head->client, - ntohs (head->pkt.hdr.size), - GNUNET_TIME_UNIT_FOREVER_REL, - &send_answer, NULL); - - GNUNET_SERVER_client_drop (query->client); - GNUNET_free (query); - return len; -} - - -static size_t -mesh_send_response (void *cls, size_t size, void *buf) -{ - GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); - struct GNUNET_MessageHeader *hdr = buf; - uint32_t *sz = cls; - struct GNUNET_MESH_Tunnel **tunnel = (struct GNUNET_MESH_Tunnel **) (sz + 1); - struct dns_pkt *dns = (struct dns_pkt *) (tunnel + 1); - - GNUNET_MESH_tunnel_set_data (*tunnel, NULL); - - hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS); - hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader)); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz, - *sz + sizeof (struct GNUNET_MessageHeader)); - - GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader))); - - memcpy (hdr + 1, dns, *sz); - struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (*tunnel); - - if (NULL != s->head) - { - struct tunnel_notify_queue *element = s->head; - struct tunnel_notify_queue *head = s->head; - struct tunnel_notify_queue *tail = s->tail; - - GNUNET_CONTAINER_DLL_remove (head, tail, element); - - s->th = - GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42, - GNUNET_TIME_relative_divide - (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), - (const struct GNUNET_PeerIdentity *) - NULL, element->len, element->cb, - element->cls); - } - - GNUNET_free (cls); - - return ntohs (hdr->size); -} - -static size_t -mesh_send (void *cls, size_t size, void *buf) -{ - struct tunnel_cls *cls_ = (struct tunnel_cls *) cls; - - GNUNET_MESH_tunnel_set_data (cls_->tunnel, NULL); - - GNUNET_assert (cls_->hdr.size <= size); - - size = cls_->hdr.size; - cls_->hdr.size = htons (cls_->hdr.size); - - memcpy (buf, &cls_->hdr, size); - - struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (cls_->tunnel); - - if (NULL != s->head) + if (NULL != dnsout6) { - struct tunnel_notify_queue *element = s->head; - struct tunnel_notify_queue *head = s->head; - struct tunnel_notify_queue *tail = s->tail;; - - GNUNET_CONTAINER_DLL_remove (head, tail, element); - - s->th = - GNUNET_MESH_notify_transmit_ready (cls_->tunnel, GNUNET_NO, 42, - GNUNET_TIME_relative_divide - (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), - (const struct GNUNET_PeerIdentity *) - NULL, element->len, element->cb, - element->cls); - - GNUNET_free (element); + GNUNET_NETWORK_socket_close (dnsout6); + dnsout6 = NULL; } - - return size; -} - - -static void -mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) -{ - if (NULL == peer) - return; - struct tunnel_cls *cls_ = (struct tunnel_cls *) cls; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Connected to peer %s, %x, sending query with id %d\n", - GNUNET_i2s (peer), peer, ntohs (cls_->dns.s.id)); - - struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (cls_->tunnel); - - if (NULL == s->head) + if (GNUNET_SCHEDULER_NO_TASK != read6_task) { - s->th = - GNUNET_MESH_notify_transmit_ready (cls_->tunnel, GNUNET_YES, 42, - GNUNET_TIME_UNIT_MINUTES, NULL, - cls_->hdr.size, mesh_send, cls); - + GNUNET_SCHEDULER_cancel (read6_task); + read6_task = GNUNET_SCHEDULER_NO_TASK; } - else + for (i=0;i<65536;i++) + cleanup_rr (&requests[i]); + GNUNET_SERVER_notification_context_destroy (nc); + nc = NULL; + if (stats != NULL) { - struct tunnel_notify_queue *head = s->head; - struct tunnel_notify_queue *tail = s->tail; - - struct tunnel_notify_queue *element = - GNUNET_malloc (sizeof (struct tunnel_notify_queue)); - element->cls = cls; - element->len = cls_->hdr.size; - element->cb = mesh_send; - - GNUNET_CONTAINER_DLL_insert_tail (head, tail, element); + GNUNET_STATISTICS_destroy (stats, GNUNET_YES); + stats = NULL; } } +/** + * We're done with some request, finish processing. + * + * @param rr request send to the network or just clean up. + */ static void -send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - return; - - struct tunnel_cls *cls_ = (struct tunnel_cls *) cls; - - struct tunnel_state *s = GNUNET_malloc (sizeof *s); - - s->head = NULL; - s->tail = NULL; - s->th = NULL; - - cls_->tunnel = - GNUNET_MESH_tunnel_create (mesh_handle, s, mesh_connect, NULL, cls_); - - GNUNET_MESH_peer_request_connect_by_type (cls_->tunnel, - GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER); - - remote_pending[cls_->dns.s.id] = cls_; -} - -static int -receive_mesh_query (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, - void **ctx GNUNET_UNUSED, - const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED, - const struct GNUNET_MessageHeader *message, - const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) +request_done (struct RequestRecord *rr) { - struct dns_pkt *dns = (struct dns_pkt *) (message + 1); - - struct sockaddr_in dest; - - struct dns_pkt_parsed *pdns = parse_dns_packet (dns); - - memset (&dest, 0, sizeof dest); - dest.sin_port = htons (53); - char *dns_resolver; - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "EXTERNAL_DNS", - &dns_resolver) || - 1 != inet_pton (AF_INET, dns_resolver, &dest.sin_addr)) - inet_pton (AF_INET, "8.8.8.8", &dest.sin_addr); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Querying for remote, id=%d\n", - ntohs (dns->s.id)); - query_states[dns->s.id].tunnel = tunnel; - query_states[dns->s.id].valid = GNUNET_YES; - - int i; - - for (i = 0; i < ntohs (pdns->s.qdcount); i++) - { - if (pdns->queries[i]->qtype == htons (28) || - pdns->queries[i]->qtype == htons (1)) - { - query_states[dns->s.id].qtype = pdns->queries[i]->qtype; - break; - } - } - free_parsed_dns_packet (pdns); - - GNUNET_NETWORK_socket_sendto (dnsout, dns, - ntohs (message->size) - - sizeof (struct GNUNET_MessageHeader), - (struct sockaddr *) &dest, sizeof dest); - - return GNUNET_SYSERR; -} - -static int -receive_mesh_answer (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, - void **ctx GNUNET_UNUSED, - const struct GNUNET_PeerIdentity *sender, - const struct GNUNET_MessageHeader *message, - const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) -{ - /* TODo: size check */ - struct dns_pkt *dns = (struct dns_pkt *) (message + 1); - - /* They sent us a packet we were not waiting for */ - if (remote_pending[dns->s.id] == NULL || - remote_pending[dns->s.id]->tunnel != tunnel) - return GNUNET_OK; - - GNUNET_free (remote_pending[dns->s.id]); - remote_pending[dns->s.id] = NULL; - - if (query_states[dns->s.id].valid != GNUNET_YES) - return GNUNET_SYSERR; - query_states[dns->s.id].valid = GNUNET_NO; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received answer from peer %s, dns-id %d\n", GNUNET_i2s (sender), - ntohs (dns->s.id)); - - size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + query_states[dns->s.id].namelen + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */ - + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */ - - struct answer_packet_list *answer = - GNUNET_malloc (len + sizeof (struct answer_packet_list) - - sizeof (struct answer_packet)); - - answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); - answer->pkt.hdr.size = htons (len); - - struct dns_pkt_parsed *pdns = parse_dns_packet (dns); - - if (ntohs (pdns->s.ancount) < 1) + struct GNUNET_MessageHeader *hdr; + size_t reply_len; + uint16_t spt; + uint16_t dpt; + + GNUNET_array_grow (rr->client_wait_list, + rr->client_wait_list_length, + 0); + if (RP_RESPONSE_MONITOR != rr->phase) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n", - ntohs (pdns->s.ancount)); - free_parsed_dns_packet (pdns); - GNUNET_free (answer); - return GNUNET_OK; - } - - int i = 0; - - while (i < ntohs (pdns->s.ancount) && ntohs (pdns->answers[i]->type) != 28 && - ntohs (pdns->answers[i]->type) != 1) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer contains %d.\n", - ntohs (pdns->answers[i]->type)); - i++; - } - - if (i >= ntohs (pdns->s.ancount)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Answer does not contain any usable answers.\n"); - free_parsed_dns_packet (pdns); - GNUNET_free (answer); - return GNUNET_OK; + /* no response, drop */ + cleanup_rr (rr); + return; } - - answer->pkt.addrsize = ntohs (pdns->answers[i]->data_len); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The first answer has the addrlen %d\n", - answer->pkt.addrsize); - memcpy (answer->pkt.addr, pdns->answers[i]->data, - ntohs (pdns->answers[i]->data_len)); - - memcpy (answer->pkt.from, query_states[dns->s.id].remote_ip, - query_states[dns->s.id].addrlen); - memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, - query_states[dns->s.id].addrlen); - answer->pkt.addrlen = query_states[dns->s.id].addrlen; - answer->pkt.dst_port = query_states[dns->s.id].local_port; - - struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data; - - dpkt->s.id = dns->s.id; - dpkt->s.aa = 1; - dpkt->s.qr = 1; - dpkt->s.ra = 1; - dpkt->s.qdcount = htons (1); - dpkt->s.ancount = htons (1); - - memcpy (dpkt->data, query_states[dns->s.id].name, - query_states[dns->s.id].namelen); - GNUNET_free (query_states[dns->s.id].name); - query_states[dns->s.id].name = NULL; - - struct dns_query_line *dque = - (struct dns_query_line *) (dpkt->data + - (query_states[dns->s.id].namelen)); - - struct dns_record_line *drec_data = - (struct dns_record_line *) (dpkt->data + - (query_states[dns->s.id].namelen) + - sizeof (struct dns_query_line) + 2); - if (htons (28) == query_states[dns->s.id].qtype) + + /* send response via hijacker */ + reply_len = sizeof (struct GNUNET_MessageHeader); + reply_len += sizeof (struct tun_header); + switch (rr->src_addr.ss_family) { - answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA; - dque->type = htons (28); /* AAAA */ - drec_data->type = htons (28); /* AAAA */ - drec_data->data_len = htons (16); + case AF_INET: + reply_len += sizeof (struct ip4_header); + break; + case AF_INET6: + reply_len += sizeof (struct ip6_header); + break; + default: + GNUNET_break (0); + cleanup_rr (rr); + return; } - else if (htons (1) == query_states[dns->s.id].qtype) + reply_len += sizeof (struct udp_packet); + reply_len += rr->payload_length; + if (reply_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) { - answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_A; - dque->type = htons (1); /* A */ - drec_data->type = htons (1); /* A */ - drec_data->data_len = htons (4); + /* response too big, drop */ + GNUNET_break (0); /* how can this be? */ + cleanup_rr(rr); + return; } - else { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "dns-answer with pending qtype = %d\n", - query_states[dns->s.id].qtype); - GNUNET_assert (0); - } - dque->class = htons (1); /* IN */ + char buf[reply_len]; + size_t off; + uint32_t udp_crc_sum; - char *anname = - (char *) (dpkt->data + (query_states[dns->s.id].namelen) + - sizeof (struct dns_query_line)); - memcpy (anname, "\xc0\x0c", 2); - drec_data->class = htons (1); /* IN */ + /* first, GNUnet message header */ + hdr = (struct GNUNET_MessageHeader*) buf; + hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER); + hdr->size = htons ((uint16_t) reply_len); + off = sizeof (struct GNUNET_MessageHeader); - drec_data->ttl = pdns->answers[i]->ttl; - - /* Calculate at which offset in the packet the IPv6-Address belongs, it is - * filled in by the daemon-vpn */ - answer->pkt.addroffset = - htons ((unsigned short) ((unsigned long) (&drec_data->data) - - (unsigned long) (&answer->pkt))); - - GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); - answer->client = query_states[dns->s.id].client; + /* first, TUN header */ + { + struct tun_header tun; - if (server_notify == NULL) - server_notify = - GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client, - len, GNUNET_TIME_UNIT_FOREVER_REL, - &send_answer, NULL); + tun.flags = htons (0); + if (rr->src_addr.ss_family == AF_INET) + tun.proto = htons (ETH_P_IPV4); + else + tun.proto = htons (ETH_P_IPV6); + memcpy (&buf[off], &tun, sizeof (struct tun_header)); + off += sizeof (struct tun_header); + } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Sent answer of length %d on to client, addroffset = %d\n", len, - answer->pkt.addroffset); + /* now IP header */ + udp_crc_sum = 0; + switch (rr->src_addr.ss_family) + { + case AF_INET: + { + struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr; + struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr; + struct ip4_header ip; + + spt = dst->sin_port; + dpt = src->sin_port; + ip.header_length = sizeof (struct ip4_header) / 4; + ip.version = IPVERSION; /* aka 4 */ + ip.diff_serv = 0; + ip.total_length = htons ((uint16_t) reply_len - off); + ip.identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, + 65536); + ip.flags = 0; + ip.fragmentation_offset = 0; + ip.ttl = 255; /* or lower? */ + ip.protocol = IPPROTO_UDP; + ip.checksum = 0; /* checksum is optional */ + ip.source_address = dst->sin_addr; + ip.destination_address = src->sin_addr; + ip.checksum = GNUNET_CRYPTO_crc16_n (&ip, sizeof (ip)); + + udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, + &ip.source_address, + sizeof (struct in_addr) * 2); + { + uint16_t tmp; + + tmp = htons (IPPROTO_UDP); + udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, + &tmp, + sizeof (uint16_t)); + tmp = htons (rr->payload_length + sizeof (struct udp_packet)); + udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, + &tmp, + sizeof (uint16_t)); + } + memcpy (&buf[off], &ip, sizeof (ip)); + off += sizeof (ip); + } + break; + case AF_INET6: + { + struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr; + struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr; + struct ip6_header ip; + + spt = dst->sin6_port; + dpt = src->sin6_port; + ip.traffic_class_h = 0; + ip.version = 6; /* is there a named constant? I couldn't find one */ + ip.traffic_class_l = 0; + ip.flow_label = 0; + ip.payload_length = htons ((uint16_t) reply_len); + ip.next_header = IPPROTO_UDP; + ip.hop_limit = 255; /* or lower? */ + ip.source_address = dst->sin6_addr; + ip.destination_address = src->sin6_addr; + udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, + &ip.source_address, + sizeof (struct in6_addr) * 2); + { + uint32_t tmp; + + tmp = htons (rr->payload_length + sizeof (struct udp_packet)); + udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, + &tmp, + sizeof (uint32_t)); + tmp = htons (IPPROTO_UDP); + udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, + &tmp, + sizeof (uint32_t)); + } + memcpy (&buf[off], &ip, sizeof (ip)); + off += sizeof (ip); + } + break; + default: + GNUNET_assert (0); + } - free_parsed_dns_packet (pdns); - return GNUNET_OK; + /* now UDP header */ + { + struct udp_packet udp; + + udp.spt = spt; + udp.dpt = dpt; + udp.len = htons (reply_len - off); + udp.crc = 0; + udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, + &udp, + sizeof (udp)); + udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, + rr->payload, + rr->payload_length); + udp.crc = GNUNET_CRYPTO_crc16_finish (udp_crc_sum); + memcpy (&buf[off], &udp, sizeof (udp)); + off += sizeof (udp); + } + /* now DNS payload */ + { + memcpy (&buf[off], rr->payload, rr->payload_length); + off += rr->payload_length; + } + /* final checks & sending */ + GNUNET_assert (off == reply_len); + GNUNET_HELPER_send (hijacker, + hdr, + GNUNET_YES, + NULL, NULL); + GNUNET_STATISTICS_update (stats, + gettext_noop ("# DNS requests answered via TUN interface"), + 1, GNUNET_NO); + } + /* clean up, we're done */ + cleanup_rr (rr); } -static void -send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - return; - - struct dns_pkt_parsed *pdns = (struct dns_pkt_parsed *) cls; - - unsigned short id = pdns->s.id; - - free_parsed_dns_packet (pdns); - - if (query_states[id].valid != GNUNET_YES) - return; - query_states[id].valid = GNUNET_NO; - - GNUNET_assert (query_states[id].namelen == 74); - - size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + 74 /* this is the length of a reverse ipv6-lookup */ - + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */ - + sizeof (struct dns_record_line) - 1 - - 2 /* We do not know the lenght of the answer yet */ ; - - struct answer_packet_list *answer = - GNUNET_malloc (len + sizeof (struct answer_packet_list) - - sizeof (struct answer_packet)); - - answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); - answer->pkt.hdr.size = htons (len); - answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV; - - memcpy (answer->pkt.from, query_states[id].remote_ip, - query_states[id].addrlen); - memcpy (answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen); - - answer->pkt.dst_port = query_states[id].local_port; - - struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data; - - dpkt->s.id = id; - dpkt->s.aa = 1; - dpkt->s.qr = 1; - dpkt->s.ra = 1; - dpkt->s.qdcount = htons (1); - dpkt->s.ancount = htons (1); - - memcpy (dpkt->data, query_states[id].name, query_states[id].namelen); - GNUNET_free (query_states[id].name); - query_states[id].name = NULL; - - struct dns_query_line *dque = - (struct dns_query_line *) (dpkt->data + (query_states[id].namelen)); - dque->type = htons (12); /* PTR */ - dque->class = htons (1); /* IN */ - - char *anname = - (char *) (dpkt->data + (query_states[id].namelen) + - sizeof (struct dns_query_line)); - memcpy (anname, "\xc0\x0c", 2); - - struct dns_record_line *drec_data = - (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) + - sizeof (struct dns_query_line) + 2); - drec_data->type = htons (12); /* AAAA */ - drec_data->class = htons (1); /* IN */ - /* FIXME: read the TTL from block: - * GNUNET_TIME_absolute_get_remaining(rec->expiration_time) - * - * But how to get the seconds out of this? - */ - drec_data->ttl = htonl (3600); - - /* Calculate at which offset in the packet the length of the name and the - * name, it is filled in by the daemon-vpn */ - answer->pkt.addroffset = - htons ((unsigned short) ((unsigned long) (&drec_data->data_len) - - (unsigned long) (&answer->pkt))); - - GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); - answer->client = query_states[id].client; - - if (server_notify == NULL) - server_notify = - GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len, - GNUNET_TIME_UNIT_FOREVER_REL, - &send_answer, NULL); -} - /** - * Receive a block from the dht. + * Show the payload of the given request record to the client + * (and wait for a response). + * + * @param rr request to send to client + * @param client client to send the response to */ static void -receive_dht (void *cls, struct GNUNET_TIME_Absolute exp GNUNET_UNUSED, - const GNUNET_HashCode * key GNUNET_UNUSED, - const struct GNUNET_PeerIdentity *get_path GNUNET_UNUSED, - unsigned int get_path_length GNUNET_UNUSED, - const struct GNUNET_PeerIdentity *put_path GNUNET_UNUSED, - unsigned int put_path_length GNUNET_UNUSED, - enum GNUNET_BLOCK_Type type, size_t size, const void *data) +send_request_to_client (struct RequestRecord *rr, + struct GNUNET_SERVER_Client *client) { + char buf[sizeof (struct GNUNET_DNS_Request) + rr->payload_length]; + struct GNUNET_DNS_Request *req; - unsigned short id = ((struct receive_dht_cls *) cls)->id; - struct GNUNET_DHT_GetHandle *handle = - ((struct receive_dht_cls *) cls)->handle; - GNUNET_free (cls); - - GNUNET_DHT_get_stop (handle); - - GNUNET_assert (type == GNUNET_BLOCK_TYPE_DNS); - - /* If no query with this id is pending, ignore the block */ - if (query_states[id].valid != GNUNET_YES) + if (sizeof (buf) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) + { + GNUNET_break (0); + cleanup_rr (rr); return; - query_states[id].valid = GNUNET_NO; - - const struct GNUNET_DNS_Record *rec = data; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Got block of size %d, peer: %08x, desc: %08x\n", size, - *((unsigned int *) &rec->peer), - *((unsigned int *) &rec->service_descriptor)); - - size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + query_states[id].namelen + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */ - + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */ - - struct answer_packet_list *answer = - GNUNET_malloc (len + sizeof (struct answer_packet_list) - - sizeof (struct answer_packet)); - - answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); - answer->pkt.hdr.size = htons (len); - answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE; - answer->client = query_states[id].client; - - GNUNET_CRYPTO_hash (&rec->peer, - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), - &answer->pkt.service_descr.peer); - - memcpy (&answer->pkt.service_descr.service_descriptor, - &rec->service_descriptor, sizeof (GNUNET_HashCode)); - memcpy (&answer->pkt.service_descr.service_type, &rec->service_type, - sizeof (answer->pkt.service_descr.service_type)); - memcpy (&answer->pkt.service_descr.ports, &rec->ports, - sizeof (answer->pkt.service_descr.ports)); - - memcpy (answer->pkt.from, query_states[id].remote_ip, - query_states[id].addrlen); - memcpy (answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen); - answer->pkt.addrlen = query_states[id].addrlen; - - answer->pkt.dst_port = query_states[id].local_port; - - struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data; - - dpkt->s.id = id; - dpkt->s.aa = 1; - dpkt->s.qr = 1; - dpkt->s.ra = 1; - dpkt->s.qdcount = htons (1); - dpkt->s.ancount = htons (1); - - memcpy (dpkt->data, query_states[id].name, query_states[id].namelen); - GNUNET_free (query_states[id].name); - query_states[id].name = NULL; - - struct dns_query_line *dque = - (struct dns_query_line *) (dpkt->data + (query_states[id].namelen)); - dque->type = htons (28); /* AAAA */ - dque->class = htons (1); /* IN */ - - char *anname = - (char *) (dpkt->data + (query_states[id].namelen) + - sizeof (struct dns_query_line)); - memcpy (anname, "\xc0\x0c", 2); - - struct dns_record_line *drec_data = - (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) + - sizeof (struct dns_query_line) + 2); - drec_data->type = htons (28); /* AAAA */ - drec_data->class = htons (1); /* IN */ - - /* FIXME: read the TTL from block: - * GNUNET_TIME_absolute_get_remaining(rec->expiration_time) - * - * But how to get the seconds out of this? - */ - drec_data->ttl = htonl (3600); - drec_data->data_len = htons (16); - - /* Calculate at which offset in the packet the IPv6-Address belongs, it is - * filled in by the daemon-vpn */ - answer->pkt.addroffset = - htons ((unsigned short) ((unsigned long) (&drec_data->data) - - (unsigned long) (&answer->pkt))); - - GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); - - if (server_notify == NULL) - server_notify = - GNUNET_SERVER_notify_transmit_ready (answer->client, len, - GNUNET_TIME_UNIT_FOREVER_REL, - &send_answer, NULL); + } + req = (struct GNUNET_DNS_Request*) buf; + req->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST); + req->header.size = htons (sizeof (buf)); + req->reserved = htonl (0); + req->request_id = rr->request_id; + memcpy (&req[1], rr->payload, rr->payload_length); + GNUNET_SERVER_notification_context_unicast (nc, + client, + &req->header, + GNUNET_NO); } -/** - * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS - */ -static void -rehijack (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message GNUNET_UNUSED) -{ - unhijack (dnsoutport); - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL); - - GNUNET_SERVER_receive_done (client, GNUNET_OK); -} /** - * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket + * A client has completed its processing for this + * request. Move on. + * + * @param rr request to process further */ static void -receive_query (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +next_phase (struct RequestRecord *rr) { - struct query_packet *pkt = (struct query_packet *) message; - struct dns_pkt *dns = (struct dns_pkt *) pkt->data; - struct dns_pkt_parsed *pdns = parse_dns_packet (dns); + struct ClientRecord *cr; + int nz; + unsigned int j; + struct GNUNET_NETWORK_Handle *dnsout; + socklen_t salen; - query_states[dns->s.id].valid = GNUNET_YES; - query_states[dns->s.id].client = client; - GNUNET_SERVER_client_keep (client); - memcpy (query_states[dns->s.id].local_ip, pkt->orig_from, pkt->addrlen); - query_states[dns->s.id].addrlen = pkt->addrlen; - query_states[dns->s.id].local_port = pkt->src_port; - memcpy (query_states[dns->s.id].remote_ip, pkt->orig_to, pkt->addrlen); - query_states[dns->s.id].namelen = strlen ((char *) dns->data) + 1; - if (query_states[dns->s.id].name != NULL) - GNUNET_free (query_states[dns->s.id].name); - query_states[dns->s.id].name = - GNUNET_malloc (query_states[dns->s.id].namelen); - memcpy (query_states[dns->s.id].name, dns->data, - query_states[dns->s.id].namelen); - - int i; - - for (i = 0; i < ntohs (pdns->s.qdcount); i++) + if (rr->phase == RP_DROP) { - if (pdns->queries[i]->qtype == htons (28) || - pdns->queries[i]->qtype == htons (1)) - { - query_states[dns->s.id].qtype = pdns->queries[i]->qtype; - break; - } - } - - /* The query is for a .gnunet-address */ - if (pdns->queries[0]->namelen > 9 && - 0 == strncmp (pdns->queries[0]->name + (pdns->queries[0]->namelen - 9), - ".gnunet.", 9)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n"); - GNUNET_HashCode key; - - GNUNET_CRYPTO_hash (pdns->queries[0]->name, pdns->queries[0]->namelen, - &key); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Getting with key %08x, len is %d\n", - *((unsigned int *) &key), pdns->queries[0]->namelen); - - struct receive_dht_cls *cls = - GNUNET_malloc (sizeof (struct receive_dht_cls)); - cls->id = dns->s.id; - - cls->handle = - GNUNET_DHT_get_start (dht, GNUNET_TIME_UNIT_MINUTES, - GNUNET_BLOCK_TYPE_DNS, &key, - 5 /* DEFAULT_GET_REPLICATION */ , - GNUNET_DHT_RO_NONE, NULL, 0, &receive_dht, cls); - - goto outfree; + cleanup_rr (rr); + return; } - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", - pdns->queries[0]->name, pdns->queries[0]->namelen); - - /* This is a PTR-Query. Check if it is for "our" network */ - if (htons (pdns->queries[0]->qtype) == 12 && 74 == pdns->queries[0]->namelen) + nz = -1; + for (j=0;jclient_wait_list_length;j++) { - char *ipv6addr; - char ipv6[16]; - char ipv6rev[74] = - "X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.ip6.arpa."; - unsigned int i; - unsigned long long ipv6prefix; - unsigned int comparelen; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", - "IPV6ADDR", - &ipv6addr)); - inet_pton (AF_INET6, ipv6addr, ipv6); - GNUNET_free (ipv6addr); - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", - "IPV6PREFIX", - &ipv6prefix)); - GNUNET_assert (ipv6prefix < 127); - ipv6prefix = (ipv6prefix + 7) / 8; - - for (i = ipv6prefix; i < 16; i++) - ipv6[i] = 0; - - for (i = 0; i < 16; i++) - { - unsigned char c1 = ipv6[i] >> 4; - unsigned char c2 = ipv6[i] & 0xf; - - if (c1 <= 9) - ipv6rev[62 - (4 * i)] = c1 + '0'; - else - ipv6rev[62 - (4 * i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */ - - if (c2 <= 9) - ipv6rev[62 - ((4 * i) + 2)] = c2 + '0'; - else - ipv6rev[62 - ((4 * i) + 2)] = c2 + 87; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev); - comparelen = 10 + 4 * ipv6prefix; - if (0 == - strncmp (pdns->queries[0]->name + - (pdns->queries[0]->namelen - comparelen), - ipv6rev + (74 - comparelen), comparelen)) + if (NULL != rr->client_wait_list[j]) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n"); - - GNUNET_SCHEDULER_add_now (send_rev_query, pdns); - - goto out; + nz = (int) j; + break; } + } + if (-1 != nz) + { + send_request_to_client (rr, rr->client_wait_list[nz]->client); + return; } - - unsigned char virt_dns_bytes[16]; - - if (pkt->addrlen == 4) + /* done with current phase, advance! */ + switch (rr->phase) { - char *virt_dns; - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", - &virt_dns)) + case RP_INIT: + rr->phase = RP_REQUEST_MONITOR; + for (cr = clients_head; NULL != cr; cr = cr->next) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'VIRTDNS' in configuration!\n"); - exit (1); + if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR)) + GNUNET_array_append (rr->client_wait_list, + rr->client_wait_list_length, + cr); } - - if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes)) + next_phase (rr); + return; + case RP_REQUEST_MONITOR: + rr->phase = RP_QUERY; + for (cr = clients_head; NULL != cr; cr = cr->next) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing 'VIRTDNS': %s; %m!\n", - virt_dns); - exit (1); + if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION)) + GNUNET_array_append (rr->client_wait_list, + rr->client_wait_list_length, + cr); } - - GNUNET_free (virt_dns); - } - else if (pkt->addrlen == 16) - { - char *virt_dns; - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS6", - &virt_dns)) + next_phase (rr); + return; + case RP_QUERY: + rr->phase = RP_INTERNET_DNS; + switch (rr->dst_addr.ss_family) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'VIRTDNS6' in configuration!\n"); - exit (1); + case AF_INET: + dnsout = dnsout4; + salen = sizeof (struct ip4_header); + break; + case AF_INET6: + dnsout = dnsout6; + salen = sizeof (struct ip6_header); + break; + default: + GNUNET_break (0); + cleanup_rr (rr); + return; } - - if (1 != inet_pton (AF_INET6, virt_dns, &virt_dns_bytes)) + if (NULL == dnsout) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Error parsing 'VIRTDNS6': %s; %m!\n", virt_dns); - exit (1); + GNUNET_STATISTICS_update (stats, + gettext_noop ("# DNS exit failed (address family not supported)"), + 1, GNUNET_NO); + cleanup_rr (rr); + return; } - - GNUNET_free (virt_dns); - } - else - { - GNUNET_assert (0); - } - - if (memcmp (virt_dns_bytes, pkt->orig_to, pkt->addrlen) == 0) - { - /* This is a packet that was sent directly to the virtual dns-server - * - * This means we have to send this query over gnunet - */ - - size_t size = - sizeof (struct GNUNET_MESH_Tunnel *) + - sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) - - sizeof (struct query_packet) + - 1); - struct tunnel_cls *cls_ = GNUNET_malloc (size); - - cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *); - - cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size); - - memcpy (&cls_->dns, dns, - cls_->hdr.size - sizeof (struct GNUNET_MessageHeader)); - GNUNET_SCHEDULER_add_now (send_mesh_query, cls_); - - if (ntohs (pdns->s.qdcount) == 1) + GNUNET_NETWORK_socket_sendto (dnsout, + rr->payload, + rr->payload_length, + (struct sockaddr*) &rr->dst_addr, + salen); + return; + case RP_INTERNET_DNS: + rr->phase = RP_MODIFY; + for (cr = clients_head; NULL != cr; cr = cr->next) { - if (ntohs (pdns->queries[0]->qtype) == 1) - pdns->queries[0]->qtype = htons (28); - else if (ntohs (pdns->queries[0]->qtype) == 28) - pdns->queries[0]->qtype = htons (1); - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "not sending second packet\n"); - goto outfree; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending second packet\n"); - struct dns_pkt *rdns = unparse_dns_packet (pdns); - size_t size = - sizeof (struct GNUNET_MESH_Tunnel *) + - sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) - - sizeof (struct query_packet) + - 1); - struct tunnel_cls *cls_ = GNUNET_malloc (size); - - cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *); - - cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size); - - memcpy (&cls_->dns, rdns, - cls_->hdr.size - sizeof (struct GNUNET_MessageHeader)); - GNUNET_SCHEDULER_add_now (send_mesh_query, cls_); - GNUNET_free (rdns); + if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION)) + GNUNET_array_append (rr->client_wait_list, + rr->client_wait_list_length, + cr); } - - goto outfree; - } - - - /* The query should be sent to the network */ - if (pkt->addrlen == 4) - { - struct sockaddr_in dest; - - memset (&dest, 0, sizeof dest); - dest.sin_port = htons (53); - memcpy (&dest.sin_addr.s_addr, pkt->orig_to, pkt->addrlen); - - GNUNET_NETWORK_socket_sendto (dnsout, dns, - ntohs (pkt->hdr.size) - - sizeof (struct query_packet) + 1, - (struct sockaddr *) &dest, sizeof dest); - } - else if (pkt->addrlen == 16) - { - struct sockaddr_in6 dest; - - memset (&dest, 0, sizeof dest); - dest.sin6_port = htons (53); - memcpy (&dest.sin6_addr, pkt->orig_to, pkt->addrlen); - - GNUNET_NETWORK_socket_sendto (dnsout6, dns, - ntohs (pkt->hdr.size) - - sizeof (struct query_packet) + 1, - (struct sockaddr *) &dest, sizeof dest); - } - -outfree: - free_parsed_dns_packet (pdns); - pdns = NULL; -out: - GNUNET_SERVER_receive_done (client, GNUNET_OK); -} - -static void -read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); - -static void -read_response6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); - -static int -open_port6 () -{ - struct sockaddr_in6 addr; - - dnsout6 = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_DGRAM, 0); - if (dnsout6 == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create socket: %m\n"); - return GNUNET_SYSERR; - } - memset (&addr, 0, sizeof (struct sockaddr_in6)); - - addr.sin6_family = AF_INET6; - int err = GNUNET_NETWORK_socket_bind (dnsout6, - (struct sockaddr *) &addr, - sizeof (struct sockaddr_in6)); - - if (err != GNUNET_OK) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n"); - return GNUNET_SYSERR; + next_phase (rr); + return; + case RP_MODIFY: + rr->phase = RP_RESPONSE_MONITOR; + for (cr = clients_head; NULL != cr; cr = cr->next) + { + if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR)) + GNUNET_array_append (rr->client_wait_list, + rr->client_wait_list_length, + cr); + } + next_phase (rr); + return; + case RP_RESPONSE_MONITOR: + request_done (rr); + break; + case RP_DROP: + cleanup_rr (rr); + break; + default: + GNUNET_break (0); + cleanup_rr (rr); + break; } - - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout6, - &read_response6, NULL); - - return GNUNET_YES; } -static int -open_port () -{ - struct sockaddr_in addr; - - dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0); - if (dnsout == NULL) - return GNUNET_SYSERR; - memset (&addr, 0, sizeof (struct sockaddr_in)); - addr.sin_family = AF_INET; - int err = GNUNET_NETWORK_socket_bind (dnsout, - (struct sockaddr *) &addr, - sizeof (struct sockaddr_in)); +/** + * A client disconnected, clean up after it. + * + * @param cls unused + * @param client handle of client that disconnected + */ +static void +client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) +{ + struct ClientRecord *cr; + struct RequestRecord *rr; + unsigned int i; + unsigned int j; - if (err != GNUNET_OK) + for (cr = clients_head; NULL != cr; cr = cr->next) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n"); - return GNUNET_SYSERR; + if (cr->client == client) + { + GNUNET_SERVER_client_drop (client); + GNUNET_CONTAINER_DLL_remove (clients_head, + clients_tail, + cr); + for (i=0;iclient_wait_list_length) + continue; /* not in use */ + for (j=0;jclient_wait_list_length;j++) + { + if (rr->client_wait_list[j] == cr) + { + rr->client_wait_list[j] = NULL; + next_phase (rr); + } + } + } + GNUNET_free (cr); + return; + } } - - /* Read the port we bound to */ - socklen_t addrlen = sizeof (struct sockaddr_in); - - err = - getsockname (GNUNET_NETWORK_get_fd (dnsout), (struct sockaddr *) &addr, - &addrlen); - - dnsoutport = htons (addr.sin_port); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bound to port %d.\n", dnsoutport); - - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout, - &read_response, NULL); - - return GNUNET_YES; } -static void -handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen, - int r); /** - * Read a response-packet of the UDP-Socket + * Read a DNS response from the (unhindered) UDP-Socket + * + * @param cls socket to read from + * @param tc scheduler context (must be shutdown or read ready) */ static void -read_response6 (void *cls GNUNET_UNUSED, - const struct GNUNET_SCHEDULER_TaskContext *tc) +read_response (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { - struct sockaddr_in6 addr; - socklen_t addrlen = sizeof (addr); - int r; + struct GNUNET_NETWORK_Handle *dnsout = cls; + struct sockaddr_in addr4; + struct sockaddr_in6 addr6; + struct sockaddr *addr; + struct dns_header *dns; + socklen_t addrlen; + struct RequestRecord *rr; + ssize_t r; int len; - if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) + if (dnsout == dnsout4) + { + addrlen = sizeof (struct sockaddr_in); + addr = (struct sockaddr* ) &addr4; + read4_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + dnsout, + &read_response, + dnsout); + } + else + { + addrlen = sizeof (struct sockaddr_in6); + addr = (struct sockaddr* ) &addr6; + read6_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + dnsout, + &read_response, + dnsout); + } + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) return; - memset (&addr, 0, sizeof addr); - #ifndef MINGW - if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout6), FIONREAD, &len)) + if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len)) { - (void) open_port6 (); - return; + /* conservative choice: */ + len = 65536; } #else /* port the code above? */ len = 65536; #endif - unsigned char buf[len]; - struct dns_pkt *dns = (struct dns_pkt *) buf; - - r = GNUNET_NETWORK_socket_recvfrom (dnsout, buf, sizeof (buf), - (struct sockaddr *) &addr, &addrlen); - - if (r < 0) { - (void) open_port6 (); - return; - } - - struct sockaddr *addr_ = GNUNET_malloc (sizeof addr); + unsigned char buf[len]; - memcpy (addr_, &addr, sizeof addr); - handle_response (dns, addr_, 4, r); - - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout6, - &read_response6, NULL); + memset (addr, 0, addrlen); + r = GNUNET_NETWORK_socket_recvfrom (dnsout, + buf, sizeof (buf), + addr, &addrlen); + if (-1 == r) + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom"); + return; + } + if (sizeof (struct dns_header) > r) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Received DNS response that is too small (%u bytes)"), + r); + return; + } + dns = (struct dns_header *) buf; + rr = &requests[dns->id]; + if (rr->phase != RP_INTERNET_DNS) + { + /* unexpected / bogus reply */ + GNUNET_STATISTICS_update (stats, + gettext_noop ("# External DNS response discarded (no matching request)"), + 1, GNUNET_NO); + return; + } + GNUNET_free_non_null (rr->payload); + rr->payload = GNUNET_malloc (len); + memcpy (rr->payload, buf, len); + rr->payload_length = len; + next_phase (rr); + } } + /** - * Read a response-packet of the UDP-Socket - */ -static void -read_response (void *cls GNUNET_UNUSED, - const struct GNUNET_SCHEDULER_TaskContext *tc) + * Open source port for sending DNS request on IPv4. + * + * @return GNUNET_OK on success + */ +static int +open_port4 () { struct sockaddr_in addr; - socklen_t addrlen = sizeof (addr); - int r; - int len; + socklen_t addrlen; - if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) - return; + dnsout4 = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0); + if (dnsout4 == NULL) + return GNUNET_SYSERR; - memset (&addr, 0, sizeof addr); + memset (&addr, 0, sizeof (struct sockaddr_in)); + addr.sin_family = AF_INET; + int err = GNUNET_NETWORK_socket_bind (dnsout4, + (struct sockaddr *) &addr, + sizeof (struct sockaddr_in)); -#ifndef MINGW - if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len)) + if (err != GNUNET_OK) { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl"); - unhijack (dnsoutport); - if (GNUNET_YES == open_port ()) - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL); - return; + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Could not bind to any port: %s\n"), + STRERROR (errno)); + GNUNET_NETWORK_socket_close (dnsout4); + dnsout4 = NULL; + return GNUNET_SYSERR; } -#else - /* port the code above? */ - len = 65536; -#endif - - unsigned char buf[len]; - struct dns_pkt *dns = (struct dns_pkt *) buf; - - r = GNUNET_NETWORK_socket_recvfrom (dnsout, buf, sizeof (buf), - (struct sockaddr *) &addr, &addrlen); - if (r < 0) + /* Read the port we bound to */ + addrlen = sizeof (struct sockaddr_in); + if (0 != getsockname (GNUNET_NETWORK_get_fd (dnsout4), + (struct sockaddr *) &addr, + &addrlen)) { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom"); - unhijack (dnsoutport); - if (GNUNET_YES == open_port ()) - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL); - return; + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Could not determine port I got: %s\n"), + STRERROR (errno)); + GNUNET_NETWORK_socket_close (dnsout4); + dnsout4 = NULL; + return GNUNET_SYSERR; } + dnsoutport = htons (addr.sin_port); - struct sockaddr *addr_ = GNUNET_malloc (sizeof addr); - - memcpy (addr_, &addr, sizeof addr); - handle_response (dns, addr_, 4, r); - - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout, - &read_response, NULL); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("GNUnet DNS will exit on source port %u\n"), + (unsigned int) dnsoutport); + read4_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + dnsout4, + &read_response, dnsout4); + return GNUNET_OK; } -static void -handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen, - int r) -{ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n", - ntohs (dns->s.id)); +/** + * Open source port for sending DNS request on IPv6. Should be + * called AFTER open_port4. + * + * @return GNUNET_OK on success + */ +static int +open_port6 () +{ + struct sockaddr_in6 addr; + socklen_t addrlen; - if (query_states[dns->s.id].valid == GNUNET_YES) + dnsout6 = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_DGRAM, 0); + if (dnsout6 == NULL) { - if (query_states[dns->s.id].tunnel != NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Answer to query %d for a remote peer!\n", ntohs (dns->s.id)); - /* This response should go through a tunnel */ - uint32_t *c = - GNUNET_malloc (4 + sizeof (struct GNUNET_MESH_Tunnel *) + r); - *c = r; - struct GNUNET_MESH_Tunnel **t = (struct GNUNET_MESH_Tunnel **) (c + 1); - - *t = query_states[dns->s.id].tunnel; - memcpy (t + 1, dns, r); - struct tunnel_state *s = - GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel); - if (NULL == s->th) - { - s->th = - GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel, - GNUNET_YES, 32, - GNUNET_TIME_UNIT_MINUTES, NULL, - r + - sizeof (struct - GNUNET_MessageHeader), - mesh_send_response, c); - } - else - { - struct tunnel_notify_queue *element = - GNUNET_malloc (sizeof (struct tunnel_notify_queue)); - element->cls = c; - element->len = r + sizeof (struct GNUNET_MessageHeader); - element->cb = mesh_send_response; + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Could not create IPv6 socket: %s\n"), + STRERROR (errno)); + return GNUNET_SYSERR; + } + memset (&addr, 0, sizeof (struct sockaddr_in6)); + addr.sin6_family = AF_INET6; + addr.sin6_port = htons (dnsoutport); + int err = GNUNET_NETWORK_socket_bind (dnsout6, + (struct sockaddr *) &addr, + sizeof (struct sockaddr_in6)); - GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, element); - } - } - else + if (err != GNUNET_OK) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Could not bind to port %u: %s\n"), + (unsigned int) dnsoutport, + STRERROR (errno)); + GNUNET_NETWORK_socket_close (dnsout6); + dnsout6 = NULL; + return GNUNET_SYSERR; + } + if (0 == dnsoutport) + { + addrlen = sizeof (struct sockaddr_in6); + if (0 != getsockname (GNUNET_NETWORK_get_fd (dnsout6), + (struct sockaddr *) &addr, + &addrlen)) { - query_states[dns->s.id].valid = GNUNET_NO; - - size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */ - struct answer_packet_list *answer = - GNUNET_malloc (len + sizeof (struct answer_packet_list) - - (sizeof (struct answer_packet))); - answer->pkt.hdr.type = - htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); - answer->pkt.hdr.size = htons (len); - answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP; - answer->pkt.addrlen = addrlen; - if (addrlen == 16) - { - struct sockaddr_in6 *addr_ = (struct sockaddr_in6 *) addr; - - memcpy (answer->pkt.from, &addr_->sin6_addr, addrlen); - memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, addrlen); - } - else if (addrlen == 4) - { - struct sockaddr_in *addr_ = (struct sockaddr_in *) addr; - - memcpy (answer->pkt.from, &addr_->sin_addr.s_addr, addrlen); - memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, addrlen); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "addrlen = %d\n", addrlen); - GNUNET_assert (0); - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending answer with addrlen = %d\n", - addrlen); - answer->pkt.dst_port = query_states[dns->s.id].local_port; - memcpy (answer->pkt.data, dns, r); - answer->client = query_states[dns->s.id].client; - - GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); - - if (server_notify == NULL) - server_notify = - GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client, - len, - GNUNET_TIME_UNIT_FOREVER_REL, - &send_answer, NULL); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Could not determine port I got: %s\n"), + STRERROR (errno)); + GNUNET_NETWORK_socket_close (dnsout6); + dnsout6 = NULL; + return GNUNET_SYSERR; } } - GNUNET_free (addr); + dnsoutport = htons (addr.sin6_port); + read6_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + dnsout6, + &read_response, dnsout6); + return GNUNET_YES; } /** - * Task run during shutdown. + * We got a new client. Make sure all new DNS requests pass by its desk. * * @param cls unused - * @param tc unused + * @param client the new client + * @param message the init message (unused) */ static void -cleanup_task (void *cls GNUNET_UNUSED, - const struct GNUNET_SCHEDULER_TaskContext *tc) +handle_client_init (void *cls GNUNET_UNUSED, + struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { - GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)); + struct ClientRecord *cr; + const struct GNUNET_DNS_Register *reg = (const struct GNUNET_DNS_Register*) message; - unhijack (dnsoutport); - GNUNET_DHT_disconnect (dht); - GNUNET_MESH_disconnect (mesh_handle); + cr = GNUNET_malloc (sizeof (struct ClientRecord)); + cr->client = client; + cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags); + GNUNET_SERVER_client_keep (client); + GNUNET_CONTAINER_DLL_insert (clients_head, + clients_tail, + cr); + GNUNET_SERVER_notification_context_add (nc, client); + GNUNET_SERVER_receive_done (client, GNUNET_OK); } + /** - * @brief Create a port-map from udp and tcp redirects + * We got a response from a client. * - * @param udp_redirects - * @param tcp_redirects - * - * @return + * @param cls unused + * @param client the client + * @param message the response */ -static uint64_t -get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects) +static void +handle_client_response (void *cls GNUNET_UNUSED, + struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { - uint64_t ret = 0; - char *cpy, *hostname, *redirect; - int local_port; - unsigned int count = 0; - - cpy = NULL; - if (NULL != udp_redirects) + const struct GNUNET_DNS_Response *resp; + struct RequestRecord *rr; + unsigned int i; + uint16_t msize; + uint16_t off; + + msize = ntohs (message->size); + if (msize < sizeof (struct GNUNET_DNS_Response)) { - cpy = GNUNET_strdup (udp_redirects); - for (redirect = strtok (cpy, " "); redirect != NULL; - redirect = strtok (NULL, " ")) - { - if (NULL == (hostname = strstr (redirect, ":"))) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Warning: option %s is not formatted correctly!\n", - redirect); - continue; - } - hostname[0] = '\0'; - local_port = atoi (redirect); - if (!((local_port > 0) && (local_port < 65536))) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Warning: %s is not a correct port.", redirect); - - ret |= (0xFFFF & htons (local_port)); - ret <<= 16; - count++; - - if (count > 4) - { - ret = 0; - goto out; - } - } - GNUNET_free (cpy); - cpy = NULL; + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; } - - if (NULL != tcp_redirects) + resp = (const struct GNUNET_DNS_Response*) message; + off = (uint16_t) resp->request_id; + rr = &requests[off]; + if (rr->request_id != resp->request_id) + { + GNUNET_STATISTICS_update (stats, + gettext_noop ("# Client response discarded (no matching request)"), + 1, GNUNET_NO); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + for (i=0;iclient_wait_list_length;i++) { - cpy = GNUNET_strdup (tcp_redirects); - for (redirect = strtok (cpy, " "); redirect != NULL; - redirect = strtok (NULL, " ")) + if (NULL == rr->client_wait_list[i]) + continue; + if (rr->client_wait_list[i]->client != client) + continue; + rr->client_wait_list[i] = NULL; + switch (ntohl (resp->drop_flag)) { - if (NULL == (hostname = strstr (redirect, ":"))) + case 0: /* drop */ + rr->phase = RP_DROP; + break; + case 1: /* no change */ + break; + case 2: /* update */ + msize -= sizeof (struct GNUNET_DNS_Response); + if ( (sizeof (struct dns_header) > msize) || + (RP_REQUEST_MONITOR == rr->phase) || + (RP_RESPONSE_MONITOR == rr->phase) ) { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Warning: option %s is not formatted correctly!\n", - redirect); - continue; + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + next_phase (rr); + return; } - hostname[0] = '\0'; - local_port = atoi (redirect); - if (!((local_port > 0) && (local_port < 65536))) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Warning: %s is not a correct port.", redirect); - - ret |= (0xFFFF & htons (local_port)); - ret <<= 16; - count++; - - if (count > 4) + GNUNET_free_non_null (rr->payload); +#if DEBUG_DNS + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + _("Changing DNS reply according to client specifications\n")); +#endif + rr->payload = GNUNET_malloc (msize); + rr->payload_length = msize; + memcpy (rr->payload, &resp[1], msize); + if (rr->phase == RP_QUERY) { - ret = 0; - goto out; + /* clear wait list, we're moving to MODIFY phase next */ + GNUNET_array_grow (rr->client_wait_list, + rr->client_wait_list_length, + 0); } + break; } - GNUNET_free (cpy); - cpy = NULL; - } - -out: - GNUNET_free_non_null (cpy); - return ret; -} - -static void -publish_name (const char *name, uint64_t ports, uint32_t service_type, - struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key) -{ - size_t size = sizeof (struct GNUNET_DNS_Record); - struct GNUNET_DNS_Record data; - - memset (&data, 0, size); - - data.purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature)); - data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD; - - GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n", - *((unsigned long long *) &data.service_descriptor)); - - data.service_type = service_type; - data.ports = ports; - - GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer); - - data.expiration_time = - GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute - (GNUNET_TIME_relative_multiply - (GNUNET_TIME_UNIT_HOURS, 2))); - - /* Sign the block */ - if (GNUNET_OK != - GNUNET_CRYPTO_rsa_sign (my_private_key, &data.purpose, &data.signature)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n"); - return; + next_phase (rr); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; } - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x, size = %d\n", - *((unsigned int *) &data.service_descriptor), size); - - GNUNET_DHT_put (dht, &data.service_descriptor, - 5 /* DEFAULT_PUT_REPLICATION */ , - GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_DNS, size, - (char *) &data, - GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), - GNUNET_TIME_UNIT_MINUTES, NULL, NULL); + /* odd, client was not on our list for the request, that ought + to be an error */ + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); } /** - * @brief Publishes the record defined by the section section + * Functions with this signature are called whenever a complete + * message is received by the tokenizer from the DNS hijack process. * * @param cls closure - * @param section the current section + * @param client identification of the client + * @param message the actual message, a DNS request we should handle */ static void -publish_iterate (void *cls GNUNET_UNUSED, const char *section) +process_helper_messages (void *cls GNUNET_UNUSED, void *client, + const struct GNUNET_MessageHeader *message) { - char *udp_redirects; - char *tcp_redirects; - char *alternative_names; - char *alternative_name; - char *keyfile; - - if ((strlen (section) < 8) || - (0 != strcmp (".gnunet.", section + (strlen (section) - 8)))) - return; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section); - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS", - &udp_redirects)) - udp_redirects = NULL; - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS", - &tcp_redirects)) - tcp_redirects = NULL; - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY", - &keyfile)) + uint16_t msize; + const struct tun_header *tun; + const struct ip4_header *ip4; + const struct ip6_header *ip6; + const struct udp_packet *udp; + const struct dns_header *dns; + struct RequestRecord *rr; + struct sockaddr_in *srca4; + struct sockaddr_in6 *srca6; + struct sockaddr_in *dsta4; + struct sockaddr_in6 *dsta6; + + msize = ntohs (message->size); + if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct tun_header) + sizeof (struct ip4_header)) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n"); - if (keyfile != NULL) - GNUNET_free (keyfile); + /* non-IP packet received on TUN!? */ + GNUNET_break (0); return; } - - struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key = - GNUNET_CRYPTO_rsa_key_create_from_file (keyfile); - GNUNET_free (keyfile); - GNUNET_assert (my_private_key != NULL); - - uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects); - uint32_t service_type = 0; - - if (NULL != udp_redirects) - service_type = GNUNET_DNS_SERVICE_TYPE_UDP; - - if (NULL != tcp_redirects) - service_type |= GNUNET_DNS_SERVICE_TYPE_TCP; - - service_type = htonl (service_type); - - - publish_name (section, ports, service_type, my_private_key); - if (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, section, "ALTERNATIVE_NAMES", - &alternative_names)) + msize -= sizeof (struct GNUNET_MessageHeader); + tun = (const struct tun_header *) &message[1]; + msize -= sizeof (struct tun_header); + switch (ntohs (tun->proto)) { - for (alternative_name = strtok (alternative_names, " "); - alternative_name != NULL; alternative_name = strtok (NULL, " ")) + case ETH_P_IPV4: + ip4 = (const struct ip4_header *) &tun[1]; + if ( (msize < sizeof (struct ip4_header)) || + (ip4->version != IPVERSION) || + (ip4->header_length != sizeof (struct ip4_header) / 4) || + (ntohs(ip4->total_length) != msize) || + (ip4->protocol != IPPROTO_UDP) ) { - char *altname = - alloca (strlen (alternative_name) + strlen (section) + 1 + 1); - strcpy (altname, alternative_name); - strcpy (altname + strlen (alternative_name) + 1, section); - altname[strlen (alternative_name)] = '.'; - - publish_name (altname, ports, service_type, my_private_key); + /* non-IP/UDP packet received on TUN (or with options) */ + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Received malformed IPv4-UDP packet on TUN interface.\n")); + return; + } + udp = (const struct udp_packet*) &ip4[1]; + msize -= sizeof (struct ip4_header); + break; + case ETH_P_IPV6: + ip6 = (const struct ip6_header *) &tun[1]; + if ( (msize < sizeof (struct ip6_header)) || + (ip6->version != 6) || + (ntohs (ip6->payload_length) != msize) || + (ip6->next_header != IPPROTO_UDP) ) + { + /* non-IP/UDP packet received on TUN (or with extensions) */ + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Received malformed IPv6-UDP packet on TUN interface.\n")); + return; } - GNUNET_free (alternative_names); + udp = (const struct udp_packet*) &ip6[1]; + msize -= sizeof (struct ip6_header); + break; + default: + /* non-IP packet received on TUN!? */ + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Got non-IP packet with %u bytes and protocol %u from TUN\n"), + (unsigned int) msize, + ntohs (tun->proto)); + return; } - GNUNET_CRYPTO_rsa_key_free (my_private_key); - GNUNET_free_non_null (udp_redirects); - GNUNET_free_non_null (tcp_redirects); -} - -/** - * Publish a DNS-record in the DHT. - */ -static void -publish_names (void *cls GNUNET_UNUSED, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + if (msize <= sizeof (struct udp_packet) + sizeof (struct dns_header)) + { + /* non-DNS packet received on TUN, ignore */ + GNUNET_STATISTICS_update (stats, + gettext_noop ("# Non-DNS UDP packet received via TUN interface"), + 1, GNUNET_NO); return; - - GNUNET_CONFIGURATION_iterate_sections (cfg, &publish_iterate, NULL); - - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS, &publish_names, NULL); + } + msize -= sizeof (struct udp_packet); + dns = (const struct dns_header*) &udp[1]; + rr = &requests[dns->id]; + + /* clean up from previous request */ + GNUNET_free_non_null (rr->payload); + rr->payload = NULL; + GNUNET_array_grow (rr->client_wait_list, + rr->client_wait_list_length, + 0); + + /* setup new request */ + rr->phase = RP_INIT; + if (ip4->version == IPVERSION) + { + srca4 = (struct sockaddr_in*) &rr->src_addr; + dsta4 = (struct sockaddr_in*) &rr->dst_addr; + memset (srca4, 0, sizeof (struct sockaddr_in)); + memset (dsta4, 0, sizeof (struct sockaddr_in)); + srca4->sin_family = AF_INET; + dsta4->sin_family = AF_INET; + srca4->sin_addr = ip4->source_address; + dsta4->sin_addr = ip4->destination_address; + srca4->sin_port = udp->spt; + dsta4->sin_port = udp->dpt; +#if HAVE_SOCKADDR_IN_SIN_LEN + srca4->sin_len = sizeof (sizeof (struct sockaddr_in)); + dsta4->sin_len = sizeof (sizeof (struct sockaddr_in)); +#endif + } + else /* ipv6 */ + { + srca6 = (struct sockaddr_in6*) &rr->src_addr; + dsta6 = (struct sockaddr_in6*) &rr->dst_addr; + memset (srca6, 0, sizeof (struct sockaddr_in6)); + memset (dsta6, 0, sizeof (struct sockaddr_in6)); + srca6->sin6_family = AF_INET6; + dsta6->sin6_family = AF_INET6; + srca6->sin6_addr = ip6->source_address; + dsta6->sin6_addr = ip6->destination_address; + srca6->sin6_port = udp->spt; + dsta6->sin6_port = udp->dpt; +#if HAVE_SOCKADDR_IN_SIN_LEN + srca6->sin6_len = sizeof (sizeof (struct sockaddr_in6)); + dsta6->sin6_len = sizeof (sizeof (struct sockaddr_in6)); +#endif + } + rr->payload = GNUNET_malloc (msize); + rr->payload_length = msize; + memcpy (rr->payload, dns, msize); + rr->request_id = dns->id | (request_id_gen << 16); + request_id_gen++; + + GNUNET_STATISTICS_update (stats, + gettext_noop ("# DNS requests received via TUN interface"), + 1, GNUNET_NO); + /* start request processing state machine */ + next_phase (rr); } + /** * @param cls closure * @param server the initialized server @@ -1683,51 +1178,102 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, { static const struct GNUNET_SERVER_MessageHandler handlers[] = { /* callback, cls, type, size */ - {&receive_query, NULL, GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS, 0}, - {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK, - sizeof (struct GNUNET_MessageHeader)}, + {&handle_client_init, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT, + sizeof (struct GNUNET_DNS_Register)}, + {&handle_client_response, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 0}, {NULL, NULL, 0, 0} }; + char port_s[6]; + char *ifc_name; + char *ipv4addr; + char *ipv4mask; + char *ipv6addr; + char *ipv6prefix; - static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = { - {receive_mesh_query, GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS, 0}, - {receive_mesh_answer, GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS, 0}, - {NULL, 0, 0} - }; - - static GNUNET_MESH_ApplicationType apptypes[] = { - GNUNET_APPLICATION_TYPE_END, - GNUNET_APPLICATION_TYPE_END - }; + cfg = cfg_; + stats = GNUNET_STATISTICS_create ("dns", cfg); + nc = GNUNET_SERVER_notification_context_create (server, 1); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task, + cls); + if (GNUNET_YES == + GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT")) + { + if ( (GNUNET_OK != open_port4 ()) && + (GNUNET_OK != open_port6 ()) ) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Failed to open any port to provide DNS exit\n")); + GNUNET_SCHEDULER_shutdown (); + return; + } + } - if (GNUNET_YES != open_port6 ()) + helper_argv[0] = GNUNET_strdup ("gnunet-dns"); + if (GNUNET_SYSERR == + GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name)) { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "No entry 'IFNAME' in configuration!\n"); GNUNET_SCHEDULER_shutdown (); return; } - - if (GNUNET_YES != open_port ()) + helper_argv[1] = ifc_name; + if ( (GNUNET_SYSERR == + GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6ADDR", + &ipv6addr)) ) { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "No entry 'IPV6ADDR' in configuration!\n"); GNUNET_SCHEDULER_shutdown (); return; } + helper_argv[2] = ipv6addr; + if (GNUNET_SYSERR == + GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6PREFIX", + &ipv6prefix)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "No entry 'IPV6PREFIX' in configuration!\n"); + GNUNET_SCHEDULER_shutdown (); + return; + } + helper_argv[3] = ipv6prefix; - if (GNUNET_YES == - GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT")) - apptypes[0] = GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER; - mesh_handle = - GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, clean_tunnel, - mesh_handlers, apptypes); - - cfg = cfg_; - dht = GNUNET_DHT_connect (cfg, 1024); - GNUNET_SCHEDULER_add_now (publish_names, NULL); + if (GNUNET_SYSERR == + GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4ADDR", + &ipv4addr)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "No entry 'IPV4ADDR' in configuration!\n"); + GNUNET_SCHEDULER_shutdown (); + return; + } + helper_argv[4] = ipv4addr; + if (GNUNET_SYSERR == + GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK", + &ipv4mask)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "No entry 'IPV4MASK' in configuration!\n"); + GNUNET_SCHEDULER_shutdown (); + return; + } + helper_argv[5] = ipv4mask; + GNUNET_snprintf (port_s, + sizeof (port_s), + "%u", + (unsigned int) dnsoutport); + helper_argv[6] = GNUNET_strdup (port_s); + helper_argv[7] = NULL; + hijacker = GNUNET_HELPER_start ("gnunet-helper-dns", + helper_argv, + &process_helper_messages, + NULL); GNUNET_SERVER_add_handlers (server, handlers); GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL); - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task, - cls); } + /** * The main function for the dns service. * @@ -1742,3 +1288,6 @@ main (int argc, char *const *argv) GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1; } + + +/* end of gnunet-service-dns_new.c */ diff --git a/src/dns/gnunet-service-dns_new.c b/src/dns/gnunet-service-dns_new.c deleted file mode 100644 index 0bb7fb21c..000000000 --- a/src/dns/gnunet-service-dns_new.c +++ /dev/null @@ -1,1293 +0,0 @@ -/* - This file is part of GNUnet. - (C) 2012 Christian Grothoff (and other contributing authors) - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. -*/ - -/** - * @file dns/gnunet-service-dns_new.c - * @author Christian Grothoff - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_constants.h" -#include "gnunet_protocols.h" -#include "gnunet_signatures.h" -#include "dns_new.h" -#include "gnunet_dns_service-new.h" -#include "gnunet_statistics_service.h" -#include "tcpip_tun.h" - -#ifndef IPVERSION -#define IPVERSION 4 -#endif - -/** - * Phases each request goes through. - */ -enum RequestPhase -{ - /** - * Request has just been received. - */ - RP_INIT, - - /** - * Showing the request to all monitor clients. If - * client list is empty, will enter QUERY phase. - */ - RP_REQUEST_MONITOR, - - /** - * Showing the request to PRE-RESOLUTION clients to find an answer. - * If client list is empty, will trigger global DNS request. - */ - RP_QUERY, - - /** - * Global Internet query is now pending. - */ - RP_INTERNET_DNS, - - /** - * Client (or global DNS request) has resulted in a response. - * Forward to all POST-RESOLUTION clients. If client list is empty, - * will enter RESPONSE_MONITOR phase. - */ - RP_MODIFY, - - /** - * Showing the request to all monitor clients. If - * client list is empty, give the result to the hijacker (and be done). - */ - RP_RESPONSE_MONITOR, - - /** - * Some client has told us to drop the request. - */ - RP_DROP -}; - - -/** - * Entry we keep for each client. - */ -struct ClientRecord -{ - /** - * Kept in doubly-linked list. - */ - struct ClientRecord *next; - - /** - * Kept in doubly-linked list. - */ - struct ClientRecord *prev; - - /** - * Handle to the client. - */ - struct GNUNET_SERVER_Client *client; - - /** - * Flags for the client. - */ - enum GNUNET_DNS_Flags flags; - -}; - - -/** - * Entry we keep for each active request. - */ -struct RequestRecord -{ - - /** - * List of clients that still need to see this request (each entry - * is set to NULL when the client is done). - */ - struct ClientRecord **client_wait_list; - - /** - * Payload of the UDP packet (the UDP payload), can be either query - * or already the response. - */ - char *payload; - - /** - * Source address of the original request (for sending response). - */ - struct sockaddr_storage src_addr; - - /** - * Destination address of the original request (for potential use as exit). - */ - struct sockaddr_storage dst_addr; - - /** - * ID of this request, also basis for hashing. Lowest 16 bit will - * be our message ID when doing a global DNS request and our index - * into the 'requests' array. - */ - uint64_t request_id; - - /** - * Number of bytes in payload. - */ - size_t payload_length; - - /** - * Length of the client wait list. - */ - unsigned int client_wait_list_length; - - /** - * In which phase this this request? - */ - enum RequestPhase phase; - -}; - - -/** - * The IPv4 UDP-Socket through which DNS-Resolves will be sent if they are not to be - * sent through gnunet. The port of this socket will not be hijacked. - */ -static struct GNUNET_NETWORK_Handle *dnsout4; - -/** - * The IPv6 UDP-Socket through which DNS-Resolves will be sent if they are not to be - * sent through gnunet. The port of this socket will not be hijacked. - */ -static struct GNUNET_NETWORK_Handle *dnsout6; - -/** - * Task for reading from dnsout4. - */ -static GNUNET_SCHEDULER_TaskIdentifier read4_task; - -/** - * Task for reading from dnsout6. - */ -static GNUNET_SCHEDULER_TaskIdentifier read6_task; - -/** - * The port bound to the socket dnsout (and/or dnsout6). We always (try) to bind - * both sockets to the same port. - */ -static uint16_t dnsoutport; - -/** - * The configuration to use - */ -static const struct GNUNET_CONFIGURATION_Handle *cfg; - -/** - * Statistics. - */ -static struct GNUNET_STATISTICS_Handle *stats; - -/** - * Handle to DNS hijacker helper process ("gnunet-helper-dns"). - */ -static struct GNUNET_HELPER_Handle *hijacker; - -/** - * Command-line arguments we are giving to the hijacker process. - */ -static char *helper_argv[8]; - -/** - * Head of DLL of clients we consult. - */ -static struct ClientRecord *clients_head; - -/** - * Tail of DLL of clients we consult. - */ -static struct ClientRecord *clients_tail; - -/** - * Our notification context. - */ -static struct GNUNET_SERVER_NotificationContext *nc; - -/** - * Array of all open requests. - */ -static struct RequestRecord requests[UINT16_MAX + 1]; - -/** - * Generator for unique request IDs. - */ -static uint64_t request_id_gen; - - -/** - * We're done processing a DNS request, free associated memory. - * - * @param rr request to clean up - */ -static void -cleanup_rr (struct RequestRecord *rr) -{ - GNUNET_free_non_null (rr->payload); - rr->payload = NULL; - rr->payload_length = 0; - GNUNET_array_grow (rr->client_wait_list, - rr->client_wait_list_length, - 0); -} - - -/** - * Task run during shutdown. - * - * @param cls unused - * @param tc unused - */ -static void -cleanup_task (void *cls GNUNET_UNUSED, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - unsigned int i; - - GNUNET_HELPER_stop (hijacker); - hijacker = NULL; - for (i=0;i<8;i++) - GNUNET_free_non_null (helper_argv[i]); - if (NULL != dnsout4) - { - GNUNET_NETWORK_socket_close (dnsout4); - dnsout4 = NULL; - } - if (GNUNET_SCHEDULER_NO_TASK != read4_task) - { - GNUNET_SCHEDULER_cancel (read4_task); - read4_task = GNUNET_SCHEDULER_NO_TASK; - } - if (NULL != dnsout6) - { - GNUNET_NETWORK_socket_close (dnsout6); - dnsout6 = NULL; - } - if (GNUNET_SCHEDULER_NO_TASK != read6_task) - { - GNUNET_SCHEDULER_cancel (read6_task); - read6_task = GNUNET_SCHEDULER_NO_TASK; - } - for (i=0;i<65536;i++) - cleanup_rr (&requests[i]); - GNUNET_SERVER_notification_context_destroy (nc); - nc = NULL; - if (stats != NULL) - { - GNUNET_STATISTICS_destroy (stats, GNUNET_YES); - stats = NULL; - } -} - - -/** - * We're done with some request, finish processing. - * - * @param rr request send to the network or just clean up. - */ -static void -request_done (struct RequestRecord *rr) -{ - struct GNUNET_MessageHeader *hdr; - size_t reply_len; - uint16_t spt; - uint16_t dpt; - - GNUNET_array_grow (rr->client_wait_list, - rr->client_wait_list_length, - 0); - if (RP_RESPONSE_MONITOR != rr->phase) - { - /* no response, drop */ - cleanup_rr (rr); - return; - } - - /* send response via hijacker */ - reply_len = sizeof (struct GNUNET_MessageHeader); - reply_len += sizeof (struct tun_header); - switch (rr->src_addr.ss_family) - { - case AF_INET: - reply_len += sizeof (struct ip4_header); - break; - case AF_INET6: - reply_len += sizeof (struct ip6_header); - break; - default: - GNUNET_break (0); - cleanup_rr (rr); - return; - } - reply_len += sizeof (struct udp_packet); - reply_len += rr->payload_length; - if (reply_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) - { - /* response too big, drop */ - GNUNET_break (0); /* how can this be? */ - cleanup_rr(rr); - return; - } - { - char buf[reply_len]; - size_t off; - uint32_t udp_crc_sum; - - /* first, GNUnet message header */ - hdr = (struct GNUNET_MessageHeader*) buf; - hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER); - hdr->size = htons ((uint16_t) reply_len); - off = sizeof (struct GNUNET_MessageHeader); - - /* first, TUN header */ - { - struct tun_header tun; - - tun.flags = htons (0); - if (rr->src_addr.ss_family == AF_INET) - tun.proto = htons (ETH_P_IPV4); - else - tun.proto = htons (ETH_P_IPV6); - memcpy (&buf[off], &tun, sizeof (struct tun_header)); - off += sizeof (struct tun_header); - } - - /* now IP header */ - udp_crc_sum = 0; - switch (rr->src_addr.ss_family) - { - case AF_INET: - { - struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr; - struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr; - struct ip4_header ip; - - spt = dst->sin_port; - dpt = src->sin_port; - ip.header_length = sizeof (struct ip4_header) / 4; - ip.version = IPVERSION; /* aka 4 */ - ip.diff_serv = 0; - ip.total_length = htons ((uint16_t) reply_len - off); - ip.identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, - 65536); - ip.flags = 0; - ip.fragmentation_offset = 0; - ip.ttl = 255; /* or lower? */ - ip.protocol = IPPROTO_UDP; - ip.checksum = 0; /* checksum is optional */ - ip.source_address = dst->sin_addr; - ip.destination_address = src->sin_addr; - ip.checksum = GNUNET_CRYPTO_crc16_n (&ip, sizeof (ip)); - - udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, - &ip.source_address, - sizeof (struct in_addr) * 2); - { - uint16_t tmp; - - tmp = htons (IPPROTO_UDP); - udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, - &tmp, - sizeof (uint16_t)); - tmp = htons (rr->payload_length + sizeof (struct udp_packet)); - udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, - &tmp, - sizeof (uint16_t)); - } - memcpy (&buf[off], &ip, sizeof (ip)); - off += sizeof (ip); - } - break; - case AF_INET6: - { - struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr; - struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr; - struct ip6_header ip; - - spt = dst->sin6_port; - dpt = src->sin6_port; - ip.traffic_class_h = 0; - ip.version = 6; /* is there a named constant? I couldn't find one */ - ip.traffic_class_l = 0; - ip.flow_label = 0; - ip.payload_length = htons ((uint16_t) reply_len); - ip.next_header = IPPROTO_UDP; - ip.hop_limit = 255; /* or lower? */ - ip.source_address = dst->sin6_addr; - ip.destination_address = src->sin6_addr; - udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, - &ip.source_address, - sizeof (struct in6_addr) * 2); - { - uint32_t tmp; - - tmp = htons (rr->payload_length + sizeof (struct udp_packet)); - udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, - &tmp, - sizeof (uint32_t)); - tmp = htons (IPPROTO_UDP); - udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, - &tmp, - sizeof (uint32_t)); - } - memcpy (&buf[off], &ip, sizeof (ip)); - off += sizeof (ip); - } - break; - default: - GNUNET_assert (0); - } - - /* now UDP header */ - { - struct udp_packet udp; - - udp.spt = spt; - udp.dpt = dpt; - udp.len = htons (reply_len - off); - udp.crc = 0; - udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, - &udp, - sizeof (udp)); - udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, - rr->payload, - rr->payload_length); - udp.crc = GNUNET_CRYPTO_crc16_finish (udp_crc_sum); - memcpy (&buf[off], &udp, sizeof (udp)); - off += sizeof (udp); - } - /* now DNS payload */ - { - memcpy (&buf[off], rr->payload, rr->payload_length); - off += rr->payload_length; - } - /* final checks & sending */ - GNUNET_assert (off == reply_len); - GNUNET_HELPER_send (hijacker, - hdr, - GNUNET_YES, - NULL, NULL); - GNUNET_STATISTICS_update (stats, - gettext_noop ("# DNS requests answered via TUN interface"), - 1, GNUNET_NO); - } - /* clean up, we're done */ - cleanup_rr (rr); -} - - -/** - * Show the payload of the given request record to the client - * (and wait for a response). - * - * @param rr request to send to client - * @param client client to send the response to - */ -static void -send_request_to_client (struct RequestRecord *rr, - struct GNUNET_SERVER_Client *client) -{ - char buf[sizeof (struct GNUNET_DNS_Request) + rr->payload_length]; - struct GNUNET_DNS_Request *req; - - if (sizeof (buf) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) - { - GNUNET_break (0); - cleanup_rr (rr); - return; - } - req = (struct GNUNET_DNS_Request*) buf; - req->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST); - req->header.size = htons (sizeof (buf)); - req->reserved = htonl (0); - req->request_id = rr->request_id; - memcpy (&req[1], rr->payload, rr->payload_length); - GNUNET_SERVER_notification_context_unicast (nc, - client, - &req->header, - GNUNET_NO); -} - - -/** - * A client has completed its processing for this - * request. Move on. - * - * @param rr request to process further - */ -static void -next_phase (struct RequestRecord *rr) -{ - struct ClientRecord *cr; - int nz; - unsigned int j; - struct GNUNET_NETWORK_Handle *dnsout; - socklen_t salen; - - if (rr->phase == RP_DROP) - { - cleanup_rr (rr); - return; - } - nz = -1; - for (j=0;jclient_wait_list_length;j++) - { - if (NULL != rr->client_wait_list[j]) - { - nz = (int) j; - break; - } - } - if (-1 != nz) - { - send_request_to_client (rr, rr->client_wait_list[nz]->client); - return; - } - /* done with current phase, advance! */ - switch (rr->phase) - { - case RP_INIT: - rr->phase = RP_REQUEST_MONITOR; - for (cr = clients_head; NULL != cr; cr = cr->next) - { - if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR)) - GNUNET_array_append (rr->client_wait_list, - rr->client_wait_list_length, - cr); - } - next_phase (rr); - return; - case RP_REQUEST_MONITOR: - rr->phase = RP_QUERY; - for (cr = clients_head; NULL != cr; cr = cr->next) - { - if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION)) - GNUNET_array_append (rr->client_wait_list, - rr->client_wait_list_length, - cr); - } - next_phase (rr); - return; - case RP_QUERY: - rr->phase = RP_INTERNET_DNS; - switch (rr->dst_addr.ss_family) - { - case AF_INET: - dnsout = dnsout4; - salen = sizeof (struct ip4_header); - break; - case AF_INET6: - dnsout = dnsout6; - salen = sizeof (struct ip6_header); - break; - default: - GNUNET_break (0); - cleanup_rr (rr); - return; - } - if (NULL == dnsout) - { - GNUNET_STATISTICS_update (stats, - gettext_noop ("# DNS exit failed (address family not supported)"), - 1, GNUNET_NO); - cleanup_rr (rr); - return; - } - GNUNET_NETWORK_socket_sendto (dnsout, - rr->payload, - rr->payload_length, - (struct sockaddr*) &rr->dst_addr, - salen); - return; - case RP_INTERNET_DNS: - rr->phase = RP_MODIFY; - for (cr = clients_head; NULL != cr; cr = cr->next) - { - if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION)) - GNUNET_array_append (rr->client_wait_list, - rr->client_wait_list_length, - cr); - } - next_phase (rr); - return; - case RP_MODIFY: - rr->phase = RP_RESPONSE_MONITOR; - for (cr = clients_head; NULL != cr; cr = cr->next) - { - if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR)) - GNUNET_array_append (rr->client_wait_list, - rr->client_wait_list_length, - cr); - } - next_phase (rr); - return; - case RP_RESPONSE_MONITOR: - request_done (rr); - break; - case RP_DROP: - cleanup_rr (rr); - break; - default: - GNUNET_break (0); - cleanup_rr (rr); - break; - } -} - - -/** - * A client disconnected, clean up after it. - * - * @param cls unused - * @param client handle of client that disconnected - */ -static void -client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) -{ - struct ClientRecord *cr; - struct RequestRecord *rr; - unsigned int i; - unsigned int j; - - for (cr = clients_head; NULL != cr; cr = cr->next) - { - if (cr->client == client) - { - GNUNET_SERVER_client_drop (client); - GNUNET_CONTAINER_DLL_remove (clients_head, - clients_tail, - cr); - for (i=0;iclient_wait_list_length) - continue; /* not in use */ - for (j=0;jclient_wait_list_length;j++) - { - if (rr->client_wait_list[j] == cr) - { - rr->client_wait_list[j] = NULL; - next_phase (rr); - } - } - } - GNUNET_free (cr); - return; - } - } -} - - -/** - * Read a DNS response from the (unhindered) UDP-Socket - * - * @param cls socket to read from - * @param tc scheduler context (must be shutdown or read ready) - */ -static void -read_response (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - struct GNUNET_NETWORK_Handle *dnsout = cls; - struct sockaddr_in addr4; - struct sockaddr_in6 addr6; - struct sockaddr *addr; - struct dns_header *dns; - socklen_t addrlen; - struct RequestRecord *rr; - ssize_t r; - int len; - - if (dnsout == dnsout4) - { - addrlen = sizeof (struct sockaddr_in); - addr = (struct sockaddr* ) &addr4; - read4_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - dnsout, - &read_response, - dnsout); - } - else - { - addrlen = sizeof (struct sockaddr_in6); - addr = (struct sockaddr* ) &addr6; - read6_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - dnsout, - &read_response, - dnsout); - } - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - return; - -#ifndef MINGW - if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len)) - { - /* conservative choice: */ - len = 65536; - } -#else - /* port the code above? */ - len = 65536; -#endif - - { - unsigned char buf[len]; - - memset (addr, 0, addrlen); - r = GNUNET_NETWORK_socket_recvfrom (dnsout, - buf, sizeof (buf), - addr, &addrlen); - if (-1 == r) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom"); - return; - } - if (sizeof (struct dns_header) > r) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Received DNS response that is too small (%u bytes)"), - r); - return; - } - dns = (struct dns_header *) buf; - rr = &requests[dns->id]; - if (rr->phase != RP_INTERNET_DNS) - { - /* unexpected / bogus reply */ - GNUNET_STATISTICS_update (stats, - gettext_noop ("# External DNS response discarded (no matching request)"), - 1, GNUNET_NO); - return; - } - GNUNET_free_non_null (rr->payload); - rr->payload = GNUNET_malloc (len); - memcpy (rr->payload, buf, len); - rr->payload_length = len; - next_phase (rr); - } -} - - -/** - * Open source port for sending DNS request on IPv4. - * - * @return GNUNET_OK on success - */ -static int -open_port4 () -{ - struct sockaddr_in addr; - socklen_t addrlen; - - dnsout4 = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0); - if (dnsout4 == NULL) - return GNUNET_SYSERR; - - memset (&addr, 0, sizeof (struct sockaddr_in)); - addr.sin_family = AF_INET; - int err = GNUNET_NETWORK_socket_bind (dnsout4, - (struct sockaddr *) &addr, - sizeof (struct sockaddr_in)); - - if (err != GNUNET_OK) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Could not bind to any port: %s\n"), - STRERROR (errno)); - GNUNET_NETWORK_socket_close (dnsout4); - dnsout4 = NULL; - return GNUNET_SYSERR; - } - - /* Read the port we bound to */ - addrlen = sizeof (struct sockaddr_in); - if (0 != getsockname (GNUNET_NETWORK_get_fd (dnsout4), - (struct sockaddr *) &addr, - &addrlen)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Could not determine port I got: %s\n"), - STRERROR (errno)); - GNUNET_NETWORK_socket_close (dnsout4); - dnsout4 = NULL; - return GNUNET_SYSERR; - } - dnsoutport = htons (addr.sin_port); - - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("GNUnet DNS will exit on source port %u\n"), - (unsigned int) dnsoutport); - read4_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - dnsout4, - &read_response, dnsout4); - return GNUNET_OK; -} - - -/** - * Open source port for sending DNS request on IPv6. Should be - * called AFTER open_port4. - * - * @return GNUNET_OK on success - */ -static int -open_port6 () -{ - struct sockaddr_in6 addr; - socklen_t addrlen; - - dnsout6 = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_DGRAM, 0); - if (dnsout6 == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Could not create IPv6 socket: %s\n"), - STRERROR (errno)); - return GNUNET_SYSERR; - } - memset (&addr, 0, sizeof (struct sockaddr_in6)); - addr.sin6_family = AF_INET6; - addr.sin6_port = htons (dnsoutport); - int err = GNUNET_NETWORK_socket_bind (dnsout6, - (struct sockaddr *) &addr, - sizeof (struct sockaddr_in6)); - - if (err != GNUNET_OK) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Could not bind to port %u: %s\n"), - (unsigned int) dnsoutport, - STRERROR (errno)); - GNUNET_NETWORK_socket_close (dnsout6); - dnsout6 = NULL; - return GNUNET_SYSERR; - } - if (0 == dnsoutport) - { - addrlen = sizeof (struct sockaddr_in6); - if (0 != getsockname (GNUNET_NETWORK_get_fd (dnsout6), - (struct sockaddr *) &addr, - &addrlen)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Could not determine port I got: %s\n"), - STRERROR (errno)); - GNUNET_NETWORK_socket_close (dnsout6); - dnsout6 = NULL; - return GNUNET_SYSERR; - } - } - dnsoutport = htons (addr.sin6_port); - read6_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - dnsout6, - &read_response, dnsout6); - return GNUNET_YES; -} - - -/** - * We got a new client. Make sure all new DNS requests pass by its desk. - * - * @param cls unused - * @param client the new client - * @param message the init message (unused) - */ -static void -handle_client_init (void *cls GNUNET_UNUSED, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) -{ - struct ClientRecord *cr; - const struct GNUNET_DNS_Register *reg = (const struct GNUNET_DNS_Register*) message; - - cr = GNUNET_malloc (sizeof (struct ClientRecord)); - cr->client = client; - cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags); - GNUNET_SERVER_client_keep (client); - GNUNET_CONTAINER_DLL_insert (clients_head, - clients_tail, - cr); - GNUNET_SERVER_notification_context_add (nc, client); - GNUNET_SERVER_receive_done (client, GNUNET_OK); -} - - -/** - * We got a response from a client. - * - * @param cls unused - * @param client the client - * @param message the response - */ -static void -handle_client_response (void *cls GNUNET_UNUSED, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) -{ - const struct GNUNET_DNS_Response *resp; - struct RequestRecord *rr; - unsigned int i; - uint16_t msize; - uint16_t off; - - msize = ntohs (message->size); - if (msize < sizeof (struct GNUNET_DNS_Response)) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } - resp = (const struct GNUNET_DNS_Response*) message; - off = (uint16_t) resp->request_id; - rr = &requests[off]; - if (rr->request_id != resp->request_id) - { - GNUNET_STATISTICS_update (stats, - gettext_noop ("# Client response discarded (no matching request)"), - 1, GNUNET_NO); - GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; - } - for (i=0;iclient_wait_list_length;i++) - { - if (NULL == rr->client_wait_list[i]) - continue; - if (rr->client_wait_list[i]->client != client) - continue; - rr->client_wait_list[i] = NULL; - switch (ntohl (resp->drop_flag)) - { - case 0: /* drop */ - rr->phase = RP_DROP; - break; - case 1: /* no change */ - break; - case 2: /* update */ - msize -= sizeof (struct GNUNET_DNS_Response); - if ( (sizeof (struct dns_header) > msize) || - (RP_REQUEST_MONITOR == rr->phase) || - (RP_RESPONSE_MONITOR == rr->phase) ) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - next_phase (rr); - return; - } - GNUNET_free_non_null (rr->payload); -#if DEBUG_DNS - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - _("Changing DNS reply according to client specifications\n")); -#endif - rr->payload = GNUNET_malloc (msize); - rr->payload_length = msize; - memcpy (rr->payload, &resp[1], msize); - if (rr->phase == RP_QUERY) - { - /* clear wait list, we're moving to MODIFY phase next */ - GNUNET_array_grow (rr->client_wait_list, - rr->client_wait_list_length, - 0); - } - break; - } - next_phase (rr); - GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; - } - /* odd, client was not on our list for the request, that ought - to be an error */ - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); -} - - -/** - * Functions with this signature are called whenever a complete - * message is received by the tokenizer from the DNS hijack process. - * - * @param cls closure - * @param client identification of the client - * @param message the actual message, a DNS request we should handle - */ -static void -process_helper_messages (void *cls GNUNET_UNUSED, void *client, - const struct GNUNET_MessageHeader *message) -{ - uint16_t msize; - const struct tun_header *tun; - const struct ip4_header *ip4; - const struct ip6_header *ip6; - const struct udp_packet *udp; - const struct dns_header *dns; - struct RequestRecord *rr; - struct sockaddr_in *srca4; - struct sockaddr_in6 *srca6; - struct sockaddr_in *dsta4; - struct sockaddr_in6 *dsta6; - - msize = ntohs (message->size); - if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct tun_header) + sizeof (struct ip4_header)) - { - /* non-IP packet received on TUN!? */ - GNUNET_break (0); - return; - } - msize -= sizeof (struct GNUNET_MessageHeader); - tun = (const struct tun_header *) &message[1]; - msize -= sizeof (struct tun_header); - switch (ntohs (tun->proto)) - { - case ETH_P_IPV4: - ip4 = (const struct ip4_header *) &tun[1]; - if ( (msize < sizeof (struct ip4_header)) || - (ip4->version != IPVERSION) || - (ip4->header_length != sizeof (struct ip4_header) / 4) || - (ntohs(ip4->total_length) != msize) || - (ip4->protocol != IPPROTO_UDP) ) - { - /* non-IP/UDP packet received on TUN (or with options) */ - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Received malformed IPv4-UDP packet on TUN interface.\n")); - return; - } - udp = (const struct udp_packet*) &ip4[1]; - msize -= sizeof (struct ip4_header); - break; - case ETH_P_IPV6: - ip6 = (const struct ip6_header *) &tun[1]; - if ( (msize < sizeof (struct ip6_header)) || - (ip6->version != 6) || - (ntohs (ip6->payload_length) != msize) || - (ip6->next_header != IPPROTO_UDP) ) - { - /* non-IP/UDP packet received on TUN (or with extensions) */ - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Received malformed IPv6-UDP packet on TUN interface.\n")); - return; - } - udp = (const struct udp_packet*) &ip6[1]; - msize -= sizeof (struct ip6_header); - break; - default: - /* non-IP packet received on TUN!? */ - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Got non-IP packet with %u bytes and protocol %u from TUN\n"), - (unsigned int) msize, - ntohs (tun->proto)); - return; - } - if (msize <= sizeof (struct udp_packet) + sizeof (struct dns_header)) - { - /* non-DNS packet received on TUN, ignore */ - GNUNET_STATISTICS_update (stats, - gettext_noop ("# Non-DNS UDP packet received via TUN interface"), - 1, GNUNET_NO); - return; - } - msize -= sizeof (struct udp_packet); - dns = (const struct dns_header*) &udp[1]; - rr = &requests[dns->id]; - - /* clean up from previous request */ - GNUNET_free_non_null (rr->payload); - rr->payload = NULL; - GNUNET_array_grow (rr->client_wait_list, - rr->client_wait_list_length, - 0); - - /* setup new request */ - rr->phase = RP_INIT; - if (ip4->version == IPVERSION) - { - srca4 = (struct sockaddr_in*) &rr->src_addr; - dsta4 = (struct sockaddr_in*) &rr->dst_addr; - memset (srca4, 0, sizeof (struct sockaddr_in)); - memset (dsta4, 0, sizeof (struct sockaddr_in)); - srca4->sin_family = AF_INET; - dsta4->sin_family = AF_INET; - srca4->sin_addr = ip4->source_address; - dsta4->sin_addr = ip4->destination_address; - srca4->sin_port = udp->spt; - dsta4->sin_port = udp->dpt; -#if HAVE_SOCKADDR_IN_SIN_LEN - srca4->sin_len = sizeof (sizeof (struct sockaddr_in)); - dsta4->sin_len = sizeof (sizeof (struct sockaddr_in)); -#endif - } - else /* ipv6 */ - { - srca6 = (struct sockaddr_in6*) &rr->src_addr; - dsta6 = (struct sockaddr_in6*) &rr->dst_addr; - memset (srca6, 0, sizeof (struct sockaddr_in6)); - memset (dsta6, 0, sizeof (struct sockaddr_in6)); - srca6->sin6_family = AF_INET6; - dsta6->sin6_family = AF_INET6; - srca6->sin6_addr = ip6->source_address; - dsta6->sin6_addr = ip6->destination_address; - srca6->sin6_port = udp->spt; - dsta6->sin6_port = udp->dpt; -#if HAVE_SOCKADDR_IN_SIN_LEN - srca6->sin6_len = sizeof (sizeof (struct sockaddr_in6)); - dsta6->sin6_len = sizeof (sizeof (struct sockaddr_in6)); -#endif - } - rr->payload = GNUNET_malloc (msize); - rr->payload_length = msize; - memcpy (rr->payload, dns, msize); - rr->request_id = dns->id | (request_id_gen << 16); - request_id_gen++; - - GNUNET_STATISTICS_update (stats, - gettext_noop ("# DNS requests received via TUN interface"), - 1, GNUNET_NO); - /* start request processing state machine */ - next_phase (rr); -} - - -/** - * @param cls closure - * @param server the initialized server - * @param cfg_ configuration to use - */ -static void -run (void *cls, struct GNUNET_SERVER_Handle *server, - const struct GNUNET_CONFIGURATION_Handle *cfg_) -{ - static const struct GNUNET_SERVER_MessageHandler handlers[] = { - /* callback, cls, type, size */ - {&handle_client_init, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT, - sizeof (struct GNUNET_DNS_Register)}, - {&handle_client_response, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 0}, - {NULL, NULL, 0, 0} - }; - char port_s[6]; - char *ifc_name; - char *ipv4addr; - char *ipv4mask; - char *ipv6addr; - char *ipv6prefix; - - cfg = cfg_; - stats = GNUNET_STATISTICS_create ("dns", cfg); - nc = GNUNET_SERVER_notification_context_create (server, 1); - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task, - cls); - if (GNUNET_YES == - GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT")) - { - if ( (GNUNET_OK != open_port4 ()) && - (GNUNET_OK != open_port6 ()) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Failed to open any port to provide DNS exit\n")); - GNUNET_SCHEDULER_shutdown (); - return; - } - } - - helper_argv[0] = GNUNET_strdup ("gnunet-dns"); - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IFNAME' in configuration!\n"); - GNUNET_SCHEDULER_shutdown (); - return; - } - helper_argv[1] = ifc_name; - if ( (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6ADDR", - &ipv6addr)) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV6ADDR' in configuration!\n"); - GNUNET_SCHEDULER_shutdown (); - return; - } - helper_argv[2] = ipv6addr; - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV6PREFIX", - &ipv6prefix)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV6PREFIX' in configuration!\n"); - GNUNET_SCHEDULER_shutdown (); - return; - } - helper_argv[3] = ipv6prefix; - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4ADDR", - &ipv4addr)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV4ADDR' in configuration!\n"); - GNUNET_SCHEDULER_shutdown (); - return; - } - helper_argv[4] = ipv4addr; - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK", - &ipv4mask)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV4MASK' in configuration!\n"); - GNUNET_SCHEDULER_shutdown (); - return; - } - helper_argv[5] = ipv4mask; - GNUNET_snprintf (port_s, - sizeof (port_s), - "%u", - (unsigned int) dnsoutport); - helper_argv[6] = GNUNET_strdup (port_s); - helper_argv[7] = NULL; - hijacker = GNUNET_HELPER_start ("gnunet-helper-dns", - helper_argv, - &process_helper_messages, - NULL); - GNUNET_SERVER_add_handlers (server, handlers); - GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL); -} - - -/** - * The main function for the dns service. - * - * @param argc number of arguments from the command line - * @param argv command line arguments - * @return 0 ok, 1 on error - */ -int -main (int argc, char *const *argv) -{ - return (GNUNET_OK == - GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE, - &run, NULL)) ? 0 : 1; -} - - -/* end of gnunet-service-dns_new.c */ diff --git a/src/include/Makefile.am b/src/include/Makefile.am index b066c7cdc..d0cd70d01 100644 --- a/src/include/Makefile.am +++ b/src/include/Makefile.am @@ -42,7 +42,6 @@ gnunetinclude_HEADERS = \ gnunet_disk_lib.h \ gnunet_dnsparser_lib.h \ gnunet_dns_service.h \ - gnunet_dns_service-new.h \ gnunet_dv_service.h \ gnunet_fragmentation_lib.h \ gnunet_fs_service.h \ diff --git a/src/include/gnunet_dns_service-new.h b/src/include/gnunet_dns_service-new.h deleted file mode 100644 index 3517a4609..000000000 --- a/src/include/gnunet_dns_service-new.h +++ /dev/null @@ -1,186 +0,0 @@ -/* - This file is part of GNUnet - (C) 2012 Christian Grothoff (and other contributing authors) - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 2, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. - */ - -/** - * @file include/gnunet_dns_service-new.h - * @brief API to access the DNS service. - * @author Christian Grothoff - */ -#ifndef GNUNET_DNS_SERVICE_NEW_H -#define GNUNET_DNS_SERVICE_NEW_H - -#include "gnunet_common.h" -#include "gnunet_util_lib.h" - - -/** - * Opaque DNS handle - */ -struct GNUNET_DNS_Handle; - -/** - * Handle to identify an individual DNS request. - */ -struct GNUNET_DNS_RequestHandle; - -/** - * Flags that specify when to call the client's handler. - */ -enum GNUNET_DNS_Flags -{ - - /** - * Useless option: never call the client. - */ - GNUNET_DNS_FLAG_NEVER = 0, - - /** - * Set this flag to see all requests first prior to resolution - * (for monitoring). Clients that set this flag must then - * call "GNUNET_DNS_request_forward" when they process a request - * for the first time. Caling "GNUNET_DNS_request_answer" is - * not allowed for MONITOR peers. - */ - GNUNET_DNS_FLAG_REQUEST_MONITOR = 1, - - /** - * This client should be called on requests that have not - * yet been resolved as this client provides a resolution - * service. Note that this does not guarantee that the - * client will see all requests as another client might be - * called first and that client might have already done the - * resolution, in which case other pre-resolution clients - * won't see the request anymore. - */ - GNUNET_DNS_FLAG_PRE_RESOLUTION = 2, - - /** - * This client wants to be called on the results of a DNS resolution - * (either resolved by PRE-RESOLUTION clients or the global DNS). - * The client then has a chance to modify the answer (or cause it to - * be dropped). There is no guarantee that other POST-RESOLUTION - * client's won't modify (or drop) the answer afterwards. - */ - GNUNET_DNS_FLAG_POST_RESOLUTION = 4, - - /** - * Set this flag to see all requests just before they are - * returned to the network. Clients that set this flag must then - * call "GNUNET_DNS_request_forward" when they process a request - * for the last time. Caling "GNUNET_DNS_request_answer" is - * not allowed for MONITOR peers. - */ - GNUNET_DNS_FLAG_RESPONSE_MONITOR = 8 - -}; - - - -/** - * Signature of a function that is called whenever the DNS service - * encounters a DNS request and needs to do something with it. The - * function has then the chance to generate or modify the response by - * calling one of the three "GNUNET_DNS_request_*" continuations. - * - * When a request is intercepted, this function is called first to - * give the client a chance to do the complete address resolution; - * "rdata" will be NULL for this first call for a DNS request, unless - * some other client has already filled in a response. - * - * If multiple clients exist, all of them are called before the global - * DNS. The global DNS is only called if all of the clients' - * functions call GNUNET_DNS_request_forward. Functions that call - * GNUNET_DNS_request_forward will be called again before a final - * response is returned to the application. If any of the clients' - * functions call GNUNET_DNS_request_drop, the response is dropped. - * - * @param cls closure - * @param rh request handle to user for reply - * @param request_length number of bytes in request - * @param request udp payload of the DNS request - */ -typedef void (*GNUNET_DNS_RequestHandler)(void *cls, - struct GNUNET_DNS_RequestHandle *rh, - size_t request_length, - const char *request); - - -/** - * If a GNUNET_DNS_RequestHandler calls this function, the client - * has no desire to interfer with the request and it should - * continue to be processed normally. - * - * @param rh request that should now be forwarded - */ -void -GNUNET_DNS_request_forward (struct GNUNET_DNS_RequestHandle *rh); - - -/** - * If a GNUNET_DNS_RequestHandler calls this function, the request is - * to be dropped and no response should be generated. - * - * @param rh request that should now be dropped - */ -void -GNUNET_DNS_request_drop (struct GNUNET_DNS_RequestHandle *rh); - - -/** - * If a GNUNET_DNS_RequestHandler calls this function, the request is - * supposed to be answered with the data provided to this call (with - * the modifications the function might have made). The reply given - * must always be a valid DNS reply and not a mutated DNS request. - * - * @param rh request that should now be answered - * @param reply_length size of reply (uint16_t to force sane size) - * @param reply reply data - */ -void -GNUNET_DNS_request_answer (struct GNUNET_DNS_RequestHandle *rh, - uint16_t reply_length, - const char *reply); - - -/** - * Connect to the service-dns - * - * @param cfg configuration to use - * @param flags when to call rh - * @param rh function to call with DNS requests - * @param rh_cls closure to pass to rh - * @return DNS handle - */ -struct GNUNET_DNS_Handle * -GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, - enum GNUNET_DNS_Flags flags, - GNUNET_DNS_RequestHandler rh, - void *rh_cls); - - -/** - * Disconnect from the DNS service. - * - * @param dh DNS handle - */ -void -GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *dh); - -#endif diff --git a/src/include/gnunet_dns_service.h b/src/include/gnunet_dns_service.h index 329a083ff..3517a4609 100644 --- a/src/include/gnunet_dns_service.h +++ b/src/include/gnunet_dns_service.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet - (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors) + (C) 2012 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -19,190 +19,168 @@ */ /** - * @file include/gnunet_dns_service.h - * @brief API to access the DNS service. Not finished at all, - * currently only contains the structs for the IPC, which - * don't even belong here (legacy code in transition) - * @author Philipp Toelke - * - * TODO: - * - replace (most?) structs with nice function (prototypes) that take - * the appropriate arguments to pass the data - * - clean up API implementation itself (nicer reconnect, etc.) + * @file include/gnunet_dns_service-new.h + * @brief API to access the DNS service. + * @author Christian Grothoff */ -#ifndef GNUNET_DNS_SERVICE_H -#define GNUNET_DNS_SERVICE_H +#ifndef GNUNET_DNS_SERVICE_NEW_H +#define GNUNET_DNS_SERVICE_NEW_H #include "gnunet_common.h" #include "gnunet_util_lib.h" /** - * Subtypes of DNS answers. + * Opaque DNS handle + */ +struct GNUNET_DNS_Handle; + +/** + * Handle to identify an individual DNS request. */ -enum GNUNET_DNS_ANSWER_Subtype +struct GNUNET_DNS_RequestHandle; + +/** + * Flags that specify when to call the client's handler. + */ +enum GNUNET_DNS_Flags { + /** - * Answers of this type contain a dns-packet that just has to be transmitted + * Useless option: never call the client. */ - GNUNET_DNS_ANSWER_TYPE_IP, + GNUNET_DNS_FLAG_NEVER = 0, /** - * Answers of this type contain an incomplete dns-packet. The IP-Address - * is all 0s. The addroffset points to it. + * Set this flag to see all requests first prior to resolution + * (for monitoring). Clients that set this flag must then + * call "GNUNET_DNS_request_forward" when they process a request + * for the first time. Caling "GNUNET_DNS_request_answer" is + * not allowed for MONITOR peers. */ - GNUNET_DNS_ANSWER_TYPE_SERVICE, + GNUNET_DNS_FLAG_REQUEST_MONITOR = 1, /** - * Answers of this type contain an incomplete dns-packet as answer to a - * PTR-Query. The resolved name is not allocated. The addroffset points to it. + * This client should be called on requests that have not + * yet been resolved as this client provides a resolution + * service. Note that this does not guarantee that the + * client will see all requests as another client might be + * called first and that client might have already done the + * resolution, in which case other pre-resolution clients + * won't see the request anymore. */ - GNUNET_DNS_ANSWER_TYPE_REV, - + GNUNET_DNS_FLAG_PRE_RESOLUTION = 2, + /** - * Answers of this type contains an IP6-Address but traffic to this IP should - * be routed through the GNUNet. + * This client wants to be called on the results of a DNS resolution + * (either resolved by PRE-RESOLUTION clients or the global DNS). + * The client then has a chance to modify the answer (or cause it to + * be dropped). There is no guarantee that other POST-RESOLUTION + * client's won't modify (or drop) the answer afterwards. */ - GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA, - + GNUNET_DNS_FLAG_POST_RESOLUTION = 4, + /** - * Answers of this type contains an IP4-Address but traffic to this IP should - * be routed through the GNUNet. + * Set this flag to see all requests just before they are + * returned to the network. Clients that set this flag must then + * call "GNUNET_DNS_request_forward" when they process a request + * for the last time. Caling "GNUNET_DNS_request_answer" is + * not allowed for MONITOR peers. */ - GNUNET_DNS_ANSWER_TYPE_REMOTE_A + GNUNET_DNS_FLAG_RESPONSE_MONITOR = 8 }; -GNUNET_NETWORK_STRUCT_BEGIN -struct GNUNET_vpn_service_descriptor -{ - GNUNET_HashCode peer GNUNET_PACKED; - GNUNET_HashCode service_descriptor GNUNET_PACKED; - uint64_t ports GNUNET_PACKED; - uint32_t service_type GNUNET_PACKED; -}; - - -struct answer_packet -{ - /* General data */ - struct GNUNET_MessageHeader hdr; - enum GNUNET_DNS_ANSWER_Subtype subtype GNUNET_PACKED; - - char from[16]; - char to[16]; - char addrlen; - unsigned dst_port:16 GNUNET_PACKED; - /* -- */ - - /* Data for GNUNET_DNS_ANSWER_TYPE_SERVICE */ - struct GNUNET_vpn_service_descriptor service_descr; - /* -- */ - - /* Data for GNUNET_DNS_ANSWER_TYPE_REV */ - /* The offsett in octets from the beginning of the struct to the field - * in data where the IP-Address has to go. */ - uint16_t addroffset GNUNET_PACKED; - /* -- */ - - /* Data for GNUNET_DNS_ANSWER_TYPE_REMOTE */ - /* either 4 or 16 */ - char addrsize; - unsigned char addr[16]; - /* -- */ - - unsigned char data[1]; -}; -GNUNET_NETWORK_STRUCT_END - - /** - * Type of a function to be called by the DNS API whenever - * a DNS reply is obtained. + * Signature of a function that is called whenever the DNS service + * encounters a DNS request and needs to do something with it. The + * function has then the chance to generate or modify the response by + * calling one of the three "GNUNET_DNS_request_*" continuations. + * + * When a request is intercepted, this function is called first to + * give the client a chance to do the complete address resolution; + * "rdata" will be NULL for this first call for a DNS request, unless + * some other client has already filled in a response. + * + * If multiple clients exist, all of them are called before the global + * DNS. The global DNS is only called if all of the clients' + * functions call GNUNET_DNS_request_forward. Functions that call + * GNUNET_DNS_request_forward will be called again before a final + * response is returned to the application. If any of the clients' + * functions call GNUNET_DNS_request_drop, the response is dropped. * * @param cls closure - * @param pkt reply that we got + * @param rh request handle to user for reply + * @param request_length number of bytes in request + * @param request udp payload of the DNS request */ -typedef void (*GNUNET_DNS_ResponseCallback)(void *cls, - const struct answer_packet *pkt); +typedef void (*GNUNET_DNS_RequestHandler)(void *cls, + struct GNUNET_DNS_RequestHandle *rh, + size_t request_length, + const char *request); /** - * Opaque DNS handle - */ -struct GNUNET_DNS_Handle; - - -/** - * Connect to the service-dns + * If a GNUNET_DNS_RequestHandler calls this function, the client + * has no desire to interfer with the request and it should + * continue to be processed normally. * - * @param cfg configuration to use - * @param cb function to call with DNS replies - * @param cb_cls closure to pass to cb - * @return DNS handle + * @param rh request that should now be forwarded */ -struct GNUNET_DNS_Handle * -GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, - GNUNET_DNS_ResponseCallback cb, - void *cb_cls); +void +GNUNET_DNS_request_forward (struct GNUNET_DNS_RequestHandle *rh); /** - * Signal the DNS service that it needs to re-initialize the DNS - * hijacking (the network setup has changed significantly). + * If a GNUNET_DNS_RequestHandler calls this function, the request is + * to be dropped and no response should be generated. * - * @param h DNS handle + * @param rh request that should now be dropped */ void -GNUNET_DNS_restart_hijack (struct GNUNET_DNS_Handle *h); +GNUNET_DNS_request_drop (struct GNUNET_DNS_RequestHandle *rh); /** - * Process a DNS request sent to an IPv4 resolver. Pass it - * to the DNS service for resolution. + * If a GNUNET_DNS_RequestHandler calls this function, the request is + * supposed to be answered with the data provided to this call (with + * the modifications the function might have made). The reply given + * must always be a valid DNS reply and not a mutated DNS request. * - * @param h DNS handle - * @param dst_ip destination IPv4 address - * @param src_ip source IPv4 address (usually local machine) - * @param src_port source port (to be used for reply) - * @param udp_packet_len length of the UDP payload in bytes - * @param udp_packet UDP payload + * @param rh request that should now be answered + * @param reply_length size of reply (uint16_t to force sane size) + * @param reply reply data */ void -GNUNET_DNS_queue_request_v4 (struct GNUNET_DNS_Handle *h, - const struct in_addr *dst_ip, - const struct in_addr *src_ip, - uint16_t src_port, - size_t udp_packet_len, - const char *udp_packet); +GNUNET_DNS_request_answer (struct GNUNET_DNS_RequestHandle *rh, + uint16_t reply_length, + const char *reply); + /** - * Process a DNS request sent to an IPv6 resolver. Pass it - * to the DNS service for resolution. + * Connect to the service-dns * - * @param h DNS handle - * @param dst_ip destination IPv6 address - * @param src_ip source IPv6 address (usually local machine) - * @param src_port source port (to be used for reply) - * @param udp_packet_len length of the UDP payload in bytes - * @param udp_packet UDP payload + * @param cfg configuration to use + * @param flags when to call rh + * @param rh function to call with DNS requests + * @param rh_cls closure to pass to rh + * @return DNS handle */ -void -GNUNET_DNS_queue_request_v6 (struct GNUNET_DNS_Handle *h, - const struct in6_addr *dst_ip, - const struct in6_addr *src_ip, - uint16_t src_port, - size_t udp_packet_len, - const char *udp_packet); +struct GNUNET_DNS_Handle * +GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, + enum GNUNET_DNS_Flags flags, + GNUNET_DNS_RequestHandler rh, + void *rh_cls); + /** * Disconnect from the DNS service. * - * @param h DNS handle + * @param dh DNS handle */ void -GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *h); +GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *dh); #endif diff --git a/src/include/gnunet_dnsparser_lib.h b/src/include/gnunet_dnsparser_lib.h index 6654394be..a1fb920a7 100644 --- a/src/include/gnunet_dnsparser_lib.h +++ b/src/include/gnunet_dnsparser_lib.h @@ -394,97 +394,4 @@ GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p, size_t *buf_length); - - -/* legacy API below */ - -// DNS-Stuff -GNUNET_NETWORK_STRUCT_BEGIN - -struct dns_static -{ - uint16_t id GNUNET_PACKED; - - unsigned rd:1 GNUNET_PACKED; // recursion desired (client -> server) - unsigned tc:1 GNUNET_PACKED; // message is truncated - unsigned aa:1 GNUNET_PACKED; // authoritative answer - unsigned op:4 GNUNET_PACKED; // query:0, inverse q.:1, status: 2 - unsigned qr:1 GNUNET_PACKED; // query:0, response:1 - - unsigned rcode:4 GNUNET_PACKED; // 0 No error - // 1 Format error - // 2 Server failure - // 3 Name Error - // 4 Not Implemented - // 5 Refused - unsigned z:3 GNUNET_PACKED; // reserved - unsigned ra:1 GNUNET_PACKED; // recursion available (server -> client) - - uint16_t qdcount GNUNET_PACKED; // number of questions - uint16_t ancount GNUNET_PACKED; // number of answers - uint16_t nscount GNUNET_PACKED; // number of authority-records - uint16_t arcount GNUNET_PACKED; // number of additional records -}; -GNUNET_NETWORK_STRUCT_END - - -struct dns_pkt -{ - struct dns_static s; - unsigned char data[1]; -}; - -struct dns_pkt_parsed -{ - struct dns_static s; - struct dns_query **queries; - struct dns_record **answers; - struct dns_record **nameservers; - struct dns_record **additional; -}; - -struct dns_query_line -{ - uint16_t type; - uint16_t class; -}; - -struct dns_query -{ - char *name; - unsigned char namelen; - uint16_t qtype; - uint16_t qclass; -}; - -struct dns_record_line -{ - uint16_t type; - uint16_t class; - uint32_t ttl; - uint16_t data_len; - unsigned char data; -}; - -struct dns_record -{ - char *name; - unsigned char namelen; - uint16_t type; - uint16_t class; - uint32_t ttl; - uint16_t data_len; - unsigned char *data; -}; - - -struct dns_pkt_parsed * -parse_dns_packet (struct dns_pkt *pkt); - -struct dns_pkt * -unparse_dns_packet (struct dns_pkt_parsed *pkt); - -void -free_parsed_dns_packet (struct dns_pkt_parsed *ppkt); - #endif diff --git a/src/vpn/Makefile.am b/src/vpn/Makefile.am index be28949c5..bb0993c66 100644 --- a/src/vpn/Makefile.am +++ b/src/vpn/Makefile.am @@ -30,23 +30,12 @@ lib_LTLIBRARIES = \ bin_PROGRAMS = \ - gnunet-daemon-exit gnunet-daemon-vpn $(VPNBIN) gnunet-service-vpn gnunet-vpn + $(VPNBIN) gnunet-service-vpn gnunet-vpn gnunet_helper_vpn_SOURCES = \ gnunet-helper-vpn.c -gnunet_daemon_vpn_SOURCES = \ - gnunet-daemon-vpn.c -gnunet_daemon_vpn_LDADD = \ - $(top_builddir)/src/core/libgnunetcore.la \ - $(top_builddir)/src/statistics/libgnunetstatistics.la \ - $(top_builddir)/src/util/libgnunetutil.la \ - $(top_builddir)/src/mesh/libgnunetmesh.la \ - $(top_builddir)/src/dns/libgnunetdnsparser.la \ - $(top_builddir)/src/dns/libgnunetdns.la \ - $(GN_LIBINTL) - gnunet_service_vpn_SOURCES = \ gnunet-service-vpn.c gnunet_service_vpn_LDADD = \ @@ -57,15 +46,6 @@ gnunet_service_vpn_LDADD = \ gnunet_service_vpn_CFLAGS = \ -I$(top_srcdir)/src/exit $(CFLAGS) -gnunet_daemon_exit_SOURCES = \ - gnunet-daemon-exit.c -gnunet_daemon_exit_LDADD = \ - $(top_builddir)/src/core/libgnunetcore.la \ - $(top_builddir)/src/statistics/libgnunetstatistics.la \ - $(top_builddir)/src/util/libgnunetutil.la \ - $(top_builddir)/src/mesh/libgnunetmesh.la \ - $(GN_LIBINTL) - gnunet_vpn_SOURCES = \ gnunet-vpn.c gnunet_vpn_LDADD = \ @@ -94,6 +74,3 @@ endif # $(top_builddir)/src/transport/libgnunettransport.la \ # $(top_builddir)/src/util/libgnunetutil.la -EXTRA_DIST = \ - gnunet-vpn-packet.h \ - test-conf.conf diff --git a/src/vpn/gnunet-daemon-exit.c b/src/vpn/gnunet-daemon-exit.c deleted file mode 100644 index 6d5dc7797..000000000 --- a/src/vpn/gnunet-daemon-exit.c +++ /dev/null @@ -1,1501 +0,0 @@ -/* - This file is part of GNUnet. - (C) 2010, 2012 Christian Grothoff - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. -*/ - -/** - * @file vpn/gnunet-daemon-exit.c - * @brief - * @author Philipp Toelke - */ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "gnunet-vpn-packet.h" - - -struct remote_addr -{ - char addrlen; - unsigned char addr[16]; - char proto; -}; - -/** - * This struct is saved into the services-hashmap - */ -struct redirect_service -{ - /** - * One of 4 or 6 - */ - unsigned int version; - uint16_t my_port; - uint16_t remote_port; - - union - { - struct - { - char ip4address[4]; - } v4; - struct - { - char ip6address[16]; - } v6; - }; -}; - -struct redirect_info -{ - /** - * The source-address of this connection. When a packet to this address is - * received, this tunnel is used to forward it. ipv4-addresses will be put - * here left-aligned */ - char addr[16]; - /** - * The source-port of this connection - */ - uint16_t pt; -}; - -/** - * This struct is saved into {tcp,udp}_connections; - */ -struct redirect_state -{ - struct GNUNET_MESH_Tunnel *tunnel; - GNUNET_HashCode desc; - struct redirect_service *serv; - struct remote_addr remote; - - struct GNUNET_CONTAINER_HeapNode *heap_node; - struct GNUNET_CONTAINER_MultiHashMap *hashmap; - GNUNET_HashCode hash; - - enum { SERVICE, REMOTE } type; - - /** - * The source-address and -port of this connection - */ - struct redirect_info redirect_info; -}; - -struct tunnel_notify_queue -{ - struct tunnel_notify_queue *next; - struct tunnel_notify_queue *prev; - void *cls; - size_t len; -}; - -struct tunnel_state -{ - struct tunnel_notify_queue *head; - struct tunnel_notify_queue *tail; - struct GNUNET_MESH_TransmitHandle *th; -}; - - -/** - * The handle to the configuration used throughout the process - */ -static const struct GNUNET_CONFIGURATION_Handle *cfg; - -/** - * The handle to the helper - */ -static struct GNUNET_HELPER_Handle *helper_handle; - -/** - * Arguments to the exit helper. - */ -static char *exit_argv[7]; - -/** - * Final status code. - */ -static int ret; - -/** - * The handle to mesh - */ -static struct GNUNET_MESH_Handle *mesh_handle; - -/** - * This hashmaps contains the mapping from peer, service-descriptor, - * source-port and destination-port to a struct redirect_state - */ -static struct GNUNET_CONTAINER_MultiHashMap *udp_connections; - -static struct GNUNET_CONTAINER_Heap *udp_connections_heap; - -static struct GNUNET_CONTAINER_MultiHashMap *tcp_connections; - -static struct GNUNET_CONTAINER_Heap *tcp_connections_heap; - -/** - * If there are at least this many udp-Connections, old ones will be removed - */ -static long long unsigned int max_udp_connections = 200; - -/** - * If there are at least this many tcp-Connections, old ones will be removed - */ -static long long unsigned int max_tcp_connections = 200; - -/** - * This hashmaps saves interesting things about the configured UDP services - */ -static struct GNUNET_CONTAINER_MultiHashMap *udp_services; - -/** - * This hashmaps saves interesting things about the configured TCP services - */ - -static struct GNUNET_CONTAINER_MultiHashMap *tcp_services; - - -/** - * Function that frees everything from a hashmap - */ -static int -free_iterate (void *cls GNUNET_UNUSED, - const GNUNET_HashCode * hash GNUNET_UNUSED, void *value) -{ - GNUNET_free (value); - return GNUNET_YES; -} - -/** - * Function scheduled as very last function, cleans up after us - */ -static void -cleanup (void *cls GNUNET_UNUSED, - const struct GNUNET_SCHEDULER_TaskContext *tskctx) -{ - unsigned int i; - - GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)); - if (mesh_handle != NULL) - { - GNUNET_MESH_disconnect (mesh_handle); - mesh_handle = NULL; - } - if (helper_handle != NULL) - { - GNUNET_HELPER_stop (helper_handle); - helper_handle = NULL; - } - GNUNET_CONTAINER_multihashmap_iterate (udp_connections, &free_iterate, NULL); - GNUNET_CONTAINER_multihashmap_iterate (tcp_connections, &free_iterate, NULL); - for (i=0;i<5;i++) - GNUNET_free_non_null (exit_argv[i]); -} - -static void * -new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, - const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED, - const struct GNUNET_ATS_Information *ats GNUNET_UNUSED) -{ - struct tunnel_state *s = GNUNET_malloc (sizeof *s); - - s->head = NULL; - s->tail = NULL; - s->th = NULL; - return s; -} - -static void -clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel, - void *tunnel_ctx) -{ - GNUNET_free (tunnel_ctx); -} - -static void -collect_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) - return; - - - struct GNUNET_CONTAINER_Heap *heap = cls; - - struct redirect_state *state = GNUNET_CONTAINER_heap_remove_root (heap); - - /* This is free()ed memory! */ - state->heap_node = NULL; - - /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */ - - GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_remove (state->hashmap, - &state->hash, state)); - - GNUNET_free (state); -} - -static void -hash_redirect_info (GNUNET_HashCode * hash, struct redirect_info *u_i, - size_t addrlen) -{ - - /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash - * - * build the hash out of the last bytes of the address and the 2 bytes of - * the port - */ - memcpy (hash, &u_i->pt, sizeof (u_i->pt)); - memcpy (((unsigned char *) hash) + 2, - u_i->addr + (addrlen - (sizeof (unsigned int) - 2)), - (sizeof (unsigned int) - 2)); - memset (((unsigned char *) hash) + sizeof (unsigned int), 0, - sizeof (GNUNET_HashCode) - sizeof (unsigned int)); -} - -/** - * cls is the pointer to a GNUNET_MessageHeader that is - * followed by the service-descriptor and the udp-packet that should be sent; - */ -static size_t -send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf) -{ - struct GNUNET_MESH_Tunnel **tunnel = cls; - - GNUNET_MESH_tunnel_set_data (*tunnel, NULL); - struct GNUNET_MessageHeader *hdr = - (struct GNUNET_MessageHeader *) (tunnel + 1); - GNUNET_assert (size >= ntohs (hdr->size)); - memcpy (buf, hdr, ntohs (hdr->size)); - size = ntohs (hdr->size); - - struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (*tunnel); - - if (NULL != s->head) - { - struct tunnel_notify_queue *element = s->head; - - GNUNET_CONTAINER_DLL_remove (s->head, s->tail, element); - - s->th = - GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42, - GNUNET_TIME_relative_divide - (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), - (const struct GNUNET_PeerIdentity *) - NULL, element->len, - send_udp_to_peer_notify_callback, - element->cls); - - /* save the handle */ - GNUNET_free (element); - } - - GNUNET_free (cls); - - return size; -} - -/** - * @brief Handles an UDP-Packet received from the helper. - * - * @param udp A pointer to the Packet - * @param dadr The IP-Destination-address - * @param addrlen The length of the address - */ -static void -udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen) -{ - struct redirect_info u_i; - struct GNUNET_MESH_Tunnel *tunnel; - uint32_t len; - struct GNUNET_MessageHeader *msg; - - memset (&u_i, 0, sizeof (struct redirect_info)); - - memcpy (&u_i.addr, dadr, addrlen); - - u_i.pt = udp->dpt; - - /* get tunnel and service-descriptor from this */ - GNUNET_HashCode hash; - - hash_redirect_info (&hash, &u_i, addrlen); - - struct redirect_state *state = - GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash); - - /* Mark this connection as freshly used */ - GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node, - GNUNET_TIME_absolute_get ().abs_value); - - tunnel = state->tunnel; - - if (state->type == SERVICE) - { - /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */ - if (ntohs (udp->spt) == state->serv->remote_port) - { - udp->spt = htons (state->serv->my_port); - } - else - { - /* otherwise the answer came from a different port (tftp does this) - * add this new port to the list of all services, so that the packets - * coming back from the client to this new port will be routed correctly - */ - struct redirect_service *serv = - GNUNET_malloc (sizeof (struct redirect_service)); - memcpy (serv, state->serv, sizeof (struct redirect_service)); - serv->my_port = ntohs (udp->spt); - serv->remote_port = ntohs (udp->spt); - uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2); - - memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc, - sizeof (GNUNET_HashCode)); - *desc = ntohs (udp->spt); - GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (udp_services, - (GNUNET_HashCode *) - desc, serv, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); - - state->serv = serv; - } - } - - /* send udp-packet back */ - len = - sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + - ntohs (udp->len); - struct GNUNET_MESH_Tunnel **ctunnel = - GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len); - *ctunnel = tunnel; - msg = (struct GNUNET_MessageHeader *) (ctunnel + 1); - msg->size = htons (len); - msg->type = - htons (state->type == - SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK : - GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK); - GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1); - - if (state->type == SERVICE) - memcpy (desc, &state->desc, sizeof (GNUNET_HashCode)); - else - memcpy (desc, &state->remote, sizeof (struct remote_addr)); - void *_udp = desc + 1; - - memcpy (_udp, udp, ntohs (udp->len)); - - struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (tunnel); - - if (NULL == s->th) - { - /* No notify is pending */ - s->th = - GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO, 42, - GNUNET_TIME_relative_divide - (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), - (const struct GNUNET_PeerIdentity *) - NULL, len, - send_udp_to_peer_notify_callback, - ctunnel); - } - else - { - struct tunnel_notify_queue *element = - GNUNET_malloc (sizeof (struct tunnel_notify_queue)); - element->cls = ctunnel; - element->len = len; - - GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, element); - } -} - -/** - * @brief Handles a TCP-Packet received from the helper. - * - * @param tcp A pointer to the Packet - * @param dadr The IP-Destination-address - * @param addrlen The length of the address - * @param pktlen the length of the packet, including its header - */ -static void -tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen, - size_t pktlen) -{ - struct redirect_info u_i; - struct GNUNET_MESH_Tunnel *tunnel; - uint32_t len; - struct GNUNET_MessageHeader *msg; - - memset (&u_i, 0, sizeof (struct redirect_info)); - - memcpy (&u_i.addr, dadr, addrlen); - u_i.pt = tcp->dpt; - - /* get tunnel and service-descriptor from this */ - GNUNET_HashCode hash; - - hash_redirect_info (&hash, &u_i, addrlen); - - struct redirect_state *state = - GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash); - - if (state == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "No mapping for this connection; hash is %x\n", - *((uint32_t *) & hash)); - return; - } - - /* Mark this connection as freshly used */ - GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node, - GNUNET_TIME_absolute_get ().abs_value); - - tunnel = state->tunnel; - - if (state->type == SERVICE) - { - /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */ - if (ntohs (tcp->spt) == state->serv->remote_port) - { - tcp->spt = htons (state->serv->my_port); - } - else - { - // This is an illegal packet. - return; - } - } - - /* send tcp-packet back */ - len = - sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen); - struct GNUNET_MESH_Tunnel **ctunnel = - GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len); - *ctunnel = tunnel; - msg = (struct GNUNET_MessageHeader *) (ctunnel + 1); - msg->size = htons (len); - msg->type = - htons (state->type == - SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK : - GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK); - GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1); - - if (state->type == SERVICE) - memcpy (desc, &state->desc, sizeof (GNUNET_HashCode)); - else - memcpy (desc, &state->remote, sizeof (struct remote_addr)); - void *_tcp = desc + 1; - - memcpy (_tcp, tcp, pktlen); - - struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (tunnel); - - if (NULL == s->th) - { - /* No notify is pending */ - s->th = - GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO, 42, - GNUNET_TIME_relative_divide - (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), - (const struct GNUNET_PeerIdentity *) - NULL, len, - send_udp_to_peer_notify_callback, - ctunnel); - } - else - { - struct tunnel_notify_queue *element = - GNUNET_malloc (sizeof (struct tunnel_notify_queue)); - element->cls = ctunnel; - element->len = len; - - GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, element); - } -} - - -/** - * Receive packets from the helper-process - */ -static void -message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED, - const struct GNUNET_MessageHeader *message) -{ - GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER); - - struct tun_pkt *pkt_tun = (struct tun_pkt *) message; - - /* ethertype is ipv6 */ - if (ntohs (pkt_tun->tun.type) == 0x86dd) - { - struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun; - - if (IPPROTO_UDP == pkt6->ip6_hdr.nxthdr) - udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr, - (unsigned char *) &pkt6->ip6_hdr.dadr, 16); - else if (IPPROTO_TCP == pkt6->ip6_hdr.nxthdr) - tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr, - (unsigned char *) &pkt6->ip6_hdr.dadr, 16, - ntohs (pkt6->ip6_hdr.paylgth)); - } - else if (ntohs (pkt_tun->tun.type) == 0x0800) - { - struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun; - - if (IPPROTO_UDP == pkt4->ip_hdr.proto) - udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr, - (unsigned char *) &pkt4->ip_hdr.dadr, 4); - else if (IPPROTO_TCP == pkt4->ip_hdr.proto) - { - size_t pktlen = ntohs (pkt4->ip_hdr.tot_lngth); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen); - pktlen -= 4 * pkt4->ip_hdr.hdr_lngth; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen); - tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr, - (unsigned char *) &pkt4->ip_hdr.dadr, 4, pktlen); - } - } - else - { - return; - } -} - -/** - * Reads the configuration servicecfg and populates udp_services - * - * @param cls unused - * @param section name of section in config, equal to hostname - */ -static void -read_service_conf (void *cls GNUNET_UNUSED, const char *section) -{ - if ((strlen (section) < 8) || - (0 != strcmp (".gnunet.", section + (strlen (section) - 8)))) - return; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n", - strlen (section), section, section + (strlen (section) - 8)); - - char *cpy; - char *redirect; - char *hostname; - char *hostport; - uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2); - - GNUNET_CRYPTO_hash (section, strlen (section) + 1, - (GNUNET_HashCode *) (desc + 1)); - -#define TCP 2 -#define UDP 1 - - int proto = UDP; - - do - { - if (proto == UDP && - (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS", - &cpy))) - goto next; - else if (proto == TCP && - (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, section, - "TCP_REDIRECTS", &cpy))) - goto next; - - for (redirect = strtok (cpy, " "); redirect != NULL; - redirect = strtok (NULL, " ")) - { - if (NULL == (hostname = strstr (redirect, ":"))) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Warning: option %s is not formatted correctly!\n", - redirect); - continue; - } - hostname[0] = '\0'; - hostname++; - if (NULL == (hostport = strstr (hostname, ":"))) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Warning: option %s is not formatted correctly!\n", - redirect); - continue; - } - hostport[0] = '\0'; - hostport++; - - int local_port = atoi (redirect); - - if (!((local_port > 0) && (local_port < 65536))) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Warning: %s is not a correct port.", redirect); - - *desc = local_port; - - struct redirect_service *serv = - GNUNET_malloc (sizeof (struct redirect_service)); - serv->my_port = local_port; - - if (0 == strcmp ("localhost4", hostname)) - { - serv->version = 4; - - char *ip4addr; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "exit", - "IPV4ADDR", - &ip4addr)); - GNUNET_assert (1 == inet_pton (AF_INET, ip4addr, serv->v4.ip4address)); - GNUNET_free (ip4addr); - } - else if (0 == strcmp ("localhost6", hostname)) - { - serv->version = 6; - - char *ip6addr; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "exit", - "IPV6ADDR", - &ip6addr)); - GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, serv->v6.ip6address)); - GNUNET_free (ip6addr); - } - else - { - struct addrinfo *res; - - int ret = getaddrinfo (hostname, NULL, NULL, &res); - - if (ret != 0) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No addresses found for %s!\n", - hostname); - GNUNET_free (serv); - continue; - } - else - { - char buf[256]; - struct addrinfo *c = res; - - if (c) - { - if (c->ai_family == AF_INET) - { - serv->version = 4; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Found %s as address for %s\n", - inet_ntop (c->ai_family, - &((struct sockaddr_in *) (c-> - ai_addr))->sin_addr, - (char *) &buf, 256), hostname); - memcpy (serv->v4.ip4address, - &((struct sockaddr_in *) (c->ai_addr))->sin_addr, 4); - } - else if (c->ai_family == AF_INET6) - { - serv->version = 6; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Found %s as address for %s\n", - inet_ntop (c->ai_family, - &((struct sockaddr_in6 *) (c-> - ai_addr))->sin6_addr, - (char *) &buf, 256), hostname); - memcpy (serv->v6.ip6address, - &((struct sockaddr_in6 *) (c->ai_addr))->sin6_addr, 16); - } - } - else - { - freeaddrinfo (res); - GNUNET_free (serv); - continue; - } - freeaddrinfo (res); - } - } - serv->remote_port = atoi (hostport); - if (UDP == proto) - GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (udp_services, - (GNUNET_HashCode *) - desc, serv, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); - else - GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (tcp_services, - (GNUNET_HashCode *) - desc, serv, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); - - } - GNUNET_free (cpy); -next: - proto = (proto == UDP) ? TCP : UDP; - } - while (proto != UDP); -} - - - -static void -prepare_ipv4_packet (size_t len, uint16_t pktlen, void *payload, - uint16_t protocol, void *ipaddress, void *tunnel, - struct redirect_state *state, struct ip_pkt *pkt4) -{ - uint32_t tmp, tmp2; - - pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); - pkt4->shdr.size = htons (len); - pkt4->tun.flags = 0; - pkt4->tun.type = htons (0x0800); - - memcpy (&pkt4->data, payload, pktlen); - - pkt4->ip_hdr.version = 4; - pkt4->ip_hdr.hdr_lngth = 5; - pkt4->ip_hdr.diff_serv = 0; - pkt4->ip_hdr.tot_lngth = htons (20 + pktlen); - pkt4->ip_hdr.ident = 0; - pkt4->ip_hdr.flags = 0; - pkt4->ip_hdr.frag_off = 0; - pkt4->ip_hdr.ttl = 255; - pkt4->ip_hdr.proto = protocol; - pkt4->ip_hdr.chks = 0; /* Will be calculated later */ - - memcpy (&pkt4->ip_hdr.dadr, ipaddress, sizeof (struct in_addr)); - - /* Generate a new src-address */ - char *ipv4addr; - char *ipv4mask; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR", - &ipv4addr)); - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK", - &ipv4mask)); - inet_pton (AF_INET, ipv4addr, &tmp); - inet_pton (AF_INET, ipv4mask, &tmp2); - GNUNET_free (ipv4addr); - GNUNET_free (ipv4mask); - - /* This should be a noop */ - tmp = tmp & tmp2; - - tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2); - - pkt4->ip_hdr.sadr.s_addr = tmp; - - memcpy (&state->redirect_info.addr, &tmp, 4); - if (IPPROTO_UDP == protocol) - { - struct ip_udp *pkt4_udp = (struct ip_udp *) pkt4; - - state->redirect_info.pt = pkt4_udp->udp_hdr.spt; - - pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */ - } - else if (IPPROTO_TCP == protocol) - { - struct ip_tcp *pkt4_tcp = (struct ip_tcp *) pkt4; - - state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt; - - pkt4_tcp->tcp_hdr.crc = 0; - uint32_t sum = 0; - - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) &pkt4->ip_hdr.sadr, sizeof (struct in_addr)); - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) &pkt4->ip_hdr.dadr, sizeof (struct in_addr)); - - tmp = (protocol << 16) | (0xffff & pktlen); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, - (0xffff & pktlen)); - - tmp = htonl (tmp); - - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4); - - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, - pktlen); - pkt4_tcp->tcp_hdr.crc = GNUNET_CRYPTO_crc16_finish (sum); - } - - pkt4->ip_hdr.chks = - GNUNET_CRYPTO_crc16_n ((uint16_t *) & pkt4->ip_hdr, 5 * 4); -} - -static void -prepare_ipv6_packet (size_t len, uint16_t pktlen, void *payload, - uint16_t protocol, void *ipaddress, void *tunnel, - struct redirect_state *state, struct ip6_pkt *pkt6) -{ - uint32_t tmp; - - pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); - pkt6->shdr.size = htons (len); - pkt6->tun.flags = 0; - - pkt6->tun.type = htons (0x86dd); - - memcpy (&pkt6->data, payload, pktlen); - - pkt6->ip6_hdr.version = 6; - pkt6->ip6_hdr.nxthdr = protocol; - pkt6->ip6_hdr.paylgth = htons (pktlen); - pkt6->ip6_hdr.hoplmt = 64; - - memcpy (&pkt6->ip6_hdr.dadr, ipaddress, sizeof (struct in6_addr)); - - /* Generate a new src-address - * This takes as much from the address of the tunnel as fits into - * the host-mask*/ - char *ipv6addr; - unsigned long long ipv6prefix; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR", - &ipv6addr)); - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_number (cfg, "exit", - "IPV6PREFIX", - &ipv6prefix)); - GNUNET_assert (ipv6prefix < 127); - ipv6prefix = (ipv6prefix + 7) / 8; - - inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr); - GNUNET_free (ipv6addr); - - if (ipv6prefix < (16 - sizeof (void *))) - ipv6prefix = 16 - sizeof (void *); - - unsigned int offset = ipv6prefix - (16 - sizeof (void *)); - - memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix, - ((char *) &tunnel) + offset, 16 - ipv6prefix); - - /* copy the needed information into the state */ - memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16); - - if (IPPROTO_UDP == protocol) - { - struct ip6_udp *pkt6_udp = (struct ip6_udp *) pkt6; - - state->redirect_info.pt = pkt6_udp->udp_hdr.spt; - - pkt6_udp->udp_hdr.crc = 0; - uint32_t sum = 0; - - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, - 16); - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, - 16); - tmp = (htons (pktlen) & 0xffff); - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4); - tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff))); - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4); - - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6_udp->udp_hdr, - ntohs (pkt6_udp->udp_hdr.len)); - pkt6_udp->udp_hdr.crc = GNUNET_CRYPTO_crc16_finish (sum); - } - else if (IPPROTO_TCP == protocol) - { - struct ip6_tcp *pkt6_tcp = (struct ip6_tcp *) pkt6; - - state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt; - - pkt6_tcp->tcp_hdr.crc = 0; - uint32_t sum = 0; - - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16); - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16); - tmp = htonl (pktlen); - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4); - tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff))); - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4); - - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6_tcp->tcp_hdr, - ntohs (pkt6->ip6_hdr.paylgth)); - pkt6_tcp->tcp_hdr.crc = GNUNET_CRYPTO_crc16_finish (sum); - } -} - -/** - * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt - */ -static int -receive_tcp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, - void **tunnel_ctx GNUNET_UNUSED, - const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED, - const struct GNUNET_MessageHeader *message, - const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) -{ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n"); - GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); - struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1); - uint16_t pkt_len = - ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - - sizeof (GNUNET_HashCode); - - /** Get the configuration from the services-hashmap. - * - * Which service is needed only depends on the service-descriptor and the - * destination-port - */ - uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2); - - memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode)); - *tcp_desc = ntohs (pkt->dpt); - struct redirect_service *serv = - GNUNET_CONTAINER_multihashmap_get (tcp_services, - (GNUNET_HashCode *) tcp_desc); - - if (NULL == serv) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for TCP dpt %d!\n", - *tcp_desc); - return GNUNET_YES; - } - - pkt->dpt = htons (serv->remote_port); - - /* - * At this point it would be possible to check against some kind of ACL. - */ - - size_t len; - - /* Prepare the state. - * This will be saved in the hashmap, so that the receiving procedure knows - * through which tunnel this connection has to be routed. - */ - struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state)); - - state->tunnel = tunnel; - state->serv = serv; - state->type = SERVICE; - state->hashmap = tcp_connections; - memcpy (&state->desc, desc, sizeof (GNUNET_HashCode)); - - len = - sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + - sizeof (struct ip6_hdr) + pkt_len; - { - char buf[len]; - - memset (buf, 0, len); - switch (serv->version) - { - case 4: - prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP, &serv->v4.ip4address, - tunnel, state, (struct ip_pkt *) buf); - break; - case 6: - prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP, &serv->v6.ip6address, - tunnel, state, (struct ip6_pkt *) buf); - - break; - default: - GNUNET_assert (0); - break; - } - - hash_redirect_info (&state->hash, &state->redirect_info, - serv->version == 4 ? 4 : 16); - - if (GNUNET_NO == - GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash)) - { - GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); - - state->heap_node = - GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state, - GNUNET_TIME_absolute_get ().abs_value); - - if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) > - max_tcp_connections) - GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap); - } - else - GNUNET_free (state); - - /* FIXME: here, flow-control with mesh would be nice to have... */ - (void) GNUNET_HELPER_send (helper_handle, - (const struct GNUNET_MessageHeader*) buf, - GNUNET_YES, - NULL, NULL); - } - return GNUNET_YES; -} - - -static int -receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, - void **tunnel_ctx GNUNET_UNUSED, - const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED, - const struct GNUNET_MessageHeader *message, - const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) -{ - GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); - struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1); - struct remote_addr *s = (struct remote_addr *) desc; - char *buf; - size_t len; - uint16_t pkt_len = - ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - - sizeof (GNUNET_HashCode); - - struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state)); - - state->tunnel = tunnel; - state->type = REMOTE; - state->hashmap = tcp_connections; - memcpy (&state->remote, s, sizeof (struct remote_addr)); - - len = - sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + - sizeof (struct ip6_hdr) + pkt_len; - buf = alloca (len); - - memset (buf, 0, len); - - switch (s->addrlen) - { - case 4: - prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP, &s->addr, tunnel, - state, (struct ip_pkt *) buf); - break; - case 16: - prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP, &s->addr, tunnel, - state, (struct ip6_pkt *) buf); - break; - default: - GNUNET_free (state); - return GNUNET_SYSERR; - } - - hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet from remote; hash is %x\n", - *((uint32_t *) & state->hash)); - - if (GNUNET_NO == - GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash)) - { - GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); - - state->heap_node = - GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state, - GNUNET_TIME_absolute_get ().abs_value); - - if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) > - max_tcp_connections) - GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap); - } - else - GNUNET_free (state); - - /* FIXME: here, flow-control with mesh would be nice to have... */ - (void) GNUNET_HELPER_send (helper_handle, - (const struct GNUNET_MessageHeader*) buf, - GNUNET_YES, - NULL, NULL); - return GNUNET_YES; - -} - -static int -receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, - void **tunnel_ctx GNUNET_UNUSED, - const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED, - const struct GNUNET_MessageHeader *message, - const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) -{ - GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); - struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1); - struct remote_addr *s = (struct remote_addr *) desc; - char *buf; - size_t len; - - GNUNET_assert (ntohs (pkt->len) == - ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - - sizeof (GNUNET_HashCode)); - - /* Prepare the state. - * This will be saved in the hashmap, so that the receiving procedure knows - * through which tunnel this connection has to be routed. - */ - struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state)); - - state->tunnel = tunnel; - state->hashmap = udp_connections; - state->type = REMOTE; - memcpy (&state->remote, s, sizeof (struct remote_addr)); - - len = - sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + - sizeof (struct ip6_hdr) + ntohs (pkt->len); - buf = alloca (len); - - memset (buf, 0, len); - - switch (s->addrlen) - { - case 4: - prepare_ipv4_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, &s->addr, - tunnel, state, (struct ip_pkt *) buf); - break; - case 16: - prepare_ipv6_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, &s->addr, - tunnel, state, (struct ip6_pkt *) buf); - break; - default: - GNUNET_assert (0); - break; - } - - hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen); - - if (GNUNET_NO == - GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash)) - { - GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); - - state->heap_node = - GNUNET_CONTAINER_heap_insert (udp_connections_heap, state, - GNUNET_TIME_absolute_get ().abs_value); - - if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) > - max_udp_connections) - GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap); - } - else - GNUNET_free (state); - - (void) GNUNET_HELPER_send (helper_handle, - (const struct GNUNET_MessageHeader*) buf, - GNUNET_YES, - NULL, NULL); - return GNUNET_YES; -} - -/** - * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt - */ -static int -receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, - void **tunnel_ctx, - const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED, - const struct GNUNET_MessageHeader *message, - const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) -{ - GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); - struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1); - - GNUNET_assert (ntohs (pkt->len) == - ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - - sizeof (GNUNET_HashCode)); - - /* Get the configuration from the hashmap */ - uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2); - - memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode)); - *udp_desc = ntohs (pkt->dpt); - struct redirect_service *serv = - GNUNET_CONTAINER_multihashmap_get (udp_services, - (GNUNET_HashCode *) udp_desc); - - if (NULL == serv) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for UDP dpt %d!\n", - *udp_desc); - return GNUNET_YES; - } - - pkt->dpt = htons (serv->remote_port); - - /* - * At this point it would be possible to check against some kind of ACL. - */ - - char *buf; - size_t len; - - /* Prepare the state. - * This will be saved in the hashmap, so that the receiving procedure knows - * through which tunnel this connection has to be routed. - */ - struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state)); - - state->tunnel = tunnel; - state->serv = serv; - state->type = SERVICE; - state->hashmap = udp_connections; - memcpy (&state->desc, desc, sizeof (GNUNET_HashCode)); - - len = - sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + - sizeof (struct ip6_hdr) + ntohs (pkt->len); - buf = alloca (len); - - memset (buf, 0, len); - - switch (serv->version) - { - case 4: - prepare_ipv4_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, - &serv->v4.ip4address, tunnel, state, - (struct ip_pkt *) buf); - break; - case 6: - prepare_ipv6_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, - &serv->v6.ip6address, tunnel, state, - (struct ip6_pkt *) buf); - - break; - default: - GNUNET_assert (0); - break; - } - - hash_redirect_info (&state->hash, &state->redirect_info, - serv->version == 4 ? 4 : 16); - - if (GNUNET_NO == - GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash)) - { - GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); - - state->heap_node = - GNUNET_CONTAINER_heap_insert (udp_connections_heap, state, - GNUNET_TIME_absolute_get ().abs_value); - - if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) > - max_udp_connections) - GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap); - } - else - GNUNET_free (state); - - (void) GNUNET_HELPER_send (helper_handle, - (const struct GNUNET_MessageHeader*) buf, - GNUNET_YES, - NULL, NULL); - return GNUNET_YES; -} - -static void -connect_to_mesh () -{ - int udp, tcp; - int handler_idx, app_idx; - - udp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_UDP"); - tcp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_TCP"); - - static struct GNUNET_MESH_MessageHandler handlers[] = { - {receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0}, - {receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP, 0}, - {NULL, 0, 0}, - {NULL, 0, 0}, - {NULL, 0, 0} - }; - - static GNUNET_MESH_ApplicationType apptypes[] = { - GNUNET_APPLICATION_TYPE_END, - GNUNET_APPLICATION_TYPE_END, - GNUNET_APPLICATION_TYPE_END - }; - - app_idx = 0; - handler_idx = 2; - - if (GNUNET_YES == udp) - { - handlers[handler_idx].callback = receive_udp_remote; - handlers[handler_idx].expected_size = 0; - handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP; - apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY; - handler_idx++; - app_idx++; - } - - if (GNUNET_YES == tcp) - { - handlers[handler_idx].callback = receive_tcp_remote; - handlers[handler_idx].expected_size = 0; - handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP; - apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY; - handler_idx++; - app_idx++; - } - - mesh_handle = - GNUNET_MESH_connect (cfg, 42, NULL, new_tunnel, clean_tunnel, handlers, - apptypes); -} - - - -/** - * @brief Main function that will be run by the scheduler. - * - * @param cls closure - * @param args remaining command-line arguments - * @param cfgfile name of the configuration file used (for saving, can be NULL!) - * @param cfg_ configuration - */ -static void -run (void *cls, char *const *args GNUNET_UNUSED, - const char *cfgfile GNUNET_UNUSED, - const struct GNUNET_CONFIGURATION_Handle *cfg_) -{ - char *ifname; - char *ipv6addr; - char *ipv6prefix; - char *ipv4addr; - char *ipv4mask; - - cfg = cfg_; - connect_to_mesh (); - udp_connections = GNUNET_CONTAINER_multihashmap_create (65536); - udp_connections_heap = - GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); - tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536); - tcp_connections_heap = - GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); - udp_services = GNUNET_CONTAINER_multihashmap_create (65536); - tcp_services = GNUNET_CONTAINER_multihashmap_create (65536); - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS", - &max_udp_connections)) - max_udp_connections = 1024; - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS", - &max_tcp_connections)) - max_tcp_connections = 256; - GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL); - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IFNAME", &ifname)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IFNAME' in configuration!\n"); - exit (1); - } - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR", - &ipv6addr)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV6ADDR' in configuration!\n"); - exit (1); - } - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX", - &ipv6prefix)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV6PREFIX' in configuration!\n"); - exit (1); - } - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR", - &ipv4addr)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV4ADDR' in configuration!\n"); - exit (1); - } - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK", - &ipv4mask)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV4MASK' in configuration!\n"); - exit (1); - } - exit_argv[0] = GNUNET_strdup ("exit-gnunet"); - exit_argv[1] = ifname; - exit_argv[2] = ipv6addr; - exit_argv[3] = ipv6prefix; - exit_argv[4] = ipv4addr; - exit_argv[5] = ipv4mask; - exit_argv[6] = NULL; - helper_handle = GNUNET_HELPER_start ("gnunet-helper-vpn", exit_argv, - &message_token, NULL); - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls); -} - - -/** - * The main function - * - * @param argc number of arguments from the command line - * @param argv command line arguments - * @return 0 ok, 1 on error - */ -int -main (int argc, char *const *argv) -{ - static const struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_OPTION_END - }; - - return (GNUNET_OK == - GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit", - gettext_noop - ("Daemon to run to provide an IP exit node for the VPN"), - options, &run, NULL)) ? ret : 1; -} - - -/* end of gnunet-daemon-exit.c */ diff --git a/src/vpn/gnunet-daemon-vpn.c b/src/vpn/gnunet-daemon-vpn.c deleted file mode 100644 index 6945fc772..000000000 --- a/src/vpn/gnunet-daemon-vpn.c +++ /dev/null @@ -1,1808 +0,0 @@ -/* - This file is part of GNUnet. - (C) 2010 Christian Grothoff - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. -*/ - -/** - * @file vpn/gnunet-daemon-vpn.c - * @brief - * @author Philipp Toelke - */ -#include "platform.h" -#include "gnunet_getopt_lib.h" -#include "gnunet_program_lib.h" -#include "gnunet-vpn-packet.h" -#include "gnunet_common.h" -#include "gnunet_protocols.h" -#include "gnunet_applications.h" -#include -#include "gnunet_client_lib.h" -#include "gnunet_container_lib.h" -#include "gnunet_constants.h" -#include -#include "gnunet_dns_service.h" - - -struct answer_packet_list -{ - struct answer_packet_list *next; - struct answer_packet_list *prev; - struct GNUNET_SERVER_Client *client; - struct answer_packet pkt; -}; - - -struct map_entry -{ - /** The description of the service (used for service) */ - struct GNUNET_vpn_service_descriptor desc; - - /** The real address of the service (used for remote) */ - char addrlen; - char addr[16]; - - struct GNUNET_MESH_Tunnel *tunnel; - uint16_t namelen; - char additional_ports[8192]; - - struct GNUNET_CONTAINER_HeapNode *heap_node; - GNUNET_HashCode hash; - /** - * After this struct the name is located in DNS-Format! - */ -}; - - -struct remote_addr -{ - char addrlen; - unsigned char addr[16]; - char proto; -}; - - -struct tunnel_notify_queue -{ - struct tunnel_notify_queue *next; - struct tunnel_notify_queue *prev; - size_t len; - void *cls; -}; - - -struct tunnel_state -{ - struct GNUNET_MESH_TransmitHandle *th; - struct tunnel_notify_queue *head, *tail; - - int addrlen; -}; - - -static const struct GNUNET_CONFIGURATION_Handle *cfg; - -static struct GNUNET_MESH_Handle *mesh_handle; - -static struct GNUNET_CONTAINER_MultiHashMap *hashmap; - -static struct GNUNET_CONTAINER_Heap *heap; - -/** - * The handle to the helper - */ -static struct GNUNET_HELPER_Handle *helper_handle; - -/** - * Arguments to the exit helper. - */ -static char *vpn_argv[7]; - -static struct GNUNET_DNS_Handle *dns_handle; - -static struct answer_packet_list *answer_proc_head; - -static struct answer_packet_list *answer_proc_tail; - -/** - * If there are at least this many address-mappings, old ones will be removed - */ -static long long unsigned int max_mappings = 200; - -/** - * Final status code. - */ -static int ret; - -/** - * This hashmap contains the mapping from peer, service-descriptor, - * source-port and destination-port to a socket - */ -static struct GNUNET_CONTAINER_MultiHashMap *udp_connections; - -static GNUNET_SCHEDULER_TaskIdentifier conn_task; - -static GNUNET_SCHEDULER_TaskIdentifier shs_task; - -/** - * The tunnels that will be used to send tcp- and udp-packets - */ -static struct GNUNET_MESH_Tunnel *tcp_tunnel; -static struct GNUNET_MESH_Tunnel *udp_tunnel; - - -static unsigned int -port_in_ports (uint64_t ports, uint16_t port) -{ - uint16_t *ps = (uint16_t *) & ports; - - return ports == 0 || ps[0] == port || ps[1] == port || ps[2] == port || - ps[3] == port; -} - - -/** - * Sets a bit active in a bitArray. - * - * @param bitArray memory area to set the bit in - * @param bitIdx which bit to set - */ -static void -setBit (char *bitArray, unsigned int bitIdx) -{ - size_t arraySlot; - unsigned int targetBit; - - arraySlot = bitIdx / 8; - targetBit = (1L << (bitIdx % 8)); - bitArray[arraySlot] |= targetBit; -} - - -/** - * Checks if a bit is active in the bitArray - * - * @param bitArray memory area to set the bit in - * @param bitIdx which bit to test - * @return GNUNET_YES if the bit is set, GNUNET_NO if not. - */ -static int -testBit (char *bitArray, unsigned int bitIdx) -{ - size_t slot; - unsigned int targetBit; - - slot = bitIdx / 8; - targetBit = (1L << (bitIdx % 8)); - if (bitArray[slot] & targetBit) - return GNUNET_YES; - else - return GNUNET_NO; -} - - -/** - * Function scheduled as very last function, cleans up after us - *{{{ - */ -static void -cleanup (void *cls GNUNET_UNUSED, - const struct GNUNET_SCHEDULER_TaskContext *tskctx) -{ - unsigned int i; - - GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)); - /* close the connection to the service-dns */ - GNUNET_DNS_disconnect (dns_handle); - if (mesh_handle != NULL) - { - GNUNET_MESH_disconnect (mesh_handle); - mesh_handle = NULL; - } - if (helper_handle != NULL) - { - GNUNET_HELPER_stop (helper_handle); - helper_handle = NULL; - } - if (GNUNET_SCHEDULER_NO_TASK != shs_task) - { - GNUNET_SCHEDULER_cancel (shs_task); - shs_task = GNUNET_SCHEDULER_NO_TASK; - } - if (GNUNET_SCHEDULER_NO_TASK != conn_task) - { - GNUNET_SCHEDULER_cancel (conn_task); - conn_task = GNUNET_SCHEDULER_NO_TASK; - } - for (i=0;i<5;i++) - GNUNET_free_non_null (vpn_argv[i]); -} - -/*}}}*/ - -/** - * @return the hash of the IP-Address if a mapping exists, NULL otherwise - */ -static GNUNET_HashCode * -address6_mapping_exists (struct in6_addr *v6addr) -{ - unsigned char *addr = (unsigned char*) v6addr; - GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode)); - unsigned char *k = (unsigned char *) key; - - memset (key, 0, sizeof (GNUNET_HashCode)); - unsigned int i; - - for (i = 0; i < 16; i++) - k[15 - i] = addr[i]; - - if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key)) - return key; - else - { - GNUNET_free (key); - return NULL; - } -} - -/** - * @return the hash of the IP-Address if a mapping exists, NULL otherwise - */ -static GNUNET_HashCode * -address4_mapping_exists (uint32_t addr) -{ - GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode)); - - memset (key, 0, sizeof (GNUNET_HashCode)); - unsigned char *c = (unsigned char *) &addr; - unsigned char *k = (unsigned char *) key; - unsigned int i; - - for (i = 0; i < 4; i++) - k[3 - i] = c[i]; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "a4_m_e: getting with key %08x, addr is %08x, %d.%d.%d.%d\n", - *((uint32_t *) (key)), addr, c[0], c[1], c[2], c[3]); - - if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key)) - return key; - else - { - GNUNET_free (key); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n"); - return NULL; - } -} - - -static void * -initialize_tunnel_state (int addrlen, struct GNUNET_MESH_TransmitHandle *th) -{ - struct tunnel_state *ts = GNUNET_malloc (sizeof *ts); - - ts->addrlen = addrlen; - ts->th = th; - return ts; -} - - -static void -send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) - return; - - struct ip_icmp *request = cls; - - struct ip_icmp *response = alloca (ntohs (request->shdr.size)); - - GNUNET_assert (response != NULL); - memset (response, 0, ntohs (request->shdr.size)); - - response->shdr.size = request->shdr.size; - response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); - - response->tun.flags = 0; - response->tun.type = htons (0x0800); - - response->ip_hdr.hdr_lngth = 5; - response->ip_hdr.version = 4; - response->ip_hdr.proto = 0x01; - response->ip_hdr.dadr = request->ip_hdr.sadr; - response->ip_hdr.sadr = request->ip_hdr.dadr; - response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth; - - response->ip_hdr.chks = - GNUNET_CRYPTO_crc16_n ((uint16_t *) & response->ip_hdr, 20); - - response->icmp_hdr.code = 0; - response->icmp_hdr.type = 0x0; - - /* Magic, more Magic! */ - response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8; - - /* Copy the rest of the packet */ - memcpy (response + 1, request + 1, - ntohs (request->shdr.size) - sizeof (struct ip_icmp)); - - (void) GNUNET_HELPER_send (helper_handle, - &response->shdr, - GNUNET_YES, - NULL, NULL); - GNUNET_free (request); -} - - -static void -send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) - return; - - struct ip6_icmp *request = cls; - - struct ip6_icmp *response = alloca (ntohs (request->shdr.size)); - - GNUNET_assert (response != NULL); - memset (response, 0, ntohs (request->shdr.size)); - - response->shdr.size = request->shdr.size; - response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); - - response->tun.flags = 0; - response->tun.type = htons (0x86dd); - - response->ip6_hdr.hoplmt = 255; - response->ip6_hdr.paylgth = request->ip6_hdr.paylgth; - response->ip6_hdr.nxthdr = 0x3a; - response->ip6_hdr.version = 6; - memcpy (&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16); - memcpy (&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16); - - response->icmp_hdr.code = 0; - response->icmp_hdr.type = 0x81; - - /* Magic, more Magic! */ - response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1; - - /* Copy the rest of the packet */ - memcpy (response + 1, request + 1, - ntohs (request->shdr.size) - sizeof (struct ip6_icmp)); - - (void) GNUNET_HELPER_send (helper_handle, - &response->shdr, - GNUNET_YES, - NULL, NULL); - GNUNET_free (request); -} - - -/** - * cls is the pointer to a GNUNET_MessageHeader that is - * followed by the service-descriptor and the packet that should be sent; - */ -static size_t -send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf) -{ - struct GNUNET_MESH_Tunnel **tunnel = cls; - - struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel); - - ts->th = NULL; - - if (NULL != buf) - { - struct GNUNET_MessageHeader *hdr = - (struct GNUNET_MessageHeader *) (tunnel + 1); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf, - size); - GNUNET_assert (size >= ntohs (hdr->size)); - memcpy (buf, hdr, ntohs (hdr->size)); - size = ntohs (hdr->size); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n"); - } - else - size = 0; - - if (NULL != ts->head) - { - struct tunnel_notify_queue *element = ts->head; - - GNUNET_CONTAINER_DLL_remove (ts->head, ts->tail, element); - - ts->th = - GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42, - GNUNET_TIME_relative_divide - (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), - (const struct GNUNET_PeerIdentity *) - NULL, element->len, - send_pkt_to_peer_notify_callback, - element->cls); - - /* save the handle */ - GNUNET_free (element); - } - GNUNET_free (cls); - - return size; -} - - -static void -send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) -{ - /* peer == NULL means that all peers in this request are connected */ - if (peer == NULL) - return; - struct GNUNET_MESH_Tunnel **tunnel = cls; - struct GNUNET_MessageHeader *hdr = - (struct GNUNET_MessageHeader *) (tunnel + 1); - - GNUNET_assert (NULL != tunnel); - GNUNET_assert (NULL != *tunnel); - - struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel); - - if (NULL == ts->th) - { - ts->th = - GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42, - GNUNET_TIME_relative_divide - (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), - (const struct GNUNET_PeerIdentity *) - NULL, ntohs (hdr->size), - send_pkt_to_peer_notify_callback, - cls); - } - else - { - struct tunnel_notify_queue *element = GNUNET_malloc (sizeof *element); - - element->cls = cls; - element->len = ntohs (hdr->size); - - GNUNET_CONTAINER_DLL_insert_tail (ts->head, ts->tail, element); - } -} - - - - -/** - * Receive packets from the helper-process - */ -static void -message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED, - const struct GNUNET_MessageHeader *message) -{ - GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER); - - struct tun_pkt *pkt_tun = (struct tun_pkt *) message; - GNUNET_HashCode *key; - - /* ethertype is ipv6 */ - if (ntohs (pkt_tun->tun.type) == 0x86dd) - { - struct ip6_pkt *pkt6 = (struct ip6_pkt *) message; - - GNUNET_assert (pkt6->ip6_hdr.version == 6); - struct ip6_tcp *pkt6_tcp; - struct ip6_udp *pkt6_udp; - struct ip6_icmp *pkt6_icmp; - - pkt6_udp = NULL; /* make compiler happy */ - switch (pkt6->ip6_hdr.nxthdr) - { - case IPPROTO_UDP: - pkt6_udp = (struct ip6_udp *) pkt6; - /* Send dns-packets to the service-dns */ - if (ntohs (pkt6_udp->udp_hdr.dpt) == 53) - { - /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */ - GNUNET_DNS_queue_request_v6 (dns_handle, - &pkt6->ip6_hdr.dadr, - &pkt6->ip6_hdr.sadr, - ntohs (pkt6_udp->udp_hdr.spt), - ntohs (pkt6_udp->udp_hdr.len) - 8, - (const void*) pkt6_udp->data); - - break; - } - /* fall through */ - case IPPROTO_TCP: - pkt6_tcp = (struct ip6_tcp *) pkt6; - - if ((key = address6_mapping_exists (&pkt6->ip6_hdr.dadr)) != NULL) - { - struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key); - - GNUNET_assert (me != NULL); - GNUNET_free (key); - - size_t size = - sizeof (struct GNUNET_MESH_Tunnel *) + - sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + - ntohs (pkt6->ip6_hdr.paylgth); - - struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size); - struct GNUNET_MessageHeader *hdr = - (struct GNUNET_MessageHeader *) (cls + 1); - GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1); - - hdr->size = - htons (sizeof (struct GNUNET_MessageHeader) + - sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth)); - - GNUNET_MESH_ApplicationType app_type = 0; /* fix compiler uninitialized warning... */ - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n", - me->addrlen); - if (me->addrlen == 0) - { - /* This is a mapping to a gnunet-service */ - memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode)); - - if (IPPROTO_UDP == pkt6->ip6_hdr.nxthdr && - (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)) && - (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt) || - testBit (me->additional_ports, ntohs (pkt6_udp->udp_hdr.dpt)))) - { - hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP); - - memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len)); - - } - else if (IPPROTO_TCP == pkt6->ip6_hdr.nxthdr && - (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP)) - && (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt))) - { - hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP); - - memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth)); - - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pip: %d\n", - port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)); - GNUNET_assert (0); - } - if (me->tunnel == NULL && NULL != cls) - { - *cls = - GNUNET_MESH_tunnel_create (mesh_handle, - initialize_tunnel_state (16, NULL), - &send_pkt_to_peer, NULL, cls); - - GNUNET_MESH_peer_request_connect_add (*cls, - (struct GNUNET_PeerIdentity *) - &me->desc.peer); - me->tunnel = *cls; - } - else if (NULL != cls) - { - *cls = me->tunnel; - send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Queued to send IPv6 to peer %x, type %d\n", - *((unsigned int *) &me->desc.peer), ntohs (hdr->type)); - } - } - else - { - /* This is a mapping to a "real" address */ - struct remote_addr *s = (struct remote_addr *) hc; - - s->addrlen = me->addrlen; - memcpy (s->addr, me->addr, me->addrlen); - s->proto = pkt6->ip6_hdr.nxthdr; - if (s->proto == IPPROTO_UDP) - { - hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP); - memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len)); - app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY; - if (NULL != udp_tunnel) - me->tunnel = udp_tunnel; - } - else if (s->proto == IPPROTO_TCP) - { - hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP); - memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth)); - app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY; - if (NULL != tcp_tunnel) - me->tunnel = tcp_tunnel; - } - else - { - GNUNET_assert (0); - } - if (me->tunnel == NULL && NULL != cls) - { - *cls = - GNUNET_MESH_tunnel_create (mesh_handle, - initialize_tunnel_state (16, NULL), - &send_pkt_to_peer, NULL, cls); - - GNUNET_MESH_peer_request_connect_by_type (*cls, app_type); - me->tunnel = *cls; - if (GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY == app_type) - udp_tunnel = *cls; - else if (GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY == app_type) - tcp_tunnel = *cls; - } - else if (NULL != cls) - { - *cls = me->tunnel; - send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL); - } - } - } - else - { - char pbuf[INET6_ADDRSTRLEN]; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Packet to %s, which has no mapping\n", - inet_ntop (AF_INET6, - &pkt6->ip6_hdr.dadr, - pbuf, - sizeof (pbuf))); - } - break; - case 0x3a: - /* ICMPv6 */ - pkt6_icmp = (struct ip6_icmp *) pkt6; - /* If this packet is an icmp-echo-request and a mapping exists, answer */ - if (pkt6_icmp->icmp_hdr.type == 0x80 && - (key = address6_mapping_exists (&pkt6->ip6_hdr.dadr)) != NULL) - { - GNUNET_free (key); - pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size)); - memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size)); - GNUNET_SCHEDULER_add_now (&send_icmp6_response, pkt6_icmp); - } - break; - } - } - /* ethertype is ipv4 */ - else if (ntohs (pkt_tun->tun.type) == 0x0800) - { - struct ip_pkt *pkt = (struct ip_pkt *) message; - struct ip_udp *udp = (struct ip_udp *) message; - struct ip_tcp *pkt_tcp; - struct ip_udp *pkt_udp; - struct ip_icmp *pkt_icmp; - - GNUNET_assert (pkt->ip_hdr.version == 4); - - /* Send dns-packets to the service-dns */ - if (pkt->ip_hdr.proto == IPPROTO_UDP && ntohs (udp->udp_hdr.dpt) == 53) - { - GNUNET_DNS_queue_request_v4 (dns_handle, - &pkt->ip_hdr.dadr, - &pkt->ip_hdr.sadr, - ntohs (udp->udp_hdr.spt), - ntohs (udp->udp_hdr.len) - 8, - (const void*) udp->data); - } - else - { - uint32_t dadr = pkt->ip_hdr.dadr.s_addr; - unsigned char *c = (unsigned char *) &dadr; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet to %d.%d.%d.%d, proto %x\n", - c[0], c[1], c[2], c[3], pkt->ip_hdr.proto); - switch (pkt->ip_hdr.proto) - { - case IPPROTO_TCP: - case IPPROTO_UDP: - pkt_tcp = (struct ip_tcp *) pkt; - pkt_udp = (struct ip_udp *) pkt; - - if ((key = address4_mapping_exists (dadr)) != NULL) - { - struct map_entry *me = - GNUNET_CONTAINER_multihashmap_get (hashmap, key); - GNUNET_assert (me != NULL); - GNUNET_free (key); - - size_t size = - sizeof (struct GNUNET_MESH_Tunnel *) + - sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + - ntohs (pkt->ip_hdr.tot_lngth) - 4 * pkt->ip_hdr.hdr_lngth; - - struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size); - struct GNUNET_MessageHeader *hdr = - (struct GNUNET_MessageHeader *) (cls + 1); - GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1); - - hdr->size = - htons (sizeof (struct GNUNET_MessageHeader) + - sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) - - 4 * pkt->ip_hdr.hdr_lngth); - - GNUNET_MESH_ApplicationType app_type = 0; /* make compiler happy */ - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n", - me->addrlen); - if (me->addrlen == 0) - { - /* This is a mapping to a gnunet-service */ - memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode)); - - if ((IPPROTO_UDP == pkt->ip_hdr.proto) && - (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)) && - (port_in_ports (me->desc.ports, pkt_udp->udp_hdr.dpt) || - testBit (me->additional_ports, ntohs (pkt_udp->udp_hdr.dpt)))) - { - hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP); - - memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len)); - - } - else if ((IPPROTO_TCP == pkt->ip_hdr.proto) && - (me-> - desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP)) - && (port_in_ports (me->desc.ports, pkt_tcp->tcp_hdr.dpt))) - { - hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP); - - memcpy (hc + 1, &pkt_tcp->tcp_hdr, - ntohs (pkt->ip_hdr.tot_lngth) - - 4 * pkt->ip_hdr.hdr_lngth); - - } - if (me->tunnel == NULL && NULL != cls) - { - *cls = - GNUNET_MESH_tunnel_create (mesh_handle, - initialize_tunnel_state (4, NULL), - send_pkt_to_peer, NULL, cls); - GNUNET_MESH_peer_request_connect_add (*cls, - (struct GNUNET_PeerIdentity - *) &me->desc.peer); - me->tunnel = *cls; - } - else if (NULL != cls) - { - *cls = me->tunnel; - send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Queued to send IPv4 to peer %x, type %d\n", - *((unsigned int *) &me->desc.peer), - ntohs (hdr->type)); - } - } - else - { - /* This is a mapping to a "real" address */ - struct remote_addr *s = (struct remote_addr *) hc; - - s->addrlen = me->addrlen; - memcpy (s->addr, me->addr, me->addrlen); - s->proto = pkt->ip_hdr.proto; - if (s->proto == IPPROTO_UDP) - { - hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP); - memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len)); - app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY; - } - else if (s->proto == IPPROTO_TCP) - { - hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP); - memcpy (hc + 1, &pkt_tcp->tcp_hdr, - ntohs (pkt->ip_hdr.tot_lngth) - - 4 * pkt->ip_hdr.hdr_lngth); - app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY; - } - else - GNUNET_assert (0); - if (me->tunnel == NULL && NULL != cls) - { - *cls = - GNUNET_MESH_tunnel_create (mesh_handle, - initialize_tunnel_state (4, NULL), - send_pkt_to_peer, NULL, cls); - - GNUNET_MESH_peer_request_connect_by_type (*cls, app_type); - me->tunnel = *cls; - } - else if (NULL != cls) - { - *cls = me->tunnel; - send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL); - } - } - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Packet to %x which has no mapping\n", dadr); - } - break; - case 0x01: - /* ICMP */ - pkt_icmp = (struct ip_icmp *) pkt; - if (pkt_icmp->icmp_hdr.type == 0x8 && - (key = address4_mapping_exists (dadr)) != NULL) - { - GNUNET_free (key); - pkt_icmp = GNUNET_malloc (ntohs (pkt->shdr.size)); - memcpy (pkt_icmp, pkt, ntohs (pkt->shdr.size)); - GNUNET_SCHEDULER_add_now (&send_icmp4_response, pkt_icmp); - } - break; - } - } - } -} - - - -static void -collect_mappings (void *cls GNUNET_UNUSED, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) - return; - - struct map_entry *me = GNUNET_CONTAINER_heap_remove_root (heap); - - /* This is free()ed memory! */ - me->heap_node = NULL; - - /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */ - - GNUNET_assert (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me)); - - GNUNET_free (me); -} - - -/** - * Create a new Address from an answer-packet - */ -static void -new_ip6addr (struct in6_addr *v6addr, - const GNUNET_HashCode * peer, - const GNUNET_HashCode * service_desc) -{ /* {{{ */ - unsigned char *buf = (unsigned char*) v6addr; - char *ipv6addr; - unsigned long long ipv6prefix; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR", - &ipv6addr)); - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", - "IPV6PREFIX", - &ipv6prefix)); - GNUNET_assert (ipv6prefix < 127); - ipv6prefix = (ipv6prefix + 7) / 8; - - inet_pton (AF_INET6, ipv6addr, buf); - GNUNET_free (ipv6addr); - - int peer_length = 16 - ipv6prefix - 6; - - if (peer_length <= 0) - peer_length = 0; - - int service_length = 16 - ipv6prefix - peer_length; - - if (service_length <= 0) - service_length = 0; - - memcpy (buf + ipv6prefix, service_desc, service_length); - memcpy (buf + ipv6prefix + service_length, peer, peer_length); -} - -/*}}}*/ - - -/** - * Create a new Address from an answer-packet - */ -static void -new_ip6addr_remote (struct in6_addr *v6addr, - unsigned char *addr, char addrlen) -{ /* {{{ */ - unsigned char *buf = (unsigned char*) v6addr; - char *ipv6addr; - unsigned long long ipv6prefix; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR", - &ipv6addr)); - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", - "IPV6PREFIX", - &ipv6prefix)); - GNUNET_assert (ipv6prefix < 127); - ipv6prefix = (ipv6prefix + 7) / 8; - - inet_pton (AF_INET6, ipv6addr, buf); - GNUNET_free (ipv6addr); - - int local_length = 16 - ipv6prefix; - - memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length)); -} - -/*}}}*/ - -/** - * Create a new Address from an answer-packet - */ -static void -new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen) -{ /* {{{ */ - char *ipv4addr; - char *ipv4mask; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR", - &ipv4addr)); - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK", - &ipv4mask)); - uint32_t mask; - - inet_pton (AF_INET, ipv4addr, buf); - int r = inet_pton (AF_INET, ipv4mask, &mask); - - mask = htonl (mask); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r, - mask); - - GNUNET_free (ipv4addr); - - int c; - - if (mask) - { - mask = (mask ^ (mask - 1)) >> 1; - for (c = 0; mask; c++) - { - mask >>= 1; - } - } - else - { - c = CHAR_BIT * sizeof (mask); - } - - c = 32 - c; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n", - ipv4mask, c); - - GNUNET_free (ipv4mask); - - if (c % 8 == 0) - c = c / 8; - else - GNUNET_assert (0); - - memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4 - c)); -} - -/*}}}*/ - -/** - * This gets scheduled with cls pointing to an answer_packet and does everything - * needed in order to send it to the helper. - * - * At the moment this means "inventing" and IPv6-Address for .gnunet-services and - * doing nothing for "real" services. - */ -static void -process_answer (void *cls, - const struct answer_packet *pkt) -{ - struct answer_packet_list *list; - - /* This answer is about a .gnunet-service - * - * It contains an almost complete DNS-Response, we have to fill in the ip - * at the offset pkt->addroffset - */ - if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE) - { - - GNUNET_HashCode key; - - memset (&key, 0, sizeof (GNUNET_HashCode)); - - list = - GNUNET_malloc (htons (pkt->hdr.size) + - sizeof (struct answer_packet_list) - - sizeof (struct answer_packet)); - memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); - - unsigned char *c = ((unsigned char *) &list->pkt) + ntohs (pkt->addroffset); - unsigned char *k = (unsigned char *) &key; - - new_ip6addr ((struct in6_addr*) c, - &pkt->service_descr.peer, - &pkt->service_descr.service_descriptor); - /* - * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed) - */ - unsigned int i; - - for (i = 0; i < 16; i++) - k[15 - i] = c[i]; - - uint16_t namelen = strlen ((char *) pkt->data + 12) + 1; - - struct map_entry *value = - GNUNET_malloc (sizeof (struct map_entry) + namelen); - char *name = (char *) (value + 1); - - value->namelen = namelen; - memcpy (name, pkt->data + 12, namelen); - - memcpy (&value->desc, &pkt->service_descr, - sizeof (struct GNUNET_vpn_service_descriptor)); - - memset (value->additional_ports, 0, 8192); - - memcpy (&value->hash, &key, sizeof (GNUNET_HashCode)); - - if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key)) - { - GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); - - value->heap_node = - GNUNET_CONTAINER_heap_insert (heap, value, - GNUNET_TIME_absolute_get ().abs_value); - if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings) - GNUNET_SCHEDULER_add_now (collect_mappings, NULL); - } - else - GNUNET_free (value); - - - list->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP; - - - } - else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV) - { - GNUNET_HashCode key; - - memset (&key, 0, sizeof key); - unsigned char *k = (unsigned char *) &key; - const unsigned char *s = pkt->data + 12; - int i = 0; - - /* Whoever designed the reverse IPv6-lookup is batshit insane */ - for (i = 0; i < 16; i++) - { - unsigned char c1 = s[(4 * i) + 1]; - unsigned char c2 = s[(4 * i) + 3]; - - if (c1 <= '9') - k[i] = c1 - '0'; - else - k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */ - if (c2 <= '9') - k[i] += 16 * (c2 - '0'); - else - k[i] += 16 * (c2 - 87); - } - - struct map_entry *map_entry = - GNUNET_CONTAINER_multihashmap_get (hashmap, &key); - uint16_t offset = ntohs (pkt->addroffset); - - if (map_entry == NULL) - return; - - GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node, - GNUNET_TIME_absolute_get ().abs_value); - - - unsigned short namelen = htons (map_entry->namelen); - char *name = (char *) (map_entry + 1); - - list = - GNUNET_malloc (sizeof (struct answer_packet_list) - - sizeof (struct answer_packet) + offset + 2 + - ntohs (namelen)); - - struct answer_packet *rpkt = &list->pkt; - - /* The offset points to the first byte belonging to the address */ - memcpy (rpkt, pkt, offset - 1); - - rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP; - rpkt->hdr.size = ntohs (offset + 2 + ntohs (namelen)); - - memcpy (((char *) rpkt) + offset, &namelen, 2); - memcpy (((char *) rpkt) + offset + 2, name, ntohs (namelen)); - - } - else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP) - { - list = - GNUNET_malloc (htons (pkt->hdr.size) + - sizeof (struct answer_packet_list) - - sizeof (struct answer_packet)); - memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); - } - else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA) - { - - GNUNET_HashCode key; - - memset (&key, 0, sizeof (GNUNET_HashCode)); - - list = - GNUNET_malloc (htons (pkt->hdr.size) + - sizeof (struct answer_packet_list) - - sizeof (struct answer_packet)); - - memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); - list->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP; - - unsigned char *c = ((unsigned char *) &list->pkt) + ntohs (list->pkt.addroffset); - - new_ip6addr_remote ((struct in6_addr*) c, - list->pkt.addr, list->pkt.addrsize); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", - c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], - c[10], c[11], c[12], c[13], c[14], c[15]); - unsigned char *k = (unsigned char *) &key; - - /* - * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table) - */ - unsigned int i; - - for (i = 0; i < 16; i++) - k[15 - i] = c[i]; - - uint16_t namelen = strlen ((char *) pkt->data + 12) + 1; - - struct map_entry *value = - GNUNET_malloc (sizeof (struct map_entry) + namelen); - char *name = (char *) (value + 1); - - value->namelen = namelen; - memcpy (name, pkt->data + 12, namelen); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", - pkt->addrsize); - value->addrlen = pkt->addrsize; - memcpy (&value->addr, &pkt->addr, pkt->addrsize); - memset (value->additional_ports, 0, 8192); - - memcpy (&value->hash, &key, sizeof (GNUNET_HashCode)); - - if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key)) - { - GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); - value->heap_node = - GNUNET_CONTAINER_heap_insert (heap, value, - GNUNET_TIME_absolute_get ().abs_value); - if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings) - GNUNET_SCHEDULER_add_now (collect_mappings, NULL); - } - else - GNUNET_free (value); - - - } - else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A) - { - list = - GNUNET_malloc (htons (pkt->hdr.size) + - sizeof (struct answer_packet_list) - - sizeof (struct answer_packet)); - - memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); - list->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP; - - GNUNET_HashCode key; - - memset (&key, 0, sizeof (GNUNET_HashCode)); - - unsigned char *c = ((unsigned char *) &list->pkt) + ntohs (pkt->addroffset); - - new_ip4addr_remote (c, list->pkt.addr, pkt->addrsize); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", c[0], - c[1], c[2], c[3]); - unsigned char *k = (unsigned char *) &key; - - /* - * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table) - */ - unsigned int i; - - for (i = 0; i < 4; i++) - k[3 - i] = c[i]; - - uint16_t namelen = strlen ((char *) pkt->data + 12) + 1; - - struct map_entry *value = - GNUNET_malloc (sizeof (struct map_entry) + namelen); - char *name = (char *) (value + 1); - - value->namelen = namelen; - memcpy (name, pkt->data + 12, namelen); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", - pkt->addrsize); - value->addrlen = pkt->addrsize; - memcpy (&value->addr, &pkt->addr, pkt->addrsize); - memset (value->additional_ports, 0, 8192); - - memcpy (&value->hash, &key, sizeof (GNUNET_HashCode)); - - if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key)) - { - GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); - value->heap_node = - GNUNET_CONTAINER_heap_insert (heap, value, - GNUNET_TIME_absolute_get ().abs_value); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Mapping is saved in the hashmap with key %08x.\n", - *((uint32_t *) (&key))); - if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings) - GNUNET_SCHEDULER_add_now (collect_mappings, NULL); - } - else - GNUNET_free (value); - - } - else - { - GNUNET_break (0); - return; - } - - GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail, - answer_proc_tail, list); - -} - - -/** - * @brief Add the port to the list of additional ports in the map_entry - * - * @param me the map_entry - * @param port the port in host-byte-order - */ -static void -add_additional_port (struct map_entry *me, uint16_t port) -{ - setBit (me->additional_ports, port); -} - -static int -receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, - void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender, - const struct GNUNET_MessageHeader *message, - const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) -{ - GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); - struct remote_addr *s = (struct remote_addr *) desc; - struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1); - const struct GNUNET_PeerIdentity *other = sender; - struct tunnel_state *ts = *tunnel_ctx; - - if (16 == ts->addrlen) - { - size_t size = - sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 - - sizeof (struct udp_pkt); - - struct ip6_udp *pkt6 = alloca (size); - - GNUNET_assert (pkt6 != NULL); - - if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK) - new_ip6addr (&pkt6->ip6_hdr.sadr, &other->hashPubKey, desc); - else - new_ip6addr_remote (&pkt6->ip6_hdr.sadr, s->addr, s->addrlen); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Relaying calc:%d gnu:%d udp:%d bytes!\n", size, - ntohs (message->size), ntohs (pkt->len)); - - pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); - pkt6->shdr.size = htons (size); - - pkt6->tun.flags = 0; - pkt6->tun.type = htons (0x86dd); - - pkt6->ip6_hdr.version = 6; - pkt6->ip6_hdr.tclass_h = 0; - pkt6->ip6_hdr.tclass_l = 0; - pkt6->ip6_hdr.flowlbl = 0; - pkt6->ip6_hdr.paylgth = pkt->len; - pkt6->ip6_hdr.nxthdr = IPPROTO_UDP; - pkt6->ip6_hdr.hoplmt = 0xff; - - { - char *ipv6addr; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", - "IPV6ADDR", - &ipv6addr)); - inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.dadr); - GNUNET_free (ipv6addr); - } - memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len)); - - GNUNET_HashCode *key = address6_mapping_exists (&pkt6->ip6_hdr.sadr); - - GNUNET_assert (key != NULL); - - struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key); - - GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node, - GNUNET_TIME_absolute_get ().abs_value); - - GNUNET_free (key); - - GNUNET_assert (me != NULL); - if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK) - { - GNUNET_assert (me->desc. - service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)); - if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt) && - !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt))) - { - add_additional_port (me, ntohs (pkt6->udp_hdr.spt)); - } - } - - pkt6->udp_hdr.crc = 0; - uint32_t sum = 0; - - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16); - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16); - uint32_t tmp = (pkt6->udp_hdr.len & 0xffff); - - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4); - tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff))); - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4); - - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->udp_hdr, - ntohs (pkt->len)); - pkt6->udp_hdr.crc = GNUNET_CRYPTO_crc16_finish (sum); - - (void) GNUNET_HELPER_send (helper_handle, - &pkt6->shdr, - GNUNET_YES, - NULL, NULL); - } - else - { - size_t size = - sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt); - - struct ip_udp *pkt4 = alloca (size); - - GNUNET_assert (pkt4 != NULL); - - GNUNET_assert (ntohs (message->type) == - GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK); - uint32_t sadr; - - new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen); - pkt4->ip_hdr.sadr.s_addr = sadr; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Relaying calc:%d gnu:%d udp:%d bytes!\n", size, - ntohs (message->size), ntohs (pkt->len)); - - pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); - pkt4->shdr.size = htons (size); - - pkt4->tun.flags = 0; - pkt4->tun.type = htons (0x0800); - - pkt4->ip_hdr.version = 4; - pkt4->ip_hdr.hdr_lngth = 5; - pkt4->ip_hdr.diff_serv = 0; - pkt4->ip_hdr.tot_lngth = htons (20 + ntohs (pkt->len)); - pkt4->ip_hdr.ident = 0; - pkt4->ip_hdr.flags = 0; - pkt4->ip_hdr.frag_off = 0; - pkt4->ip_hdr.ttl = 255; - pkt4->ip_hdr.proto = IPPROTO_UDP; - pkt4->ip_hdr.chks = 0; /* Will be calculated later */ - - { - char *ipv4addr; - uint32_t dadr; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", - "IPV4ADDR", - &ipv4addr)); - inet_pton (AF_INET, ipv4addr, &dadr); - GNUNET_free (ipv4addr); - pkt4->ip_hdr.dadr.s_addr = dadr; - } - memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len)); - - GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr.s_addr); - - GNUNET_assert (key != NULL); - - struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key); - - GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node, - GNUNET_TIME_absolute_get ().abs_value); - - GNUNET_free (key); - - GNUNET_assert (me != NULL); - - pkt4->udp_hdr.crc = 0; /* Optional for IPv4 */ - - pkt4->ip_hdr.chks = - GNUNET_CRYPTO_crc16_n ((uint16_t *) & pkt4->ip_hdr, 5 * 4); - - (void) GNUNET_HELPER_send (helper_handle, - &pkt4->shdr, - GNUNET_YES, - NULL, NULL); - } - - return GNUNET_OK; -} - -static int -receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, - void **tunnel_ctx, - const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED, - const struct GNUNET_MessageHeader *message, - const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) -{ - GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); - struct remote_addr *s = (struct remote_addr *) desc; - struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1); - const struct GNUNET_PeerIdentity *other = sender; - struct tunnel_state *ts = *tunnel_ctx; - - size_t pktlen = - ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - - sizeof (GNUNET_HashCode); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received TCP-Packet back, addrlen = %d\n", s->addrlen); - - if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK || - ts->addrlen == 16) - { - size_t size = pktlen + sizeof (struct ip6_tcp) - 1; - - struct ip6_tcp *pkt6 = alloca (size); - - memset (pkt6, 0, size); - - GNUNET_assert (pkt6 != NULL); - - if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK) - new_ip6addr (&pkt6->ip6_hdr.sadr, &other->hashPubKey, desc); - else - new_ip6addr_remote (&pkt6->ip6_hdr.sadr, s->addr, s->addrlen); - - pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); - pkt6->shdr.size = htons (size); - - pkt6->tun.flags = 0; - pkt6->tun.type = htons (0x86dd); - - pkt6->ip6_hdr.version = 6; - pkt6->ip6_hdr.tclass_h = 0; - pkt6->ip6_hdr.tclass_l = 0; - pkt6->ip6_hdr.flowlbl = 0; - pkt6->ip6_hdr.paylgth = htons (pktlen); - pkt6->ip6_hdr.nxthdr = IPPROTO_TCP; - pkt6->ip6_hdr.hoplmt = 0xff; - - { - char *ipv6addr; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", - "IPV6ADDR", - &ipv6addr)); - inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.dadr); - GNUNET_free (ipv6addr); - } - memcpy (&pkt6->tcp_hdr, pkt, pktlen); - - GNUNET_HashCode *key = address6_mapping_exists (&pkt6->ip6_hdr.sadr); - - GNUNET_assert (key != NULL); - - struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key); - - GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node, - GNUNET_TIME_absolute_get ().abs_value); - - GNUNET_free (key); - - GNUNET_assert (me != NULL); - if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK) - GNUNET_assert (me->desc. - service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP)); - - pkt6->tcp_hdr.crc = 0; - uint32_t sum = 0; - uint32_t tmp; - - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16); - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16); - tmp = htonl (pktlen); - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4); - tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff))); - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4); - - sum = - GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->tcp_hdr, - ntohs (pkt6->ip6_hdr.paylgth)); - pkt6->tcp_hdr.crc = GNUNET_CRYPTO_crc16_finish (sum); - - (void) GNUNET_HELPER_send (helper_handle, - &pkt6->shdr, - GNUNET_YES, - NULL, NULL); - } - else - { - size_t size = pktlen + sizeof (struct ip_tcp) - 1; - - struct ip_tcp *pkt4 = alloca (size); - - GNUNET_assert (pkt4 != NULL); - memset (pkt4, 0, size); - - GNUNET_assert (ntohs (message->type) == - GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK); - uint32_t sadr; - - new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen); - pkt4->ip_hdr.sadr.s_addr = sadr; - - pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); - pkt4->shdr.size = htons (size); - - pkt4->tun.flags = 0; - pkt4->tun.type = htons (0x0800); - - pkt4->ip_hdr.version = 4; - pkt4->ip_hdr.hdr_lngth = 5; - pkt4->ip_hdr.diff_serv = 0; - pkt4->ip_hdr.tot_lngth = htons (20 + pktlen); - pkt4->ip_hdr.ident = 0; - pkt4->ip_hdr.flags = 0; - pkt4->ip_hdr.frag_off = 0; - pkt4->ip_hdr.ttl = 255; - pkt4->ip_hdr.proto = IPPROTO_TCP; - pkt4->ip_hdr.chks = 0; /* Will be calculated later */ - - { - char *ipv4addr; - uint32_t dadr; - - GNUNET_assert (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", - "IPV4ADDR", - &ipv4addr)); - inet_pton (AF_INET, ipv4addr, &dadr); - GNUNET_free (ipv4addr); - pkt4->ip_hdr.dadr.s_addr = dadr; - } - - memcpy (&pkt4->tcp_hdr, pkt, pktlen); - - GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr.s_addr); - - GNUNET_assert (key != NULL); - - struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key); - - GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node, - GNUNET_TIME_absolute_get ().abs_value); - - GNUNET_free (key); - - GNUNET_assert (me != NULL); - pkt4->tcp_hdr.crc = 0; - uint32_t sum = 0; - uint32_t tmp; - - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) &pkt4->ip_hdr.sadr, 4); - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) &pkt4->ip_hdr.dadr, 4); - - tmp = (0x06 << 16) | (0xffff & pktlen); // 0x06 for TCP? - - tmp = htonl (tmp); - - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4); - - sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen); - pkt4->tcp_hdr.crc = GNUNET_CRYPTO_crc16_finish (sum); - - pkt4->ip_hdr.chks = - GNUNET_CRYPTO_crc16_n ((uint16_t *) & pkt4->ip_hdr, 5 * 4); - - (void) GNUNET_HELPER_send (helper_handle, - &pkt4->shdr, - GNUNET_YES, - NULL, NULL); - - } - - return GNUNET_OK; -} - -static void * -new_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel, - const struct GNUNET_PeerIdentity *initiator, - const struct GNUNET_ATS_Information *atsi) -{ - /* Why should anyone open an inbound tunnel to vpn? */ - GNUNET_break (0); - return NULL; -} - -static void -cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx) -{ - /* Why should anyone open an inbound tunnel to vpn? */ - GNUNET_break (0); -} - -/** - * Main function that will be run by the scheduler. - * - * @param cls closure - * @param args remaining command-line arguments - * @param cfgfile name of the configuration file used (for saving, can be NULL!) - * @param cfg_ configuration - */ -static void -run (void *cls, char *const *args GNUNET_UNUSED, - const char *cfgfile GNUNET_UNUSED, - const struct GNUNET_CONFIGURATION_Handle *cfg_) -{ - static const struct GNUNET_MESH_MessageHandler handlers[] = { - {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0}, - {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0}, - {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0}, - {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0}, - {NULL, 0, 0} - }; - static const GNUNET_MESH_ApplicationType types[] = { - GNUNET_APPLICATION_TYPE_END - }; - char *ifname; - char *ipv6addr; - char *ipv6prefix; - char *ipv4addr; - char *ipv4mask; - - mesh_handle = - GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, cleaner, handlers, - types); - cfg = cfg_; - hashmap = GNUNET_CONTAINER_multihashmap_create (65536); - heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); - GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg", - &max_mappings); - udp_connections = GNUNET_CONTAINER_multihashmap_create (65536); - dns_handle = GNUNET_DNS_connect (cfg, - &process_answer, - NULL); - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IFNAME", &ifname)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IFNAME' in configuration!\n"); - exit (1); - } - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR", &ipv6addr)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV6ADDR' in configuration!\n"); - exit (1); - } - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6PREFIX", - &ipv6prefix)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV6PREFIX' in configuration!\n"); - exit (1); - } - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR", &ipv4addr)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV4ADDR' in configuration!\n"); - exit (1); - } - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK", &ipv4mask)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "No entry 'IPV4MASK' in configuration!\n"); - exit (1); - } - - vpn_argv[0] = GNUNET_strdup ("vpn-gnunet"); - vpn_argv[1] = ifname; - vpn_argv[2] = ipv6addr; - vpn_argv[3] = ipv6prefix; - vpn_argv[4] = ipv4addr; - vpn_argv[5] = ipv4mask; - vpn_argv[6] = NULL; - - helper_handle = GNUNET_HELPER_start ("gnunet-helper-vpn", vpn_argv, - &message_token, NULL); - GNUNET_DNS_restart_hijack (dns_handle); - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls); -} - -/** - * The main function to obtain template from gnunetd. - * - * @param argc number of arguments from the command line - * @param argv command line arguments - * @return 0 ok, 1 on error - */ -int -main (int argc, char *const *argv) -{ - static const struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_OPTION_END - }; - - return (GNUNET_OK == - GNUNET_PROGRAM_run (argc, argv, "vpn", gettext_noop ("help text"), - options, &run, NULL)) ? ret : 1; -} - -/* end of gnunet-daemon-vpn.c */ diff --git a/src/vpn/test-conf.conf b/src/vpn/test-conf.conf deleted file mode 100644 index b805c7c94..000000000 --- a/src/vpn/test-conf.conf +++ /dev/null @@ -1,276 +0,0 @@ -[PATHS] -SERVICEHOME = ~/.gnunet/ -# SERVICEHOME = /var/lib/gnunet/ -# DEFAULTCONFIG = /etc/gnunet.conf -# If 'DEFAULTCONFIG' is not defined, the current -# configuration file is assumed to be the default, -# which is what we want by default... - -[gnunetd] -HOSTKEY = $SERVICEHOME/.hostkey - -[TESTING] -WEAKRANDOM = NO - -[client] -HOME = $SERVICEHOME - -[transport-tcp] -PORT = 2086 -TIMEOUT = 300s -# MAXBUF = -# DISABLEV6 = -# BINDTO = -# ACCEPT_FROM = -# ACCEPT_FROM6 = -# REJECT_FROM = -# REJECT_FROM6 = - -[dht] -AUTOSTART = YES -DEBUG = YES -PORT = 2095 -HOSTNAME = localhost -HOME = $SERVICEHOME -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-service-dht -ACCEPT_FROM = 127.0.0.1; -ACCEPT_FROM6 = ::1; -UNIXPATH = /tmp/gnunet-service-dht.sock -# DISABLE_SOCKET_FORWARDING = NO -# DEBUG = YES -# USERNAME = -# MAXBUF = -# TIMEOUT = -# DISABLEV6 = -# BINDTO = -# REJECT_FROM = -# REJECT_FROM6 = -# PREFIX = - -[dhtcache] -QUOTA = 1 MB -DATABASE = sqlite - -[arm] -PORT = 2087 -HOSTNAME = localhost -HOME = $SERVICEHOME -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-service-arm -ACCEPT_FROM = 127.0.0.1; -ACCEPT_FROM6 = ::1; -DEFAULTSERVICES = core transport dht vpn dns -UNIXPATH = /tmp/gnunet-service-arm.sock -# GLOBAL_POSTFIX = -l $SERVICEHOME/{}-logs -# GLOBAL_PREFIX = -# USERNAME = -# MAXBUF = -# TIMEOUT = -# DISABLEV6 = -# BINDTO = -# REJECT_FROM = -# REJECT_FROM6 = -# PREFIX = - -[statistics] -AUTOSTART = YES -PORT = 2088 -HOSTNAME = localhost -HOME = $SERVICEHOME -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-service-statistics -ACCEPT_FROM = 127.0.0.1; -ACCEPT_FROM6 = ::1; -UNIXPATH = /tmp/gnunet-service-statistics.sock -# USERNAME = -# MAXBUF = -# TIMEOUT = -# DISABLEV6 = -# BINDTO = -# REJECT_FROM = -# REJECT_FROM6 = -# PREFIX = - -[resolver] -AUTOSTART = YES -PORT = 2089 -HOSTNAME = localhost -HOME = $SERVICEHOME -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-service-resolver -ACCEPT_FROM = 127.0.0.1; -ACCEPT_FROM6 = ::1; -UNIXPATH = /tmp/gnunet-service-resolver.sock -# USERNAME = -# MAXBUF = -# TIMEOUT = -# DISABLEV6 = -# BINDTO = -# REJECT_FROM = -# REJECT_FROM6 = -# PREFIX = - -[peerinfo] -AUTOSTART = YES -PORT = 2090 -HOSTNAME = localhost -HOME = $SERVICEHOME -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-service-peerinfo -ACCEPT_FROM = 127.0.0.1; -ACCEPT_FROM6 = ::1; -UNIXPATH = /tmp/gnunet-service-peerinfo.sock -# USERNAME = -# MAXBUF = -# TIMEOUT = -# DISABLEV6 = -# BINDTO = -# REJECT_FROM = -# REJECT_FROM6 = -# PREFIX = -HOSTS = $SERVICEHOME/data/hosts/ -TRUST = $SERVICEHOME/data/credit/ - - -[transport] -AUTOSTART = YES -PORT = 2091 -HOSTNAME = localhost -HOME = $SERVICEHOME -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-service-transport -NEIGHBOUR_LIMIT = 50 -ACCEPT_FROM = 127.0.0.1; -ACCEPT_FROM6 = ::1; -PLUGINS = tcp -UNIXPATH = /tmp/gnunet-service-transport.sock -# USERNAME = -# MAXBUF = -# TIMEOUT = -# DISABLEV6 = -# BINDTO = -# REJECT_FROM = -# REJECT_FROM6 = -# PREFIX = - -[ats] -WAN_QUOTA_IN = 64 kiB -WAN_QUOTA_OUT = 64 kiB - -[core] -AUTOSTART = YES -PORT = 2092 -HOSTNAME = localhost -HOME = $SERVICEHOME -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-service-core -ACCEPT_FROM = 127.0.0.1; -ACCEPT_FROM6 = ::1; -UNIXPATH = /tmp/gnunet-service-core.sock -# DEBUG = YES -# USERNAME = -# MAXBUF = -# TIMEOUT = -# DISABLEV6 = -# BINDTO = -# REJECT_FROM = -# REJECT_FROM6 = -# PREFIX = - - -[topology] -MINIMUM-FRIENDS = 0 -FRIENDS-ONLY = NO -AUTOCONNECT = YES -TARGET-CONNECTION-COUNT = 16 -FRIENDS = $SERVICEHOME/friends -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-daemon-topology - -[hostlist] -# port for hostlist http server -AUTOSTART = NO -HTTPPORT = 8080 -DEBUG = YES -HOME = $SERVICEHOME -HOSTLISTFILE = $SERVICEHOME/hostlist/learned.data -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-daemon-hostlist -# consider having "-e" as default as well once implemented -OPTIONS = -p -e -a -SERVERS = # http://v9.gnunet.org:58080/ -# proxy for downloading hostlists -HTTP-PROXY = - -[vpn] -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-daemon-vpn -DEBUG = YES - -[datastore] -AUTOSTART = YES -UNIXPATH = /tmp/gnunet-service-datastore.sock -PORT = 2093 -HOSTNAME = localhost -HOME = $SERVICEHOME -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-service-datastore -ACCEPT_FROM = 127.0.0.1; -ACCEPT_FROM6 = ::1; -QUOTA = 100 MB -BLOOMFILTER = $SERVICEHOME/fs/bloomfilter -DATABASE = sqlite - -[datastore-sqlite] -FILENAME = $SERVICEHOME/datastore/sqlite.db - -[fs] -AUTOSTART = YES -INDEXDB = $SERVICEHOME/idxinfo.lst -IDENTITY_DIR = $SERVICEHOME/identities/ -STATE_DIR = $SERVICEHOME/persistence/ -PORT = 2094 -HOSTNAME = localhost -HOME = $SERVICEHOME -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-service-fs -ACCEPT_FROM = 127.0.0.1; -ACCEPT_FROM6 = ::1; -ACTIVEMIGRATION = YES -UNIXPATH = /tmp/gnunet-service-fs.sock -# DEBUG = YES - -[dns] -AUTOSTART = YES -PORT = 2098 -HOSTNAME = localhost -HOME = $SERVICEHOME -CONFIG = $DEFAULTCONFIG -BINARY = gnunet-service-dns -ACCEPT_FROM = 127.0.0.1; -ACCEPT_FROM6 = ::1; -# quotas are in bytes per second now! -TOTAL_QUOTA_IN = 65536 -TOTAL_QUOTA_OUT = 65536 -UNIXPATH = /tmp/gnunet-service-dns.sock -DEBUG = YES -# USERNAME = -# MAXBUF = -# TIMEOUT = -# DISABLEV6 = -# BINDTO = -# REJECT_FROM = -# REJECT_FROM6 = -# PREFIX = - -[block] -PLUGINS = fs dns - - - - -[nse] -AUTOSTART = NO - - -- cgit v1.2.3