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)
51 * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation.
53 struct GNUNET_NAT_ExternalHandle
57 * Function to call with the result.
59 GNUNET_NAT_IPCallback cb;
69 GNUNET_SCHEDULER_TaskIdentifier task;
72 * Handle to 'external-ip' process.
74 struct GNUNET_OS_Process *eip;
77 * Handle to stdout pipe of 'external-ip'.
79 struct GNUNET_DISK_PipeHandle *opipe;
82 * Read handle of 'opipe'.
84 const struct GNUNET_DISK_FileHandle *r;
87 * When should this operation time out?
89 struct GNUNET_TIME_Absolute timeout;
92 * Number of bytes in 'buf' that are valid.
97 * Destination of our read operation (output of 'external-ip').
105 * Read the output of 'external-ip' into buf. When complete, parse the
106 * address and call our callback.
108 * @param cls the 'struct GNUNET_NAT_ExternalHandle'
109 * @param tc scheduler context
112 read_external_ipv4 (void *cls,
113 const struct GNUNET_SCHEDULER_TaskContext *tc)
115 struct GNUNET_NAT_ExternalHandle *eh = cls;
120 eh->task = GNUNET_SCHEDULER_NO_TASK;
122 GNUNET_NETWORK_fdset_handle_isset (tc->read_ready,
124 ret = GNUNET_DISK_file_read (eh->r,
126 sizeof (eh->buf)-eh->off);
128 ret = -1; /* error reading, timeout, etc. */
131 /* try to read more */
133 eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining (eh->timeout),
140 if ( (eh->off > 7) &&
141 (eh->buf[eh->off-1] == '\n') )
143 eh->buf[eh->off-1] = '\0';
144 if (1 == inet_pton (AF_INET, eh->buf, &addr))
146 if (addr.s_addr == 0)
147 iret = GNUNET_NO; /* got 0.0.0.0 */
153 (iret == GNUNET_OK) ? &addr : NULL);
154 GNUNET_NAT_mini_get_external_ipv4_cancel (eh);
159 * Try to get the external IPv4 address of this peer.
161 * @param timeout when to fail
162 * @param cb function to call with result
163 * @param cb_cls closure for 'cb'
164 * @return handle for cancellation (can only be used until 'cb' is called), NULL on error
166 struct GNUNET_NAT_ExternalHandle *
167 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
168 GNUNET_NAT_IPCallback cb,
171 struct GNUNET_NAT_ExternalHandle *eh;
173 eh = GNUNET_malloc (sizeof (struct GNUNET_NAT_ExternalHandle));
176 eh->opipe = GNUNET_DISK_pipe (GNUNET_YES,
179 if (NULL == eh->opipe)
184 eh->eip = GNUNET_OS_start_process (NULL,
187 "external-ip", NULL);
190 GNUNET_DISK_pipe_close (eh->opipe);
194 GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE);
195 eh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
196 eh->r = GNUNET_DISK_pipe_handle (eh->opipe,
197 GNUNET_DISK_PIPE_END_READ);
198 eh->task = GNUNET_SCHEDULER_add_read_file (timeout,
209 * @param eh operation to cancel
212 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh)
214 (void) GNUNET_OS_process_kill (eh->eip, SIGKILL);
215 GNUNET_OS_process_close (eh->eip);
216 GNUNET_DISK_pipe_close (eh->opipe);
217 if (GNUNET_SCHEDULER_NO_TASK != eh->task)
218 GNUNET_SCHEDULER_cancel (eh->task);
224 * Handle to a mapping created with upnpc.
226 struct GNUNET_NAT_MiniHandle
230 * Function to call on mapping changes.
232 GNUNET_NAT_AddressCallback ac;
240 * Command used to install the map.
242 struct GNUNET_OS_CommandHandle *map_cmd;
245 * Command used to refresh our map information.
247 struct GNUNET_OS_CommandHandle *refresh_cmd;
250 * Command used to remove the mapping.
252 struct GNUNET_OS_CommandHandle *unmap_cmd;
255 * Our current external mapping (if we have one).
257 struct sockaddr_in current_addr;
260 * We check the mapping periodically to see if it
261 * still works. This task triggers the check.
263 GNUNET_SCHEDULER_TaskIdentifier refresh_task;
266 * Are we mapping TCP or UDP?
271 * Did we succeed with creating a mapping?
276 * Did we find our mapping during refresh scan?
281 * Which port are we mapping?
289 * Run upnpc -l to find out if our mapping changed.
291 * @param cls the 'struct GNUNET_NAT_MiniHandle'
292 * @param tc scheduler context
295 do_refresh (void *cls,
296 const struct GNUNET_SCHEDULER_TaskContext *tc);
300 * Process the output from the 'upnpc -r' command.
302 * @param cls the 'struct GNUNET_NAT_MiniHandle'
303 * @param line line of output, NULL at the end
306 process_map_output (void *cls,
311 * Process the output from 'upnpc -l' to see if our
312 * external mapping changed. If so, do the notifications.
314 * @param cls the 'struct GNUNET_NAT_MiniHandle'
315 * @param line line of output, NULL at the end
318 process_refresh_output (void *cls,
321 struct GNUNET_NAT_MiniHandle *mini = cls;
329 GNUNET_OS_command_stop (mini->refresh_cmd);
330 mini->refresh_cmd = NULL;
331 if (mini->found == GNUNET_NO)
333 /* mapping disappeared, try to re-create */
336 mini->ac (mini->ac_cls, GNUNET_NO,
337 (const struct sockaddr*) &mini->current_addr,
338 sizeof (mini->current_addr));
339 mini->did_map = GNUNET_NO;
341 GNUNET_snprintf (pstr, sizeof (pstr),
343 (unsigned int) mini->port);
344 mini->map_cmd = GNUNET_OS_command_run (&process_map_output,
350 mini->is_tcp ? "tcp" : "udp",
352 if (NULL != mini->map_cmd)
355 mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
361 return; /* never mapped, won't find our mapping anyway */
363 /* we're looking for output of the form:
364 "ExternalIPAddress = 12.134.41.124" */
366 s = strstr (line, "ExternalIPAddress = ");
369 s += strlen ("ExternalIPAddress = ");
370 if (1 != inet_pton (AF_INET,
373 if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
374 return; /* no change */
376 mini->ac (mini->ac_cls, GNUNET_NO,
377 (const struct sockaddr*) &mini->current_addr,
378 sizeof (mini->current_addr));
379 mini->current_addr.sin_addr = exip;
380 mini->ac (mini->ac_cls, GNUNET_YES,
381 (const struct sockaddr*) &mini->current_addr,
382 sizeof (mini->current_addr));
386 we're looking for output of the form:
388 "0 TCP 3000->192.168.2.150:3000 'libminiupnpc' ''"
389 "1 UDP 3001->192.168.2.150:3001 'libminiupnpc' ''"
391 the pattern we look for is:
393 "%s TCP PORT->STRING:OURPORT *" or
394 "%s UDP PORT->STRING:OURPORT *"
396 GNUNET_snprintf (pstr, sizeof (pstr),
399 if (NULL == (s = strstr (line, "->")))
401 if (NULL == strstr (s, pstr))
403 if (1 != sscanf (line,
405 ? "%*u TCP %u->%*s:%*u %*s"
406 : "%*u UDP %u->%*s:%*u %*s",
409 mini->found = GNUNET_YES;
410 if (nport == ntohs (mini->current_addr.sin_port))
411 return; /* no change */
413 /* external port changed, update mapping */
414 mini->ac (mini->ac_cls, GNUNET_NO,
415 (const struct sockaddr*) &mini->current_addr,
416 sizeof (mini->current_addr));
417 mini->current_addr.sin_port = htons ((uint16_t) nport);
418 mini->ac (mini->ac_cls, GNUNET_YES,
419 (const struct sockaddr*) &mini->current_addr,
420 sizeof (mini->current_addr));
425 * Run upnpc -l to find out if our mapping changed.
427 * @param cls the 'struct GNUNET_NAT_MiniHandle'
428 * @param tc scheduler context
431 do_refresh (void *cls,
432 const struct GNUNET_SCHEDULER_TaskContext *tc)
434 struct GNUNET_NAT_MiniHandle *mini = cls;
436 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
437 mini->found = GNUNET_NO;
438 mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output,
449 * Process the output from the 'upnpc -r' command.
451 * @param cls the 'struct GNUNET_NAT_MiniHandle'
452 * @param line line of output, NULL at the end
455 process_map_output (void *cls,
458 struct GNUNET_NAT_MiniHandle *mini = cls;
466 GNUNET_OS_command_stop (mini->map_cmd);
467 mini->map_cmd = NULL;
468 mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
474 The upnpc output we're after looks like this:
476 "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
478 if ( (NULL == (ipaddr = strstr (line, " "))) ||
479 (NULL == (pstr = strstr (ipaddr, ":"))) ||
480 (1 != sscanf (pstr + 1, "%u", &port)) )
482 return; /* skip line */
484 ipa = GNUNET_strdup (ipaddr + 1);
485 strstr (ipa, ":")[0] = '\0';
486 if (1 != inet_pton (AF_INET,
488 &mini->current_addr.sin_addr))
491 return; /* skip line */
495 mini->current_addr.sin_port = htons (port);
496 mini->current_addr.sin_family = AF_INET;
497 #if HAVE_SOCKADDR_IN_SIN_LEN
498 mini->current_addr.sin_len = sizeof (struct sockaddr_in);
500 mini->did_map = GNUNET_YES;
501 mini->ac (mini->ac_cls, GNUNET_YES,
502 (const struct sockaddr*) &mini->current_addr,
503 sizeof (mini->current_addr));
508 * Start mapping the given port using (mini)upnpc. This function
509 * should typically not be used directly (it is used within the
510 * general-purpose 'GNUNET_NAT_register' code). However, it can be
511 * used if specifically UPnP-based NAT traversal is to be used or
514 * @param port port to map
515 * @param is_tcp GNUNET_YES to map TCP, GNUNET_NO for UDP
516 * @param ac function to call with mapping result
517 * @param ac_cls closure for 'ac'
518 * @return NULL on error (no 'upnpc' installed)
520 struct GNUNET_NAT_MiniHandle *
521 GNUNET_NAT_mini_map_start (uint16_t port,
523 GNUNET_NAT_AddressCallback ac,
526 struct GNUNET_NAT_MiniHandle *ret;
530 GNUNET_OS_check_helper_binary ("upnpc"))
532 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_MiniHandle));
534 ret->ac_cls = ac_cls;
535 ret->is_tcp = is_tcp;
537 GNUNET_snprintf (pstr, sizeof (pstr),
539 (unsigned int) port);
540 ret->map_cmd = GNUNET_OS_command_run (&process_map_output,
546 is_tcp ? "tcp" : "udp",
548 if (NULL != ret->map_cmd)
550 ret->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
559 * Process output from our 'unmap' command.
561 * @param cls the 'struct GNUNET_NAT_MiniHandle'
562 * @param line line of output, NULL at the end
565 process_unmap_output (void *cls,
568 struct GNUNET_NAT_MiniHandle *mini = cls;
573 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
575 "UPnP unmap done\n");
577 GNUNET_OS_command_stop (mini->unmap_cmd);
578 mini->unmap_cmd = NULL;
582 /* we don't really care about the output... */
587 * Remove a mapping created with (mini)upnpc. Calling
588 * this function will give 'upnpc' 1s to remove tha mapping,
589 * so while this function is non-blocking, a task will be
590 * left with the scheduler for up to 1s past this call.
592 * @param mini the handle
595 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
599 if (GNUNET_SCHEDULER_NO_TASK != mini->refresh_task)
601 GNUNET_SCHEDULER_cancel (mini->refresh_task);
602 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
604 if (mini->refresh_cmd != NULL)
606 GNUNET_OS_command_stop (mini->refresh_cmd);
607 mini->refresh_cmd = NULL;
611 if (mini->map_cmd != NULL)
613 GNUNET_OS_command_stop (mini->map_cmd);
614 mini->map_cmd = NULL;
619 mini->ac (mini->ac_cls, GNUNET_NO,
620 (const struct sockaddr*) &mini->current_addr,
621 sizeof (mini->current_addr));
622 /* Note: oddly enough, deletion uses the external port whereas
623 addition uses the internal port; this rarely matters since they
624 often are the same, but it might... */
625 GNUNET_snprintf (pstr, sizeof (pstr),
627 (unsigned int) ntohs (mini->current_addr.sin_port));
629 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
631 "Unmapping port %u with UPnP\n",
632 ntohs (mini->current_addr.sin_port));
634 mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output,
640 mini->is_tcp ? "tcp" : "udp",
645 /* end of nat_mini.c */