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