summaryrefslogtreecommitdiff
path: root/src/nat/nat_mini.c
blob: 8f13d298fa8846178f33e15fec3326b71ae7047f (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
423
424
425
426
/*
     This file is part of GNUnet.
     (C) 2011 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.
*/

/**
 * @file nat/nat_mini.c
 * @brief functions for interaction with miniupnp
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_nat_lib.h"
#include "nat.h"

/**
 * How long do we give upnpc to create a mapping?
 */
#define MAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)


/**
 * How long do we give upnpc to remove a mapping?
 */
#define UNMAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)

/**
 * How often do we check for changes in the mapping?
 */
#define MAP_REFRESH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)


/**
 * Try to get the external IPv4 address of this peer.
 * Note: calling this function may block this process
 * for a few seconds (!).
 *
 * @param addr address to set
 * @return GNUNET_OK on success,
 *         GNUNET_NO if the result is questionable,
 *         GNUNET_SYSERR on error
 */
int
GNUNET_NAT_mini_get_external_ipv4 (struct in_addr *addr)
{
  struct GNUNET_OS_Process *eip;
  struct GNUNET_DISK_PipeHandle *opipe;
  const struct GNUNET_DISK_FileHandle *r;
  size_t off;
  char buf[17];
  ssize_t ret;
  int iret;

  opipe = GNUNET_DISK_pipe (GNUNET_YES,
			    GNUNET_NO,
			    GNUNET_YES);
  if (NULL == opipe)
    return GNUNET_SYSERR;
  eip = GNUNET_OS_start_process (NULL,
				 opipe,
				 "external-ip",
				 "external-ip", NULL);
  if (NULL == eip)
    {
      GNUNET_DISK_pipe_close (opipe);
      return GNUNET_SYSERR;
    }
  GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
  iret = GNUNET_SYSERR;
  r = GNUNET_DISK_pipe_handle (opipe,
			       GNUNET_DISK_PIPE_END_READ);
  off = 0;
  while (0 < (ret = GNUNET_DISK_file_read (r, &buf[off], sizeof (buf)-off)))
    off += ret;
  if ( (off > 7) &&    
       (buf[off-1] == '\n') )    
    {
      buf[off-1] = '\0';
      if (1 == inet_pton (AF_INET, buf, addr))
	{
	  if (addr->s_addr == 0)
	    iret = GNUNET_NO; /* got 0.0.0.0 */
	  iret = GNUNET_OK;
	}
    }
  (void) GNUNET_OS_process_kill (eip, SIGKILL);
  GNUNET_OS_process_close (eip);
  GNUNET_DISK_pipe_close (opipe);
  return iret; 
}


/**
 * Handle to a mapping created with upnpc.
 */ 
struct GNUNET_NAT_MiniHandle
{

  /**
   * Function to call on mapping changes.
   */
  GNUNET_NAT_AddressCallback ac;

  /**
   * Closure for 'ac'.
   */
  void *ac_cls;

  /**
   * Command used to install the map.
   */
  struct GNUNET_OS_CommandHandle *map_cmd;

  /**
   * Command used to refresh our map information.
   */
  struct GNUNET_OS_CommandHandle *refresh_cmd;

  /**
   * Command used to remove the mapping.
   */
  struct GNUNET_OS_CommandHandle *unmap_cmd;

  /**
   * Our current external mapping (if we have one).
   */
  struct sockaddr_in current_addr;

  /**
   * We check the mapping periodically to see if it
   * still works.  This task triggers the check.
   */
  GNUNET_SCHEDULER_TaskIdentifier refresh_task;

  /**
   * Are we mapping TCP or UDP?
   */
  int is_tcp;

  /**
   * Did we succeed with creating a mapping?
   */
  int did_map;

  /**
   * Which port are we mapping?
   */
  uint16_t port;

};


/**
 * Run upnpc -l to find out if our mapping changed.
 *
 * @param cls the 'struct GNUNET_NAT_MiniHandle'
 * @param tc scheduler context
 */
static void
do_refresh (void *cls,
	    const struct GNUNET_SCHEDULER_TaskContext *tc);


/**
 * Process the output from 'upnpc -l' to see if our
 * external mapping changed.  If so, do the notifications.
 *
 * @param cls the 'struct GNUNET_NAT_MiniHandle'
 * @param line line of output, NULL at the end
 */
static void
process_refresh_output (void *cls,
			const char *line)
{
  struct GNUNET_NAT_MiniHandle *mini = cls;
  enum GNUNET_OS_ProcessStatusType type;
  unsigned long code;

  if (NULL == line)
    {
      GNUNET_OS_command_stop (mini->refresh_cmd,
			      &type, &code);
      mini->refresh_cmd = NULL;
      mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
							 &do_refresh,
							 mini);
      return;
    }
  /* FIXME: parse 'line' */
  fprintf (stderr,
	   "Refresh output: `%s'\n",
	   line);
}


/**
 * Run upnpc -l to find out if our mapping changed.
 *
 * @param cls the 'struct GNUNET_NAT_MiniHandle'
 * @param tc scheduler context
 */
static void
do_refresh (void *cls,
	    const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_NAT_MiniHandle *mini = cls;

  mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
  mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output,
					     mini,
					     MAP_TIMEOUT,
					     "upnpc",
					     "upnpc",
					     "-l",
					     NULL);
}


/**
 * Process the output from the 'upnpc -r' command.
 *
 * @param cls the 'struct GNUNET_NAT_MiniHandle'
 * @param line line of output, NULL at the end
 */
