96e108239033fb73c88fc5169f307ce6bedc33ba
[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.
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 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)
69  */
70 typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
71                                             struct GNUNET_MESH_Tunnel * tunnel,
72                                             void **tunnel_ctx,
73                                             const struct GNUNET_PeerIdentity *
74                                             sender,
75                                             const struct GNUNET_MessageHeader *
76                                             message,
77                                             const struct GNUNET_ATS_Information
78                                             * atsi);
79
80
81 /**
82  * Message handler.  Each struct specifies how to handle on particular
83  * type of message received.
84  */
85 struct GNUNET_MESH_MessageHandler
86 {
87   /**
88    * Function to call for messages of "type".
89    */
90   GNUNET_MESH_MessageCallback callback;
91
92   /**
93    * Type of the message this handler covers.
94    */
95   uint16_t type;
96
97   /**
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.
101    */
102   uint16_t expected_size;
103
104 };
105
106
107 /**
108  * Method called whenever another peer has added us to a tunnel
109  * the other peer initiated.
110  *
111  * @param cls closure
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)
117  */
118 typedef void *(GNUNET_MESH_InboundTunnelNotificationHandler) (void *cls,
119                                                               struct
120                                                               GNUNET_MESH_Tunnel
121                                                               * tunnel,
122                                                               const struct
123                                                               GNUNET_PeerIdentity
124                                                               * initiator,
125                                                               const struct
126                                                               GNUNET_ATS_Information
127                                                               * atsi);
128
129
130 /**
131  * Function called whenever an inbound tunnel is destroyed.  Should clean up
132  * any associated state.
133  *
134  * @param cls closure (set from GNUNET_MESH_connect)
135  * @param tunnel connection to the other end (henceforth invalid)
136  * @param tunnel_ctx place where local state associated
137  *                   with the tunnel is stored
138  */
139 typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
140                                              const struct GNUNET_MESH_Tunnel *
141                                              tunnel, void *tunnel_ctx);
142
143
144 /**
145  * Type for an application.  Values defined in gnunet_applications.h
146  */
147 typedef uint32_t GNUNET_MESH_ApplicationType;
148
149
150 /**
151  * Connect to the mesh service.
152  *
153  * @param cfg configuration to use
154  * @param queue_size size of the data message queue, shared among all tunnels
155  *                   (each tunnel is guaranteed to accept at least one message,
156  *                    no matter what is the status of other tunnels)
157  * @param cls closure for the various callbacks that follow
158  *            (including handlers in the handlers array)
159  * @param new_tunnel function called when an *inbound* tunnel is created
160  * @param cleaner function called when an *inbound* tunnel is destroyed
161  * @param handlers callbacks for messages we care about, NULL-terminated
162  *                note that the mesh is allowed to drop notifications about
163  *                inbound messages if the client does not process them fast
164  *                enough (for this notification type, a bounded queue is used)
165  * @param stypes list of the applications that this client claims to provide
166  * @return handle to the mesh service NULL on error
167  *         (in this case, init is never called)
168  */
169 struct GNUNET_MESH_Handle *
170 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
171                      unsigned int queue_size, void *cls,
172                      GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
173                      GNUNET_MESH_TunnelEndHandler cleaner,
174                      const struct GNUNET_MESH_MessageHandler *handlers,
175                      const GNUNET_MESH_ApplicationType *stypes);
176
177
178 /**
179  * Disconnect from the mesh service.
180  *
181  * @param handle connection to mesh to disconnect
182  */
183 void
184 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
185
186
187 /**
188  * Method called whenever a peer has disconnected from the tunnel.
189  * Implementations of this callback must NOT call
190  * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
191  * to run in some other task later.  However, calling 
192  * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
193  *
194  * @param cls closure
195  * @param peer peer identity the tunnel stopped working with
196  */
197 typedef void (*GNUNET_MESH_PeerDisconnectHandler) (void *cls,
198                                                    const struct
199                                                    GNUNET_PeerIdentity * peer);
200
201
202 /**
203  * Method called whenever a peer has connected to the tunnel.
204  *
205  * @param cls closure
206  * @param peer peer identity the tunnel was created to, NULL on timeout
207  * @param atsi performance data for the connection
208  *
209  * TODO: change to return int to let client allow the new peer or not?
210  */
211 typedef void (*GNUNET_MESH_PeerConnectHandler) (void *cls,
212                                                 const struct GNUNET_PeerIdentity
213                                                 * peer,
214                                                 const struct
215                                                 GNUNET_ATS_Information * atsi);
216
217
218
219 /**
220  * Create a new tunnel (we're initiator and will be allowed to add/remove peers
221  * and to broadcast).
222  *
223  * @param h mesh handle
224  * @param tunnel_ctx client's tunnel context to associate with the tunnel
225  * @param connect_handler function to call when peers are actually connected
226  * @param disconnect_handler function to call when peers are disconnected
227  * @param handler_cls closure for connect/disconnect handlers
228  */
229 struct GNUNET_MESH_Tunnel *
230 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, void *tunnel_ctx,
231                            GNUNET_MESH_PeerConnectHandler connect_handler,
232                            GNUNET_MESH_PeerDisconnectHandler disconnect_handler,
233                            void *handler_cls);
234
235 /**
236  * Destroy an existing tunnel.
237  *
238  * @param tunnel tunnel handle
239  */
240 void
241 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel);
242
243
244 /**
245  * Request that a peer should be added to the tunnel.  The connect handler
246  * will be called when the peer connects
247  *
248  * @param tunnel handle to existing tunnel
249  * @param peer peer to add
250  */
251 void
252 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
253                                       const struct GNUNET_PeerIdentity *peer);
254
255
256 /**
257  * Request that a peer should be removed from the tunnel.  The existing
258  * disconnect handler will be called ONCE if we were connected.
259  *
260  * @param tunnel handle to existing tunnel
261  * @param peer peer to remove
262  */
263 void
264 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
265                                       const struct GNUNET_PeerIdentity *peer);
266
267
268 /**
269  * Request that the mesh should try to connect to a peer supporting the given
270  * message type.
271  *
272  * @param tunnel handle to existing tunnel
273  * @param app_type application type that must be supported by the peer
274  *                 (MESH should discover peer in proximity handling this type)
275  */
276 void
277 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
278                                           GNUNET_MESH_ApplicationType app_type);
279
280
281 /**
282  * Handle for a transmission request.
283  */
284 struct GNUNET_MESH_TransmitHandle;
285
286
287 /**
288  * Ask the mesh to call "notify" once it is ready to transmit the
289  * given number of bytes to the specified tunnel or target.
290  *
291  * @param tunnel tunnel to use for transmission
292  * @param cork is corking allowed for this transmission?
293  * @param priority how important is the message?
294  * @param maxdelay how long can the message wait?
295  * @param target destination for the message
296  *               NULL for multicast to all tunnel targets
297  * @param notify_size how many bytes of buffer space does notify want?
298  * @param notify function to call when buffer space is available;
299  *        will be called with NULL on timeout or if the overall queue
300  *        for this peer is larger than queue_size and this is currently
301  *        the message with the lowest priority
302  * @param notify_cls closure for notify
303  * @return non-NULL if the notify callback was queued,
304  *         NULL if we can not even queue the request (insufficient
305  *         memory); if NULL is returned, "notify" will NOT be called.
306  */
307 struct GNUNET_MESH_TransmitHandle *
308 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
309                                    uint32_t priority,
310                                    struct GNUNET_TIME_Relative maxdelay,
311                                    const struct GNUNET_PeerIdentity *target,
312                                    size_t notify_size,
313                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
314                                    void *notify_cls);
315
316
317 /**
318  * Cancel the specified transmission-ready notification.
319  *
320  * @param th handle that was returned by "notify_transmit_ready".
321  */
322 void
323 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
324                                           *th);
325
326
327 /**
328  * Transition API for tunnel ctx management
329  */
330 void
331 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data);
332
333 /**
334  * Transition API for tunnel ctx management
335  */
336 void *
337 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel);
338
339
340 #if 0                           /* keep Emacsens' auto-indent happy */
341 {
342 #endif
343 #ifdef __cplusplus
344 }
345 #endif
346
347 /* ifndef GNUNET_MESH_SERVICE_H */
348 #endif
349 /* end of gnunet_mesh_service.h */