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 {
80 GNUNET_NAT_ERROR_SUCCESS = GNUNET_OK,
83 * `upnpc` command not found
85 GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
88 * Failed to run `upnpc` command
90 GNUNET_NAT_ERROR_UPNPC_FAILED,
93 * `upnpc' command took too long, process killed
95 GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
98 * `upnpc' command failed to establish port mapping
100 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
103 * `external-ip' command not found
105 GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
108 * "no valid address was returned by `external-ip'"
110 GNUNET_NAT_ERROR_EXTERNAL_IP_NO_VALID_ADDRESS_FOUND,
124 * Attempt to enable port redirection and detect public IP address
125 * contacting UPnP or NAT-PMP routers on the local network. Use addr
126 * to specify to which of the local host's addresses should the
127 * external port be mapped. The port is taken from the corresponding
128 * sockaddr_in[6] field. The NAT module should call the given
129 * callback for any 'plausible' external address.
131 * @param cfg configuration to use
132 * @param is_tcp #GNUNET_YES for TCP, #GNUNET_NO for UDP
133 * @param adv_port advertised port (port we are either bound to or that our OS
134 * locally performs redirection from to our bound port).
135 * @param num_addrs number of addresses in @a addrs
136 * @param addrs list of local addresses packets should be redirected to
137 * @param addrlens actual lengths of the addresses in @a addrs
138 * @param address_callback function to call everytime the public IP address changes
139 * @param reversal_callback function to call if someone wants connection reversal from us,
140 * NULL if connection reversal is not supported
141 * @param callback_cls closure for callbacks
142 * @return NULL on error, otherwise handle that can be used to unregister
144 struct GNUNET_NAT_Handle *
145 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
148 unsigned int num_addrs,
149 const struct sockaddr **addrs,
150 const socklen_t *addrlens,
151 GNUNET_NAT_AddressCallback address_callback,
152 GNUNET_NAT_ReversalCallback reversal_callback,
157 * Test if the given address is (currently) a plausible IP address for
160 * @param h the handle returned by register
161 * @param addr IP address to test (IPv4 or IPv6)
162 * @param addrlen number of bytes in @a addr
163 * @return #GNUNET_YES if the address is plausible,
164 * #GNUNET_NO if the address is not plausible,
165 * #GNUNET_SYSERR if the address is malformed
168 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
174 * We learned about a peer (possibly behind NAT) so run the
175 * gnunet-nat-client to send dummy ICMP responses to cause
176 * that peer to connect to us (connection reversal).
178 * @param h handle (used for configuration)
179 * @param sa the address of the peer (IPv4-only)
180 * @return #GNUNET_SYSERR on error, #GNUNET_NO if nat client is disabled,
181 * #GNUNET_OK otherwise
184 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
185 const struct sockaddr_in *sa);
189 * Stop port redirection and public IP address detection for the given
190 * handle. This frees the handle, after having sent the needed
191 * commands to close open ports.
193 * @param h the handle to stop
196 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h);
200 * Handle to a NAT test.
202 struct GNUNET_NAT_Test;
206 * Function called to report success or failure for
207 * NAT configuration test.
210 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
212 typedef void (*GNUNET_NAT_TestCallback) (void *cls,
213 enum GNUNET_NAT_FailureCode result);
217 * Start testing if NAT traversal works using the
218 * given configuration (IPv4-only).
220 * @param cfg configuration for the NAT traversal
221 * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
222 * @param bnd_port port to bind to, 0 for connection reversal
223 * @param adv_port externally advertised port to use
224 * @param report function to call with the result of the test
225 * @param report_cls closure for @a report
226 * @return handle to cancel NAT test
228 struct GNUNET_NAT_Test *
229 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
233 GNUNET_NAT_TestCallback report,
238 * Stop an active NAT test.
240 * @param tst test to stop.
243 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
247 * Signature of a callback that is given an IP address.
250 * @param addr the address, NULL on errors
251 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
253 typedef void (*GNUNET_NAT_IPCallback) (void *cls,
254 const struct in_addr *addr,
255 enum GNUNET_NAT_FailureCode result);
260 * Opaque handle to cancel #GNUNET_NAT_mini_get_external_ipv4() operation.
262 struct GNUNET_NAT_ExternalHandle;
266 * Try to get the external IPv4 address of this peer.
268 * @param timeout when to fail
269 * @param cb function to call with result
270 * @param cb_cls closure for @a cb
271 * @return handle for cancellation (can only be used until @a cb is called), NULL on error
273 struct GNUNET_NAT_ExternalHandle *
274 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
275 GNUNET_NAT_IPCallback cb,
282 * @param eh operation to cancel
285 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh);
289 * Handle to a mapping created with upnpc.
291 struct GNUNET_NAT_MiniHandle;
295 * Signature of the callback passed to #GNUNET_NAT_register() for
296 * a function to call whenever our set of 'valid' addresses changes.
299 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
300 * the previous (now invalid) one, #GNUNET_SYSERR indicates an error
301 * @param addr either the previous or the new public IP address
302 * @param addrlen actual length of the @a addr
303 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
306 (*GNUNET_NAT_MiniAddressCallback) (void *cls,
308 const struct sockaddr *addr,
310 enum GNUNET_NAT_FailureCode result);
314 * Start mapping the given port using (mini)upnpc. This function
315 * should typically not be used directly (it is used within the
316 * general-purpose #GNUNET_NAT_register() code). However, it can be
317 * used if specifically UPnP-based NAT traversal is to be used or
320 * @param port port to map
321 * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
322 * @param ac function to call with mapping result
323 * @param ac_cls closure for @a ac
324 * @return NULL on error
326 struct GNUNET_NAT_MiniHandle *
327 GNUNET_NAT_mini_map_start (uint16_t port,
329 GNUNET_NAT_MiniAddressCallback ac,
334 * Remove a mapping created with (mini)upnpc. Calling
335 * this function will give 'upnpc' 1s to remove the mapping,
336 * so while this function is non-blocking, a task will be
337 * left with the scheduler for up to 1s past this call.
339 * @param mini the handle
342 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini);
346 * Handle to auto-configuration in progress.
348 struct GNUNET_NAT_AutoHandle;
352 * Function called with the result from the autoconfiguration.
355 * @param diff minimal suggested changes to the original configuration
356 * to make it work (as best as we can)
357 * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
360 (*GNUNET_NAT_AutoResultCallback)(void *cls,
361 const struct GNUNET_CONFIGURATION_Handle *diff,
362 enum GNUNET_NAT_FailureCode result);
366 * Start auto-configuration routine. The resolver service should
367 * be available when this function is called.
369 * @param cfg initial configuration
370 * @param cb function to call with autoconfiguration result
371 * @param cb_cls closure for @a cb
372 * @return handle to cancel operation
374 struct GNUNET_NAT_AutoHandle *
375 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
376 GNUNET_NAT_AutoResultCallback cb,
381 * Abort autoconfiguration.
383 * @param ah handle for operation to abort
386 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah);
390 /* end of gnunet_nat_lib.h */