2 This file is part of GNUnet.
3 Copyright (C) 2011-2014, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file nat/gnunet-service-nat_mini.c
21 * @brief functions for interaction with miniupnp; tested with miniupnpc 1.5
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_nat_service.h"
27 #include "gnunet-service-nat_mini.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
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)
48 /* ************************* external-ip calling ************************ */
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 * Number of bytes in @e buf that are valid.
92 * Destination of our read operation (output of 'external-ip').
97 * Error code for better debugging and user feedback
99 enum GNUNET_NAT_StatusCode ret;
104 * Read the output of `external-ip` into `buf`. When complete, parse
105 * the address and call our callback.
107 * @param cls the `struct GNUNET_NAT_ExternalHandle`
110 read_external_ipv4 (void *cls)
112 struct GNUNET_NAT_ExternalHandle *eh = cls;
117 ret = GNUNET_DISK_file_read (eh->r,
119 sizeof (eh->buf) - eh->off);
122 /* try to read more */
125 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
131 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID;
132 if ( (eh->off > 7) &&
133 (eh->buf[eh->off - 1] == '\n') )
135 eh->buf[eh->off - 1] = '\0';
136 if (1 == inet_pton (AF_INET,
140 if (0 == addr.s_addr)
141 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */
143 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
147 (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL,
149 GNUNET_NAT_mini_get_external_ipv4_cancel_ (eh);
154 * (Asynchronously) signal error invoking `external-ip` to client.
156 * @param cls the `struct GNUNET_NAT_ExternalHandle` (freed)
159 signal_external_ip_error (void *cls)
161 struct GNUNET_NAT_ExternalHandle *eh = cls;
172 * Try to get the external IPv4 address of this peer.
174 * @param cb function to call with result
175 * @param cb_cls closure for @a cb
176 * @return handle for cancellation (can only be used until @a cb is called), never NULL
178 struct GNUNET_NAT_ExternalHandle *
179 GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb,
182 struct GNUNET_NAT_ExternalHandle *eh;
184 eh = GNUNET_new (struct GNUNET_NAT_ExternalHandle);
187 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
189 GNUNET_OS_check_helper_binary ("external-ip",
193 LOG (GNUNET_ERROR_TYPE_INFO,
194 _("`external-ip' command not found\n"));
195 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND;
196 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
200 LOG (GNUNET_ERROR_TYPE_DEBUG,
201 "Running `external-ip' to determine our external IP\n");
202 eh->opipe = GNUNET_DISK_pipe (GNUNET_YES,
206 if (NULL == eh->opipe)
208 eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE;
209 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
214 GNUNET_OS_start_process (GNUNET_NO,
224 GNUNET_DISK_pipe_close (eh->opipe);
225 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED;
226 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
230 GNUNET_DISK_pipe_close_end (eh->opipe,
231 GNUNET_DISK_PIPE_END_WRITE);
232 eh->r = GNUNET_DISK_pipe_handle (eh->opipe,
233 GNUNET_DISK_PIPE_END_READ);
235 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
246 * @param eh operation to cancel
249 GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh)
253 (void) GNUNET_OS_process_kill (eh->eip,
255 GNUNET_break (GNUNET_OK ==
256 GNUNET_OS_process_wait (eh->eip));
257 GNUNET_OS_process_destroy (eh->eip);
259 if (NULL != eh->opipe)
261 GNUNET_DISK_pipe_close (eh->opipe);
264 if (NULL != eh->task)
266 GNUNET_SCHEDULER_cancel (eh->task);
273 /* ************************* upnpc calling ************************ */
277 * Handle to a mapping created with upnpc.
279 struct GNUNET_NAT_MiniHandle
283 * Function to call on mapping changes.
285 GNUNET_NAT_MiniAddressCallback ac;
293 * Command used to install the map.
295 struct GNUNET_OS_CommandHandle *map_cmd;
298 * Command used to refresh our map information.
300 struct GNUNET_OS_CommandHandle *refresh_cmd;
303 * Command used to remove the mapping.
305 struct GNUNET_OS_CommandHandle *unmap_cmd;
308 * Our current external mapping (if we have one).
310 struct sockaddr_in current_addr;
313 * We check the mapping periodically to see if it
314 * still works. This task triggers the check.
316 struct GNUNET_SCHEDULER_Task *refresh_task;
319 * Are we mapping TCP or UDP?
324 * Did we succeed with creating a mapping?
329 * Did we find our mapping during refresh scan?
334 * Which port are we mapping?
342 * Run "upnpc -l" to find out if our mapping changed.
344 * @param cls the `struct GNUNET_NAT_MiniHandle`
347 do_refresh (void *cls);
351 * Process the output from the "upnpc -r" command.
353 * @param cls the `struct GNUNET_NAT_MiniHandle`
354 * @param line line of output, NULL at the end
357 process_map_output (void *cls,
362 * Run "upnpc -r" to map our internal port.
364 * @param mini our handle
367 run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini)
371 GNUNET_snprintf (pstr,
374 (unsigned int) mini->port);
376 = GNUNET_OS_command_run (&process_map_output,
383 mini->is_tcp ? "tcp" : "udp",
385 if (NULL == mini->map_cmd)
387 mini->ac (mini->ac_cls,
391 GNUNET_NAT_ERROR_UPNPC_FAILED);
398 * Process the output from "upnpc -l" to see if our
399 * external mapping changed. If so, do the notifications.
401 * @param cls the `struct GNUNET_NAT_MiniHandle`
402 * @param line line of output, NULL at the end
405 process_refresh_output (void *cls,
408 struct GNUNET_NAT_MiniHandle *mini = cls;
416 GNUNET_OS_command_stop (mini->refresh_cmd);
417 mini->refresh_cmd = NULL;
418 if (GNUNET_NO == mini->found)
420 /* mapping disappeared, try to re-create */
421 if (GNUNET_YES == mini->did_map)
423 mini->ac (mini->ac_cls,
425 (const struct sockaddr *) &mini->current_addr,
426 sizeof (mini->current_addr),
427 GNUNET_NAT_ERROR_SUCCESS);
428 mini->did_map = GNUNET_NO;
435 return; /* never mapped, won't find our mapping anyway */
437 /* we're looking for output of the form:
438 * "ExternalIPAddress = 12.134.41.124" */
441 "ExternalIPAddress = ");
444 s += strlen ("ExternalIPAddress = ");
445 if (1 != inet_pton (AF_INET,
449 if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
450 return; /* no change */
452 mini->ac (mini->ac_cls,
454 (const struct sockaddr *) &mini->current_addr,
455 sizeof (mini->current_addr),
456 GNUNET_NAT_ERROR_SUCCESS);
457 mini->current_addr.sin_addr = exip;
458 mini->ac (mini->ac_cls,
460 (const struct sockaddr *) &mini->current_addr,
461 sizeof (mini->current_addr),
462 GNUNET_NAT_ERROR_SUCCESS);
466 * we're looking for output of the form:
468 * "0 TCP 3000->192.168.2.150:3000 'libminiupnpc' ''"
469 * "1 UDP 3001->192.168.2.150:3001 'libminiupnpc' ''"
471 * the pattern we look for is:
473 * "%s TCP PORT->STRING:OURPORT *" or
474 * "%s UDP PORT->STRING:OURPORT *"
476 GNUNET_snprintf (pstr,
480 if (NULL == (s = strstr (line, "->")))
482 if (NULL == strstr (s, pstr))
486 (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" :
487 "%*u UDP %u->%*s:%*u %*s", &nport))
489 mini->found = GNUNET_YES;
490 if (nport == ntohs (mini->current_addr.sin_port))
491 return; /* no change */
493 /* external port changed, update mapping */
494 mini->ac (mini->ac_cls,
496 (const struct sockaddr *) &mini->current_addr,
497 sizeof (mini->current_addr),
498 GNUNET_NAT_ERROR_SUCCESS);
499 mini->current_addr.sin_port = htons ((uint16_t) nport);
500 mini->ac (mini->ac_cls,
502 (const struct sockaddr *) &mini->current_addr,
503 sizeof (mini->current_addr),
504 GNUNET_NAT_ERROR_SUCCESS);
509 * Run "upnpc -l" to find out if our mapping changed.
511 * @param cls the 'struct GNUNET_NAT_MiniHandle'
514 do_refresh (void *cls)
516 struct GNUNET_NAT_MiniHandle *mini = cls;
520 = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
523 LOG (GNUNET_ERROR_TYPE_DEBUG,
524 "Running `upnpc' to check if our mapping still exists\n");
525 mini->found = GNUNET_NO;
527 if (NULL != mini->map_cmd)
529 /* took way too long, abort it! */
530 GNUNET_OS_command_stop (mini->map_cmd);
531 mini->map_cmd = NULL;
534 if (NULL != mini->refresh_cmd)
536 /* took way too long, abort it! */
537 GNUNET_OS_command_stop (mini->refresh_cmd);
538 mini->refresh_cmd = NULL;
542 = GNUNET_OS_command_run (&process_refresh_output,
549 if (GNUNET_YES == ac)
550 mini->ac (mini->ac_cls,
554 GNUNET_NAT_ERROR_UPNPC_TIMEOUT);
559 * Process the output from the 'upnpc -r' command.
561 * @param cls the `struct GNUNET_NAT_MiniHandle`
562 * @param line line of output, NULL at the end
565 process_map_output (void *cls,
568 struct GNUNET_NAT_MiniHandle *mini = cls;
576 GNUNET_OS_command_stop (mini->map_cmd);
577 mini->map_cmd = NULL;
578 if (GNUNET_YES != mini->did_map)
579 mini->ac (mini->ac_cls,
583 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED);
584 if (NULL == mini->refresh_task)
586 = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
592 * The upnpc output we're after looks like this:
594 * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
596 if ((NULL == (ipaddr = strstr (line, " "))) ||
597 (NULL == (pstr = strstr (ipaddr, ":"))) ||
598 (1 != SSCANF (pstr + 1, "%u", &port)))
600 return; /* skip line */
602 ipa = GNUNET_strdup (ipaddr + 1);
603 strstr (ipa, ":")[0] = '\0';
604 if (1 != inet_pton (AF_INET,
606 &mini->current_addr.sin_addr))
609 return; /* skip line */
613 mini->current_addr.sin_port = htons (port);
614 mini->current_addr.sin_family = AF_INET;
615 #if HAVE_SOCKADDR_IN_SIN_LEN
616 mini->current_addr.sin_len = sizeof (struct sockaddr_in);
618 mini->did_map = GNUNET_YES;
619 mini->ac (mini->ac_cls,
621 (const struct sockaddr *) &mini->current_addr,
622 sizeof (mini->current_addr),
623 GNUNET_NAT_ERROR_SUCCESS);
628 * Start mapping the given port using (mini)upnpc. This function
629 * should typically not be used directly (it is used within the
630 * general-purpose #GNUNET_NAT_register() code). However, it can be
631 * used if specifically UPnP-based NAT traversal is to be used or
634 * @param port port to map
635 * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
636 * @param ac function to call with mapping result
637 * @param ac_cls closure for @a ac
638 * @return NULL on error (no 'upnpc' installed)
640 struct GNUNET_NAT_MiniHandle *
641 GNUNET_NAT_mini_map_start (uint16_t port,
643 GNUNET_NAT_MiniAddressCallback ac,
646 struct GNUNET_NAT_MiniHandle *ret;
649 GNUNET_OS_check_helper_binary ("upnpc",
653 LOG (GNUNET_ERROR_TYPE_INFO,
654 _("`upnpc' command not found\n"));
658 GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
661 LOG (GNUNET_ERROR_TYPE_DEBUG,
662 "Running `upnpc' to install mapping\n");
663 ret = GNUNET_new (struct GNUNET_NAT_MiniHandle);
665 ret->ac_cls = ac_cls;
666 ret->is_tcp = is_tcp;
669 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
678 * Process output from our 'unmap' command.
680 * @param cls the `struct GNUNET_NAT_MiniHandle`
681 * @param line line of output, NULL at the end
684 process_unmap_output (void *cls,
687 struct GNUNET_NAT_MiniHandle *mini = cls;
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
692 "UPnP unmap done\n");
693 GNUNET_OS_command_stop (mini->unmap_cmd);
694 mini->unmap_cmd = NULL;
698 /* we don't really care about the output... */
703 * Remove a mapping created with (mini)upnpc. Calling
704 * this function will give 'upnpc' 1s to remove tha mapping,
705 * so while this function is non-blocking, a task will be
706 * left with the scheduler for up to 1s past this call.
708 * @param mini the handle
711 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
715 if (NULL != mini->refresh_task)
717 GNUNET_SCHEDULER_cancel (mini->refresh_task);
718 mini->refresh_task = NULL;
720 if (NULL != mini->refresh_cmd)
722 GNUNET_OS_command_stop (mini->refresh_cmd);
723 mini->refresh_cmd = NULL;
725 if (NULL != mini->map_cmd)
727 GNUNET_OS_command_stop (mini->map_cmd);
728 mini->map_cmd = NULL;
730 if (GNUNET_NO == mini->did_map)
735 mini->ac (mini->ac_cls,
737 (const struct sockaddr *) &mini->current_addr,
738 sizeof (mini->current_addr),
739 GNUNET_NAT_ERROR_SUCCESS);
740 /* Note: oddly enough, deletion uses the external port whereas
741 * addition uses the internal port; this rarely matters since they
742 * often are the same, but it might... */
743 GNUNET_snprintf (pstr,
746 (unsigned int) ntohs (mini->current_addr.sin_port));
747 LOG (GNUNET_ERROR_TYPE_DEBUG,
748 "Unmapping port %u with UPnP\n",
749 ntohs (mini->current_addr.sin_port));
751 = GNUNET_OS_command_run (&process_unmap_output,
758 mini->is_tcp ? "tcp" : "udp",
763 /* end of gnunet-service-nat_mini.c */