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