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 NAT-PMP support for the NAT library
31 * @author Milan Bouchet-Valat
38 #define ENABLE_STRNATPMPERR
39 #include <libnatpmp/natpmp.h>
42 #include "gnunet_common.h"
43 #include "gnunet_nat_lib.h"
46 #define LIFETIME_SECS 3600
47 #define COMMAND_WAIT_SECS 8
48 /* Component name for logging */
49 #define COMP_NAT_NATPMP _("NAT (NAT-PMP))")
64 struct GNUNET_NAT_NATPMP_Handle
66 const struct sockaddr *addr;
68 struct sockaddr *ext_addr;
83 log_val (const char *func, int ret)
86 if (ret == NATPMP_TRYAGAIN)
87 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP, _("%s retry (%d)\n"), func, ret);
90 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP, _("%s succeeded (%d)\n"), func, ret);
92 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
93 "%s failed. natpmp returned %d (%s); errno is %d (%s)\n",
94 func, ret, strnatpmperr (ret), errno, strerror (errno));
100 struct GNUNET_NAT_NATPMP_Handle *
101 GNUNET_NAT_NATPMP_init (const struct sockaddr *addr, socklen_t addrlen,
104 struct GNUNET_NAT_NATPMP_Handle *nat;
106 nat = GNUNET_malloc (sizeof (struct GNUNET_NAT_NATPMP_Handle));
107 nat->state = NATPMP_DISCOVER;
110 nat->addrlen = addrlen;
111 nat->ext_addr = NULL;
116 GNUNET_NAT_NATPMP_close (GNUNET_NAT_NATPMP_Handle * nat)
120 closenatpmp (&nat->natpmp);
126 can_send_command (const struct GNUNET_NAT_NATPMP_Handle *nat)
128 return time (NULL) >= nat->command_time;
132 set_command_time (struct GNUNET_NAT_NATPMP_Handle *nat)
134 nat->command_time = time (NULL) + COMMAND_WAIT_SECS;
138 GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
139 struct sockaddr **ext_addr)
143 /* Keep to NULL if address could not be found */
146 if (is_enabled && (nat->state == NATPMP_DISCOVER))
148 int val = initnatpmp (&nat->natpmp);
149 log_val ("initnatpmp", val);
150 val = sendpublicaddressrequest (&nat->natpmp);
151 log_val ("sendpublicaddressrequest", val);
152 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_PUB;
153 nat->has_discovered = 1;
154 set_command_time (nat);
157 if ((nat->state == NATPMP_RECV_PUB) && can_send_command (nat))
159 natpmpresp_t response;
160 const int val = readnatpmpresponseorretry (&nat->natpmp,
162 log_val ("readnatpmpresponseorretry", val);
167 GNUNET_free (nat->ext_addr);
168 nat->ext_addr = NULL;
171 if (response.pnu.publicaddress.family == AF_INET)
174 GNUNET_malloc (sizeof (struct in_addr));
175 memcpy (nat->ext_addr, &response.pnu.publicaddress.addr,
176 sizeof (struct in_addr));
181 GNUNET_malloc (sizeof (struct in6_addr));
182 memcpy (nat->ext_addr, &response.pnu.publicaddress.addr6,
183 (sizeof (struct in6_addr)));
186 *ext_addr = nat->ext_addr;
188 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
189 _("Found public IP address %s\n"),
190 GNUNET_a2s (*ext_addr, sizeof (*ext_addr)));
192 nat->state = NATPMP_IDLE;
194 else if (val != NATPMP_TRYAGAIN)
196 nat->state = NATPMP_ERR;
200 if ((nat->state == NATPMP_IDLE) || (nat->state == NATPMP_ERR))
202 if (nat->is_mapped && !is_enabled)
203 nat->state = NATPMP_SEND_UNMAP;
206 if ((nat->state == NATPMP_SEND_UNMAP) && can_send_command (nat))
209 sendnewportmappingrequest (&nat->natpmp, NATPMP_PROTOCOL_TCP,
210 nat->port, nat->port,
212 log_val ("sendnewportmappingrequest", val);
213 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_UNMAP;
214 set_command_time (nat);
217 if (nat->state == NATPMP_RECV_UNMAP)
220 const int val = readnatpmpresponseorretry (&nat->natpmp, &resp);
221 log_val ("readnatpmpresponseorretry", val);
224 const int p = resp.pnu.newportmapping.privateport;
225 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP,
226 _("No longer forwarding port %d\n"), p);
230 nat->state = NATPMP_IDLE;
234 else if (val != NATPMP_TRYAGAIN)
236 nat->state = NATPMP_ERR;
240 if (nat->state == NATPMP_IDLE)
242 if (is_enabled && !nat->is_mapped && nat->has_discovered)
243 nat->state = NATPMP_SEND_MAP;
245 else if (nat->is_mapped && time (NULL) >= nat->renew_time)
246 nat->state = NATPMP_SEND_MAP;
249 if ((nat->state == NATPMP_SEND_MAP) && can_send_command (nat))
252 sendnewportmappingrequest (&nat->natpmp, NATPMP_PROTOCOL_TCP,
256 log_val ("sendnewportmappingrequest", val);
257 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_MAP;
258 set_command_time (nat);
261 if (nat->state == NATPMP_RECV_MAP)
264 const int val = readnatpmpresponseorretry (&nat->natpmp, &resp);
265 log_val ("readnatpmpresponseorretry", val);
268 nat->state = NATPMP_IDLE;
270 nat->renew_time = time (NULL) + LIFETIME_SECS;
271 nat->port = resp.pnu.newportmapping.privateport;
272 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP,
273 _("Port %d forwarded successfully\n"), nat->port);
275 else if (val != NATPMP_TRYAGAIN)
277 nat->state = NATPMP_ERR;
285 nat->is_mapped ? GNUNET_NAT_PORT_MAPPED : GNUNET_NAT_PORT_UNMAPPED;
288 case NATPMP_DISCOVER:
289 ret = GNUNET_NAT_PORT_UNMAPPED;
292 case NATPMP_RECV_PUB:
293 case NATPMP_SEND_MAP:
294 case NATPMP_RECV_MAP:
295 ret = GNUNET_NAT_PORT_MAPPING;
298 case NATPMP_SEND_UNMAP:
299 case NATPMP_RECV_UNMAP:
300 ret = GNUNET_NAT_PORT_UNMAPPING;
304 ret = GNUNET_NAT_PORT_ERROR;