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