2 This file is part of GNUnet.
3 Copyright (C) 2011-2014 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
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 struct GNUNET_SCHEDULER_Task * 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 @e 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').
102 * Error code for better debugging and user feedback
104 enum GNUNET_NAT_StatusCode ret;
109 * Read the output of 'external-ip' into buf. When complete, parse the
110 * address and call our callback.
112 * @param cls the `struct GNUNET_NAT_ExternalHandle`
115 read_external_ipv4 (void *cls)
117 struct GNUNET_NAT_ExternalHandle *eh = cls;
120 const struct GNUNET_SCHEDULER_TaskContext *tc;
123 tc = GNUNET_SCHEDULER_get_task_context ();
125 GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, eh->r))
128 GNUNET_DISK_file_read (eh->r, &eh->buf[eh->off],
129 sizeof (eh->buf) - eh->off);
133 eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE;
134 ret = -1; /* error reading, timeout, etc. */
138 /* try to read more */
141 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
142 (eh->timeout), eh->r,
143 &read_external_ipv4, eh);
146 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID;
147 if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n'))
149 eh->buf[eh->off - 1] = '\0';
150 if (1 == inet_pton (AF_INET, eh->buf, &addr))
152 if (0 != addr.s_addr)
153 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */
155 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
159 (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL,
161 GNUNET_NAT_mini_get_external_ipv4_cancel (eh);
166 * (Asynchronously) signal error invoking "external-ip" to client.
168 * @param cls the `struct GNUNET_NAT_ExternalHandle` (freed)
171 signal_external_ip_error (void *cls)
173 struct GNUNET_NAT_ExternalHandle *eh = cls;
184 * Try to get the external IPv4 address of this peer.
186 * @param timeout when to fail
187 * @param cb function to call with result
188 * @param cb_cls closure for @a cb
189 * @return handle for cancellation (can only be used until @a cb is called), never NULL
191 struct GNUNET_NAT_ExternalHandle *
192 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
193 GNUNET_NAT_IPCallback cb, void *cb_cls)
195 struct GNUNET_NAT_ExternalHandle *eh;
197 eh = GNUNET_new (struct GNUNET_NAT_ExternalHandle);
200 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
202 GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL))
204 LOG (GNUNET_ERROR_TYPE_INFO,
205 _("`external-ip' command not found\n"));
206 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND;
207 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
211 LOG (GNUNET_ERROR_TYPE_DEBUG,
212 "Running `external-ip' to determine our external IP\n");
213 eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
214 if (NULL == eh->opipe)
216 eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE;
217 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
222 GNUNET_OS_start_process (GNUNET_NO, 0, NULL, eh->opipe, NULL,
223 "external-ip", "external-ip",
227 GNUNET_DISK_pipe_close (eh->opipe);
228 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED;
229 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
233 GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE);
234 eh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
235 eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ);
237 GNUNET_SCHEDULER_add_read_file (timeout,
239 &read_external_ipv4, eh);
247 * @param eh operation to cancel
250 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh)
254 (void) GNUNET_OS_process_kill (eh->eip, SIGKILL);
255 GNUNET_OS_process_destroy (eh->eip);
257 if (NULL != eh->opipe)
258 GNUNET_DISK_pipe_close (eh->opipe);
259 if (NULL != eh->task)
260 GNUNET_SCHEDULER_cancel (eh->task);
266 * Handle to a mapping created with upnpc.
268 struct GNUNET_NAT_MiniHandle
272 * Function to call on mapping changes.
274 GNUNET_NAT_MiniAddressCallback ac;
282 * Command used to install the map.
284 struct GNUNET_OS_CommandHandle *map_cmd;
287 * Command used to refresh our map information.
289 struct GNUNET_OS_CommandHandle *refresh_cmd;
292 * Command used to remove the mapping.
294 struct GNUNET_OS_CommandHandle *unmap_cmd;
297 * Our current external mapping (if we have one).
299 struct sockaddr_in current_addr;
302 * We check the mapping periodically to see if it
303 * still works. This task triggers the check.
305 struct GNUNET_SCHEDULER_Task * refresh_task;
308 * Are we mapping TCP or UDP?
313 * Did we succeed with creating a mapping?
318 * Did we find our mapping during refresh scan?
323 * Which port are we mapping?
331 * Run "upnpc -l" to find out if our mapping changed.
333 * @param cls the `struct GNUNET_NAT_MiniHandle`
336 do_refresh (void *cls);
340 * Process the output from the "upnpc -r" command.
342 * @param cls the `struct GNUNET_NAT_MiniHandle`
343 * @param line line of output, NULL at the end
346 process_map_output (void *cls, const char *line);
350 * Run "upnpc -r" to map our internal port.
352 * @param mini our handle
355 run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini)
359 GNUNET_snprintf (pstr,
362 (unsigned int) mini->port);
364 GNUNET_OS_command_run (&process_map_output, mini, MAP_TIMEOUT,
365 "upnpc", "upnpc", "-r", pstr,
366 mini->is_tcp ? "tcp" : "udp", NULL);
367 if (NULL == mini->map_cmd)
369 mini->ac (mini->ac_cls,
372 GNUNET_NAT_ERROR_UPNPC_FAILED);
379 * Process the output from "upnpc -l" to see if our
380 * external mapping changed. If so, do the notifications.
382 * @param cls the `struct GNUNET_NAT_MiniHandle`
383 * @param line line of output, NULL at the end
386 process_refresh_output (void *cls, const char *line)
388 struct GNUNET_NAT_MiniHandle *mini = cls;
396 GNUNET_OS_command_stop (mini->refresh_cmd);
397 mini->refresh_cmd = NULL;
398 if (GNUNET_NO == mini->found)
400 /* mapping disappeared, try to re-create */
401 if (GNUNET_YES == mini->did_map)
403 mini->ac (mini->ac_cls,
405 (const struct sockaddr *) &mini->current_addr,
406 sizeof (mini->current_addr),
407 GNUNET_NAT_ERROR_SUCCESS);
408 mini->did_map = GNUNET_NO;
415 return; /* never mapped, won't find our mapping anyway */
417 /* we're looking for output of the form:
418 * "ExternalIPAddress = 12.134.41.124" */
420 s = strstr (line, "ExternalIPAddress = ");
423 s += strlen ("ExternalIPAddress = ");
424 if (1 != inet_pton (AF_INET, s, &exip))
426 if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
427 return; /* no change */
429 mini->ac (mini->ac_cls, GNUNET_NO,
430 (const struct sockaddr *) &mini->current_addr,
431 sizeof (mini->current_addr),
432 GNUNET_NAT_ERROR_SUCCESS);
433 mini->current_addr.sin_addr = exip;
434 mini->ac (mini->ac_cls, GNUNET_YES,
435 (const struct sockaddr *) &mini->current_addr,
436 sizeof (mini->current_addr),
437 GNUNET_NAT_ERROR_SUCCESS);
441 * we're looking for output of the form:
443 * "0 TCP 3000->192.168.2.150:3000 'libminiupnpc' ''"
444 * "1 UDP 3001->192.168.2.150:3001 'libminiupnpc' ''"
446 * the pattern we look for is:
448 * "%s TCP PORT->STRING:OURPORT *" or
449 * "%s UDP PORT->STRING:OURPORT *"
451 GNUNET_snprintf (pstr, sizeof (pstr), ":%u ", mini->port);
452 if (NULL == (s = strstr (line, "->")))
454 if (NULL == strstr (s, pstr))
458 (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" :
459 "%*u UDP %u->%*s:%*u %*s", &nport))
461 mini->found = GNUNET_YES;
462 if (nport == ntohs (mini->current_addr.sin_port))
463 return; /* no change */
465 /* external port changed, update mapping */
466 mini->ac (mini->ac_cls, GNUNET_NO,
467 (const struct sockaddr *) &mini->current_addr,
468 sizeof (mini->current_addr),
469 GNUNET_NAT_ERROR_SUCCESS);
470 mini->current_addr.sin_port = htons ((uint16_t) nport);
471 mini->ac (mini->ac_cls, GNUNET_YES,
472 (const struct sockaddr *) &mini->current_addr,
473 sizeof (mini->current_addr),
474 GNUNET_NAT_ERROR_SUCCESS);
479 * Run "upnpc -l" to find out if our mapping changed.
481 * @param cls the 'struct GNUNET_NAT_MiniHandle'
484 do_refresh (void *cls)
486 struct GNUNET_NAT_MiniHandle *mini = cls;
490 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
492 LOG (GNUNET_ERROR_TYPE_DEBUG,
493 "Running `upnpc' to check if our mapping still exists\n");
494 mini->found = GNUNET_NO;
496 if (NULL != mini->map_cmd)
498 /* took way too long, abort it! */
499 GNUNET_OS_command_stop (mini->map_cmd);
500 mini->map_cmd = NULL;
503 if (NULL != mini->refresh_cmd)
505 /* took way too long, abort it! */
506 GNUNET_OS_command_stop (mini->refresh_cmd);
507 mini->refresh_cmd = NULL;
511 GNUNET_OS_command_run (&process_refresh_output, mini, MAP_TIMEOUT,
512 "upnpc", "upnpc", "-l", NULL);
513 if (GNUNET_YES == ac)
514 mini->ac (mini->ac_cls,
517 GNUNET_NAT_ERROR_UPNPC_TIMEOUT);
522 * Process the output from the 'upnpc -r' command.
524 * @param cls the `struct GNUNET_NAT_MiniHandle`
525 * @param line line of output, NULL at the end
528 process_map_output (void *cls,
531 struct GNUNET_NAT_MiniHandle *mini = cls;
539 GNUNET_OS_command_stop (mini->map_cmd);
540 mini->map_cmd = NULL;
541 if (GNUNET_YES != mini->did_map)
542 mini->ac (mini->ac_cls,
545 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED);
546 if (NULL == mini->refresh_task)
548 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
554 * The upnpc output we're after looks like this:
556 * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
558 if ((NULL == (ipaddr = strstr (line, " "))) ||
559 (NULL == (pstr = strstr (ipaddr, ":"))) ||
560 (1 != SSCANF (pstr + 1, "%u", &port)))
562 return; /* skip line */
564 ipa = GNUNET_strdup (ipaddr + 1);
565 strstr (ipa, ":")[0] = '\0';
566 if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr))
569 return; /* skip line */
573 mini->current_addr.sin_port = htons (port);
574 mini->current_addr.sin_family = AF_INET;
575 #if HAVE_SOCKADDR_IN_SIN_LEN
576 mini->current_addr.sin_len = sizeof (struct sockaddr_in);
578 mini->did_map = GNUNET_YES;
579 mini->ac (mini->ac_cls, GNUNET_YES,
580 (const struct sockaddr *) &mini->current_addr,
581 sizeof (mini->current_addr),
582 GNUNET_NAT_ERROR_SUCCESS);
587 * Start mapping the given port using (mini)upnpc. This function
588 * should typically not be used directly (it is used within the
589 * general-purpose #GNUNET_NAT_register() code). However, it can be
590 * used if specifically UPnP-based NAT traversal is to be used or
593 * @param port port to map
594 * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
595 * @param ac function to call with mapping result
596 * @param ac_cls closure for @a ac
597 * @return NULL on error (no 'upnpc' installed)
599 struct GNUNET_NAT_MiniHandle *
600 GNUNET_NAT_mini_map_start (uint16_t port,
602 GNUNET_NAT_MiniAddressCallback ac,
605 struct GNUNET_NAT_MiniHandle *ret;
608 GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL))
610 LOG (GNUNET_ERROR_TYPE_INFO,
611 _("`upnpc' command not found\n"));
615 GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
618 LOG (GNUNET_ERROR_TYPE_DEBUG,
619 "Running `upnpc' to install mapping\n");
620 ret = GNUNET_new (struct GNUNET_NAT_MiniHandle);
622 ret->ac_cls = ac_cls;
623 ret->is_tcp = is_tcp;
626 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
635 * Process output from our 'unmap' command.
637 * @param cls the `struct GNUNET_NAT_MiniHandle`
638 * @param line line of output, NULL at the end
641 process_unmap_output (void *cls, const char *line)
643 struct GNUNET_NAT_MiniHandle *mini = cls;
647 LOG (GNUNET_ERROR_TYPE_DEBUG,
648 "UPnP unmap done\n");
649 GNUNET_OS_command_stop (mini->unmap_cmd);
650 mini->unmap_cmd = NULL;
654 /* we don't really care about the output... */
659 * Remove a mapping created with (mini)upnpc. Calling
660 * this function will give 'upnpc' 1s to remove tha mapping,
661 * so while this function is non-blocking, a task will be
662 * left with the scheduler for up to 1s past this call.
664 * @param mini the handle
667 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
671 if (NULL != mini->refresh_task)
673 GNUNET_SCHEDULER_cancel (mini->refresh_task);
674 mini->refresh_task = NULL;
676 if (NULL != mini->refresh_cmd)
678 GNUNET_OS_command_stop (mini->refresh_cmd);
679 mini->refresh_cmd = NULL;
681 if (NULL != mini->map_cmd)
683 GNUNET_OS_command_stop (mini->map_cmd);
684 mini->map_cmd = NULL;
686 if (GNUNET_NO == mini->did_map)
691 mini->ac (mini->ac_cls, GNUNET_NO,
692 (const struct sockaddr *) &mini->current_addr,
693 sizeof (mini->current_addr),
694 GNUNET_NAT_ERROR_SUCCESS);
695 /* Note: oddly enough, deletion uses the external port whereas
696 * addition uses the internal port; this rarely matters since they
697 * often are the same, but it might... */
698 GNUNET_snprintf (pstr,
701 (unsigned int) ntohs (mini->current_addr.sin_port));
702 LOG (GNUNET_ERROR_TYPE_DEBUG,
703 "Unmapping port %u with UPnP\n",
704 ntohs (mini->current_addr.sin_port));
706 GNUNET_OS_command_run (&process_unmap_output, mini, UNMAP_TIMEOUT,
707 "upnpc", "upnpc", "-d", pstr,
708 mini->is_tcp ? "tcp" : "udp", NULL);
712 /* end of nat_mini.c */