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