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