2 This file is part of GNUnet.
3 (C) 2011 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 nat/nat_mini.c
23 * @brief functions for interaction with miniupnp; tested with miniupnpc 1.5
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_nat_lib.h"
31 #define DEBUG_NAT GNUNET_NO
34 * How long do we give upnpc to create a mapping?
36 #define MAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
39 * How long do we give upnpc to remove a mapping?
41 #define UNMAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
44 * How often do we check for changes in the mapping?
46 #define MAP_REFRESH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
50 * Try to get the external IPv4 address of this peer.
51 * Note: calling this function may block this process
52 * for a few seconds (!).
54 * @param addr address to set
55 * @return GNUNET_OK on success,
56 * GNUNET_NO if the result is questionable,
57 * GNUNET_SYSERR on error
60 GNUNET_NAT_mini_get_external_ipv4 (struct in_addr *addr)
62 struct GNUNET_OS_Process *eip;
63 struct GNUNET_DISK_PipeHandle *opipe;
64 const struct GNUNET_DISK_FileHandle *r;
70 opipe = GNUNET_DISK_pipe (GNUNET_YES,
75 eip = GNUNET_OS_start_process (NULL,
81 GNUNET_DISK_pipe_close (opipe);
84 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
86 r = GNUNET_DISK_pipe_handle (opipe,
87 GNUNET_DISK_PIPE_END_READ);
89 while (0 < (ret = GNUNET_DISK_file_read (r, &buf[off], sizeof (buf)-off)))
92 (buf[off-1] == '\n') )
95 if (1 == inet_pton (AF_INET, buf, addr))
97 if (addr->s_addr == 0)
98 iret = GNUNET_NO; /* got 0.0.0.0 */
102 (void) GNUNET_OS_process_kill (eip, SIGKILL);
103 GNUNET_OS_process_close (eip);
104 GNUNET_DISK_pipe_close (opipe);
110 * Handle to a mapping created with upnpc.
112 struct GNUNET_NAT_MiniHandle
116 * Function to call on mapping changes.
118 GNUNET_NAT_AddressCallback ac;
126 * Command used to install the map.
128 struct GNUNET_OS_CommandHandle *map_cmd;
131 * Command used to refresh our map information.
133 struct GNUNET_OS_CommandHandle *refresh_cmd;
136 * Command used to remove the mapping.
138 struct GNUNET_OS_CommandHandle *unmap_cmd;
141 * Our current external mapping (if we have one).
143 struct sockaddr_in current_addr;
146 * We check the mapping periodically to see if it
147 * still works. This task triggers the check.
149 GNUNET_SCHEDULER_TaskIdentifier refresh_task;
152 * Are we mapping TCP or UDP?
157 * Did we succeed with creating a mapping?
162 * Did we find our mapping during refresh scan?
167 * Which port are we mapping?
175 * Run upnpc -l to find out if our mapping changed.
177 * @param cls the 'struct GNUNET_NAT_MiniHandle'
178 * @param tc scheduler context
181 do_refresh (void *cls,
182 const struct GNUNET_SCHEDULER_TaskContext *tc);
186 * Process the output from the 'upnpc -r' command.
188 * @param cls the 'struct GNUNET_NAT_MiniHandle'
189 * @param line line of output, NULL at the end
192 process_map_output (void *cls,
197 * Process the output from 'upnpc -l' to see if our
198 * external mapping changed. If so, do the notifications.
200 * @param cls the 'struct GNUNET_NAT_MiniHandle'
201 * @param line line of output, NULL at the end
204 process_refresh_output (void *cls,
207 struct GNUNET_NAT_MiniHandle *mini = cls;
215 GNUNET_OS_command_stop (mini->refresh_cmd);
216 mini->refresh_cmd = NULL;
217 if (mini->found == GNUNET_NO)
219 /* mapping disappeared, try to re-create */
222 mini->ac (mini->ac_cls, GNUNET_NO,
223 (const struct sockaddr*) &mini->current_addr,
224 sizeof (mini->current_addr));
225 mini->did_map = GNUNET_NO;
227 GNUNET_snprintf (pstr, sizeof (pstr),
229 (unsigned int) mini->port);
230 mini->map_cmd = GNUNET_OS_command_run (&process_map_output,
236 mini->is_tcp ? "tcp" : "udp",
238 if (NULL != mini->map_cmd)
241 mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
247 return; /* never mapped, won't find our mapping anyway */
249 /* we're looking for output of the form:
250 "ExternalIPAddress = 12.134.41.124" */
252 s = strstr (line, "ExternalIPAddress = ");
255 s += strlen ("ExternalIPAddress = ");
256 if (1 != inet_pton (AF_INET,
259 if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
260 return; /* no change */
262 mini->ac (mini->ac_cls, GNUNET_NO,
263 (const struct sockaddr*) &mini->current_addr,
264 sizeof (mini->current_addr));
265 mini->current_addr.sin_addr = exip;
266 mini->ac (mini->ac_cls, GNUNET_YES,
267 (const struct sockaddr*) &mini->current_addr,
268 sizeof (mini->current_addr));
272 we're looking for output of the form:
274 "0 TCP 3000->192.168.2.150:3000 'libminiupnpc' ''"
275 "1 UDP 3001->192.168.2.150:3001 'libminiupnpc' ''"
277 the pattern we look for is:
279 "%s TCP PORT->STRING:OURPORT *" or
280 "%s UDP PORT->STRING:OURPORT *"
282 GNUNET_snprintf (pstr, sizeof (pstr),
285 if (NULL == (s = strstr (line, "->")))
287 if (NULL == (s = strstr (s, pstr)))
289 if (1 != sscanf (line,
291 ? "%*u TCP %u->%*s:%*u %*s"
292 : "%*u UDP %u->%*s:%*u %*s",
295 mini->found = GNUNET_YES;
296 if (nport == ntohs (mini->current_addr.sin_port))
297 return; /* no change */
299 /* external port changed, update mapping */
300 mini->ac (mini->ac_cls, GNUNET_NO,
301 (const struct sockaddr*) &mini->current_addr,
302 sizeof (mini->current_addr));
303 mini->current_addr.sin_port = htons ((uint16_t) nport);
304 mini->ac (mini->ac_cls, GNUNET_YES,
305 (const struct sockaddr*) &mini->current_addr,
306 sizeof (mini->current_addr));
311 * Run upnpc -l to find out if our mapping changed.
313 * @param cls the 'struct GNUNET_NAT_MiniHandle'
314 * @param tc scheduler context
317 do_refresh (void *cls,
318 const struct GNUNET_SCHEDULER_TaskContext *tc)
320 struct GNUNET_NAT_MiniHandle *mini = cls;
322 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
323 mini->found = GNUNET_NO;
324 mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output,
335 * Process the output from the 'upnpc -r' command.
337 * @param cls the 'struct GNUNET_NAT_MiniHandle'
338 * @param line line of output, NULL at the end
341 process_map_output (void *cls,
344 struct GNUNET_NAT_MiniHandle *mini = cls;
352 GNUNET_OS_command_stop (mini->map_cmd);
353 mini->map_cmd = NULL;
354 mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
360 The upnpc output we're after looks like this:
362 "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
364 if ( (NULL == (ipaddr = strstr (line, " "))) ||
365 (NULL == (pstr = strstr (ipaddr, ":"))) ||
366 (1 != sscanf (pstr + 1, "%u", &port)) )
368 return; /* skip line */
370 ipa = GNUNET_strdup (ipaddr + 1);
371 strstr (ipa, ":")[0] = '\0';
372 if (1 != inet_pton (AF_INET,
374 &mini->current_addr.sin_addr))
377 return; /* skip line */
381 mini->current_addr.sin_port = htons (port);
382 mini->current_addr.sin_family = AF_INET;
383 #if HAVE_SOCKADDR_IN_SIN_LEN
384 mini->current_addr.sin_len = sizeof (struct sockaddr_in);
386 mini->did_map = GNUNET_YES;
387 mini->ac (mini->ac_cls, GNUNET_YES,
388 (const struct sockaddr*) &mini->current_addr,
389 sizeof (mini->current_addr));
394 * Start mapping the given port using (mini)upnpc. This function
395 * should typically not be used directly (it is used within the
396 * general-purpose 'GNUNET_NAT_register' code). However, it can be
397 * used if specifically UPnP-based NAT traversal is to be used or
400 * @param port port to map
401 * @param is_tcp GNUNET_YES to map TCP, GNUNET_NO for UDP
402 * @param ac function to call with mapping result
403 * @param ac_cls closure for 'ac'
404 * @return NULL on error (no 'upnpc' installed)
406 struct GNUNET_NAT_MiniHandle *
407 GNUNET_NAT_mini_map_start (uint16_t port,
409 GNUNET_NAT_AddressCallback ac,
412 struct GNUNET_NAT_MiniHandle *ret;
416 GNUNET_OS_check_helper_binary ("upnpc"))
418 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_MiniHandle));
420 ret->ac_cls = ac_cls;
421 ret->is_tcp = is_tcp;
423 GNUNET_snprintf (pstr, sizeof (pstr),
425 (unsigned int) port);
426 ret->map_cmd = GNUNET_OS_command_run (&process_map_output,
432 is_tcp ? "tcp" : "udp",
434 if (NULL != ret->map_cmd)
436 ret->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
445 * Process output from our 'unmap' command.
447 * @param cls the 'struct GNUNET_NAT_MiniHandle'
448 * @param line line of output, NULL at the end
451 process_unmap_output (void *cls,
454 struct GNUNET_NAT_MiniHandle *mini = cls;
459 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
461 "UPnP unmap done\n");
463 GNUNET_OS_command_stop (mini->unmap_cmd);
464 mini->unmap_cmd = NULL;
468 /* we don't really care about the output... */
473 * Remove a mapping created with (mini)upnpc. Calling
474 * this function will give 'upnpc' 1s to remove tha mapping,
475 * so while this function is non-blocking, a task will be
476 * left with the scheduler for up to 1s past this call.
478 * @param mini the handle
481 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
485 if (GNUNET_SCHEDULER_NO_TASK != mini->refresh_task)
487 GNUNET_SCHEDULER_cancel (mini->refresh_task);
488 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
490 if (mini->refresh_cmd != NULL)
492 GNUNET_OS_command_stop (mini->refresh_cmd);
493 mini->refresh_cmd = NULL;
497 if (mini->map_cmd != NULL)
499 GNUNET_OS_command_stop (mini->map_cmd);
500 mini->map_cmd = NULL;
505 mini->ac (mini->ac_cls, GNUNET_NO,
506 (const struct sockaddr*) &mini->current_addr,
507 sizeof (mini->current_addr));
508 /* Note: oddly enough, deletion uses the external port whereas
509 addition uses the internal port; this rarely matters since they
510 often are the same, but it might... */
511 GNUNET_snprintf (pstr, sizeof (pstr),
513 (unsigned int) ntohs (mini->current_addr.sin_port));
515 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
517 "Unmapping port %u with UPnP\n",
518 ntohs (mini->current_addr.sin_port));
520 mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output,
526 mini->is_tcp ? "tcp" : "udp",
531 /* end of nat_mini.c */