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_enc.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;
60 * Second line indicates filed in the MeshChannelInfo union carrying the answer.
65 * Disable buffering on intermediate nodes (for minimum latency).
68 GNUNET_MESH_OPTION_NOBUFFER = 0x1,
71 * Enable channel reliability, lost messages will be retransmitted.
74 GNUNET_MESH_OPTION_RELIABLE = 0x2,
77 * Enable out of order delivery of messages.
80 GNUNET_MESH_OPTION_OOORDER = 0x4,
83 * Who is the peer at the other end of the channel.
84 * struct GNUNET_PeerIdentity *peer
86 GNUNET_MESH_OPTION_PEER = 0x8
92 * Functions with this signature are called whenever a message is
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.
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 * @return #GNUNET_OK to keep the channel open,
104 * #GNUNET_SYSERR to close it (signal serious error).
106 typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
107 struct GNUNET_MESH_Channel *channel,
109 const struct GNUNET_MessageHeader *message);
113 * Message handler. Each struct specifies how to handle on particular
114 * type of message received.
116 struct GNUNET_MESH_MessageHandler
119 * Function to call for messages of "type".
121 GNUNET_MESH_MessageCallback callback;
124 * Type of the message this handler covers.
129 * Expected size of messages of this type. Use 0 for variable-size.
130 * If non-zero, messages of the given type will be discarded if they
131 * do not have the right size.
133 uint16_t expected_size;
138 * Method called whenever another peer has added us to a channel
139 * the other peer initiated.
140 * Only called (once) upon reception of data with a message type which was
141 * subscribed to in #GNUNET_MESH_connect. A call to #GNUNET_MESH_channel_destroy
142 * causes te channel to be ignored and no further notifications are sent about
146 * @param channel new handle to the channel
147 * @param initiator peer that started the channel
148 * @param port Port this channel is for.
149 * @return initial channel context for the channel
150 * (can be NULL -- that's not an error)
152 typedef void *(GNUNET_MESH_InboundChannelNotificationHandler) (void *cls,
163 * Function called whenever a channel is destroyed. Should clean up
164 * any associated state.
166 * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
168 * @param cls closure (set from #GNUNET_MESH_connect)
169 * @param channel connection to the other end (henceforth invalid)
170 * @param channel_ctx place where local state associated
171 * with the channel is stored
173 typedef void (GNUNET_MESH_ChannelEndHandler) (void *cls,
174 const struct GNUNET_MESH_Channel *
180 * Connect to the mesh service.
182 * @param cfg Configuration to use.
183 * @param cls Closure for the various callbacks that follow (including
184 * handlers in the handlers array).
185 * @param new_channel Function called when an *incoming* channel is created.
186 * Can be NULL if no inbound channels are desired.
188 * @param cleaner Function called when a channel is destroyed by the remote peer.
189 * It is NOT called if #GNUNET_MESH_channel_destroy is called on
191 * @param handlers Callbacks for messages we care about, NULL-terminated. Each
192 * one must call #GNUNET_MESH_receive_done on the channel to
193 * receive the next message. Messages of a type that is not
194 * in the handlers array are ignored if received.
195 * @param ports NULL or 0-terminated array of port numbers for incoming channels.
196 * See @a new_channel.
198 * @return handle to the mesh service NULL on error
199 * (in this case, init is never called)
201 struct GNUNET_MESH_Handle *
202 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
203 GNUNET_MESH_InboundChannelNotificationHandler new_channel,
204 GNUNET_MESH_ChannelEndHandler cleaner,
205 const struct GNUNET_MESH_MessageHandler *handlers,
206 const uint32_t *ports);
210 * Disconnect from the mesh service. All channels will be destroyed. All channel
211 * disconnect callbacks will be called on any still connected peers, notifying
212 * about their disconnection. The registered inbound channel cleaner will be
213 * called should any inbound channels still exist.
215 * @param handle connection to mesh to disconnect
218 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
222 * Create a new channel (we're initiator and will be allowed to add/remove peers
225 * @param h mesh handle
226 * @param channel_ctx client's channel context to associate with the channel
227 * @param peer peer identity the channel should go to
228 * @param port Port number.
229 * @param nobuffer Flag for disabling buffering on relay nodes.
230 * @param reliable Flag for end-to-end reliability.
231 * @return handle to the channel
233 struct GNUNET_MESH_Channel *
234 GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
236 const struct GNUNET_PeerIdentity *peer,
243 * Destroy an existing channel.
245 * The existing end callback for the channel will be called immediately.
246 * Any pending outgoing messages will be sent but no incoming messages will be
247 * accepted and no data callbacks will be called.
249 * @param channel Channel handle, becomes invalid after this call.
252 GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel);
256 * Struct to retrieve info about a channel.
258 union GNUNET_MESH_ChannelInfo
262 * #GNUNET_YES / #GNUNET_NO, for binary flags.
267 * Peer on the other side of the channel
269 const struct GNUNET_PeerIdentity *peer;
274 * Get information about a channel.
276 * @param channel Channel handle.
277 * @param option Query type GNUNET_MESH_OPTION_*
278 * @param ... dependant on option, currently not used
279 * @return Union with an answer to the query.
281 const union GNUNET_MESH_ChannelInfo *
282 GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
283 enum MeshOption option, ...);
287 * Handle for a transmission request.
289 struct GNUNET_MESH_TransmitHandle;
293 * Ask the mesh to call @a notify once it is ready to transmit the
294 * given number of bytes to the specified channel.
295 * Only one call can be active at any time, to issue another request,
296 * wait for the callback or cancel the current request.
298 * @param channel channel to use for transmission
299 * @param cork is corking allowed for this transmission?
300 * @param maxdelay how long can the message wait?
301 * @param notify_size how many bytes of buffer space does notify want?
302 * @param notify function to call when buffer space is available;
303 * will be called with NULL on timeout or if the overall queue
304 * for this peer is larger than queue_size and this is currently
305 * the message with the lowest priority
306 * @param notify_cls closure for @a notify
307 * @return non-NULL if the notify callback was queued,
308 * NULL if we can not even queue the request (insufficient
309 * memory); if NULL is returned, @a notify will NOT be called.
311 struct GNUNET_MESH_TransmitHandle *
312 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel,
314 struct GNUNET_TIME_Relative maxdelay,
316 GNUNET_CONNECTION_TransmitReadyNotify notify,
321 * Cancel the specified transmission-ready notification.
323 * @param th handle that was returned by "notify_transmit_ready".
326 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
331 * Indicate readiness to receive the next message on a channel.
333 * Should only be called once per handler called.
335 * @param channel Channel that will be allowed to call another handler.
338 GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel);
342 /******************************************************************************/
343 /******************** MONITORING /DEBUG API *************************/
344 /******************************************************************************/
345 /* The following calls are not useful for normal MESH operation, but for */
346 /* debug and monitoring of the mesh state. They can be safely ignored. */
347 /* The API can change at any point without notice. */
348 /* Please contact the developer if you consider any of this calls useful for */
349 /* normal mesh applications. */
350 /******************************************************************************/
353 * Method called to retrieve information about each channel the mesh peer
356 * @param cls Closure.
357 * @param channel_number Channel number.
358 * @param origin that started the channel (owner).
359 * @param target other endpoint of the channel
361 typedef void (*GNUNET_MESH_ChannelsCB) (void *cls,
362 uint32_t channel_number,
363 const struct GNUNET_PeerIdentity *origin,
364 const struct GNUNET_PeerIdentity *target);
368 * Method called to retrieve information about a specific channel the mesh peer
369 * is aware of, including all transit nodes.
371 * @param cls Closure.
372 * @param peer Peer in the channel's tree.
373 * @param parent Parent of the current peer. All 0 when peer is root.
375 typedef void (*GNUNET_MESH_ChannelCB) (void *cls,
376 const struct GNUNET_PeerIdentity *peer,
377 const struct GNUNET_PeerIdentity *parent);
381 * Request information about the running mesh peer.
382 * The callback will be called for every channel known to the service,
383 * listing all active peers that belong to the channel.
385 * If called again on the same handle, it will overwrite the previous
386 * callback and cls. To retrieve the cls, monitor_cancel must be
389 * WARNING: unstable API, likely to change in the future!
391 * @param h Handle to the mesh peer.
392 * @param callback Function to call with the requested data.
393 * @param callback_cls Closure for @c callback.
396 GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
397 GNUNET_MESH_ChannelsCB callback,
402 * Request information about a specific channel of the running mesh peer.
404 * WARNING: unstable API, likely to change in the future!
406 * @param h Handle to the mesh peer.
407 * @param initiator ID of the owner of the channel.
408 * @param channel_number Channel number.
409 * @param callback Function to call with the requested data.
410 * @param callback_cls Closure for @c callback.
413 GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
414 struct GNUNET_PeerIdentity *initiator,
415 uint32_t channel_number,
416 GNUNET_MESH_ChannelCB callback,
421 * Cancel a monitor request. The monitor callback will not be called.
423 * WARNING: unstable API, likely to change in the future!
425 * @param h Mesh handle.
427 * @return Closure given to GNUNET_MESH_monitor, if any.
430 GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h);
434 * Create a message queue for a mesh channel.
435 * The message queue can only be used to transmit messages,
436 * not to receive them.
438 * @param channel the channel to create the message qeue for
439 * @return a message queue to messages over the channel
441 struct GNUNET_MQ_Handle *
442 GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel);
445 #if 0 /* keep Emacsens' auto-indent happy */
452 /* ifndef GNUNET_MESH_SERVICE_H */
454 /* end of gnunet_mesh_service.h */