2 This file is part of GNUnet.
3 (C) 2007, 2008, 2009, 2010, 2011, 2012 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
26 * @author Milan Bouchet-Valat
29 #ifndef GNUNET_NAT_LIB_H
30 #define GNUNET_NAT_LIB_H
32 #include "gnunet_util_lib.h"
35 * Signature of the callback passed to GNUNET_NAT_register for
36 * a function to call whenever our set of 'valid' addresses changes.
39 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
40 * the previous (now invalid) one
41 * @param addr either the previous or the new public IP address
42 * @param addrlen actual lenght of the address
44 typedef void (*GNUNET_NAT_AddressCallback) (void *cls, int add_remove,
45 const struct sockaddr * addr,
50 * Signature of the callback passed to GNUNET_NAT_register
51 * for a function to call whenever someone asks us to do connection
55 * @param addr public IP address of the other peer
56 * @param addrlen actual lenght of the address
58 typedef void (*GNUNET_NAT_ReversalCallback) (void *cls,
59 const struct sockaddr * addr,
64 * Handle for active NAT registrations.
66 struct GNUNET_NAT_Handle;
70 * Attempt to enable port redirection and detect public IP address contacting
71 * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
72 * of the local host's addresses should the external port be mapped. The port
73 * is taken from the corresponding sockaddr_in[6] field. The NAT module
74 * should call the given callback for any 'plausible' external address.
76 * @param cfg configuration to use
77 * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
78 * @param adv_port advertised port (port we are either bound to or that our OS
79 * locally performs redirection from to our bound port).
80 * @param num_addrs number of addresses in 'addrs'
81 * @param addrs list of local addresses packets should be redirected to
82 * @param addrlens actual lengths of the addresses
83 * @param address_callback function to call everytime the public IP address changes
84 * @param reversal_callback function to call if someone wants connection reversal from us,
85 * NULL if connection reversal is not supported
86 * @param callback_cls closure for callback
87 * @return NULL on error, otherwise handle that can be used to unregister
89 struct GNUNET_NAT_Handle *
90 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, int is_tcp,
91 uint16_t adv_port, unsigned int num_addrs,
92 const struct sockaddr **addrs, const socklen_t * addrlens,
93 GNUNET_NAT_AddressCallback address_callback,
94 GNUNET_NAT_ReversalCallback reversal_callback,
99 * Test if the given address is (currently) a plausible IP address for this peer.
101 * @param h the handle returned by register
102 * @param addr IP address to test (IPv4 or IPv6)
103 * @param addrlen number of bytes in addr
104 * @return GNUNET_YES if the address is plausible,
105 * GNUNET_NO if the address is not plausible,
106 * GNUNET_SYSERR if the address is malformed
109 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h, const void *addr,
114 * We learned about a peer (possibly behind NAT) so run the
115 * gnunet-nat-client to send dummy ICMP responses to cause
116 * that peer to connect to us (connection reversal).
118 * @param h handle (used for configuration)
119 * @param sa the address of the peer (IPv4-only)
121 * @return GNUNET_SYSERR on error, GNUNET_NO if nat client is disabled,
122 * GNUNET_OK otherwise
125 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
126 const struct sockaddr_in *sa);
131 * Stop port redirection and public IP address detection for the given handle.
132 * This frees the handle, after having sent the needed commands to close open ports.
134 * @param h the handle to stop
137 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h);
141 * Handle to a NAT test.
143 struct GNUNET_NAT_Test;
146 * Function called to report success or failure for
147 * NAT configuration test.
150 * @param success GNUNET_OK on success, GNUNET_NO on failure,
151 * GNUNET_SYSERR if the test could not be
152 * properly started (internal failure)
154 typedef void (*GNUNET_NAT_TestCallback) (void *cls, int success);
157 * Start testing if NAT traversal works using the
158 * given configuration (IPv4-only).
160 * @param cfg configuration for the NAT traversal
161 * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
162 * @param bnd_port port to bind to, 0 for connection reversal
163 * @param adv_port externally advertised port to use
164 * @param report function to call with the result of the test
165 * @param report_cls closure for report
166 * @return handle to cancel NAT test
168 struct GNUNET_NAT_Test *
169 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
170 int is_tcp, uint16_t bnd_port, uint16_t adv_port,
171 GNUNET_NAT_TestCallback report, void *report_cls);
175 * Stop an active NAT test.
177 * @param tst test to stop.
180 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
184 * Signature of a callback that is given an IP address.
187 * @param addr the address, NULL on errors
189 typedef void (*GNUNET_NAT_IPCallback) (void *cls, const struct in_addr * addr);
194 * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation.
196 struct GNUNET_NAT_ExternalHandle;
200 * Try to get the external IPv4 address of this peer.
202 * @param timeout when to fail
203 * @param cb function to call with result
204 * @param cb_cls closure for 'cb'
205 * @return handle for cancellation (can only be used until 'cb' is called), NULL on error
207 struct GNUNET_NAT_ExternalHandle *
208 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
209 GNUNET_NAT_IPCallback cb, void *cb_cls);
215 * @param eh operation to cancel
218 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh);
222 * Handle to a mapping created with upnpc.
224 struct GNUNET_NAT_MiniHandle;
228 * Start mapping the given port using (mini)upnpc. This function
229 * should typically not be used directly (it is used within the
230 * general-purpose 'GNUNET_NAT_register' code). However, it can be
231 * used if specifically UPnP-based NAT traversal is to be used or
234 * @param port port to map
235 * @param is_tcp GNUNET_YES to map TCP, GNUNET_NO for UDP
236 * @param ac function to call with mapping result
237 * @param ac_cls closure for 'ac'
238 * @return NULL on error
240 struct GNUNET_NAT_MiniHandle *
241 GNUNET_NAT_mini_map_start (uint16_t port, int is_tcp,
242 GNUNET_NAT_AddressCallback ac, void *ac_cls);
246 * Remove a mapping created with (mini)upnpc. Calling
247 * this function will give 'upnpc' 1s to remove tha mapping,
248 * so while this function is non-blocking, a task will be
249 * left with the scheduler for up to 1s past this call.
251 * @param mini the handle
254 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini);
258 * Handle to auto-configuration in progress.
260 struct GNUNET_NAT_AutoHandle;
264 * Function called with the result from the autoconfiguration.
267 * @param diff minimal suggested changes to the original configuration
268 * to make it work (as best as we can)
270 typedef void (*GNUNET_NAT_AutoResultCallback)(void *cls,
271 const struct GNUNET_CONFIGURATION_Handle *diff);
275 * Start auto-configuration routine. The resolver service should
276 * be available when this function is called.
278 * @param cfg initial configuration
279 * @param cb function to call with autoconfiguration result
280 * @param cb_cls closure for cb
281 * @return handle to cancel operation
283 struct GNUNET_NAT_AutoHandle *
284 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
285 GNUNET_NAT_AutoResultCallback cb,
290 * Abort autoconfiguration.
292 * @param ah handle for operation to abort
295 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah);
299 /* end of gnunet_nat_lib.h */