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