-fix indent
[oweals/gnunet.git] / src / include / gnunet_nat_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 /**
75  * What the situation of the NAT connectivity
76  */
77 enum GNUNET_NAT_Type
78 {
79   /**
80    * We have a direct connection
81    */
82   GNUNET_NAT_TYPE_NO_NAT = GNUNET_OK,
83
84   /**
85    * We are under a NAT but cannot traverse it
86    */
87   GNUNET_NAT_TYPE_UNREACHABLE_NAT,
88
89   /**
90    * We can traverse using STUN
91    */
92   GNUNET_NAT_TYPE_STUN_PUNCHED_NAT,
93
94   /**
95    * WE can traverse using UPNP
96    */
97   GNUNET_NAT_TYPE_UPNP_NAT
98
99 };
100
101 /**
102  * Error Types for the NAT subsystem (which can then later be converted/resolved to a string)
103  */
104 enum GNUNET_NAT_StatusCode
105 {
106   /**
107    * Just the default
108    */
109   GNUNET_NAT_ERROR_SUCCESS = GNUNET_OK,
110
111   /**
112    * IPC Failure
113    */
114   GNUNET_NAT_ERROR_IPC_FAILURE,
115
116   /**
117    * Failure in network subsystem, check permissions
118    */
119   GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR,
120
121   /**
122    * test timed out
123    */
124   GNUNET_NAT_ERROR_TIMEOUT,
125
126   /**
127    * detected that we are offline
128    */
129   GNUNET_NAT_ERROR_NOT_ONLINE,
130
131   /**
132    * `upnpc` command not found
133    */
134   GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
135
136   /**
137    * Failed to run `upnpc` command
138    */
139   GNUNET_NAT_ERROR_UPNPC_FAILED,
140
141   /**
142    * `upnpc' command took too long, process killed
143    */
144   GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
145
146   /**
147    * `upnpc' command failed to establish port mapping
148    */
149   GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
150
151   /**
152    * `external-ip' command not found
153    */
154   GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
155
156   /**
157    * Failed to run `external-ip` command
158    */
159   GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED,
160
161   /**
162    * `external-ip' command output invalid
163    */
164   GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID,
165
166   /**
167    * "no valid address was returned by `external-ip'"
168    */
169   GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID,
170
171   /**
172    * Could not determine interface with internal/local network address
173    */
174   GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO,
175
176   /**
177    * No working gnunet-helper-nat-server found
178    */
179   GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND,
180
181   /**
182    * NAT test could not be initialized
183    */
184   GNUNET_NAT_ERROR_NAT_TEST_START_FAILED,
185
186   /**
187    * NAT test timeout
188    */
189   GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT,
190
191   /**
192    * NAT test failed to initiate
193    */
194   GNUNET_NAT_ERROR_NAT_REGISTER_FAILED,
195
196   /**
197    *
198    */
199   GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND,
200
201   /**
202    *
203    */
204   GNUNET_NAT_ERROR_
205 };
206
207
208 /**
209  * Converts `enum GNUNET_NAT_StatusCode` to string
210  *
211  * @param err error code to resolve to a string
212  * @return point to a static string containing the error code
213  */
214 const char *
215 GNUNET_NAT_status2string (enum GNUNET_NAT_StatusCode err);
216
217
218 /**
219  * Attempt to enable port redirection and detect public IP address
220  * contacting UPnP or NAT-PMP routers on the local network. Use addr
221  * to specify to which of the local host's addresses should the
222  * external port be mapped. The port is taken from the corresponding
223  * sockaddr_in[6] field.  The NAT module should call the given
224  * callback for any 'plausible' external address.
225  *
226  * @param cfg configuration to use
227  * @param is_tcp #GNUNET_YES for TCP, #GNUNET_NO for UDP
228  * @param adv_port advertised port (port we are either bound to or that our OS
229  *                 locally performs redirection from to our bound port).
230  * @param num_addrs number of addresses in @a addrs
231  * @param addrs list of local addresses packets should be redirected to
232  * @param addrlens actual lengths of the addresses in @a addrs
233  * @param address_callback function to call everytime the public IP address changes
234  * @param reversal_callback function to call if someone wants connection reversal from us,
235  *        NULL if connection reversal is not supported
236  * @param callback_cls closure for callbacks
237  * @return NULL on error, otherwise handle that can be used to unregister
238  */
239 struct GNUNET_NAT_Handle *
240 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
241                      int is_tcp,
242                      uint16_t adv_port,
243                      unsigned int num_addrs,
244                      const struct sockaddr **addrs,
245                      const socklen_t *addrlens,
246                      GNUNET_NAT_AddressCallback address_callback,
247                      GNUNET_NAT_ReversalCallback reversal_callback,
248                      void *callback_cls,
249                      struct GNUNET_NETWORK_Handle* sock);
250
251
252 /**
253  * Test if the given address is (currently) a plausible IP address for
254  * this peer.
255  *
256  * @param h the handle returned by register
257  * @param addr IP address to test (IPv4 or IPv6)
258  * @param addrlen number of bytes in @a addr
259  * @return #GNUNET_YES if the address is plausible,
260  *         #GNUNET_NO if the address is not plausible,
261  *         #GNUNET_SYSERR if the address is malformed
262  */
263 int
264 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
265                          const void *addr,
266                          socklen_t addrlen);
267
268
269 /**
270  * We learned about a peer (possibly behind NAT) so run the
271  * gnunet-nat-client to send dummy ICMP responses to cause
272  * that peer to connect to us (connection reversal).
273  *
274  * @param h handle (used for configuration)
275  * @param sa the address of the peer (IPv4-only)
276  * @return #GNUNET_SYSERR on error, #GNUNET_NO if nat client is disabled,
277  *         #GNUNET_OK otherwise
278  */
279 int
280 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
281                        const struct sockaddr_in *sa);
282
283
284 /**
285  * Stop port redirection and public IP address detection for the given
286  * handle.  This frees the handle, after having sent the needed
287  * commands to close open ports.
288  *
289  * @param h the handle to stop
290  */
291 void
292 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h);
293
294
295 /**
296  * Handle to a NAT test.
297  */
298 struct GNUNET_NAT_Test;
299
300
301 /**
302  * Function called to report success or failure for
303  * NAT configuration test.
304  *
305  * @param cls closure
306  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
307  */
308 typedef void (*GNUNET_NAT_TestCallback) (void *cls,
309                                          enum GNUNET_NAT_StatusCode result);
310
311
312 /**
313  * Start testing if NAT traversal works using the
314  * given configuration (IPv4-only).
315  *
316  * @param cfg configuration for the NAT traversal
317  * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
318  * @param bnd_port port to bind to, 0 for connection reversal
319  * @param adv_port externally advertised port to use
320  * @param timeout delay after which the test should be aborted
321  * @param report function to call with the result of the test;
322  *               you still must call #GNUNET_NAT_test_stop().
323  * @param report_cls closure for @a report
324  * @return handle to cancel NAT test
325  */
326 struct GNUNET_NAT_Test *
327 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
328                        int is_tcp,
329                        uint16_t bnd_port,
330                        uint16_t adv_port,
331                        struct GNUNET_TIME_Relative timeout,
332                        GNUNET_NAT_TestCallback report,
333                        void *report_cls);
334
335
336 /**
337  * Stop an active NAT test.
338  *
339  * @param tst test to stop.
340  */
341 void
342 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
343
344
345 /**
346  * Signature of a callback that is given an IP address.
347  *
348  * @param cls closure
349  * @param addr the address, NULL on errors
350  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
351  */
352 typedef void
353 (*GNUNET_NAT_IPCallback) (void *cls,
354                           const struct in_addr *addr,
355                           enum GNUNET_NAT_StatusCode result);
356
357
358
359 /**
360  * Opaque handle to cancel #GNUNET_NAT_mini_get_external_ipv4() operation.
361  */
362 struct GNUNET_NAT_ExternalHandle;
363
364
365 /**
366  * Try to get the external IPv4 address of this peer.
367  *
368  * @param timeout when to fail
369  * @param cb function to call with result
370  * @param cb_cls closure for @a cb
371  * @return handle for cancellation (can only be used until @a cb is called), NULL on error
372  */
373 struct GNUNET_NAT_ExternalHandle *
374 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
375                                    GNUNET_NAT_IPCallback cb,
376                                    void *cb_cls);
377
378
379 /**
380  * Cancel operation.
381  *
382  * @param eh operation to cancel
383  */
384 void
385 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh);
386
387
388 /**
389  * Handle to a mapping created with upnpc.
390  */
391 struct GNUNET_NAT_MiniHandle;
392
393
394 /**
395  * Signature of the callback passed to #GNUNET_NAT_register() for
396  * a function to call whenever our set of 'valid' addresses changes.
397  *
398  * @param cls closure
399  * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
400  *     the previous (now invalid) one, #GNUNET_SYSERR indicates an error
401  * @param addr either the previous or the new public IP address
402  * @param addrlen actual length of the @a addr
403  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
404  */
405 typedef void
406 (*GNUNET_NAT_MiniAddressCallback) (void *cls,
407                                    int add_remove,
408                                    const struct sockaddr *addr,
409                                    socklen_t addrlen,
410                                    enum GNUNET_NAT_StatusCode result);
411
412
413 /**
414  * Start mapping the given port using (mini)upnpc.  This function
415  * should typically not be used directly (it is used within the
416  * general-purpose #GNUNET_NAT_register() code).  However, it can be
417  * used if specifically UPnP-based NAT traversal is to be used or
418  * tested.
419  *
420  * @param port port to map
421  * @param is_tcp #GNUNET_YES to map TCP, #GNUNET_NO for UDP
422  * @param ac function to call with mapping result
423  * @param ac_cls closure for @a ac
424  * @return NULL on error
425  */
426 struct GNUNET_NAT_MiniHandle *
427 GNUNET_NAT_mini_map_start (uint16_t port,
428                            int is_tcp,
429                            GNUNET_NAT_MiniAddressCallback ac,
430                            void *ac_cls);
431
432
433 /**
434  * Remove a mapping created with (mini)upnpc.  Calling
435  * this function will give 'upnpc' 1s to remove the mapping,
436  * so while this function is non-blocking, a task will be
437  * left with the scheduler for up to 1s past this call.
438  *
439  * @param mini the handle
440  */
441 void
442 GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini);
443
444
445 /**
446  * Handle to auto-configuration in progress.
447  */
448 struct GNUNET_NAT_AutoHandle;
449
450
451 /**
452  * Function called with the result from the autoconfiguration.
453  *
454  * @param cls closure
455  * @param diff minimal suggested changes to the original configuration
456  *             to make it work (as best as we can)
457  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
458  * @param type what the situation of the NAT
459  */
460 typedef void
461 (*GNUNET_NAT_AutoResultCallback)(void *cls,
462                                  const struct GNUNET_CONFIGURATION_Handle *diff,
463                                  enum GNUNET_NAT_StatusCode result,
464                                  enum GNUNET_NAT_Type type);
465
466
467 /**
468  * Start auto-configuration routine.  The resolver service should
469  * be available when this function is called.
470  *
471  * @param cfg initial configuration
472  * @param cb function to call with autoconfiguration result
473  * @param cb_cls closure for @a cb
474  * @return handle to cancel operation
475  */
476 struct GNUNET_NAT_AutoHandle *
477 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
478                              GNUNET_NAT_AutoResultCallback cb,
479                              void *cb_cls);
480
481
482 /**
483  * Abort autoconfiguration.
484  *
485  * @param ah handle for operation to abort
486  */
487 void
488 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah);
489
490 /**
491  * Handle for active STUN Requests.
492  */
493 struct GNUNET_NAT_STUN_Handle;
494
495
496
497
498 /**
499  * Function called with the result if an error happened during STUN request.
500  *
501  * @param cls closure
502  * @param result the specific error code
503  */
504 typedef void
505 (*GNUNET_NAT_STUN_ErrorCallback)(void *cls,
506                                  enum GNUNET_NAT_StatusCode error);
507
508
509 /**
510  * Handle to a request given to the resolver.  Can be used to cancel
511  * the request prior to the timeout or successful execution.  Also
512  * used to track our internal state for the request.
513  */
514 struct GNUNET_NAT_STUN_Handle;
515
516
517 /**
518  * Make generic STUN request.  Sends a generic stun request to the
519  * server specified using the specified socket.  The caller must
520  * wait for a reply on the @a sock and call
521  * #GNUNET_NAT_stun_handle_packet() if a reply is received.
522  *
523  * @param server the address of the stun server
524  * @param port port of the stun server
525  * @param sock the socket used to send the request
526  * @param cb callback in case of error (or completion)
527  * @param cb_cls closure for @a cb
528  * @return NULL on error
529  */
530 struct GNUNET_NAT_STUN_Handle *
531 GNUNET_NAT_stun_make_request (const char *server,
532                               uint16_t port,
533                               struct GNUNET_NETWORK_Handle *sock,
534                               GNUNET_NAT_STUN_ErrorCallback cb,
535                               void *cb_cls);
536
537
538 /**
539  * Cancel active STUN request. Frees associated resources
540  * and ensures that the callback is no longer invoked.
541  *
542  * @param rh request to cancel
543  */
544 void
545 GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh);
546
547
548 /**
549  * Handle an incoming STUN message. Do some basic sanity checks on
550  * packet size and content, try to extract a bit of information, and
551  * possibly reply.  At the moment this only processes BIND requests,
552  * and returns the externally visible address of the request.  If a
553  * callback is specified, invoke it with the attribute.
554  *
555  * @param data the packet
556  * @param len the length of the packet
557  * @param arg sockaddr_in where we will set our discovered packet
558  * @return #GNUNET_OK on OK,
559  *          #GNUNET_NO if the packet is not a stun packet
560  */
561 int
562 GNUNET_NAT_stun_handle_packet (const void *data,
563                                size_t len,
564                                struct sockaddr_in *arg);
565
566
567 /**
568  * CHECK if is a valid STUN packet sending to #GNUNET_NAT_stun_handle_packet().
569  * It also check if it can handle the packet based on the NAT handler.
570  * You don't need to call anything else to check if the packet is valid,
571  *
572  * @param cls the NAT handle
573  * @param data, packet
574  * @param len, packet length
575  *
576  * @return #GNUNET_NO if it can't decode, #GNUNET_YES if is a packet
577  */
578 int
579 GNUNET_NAT_is_valid_stun_packet (void *cls,
580                                  const void *data,
581                                  size_t len);
582
583
584
585 #endif
586
587 /* end of gnunet_nat_lib.h */