2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file vpn/gnunet-daemon-vpn-dns.c
24 * @author Philipp Toelke
27 #include <gnunet_common.h>
28 #include <gnunet_client_lib.h>
29 #include <gnunet_os_lib.h>
30 #include <gnunet_mesh_service.h>
31 #include <gnunet_protocols.h>
32 #include <gnunet_server_lib.h>
33 #include <gnunet_container_lib.h>
34 #include <block_dns.h>
36 #include "gnunet-daemon-vpn-dns.h"
37 #include "gnunet-daemon-vpn.h"
38 #include "gnunet-daemon-vpn-helper.h"
39 #include "gnunet-service-dns-p.h"
40 #include "gnunet-vpn-packet.h"
42 struct query_packet_list *head;
43 struct query_packet_list *tail;
44 struct GNUNET_CLIENT_Connection *dns_connection;
45 unsigned char restart_hijack;
46 struct answer_packet_list *answer_proc_head;
47 struct answer_packet_list *answer_proc_tail;
50 * Callback called by notify_transmit_ready; sends dns-queries or rehijack-messages
55 send_query(void* cls, size_t size, void* buf) {
58 * Send the rehijack-message
60 if (restart_hijack == 1)
64 * The message is just a header
66 GNUNET_assert(sizeof(struct GNUNET_MessageHeader) <= size);
67 struct GNUNET_MessageHeader* hdr = buf;
68 len = sizeof(struct GNUNET_MessageHeader);
69 hdr->size = htons(len);
70 hdr->type = htons(GNUNET_MESSAGE_TYPE_REHIJACK);
72 else if (head != NULL)
74 struct query_packet_list* query = head;
75 len = ntohs(query->pkt.hdr.size);
77 GNUNET_assert(len <= size);
79 memcpy(buf, &query->pkt.hdr, len);
81 GNUNET_CONTAINER_DLL_remove (head, tail, query);
92 * Check whether more data is to be sent
96 GNUNET_CLIENT_notify_transmit_ready(dns_connection, ntohs(head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
98 else if (restart_hijack == 1)
100 GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
109 * Connect to the service-dns
112 connect_to_service_dns (void *cls,
113 const struct GNUNET_SCHEDULER_TaskContext *tc) {
114 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
116 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n");
117 GNUNET_assert (dns_connection == NULL);
118 dns_connection = GNUNET_CLIENT_connect ("dns", cfg);
119 /* This would most likely be a misconfiguration */
120 GNUNET_assert(NULL != dns_connection);
121 GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
123 /* We might not yet be connected. Yay, mps. */
124 if (NULL == dns_connection) return;
126 /* If a packet is already in the list, schedule to send it */
128 GNUNET_CLIENT_notify_transmit_ready(dns_connection,
129 ntohs(head->pkt.hdr.size),
130 GNUNET_TIME_UNIT_FOREVER_REL,
134 else if (restart_hijack == 1)
136 GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
141 * This receives packets from the service-dns and schedules process_answer to
145 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg) {
146 /* the service disconnected, reconnect after short wait */
149 GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO);
150 dns_connection = NULL;
151 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
152 &connect_to_service_dns,
157 /* the service did something strange, reconnect immediately */
158 if (msg->type != htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS))
161 GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO);
162 dns_connection = NULL;
163 GNUNET_SCHEDULER_add_now (&connect_to_service_dns,
167 void *pkt = GNUNET_malloc(ntohs(msg->size));
169 memcpy(pkt, msg, ntohs(msg->size));
171 GNUNET_SCHEDULER_add_now(process_answer, pkt);
172 GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);