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;
82 log_val (const char *func, int ret)
85 if (ret == NATPMP_TRYAGAIN)
86 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP, _("%s retry (%d)\n"), func, ret);
89 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP, _("%s succeeded (%d)\n"), func, ret);
91 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
92 "%s failed. natpmp returned %d (%s); errno is %d (%s)\n",
93 func, ret, strnatpmperr (ret), errno, strerror (errno));
99 struct GNUNET_NAT_NATPMP_Handle *
100 GNUNET_NAT_NATPMP_init (const struct sockaddr *addr, socklen_t addrlen,
103 struct GNUNET_NAT_NATPMP_Handle *nat;
105 nat = GNUNET_malloc (sizeof (struct GNUNET_NAT_NATPMP_Handle));
106 nat->state = NATPMP_DISCOVER;
109 nat->addrlen = addrlen;
114 GNUNET_NAT_NATPMP_close (GNUNET_NAT_NATPMP_Handle * nat)
118 closenatpmp (&nat->natpmp);
124 can_send_command (const struct GNUNET_NAT_NATPMP_Handle *nat)
126 return time (NULL) >= nat->command_time;
130 set_command_time (struct GNUNET_NAT_NATPMP_Handle *nat)
132 nat->command_time = time (NULL) + COMMAND_WAIT_SECS;
136 GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
137 struct sockaddr **ext_addr)
141 /* Keep to NULL if address could not be found */
144 if (is_enabled && (nat->state == NATPMP_DISCOVER))
146 int val = initnatpmp (&nat->natpmp);
147 log_val ("initnatpmp", val);
148 val = sendpublicaddressrequest (&nat->natpmp);
149 log_val ("sendpublicaddressrequest", val);
150 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_PUB;
151 nat->has_discovered = 1;
152 set_command_time (nat);
155 if ((nat->state == NATPMP_RECV_PUB) && can_send_command (nat))
157 natpmpresp_t response;
158 const int val = readnatpmpresponseorretry (&nat->natpmp,
160 log_val ("readnatpmpresponseorretry", val);
164 GNUNET_malloc (sizeof (response.pnu.publicaddress.addr));
165 memcpy (*ext_addr, &response.pnu.publicaddress.addr,
166 (sizeof (response.pnu.publicaddress.addr)));
168 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
169 _("Found public IP address %s\n"),
170 GNUNET_a2s (*ext_addr, sizeof (*ext_addr)));
172 nat->state = NATPMP_IDLE;
174 else if (val != NATPMP_TRYAGAIN)
176 nat->state = NATPMP_ERR;
180 if ((nat->state == NATPMP_IDLE) || (nat->state == NATPMP_ERR))
182 if (nat->is_mapped && !is_enabled)
183 nat->state = NATPMP_SEND_UNMAP;
186 if ((nat->state == NATPMP_SEND_UNMAP) && can_send_command (nat))
189 sendnewportmappingrequest (&nat->natpmp, NATPMP_PROTOCOL_TCP,
190 nat->port, nat->port,
192 log_val ("sendnewportmappingrequest", val);
193 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_UNMAP;
194 set_command_time (nat);
197 if (nat->state == NATPMP_RECV_UNMAP)
200 const int val = readnatpmpresponseorretry (&nat->natpmp, &resp);
201 log_val ("readnatpmpresponseorretry", val);
204 const int p = resp.pnu.newportmapping.privateport;
205 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP,
206 _("No longer forwarding port %d\n"), p);
210 nat->state = NATPMP_IDLE;
214 else if (val != NATPMP_TRYAGAIN)
216 nat->state = NATPMP_ERR;
220 if (nat->state == NATPMP_IDLE)
222 if (is_enabled && !nat->is_mapped && nat->has_discovered)
223 nat->state = NATPMP_SEND_MAP;
225 else if (nat->is_mapped && time (NULL) >= nat->renew_time)
226 nat->state = NATPMP_SEND_MAP;
229 if ((nat->state == NATPMP_SEND_MAP) && can_send_command (nat))
232 sendnewportmappingrequest (&nat->natpmp, NATPMP_PROTOCOL_TCP,
236 log_val ("sendnewportmappingrequest", val);
237 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_MAP;
238 set_command_time (nat);
241 if (nat->state == NATPMP_RECV_MAP)
244 const int val = readnatpmpresponseorretry (&nat->natpmp, &resp);
245 log_val ("readnatpmpresponseorretry", val);
248 nat->state = NATPMP_IDLE;
250 nat->renew_time = time (NULL) + LIFETIME_SECS;
251 nat->port = resp.pnu.newportmapping.privateport;
252 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP,
253 _("Port %d forwarded successfully\n"), nat->port);
255 else if (val != NATPMP_TRYAGAIN)
257 nat->state = NATPMP_ERR;
265 nat->is_mapped ? GNUNET_NAT_PORT_MAPPED : GNUNET_NAT_PORT_UNMAPPED;
268 case NATPMP_DISCOVER:
269 ret = GNUNET_NAT_PORT_UNMAPPED;
272 case NATPMP_RECV_PUB:
273 case NATPMP_SEND_MAP:
274 case NATPMP_RECV_MAP:
275 ret = GNUNET_NAT_PORT_MAPPING;
278 case NATPMP_SEND_UNMAP:
279 case NATPMP_RECV_UNMAP:
280 ret = GNUNET_NAT_PORT_UNMAPPING;
284 ret = GNUNET_NAT_PORT_ERROR;