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 * Global return value.
88 * Option '-d': duration of the mapping
90 static struct GNUNET_TIME_Relative duration = { 5 * 60 * 1000} ;
97 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 GNUNET_VPN_cancel_request (request);
106 GNUNET_VPN_disconnect (handle);
109 GNUNET_free_non_null (peer_id);
110 GNUNET_free_non_null (service_name);
111 GNUNET_free_non_null (target_ip);
116 * Callback invoked from the VPN service once a redirection is
117 * available. Provides the IP address that can now be used to
118 * reach the requested destination.
121 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
122 * will match 'result_af' from the request
123 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
124 * that the VPN allocated for the redirection;
125 * traffic to this IP will now be redirected to the
126 * specified target peer; NULL on error
129 allocation_cb (void *cls,
133 char buf[INET6_ADDRSTRLEN];
142 inet_ntop (af, address, buf, sizeof (buf)));
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146 _("Error creating tunnel\n"));
152 GNUNET_SCHEDULER_shutdown ();
157 * Main function that will be run by the scheduler.
160 * @param args remaining command-line arguments
161 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
162 * @param cfg configuration
165 run (void *cls, char *const *args, const char *cfgfile,
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_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
180 &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_hash_from_string (peer_id,
232 FPRINTF (stderr, _("`%s' is not a valid peer identifier.\n"),
236 GNUNET_CRYPTO_hash (service_name,
237 strlen (service_name),
239 request = GNUNET_VPN_redirect_to_peer (handle,
245 &allocation_cb, NULL);
249 if (1 != inet_pton (AF_INET6, target_ip, &v6))
251 if (1 != inet_pton (AF_INET, target_ip, &v4))
253 FPRINTF (stderr, _("`%s' is not a valid IP address.\n"),
268 request = GNUNET_VPN_redirect_to_ip (handle,
273 &allocation_cb, NULL);
278 GNUNET_SCHEDULER_shutdown ();
284 main (int argc, char *const *argv)
286 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
288 gettext_noop ("request that result should be an IPv4 address"),
289 0, &GNUNET_GETOPT_set_one, &ipv4},
291 gettext_noop ("request that result should be an IPv6 address"),
292 0, &GNUNET_GETOPT_set_one, &ipv6},
293 {'d', "duration", "TIME",
294 gettext_noop ("how long should the mapping be valid for new tunnels?"),
295 1, &GNUNET_GETOPT_set_relative_time, &duration},
297 gettext_noop ("destination IP for the tunnel"),
298 1, &GNUNET_GETOPT_set_string, &target_ip},
299 {'p', "peer", "PEERID",
300 gettext_noop ("peer offering the service we would like to access"),
301 1, &GNUNET_GETOPT_set_string, &peer_id},
302 {'s', "service", "NAME",
303 gettext_noop ("name of the service we would like to access"),
304 1, &GNUNET_GETOPT_set_string, &service_name},
306 gettext_noop ("service is offered via TCP"),
307 0, &GNUNET_GETOPT_set_one, &tcp},
309 gettext_noop ("service is offered via UDP"),
310 0, &GNUNET_GETOPT_set_one, &udp},
312 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
313 GNUNET_GETOPT_OPTION_END
315 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
319 GNUNET_PROGRAM_run (argc, argv, "gnunet-vpn",
321 ("Setup tunnels via VPN."), options,
322 &run, NULL)) ? ret : 1;
323 GNUNET_free ((void *) argv);
328 /* end of gnunet-vpn.c */