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