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.c
24 * @author Philipp Tölke
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet-vpn-helper-p.h"
31 #include "gnunet-vpn-packet.h"
32 #include "gnunet-vpn-pretty-print.h"
33 #include "gnunet_common.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet-service-dns-p.h"
37 #include "gnunet_client_lib.h"
38 #include "gnunet_container_lib.h"
46 struct GNUNET_DISK_PipeHandle* helper_in; // From the helper
47 struct GNUNET_DISK_PipeHandle* helper_out; // To the helper
48 const struct GNUNET_DISK_FileHandle* fh_from_helper;
49 const struct GNUNET_DISK_FileHandle* fh_to_helper;
51 struct GNUNET_SERVER_MessageStreamTokenizer* mst;
53 struct GNUNET_SCHEDULER_Handle *sched;
55 struct GNUNET_CLIENT_Connection *dns_connection;
59 const struct GNUNET_CONFIGURATION_Handle *cfg;
61 struct query_packet_list *head;
62 struct query_packet_list *tail;
64 struct answer_packet_list *answer_proc_head;
65 struct answer_packet_list *answer_proc_tail;
68 static struct vpn_cls mycls;
70 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
71 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
72 PLIBC_KILL(mycls.helper_pid, SIGTERM);
73 GNUNET_OS_process_wait(mycls.helper_pid);
74 if (mycls.dns_connection != NULL)
76 GNUNET_CLIENT_disconnect (mycls.dns_connection, GNUNET_NO);
77 mycls.dns_connection = NULL;
81 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
83 static void start_helper_and_schedule(void *cls,
84 const struct GNUNET_SCHEDULER_TaskContext *tc) {
86 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
89 mycls.helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
90 mycls.helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
92 if (mycls.helper_in == NULL || mycls.helper_out == NULL) return;
94 mycls.helper_pid = GNUNET_OS_start_process(mycls.helper_in, mycls.helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
96 mycls.fh_from_helper = GNUNET_DISK_pipe_handle (mycls.helper_out, GNUNET_DISK_PIPE_END_READ);
97 mycls.fh_to_helper = GNUNET_DISK_pipe_handle (mycls.helper_in, GNUNET_DISK_PIPE_END_WRITE);
99 GNUNET_DISK_pipe_close_end(mycls.helper_out, GNUNET_DISK_PIPE_END_WRITE);
100 GNUNET_DISK_pipe_close_end(mycls.helper_in, GNUNET_DISK_PIPE_END_READ);
102 GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
106 static void restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
108 PLIBC_KILL(mycls.helper_pid, SIGKILL);
109 GNUNET_OS_process_wait(mycls.helper_pid);
111 // FIXME: send msg to service-dns -- the hijacker has to be started again, too, the routing table is flushed if it depends on one interface
113 GNUNET_DISK_pipe_close(mycls.helper_in);
114 GNUNET_DISK_pipe_close(mycls.helper_out);
116 // Restart the helper
117 GNUNET_SCHEDULER_add_delayed (mycls.sched, GNUNET_TIME_UNIT_SECONDS, start_helper_and_schedule, NULL);
121 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
124 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
127 int t = GNUNET_DISK_file_read(mycls.fh_from_helper, &buf, 65535);
129 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read error for header from vpn-helper: %m\n");
130 GNUNET_SCHEDULER_add_now(mycls.sched, restart_helper, cls);
134 /* FIXME */ GNUNET_SERVER_mst_receive(mycls.mst, NULL, buf, t, 0, 0);
136 GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
139 static uint16_t calculate_ip_checksum(uint16_t* hdr, short len) {
141 for(; len >= 2; len -= 2)
144 sum += *((unsigned char*)hdr);
146 sum = (sum >> 16) + (sum & 0xFFFF);
151 static void helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
152 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
154 struct answer_packet_list* ans = mycls.answer_proc_head;
155 size_t len = ntohs(ans->pkt.hdr.size);
157 GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
159 size_t data_len = len - sizeof(struct answer_packet) + 1;
160 size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
161 size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
163 struct ip_udp_dns* pkt = alloca(pkt_len);
164 memset(pkt, 0, pkt_len);
166 pkt->shdr.size = htons(pkt_len);
167 pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
170 pkt->tun.type = htons(0x0800);
172 pkt->ip_hdr.version = 4;
173 pkt->ip_hdr.hdr_lngth = 5;
174 pkt->ip_hdr.diff_serv = 0;
175 pkt->ip_hdr.tot_lngth = htons(net_len);
176 pkt->ip_hdr.ident = 0;
177 pkt->ip_hdr.flags = 0;
178 pkt->ip_hdr.frag_off = 0;
179 pkt->ip_hdr.ttl = 255;
180 pkt->ip_hdr.proto = 0x11; /* UDP */
181 pkt->ip_hdr.chks = 0; /* Will be calculated later*/
182 pkt->ip_hdr.sadr = ans->pkt.from;
183 pkt->ip_hdr.dadr = ans->pkt.to;
185 pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
187 pkt->udp_dns.udp_hdr.spt = htons(53);
188 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
189 pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
190 pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
192 memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
194 GNUNET_CONTAINER_DLL_remove (mycls.answer_proc_head, mycls.answer_proc_tail, ans);
197 /* FIXME */ GNUNET_DISK_file_write(mycls.fh_to_helper, pkt, pkt_len);
199 if (mycls.answer_proc_head != NULL)
200 GNUNET_SCHEDULER_add_write_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_to_helper, &helper_write, NULL);
203 size_t send_query(void* cls, size_t size, void* buf)
205 struct query_packet_list* query = mycls.head;
206 size_t len = ntohs(query->pkt.hdr.size);
208 GNUNET_assert(len <= size);
210 memcpy(buf, &query->pkt.hdr, len);
212 GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
216 if (mycls.head != NULL) {
217 GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
223 static void message_token(void *cls, void *client, const struct GNUNET_MessageHeader *message) {
224 if (ntohs(message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) return;
226 struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
228 if (ntohs(pkt_tun->tun.type) == 0x86dd) {
229 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
230 struct ip6_tcp *pkt6_tcp;
231 struct ip6_udp *pkt6_udp;
234 switch(pkt6->ip6_hdr.nxthdr) {
236 pkt6_tcp = (struct ip6_tcp*)pkt6;
237 pkt_printf_ip6tcp(pkt6_tcp);
240 pkt6_udp = (struct ip6_udp*)pkt6;
241 pkt_printf_ip6udp(pkt6_udp);
242 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
243 pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
247 } else if (ntohs(pkt_tun->tun.type) == 0x0800) {
248 struct ip_pkt *pkt = (struct ip_pkt*) message;
249 struct ip_udp *udp = (struct ip_udp*) message;
250 GNUNET_assert(pkt->ip_hdr.version == 4);
251 if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 ) {
252 size_t len = sizeof(struct query_packet) + ntohs(udp->udp_hdr.len) - 9; /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
253 struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
254 query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
255 query->pkt.hdr.size = htons(len);
256 query->pkt.orig_to = pkt->ip_hdr.dadr;
257 query->pkt.orig_from = pkt->ip_hdr.sadr;
258 query->pkt.src_port = udp->udp_hdr.spt;
259 memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
261 GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, query);
263 if (mycls.dns_connection != NULL)
264 /* struct GNUNET_CLIENT_TransmitHandle* th = */ GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, len, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
271 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg);
274 reconnect_to_service_dns (void *cls,
275 const struct GNUNET_SCHEDULER_TaskContext *tc) {
276 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
278 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting\n");
279 GNUNET_assert (mycls.dns_connection == NULL);
280 mycls.dns_connection = GNUNET_CLIENT_connect (mycls.sched, "dns", mycls.cfg);
281 GNUNET_CLIENT_receive(mycls.dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
282 if (mycls.head != NULL)
283 /* struct GNUNET_CLIENT_TransmitHandle* th = */ GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
287 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
288 struct answer_packet* pkt = cls;
290 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
292 struct answer_packet_list* list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
294 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
296 GNUNET_CONTAINER_DLL_insert_after(mycls.answer_proc_head, mycls.answer_proc_tail, mycls.answer_proc_tail, list);
298 GNUNET_SCHEDULER_add_write_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_to_helper, &helper_write, NULL);
303 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
306 * -find new IP-address for this service
307 * -create a DNS-Answer
313 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg)
317 GNUNET_CLIENT_disconnect(mycls.dns_connection, GNUNET_NO);
318 mycls.dns_connection = NULL;
319 GNUNET_SCHEDULER_add_delayed (mycls.sched,
320 GNUNET_TIME_UNIT_SECONDS,
321 &reconnect_to_service_dns,
326 if (msg->type != htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS))
329 GNUNET_CLIENT_disconnect(mycls.dns_connection, GNUNET_NO);
330 mycls.dns_connection = NULL;
331 GNUNET_SCHEDULER_add_now (mycls.sched,
332 &reconnect_to_service_dns,
336 void *pkt = GNUNET_malloc(ntohs(msg->size));
338 memcpy(pkt, msg, ntohs(msg->size));
340 GNUNET_SCHEDULER_add_now(mycls.sched, process_answer, pkt);
341 GNUNET_CLIENT_receive(mycls.dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
345 * Main function that will be run by the scheduler.
348 * @param sched the scheduler to use
349 * @param args remaining command-line arguments
350 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
351 * @param cfg configuration
355 struct GNUNET_SCHEDULER_Handle *sched,
358 const struct GNUNET_CONFIGURATION_Handle *cfg)
361 mycls.mst = GNUNET_SERVER_mst_create(&message_token, NULL);
363 GNUNET_SCHEDULER_add_now (sched, &reconnect_to_service_dns, NULL);
364 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
365 GNUNET_SCHEDULER_add_now (sched, start_helper_and_schedule, NULL);
370 * The main function to obtain template from gnunetd.
372 * @param argc number of arguments from the command line
373 * @param argv command line arguments
374 * @return 0 ok, 1 on error
377 main (int argc, char *const *argv)
379 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
380 GNUNET_GETOPT_OPTION_END
384 GNUNET_PROGRAM_run (argc,
387 gettext_noop ("help text"),
388 options, &run, NULL)) ? ret : 1;
391 /* end of gnunet-daemon-vpn.c */