Merge remote-tracking branch 'origin/master' into credentials
[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    * 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    * Bitmask for "any" address.
127    */
128   GNUNET_NAT_AC_ANY = 65535
129   
130 };
131
132
133 /**
134  * Signature of the callback passed to #GNUNET_NAT_register() for
135  * a function to call whenever our set of 'valid' addresses changes.
136  *
137  * @param cls closure
138  * @param add_remove #GNUNET_YES to add a new public IP address, 
139  *                   #GNUNET_NO to remove a previous (now invalid) one
140  * @param ac address class the address belongs to
141  * @param addr either the previous or the new public IP address
142  * @param addrlen actual length of the @a addr
143  */
144 typedef void
145 (*GNUNET_NAT_AddressCallback) (void *cls,
146                                int add_remove,
147                                enum GNUNET_NAT_AddressClass ac,
148                                const struct sockaddr *addr,
149                                socklen_t addrlen);
150
151
152 /**
153  * Signature of the callback passed to #GNUNET_NAT_register().
154  * for a function to call whenever someone asks us to do connection
155  * reversal.
156  *
157  * @param cls closure
158  * @param remote_addr public IP address of the other peer
159  * @param remote_addrlen actual length of the @a remote_addr
160  */
161 typedef void
162 (*GNUNET_NAT_ReversalCallback) (void *cls,
163                                 const struct sockaddr *remote_addr,
164                                 socklen_t remote_addrlen);
165
166
167 /**
168  * Handle for active NAT registrations.
169  */
170 struct GNUNET_NAT_Handle;
171
172
173 /**
174  * Attempt to enable port redirection and detect public IP address
175  * contacting UPnP or NAT-PMP routers on the local network. Use @a
176  * addr to specify to which of the local host's addresses should the
177  * external port be mapped. The port is taken from the corresponding
178  * sockaddr_in[6] field.  The NAT module should call the given @a
179  * address_callback for any 'plausible' external address.
180  *
181  * @param cfg configuration to use
182  * @param proto protocol this is about, IPPROTO_TCP or IPPROTO_UDP
183  * @param hole_external hostname and port of manually punched hole in NAT, otherwise NULL (or empty string)
184  * @param num_addrs number of addresses in @a addrs
185  * @param addrs list of local addresses packets should be redirected to
186  * @param addrlens actual lengths of the addresses in @a addrs
187  * @param address_callback function to call everytime the public IP address changes
188  * @param reversal_callback function to call if someone wants connection reversal from us,
189  *        NULL if connection reversal is not supported
190  * @param callback_cls closure for callbacks
191  * @return NULL on error, otherwise handle that can be used to unregister
192  */
193 struct GNUNET_NAT_Handle *
194 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
195                      uint8_t proto,
196                      const char *hole_external,
197                      unsigned int num_addrs,
198                      const struct sockaddr **addrs,
199                      const socklen_t *addrlens,
200                      GNUNET_NAT_AddressCallback address_callback,
201                      GNUNET_NAT_ReversalCallback reversal_callback,
202                      void *callback_cls);
203
204
205 /**
206  * Test if the given address is (currently) a plausible IP address for
207  * this peer.  Mostly a convenience function so that clients do not
208  * have to explicitly track all IPs that the #GNUNET_NAT_AddressCallback
209  * has returned so far.
210  *
211  * @param nh the handle returned by register
212  * @param addr IP address to test (IPv4 or IPv6)
213  * @param addrlen number of bytes in @a addr
214  * @return #GNUNET_YES if the address is plausible,
215  *         #GNUNET_NO if the address is not plausible,
216  *         #GNUNET_SYSERR if the address is malformed
217  */
218 int
219 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
220                          const void *addr,
221                          socklen_t addrlen);
222
223
224 /**
225  * We learned about a peer (possibly behind NAT) so run the
226  * gnunet-nat-client to send dummy ICMP responses to cause
227  * that peer to connect to us (connection reversal).
228  *
229  * @param nh handle (used for configuration)
230  * @param local_sa our local address of the peer (IPv4-only)
231  * @param remote_sa the remote address of the peer (IPv4-only)
232  * @return #GNUNET_SYSERR on error, 
233  *         #GNUNET_NO if connection reversal is unavailable,
234  *         #GNUNET_OK otherwise (presumably in progress)
235  */
236 int
237 GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
238                              const struct sockaddr_in *local_sa,
239                              const struct sockaddr_in *remote_sa);
240
241
242 /**
243  * Stop port redirection and public IP address detection for the given
244  * handle.  This frees the handle, after having sent the needed
245  * commands to close open ports.
246  *
247  * @param nh the handle to unregister
248  */
249 void
250 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh);
251
252
253 /**
254  * Handle to a NAT test.
255  */
256 struct GNUNET_NAT_Test;
257
258
259 /**
260  * Error Types for the NAT subsystem (which can then later be converted/resolved to a string)
261  */
262 enum GNUNET_NAT_StatusCode
263 {
264   /**
265    * Just the default
266    */
267   GNUNET_NAT_ERROR_SUCCESS = GNUNET_OK,
268
269   /**
270    * IPC Failure
271    */
272   GNUNET_NAT_ERROR_IPC_FAILURE,
273
274   /**
275    * Failure in network subsystem, check permissions
276    */
277   GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR,
278
279   /**
280    * test timed out
281    */
282   GNUNET_NAT_ERROR_TIMEOUT,
283
284   /**
285    * detected that we are offline
286    */
287   GNUNET_NAT_ERROR_NOT_ONLINE,
288
289   /**
290    * `upnpc` command not found
291    */
292   GNUNET_NAT_ERROR_UPNPC_NOT_FOUND,
293
294   /**
295    * Failed to run `upnpc` command
296    */
297   GNUNET_NAT_ERROR_UPNPC_FAILED,
298
299   /**
300    * `upnpc' command took too long, process killed
301    */
302   GNUNET_NAT_ERROR_UPNPC_TIMEOUT,
303
304   /**
305    * `upnpc' command failed to establish port mapping
306    */
307   GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED,
308
309   /**
310    * `external-ip' command not found
311    */
312   GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND,
313
314   /**
315    * Failed to run `external-ip` command
316    */
317   GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED,
318
319   /**
320    * `external-ip' command output invalid
321    */
322   GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID,
323
324   /**
325    * "no valid address was returned by `external-ip'"
326    */
327   GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID,
328
329   /**
330    * Could not determine interface with internal/local network address
331    */
332   GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO,
333
334   /**
335    * No working gnunet-helper-nat-server found
336    */
337   GNUNET_NAT_ERROR_HELPER_NAT_SERVER_NOT_FOUND,
338
339   /**
340    * NAT test could not be initialized
341    */
342   GNUNET_NAT_ERROR_NAT_TEST_START_FAILED,
343
344   /**
345    * NAT test timeout
346    */
347   GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT,
348
349   /**
350    * NAT test failed to initiate
351    */
352   GNUNET_NAT_ERROR_NAT_REGISTER_FAILED,
353
354   /**
355    *
356    */
357   GNUNET_NAT_ERROR_HELPER_NAT_CLIENT_NOT_FOUND
358   
359 };
360
361
362 /**
363  * Function called to report success or failure for
364  * NAT configuration test.
365  *
366  * @param cls closure
367  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
368  */
369 typedef void
370 (*GNUNET_NAT_TestCallback) (void *cls,
371                             enum GNUNET_NAT_StatusCode result);
372
373
374 /**
375  * Handle an incoming STUN message.  This function is useful as
376  * some GNUnet service may be listening on a UDP port and might
377  * thus receive STUN messages while trying to receive other data.
378  * In this case, this function can be used to process replies
379  * to STUN requests.
380  *
381  * The function does some basic sanity checks on packet size and
382  * content, try to extract a bit of information.
383  * 
384  * At the moment this only processes BIND requests, and returns the
385  * externally visible address of the request to the rest of the
386  * NAT logic.
387  *
388  * @param nh handle to the NAT service
389  * @param sender_addr address from which we got @a data
390  * @param sender_addr_len number of bytes in @a sender_addr
391  * @param data the packet
392  * @param data_size number of bytes in @a data
393  * @return #GNUNET_OK on success
394  *         #GNUNET_NO if the packet is not a STUN packet
395  *         #GNUNET_SYSERR on internal error handling the packet
396  */
397 int
398 GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
399                                const struct sockaddr *sender_addr,
400                                size_t sender_addr_len,
401                                const void *data,
402                                size_t data_size);
403
404
405 /**
406  * Handle to a request given to the resolver.  Can be used to cancel
407  * the request prior to the timeout or successful execution.  Also
408  * used to track our internal state for the request.
409  */
410 struct GNUNET_NAT_STUN_Handle;
411
412
413 /**
414  * Make Generic STUN request. Sends a generic stun request to the
415  * server specified using the specified socket.  If we do this,
416  * we need to watch for possible responses and call
417  * #GNUNET_NAT_stun_handle_packet() on incoming packets.
418  *
419  * @param server the address of the stun server
420  * @param port port of the stun server, in host byte order
421  * @param sock the socket used to send the request, must be a
422  *             UDP socket
423  * @param cb callback in case of error
424  * @param cb_cls closure for @a cb
425  * @return NULL on error
426  */
427 struct GNUNET_NAT_STUN_Handle *
428 GNUNET_NAT_stun_make_request (const char *server,
429                               uint16_t port,
430                               struct GNUNET_NETWORK_Handle *sock,
431                               GNUNET_NAT_TestCallback cb,
432                               void *cb_cls);
433
434
435 /**
436  * Cancel active STUN request. Frees associated resources
437  * and ensures that the callback is no longer invoked.
438  *
439  * @param rh request to cancel
440  */
441 void
442 GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh);
443
444
445 /**
446  * Start testing if NAT traversal works using the given configuration
447  * (IPv4-only).  The transport adapters should be down while using
448  * this function.
449  *
450  * @param cfg configuration for the NAT traversal
451  * @param proto protocol to test, i.e. IPPROTO_TCP or IPPROTO_UDP
452  * @param bind_ip IPv4 address to bind to
453  * @param bnd_port port to bind to, 0 to test connection reversal
454  * @param extern_ip IPv4 address to externally advertise
455  * @param extern_port externally advertised port to use
456  * @param report function to call with the result of the test
457  * @param report_cls closure for @a report
458  * @return handle to cancel NAT test
459  */
460 struct GNUNET_NAT_Test *
461 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
462                        uint8_t proto,
463                        struct in_addr bind_ip,
464                        uint16_t bnd_port,
465                        struct in_addr extern_ip,
466                        uint16_t extern_port,
467                        GNUNET_NAT_TestCallback report,
468                        void *report_cls);
469
470
471 /**
472  * Stop an active NAT test.
473  *
474  * @param tst test to stop.
475  */
476 void
477 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
478
479
480 /**
481  * Handle to auto-configuration in progress.
482  */
483 struct GNUNET_NAT_AutoHandle;
484
485
486 /**
487  * What the situation of the NAT connectivity
488  */
489 enum GNUNET_NAT_Type
490 {
491   /**
492    * We have a direct connection
493    */
494   GNUNET_NAT_TYPE_NO_NAT = GNUNET_OK,
495
496   /**
497    * We are under a NAT but cannot traverse it
498    */
499   GNUNET_NAT_TYPE_UNREACHABLE_NAT,
500
501   /**
502    * We can traverse using STUN
503    */
504   GNUNET_NAT_TYPE_STUN_PUNCHED_NAT,
505
506   /**
507    * We can traverse using UPNP
508    */
509   GNUNET_NAT_TYPE_UPNP_NAT,
510
511   /**
512    * We know nothing about the NAT.
513    */
514   GNUNET_NAT_TYPE_UNKNOWN
515
516 };
517
518
519 /**
520  * Converts `enum GNUNET_NAT_StatusCode` to string
521  *
522  * @param err error code to resolve to a string
523  * @return point to a static string containing the error code
524  */
525 const char *
526 GNUNET_NAT_status2string (enum GNUNET_NAT_StatusCode err);
527
528
529 /**
530  * Function called with the result from the autoconfiguration.
531  *
532  * @param cls closure
533  * @param diff minimal suggested changes to the original configuration
534  *             to make it work (as best as we can)
535  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
536  * @param type what the situation of the NAT
537  */
538 typedef void
539 (*GNUNET_NAT_AutoResultCallback)(void *cls,
540                                  const struct GNUNET_CONFIGURATION_Handle *diff,
541                                  enum GNUNET_NAT_StatusCode result,
542                                  enum GNUNET_NAT_Type type);
543
544
545 /**
546  * Start auto-configuration routine.  The transport adapters should
547  * be stopped while this function is called.
548  *
549  * @param cfg initial configuration
550  * @param cb function to call with autoconfiguration result
551  * @param cb_cls closure for @a cb
552  * @return handle to cancel operation
553  */
554 struct GNUNET_NAT_AutoHandle *
555 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
556                              GNUNET_NAT_AutoResultCallback cb,
557                              void *cb_cls);
558
559
560 /**
561  * Abort autoconfiguration.
562  *
563  * @param ah handle for operation to abort
564  */
565 void
566 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah);
567
568
569 #endif
570
571 /** @} */  /* end of group */
572
573 /* end of gnunet_nat_service.h */