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"
33 * How long do we give upnpc to create a mapping?
35 #define MAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
38 * How long do we give upnpc to remove a mapping?
40 #define UNMAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
43 * How often do we check for changes in the mapping?
45 #define MAP_REFRESH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
50 * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation.
52 struct GNUNET_NAT_ExternalHandle
56 * Function to call with the result.
58 GNUNET_NAT_IPCallback cb;
68 GNUNET_SCHEDULER_TaskIdentifier task;
71 * Handle to 'external-ip' process.
73 struct GNUNET_OS_Process *eip;
76 * Handle to stdout pipe of 'external-ip'.
78 struct GNUNET_DISK_PipeHandle *opipe;
81 * Read handle of 'opipe'.
83 const struct GNUNET_DISK_FileHandle *r;
86 * When should this operation time out?
88 struct GNUNET_TIME_Absolute timeout;
91 * Number of bytes in 'buf' that are valid.
96 * Destination of our read operation (output of 'external-ip').
104 * Read the output of 'external-ip' into buf. When complete, parse the
105 * address and call our callback.
107 * @param cls the 'struct GNUNET_NAT_ExternalHandle'
108 * @param tc scheduler context
111 read_external_ipv4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 struct GNUNET_NAT_ExternalHandle *eh = cls;
118 eh->task = GNUNET_SCHEDULER_NO_TASK;
119 if (GNUNET_YES == GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, eh->r))
121 GNUNET_DISK_file_read (eh->r, &eh->buf[eh->off],
122 sizeof (eh->buf) - eh->off);
124 ret = -1; /* error reading, timeout, etc. */
127 /* try to read more */
130 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
131 (eh->timeout), eh->r,
132 &read_external_ipv4, eh);
136 if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n'))
138 eh->buf[eh->off - 1] = '\0';
139 if (1 == inet_pton (AF_INET, eh->buf, &addr))
141 if (addr.s_addr == 0)
142 iret = GNUNET_NO; /* got 0.0.0.0 */
147 eh->cb (eh->cb_cls, (iret == GNUNET_OK) ? &addr : NULL);
148 GNUNET_NAT_mini_get_external_ipv4_cancel (eh);
153 * Try to get the external IPv4 address of this peer.
155 * @param timeout when to fail
156 * @param cb function to call with result
157 * @param cb_cls closure for 'cb'
158 * @return handle for cancellation (can only be used until 'cb' is called), NULL on error
160 struct GNUNET_NAT_ExternalHandle *
161 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
162 GNUNET_NAT_IPCallback cb, void *cb_cls)
164 struct GNUNET_NAT_ExternalHandle *eh;
166 eh = GNUNET_malloc (sizeof (struct GNUNET_NAT_ExternalHandle));
169 eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
170 if (NULL == eh->opipe)
176 GNUNET_OS_start_process (NULL, eh->opipe, "external-ip", "external-ip",
180 GNUNET_DISK_pipe_close (eh->opipe);
184 GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE);
185 eh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
186 eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ);
188 GNUNET_SCHEDULER_add_read_file (timeout, eh->r, &read_external_ipv4, eh);
196 * @param eh operation to cancel
199 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh)
201 (void) GNUNET_OS_process_kill (eh->eip, SIGKILL);
202 GNUNET_OS_process_close (eh->eip);
203 GNUNET_DISK_pipe_close (eh->opipe);
204 if (GNUNET_SCHEDULER_NO_TASK != eh->task)
205 GNUNET_SCHEDULER_cancel (eh->task);
211 * Handle to a mapping created with upnpc.
213 struct GNUNET_NAT_MiniHandle
217 * Function to call on mapping changes.
219 GNUNET_NAT_AddressCallback ac;
227 * Command used to install the map.
229 struct GNUNET_OS_CommandHandle *map_cmd;
232 * Command used to refresh our map information.
234 struct GNUNET_OS_CommandHandle *refresh_cmd;
237 * Command used to remove the mapping.
239 struct GNUNET_OS_CommandHandle *unmap_cmd;
242 * Our current external mapping (if we have one).
244 struct sockaddr_in current_addr;
247 * We check the mapping periodically to see if it
248 * still works. This task triggers the check.
250 GNUNET_SCHEDULER_TaskIdentifier refresh_task;
253 * Are we mapping TCP or UDP?
258 * Did we succeed with creating a mapping?
263 * Did we find our mapping during refresh scan?
268 * Which port are we mapping?
276 * Run upnpc -l to find out if our mapping changed.
278 * @param cls the 'struct GNUNET_NAT_MiniHandle'
279 * @param tc scheduler context
282 do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
286 * Process the output from the 'upnpc -r' command.
288 * @param cls the 'struct GNUNET_NAT_MiniHandle'
289 * @param line line of output, NULL at the end
292 process_map_output (void *cls, const char *line);
296 * Process the output from 'upnpc -l' to see if our
297 * external mapping changed. If so, do the notifications.
299 * @param cls the 'struct GNUNET_NAT_MiniHandle'
300 * @param line line of output, NULL at the end
303 process_refresh_output (void *cls, const char *line)
305 struct GNUNET_NAT_MiniHandle *mini = cls;
313 GNUNET_OS_command_stop (mini->refresh_cmd);
314 mini->refresh_cmd = NULL;
315 if (mini->found == GNUNET_NO)
317 /* mapping disappeared, try to re-create */
320 mini->ac (mini->ac_cls, GNUNET_NO,
321 (const struct sockaddr *) &mini->current_addr,
322 sizeof (mini->current_addr));
323 mini->did_map = GNUNET_NO;
325 GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port);
327 GNUNET_OS_command_run (&process_map_output, mini, MAP_TIMEOUT,
328 "upnpc", "upnpc", "-r", pstr,
329 mini->is_tcp ? "tcp" : "udp", NULL);
330 if (NULL != mini->map_cmd)
334 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
338 return; /* never mapped, won't find our mapping anyway */
340 /* we're looking for output of the form:
341 * "ExternalIPAddress = 12.134.41.124" */
343 s = strstr (line, "ExternalIPAddress = ");
346 s += strlen ("ExternalIPAddress = ");
347 if (1 != inet_pton (AF_INET, s, &exip))
349 if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
350 return; /* no change */
352 mini->ac (mini->ac_cls, GNUNET_NO,
353 (const struct sockaddr *) &mini->current_addr,
354 sizeof (mini->current_addr));
355 mini->current_addr.sin_addr = exip;
356 mini->ac (mini->ac_cls, GNUNET_YES,
357 (const struct sockaddr *) &mini->current_addr,
358 sizeof (mini->current_addr));
362 * we're looking for output of the form:
364 * "0 TCP 3000->192.168.2.150:3000 'libminiupnpc' ''"
365 * "1 UDP 3001->192.168.2.150:3001 'libminiupnpc' ''"
367 * the pattern we look for is:
369 * "%s TCP PORT->STRING:OURPORT *" or
370 * "%s UDP PORT->STRING:OURPORT *"
372 GNUNET_snprintf (pstr, sizeof (pstr), ":%u ", mini->port);
373 if (NULL == (s = strstr (line, "->")))
375 if (NULL == strstr (s, pstr))
379 (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" :
380 "%*u UDP %u->%*s:%*u %*s", &nport))
382 mini->found = GNUNET_YES;
383 if (nport == ntohs (mini->current_addr.sin_port))
384 return; /* no change */
386 /* external port changed, update mapping */
387 mini->ac (mini->ac_cls, GNUNET_NO,
388 (const struct sockaddr *) &mini->current_addr,
389 sizeof (mini->current_addr));
390 mini->current_addr.sin_port = htons ((uint16_t) nport);
391 mini->ac (mini->ac_cls, GNUNET_YES,
392 (const struct sockaddr *) &mini->current_addr,
393 sizeof (mini->current_addr));
398 * Run upnpc -l to find out if our mapping changed.
400 * @param cls the 'struct GNUNET_NAT_MiniHandle'
401 * @param tc scheduler context
404 do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
406 struct GNUNET_NAT_MiniHandle *mini = cls;
408 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
409 mini->found = GNUNET_NO;
411 GNUNET_OS_command_run (&process_refresh_output, mini, MAP_TIMEOUT,
412 "upnpc", "upnpc", "-l", NULL);
417 * Process the output from the 'upnpc -r' command.
419 * @param cls the 'struct GNUNET_NAT_MiniHandle'
420 * @param line line of output, NULL at the end
423 process_map_output (void *cls, const char *line)
425 struct GNUNET_NAT_MiniHandle *mini = cls;
433 GNUNET_OS_command_stop (mini->map_cmd);
434 mini->map_cmd = NULL;
436 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
440 * The upnpc output we're after looks like this:
442 * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
444 if ((NULL == (ipaddr = strstr (line, " "))) ||
445 (NULL == (pstr = strstr (ipaddr, ":"))) ||
446 (1 != sscanf (pstr + 1, "%u", &port)))
448 return; /* skip line */
450 ipa = GNUNET_strdup (ipaddr + 1);
451 strstr (ipa, ":")[0] = '\0';
452 if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr))
455 return; /* skip line */
459 mini->current_addr.sin_port = htons (port);
460 mini->current_addr.sin_family = AF_INET;
461 #if HAVE_SOCKADDR_IN_SIN_LEN
462 mini->current_addr.sin_len = sizeof (struct sockaddr_in);
464 mini->did_map = GNUNET_YES;
465 mini->ac (mini->ac_cls, GNUNET_YES,
466 (const struct sockaddr *) &mini->current_addr,
467 sizeof (mini->current_addr));
472 * Start mapping the given port using (mini)upnpc. This function
473 * should typically not be used directly (it is used within the
474 * general-purpose 'GNUNET_NAT_register' code). However, it can be
475 * used if specifically UPnP-based NAT traversal is to be used or
478 * @param port port to map
479 * @param is_tcp GNUNET_YES to map TCP, GNUNET_NO for UDP
480 * @param ac function to call with mapping result
481 * @param ac_cls closure for 'ac'
482 * @return NULL on error (no 'upnpc' installed)
484 struct GNUNET_NAT_MiniHandle *
485 GNUNET_NAT_mini_map_start (uint16_t port, int is_tcp,
486 GNUNET_NAT_AddressCallback ac, void *ac_cls)
488 struct GNUNET_NAT_MiniHandle *ret;
491 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("upnpc"))
493 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_MiniHandle));
495 ret->ac_cls = ac_cls;
496 ret->is_tcp = is_tcp;
498 GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) port);
500 GNUNET_OS_command_run (&process_map_output, ret, MAP_TIMEOUT, "upnpc",
501 "upnpc", "-r", pstr, is_tcp ? "tcp" : "udp", NULL);
502 if (NULL != ret->map_cmd)
505 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret);
512 * Process output from our 'unmap' command.
514 * @param cls the 'struct GNUNET_NAT_MiniHandle'
515 * @param line line of output, NULL at the end
518 process_unmap_output (void *cls, const char *line)
520 struct GNUNET_NAT_MiniHandle *mini = cls;
525 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat", "UPnP unmap done\n");
527 GNUNET_OS_command_stop (mini->unmap_cmd);
528 mini->unmap_cmd = NULL;
532 /* we don't really care about the output... */
537 * Remove a mapping created with (mini)upnpc. Calling
538 * this function will give 'upnpc' 1s to remove tha mapping,
539 * so while this function is non-blocking, a task will be
540 * left with the scheduler for up to 1s past this call.
542 * @param mini the handle
545 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
549 if (GNUNET_SCHEDULER_NO_TASK != mini->refresh_task)
551 GNUNET_SCHEDULER_cancel (mini->refresh_task);
552 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
554 if (mini->refresh_cmd != NULL)
556 GNUNET_OS_command_stop (mini->refresh_cmd);
557 mini->refresh_cmd = NULL;
561 if (mini->map_cmd != NULL)
563 GNUNET_OS_command_stop (mini->map_cmd);
564 mini->map_cmd = NULL;
569 mini->ac (mini->ac_cls, GNUNET_NO,
570 (const struct sockaddr *) &mini->current_addr,
571 sizeof (mini->current_addr));
572 /* Note: oddly enough, deletion uses the external port whereas
573 * addition uses the internal port; this rarely matters since they
574 * often are the same, but it might... */
575 GNUNET_snprintf (pstr, sizeof (pstr), "%u",
576 (unsigned int) ntohs (mini->current_addr.sin_port));
578 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
579 "Unmapping port %u with UPnP\n",
580 ntohs (mini->current_addr.sin_port));
583 GNUNET_OS_command_run (&process_unmap_output, mini, UNMAP_TIMEOUT,
584 "upnpc", "upnpc", "-d", pstr,
585 mini->is_tcp ? "tcp" : "udp", NULL);
589 /* end of nat_mini.c */