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