remove send on connect
[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 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  * @param stypes Application Types the client claims to offer
135  * @return handle to the mesh service 
136  *           NULL on error (in this case, init is never called)
137  */
138 struct GNUNET_MESH_Handle *
139 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
140                      void *cls,
141                      GNUNET_MESH_TunnelEndHandler cleaner,
142                      const struct GNUNET_MESH_MessageHandler *handlers, 
143                      const GNUNET_MESH_ApplicationType *stypes);
144
145 /**
146  * Get the peer on the other side of this tunnel if it is just one. Return NULL otherwise
147  * 
148  * @param tunnel the tunnel
149  * @return the peer or NULL
150  */
151 const struct GNUNET_PeerIdentity*
152 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel);
153
154
155 /**
156  * Disconnect from the mesh service.
157  *
158  * @param handle connection to mesh to disconnect
159  */
160 void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
161
162
163
164
165
166 /**
167  * Method called whenever a tunnel falls apart.
168  *
169  * @param cls closure
170  * @param peer peer identity the tunnel stopped working with
171  */
172 typedef void (*GNUNET_MESH_TunnelDisconnectHandler) (void *cls,
173                                                      const struct GNUNET_PeerIdentity *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 GNUNET_PeerIdentity *peer,
185                                                   const struct GNUNET_TRANSPORT_ATS_Information *atsi);
186
187
188
189 /**
190  * Handle for a request to the mesh to connect or disconnect
191  * from a particular peer.  Can be used to cancel the request
192  * (before the 'cont'inuation is called).
193  */
194 struct GNUNET_MESH_PeerRequestHandle;
195
196
197 /**
198  * Request that the mesh should try to connect to any of the given peers.
199  *
200  * @param h mesh handle
201  * @param timeout how long to try to establish a connection
202  * @param num_peers length of the peers array
203  * @param peers list of candidates to connect to
204  * @param connect_handler function to call on successful connect (or timeout)
205  * @param disconnect_handler function to call on disconnect
206  * @param handler_cls closure for handler
207  * @return NULL on error (handler will not be called), otherwise handle for cancellation
208  */
209 struct GNUNET_MESH_Tunnel *
210 GNUNET_MESH_peer_request_connect_any (struct GNUNET_MESH_Handle *h,
211                                       struct GNUNET_TIME_Relative timeout,
212                                       unsigned int num_peers,
213                                       const struct GNUNET_PeerIdentity *peers,
214                                       GNUNET_MESH_TunnelConnectHandler connect_handler,
215                                       GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
216                                       void *handler_cls);
217
218
219 /**
220  * Request that the mesh should try to connect to all of the given peers.
221  * Messages send to the tunnel will be broadcast.
222  *
223  * @param h mesh handle
224  * @param timeout how long to try to establish a connection
225  * @param num_peers length of the peers array
226  * @param peers list of candidates to connect to
227  * @param connect_handler function to call on successful connect (or timeout);
228  *                will be called for EACH of the peers in the list and
229  *                once at the end with 'NULL' on timeout or once we've connected
230  *                to each of the peers in the list
231  * @param disconnect_handler function called if a peer drops out of the tunnel;
232  *                the mesh will NOT try to add it back automatically
233  * @param handler_cls closure for handler
234  * @return NULL on error (handler will not be called), otherwise handle for cancellation
235  */
236 struct GNUNET_MESH_Tunnel *
237 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *h,
238                                       struct GNUNET_TIME_Relative timeout,
239                                       unsigned int num_peers,
240                                       const struct GNUNET_PeerIdentity *peers,
241                                       GNUNET_MESH_TunnelConnectHandler connect_handler,
242                                       GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
243                                       void *handler_cls);
244
245
246 /**
247  * Request that a peer should be added to the tunnel.  The existing
248  * connect handler will be called ONCE with either success or failure.
249  *
250  * @param tunnel handle to existing tunnel
251  * @param timeout how long to try to establish a connection
252  * @param peer peer to add
253  */
254 void
255 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
256                                       struct GNUNET_TIME_Relative timeout,
257                                       const struct GNUNET_PeerIdentity *peer);
258
259
260 /**
261  * Request that a peer should be removed from the tunnel.  The existing
262  * disconnect handler will be called ONCE if we were connected.
263  *
264  * @param tunnel handle to existing tunnel
265  * @param peer peer to remove
266  */
267 void
268 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
269                                       const struct GNUNET_PeerIdentity *peer);
270
271
272 /**
273  * Request that the mesh should try to connect to a peer supporting the given
274  * message type.
275  *
276  * @param h mesh handle
277  * @param timeout how long to try to establish a connection
278  * @param app_type application type that must be supported by the peer (MESH should
279  *                discover peer in proximity handling this type)
280  * @param connect_handler function to call on successful connect (or timeout);
281  *                will be called for EACH of the peers in the list and
282  *                once at the end with 'NULL' on timeout or once we've connected
283  *                to each of the peers in the list
284  * @param disconnect_handler function called if a peer drops out of the tunnel;
285  *                the mesh will NOT try to add it back automatically
286  * @param handler_cls closure for handler
287  * @return NULL on error (handler will not be called), otherwise handle for cancellation
288  */
289 struct GNUNET_MESH_Tunnel *
290 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *h,
291                                           struct GNUNET_TIME_Relative timeout,
292                                           GNUNET_MESH_ApplicationType app_type,
293                                           GNUNET_MESH_TunnelConnectHandler connect_handler,
294                                           GNUNET_MESH_TunnelDisconnectHandler disconnect_handler,
295                                           void *handler_cls);
296
297
298 /**
299  * Cancel a pending request to connect to a particular peer.  Destroys the
300  * tunnel. 
301  *
302  * @param req request handle that was returned for the original request
303  */
304 void
305 GNUNET_MESH_peer_request_connect_cancel (struct GNUNET_MESH_Tunnel *req);
306
307
308 /**
309  * Handle for a transmission request.
310  */
311 struct GNUNET_MESH_TransmitHandle;
312
313
314 /**
315  * Ask the mesh to call "notify" once it is ready to transmit the
316  * given number of bytes to the specified "target".  If we are not yet
317  * connected to the specified peer, a call to this function will cause
318  * us to try to establish a connection.
319  *
320  * @param tunnel tunnel to use for transmission
321  * @param cork is corking allowed for this transmission?
322  * @param priority how important is the message?
323  * @param maxdelay how long can the message wait?
324  * @param target destination for the message, NULL for multicast to all tunnel targets 
325  * @param notify_size how many bytes of buffer space does notify want?
326  * @param notify function to call when buffer space is available;
327  *        will be called with NULL on timeout or if the overall queue
328  *        for this peer is larger than queue_size and this is currently
329  *        the message with the lowest priority
330  * @param notify_cls closure for notify
331  * @return non-NULL if the notify callback was queued,
332  *         NULL if we can not even queue the request (insufficient
333  *         memory); if NULL is returned, "notify" will NOT be called.
334  */
335 struct GNUNET_MESH_TransmitHandle *
336 GNUNET_MESH_notify_transmit_ready (struct
337                                    GNUNET_MESH_Tunnel
338                                    *tunnel,
339                                    int cork,
340                                    uint32_t priority,
341                                    struct
342                                    GNUNET_TIME_Relative
343                                    maxdelay,
344                                    const struct GNUNET_PeerIdentity *target,
345                                    size_t
346                                    notify_size,
347                                    GNUNET_CONNECTION_TransmitReadyNotify
348                                    notify,
349                                    void
350                                    *notify_cls);
351
352
353 /**
354  * Cancel the specified transmission-ready notification.
355  *
356  * @param th handle that was returned by "notify_transmit_ready".
357  */
358 void
359 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
360                                           *th);
361
362
363 #if 0                           /* keep Emacsens' auto-indent happy */
364 {
365 #endif
366 #ifdef __cplusplus
367 }
368 #endif
369
370 /* ifndef GNUNET_MESH_SERVICE_H */
371 #endif
372 /* end of gnunet_mesh_service.h */