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_NOT_ONLINE,
94 * `upnpc` command not found
96 GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
99 * Failed to run `upnpc` command
101 GNUNET_NAT_ERROR_UPNPC_FAILED,
104 * `upnpc' command took too long, process killed
106 GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
109 * `upnpc' command failed to establish port mapping
111 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
114 * `external-ip' command not found
116 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
119 * `external-ip' command not found
121 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_EXECUTEABLE,
124 * `external-ip' command not found
126 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID,
129 * "no valid address was returned by `external-ip'"
131 GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID,
134 * Could not determine interface with internal/local network address
136 GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO,
139 * No working gnunet-helper-nat-server found
141 GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND,
144 * NAT test could not be initialized
146 GNUNET_NAT_ERROR_NAT_TEST_START_FAILED,
151 GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT,
156 GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND,
167 * Attempt to enable port redirection and detect public IP address
168 * contacting UPnP or NAT-PMP routers on the local network. Use addr
169 * to specify to which of the local host's addresses should the
170 * external port be mapped. The port is taken from the corresponding
171 * sockaddr_in[6] field. The NAT module should call the given
172 * callback for any 'plausible' external address.
174 * @param cfg configuration to use
175 * @param is_tcp #GNUNET_YES for TCP, #GNUNET_NO for UDP
176 * @param adv_port advertised port (port we are either bound to or that our OS
177 * locally performs redirection from to our bound port).
178 * @param num_addrs number of addresses in @a addrs
179 * @param addrs list of local addresses packets should be redirected to
180 * @param addrlens actual lengths of the addresses in @a addrs
181 * @param address_callback function to call everytime the public IP address changes
182 * @param reversal_callback function to call if someone wants connection reversal from us,
183 * NULL if connection reversal is not supported
184 * @param callback_cls closure for callbacks
185 * @return NULL on error, otherwise handle that can be used to unregister
187 struct GNUNET_NAT_Handle *
188 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
191 unsigned int num_addrs,
192 const struct sockaddr **addrs,
193 const socklen_t *addrlens,
194 GNUNET_NAT_AddressCallback address_callback,
195 GNUNET_NAT_ReversalCallback reversal_callback,
200 * Test if the given address is (currently) a plausible IP address for
203 * @param h the handle returned by register
204 * @param addr IP address to test (IPv4 or IPv6)
205 * @param addrlen number of bytes in @a addr
206 * @return #GNUNET_YES if the address is plausible,
207 * #GNUNET_NO if the address is not plausible,
208 * #GNUNET_SYSERR if the address is malformed
211 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
217 * We learned about a peer (possibly behind NAT) so run the
218 * gnunet-nat-client to send dummy ICMP responses to cause
219 * that peer to connect to us (connection reversal).
221 * @param h handle (used for configuration)
222 * @param sa the address of the peer (IPv4-only)
223 * @return #GNUNET_SYSERR on error, #GNUNET_NO if nat client is disabled,
224 * #GNUNET_OK otherwise
227 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
228 const struct sockaddr_in *sa);
232 * Stop port redirection and public IP address detection for the given
233 * handle. This frees the handle, after having sent the needed
234 * commands to close open ports.
236 * @param h the handle to stop
239 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h);
243 * Handle to a NAT test.
245 struct GNUNET_NAT_Test;
249 * Function called to report success or failure for
250 * NAT configuration test.
253 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
255 typedef void (*GNUNET_NAT_TestCallback) (void *cls,
256 enum GNUNET_NAT_FailureCode result);
260 * Start testing if NAT traversal works using the
261 * given configuration (IPv4-only).
263 * @param cfg configuration for the NAT traversal
264 * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
265 * @param bnd_port port to bind to, 0 for connection reversal
266 * @param adv_port externally advertised port to use
267 * @param report function to call with the result of the test
268 * @param report_cls closure for @a report
269 * @return handle to cancel NAT test
271 struct GNUNET_NAT_Test *
272 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
276 GNUNET_NAT_TestCallback report,
281 * Stop an active NAT test.
283 * @param tst test to stop.
286 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
290 * Signature of a callback that is given an IP address.
293 * @param addr the address, NULL on errors
294 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
296 typedef void (*GNUNET_NAT_IPCallback) (void *cls,
297 const struct in_addr *addr,
298 enum GNUNET_NAT_FailureCode result);
303 * Opaque handle to cancel #GNUNET_NAT_mini_get_external_ipv4() operation.
305 struct GNUNET_NAT_ExternalHandle;
309 * Try to get the external IPv4 address of this peer.
311 * @param timeout when to fail
312 * @param cb function to call with result
313 * @param cb_cls closure for @a cb
314 * @return handle for cancellation (can only be used until @a cb is called), NULL on error
316 struct GNUNET_NAT_ExternalHandle *
317 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
318 GNUNET_NAT_IPCallback cb,
325 * @param eh operation to cancel
328 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh);
332 * Handle to a mapping created with upnpc.
334 struct GNUNET_NAT_MiniHandle;
338 * Signature of the callback passed to #GNUNET_NAT_register() for
339 * a function to call whenever our set of 'valid' addresses changes.
342 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
343 * the previous (now invalid) one, #GNUNET_SYSERR indicates an error
344 * @param addr either the previous or the new public IP address
345 * @param addrlen actual length of the @a addr
346 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
349 (*GNUNET_NAT_MiniAddressCallback) (void *cls,
351 const struct sockaddr *addr,
353 enum GNUNET_NAT_FailureCode result);
357 * Start mapping the given port using (mini)upnpc. This function
358 * should typically not be used directly (it is used within the
359 * general-purpose #GNUNET_NAT_register() code). However, it can be
360 * used if specifically UPnP-based NAT traversal is to be used or
363 * @param port port to map
364 * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
365 * @param ac function to call with mapping result
366 * @param ac_cls closure for @a ac
367 * @return NULL on error
369 struct GNUNET_NAT_MiniHandle *
370 GNUNET_NAT_mini_map_start (uint16_t port,
372 GNUNET_NAT_MiniAddressCallback ac,
377 * Remove a mapping created with (mini)upnpc. Calling
378 * this function will give 'upnpc' 1s to remove the mapping,
379 * so while this function is non-blocking, a task will be
380 * left with the scheduler for up to 1s past this call.
382 * @param mini the handle
385 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini);
389 * Handle to auto-configuration in progress.
391 struct GNUNET_NAT_AutoHandle;
395 * Function called with the result from the autoconfiguration.
398 * @param diff minimal suggested changes to the original configuration
399 * to make it work (as best as we can)
400 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
403 (*GNUNET_NAT_AutoResultCallback)(void *cls,
404 const struct GNUNET_CONFIGURATION_Handle *diff,
405 enum GNUNET_NAT_FailureCode result);
409 * Start auto-configuration routine. The resolver service should
410 * be available when this function is called.
412 * @param cfg initial configuration
413 * @param cb function to call with autoconfiguration result
414 * @param cb_cls closure for @a cb
415 * @return handle to cancel operation
417 struct GNUNET_NAT_AutoHandle *
418 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
419 GNUNET_NAT_AutoResultCallback cb,
424 * Abort autoconfiguration.
426 * @param ah handle for operation to abort
429 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah);
433 /* end of gnunet_nat_lib.h */