- change mesh channel creation/notification API
[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 MeshOption
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. A call to #GNUNET_MESH_channel_destroy
148  * causes te channel to be ignored and no further notifications are sent about
149  * the same channel.
150  *
151  * @param cls closure
152  * @param channel new handle to the channel
153  * @param initiator peer that started the channel
154  * @param port Port this channel is for.
155  * @param options MeshOption flag field, with all active option bits set to 1.
156  *
157  * @return initial channel context for the channel
158  *         (can be NULL -- that's not an error)
159  */
160 typedef void *(GNUNET_MESH_InboundChannelNotificationHandler) (void *cls,
161                                                                struct
162                                                                GNUNET_MESH_Channel
163                                                                * channel,
164                                                                const struct
165                                                                GNUNET_PeerIdentity
166                                                                * initiator,
167                                                                uint32_t port,
168                                                                enum MeshOption
169                                                                options);
170
171
172 /**
173  * Function called whenever a channel is destroyed.  Should clean up
174  * any associated state.
175  *
176  * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
177  *
178  * @param cls closure (set from #GNUNET_MESH_connect)
179  * @param channel connection to the other end (henceforth invalid)
180  * @param channel_ctx place where local state associated
181  *                   with the channel is stored
182  */
183 typedef void (GNUNET_MESH_ChannelEndHandler) (void *cls,
184                                               const struct GNUNET_MESH_Channel *
185                                               channel,
186                                               void *channel_ctx);
187
188
189 /**
190  * Connect to the mesh service.
191  *
192  * @param cfg Configuration to use.
193  * @param cls Closure for the various callbacks that follow (including
194  *            handlers in the handlers array).
195  * @param new_channel Function called when an *incoming* channel is created.
196  *                    Can be NULL if no inbound channels are desired.
197  *                    See @a ports.
198  * @param cleaner Function called when a channel is destroyed by the remote peer.
199  *                It is NOT called if #GNUNET_MESH_channel_destroy is called on
200  *                the channel.
201  * @param handlers Callbacks for messages we care about, NULL-terminated. Each
202  *                 one must call #GNUNET_MESH_receive_done on the channel to
203  *                 receive the next message.  Messages of a type that is not
204  *                 in the handlers array are ignored if received.
205  * @param ports NULL or 0-terminated array of port numbers for incoming channels.
206  *              See @a new_channel.
207  *
208  * @return handle to the mesh service NULL on error
209  *         (in this case, init is never called)
210  */
211 struct GNUNET_MESH_Handle *
212 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
213                      GNUNET_MESH_InboundChannelNotificationHandler new_channel,
214                      GNUNET_MESH_ChannelEndHandler cleaner,
215                      const struct GNUNET_MESH_MessageHandler *handlers,
216                      const uint32_t *ports);
217
218
219 /**
220  * Disconnect from the mesh service. All channels will be destroyed. All channel
221  * disconnect callbacks will be called on any still connected peers, notifying
222  * about their disconnection. The registered inbound channel cleaner will be
223  * called should any inbound channels still exist.
224  *
225  * @param handle connection to mesh to disconnect
226  */
227 void
228 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
229
230
231 /**
232  * Create a new channel towards a remote peer.
233  *
234  * If the destination port is not open by any peer or the destination peer
235  * does not accept the channel, #GNUNET_MESH_ChannelEndHandler will be called
236  * for this channel.
237  *
238  * @param h mesh handle
239  * @param channel_ctx client's channel context to associate with the channel
240  * @param peer peer identity the channel should go to
241  * @param port Port number.
242  * @param options MeshOption flag field, with all desired option bits set to 1.
243  *
244  * @return handle to the channel
245  */
246 struct GNUNET_MESH_Channel *
247 GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
248                             void *channel_ctx,
249                             const struct GNUNET_PeerIdentity *peer,
250                             uint32_t port,
251                             enum MeshOption options);
252
253
254 /**
255  * Destroy an existing channel.
256  *
257  * The existing end callback for the channel will be called immediately.
258  * Any pending outgoing messages will be sent but no incoming messages will be
259  * accepted and no data callbacks will be called.
260  *
261  * @param channel Channel handle, becomes invalid after this call.
262  */
263 void
264 GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel);
265
266
267 /**
268  * Struct to retrieve info about a channel.
269  */
270 union GNUNET_MESH_ChannelInfo
271 {
272
273   /**
274    * #GNUNET_YES / #GNUNET_NO, for binary flags.
275    */
276   int yes_no;
277
278   /**
279    * Peer on the other side of the channel
280    */
281   const struct GNUNET_PeerIdentity peer;
282 };
283
284
285 /**
286  * Get information about a channel.
287  *
288  * @param channel Channel handle.
289  * @param option Query type GNUNET_MESH_OPTION_*
290  * @param ... dependant on option, currently not used
291  * @return Union with an answer to the query.
292  */
293 const union GNUNET_MESH_ChannelInfo *
294 GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
295                               enum MeshOption option, ...);
296
297
298 /**
299  * Handle for a transmission request.
300  */
301 struct GNUNET_MESH_TransmitHandle;
302
303
304 /**
305  * Ask the mesh to call @a notify once it is ready to transmit the
306  * given number of bytes to the specified channel.
307  * Only one call can be active at any time, to issue another request,
308  * wait for the callback or cancel the current request.
309  *
310  * @param channel channel to use for transmission
311  * @param cork is corking allowed for this transmission?
312  * @param maxdelay how long can the message wait?
313  * @param notify_size how many bytes of buffer space does notify want?
314  * @param notify function to call when buffer space is available;
315  *        will be called with NULL on timeout or if the overall queue
316  *        for this peer is larger than queue_size and this is currently
317  *        the message with the lowest priority
318  * @param notify_cls closure for @a notify
319  * @return non-NULL if the notify callback was queued,
320  *         NULL if we can not even queue the request (insufficient
321  *         memory); if NULL is returned, @a notify will NOT be called.
322  */
323 struct GNUNET_MESH_TransmitHandle *
324 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel,
325                                    int cork,
326                                    struct GNUNET_TIME_Relative maxdelay,
327                                    size_t notify_size,
328                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
329                                    void *notify_cls);
330
331
332 /**
333  * Cancel the specified transmission-ready notification.
334  *
335  * @param th handle that was returned by "notify_transmit_ready".
336  */
337 void
338 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
339                                           *th);
340
341
342 /**
343  * Indicate readiness to receive the next message on a channel.
344  *
345  * Should only be called once per handler called.
346  *
347  * @param channel Channel that will be allowed to call another handler.
348  */
349 void
350 GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel);
351
352
353
354 /******************************************************************************/
355 /********************       MONITORING /DEBUG API     *************************/
356 /******************************************************************************/
357 /* The following calls are not useful for normal MESH operation, but for      */
358 /* debug and monitoring of the mesh state. They can be safely ignored.        */
359 /* The API can change at any point without notice.                            */
360 /* Please contact the developer if you consider any of this calls useful for  */
361 /* normal mesh applications.                                                  */
362 /******************************************************************************/
363
364 /**
365  * Method called to retrieve information about each channel the mesh peer
366  * is aware of.
367  *
368  * @param cls Closure.
369  * @param channel_number Channel number.
370  * @param origin that started the channel (owner).
371  * @param target other endpoint of the channel
372  */
373 typedef void (*GNUNET_MESH_ChannelsCB) (void *cls,
374                                         uint32_t channel_number,
375                                         const struct GNUNET_PeerIdentity *origin,
376                                         const struct GNUNET_PeerIdentity *target);
377
378
379 /**
380  * Method called to retrieve information about a specific channel the mesh peer
381  * is aware of, including all transit nodes.
382  *
383  * @param cls Closure.
384  * @param peer Peer in the channel's tree.
385  * @param parent Parent of the current peer. All 0 when peer is root.
386  */
387 typedef void (*GNUNET_MESH_ChannelCB) (void *cls,
388                                       const struct GNUNET_PeerIdentity *peer,
389                                       const struct GNUNET_PeerIdentity *parent);
390
391
392 /**
393  * Request information about the running mesh peer.
394  * The callback will be called for every channel known to the service,
395  * listing all active peers that belong to the channel.
396  *
397  * If called again on the same handle, it will overwrite the previous
398  * callback and cls. To retrieve the cls, monitor_cancel must be
399  * called first.
400  *
401  * WARNING: unstable API, likely to change in the future!
402  *
403  * @param h Handle to the mesh peer.
404  * @param callback Function to call with the requested data.
405  * @param callback_cls Closure for @c callback.
406  */
407 void
408 GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
409                          GNUNET_MESH_ChannelsCB callback,
410                          void *callback_cls);
411
412
413 /**
414  * Request information about a specific channel of the running mesh peer.
415  *
416  * WARNING: unstable API, likely to change in the future!
417  *
418  * @param h Handle to the mesh peer.
419  * @param initiator ID of the owner of the channel.
420  * @param channel_number Channel number.
421  * @param callback Function to call with the requested data.
422  * @param callback_cls Closure for @c callback.
423  */
424 void
425 GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
426                          struct GNUNET_PeerIdentity *initiator,
427                          uint32_t channel_number,
428                          GNUNET_MESH_ChannelCB callback,
429                          void *callback_cls);
430
431
432 /**
433  * Cancel a monitor request. The monitor callback will not be called.
434  *
435  * WARNING: unstable API, likely to change in the future!
436  *
437  * @param h Mesh handle.
438  *
439  * @return Closure given to GNUNET_MESH_monitor, if any.
440  */
441 void *
442 GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h);
443
444
445 /**
446  * Create a message queue for a mesh channel.
447  * The message queue can only be used to transmit messages,
448  * not to receive them.
449  *
450  * @param channel the channel to create the message qeue for
451  * @return a message queue to messages over the channel
452  */
453 struct GNUNET_MQ_Handle *
454 GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel);
455
456
457 #if 0                           /* keep Emacsens' auto-indent happy */
458 {
459 #endif
460 #ifdef __cplusplus
461 }
462 #endif
463
464 /* ifndef GNUNET_MESH_SERVICE_H */
465 #endif
466 /* end of gnunet_mesh_service.h */