2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
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.
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.
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.
22 * @file src/vpn/gnunet-vpn.c
23 * @brief Tool to manually request VPN tunnels to be created
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_tun_lib.h"
30 #include "gnunet_vpn_service.h"
34 * Handle to vpn service.
36 static struct GNUNET_VPN_Handle *handle;
39 * Opaque redirection request handle.
41 static struct GNUNET_VPN_RedirectionRequest *request;
44 * Option -p: destination peer identity for service
49 * Option -s: service name (hash to get service descriptor)
51 static char *service_name;
54 * Option -i: target IP
56 static char *target_ip;
59 * Option -4: IPv4 requested.
64 * Option -6: IPv6 requested.
69 * Option -t: TCP requested.
74 * Option -u: UDP requested.
79 * Selected level of verbosity.
84 * Global return value.
89 * Option '-d': duration of the mapping
91 static struct GNUNET_TIME_Relative duration = { 5 * 60 * 1000} ;
98 do_disconnect (void *cls)
102 GNUNET_VPN_cancel_request (request);
107 GNUNET_VPN_disconnect (handle);
110 GNUNET_free_non_null (peer_id);
111 GNUNET_free_non_null (service_name);
112 GNUNET_free_non_null (target_ip);
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.
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
130 allocation_cb (void *cls,
134 char buf[INET6_ADDRSTRLEN];
143 inet_ntop (af, address, buf, sizeof (buf)));
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147 _("Error creating tunnel\n"));
153 GNUNET_SCHEDULER_shutdown ();
158 * Main function that will be run by the scheduler.
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
169 const struct GNUNET_CONFIGURATION_Handle *cfg)
173 struct GNUNET_PeerIdentity peer;
174 struct GNUNET_HashCode sd;
179 struct GNUNET_TIME_Absolute etime;
181 etime = GNUNET_TIME_relative_to_absolute (duration);
182 GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
183 handle = GNUNET_VPN_connect (cfg);
191 FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
200 if (NULL == target_ip)
202 if (NULL == service_name)
204 FPRINTF (stderr, _("Option `%s' or `%s' is required.\n"),
210 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
216 FPRINTF (stderr, _("Option `%s' or `%s' is required when using option `%s'.\n"),
222 FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
227 protocol = IPPROTO_TCP;
229 protocol = IPPROTO_UDP;
231 GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
236 _("`%s' is not a valid peer identifier.\n"),
240 GNUNET_TUN_service_name_to_hash (service_name,
242 request = GNUNET_VPN_redirect_to_peer (handle,
248 &allocation_cb, NULL);
252 if (1 != inet_pton (AF_INET6, target_ip, &v6))
254 if (1 != inet_pton (AF_INET, target_ip, &v4))
256 FPRINTF (stderr, _("`%s' is not a valid IP address.\n"),
271 request = GNUNET_VPN_redirect_to_ip (handle,
276 &allocation_cb, NULL);
281 GNUNET_SCHEDULER_shutdown ();
287 main (int argc, char *const *argv)
289 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
291 gettext_noop ("request that result should be an IPv4 address"),
292 0, &GNUNET_GETOPT_set_one, &ipv4},
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},
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},
309 gettext_noop ("service is offered via TCP"),
310 0, &GNUNET_GETOPT_set_one, &tcp},
312 gettext_noop ("service is offered via UDP"),
313 0, &GNUNET_GETOPT_set_one, &udp},
315 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
316 GNUNET_GETOPT_OPTION_END
318 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
322 GNUNET_PROGRAM_run (argc, argv, "gnunet-vpn",
324 ("Setup tunnels via VPN."), options,
325 &run, NULL)) ? ret : 1;
326 GNUNET_free ((void *) argv);
331 /* end of gnunet-vpn.c */