2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_vpn_service.h"
33 * Handle to vpn service.
35 static struct GNUNET_VPN_Handle *handle;
38 * Opaque redirection request handle.
40 static struct GNUNET_VPN_RedirectionRequest *request;
43 * Option -p: destination peer identity for service
48 * Option -s: service name (hash to get service descriptor)
50 static char *service_name;
53 * Option -i: target IP
55 static char *target_ip;
58 * Option -4: IPv4 requested.
63 * Option -6: IPv6 requested.
68 * Option -t: TCP requested.
73 * Option -u: UDP requested.
78 * Selected level of verbosity.
83 * Option '-a': Notify only once the tunnel is connected?
88 * Global return value.
93 * Option '-d': duration of the mapping
95 static unsigned long long duration = 5 * 60;
102 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 GNUNET_VPN_cancel_request (request);
111 GNUNET_VPN_disconnect (handle);
114 GNUNET_free_non_null (peer_id);
115 GNUNET_free_non_null (service_name);
116 GNUNET_free_non_null (target_ip);
121 * Callback invoked from the VPN service once a redirection is
122 * available. Provides the IP address that can now be used to
123 * reach the requested destination.
126 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
127 * will match 'result_af' from the request
128 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
129 * that the VPN allocated for the redirection;
130 * traffic to this IP will now be redirected to the
131 * specified target peer; NULL on error
134 allocation_cb (void *cls,
138 char buf[INET6_ADDRSTRLEN];
147 inet_ntop (af, address, buf, sizeof (buf)));
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 _("Error creating tunnel\n"));
157 GNUNET_SCHEDULER_shutdown ();
162 * Main function that will be run by the scheduler.
165 * @param args remaining command-line arguments
166 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
167 * @param cfg configuration
170 run (void *cls, char *const *args, const char *cfgfile,
171 const struct GNUNET_CONFIGURATION_Handle *cfg)
175 struct GNUNET_PeerIdentity peer;
176 struct GNUNET_HashCode sd;
181 struct GNUNET_TIME_Absolute etime;
183 etime = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
184 (unsigned int) duration));
185 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
186 &do_disconnect, NULL);
187 handle = GNUNET_VPN_connect (cfg);
195 FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
204 if (NULL == target_ip)
206 if (NULL == service_name)
208 FPRINTF (stderr, _("Option `%s' or `%s' is required.\n"),
214 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
220 FPRINTF (stderr, _("Option `%s' or `%s' is required when using option `%s'.\n"),
226 FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
231 protocol = IPPROTO_TCP;
233 protocol = IPPROTO_UDP;
235 GNUNET_CRYPTO_hash_from_string (peer_id,
238 FPRINTF (stderr, _("`%s' is not a valid peer identifier.\n"),
242 GNUNET_CRYPTO_hash (service_name,
243 strlen (service_name),
245 request = GNUNET_VPN_redirect_to_peer (handle,
252 &allocation_cb, NULL);
256 if (1 != inet_pton (AF_INET6, target_ip, &v6))
258 if (1 != inet_pton (AF_INET, target_ip, &v4))
260 FPRINTF (stderr, _("`%s' is not a valid IP address.\n"),
275 request = GNUNET_VPN_redirect_to_ip (handle,
281 &allocation_cb, NULL);
286 GNUNET_SCHEDULER_shutdown ();
292 main (int argc, char *const *argv)
294 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
296 gettext_noop ("request that result should be an IPv4 address"),
297 0, &GNUNET_GETOPT_set_one, &ipv4},
299 gettext_noop ("request that result should be an IPv6 address"),
300 0, &GNUNET_GETOPT_set_one, &ipv6},
301 {'a', "after-connect", NULL,
302 gettext_noop ("print IP address only after mesh tunnel has been created"),
303 0, &GNUNET_GETOPT_set_one, &ipv6},
304 {'d', "duration", "SECONDS",
305 gettext_noop ("how long should the mapping be valid for new tunnels?"),
306 1, &GNUNET_GETOPT_set_ulong, &duration},
308 gettext_noop ("destination IP for the tunnel"),
309 1, &GNUNET_GETOPT_set_string, &target_ip},
310 {'p', "peer", "PEERID",
311 gettext_noop ("peer offering the service we would like to access"),
312 1, &GNUNET_GETOPT_set_string, &peer_id},
313 {'s', "service", "NAME",
314 gettext_noop ("name of the service we would like to access"),
315 1, &GNUNET_GETOPT_set_string, &service_name},
317 gettext_noop ("service is offered via TCP"),
318 0, &GNUNET_GETOPT_set_one, &tcp},
320 gettext_noop ("service is offered via UDP"),
321 0, &GNUNET_GETOPT_set_one, &udp},
323 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
324 GNUNET_GETOPT_OPTION_END
327 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
331 GNUNET_PROGRAM_run (argc, argv, "gnunet-vpn",
333 ("Setup tunnels via VPN."), options,
334 &run, NULL)) ? ret : 1;
338 /* end of gnunet-vpn.c */