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 message the actual message
65 * @param atsi performance data for the connection
66 * @return GNUNET_OK to keep the connection open,
67 * GNUNET_SYSERR to close it (signal serious error)
70 (*GNUNET_MESH_MessageCallback) (void *cls,
71 struct GNUNET_MESH_Tunnel *tunnel,
73 const struct GNUNET_MessageHeader *message,
74 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
78 * Message handler. Each struct specifies how to handle on particular
79 * type of message received.
81 struct GNUNET_MESH_MessageHandler
84 * Function to call for messages of "type".
86 GNUNET_MESH_MessageCallback callback;
89 * Type of the message this handler covers.
94 * Expected size of messages of this type. Use 0 for variable-size.
95 * If non-zero, messages of the given type will be discarded if they
96 * do not have the right size.
98 uint16_t expected_size;
104 * Function called whenever an inbound tunnel is destroyed. Should clean up
105 * any associated state.
107 * @param cls closure (set from GNUNET_MESH_connect)
108 * @param tunnel connection to the other end (henceforth invalid)
109 * @param tunnel_ctx place where local state associated with the tunnel is stored
111 typedef void (GNUNET_MESH_TunnelEndHandler)(void *cls,
112 const struct GNUNET_MESH_Tunnel *tunnel,
117 * Connect to the mesh service.
119 * @param cfg configuration to use
120 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
121 * @param cleaner function called when an *inbound* tunnel is destroyed
122 * @param handlers callbacks for messages we care about, NULL-terminated
123 * note that the mesh is allowed to drop notifications about inbound
124 * messages if the client does not process them fast enough (for this
125 * notification type, a bounded queue is used)
126 * @return handle to the mesh service
127 * NULL on error (in this case, init is never called)
129 struct GNUNET_MESH_Handle *
130 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
132 GNUNET_MESH_TunnelEndHandler cleaner,
133 const struct GNUNET_MESH_MessageHandler *handlers);
137 * Get the peer on the other side of this tunnel if it is just one. Return NULL otherwise
139 * @param tunnel the tunnel
140 * @return the peer or NULL
142 const struct GNUNET_PeerIdentity*
143 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel);
147 * Disconnect from the mesh service.
149 * @param handle connection to mesh to disconnect
151 void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
158 * Method called whenever a tunnel falls apart.
161 * @param peer peer identity the tunnel stopped working with
163 typedef void (*GNUNET_MESH_TunnelDisconnectHandler) (void *cls,
164 const struct GNUNET_PeerIdentity *peer);
168 * Method called whenever a tunnel is established.
171 * @param peer peer identity the tunnel was created to, NULL on timeout
172 * @param atsi performance data for the connection
174 typedef void (*GNUNET_MESH_TunnelConnectHandler) (void *cls,
175 const struct GNUNET_PeerIdentity *peer,
176 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
181 * Handle for a request to the mesh to connect or disconnect
182 * from a particular peer. Can be used to cancel the request
183 * (before the 'cont'inuation is called).
185 struct GNUNET_MESH_PeerRequestHandle;
189 * Request that the mesh should try to connect to any of the given peers.
191 * @param h mesh handle
192 * @param timeout how long to try to establish a connection
193 * @param num_peers length of the peers arrray
194 * @param peers list of candidates to connect to
195 * @param connect_handler function to call on successful connect (or timeout)
196 * @param disconnect_handler function to call on disconnect
197 * @param handler_cls closure for handler
198 * @return NULL on error (handler will not be called), otherwise handle for cancellation
200 struct GNUNET_MESH_Tunnel *
201 GNUNET_MESH_peer_request_connect_any (struct GNUNET_MESH_Handle *h,
202 struct GNUNET_TIME_Relative timeout,
203 unsigned int num_peers,
204 const struct GNUNET_PeerIdentity *peers,
205 GNUNET_MESH_TunnelConnectHandler connect_handler,
206 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
211 * Request that the mesh should try to connect to all of the given peers.
212 * Messages send to the tunnel will be broadcast.
214 * @param h mesh handle
215 * @param timeout how long to try to establish a connection
216 * @param num_peers length of the peers arrray
217 * @param peers list of candidates to connect to
218 * @param connect_handler function to call on successful connect (or timeout);
219 * will be called for EACH of the peers in the list and
220 * once at the end with 'NULL' on timeout or once we've connected
221 * to each of the peers in the list
222 * @param disconnect_handler function called if a peer drops out of the tunnel;
223 * the mesh will NOT try to add it back automatically
224 * @param handler_cls closure for handler
225 * @return NULL on error (handler will not be called), otherwise handle for cancellation
227 struct GNUNET_MESH_Tunnel *
228 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *h,
229 struct GNUNET_TIME_Relative timeout,
230 unsigned int num_peers,
231 const struct GNUNET_PeerIdentity *peers,
232 GNUNET_MESH_TunnelConnectHandler connect_handler,
233 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
238 * Request that a peer should be added to the tunnel. The existing
239 * connect handler will be called ONCE with either success or failure.
241 * @param tunnel handle to existing tunnel
242 * @param timeout how long to try to establish a connection
243 * @param peer peer to add
246 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
247 struct GNUNET_TIME_Relative timeout,
248 const struct GNUNET_PeerIdentity *peer);
252 * Request that the mesh should try to connect to a peer supporting the given
255 * @param h mesh handle
256 * @param timeout how long to try to establish a connection
257 * @param message_type message type that must be supported by the peer (MESH should
258 * discover peer in proximity handling this type)
259 * @param connect_handler function to call on successful connect (or timeout);
260 * will be called for EACH of the peers in the list and
261 * once at the end with 'NULL' on timeout or once we've connected
262 * to each of the peers in the list
263 * @param disconnect_handler function called if a peer drops out of the tunnel;
264 * the mesh will NOT try to add it back automatically
265 * @param handler_cls closure for handler
266 * @return NULL on error (handler will not be called), otherwise handle for cancellation
268 struct GNUNET_MESH_Tunnel *
269 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *h,
270 struct GNUNET_TIME_Relative timeout,
271 uint16_t message_type,
272 GNUNET_MESH_TunnelConnectHandler connect_handler,
273 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
278 * Cancel a pending request to connect to a particular peer. Destroys the
281 * @param req request handle that was returned for the original request
284 GNUNET_MESH_peer_request_connect_cancel (struct GNUNET_MESH_Tunnel *req);
288 * Handle for a transmission request.
290 struct GNUNET_MESH_TransmitHandle;
294 * Ask the mesh to call "notify" once it is ready to transmit the
295 * given number of bytes to the specified "target". If we are not yet
296 * connected to the specified peer, a call to this function will cause
297 * us to try to establish a connection.
299 * @param tunnel tunnel to use for transmission
300 * @param cork is corking allowed for this transmission?
301 * @param priority how important is the message?
302 * @param maxdelay how long can the message wait?
303 * @param notify_size how many bytes of buffer space does notify want?
304 * @param notify function to call when buffer space is available;
305 * will be called with NULL on timeout or if the overall queue
306 * for this peer is larger than queue_size and this is currently
307 * the message with the lowest priority
308 * @param notify_cls closure for notify
309 * @return non-NULL if the notify callback was queued,
310 * NULL if we can not even queue the request (insufficient
311 * memory); if NULL is returned, "notify" will NOT be called.
313 struct GNUNET_MESH_TransmitHandle *
314 GNUNET_MESH_notify_transmit_ready (struct
324 GNUNET_CONNECTION_TransmitReadyNotify
331 * Cancel the specified transmission-ready notification.
333 * @param th handle that was returned by "notify_transmit_ready".
336 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
340 #if 0 /* keep Emacsens' auto-indent happy */
347 /* ifndef GNUNET_MESH_SERVICE_H */
349 /* end of gnunet_mesh_service.h */