doxy
[oweals/gnunet.git] / src / include / gnunet_mesh_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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_mesh_service.h
23  * @brief mesh service; establish tunnels to distant peers
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_MESH_SERVICE_H
28 #define GNUNET_MESH_SERVICE_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 #include "gnunet_util_lib.h"
39 #include "gnunet_transport_service.h"
40
41 /**
42  * Version number of GNUnet-mesh API.
43  */
44 #define GNUNET_MESH_VERSION 0x00000000
45
46
47 /**
48  * Opaque handle to the service.
49  */
50 struct GNUNET_MESH_Handle;
51
52 /**
53  * Opaque handle to a tunnel.
54  */
55 struct GNUNET_MESH_Tunnel;
56
57 /**
58  * Functions with this signature are called whenever a message is
59  * received.
60  *
61  * @param cls closure (set from GNUNET_MESH_connect)
62  * @param tunnel connection to the other end
63  * @param tunnel_ctx place to store local state associated with the tunnel
64  * @param sender who sent the message
65  * @param message the actual message
66  * @param atsi performance data for the connection
67  * @return GNUNET_OK to keep the connection open,
68  *         GNUNET_SYSERR to close it (signal serious error)
69  */
70 typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
71                                             struct GNUNET_MESH_Tunnel * tunnel,
72                                             void **tunnel_ctx,
73                                             const struct GNUNET_PeerIdentity *
74                                             sender,
75                                             const struct GNUNET_MessageHeader *
76                                             message,
77                                             const struct GNUNET_ATS_Information
78                                             * atsi);
79
80
81 /**
82  * Message handler.  Each struct specifies how to handle on particular
83  * type of message received.
84  */
85 struct GNUNET_MESH_MessageHandler
86 {
87   /**
88    * Function to call for messages of "type".
89    */
90   GNUNET_MESH_MessageCallback callback;
91
92   /**
93    * Type of the message this handler covers.
94    */
95   uint16_t type;
96
97   /**
98    * Expected size of messages of this type.  Use 0 for variable-size.
99    * If non-zero, messages of the given type will be discarded if they
100    * do not have the right size.
101    */
102   uint16_t expected_size;
103
104 };
105
106
107 /**
108  * Method called whenever another peer has added us to a tunnel
109  * the other peer initiated.
110  *
111  * @param cls closure
112  * @param tunnel new handle to the tunnel
113  * @param initiator peer that started the tunnel
114  * @param atsi performance information for the tunnel
115  * @return initial tunnel context for the tunnel
116  *         (can be NULL -- that's not an error)
117  */
118 typedef void *(GNUNET_MESH_InboundTunnelNotificationHandler) (void *cls,
119                                                               struct
120                                                               GNUNET_MESH_Tunnel
121                                                               * tunnel,
122                                                               const struct
123                                                               GNUNET_PeerIdentity
124                                                               * initiator,
125                                                               const struct
126                                                               GNUNET_ATS_Information
127                                                               * atsi);
128
129
130 /**
131  * Function called whenever an inbound tunnel is destroyed.  Should clean up
132  * any associated state.  This function is NOT called if the client has
133  * explicitly asked for the tunnel to be destroyed using
134  * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
135  * the tunnel.
136  *
137  * @param cls closure (set from GNUNET_MESH_connect)
138  * @param tunnel connection to the other end (henceforth invalid)
139  * @param tunnel_ctx place where local state associated
140  *                   with the tunnel is stored
141  */
142 typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
143                                              const struct GNUNET_MESH_Tunnel *
144                                              tunnel, void *tunnel_ctx);
145
146
147 /**
148  * Type for an application.  Values defined in gnunet_applications.h
149  */
150 typedef uint32_t GNUNET_MESH_ApplicationType;
151
152
153 /**
154  * Connect to the mesh service.
155  *
156  * @param cfg configuration to use
157  * @param cls closure for the various callbacks that follow
158  *            (including handlers in the handlers array)
159  * @param new_tunnel function called when an *inbound* tunnel is created
160  * @param cleaner function called when an *inbound* tunnel is destroyed by the
161  *                remote peer, it is *not* called if GNUNET_MESH_tunnel_destroy
162  *                is called on the tunnel
163  * @param handlers callbacks for messages we care about, NULL-terminated
164  *                note that the mesh is allowed to drop notifications about
165  *                inbound messages if the client does not process them fast
166  *                enough (for this notification type, a bounded queue is used)
167  * @param stypes list of the applications that this client claims to provide
168  * @return handle to the mesh service NULL on error
169  *         (in this case, init is never called)
170  */
171 struct GNUNET_MESH_Handle *
172 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
173                      GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
174                      GNUNET_MESH_TunnelEndHandler cleaner,
175                      const struct GNUNET_MESH_MessageHandler *handlers,
176                      const GNUNET_MESH_ApplicationType *stypes);
177
178
179 /**
180  * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
181  * disconnect callbacks will be called on any still connected peers, notifying
182  * about their disconnection. The registered inbound tunnel cleaner will be
183  * called should any inbound tunnels still exist.
184  *
185  * @param handle connection to mesh to disconnect
186  */
187 void
188 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
189
190
191 /**
192  * Method called whenever a peer has disconnected from the tunnel.
193  * Implementations of this callback must NOT call
194  * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
195  * to run in some other task later.  However, calling 
196  * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
197  *
198  * @param cls closure
199  * @param peer peer identity the tunnel stopped working with
200  */
201 typedef void (*GNUNET_MESH_PeerDisconnectHandler) (void *cls,
202                                                    const struct
203                                                    GNUNET_PeerIdentity * peer);
204
205
206 /**
207  * Method called whenever a peer has connected to the tunnel.
208  *
209  * @param cls closure
210  * @param peer peer identity the tunnel was created to, NULL on timeout
211  * @param atsi performance data for the connection
212  *
213  * TODO: change to return int to let client allow the new peer or not?
214  */
215 typedef void (*GNUNET_MESH_PeerConnectHandler) (void *cls,
216                                                 const struct GNUNET_PeerIdentity
217                                                 * peer,
218                                                 const struct
219                                                 GNUNET_ATS_Information * atsi);
220
221
222 /**
223  * Announce to ther peer the availability of services described by the regex,
224  * in order to be reachable to other peers via connect_by_string.
225  * 
226  * Note that the first 8 characters are considered to be part of a prefix,
227  * (for instance 'gnunet://'). If you put a variable part in there (*, +. ()),
228  * all matching strings will be stored in the DHT.
229  *
230  * @param h handle to mesh.
231  * @param regex string with the regular expression describing local services.
232  */
233 void
234 GNUNET_MESH_announce_regex (struct GNUNET_MESH_Handle *h,
235                             const char *regex);
236
237
238 /**
239  * Create a new tunnel (we're initiator and will be allowed to add/remove peers
240  * and to broadcast).
241  *
242  * @param h mesh handle
243  * @param tunnel_ctx client's tunnel context to associate with the tunnel
244  * @param connect_handler function to call when peers are actually connected
245  * @param disconnect_handler function to call when peers are disconnected
246  * @param handler_cls closure for connect/disconnect handlers
247  */
248 struct GNUNET_MESH_Tunnel *
249 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, void *tunnel_ctx,
250                            GNUNET_MESH_PeerConnectHandler connect_handler,
251                            GNUNET_MESH_PeerDisconnectHandler disconnect_handler,
252                            void *handler_cls);
253
254 /**
255  * Destroy an existing tunnel. The existing callback for the tunnel will NOT
256  * be called.
257  *
258  * @param tunnel tunnel handle
259  */
260 void
261 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel);
262
263
264 /**
265  * Request that the tunnel data rate is limited to the speed of the slowest
266  * receiver.
267  * 
268  * @param tunnel Tunnel affected.
269  */
270 void
271 GNUNET_MESH_tunnel_speed_min (struct GNUNET_MESH_Tunnel *tunnel);
272
273
274 /**
275  * Request that the tunnel data rate is limited to the speed of the fastest
276  * receiver. This is the default behavior.
277  * 
278  * @param tunnel Tunnel affected.
279  */
280 void
281 GNUNET_MESH_tunnel_speed_max (struct GNUNET_MESH_Tunnel *tunnel);
282
283
284 /**
285  * Turn on/off the buffering status of the tunnel.
286  * 
287  * @param tunnel Tunnel affected.
288  * @param buffer GNUNET_YES to turn buffering on (default),
289  *               GNUNET_NO otherwise.
290  */
291 void
292 GNUNET_MESH_tunnel_buffer (struct GNUNET_MESH_Tunnel *tunnel, int buffer);
293
294
295 /**
296  * Request that a peer should be added to the tunnel.  The connect handler
297  * will be called when the peer connects
298  *
299  * @param tunnel handle to existing tunnel
300  * @param peer peer to add
301  */
302 void
303 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
304                                       const struct GNUNET_PeerIdentity *peer);
305
306
307 /**
308  * Request that a peer should be removed from the tunnel.  The existing
309  * disconnect handler will be called ONCE if we were connected.
310  *
311  * @param tunnel handle to existing tunnel
312  * @param peer peer to remove
313  */
314 void
315 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
316                                       const struct GNUNET_PeerIdentity *peer);
317
318
319 /**
320  * Request that the mesh should try to connect to a peer supporting the given
321  * message type.
322  *
323  * @param tunnel handle to existing tunnel
324  * @param app_type application type that must be supported by the peer
325  *                 (MESH should discover peer in proximity handling this type)
326  */
327 void
328 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
329                                           GNUNET_MESH_ApplicationType app_type);
330
331
332 /**
333  * Request that the mesh should try to connect to a peer matching the
334  * description given in the service string.
335  *
336  * @param tunnel handle to existing tunnel
337  * @param description string describing the destination node requirements
338  */
339 void
340 GNUNET_MESH_peer_request_connect_by_string (struct GNUNET_MESH_Tunnel *tunnel,
341                                             const char *description);
342
343
344 /**
345  * Request that the given peer isn't added to this tunnel in calls to
346  * connect_by_* calls, (due to misbehaviour, bad performance, ...).
347  *
348  * @param tunnel handle to existing tunnel.
349  * @param peer peer identity of the peer which should be blacklisted
350  *                  for the tunnel.
351  */
352 void
353 GNUNET_MESH_peer_blacklist (struct GNUNET_MESH_Tunnel *tunnel,
354                             const struct GNUNET_PeerIdentity *peer);
355
356
357 /**
358  * Request that the given peer isn't blacklisted anymore from this tunnel,
359  * and therefore can be added in future calls to connect_by_*.
360  * The peer must have been previously blacklisted for this tunnel.
361  *
362  * @param tunnel handle to existing tunnel.
363  * @param peer peer identity of the peer which shouldn't be blacklisted
364  *                  for the tunnel anymore.
365  */
366 void
367 GNUNET_MESH_peer_unblacklist (struct GNUNET_MESH_Tunnel *tunnel,
368                               const struct GNUNET_PeerIdentity *peer);
369
370
371 /**
372  * Handle for a transmission request.
373  */
374 struct GNUNET_MESH_TransmitHandle;
375
376
377 /**
378  * Ask the mesh to call "notify" once it is ready to transmit the
379  * given number of bytes to the specified tunnel or target.
380  * Only one call can be active at any time, to issue another request,
381  * wait for the callback or cancel the current request.
382  *
383  * @param tunnel tunnel to use for transmission
384  * @param cork is corking allowed for this transmission?
385  * @param maxdelay how long can the message wait?
386  * @param target destination for the message
387  *               NULL for multicast to all tunnel targets
388  * @param notify_size how many bytes of buffer space does notify want?
389  * @param notify function to call when buffer space is available;
390  *        will be called with NULL on timeout or if the overall queue
391  *        for this peer is larger than queue_size and this is currently
392  *        the message with the lowest priority
393  * @param notify_cls closure for notify
394  * @return non-NULL if the notify callback was queued,
395  *         NULL if we can not even queue the request (insufficient
396  *         memory); if NULL is returned, "notify" will NOT be called.
397  */
398 struct GNUNET_MESH_TransmitHandle *
399 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
400                                    struct GNUNET_TIME_Relative maxdelay,
401                                    const struct GNUNET_PeerIdentity *target,
402                                    size_t notify_size,
403                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
404                                    void *notify_cls);
405
406
407 /**
408  * Cancel the specified transmission-ready notification.
409  *
410  * @param th handle that was returned by "notify_transmit_ready".
411  */
412 void
413 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
414                                           *th);
415
416
417 /**
418  * Transition API for tunnel ctx management
419  * 
420  * FIXME deprecated
421  */
422 void
423 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data);
424
425 /**
426  * Transition API for tunnel ctx management
427  * 
428  * FIXME deprecated
429  */
430 void *
431 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel);
432
433
434 #if 0                           /* keep Emacsens' auto-indent happy */
435 {
436 #endif
437 #ifdef __cplusplus
438 }
439 #endif
440
441 /* ifndef GNUNET_MESH_SERVICE_H */
442 #endif
443 /* end of gnunet_mesh_service.h */