extending bandwidth tracker api to support notifications
[oweals/gnunet.git] / src / include / gnunet_mesh_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009 - 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 channels 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 0x00000003
45
46
47 /**
48  * Opaque handle to the service.
49  */
50 struct GNUNET_MESH_Handle;
51
52 /**
53  * Opaque handle to a channel.
54  */
55 struct GNUNET_MESH_Channel;
56
57
58 /**
59  * Channel options.
60  * Second line indicates filed in the MeshChannelInfo union carrying the answer.
61  */
62 enum GNUNET_MESH_ChannelOption
63 {
64   /**
65    * Default options: unreliable, default buffering, not out of order.
66    */
67   GNUNET_MESH_OPTION_DEFAULT    = 0x0,
68
69   /**
70    * Disable buffering on intermediate nodes (for minimum latency).
71    * Yes/No.
72    */
73   GNUNET_MESH_OPTION_NOBUFFER   = 0x1,
74
75   /**
76    * Enable channel reliability, lost messages will be retransmitted.
77    * Yes/No.
78    */
79   GNUNET_MESH_OPTION_RELIABLE   = 0x2,
80
81   /**
82    * Enable out of order delivery of messages.
83    * Yes/No.
84    */
85   GNUNET_MESH_OPTION_OOORDER    = 0x4,
86
87   /**
88    * Who is the peer at the other end of the channel.
89    * Only for use in @c GNUNET_MESH_channel_get_info
90    * struct GNUNET_PeerIdentity *peer
91    */
92   GNUNET_MESH_OPTION_PEER       = 0x8
93
94 };
95
96
97 /**
98  * Functions with this signature are called whenever a message is
99  * received.
100  *
101  * Each time the function must call #GNUNET_MESH_receive_done on the channel
102  * in order to receive the next message. This doesn't need to be immediate:
103  * can be delayed if some processing is done on the message.
104  *
105  * @param cls Closure (set from #GNUNET_MESH_connect).
106  * @param channel Connection to the other end.
107  * @param channel_ctx Place to store local state associated with the channel.
108  * @param message The actual message.
109  * @return #GNUNET_OK to keep the channel open,
110  *         #GNUNET_SYSERR to close it (signal serious error).
111  */
112 typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
113                                             struct GNUNET_MESH_Channel *channel,
114                                             void **channel_ctx,
115                                             const struct GNUNET_MessageHeader *message);
116
117
118 /**
119  * Message handler.  Each struct specifies how to handle on particular
120  * type of message received.
121  */
122 struct GNUNET_MESH_MessageHandler
123 {
124   /**
125    * Function to call for messages of "type".
126    */
127   GNUNET_MESH_MessageCallback callback;
128
129   /**
130    * Type of the message this handler covers.
131    */
132   uint16_t type;
133
134   /**
135    * Expected size of messages of this type.  Use 0 for variable-size.
136    * If non-zero, messages of the given type will be discarded if they
137    * do not have the right size.
138    */
139   uint16_t expected_size;
140 };
141
142
143 /**
144  * Method called whenever another peer has added us to a channel
145  * the other peer initiated.
146  * Only called (once) upon reception of data with a message type which was
147  * subscribed to in #GNUNET_MESH_connect.
148  *
149  * A call to #GNUNET_MESH_channel_destroy causes te channel to be ignored. In
150  * this case the handler MUST return NULL.
151  *
152  * @param cls closure
153  * @param channel new handle to the channel
154  * @param initiator peer that started the channel
155  * @param port Port this channel is for.
156  * @param options MeshOption flag field, with all active option bits set to 1.
157  *
158  * @return initial channel context for the channel
159  *         (can be NULL -- that's not an error)
160  */
161 typedef void *(GNUNET_MESH_InboundChannelNotificationHandler) (void *cls,
162                                                                struct
163                                                                GNUNET_MESH_Channel
164                                                                * channel,
165                                                                const struct
166                                                                GNUNET_PeerIdentity
167                                                                * initiator,
168                                                                uint32_t port,
169                                                                enum GNUNET_MESH_ChannelOption
170                                                                options);
171
172
173 /**
174  * Function called whenever a channel is destroyed.  Should clean up
175  * any associated state.
176  *
177  * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
178  *
179  * @param cls closure (set from #GNUNET_MESH_connect)
180  * @param channel connection to the other end (henceforth invalid)
181  * @param channel_ctx place where local state associated
182  *                   with the channel is stored
183  */
184 typedef void (GNUNET_MESH_ChannelEndHandler) (void *cls,
185                                               const struct GNUNET_MESH_Channel *
186                                               channel,
187                                               void *channel_ctx);
188
189
190 /**
191  * Connect to the mesh service.
192  *
193  * @param cfg Configuration to use.
194  * @param cls Closure for the various callbacks that follow (including
195  *            handlers in the handlers array).
196  * @param new_channel Function called when an *incoming* channel is created.
197  *                    Can be NULL if no inbound channels are desired.
198  *                    See @a ports.
199  * @param cleaner Function called when a channel is destroyed by the remote peer.
200  *                It is NOT called if #GNUNET_MESH_channel_destroy is called on
201  *                the channel.
202  * @param handlers Callbacks for messages we care about, NULL-terminated. Each
203  *                 one must call #GNUNET_MESH_receive_done on the channel to
204  *                 receive the next message.  Messages of a type that is not
205  *                 in the handlers array are ignored if received.
206  * @param ports NULL or 0-terminated array of port numbers for incoming channels.
207  *              See @a new_channel.
208  *
209  * @return handle to the mesh service NULL on error
210  *         (in this case, init is never called)
211  */
212 struct GNUNET_MESH_Handle *
213 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
214                      GNUNET_MESH_InboundChannelNotificationHandler new_channel,
215                      GNUNET_MESH_ChannelEndHandler cleaner,
216                      const struct GNUNET_MESH_MessageHandler *handlers,
217                      const uint32_t *ports);
218
219
220 /**
221  * Disconnect from the mesh service. All channels will be destroyed. All channel
222  * disconnect callbacks will be called on any still connected peers, notifying
223  * about their disconnection. The registered inbound channel cleaner will be
224  * called should any inbound channels still exist.
225  *
226  * @param handle connection to mesh to disconnect
227  */
228 void
229 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
230
231
232 /**
233  * Create a new channel towards a remote peer.
234  *
235  * If the destination port is not open by any peer or the destination peer
236  * does not accept the channel, #GNUNET_MESH_ChannelEndHandler will be called
237  * for this channel.
238  *
239  * @param h mesh handle
240  * @param channel_ctx client's channel context to associate with the channel
241  * @param peer peer identity the channel should go to
242  * @param port Port number.
243  * @param options MeshOption flag field, with all desired option bits set to 1.
244  *
245  * @return handle to the channel
246  */
247 struct GNUNET_MESH_Channel *
248 GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
249                             void *channel_ctx,
250                             const struct GNUNET_PeerIdentity *peer,
251                             uint32_t port,
252                             enum GNUNET_MESH_ChannelOption options);
253
254
255 /**
256  * Destroy an existing channel.
257  *
258  * The existing end callback for the channel will be called immediately.
259  * Any pending outgoing messages will be sent but no incoming messages will be
260  * accepted and no data callbacks will be called.
261  *
262  * @param channel Channel handle, becomes invalid after this call.
263  */
264 void
265 GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel);
266
267
268 /**
269  * Struct to retrieve info about a channel.
270  */
271 union GNUNET_MESH_ChannelInfo
272 {
273
274   /**
275    * #GNUNET_YES / #GNUNET_NO, for binary flags.
276    */
277   int yes_no;
278
279   /**
280    * Peer on the other side of the channel
281    */
282   const struct GNUNET_PeerIdentity peer;
283 };
284
285
286 /**
287  * Get information about a channel.
288  *
289  * @param channel Channel handle.
290  * @param option Query type GNUNET_MESH_OPTION_*
291  * @param ... dependant on option, currently not used
292  * @return Union with an answer to the query.
293  */
294 const union GNUNET_MESH_ChannelInfo *
295 GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
296                               enum GNUNET_MESH_ChannelOption option, ...);
297
298
299 /**
300  * Handle for a transmission request.
301  */
302 struct GNUNET_MESH_TransmitHandle;
303
304
305 /**
306  * Ask the mesh to call @a notify once it is ready to transmit the
307  * given number of bytes to the specified channel.
308  * Only one call can be active at any time, to issue another request,
309  * wait for the callback or cancel the current request.
310  *
311  * @param channel channel to use for transmission
312  * @param cork is corking allowed for this transmission?
313  * @param maxdelay how long can the message wait?
314  * @param notify_size how many bytes of buffer space does notify want?
315  * @param notify function to call when buffer space is available;
316  *        will be called with NULL on timeout or if the overall queue
317  *        for this peer is larger than queue_size and this is currently
318  *        the message with the lowest priority
319  * @param notify_cls closure for @a notify
320  * @return non-NULL if the notify callback was queued,
321  *         NULL if we can not even queue the request (insufficient
322  *         memory); if NULL is returned, @a notify will NOT be called.
323  */
324 struct GNUNET_MESH_TransmitHandle *
325 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel,
326                                    int cork,
327                                    struct GNUNET_TIME_Relative maxdelay,
328                                    size_t notify_size,
329                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
330                                    void *notify_cls);
331
332
333 /**
334  * Cancel the specified transmission-ready notification.
335  *
336  * @param th handle that was returned by "notify_transmit_ready".
337  */
338 void
339 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
340                                           *th);
341
342
343 /**
344  * Indicate readiness to receive the next message on a channel.
345  *
346  * Should only be called once per handler called.
347  *
348  * @param channel Channel that will be allowed to call another handler.
349  */
350 void
351 GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel);
352
353
354
355 /******************************************************************************/
356 /********************       MONITORING /DEBUG API     *************************/
357 /******************************************************************************/
358 /* The following calls are not useful for normal MESH operation, but for      */
359 /* debug and monitoring of the mesh state. They can be safely ignored.        */
360 /* The API can change at any point without notice.                            */
361 /* Please contact the developer if you consider any of this calls useful for  */
362 /* normal mesh applications.                                                  */
363 /******************************************************************************/
364
365
366 /**
367  * Method called to retrieve information about a specific channel the mesh peer
368  * is aware of, including all transit nodes.
369  *
370  * @param cls Closure.
371  * @param root Root of the channel.
372  * @param dest Destination of the channel.
373  * @param port Destination port of the channel.
374  * @param root_channel_number Local number for root, if known.
375  * @param dest_channel_number Local number for dest, if known.
376  * @param public_channel_numbe Number for P2P, always known.
377  */
378 typedef void (*GNUNET_MESH_ChannelCB) (void *cls,
379                                        const struct GNUNET_PeerIdentity *root,
380                                        const struct GNUNET_PeerIdentity *dest,
381                                        uint32_t port,
382                                        uint32_t root_channel_number,
383                                        uint32_t dest_channel_number,
384                                        uint32_t public_channel_number);
385
386 /**
387  * Method called to retrieve information about all peers in MESH, called
388  * once per peer.
389  *
390  * After last peer has been reported, an additional call with NULL is done.
391  *
392  * @param cls Closure.
393  * @param peer Peer, or NULL on "EOF".
394  * @param tunnel Do we have a tunnel towards this peer?
395  * @param n_paths Number of known paths towards this peer.
396  * @param best_path How long is the best path?
397  *                  (0 = unknown, 1 = ourselves, 2 = neighbor)
398  */
399 typedef void (*GNUNET_MESH_PeersCB) (void *cls,
400                                      const struct GNUNET_PeerIdentity *peer,
401                                      int tunnel, unsigned int n_paths,
402                                      unsigned int best_path);
403
404
405 /**
406  * Method called to retrieve information about all tunnels in MESH, called
407  * once per tunnel.
408  *
409  * After last tunnel has been reported, an additional call with NULL is done.
410  *
411  * @param cls Closure.
412  * @param peer Destination peer, or NULL on "EOF".
413  * @param channels Number of channels.
414  * @param connections Number of connections.
415  * @param estate Encryption state.
416  * @param cstate Connectivity state.
417  */
418 typedef void (*GNUNET_MESH_TunnelsCB) (void *cls,
419                                        const struct GNUNET_PeerIdentity *peer,
420                                        unsigned int channels,
421                                        unsigned int connections,
422                                        uint16_t estate,
423                                        uint16_t cstate);
424
425
426
427 /**
428  * Method called to retrieve information about a specific tunnel the mesh peer
429  * has established, o`r is trying to establish.
430  *
431  * @param cls Closure.
432  * @param peer Peer towards whom the tunnel is directed.
433  * @param n_channels Number of channels.
434  * @param n_connections Number of connections.
435  * @param channels Channels.
436  * @param connections Connections.
437  * @param estate Encryption state.
438  * @param cstate Connectivity state.
439  */
440 typedef void (*GNUNET_MESH_TunnelCB) (void *cls,
441                                       const struct GNUNET_PeerIdentity *peer,
442                                       unsigned int n_channels,
443                                       unsigned int n_connections,
444                                       uint32_t *channels,
445                                       struct GNUNET_HashCode *connections,
446                                       unsigned int estate,
447                                       unsigned int cstate);
448
449 /**
450  * Request information about a specific channel of the running mesh peer.
451  *
452  * WARNING: unstable API, likely to change in the future!
453  *
454  * @param h Handle to the mesh peer.
455  * @param peer ID of the other end of the channel.
456  * @param channel_number Channel number.
457  * @param callback Function to call with the requested data.
458  * @param callback_cls Closure for @c callback.
459  */
460 void
461 GNUNET_MESH_get_channel (struct GNUNET_MESH_Handle *h,
462                          struct GNUNET_PeerIdentity *peer,
463                          uint32_t channel_number,
464                          GNUNET_MESH_ChannelCB callback,
465                          void *callback_cls);
466
467
468 /**
469  * Request information about peers known to the running mesh service.
470  * The callback will be called for every peer known to the service.
471  * Only one info request (of any kind) can be active at once.
472  *
473  *
474  * WARNING: unstable API, likely to change in the future!
475  *
476  * @param h Handle to the mesh peer.
477  * @param callback Function to call with the requested data.
478  * @param callback_cls Closure for @c callback.
479  *
480  * @return #GNUNET_OK / #GNUNET_SYSERR
481  */
482 int
483 GNUNET_MESH_get_peers (struct GNUNET_MESH_Handle *h,
484                        GNUNET_MESH_PeersCB callback,
485                        void *callback_cls);
486
487 /**
488  * Cancel a peer info request. The callback will not be called (anymore).
489  *
490  * WARNING: unstable API, likely to change in the future!
491  *
492  * @param h Mesh handle.
493  *
494  * @return Closure given to GNUNET_MESH_get_peers.
495  */
496 void *
497 GNUNET_MESH_get_peers_cancel (struct GNUNET_MESH_Handle *h);
498
499 /**
500  * Request information about tunnels of the running mesh peer.
501  * The callback will be called for every tunnel of the service.
502  * Only one info request (of any kind) can be active at once.
503  *
504  * WARNING: unstable API, likely to change in the future!
505  *
506  * @param h Handle to the mesh peer.
507  * @param callback Function to call with the requested data.
508  * @param callback_cls Closure for @c callback.
509  *
510  * @return #GNUNET_OK / #GNUNET_SYSERR
511  */
512 int
513 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
514                          GNUNET_MESH_TunnelsCB callback,
515                          void *callback_cls);
516
517 /**
518  * Cancel a monitor request. The monitor callback will not be called.
519  *
520  * @param h Mesh handle.
521  *
522  * @return Closure given to GNUNET_MESH_monitor, if any.
523  */
524 void *
525 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h);
526
527
528 /**
529  * Request information about a tunnel of the running mesh peer.
530  * The callback will be called for the tunnel once.
531  * Only one info request (of any kind) can be active at once.
532  *
533  * WARNING: unstable API, likely to change in the future!
534  *
535  * @param h Handle to the mesh peer.
536  * @param id Peer whose tunnel to examine.
537  * @param callback Function to call with the requested data.
538  * @param callback_cls Closure for @c callback.
539  *
540  * @return #GNUNET_OK / #GNUNET_SYSERR
541  */
542 int
543 GNUNET_MESH_get_tunnel (struct GNUNET_MESH_Handle *h,
544                         const struct GNUNET_PeerIdentity *id,
545                         GNUNET_MESH_TunnelCB callback,
546                         void *callback_cls);
547
548 /**
549  * Create a message queue for a mesh channel.
550  * The message queue can only be used to transmit messages,
551  * not to receive them.
552  *
553  * @param channel the channel to create the message qeue for
554  * @return a message queue to messages over the channel
555  */
556 struct GNUNET_MQ_Handle *
557 GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel);
558
559
560 #if 0                           /* keep Emacsens' auto-indent happy */
561 {
562 #endif
563 #ifdef __cplusplus
564 }
565 #endif
566
567 /* ifndef GNUNET_MESH_SERVICE_H */
568 #endif
569 /* end of gnunet_mesh_service.h */