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