2 This file is part of GNUnet.
3 (C) 2009, 2010 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 #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 0x00000000
48 * Opaque handle to the service.
50 struct GNUNET_MESH_Handle;
53 * Opaque handle to a tunnel.
55 struct GNUNET_MESH_Tunnel;
58 * Functions with this signature are called whenever a message is
59 * received or transmitted.
61 * @param cls closure (set from GNUNET_MESH_connect)
62 * @param tunnel connection to the other end
63 * @param tunnel_ctx place to store local state associated with the tunnel
64 * @param sender who sent the message
65 * @param message the actual message
66 * @param atsi performance data for the connection
67 * @param atsi_count number of records in 'atsi'
68 * @return GNUNET_OK to keep the connection open,
69 * GNUNET_SYSERR to close it (signal serious error)
71 typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
72 struct GNUNET_MESH_Tunnel * tunnel,
74 const struct GNUNET_PeerIdentity *
76 const struct GNUNET_MessageHeader *
79 GNUNET_ATS_Information *
81 unsigned int atsi_count);
85 * Message handler. Each struct specifies how to handle on particular
86 * type of message received.
88 struct GNUNET_MESH_MessageHandler
91 * Function to call for messages of "type".
93 GNUNET_MESH_MessageCallback callback;
96 * Type of the message this handler covers.
101 * Expected size of messages of this type. Use 0 for variable-size.
102 * If non-zero, messages of the given type will be discarded if they
103 * do not have the right size.
105 uint16_t expected_size;
111 * Function called whenever an inbound tunnel is destroyed. Should clean up
112 * any associated state.
114 * @param cls closure (set from GNUNET_MESH_connect)
115 * @param tunnel connection to the other end (henceforth invalid)
116 * @param tunnel_ctx place where local state associated with the tunnel is stored
118 typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
119 const struct GNUNET_MESH_Tunnel *
120 tunnel, void **tunnel_ctx);
124 * Type for an application. Values defined in gnunet_applications.h
126 typedef uint32_t GNUNET_MESH_ApplicationType;
130 * Connect to the mesh service.
132 * @param cfg configuration to use
133 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
134 * @param cleaner function called when an *inbound* tunnel is destroyed
135 * @param handlers callbacks for messages we care about, NULL-terminated
136 * note that the mesh is allowed to drop notifications about inbound
137 * messages if the client does not process them fast enough (for this
138 * notification type, a bounded queue is used)
139 * @param stypes Application Types the client claims to offer
140 * @return handle to the mesh service
141 * NULL on error (in this case, init is never called)
143 struct GNUNET_MESH_Handle *
144 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
145 GNUNET_MESH_TunnelEndHandler cleaner,
146 const struct GNUNET_MESH_MessageHandler *handlers,
147 const GNUNET_MESH_ApplicationType *stypes);
150 * Get the peer on the other side of this tunnel if it is just one. Return NULL otherwise
152 * @param tunnel the tunnel
153 * @return the peer or NULL
155 const struct GNUNET_PeerIdentity *
156 GNUNET_MESH_get_peer (const struct GNUNET_MESH_Tunnel *tunnel);
160 * Disconnect from the mesh service.
162 * @param handle connection to mesh to disconnect
165 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
172 * Method called whenever a tunnel falls apart.
175 * @param peer peer identity the tunnel stopped working with
177 typedef void (*GNUNET_MESH_TunnelDisconnectHandler) (void *cls,
179 GNUNET_PeerIdentity *
184 * Method called whenever a tunnel is established.
187 * @param peer peer identity the tunnel was created to, NULL on timeout
188 * @param atsi performance data for the connection
189 * @param atsi_count number of records in 'atsi'
191 typedef void (*GNUNET_MESH_TunnelConnectHandler) (void *cls,
193 GNUNET_PeerIdentity * peer,
195 GNUNET_ATS_Information
197 unsigned int atsi_count);
202 * Handle for a request to the mesh to connect or disconnect
203 * from a particular peer. Can be used to cancel the request
204 * (before the 'cont'inuation is called).
206 struct GNUNET_MESH_PeerRequestHandle;
210 * Request that the mesh should try to connect to any of the given peers.
212 * @param h mesh handle
213 * @param timeout how long to try to establish a connection
214 * @param num_peers length of the peers array
215 * @param peers list of candidates to connect to
216 * @param connect_handler function to call on successful connect (or timeout)
217 * @param disconnect_handler function to call on disconnect
218 * @param handler_cls closure for handler
219 * @return NULL on error (handler will not be called), otherwise handle for cancellation
221 struct GNUNET_MESH_Tunnel *
222 GNUNET_MESH_peer_request_connect_any (struct GNUNET_MESH_Handle *h,
223 struct GNUNET_TIME_Relative timeout,
224 unsigned int num_peers,
225 const struct GNUNET_PeerIdentity *peers,
226 GNUNET_MESH_TunnelConnectHandler
228 GNUNET_MESH_TunnelDisconnectHandler
229 disconnect_handler, void *handler_cls);
233 * Request that the mesh should try to connect to all of the given peers.
234 * Messages send to the tunnel will be broadcast.
236 * @param h mesh handle
237 * @param timeout how long to try to establish a connection
238 * @param num_peers length of the peers array
239 * @param peers list of candidates to connect to
240 * @param connect_handler function to call on successful connect (or timeout);
241 * will be called for EACH of the peers in the list and
242 * once at the end with 'NULL' on timeout or once we've connected
243 * to each of the peers in the list
244 * @param disconnect_handler function called if a peer drops out of the tunnel;
245 * the mesh will NOT try to add it back automatically
246 * @param handler_cls closure for handler
247 * @return NULL on error (handler will not be called), otherwise handle for cancellation
249 struct GNUNET_MESH_Tunnel *
250 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *h,
251 struct GNUNET_TIME_Relative timeout,
252 unsigned int num_peers,
253 const struct GNUNET_PeerIdentity *peers,
254 GNUNET_MESH_TunnelConnectHandler
256 GNUNET_MESH_TunnelDisconnectHandler
257 disconnect_handler, void *handler_cls);
261 * Request that a peer should be added to the tunnel. The existing
262 * connect handler will be called ONCE with either success or failure.
264 * @param tunnel handle to existing tunnel
265 * @param timeout how long to try to establish a connection
266 * @param peer peer to add
269 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
270 struct GNUNET_TIME_Relative timeout,
271 const struct GNUNET_PeerIdentity *peer);
275 * Request that a peer should be removed from the tunnel. The existing
276 * disconnect handler will be called ONCE if we were connected.
278 * @param tunnel handle to existing tunnel
279 * @param peer peer to remove
282 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
283 const struct GNUNET_PeerIdentity *peer);
287 * Request that the mesh should try to connect to a peer supporting the given
290 * @param h mesh handle
291 * @param timeout how long to try to establish a connection
292 * @param app_type application type that must be supported by the peer (MESH should
293 * discover peer in proximity handling this type)
294 * @param connect_handler function to call on successful connect (or timeout);
295 * will be called for EACH of the peers in the list and
296 * once at the end with 'NULL' on timeout or once we've connected
297 * to each of the peers in the list
298 * @param disconnect_handler function called if a peer drops out of the tunnel;
299 * the mesh will NOT try to add it back automatically
300 * @param handler_cls closure for handler
301 * @return NULL on error (handler will not be called), otherwise handle for cancellation
303 struct GNUNET_MESH_Tunnel *
304 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *h,
305 struct GNUNET_TIME_Relative timeout,
306 GNUNET_MESH_ApplicationType app_type,
307 GNUNET_MESH_TunnelConnectHandler
309 GNUNET_MESH_TunnelDisconnectHandler
315 * Cancel a pending request to connect to a particular peer. Destroys the
318 * @param req request handle that was returned for the original request
321 GNUNET_MESH_peer_request_connect_cancel (struct GNUNET_MESH_Tunnel *req);
325 * Handle for a transmission request.
327 struct GNUNET_MESH_TransmitHandle;
331 * Ask the mesh to call "notify" once it is ready to transmit the
332 * given number of bytes to the specified "target". If we are not yet
333 * connected to the specified peer, a call to this function will cause
334 * us to try to establish a connection.
336 * @param tunnel tunnel to use for transmission
337 * @param cork is corking allowed for this transmission?
338 * @param priority how important is the message?
339 * @param maxdelay how long can the message wait?
340 * @param target destination for the message, NULL for multicast to all tunnel targets
341 * @param notify_size how many bytes of buffer space does notify want?
342 * @param notify function to call when buffer space is available;
343 * will be called with NULL on timeout or if the overall queue
344 * for this peer is larger than queue_size and this is currently
345 * the message with the lowest priority
346 * @param notify_cls closure for notify
347 * @return non-NULL if the notify callback was queued,
348 * NULL if we can not even queue the request (insufficient
349 * memory); if NULL is returned, "notify" will NOT be called.
351 struct GNUNET_MESH_TransmitHandle *
352 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
354 struct GNUNET_TIME_Relative maxdelay,
355 const struct GNUNET_PeerIdentity *target,
357 GNUNET_CONNECTION_TransmitReadyNotify notify,
362 * Cancel the specified transmission-ready notification.
364 * @param th handle that was returned by "notify_transmit_ready".
367 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
371 GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel, void *head);
373 GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel, void *tail);
375 GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel);
377 GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel);
380 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data);
382 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel);
384 #if 0 /* keep Emacsens' auto-indent happy */
391 /* ifndef GNUNET_MESH_SERVICE_H */
393 /* end of gnunet_mesh_service.h */