89b1c881ee5b01f7642bcd5dd5df964af9d8fe0d
[oweals/gnunet.git] / src / include / gnunet_mesh_service_new.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 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
78                                             GNUNET_TRANSPORT_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  * Function called whenever an inbound tunnel is destroyed.  Should clean up
110  * any associated state.
111  *
112  * @param cls closure (set from GNUNET_MESH_connect)
113  * @param tunnel connection to the other end (henceforth invalid)
114  * @param tunnel_ctx place where local state associated
115  *                   with the tunnel is stored
116  */
117 typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
118                                              const struct GNUNET_MESH_Tunnel *
119                                              tunnel, void **tunnel_ctx);
120
121
122 /**
123  * Type for an application.  Values defined in gnunet_applications.h
124  */
125 typedef uint32_t GNUNET_MESH_ApplicationType;
126
127
128 /**
129  * Connect to the mesh service.
130  *
131  * @param cfg configuration to use
132  * @param cls closure for the various callbacks that follow
133  *            (including handlers in the handlers array)
134  * @param cleaner function called when an *inbound* tunnel is destroyed
135  * @param handlers callbacks for messages we care about, NULL-terminated
136  *                note that the mesh is allowed to drop notifications about
137  *                inbound messages if the client does not process them fast
138  *                enough (for this notification type, a bounded queue is used)
139  * @param stypes list of the applications that this client claims to provide
140  * @return handle to the mesh service NULL on error
141  *         (in this case, init is never called)
142  */
143 struct GNUNET_MESH_Handle *GNUNET_MESH_connect (const struct
144                                                 GNUNET_CONFIGURATION_Handle
145                                                 *cfg, void *cls,
146                                                 GNUNET_MESH_TunnelEndHandler
147                                                 cleaner,
148                                                 const struct
149                                                 GNUNET_MESH_MessageHandler
150                                                 *handlers,
151                                                 const
152                                                 GNUNET_MESH_ApplicationType
153                                                 *stypes);
154
155
156 /**
157  * Disconnect from the mesh service.
158  *
159  * @param handle connection to mesh to disconnect
160  */
161 void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
162
163
164 /**
165  * Method called whenever a tunnel falls apart.
166  *
167  * @param cls closure
168  * @param peer peer identity the tunnel stopped working with
169  */
170 typedef void (*GNUNET_MESH_TunnelDisconnectHandler) (void *cls,
171                                                      const struct
172                                                      GNUNET_PeerIdentity *
173                                                      peer);
174
175
176 /**
177  * Method called whenever a tunnel is established.
178  *
179  * @param cls closure
180  * @param peer peer identity the tunnel was created to, NULL on timeout
181  * @param atsi performance data for the connection
182  */
183 typedef void (*GNUNET_MESH_TunnelConnectHandler) (void *cls,
184                                                   const struct
185                                                   GNUNET_PeerIdentity * peer,
186                                                   const struct
187                                                   GNUNET_TRANSPORT_ATS_Information
188                                                   * atsi);
189
190
191
192 /**
193  * Create a new tunnel (we're initiator and will be allowed to add/remove peers
194  * and to broadcast).
195  *
196  * @param h mesh handle
197  * @param connect_handler function to call when peers are actually connected
198  * @param disconnect_handler function to call when peers are disconnected
199  * @param handler_cls closure for connect/disconnect handlers
200  */
201 struct GNUNET_MESH_Tunnel *GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle
202                                                       *h,
203                                                       GNUNET_MESH_TunnelConnectHandler
204                                                       connect_handler,
205                                                       GNUNET_MESH_TunnelDisconnectHandler
206                                                       disconnect_handler,
207                                                       void *handler_cls);
208
209 /**
210  * Destroy an existing tunnel.
211  *
212  * @param tun tunnel handle
213  */
214 void GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tun);
215
216
217 /**
218  * Request that a peer should be added to the tunnel.  The existing
219  * connect handler will be called ONCE with either success or failure.
220  *
221  * @param tunnel handle to existing tunnel
222  * @param timeout how long to try to establish a connection
223  * @param peer peer to add
224  */
225 void GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
226                                            struct GNUNET_TIME_Relative timeout,
227                                            const struct GNUNET_PeerIdentity
228                                            *peer);
229
230
231 /**
232  * Request that a peer should be removed from the tunnel.  The existing
233  * disconnect handler will be called ONCE if we were connected.
234  *
235  * @param tunnel handle to existing tunnel
236  * @param peer peer to remove
237  */
238 void GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
239                                            const struct GNUNET_PeerIdentity
240                                            *peer);
241
242
243 /**
244  * Request that the mesh should try to connect to a peer supporting the given
245  * message type.
246  *
247  * @param tunnel handle to existing tunnel
248  * @param timeout how long to try to establish a connection
249  * @param app_type application type that must be supported by the peer
250  *                 (MESH should discover peer in proximity handling this type)
251  */
252 void GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel
253                                                *tunnel,
254                                                struct GNUNET_TIME_Relative
255                                                timeout,
256                                                GNUNET_MESH_ApplicationType
257                                                app_type);
258
259
260 /**
261  * Handle for a transmission request.
262  */
263 struct GNUNET_MESH_TransmitHandle;
264
265
266 /**
267  * Ask the mesh to call "notify" once it is ready to transmit the
268  * given number of bytes to the specified tunnel or target. 
269  *
270  * @param tunnel tunnel to use for transmission
271  * @param cork is corking allowed for this transmission?
272  * @param priority how important is the message?
273  * @param maxdelay how long can the message wait?
274  * @param target destination for the message
275  *               NULL for multicast to all tunnel targets 
276  * @param notify_size how many bytes of buffer space does notify want?
277  * @param notify function to call when buffer space is available;
278  *        will be called with NULL on timeout or if the overall queue
279  *        for this peer is larger than queue_size and this is currently
280  *        the message with the lowest priority
281  * @param notify_cls closure for notify
282  * @return non-NULL if the notify callback was queued,
283  *         NULL if we can not even queue the request (insufficient
284  *         memory); if NULL is returned, "notify" will NOT be called.
285  */
286 struct GNUNET_MESH_TransmitHandle *GNUNET_MESH_notify_transmit_ready (struct
287                                                                       GNUNET_MESH_Tunnel
288                                                                       *tunnel,
289                                                                       int cork,
290                                                                       uint32_t
291                                                                       priority,
292                                                                       struct
293                                                                       GNUNET_TIME_Relative
294                                                                       maxdelay,
295                                                                       const
296                                                                       struct
297                                                                       GNUNET_PeerIdentity
298                                                                       *target,
299                                                                       size_t
300                                                                       notify_size,
301                                                                       GNUNET_CONNECTION_TransmitReadyNotify
302                                                                       notify,
303                                                                       void
304                                                                       *notify_cls);
305
306
307 /**
308  * Cancel the specified transmission-ready notification.
309  *
310  * @param th handle that was returned by "notify_transmit_ready".
311  */
312 void GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
313                                                *th);
314
315
316 #if 0                           /* keep Emacsens' auto-indent happy */
317 {
318 #endif
319 #ifdef __cplusplus
320 }
321 #endif
322
323 /* ifndef GNUNET_MESH_SERVICE_H */
324 #endif
325 /* end of gnunet_mesh_service.h */