initial NAT lib commit (UPnP and NAT-PMP support)
[oweals/gnunet.git] / src / include / gnunet_nat_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_nat_lib.h
23  * @brief Library handling UPnP and NAT-PMP port forwarding and
24  *     external IP address retrieval
25  *
26  * @author Milan Bouchet-Valat
27  */
28
29 #ifndef GNUNET_NAT_LIB_H
30 #define GNUNET_NAT_LIB_H 1
31
32 #include "platform.h"
33 #include "gnunet_util_lib.h"
34 #include "upnp.h"
35 #include "natpmp.h"
36
37 #include <inttypes.h>
38
39 /* Used to communicate with the UPnP and NAT-PMP plugins */
40 typedef enum
41 {
42   GNUNET_NAT_PORT_ERROR,
43   GNUNET_NAT_PORT_UNMAPPED,
44   GNUNET_NAT_PORT_UNMAPPING,
45   GNUNET_NAT_PORT_MAPPING,
46   GNUNET_NAT_PORT_MAPPED
47 }
48 GNUNET_NAT_port_forwarding;
49
50 /**
51  * Signature of the callback passed to GNUNET_NAT_register.
52  *
53  * @cls closure
54  * @add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
55  *     the previous (now invalid) one
56  * @addr either the previous or the new public IP address
57  * @addrlen actual lenght of the address
58  */
59 typedef void (*GNUNET_NAT_AddressCallback) (void *cls, int add_remove,
60                                             const struct sockaddr * addr,
61                                             socklen_t addrlen);
62
63 typedef struct GNUNET_NAT_Handle GNUNET_NAT_Handle;
64
65 /**
66  * Attempt to enable port redirection and detect public IP address contacting
67  * UPnP or NAT-PMP routers on the local network. Use @addr to specify to which
68  * of the local host's addresses should the external port be mapped. The port
69  * is taken from the corresponding sockaddr_in[6] field.
70  *
71  * @sched the sheduler used in the program
72  * @addr the local address packets should be redirected to
73  * @addrlen actual lenght of the address
74  * @callback function to call everytime the public IP address changes
75  * @callback_cls closure for @callback
76  */
77 struct GNUNET_NAT_Handle *GNUNET_NAT_register (struct GNUNET_SCHEDULER_Handle
78                                                *sched,
79                                                const struct sockaddr *addr,
80                                                socklen_t addrlen,
81                                                GNUNET_NAT_AddressCallback
82                                                callback, void *callback_cls);
83
84 /**
85  * Stop port redirection and public IP address detection for the given handle.
86  * This frees the handle, after having sent the needed commands to close open ports.
87  *
88  * @h the handle to stop
89  */
90 void GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h);
91
92 /**
93  * Compare the sin(6)_addr fields of AF_INET or AF_INET(6) sockaddr.
94  * @param a first sockaddr
95  * @param second sockaddr
96  * @returns 0 if addresses are equal, non-null value otherwise */
97 int GNUNET_NAT_cmp_addr (const struct sockaddr *a, const struct sockaddr *b);
98
99 #endif /* GNUNET_NAT_LIB_H */