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"
39 #include "block_dns.h"
47 * The scheduler to use throughout the daemon
49 static struct GNUNET_SCHEDULER_Handle *sched;
52 * The configuration to use
54 static const struct GNUNET_CONFIGURATION_Handle *cfg;
57 * PipeHandle to receive data from the helper
59 static struct GNUNET_DISK_PipeHandle* helper_in;
62 * PipeHandle to send data to the helper
64 static struct GNUNET_DISK_PipeHandle* helper_out;
67 * FileHandle to receive data from the helper
69 static const struct GNUNET_DISK_FileHandle* fh_from_helper;
72 * FileHandle to send data to the helper
74 static const struct GNUNET_DISK_FileHandle* fh_to_helper;
77 * The Message-Tokenizer that tokenizes the messages comming from the helper
79 static struct GNUNET_SERVER_MessageStreamTokenizer* mst;
82 * The connection to the service-dns
84 static struct GNUNET_CLIENT_Connection *dns_connection;
87 * A flag to show that the service-dns has to rehijack the outbound dns-packets
89 * This gets set when the helper restarts as the routing-tables are flushed when
90 * the interface vanishes.
92 static unsigned char restart_hijack;
95 * The process id of the helper
97 static GNUNET_OS_Process *helper_proc;
100 * a list of outgoing dns-query-packets
102 static struct query_packet_list *head;
105 * The last element of the list of outgoing dns-query-packets
107 static struct query_packet_list *tail;
110 * A list of processed dns-responses.
112 * "processed" means that the packet is complete and can be sent out via udp
115 static struct answer_packet_list *answer_proc_head;
118 * The last element of the list of processed dns-responses.
120 static struct answer_packet_list *answer_proc_tail;
122 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
123 static void dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg);
126 * Callback called by notify_transmit_ready; sends dns-queries or rehijack-messages
130 send_query(void* cls, size_t size, void* buf) {
133 * Send the rehijack-message
135 if (restart_hijack == 1)
139 * The message is just a header
141 GNUNET_assert(sizeof(struct GNUNET_MessageHeader) >= size);
142 struct GNUNET_MessageHeader* hdr = buf;
143 len = sizeof(struct GNUNET_MessageHeader);
144 hdr->size = htons(len);
145 hdr->type = htons(GNUNET_MESSAGE_TYPE_REHIJACK);
149 struct query_packet_list* query = head;
150 len = ntohs(query->pkt.hdr.size);
152 GNUNET_assert(len <= size);
154 memcpy(buf, &query->pkt.hdr, len);
156 GNUNET_CONTAINER_DLL_remove (head, tail, query);
162 * Check whether more data is to be sent
164 if (head != NULL || restart_hijack == 1)
166 GNUNET_CLIENT_notify_transmit_ready(dns_connection, ntohs(head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
173 * Function scheduled as very last function, cleans up after us
176 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
177 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
179 /* stop the helper */
180 GNUNET_OS_process_kill (helper_proc, SIGTERM);
181 GNUNET_OS_process_wait (helper_proc);
182 GNUNET_OS_process_close (helper_proc);
185 /* close the connection to the service-dns */
186 if (dns_connection != NULL)
188 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
189 dns_connection = NULL;
194 * Start the helper-process
197 start_helper_and_schedule(void *cls,
198 const struct GNUNET_SCHEDULER_TaskContext *tc) {
199 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
202 helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
203 helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
205 if (helper_in == NULL || helper_out == NULL) return;
207 helper_proc = GNUNET_OS_start_process(helper_in, helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
209 fh_from_helper = GNUNET_DISK_pipe_handle (helper_out, GNUNET_DISK_PIPE_END_READ);
210 fh_to_helper = GNUNET_DISK_pipe_handle (helper_in, GNUNET_DISK_PIPE_END_WRITE);
212 GNUNET_DISK_pipe_close_end(helper_out, GNUNET_DISK_PIPE_END_WRITE);
213 GNUNET_DISK_pipe_close_end(helper_in, GNUNET_DISK_PIPE_END_READ);
215 GNUNET_SCHEDULER_add_read_file (sched, GNUNET_TIME_UNIT_FOREVER_REL, fh_from_helper, &helper_read, NULL);
219 * Restart the helper-process
222 restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
224 GNUNET_OS_process_kill (helper_proc, SIGKILL);
225 GNUNET_OS_process_wait (helper_proc);
226 GNUNET_OS_process_close (helper_proc);
229 /* Tell the dns-service to rehijack the dns-port
230 * The routing-table gets flushed if an interface disappears.
233 GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
235 GNUNET_DISK_pipe_close(helper_in);
236 GNUNET_DISK_pipe_close(helper_out);
238 /* Restart the helper */
239 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_UNIT_SECONDS, start_helper_and_schedule, NULL);
243 * Read from the helper-process
246 helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
247 /* no message can be bigger then 64k */
250 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
253 int t = GNUNET_DISK_file_read(fh_from_helper, &buf, 65535);
255 /* On read-error, restart the helper */
257 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read error for header from vpn-helper: %m\n");
258 GNUNET_SCHEDULER_add_now(sched, restart_helper, cls);
262 /* FIXME */ GNUNET_SERVER_mst_receive(mst, NULL, buf, t, 0, 0);
264 GNUNET_SCHEDULER_add_read_file (sched, GNUNET_TIME_UNIT_FOREVER_REL, fh_from_helper, &helper_read, NULL);
268 * Calculate the checksum of an IPv4-Header
271 calculate_ip_checksum(uint16_t* hdr, short len) {
273 for(; len >= 2; len -= 2)
276 sum += *((unsigned char*)hdr);
278 sum = (sum >> 16) + (sum & 0xFFFF);
284 * Send an dns-answer-packet to the helper
287 helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
288 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
291 struct answer_packet_list* ans = answer_proc_head;
292 size_t len = ntohs(ans->pkt.hdr.size);
294 GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
296 size_t data_len = len - sizeof(struct answer_packet) + 1;
297 size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
298 size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
300 struct ip_udp_dns* pkt = alloca(pkt_len);
301 memset(pkt, 0, pkt_len);
303 /* set the gnunet-header */
304 pkt->shdr.size = htons(pkt_len);
305 pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
307 /* set the tun-header (no flags and ethertype of IPv4) */
309 pkt->tun.type = htons(0x0800);
311 /* set the ip-header */
312 pkt->ip_hdr.version = 4;
313 pkt->ip_hdr.hdr_lngth = 5;
314 pkt->ip_hdr.diff_serv = 0;
315 pkt->ip_hdr.tot_lngth = htons(net_len);
316 pkt->ip_hdr.ident = 0;
317 pkt->ip_hdr.flags = 0;
318 pkt->ip_hdr.frag_off = 0;
319 pkt->ip_hdr.ttl = 255;
320 pkt->ip_hdr.proto = 0x11; /* UDP */
321 pkt->ip_hdr.chks = 0; /* Will be calculated later*/
322 pkt->ip_hdr.sadr = ans->pkt.from;
323 pkt->ip_hdr.dadr = ans->pkt.to;
325 pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
327 /* set the udp-header */
328 pkt->udp_dns.udp_hdr.spt = htons(53);
329 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
330 pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
331 pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
333 memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
335 GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
338 /* FIXME */ GNUNET_DISK_file_write(fh_to_helper, pkt, pkt_len);
340 /* if more packets are available, reschedule */
341 if (answer_proc_head != NULL)
342 GNUNET_SCHEDULER_add_write_file (sched,
343 GNUNET_TIME_UNIT_FOREVER_REL,
350 * Receive packets from the helper-process
353 message_token(void *cls,
355 const struct GNUNET_MessageHeader *message) {
356 GNUNET_assert(ntohs(message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
358 struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
360 /* ethertype is ipv6 */
361 if (ntohs(pkt_tun->tun.type) == 0x86dd)
363 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
364 struct ip6_tcp *pkt6_tcp;
365 struct ip6_udp *pkt6_udp;
368 switch(pkt6->ip6_hdr.nxthdr)
371 pkt6_tcp = (struct ip6_tcp*)pkt6;
372 pkt_printf_ip6tcp(pkt6_tcp);
375 pkt6_udp = (struct ip6_udp*)pkt6;
376 pkt_printf_ip6udp(pkt6_udp);
377 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
378 pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
383 /* ethertype is ipv4 */
384 else if (ntohs(pkt_tun->tun.type) == 0x0800)
386 struct ip_pkt *pkt = (struct ip_pkt*) message;
387 struct ip_udp *udp = (struct ip_udp*) message;
388 GNUNET_assert(pkt->ip_hdr.version == 4);
390 /* Send dns-packets to the service-dns */
391 if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 )
393 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
394 size_t len = sizeof(struct query_packet) + ntohs(udp->udp_hdr.len) - 9;
396 struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
397 query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
398 query->pkt.hdr.size = htons(len);
399 query->pkt.orig_to = pkt->ip_hdr.dadr;
400 query->pkt.orig_from = pkt->ip_hdr.sadr;
401 query->pkt.src_port = udp->udp_hdr.spt;
402 memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
404 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, query);
406 if (dns_connection != NULL)
407 GNUNET_CLIENT_notify_transmit_ready(dns_connection,
409 GNUNET_TIME_UNIT_FOREVER_REL,
418 * Connect to the service-dns
421 connect_to_service_dns (void *cls,
422 const struct GNUNET_SCHEDULER_TaskContext *tc) {
423 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
425 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n");
426 GNUNET_assert (dns_connection == NULL);
427 dns_connection = GNUNET_CLIENT_connect (sched, "dns", cfg);
428 GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
430 /* If a packet is already in the list, schedule to send it */
432 GNUNET_CLIENT_notify_transmit_ready(dns_connection,
433 ntohs(head->pkt.hdr.size),
434 GNUNET_TIME_UNIT_FOREVER_REL,
441 * This gets scheduled with cls pointing to an answer_packet and does everything
442 * needed in order to send it to the helper.
444 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
445 * doing nothing for "real" services.
448 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
449 struct answer_packet* pkt = cls;
451 /* This answer is about a .gnunet-service
453 * It contains an almost complete DNS-Response, we have to fill in the ip
454 * at the offset pkt->addroffset
456 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
458 unsigned char ip6addr[16];
460 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
461 memcpy(ip6addr, (int[]){htons(0x1234)}, 2);
462 memcpy(ip6addr+2, &pkt->peer, 7);
463 memcpy(ip6addr+9, &pkt->service_descriptor, 7);
465 memcpy(((char*)pkt)+ntohs(pkt->addroffset), ip6addr, 16);
468 * -save DNS_Record into hashmap, pointed to by ip
469 * -regularily walk through hashmap, deleting old entries
470 * when is an entry old?
471 * have a last-used field
472 * don't remove if last-used "recent", ask dht again if record expired
476 struct answer_packet_list* list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
478 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
480 GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
482 GNUNET_SCHEDULER_add_write_file (sched, GNUNET_TIME_UNIT_FOREVER_REL, fh_to_helper, &helper_write, NULL);
488 * This receives packets from the service-dns and schedules process_answer to
492 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg) {
493 /* the service disconnected, reconnect after short wait */
496 GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO);
497 dns_connection = NULL;
498 GNUNET_SCHEDULER_add_delayed (sched,
499 GNUNET_TIME_UNIT_SECONDS,
500 &connect_to_service_dns,
505 /* the service did something strange, reconnect immediately */
506 if (msg->type != htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS))
509 GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO);
510 dns_connection = NULL;
511 GNUNET_SCHEDULER_add_now (sched,
512 &connect_to_service_dns,
516 void *pkt = GNUNET_malloc(ntohs(msg->size));
518 memcpy(pkt, msg, ntohs(msg->size));
520 GNUNET_SCHEDULER_add_now(sched, process_answer, pkt);
521 GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
525 * Main function that will be run by the scheduler.
528 * @param sched the scheduler to use
529 * @param args remaining command-line arguments
530 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
531 * @param cfg configuration
535 struct GNUNET_SCHEDULER_Handle *sched_,
538 const struct GNUNET_CONFIGURATION_Handle *cfg_) {
540 mst = GNUNET_SERVER_mst_create(&message_token, NULL);
543 GNUNET_SCHEDULER_add_now (sched, connect_to_service_dns, NULL);
544 GNUNET_SCHEDULER_add_now (sched, start_helper_and_schedule, NULL);
545 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
549 * The main function to obtain template from gnunetd.
551 * @param argc number of arguments from the command line
552 * @param argv command line arguments
553 * @return 0 ok, 1 on error
556 main (int argc, char *const *argv) {
557 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
558 GNUNET_GETOPT_OPTION_END
562 GNUNET_PROGRAM_run (argc,
565 gettext_noop ("help text"),
566 options, &run, NULL)) ? ret : 1;
569 /* end of gnunet-daemon-vpn.c */