aboutsummaryrefslogtreecommitdiff
path: root/src/nat/upnp.c
blob: 45c7df41994220a3914802684b7400e3e727bb8d (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
     This file is part of GNUnet.
     (C) 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/*
 * This file has been adapted from the Transmission project:
 * Originally licensed by the GPL version 2.
 * Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com>
 */

/**
 * @file nat/upnp.c
 * @brief UPnP support for the NAT library
 *
 * @author Milan Bouchet-Valat
 */
#include "platform.h"
#include "gnunet_common.h"

#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <string.h>

#include "gnunet_nat_lib.h"
#include "nat.h"
#include "upnp-discover.h"
#include "upnp-commands.h"
#include "upnp.h"

/* Component name for logging */
#define COMP_NAT_UPNP _("NAT (UPnP)")

enum UPNP_State
{
  UPNP_IDLE,
  UPNP_ERR,
  UPNP_DISCOVER,
  UPNP_MAP,
  UPNP_UNMAP
};

struct GNUNET_NAT_UPNP_Handle
{
  int hasDiscovered;
  char *control_url;
  char *service_type;
  int port;
  const struct sockaddr *addr;
  socklen_t addrlen;
  unsigned int is_mapped;
  enum UPNP_State state;
  struct sockaddr *ext_addr;
  char *iface;
  int processing;
  GNUNET_NAT_UPNP_pulse_cb pulse_cb;
  void *pulse_cls;
};

static int
process_if (void *cls,
            const char *name,
            int isDefault, const struct sockaddr *addr, socklen_t addrlen)
{
  struct GNUNET_NAT_UPNP_Handle *upnp = cls;
  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "UPNP found if `%s'\n", name);
  if (addr && GNUNET_NAT_cmp_addr (upnp->addr, addr) == 0)
    {
      upnp->iface = GNUNET_strdup(name);       // BADNESS!
      return GNUNET_SYSERR;
    }

  return GNUNET_OK;
}


struct GNUNET_NAT_UPNP_Handle *
GNUNET_NAT_UPNP_init (const struct sockaddr *addr,
                      socklen_t addrlen,
                      u_short port,
                      GNUNET_NAT_UPNP_pulse_cb pulse_cb, void *pulse_cls)
{
  struct GNUNET_NAT_UPNP_Handle *handle;

  handle = GNUNET_malloc (sizeof (struct GNUNET_NAT_UPNP_Handle));
  handle->processing = GNUNET_NO;
  handle->state = UPNP_DISCOVER;
  handle->addr = addr;
  handle->addrlen = addrlen;
  handle->port = port;
  handle->pulse_cb = pulse_cb;
  handle->pulse_cls = pulse_cls;
  handle->control_url = NULL;
  handle->service_type = NULL;

  /* Find the interface corresponding to the address,
   * on which we should broadcast call for routers */
  GNUNET_OS_network_interfaces_list (&process_if, handle);
  if (!handle->iface)
    GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
                     COMP_NAT_UPNP,
                     "Could not find an interface matching the wanted address.\n");
  return handle;
}


void
GNUNET_NAT_UPNP_close (struct GNUNET_NAT_UPNP_Handle *handle)
{
  GNUNET_assert (!handle->is_mapped);
  GNUNET_assert ((handle->state == UPNP_IDLE)
                 || (handle->state == UPNP_ERR)
                 || (handle->state == UPNP_DISCOVER));

  GNUNET_free_non_null (handle->control_url);
  GNUNET_free_non_null (handle->service_type);
  GNUNET_free (handle);
}

