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"
45 struct GNUNET_DISK_PipeHandle* helper_in; // From the helper
46 struct GNUNET_DISK_PipeHandle* helper_out; // To the helper
47 const struct GNUNET_DISK_FileHandle* fh_from_helper;
49 struct GNUNET_SERVER_MessageStreamTokenizer* mst;
51 struct GNUNET_SCHEDULER_Handle *sched;
53 struct GNUNET_CLIENT_Connection *dns_connection;
58 static struct vpn_cls mycls;
60 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
61 if (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) {
62 PLIBC_KILL(mycls.helper_pid, SIGTERM);
63 GNUNET_OS_process_wait(mycls.helper_pid);
67 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
69 static void start_helper_and_schedule() {
70 mycls.helper_in = GNUNET_DISK_pipe(1);
71 mycls.helper_out = GNUNET_DISK_pipe(1);
73 mycls.helper_pid = GNUNET_OS_start_process(mycls.helper_in, mycls.helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
75 mycls.fh_from_helper = GNUNET_DISK_pipe_handle (mycls.helper_out, GNUNET_DISK_PIPE_END_READ);
77 GNUNET_DISK_pipe_close_end(mycls.helper_out, GNUNET_DISK_PIPE_END_WRITE);
78 GNUNET_DISK_pipe_close_end(mycls.helper_in, GNUNET_DISK_PIPE_END_READ);
80 GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
84 static void restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
85 // FIXME: Ratelimit this!
88 PLIBC_KILL(mycls.helper_pid, SIGKILL);
89 GNUNET_OS_process_wait(mycls.helper_pid);
92 start_helper_and_schedule(mycls);
96 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
99 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
102 int t = GNUNET_DISK_file_read(mycls.fh_from_helper, &buf, 65535);
104 fprintf(stderr, "Read error for header: %m\n");
105 GNUNET_SCHEDULER_add_now(mycls.sched, restart_helper, cls);
109 /* FIXME */ GNUNET_SERVER_mst_receive(mycls.mst, NULL, buf, t, 0, 0);
111 GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
114 static void message_token(void *cls, void *client, const struct GNUNET_MessageHeader *message) {
115 if (ntohs(message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) return;
117 struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
119 fprintf(stderr, "Packet, Type: %x\n", ntohs(pkt_tun->tun.type));
121 if (ntohs(pkt_tun->tun.type) == 0x86dd) {
122 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
123 struct ip6_tcp *pkt6_tcp;
124 struct ip6_udp *pkt6_udp;
127 switch(pkt6->ip6_hdr.nxthdr) {
129 pkt6_tcp = (struct ip6_tcp*)pkt6;
130 pkt_printf_ip6tcp(pkt6_tcp);
133 pkt6_udp = (struct ip6_udp*)pkt6;
134 pkt_printf_ip6udp(pkt6_udp);
135 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
136 pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
140 } else if (ntohs(pkt_tun->tun.type) == 0x0800) {
141 struct ip_pkt *pkt = (struct ip_pkt*) message;
142 struct ip_udp *udp = (struct ip_udp*) message;
143 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "IPv4\n");
144 if (pkt->ip_hdr.proto == 0x11 && ntohl(udp->ip_hdr.dadr) == 0x0a0a0a02 && ntohs(udp->udp_hdr.dpt) == 53 ) {
145 pkt_printf_ipdns((struct ip_udp_dns*)udp);
146 struct query_packet* query = alloca((sizeof query) + ntohs(udp->udp_hdr.len) - 7); /* 7 = 8 for the udp-header - 1 for the unsigned char data[1]; */
147 query->hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
148 query->hdr.size = htons((sizeof query) + ntohs(udp->udp_hdr.len) - 7);
149 query->orig_to = pkt->ip_hdr.dadr;
150 query->orig_from = pkt->ip_hdr.sadr;
151 query->src_port = udp->udp_hdr.spt;
152 memcpy(query->data, udp->data, ntohs(udp->udp_hdr.len) - 8);
159 * Main function that will be run by the scheduler.
162 * @param sched the scheduler to use
163 * @param args remaining command-line arguments
164 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
165 * @param cfg configuration
169 struct GNUNET_SCHEDULER_Handle *sched,
172 const struct GNUNET_CONFIGURATION_Handle *cfg)
175 mycls.mst = GNUNET_SERVER_mst_create(&message_token, NULL);
177 mycls.dns_connection = GNUNET_CLIENT_connect (sched, "gnunet-service-dns", cfg);
178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connection: %x\n", mycls.dns_connection);
180 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
181 start_helper_and_schedule(mycls);
186 * The main function to obtain template from gnunetd.
188 * @param argc number of arguments from the command line
189 * @param argv command line arguments
190 * @return 0 ok, 1 on error
193 main (int argc, char *const *argv)
195 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
196 GNUNET_GETOPT_OPTION_END
200 GNUNET_PROGRAM_run (argc,
203 gettext_noop ("help text"),
204 options, &run, NULL)) ? ret : 1;
207 /* end of gnunet-daemon-vpn.c */