2 This file is part of GNUnet.
3 (C) 2009 - 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_mesh_service.h
23 * @brief mesh service; establish channels to distant peers
24 * @author Christian Grothoff
27 #ifndef GNUNET_MESH_SERVICE_H
28 #define GNUNET_MESH_SERVICE_H
33 #if 0 /* keep Emacsens' auto-indent happy */
38 #include "gnunet_util_lib.h"
39 #include "gnunet_transport_service.h"
42 * Version number of GNUnet-mesh API.
44 #define GNUNET_MESH_VERSION 0x00000003
48 * Opaque handle to the service.
50 struct GNUNET_MESH_Handle;
53 * Opaque handle to a channel.
55 struct GNUNET_MESH_Channel;
58 * Hash to be used in Mesh communication. Only 256 bits needed,
59 * instead of the 512 from @c GNUNET_HashCode.
62 struct GNUNET_MESH_Hash
64 unsigned char bits[256 / 8];
69 * Second line indicates filed in the MeshChannelInfo union carrying the answer.
71 enum GNUNET_MESH_ChannelOption
74 * Default options: unreliable, default buffering, not out of order.
76 GNUNET_MESH_OPTION_DEFAULT = 0x0,
79 * Disable buffering on intermediate nodes (for minimum latency).
82 GNUNET_MESH_OPTION_NOBUFFER = 0x1,
85 * Enable channel reliability, lost messages will be retransmitted.
88 GNUNET_MESH_OPTION_RELIABLE = 0x2,
91 * Enable out of order delivery of messages.
94 GNUNET_MESH_OPTION_OOORDER = 0x4,
97 * Who is the peer at the other end of the channel.
98 * Only for use in @c GNUNET_MESH_channel_get_info
99 * struct GNUNET_PeerIdentity *peer
101 GNUNET_MESH_OPTION_PEER = 0x8
107 * Functions with this signature are called whenever a message is
110 * Each time the function must call #GNUNET_MESH_receive_done on the channel
111 * in order to receive the next message. This doesn't need to be immediate:
112 * can be delayed if some processing is done on the message.
114 * @param cls Closure (set from #GNUNET_MESH_connect).
115 * @param channel Connection to the other end.
116 * @param channel_ctx Place to store local state associated with the channel.
117 * @param message The actual message.
118 * @return #GNUNET_OK to keep the channel open,
119 * #GNUNET_SYSERR to close it (signal serious error).
121 typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
122 struct GNUNET_MESH_Channel *channel,
124 const struct GNUNET_MessageHeader *message);
128 * Message handler. Each struct specifies how to handle on particular
129 * type of message received.
131 struct GNUNET_MESH_MessageHandler
134 * Function to call for messages of "type".
136 GNUNET_MESH_MessageCallback callback;
139 * Type of the message this handler covers.
144 * Expected size of messages of this type. Use 0 for variable-size.
145 * If non-zero, messages of the given type will be discarded if they
146 * do not have the right size.
148 uint16_t expected_size;
153 * Method called whenever another peer has added us to a channel
154 * the other peer initiated.
155 * Only called (once) upon reception of data with a message type which was
156 * subscribed to in #GNUNET_MESH_connect.
158 * A call to #GNUNET_MESH_channel_destroy causes te channel to be ignored. In
159 * this case the handler MUST return NULL.
162 * @param channel new handle to the channel
163 * @param initiator peer that started the channel
164 * @param port Port this channel is for.
165 * @param options MeshOption flag field, with all active option bits set to 1.
167 * @return initial channel context for the channel
168 * (can be NULL -- that's not an error)
170 typedef void *(GNUNET_MESH_InboundChannelNotificationHandler) (void *cls,
178 enum GNUNET_MESH_ChannelOption
183 * Function called whenever a channel is destroyed. Should clean up
184 * any associated state.
186 * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
188 * @param cls closure (set from #GNUNET_MESH_connect)
189 * @param channel connection to the other end (henceforth invalid)
190 * @param channel_ctx place where local state associated
191 * with the channel is stored
193 typedef void (GNUNET_MESH_ChannelEndHandler) (void *cls,
194 const struct GNUNET_MESH_Channel *
200 * Connect to the mesh service.
202 * @param cfg Configuration to use.
203 * @param cls Closure for the various callbacks that follow (including
204 * handlers in the handlers array).
205 * @param new_channel Function called when an *incoming* channel is created.
206 * Can be NULL if no inbound channels are desired.
208 * @param cleaner Function called when a channel is destroyed by the remote peer.
209 * It is NOT called if #GNUNET_MESH_channel_destroy is called on
211 * @param handlers Callbacks for messages we care about, NULL-terminated. Each
212 * one must call #GNUNET_MESH_receive_done on the channel to
213 * receive the next message. Messages of a type that is not
214 * in the handlers array are ignored if received.
215 * @param ports NULL or 0-terminated array of port numbers for incoming channels.
216 * See @a new_channel.
218 * @return handle to the mesh service NULL on error
219 * (in this case, init is never called)
221 struct GNUNET_MESH_Handle *
222 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
223 GNUNET_MESH_InboundChannelNotificationHandler new_channel,
224 GNUNET_MESH_ChannelEndHandler cleaner,
225 const struct GNUNET_MESH_MessageHandler *handlers,
226 const uint32_t *ports);
230 * Disconnect from the mesh service. All channels will be destroyed. All channel
231 * disconnect callbacks will be called on any still connected peers, notifying
232 * about their disconnection. The registered inbound channel cleaner will be
233 * called should any inbound channels still exist.
235 * @param handle connection to mesh to disconnect
238 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
242 * Create a new channel towards a remote peer.
244 * If the destination port is not open by any peer or the destination peer
245 * does not accept the channel, #GNUNET_MESH_ChannelEndHandler will be called
248 * @param h mesh handle
249 * @param channel_ctx client's channel context to associate with the channel
250 * @param peer peer identity the channel should go to
251 * @param port Port number.
252 * @param options MeshOption flag field, with all desired option bits set to 1.
254 * @return handle to the channel
256 struct GNUNET_MESH_Channel *
257 GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
259 const struct GNUNET_PeerIdentity *peer,
261 enum GNUNET_MESH_ChannelOption options);
265 * Destroy an existing channel.
267 * The existing end callback for the channel will be called immediately.
268 * Any pending outgoing messages will be sent but no incoming messages will be
269 * accepted and no data callbacks will be called.
271 * @param channel Channel handle, becomes invalid after this call.
274 GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel);
278 * Struct to retrieve info about a channel.
280 union GNUNET_MESH_ChannelInfo
284 * #GNUNET_YES / #GNUNET_NO, for binary flags.
289 * Peer on the other side of the channel
291 const struct GNUNET_PeerIdentity peer;
296 * Get information about a channel.
298 * @param channel Channel handle.
299 * @param option Query type GNUNET_MESH_OPTION_*
300 * @param ... dependant on option, currently not used
301 * @return Union with an answer to the query.
303 const union GNUNET_MESH_ChannelInfo *
304 GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
305 enum GNUNET_MESH_ChannelOption option, ...);
309 * Handle for a transmission request.
311 struct GNUNET_MESH_TransmitHandle;
315 * Ask the mesh to call @a notify once it is ready to transmit the
316 * given number of bytes to the specified channel.
317 * Only one call can be active at any time, to issue another request,
318 * wait for the callback or cancel the current request.
320 * @param channel channel to use for transmission
321 * @param cork is corking allowed for this transmission?
322 * @param maxdelay how long can the message wait?
323 * @param notify_size how many bytes of buffer space does notify want?
324 * @param notify function to call when buffer space is available;
325 * will be called with NULL on timeout or if the overall queue
326 * for this peer is larger than queue_size and this is currently
327 * the message with the lowest priority
328 * @param notify_cls closure for @a notify
329 * @return non-NULL if the notify callback was queued,
330 * NULL if we can not even queue the request (insufficient
331 * memory); if NULL is returned, @a notify will NOT be called.
333 struct GNUNET_MESH_TransmitHandle *
334 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel,
336 struct GNUNET_TIME_Relative maxdelay,
338 GNUNET_CONNECTION_TransmitReadyNotify notify,
343 * Cancel the specified transmission-ready notification.
345 * @param th handle that was returned by "notify_transmit_ready".
348 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
353 * Indicate readiness to receive the next message on a channel.
355 * Should only be called once per handler called.
357 * @param channel Channel that will be allowed to call another handler.
360 GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel);
364 /******************************************************************************/
365 /******************** MONITORING /DEBUG API *************************/
366 /******************************************************************************/
367 /* The following calls are not useful for normal MESH operation, but for */
368 /* debug and monitoring of the mesh state. They can be safely ignored. */
369 /* The API can change at any point without notice. */
370 /* Please contact the developer if you consider any of this calls useful for */
371 /* normal mesh applications. */
372 /******************************************************************************/
376 * Method called to retrieve information about a specific channel the mesh peer
377 * is aware of, including all transit nodes.
379 * @param cls Closure.
380 * @param root Root of the channel.
381 * @param dest Destination of the channel.
382 * @param port Destination port of the channel.
383 * @param root_channel_number Local number for root, if known.
384 * @param dest_channel_number Local number for dest, if known.
385 * @param public_channel_numbe Number for P2P, always known.
387 typedef void (*GNUNET_MESH_ChannelCB) (void *cls,
388 const struct GNUNET_PeerIdentity *root,
389 const struct GNUNET_PeerIdentity *dest,
391 uint32_t root_channel_number,
392 uint32_t dest_channel_number,
393 uint32_t public_channel_number);
396 * Method called to retrieve information about all peers in MESH, called
399 * After last peer has been reported, an additional call with NULL is done.
401 * @param cls Closure.
402 * @param peer Peer, or NULL on "EOF".
403 * @param tunnel Do we have a tunnel towards this peer?
404 * @param n_paths Number of known paths towards this peer.
405 * @param best_path How long is the best path?
406 * (0 = unknown, 1 = ourselves, 2 = neighbor)
408 typedef void (*GNUNET_MESH_PeersCB) (void *cls,
409 const struct GNUNET_PeerIdentity *peer,
410 int tunnel, unsigned int n_paths,
411 unsigned int best_path);
414 * Method called to retrieve information about a specific peer
415 * known to the service.
417 * @param cls Closure.
418 * @param peer Peer ID.
419 * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
420 * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
421 * @param n_paths Number of paths known towards peer.
422 * @param paths Array of PEER_IDs representing all paths to reach the peer.
423 * Each path starts with the local peer.
424 * Each path ends with the destination peer (given in @c peer).
426 typedef void (*GNUNET_MESH_PeerCB) (void *cls,
427 const struct GNUNET_PeerIdentity *peer,
430 unsigned int n_paths,
431 struct GNUNET_PeerIdentity *paths);
435 * Method called to retrieve information about all tunnels in MESH, called
438 * After last tunnel has been reported, an additional call with NULL is done.
440 * @param cls Closure.
441 * @param peer Destination peer, or NULL on "EOF".
442 * @param channels Number of channels.
443 * @param connections Number of connections.
444 * @param estate Encryption state.
445 * @param cstate Connectivity state.
447 typedef void (*GNUNET_MESH_TunnelsCB) (void *cls,
448 const struct GNUNET_PeerIdentity *peer,
449 unsigned int channels,
450 unsigned int connections,
457 * Method called to retrieve information about a specific tunnel the mesh peer
458 * has established, o`r is trying to establish.
460 * @param cls Closure.
461 * @param peer Peer towards whom the tunnel is directed.
462 * @param n_channels Number of channels.
463 * @param n_connections Number of connections.
464 * @param channels Channels.
465 * @param connections Connections.
466 * @param estate Encryption state.
467 * @param cstate Connectivity state.
469 typedef void (*GNUNET_MESH_TunnelCB) (void *cls,
470 const struct GNUNET_PeerIdentity *peer,
471 unsigned int n_channels,
472 unsigned int n_connections,
474 struct GNUNET_MESH_Hash *connections,
476 unsigned int cstate);
479 * Request information about a specific channel of the running mesh peer.
481 * WARNING: unstable API, likely to change in the future!
483 * @param h Handle to the mesh peer.
484 * @param peer ID of the other end of the channel.
485 * @param channel_number Channel number.
486 * @param callback Function to call with the requested data.
487 * @param callback_cls Closure for @c callback.
490 GNUNET_MESH_get_channel (struct GNUNET_MESH_Handle *h,
491 struct GNUNET_PeerIdentity *peer,
492 uint32_t channel_number,
493 GNUNET_MESH_ChannelCB callback,
498 * Request information about peers known to the running mesh service.
499 * The callback will be called for every peer known to the service.
500 * Only one info request (of any kind) can be active at once.
503 * WARNING: unstable API, likely to change in the future!
505 * @param h Handle to the mesh peer.
506 * @param callback Function to call with the requested data.
507 * @param callback_cls Closure for @c callback.
509 * @return #GNUNET_OK / #GNUNET_SYSERR
512 GNUNET_MESH_get_peers (struct GNUNET_MESH_Handle *h,
513 GNUNET_MESH_PeersCB callback,
517 * Cancel a peer info request. The callback will not be called (anymore).
519 * WARNING: unstable API, likely to change in the future!
521 * @param h Mesh handle.
523 * @return Closure given to GNUNET_MESH_get_peers.
526 GNUNET_MESH_get_peers_cancel (struct GNUNET_MESH_Handle *h);
530 * Request information about a peer known to the running mesh peer.
531 * The callback will be called for the tunnel once.
532 * Only one info request (of any kind) can be active at once.
534 * WARNING: unstable API, likely to change in the future!
536 * @param h Handle to the mesh peer.
537 * @param id Peer whose tunnel to examine.
538 * @param callback Function to call with the requested data.
539 * @param callback_cls Closure for @c callback.
541 * @return #GNUNET_OK / #GNUNET_SYSERR
544 GNUNET_MESH_get_peer (struct GNUNET_MESH_Handle *h,
545 const struct GNUNET_PeerIdentity *id,
546 GNUNET_MESH_PeerCB callback,
550 * Request information about tunnels of the running mesh peer.
551 * The callback will be called for every tunnel of the service.
552 * Only one info request (of any kind) can be active at once.
554 * WARNING: unstable API, likely to change in the future!
556 * @param h Handle to the mesh peer.
557 * @param callback Function to call with the requested data.
558 * @param callback_cls Closure for @c callback.
560 * @return #GNUNET_OK / #GNUNET_SYSERR
563 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
564 GNUNET_MESH_TunnelsCB callback,
568 * Cancel a monitor request. The monitor callback will not be called.
570 * @param h Mesh handle.
572 * @return Closure given to GNUNET_MESH_monitor, if any.
575 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h);
579 * Request information about a tunnel of the running mesh peer.
580 * The callback will be called for the tunnel once.
581 * Only one info request (of any kind) can be active at once.
583 * WARNING: unstable API, likely to change in the future!
585 * @param h Handle to the mesh peer.
586 * @param id Peer whose tunnel to examine.
587 * @param callback Function to call with the requested data.
588 * @param callback_cls Closure for @c callback.
590 * @return #GNUNET_OK / #GNUNET_SYSERR
593 GNUNET_MESH_get_tunnel (struct GNUNET_MESH_Handle *h,
594 const struct GNUNET_PeerIdentity *id,
595 GNUNET_MESH_TunnelCB callback,
599 * Create a message queue for a mesh channel.
600 * The message queue can only be used to transmit messages,
601 * not to receive them.
603 * @param channel the channel to create the message qeue for
604 * @return a message queue to messages over the channel
606 struct GNUNET_MQ_Handle *
607 GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel);
610 #if 0 /* keep Emacsens' auto-indent happy */
617 /* ifndef GNUNET_MESH_SERVICE_H */
619 /* end of gnunet_mesh_service.h */