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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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.
81 static unsigned int 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, int af, const void *address)
132 char buf[INET6_ADDRSTRLEN];
139 fprintf (stdout, "%s\n", inet_ntop (af, address, buf, sizeof(buf)));
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Error creating tunnel\n"));
150 GNUNET_SCHEDULER_shutdown ();
155 * Main function that will be run by the scheduler.
158 * @param args remaining command-line arguments
159 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
160 * @param cfg configuration
166 const struct GNUNET_CONFIGURATION_Handle *cfg)
170 struct GNUNET_PeerIdentity peer;
171 struct GNUNET_HashCode sd;
176 struct GNUNET_TIME_Absolute etime;
178 etime = GNUNET_TIME_relative_to_absolute (duration);
179 GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
180 handle = GNUNET_VPN_connect (cfg);
189 _ ("Option `%s' makes no sense with option `%s'.\n"),
199 if (NULL == target_ip)
201 if (NULL == service_name)
203 fprintf (stderr, _ ("Option `%s' or `%s' is required.\n"), "-i", "-s");
209 _ ("Option `%s' is required when using option `%s'.\n"),
217 _ ("Option `%s' or `%s' is required when using option `%s'.\n"),
226 _ ("Option `%s' makes no sense with option `%s'.\n"),
232 protocol = IPPROTO_TCP;
234 protocol = IPPROTO_UDP;
236 GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
240 fprintf (stderr, _ ("`%s' is not a valid peer identifier.\n"), peer_id);
243 GNUNET_TUN_service_name_to_hash (service_name, &sd);
244 request = GNUNET_VPN_redirect_to_peer (handle,
255 if (1 != inet_pton (AF_INET6, target_ip, &v6))
257 if (1 != inet_pton (AF_INET, target_ip, &v4))
259 fprintf (stderr, _ ("`%s' is not a valid IP address.\n"), target_ip);
273 request = GNUNET_VPN_redirect_to_ip (handle,
284 GNUNET_SCHEDULER_shutdown ();
290 main (int argc, char *const *argv)
292 struct GNUNET_GETOPT_CommandLineOption options[] =
293 { GNUNET_GETOPT_option_flag ('4',
296 "request that result should be an IPv4 address"),
299 GNUNET_GETOPT_option_flag ('6',
302 "request that result should be an IPv6 address"),
305 GNUNET_GETOPT_option_relative_time (
309 gettext_noop ("how long should the mapping be valid for new tunnels?"),
312 GNUNET_GETOPT_option_string ('i',
316 "destination IP for the tunnel"),
319 GNUNET_GETOPT_option_string (
323 gettext_noop ("peer offering the service we would like to access"),
326 GNUNET_GETOPT_option_string ('s',
330 "name of the service we would like to access"),
333 GNUNET_GETOPT_option_flag ('t',
335 gettext_noop ("service is offered via TCP"),
338 GNUNET_GETOPT_option_flag ('u',
340 gettext_noop ("service is offered via UDP"),
343 GNUNET_GETOPT_option_verbose (&verbosity),
345 GNUNET_GETOPT_OPTION_END };
347 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
351 (GNUNET_OK == GNUNET_PROGRAM_run (argc,
354 gettext_noop ("Setup tunnels via VPN."),
360 GNUNET_free ((void *) argv);
365 /* end of gnunet-vpn.c */