- doc
[oweals/gnunet.git] / src / include / gnunet_mesh_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011, 2012, 2013 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  * TODO:
27  * - need to do sanity check that this is consistent
28  *   with current ideas for the multicast layer's needs
29  */
30
31 #ifndef GNUNET_MESH_SERVICE_H
32 #define GNUNET_MESH_SERVICE_H
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #if 0                           /* keep Emacsens' auto-indent happy */
38 }
39 #endif
40 #endif
41
42 #include "gnunet_util_lib.h"
43 #include "gnunet_transport_service.h"
44
45 /**
46  * Version number of GNUnet-mesh API.
47  */
48 #define GNUNET_MESH_VERSION 0x00000001
49
50
51 /**
52  * Opaque handle to the service.
53  */
54 struct GNUNET_MESH_Handle;
55
56 /**
57  * Opaque handle to a tunnel.
58  */
59 struct GNUNET_MESH_Tunnel;
60
61
62 /**
63  * Functions with this signature are called whenever a message is
64  * received.
65  * 
66  * Each time the function must call GNUNET_MESH_receive_done on the tunnel
67  * in order to receive the next message. This doesn't need to be immediate:
68  * can be delayed if some processing is done on the message.
69  *
70  * @param cls Closure (set from GNUNET_MESH_connect).
71  * @param tunnel Connection to the other end.
72  * @param tunnel_ctx Place to store local state associated with the tunnel.
73  * @param message The actual message.
74  * 
75  * @return GNUNET_OK to keep the tunnel open,
76  *         GNUNET_SYSERR to close it (signal serious error).
77  */
78 typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
79                                             struct GNUNET_MESH_Tunnel *tunnel,
80                                             void **tunnel_ctx,
81                                             const struct GNUNET_MessageHeader *message);
82
83
84 /**
85  * Message handler.  Each struct specifies how to handle on particular
86  * type of message received.
87  */
88 struct GNUNET_MESH_MessageHandler
89 {
90   /**
91    * Function to call for messages of "type".
92    */
93   GNUNET_MESH_MessageCallback callback;
94
95   /**
96    * Type of the message this handler covers.
97    */
98   uint16_t type;
99
100   /**
101    * Expected size of messages of this type.  Use 0 for variable-size.
102    * If non-zero, messages of the given type will be discarded if they
103    * do not have the right size.
104    */
105   uint16_t expected_size;
106 };
107
108
109 /**
110  * Method called whenever another peer has added us to a tunnel
111  * the other peer initiated.
112  * Only called (once) upon reception of data with a message type which was
113  * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
114  * causes te tunnel to be ignored and no further notifications are sent about
115  * the same tunnel.
116  *
117  * @param cls closure
118  * @param tunnel new handle to the tunnel
119  * @param initiator peer that started the tunnel
120  * @param port Port this tunnel is for.
121  * @return initial tunnel context for the tunnel
122  *         (can be NULL -- that's not an error)
123  */
124 typedef void *(GNUNET_MESH_InboundTunnelNotificationHandler) (void *cls,
125                                                               struct
126                                                               GNUNET_MESH_Tunnel
127                                                               * tunnel,
128                                                               const struct
129                                                               GNUNET_PeerIdentity
130                                                               * initiator,
131                                                               uint32_t port);
132
133
134 /**
135  * Function called whenever a tunnel is destroyed.  Should clean up
136  * any associated state. 
137  * 
138  * It must NOT call GNUNET_MESH_tunnel_destroy on the tunnel.
139  *
140  * @param cls closure (set from GNUNET_MESH_connect)
141  * @param tunnel connection to the other end (henceforth invalid)
142  * @param tunnel_ctx place where local state associated
143  *                   with the tunnel is stored
144  */
145 typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
146                                              const struct GNUNET_MESH_Tunnel *
147                                              tunnel, void *tunnel_ctx);
148
149
150 /**
151  * Connect to the mesh service.
152  *
153  * @param cfg Configuration to use.
154  * @param cls Closure for the various callbacks that follow (including 
155  *            handlers in the handlers array).
156  * @param new_tunnel Function called when an *inbound* tunnel is created.
157  *                   Can be NULL if no inbound tunnels are desired.
158  * @param cleaner Function called when a tunnel is destroyed by the remote peer.
159  *                It is NOT called if GNUNET_MESH_tunnel_destroy is called on
160  *                the tunnel.
161  * @param handlers Callbacks for messages we care about, NULL-terminated. Each
162  *                 one must call GNUNET_MESH_receive_done on the tunnel to
163  *                 receive the next message.  Messages of a type that is not
164  *                 in the handlers array are ignored if received. 
165  * @param ports NULL or 0-terminated array of port numbers for incoming tunnels.
166  * 
167  * @return handle to the mesh service NULL on error
168  *         (in this case, init is never called)
169  */
170 struct GNUNET_MESH_Handle *
171 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
172                      GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
173                      GNUNET_MESH_TunnelEndHandler cleaner,
174                      const struct GNUNET_MESH_MessageHandler *handlers,
175                      const uint32_t *ports);
176
177
178 /**
179  * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
180  * disconnect callbacks will be called on any still connected peers, notifying
181  * about their disconnection. The registered inbound tunnel cleaner will be
182  * called should any inbound tunnels still exist.
183  *
184  * @param handle connection to mesh to disconnect
185  */
186 void
187 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
188
189
190 /**
191  * Create a new tunnel (we're initiator and will be allowed to add/remove peers
192  * and to broadcast).
193  *
194  * @param h mesh handle
195  * @param tunnel_ctx client's tunnel context to associate with the tunnel
196  * @param peer peer identity the tunnel should go to
197  * @param port Port number.
198  * @param buffer Flag for buffering on relay nodes.
199  * @param reliable Flag for end-to-end reliability.
200  *
201  * @return handle to the tunnel
202  */
203 struct GNUNET_MESH_Tunnel *
204 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, 
205                            void *tunnel_ctx,
206                            const struct GNUNET_PeerIdentity *peer,
207                            uint32_t port,
208                            int buffer,
209                            int reliable);
210
211
212 /**
213  * Destroy an existing tunnel.
214  * 
215  * The existing end callback for the tunnel will be called immediately.
216  * Any pending outgoing messages will be sent but no incoming messages will be
217  * accepted and no data callbacks will be called.
218  *
219  * @param tunnel Tunnel handle, becomes invalid after this call.
220  */
221 void
222 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel);
223
224
225 /**
226  * Struct to retrieve info about a tunnel.
227  */
228 struct MeshTunnelInfo {
229
230   /**
231    * Property, as listed in src/mesh/mesh.h (GNUNET_MESH_OPTION_*)
232    */
233   unsigned int prop;
234
235   /**
236    * Value, of type dependant on @c prop.
237    */
238   void *value;
239 };
240
241
242 /**
243  * Get information about a tunnel.
244  *
245  * @param tunnel Tunnel handle.
246  * 
247  * @return Allocated, {0, NULL} terminated set of tunnel properties.
248  */
249 struct MeshTunnelInfo *
250 GNUNET_MESH_tunnel_get_info (struct GNUNET_MESH_Tunnel *tunnel);
251
252
253 /**
254  * Handle for a transmission request.
255  */
256 struct GNUNET_MESH_TransmitHandle;
257
258
259 /**
260  * Ask the mesh to call "notify" once it is ready to transmit the
261  * given number of bytes to the specified tunnel.
262  * Only one call can be active at any time, to issue another request,
263  * wait for the callback or cancel the current request.
264  *
265  * @param tunnel tunnel to use for transmission
266  * @param cork is corking allowed for this transmission?
267  * @param maxdelay how long can the message wait?
268  * @param notify_size how many bytes of buffer space does notify want?
269  * @param notify function to call when buffer space is available;
270  *        will be called with NULL on timeout or if the overall queue
271  *        for this peer is larger than queue_size and this is currently
272  *        the message with the lowest priority
273  * @param notify_cls closure for notify
274  * @return non-NULL if the notify callback was queued,
275  *         NULL if we can not even queue the request (insufficient
276  *         memory); if NULL is returned, "notify" will NOT be called.
277  */
278 struct GNUNET_MESH_TransmitHandle *
279 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
280                                    struct GNUNET_TIME_Relative maxdelay,
281                                    size_t notify_size,
282                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
283                                    void *notify_cls);
284
285
286 /**
287  * Cancel the specified transmission-ready notification.
288  *
289  * @param th handle that was returned by "notify_transmit_ready".
290  */
291 void
292 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
293                                           *th);
294
295
296 /**
297  * Indicate readiness to receive the next message on a tunnel.
298  * 
299  * Should only be called once per handler called.
300  *
301  * @param tunnel Tunnel that will be allowed to call another handler.
302  */
303 void
304 GNUNET_MESH_receive_done (struct GNUNET_MESH_Tunnel *tunnel);
305
306
307
308 /******************************************************************************/
309 /********************       MONITORING /DEBUG API     *************************/
310 /******************************************************************************/
311 /* The following calls are not useful for normal MESH operation, but for      */
312 /* debug and monitoring of the mesh state. They can be safely ignored.        */
313 /* The API can change at any point without notice.                            */
314 /* Please contact the developer if you consider any of this calls useful for  */
315 /* normal mesh applications.                                                  */
316 /******************************************************************************/
317
318 /**
319  * Method called to retrieve information about each tunnel the mesh peer
320  * is aware of.
321  *
322  * @param cls Closure.
323  * @param tunnel_number Tunnel number.
324  * @param origin that started the tunnel (owner).
325  * @param target other endpoint of the tunnel
326  */
327 typedef void (*GNUNET_MESH_TunnelsCB) (void *cls,
328                                        uint32_t tunnel_number,
329                                        const struct GNUNET_PeerIdentity *origin,
330                                        const struct GNUNET_PeerIdentity *target);
331
332
333 /**
334  * Method called to retrieve information about a specific tunnel the mesh peer
335  * is aware of, including all transit nodes.
336  *
337  * @param cls Closure.
338  * @param peer Peer in the tunnel's tree.
339  * @param parent Parent of the current peer. All 0 when peer is root.
340  */
341 typedef void (*GNUNET_MESH_TunnelCB) (void *cls,
342                                       const struct GNUNET_PeerIdentity *peer,
343                                       const struct GNUNET_PeerIdentity *parent);
344
345
346 /**
347  * Request information about the running mesh peer.
348  * The callback will be called for every tunnel known to the service,
349  * listing all active peers that belong to the tunnel.
350  *
351  * If called again on the same handle, it will overwrite the previous
352  * callback and cls. To retrieve the cls, monitor_cancel must be
353  * called first.
354  *
355  * WARNING: unstable API, likely to change in the future!
356  *
357  * @param h Handle to the mesh peer.
358  * @param callback Function to call with the requested data.
359  * @param callback_cls Closure for @c callback.
360  */
361 void
362 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
363                          GNUNET_MESH_TunnelsCB callback,
364                          void *callback_cls);
365
366
367 /**
368  * Request information about a specific tunnel of the running mesh peer.
369  *
370  * WARNING: unstable API, likely to change in the future!
371  *
372  * @param h Handle to the mesh peer.
373  * @param initiator ID of the owner of the tunnel.
374  * @param tunnel_number Tunnel number.
375  * @param callback Function to call with the requested data.
376  * @param callback_cls Closure for @c callback.
377  */
378 void
379 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
380                          struct GNUNET_PeerIdentity *initiator,
381                          uint32_t tunnel_number,
382                          GNUNET_MESH_TunnelCB callback,
383                          void *callback_cls);
384
385
386 /**
387  * Cancel a monitor request. The monitor callback will not be called.
388  *
389  * WARNING: unstable API, likely to change in the future!
390  *
391  * @param h Mesh handle.
392  *
393  * @return Closure given to GNUNET_MESH_monitor, if any.
394  */
395 void *
396 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h);
397
398
399 /**
400  * Create a message queue for a mesh tunnel.
401  * The message queue can only be used to transmit messages,
402  * not to receive them.
403  *
404  * @param tunnel the tunnel to create the message qeue for
405  * @return a message queue to messages over the tunnel
406  */
407 struct GNUNET_MQ_Handle *
408 GNUNET_MESH_mq_create (struct GNUNET_MESH_Tunnel *tunnel);
409
410
411 #if 0                           /* keep Emacsens' auto-indent happy */
412 {
413 #endif
414 #ifdef __cplusplus
415 }
416 #endif
417
418 /* ifndef GNUNET_MESH_SERVICE_H */
419 #endif
420 /* end of gnunet_mesh_service.h */