indentation
[oweals/gnunet.git] / src / include / gnunet_nat_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2007, 2008, 2009, 2010, 2011 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 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., 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
31
32 #include "gnunet_util_lib.h"
33
34 /**
35  * Signature of the callback passed to GNUNET_NAT_register for
36  * a function to call whenever our set of 'valid' addresses changes.
37  *
38  * @param cls closure
39  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
40  *     the previous (now invalid) one
41  * @param addr either the previous or the new public IP address
42  * @param addrlen actual lenght of the address
43  */
44 typedef void (*GNUNET_NAT_AddressCallback) (void *cls,
45                                             int add_remove,
46                                             const struct sockaddr * addr,
47                                             socklen_t addrlen);
48
49
50 /**
51  * Signature of the callback passed to GNUNET_NAT_register
52  * for a function to call whenever someone asks us to do connection
53  * reversal.
54  *
55  * @param cls closure
56  * @param addr public IP address of the other peer
57  * @param addrlen actual lenght of the address
58  */
59 typedef void (*GNUNET_NAT_ReversalCallback) (void *cls,
60                                              const struct sockaddr * addr,
61                                              socklen_t addrlen);
62
63
64 /**
65  * Handle for active NAT registrations.
66  */
67 struct GNUNET_NAT_Handle;
68
69
70 /**
71  * Attempt to enable port redirection and detect public IP address contacting
72  * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
73  * of the local host's addresses should the external port be mapped. The port
74  * is taken from the corresponding sockaddr_in[6] field.  The NAT module
75  * should call the given callback for any 'plausible' external address.
76  *
77  * @param cfg configuration to use
78  * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
79  * @param adv_port advertised port (port we are either bound to or that our OS
80  *                 locally performs redirection from to our bound port).
81  * @param num_addrs number of addresses in 'addrs'
82  * @param addrs list of local addresses packets should be redirected to
83  * @param addrlens actual lengths of the addresses
84  * @param address_callback function to call everytime the public IP address changes
85  * @param reversal_callback function to call if someone wants connection reversal from us,
86  *        NULL if connection reversal is not supported
87  * @param callback_cls closure for callback
88  * @return NULL on error, otherwise handle that can be used to unregister 
89  */
90 struct GNUNET_NAT_Handle *GNUNET_NAT_register (const struct
91                                                GNUNET_CONFIGURATION_Handle *cfg,
92                                                int is_tcp, uint16_t adv_port,
93                                                unsigned int num_addrs,
94                                                const struct sockaddr **addrs,
95                                                const socklen_t * addrlens,
96                                                GNUNET_NAT_AddressCallback
97                                                address_callback,
98                                                GNUNET_NAT_ReversalCallback
99                                                reversal_callback,
100                                                void *callback_cls);
101
102
103 /**
104  * Test if the given address is (currently) a plausible IP address for this peer.
105  *
106  * @param h the handle returned by register
107  * @param addr IP address to test (IPv4 or IPv6)
108  * @param addrlen number of bytes in addr
109  * @return GNUNET_YES if the address is plausible,
110  *         GNUNET_NO if the address is not plausible,
111  *         GNUNET_SYSERR if the address is malformed
112  */
113 int
114 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
115                          const void *addr, socklen_t addrlen);
116
117
118 /**
119  * We learned about a peer (possibly behind NAT) so run the
120  * gnunet-nat-client to send dummy ICMP responses to cause
121  * that peer to connect to us (connection reversal).
122  *
123  * @param h handle (used for configuration)
124  * @param sa the address of the peer (IPv4-only)
125  */
126 void
127 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
128                        const struct sockaddr_in *sa);
129
130
131
132 /**
133  * Stop port redirection and public IP address detection for the given handle.
134  * This frees the handle, after having sent the needed commands to close open ports.
135  *
136  * @param h the handle to stop
137  */
138 void GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h);
139
140
141 /**
142  * Handle to a NAT test.
143  */
144 struct GNUNET_NAT_Test;
145
146 /**
147  * Function called to report success or failure for
148  * NAT configuration test.
149  *
150  * @param cls closure
151  * @param success GNUNET_OK on success, GNUNET_NO on failure,
152  *                GNUNET_SYSERR if the test could not be 
153  *                properly started (internal failure)
154  */
155 typedef void (*GNUNET_NAT_TestCallback) (void *cls, int success);
156
157 /**
158  * Start testing if NAT traversal works using the
159  * given configuration (IPv4-only).
160  *
161  * @param cfg configuration for the NAT traversal
162  * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
163  * @param bnd_port port to bind to, 0 for connection reversal
164  * @param adv_port externally advertised port to use
165  * @param report function to call with the result of the test
166  * @param report_cls closure for report
167  * @return handle to cancel NAT test
168  */
169 struct GNUNET_NAT_Test *GNUNET_NAT_test_start (const struct
170                                                GNUNET_CONFIGURATION_Handle *cfg,
171                                                int is_tcp, uint16_t bnd_port,
172                                                uint16_t adv_port,
173                                                GNUNET_NAT_TestCallback report,
174                                                void *report_cls);
175
176
177 /**
178  * Stop an active NAT test.
179  *
180  * @param tst test to stop.
181  */
182 void GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
183
184
185 /**
186  * Signature of a callback that is given an IP address.
187  *
188  * @param cls closure
189  * @param addr the address, NULL on errors
190  */
191 typedef void (*GNUNET_NAT_IPCallback) (void *cls, const struct in_addr * addr);
192
193
194
195 /**
196  * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation.
197  */
198 struct GNUNET_NAT_ExternalHandle;
199
200
201 /**
202  * Try to get the external IPv4 address of this peer.
203  *
204  * @param timeout when to fail
205  * @param cb function to call with result
206  * @param cb_cls closure for 'cb'
207  * @return handle for cancellation (can only be used until 'cb' is called), NULL on error
208  */
209 struct GNUNET_NAT_ExternalHandle *GNUNET_NAT_mini_get_external_ipv4 (struct
210                                                                      GNUNET_TIME_Relative
211                                                                      timeout,
212                                                                      GNUNET_NAT_IPCallback
213                                                                      cb,
214                                                                      void
215                                                                      *cb_cls);
216
217
218 /**
219  * Cancel operation.
220  *
221  * @param eh operation to cancel
222  */
223 void
224 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh);
225
226
227 /**
228  * Handle to a mapping created with upnpc.
229  */
230 struct GNUNET_NAT_MiniHandle;
231
232
233 /**
234  * Start mapping the given port using (mini)upnpc.  This function
235  * should typically not be used directly (it is used within the
236  * general-purpose 'GNUNET_NAT_register' code).  However, it can be
237  * used if specifically UPnP-based NAT traversal is to be used or
238  * tested.
239  * 
240  * @param port port to map
241  * @param is_tcp GNUNET_YES to map TCP, GNUNET_NO for UDP
242  * @param ac function to call with mapping result
243  * @param ac_cls closure for 'ac'
244  * @return NULL on error
245  */
246 struct GNUNET_NAT_MiniHandle *GNUNET_NAT_mini_map_start (uint16_t port,
247                                                          int is_tcp,
248                                                          GNUNET_NAT_AddressCallback
249                                                          ac, void *ac_cls);
250
251
252 /**
253  * Remove a mapping created with (mini)upnpc.  Calling
254  * this function will give 'upnpc' 1s to remove tha mapping,
255  * so while this function is non-blocking, a task will be
256  * left with the scheduler for up to 1s past this call.
257  * 
258  * @param mini the handle
259  */
260 void GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini);
261
262
263 #endif
264
265 /* end of gnunet_nat_lib.h */