+
+/**
+ * Run "upnpc -l" to find out if our mapping changed.
+ *
+ * @param cls the 'struct GNUNET_NAT_MiniHandle'
+ * @param tc scheduler context
+ */
+static void
+do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_NAT_MiniHandle *mini = cls;
+ int ac;
+
+ mini->refresh_task =
+ GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
+ &do_refresh, mini);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Running `upnpc' to check if our mapping still exists\n");
+ mini->found = GNUNET_NO;
+ ac = GNUNET_NO;
+ if (NULL != mini->map_cmd)
+ {
+ /* took way too long, abort it! */
+ GNUNET_OS_command_stop (mini->map_cmd);
+ mini->map_cmd = NULL;
+ ac = GNUNET_YES;
+ }
+ if (NULL != mini->refresh_cmd)
+ {
+ /* took way too long, abort it! */
+ GNUNET_OS_command_stop (mini->refresh_cmd);
+ mini->refresh_cmd = NULL;
+ ac = GNUNET_YES;
+ }
+ mini->refresh_cmd =
+ GNUNET_OS_command_run (&process_refresh_output, mini, MAP_TIMEOUT,
+ "upnpc", "upnpc", "-l", NULL);
+ if (GNUNET_YES == ac)
+ mini->ac (mini->ac_cls,
+ GNUNET_SYSERR,
+ NULL, 0,
+ GNUNET_NAT_ERROR_UPNPC_TIMEOUT);
+}
+
+
+/**
+ * Process the output from the 'upnpc -r' command.
+ *
+ * @param cls the `struct GNUNET_NAT_MiniHandle`
+ * @param line line of output, NULL at the end
+ */
+static void
+process_map_output (void *cls,
+ const char *line)
+{
+ struct GNUNET_NAT_MiniHandle *mini = cls;
+ const char *ipaddr;
+ char *ipa;
+ const char *pstr;
+ unsigned int port;
+
+ if (NULL == line)
+ {
+ GNUNET_OS_command_stop (mini->map_cmd);
+ mini->map_cmd = NULL;
+ if (GNUNET_YES != mini->did_map)
+ mini->ac (mini->ac_cls,
+ GNUNET_SYSERR,
+ NULL, 0,
+ GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED);
+ if (NULL == mini->refresh_task)
+ mini->refresh_task =
+ GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
+ return;
+ }
+ /*
+ * The upnpc output we're after looks like this:
+ *
+ * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
+ */
+ if ((NULL == (ipaddr = strstr (line, " "))) ||
+ (NULL == (pstr = strstr (ipaddr, ":"))) ||
+ (1 != SSCANF (pstr + 1, "%u", &port)))
+ {
+ return; /* skip line */
+ }
+ ipa = GNUNET_strdup (ipaddr + 1);
+ strstr (ipa, ":")[0] = '\0';
+ if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr))
+ {
+ GNUNET_free (ipa);
+ return; /* skip line */
+ }
+ GNUNET_free (ipa);
+
+ mini->current_addr.sin_port = htons (port);
+ mini->current_addr.sin_family = AF_INET;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ mini->current_addr.sin_len = sizeof (struct sockaddr_in);
+#endif
+ mini->did_map = GNUNET_YES;
+ mini->ac (mini->ac_cls, GNUNET_YES,
+ (const struct sockaddr *) &mini->current_addr,
+ sizeof (mini->current_addr),
+ GNUNET_NAT_ERROR_SUCCESS);
+}
+
+
+/**
+ * Start mapping the given port using (mini)upnpc. This function
+ * should typically not be used directly (it is used within the
+ * general-purpose #GNUNET_NAT_register() code). However, it can be
+ * used if specifically UPnP-based NAT traversal is to be used or
+ * tested.
+ *
+ * @param port port to map
+ * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
+ * @param ac function to call with mapping result
+ * @param ac_cls closure for @a ac
+ * @return NULL on error (no 'upnpc' installed)
+ */
+struct GNUNET_NAT_MiniHandle *
+GNUNET_NAT_mini_map_start (uint16_t port,
+ int is_tcp,
+ GNUNET_NAT_MiniAddressCallback ac,
+ void *ac_cls)
+{
+ struct GNUNET_NAT_MiniHandle *ret;
+
+ if (GNUNET_SYSERR ==
+ GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL))
+ {
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ _("`upnpc' command not found\n"));
+ ac (ac_cls,
+ GNUNET_SYSERR,
+ NULL, 0,
+ GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
+ return NULL;
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Running `upnpc' to install mapping\n");
+ ret = GNUNET_new (struct GNUNET_NAT_MiniHandle);
+ ret->ac = ac;
+ ret->ac_cls = ac_cls;
+ ret->is_tcp = is_tcp;
+ ret->port = port;
+ ret->refresh_task =
+ GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret);
+ run_upnpc_r (ret);
+ return ret;
+}
+
+
+/**
+ * Process output from our 'unmap' command.
+ *
+ * @param cls the `struct GNUNET_NAT_MiniHandle`
+ * @param line line of output, NULL at the end
+ */
+static void
+process_unmap_output (void *cls, const char *line)
+{
+ struct GNUNET_NAT_MiniHandle *mini = cls;
+
+ if (NULL == line)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "UPnP unmap done\n");
+ GNUNET_OS_command_stop (mini->unmap_cmd);
+ mini->unmap_cmd = NULL;
+ GNUNET_free (mini);
+ return;
+ }
+ /* we don't really care about the output... */
+}
+
+
+/**
+ * Remove a mapping created with (mini)upnpc. Calling
+ * this function will give 'upnpc' 1s to remove tha mapping,
+ * so while this function is non-blocking, a task will be
+ * left with the scheduler for up to 1s past this call.
+ *
+ * @param mini the handle
+ */
+void
+GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
+{
+ char pstr[6];
+
+ if (NULL != mini->refresh_task)
+ {
+ GNUNET_SCHEDULER_cancel (mini->refresh_task);
+ mini->refresh_task = NULL;
+ }
+ if (NULL != mini->refresh_cmd)
+ {
+ GNUNET_OS_command_stop (mini->refresh_cmd);
+ mini->refresh_cmd = NULL;
+ }
+ if (NULL != mini->map_cmd)
+ {
+ GNUNET_OS_command_stop (mini->map_cmd);
+ mini->map_cmd = NULL;
+ }
+ if (GNUNET_NO == mini->did_map)
+ {
+ GNUNET_free (mini);
+ return;
+ }
+ mini->ac (mini->ac_cls, GNUNET_NO,
+ (const struct sockaddr *) &mini->current_addr,
+ sizeof (mini->current_addr),
+ GNUNET_NAT_ERROR_SUCCESS);
+ /* Note: oddly enough, deletion uses the external port whereas
+ * addition uses the internal port; this rarely matters since they
+ * often are the same, but it might... */
+ GNUNET_snprintf (pstr,
+ sizeof (pstr),
+ "%u",
+ (unsigned int) ntohs (mini->current_addr.sin_port));
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Unmapping port %u with UPnP\n",
+ ntohs (mini->current_addr.sin_port));
+ mini->unmap_cmd =
+ GNUNET_OS_command_run (&process_unmap_output, mini, UNMAP_TIMEOUT,
+ "upnpc", "upnpc", "-d", pstr,
+ mini->is_tcp ? "tcp" : "udp", NULL);
+}
+
+