static void
pulse_finish (struct GNUNET_NAT_UPNP_Handle *handle)
{
  enum GNUNET_NAT_PortState status;
  handle->processing = GNUNET_NO;

  switch (handle->state)
    {
    case UPNP_DISCOVER:
      status = GNUNET_NAT_PORT_UNMAPPED;
      break;

    case UPNP_MAP:
      status = GNUNET_NAT_PORT_MAPPING;
      break;

    case UPNP_UNMAP:
      status = GNUNET_NAT_PORT_UNMAPPING;
      break;

    case UPNP_IDLE:
      status =
        handle->is_mapped ? GNUNET_NAT_PORT_MAPPED : GNUNET_NAT_PORT_UNMAPPED;
      break;

    default:
      status = GNUNET_NAT_PORT_ERROR;
      break;
    }

  handle->pulse_cb (status, handle->ext_addr, handle->pulse_cls);
}

static void
discover_cb (const char *control_url, const char *service_type, void *cls)
{
  struct GNUNET_NAT_UPNP_Handle *handle = cls;

  if (control_url)
    {
      GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
                       _("Found Internet Gateway Device \"%s\"\n"),
                       control_url);

      GNUNET_free_non_null (handle->control_url);
      GNUNET_free_non_null (handle->service_type);

      handle->control_url = GNUNET_strdup (control_url);
      handle->service_type = GNUNET_strdup (service_type);
      handle->state = UPNP_IDLE;
      handle->hasDiscovered = 1;
    }
  else
    {
      handle->control_url = NULL;
      handle->service_type = NULL;
      handle->state = UPNP_ERR;
#ifdef DEBUG_UPNP
      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_UPNP,
                       "UPNP device discovery failed\n");
#endif
    }

  pulse_finish (handle);
}

static void
check_port_mapping_cb (int error, const char *control_url,
                       const char *service_type, const char *extPort,
                       const char *inPort, const char *proto,
                       const char *remoteHost, void *cls)
{
  struct GNUNET_NAT_UPNP_Handle *handle = cls;

  if (error)
    {
      GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
                       _("Port %d isn't forwarded\n"), handle->port);
      handle->is_mapped = GNUNET_NO;
    }

  pulse_finish (handle);
}

static void
delete_port_mapping_cb (int error, const char *control_url,
                        const char *service_type, const char *extPort,
                        const char *inPort, const char *proto,
                        const char *remoteHost, void *cls)
{
  struct GNUNET_NAT_UPNP_Handle *handle = cls;

  if (error)
    GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
                     _
                     ("Could not stop port forwarding through \"%s\", service \"%s\": error %d\n"),
                     handle->control_url, handle->service_type, error);
  else
    {
      GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
                       _
                       ("Stopped port forwarding through \"%s\", service \"%s\"\n"),
                       handle->control_url, handle->service_type);
      handle->is_mapped = !error;
      handle->state = UPNP_IDLE;
      handle->port = -1;
    }

  pulse_finish (handle);
}

static void
add_port_mapping_cb (int error, const char *control_url,
                     const char *service_type, const char *extPort,
                     const char *inPort, const char *proto,
                     const char *remoteHost, void *cls)
{
  struct GNUNET_NAT_UPNP_Handle *handle = cls;

  if (error)
    {
      handle->is_mapped = GNUNET_NO;
      handle->state = UPNP_ERR;
      GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
                       _
                       ("Port forwarding through \"%s\", service \"%s\" failed with error %d\n"),
                       handle->control_url, handle->service_type, error);
      return;
    }
  else
    {
      handle->is_mapped = GNUNET_NO;
      handle->state = UPNP_IDLE;
      GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
                       _("Port %d forwarded successfully\n"), handle->port);
    }

  pulse_finish (handle);
}

