2 This file is part of GNUnet.
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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,
183 * Converts `enum GNUNET_NAT_StatusCode` to string
185 * @param err error code to resolve to a string
186 * @return point to a static string containing the error code
189 GNUNET_NAT_status2string (enum GNUNET_NAT_StatusCode err);
193 * Attempt to enable port redirection and detect public IP address
194 * contacting UPnP or NAT-PMP routers on the local network. Use addr
195 * to specify to which of the local host's addresses should the
196 * external port be mapped. The port is taken from the corresponding
197 * sockaddr_in[6] field. The NAT module should call the given
198 * callback for any 'plausible' external address.
200 * @param cfg configuration to use
201 * @param is_tcp #GNUNET_YES for TCP, #GNUNET_NO for UDP
202 * @param adv_port advertised port (port we are either bound to or that our OS
203 * locally performs redirection from to our bound port).
204 * @param num_addrs number of addresses in @a addrs
205 * @param addrs list of local addresses packets should be redirected to
206 * @param addrlens actual lengths of the addresses in @a addrs
207 * @param address_callback function to call everytime the public IP address changes
208 * @param reversal_callback function to call if someone wants connection reversal from us,
209 * NULL if connection reversal is not supported
210 * @param callback_cls closure for callbacks
211 * @return NULL on error, otherwise handle that can be used to unregister
213 struct GNUNET_NAT_Handle *
214 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
217 unsigned int num_addrs,
218 const struct sockaddr **addrs,
219 const socklen_t *addrlens,
220 GNUNET_NAT_AddressCallback address_callback,
221 GNUNET_NAT_ReversalCallback reversal_callback,
223 struct GNUNET_NETWORK_Handle* sock );
227 * Test if the given address is (currently) a plausible IP address for
230 * @param h the handle returned by register
231 * @param addr IP address to test (IPv4 or IPv6)
232 * @param addrlen number of bytes in @a addr
233 * @return #GNUNET_YES if the address is plausible,
234 * #GNUNET_NO if the address is not plausible,
235 * #GNUNET_SYSERR if the address is malformed
238 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
244 * We learned about a peer (possibly behind NAT) so run the
245 * gnunet-nat-client to send dummy ICMP responses to cause
246 * that peer to connect to us (connection reversal).
248 * @param h handle (used for configuration)
249 * @param sa the address of the peer (IPv4-only)
250 * @return #GNUNET_SYSERR on error, #GNUNET_NO if nat client is disabled,
251 * #GNUNET_OK otherwise
254 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
255 const struct sockaddr_in *sa);
259 * Stop port redirection and public IP address detection for the given
260 * handle. This frees the handle, after having sent the needed
261 * commands to close open ports.
263 * @param h the handle to stop
266 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h);
270 * Handle to a NAT test.
272 struct GNUNET_NAT_Test;
276 * Function called to report success or failure for
277 * NAT configuration test.
280 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
282 typedef void (*GNUNET_NAT_TestCallback) (void *cls,
283 enum GNUNET_NAT_StatusCode result);
287 * Start testing if NAT traversal works using the
288 * given configuration (IPv4-only).
290 * @param cfg configuration for the NAT traversal
291 * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
292 * @param bnd_port port to bind to, 0 for connection reversal
293 * @param adv_port externally advertised port to use
294 * @param timeout delay after which the test should be aborted
295 * @param report function to call with the result of the test;
296 * you still must call #GNUNET_NAT_test_stop().
297 * @param report_cls closure for @a report
298 * @return handle to cancel NAT test
300 struct GNUNET_NAT_Test *
301 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
305 struct GNUNET_TIME_Relative timeout,
306 GNUNET_NAT_TestCallback report,
311 * Stop an active NAT test.
313 * @param tst test to stop.
316 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
320 * Signature of a callback that is given an IP address.
323 * @param addr the address, NULL on errors
324 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
326 typedef void (*GNUNET_NAT_IPCallback) (void *cls,
327 const struct in_addr *addr,
328 enum GNUNET_NAT_StatusCode result);
333 * Opaque handle to cancel #GNUNET_NAT_mini_get_external_ipv4() operation.
335 struct GNUNET_NAT_ExternalHandle;
339 * Try to get the external IPv4 address of this peer.
341 * @param timeout when to fail
342 * @param cb function to call with result
343 * @param cb_cls closure for @a cb
344 * @return handle for cancellation (can only be used until @a cb is called), NULL on error
346 struct GNUNET_NAT_ExternalHandle *
347 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
348 GNUNET_NAT_IPCallback cb,
355 * @param eh operation to cancel
358 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh);
362 * Handle to a mapping created with upnpc.
364 struct GNUNET_NAT_MiniHandle;
368 * Signature of the callback passed to #GNUNET_NAT_register() for
369 * a function to call whenever our set of 'valid' addresses changes.
372 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
373 * the previous (now invalid) one, #GNUNET_SYSERR indicates an error
374 * @param addr either the previous or the new public IP address
375 * @param addrlen actual length of the @a addr
376 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
379 (*GNUNET_NAT_MiniAddressCallback) (void *cls,
381 const struct sockaddr *addr,
383 enum GNUNET_NAT_StatusCode result);
387 * Start mapping the given port using (mini)upnpc. This function
388 * should typically not be used directly (it is used within the
389 * general-purpose #GNUNET_NAT_register() code). However, it can be
390 * used if specifically UPnP-based NAT traversal is to be used or
393 * @param port port to map
394 * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
395 * @param ac function to call with mapping result
396 * @param ac_cls closure for @a ac
397 * @return NULL on error
399 struct GNUNET_NAT_MiniHandle *
400 GNUNET_NAT_mini_map_start (uint16_t port,
402 GNUNET_NAT_MiniAddressCallback ac,
407 * Remove a mapping created with (mini)upnpc. Calling
408 * this function will give 'upnpc' 1s to remove the mapping,
409 * so while this function is non-blocking, a task will be
410 * left with the scheduler for up to 1s past this call.
412 * @param mini the handle
415 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini);
419 * Handle to auto-configuration in progress.
421 struct GNUNET_NAT_AutoHandle;
425 * Function called with the result from the autoconfiguration.
428 * @param diff minimal suggested changes to the original configuration
429 * to make it work (as best as we can)
430 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
433 (*GNUNET_NAT_AutoResultCallback)(void *cls,
434 const struct GNUNET_CONFIGURATION_Handle *diff,
435 enum GNUNET_NAT_StatusCode result);
439 * Start auto-configuration routine. The resolver service should
440 * be available when this function is called.
442 * @param cfg initial configuration
443 * @param cb function to call with autoconfiguration result
444 * @param cb_cls closure for @a cb
445 * @return handle to cancel operation
447 struct GNUNET_NAT_AutoHandle *
448 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
449 GNUNET_NAT_AutoResultCallback cb,
454 * Abort autoconfiguration.
456 * @param ah handle for operation to abort
459 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah);
462 * Handle for active STUN Requests.
464 struct GNUNET_NAT_STUN_Handle;
470 * Function called with the result from NAT request.
473 * @param diff minimal suggested changes to the original configuration
474 * to make it work (as best as we can)
475 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
478 (*GNUNET_NAT_stun_RequestCallback)(void *cls,
479 enum GNUNET_NAT_StatusCode result);
483 * Make Generic STUN request and
484 * Send a generic stun request to the server specified using the specified socket.
485 * possibly waiting for a reply and filling the 'reply' field with
486 * the externally visible address.
489 * @param server, the address of the stun server
490 * @param port, port of the stun server
491 * @param sock the socket used to send the request
492 * @return GNUNET_NAT_STUN_Handle on success, NULL on error.
494 struct GNUNET_NAT_STUN_Handle *
495 GNUNET_NAT_stun_make_request(char * server, int port,
496 struct GNUNET_NETWORK_Handle * sock, GNUNET_NAT_stun_RequestCallback cb,
501 * Handle an incoming STUN message, Do some basic sanity checks on packet size and content,
502 * try to extract a bit of information, and possibly reply.
503 * At the moment this only processes BIND requests, and returns
504 * the externally visible address of the request.
505 * If a callback is specified, invoke it with the attribute.
507 * @param data, pointer where we will set the type
508 * @param len, pointer where we will set the type
509 * @param st, pointer where we will set the type
511 * @return, 0 on IGNORE, -1 if the packet is invalid ( not a stun packet)
514 GNUNET_NAT_stun_handle_packet(const uint8_t *data, size_t len,struct sockaddr_in *arg);
517 * CHECK if is a valid STUN packet sending to GNUNET_NAT_stun_handle_packet
519 * @param cls, NAT callback
520 * @param data, pointer where we will set the type
521 * @param len, pointer where we will set the type
522 * @param st, pointer where we will set the type
524 * @return, 0 on IGNORE, -1 if the packet is invalid ( not a stun packet)
527 GNUNET_NAT_try_decode_stun_packet(void *cls, const uint8_t *data, size_t len);
533 /* end of gnunet_nat_lib.h */