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"
32 * How long do we give upnpc to create a mapping?
34 #define MAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
37 * How long do we give upnpc to remove a mapping?
39 #define UNMAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
42 * How often do we check for changes in the mapping?
44 #define MAP_REFRESH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
48 * Try to get the external IPv4 address of this peer.
49 * Note: calling this function may block this process
50 * for a few seconds (!).
52 * @param addr address to set
53 * @return GNUNET_OK on success,
54 * GNUNET_NO if the result is questionable,
55 * GNUNET_SYSERR on error
58 GNUNET_NAT_mini_get_external_ipv4 (struct in_addr *addr)
60 struct GNUNET_OS_Process *eip;
61 struct GNUNET_DISK_PipeHandle *opipe;
62 const struct GNUNET_DISK_FileHandle *r;
68 opipe = GNUNET_DISK_pipe (GNUNET_YES,
73 eip = GNUNET_OS_start_process (NULL,
79 GNUNET_DISK_pipe_close (opipe);
82 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
84 r = GNUNET_DISK_pipe_handle (opipe,
85 GNUNET_DISK_PIPE_END_READ);
87 while (0 < (ret = GNUNET_DISK_file_read (r, &buf[off], sizeof (buf)-off)))
90 (buf[off-1] == '\n') )
93 if (1 == inet_pton (AF_INET, buf, addr))
95 if (addr->s_addr == 0)
96 iret = GNUNET_NO; /* got 0.0.0.0 */
100 (void) GNUNET_OS_process_kill (eip, SIGKILL);
101 GNUNET_OS_process_close (eip);
102 GNUNET_DISK_pipe_close (opipe);
108 * Handle to a mapping created with upnpc.
110 struct GNUNET_NAT_MiniHandle
114 * Function to call on mapping changes.
116 GNUNET_NAT_AddressCallback ac;
124 * Command used to install the map.
126 struct GNUNET_OS_CommandHandle *map_cmd;
129 * Command used to refresh our map information.
131 struct GNUNET_OS_CommandHandle *refresh_cmd;
134 * Command used to remove the mapping.
136 struct GNUNET_OS_CommandHandle *unmap_cmd;
139 * Our current external mapping (if we have one).
141 struct sockaddr_in current_addr;
144 * We check the mapping periodically to see if it
145 * still works. This task triggers the check.
147 GNUNET_SCHEDULER_TaskIdentifier refresh_task;
150 * Are we mapping TCP or UDP?
155 * Did we succeed with creating a mapping?
160 * Did we find our mapping during refresh scan?
165 * Which port are we mapping?
173 * Run upnpc -l to find out if our mapping changed.
175 * @param cls the 'struct GNUNET_NAT_MiniHandle'
176 * @param tc scheduler context
179 do_refresh (void *cls,
180 const struct GNUNET_SCHEDULER_TaskContext *tc);
184 * Process the output from the 'upnpc -r' command.
186 * @param cls the 'struct GNUNET_NAT_MiniHandle'
187 * @param line line of output, NULL at the end
190 process_map_output (void *cls,
195 * Process the output from 'upnpc -l' to see if our
196 * external mapping changed. If so, do the notifications.
198 * @param cls the 'struct GNUNET_NAT_MiniHandle'
199 * @param line line of output, NULL at the end
202 process_refresh_output (void *cls,
205 struct GNUNET_NAT_MiniHandle *mini = cls;
213 GNUNET_OS_command_stop (mini->refresh_cmd);
214 mini->refresh_cmd = NULL;
215 if (mini->found == GNUNET_NO)
217 /* mapping disappeared, try to re-create */
220 mini->ac (mini->ac_cls, GNUNET_NO,
221 (const struct sockaddr*) &mini->current_addr,
222 sizeof (mini->current_addr));
223 mini->did_map = GNUNET_NO;
225 GNUNET_snprintf (pstr, sizeof (pstr),
227 (unsigned int) mini->port);
228 mini->map_cmd = GNUNET_OS_command_run (&process_map_output,
234 mini->is_tcp ? "tcp" : "udp",
236 if (NULL != mini->map_cmd)
239 mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
245 return; /* never mapped, won't find our mapping anyway */
247 /* we're looking for output of the form:
248 "ExternalIPAddress = 12.134.41.124" */
250 s = strstr (line, "ExternalIPAddress = ");
253 s += strlen ("ExternalIPAddress = ");
254 if (1 != inet_pton (AF_INET,
257 if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
258 return; /* no change */
260 mini->ac (mini->ac_cls, GNUNET_NO,
261 (const struct sockaddr*) &mini->current_addr,
262 sizeof (mini->current_addr));
263 mini->current_addr.sin_addr = exip;
264 mini->ac (mini->ac_cls, GNUNET_YES,
265 (const struct sockaddr*) &mini->current_addr,
266 sizeof (mini->current_addr));
270 we're looking for output of the form:
272 "0 TCP 3000->192.168.2.150:3000 'libminiupnpc' ''"
273 "1 UDP 3001->192.168.2.150:3001 'libminiupnpc' ''"
275 the pattern we look for is:
277 "%s TCP PORT->STRING:OURPORT *" or
278 "%s UDP PORT->STRING:OURPORT *"
280 GNUNET_snprintf (pstr, sizeof (pstr),
283 if (NULL == (s = strstr (line, "->")))
285 if (NULL == (s = strstr (s, pstr)))
287 if (1 != sscanf (line,
289 ? "%*u TCP %u->%*s:%*u %*s"
290 : "%*u UDP %u->%*s:%*u %*s",
293 mini->found = GNUNET_YES;
294 if (nport == ntohs (mini->current_addr.sin_port))
295 return; /* no change */
297 /* external port changed, update mapping */
298 mini->ac (mini->ac_cls, GNUNET_NO,
299 (const struct sockaddr*) &mini->current_addr,
300 sizeof (mini->current_addr));
301 mini->current_addr.sin_port = htons ((uint16_t) nport);
302 mini->ac (mini->ac_cls, GNUNET_YES,
303 (const struct sockaddr*) &mini->current_addr,
304 sizeof (mini->current_addr));
309 * Run upnpc -l to find out if our mapping changed.
311 * @param cls the 'struct GNUNET_NAT_MiniHandle'
312 * @param tc scheduler context
315 do_refresh (void *cls,
316 const struct GNUNET_SCHEDULER_TaskContext *tc)
318 struct GNUNET_NAT_MiniHandle *mini = cls;
320 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
321 mini->found = GNUNET_NO;
322 mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output,
333 * Process the output from the 'upnpc -r' command.
335 * @param cls the 'struct GNUNET_NAT_MiniHandle'
336 * @param line line of output, NULL at the end
339 process_map_output (void *cls,
342 struct GNUNET_NAT_MiniHandle *mini = cls;
350 GNUNET_OS_command_stop (mini->map_cmd);
351 mini->map_cmd = NULL;
352 mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
358 The upnpc output we're after looks like this:
360 "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
362 if ( (NULL == (ipaddr = strstr (line, " "))) ||
363 (NULL == (pstr = strstr (ipaddr, ":"))) ||
364 (1 != sscanf (pstr + 1, "%u", &port)) )
366 return; /* skip line */
368 ipa = GNUNET_strdup (ipaddr + 1);
369 strstr (ipa, ":")[0] = '\0';
370 if (1 != inet_pton (AF_INET,
372 &mini->current_addr.sin_addr))
375 return; /* skip line */
379 mini->current_addr.sin_port = htons (port);
380 mini->current_addr.sin_family = AF_INET;
381 #if HAVE_SOCKADDR_IN_SIN_LEN
382 mini->current_addr.sin_len = sizeof (struct sockaddr_in);
384 mini->did_map = GNUNET_YES;
385 mini->ac (mini->ac_cls, GNUNET_YES,
386 (const struct sockaddr*) &mini->current_addr,
387 sizeof (mini->current_addr));
392 * Start mapping the given port using (mini)upnpc. This function
393 * should typically not be used directly (it is used within the
394 * general-purpose 'GNUNET_NAT_register' code). However, it can be
395 * used if specifically UPnP-based NAT traversal is to be used or
398 * @param port port to map
399 * @param is_tcp GNUNET_YES to map TCP, GNUNET_NO for UDP
400 * @param ac function to call with mapping result
401 * @param ac_cls closure for 'ac'
402 * @return NULL on error
404 struct GNUNET_NAT_MiniHandle *
405 GNUNET_NAT_mini_map_start (uint16_t port,
407 GNUNET_NAT_AddressCallback ac,
410 struct GNUNET_NAT_MiniHandle *ret;
413 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_MiniHandle));
415 ret->ac_cls = ac_cls;
416 ret->is_tcp = is_tcp;
418 GNUNET_snprintf (pstr, sizeof (pstr),
420 (unsigned int) port);
421 ret->map_cmd = GNUNET_OS_command_run (&process_map_output,
427 is_tcp ? "tcp" : "udp",
429 if (NULL != ret->map_cmd)
431 ret->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
440 * Process output from our 'unmap' command.
442 * @param cls the 'struct GNUNET_NAT_MiniHandle'
443 * @param line line of output, NULL at the end
446 process_unmap_output (void *cls,
449 struct GNUNET_NAT_MiniHandle *mini = cls;
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
454 "UPnP unmap done\n");
455 GNUNET_OS_command_stop (mini->unmap_cmd);
456 mini->unmap_cmd = NULL;
460 /* we don't really care about the output... */
465 * Remove a mapping created with (mini)upnpc. Calling
466 * this function will give 'upnpc' 1s to remove tha mapping,
467 * so while this function is non-blocking, a task will be
468 * left with the scheduler for up to 1s past this call.
470 * @param mini the handle
473 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
479 if (mini->map_cmd != NULL)
481 GNUNET_OS_command_stop (mini->map_cmd);
482 mini->map_cmd = NULL;
487 if (GNUNET_SCHEDULER_NO_TASK != mini->refresh_task)
489 GNUNET_SCHEDULER_cancel (mini->refresh_task);
490 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
492 if (mini->refresh_cmd != NULL)
494 GNUNET_OS_command_stop (mini->refresh_cmd);
495 mini->refresh_cmd = NULL;
497 mini->ac (mini->ac_cls, GNUNET_NO,
498 (const struct sockaddr*) &mini->current_addr,
499 sizeof (mini->current_addr));
500 /* Note: oddly enough, deletion uses the external port whereas
501 addition uses the internal port; this rarely matters since they
502 often are the same, but it might... */
503 GNUNET_snprintf (pstr, sizeof (pstr),
505 (unsigned int) ntohs (mini->current_addr.sin_port));
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Unmapping port %u\n",
508 ntohs (mini->current_addr.sin_port));
509 mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output,
515 mini->is_tcp ? "tcp" : "udp",
520 /* end of nat_mini.c */