static void
get_ip_address_cb (int error, char *ext_addr, void *cls)
{
  struct GNUNET_NAT_UPNP_Handle *handle = cls;

  if (error)
    {
      if (handle->ext_addr)
        {
          GNUNET_free (handle->ext_addr);
          handle->ext_addr = NULL;
        }
#ifdef DEBUG_UPNP
      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_UPNP,
                       "UPNP_get_external_ip_address_ failed (error %d)\n",
                       error);
#endif
    }
  else
    {
      struct in_addr addr;
      struct in6_addr addr6;

      if (handle->ext_addr)
        {
          GNUNET_free (handle->ext_addr);
          handle->ext_addr = NULL;
        }

      /* Try IPv4 and IPv6 as we don't know what's the format */
#ifndef MINGW
      if (inet_aton (ext_addr, &addr) != 0)
#else
      addr.S_un.S_addr = inet_addr(ext_addr);
      if (addr.S_un.S_addr == INADDR_NONE)
#endif
        {
          handle->ext_addr = GNUNET_malloc (sizeof (struct sockaddr_in));
          handle->ext_addr->sa_family = AF_INET;
          ((struct sockaddr_in *) handle->ext_addr)->sin_addr = addr;
        }
      else if (inet_pton (AF_INET6, ext_addr, &addr6) != 1)
        {
          handle->ext_addr = GNUNET_malloc (sizeof (struct sockaddr_in6));
          handle->ext_addr->sa_family = AF_INET6;
          ((struct sockaddr_in6 *) handle->ext_addr)->sin6_addr = addr6;
        }
      else
        GNUNET_assert (GNUNET_YES);

#ifdef DEBUG_UPNP
      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_UPNP,
                       _("Found public IP address %s\n"), ext_addr);
#endif
    }

  pulse_finish (handle);
}

/**
 * Check state of UPnP NAT: port redirection, external IP address.
 * 
 * 
 * @param handle the handle for UPnP object
 * @param is_enabled whether enable port redirection
 * @param doPortCheck do port check
 */
void
GNUNET_NAT_UPNP_pulse (struct GNUNET_NAT_UPNP_Handle *handle,
                       int is_enabled, int doPortCheck)
{
  /* Stop if we're already waiting for an action to complete */
  if (handle->processing == GNUNET_YES)
    return;

  if (is_enabled && (handle->state == UPNP_DISCOVER))
    {
      handle->processing = GNUNET_YES;
      UPNP_discover_ (handle->iface, handle->addr, discover_cb,
                      handle);
    }

  if (handle->state == UPNP_IDLE)
    {
      if (handle->is_mapped && !is_enabled)
        handle->state = UPNP_UNMAP;
    }

  if (is_enabled && handle->is_mapped && doPortCheck)
    {
      char portStr[8];

      GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port);

      handle->processing = GNUNET_YES;
      UPNP_get_specific_port_mapping_entry_ (handle->control_url,
                                             handle->service_type, portStr,
                                             "TCP", check_port_mapping_cb,
                                             handle);
    }

  if (handle->state == UPNP_UNMAP)
    {
      char portStr[16];
      GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port);

      handle->processing = GNUNET_YES;
      UPNP_delete_port_mapping_ (handle->control_url,
                                 handle->service_type, portStr, "TCP", NULL,
                                 delete_port_mapping_cb, handle);
    }

  if (handle->state == UPNP_IDLE)
    {
      if (is_enabled && !handle->is_mapped)
        handle->state = UPNP_MAP;
    }

  if (handle->state == UPNP_MAP)
    {
      if (!handle->control_url)
        handle->is_mapped = 0;
      else
        {
          char portStr[16];
          char desc[64];
          GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port);
          GNUNET_snprintf (desc, sizeof (desc), "GNUnet at %d", handle->port);

          handle->processing = GNUNET_YES;
          UPNP_add_port_mapping_ (handle->control_url,
                                  handle->service_type,
                                  portStr, portStr, GNUNET_a2s (handle->addr,
                                                                handle->addrlen),
                                  desc, "TCP", NULL, add_port_mapping_cb,
                                  handle);
        }
    }

  if (handle->state != UPNP_DISCOVER)
    {
      handle->processing = GNUNET_YES;
      UPNP_get_external_ip_address_ (handle->control_url,
                                     handle->service_type,
                                     get_ip_address_cb, handle);
    }
}