- verboser log, faster start
[oweals/gnunet.git] / src / include / gnunet_mesh2_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011, 2012, 2013 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 0x00000001
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 /**
59  * Functions with this signature are called whenever a message is
60  * received.
61  *
62  * @param cls closure (set from GNUNET_MESH_connect)
63  * @param tunnel connection to the other end
64  * @param tunnel_ctx place to store local state associated with the tunnel
65  * @param sender who sent the message
66  * @param message the actual message
67  * @param atsi performance data for the connection
68  * @return GNUNET_OK to keep the connection open,
69  *         GNUNET_SYSERR to close it (signal serious error)
70  */
71 typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
72                                             struct GNUNET_MESH_Tunnel * tunnel,
73                                             void **tunnel_ctx,
74                                             const struct GNUNET_PeerIdentity *
75                                             sender,
76                                             const struct GNUNET_MessageHeader *
77                                             message,
78                                             const struct GNUNET_ATS_Information
79                                             * atsi);
80
81
82 /**
83  * Message handler.  Each struct specifies how to handle on particular
84  * type of message received.
85  */
86 struct GNUNET_MESH_MessageHandler
87 {
88   /**
89    * Function to call for messages of "type".
90    */
91   GNUNET_MESH_MessageCallback callback;
92
93   /**
94    * Type of the message this handler covers.
95    */
96   uint16_t type;
97
98   /**
99    * Expected size of messages of this type.  Use 0 for variable-size.
100    * If non-zero, messages of the given type will be discarded if they
101    * do not have the right size.
102    */
103   uint16_t expected_size;
104
105 };
106
107
108 /**
109  * Method called whenever another peer has added us to a tunnel
110  * the other peer initiated.
111  * Only called (once) upon reception of data with a message type which was
112  * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
113  * causes te tunnel to be ignored and no further notifications are sent about
114  * the same tunnel.
115  *
116  * @param cls closure
117  * @param tunnel new handle to the tunnel
118  * @param initiator peer that started the tunnel
119  * @param atsi performance information for the tunnel
120  * @return initial tunnel context for the tunnel
121  *         (can be NULL -- that's not an error)
122  */
123 typedef void *(GNUNET_MESH_InboundTunnelNotificationHandler) (void *cls,
124                                                               struct
125                                                               GNUNET_MESH_Tunnel
126                                                               * tunnel,
127                                                               const struct
128                                                               GNUNET_PeerIdentity
129                                                               * initiator,
130                                                               const struct
131                                                               GNUNET_ATS_Information
132                                                               * atsi);
133
134
135 /**
136  * Function called whenever an inbound tunnel is destroyed.  Should clean up
137  * any associated state.  This function is NOT called if the client has
138  * explicitly asked for the tunnel to be destroyed using
139  * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
140  * the tunnel.
141  *
142  * @param cls closure (set from GNUNET_MESH_connect)
143  * @param tunnel connection to the other end (henceforth invalid)
144  * @param tunnel_ctx place where local state associated
145  *                   with the tunnel is stored
146  */
147 typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
148                                              const struct GNUNET_MESH_Tunnel *
149                                              tunnel, void *tunnel_ctx);
150
151
152 /**
153  * Connect to the mesh service.
154  *
155  * @param cfg configuration to use
156  * @param cls closure for the various callbacks that follow
157  *            (including handlers in the handlers array)
158  * @param new_tunnel function called when an *inbound* tunnel is created
159  * @param cleaner function called when an *inbound* tunnel is destroyed by the
160  *                remote peer, it is *not* called if GNUNET_MESH_tunnel_destroy
161  *                is called on the tunnel
162  * @param handlers callbacks for messages we care about, NULL-terminated
163  *                note that the mesh is allowed to drop notifications about
164  *                inbound messages if the client does not process them fast
165  *                enough (for this notification type, a bounded queue is used)
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, void *cls,
171                      GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
172                      GNUNET_MESH_TunnelEndHandler cleaner,
173                      const struct GNUNET_MESH_MessageHandler *handlers);
174
175
176 /**
177  * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
178  * disconnect callbacks will be called on any still connected peers, notifying
179  * about their disconnection. The registered inbound tunnel cleaner will be
180  * called should any inbound tunnels still exist.
181  *
182  * @param handle connection to mesh to disconnect
183  */
184 void
185 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
186
187
188 /**
189  * Create a new tunnel (we're initiator and will be allowed to add/remove peers
190  * and to broadcast).
191  *
192  * @param h mesh handle
193  * @param tunnel_ctx client's tunnel context to associate with the tunnel
194  * @param peer peer identity the tunnel should go to
195  * @return handle to the tunnel
196  */
197 struct GNUNET_MESH_Tunnel *
198 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, 
199                            void *tunnel_ctx,
200                            const struct GNUNET_PeerIdentity *peer);
201
202
203 /**
204  * Destroy an existing tunnel. The existing callback for the tunnel will NOT
205  * be called.
206  *
207  * @param tunnel tunnel handle
208  */
209 void
210 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel);
211
212
213 /**
214  * Turn on/off the buffering status of the tunnel.
215  * 
216  * @param tunnel Tunnel affected.
217  * @param buffer GNUNET_YES to turn buffering on (default),
218  *               GNUNET_NO otherwise.
219  */
220 void
221 GNUNET_MESH_tunnel_buffer (struct GNUNET_MESH_Tunnel *tunnel, int buffer);
222
223
224 /**
225  * Handle for a transmission request.
226  */
227 struct GNUNET_MESH_TransmitHandle;
228
229
230 /**
231  * Ask the mesh to call "notify" once it is ready to transmit the
232  * given number of bytes to the specified tunnel.
233  * Only one call can be active at any time, to issue another request,
234  * wait for the callback or cancel the current request.
235  *
236  * @param tunnel tunnel to use for transmission
237  * @param cork is corking allowed for this transmission?
238  * @param maxdelay how long can the message wait?
239  * @param notify_size how many bytes of buffer space does notify want?
240  * @param notify function to call when buffer space is available;
241  *        will be called with NULL on timeout or if the overall queue
242  *        for this peer is larger than queue_size and this is currently
243  *        the message with the lowest priority
244  * @param notify_cls closure for notify
245  * @return non-NULL if the notify callback was queued,
246  *         NULL if we can not even queue the request (insufficient
247  *         memory); if NULL is returned, "notify" will NOT be called.
248  */
249 struct GNUNET_MESH_TransmitHandle *
250 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
251                                    struct GNUNET_TIME_Relative maxdelay,
252                                    size_t notify_size,
253                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
254                                    void *notify_cls);
255
256
257 /**
258  * Cancel the specified transmission-ready notification.
259  *
260  * @param th handle that was returned by "notify_transmit_ready".
261  */
262 void
263 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
264                                           *th);
265
266
267 /**
268  * Method called to retrieve information about each tunnel the mesh peer
269  * is aware of.
270  *
271  * @param cls Closure.
272  * @param tunnel_number Tunnel number.
273  * @param origin that started the tunnel (owner).
274  * @param target other endpoint of the tunnel
275  */
276 typedef void (*GNUNET_MESH_TunnelsCB) (void *cls,
277                                        uint32_t tunnel_number,
278                                        const struct GNUNET_PeerIdentity *origin,
279                                        const struct GNUNET_PeerIdentity *target);
280
281
282 /**
283  * Method called to retrieve information about a specific tunnel the mesh peer
284  * is aware of, including all transit nodes.
285  *
286  * @param cls Closure.
287  * @param peer Peer in the tunnel's tree.
288  * @param parent Parent of the current peer. All 0 when peer is root.
289  */
290 typedef void (*GNUNET_MESH_TunnelCB) (void *cls,
291                                       const struct GNUNET_PeerIdentity *peer,
292                                       const struct GNUNET_PeerIdentity *parent);
293
294
295 /**
296  * Request information about the running mesh peer.
297  * The callback will be called for every tunnel known to the service,
298  * listing all active peers that blong to the tunnel.
299  *
300  * If called again on the same handle, it will overwrite the previous
301  * callback and cls. To retrieve the cls, monitor_cancel must be
302  * called first.
303  *
304  * WARNING: unstable API, likely to change in the future!
305  *
306  * @param h Handle to the mesh peer.
307  * @param callback Function to call with the requested data.
308  * @param callback_cls Closure for @c callback.
309  */
310 void
311 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
312                          GNUNET_MESH_TunnelsCB callback,
313                          void *callback_cls);
314
315
316 /**
317  * Request information about a specific tunnel of the running mesh peer.
318  *
319  * WARNING: unstable API, likely to change in the future!
320  *
321  * @param h Handle to the mesh peer.
322  * @param initiator ID of the owner of the tunnel.
323  * @param tunnel_number Tunnel number.
324  * @param callback Function to call with the requested data.
325  * @param callback_cls Closure for @c callback.
326  */
327 void
328 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
329                          struct GNUNET_PeerIdentity *initiator,
330                          uint32_t tunnel_number,
331                          GNUNET_MESH_TunnelCB callback,
332                          void *callback_cls);
333
334
335 /**
336  * Cancel a monitor request. The monitor callback will not be called.
337  *
338  * WARNING: unstable API, likely to change in the future!
339  *
340  * @param h Mesh handle.
341  *
342  * @return Closure given to GNUNET_MESH_monitor, if any.
343  */
344 void *
345 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h);
346
347
348 #if 0                           /* keep Emacsens' auto-indent happy */
349 {
350 #endif
351 #ifdef __cplusplus
352 }
353 #endif
354
355 /* ifndef GNUNET_MESH_SERVICE_H */
356 #endif
357 /* end of gnunet_mesh_service.h */