seaspidering
[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 #include "block_dns.h"
40
41 /**
42  * Final status code.
43  */
44 static int ret;
45
46 struct vpn_cls {
47         struct GNUNET_DISK_PipeHandle* helper_in; // From the helper
48         struct GNUNET_DISK_PipeHandle* helper_out; // To the helper
49         const struct GNUNET_DISK_FileHandle* fh_from_helper;
50         const struct GNUNET_DISK_FileHandle* fh_to_helper;
51
52         struct GNUNET_SERVER_MessageStreamTokenizer* mst;
53
54         struct GNUNET_SCHEDULER_Handle *sched;
55
56         struct GNUNET_CLIENT_Connection *dns_connection;
57         unsigned char restart_hijack;
58
59         pid_t helper_pid;
60
61         const struct GNUNET_CONFIGURATION_Handle *cfg;
62
63         struct query_packet_list *head;
64         struct query_packet_list *tail;
65
66         struct answer_packet_list *answer_proc_head;
67         struct answer_packet_list *answer_proc_tail;
68 };
69
70 static struct vpn_cls mycls;
71
72 size_t send_query(void* cls, size_t size, void* buf);
73
74 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
75   GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
76   PLIBC_KILL(mycls.helper_pid, SIGTERM);
77   GNUNET_OS_process_wait(mycls.helper_pid);
78   if (mycls.dns_connection != NULL)
79     {
80       GNUNET_CLIENT_disconnect (mycls.dns_connection, GNUNET_NO);
81       mycls.dns_connection = NULL;
82     }
83 }
84
85 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx);
86
87 static void start_helper_and_schedule(void *cls,
88                                       const struct GNUNET_SCHEDULER_TaskContext *tc) {
89
90         if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
91           return;
92
93         mycls.helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
94         mycls.helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
95
96         if (mycls.helper_in == NULL || mycls.helper_out == NULL) return;
97
98         mycls.helper_pid = GNUNET_OS_start_process(mycls.helper_in, mycls.helper_out, "gnunet-helper-vpn", "gnunet-helper-vpn", NULL);
99
100         mycls.fh_from_helper = GNUNET_DISK_pipe_handle (mycls.helper_out, GNUNET_DISK_PIPE_END_READ);
101         mycls.fh_to_helper = GNUNET_DISK_pipe_handle (mycls.helper_in, GNUNET_DISK_PIPE_END_WRITE);
102
103         GNUNET_DISK_pipe_close_end(mycls.helper_out, GNUNET_DISK_PIPE_END_WRITE);
104         GNUNET_DISK_pipe_close_end(mycls.helper_in, GNUNET_DISK_PIPE_END_READ);
105
106         GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
107 }
108
109
110 static void restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
111         // Kill the helper
112         PLIBC_KILL(mycls.helper_pid, SIGKILL);
113         GNUNET_OS_process_wait(mycls.helper_pid);
114
115         /* Tell the dns-service to rehijack the dns-port
116          * The routing-table gets flushed if an interface disappears.
117          */
118         mycls.restart_hijack = 1;
119         GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
120
121         GNUNET_DISK_pipe_close(mycls.helper_in);
122         GNUNET_DISK_pipe_close(mycls.helper_out);
123
124         // Restart the helper
125         GNUNET_SCHEDULER_add_delayed (mycls.sched, GNUNET_TIME_UNIT_SECONDS, start_helper_and_schedule, NULL);
126
127 }
128
129 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
130         char buf[65535];
131
132         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
133                 return;
134
135         int t = GNUNET_DISK_file_read(mycls.fh_from_helper, &buf, 65535);
136         if (t<=0) {
137                 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read error for header from vpn-helper: %m\n");
138                 GNUNET_SCHEDULER_add_now(mycls.sched, restart_helper, cls);
139                 return;
140         }
141
142         /* FIXME */ GNUNET_SERVER_mst_receive(mycls.mst, NULL, buf, t, 0, 0);
143
144         GNUNET_SCHEDULER_add_read_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_from_helper, &helper_read, NULL);
145 }
146
147 static uint16_t calculate_ip_checksum(uint16_t* hdr, short len) {
148         uint32_t sum = 0;
149         for(; len >= 2; len -= 2)
150                 sum += *(hdr++);
151         if (len == 1)
152                 sum += *((unsigned char*)hdr);
153
154         sum = (sum >> 16) + (sum & 0xFFFF);
155
156         return ~sum;
157 }
158
159 static void helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
160         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
161                 return;
162         struct answer_packet_list* ans = mycls.answer_proc_head;
163         size_t len = ntohs(ans->pkt.hdr.size);
164
165         GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
166
167         size_t data_len = len - sizeof(struct answer_packet) + 1;
168         size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
169         size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
170
171         struct ip_udp_dns* pkt = alloca(pkt_len);
172         memset(pkt, 0, pkt_len);
173
174         pkt->shdr.size = htons(pkt_len);
175         pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
176
177         pkt->tun.flags = 0;
178         pkt->tun.type = htons(0x0800);
179
180         pkt->ip_hdr.version = 4;
181         pkt->ip_hdr.hdr_lngth = 5;
182         pkt->ip_hdr.diff_serv = 0;
183         pkt->ip_hdr.tot_lngth = htons(net_len);
184         pkt->ip_hdr.ident = 0;
185         pkt->ip_hdr.flags = 0;
186         pkt->ip_hdr.frag_off = 0;
187         pkt->ip_hdr.ttl = 255;
188         pkt->ip_hdr.proto = 0x11; /* UDP */
189         pkt->ip_hdr.chks = 0; /* Will be calculated later*/
190         pkt->ip_hdr.sadr = ans->pkt.from;
191         pkt->ip_hdr.dadr = ans->pkt.to;
192
193         pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
194
195         pkt->udp_dns.udp_hdr.spt = htons(53);
196         pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
197         pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
198         pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
199
200         memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
201         
202         GNUNET_CONTAINER_DLL_remove (mycls.answer_proc_head, mycls.answer_proc_tail, ans);
203         GNUNET_free(ans);
204
205         /* FIXME */ GNUNET_DISK_file_write(mycls.fh_to_helper, pkt, pkt_len);
206
207         if (mycls.answer_proc_head != NULL)
208                 GNUNET_SCHEDULER_add_write_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_to_helper, &helper_write, NULL);
209 }
210
211 size_t send_query(void* cls, size_t size, void* buf)
212 {
213   size_t len;
214   if (mycls.restart_hijack == 1)
215     {
216       mycls.restart_hijack = 0;
217       GNUNET_assert(sizeof(struct GNUNET_MessageHeader) >= size);
218       struct GNUNET_MessageHeader* hdr = buf;
219       len = sizeof(struct GNUNET_MessageHeader);
220       hdr->size = htons(len);
221       hdr->type = htons(GNUNET_MESSAGE_TYPE_REHIJACK);
222     }
223   else
224     {
225         struct query_packet_list* query = mycls.head;
226         len = ntohs(query->pkt.hdr.size);
227
228         GNUNET_assert(len <= size);
229
230         memcpy(buf, &query->pkt.hdr, len);
231
232         GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
233
234         GNUNET_free(query);
235     }
236
237         if (mycls.head != NULL || mycls.restart_hijack == 1) {
238                 GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
239         }
240
241         return len;
242 }
243
244 static void message_token(void *cls, void *client, const struct GNUNET_MessageHeader *message) {
245         if (ntohs(message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) return;
246
247         struct tun_pkt *pkt_tun = (struct tun_pkt*) message;
248
249         if (ntohs(pkt_tun->tun.type) == 0x86dd) {
250                 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
251                 struct ip6_tcp *pkt6_tcp;
252                 struct ip6_udp *pkt6_udp;
253
254                 pkt_printf(pkt6);
255                 switch(pkt6->ip6_hdr.nxthdr) {
256                         case 0x06:
257                                 pkt6_tcp = (struct ip6_tcp*)pkt6;
258                                 pkt_printf_ip6tcp(pkt6_tcp);
259                                 break;
260                         case 0x11:
261                                 pkt6_udp = (struct ip6_udp*)pkt6;
262                                 pkt_printf_ip6udp(pkt6_udp);
263                                 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
264                                         pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
265                                 }
266                                 break;
267                 }
268         } else if (ntohs(pkt_tun->tun.type) == 0x0800) {
269                 struct ip_pkt *pkt = (struct ip_pkt*) message;
270                 struct ip_udp *udp = (struct ip_udp*) message;
271                 GNUNET_assert(pkt->ip_hdr.version == 4);
272                 if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 ) {
273                         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]; */
274                         struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
275                         query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
276                         query->pkt.hdr.size = htons(len);
277                         query->pkt.orig_to = pkt->ip_hdr.dadr;
278                         query->pkt.orig_from = pkt->ip_hdr.sadr;
279                         query->pkt.src_port = udp->udp_hdr.spt;
280                         memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
281
282                         GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, query);
283
284                         if (mycls.dns_connection != NULL)
285                           /* struct GNUNET_CLIENT_TransmitHandle* th = */ GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, len, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
286                 }
287         }
288
289 }
290
291 static void 
292 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg);
293
294 static void 
295 reconnect_to_service_dns (void *cls,
296                           const struct GNUNET_SCHEDULER_TaskContext *tc) {
297   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
298     return;
299   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting\n");
300   GNUNET_assert (mycls.dns_connection == NULL);
301   mycls.dns_connection = GNUNET_CLIENT_connect (mycls.sched, "dns", mycls.cfg); 
302   GNUNET_CLIENT_receive(mycls.dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
303   if (mycls.head != NULL)
304     /* struct GNUNET_CLIENT_TransmitHandle* th = */ GNUNET_CLIENT_notify_transmit_ready(mycls.dns_connection, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
305 }
306
307 static void
308 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
309     struct answer_packet* pkt = cls;
310
311     if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
312       {
313         unsigned char ip6addr[16];
314
315         pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
316         memcpy(ip6addr, (int[]){htons(0x1234)}, 2);
317         memcpy(ip6addr+2, &pkt->peer, 7);
318         memcpy(ip6addr+9, &pkt->service_descriptor, 7);
319
320         memcpy(((char*)pkt)+ntohs(pkt->addroffset), ip6addr, 16);
321
322           /*FIXME:
323            * -save DNS_Record into hashmap, pointed to by ip
324            * -regularily walk through hashmap, deleting old entries
325            *  when is an entry old?
326            *  have a last-used field
327            *  don't remove if last-used "recent", ask dht again if record expired
328            */
329       }
330
331         struct answer_packet_list* list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
332
333         memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
334
335         GNUNET_CONTAINER_DLL_insert_after(mycls.answer_proc_head, mycls.answer_proc_tail, mycls.answer_proc_tail, list);
336
337         GNUNET_SCHEDULER_add_write_file (mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.fh_to_helper, &helper_write, NULL);
338
339         return;
340 }
341
342 static void
343 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg)
344 {
345   if (msg == NULL)
346     {
347       GNUNET_CLIENT_disconnect(mycls.dns_connection, GNUNET_NO);
348       mycls.dns_connection = NULL;
349       GNUNET_SCHEDULER_add_delayed (mycls.sched,
350                                     GNUNET_TIME_UNIT_SECONDS,
351                                     &reconnect_to_service_dns,
352                                     NULL);
353       return;
354     }
355
356   if (msg->type != htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS)) 
357     {
358       GNUNET_break (0);
359       GNUNET_CLIENT_disconnect(mycls.dns_connection, GNUNET_NO);
360       mycls.dns_connection = NULL;
361       GNUNET_SCHEDULER_add_now (mycls.sched,
362                                 &reconnect_to_service_dns,
363                                 NULL);
364       return;
365     }
366   void *pkt = GNUNET_malloc(ntohs(msg->size));
367
368   memcpy(pkt, msg, ntohs(msg->size));
369
370   GNUNET_SCHEDULER_add_now(mycls.sched, process_answer, pkt);
371   GNUNET_CLIENT_receive(mycls.dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
372 }
373
374 /**
375  * Main function that will be run by the scheduler.
376  *
377  * @param cls closure
378  * @param sched the scheduler to use
379  * @param args remaining command-line arguments
380  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
381  * @param cfg configuration
382  */
383 static void
384 run (void *cls,
385      struct GNUNET_SCHEDULER_Handle *sched,
386      char *const *args,
387      const char *cfgfile,
388      const struct GNUNET_CONFIGURATION_Handle *cfg) 
389 {
390   mycls.sched = sched;
391   mycls.mst = GNUNET_SERVER_mst_create(&message_token, NULL);
392   mycls.cfg = cfg;
393   mycls.restart_hijack = 0;
394   GNUNET_SCHEDULER_add_now (sched, &reconnect_to_service_dns, NULL);
395   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls); 
396   GNUNET_SCHEDULER_add_now (sched, start_helper_and_schedule, NULL);
397 }
398
399
400 /**
401  * The main function to obtain template from gnunetd.
402  *
403  * @param argc number of arguments from the command line
404  * @param argv command line arguments
405  * @return 0 ok, 1 on error
406  */
407 int
408 main (int argc, char *const *argv)
409 {
410   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
411     GNUNET_GETOPT_OPTION_END
412   };
413
414   return (GNUNET_OK ==
415           GNUNET_PROGRAM_run (argc,
416                               argv,
417                               "gnunet-daemon-vpn",
418                               gettext_noop ("help text"),
419                               options, &run, NULL)) ? ret : 1;
420 }
421
422 /* end of gnunet-daemon-vpn.c */