fix
[oweals/gnunet.git] / src / include / gnunet_mesh_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file include/gnunet_mesh_service.h
23  * @brief mesh service; establish tunnels to distant peers
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_MESH_SERVICE_H
28 #define GNUNET_MESH_SERVICE_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 #include "gnunet_util_lib.h"
39 #include "gnunet_transport_service.h"
40
41 /**
42  * Version number of GNUnet-mesh API.
43  */
44 #define GNUNET_MESH_VERSION 0x00000000
45
46
47 /**
48  * Opaque handle to the service.
49  */
50 struct GNUNET_MESH_Handle;
51
52 /**
53  * Opaque handle to a tunnel.
54  */
55 struct GNUNET_MESH_Tunnel;
56
57 /**
58  * Functions with this signature are called whenever a message is
59  * received or transmitted.
60  *
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)
68  */
69 typedef int
70   (*GNUNET_MESH_MessageCallback) (void *cls,
71                                   const struct GNUNET_MESH_Tunnel *tunnel,
72                                   void **tunnel_ctx,
73                                   const struct GNUNET_MessageHeader *message,
74                                   const struct GNUNET_TRANSPORT_ATS_Information *atsi);
75
76
77 /**
78  * Message handler.  Each struct specifies how to handle on particular
79  * type of message received.
80  */
81 struct GNUNET_MESH_MessageHandler
82 {
83   /**
84    * Function to call for messages of "type".
85    */
86   GNUNET_MESH_MessageCallback callback;
87
88   /**
89    * Type of the message this handler covers.
90    */
91   uint16_t type;
92
93   /**
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.
97    */
98   uint16_t expected_size;
99
100 };
101
102
103 /**
104  * Function called whenever an inbound tunnel is destroyed.  Should clean up
105  * any associated state.
106  *
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
110  */
111 typedef void (GNUNET_MESH_TunnelEndHandler)(void *cls,
112                                             const struct GNUNET_MESH_Tunnel *tunnel,
113                                             void **tunnel_ctx);
114
115
116 /**
117  * Connect to the mesh service.  
118  *
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)
128  */
129 struct GNUNET_MESH_Handle *
130 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
131                      void *cls,
132                      GNUNET_MESH_TunnelEndHandler cleaner,
133                      const struct GNUNET_MESH_MessageHandler *handlers);
134
135
136 /**
137  * Disconnect from the mesh service.
138  *
139  * @param handle connection to mesh to disconnect
140  */
141 void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
142
143
144
145
146
147 /**
148  * Method called whenever a tunnel falls apart.
149  *
150  * @param cls closure
151  * @param peer peer identity the tunnel stopped working with
152  */
153 typedef void (*GNUNET_MESH_TunnelDisconnectHandler) (void *cls,
154                                                      const struct GNUNET_PeerIdentity *peer);
155
156
157 /**
158  * Method called whenever a tunnel is established.
159  *
160  * @param cls closure
161  * @param peer peer identity the tunnel was created to, NULL on timeout
162  * @param atsi performance data for the connection
163  */
164 typedef void (*GNUNET_MESH_TunnelConnectHandler) (void *cls,
165                                                   const struct GNUNET_PeerIdentity *peer,
166                                                   const struct GNUNET_TRANSPORT_ATS_Information *atsi);
167
168
169
170 /**
171  * Handle for a request to the mesh to connect or disconnect
172  * from a particular peer.  Can be used to cancel the request
173  * (before the 'cont'inuation is called).
174  */
175 struct GNUNET_MESH_PeerRequestHandle;
176
177
178 /**
179  * Request that the mesh should try to connect to any of the given peers.
180  *
181  * @param h mesh handle
182  * @param timeout how long to try to establish a connection
183  * @param num_peers length of the peers arrray
184  * @param peers list of candidates to connect to
185  * @param connect_handler function to call on successful connect (or timeout)
186  * @param disconnect_handler function to call on disconnect
187  * @param handler_cls closure for handler
188  * @return NULL on error (handler will not be called), otherwise handle for cancellation
189  */
190 struct GNUNET_MESH_Tunnel *
191 GNUNET_MESH_peer_request_connect_any (struct GNUNET_MESH_Handle *h,
192                                       struct GNUNET_TIME_Relative timeout,
193                                       unsigned int num_peers,
194                                       const struct GNUNET_PeerIdentity *peers,
195                                       GNUNET_MESH_TunnelConnectHandler connect_handler,
196                                       GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
197                                       void *handler_cls);
198
199
200 /**
201  * Request that the mesh should try to connect to all of the given peers.
202  * Messages send to the tunnel will be broadcast.
203  *
204  * @param h mesh handle
205  * @param timeout how long to try to establish a connection
206  * @param num_peers length of the peers arrray
207  * @param peers list of candidates to connect to
208  * @param connect_handler function to call on successful connect (or timeout);
209  *                will be called for EACH of the peers in the list and
210  *                once at the end with 'NULL' on timeout or once we've connected
211  *                to each of the peers in the list
212  * @param disconnect_handler function called if a peer drops out of the tunnel;
213  *                the mesh will NOT try to add it back automatically
214  * @param handler_cls closure for handler
215  * @return NULL on error (handler will not be called), otherwise handle for cancellation
216  */
217 struct GNUNET_MESH_Tunnel *
218 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *h,
219                                       struct GNUNET_TIME_Relative timeout,
220                                       unsigned int num_peers,
221                                       const struct GNUNET_PeerIdentity *peers,
222                                       GNUNET_MESH_TunnelConnectHandler connect_handler,
223                                       GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
224                                       void *handler_cls);
225
226
227 /**
228  * Request that a peer should be added to the tunnel.  The existing
229  * connect handler will be called ONCE with either success or failure.
230  *
231  * @param tunnel handle to existing tunnel
232  * @param timeout how long to try to establish a connection
233  * @param peer peer to add
234  */
235 void
236 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
237                                       struct GNUNET_TIME_Relative timeout,
238                                       const struct GNUNET_PeerIdentity *peer);
239
240
241 /**
242  * Request that the mesh should try to connect to a peer supporting the given
243  * message type.
244  *
245  * @param h mesh handle
246  * @param timeout how long to try to establish a connection
247  * @param message_type message type that must be supported by the peer (MESH should
248  *                discover peer in proximity handling this type)
249  * @param connect_handler function to call on successful connect (or timeout);
250  *                will be called for EACH of the peers in the list and
251  *                once at the end with 'NULL' on timeout or once we've connected
252  *                to each of the peers in the list
253  * @param disconnect_handler function called if a peer drops out of the tunnel;
254  *                the mesh will NOT try to add it back automatically
255  * @param handler_cls closure for handler
256  * @return NULL on error (handler will not be called), otherwise handle for cancellation
257  */
258 struct GNUNET_MESH_Tunnel *
259 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *h,
260                                           struct GNUNET_TIME_Relative timeout,
261                                           uint16_t message_type,
262                                           GNUNET_MESH_TunnelConnectHandler connect_handler,
263                                           GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
264                                           void *handler_cls);
265
266
267 /**
268  * Cancel a pending request to connect to a particular peer.  Destroys the
269  * tunnel. 
270  *
271  * @param req request handle that was returned for the original request
272  */
273 void
274 GNUNET_MESH_peer_request_connect_cancel (struct GNUNET_MESH_Tunnel *req);
275
276
277 /**
278  * Handle for a transmission request.
279  */
280 struct GNUNET_MESH_TransmitHandle;
281
282
283 /**
284  * Ask the mesh to call "notify" once it is ready to transmit the
285  * given number of bytes to the specified "target".  If we are not yet
286  * connected to the specified peer, a call to this function will cause
287  * us to try to establish a connection.
288  *
289  * @param tunnel tunnel to use for transmission
290  * @param cork is corking allowed for this transmission?
291  * @param priority how important is the message?
292  * @param maxdelay how long can the message wait?
293  * @param notify_size how many bytes of buffer space does notify want?
294  * @param notify function to call when buffer space is available;
295  *        will be called with NULL on timeout or if the overall queue
296  *        for this peer is larger than queue_size and this is currently
297  *        the message with the lowest priority
298  * @param notify_cls closure for notify
299  * @return non-NULL if the notify callback was queued,
300  *         NULL if we can not even queue the request (insufficient
301  *         memory); if NULL is returned, "notify" will NOT be called.
302  */
303 struct GNUNET_MESH_TransmitHandle *
304 GNUNET_MESH_notify_transmit_ready (struct
305                                    GNUNET_MESH_Tunnel
306                                    *tunnel,
307                                    int cork,
308                                    uint32_t priority,
309                                    struct
310                                    GNUNET_TIME_Relative
311                                    maxdelay,
312                                    size_t
313                                    notify_size,
314                                    GNUNET_CONNECTION_TransmitReadyNotify
315                                    notify,
316                                    void
317                                    *notify_cls);
318
319
320 /**
321  * Cancel the specified transmission-ready notification.
322  *
323  * @param th handle that was returned by "notify_transmit_ready".
324  */
325 void
326 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
327                                           *th);
328
329
330 #if 0                           /* keep Emacsens' auto-indent happy */
331 {
332 #endif
333 #ifdef __cplusplus
334 }
335 #endif
336
337 /* ifndef GNUNET_MESH_SERVICE_H */
338 #endif
339 /* end of gnunet_mesh_service.h */