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 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 * 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"
47 #define LIFETIME_SECS 3600
48 #define COMMAND_WAIT_SECS 8
49 /* Component name for logging */
50 #define COMP_NAT_NATPMP _("NAT (NAT-PMP))")
65 struct GNUNET_NAT_NATPMP_Handle
67 const struct sockaddr *addr;
69 struct sockaddr *ext_addr;
75 enum NATPMP_state state;
76 struct natpmp_t natpmp;
77 struct GNUNET_SCHEDULER_Handle *sched;
82 log_val (const char *func, int ret)
85 if (ret == NATPMP_TRYAGAIN)
86 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
87 COMP_NAT_NATPMP, _("%s retry (%d)\n"), func, ret);
89 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
90 COMP_NAT_NATPMP, _("%s succeeded (%d)\n"), func, ret);
92 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
94 "%s failed. natpmp returned %d (%s); errno is %d (%s)\n",
95 func, ret, strnatpmperr (ret), errno, strerror (errno));
99 struct GNUNET_NAT_NATPMP_Handle *
100 GNUNET_NAT_NATPMP_init (struct GNUNET_SCHEDULER_Handle *sched,
101 const struct sockaddr *addr, socklen_t addrlen,
104 struct GNUNET_NAT_NATPMP_Handle *nat;
106 nat = GNUNET_malloc (sizeof (struct GNUNET_NAT_NATPMP_Handle));
108 nat->state = NATPMP_DISCOVER;
111 nat->addrlen = addrlen;
116 GNUNET_NAT_NATPMP_close (struct 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)
142 char buf[INET6_ADDRSTRLEN];
144 struct sockaddr_in *v4;
145 struct sockaddr_in6 *v6;
148 /* Keep to NULL if address could not be found */
151 if (is_enabled && (nat->state == NATPMP_DISCOVER))
153 int val = initnatpmp (&nat->natpmp);
154 log_val ("initnatpmp", val);
155 val = sendpublicaddressrequest (&nat->natpmp);
156 log_val ("sendpublicaddressrequest", val);
157 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_PUB;
158 nat->has_discovered = 1;
159 set_command_time (nat);
162 if ((nat->state == NATPMP_RECV_PUB) && can_send_command (nat))
164 struct natpmpresp_t response;
165 const int val = readnatpmpresponseorretry (&nat->natpmp,
167 log_val ("readnatpmpresponseorretry", val);
170 if (NULL != nat->ext_addr)
172 GNUNET_free (nat->ext_addr);
173 nat->ext_addr = NULL;
176 if (response.pnu.publicaddress.family == AF_INET)
178 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
179 nat->ext_addr = (struct sockaddr *) v4;
180 v4->sin_family = AF_INET;
181 v4->sin_port = response.pnu.newportmapping.mappedpublicport;
182 memcpy (&v4->sin_addr, &response.pnu.publicaddress.addr,
183 sizeof (struct in_addr));
185 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
186 _("Found public IP address %s\n"),
188 &response.pnu.publicaddress.addr,
194 v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
195 nat->ext_addr = (struct sockaddr *) v6;
196 v6->sin6_family = AF_INET6;
197 v6->sin6_port = response.pnu.newportmapping.mappedpublicport;
198 memcpy (&v6->sin6_addr,
199 &response.pnu.publicaddress.addr6,
200 (sizeof (struct in6_addr)));
202 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
203 _("Found public IP address %s\n"),
205 &response.pnu.publicaddress.addr6,
209 *ext_addr = nat->ext_addr;
210 nat->state = NATPMP_IDLE;
212 else if (val != NATPMP_TRYAGAIN)
214 nat->state = NATPMP_ERR;
218 if ((nat->state == NATPMP_IDLE) || (nat->state == NATPMP_ERR))
220 if (nat->is_mapped && !is_enabled)
221 nat->state = NATPMP_SEND_UNMAP;
224 if ((nat->state == NATPMP_SEND_UNMAP) && can_send_command (nat))
227 sendnewportmappingrequest (&nat->natpmp, NATPMP_PROTOCOL_TCP,
228 nat->port, nat->port,
230 log_val ("sendnewportmappingrequest", val);
231 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_UNMAP;
232 set_command_time (nat);
235 if (nat->state == NATPMP_RECV_UNMAP)
237 struct natpmpresp_t resp;
238 const int val = readnatpmpresponseorretry (&nat->natpmp, &resp);
239 log_val ("readnatpmpresponseorretry", val);
242 const int p = resp.pnu.newportmapping.privateport;
243 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP,
244 _("No longer forwarding port %d\n"), p);
248 nat->state = NATPMP_IDLE;
252 else if (val != NATPMP_TRYAGAIN)
254 nat->state = NATPMP_ERR;
258 if (nat->state == NATPMP_IDLE)
260 if (is_enabled && !nat->is_mapped && nat->has_discovered)
261 nat->state = NATPMP_SEND_MAP;
263 else if (nat->is_mapped && time (NULL) >= nat->renew_time)
264 nat->state = NATPMP_SEND_MAP;
267 if ((nat->state == NATPMP_SEND_MAP) && can_send_command (nat))
270 sendnewportmappingrequest (&nat->natpmp, NATPMP_PROTOCOL_TCP,
274 log_val ("sendnewportmappingrequest", val);
275 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_MAP;
276 set_command_time (nat);
279 if (nat->state == NATPMP_RECV_MAP)
281 struct natpmpresp_t resp;
282 const int val = readnatpmpresponseorretry (&nat->natpmp, &resp);
283 log_val ("readnatpmpresponseorretry", val);
286 nat->state = NATPMP_IDLE;
288 nat->renew_time = time (NULL) + LIFETIME_SECS;
289 nat->port = resp.pnu.newportmapping.privateport;
290 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP,
291 _("Port %d forwarded successfully\n"), nat->port);
293 else if (val != NATPMP_TRYAGAIN)
295 nat->state = NATPMP_ERR;
303 nat->is_mapped ? GNUNET_NAT_PORT_MAPPED : GNUNET_NAT_PORT_UNMAPPED;
306 case NATPMP_DISCOVER:
307 ret = GNUNET_NAT_PORT_UNMAPPED;
310 case NATPMP_RECV_PUB:
311 case NATPMP_SEND_MAP:
312 case NATPMP_RECV_MAP:
313 ret = GNUNET_NAT_PORT_MAPPING;
316 case NATPMP_SEND_UNMAP:
317 case NATPMP_RECV_UNMAP:
318 ret = GNUNET_NAT_PORT_UNMAPPING;
322 ret = GNUNET_NAT_PORT_ERROR;
328 /* end of natpmp.c */