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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file nat/gnunet-service-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_service.h"
29 #include "gnunet-service-nat_mini.h"
32 #define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
35 * How long do we give upnpc to create a mapping?
37 #define MAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
40 * How long do we give upnpc to remove a mapping?
42 #define UNMAP_TIMEOUT \
43 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
46 * How often do we check for changes in the mapping?
48 #define MAP_REFRESH_FREQ \
49 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
52 /* ************************* external-ip calling ************************ */
55 * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation.
57 struct GNUNET_NAT_ExternalHandle
60 * Function to call with the result.
62 GNUNET_NAT_IPCallback cb;
72 struct GNUNET_SCHEDULER_Task *task;
75 * Handle to `external-ip` process.
77 struct GNUNET_OS_Process *eip;
80 * Handle to stdout pipe of `external-ip`.
82 struct GNUNET_DISK_PipeHandle *opipe;
85 * Read handle of @e opipe.
87 const struct GNUNET_DISK_FileHandle *r;
90 * Number of bytes in @e buf that are valid.
95 * Destination of our read operation (output of 'external-ip').
100 * Error code for better debugging and user feedback
102 enum GNUNET_NAT_StatusCode ret;
107 * Read the output of `external-ip` into `buf`. When complete, parse
108 * the address and call our callback.
110 * @param cls the `struct GNUNET_NAT_ExternalHandle`
113 read_external_ipv4 (void *cls)
115 struct GNUNET_NAT_ExternalHandle *eh = cls;
120 ret = GNUNET_DISK_file_read (eh->r,
122 sizeof(eh->buf) - eh->off);
125 /* try to read more */
127 eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
133 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID;
134 if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n'))
136 eh->buf[eh->off - 1] = '\0';
137 if (1 == inet_pton (AF_INET, eh->buf, &addr))
139 if (0 == addr.s_addr)
141 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;
164 eh->cb (eh->cb_cls, NULL, eh->ret);
170 * Try to get the external IPv4 address of this peer.
172 * @param cb function to call with result
173 * @param cb_cls closure for @a cb
174 * @return handle for cancellation (can only be used until @a cb is called), never NULL
176 struct GNUNET_NAT_ExternalHandle *
177 GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls)
179 struct GNUNET_NAT_ExternalHandle *eh;
181 eh = GNUNET_new (struct GNUNET_NAT_ExternalHandle);
184 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
186 GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL))
188 LOG (GNUNET_ERROR_TYPE_INFO, _ ("`external-ip' command not found\n"));
189 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND;
190 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh);
193 LOG (GNUNET_ERROR_TYPE_DEBUG,
194 "Running `external-ip' to determine our external IP\n");
195 eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
196 if (NULL == eh->opipe)
198 eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE;
199 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh);
202 eh->eip = GNUNET_OS_start_process (GNUNET_NO,
212 GNUNET_DISK_pipe_close (eh->opipe);
213 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED;
214 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh);
217 GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE);
218 eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ);
219 eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
230 * @param eh operation to cancel
233 GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh)
237 (void) GNUNET_OS_process_kill (eh->eip, SIGKILL);
238 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (eh->eip));
239 GNUNET_OS_process_destroy (eh->eip);
241 if (NULL != eh->opipe)
243 GNUNET_DISK_pipe_close (eh->opipe);
246 if (NULL != eh->task)
248 GNUNET_SCHEDULER_cancel (eh->task);
255 /* ************************* upnpc calling ************************ */
259 * Handle to a mapping created with upnpc.
261 struct GNUNET_NAT_MiniHandle
264 * Function to call on mapping changes.
266 GNUNET_NAT_MiniAddressCallback ac;
274 * Command used to install the map.
276 struct GNUNET_OS_CommandHandle *map_cmd;
279 * Command used to refresh our map information.
281 struct GNUNET_OS_CommandHandle *refresh_cmd;
284 * Command used to remove the mapping.
286 struct GNUNET_OS_CommandHandle *unmap_cmd;
289 * Our current external mapping (if we have one).
291 struct sockaddr_in current_addr;
294 * We check the mapping periodically to see if it
295 * still works. This task triggers the check.
297 struct GNUNET_SCHEDULER_Task *refresh_task;
300 * Are we mapping TCP or UDP?
305 * Did we succeed with creating a mapping?
310 * Did we find our mapping during refresh scan?
315 * Which port are we mapping?
322 * Run "upnpc -l" to find out if our mapping changed.
324 * @param cls the `struct GNUNET_NAT_MiniHandle`
327 do_refresh (void *cls);
331 * Process the output from the "upnpc -r" command.
333 * @param cls the `struct GNUNET_NAT_MiniHandle`
334 * @param line line of output, NULL at the end
337 process_map_output (void *cls, const char *line);
341 * Run "upnpc -r" to map our internal port.
343 * @param mini our handle
346 run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini)
350 GNUNET_snprintf (pstr, sizeof(pstr), "%u", (unsigned int) mini->port);
351 mini->map_cmd = GNUNET_OS_command_run (&process_map_output,
358 mini->is_tcp ? "tcp" : "udp",
360 if (NULL == mini->map_cmd)
362 mini->ac (mini->ac_cls,
366 GNUNET_NAT_ERROR_UPNPC_FAILED);
373 * Process the output from "upnpc -l" to see if our
374 * external mapping changed. If so, do the notifications.
376 * @param cls the `struct GNUNET_NAT_MiniHandle`
377 * @param line line of output, NULL at the end
380 process_refresh_output (void *cls, const char *line)
382 struct GNUNET_NAT_MiniHandle *mini = cls;
390 GNUNET_OS_command_stop (mini->refresh_cmd);
391 mini->refresh_cmd = NULL;
392 if (GNUNET_NO == mini->found)
394 /* mapping disappeared, try to re-create */
395 if (GNUNET_YES == mini->did_map)
397 mini->ac (mini->ac_cls,
399 (const struct sockaddr *) &mini->current_addr,
400 sizeof(mini->current_addr),
401 GNUNET_NAT_ERROR_SUCCESS);
402 mini->did_map = GNUNET_NO;
409 return; /* never mapped, won't find our mapping anyway */
411 /* we're looking for output of the form:
412 * "ExternalIPAddress = 12.134.41.124" */
414 s = strstr (line, "ExternalIPAddress = ");
417 s += strlen ("ExternalIPAddress = ");
418 if (1 != inet_pton (AF_INET, s, &exip))
420 if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
421 return; /* no change */
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->current_addr.sin_addr = exip;
429 mini->ac (mini->ac_cls,
431 (const struct sockaddr *) &mini->current_addr,
432 sizeof(mini->current_addr),
433 GNUNET_NAT_ERROR_SUCCESS);
437 * we're looking for output of the form:
439 * "0 TCP 3000->192.168.2.150:3000 'libminiupnpc' ''"
440 * "1 UDP 3001->192.168.2.150:3001 'libminiupnpc' ''"
442 * the pattern we look for is:
444 * "%s TCP PORT->STRING:OURPORT *" or
445 * "%s UDP PORT->STRING:OURPORT *"
446 */GNUNET_snprintf (pstr, sizeof(pstr), ":%u ", mini->port);
447 if (NULL == (s = strstr (line, "->")))
449 if (NULL == strstr (s, pstr))
451 if (1 != sscanf (line,
452 (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s"
453 : "%*u UDP %u->%*s:%*u %*s",
456 mini->found = GNUNET_YES;
457 if (nport == ntohs (mini->current_addr.sin_port))
458 return; /* no change */
460 /* external port changed, update mapping */
461 mini->ac (mini->ac_cls,
463 (const struct sockaddr *) &mini->current_addr,
464 sizeof(mini->current_addr),
465 GNUNET_NAT_ERROR_SUCCESS);
466 mini->current_addr.sin_port = htons ((uint16_t) nport);
467 mini->ac (mini->ac_cls,
469 (const struct sockaddr *) &mini->current_addr,
470 sizeof(mini->current_addr),
471 GNUNET_NAT_ERROR_SUCCESS);
476 * Run "upnpc -l" to find out if our mapping changed.
478 * @param cls the 'struct GNUNET_NAT_MiniHandle'
481 do_refresh (void *cls)
483 struct GNUNET_NAT_MiniHandle *mini = cls;
487 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
488 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 "Running `upnpc' to check if our mapping still exists\n");
490 mini->found = GNUNET_NO;
492 if (NULL != mini->map_cmd)
494 /* took way too long, abort it! */
495 GNUNET_OS_command_stop (mini->map_cmd);
496 mini->map_cmd = NULL;
499 if (NULL != mini->refresh_cmd)
501 /* took way too long, abort it! */
502 GNUNET_OS_command_stop (mini->refresh_cmd);
503 mini->refresh_cmd = NULL;
506 mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output,
513 if (GNUNET_YES == ac)
514 mini->ac (mini->ac_cls,
518 GNUNET_NAT_ERROR_UPNPC_TIMEOUT);
523 * Process the output from the 'upnpc -r' command.
525 * @param cls the `struct GNUNET_NAT_MiniHandle`
526 * @param line line of output, NULL at the end
529 process_map_output (void *cls, const char *line)
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,
546 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED);
547 if (NULL == mini->refresh_task)
549 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
553 * The upnpc output we're after looks like this:
555 * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
556 */if ((NULL == (ipaddr = strstr (line, " "))) ||
557 (NULL == (pstr = strstr (ipaddr, ":"))) ||
558 (1 != sscanf (pstr + 1, "%u", &port)))
560 return; /* skip line */
562 ipa = GNUNET_strdup (ipaddr + 1);
563 strstr (ipa, ":")[0] = '\0';
564 if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr))
567 return; /* skip line */
571 mini->current_addr.sin_port = htons (port);
572 mini->current_addr.sin_family = AF_INET;
573 #if HAVE_SOCKADDR_IN_SIN_LEN
574 mini->current_addr.sin_len = sizeof(struct sockaddr_in);
576 mini->did_map = GNUNET_YES;
577 mini->ac (mini->ac_cls,
579 (const struct sockaddr *) &mini->current_addr,
580 sizeof(mini->current_addr),
581 GNUNET_NAT_ERROR_SUCCESS);
586 * Start mapping the given port using (mini)upnpc. This function
587 * should typically not be used directly (it is used within the
588 * general-purpose #GNUNET_NAT_register() code). However, it can be
589 * used if specifically UPnP-based NAT traversal is to be used or
592 * @param port port to map
593 * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
594 * @param ac function to call with mapping result
595 * @param ac_cls closure for @a ac
596 * @return NULL on error (no 'upnpc' installed)
598 struct GNUNET_NAT_MiniHandle *
599 GNUNET_NAT_mini_map_start (uint16_t port,
601 GNUNET_NAT_MiniAddressCallback ac,
604 struct GNUNET_NAT_MiniHandle *ret;
606 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL))
608 LOG (GNUNET_ERROR_TYPE_INFO, _ ("`upnpc' command not found\n"));
609 ac (ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
612 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n");
613 ret = GNUNET_new (struct GNUNET_NAT_MiniHandle);
615 ret->ac_cls = ac_cls;
616 ret->is_tcp = is_tcp;
619 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret);
626 * Process output from our 'unmap' command.
628 * @param cls the `struct GNUNET_NAT_MiniHandle`
629 * @param line line of output, NULL at the end
632 process_unmap_output (void *cls, const char *line)
634 struct GNUNET_NAT_MiniHandle *mini = cls;
638 LOG (GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n");
639 GNUNET_OS_command_stop (mini->unmap_cmd);
640 mini->unmap_cmd = NULL;
644 /* we don't really care about the output... */
649 * Remove a mapping created with (mini)upnpc. Calling
650 * this function will give 'upnpc' 1s to remove tha mapping,
651 * so while this function is non-blocking, a task will be
652 * left with the scheduler for up to 1s past this call.
654 * @param mini the handle
657 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
661 if (NULL != mini->refresh_task)
663 GNUNET_SCHEDULER_cancel (mini->refresh_task);
664 mini->refresh_task = NULL;
666 if (NULL != mini->refresh_cmd)
668 GNUNET_OS_command_stop (mini->refresh_cmd);
669 mini->refresh_cmd = NULL;
671 if (NULL != mini->map_cmd)
673 GNUNET_OS_command_stop (mini->map_cmd);
674 mini->map_cmd = NULL;
676 if (GNUNET_NO == mini->did_map)
681 mini->ac (mini->ac_cls,
683 (const struct sockaddr *) &mini->current_addr,
684 sizeof(mini->current_addr),
685 GNUNET_NAT_ERROR_SUCCESS);
686 /* Note: oddly enough, deletion uses the external port whereas
687 * addition uses the internal port; this rarely matters since they
688 * often are the same, but it might... */
689 GNUNET_snprintf (pstr,
692 (unsigned int) ntohs (mini->current_addr.sin_port));
693 LOG (GNUNET_ERROR_TYPE_DEBUG,
694 "Unmapping port %u with UPnP\n",
695 ntohs (mini->current_addr.sin_port));
696 mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output,
703 mini->is_tcp ? "tcp" : "udp",
708 /* end of gnunet-service-nat_mini.c */