paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / vpn / gnunet-vpn.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file src/vpn/gnunet-vpn.c
21  * @brief Tool to manually request VPN tunnels to be created
22  * @author Christian Grothoff
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_tun_lib.h"
28 #include "gnunet_vpn_service.h"
29
30
31 /**
32  * Handle to vpn service.
33  */
34 static struct GNUNET_VPN_Handle *handle;
35
36 /**
37  * Opaque redirection request handle.
38  */
39 static struct GNUNET_VPN_RedirectionRequest *request;
40
41 /**
42  * Option -p: destination peer identity for service
43  */
44 static char *peer_id;
45
46 /**
47  * Option -s: service name (hash to get service descriptor)
48  */
49 static char *service_name;
50
51 /**
52  * Option -i: target IP
53  */
54 static char *target_ip;
55
56 /**
57  * Option -4: IPv4 requested.
58  */
59 static int ipv4;
60
61 /**
62  * Option -6: IPv6 requested.
63  */
64 static int ipv6;
65
66 /**
67  * Option -t: TCP requested.
68  */
69 static int tcp;
70
71 /**
72  * Option -u: UDP requested.
73  */
74 static int udp;
75
76 /**
77  * Selected level of verbosity.
78  */
79 static unsigned int verbosity;
80
81 /**
82  * Global return value.
83  */
84 static int ret;
85
86 /**
87  * Option '-d': duration of the mapping
88  */
89 static struct GNUNET_TIME_Relative duration = { 5 * 60 * 1000} ;
90
91
92 /**
93  * Shutdown.
94  */
95 static void
96 do_disconnect (void *cls)
97 {
98   if (NULL != request)
99   {
100     GNUNET_VPN_cancel_request (request);
101     request = NULL;
102   }
103   if (NULL != handle)
104   {
105     GNUNET_VPN_disconnect (handle);
106     handle = NULL;
107   }
108   GNUNET_free_non_null (peer_id);
109   GNUNET_free_non_null (service_name);
110   GNUNET_free_non_null (target_ip);
111 }
112
113
114 /**
115  * Callback invoked from the VPN service once a redirection is
116  * available.  Provides the IP address that can now be used to
117  * reach the requested destination.
118  *
119  * @param cls closure
120  * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
121  *                will match 'result_af' from the request
122  * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
123  *                that the VPN allocated for the redirection;
124  *                traffic to this IP will now be redirected to the
125  *                specified target peer; NULL on error
126  */
127 static void
128 allocation_cb (void *cls,
129                int af,
130                const void *address)
131 {
132   char buf[INET6_ADDRSTRLEN];
133
134   request = NULL;
135   switch (af)
136   {
137   case AF_INET6:
138   case AF_INET:
139     FPRINTF (stdout,
140              "%s\n",
141              inet_ntop (af, address, buf, sizeof (buf)));
142     break;
143   case AF_UNSPEC:
144     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145                 _("Error creating tunnel\n"));
146     ret = 1;
147     break;
148   default:
149     break;
150   }
151   GNUNET_SCHEDULER_shutdown ();
152 }
153
154
155 /**
156  * Main function that will be run by the scheduler.
157  *
158  * @param cls closure
159  * @param args remaining command-line arguments
160  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
161  * @param cfg configuration
162  */
163 static void
164 run (void *cls,
165      char *const *args,
166      const char *cfgfile,
167      const struct GNUNET_CONFIGURATION_Handle *cfg)
168 {
169   int dst_af;
170   int req_af;
171   struct GNUNET_PeerIdentity peer;
172   struct GNUNET_HashCode sd;
173   const void *addr;
174   struct in_addr v4;
175   struct in6_addr v6;
176   uint8_t protocol;
177   struct GNUNET_TIME_Absolute etime;
178
179   etime = GNUNET_TIME_relative_to_absolute (duration);
180   GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
181   handle = GNUNET_VPN_connect (cfg);
182   if (NULL == handle)
183     goto error;
184   req_af = AF_UNSPEC;
185   if (ipv4)
186   {
187     if (ipv6)
188     {
189       FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
190                "-4", "-6");
191       goto error;
192     }
193     req_af = AF_INET;
194   }
195   if (ipv6)
196     req_af = AF_INET6;
197
198   if (NULL == target_ip)
199   {
200     if (NULL == service_name)
201     {
202       FPRINTF (stderr, _("Option `%s' or `%s' is required.\n"),
203                "-i", "-s");
204       goto error;
205     }
206     if (NULL == peer_id)
207     {
208       FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
209                "-p", "-s");
210       goto error;
211     }
212     if (! (tcp | udp) )
213     {
214       FPRINTF (stderr, _("Option `%s' or `%s' is required when using option `%s'.\n"),
215                "-t", "-u", "-s");
216       goto error;
217     }
218     if (tcp & udp)
219     {
220       FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
221                "-t", "-u");
222       goto error;
223     }
224     if (tcp)
225       protocol = IPPROTO_TCP;
226     if (udp)
227       protocol = IPPROTO_UDP;
228     if (GNUNET_OK !=
229         GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
230                                                     strlen (peer_id),
231                                                     &peer.public_key))
232     {
233       FPRINTF (stderr,
234                _("`%s' is not a valid peer identifier.\n"),
235                peer_id);
236       goto error;
237     }
238     GNUNET_TUN_service_name_to_hash (service_name,
239                                      &sd);
240     request = GNUNET_VPN_redirect_to_peer (handle,
241                                            req_af,
242                                            protocol,
243                                            &peer,
244                                            &sd,
245                                            etime,
246                                            &allocation_cb, NULL);
247   }
248   else
249   {
250     if (1 != inet_pton (AF_INET6, target_ip, &v6))
251     {
252       if (1 != inet_pton (AF_INET, target_ip, &v4))
253       {
254         FPRINTF (stderr, _("`%s' is not a valid IP address.\n"),
255                  target_ip);
256         goto error;
257       }
258       else
259       {
260         dst_af = AF_INET;
261         addr = &v4;
262       }
263     }
264     else
265     {
266       dst_af = AF_INET6;
267       addr = &v6;
268     }
269     request = GNUNET_VPN_redirect_to_ip (handle,
270                                          req_af,
271                                          dst_af,
272                                          addr,
273                                          etime,
274                                          &allocation_cb, NULL);
275   }
276   return;
277
278  error:
279   GNUNET_SCHEDULER_shutdown ();
280   ret = 1;
281 }
282
283
284 int
285 main (int argc, char *const *argv)
286 {
287   struct GNUNET_GETOPT_CommandLineOption options[] = {
288     GNUNET_GETOPT_option_flag ('4',
289                                   "ipv4",
290                                   gettext_noop ("request that result should be an IPv4 address"),
291                                   &ipv4),
292
293     GNUNET_GETOPT_option_flag ('6',
294                                   "ipv6",
295                                   gettext_noop ("request that result should be an IPv6 address"),
296                                   &ipv6),
297
298     GNUNET_GETOPT_option_relative_time ('d',
299                                             "duration",
300                                             "TIME",
301                                             gettext_noop ("how long should the mapping be valid for new tunnels?"),
302                                             &duration),
303
304     GNUNET_GETOPT_option_string ('i',
305                                  "ip",
306                                  "IP",
307                                  gettext_noop ("destination IP for the tunnel"),
308                                  &target_ip),
309
310     GNUNET_GETOPT_option_string ('p',
311                                  "peer",
312                                  "PEERID",
313                                  gettext_noop ("peer offering the service we would like to access"),
314                                  &peer_id),
315
316     GNUNET_GETOPT_option_string ('s', 
317                                  "service",
318                                  "NAME",
319                                  gettext_noop ("name of the service we would like to access"),
320                                  &service_name),
321
322     GNUNET_GETOPT_option_flag ('t',
323                                   "tcp",
324                                   gettext_noop ("service is offered via TCP"),
325                                   &tcp),
326
327     GNUNET_GETOPT_option_flag ('u',
328                                   "udp",
329                                   gettext_noop ("service is offered via UDP"),
330                                   &udp),
331
332     GNUNET_GETOPT_option_verbose (&verbosity),
333
334     GNUNET_GETOPT_OPTION_END
335   };
336   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
337     return 2;
338
339   ret = (GNUNET_OK ==
340          GNUNET_PROGRAM_run (argc, argv, "gnunet-vpn",
341                              gettext_noop
342                              ("Setup tunnels via VPN."), options,
343                               &run, NULL)) ? ret : 1;
344   GNUNET_free ((void *) argv);
345   return ret;
346 }
347
348
349 /* end of gnunet-vpn.c */