use NULL value in load_path_suffix to NOT load any files
[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      SPDX-License-Identifier: AGPL3.0-or-later
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    * No address.
56    */
57   GNUNET_NAT_AC_NONE = 0,
58
59   /**
60    * Addresses that fall into no other category
61    * (i.e. incoming which we cannot use elsewhere).
62    */
63   GNUNET_NAT_AC_OTHER = 1,
64
65   /**
66    * Flag for addresses that are highly sensitive
67    * (i.e. IPv6 with our MAC).
68    */
69   GNUNET_NAT_AC_PRIVATE = 2,
70
71   /**
72    * Addresses that are global (i.e. IPv4).
73    */
74   GNUNET_NAT_AC_GLOBAL = 4,
75
76   /**
77    * Addresses that are global and are sensitive
78    * (i.e. IPv6 with our MAC).
79    */
80   GNUNET_NAT_AC_GLOBAL_PRIVATE = 6,
81
82   /**
83    * Addresses useful in the local wired network,
84    * i.e. a MAC.  Sensitive, but obvious to people nearby.
85    *
86    * Useful for broadcasts.
87    */
88   GNUNET_NAT_AC_LAN = 8,
89
90   /**
91    * Addresses useful in the local wired network,
92    * i.e. a MAC.  Sensitive, but obvious to people nearby.
93    * Useful for broadcasts.
94    */
95   GNUNET_NAT_AC_LAN_PRIVATE = 10,
96
97   /**
98    * Addresses useful in the local wireless network,
99    * i.e. a MAC.  Sensitive, but obvious to people nearby.
100    * Useful for broadcasts.
101    */
102   GNUNET_NAT_AC_WLAN = 16,
103
104   /**
105    * Addresses useful in the local bluetooth network.  Sensitive, but
106    * obvious to people nearby.  Useful for broadcasts.
107    */
108   GNUNET_NAT_AC_BT = 32,
109
110   /**
111    * Loopback addresses, only useful under special cirumstances.
112    */
113   GNUNET_NAT_AC_LOOPBACK = 64,
114
115   /**
116    * Addresses that should be our external IP address
117    * on the outside of a NAT.  Might be incorrectly determined.
118    * Used as a bit in combination with #GNUNET_NAT_AC_GLOBAL,
119    * or in case of double-NAT with
120    * #GNUNET_NAT_AC_LAN.
121    */
122   GNUNET_NAT_AC_EXTERN = 128,
123
124   /**
125    * Addresses that were manually configured by the user.
126    * Used as a bit in combination with #GNUNET_NAT_AC_GLOBAL.
127    */
128   GNUNET_NAT_AC_MANUAL = 256,
129
130   /**
131    * Bitmask for "any" address.
132    */
133   GNUNET_NAT_AC_ANY = 65535
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  * What the situation of the NAT connectivity
241  */
242 enum GNUNET_NAT_Type
243 {
244   /**
245    * We have a direct connection
246    */
247   GNUNET_NAT_TYPE_NO_NAT = GNUNET_OK,
248
249   /**
250    * We are under a NAT but cannot traverse it
251    */
252   GNUNET_NAT_TYPE_UNREACHABLE_NAT,
253
254   /**
255    * We can traverse using STUN
256    */
257   GNUNET_NAT_TYPE_STUN_PUNCHED_NAT,
258
259   /**
260    * We can traverse using UPNP
261    */
262   GNUNET_NAT_TYPE_UPNP_NAT,
263
264   /**
265    * We know nothing about the NAT.
266    */
267   GNUNET_NAT_TYPE_UNKNOWN
268 };
269
270
271 /**
272  * Signature of the callback passed to #GNUNET_NAT_register() for
273  * a function to call whenever our set of 'valid' addresses changes.
274  *
275  * @param cls closure
276  * @param app_ctx[in,out] location where the app can store stuff
277  *                  on add and retrieve it on remove
278  * @param add_remove #GNUNET_YES to add a new public IP address,
279  *                   #GNUNET_NO to remove a previous (now invalid) one
280  * @param ac address class the address belongs to
281  * @param addr either the previous or the new public IP address
282  * @param addrlen actual length of the @a addr
283  */
284 typedef void
285 (*GNUNET_NAT_AddressCallback) (void *cls,
286                                void **app_ctx,
287                                int add_remove,
288                                enum GNUNET_NAT_AddressClass ac,
289                                const struct sockaddr *addr,
290                                socklen_t addrlen);
291
292
293 /**
294  * Signature of the callback passed to #GNUNET_NAT_register().
295  * for a function to call whenever someone asks us to do connection
296  * reversal.
297  *
298  * @param cls closure
299  * @param remote_addr public IP address of the other peer
300  * @param remote_addrlen actual length of the @a remote_addr
301  */
302 typedef void
303 (*GNUNET_NAT_ReversalCallback) (void *cls,
304                                 const struct sockaddr *remote_addr,
305                                 socklen_t remote_addrlen);
306
307
308 /**
309  * Handle for active NAT registrations.
310  */
311 struct GNUNET_NAT_Handle;
312
313
314 /**
315  * Attempt to enable port redirection and detect public IP address
316  * contacting UPnP or NAT-PMP routers on the local network. Use @a
317  * addr to specify to which of the local host's addresses should the
318  * external port be mapped. The port is taken from the corresponding
319  * sockaddr_in[6] field.  The NAT module should call the given @a
320  * address_callback for any 'plausible' external address.
321  *
322  * @param cfg configuration to use
323  * @param config_section name of the configuration section for options
324  * @param proto protocol this is about, IPPROTO_TCP or IPPROTO_UDP
325  * @param num_addrs number of addresses in @a addrs
326  * @param addrs list of local addresses packets should be redirected to
327  * @param addrlens actual lengths of the addresses in @a addrs
328  * @param address_callback function to call everytime the public IP address changes
329  * @param reversal_callback function to call if someone wants connection reversal from us,
330  *        NULL if connection reversal is not supported
331  * @param callback_cls closure for callbacks
332  * @return NULL on error, otherwise handle that can be used to unregister
333  */
334 struct GNUNET_NAT_Handle *
335 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
336                      const char *config_section,
337                      uint8_t proto,
338                      unsigned int num_addrs,
339                      const struct sockaddr **addrs,
340                      const socklen_t *addrlens,
341                      GNUNET_NAT_AddressCallback address_callback,
342                      GNUNET_NAT_ReversalCallback reversal_callback,
343                      void *callback_cls);
344
345
346 /**
347  * Test if the given address is (currently) a plausible IP address for
348  * this peer.  Mostly a convenience function so that clients do not
349  * have to explicitly track all IPs that the #GNUNET_NAT_AddressCallback
350  * has returned so far.
351  *
352  * @param nh the handle returned by register
353  * @param addr IP address to test (IPv4 or IPv6)
354  * @param addrlen number of bytes in @a addr
355  * @return #GNUNET_YES if the address is plausible,
356  *         #GNUNET_NO if the address is not plausible,
357  *         #GNUNET_SYSERR if the address is malformed
358  */
359 int
360 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
361                          const void *addr,
362                          socklen_t addrlen);
363
364
365 /**
366  * We learned about a peer (possibly behind NAT) so run the
367  * gnunet-nat-client to send dummy ICMP responses to cause
368  * that peer to connect to us (connection reversal).
369  *
370  * @param nh handle (used for configuration)
371  * @param local_sa our local address of the peer (IPv4-only)
372  * @param remote_sa the remote address of the peer (IPv4-only)
373  * @return #GNUNET_SYSERR on error,
374  *         #GNUNET_NO if connection reversal is unavailable,
375  *         #GNUNET_OK otherwise (presumably in progress)
376  */
377 int
378 GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
379                              const struct sockaddr_in *local_sa,
380                              const struct sockaddr_in *remote_sa);
381
382
383 /**
384  * Stop port redirection and public IP address detection for the given
385  * handle.  This frees the handle, after having sent the needed
386  * commands to close open ports.
387  *
388  * @param nh the handle to unregister
389  */
390 void
391 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh);
392
393
394 /**
395  * Handle an incoming STUN message.  This function is useful as
396  * some GNUnet service may be listening on a UDP port and might
397  * thus receive STUN messages while trying to receive other data.
398  * In this case, this function can be used to process replies
399  * to STUN requests.
400  *
401  * The function does some basic sanity checks on packet size and
402  * content, try to extract a bit of information.
403  *
404  * At the moment this only processes BIND requests, and returns the
405  * externally visible address of the request to the rest of the
406  * NAT logic.
407  *
408  * @param nh handle to the NAT service
409  * @param sender_addr address from which we got @a data
410  * @param sender_addr_len number of bytes in @a sender_addr
411  * @param data the packet
412  * @param data_size number of bytes in @a data
413  * @return #GNUNET_OK on success
414  *         #GNUNET_NO if the packet is not a STUN packet
415  *         #GNUNET_SYSERR on internal error handling the packet
416  */
417 int
418 GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
419                                const struct sockaddr *sender_addr,
420                                size_t sender_addr_len,
421                                const void *data,
422                                size_t data_size);
423
424
425 /**
426  * Handle to a request given to the resolver.  Can be used to cancel
427  * the request prior to the timeout or successful execution.  Also
428  * used to track our internal state for the request.
429  */
430 struct GNUNET_NAT_STUN_Handle;
431
432
433 /**
434  * Function called to report success or failure for
435  * NAT configuration test.
436  *
437  * @param cls closure
438  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
439  */
440 typedef void
441 (*GNUNET_NAT_TestCallback) (void *cls,
442                             enum GNUNET_NAT_StatusCode result);
443
444
445 /**
446  * Make Generic STUN request. Sends a generic stun request to the
447  * server specified using the specified socket.  If we do this,
448  * we need to watch for possible responses and call
449  * #GNUNET_NAT_stun_handle_packet() on incoming packets.
450  *
451  * @param server the address of the stun server
452  * @param port port of the stun server, in host byte order
453  * @param sock the socket used to send the request, must be a
454  *             UDP socket
455  * @param cb callback in case of error
456  * @param cb_cls closure for @a cb
457  * @return NULL on error
458  */
459 struct GNUNET_NAT_STUN_Handle *
460 GNUNET_NAT_stun_make_request (const char *server,
461                               uint16_t port,
462                               struct GNUNET_NETWORK_Handle *sock,
463                               GNUNET_NAT_TestCallback cb,
464                               void *cb_cls);
465
466
467 /**
468  * Cancel active STUN request. Frees associated resources
469  * and ensures that the callback is no longer invoked.
470  *
471  * @param rh request to cancel
472  */
473 void
474 GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh);
475
476
477 #endif
478
479 /** @} */  /* end of group */
480
481 /* end of gnunet_nat_service.h */