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 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.
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.
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/>.
20 * @file src/vpn/gnunet-vpn.c
21 * @brief Tool to manually request VPN tunnels to be created
22 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_tun_lib.h"
28 #include "gnunet_vpn_service.h"
32 * Handle to vpn service.
34 static struct GNUNET_VPN_Handle *handle;
37 * Opaque redirection request handle.
39 static struct GNUNET_VPN_RedirectionRequest *request;
42 * Option -p: destination peer identity for service
47 * Option -s: service name (hash to get service descriptor)
49 static char *service_name;
52 * Option -i: target IP
54 static char *target_ip;
57 * Option -4: IPv4 requested.
62 * Option -6: IPv6 requested.
67 * Option -t: TCP requested.
72 * Option -u: UDP requested.
77 * Selected level of verbosity.
79 static unsigned int verbosity;
82 * Global return value.
87 * Option '-d': duration of the mapping
89 static struct GNUNET_TIME_Relative duration = { 5 * 60 * 1000} ;
96 do_disconnect (void *cls)
100 GNUNET_VPN_cancel_request (request);
105 GNUNET_VPN_disconnect (handle);
108 GNUNET_free_non_null (peer_id);
109 GNUNET_free_non_null (service_name);
110 GNUNET_free_non_null (target_ip);
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.
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
128 allocation_cb (void *cls,
132 char buf[INET6_ADDRSTRLEN];
141 inet_ntop (af, address, buf, sizeof (buf)));
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145 _("Error creating tunnel\n"));
151 GNUNET_SCHEDULER_shutdown ();
156 * Main function that will be run by the scheduler.
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
167 const struct GNUNET_CONFIGURATION_Handle *cfg)
171 struct GNUNET_PeerIdentity peer;
172 struct GNUNET_HashCode sd;
177 struct GNUNET_TIME_Absolute etime;
179 etime = GNUNET_TIME_relative_to_absolute (duration);
180 GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
181 handle = GNUNET_VPN_connect (cfg);
189 FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
198 if (NULL == target_ip)
200 if (NULL == service_name)
202 FPRINTF (stderr, _("Option `%s' or `%s' is required.\n"),
208 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
214 FPRINTF (stderr, _("Option `%s' or `%s' is required when using option `%s'.\n"),
220 FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
225 protocol = IPPROTO_TCP;
227 protocol = IPPROTO_UDP;
229 GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
234 _("`%s' is not a valid peer identifier.\n"),
238 GNUNET_TUN_service_name_to_hash (service_name,
240 request = GNUNET_VPN_redirect_to_peer (handle,
246 &allocation_cb, NULL);
250 if (1 != inet_pton (AF_INET6, target_ip, &v6))
252 if (1 != inet_pton (AF_INET, target_ip, &v4))
254 FPRINTF (stderr, _("`%s' is not a valid IP address.\n"),
269 request = GNUNET_VPN_redirect_to_ip (handle,
274 &allocation_cb, NULL);
279 GNUNET_SCHEDULER_shutdown ();
285 main (int argc, char *const *argv)
287 struct GNUNET_GETOPT_CommandLineOption options[] = {
288 GNUNET_GETOPT_option_flag ('4',
290 gettext_noop ("request that result should be an IPv4 address"),
293 GNUNET_GETOPT_option_flag ('6',
295 gettext_noop ("request that result should be an IPv6 address"),
298 GNUNET_GETOPT_option_relative_time ('d',
301 gettext_noop ("how long should the mapping be valid for new tunnels?"),
304 GNUNET_GETOPT_option_string ('i',
307 gettext_noop ("destination IP for the tunnel"),
310 GNUNET_GETOPT_option_string ('p',
313 gettext_noop ("peer offering the service we would like to access"),
316 GNUNET_GETOPT_option_string ('s',
319 gettext_noop ("name of the service we would like to access"),
322 GNUNET_GETOPT_option_flag ('t',
324 gettext_noop ("service is offered via TCP"),
327 GNUNET_GETOPT_option_flag ('u',
329 gettext_noop ("service is offered via UDP"),
332 GNUNET_GETOPT_option_verbose (&verbosity),
334 GNUNET_GETOPT_OPTION_END
336 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
340 GNUNET_PROGRAM_run (argc, argv, "gnunet-vpn",
342 ("Setup tunnels via VPN."), options,
343 &run, NULL)) ? ret : 1;
344 GNUNET_free ((void *) argv);
349 /* end of gnunet-vpn.c */