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 * @return GNUNET_OK to keep the connection open,
68 * GNUNET_SYSERR to close it (signal serious error)
71 (*GNUNET_MESH_MessageCallback) (void *cls,
72 struct GNUNET_MESH_Tunnel *tunnel,
74 const struct GNUNET_PeerIdentity *sender,
75 const struct GNUNET_MessageHeader *message,
76 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
80 * Message handler. Each struct specifies how to handle on particular
81 * type of message received.
83 struct GNUNET_MESH_MessageHandler
86 * Function to call for messages of "type".
88 GNUNET_MESH_MessageCallback callback;
91 * Type of the message this handler covers.
96 * Expected size of messages of this type. Use 0 for variable-size.
97 * If non-zero, messages of the given type will be discarded if they
98 * do not have the right size.
100 uint16_t expected_size;
106 * Function called whenever an inbound tunnel is destroyed. Should clean up
107 * any associated state.
109 * @param cls closure (set from GNUNET_MESH_connect)
110 * @param tunnel connection to the other end (henceforth invalid)
111 * @param tunnel_ctx place where local state associated with the tunnel is stored
113 typedef void (GNUNET_MESH_TunnelEndHandler)(void *cls,
114 const struct GNUNET_MESH_Tunnel *tunnel,
119 * Type for an application. Values defined in gnunet_applications.h
121 typedef uint32_t GNUNET_MESH_ApplicationType;
125 * Connect to the mesh service.
127 * @param cfg configuration to use
128 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
129 * @param cleaner function called when an *inbound* tunnel is destroyed
130 * @param handlers callbacks for messages we care about, NULL-terminated
131 * note that the mesh is allowed to drop notifications about inbound
132 * messages if the client does not process them fast enough (for this
133 * notification type, a bounded queue is used)
134 * @return handle to the mesh service
135 * NULL on error (in this case, init is never called)
137 struct GNUNET_MESH_Handle *
138 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
140 GNUNET_MESH_TunnelEndHandler cleaner,
141 const struct GNUNET_MESH_MessageHandler *handlers,
142 const GNUNET_MESH_ServiceType *stypes);
145 * Get the peer on the other side of this tunnel if it is just one. Return NULL otherwise
147 * @param tunnel the tunnel
148 * @return the peer or NULL
150 const struct GNUNET_PeerIdentity*
151 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel);
155 * Disconnect from the mesh service.
157 * @param handle connection to mesh to disconnect
159 void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
166 * Method called whenever a tunnel falls apart.
169 * @param peer peer identity the tunnel stopped working with
171 typedef void (*GNUNET_MESH_TunnelDisconnectHandler) (void *cls,
172 const struct GNUNET_PeerIdentity *peer);
176 * Method called whenever a tunnel is established.
179 * @param peer peer identity the tunnel was created to, NULL on timeout
180 * @param atsi performance data for the connection
182 typedef void (*GNUNET_MESH_TunnelConnectHandler) (void *cls,
183 const struct GNUNET_PeerIdentity *peer,
184 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
189 * Handle for a request to the mesh to connect or disconnect
190 * from a particular peer. Can be used to cancel the request
191 * (before the 'cont'inuation is called).
193 struct GNUNET_MESH_PeerRequestHandle;
197 * Request that the mesh should try to connect to any of the given peers.
199 * @param h mesh handle
200 * @param timeout how long to try to establish a connection
201 * @param num_peers length of the peers array
202 * @param peers list of candidates to connect to
203 * @param connect_handler function to call on successful connect (or timeout)
204 * @param disconnect_handler function to call on disconnect
205 * @param handler_cls closure for handler
206 * @return NULL on error (handler will not be called), otherwise handle for cancellation
208 struct GNUNET_MESH_Tunnel *
209 GNUNET_MESH_peer_request_connect_any (struct GNUNET_MESH_Handle *h,
210 struct GNUNET_TIME_Relative timeout,
211 unsigned int num_peers,
212 const struct GNUNET_PeerIdentity *peers,
213 GNUNET_MESH_TunnelConnectHandler connect_handler,
214 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
219 * Request that the mesh should try to connect to all of the given peers.
220 * Messages send to the tunnel will be broadcast.
222 * @param h mesh handle
223 * @param timeout how long to try to establish a connection
224 * @param num_peers length of the peers array
225 * @param peers list of candidates to connect to
226 * @param connect_handler function to call on successful connect (or timeout);
227 * will be called for EACH of the peers in the list and
228 * once at the end with 'NULL' on timeout or once we've connected
229 * to each of the peers in the list
230 * @param disconnect_handler function called if a peer drops out of the tunnel;
231 * the mesh will NOT try to add it back automatically
232 * @param handler_cls closure for handler
233 * @return NULL on error (handler will not be called), otherwise handle for cancellation
235 struct GNUNET_MESH_Tunnel *
236 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *h,
237 struct GNUNET_TIME_Relative timeout,
238 unsigned int num_peers,
239 const struct GNUNET_PeerIdentity *peers,
240 GNUNET_MESH_TunnelConnectHandler connect_handler,
241 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
246 * Request that a peer should be added to the tunnel. The existing
247 * connect handler will be called ONCE with either success or failure.
249 * @param tunnel handle to existing tunnel
250 * @param timeout how long to try to establish a connection
251 * @param peer peer to add
254 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
255 struct GNUNET_TIME_Relative timeout,
256 const struct GNUNET_PeerIdentity *peer);
260 * Request that a peer should be removed from the tunnel. The existing
261 * disconnect handler will be called ONCE if we were connected.
263 * @param tunnel handle to existing tunnel
264 * @param peer peer to remove
267 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
268 const struct GNUNET_PeerIdentity *peer);
272 * Request that the mesh should try to connect to a peer supporting the given
275 * @param h mesh handle
276 * @param timeout how long to try to establish a connection
277 * @param app_type application type that must be supported by the peer (MESH should
278 * discover peer in proximity handling this type)
279 * @param connect_handler function to call on successful connect (or timeout);
280 * will be called for EACH of the peers in the list and
281 * once at the end with 'NULL' on timeout or once we've connected
282 * to each of the peers in the list
283 * @param disconnect_handler function called if a peer drops out of the tunnel;
284 * the mesh will NOT try to add it back automatically
285 * @param handler_cls closure for handler
286 * @return NULL on error (handler will not be called), otherwise handle for cancellation
288 struct GNUNET_MESH_Tunnel *
289 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *h,
290 struct GNUNET_TIME_Relative timeout,
291 GNUNET_MESH_ApplicationType app_type,
292 GNUNET_MESH_TunnelConnectHandler connect_handler,
293 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
298 * Cancel a pending request to connect to a particular peer. Destroys the
301 * @param req request handle that was returned for the original request
304 GNUNET_MESH_peer_request_connect_cancel (struct GNUNET_MESH_Tunnel *req);
308 * Handle for a transmission request.
310 struct GNUNET_MESH_TransmitHandle;
314 * Ask the mesh to call "notify" once it is ready to transmit the
315 * given number of bytes to the specified "target". If we are not yet
316 * connected to the specified peer, a call to this function will cause
317 * us to try to establish a connection.
319 * @param tunnel tunnel to use for transmission
320 * @param cork is corking allowed for this transmission?
321 * @param priority how important is the message?
322 * @param maxdelay how long can the message wait?
323 * @param target destination for the message, NULL for multicast to all tunnel targets
324 * @param notify_size how many bytes of buffer space does notify want?
325 * @param notify function to call when buffer space is available;
326 * will be called with NULL on timeout or if the overall queue
327 * for this peer is larger than queue_size and this is currently
328 * the message with the lowest priority
329 * @param notify_cls closure for notify
330 * @return non-NULL if the notify callback was queued,
331 * NULL if we can not even queue the request (insufficient
332 * memory); if NULL is returned, "notify" will NOT be called.
334 struct GNUNET_MESH_TransmitHandle *
335 GNUNET_MESH_notify_transmit_ready (struct
343 const struct GNUNET_PeerIdentity *target,
346 GNUNET_CONNECTION_TransmitReadyNotify
353 * Cancel the specified transmission-ready notification.
355 * @param th handle that was returned by "notify_transmit_ready".
358 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
362 #if 0 /* keep Emacsens' auto-indent happy */
369 /* ifndef GNUNET_MESH_SERVICE_H */
371 /* end of gnunet_mesh_service.h */