Move type check after initialization to make compiler happy
[oweals/gnunet.git] / src / include / gnunet_nat_service.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2007-2016 GNUnet e.V.
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  * @author Christian Grothoff
23  * @author Milan Bouchet-Valat
24  *
25  * @file
26  * Service for handling UPnP and NAT-PMP port forwarding
27  * and external IP address retrieval
28  *
29  * @defgroup nat  NAT library
30  * Service for handling UPnP and NAT-PMP port forwarding
31  * and external IP address retrieval
32  *
33  * @{
34  */
35
36 #ifndef GNUNET_NAT_SERVICE_H
37 #define GNUNET_NAT_SERVICE_H
38
39 #include "gnunet_util_lib.h"
40
41
42 /**
43  * Some addresses contain sensitive information or are
44  * not suitable for global distribution.  We use address
45  * classes to filter addresses by which domain they make
46  * sense to be used in.  These are used in a bitmask.
47  *
48  * FIXME: might want to define this elsewhere; we have
49  * an equivalent enum in gnunet_transport_hello_service.h;
50  * might ultimately belong with the new HELLO definition.
51  */
52 enum GNUNET_NAT_AddressClass
53 {
54
55   /**
56    * No address.
57    */
58   GNUNET_NAT_AC_NONE = 0,
59
60   /**
61    * Addresses that fall into no other category
62    * (i.e. incoming which we cannot use elsewhere).
63    */
64   GNUNET_NAT_AC_OTHER = 1,
65
66   /**
67    * Addresses that are global and are insensitive
68    * (i.e. IPv4).
69    */
70   GNUNET_NAT_AC_GLOBAL = 2,
71
72   /**
73    * Addresses that are global and are sensitive
74    * (i.e. IPv6 with our MAC).
75    */
76   GNUNET_NAT_AC_GLOBAL_PRIVATE = 4,
77
78   /**
79    * Addresses useful in the local wired network,
80    * i.e. a MAC.  Sensitive, but obvious to people nearby.
81    * Useful for broadcasts.
82    */
83   GNUNET_NAT_AC_LAN = 8,
84
85   /**
86    * Addresses useful in the local wireless network,
87    * i.e. a MAC.  Sensitive, but obvious to people nearby.
88    * Useful for broadcasts.
89    */
90   GNUNET_NAT_AC_WLAN = 16,
91
92   /**
93    * Addresses useful in the local bluetooth network.  Sensitive, but
94    * obvious to people nearby.  Useful for broadcasts.
95    */
96   GNUNET_NAT_AC_BT = 32,
97
98   /**
99    * Bitmask for "any" address.
100    */
101   GNUNET_NAT_AC_ANY = 65535
102   
103 };
104
105
106 /**
107  * Signature of the callback passed to #GNUNET_NAT_register() for
108  * a function to call whenever our set of 'valid' addresses changes.
109  *
110  * @param cls closure
111  * @param add_remove #GNUNET_YES to add a new public IP address, 
112  *                   #GNUNET_NO to remove a previous (now invalid) one
113  * @param ac address class the address belongs to
114  * @param addr either the previous or the new public IP address
115  * @param addrlen actual length of the @a addr
116  */
117 typedef void
118 (*GNUNET_NAT_AddressCallback) (void *cls,
119                                int add_remove,
120                                enum GNUNET_NAT_AddressClass ac,
121                                const struct sockaddr *addr,
122                                socklen_t addrlen);
123
124
125 /**
126  * Signature of the callback passed to #GNUNET_NAT_register().
127  * for a function to call whenever someone asks us to do connection
128  * reversal.
129  *
130  * @param cls closure
131  * @param local_addr address where we received the request
132  * @param local_addrlen actual length of the @a local_addr
133  * @param remote_addr public IP address of the other peer
134  * @param remote_addrlen actual length of the @a remote_addr
135  */
136 typedef void
137 (*GNUNET_NAT_ReversalCallback) (void *cls,
138                                 const struct sockaddr *local_addr,
139                                 socklen_t local_addrlen,
140                                 const struct sockaddr *remote_addr,
141                                 socklen_t remote_addrlen);
142
143
144 /**
145  * Handle for active NAT registrations.
146  */
147 struct GNUNET_NAT_Handle;
148
149
150 /**
151  * Attempt to enable port redirection and detect public IP address
152  * contacting UPnP or NAT-PMP routers on the local network. Use @a
153  * addr to specify to which of the local host's addresses should the
154  * external port be mapped. The port is taken from the corresponding
155  * sockaddr_in[6] field.  The NAT module should call the given @a
156  * address_callback for any 'plausible' external address.
157  *
158  * @param cfg configuration to use
159  * @param proto protocol this is about, IPPROTO_TCP or IPPROTO_UDP
160  * @param adv_port advertised port (port we are either bound to or that our OS
161  *                 locally performs redirection from to our bound port).
162  * @param num_addrs number of addresses in @a addrs
163  * @param addrs list of local addresses packets should be redirected to
164  * @param addrlens actual lengths of the addresses in @a addrs
165  * @param address_callback function to call everytime the public IP address changes
166  * @param reversal_callback function to call if someone wants connection reversal from us,
167  *        NULL if connection reversal is not supported
168  * @param callback_cls closure for callbacks
169  * @return NULL on error, otherwise handle that can be used to unregister
170  */
171 struct GNUNET_NAT_Handle *
172 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
173                      uint8_t proto,
174                      uint16_t adv_port,
175                      unsigned int num_addrs,
176                      const struct sockaddr **addrs,
177                      const socklen_t *addrlens,
178                      GNUNET_NAT_AddressCallback address_callback,
179                      GNUNET_NAT_ReversalCallback reversal_callback,
180                      void *callback_cls);
181
182
183 /**
184  * Handle an incoming STUN message.  This function is useful as
185  * some GNUnet service may be listening on a UDP port and might
186  * thus receive STUN messages while trying to receive other data.
187  * In this case, this function can be used to act as a proper
188  * STUN server (if desired).
189  *
190  * The function does some basic sanity checks on packet size and
191  * content, try to extract a bit of information, and possibly replies
192  * if this is an actual STUN message.
193  * 
194  * At the moment this only processes BIND requests, and returns the
195  * externally visible address of the request. 
196  *
197  * @param nh handle to the NAT service
198  * @param sender_addr address from which we got @a data
199  * @param sender_addr_len number of bytes in @a sender_addr
200  * @param data the packet
201  * @param data_size number of bytes in @a data
202  * @return #GNUNET_OK on success
203  *         #GNUNET_NO if the packet is not a STUN packet
204  *         #GNUNET_SYSERR on internal error handling the packet
205  */
206 int
207 GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
208                                const struct sockaddr *sender_addr,
209                                size_t sender_addr_len,
210                                const void *data,
211                                size_t data_size);
212
213
214 /**
215  * Test if the given address is (currently) a plausible IP address for
216  * this peer.  Mostly a convenience function so that clients do not
217  * have to explicitly track all IPs that the #GNUNET_NAT_AddressCallback
218  * has returned so far.
219  *
220  * @param nh the handle returned by register
221  * @param addr IP address to test (IPv4 or IPv6)
222  * @param addrlen number of bytes in @a addr
223  * @return #GNUNET_YES if the address is plausible,
224  *         #GNUNET_NO if the address is not plausible,
225  *         #GNUNET_SYSERR if the address is malformed
226  */
227 int
228 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
229                          const void *addr,
230                          socklen_t addrlen);
231
232
233 /**
234  * We learned about a peer (possibly behind NAT) so run the
235  * gnunet-nat-client to send dummy ICMP responses to cause
236  * that peer to connect to us (connection reversal).
237  *
238  * @param nh handle (used for configuration)
239  * @param local_sa our local address of the peer (IPv4-only)
240  * @param remote_sa the remote address of the peer (IPv4-only)
241  * @return #GNUNET_SYSERR on error, 
242  *         #GNUNET_NO if connection reversal is unavailable,
243  *         #GNUNET_OK otherwise (presumably in progress)
244  */
245 int
246 GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
247                              const struct sockaddr_in *local_sa,
248                              const struct sockaddr_in *remote_sa);
249
250
251 /**
252  * Stop port redirection and public IP address detection for the given
253  * handle.  This frees the handle, after having sent the needed
254  * commands to close open ports.
255  *
256  * @param nh the handle to unregister
257  */
258 void
259 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh);
260
261
262 /**
263  * Handle to a NAT test.
264  */
265 struct GNUNET_NAT_Test;
266
267
268 /**
269  * Error Types for the NAT subsystem (which can then later be converted/resolved to a string)
270  */
271 enum GNUNET_NAT_StatusCode
272 {
273   /**
274    * Just the default
275    */
276   GNUNET_NAT_ERROR_SUCCESS = GNUNET_OK,
277
278   /**
279    * IPC Failure
280    */
281   GNUNET_NAT_ERROR_IPC_FAILURE,
282
283   /**
284    * Failure in network subsystem, check permissions
285    */
286   GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR,
287
288   /**
289    * test timed out
290    */
291   GNUNET_NAT_ERROR_TIMEOUT,
292
293   /**
294    * detected that we are offline
295    */
296   GNUNET_NAT_ERROR_NOT_ONLINE,
297
298   /**
299    * `upnpc` command not found
300    */
301   GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
302
303   /**
304    * Failed to run `upnpc` command
305    */
306   GNUNET_NAT_ERROR_UPNPC_FAILED,
307
308   /**
309    * `upnpc' command took too long, process killed
310    */
311   GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
312
313   /**
314    * `upnpc' command failed to establish port mapping
315    */
316   GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
317
318   /**
319    * `external-ip' command not found
320    */
321   GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
322
323   /**
324    * Failed to run `external-ip` command
325    */
326   GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED,
327
328   /**
329    * `external-ip' command output invalid
330    */
331   GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID,
332
333   /**
334    * "no valid address was returned by `external-ip'"
335    */
336   GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID,
337
338   /**
339    * Could not determine interface with internal/local network address
340    */
341   GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO,
342
343   /**
344    * No working gnunet-helper-nat-server found
345    */
346   GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND,
347
348   /**
349    * NAT test could not be initialized
350    */
351   GNUNET_NAT_ERROR_NAT_TEST_START_FAILED,
352
353   /**
354    * NAT test timeout
355    */
356   GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT,
357
358   /**
359    * NAT test failed to initiate
360    */
361   GNUNET_NAT_ERROR_NAT_REGISTER_FAILED,
362
363   /**
364    *
365    */
366   GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND
367   
368 };
369
370
371 /**
372  * Function called to report success or failure for
373  * NAT configuration test.
374  *
375  * @param cls closure
376  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
377  */
378 typedef void
379 (*GNUNET_NAT_TestCallback) (void *cls,
380                             enum GNUNET_NAT_StatusCode result);
381
382
383 /**
384  * Start testing if NAT traversal works using the given configuration
385  * (IPv4-only).  The transport adapters should be down while using
386  * this function.
387  *
388  * @param cfg configuration for the NAT traversal
389  * @param proto protocol to test, i.e. IPPROTO_TCP or IPPROTO_UDP
390  * @param bind_ip IPv4 address to bind to
391  * @param bnd_port port to bind to, 0 to test connection reversal
392  * @param extern_ip IPv4 address to externally advertise
393  * @param extern_port externally advertised port to use
394  * @param report function to call with the result of the test
395  * @param report_cls closure for @a report
396  * @return handle to cancel NAT test
397  */
398 struct GNUNET_NAT_Test *
399 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
400                        uint8_t proto,
401                        struct in_addr bind_ip,
402                        uint16_t bnd_port,
403                        struct in_addr extern_ip,
404                        uint16_t extern_port,
405                        GNUNET_NAT_TestCallback report,
406                        void *report_cls);
407
408
409 /**
410  * Stop an active NAT test.
411  *
412  * @param tst test to stop.
413  */
414 void
415 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
416
417
418 /**
419  * Handle to auto-configuration in progress.
420  */
421 struct GNUNET_NAT_AutoHandle;
422
423
424 /**
425  * What the situation of the NAT connectivity
426  */
427 enum GNUNET_NAT_Type
428 {
429   /**
430    * We have a direct connection
431    */
432   GNUNET_NAT_TYPE_NO_NAT = GNUNET_OK,
433
434   /**
435    * We are under a NAT but cannot traverse it
436    */
437   GNUNET_NAT_TYPE_UNREACHABLE_NAT,
438
439   /**
440    * We can traverse using STUN
441    */
442   GNUNET_NAT_TYPE_STUN_PUNCHED_NAT,
443
444   /**
445    * We can traverse using UPNP
446    */
447   GNUNET_NAT_TYPE_UPNP_NAT,
448
449   /**
450    * We know nothing about the NAT.
451    */
452   GNUNET_NAT_TYPE_UNKNOWN
453
454 };
455
456
457 /**
458  * Converts `enum GNUNET_NAT_StatusCode` to string
459  *
460  * @param err error code to resolve to a string
461  * @return point to a static string containing the error code
462  */
463 const char *
464 GNUNET_NAT_status2string (enum GNUNET_NAT_StatusCode err);
465
466
467 /**
468  * Function called with the result from the autoconfiguration.
469  *
470  * @param cls closure
471  * @param diff minimal suggested changes to the original configuration
472  *             to make it work (as best as we can)
473  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
474  * @param type what the situation of the NAT
475  */
476 typedef void
477 (*GNUNET_NAT_AutoResultCallback)(void *cls,
478                                  const struct GNUNET_CONFIGURATION_Handle *diff,
479                                  enum GNUNET_NAT_StatusCode result,
480                                  enum GNUNET_NAT_Type type);
481
482
483 /**
484  * Start auto-configuration routine.  The transport adapters should
485  * be stopped while this function is called.
486  *
487  * @param cfg initial configuration
488  * @param cb function to call with autoconfiguration result
489  * @param cb_cls closure for @a cb
490  * @return handle to cancel operation
491  */
492 struct GNUNET_NAT_AutoHandle *
493 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
494                              GNUNET_NAT_AutoResultCallback cb,
495                              void *cb_cls);
496
497
498 /**
499  * Abort autoconfiguration.
500  *
501  * @param ah handle for operation to abort
502  */
503 void
504 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah);
505
506
507 #endif
508
509 /** @} */  /* end of group */
510
511 /* end of gnunet_nat_service.h */