2 This file is part of GNUnet.
3 (C) 2007-2014 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 * @file include/gnunet_nat_lib.h
23 * @brief Library handling UPnP and NAT-PMP port forwarding and
24 * external IP address retrieval
25 * @author Christian Grothoff
26 * @author Milan Bouchet-Valat
29 #ifndef GNUNET_NAT_LIB_H
30 #define GNUNET_NAT_LIB_H
32 #include "gnunet_util_lib.h"
36 * Signature of the callback passed to #GNUNET_NAT_register() for
37 * a function to call whenever our set of 'valid' addresses changes.
40 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
41 * the previous (now invalid) one
42 * @param addr either the previous or the new public IP address
43 * @param addrlen actual length of the @a addr
46 (*GNUNET_NAT_AddressCallback) (void *cls,
48 const struct sockaddr *addr,
53 * Signature of the callback passed to #GNUNET_NAT_register().
54 * for a function to call whenever someone asks us to do connection
58 * @param addr public IP address of the other peer
59 * @param addrlen actual lenght of the @a addr
62 (*GNUNET_NAT_ReversalCallback) (void *cls,
63 const struct sockaddr *addr,
68 * Handle for active NAT registrations.
70 struct GNUNET_NAT_Handle;
74 * Error Types for the NAT subsystem (which can then later be converted/resolved to a string)
76 enum GNUNET_NAT_StatusCode
81 GNUNET_NAT_ERROR_SUCCESS = GNUNET_OK,
86 GNUNET_NAT_ERROR_IPC_FAILURE,
89 * Failure in network subsystem, check permissions
91 GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR,
96 GNUNET_NAT_ERROR_TIMEOUT,
99 * detected that we are offline
101 GNUNET_NAT_ERROR_NOT_ONLINE,
104 * `upnpc` command not found
106 GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
109 * Failed to run `upnpc` command
111 GNUNET_NAT_ERROR_UPNPC_FAILED,
114 * `upnpc' command took too long, process killed
116 GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
119 * `upnpc' command failed to establish port mapping
121 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
124 * `external-ip' command not found
126 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
129 * Failed to run `external-ip` command
131 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED,
134 * `external-ip' command output invalid
136 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID,
139 * "no valid address was returned by `external-ip'"
141 GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID,
144 * Could not determine interface with internal/local network address
146 GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO,
149 * No working gnunet-helper-nat-server found
151 GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND,
154 * NAT test could not be initialized
156 GNUNET_NAT_ERROR_NAT_TEST_START_FAILED,
161 GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT,
164 * NAT test failed to initiate
166 GNUNET_NAT_ERROR_NAT_REGISTER_FAILED,
171 GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND,
182 * Converts enum GNUNET_NAT_StatusCode to string
184 * @param err error code to resolve to a string
185 * @return point to a static string containing the error code
188 GNUNET_NAT_status2string (enum GNUNET_NAT_StatusCode err);
191 * Attempt to enable port redirection and detect public IP address
192 * contacting UPnP or NAT-PMP routers on the local network. Use addr
193 * to specify to which of the local host's addresses should the
194 * external port be mapped. The port is taken from the corresponding
195 * sockaddr_in[6] field. The NAT module should call the given
196 * callback for any 'plausible' external address.
198 * @param cfg configuration to use
199 * @param is_tcp #GNUNET_YES for TCP, #GNUNET_NO for UDP
200 * @param adv_port advertised port (port we are either bound to or that our OS
201 * locally performs redirection from to our bound port).
202 * @param num_addrs number of addresses in @a addrs
203 * @param addrs list of local addresses packets should be redirected to
204 * @param addrlens actual lengths of the addresses in @a addrs
205 * @param address_callback function to call everytime the public IP address changes
206 * @param reversal_callback function to call if someone wants connection reversal from us,
207 * NULL if connection reversal is not supported
208 * @param callback_cls closure for callbacks
209 * @return NULL on error, otherwise handle that can be used to unregister
211 struct GNUNET_NAT_Handle *
212 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
215 unsigned int num_addrs,
216 const struct sockaddr **addrs,
217 const socklen_t *addrlens,
218 GNUNET_NAT_AddressCallback address_callback,
219 GNUNET_NAT_ReversalCallback reversal_callback,
224 * Test if the given address is (currently) a plausible IP address for
227 * @param h the handle returned by register
228 * @param addr IP address to test (IPv4 or IPv6)
229 * @param addrlen number of bytes in @a addr
230 * @return #GNUNET_YES if the address is plausible,
231 * #GNUNET_NO if the address is not plausible,
232 * #GNUNET_SYSERR if the address is malformed
235 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
241 * We learned about a peer (possibly behind NAT) so run the
242 * gnunet-nat-client to send dummy ICMP responses to cause
243 * that peer to connect to us (connection reversal).
245 * @param h handle (used for configuration)
246 * @param sa the address of the peer (IPv4-only)
247 * @return #GNUNET_SYSERR on error, #GNUNET_NO if nat client is disabled,
248 * #GNUNET_OK otherwise
251 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
252 const struct sockaddr_in *sa);
256 * Stop port redirection and public IP address detection for the given
257 * handle. This frees the handle, after having sent the needed
258 * commands to close open ports.
260 * @param h the handle to stop
263 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h);
267 * Handle to a NAT test.
269 struct GNUNET_NAT_Test;
273 * Function called to report success or failure for
274 * NAT configuration test.
277 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
279 typedef void (*GNUNET_NAT_TestCallback) (void *cls,
280 enum GNUNET_NAT_StatusCode result);
284 * Start testing if NAT traversal works using the
285 * given configuration (IPv4-only).
287 * @param cfg configuration for the NAT traversal
288 * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
289 * @param bnd_port port to bind to, 0 for connection reversal
290 * @param adv_port externally advertised port to use
291 * @param timeout delay after which the test should be aborted
292 * @param report function to call with the result of the test
293 * @param report_cls closure for @a report
294 * @return handle to cancel NAT test
296 struct GNUNET_NAT_Test *
297 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
301 struct GNUNET_TIME_Relative timeout,
302 GNUNET_NAT_TestCallback report,
307 * Stop an active NAT test.
309 * @param tst test to stop.
312 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
316 * Signature of a callback that is given an IP address.
319 * @param addr the address, NULL on errors
320 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
322 typedef void (*GNUNET_NAT_IPCallback) (void *cls,
323 const struct in_addr *addr,
324 enum GNUNET_NAT_StatusCode result);
329 * Opaque handle to cancel #GNUNET_NAT_mini_get_external_ipv4() operation.
331 struct GNUNET_NAT_ExternalHandle;
335 * Try to get the external IPv4 address of this peer.
337 * @param timeout when to fail
338 * @param cb function to call with result
339 * @param cb_cls closure for @a cb
340 * @return handle for cancellation (can only be used until @a cb is called), NULL on error
342 struct GNUNET_NAT_ExternalHandle *
343 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
344 GNUNET_NAT_IPCallback cb,
351 * @param eh operation to cancel
354 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh);
358 * Handle to a mapping created with upnpc.
360 struct GNUNET_NAT_MiniHandle;
364 * Signature of the callback passed to #GNUNET_NAT_register() for
365 * a function to call whenever our set of 'valid' addresses changes.
368 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
369 * the previous (now invalid) one, #GNUNET_SYSERR indicates an error
370 * @param addr either the previous or the new public IP address
371 * @param addrlen actual length of the @a addr
372 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
375 (*GNUNET_NAT_MiniAddressCallback) (void *cls,
377 const struct sockaddr *addr,
379 enum GNUNET_NAT_StatusCode result);
383 * Start mapping the given port using (mini)upnpc. This function
384 * should typically not be used directly (it is used within the
385 * general-purpose #GNUNET_NAT_register() code). However, it can be
386 * used if specifically UPnP-based NAT traversal is to be used or
389 * @param port port to map
390 * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
391 * @param ac function to call with mapping result
392 * @param ac_cls closure for @a ac
393 * @return NULL on error
395 struct GNUNET_NAT_MiniHandle *
396 GNUNET_NAT_mini_map_start (uint16_t port,
398 GNUNET_NAT_MiniAddressCallback ac,
403 * Remove a mapping created with (mini)upnpc. Calling
404 * this function will give 'upnpc' 1s to remove the mapping,
405 * so while this function is non-blocking, a task will be
406 * left with the scheduler for up to 1s past this call.
408 * @param mini the handle
411 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini);
415 * Handle to auto-configuration in progress.
417 struct GNUNET_NAT_AutoHandle;
421 * Function called with the result from the autoconfiguration.
424 * @param diff minimal suggested changes to the original configuration
425 * to make it work (as best as we can)
426 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
429 (*GNUNET_NAT_AutoResultCallback)(void *cls,
430 const struct GNUNET_CONFIGURATION_Handle *diff,
431 enum GNUNET_NAT_StatusCode result);
435 * Start auto-configuration routine. The resolver service should
436 * be available when this function is called.
438 * @param cfg initial configuration
439 * @param cb function to call with autoconfiguration result
440 * @param cb_cls closure for @a cb
441 * @return handle to cancel operation
443 struct GNUNET_NAT_AutoHandle *
444 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
445 GNUNET_NAT_AutoResultCallback cb,
450 * Abort autoconfiguration.
452 * @param ah handle for operation to abort
455 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah);
459 /* end of gnunet_nat_lib.h */