2 This file is part of GNUnet.
3 (C) 2009 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 2, 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 * This file has been adapted from the Transmission project:
23 * Originally licensed by the GPL version 2.
24 * Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com>
29 * @brief UPnP support for the NAT library
31 * @author Milan Bouchet-Valat
38 #include <miniupnp/miniupnpc.h>
39 #include <miniupnp/upnpcommands.h>
42 #include "gnunet_common.h"
43 #include "gnunet_nat_lib.h"
46 /* Component name for logging */
47 #define COMP_NAT_UPNP _("NAT (UPnP)")
59 struct GNUNET_NAT_UPNP_Handle
65 const struct sockaddr *addr;
67 unsigned int is_mapped;
69 struct sockaddr *ext_addr;
74 process_if (void *cls,
77 const struct sockaddr *addr,
80 struct GNUNET_NAT_UPNP_Handle *upnp = cls;
82 if (addr && GNUNET_NAT_cmp_addr (upnp->addr, addr) == 0)
91 GNUNET_NAT_UPNP_Handle *
92 GNUNET_NAT_UPNP_init (const struct sockaddr *addr, socklen_t addrlen,
95 GNUNET_NAT_UPNP_Handle *upnp =
96 GNUNET_malloc (sizeof (GNUNET_NAT_UPNP_Handle));
98 upnp->state = UPNP_DISCOVER;
100 upnp->addrlen = addrlen;
103 /* Find the interface corresponding to the address,
104 * on which we should broadcast call for routers */
106 GNUNET_OS_network_interfaces_list (process_if, upnp);
108 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, COMP_NAT_UPNP, "Could not find an interface matching the wanted address.\n");
114 GNUNET_NAT_UPNP_close (GNUNET_NAT_UPNP_Handle * handle)
116 GNUNET_assert (!handle->is_mapped);
117 GNUNET_assert ((handle->state == UPNP_IDLE)
118 || (handle->state == UPNP_ERR) || (handle->state == UPNP_DISCOVER));
120 if (handle->hasDiscovered)
121 FreeUPNPUrls (&handle->urls);
122 GNUNET_free (handle);
126 * Check state of UPnP NAT: port redirection, external IP address.
129 * @param handle the handle for UPnP object
130 * @param is_enabled whether enable port redirection
131 * @param doPortCheck FIXME
132 * @param ext_addr pointer for returning external IP address.
133 * Will be set to NULL if address could not be found. Don't free the sockaddr.
136 GNUNET_NAT_UPNP_pulse (GNUNET_NAT_UPNP_Handle * handle, int is_enabled,
137 int doPortCheck, struct sockaddr **ext_addr)
141 if (is_enabled && (handle->state == UPNP_DISCOVER))
143 struct UPNPDev *devlist;
145 devlist = upnpDiscover (2000, handle->iface, handle->addr, NULL, 0);
149 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_UPNP,
150 "upnpDiscover failed (errno %d - %s)\n", errno,
155 if (UPNP_GetValidIGD (devlist, &handle->urls, &handle->data,
158 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
159 _("Found Internet Gateway Device \"%s\"\n"),
160 handle->urls.controlURL);
161 handle->state = UPNP_IDLE;
162 handle->hasDiscovered = 1;
166 handle->state = UPNP_ERR;
168 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_UPNP,
169 "UPNP_GetValidIGD failed (errno %d - %s)\n",
170 errno, strerror (errno));
173 freeUPNPDevlist (devlist);
176 if (handle->state == UPNP_IDLE)
178 if (handle->is_mapped && !is_enabled)
179 handle->state = UPNP_UNMAP;
182 if (is_enabled && handle->is_mapped && doPortCheck)
189 GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port);
190 i = UPNP_GetSpecificPortMappingEntry (handle->urls.controlURL,
191 handle->data.servicetype, portStr,
192 "TCP", intClient, intPort);
193 if (i != UPNPCOMMAND_SUCCESS)
195 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
196 _("Port %d isn't forwarded\n"), handle->port);
197 handle->is_mapped = GNUNET_NO;
201 if (handle->state == UPNP_UNMAP)
204 GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port);
205 UPNP_DeletePortMapping (handle->urls.controlURL,
206 handle->data.servicetype, portStr, "TCP", NULL);
207 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
209 ("Stopping port forwarding through \"%s\", service \"%s\"\n"),
210 handle->urls.controlURL, handle->data.servicetype);
211 handle->is_mapped = 0;
212 handle->state = UPNP_IDLE;
216 if (handle->state == UPNP_IDLE)
218 if (is_enabled && !handle->is_mapped)
219 handle->state = UPNP_MAP;
222 if (handle->state == UPNP_MAP)
227 if (!handle->urls.controlURL)
228 handle->is_mapped = 0;
233 GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port);
234 GNUNET_snprintf (desc, sizeof (desc), "GNUnet at %d", handle->port);
235 err = UPNP_AddPortMapping (handle->urls.controlURL,
236 handle->data.servicetype,
237 portStr, portStr, GNUNET_a2s (handle->addr, handle->addrlen),
239 handle->is_mapped = !err;
241 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
243 ("Port forwarding through \"%s\", service \"%s\"\n"),
244 handle->urls.controlURL, handle->data.servicetype);
245 if (handle->is_mapped)
247 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
248 _("Port %d forwarded successfully\n"), handle->port);
249 handle->state = UPNP_IDLE;
253 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_UPNP,
254 "Port forwarding failed with error %d (errno %d - %s)\n",
255 err, errno, strerror (errno));
256 handle->state = UPNP_ERR;
260 if (ext_addr && handle->state != UPNP_DISCOVER)
265 struct in6_addr addr6;
267 /* Keep to NULL if address could not be found */
269 err = UPNP_GetExternalIPAddress (handle->urls.controlURL,
270 handle->data.servicetype, addr_str);
273 if (handle->ext_addr)
275 GNUNET_free (handle->ext_addr);
276 handle->ext_addr = NULL;
279 /* Try IPv4 and IPv6 as we don't know what's the format */
280 if (inet_aton (addr_str, &addr) != 0)
282 handle->ext_addr = GNUNET_malloc (sizeof (struct sockaddr_in));
283 handle->ext_addr->sa_family = AF_INET;
284 ((struct sockaddr_in *) handle->ext_addr)->sin_addr = addr;
285 *ext_addr = handle->ext_addr;
287 else if (inet_pton (AF_INET6, addr_str, &addr6) != 1)
289 handle->ext_addr = GNUNET_malloc (sizeof (struct sockaddr_in6));
290 handle->ext_addr->sa_family = AF_INET6;
291 ((struct sockaddr_in6 *) handle->ext_addr)->sin6_addr = addr6;
292 *ext_addr = handle->ext_addr;
295 GNUNET_assert (GNUNET_YES);
297 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_UPNP,
298 _("Found public IP address %s\n"),
305 if (handle->ext_addr)
307 GNUNET_free (handle->ext_addr);
308 handle->ext_addr = NULL;
311 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_UPNP,
312 "UPNP_GetExternalIPAddress failed (error %d)\n", err);
317 switch (handle->state)
320 ret = GNUNET_NAT_PORT_UNMAPPED;
324 ret = GNUNET_NAT_PORT_MAPPING;
328 ret = GNUNET_NAT_PORT_UNMAPPING;
333 handle->is_mapped ? GNUNET_NAT_PORT_MAPPED : GNUNET_NAT_PORT_UNMAPPED;
337 ret = GNUNET_NAT_PORT_ERROR;