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
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)
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, 15)
49 * Try to get the external IPv4 address of this peer.
50 * Note: calling this function may block this process
51 * for a few seconds (!).
53 * @param addr address to set
54 * @return GNUNET_OK on success,
55 * GNUNET_NO if the result is questionable,
56 * GNUNET_SYSERR on error
59 GNUNET_NAT_mini_get_external_ipv4 (struct in_addr *addr)
61 struct GNUNET_OS_Process *eip;
62 struct GNUNET_DISK_PipeHandle *opipe;
63 const struct GNUNET_DISK_FileHandle *r;
69 opipe = GNUNET_DISK_pipe (GNUNET_YES,
74 eip = GNUNET_OS_start_process (NULL,
80 GNUNET_DISK_pipe_close (opipe);
83 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
85 r = GNUNET_DISK_pipe_handle (opipe,
86 GNUNET_DISK_PIPE_END_READ);
88 while (0 < (ret = GNUNET_DISK_file_read (r, &buf[off], sizeof (buf)-off)))
91 (buf[off-1] == '\n') )
94 if (1 == inet_pton (AF_INET, buf, addr))
96 if (addr->s_addr == 0)
97 iret = GNUNET_NO; /* got 0.0.0.0 */
101 (void) GNUNET_OS_process_kill (eip, SIGKILL);
102 GNUNET_OS_process_close (eip);
103 GNUNET_DISK_pipe_close (opipe);
109 * Handle to a mapping created with upnpc.
111 struct GNUNET_NAT_MiniHandle
115 * Function to call on mapping changes.
117 GNUNET_NAT_AddressCallback ac;
125 * Command used to install the map.
127 struct GNUNET_OS_CommandHandle *map_cmd;
130 * Command used to refresh our map information.
132 struct GNUNET_OS_CommandHandle *refresh_cmd;
135 * Command used to remove the mapping.
137 struct GNUNET_OS_CommandHandle *unmap_cmd;
140 * Our current external mapping (if we have one).
142 struct sockaddr_in current_addr;
145 * We check the mapping periodically to see if it
146 * still works. This task triggers the check.
148 GNUNET_SCHEDULER_TaskIdentifier refresh_task;
151 * Are we mapping TCP or UDP?
156 * Did we succeed with creating a mapping?
161 * Which port are we mapping?
169 * Run upnpc -l to find out if our mapping changed.
171 * @param cls the 'struct GNUNET_NAT_MiniHandle'
172 * @param tc scheduler context
175 do_refresh (void *cls,
176 const struct GNUNET_SCHEDULER_TaskContext *tc);
180 * Process the output from 'upnpc -l' to see if our
181 * external mapping changed. If so, do the notifications.
183 * @param cls the 'struct GNUNET_NAT_MiniHandle'
184 * @param line line of output, NULL at the end
187 process_refresh_output (void *cls,
190 struct GNUNET_NAT_MiniHandle *mini = cls;
191 enum GNUNET_OS_ProcessStatusType type;
196 GNUNET_OS_command_stop (mini->refresh_cmd,
198 mini->refresh_cmd = NULL;
199 mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
204 /* FIXME: parse 'line' */
206 "Refresh output: `%s'\n",
212 * Run upnpc -l to find out if our mapping changed.
214 * @param cls the 'struct GNUNET_NAT_MiniHandle'
215 * @param tc scheduler context
218 do_refresh (void *cls,
219 const struct GNUNET_SCHEDULER_TaskContext *tc)
221 struct GNUNET_NAT_MiniHandle *mini = cls;
223 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
224 mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output,
235 * Process the output from the 'upnpc -r' command.
237 * @param cls the 'struct GNUNET_NAT_MiniHandle'
238 * @param line line of output, NULL at the end
241 process_map_output (void *cls,
244 struct GNUNET_NAT_MiniHandle *mini = cls;
245 enum GNUNET_OS_ProcessStatusType type;
254 GNUNET_OS_command_stop (mini->map_cmd,
256 mini->map_cmd = NULL;
257 if (mini->did_map == GNUNET_YES)
258 mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
264 The upnpc output we're after looks like this:
266 "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
268 if ( (NULL == (ipaddr = strstr (line, " "))) ||
269 (NULL == (pstr = strstr (ipaddr, ":"))) ||
270 (1 != sscanf (pstr + 1, "%u", &port)) )
273 "Skipping output `%s'\n",
275 return; /* skip line */
277 ipa = GNUNET_strdup (ipaddr + 1);
278 strstr (ipa, ":")[0] = '\0';
279 if (1 != inet_pton (AF_INET,
281 &mini->current_addr.sin_addr))
285 "Skipping output `%s'\n",
287 return; /* skip line */
291 mini->current_addr.sin_port = htons (port);
292 mini->current_addr.sin_family = AF_INET;
293 #if HAVE_SOCKADDR_IN_SIN_LEN
294 mini->current_addr.sin_len = sizeof (struct sockaddr_in);
296 mini->did_map = GNUNET_YES;
297 mini->ac (mini->ac_cls, GNUNET_YES,
298 (const struct sockaddr*) &mini->current_addr,
299 sizeof (mini->current_addr));
304 * Start mapping the given port using (mini)upnpc. This function
305 * should typically not be used directly (it is used within the
306 * general-purpose 'GNUNET_NAT_register' code). However, it can be
307 * used if specifically UPnP-based NAT traversal is to be used or
310 * @param port port to map
311 * @param is_tcp GNUNET_YES to map TCP, GNUNET_NO for UDP
312 * @param ac function to call with mapping result
313 * @param ac_cls closure for 'ac'
314 * @return NULL on error
316 struct GNUNET_NAT_MiniHandle *
317 GNUNET_NAT_mini_map_start (uint16_t port,
319 GNUNET_NAT_AddressCallback ac,
322 struct GNUNET_NAT_MiniHandle *ret;
325 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_MiniHandle));
327 ret->ac_cls = ac_cls;
328 ret->is_tcp = is_tcp;
330 GNUNET_snprintf (pstr, sizeof (pstr),
332 (unsigned int) port);
333 ret->map_cmd = GNUNET_OS_command_run (&process_map_output,
339 is_tcp ? "tcp" : "udp",
347 * Process output from our 'unmap' command.
349 * @param cls the 'struct GNUNET_NAT_MiniHandle'
350 * @param line line of output, NULL at the end
353 process_unmap_output (void *cls,
356 struct GNUNET_NAT_MiniHandle *mini = cls;
357 enum GNUNET_OS_ProcessStatusType type;
362 GNUNET_OS_command_stop (mini->unmap_cmd,
364 mini->unmap_cmd = NULL;
368 /* we don't really care about the output... */
373 * Remove a mapping created with (mini)upnpc. Calling
374 * this function will give 'upnpc' 1s to remove tha mapping,
375 * so while this function is non-blocking, a task will be
376 * left with the scheduler for up to 1s past this call.
378 * @param mini the handle
381 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
384 enum GNUNET_OS_ProcessStatusType type;
389 if (mini->map_cmd != NULL)
391 GNUNET_OS_command_stop (mini->map_cmd,
393 mini->map_cmd = NULL;
398 if (GNUNET_SCHEDULER_NO_TASK != mini->refresh_task)
400 GNUNET_SCHEDULER_cancel (mini->refresh_task);
401 mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
403 if (mini->refresh_cmd != NULL)
405 GNUNET_OS_command_stop (mini->refresh_cmd,
407 mini->refresh_cmd = NULL;
409 mini->ac (mini->ac_cls, GNUNET_NO,
410 (const struct sockaddr*) &mini->current_addr,
411 sizeof (mini->current_addr));
412 GNUNET_snprintf (pstr, sizeof (pstr),
414 (unsigned int) mini->port);
415 mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output,
421 mini->is_tcp ? "tcp" : "udp",
426 /* end of nat_mini.c */