2 This file is part of GNUnet.
3 Copyright (C) 2012 Christian Grothoff (and other contributing authors)
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, const struct GNUNET_SCHEDULER_TaskContext *tc)
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
166 run (void *cls, char *const *args, const char *cfgfile,
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_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
181 &do_disconnect, NULL);
182 handle = GNUNET_VPN_connect (cfg);
190 FPRINTF (stderr, _("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"),
209 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
215 FPRINTF (stderr, _("Option `%s' or `%s' is required when using option `%s'.\n"),
221 FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
226 protocol = IPPROTO_TCP;
228 protocol = IPPROTO_UDP;
230 GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
235 _("`%s' is not a valid peer identifier.\n"),
239 GNUNET_TUN_service_name_to_hash (service_name,
241 request = GNUNET_VPN_redirect_to_peer (handle,
247 &allocation_cb, NULL);
251 if (1 != inet_pton (AF_INET6, target_ip, &v6))
253 if (1 != inet_pton (AF_INET, target_ip, &v4))
255 FPRINTF (stderr, _("`%s' is not a valid IP address.\n"),
270 request = GNUNET_VPN_redirect_to_ip (handle,
275 &allocation_cb, NULL);
280 GNUNET_SCHEDULER_shutdown ();
286 main (int argc, char *const *argv)
288 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
290 gettext_noop ("request that result should be an IPv4 address"),
291 0, &GNUNET_GETOPT_set_one, &ipv4},
293 gettext_noop ("request that result should be an IPv6 address"),
294 0, &GNUNET_GETOPT_set_one, &ipv6},
295 {'d', "duration", "TIME",
296 gettext_noop ("how long should the mapping be valid for new tunnels?"),
297 1, &GNUNET_GETOPT_set_relative_time, &duration},
299 gettext_noop ("destination IP for the tunnel"),
300 1, &GNUNET_GETOPT_set_string, &target_ip},
301 {'p', "peer", "PEERID",
302 gettext_noop ("peer offering the service we would like to access"),
303 1, &GNUNET_GETOPT_set_string, &peer_id},
304 {'s', "service", "NAME",
305 gettext_noop ("name of the service we would like to access"),
306 1, &GNUNET_GETOPT_set_string, &service_name},
308 gettext_noop ("service is offered via TCP"),
309 0, &GNUNET_GETOPT_set_one, &tcp},
311 gettext_noop ("service is offered via UDP"),
312 0, &GNUNET_GETOPT_set_one, &udp},
314 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
315 GNUNET_GETOPT_OPTION_END
317 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
321 GNUNET_PROGRAM_run (argc, argv, "gnunet-vpn",
323 ("Setup tunnels via VPN."), options,
324 &run, NULL)) ? ret : 1;
325 GNUNET_free ((void *) argv);
330 /* end of gnunet-vpn.c */