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_FailureCode
81 GNUNET_NAT_ERROR_SUCCESS = GNUNET_OK,
84 * `external-ip' command not found
86 GNUNET_NAT_ERROR_IPC_FAILURE,
89 * `external-ip' command not found
91 GNUNET_NAT_ERROR_TIMEOUT,
94 * `external-ip' command not found
96 GNUNET_NAT_ERROR_NOT_ONLINE,
99 * `upnpc` command not found
101 GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
104 * Failed to run `upnpc` command
106 GNUNET_NAT_ERROR_UPNPC_FAILED,
109 * `upnpc' command took too long, process killed
111 GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
114 * `upnpc' command failed to establish port mapping
116 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
119 * `external-ip' command not found
121 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
124 * `external-ip' command not found
126 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_EXECUTEABLE,
129 * `external-ip' command not found
131 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID,
134 * "no valid address was returned by `external-ip'"
136 GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID,
139 * Could not determine interface with internal/local network address
141 GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO,
144 * No working gnunet-helper-nat-server found
146 GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND,
149 * NAT test could not be initialized
151 GNUNET_NAT_ERROR_NAT_TEST_START_FAILED,
156 GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT,
161 GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND,
172 * Attempt to enable port redirection and detect public IP address
173 * contacting UPnP or NAT-PMP routers on the local network. Use addr
174 * to specify to which of the local host's addresses should the
175 * external port be mapped. The port is taken from the corresponding
176 * sockaddr_in[6] field. The NAT module should call the given
177 * callback for any 'plausible' external address.
179 * @param cfg configuration to use
180 * @param is_tcp #GNUNET_YES for TCP, #GNUNET_NO for UDP
181 * @param adv_port advertised port (port we are either bound to or that our OS
182 * locally performs redirection from to our bound port).
183 * @param num_addrs number of addresses in @a addrs
184 * @param addrs list of local addresses packets should be redirected to
185 * @param addrlens actual lengths of the addresses in @a addrs
186 * @param address_callback function to call everytime the public IP address changes
187 * @param reversal_callback function to call if someone wants connection reversal from us,
188 * NULL if connection reversal is not supported
189 * @param callback_cls closure for callbacks
190 * @return NULL on error, otherwise handle that can be used to unregister
192 struct GNUNET_NAT_Handle *
193 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
196 unsigned int num_addrs,
197 const struct sockaddr **addrs,
198 const socklen_t *addrlens,
199 GNUNET_NAT_AddressCallback address_callback,
200 GNUNET_NAT_ReversalCallback reversal_callback,
205 * Test if the given address is (currently) a plausible IP address for
208 * @param h the handle returned by register
209 * @param addr IP address to test (IPv4 or IPv6)
210 * @param addrlen number of bytes in @a addr
211 * @return #GNUNET_YES if the address is plausible,
212 * #GNUNET_NO if the address is not plausible,
213 * #GNUNET_SYSERR if the address is malformed
216 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
222 * We learned about a peer (possibly behind NAT) so run the
223 * gnunet-nat-client to send dummy ICMP responses to cause
224 * that peer to connect to us (connection reversal).
226 * @param h handle (used for configuration)
227 * @param sa the address of the peer (IPv4-only)
228 * @return #GNUNET_SYSERR on error, #GNUNET_NO if nat client is disabled,
229 * #GNUNET_OK otherwise
232 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
233 const struct sockaddr_in *sa);
237 * Stop port redirection and public IP address detection for the given
238 * handle. This frees the handle, after having sent the needed
239 * commands to close open ports.
241 * @param h the handle to stop
244 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h);
248 * Handle to a NAT test.
250 struct GNUNET_NAT_Test;
254 * Function called to report success or failure for
255 * NAT configuration test.
258 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
260 typedef void (*GNUNET_NAT_TestCallback) (void *cls,
261 enum GNUNET_NAT_FailureCode result);
265 * Start testing if NAT traversal works using the
266 * given configuration (IPv4-only).
268 * @param cfg configuration for the NAT traversal
269 * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
270 * @param bnd_port port to bind to, 0 for connection reversal
271 * @param adv_port externally advertised port to use
272 * @param timeout delay after which the test should be aborted
273 * @param report function to call with the result of the test
274 * @param report_cls closure for @a report
275 * @return handle to cancel NAT test
277 struct GNUNET_NAT_Test *
278 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
282 struct GNUNET_TIME_Relative timeout,
283 GNUNET_NAT_TestCallback report,
288 * Stop an active NAT test.
290 * @param tst test to stop.
293 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
297 * Signature of a callback that is given an IP address.
300 * @param addr the address, NULL on errors
301 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
303 typedef void (*GNUNET_NAT_IPCallback) (void *cls,
304 const struct in_addr *addr,
305 enum GNUNET_NAT_FailureCode result);
310 * Opaque handle to cancel #GNUNET_NAT_mini_get_external_ipv4() operation.
312 struct GNUNET_NAT_ExternalHandle;
316 * Try to get the external IPv4 address of this peer.
318 * @param timeout when to fail
319 * @param cb function to call with result
320 * @param cb_cls closure for @a cb
321 * @return handle for cancellation (can only be used until @a cb is called), NULL on error
323 struct GNUNET_NAT_ExternalHandle *
324 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
325 GNUNET_NAT_IPCallback cb,
332 * @param eh operation to cancel
335 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh);
339 * Handle to a mapping created with upnpc.
341 struct GNUNET_NAT_MiniHandle;
345 * Signature of the callback passed to #GNUNET_NAT_register() for
346 * a function to call whenever our set of 'valid' addresses changes.
349 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
350 * the previous (now invalid) one, #GNUNET_SYSERR indicates an error
351 * @param addr either the previous or the new public IP address
352 * @param addrlen actual length of the @a addr
353 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
356 (*GNUNET_NAT_MiniAddressCallback) (void *cls,
358 const struct sockaddr *addr,
360 enum GNUNET_NAT_FailureCode result);
364 * Start mapping the given port using (mini)upnpc. This function
365 * should typically not be used directly (it is used within the
366 * general-purpose #GNUNET_NAT_register() code). However, it can be
367 * used if specifically UPnP-based NAT traversal is to be used or
370 * @param port port to map
371 * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
372 * @param ac function to call with mapping result
373 * @param ac_cls closure for @a ac
374 * @return NULL on error
376 struct GNUNET_NAT_MiniHandle *
377 GNUNET_NAT_mini_map_start (uint16_t port,
379 GNUNET_NAT_MiniAddressCallback ac,
384 * Remove a mapping created with (mini)upnpc. Calling
385 * this function will give 'upnpc' 1s to remove the mapping,
386 * so while this function is non-blocking, a task will be
387 * left with the scheduler for up to 1s past this call.
389 * @param mini the handle
392 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini);
396 * Handle to auto-configuration in progress.
398 struct GNUNET_NAT_AutoHandle;
402 * Function called with the result from the autoconfiguration.
405 * @param diff minimal suggested changes to the original configuration
406 * to make it work (as best as we can)
407 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
410 (*GNUNET_NAT_AutoResultCallback)(void *cls,
411 const struct GNUNET_CONFIGURATION_Handle *diff,
412 enum GNUNET_NAT_FailureCode result);
416 * Start auto-configuration routine. The resolver service should
417 * be available when this function is called.
419 * @param cfg initial configuration
420 * @param cb function to call with autoconfiguration result
421 * @param cb_cls closure for @a cb
422 * @return handle to cancel operation
424 struct GNUNET_NAT_AutoHandle *
425 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
426 GNUNET_NAT_AutoResultCallback cb,
431 * Abort autoconfiguration.
433 * @param ah handle for operation to abort
436 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah);
440 /* end of gnunet_nat_lib.h */