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