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