increase timeout to give sparc buildbot a chance
[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-service-dns-p.h"
37 #include "gnunet_client_lib.h"
38 #include "gnunet_container_lib.h"
39
40 /**
41  * Final status code.
42  */
43 static int ret;
44
45 struct vpn_cls {
46         struct GNUNET_DISK_PipeHandle* helper_in; // From the helper
47         struct GNUNET_DISK_PipeHandle* helper_out; // To the helper
48         const struct GNUNET_DISK_FileHandle* fh_from_helper;
49
50         struct GNUNET_SERVER_MessageStreamTokenizer* mst;
51
52         struct GNUNET_SCHEDULER_Handle *sched;
53
54         struct GNUNET_CLIENT_Connection *dns_connection;
55
56         pid_t helper_pid;
57
58         struct query_packet_list *head;
59         struct query_packet_list *tail;
60 };
61
62 static struct vpn_cls mycls;
63
64 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
65         if (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) {
66                 PLIBC_KILL(mycls.helper_pid, SIGTERM);
67                 GNUNET_OS_process_wait(mycls.helper_pid);
68         }
69 }
70
71 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
72
73 static void start_helper_and_schedule() {
74         mycls.helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);;
75         mycls.helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
76
77         if (mycls.helper_in == NULL || mycls.helper_out == NULL) return;
78
79         mycls.helper_pid = GNUNET_OS_start_process(mycls.helper_in, mycls.helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
80
81         mycls.fh_from_helper = GNUNET_DISK_pipe_handle (mycls.helper_out, GNUNET_DISK_PIPE_END_READ);
82
83         GNUNET_DISK_pipe_close_end(mycls.helper_out, GNUNET_DISK_PIPE_END_WRITE);
84         GNUNET_DISK_pipe_close_end(mycls.helper_in, GNUNET_DISK_PIPE_END_READ);
85
86         GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
87 }
88
89
90 static void restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
91         // FIXME: Ratelimit this!
92
93         // Kill the helper
94         PLIBC_KILL(mycls.helper_pid, SIGKILL);
95         GNUNET_OS_process_wait(mycls.helper_pid);
96
97         GNUNET_DISK_pipe_close(mycls.helper_in);
98         GNUNET_DISK_pipe_close(mycls.helper_out);
99
100         // Restart the helper
101         start_helper_and_schedule(mycls);
102
103 }
104
105 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
106         char buf[65535];
107
108         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
109                 return;
110
111         int t = GNUNET_DISK_file_read(mycls.fh_from_helper, &buf, 65535);
112         if (t<=0) {
113                 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Read error for header: %m\n");
114                 GNUNET_SCHEDULER_add_now(mycls.sched, restart_helper, cls);
115                 return;
116         }
117
118         /* FIXME */ GNUNET_SERVER_mst_receive(mycls.mst, NULL, buf, t, 0, 0);
119
120         GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
121 }
122
123 size_t send_query(void* cls, size_t size, void* buf)
124 {
125         struct query_packet_list* query = mycls.head;
126         size_t len = ntohs(query->pkt.hdr.size);
127
128         GNUNET_assert(len <= size);
129
130         memcpy(buf, &query->pkt.hdr, len);
131
132         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes.\n", len);
133
134         GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
135
136         GNUNET_free(query);
137
138         if (mycls.head != NULL) {
139                 GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
140         }
141
142         return len;
143 }
144
145 static void message_token(void *cls, void *client, const struct GNUNET_MessageHeader *message) {
146         if (ntohs(message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) return;
147
148         struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
149
150         if (ntohs(pkt_tun->tun.type) == 0x86dd) {
151                 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
152                 struct ip6_tcp *pkt6_tcp;
153                 struct ip6_udp *pkt6_udp;
154
155                 pkt_printf(pkt6);
156                 switch(pkt6->ip6_hdr.nxthdr) {
157                         case 0x06:
158                                 pkt6_tcp = (struct ip6_tcp*)pkt6;
159                                 pkt_printf_ip6tcp(pkt6_tcp);
160                                 break;
161                         case 0x11:
162                                 pkt6_udp = (struct ip6_udp*)pkt6;
163                                 pkt_printf_ip6udp(pkt6_udp);
164                                 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
165                                         pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
166                                 }
167                                 break;
168                 }
169         } else if (ntohs(pkt_tun->tun.type) == 0x0800) {
170                 struct ip_pkt *pkt = (struct ip_pkt*) message;
171                 struct ip_udp *udp = (struct ip_udp*) message;
172                 if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 ) {
173                         size_t len = sizeof(struct query_packet) + ntohs(udp->udp_hdr.len) - 9; /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
174                         struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
175                         query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
176                         query->pkt.hdr.size = htons(len);
177                         query->pkt.orig_to = pkt->ip_hdr.dadr;
178                         query->pkt.orig_from = pkt->ip_hdr.sadr;
179                         query->pkt.src_port = udp->udp_hdr.spt;
180                         memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
181
182                         GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, query);
183
184                         struct GNUNET_CLIENT_TransmitHandle* th = GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, len, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
185                         if (th != NULL)
186                                 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Queued sending of %d bytes.\n", len);
187                         else
188                                 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Already queued for %d bytes.\n", len);
189                 }
190         }
191
192 }
193
194 /**
195  * Main function that will be run by the scheduler.
196  *
197  * @param cls closure
198  * @param sched the scheduler to use
199  * @param args remaining command-line arguments
200  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
201  * @param cfg configuration
202  */
203 static void
204 run (void *cls,
205      struct GNUNET_SCHEDULER_Handle *sched,
206      char *const *args,
207      const char *cfgfile,
208      const struct GNUNET_CONFIGURATION_Handle *cfg) 
209 {
210   mycls.sched = sched;
211   mycls.mst = GNUNET_SERVER_mst_create(&message_token, NULL);
212
213   mycls.dns_connection = GNUNET_CLIENT_connect (sched, "dns", cfg);
214   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connection: %x\n", mycls.dns_connection);
215
216   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls); 
217   start_helper_and_schedule(mycls);
218 }
219
220
221 /**
222  * The main function to obtain template from gnunetd.
223  *
224  * @param argc number of arguments from the command line
225  * @param argv command line arguments
226  * @return 0 ok, 1 on error
227  */
228 int
229 main (int argc, char *const *argv)
230 {
231   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
232     GNUNET_GETOPT_OPTION_END
233   };
234
235   return (GNUNET_OK ==
236           GNUNET_PROGRAM_run (argc,
237                               argv,
238                               "gnunet-daemon-vpn",
239                               gettext_noop ("help text"),
240                               options, &run, NULL)) ? ret : 1;
241 }
242
243 /* end of gnunet-daemon-vpn.c */