2 This file is part of GNUnet.
3 Copyright (C) 2007-2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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
29 * @defgroup nat NAT library
30 * Service for handling UPnP and NAT-PMP port forwarding
31 * and external IP address retrieval
36 #ifndef GNUNET_NAT_SERVICE_H
37 #define GNUNET_NAT_SERVICE_H
39 #include "gnunet_util_lib.h"
43 * Some addresses contain sensitive information or are
44 * not suitable for global distribution. We use address
45 * classes to filter addresses by which domain they make
46 * sense to be used in. These are used in a bitmask.
48 * FIXME: might want to define this elsewhere; we have
49 * an equivalent enum in gnunet_transport_hello_service.h;
50 * might ultimately belong with the new HELLO definition.
52 enum GNUNET_NAT_AddressClass
57 GNUNET_NAT_AC_NONE = 0,
60 * Addresses that fall into no other category
61 * (i.e. incoming which we cannot use elsewhere).
63 GNUNET_NAT_AC_OTHER = 1,
66 * Flag for addresses that are highly sensitive
67 * (i.e. IPv6 with our MAC).
69 GNUNET_NAT_AC_PRIVATE = 2,
72 * Addresses that are global (i.e. IPv4).
74 GNUNET_NAT_AC_GLOBAL = 4,
77 * Addresses that are global and are sensitive
78 * (i.e. IPv6 with our MAC).
80 GNUNET_NAT_AC_GLOBAL_PRIVATE = 6,
83 * Addresses useful in the local wired network,
84 * i.e. a MAC. Sensitive, but obvious to people nearby.
86 * Useful for broadcasts.
88 GNUNET_NAT_AC_LAN = 8,
91 * Addresses useful in the local wired network,
92 * i.e. a MAC. Sensitive, but obvious to people nearby.
93 * Useful for broadcasts.
95 GNUNET_NAT_AC_LAN_PRIVATE = 10,
98 * Addresses useful in the local wireless network,
99 * i.e. a MAC. Sensitive, but obvious to people nearby.
100 * Useful for broadcasts.
102 GNUNET_NAT_AC_WLAN = 16,
105 * Addresses useful in the local bluetooth network. Sensitive, but
106 * obvious to people nearby. Useful for broadcasts.
108 GNUNET_NAT_AC_BT = 32,
111 * Loopback addresses, only useful under special cirumstances.
113 GNUNET_NAT_AC_LOOPBACK = 64,
116 * Addresses that should be our external IP address
117 * on the outside of a NAT. Might be incorrectly determined.
118 * Used as a bit in combination with #GNUNET_NAT_AC_GLOBAL,
119 * or in case of double-NAT with
120 * #GNUNET_NAT_AC_LAN.
122 GNUNET_NAT_AC_EXTERN = 128,
125 * Addresses that were manually configured by the user.
126 * Used as a bit in combination with #GNUNET_NAT_AC_GLOBAL.
128 GNUNET_NAT_AC_MANUAL = 256,
131 * Bitmask for "any" address.
133 GNUNET_NAT_AC_ANY = 65535
138 * Error Types for the NAT subsystem (which can then later be converted/resolved to a string)
140 enum GNUNET_NAT_StatusCode
145 GNUNET_NAT_ERROR_SUCCESS = GNUNET_OK,
150 GNUNET_NAT_ERROR_IPC_FAILURE,
153 * Failure in network subsystem, check permissions
155 GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR,
160 GNUNET_NAT_ERROR_TIMEOUT,
163 * detected that we are offline
165 GNUNET_NAT_ERROR_NOT_ONLINE,
168 * `upnpc` command not found
170 GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
173 * Failed to run `upnpc` command
175 GNUNET_NAT_ERROR_UPNPC_FAILED,
178 * `upnpc' command took too long, process killed
180 GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
183 * `upnpc' command failed to establish port mapping
185 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
188 * `external-ip' command not found
190 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
193 * Failed to run `external-ip` command
195 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED,
198 * `external-ip' command output invalid
200 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID,
203 * "no valid address was returned by `external-ip'"
205 GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID,
208 * Could not determine interface with internal/local network address
210 GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO,
213 * No working gnunet-helper-nat-server found
215 GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND,
218 * NAT test could not be initialized
220 GNUNET_NAT_ERROR_NAT_TEST_START_FAILED,
225 GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT,
228 * NAT test failed to initiate
230 GNUNET_NAT_ERROR_NAT_REGISTER_FAILED,
235 GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND
240 * What the situation of the NAT connectivity
245 * We have a direct connection
247 GNUNET_NAT_TYPE_NO_NAT = GNUNET_OK,
250 * We are under a NAT but cannot traverse it
252 GNUNET_NAT_TYPE_UNREACHABLE_NAT,
255 * We can traverse using STUN
257 GNUNET_NAT_TYPE_STUN_PUNCHED_NAT,
260 * We can traverse using UPNP
262 GNUNET_NAT_TYPE_UPNP_NAT,
265 * We know nothing about the NAT.
267 GNUNET_NAT_TYPE_UNKNOWN
272 * Signature of the callback passed to #GNUNET_NAT_register() for
273 * a function to call whenever our set of 'valid' addresses changes.
276 * @param app_ctx[in,out] location where the app can store stuff
277 * on add and retrieve it on remove
278 * @param add_remove #GNUNET_YES to add a new public IP address,
279 * #GNUNET_NO to remove a previous (now invalid) one
280 * @param ac address class the address belongs to
281 * @param addr either the previous or the new public IP address
282 * @param addrlen actual length of the @a addr
285 (*GNUNET_NAT_AddressCallback) (void *cls,
288 enum GNUNET_NAT_AddressClass ac,
289 const struct sockaddr *addr,
294 * Signature of the callback passed to #GNUNET_NAT_register().
295 * for a function to call whenever someone asks us to do connection
299 * @param remote_addr public IP address of the other peer
300 * @param remote_addrlen actual length of the @a remote_addr
303 (*GNUNET_NAT_ReversalCallback) (void *cls,
304 const struct sockaddr *remote_addr,
305 socklen_t remote_addrlen);
309 * Handle for active NAT registrations.
311 struct GNUNET_NAT_Handle;
315 * Attempt to enable port redirection and detect public IP address
316 * contacting UPnP or NAT-PMP routers on the local network. Use @a
317 * addr to specify to which of the local host's addresses should the
318 * external port be mapped. The port is taken from the corresponding
319 * sockaddr_in[6] field. The NAT module should call the given @a
320 * address_callback for any 'plausible' external address.
322 * @param cfg configuration to use
323 * @param config_section name of the configuration section for options
324 * @param proto protocol this is about, IPPROTO_TCP or IPPROTO_UDP
325 * @param num_addrs number of addresses in @a addrs
326 * @param addrs list of local addresses packets should be redirected to
327 * @param addrlens actual lengths of the addresses in @a addrs
328 * @param address_callback function to call everytime the public IP address changes
329 * @param reversal_callback function to call if someone wants connection reversal from us,
330 * NULL if connection reversal is not supported
331 * @param callback_cls closure for callbacks
332 * @return NULL on error, otherwise handle that can be used to unregister
334 struct GNUNET_NAT_Handle *
335 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
336 const char *config_section,
338 unsigned int num_addrs,
339 const struct sockaddr **addrs,
340 const socklen_t *addrlens,
341 GNUNET_NAT_AddressCallback address_callback,
342 GNUNET_NAT_ReversalCallback reversal_callback,
347 * Test if the given address is (currently) a plausible IP address for
348 * this peer. Mostly a convenience function so that clients do not
349 * have to explicitly track all IPs that the #GNUNET_NAT_AddressCallback
350 * has returned so far.
352 * @param nh the handle returned by register
353 * @param addr IP address to test (IPv4 or IPv6)
354 * @param addrlen number of bytes in @a addr
355 * @return #GNUNET_YES if the address is plausible,
356 * #GNUNET_NO if the address is not plausible,
357 * #GNUNET_SYSERR if the address is malformed
360 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
366 * We learned about a peer (possibly behind NAT) so run the
367 * gnunet-nat-client to send dummy ICMP responses to cause
368 * that peer to connect to us (connection reversal).
370 * @param nh handle (used for configuration)
371 * @param local_sa our local address of the peer (IPv4-only)
372 * @param remote_sa the remote address of the peer (IPv4-only)
373 * @return #GNUNET_SYSERR on error,
374 * #GNUNET_NO if connection reversal is unavailable,
375 * #GNUNET_OK otherwise (presumably in progress)
378 GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
379 const struct sockaddr_in *local_sa,
380 const struct sockaddr_in *remote_sa);
384 * Stop port redirection and public IP address detection for the given
385 * handle. This frees the handle, after having sent the needed
386 * commands to close open ports.
388 * @param nh the handle to unregister
391 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh);
395 * Handle an incoming STUN message. This function is useful as
396 * some GNUnet service may be listening on a UDP port and might
397 * thus receive STUN messages while trying to receive other data.
398 * In this case, this function can be used to process replies
401 * The function does some basic sanity checks on packet size and
402 * content, try to extract a bit of information.
404 * At the moment this only processes BIND requests, and returns the
405 * externally visible address of the request to the rest of the
408 * @param nh handle to the NAT service
409 * @param sender_addr address from which we got @a data
410 * @param sender_addr_len number of bytes in @a sender_addr
411 * @param data the packet
412 * @param data_size number of bytes in @a data
413 * @return #GNUNET_OK on success
414 * #GNUNET_NO if the packet is not a STUN packet
415 * #GNUNET_SYSERR on internal error handling the packet
418 GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
419 const struct sockaddr *sender_addr,
420 size_t sender_addr_len,
426 * Handle to a request given to the resolver. Can be used to cancel
427 * the request prior to the timeout or successful execution. Also
428 * used to track our internal state for the request.
430 struct GNUNET_NAT_STUN_Handle;
434 * Function called to report success or failure for
435 * NAT configuration test.
438 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
441 (*GNUNET_NAT_TestCallback) (void *cls,
442 enum GNUNET_NAT_StatusCode result);
446 * Make Generic STUN request. Sends a generic stun request to the
447 * server specified using the specified socket. If we do this,
448 * we need to watch for possible responses and call
449 * #GNUNET_NAT_stun_handle_packet() on incoming packets.
451 * @param server the address of the stun server
452 * @param port port of the stun server, in host byte order
453 * @param sock the socket used to send the request, must be a
455 * @param cb callback in case of error
456 * @param cb_cls closure for @a cb
457 * @return NULL on error
459 struct GNUNET_NAT_STUN_Handle *
460 GNUNET_NAT_stun_make_request (const char *server,
462 struct GNUNET_NETWORK_Handle *sock,
463 GNUNET_NAT_TestCallback cb,
468 * Cancel active STUN request. Frees associated resources
469 * and ensures that the callback is no longer invoked.
471 * @param rh request to cancel
474 GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh);
479 /** @} */ /* end of group */
481 /* end of gnunet_nat_service.h */