7e332c7498593fc77f5421f1170ac9fa1a68d75f
[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
77                                     GNUNET_TRANSPORT_ATS_Information * atsi);
78
79
80 /**
81  * Message handler.  Each struct specifies how to handle on particular
82  * type of message received.
83  */
84 struct GNUNET_MESH_MessageHandler
85 {
86   /**
87    * Function to call for messages of "type".
88    */
89   GNUNET_MESH_MessageCallback callback;
90
91   /**
92    * Type of the message this handler covers.
93    */
94   uint16_t type;
95
96   /**
97    * Expected size of messages of this type.  Use 0 for variable-size.
98    * If non-zero, messages of the given type will be discarded if they
99    * do not have the right size.
100    */
101   uint16_t expected_size;
102
103 };
104
105
106 /**
107  * Function called whenever an inbound tunnel is destroyed.  Should clean up
108  * any associated state.
109  *
110  * @param cls closure (set from GNUNET_MESH_connect)
111  * @param tunnel connection to the other end (henceforth invalid)
112  * @param tunnel_ctx place where local state associated with the tunnel is stored
113  */
114 typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
115                                              const struct GNUNET_MESH_Tunnel *
116                                              tunnel, void **tunnel_ctx);
117
118
119 /**
120  * Type for an application.  Values defined in gnunet_applications.h
121  */
122 typedef uint32_t GNUNET_MESH_ApplicationType;
123
124
125 /**
126  * Connect to the mesh service.
127  *
128  * @param cfg configuration to use
129  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
130  * @param cleaner function called when an *inbound* tunnel is destroyed
131  * @param handlers callbacks for messages we care about, NULL-terminated
132  *                note that the mesh is allowed to drop notifications about inbound
133  *                messages if the client does not process them fast enough (for this
134  *                notification type, a bounded queue is used)
135  * @param stypes Application Types the client claims to offer
136  * @return handle to the mesh service 
137  *           NULL on error (in this case, init is never called)
138  */
139 struct GNUNET_MESH_Handle *GNUNET_MESH_connect (const struct
140                                                 GNUNET_CONFIGURATION_Handle
141                                                 *cfg, void *cls,
142                                                 GNUNET_MESH_TunnelEndHandler
143                                                 cleaner,
144                                                 const struct
145                                                 GNUNET_MESH_MessageHandler
146                                                 *handlers,
147                                                 const
148                                                 GNUNET_MESH_ApplicationType
149                                                 *stypes);
150
151 /**
152  * Get the peer on the other side of this tunnel if it is just one. Return NULL otherwise
153  * 
154  * @param tunnel the tunnel
155  * @return the peer or NULL
156  */
157 const struct GNUNET_PeerIdentity *GNUNET_MESH_get_peer (const struct
158                                                         GNUNET_MESH_Tunnel
159                                                         *tunnel);
160
161
162 /**
163  * Disconnect from the mesh service.
164  *
165  * @param handle connection to mesh to disconnect
166  */
167 void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
168
169
170
171
172
173 /**
174  * Method called whenever a tunnel falls apart.
175  *
176  * @param cls closure
177  * @param peer peer identity the tunnel stopped working with
178  */
179 typedef void (*GNUNET_MESH_TunnelDisconnectHandler) (void *cls,
180                                                      const struct
181                                                      GNUNET_PeerIdentity *
182                                                      peer);
183
184
185 /**
186  * Method called whenever a tunnel is established.
187  *
188  * @param cls closure
189  * @param peer peer identity the tunnel was created to, NULL on timeout
190  * @param atsi performance data for the connection
191  */
192 typedef void (*GNUNET_MESH_TunnelConnectHandler) (void *cls,
193                                                   const struct
194                                                   GNUNET_PeerIdentity * peer,
195                                                   const struct
196                                                   GNUNET_TRANSPORT_ATS_Information
197                                                   * atsi);
198
199
200
201 /**
202  * Handle for a request to the mesh to connect or disconnect
203  * from a particular peer.  Can be used to cancel the request
204  * (before the 'cont'inuation is called).
205  */
206 struct GNUNET_MESH_PeerRequestHandle;
207
208
209 /**
210  * Request that the mesh should try to connect to any of the given peers.
211  *
212  * @param h mesh handle
213  * @param timeout how long to try to establish a connection
214  * @param num_peers length of the peers array
215  * @param peers list of candidates to connect to
216  * @param connect_handler function to call on successful connect (or timeout)
217  * @param disconnect_handler function to call on disconnect
218  * @param handler_cls closure for handler
219  * @return NULL on error (handler will not be called), otherwise handle for cancellation
220  */
221 struct GNUNET_MESH_Tunnel *GNUNET_MESH_peer_request_connect_any (struct
222                                                                  GNUNET_MESH_Handle
223                                                                  *h,
224                                                                  struct
225                                                                  GNUNET_TIME_Relative
226                                                                  timeout,
227                                                                  unsigned int
228                                                                  num_peers,
229                                                                  const struct
230                                                                  GNUNET_PeerIdentity
231                                                                  *peers,
232                                                                  GNUNET_MESH_TunnelConnectHandler
233                                                                  connect_handler,
234                                                                  GNUNET_MESH_TunnelDisconnectHandler
235                                                                  disconnect_handler,
236                                                                  void
237                                                                  *handler_cls);
238
239
240 /**
241  * Request that the mesh should try to connect to all of the given peers.
242  * Messages send to the tunnel will be broadcast.
243  *
244  * @param h mesh handle
245  * @param timeout how long to try to establish a connection
246  * @param num_peers length of the peers array
247  * @param peers list of candidates to connect to
248  * @param connect_handler function to call on successful connect (or timeout);
249  *                will be called for EACH of the peers in the list and
250  *                once at the end with 'NULL' on timeout or once we've connected
251  *                to each of the peers in the list
252  * @param disconnect_handler function called if a peer drops out of the tunnel;
253  *                the mesh will NOT try to add it back automatically
254  * @param handler_cls closure for handler
255  * @return NULL on error (handler will not be called), otherwise handle for cancellation
256  */
257 struct GNUNET_MESH_Tunnel *GNUNET_MESH_peer_request_connect_all (struct
258                                                                  GNUNET_MESH_Handle
259                                                                  *h,
260                                                                  struct
261                                                                  GNUNET_TIME_Relative
262                                                                  timeout,
263                                                                  unsigned int
264                                                                  num_peers,
265                                                                  const struct
266                                                                  GNUNET_PeerIdentity
267                                                                  *peers,
268                                                                  GNUNET_MESH_TunnelConnectHandler
269                                                                  connect_handler,
270                                                                  GNUNET_MESH_TunnelDisconnectHandler
271                                                                  disconnect_handler,
272                                                                  void
273                                                                  *handler_cls);
274
275
276 /**
277  * Request that a peer should be added to the tunnel.  The existing
278  * connect handler will be called ONCE with either success or failure.
279  *
280  * @param tunnel handle to existing tunnel
281  * @param timeout how long to try to establish a connection
282  * @param peer peer to add
283  */
284 void
285 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
286                                       struct GNUNET_TIME_Relative timeout,
287                                       const struct GNUNET_PeerIdentity *peer);
288
289
290 /**
291  * Request that a peer should be removed from the tunnel.  The existing
292  * disconnect handler will be called ONCE if we were connected.
293  *
294  * @param tunnel handle to existing tunnel
295  * @param peer peer to remove
296  */
297 void
298 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
299                                       const struct GNUNET_PeerIdentity *peer);
300
301
302 /**
303  * Request that the mesh should try to connect to a peer supporting the given
304  * message type.
305  *
306  * @param h mesh handle
307  * @param timeout how long to try to establish a connection
308  * @param app_type application type that must be supported by the peer (MESH should
309  *                discover peer in proximity handling this type)
310  * @param connect_handler function to call on successful connect (or timeout);
311  *                will be called for EACH of the peers in the list and
312  *                once at the end with 'NULL' on timeout or once we've connected
313  *                to each of the peers in the list
314  * @param disconnect_handler function called if a peer drops out of the tunnel;
315  *                the mesh will NOT try to add it back automatically
316  * @param handler_cls closure for handler
317  * @return NULL on error (handler will not be called), otherwise handle for cancellation
318  */
319 struct GNUNET_MESH_Tunnel *GNUNET_MESH_peer_request_connect_by_type (struct
320                                                                      GNUNET_MESH_Handle
321                                                                      *h,
322                                                                      struct
323                                                                      GNUNET_TIME_Relative
324                                                                      timeout,
325                                                                      GNUNET_MESH_ApplicationType
326                                                                      app_type,
327                                                                      GNUNET_MESH_TunnelConnectHandler
328                                                                      connect_handler,
329                                                                      GNUNET_MESH_TunnelDisconnectHandler
330                                                                      disconnect_handler,
331                                                                      void
332                                                                      *handler_cls);
333
334
335 /**
336  * Cancel a pending request to connect to a particular peer.  Destroys the
337  * tunnel. 
338  *
339  * @param req request handle that was returned for the original request
340  */
341 void GNUNET_MESH_peer_request_connect_cancel (struct GNUNET_MESH_Tunnel *req);
342
343
344 /**
345  * Handle for a transmission request.
346  */
347 struct GNUNET_MESH_TransmitHandle;
348
349
350 /**
351  * Ask the mesh to call "notify" once it is ready to transmit the
352  * given number of bytes to the specified "target".  If we are not yet
353  * connected to the specified peer, a call to this function will cause
354  * us to try to establish a connection.
355  *
356  * @param tunnel tunnel to use for transmission
357  * @param cork is corking allowed for this transmission?
358  * @param priority how important is the message?
359  * @param maxdelay how long can the message wait?
360  * @param target destination for the message, NULL for multicast to all tunnel targets 
361  * @param notify_size how many bytes of buffer space does notify want?
362  * @param notify function to call when buffer space is available;
363  *        will be called with NULL on timeout or if the overall queue
364  *        for this peer is larger than queue_size and this is currently
365  *        the message with the lowest priority
366  * @param notify_cls closure for notify
367  * @return non-NULL if the notify callback was queued,
368  *         NULL if we can not even queue the request (insufficient
369  *         memory); if NULL is returned, "notify" will NOT be called.
370  */
371 struct GNUNET_MESH_TransmitHandle *GNUNET_MESH_notify_transmit_ready (struct
372                                                                       GNUNET_MESH_Tunnel
373                                                                       *tunnel,
374                                                                       int cork,
375                                                                       uint32_t
376                                                                       priority,
377                                                                       struct
378                                                                       GNUNET_TIME_Relative
379                                                                       maxdelay,
380                                                                       const
381                                                                       struct
382                                                                       GNUNET_PeerIdentity
383                                                                       *target,
384                                                                       size_t
385                                                                       notify_size,
386                                                                       GNUNET_CONNECTION_TransmitReadyNotify
387                                                                       notify,
388                                                                       void
389                                                                       *notify_cls);
390
391
392 /**
393  * Cancel the specified transmission-ready notification.
394  *
395  * @param th handle that was returned by "notify_transmit_ready".
396  */
397 void
398 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
399                                           *th);
400
401 void GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel,
402                                   void *head);
403 void GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel,
404                                   void *tail);
405 void *GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel);
406 void *GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel);
407
408 void GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel,
409                                   void *data);
410 void *GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel);
411
412 #if 0                           /* keep Emacsens' auto-indent happy */
413 {
414 #endif
415 #ifdef __cplusplus
416 }
417 #endif
418
419 /* ifndef GNUNET_MESH_SERVICE_H */
420 #endif
421 /* end of gnunet_mesh_service.h */