2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011, 2012, 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 tunnels to distant peers
24 * @author Christian Grothoff
27 * - need to do sanity check that this is consistent
28 * with current ideas for the multicast layer's needs
31 #ifndef GNUNET_MESH_SERVICE_H
32 #define GNUNET_MESH_SERVICE_H
37 #if 0 /* keep Emacsens' auto-indent happy */
42 #include "gnunet_util_lib.h"
43 #include "gnunet_transport_service.h"
46 * Version number of GNUnet-mesh API.
48 #define GNUNET_MESH_VERSION 0x00000002
52 * Opaque handle to the service.
54 struct GNUNET_MESH_Handle;
57 * Opaque handle to a tunnel.
59 struct GNUNET_MESH_Tunnel;
63 * Options for querying a tunnel.
64 * Second line indicates filed in the MeshTunnelInfo union carrying the answer.
69 * Disable buffering on intermediate nodes (for minimum latency).
72 GNUNET_MESH_OPTION_NOBUFFER = 0x1,
75 * Enable tunnel reliability, lost messages will be retransmitted.
78 GNUNET_MESH_OPTION_RELIABLE = 0x2,
81 * Enable out of order delivery of messages.
84 GNUNET_MESH_OPTION_OOORDER = 0x4,
87 * Who is the peer at the other end of the tunnel.
88 * struct GNUNET_PeerIdentity *peer
90 GNUNET_MESH_OPTION_PEER = 0x8
96 * Functions with this signature are called whenever a message is
99 * Each time the function must call GNUNET_MESH_receive_done on the tunnel
100 * in order to receive the next message. This doesn't need to be immediate:
101 * can be delayed if some processing is done on the message.
103 * @param cls Closure (set from GNUNET_MESH_connect).
104 * @param tunnel Connection to the other end.
105 * @param tunnel_ctx Place to store local state associated with the tunnel.
106 * @param message The actual message.
108 * @return GNUNET_OK to keep the tunnel open,
109 * GNUNET_SYSERR to close it (signal serious error).
111 typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
112 struct GNUNET_MESH_Tunnel *tunnel,
114 const struct GNUNET_MessageHeader *message);
118 * Message handler. Each struct specifies how to handle on particular
119 * type of message received.
121 struct GNUNET_MESH_MessageHandler
124 * Function to call for messages of "type".
126 GNUNET_MESH_MessageCallback callback;
129 * Type of the message this handler covers.
134 * Expected size of messages of this type. Use 0 for variable-size.
135 * If non-zero, messages of the given type will be discarded if they
136 * do not have the right size.
138 uint16_t expected_size;
143 * Method called whenever another peer has added us to a tunnel
144 * the other peer initiated.
145 * Only called (once) upon reception of data with a message type which was
146 * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
147 * causes te tunnel to be ignored and no further notifications are sent about
151 * @param tunnel new handle to the tunnel
152 * @param initiator peer that started the tunnel
153 * @param port Port this tunnel is for.
154 * @return initial tunnel context for the tunnel
155 * (can be NULL -- that's not an error)
157 typedef void *(GNUNET_MESH_InboundTunnelNotificationHandler) (void *cls,
168 * Function called whenever a tunnel is destroyed. Should clean up
169 * any associated state.
171 * It must NOT call GNUNET_MESH_tunnel_destroy on the tunnel.
173 * @param cls closure (set from GNUNET_MESH_connect)
174 * @param tunnel connection to the other end (henceforth invalid)
175 * @param tunnel_ctx place where local state associated
176 * with the tunnel is stored
178 typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
179 const struct GNUNET_MESH_Tunnel *
180 tunnel, void *tunnel_ctx);
184 * Connect to the mesh service.
186 * @param cfg Configuration to use.
187 * @param cls Closure for the various callbacks that follow (including
188 * handlers in the handlers array).
189 * @param new_tunnel Function called when an *incoming* tunnel is created.
190 * Can be NULL if no inbound tunnels are desired.
192 * @param cleaner Function called when a tunnel is destroyed by the remote peer.
193 * It is NOT called if GNUNET_MESH_tunnel_destroy is called on
195 * @param handlers Callbacks for messages we care about, NULL-terminated. Each
196 * one must call GNUNET_MESH_receive_done on the tunnel to
197 * receive the next message. Messages of a type that is not
198 * in the handlers array are ignored if received.
199 * @param ports NULL or 0-terminated array of port numbers for incoming tunnels.
202 * @return handle to the mesh service NULL on error
203 * (in this case, init is never called)
205 struct GNUNET_MESH_Handle *
206 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
207 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
208 GNUNET_MESH_TunnelEndHandler cleaner,
209 const struct GNUNET_MESH_MessageHandler *handlers,
210 const uint32_t *ports);
214 * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
215 * disconnect callbacks will be called on any still connected peers, notifying
216 * about their disconnection. The registered inbound tunnel cleaner will be
217 * called should any inbound tunnels still exist.
219 * @param handle connection to mesh to disconnect
222 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
226 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
229 * @param h mesh handle
230 * @param tunnel_ctx client's tunnel context to associate with the tunnel
231 * @param peer peer identity the tunnel should go to
232 * @param port Port number.
233 * @param nobuffer Flag for disabling buffering on relay nodes.
234 * @param reliable Flag for end-to-end reliability.
236 * @return handle to the tunnel
238 struct GNUNET_MESH_Tunnel *
239 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
241 const struct GNUNET_PeerIdentity *peer,
248 * Destroy an existing tunnel.
250 * The existing end callback for the tunnel will be called immediately.
251 * Any pending outgoing messages will be sent but no incoming messages will be
252 * accepted and no data callbacks will be called.
254 * @param tunnel Tunnel handle, becomes invalid after this call.
257 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel);
261 * Struct to retrieve info about a tunnel.
263 union MeshTunnelInfo {
266 * GNUNET_YES / GNUNET_NO, for binary flags.
271 * Peer on the other side of the tunnel
273 struct GNUNET_PeerIdentity *peer;
278 * Get information about a tunnel.
280 * @param tunnel Tunnel handle.
281 * @param option Query, as listed in src/mesh/mesh.h (GNUNET_MESH_OPTION_*)
282 * @param ... dependant on option, currently not used
284 * @return Union with an answer to the query.
286 const union MeshTunnelInfo *
287 GNUNET_MESH_tunnel_get_info (struct GNUNET_MESH_Tunnel *tunnel,
288 enum MeshTunnelOption option, ...);
292 * Handle for a transmission request.
294 struct GNUNET_MESH_TransmitHandle;
298 * Ask the mesh to call "notify" once it is ready to transmit the
299 * given number of bytes to the specified tunnel.
300 * Only one call can be active at any time, to issue another request,
301 * wait for the callback or cancel the current request.
303 * @param tunnel tunnel to use for transmission
304 * @param cork is corking allowed for this transmission?
305 * @param maxdelay how long can the message wait?
306 * @param notify_size how many bytes of buffer space does notify want?
307 * @param notify function to call when buffer space is available;
308 * will be called with NULL on timeout or if the overall queue
309 * for this peer is larger than queue_size and this is currently
310 * the message with the lowest priority
311 * @param notify_cls closure for notify
312 * @return non-NULL if the notify callback was queued,
313 * NULL if we can not even queue the request (insufficient
314 * memory); if NULL is returned, "notify" will NOT be called.
316 struct GNUNET_MESH_TransmitHandle *
317 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
318 struct GNUNET_TIME_Relative maxdelay,
320 GNUNET_CONNECTION_TransmitReadyNotify notify,
325 * Cancel the specified transmission-ready notification.
327 * @param th handle that was returned by "notify_transmit_ready".
330 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
335 * Indicate readiness to receive the next message on a tunnel.
337 * Should only be called once per handler called.
339 * @param tunnel Tunnel that will be allowed to call another handler.
342 GNUNET_MESH_receive_done (struct GNUNET_MESH_Tunnel *tunnel);
346 /******************************************************************************/
347 /******************** MONITORING /DEBUG API *************************/
348 /******************************************************************************/
349 /* The following calls are not useful for normal MESH operation, but for */
350 /* debug and monitoring of the mesh state. They can be safely ignored. */
351 /* The API can change at any point without notice. */
352 /* Please contact the developer if you consider any of this calls useful for */
353 /* normal mesh applications. */
354 /******************************************************************************/
357 * Method called to retrieve information about each tunnel the mesh peer
360 * @param cls Closure.
361 * @param tunnel_number Tunnel number.
362 * @param origin that started the tunnel (owner).
363 * @param target other endpoint of the tunnel
365 typedef void (*GNUNET_MESH_TunnelsCB) (void *cls,
366 uint32_t tunnel_number,
367 const struct GNUNET_PeerIdentity *origin,
368 const struct GNUNET_PeerIdentity *target);
372 * Method called to retrieve information about a specific tunnel the mesh peer
373 * is aware of, including all transit nodes.
375 * @param cls Closure.
376 * @param peer Peer in the tunnel's tree.
377 * @param parent Parent of the current peer. All 0 when peer is root.
379 typedef void (*GNUNET_MESH_TunnelCB) (void *cls,
380 const struct GNUNET_PeerIdentity *peer,
381 const struct GNUNET_PeerIdentity *parent);
385 * Request information about the running mesh peer.
386 * The callback will be called for every tunnel known to the service,
387 * listing all active peers that belong to the tunnel.
389 * If called again on the same handle, it will overwrite the previous
390 * callback and cls. To retrieve the cls, monitor_cancel must be
393 * WARNING: unstable API, likely to change in the future!
395 * @param h Handle to the mesh peer.
396 * @param callback Function to call with the requested data.
397 * @param callback_cls Closure for @c callback.
400 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
401 GNUNET_MESH_TunnelsCB callback,
406 * Request information about a specific tunnel of the running mesh peer.
408 * WARNING: unstable API, likely to change in the future!
410 * @param h Handle to the mesh peer.
411 * @param initiator ID of the owner of the tunnel.
412 * @param tunnel_number Tunnel number.
413 * @param callback Function to call with the requested data.
414 * @param callback_cls Closure for @c callback.
417 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
418 struct GNUNET_PeerIdentity *initiator,
419 uint32_t tunnel_number,
420 GNUNET_MESH_TunnelCB callback,
425 * Cancel a monitor request. The monitor callback will not be called.
427 * WARNING: unstable API, likely to change in the future!
429 * @param h Mesh handle.
431 * @return Closure given to GNUNET_MESH_monitor, if any.
434 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h);
438 * Create a message queue for a mesh tunnel.
439 * The message queue can only be used to transmit messages,
440 * not to receive them.
442 * @param tunnel the tunnel to create the message qeue for
443 * @return a message queue to messages over the tunnel
445 struct GNUNET_MQ_Handle *
446 GNUNET_MESH_mq_create (struct GNUNET_MESH_Tunnel *tunnel);
449 #if 0 /* keep Emacsens' auto-indent happy */
456 /* ifndef GNUNET_MESH_SERVICE_H */
458 /* end of gnunet_mesh_service.h */