not here
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file vpn/gnunet-daemon-vpn.c
23  * @brief 
24  * @author Philipp Tölke
25  */
26 #include "platform.h"
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
37 /**
38  * Final status code.
39  */
40 static int ret;
41
42 struct vpn_cls {
43         struct GNUNET_DISK_PipeHandle* helper_in; // From the helper
44         struct GNUNET_DISK_PipeHandle* helper_out; // To the helper
45         const struct GNUNET_DISK_FileHandle* fh_from_helper;
46
47         struct GNUNET_SERVER_MessageStreamTokenizer* mst;
48
49         struct GNUNET_SCHEDULER_Handle *sched;
50
51         pid_t helper_pid;
52 };
53
54 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
55         struct vpn_cls* mycls = (struct vpn_cls*) cls;
56         if (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) {
57                 PLIBC_KILL(mycls->helper_pid, SIGTERM);
58                 GNUNET_OS_process_wait(mycls->helper_pid);
59         }
60 }
61
62 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
63
64 static void start_helper_and_schedule(struct vpn_cls* mycls) {
65         mycls->helper_in = GNUNET_DISK_pipe(1);
66         mycls->helper_out = GNUNET_DISK_pipe(1);
67
68         mycls->helper_pid = GNUNET_OS_start_process(mycls->helper_in, mycls->helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
69
70         mycls->fh_from_helper = GNUNET_DISK_pipe_handle (mycls->helper_out, GNUNET_DISK_PIPE_END_READ);
71
72         GNUNET_DISK_pipe_close_end(mycls->helper_out, GNUNET_DISK_PIPE_END_WRITE);
73         GNUNET_DISK_pipe_close_end(mycls->helper_in, GNUNET_DISK_PIPE_END_READ);
74
75         GNUNET_SCHEDULER_add_read_file (mycls->sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls->fh_from_helper, &helper_read, mycls);
76 }
77
78
79 static void restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
80         // FIXME: Ratelimit this!
81         struct vpn_cls* mycls = (struct vpn_cls*) cls;
82
83         // Kill the helper
84         PLIBC_KILL(mycls->helper_pid, SIGKILL);
85         GNUNET_OS_process_wait(mycls->helper_pid);
86
87         // Restart the helper
88         start_helper_and_schedule(mycls);
89
90 }
91
92 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
93         struct vpn_cls* mycls = (struct vpn_cls*) cls;
94         char buf[65535];
95
96         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
97                 return;
98
99         int t = GNUNET_DISK_file_read(mycls->fh_from_helper, &buf, 65535);
100         if (t<=0) {
101                 fprintf(stderr, "Read error for header: %m\n");
102                 GNUNET_SCHEDULER_add_now(mycls->sched, restart_helper, cls);
103                 return;
104         }
105
106         /* FIXME */ GNUNET_SERVER_mst_receive(mycls->mst, NULL, buf, t, 0, 0);
107
108         GNUNET_SCHEDULER_add_read_file (mycls->sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls->fh_from_helper, &helper_read, mycls);
109 }
110
111 static void message_token(void *cls, void *client, const struct GNUNET_MessageHeader *message) {
112         if (ntohs(message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) return;
113
114         struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
115
116         fprintf(stderr, "Packet, Type: %x\n", ntohs(pkt_tun->tun.type));
117
118         if (ntohs(pkt_tun->tun.type) == 0x86dd) {
119                 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
120                 struct ip6_tcp *pkt6_tcp;
121                 struct ip6_udp *pkt6_udp;
122
123                 pkt_printf(pkt6);
124                 switch(pkt6->ip6_hdr.nxthdr) {
125                         case 0x06:
126                                 pkt6_tcp = (struct ip6_tcp*)pkt6;
127                                 pkt_printf_ip6tcp(pkt6_tcp);
128                                 break;
129                         case 0x11:
130                                 pkt6_udp = (struct ip6_udp*)pkt6;
131                                 pkt_printf_ip6udp(pkt6_udp);
132                                 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
133                                         pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
134                                 }
135                                 break;
136                 }
137         } else if (ntohs(pkt_tun->tun.type) == 0x0800) {
138                 struct ip_pkt *pkt = (struct ip_pkt*) message;
139                 struct ip_udp *udp = (struct ip_udp*) message;
140                 fprintf(stderr, "IPv4\n");
141                 if (pkt->ip_hdr.proto == 0x11 && ntohl(udp->ip_hdr.dadr) == 0x0a0a0a02 && ntohs(udp->udp_hdr.dpt) == 53 ) {
142                         pkt_printf_ipdns((struct ip_udp_dns*)udp);
143                 }
144         }
145
146 }
147
148 /**
149  * Main function that will be run by the scheduler.
150  *
151  * @param cls closure
152  * @param sched the scheduler to use
153  * @param args remaining command-line arguments
154  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
155  * @param cfg configuration
156  */
157 static void
158 run (void *cls,
159                 struct GNUNET_SCHEDULER_Handle *sched,
160                 char *const *args,
161                 const char *cfgfile,
162                 const struct GNUNET_CONFIGURATION_Handle *cfg) {
163
164         struct vpn_cls* mycls = (struct vpn_cls*) cls;
165
166         mycls->sched = sched;
167
168         mycls->mst = GNUNET_SERVER_mst_create(&message_token, mycls);
169
170         GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
171
172         start_helper_and_schedule(mycls);
173 }
174
175
176 /**
177  * The main function to obtain template from gnunetd.
178  *
179  * @param argc number of arguments from the command line
180  * @param argv command line arguments
181  * @return 0 ok, 1 on error
182  */
183 int
184 main (int argc, char *const *argv)
185 {
186   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
187     GNUNET_GETOPT_OPTION_END
188   };
189
190   struct vpn_cls* cls = GNUNET_malloc(sizeof(struct vpn_cls));
191
192   return (GNUNET_OK ==
193           GNUNET_PROGRAM_run (argc,
194                               argv,
195                               "gnunet-daemon-vpn",
196                               gettext_noop ("help text"),
197                               options, &run, cls)) ? ret : 1;
198
199   GNUNET_free(cls); // Make clang happy
200 }
201
202 /* end of gnunet-daemon-vpn.c */