- distribute peers equally among island nodes on SuperMUC
[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.
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 GNUNET_ATS_Information
78                                             * atsi);
79
80
81 /**
82  * Message handler.  Each struct specifies how to handle on particular
83  * type of message received.
84  */
85 struct GNUNET_MESH_MessageHandler
86 {
87   /**
88    * Function to call for messages of "type".
89    */
90   GNUNET_MESH_MessageCallback callback;
91
92   /**
93    * Type of the message this handler covers.
94    */
95   uint16_t type;
96
97   /**
98    * Expected size of messages of this type.  Use 0 for variable-size.
99    * If non-zero, messages of the given type will be discarded if they
100    * do not have the right size.
101    */
102   uint16_t expected_size;
103
104 };
105
106
107 /**
108  * Method called whenever another peer has added us to a tunnel
109  * the other peer initiated.
110  * Only called (once) upon reception of data with a message type which was
111  * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
112  * causes te tunnel to be ignored and no further notifications are sent about
113  * the same tunnel.
114  *
115  * @param cls closure
116  * @param tunnel new handle to the tunnel
117  * @param initiator peer that started the tunnel
118  * @param atsi performance information for the tunnel
119  * @return initial tunnel context for the tunnel
120  *         (can be NULL -- that's not an error)
121  */
122 typedef void *(GNUNET_MESH_InboundTunnelNotificationHandler) (void *cls,
123                                                               struct
124                                                               GNUNET_MESH_Tunnel
125                                                               * tunnel,
126                                                               const struct
127                                                               GNUNET_PeerIdentity
128                                                               * initiator,
129                                                               const struct
130                                                               GNUNET_ATS_Information
131                                                               * atsi);
132
133
134 /**
135  * Function called whenever an inbound tunnel is destroyed.  Should clean up
136  * any associated state.  This function is NOT called if the client has
137  * explicitly asked for the tunnel to be destroyed using
138  * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
139  * the tunnel.
140  *
141  * @param cls closure (set from GNUNET_MESH_connect)
142  * @param tunnel connection to the other end (henceforth invalid)
143  * @param tunnel_ctx place where local state associated
144  *                   with the tunnel is stored
145  */
146 typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
147                                              const struct GNUNET_MESH_Tunnel *
148                                              tunnel, void *tunnel_ctx);
149
150
151 /**
152  * Type for an application.  Values defined in gnunet_applications.h
153  * DEPRECATED - not present in mesh2
154  */
155 typedef uint32_t GNUNET_MESH_ApplicationType;
156
157
158 /**
159  * Connect to the mesh service.
160  *
161  * @param cfg configuration to use
162  * @param cls closure for the various callbacks that follow
163  *            (including handlers in the handlers array)
164  * @param new_tunnel function called when an *inbound* tunnel is created
165  * @param cleaner function called when an *inbound* tunnel is destroyed by the
166  *                remote peer, it is *not* called if GNUNET_MESH_tunnel_destroy
167  *                is called on the tunnel
168  * @param handlers callbacks for messages we care about, NULL-terminated
169  *                note that the mesh is allowed to drop notifications about
170  *                inbound messages if the client does not process them fast
171  *                enough (for this notification type, a bounded queue is used)
172  * @param stypes list of the applications that this client claims to provide
173  * @return handle to the mesh service NULL on error
174  *         (in this case, init is never called)
175  */
176 struct GNUNET_MESH_Handle *
177 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
178                      GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
179                      GNUNET_MESH_TunnelEndHandler cleaner,
180                      const struct GNUNET_MESH_MessageHandler *handlers,
181                      const GNUNET_MESH_ApplicationType *stypes);
182
183
184 /**
185  * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
186  * disconnect callbacks will be called on any still connected peers, notifying
187  * about their disconnection. The registered inbound tunnel cleaner will be
188  * called should any inbound tunnels still exist.
189  *
190  * @param handle connection to mesh to disconnect
191  */
192 void
193 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
194
195
196 /**
197  * Method called whenever a peer has disconnected from the tunnel.
198  * Implementations of this callback must NOT call
199  * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
200  * to run in some other task later.  However, calling 
201  * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
202  *
203  * @param cls closure
204  * @param peer peer identity the tunnel stopped working with
205  */
206 typedef void (*GNUNET_MESH_PeerDisconnectHandler) (void *cls,
207                                                    const struct
208                                                    GNUNET_PeerIdentity * peer);
209
210
211 /**
212  * Method called whenever a peer has connected to the tunnel.
213  *
214  * @param cls closure
215  * @param peer peer identity the tunnel was created to, NULL on timeout
216  * @param atsi performance data for the connection
217  *
218  * TODO: change to return int to let client allow the new peer or not?
219  */
220 typedef void (*GNUNET_MESH_PeerConnectHandler) (void *cls,
221                                                 const struct GNUNET_PeerIdentity
222                                                 * peer,
223                                                 const struct
224                                                 GNUNET_ATS_Information * atsi);
225
226
227 /**
228  * Announce to ther peer the availability of services described by the regex,
229  * in order to be reachable to other peers via connect_by_string.
230  * 
231  * Note that the first GNUNET_REGEX_INITIAL_BYTES characters are considered
232  * to be part of a prefix, (for instance 'gnunet://').
233  * If you put a variable part in there (*, +. ()), all matching strings
234  * will be stored in the DHT.
235  *
236  * @param h Handle to mesh.
237  * @param regex String with the regular expression describing local services.
238  * @param compression_characters How many characters can be assigned to one
239  *                               edge of the graph. The bigger the variability
240  *                               of the data, the smaller this parameter should
241  *                               be (down to 1).
242  *                               For maximum compression, use strlen (regex)
243  *                               or 0 (special value). Use with care!
244  */
245 void
246 GNUNET_MESH_announce_regex (struct GNUNET_MESH_Handle *h,
247                             const char *regex,
248                             unsigned int compression_characters);
249
250
251 /**
252  * Create a new tunnel (we're initiator and will be allowed to add/remove peers
253  * and to broadcast).
254  *
255  * @param h mesh handle
256  * @param tunnel_ctx client's tunnel context to associate with the tunnel
257  * @param connect_handler function to call when peers are actually connected
258  * @param disconnect_handler function to call when peers are disconnected
259  * @param handler_cls closure for connect/disconnect handlers
260  */
261 struct GNUNET_MESH_Tunnel *
262 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, void *tunnel_ctx,
263                            GNUNET_MESH_PeerConnectHandler connect_handler,
264                            GNUNET_MESH_PeerDisconnectHandler disconnect_handler,
265                            void *handler_cls);
266
267 /**
268  * Destroy an existing tunnel. The existing callback for the tunnel will NOT
269  * be called.
270  *
271  * @param tunnel tunnel handle
272  */
273 void
274 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel);
275
276
277 /**
278  * Request that the tunnel data rate is limited to the speed of the slowest
279  * receiver.
280  * 
281  * @param tunnel Tunnel affected.
282  */
283 void
284 GNUNET_MESH_tunnel_speed_min (struct GNUNET_MESH_Tunnel *tunnel);
285
286
287 /**
288  * Request that the tunnel data rate is limited to the speed of the fastest
289  * receiver. This is the default behavior.
290  * 
291  * @param tunnel Tunnel affected.
292  */
293 void
294 GNUNET_MESH_tunnel_speed_max (struct GNUNET_MESH_Tunnel *tunnel);
295
296
297 /**
298  * Turn on/off the buffering status of the tunnel.
299  * 
300  * @param tunnel Tunnel affected.
301  * @param buffer GNUNET_YES to turn buffering on (default),
302  *               GNUNET_NO otherwise.
303  */
304 void
305 GNUNET_MESH_tunnel_buffer (struct GNUNET_MESH_Tunnel *tunnel, int buffer);
306
307
308 /**
309  * Request that a peer should be added to the tunnel.  The connect handler
310  * will be called when the peer connects
311  *
312  * @param tunnel handle to existing tunnel
313  * @param peer peer to add
314  */
315 void
316 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
317                                       const struct GNUNET_PeerIdentity *peer);
318
319
320 /**
321  * Request that a peer should be removed from the tunnel.  The existing
322  * disconnect handler will be called ONCE if we were connected.
323  *
324  * @param tunnel handle to existing tunnel
325  * @param peer peer to remove
326  */
327 void
328 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
329                                       const struct GNUNET_PeerIdentity *peer);
330
331
332 /**
333  * Request that the mesh should try to connect to a peer supporting the given
334  * message type.
335  *
336  * @param tunnel handle to existing tunnel
337  * @param app_type application type that must be supported by the peer
338  *                 (MESH should discover peer in proximity handling this type)
339  */
340 void
341 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
342                                           GNUNET_MESH_ApplicationType app_type);
343
344
345 /**
346  * Request that the mesh should try to connect to a peer matching the
347  * description given in the service string.
348  *
349  * @param tunnel handle to existing tunnel
350  * @param description string describing the destination node requirements
351  */
352 void
353 GNUNET_MESH_peer_request_connect_by_string (struct GNUNET_MESH_Tunnel *tunnel,
354                                             const char *description);
355
356
357 /**
358  * Request that the given peer isn't added to this tunnel in calls to
359  * connect_by_* calls, (due to misbehaviour, bad performance, ...).
360  *
361  * @param tunnel handle to existing tunnel.
362  * @param peer peer identity of the peer which should be blacklisted
363  *                  for the tunnel.
364  */
365 void
366 GNUNET_MESH_peer_blacklist (struct GNUNET_MESH_Tunnel *tunnel,
367                             const struct GNUNET_PeerIdentity *peer);
368
369
370 /**
371  * Request that the given peer isn't blacklisted anymore from this tunnel,
372  * and therefore can be added in future calls to connect_by_*.
373  * The peer must have been previously blacklisted for this tunnel.
374  *
375  * @param tunnel handle to existing tunnel.
376  * @param peer peer identity of the peer which shouldn't be blacklisted
377  *                  for the tunnel anymore.
378  */
379 void
380 GNUNET_MESH_peer_unblacklist (struct GNUNET_MESH_Tunnel *tunnel,
381                               const struct GNUNET_PeerIdentity *peer);
382
383
384 /**
385  * Handle for a transmission request.
386  */
387 struct GNUNET_MESH_TransmitHandle;
388
389
390 /**
391  * Ask the mesh to call "notify" once it is ready to transmit the
392  * given number of bytes to the specified tunnel or target.
393  * Only one call can be active at any time, to issue another request,
394  * wait for the callback or cancel the current request.
395  *
396  * @param tunnel tunnel to use for transmission
397  * @param cork is corking allowed for this transmission?
398  * @param maxdelay how long can the message wait?
399  * @param target destination for the message
400  *               NULL for multicast to all tunnel targets
401  * @param notify_size how many bytes of buffer space does notify want?
402  * @param notify function to call when buffer space is available;
403  *        will be called with NULL on timeout or if the overall queue
404  *        for this peer is larger than queue_size and this is currently
405  *        the message with the lowest priority
406  * @param notify_cls closure for notify
407  * @return non-NULL if the notify callback was queued,
408  *         NULL if we can not even queue the request (insufficient
409  *         memory); if NULL is returned, "notify" will NOT be called.
410  */
411 struct GNUNET_MESH_TransmitHandle *
412 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
413                                    struct GNUNET_TIME_Relative maxdelay,
414                                    const struct GNUNET_PeerIdentity *target,
415                                    size_t notify_size,
416                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
417                                    void *notify_cls);
418
419
420 /**
421  * Cancel the specified transmission-ready notification.
422  *
423  * @param th handle that was returned by "notify_transmit_ready".
424  */
425 void
426 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
427                                           *th);
428
429
430 /**
431  * Method called to retrieve information about each tunnel the mesh peer
432  * is aware of.
433  *
434  * @param cls Closure.
435  * @param initiator Peer that started the tunnel (owner).
436  * @param tunnel_number Tunnel number.
437  * @param peers Array of peer identities that participate in the tunnel.
438  * @param npeers Number of peers in peers.
439  */
440 typedef void (*GNUNET_MESH_TunnelsCB) (void *cls,
441                                        const struct GNUNET_PeerIdentity *owner,
442                                        unsigned int tunnel_number,
443                                        const struct GNUNET_PeerIdentity *peers,
444                                        unsigned int npeers);
445
446
447 /**
448  * Method called to retrieve information about a specific tunnel the mesh peer
449  * is aware of, including all transit nodes.
450  *
451  * @param cls Closure.
452  * @param peer Peer in the tunnel's tree.
453  * @param parent Parent of the current peer. All 0 when peer is root.
454  */
455 typedef void (*GNUNET_MESH_TunnelCB) (void *cls,
456                                       const struct GNUNET_PeerIdentity *peer,
457                                       const struct GNUNET_PeerIdentity *parent);
458
459
460 /**
461  * Request information about the running mesh peer.
462  * The callback will be called for every tunnel known to the service,
463  * listing all active peers that blong to the tunnel.
464  *
465  * If called again on the same handle, it will overwrite the previous
466  * callback and cls. To retrieve the cls, monitor_cancel must be
467  * called first.
468  *
469  * WARNING: unstable API, likely to change in the future!
470  *
471  * @param h Handle to the mesh peer.
472  * @param callback Function to call with the requested data.
473  * @param callback_cls Closure for @c callback.
474  */
475 void
476 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
477                          GNUNET_MESH_TunnelsCB callback,
478                          void *callback_cls);
479
480
481 /**
482  * Request information about a specific tunnel of the running mesh peer.
483  *
484  * WARNING: unstable API, likely to change in the future!
485  *
486  * @param h Handle to the mesh peer.
487  * @param initiator ID of the owner of the tunnel.
488  * @param tunnel_number Tunnel number.
489  * @param callback Function to call with the requested data.
490  * @param callback_cls Closure for @c callback.
491  */
492 void
493 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
494                          struct GNUNET_PeerIdentity *initiator,
495                          unsigned int tunnel_number,
496                          GNUNET_MESH_TunnelCB callback,
497                          void *callback_cls);
498
499
500 /**
501  * Cancel a monitor request. The monitor callback will not be called.
502  *
503  * WARNING: unstable API, likely to change in the future!
504  *
505  * @param h Mesh handle.
506  *
507  * @return Closure given to GNUNET_MESH_monitor, if any.
508  */
509 void *
510 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h);
511
512
513 /**
514  * Transition API for tunnel ctx management
515  * 
516  * FIXME deprecated
517  */
518 void
519 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data);
520
521 /**
522  * Transition API for tunnel ctx management
523  * 
524  * FIXME deprecated
525  */
526 void *
527 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel);
528
529
530 #if 0                           /* keep Emacsens' auto-indent happy */
531 {
532 #endif
533 #ifdef __cplusplus
534 }
535 #endif
536
537 /* ifndef GNUNET_MESH_SERVICE_H */
538 #endif
539 /* end of gnunet_mesh_service.h */