-fix install
[oweals/gnunet.git] / src / include / gnunet_nat_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2007-2014 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
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
27  */
28
29 #ifndef GNUNET_NAT_LIB_H
30 #define GNUNET_NAT_LIB_H
31
32 #include "gnunet_util_lib.h"
33
34
35 /**
36  * Signature of the callback passed to #GNUNET_NAT_register() for
37  * a function to call whenever our set of 'valid' addresses changes.
38  *
39  * @param cls closure
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
44  */
45 typedef void
46 (*GNUNET_NAT_AddressCallback) (void *cls,
47                                int add_remove,
48                                const struct sockaddr *addr,
49                                socklen_t addrlen);
50
51
52 /**
53  * Signature of the callback passed to #GNUNET_NAT_register().
54  * for a function to call whenever someone asks us to do connection
55  * reversal.
56  *
57  * @param cls closure
58  * @param addr public IP address of the other peer
59  * @param addrlen actual lenght of the @a addr
60  */
61 typedef void
62 (*GNUNET_NAT_ReversalCallback) (void *cls,
63                                 const struct sockaddr *addr,
64                                 socklen_t addrlen);
65
66
67 /**
68  * Handle for active NAT registrations.
69  */
70 struct GNUNET_NAT_Handle;
71
72
73 /**
74  * Error Types for the NAT subsystem (which can then later be converted/resolved to a string)
75  */
76 enum GNUNET_NAT_FailureCode 
77 {
78   /**
79    * Just the default
80    */
81   GNUNET_NAT_ERROR_SUCCESS = GNUNET_OK,
82   
83   /**
84    * `upnpc` command not found
85    */
86   GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
87   
88   /**
89    * Failed to run `upnpc` command
90    */
91   GNUNET_NAT_ERROR_UPNPC_FAILED,
92   
93   /**
94    * `upnpc' command took too long, process killed
95    */
96   GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
97   
98   /**
99    * `upnpc' command failed to establish port mapping
100    */
101   GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
102   
103   /**
104    * `external-ip' command not found
105    */
106   GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
107   
108   /**
109    * "no valid address was returned by `external-ip'"
110    */
111   GNUNET_NAT_ERROR_EXTERNAL_IP_NO_VALID_ADDRESS_FOUND,
112     
113   /**
114    * 
115    */
116   GNUNET_NAT_ERROR_  
117   
118 };
119
120
121 /**
122  * Attempt to enable port redirection and detect public IP address
123  * contacting UPnP or NAT-PMP routers on the local network. Use addr
124  * to specify to which of the local host's addresses should the
125  * external port be mapped. The port is taken from the corresponding
126  * sockaddr_in[6] field.  The NAT module should call the given
127  * callback for any 'plausible' external address.
128  *
129  * @param cfg configuration to use
130  * @param is_tcp #GNUNET_YES for TCP, #GNUNET_NO for UDP
131  * @param adv_port advertised port (port we are either bound to or that our OS
132  *                 locally performs redirection from to our bound port).
133  * @param num_addrs number of addresses in @a addrs
134  * @param addrs list of local addresses packets should be redirected to
135  * @param addrlens actual lengths of the addresses in @a addrs
136  * @param address_callback function to call everytime the public IP address changes
137  * @param reversal_callback function to call if someone wants connection reversal from us,
138  *        NULL if connection reversal is not supported
139  * @param callback_cls closure for callbacks
140  * @return NULL on error, otherwise handle that can be used to unregister
141  */
142 struct GNUNET_NAT_Handle *
143 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
144                      int is_tcp,
145                      uint16_t adv_port,
146                      unsigned int num_addrs,
147                      const struct sockaddr **addrs,
148                      const socklen_t *addrlens,
149                      GNUNET_NAT_AddressCallback address_callback,
150                      GNUNET_NAT_ReversalCallback reversal_callback,
151                      void *callback_cls);
152
153
154 /**
155  * Test if the given address is (currently) a plausible IP address for
156  * this peer.
157  *
158  * @param h the handle returned by register
159  * @param addr IP address to test (IPv4 or IPv6)
160  * @param addrlen number of bytes in @a addr
161  * @return #GNUNET_YES if the address is plausible,
162  *         #GNUNET_NO if the address is not plausible,
163  *         #GNUNET_SYSERR if the address is malformed
164  */
165 int
166 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
167                          const void *addr,
168                          socklen_t addrlen);
169
170
171 /**
172  * We learned about a peer (possibly behind NAT) so run the
173  * gnunet-nat-client to send dummy ICMP responses to cause
174  * that peer to connect to us (connection reversal).
175  *
176  * @param h handle (used for configuration)
177  * @param sa the address of the peer (IPv4-only)
178  * @return #GNUNET_SYSERR on error, #GNUNET_NO if nat client is disabled,
179  *         #GNUNET_OK otherwise
180  */
181 int
182 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
183                        const struct sockaddr_in *sa);
184
185
186 /**
187  * Stop port redirection and public IP address detection for the given
188  * handle.  This frees the handle, after having sent the needed
189  * commands to close open ports.
190  *
191  * @param h the handle to stop
192  */
193 void
194 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h);
195
196
197 /**
198  * Handle to a NAT test.
199  */
200 struct GNUNET_NAT_Test;
201
202
203 /**
204  * Function called to report success or failure for
205  * NAT configuration test.
206  *
207  * @param cls closure
208  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
209  */
210 typedef void (*GNUNET_NAT_TestCallback) (void *cls,
211                                          enum GNUNET_NAT_FailureCode result);
212
213
214 /**
215  * Start testing if NAT traversal works using the
216  * given configuration (IPv4-only).
217  *
218  * @param cfg configuration for the NAT traversal
219  * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
220  * @param bnd_port port to bind to, 0 for connection reversal
221  * @param adv_port externally advertised port to use
222  * @param report function to call with the result of the test
223  * @param report_cls closure for @a report
224  * @return handle to cancel NAT test
225  */
226 struct GNUNET_NAT_Test *
227 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
228                        int is_tcp,
229                        uint16_t bnd_port,
230                        uint16_t adv_port,
231                        GNUNET_NAT_TestCallback report,
232                        void *report_cls);
233
234
235 /**
236  * Stop an active NAT test.
237  *
238  * @param tst test to stop.
239  */
240 void
241 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
242
243
244 /**
245  * Signature of a callback that is given an IP address.
246  *
247  * @param cls closure
248  * @param addr the address, NULL on errors
249  * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
250  */
251 typedef void (*GNUNET_NAT_IPCallback) (void *cls,
252                                        const struct in_addr *addr,
253                                        enum GNUNET_NAT_FailureCode result);
254
255
256
257 /**
258  * Opaque handle to cancel #GNUNET_NAT_mini_get_external_ipv4() operation.
259  */
260 struct GNUNET_NAT_ExternalHandle;
261
262
263 /**
264  * Try to get the external IPv4 address of this peer.
265  *
266  * @param timeout when to fail
267  * @param cb function to call with result
268  * @param cb_cls closure for @a cb
269  * @return handle for cancellation (can only be used until @a cb is called), NULL on error
270  */
271 struct GNUNET_NAT_ExternalHandle *
272 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
273                                    GNUNET_NAT_IPCallback cb,
274                                    void *cb_cls);
275
276
277 /**
278  * Cancel operation.
279  *
280  * @param eh operation to cancel
281  */
282 void
283 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh);
284
285
286 /**
287  * Handle to a mapping created with upnpc.
288  */
289 struct GNUNET_NAT_MiniHandle;
290
291
292 /**
293  * Signature of the callback passed to #GNUNET_NAT_register() for
294  * a function to call whenever our set of 'valid' addresses changes.
295  *
296  * @param cls closure
297  * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
298  *     the previous (now invalid) one, #GNUNET_SYSERR indicates an error
299  * @param addr either the previous or the new public IP address
300  * @param addrlen actual length of the @a addr
301  * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
302  */
303 typedef void
304 (*GNUNET_NAT_MiniAddressCallback) (void *cls,
305                                    int add_remove,
306                                    const struct sockaddr *addr,
307                                    socklen_t addrlen,
308                                    enum GNUNET_NAT_FailureCode result);
309
310
311 /**
312  * Start mapping the given port using (mini)upnpc.  This function
313  * should typically not be used directly (it is used within the
314  * general-purpose #GNUNET_NAT_register() code).  However, it can be
315  * used if specifically UPnP-based NAT traversal is to be used or
316  * tested.
317  *
318  * @param port port to map
319  * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
320  * @param ac function to call with mapping result
321  * @param ac_cls closure for @a ac
322  * @return NULL on error
323  */
324 struct GNUNET_NAT_MiniHandle *
325 GNUNET_NAT_mini_map_start (uint16_t port,
326                            int is_tcp,
327                            GNUNET_NAT_MiniAddressCallback ac,
328                            void *ac_cls);
329
330
331 /**
332  * Remove a mapping created with (mini)upnpc.  Calling
333  * this function will give 'upnpc' 1s to remove the mapping,
334  * so while this function is non-blocking, a task will be
335  * left with the scheduler for up to 1s past this call.
336  *
337  * @param mini the handle
338  */
339 void
340 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini);
341
342
343 /**
344  * Handle to auto-configuration in progress.
345  */
346 struct GNUNET_NAT_AutoHandle;
347
348
349 /**
350  * Function called with the result from the autoconfiguration.
351  *
352  * @param cls closure
353  * @param diff minimal suggested changes to the original configuration
354  *             to make it work (as best as we can)
355  * @param result GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
356  */
357 typedef void
358 (*GNUNET_NAT_AutoResultCallback)(void *cls,
359                                  const struct GNUNET_CONFIGURATION_Handle *diff,
360                                  enum GNUNET_NAT_FailureCode result);
361
362
363 /**
364  * Start auto-configuration routine.  The resolver service should
365  * be available when this function is called.
366  *
367  * @param cfg initial configuration
368  * @param cb function to call with autoconfiguration result
369  * @param cb_cls closure for @a cb
370  * @return handle to cancel operation
371  */
372 struct GNUNET_NAT_AutoHandle *
373 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
374                              GNUNET_NAT_AutoResultCallback cb,
375                              void *cb_cls);
376
377
378 /**
379  * Abort autoconfiguration.
380  *
381  * @param ah handle for operation to abort
382  */
383 void
384 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah);
385
386 #endif
387
388 /* end of gnunet_nat_lib.h */