2 This file is part of GNUnet.
3 Copyright (C) 2007-2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @author Christian Grothoff
23 * @author Milan Bouchet-Valat
26 * Service for handling UPnP and NAT-PMP port forwarding
27 * and external IP address retrieval
30 #include "gnunet_nat_service.h"
34 * Handle for active NAT registrations.
36 struct GNUNET_NAT_Handle
40 * Configuration we use.
42 const struct GNUNET_CONFIGURATION_Handle *cfg;
45 * Message queue for communicating with the NAT service.
47 struct GNUNET_MQ_Handle *mq;
50 * Our registration message.
52 struct GNUNET_MessageHeader *reg;
55 * Function to call when our addresses change.
57 GNUNET_NAT_AddressCallback address_callback;
60 * Function to call when another peer requests connection reversal.
62 GNUNET_NAT_ReversalCallback reversal_callback;
65 * Closure for the various callbacks.
73 * Attempt to enable port redirection and detect public IP address
74 * contacting UPnP or NAT-PMP routers on the local network. Use @a
75 * addr to specify to which of the local host's addresses should the
76 * external port be mapped. The port is taken from the corresponding
77 * sockaddr_in[6] field. The NAT module should call the given @a
78 * address_callback for any 'plausible' external address.
80 * @param cfg configuration to use
81 * @param proto protocol this is about, IPPROTO_TCP or IPPROTO_UDP
82 * @param adv_port advertised port (port we are either bound to or that our OS
83 * locally performs redirection from to our bound port).
84 * @param num_addrs number of addresses in @a addrs
85 * @param addrs list of local addresses packets should be redirected to
86 * @param addrlens actual lengths of the addresses in @a addrs
87 * @param address_callback function to call everytime the public IP address changes
88 * @param reversal_callback function to call if someone wants connection reversal from us,
89 * NULL if connection reversal is not supported
90 * @param callback_cls closure for callbacks
91 * @return NULL on error, otherwise handle that can be used to unregister
93 struct GNUNET_NAT_Handle *
94 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
97 unsigned int num_addrs,
98 const struct sockaddr **addrs,
99 const socklen_t *addrlens,
100 GNUNET_NAT_AddressCallback address_callback,
101 GNUNET_NAT_ReversalCallback reversal_callback,
104 struct GNUNET_NAT_Handle *nh = GNUNET_new (struct GNUNET_NAT_Handle);
107 nh->address_callback = address_callback;
108 nh->reversal_callback = reversal_callback;
109 nh->callback_cls = callback_cls;
116 * Handle an incoming STUN message. This function is useful as
117 * some GNUnet service may be listening on a UDP port and might
118 * thus receive STUN messages while trying to receive other data.
119 * In this case, this function can be used to act as a proper
120 * STUN server (if desired).
122 * The function does some basic sanity checks on packet size and
123 * content, try to extract a bit of information, and possibly replies
124 * if this is an actual STUN message.
126 * At the moment this only processes BIND requests, and returns the
127 * externally visible address of the request.
129 * @param nh handle to the NAT service
130 * @param sender_addr address from which we got @a data
131 * @param data the packet
132 * @param data_size number of bytes in @a data
133 * @return #GNUNET_OK on success
134 * #GNUNET_NO if the packet is not a STUN packet
135 * #GNUNET_SYSERR on internal error handling the packet
138 GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
139 const struct sockaddr *sender_addr,
144 return GNUNET_SYSERR;
149 * Test if the given address is (currently) a plausible IP address for
150 * this peer. Mostly a convenience function so that clients do not
151 * have to explicitly track all IPs that the #GNUNET_NAT_AddressCallback
152 * has returned so far.
154 * @param nh the handle returned by register
155 * @param addr IP address to test (IPv4 or IPv6)
156 * @param addrlen number of bytes in @a addr
157 * @return #GNUNET_YES if the address is plausible,
158 * #GNUNET_NO if the address is not plausible,
159 * #GNUNET_SYSERR if the address is malformed
162 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
167 return GNUNET_SYSERR;
172 * We learned about a peer (possibly behind NAT) so run the
173 * gnunet-nat-client to send dummy ICMP responses to cause
174 * that peer to connect to us (connection reversal).
176 * @param nh handle (used for configuration)
177 * @param local_sa our local address of the peer (IPv4-only)
178 * @param remote_sa the remote address of the peer (IPv4-only)
179 * @return #GNUNET_SYSERR on error,
180 * #GNUNET_NO if connection reversal is unavailable,
181 * #GNUNET_OK otherwise (presumably in progress)
184 GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
185 const struct sockaddr_in *local_sa,
186 const struct sockaddr_in *remote_sa)
189 return GNUNET_SYSERR;
194 * Stop port redirection and public IP address detection for the given
195 * handle. This frees the handle, after having sent the needed
196 * commands to close open ports.
198 * @param nh the handle to stop
201 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh)
203 GNUNET_MQ_destroy (nh->mq);
204 GNUNET_free (nh->reg);
210 * Handle to a NAT test.
212 struct GNUNET_NAT_Test
216 * Configuration we use.
218 const struct GNUNET_CONFIGURATION_Handle *cfg;
221 * Message queue for communicating with the NAT service.
223 struct GNUNET_MQ_Handle *mq;
226 * Function called to report success or failure for
227 * NAT configuration test.
229 GNUNET_NAT_TestCallback cb;
240 * Start testing if NAT traversal works using the given configuration
241 * (IPv4-only). The transport adapters should be down while using
244 * @param cfg configuration for the NAT traversal
245 * @param proto protocol to test, i.e. IPPROTO_TCP or IPPROTO_UDP
246 * @param bind_ip IPv4 address to bind to
247 * @param bnd_port port to bind to, 0 to test connection reversal
248 * @param extern_ip IPv4 address to externally advertise
249 * @param extern_port externally advertised port to use
250 * @param report function to call with the result of the test
251 * @param report_cls closure for @a report
252 * @return handle to cancel NAT test
254 struct GNUNET_NAT_Test *
255 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
257 struct in_addr bind_ip,
259 struct in_addr extern_ip,
260 uint16_t extern_port,
261 GNUNET_NAT_TestCallback report,
264 struct GNUNET_NAT_Test *tst = GNUNET_new (struct GNUNET_NAT_Test);
267 tst->cb_cls = report_cls;
274 * Stop an active NAT test.
276 * @param tst test to stop.
279 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
282 GNUNET_MQ_destroy (tst->mq);
288 * Handle to auto-configuration in progress.
290 struct GNUNET_NAT_AutoHandle
294 * Configuration we use.
296 const struct GNUNET_CONFIGURATION_Handle *cfg;
299 * Message queue for communicating with the NAT service.
301 struct GNUNET_MQ_Handle *mq;
304 * Function called with the result from the autoconfiguration.
306 GNUNET_NAT_AutoResultCallback arc;
309 * Closure for @e arc.
317 * Converts `enum GNUNET_NAT_StatusCode` to string
319 * @param err error code to resolve to a string
320 * @return point to a static string containing the error code
323 GNUNET_NAT_status2string (enum GNUNET_NAT_StatusCode err)
331 * Start auto-configuration routine. The transport adapters should
332 * be stopped while this function is called.
334 * @param cfg initial configuration
335 * @param cb function to call with autoconfiguration result
336 * @param cb_cls closure for @a cb
337 * @return handle to cancel operation
339 struct GNUNET_NAT_AutoHandle *
340 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
341 GNUNET_NAT_AutoResultCallback cb,
344 struct GNUNET_NAT_AutoHandle *ah = GNUNET_new (struct GNUNET_NAT_AutoHandle);
348 ah->arc_cls = cb_cls;
355 * Abort autoconfiguration.
357 * @param ah handle for operation to abort
360 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah)
363 GNUNET_MQ_destroy (ah->mq);
367 /* end of nat_api.c */