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"
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;
80 log_val (const char *func, int ret)
83 if (ret == NATPMP_TRYAGAIN)
84 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
85 COMP_NAT_NATPMP, _("%s retry (%d)\n"),
88 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
89 COMP_NAT_NATPMP, _("%s succeeded (%d)\n"),
92 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
94 "%s failed. natpmp returned %d (%s); errno is %d (%s)\n",
96 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;
115 GNUNET_NAT_NATPMP_close (GNUNET_NAT_NATPMP_Handle * nat)
119 closenatpmp (&nat->natpmp);
125 can_send_command (const struct GNUNET_NAT_NATPMP_Handle *nat)
127 return time (NULL) >= nat->command_time;
131 set_command_time (struct GNUNET_NAT_NATPMP_Handle *nat)
133 nat->command_time = time (NULL) + COMMAND_WAIT_SECS;
137 GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
138 struct sockaddr **ext_addr)
141 char buf[INET6_ADDRSTRLEN];
143 struct sockaddr_in *v4;
144 struct sockaddr_in6 *v6;
147 /* Keep to NULL if address could not be found */
150 if (is_enabled && (nat->state == NATPMP_DISCOVER))
152 int val = initnatpmp (&nat->natpmp);
153 log_val ("initnatpmp", val);
154 val = sendpublicaddressrequest (&nat->natpmp);
155 log_val ("sendpublicaddressrequest", val);
156 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_PUB;
157 nat->has_discovered = 1;
158 set_command_time (nat);
161 if ((nat->state == NATPMP_RECV_PUB) && can_send_command (nat))
163 natpmpresp_t response;
164 const int val = readnatpmpresponseorretry (&nat->natpmp,
166 log_val ("readnatpmpresponseorretry", val);
169 if (NULL != nat->ext_addr)
171 GNUNET_free (nat->ext_addr);
172 nat->ext_addr = NULL;
175 if (response.pnu.publicaddress.family == AF_INET)
177 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
178 nat->ext_addr = (struct sockaddr*) v4;
179 v4->sin_family = AF_INET;
180 v4->sin_port = response.pnu.newportmapping.mappedpublicport;
181 memcpy (&v4->sin_addr, &response.pnu.publicaddress.addr,
182 sizeof (struct in_addr));
184 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
185 _("Found public IP address %s\n"),
187 &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,
210 *ext_addr = nat->ext_addr;
211 nat->state = NATPMP_IDLE;
213 else if (val != NATPMP_TRYAGAIN)
215 nat->state = NATPMP_ERR;
219 if ((nat->state == NATPMP_IDLE) || (nat->state == NATPMP_ERR))
221 if (nat->is_mapped && !is_enabled)
222 nat->state = NATPMP_SEND_UNMAP;
225 if ((nat->state == NATPMP_SEND_UNMAP) && can_send_command (nat))
228 sendnewportmappingrequest (&nat->natpmp, NATPMP_PROTOCOL_TCP,
229 nat->port, nat->port,
231 log_val ("sendnewportmappingrequest", val);
232 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_UNMAP;
233 set_command_time (nat);
236 if (nat->state == NATPMP_RECV_UNMAP)
239 const int val = readnatpmpresponseorretry (&nat->natpmp, &resp);
240 log_val ("readnatpmpresponseorretry", val);
243 const int p = resp.pnu.newportmapping.privateport;
244 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP,
245 _("No longer forwarding port %d\n"), p);
249 nat->state = NATPMP_IDLE;
253 else if (val != NATPMP_TRYAGAIN)
255 nat->state = NATPMP_ERR;
259 if (nat->state == NATPMP_IDLE)
261 if (is_enabled && !nat->is_mapped && nat->has_discovered)
262 nat->state = NATPMP_SEND_MAP;
264 else if (nat->is_mapped && time (NULL) >= nat->renew_time)
265 nat->state = NATPMP_SEND_MAP;
268 if ((nat->state == NATPMP_SEND_MAP) && can_send_command (nat))
271 sendnewportmappingrequest (&nat->natpmp, NATPMP_PROTOCOL_TCP,
275 log_val ("sendnewportmappingrequest", val);
276 nat->state = val < 0 ? NATPMP_ERR : NATPMP_RECV_MAP;
277 set_command_time (nat);
280 if (nat->state == NATPMP_RECV_MAP)
283 const int val = readnatpmpresponseorretry (&nat->natpmp, &resp);
284 log_val ("readnatpmpresponseorretry", val);
287 nat->state = NATPMP_IDLE;
289 nat->renew_time = time (NULL) + LIFETIME_SECS;
290 nat->port = resp.pnu.newportmapping.privateport;
291 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP,
292 _("Port %d forwarded successfully\n"), nat->port);
294 else if (val != NATPMP_TRYAGAIN)
296 nat->state = NATPMP_ERR;
304 nat->is_mapped ? GNUNET_NAT_PORT_MAPPED : GNUNET_NAT_PORT_UNMAPPED;
307 case NATPMP_DISCOVER:
308 ret = GNUNET_NAT_PORT_UNMAPPED;
311 case NATPMP_RECV_PUB:
312 case NATPMP_SEND_MAP:
313 case NATPMP_RECV_MAP:
314 ret = GNUNET_NAT_PORT_MAPPING;
317 case NATPMP_SEND_UNMAP:
318 case NATPMP_RECV_UNMAP:
319 ret = GNUNET_NAT_PORT_UNMAPPING;
323 ret = GNUNET_NAT_PORT_ERROR;
329 /* end of natpmp.c */