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