static void
process_map_output (void *cls,
		    const char *line)
{
  struct GNUNET_NAT_MiniHandle *mini = cls;
  enum GNUNET_OS_ProcessStatusType type;
  unsigned long code;
  const char *ipaddr;
  char *ipa;
  const char *pstr;
  unsigned int port;

  if (NULL == line)
    {
      GNUNET_OS_command_stop (mini->map_cmd,
			      &type, &code);
      mini->map_cmd = NULL;
      if (mini->did_map == GNUNET_YES)
	mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
							   &do_refresh,
							   mini);
      return;
    }
  /*
    The upnpc output we're after looks like this:

     "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
  */
  if ( (NULL == (ipaddr = strstr (line, " "))) ||
       (NULL == (pstr = strstr (ipaddr, ":"))) ||
       (1 != sscanf (pstr + 1, "%u", &port)) )
    {
      fprintf (stderr,
	       "Skipping output `%s'\n",
	       line);
      return; /* skip line */
    }
  ipa = GNUNET_strdup (ipaddr + 1);
  strstr (ipa, ":")[0] = '\0';
  if (1 != inet_pton (AF_INET,
		      ipa, 
		      &mini->current_addr.sin_addr))
    {
      GNUNET_free (ipa);
      fprintf (stderr,
	       "Skipping output `%s'\n",
	       line);
      return; /* skip line */
    }
  GNUNET_free (ipa);	      

  mini->current_addr.sin_port = htons (port);
  mini->current_addr.sin_family = AF_INET;
#if HAVE_SOCKADDR_IN_SIN_LEN
  mini->current_addr.sin_len = sizeof (struct sockaddr_in);
#endif
  mini->did_map = GNUNET_YES;
  mini->ac (mini->ac_cls, GNUNET_YES,
	    (const struct sockaddr*) &mini->current_addr,
	    sizeof (mini->current_addr));
}


/**
 * Start mapping the given port using (mini)upnpc.  This function
 * should typically not be used directly (it is used within the
 * general-purpose 'GNUNET_NAT_register' code).  However, it can be
 * used if specifically UPnP-based NAT traversal is to be used or
 * tested.
 * 
 * @param port port to map
 * @param is_tcp GNUNET_YES to map TCP, GNUNET_NO for UDP
 * @param ac function to call with mapping result
 * @param ac_cls closure for 'ac'
 * @return NULL on error
 */
struct GNUNET_NAT_MiniHandle *
GNUNET_NAT_mini_map_start (uint16_t port,
			   int is_tcp,
			   GNUNET_NAT_AddressCallback ac,
			   void *ac_cls)
{
  struct GNUNET_NAT_MiniHandle *ret;
  char pstr[6];

  ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_MiniHandle));
  ret->ac = ac;
  ret->ac_cls = ac_cls;
  ret->is_tcp = is_tcp;
  ret->port = port;
  GNUNET_snprintf (pstr, sizeof (pstr),
		   "%u",
		   (unsigned int) port);
  ret->map_cmd = GNUNET_OS_command_run (&process_map_output,
					ret,
					MAP_TIMEOUT,
					"upnpc",
					"upnpc",
					"-r", pstr, 
					is_tcp ? "tcp" : "udp",
					NULL);
  
  return ret;
}


/**
 * Process output from our 'unmap' command.
 *
 * @param cls the 'struct GNUNET_NAT_MiniHandle'
 * @param line line of output, NULL at the end
 */
static void
process_unmap_output (void *cls,
		      const char *line)
{
  struct GNUNET_NAT_MiniHandle *mini = cls;
  enum GNUNET_OS_ProcessStatusType type;
  unsigned long code;

  if (NULL == line)
    {
      GNUNET_OS_command_stop (mini->unmap_cmd,
			      &type, &code);
      mini->unmap_cmd = NULL;
      GNUNET_free (mini);
      return;
    }
  /* we don't really care about the output... */
}


/**
 * Remove a mapping created with (mini)upnpc.  Calling
 * this function will give 'upnpc' 1s to remove tha mapping,
 * so while this function is non-blocking, a task will be
 * left with the scheduler for up to 1s past this call.
 * 
 * @param mini the handle
 */
void
GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
{
  char pstr[6];
  enum GNUNET_OS_ProcessStatusType type;
  unsigned long code;

  if (! mini->did_map)
    {
      if (mini->map_cmd != NULL)
	{
	  GNUNET_OS_command_stop (mini->map_cmd, 
				  &type, &code);
	  mini->map_cmd = NULL;
	}
      GNUNET_free (mini);
      return;
    }
  if (GNUNET_SCHEDULER_NO_TASK != mini->refresh_task)
    {
      GNUNET_SCHEDULER_cancel (mini->refresh_task);
      mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
    }
  if (mini->refresh_cmd != NULL)
    {
      GNUNET_OS_command_stop (mini->refresh_cmd,
			      &type, &code);
      mini->refresh_cmd = NULL;
    }
  mini->ac (mini->ac_cls, GNUNET_NO,
	    (const struct sockaddr*) &mini->current_addr,
	    sizeof (mini->current_addr));
  GNUNET_snprintf (pstr, sizeof (pstr),
		   "%u",
		   (unsigned int) mini->port);
  mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output,
					   mini,
					   UNMAP_TIMEOUT,
					   "upnpc",
					   "upnpc",
					   "-d", pstr, 
					   mini->is_tcp ? "tcp" : "udp",
					   NULL);
}


/* end of nat_mini.c */