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