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
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 * @return GNUNET_OK to keep the connection open,
68 * GNUNET_SYSERR to close it (signal serious error)
70 typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
71 struct GNUNET_MESH_Tunnel * tunnel,
73 const struct GNUNET_PeerIdentity *
75 const struct GNUNET_MessageHeader *
77 const struct GNUNET_ATS_Information
82 * Message handler. Each struct specifies how to handle on particular
83 * type of message received.
85 struct GNUNET_MESH_MessageHandler
88 * Function to call for messages of "type".
90 GNUNET_MESH_MessageCallback callback;
93 * Type of the message this handler covers.
98 * Expected size of messages of this type. Use 0 for variable-size.
99 * If non-zero, messages of the given type will be discarded if they
100 * do not have the right size.
102 uint16_t expected_size;
108 * Method called whenever another peer has added us to a tunnel
109 * the other peer initiated.
112 * @param tunnel new handle to the tunnel
113 * @param initiator peer that started the tunnel
114 * @param atsi performance information for the tunnel
115 * @return initial tunnel context for the tunnel
116 * (can be NULL -- that's not an error)
118 typedef void *(GNUNET_MESH_InboundTunnelNotificationHandler) (void *cls,
126 GNUNET_ATS_Information
131 * Function called whenever an inbound tunnel is destroyed. Should clean up
132 * any associated state. This function is NOT called if the client has
133 * explicitly asked for the tunnel to be destroyed using
134 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
137 * @param cls closure (set from GNUNET_MESH_connect)
138 * @param tunnel connection to the other end (henceforth invalid)
139 * @param tunnel_ctx place where local state associated
140 * with the tunnel is stored
142 typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
143 const struct GNUNET_MESH_Tunnel *
144 tunnel, void *tunnel_ctx);
148 * Type for an application. Values defined in gnunet_applications.h
150 typedef uint32_t GNUNET_MESH_ApplicationType;
154 * Connect to the mesh service.
156 * @param cfg configuration to use
157 * @param queue_size size of the data message queue, shared among all tunnels
158 * (each tunnel is guaranteed to accept at least one message,
159 * no matter what is the status of other tunnels)
160 * @param cls closure for the various callbacks that follow
161 * (including handlers in the handlers array)
162 * @param new_tunnel function called when an *inbound* tunnel is created
163 * @param cleaner function called when an *inbound* tunnel is destroyed by the
164 * remote peer, it is *not* called if GNUNET_MESH_tunnel_destroy
165 * is called on the tunnel
166 * @param handlers callbacks for messages we care about, NULL-terminated
167 * note that the mesh is allowed to drop notifications about
168 * inbound messages if the client does not process them fast
169 * enough (for this notification type, a bounded queue is used)
170 * @param stypes list of the applications that this client claims to provide
171 * @return handle to the mesh service NULL on error
172 * (in this case, init is never called)
174 struct GNUNET_MESH_Handle *
175 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
176 unsigned int queue_size, void *cls,
177 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
178 GNUNET_MESH_TunnelEndHandler cleaner,
179 const struct GNUNET_MESH_MessageHandler *handlers,
180 const GNUNET_MESH_ApplicationType *stypes);
184 * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
185 * disconnect callbacks will be called on any still connected peers, notifying
186 * about their disconnection. The registered inbound tunnel cleaner will be
187 * called should any inbound tunnels still exist.
189 * @param handle connection to mesh to disconnect
192 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
196 * Method called whenever a peer has disconnected from the tunnel.
197 * Implementations of this callback must NOT call
198 * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
199 * to run in some other task later. However, calling
200 * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
203 * @param peer peer identity the tunnel stopped working with
205 typedef void (*GNUNET_MESH_PeerDisconnectHandler) (void *cls,
207 GNUNET_PeerIdentity * peer);
211 * Method called whenever a peer has connected to the tunnel.
214 * @param peer peer identity the tunnel was created to, NULL on timeout
215 * @param atsi performance data for the connection
217 * TODO: change to return int to let client allow the new peer or not?
219 typedef void (*GNUNET_MESH_PeerConnectHandler) (void *cls,
220 const struct GNUNET_PeerIdentity
223 GNUNET_ATS_Information * atsi);
227 * Store the given regex (describing offered local services) in the DHT,
228 * in order to be reachable to other peers via connect_by_string.
230 * @param h handle to mesh.
231 * @param regex string with the regular expression describing local services.
234 GNUNET_MESH_announce_regex (struct GNUNET_MESH_Handle *h,
239 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
242 * @param h mesh handle
243 * @param tunnel_ctx client's tunnel context to associate with the tunnel
244 * @param connect_handler function to call when peers are actually connected
245 * @param disconnect_handler function to call when peers are disconnected
246 * @param handler_cls closure for connect/disconnect handlers
248 struct GNUNET_MESH_Tunnel *
249 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, void *tunnel_ctx,
250 GNUNET_MESH_PeerConnectHandler connect_handler,
251 GNUNET_MESH_PeerDisconnectHandler disconnect_handler,
255 * Destroy an existing tunnel. The existing callback for the tunnel will NOT
258 * @param tunnel tunnel handle
261 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel);
265 * Request that a peer should be added to the tunnel. The connect handler
266 * will be called when the peer connects
268 * @param tunnel handle to existing tunnel
269 * @param peer peer to add
272 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
273 const struct GNUNET_PeerIdentity *peer);
277 * Request that a peer should be removed from the tunnel. The existing
278 * disconnect handler will be called ONCE if we were connected.
280 * @param tunnel handle to existing tunnel
281 * @param peer peer to remove
284 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
285 const struct GNUNET_PeerIdentity *peer);
289 * Request that the mesh should try to connect to a peer supporting the given
292 * @param tunnel handle to existing tunnel
293 * @param app_type application type that must be supported by the peer
294 * (MESH should discover peer in proximity handling this type)
297 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
298 GNUNET_MESH_ApplicationType app_type);
302 * Request that the mesh should try to connect to a peer matching the
303 * description given in the service string.
305 * @param tunnel handle to existing tunnel
306 * @param description string describing the destination node requirements
309 GNUNET_MESH_peer_request_connect_by_string (struct GNUNET_MESH_Tunnel *tunnel,
310 const char *description);
314 * Request that the given peer isn't added to this tunnel in calls to
315 * connect_by_* calls, (due to misbehaviour, bad performance, ...).
317 * @param tunnel handle to existing tunnel.
318 * @param peer peer identity of the peer which should be blacklisted
322 GNUNET_MESH_peer_blacklist (struct GNUNET_MESH_Tunnel *tunnel,
323 const struct GNUNET_PeerIdentity *peer);
327 * Request that the given peer isn't blacklisted anymore from this tunnel,
328 * and therefore can be added in future calls to connect_by_*.
329 * The peer must have been previously blacklisted for this tunnel.
331 * @param tunnel handle to existing tunnel.
332 * @param peer peer identity of the peer which shouldn't be blacklisted
333 * for the tunnel anymore.
336 GNUNET_MESH_peer_unblacklist (struct GNUNET_MESH_Tunnel *tunnel,
337 const struct GNUNET_PeerIdentity *peer);
341 * Handle for a transmission request.
343 struct GNUNET_MESH_TransmitHandle;
347 * Ask the mesh to call "notify" once it is ready to transmit the
348 * given number of bytes to the specified tunnel or target.
350 * @param tunnel tunnel to use for transmission
351 * @param cork is corking allowed for this transmission?
352 * @param priority how important is the message?
353 * @param maxdelay how long can the message wait?
354 * @param target destination for the message
355 * NULL for multicast to all tunnel targets
356 * @param notify_size how many bytes of buffer space does notify want?
357 * @param notify function to call when buffer space is available;
358 * will be called with NULL on timeout or if the overall queue
359 * for this peer is larger than queue_size and this is currently
360 * the message with the lowest priority
361 * @param notify_cls closure for notify
362 * @return non-NULL if the notify callback was queued,
363 * NULL if we can not even queue the request (insufficient
364 * memory); if NULL is returned, "notify" will NOT be called.
366 struct GNUNET_MESH_TransmitHandle *
367 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
369 struct GNUNET_TIME_Relative maxdelay,
370 const struct GNUNET_PeerIdentity *target,
372 GNUNET_CONNECTION_TransmitReadyNotify notify,
377 * Cancel the specified transmission-ready notification.
379 * @param th handle that was returned by "notify_transmit_ready".
382 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
387 * Transition API for tunnel ctx management
390 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data);
393 * Transition API for tunnel ctx management
396 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel);
399 #if 0 /* keep Emacsens' auto-indent happy */
406 /* ifndef GNUNET_MESH_SERVICE_H */
408 /* end of gnunet_mesh_service.h */