aboutsummaryrefslogtreecommitdiff
path: root/src/vpn/gnunet-vpn.c
blob: 60d631d5e65e2ddcf19b9a1997101d1873b0db6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
     This file is part of GNUnet.
     Copyright (C) 2012 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file src/vpn/gnunet-vpn.c
 * @brief Tool to manually request VPN tunnels to be created
 * @author Christian Grothoff
 */

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_tun_lib.h"
#include "gnunet_vpn_service.h"


/**
 * Handle to vpn service.
 */
static struct GNUNET_VPN_Handle *handle;

/**
 * Opaque redirection request handle.
 */
static struct GNUNET_VPN_RedirectionRequest *request;

/**
 * Option -p: destination peer identity for service
 */
static char *peer_id;

/**
 * Option -s: service name (hash to get service descriptor)
 */
static char *service_name;

/**
 * Option -i: target IP
 */
static char *target_ip;

/**
 * Option -4: IPv4 requested.
 */
static int ipv4;

/**
 * Option -6: IPv6 requested.
 */
static int ipv6;

/**
 * Option -t: TCP requested.
 */
static int tcp;

/**
 * Option -u: UDP requested.
 */
static int udp;

/**
 * Selected level of verbosity.
 */
static unsigned int verbosity;

/**
 * Global return value.
 */
static int ret;

/**
 * Option '-d': duration of the mapping
 */
static struct GNUNET_TIME_Relative duration = { 5 * 60 * 1000 };


/**
 * Shutdown.
 */
static void
do_disconnect (void *cls)
{
  if (NULL != request)
  {
    GNUNET_VPN_cancel_request (request);
    request = NULL;
  }
  if (NULL != handle)
  {
    GNUNET_VPN_disconnect (handle);
    handle = NULL;
  }
  GNUNET_free (peer_id);
  GNUNET_free (service_name);
  GNUNET_free (target_ip);
}


/**
 * Callback invoked from the VPN service once a redirection is
 * available.  Provides the IP address that can now be used to
 * reach the requested destination.
 *
 * @param cls closure
 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
 *                will match 'result_af' from the request
 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
 *                that the VPN allocated for the redirection;
 *                traffic to this IP will now be redirected to the
 *                specified target peer; NULL on error
 */
static void
allocation_cb (void *cls, int af, const void *address)
{
  char buf[INET6_ADDRSTRLEN];

  request = NULL;
  switch (af)
  {
  case AF_INET6:
  case AF_INET:
    fprintf (stdout, "%s\n", inet_ntop (af, address, buf, sizeof(buf)));
    break;

  case AF_UNSPEC:
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Error creating tunnel\n"));
    ret = 1;
    break;

  default:
    break;
  }
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Main function that will be run by the scheduler.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  int dst_af;
  int req_af;
  struct GNUNET_PeerIdentity peer;
  struct GNUNET_HashCode sd;
  const void *addr;
  struct in_addr v4;
  struct in6_addr v6;
  uint8_t protocol;
  struct GNUNET_TIME_Absolute etime;

  etime = GNUNET_TIME_relative_to_absolute (duration);
  GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
  handle = GNUNET_VPN_connect (cfg);
  if (NULL == handle)
    goto error;
  req_af = AF_UNSPEC;
  if (ipv4)
  {
    if (ipv6)
    {
      fprintf (stderr,
               _ ("Option `%s' makes no sense with option `%s'.\n"),
               "-4",
               "-6");
      goto error;
    }
    req_af = AF_INET;
  }
  if (ipv6)
    req_af = AF_INET6;

  if (NULL == target_ip)
  {
    if (NULL == service_name)
    {
      fprintf (stderr, _ ("Option `%s' or `%s' is required.\n"), "-i", "-s");
      goto error;
    }
    if (NULL == peer_id)
    {
      fprintf (stderr,
               _ ("Option `%s' is required when using option `%s'.\n"),
               "-p",
               "-s");
      goto error;
    }
    if (! (tcp | udp))
    {
      fprintf (stderr,
               _ ("Option `%s' or `%s' is required when using option `%s'.\n"),
               "-t",
               "-u",
               "-s");
      goto error;
    }
    if (tcp & udp)
    {
      fprintf (stderr,
               _ ("Option `%s' makes no sense with option `%s'.\n"),
               "-t",
               "-u");
      goto error;
    }
    if (tcp)
      protocol = IPPROTO_TCP;
    if (udp)
      protocol = IPPROTO_UDP;
    if (GNUNET_OK !=
        GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
                                                    strlen (peer_id),
                                                    &peer.public_key))
    {
      fprintf (stderr, _ ("`%s' is not a valid peer identifier.\n"), peer_id);
      goto error;
    }
    GNUNET_TUN_service_name_to_hash (service_name, &sd);
    request = GNUNET_VPN_redirect_to_peer (handle,
                                           req_af,
                                           protocol,
                                           &peer,
                                           &sd,
                                           etime,
                                           &allocation_cb,
                                           NULL);
  }
  else
  {
    if (1 != inet_pton (AF_INET6, target_ip, &v6))
    {
      if (1 != inet_pton (AF_INET, target_ip, &v4))
      {
        fprintf (stderr, _ ("`%s' is not a valid IP address.\n"), target_ip);
        goto error;
      }
      else
      {
        dst_af = AF_INET;
        addr = &v4;
      }
    }
    else
    {
      dst_af = AF_INET6;
      addr = &v6;
    }
    request = GNUNET_VPN_redirect_to_ip (handle,
                                         req_af,
                                         dst_af,
                                         addr,
                                         etime,
                                         &allocation_cb,
                                         NULL);
  }
  return;

error:
  GNUNET_SCHEDULER_shutdown ();
  ret = 1;
}


int
main (int argc, char *const *argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] =
  { GNUNET_GETOPT_option_flag ('4',
                               "ipv4",
                               gettext_noop (
                                 "request that result should be an IPv4 address"),
                               &ipv4),

    GNUNET_GETOPT_option_flag ('6',
                               "ipv6",
                               gettext_noop (
                                 "request that result should be an IPv6 address"),
                               &ipv6),

    GNUNET_GETOPT_option_relative_time (
      'd',
      "duration",
      "TIME",
      gettext_noop ("how long should the mapping be valid for new tunnels?"),
      &duration),

    GNUNET_GETOPT_option_string ('i',
                                 "ip",
                                 "IP",
                                 gettext_noop (
                                   "destination IP for the tunnel"),
                                 &target_ip),

    GNUNET_GETOPT_option_string (
      'p',
      "peer",
      "PEERID",
      gettext_noop ("peer offering the service we would like to access"),
      &peer_id),

    GNUNET_GETOPT_option_string ('s',
                                 "service",
                                 "NAME",
                                 gettext_noop (
                                   "name of the service we would like to access"),
                                 &service_name),

    GNUNET_GETOPT_option_flag ('t',
                               "tcp",
                               gettext_noop ("service is offered via TCP"),
                               &tcp),

    GNUNET_GETOPT_option_flag ('u',
                               "udp",
                               gettext_noop ("service is offered via UDP"),
                               &udp),

    GNUNET_GETOPT_option_verbose (&verbosity),

    GNUNET_GETOPT_OPTION_END };

  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
    return 2;

  ret =
    (GNUNET_OK == GNUNET_PROGRAM_run (argc,
                                      argv,
                                      "gnunet-vpn",
                                      gettext_noop ("Setup tunnels via VPN."),
                                      options,
                                      &run,
                                      NULL))
    ? ret
    : 1;
  GNUNET_free_nz ((void *) argv);
  return ret;
}


/* end of gnunet-vpn.c */