fix #3284: support lib/MULTIARCH/ paths in installation, use GNUNET_PREFIX=@libdir...
[oweals/gnunet.git] / src / vpn / gnunet-vpn.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file src/vpn/gnunet-vpn.c
23  * @brief Tool to manually request VPN tunnels to be created
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_tun_lib.h"
30 #include "gnunet_vpn_service.h"
31
32
33 /**
34  * Handle to vpn service.
35  */
36 static struct GNUNET_VPN_Handle *handle;
37
38 /**
39  * Opaque redirection request handle.
40  */
41 static struct GNUNET_VPN_RedirectionRequest *request;
42
43 /**
44  * Option -p: destination peer identity for service
45  */
46 static char *peer_id;
47
48 /**
49  * Option -s: service name (hash to get service descriptor)
50  */
51 static char *service_name;
52
53 /**
54  * Option -i: target IP
55  */
56 static char *target_ip;
57
58 /**
59  * Option -4: IPv4 requested.
60  */
61 static int ipv4;
62
63 /**
64  * Option -6: IPv6 requested.
65  */
66 static int ipv6;
67
68 /**
69  * Option -t: TCP requested.
70  */
71 static int tcp;
72
73 /**
74  * Option -u: UDP requested.
75  */
76 static int udp;
77
78 /**
79  * Selected level of verbosity.
80  */
81 static int verbosity;
82
83 /**
84  * Global return value.
85  */
86 static int ret;
87
88 /**
89  * Option '-d': duration of the mapping
90  */
91 static struct GNUNET_TIME_Relative duration = { 5 * 60 * 1000} ;
92
93
94 /**
95  * Shutdown.
96  */
97 static void
98 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   if (NULL != request)
101   {
102     GNUNET_VPN_cancel_request (request);
103     request = NULL;
104   }
105   if (NULL != handle)
106   {
107     GNUNET_VPN_disconnect (handle);
108     handle = NULL;
109   }
110   GNUNET_free_non_null (peer_id);
111   GNUNET_free_non_null (service_name);
112   GNUNET_free_non_null (target_ip);
113 }
114
115
116 /**
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.
120  *
121  * @param cls closure
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
128  */
129 static void
130 allocation_cb (void *cls,
131                int af,
132                const void *address)
133 {
134   char buf[INET6_ADDRSTRLEN];
135
136   request = NULL;
137   switch (af)
138   {
139   case AF_INET6:
140   case AF_INET:
141     FPRINTF (stdout,
142              "%s\n",
143              inet_ntop (af, address, buf, sizeof (buf)));
144     break;
145   case AF_UNSPEC:
146     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147                 _("Error creating tunnel\n"));
148     ret = 1;
149     break;
150   default:
151     break;
152   }
153   GNUNET_SCHEDULER_shutdown ();
154 }
155
156
157 /**
158  * Main function that will be run by the scheduler.
159  *
160  * @param cls closure
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
164  */
165 static void
166 run (void *cls, char *const *args, const char *cfgfile,
167      const struct GNUNET_CONFIGURATION_Handle *cfg)
168 {
169   int dst_af;
170   int req_af;
171   struct GNUNET_PeerIdentity peer;
172   struct GNUNET_HashCode sd;
173   const void *addr;
174   struct in_addr v4;
175   struct in6_addr v6;
176   uint8_t protocol;
177   struct GNUNET_TIME_Absolute etime;
178
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);
183   if (NULL == handle)
184     goto error;
185   req_af = AF_UNSPEC;
186   if (ipv4)
187   {
188     if (ipv6)
189     {
190       FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
191                "-4", "-6");
192       goto error;
193     }
194     req_af = AF_INET;
195   }
196   if (ipv6)
197     req_af = AF_INET6;
198
199   if (NULL == target_ip)
200   {
201     if (NULL == service_name)
202     {
203       FPRINTF (stderr, _("Option `%s' or `%s' is required.\n"),
204                "-i", "-s");
205       goto error;
206     }
207     if (NULL == peer_id)
208     {
209       FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
210                "-p", "-s");
211       goto error;
212     }
213     if (! (tcp | udp) )
214     {
215       FPRINTF (stderr, _("Option `%s' or `%s' is required when using option `%s'.\n"),
216                "-t", "-u", "-s");
217       goto error;
218     }
219     if (tcp & udp)
220     {
221       FPRINTF (stderr, _("Option `%s' makes no sense with option `%s'.\n"),
222                "-t", "-u");
223       goto error;
224     }
225     if (tcp)
226       protocol = IPPROTO_TCP;
227     if (udp)
228       protocol = IPPROTO_UDP;
229     if (GNUNET_OK !=
230         GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
231                                                     strlen (peer_id),
232                                                     &peer.public_key))
233     {
234       FPRINTF (stderr,
235                _("`%s' is not a valid peer identifier.\n"),
236                peer_id);
237       goto error;
238     }
239     GNUNET_TUN_service_name_to_hash (service_name,
240                                      &sd);
241     request = GNUNET_VPN_redirect_to_peer (handle,
242                                            req_af,
243                                            protocol,
244                                            &peer,
245                                            &sd,
246                                            etime,
247                                            &allocation_cb, NULL);
248   }
249   else
250   {
251     if (1 != inet_pton (AF_INET6, target_ip, &v6))
252     {
253       if (1 != inet_pton (AF_INET, target_ip, &v4))
254       {
255         FPRINTF (stderr, _("`%s' is not a valid IP address.\n"),
256                  target_ip);
257         goto error;
258       }
259       else
260       {
261         dst_af = AF_INET;
262         addr = &v4;
263       }
264     }
265     else
266     {
267       dst_af = AF_INET6;
268       addr = &v6;
269     }
270     request = GNUNET_VPN_redirect_to_ip (handle,
271                                          req_af,
272                                          dst_af,
273                                          addr,
274                                          etime,
275                                          &allocation_cb, NULL);
276   }
277   return;
278
279  error:
280   GNUNET_SCHEDULER_shutdown ();
281   ret = 1;
282 }
283
284
285 int
286 main (int argc, char *const *argv)
287 {
288   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
289     {'4', "ipv4", NULL,
290      gettext_noop ("request that result should be an IPv4 address"),
291      0, &GNUNET_GETOPT_set_one, &ipv4},
292     {'6', "ipv6", NULL,
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},
298     {'i', "ip", "IP",
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},
307     {'t', "tcp", NULL,
308      gettext_noop ("service is offered via TCP"),
309      0, &GNUNET_GETOPT_set_one, &tcp},
310     {'u', "udp", NULL,
311      gettext_noop ("service is offered via UDP"),
312      0, &GNUNET_GETOPT_set_one, &udp},
313
314     GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
315     GNUNET_GETOPT_OPTION_END
316   };
317   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
318     return 2;
319
320   ret = (GNUNET_OK ==
321          GNUNET_PROGRAM_run (argc, argv, "gnunet-vpn",
322                              gettext_noop
323                              ("Setup tunnels via VPN."), options,
324                               &run, NULL)) ? ret : 1;
325   GNUNET_free ((void *) argv);
326   return ret;
327 }
328
329
330 /* end of gnunet-vpn.c */