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