Exponential backoff for find finger trail task
[oweals/gnunet.git] / src / include / gnunet_transport_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009-2014 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_transport_service.h
23  * @brief low-level P2P IO
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_TRANSPORT_SERVICE_H
28 #define GNUNET_TRANSPORT_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_ats_service.h"
40
41 /**
42  * Version number of the transport API.
43  */
44 #define GNUNET_TRANSPORT_VERSION 0x00000001
45
46
47 /**
48  * Function called by the transport for each received message.
49  *
50  * @param cls closure
51  * @param peer (claimed) identity of the other peer
52  * @param message the message
53  */
54 typedef void
55 (*GNUNET_TRANSPORT_ReceiveCallback) (void *cls,
56                                      const struct GNUNET_PeerIdentity *peer,
57                                      const struct GNUNET_MessageHeader *message);
58
59
60 /**
61  * Opaque handle to the service.
62  */
63 struct GNUNET_TRANSPORT_Handle;
64
65
66 /**
67  * Function called to notify transport users that another
68  * peer connected to us.
69  *
70  * @param cls closure
71  * @param peer the peer that connected
72  */
73 typedef void
74 (*GNUNET_TRANSPORT_NotifyConnect) (void *cls,
75                                    const struct GNUNET_PeerIdentity *peer);
76
77 /**
78  * Function called to notify transport users that another
79  * peer disconnected from us.
80  *
81  * @param cls closure
82  * @param peer the peer that disconnected
83  */
84 typedef void
85 (*GNUNET_TRANSPORT_NotifyDisconnect) (void *cls,
86                                       const struct GNUNET_PeerIdentity *peer);
87
88
89 /**
90  * Connect to the transport service.  Note that the connection may
91  * complete (or fail) asynchronously.
92  *
93  * @param cfg configuration to use
94  * @param self our own identity (API should check that it matches
95  *             the identity found by transport), or NULL (no check)
96  * @param cls closure for the callbacks
97  * @param rec receive function to call, or NULL
98  * @param nc function to call on connect events, or NULL
99  * @param nd function to call on disconnect events, or NULL
100  * @return NULL on error
101  */
102 struct GNUNET_TRANSPORT_Handle *
103 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
104                           const struct GNUNET_PeerIdentity *self,
105                           void *cls,
106                           GNUNET_TRANSPORT_ReceiveCallback rec,
107                           GNUNET_TRANSPORT_NotifyConnect nc,
108                           GNUNET_TRANSPORT_NotifyDisconnect nd);
109
110
111 /**
112  * Function called if we have "excess" bandwidth to a peer.
113  * The notification will happen the first time we have excess
114  * bandwidth, and then only again after the client has performed
115  * some transmission to the peer.
116  *
117  * Excess bandwidth is defined as being allowed (by ATS) to send
118  * more data, and us reaching the limit of the capacity build-up
119  * (which, if we go past it, means we don't use available bandwidth).
120  * See also the "max carry" in `struct GNUNET_BANDWIDTH_Tracker`.
121  *
122  * @param cls the closure
123  * @param neighbour peer that we have excess bandwidth to
124  */
125 typedef void
126 (*GNUNET_TRANSPORT_NotifyExcessBandwidth)(void *cls,
127                                           const struct GNUNET_PeerIdentity *neighbour);
128
129
130 /**
131  * Connect to the transport service.  Note that the connection may
132  * complete (or fail) asynchronously.
133  *
134  * @param cfg configuration to use
135  * @param self our own identity (API should check that it matches
136  *             the identity found by transport), or NULL (no check)
137  * @param cls closure for the callbacks
138  * @param rec receive function to call, or NULL
139  * @param nc function to call on connect events, or NULL
140  * @param nd function to call on disconnect events, or NULL
141  * @param neb function to call if we have excess bandwidth to a peer
142  * @return NULL on error
143  */
144 struct GNUNET_TRANSPORT_Handle *
145 GNUNET_TRANSPORT_connect2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
146                            const struct GNUNET_PeerIdentity *self,
147                            void *cls,
148                            GNUNET_TRANSPORT_ReceiveCallback rec,
149                            GNUNET_TRANSPORT_NotifyConnect nc,
150                            GNUNET_TRANSPORT_NotifyDisconnect nd,
151                            GNUNET_TRANSPORT_NotifyExcessBandwidth neb);
152
153
154 /**
155  * Disconnect from the transport service.
156  *
157  * @param handle handle returned from connect
158  */
159 void
160 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle);
161
162
163 /* ************************* Connections *********************** */
164
165 /**
166  * Opaque handle for a transmission-ready request.
167  */
168 struct GNUNET_TRANSPORT_TryConnectHandle;
169
170 /**
171  * Function to call with result of the try connect request.
172  *
173  * @param cls closure
174  * @param result #GNUNET_OK if message was transmitted to transport service
175  *               #GNUNET_SYSERR if message was not transmitted to transport service
176  */
177 typedef void
178 (*GNUNET_TRANSPORT_TryConnectCallback) (void *cls,
179                                         const int result);
180
181
182 /**
183  * Ask the transport service to establish a connection to
184  * the given peer.
185  *
186  * @param handle connection to transport service
187  * @param target who we should try to connect to
188  * @param cb callback to be called when request was transmitted to transport
189  *         service
190  * @param cb_cls closure for the callback @a cb
191  * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
192  *         NULL on failure (@a cb will not be called)
193  */
194 struct GNUNET_TRANSPORT_TryConnectHandle *
195 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
196                               const struct GNUNET_PeerIdentity *target,
197                               GNUNET_TRANSPORT_TryConnectCallback cb,
198                               void *cb_cls);
199
200
201 /**
202  * Cancel the request to transport to try a connect
203  * Callback will not be called
204  *
205  * @param tch handle to cancel
206  */
207 void
208 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch);
209
210
211 /**
212  * Ask the transport service to disconnect from the given peer.
213  *
214  * @param handle connection to transport service
215  * @param target who we should try to disconnect from
216  * @param cb callback to be called when request was transmitted to transport
217  *         service
218  * @param cb_cls closure for the callback @a cb
219  * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
220  *         NULL on failure (@a cb will not be called)
221  */
222 struct GNUNET_TRANSPORT_TryConnectHandle *
223 GNUNET_TRANSPORT_try_disconnect (struct GNUNET_TRANSPORT_Handle *handle,
224                                  const struct GNUNET_PeerIdentity *target,
225                                  GNUNET_TRANSPORT_TryConnectCallback cb,
226                                  void *cb_cls);
227
228
229 /**
230  * Cancel the request to transport to disconnect.
231  * Callback will not be called anymore.
232  *
233  * @param tch handle for operation to cancel
234  */
235 void
236 GNUNET_TRANSPORT_try_disconnect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch);
237
238
239 /* ************************* Sending *************************** */
240
241 /**
242  * Opaque handle for a transmission-ready request.
243  */
244 struct GNUNET_TRANSPORT_TransmitHandle;
245
246
247 /**
248  * Function called to notify a client about the connection begin ready
249  * to queue more data.  @a buf will be NULL and @a size zero if the
250  * connection was closed for writing in the meantime.
251  *
252  * @param cls closure
253  * @param size number of bytes available in @a buf
254  * @param buf where the callee should write the message
255  * @return number of bytes written to @a buf
256  */
257 typedef size_t
258 (*GNUNET_TRANSPORT_TransmitReadyNotify) (void *cls,
259                                          size_t size,
260                                          void *buf);
261
262
263 /**
264  * Check if we could queue a message of the given size for
265  * transmission.  The transport service will take both its internal
266  * buffers and bandwidth limits imposed by the other peer into
267  * consideration when answering this query.
268  *
269  * @param handle connection to transport service
270  * @param target who should receive the message
271  * @param size how big is the message we want to transmit?
272  * @param timeout after how long should we give up (and call
273  *        notify with buf NULL and size 0)?
274  * @param notify function to call when we are ready to
275  *        send such a message
276  * @param notify_cls closure for @a notify
277  * @return NULL if someone else is already waiting to be notified
278  *         non-NULL if the notify callback was queued (can be used to cancel
279  *         using #GNUNET_TRANSPORT_notify_transmit_ready_cancel())
280  */
281 struct GNUNET_TRANSPORT_TransmitHandle *
282 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
283                                         const struct GNUNET_PeerIdentity *target,
284                                         size_t size,
285                                         struct GNUNET_TIME_Relative timeout,
286                                         GNUNET_TRANSPORT_TransmitReadyNotify notify,
287                                         void *notify_cls);
288
289
290 /**
291  * Cancel the specified transmission-ready notification.
292  *
293  * @param th handle of the transmission notification request to cancel
294  */
295 void
296 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct GNUNET_TRANSPORT_TransmitHandle *th);
297
298
299
300 /**
301  * Checks if a given peer is connected to us
302  *
303  * @param handle connection to transport service
304  * @param peer the peer to check
305  * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
306  */
307 int
308 GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
309                                        const struct GNUNET_PeerIdentity *peer);
310
311
312
313 /* *********************** Metric manipulation ***************** */
314
315 /**
316  * Set transport metrics for a peer and a direction
317  *
318  * @param handle transport handle
319  * @param peer the peer to set the metric for
320  * @param inbound set inbound direction (#GNUNET_YES or #GNUNET_NO)
321  * @param outbound set outbound direction (#GNUNET_YES or #GNUNET_NO)
322  * @param ats the metric as ATS information
323  * @param ats_count the number of metrics
324  *
325  * Supported ATS values:
326  * #GNUNET_ATS_QUALITY_NET_DELAY  (value in ms)
327  * #GNUNET_ATS_QUALITY_NET_DISTANCE (value in count(hops))
328  *
329  * Example
330  * To enforce a delay of 10 ms for peer p1 in sending direction use:
331  *
332  * struct GNUNET_ATS_Information ats;
333  * ats.type = ntohl (GNUNET_ATS_QUALITY_NET_DELAY);
334  * ats.value = ntohl (10);
335  * GNUNET_TRANSPORT_set_traffic_metric (th, p1, TM_SEND, &ats, 1);
336  *
337  * Note:
338  * Delay restrictions in receiving direction will be enforced with
339  * 1 message delay.
340  */
341 void
342 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
343                                      const struct GNUNET_PeerIdentity *peer,
344                                      int inbound,
345                                      int outbound,
346                                      const struct GNUNET_ATS_Information *ats,
347                                      size_t ats_count);
348
349
350 /* *************************** HELLO *************************** */
351
352
353 /**
354  * Function called whenever there is an update to the
355  * HELLO of this peer.
356  *
357  * @param cls closure
358  * @param hello our updated HELLO
359  */
360 typedef void
361 (*GNUNET_TRANSPORT_HelloUpdateCallback) (void *cls,
362                                          const struct GNUNET_MessageHeader *hello);
363
364
365 /**
366  * Handle to cancel a #GNUNET_TRANSPORT_get_hello() operation.
367  */
368 struct GNUNET_TRANSPORT_GetHelloHandle;
369
370
371 /**
372  * Obtain updates on changes to the HELLO message for this peer. The callback
373  * given in this function is never called synchronously.
374  *
375  * @param handle connection to transport service
376  * @param rec function to call with the HELLO
377  * @param rec_cls closure for @a rec
378  * @return handle to cancel the operation
379  */
380 struct GNUNET_TRANSPORT_GetHelloHandle *
381 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
382                             GNUNET_TRANSPORT_HelloUpdateCallback rec,
383                             void *rec_cls);
384
385
386 /**
387  * Stop receiving updates about changes to our HELLO message.
388  *
389  * @param ghh handle to cancel
390  */
391 void
392 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh);
393
394
395 /**
396  * Handle for a #GNUNET_TRANSPORT_offer_hello operation
397  */
398 struct GNUNET_TRANSPORT_OfferHelloHandle;
399
400 /**
401  * Offer the transport service the HELLO of another peer.  Note that
402  * the transport service may just ignore this message if the HELLO is
403  * malformed or useless due to our local configuration.
404  *
405  * @param handle connection to transport service
406  * @param hello the hello message
407  * @param cont continuation to call when HELLO has been sent,
408  *      tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
409  *      tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
410  * @param cls closure for continuation
411  * @return a GNUNET_TRANSPORT_OfferHelloHandle handle or NULL on failure,
412  *      in case of failure cont will not be called
413  *
414  */
415 struct GNUNET_TRANSPORT_OfferHelloHandle *
416 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
417                               const struct GNUNET_MessageHeader *hello,
418                               GNUNET_SCHEDULER_Task cont, void *cls);
419
420
421 /**
422  * Cancel the request to transport to offer the HELLO message
423  *
424  * @param ohh the `struct GNUNET_TRANSPORT_OfferHelloHandle` to cancel
425  */
426 void
427 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh);
428
429
430 /* *********************** Address to String ******************* */
431
432 /**
433  * Handle to cancel a pending address lookup.
434  */
435 struct GNUNET_TRANSPORT_AddressToStringContext;
436
437
438 /**
439  * Function to call with a textual representation of an address.  This
440  * function will be called several times with different possible
441  * textual representations, and a last time with @a address being NULL
442  * to signal the end of the iteration.  Note that @a address NULL
443  * always is the last call, regardless of the value in @a res.
444  *
445  * @param cls closure
446  * @param address NULL on end of iteration,
447  *        otherwise 0-terminated printable UTF-8 string,
448  *        in particular an empty string if @a res is #GNUNET_NO
449  * @param res result of the address to string conversion:
450  *        if #GNUNET_OK: conversion successful
451  *        if #GNUNET_NO: address was invalid (or not supported)
452  *        if #GNUNET_SYSERR: communication error (IPC error)
453  */
454 typedef void
455 (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls,
456                                              const char *address,
457                                              int res);
458
459
460 /**
461  * Convert a binary address into a human readable address.
462  *
463  * @param cfg configuration to use
464  * @param address address to convert (binary format)
465  * @param numeric should (IP) addresses be displayed in numeric form
466  *                (otherwise do reverse DNS lookup)
467  * @param timeout how long is the lookup allowed to take at most
468  * @param aluc function to call with the results
469  * @param aluc_cls closure for @a aluc
470  * @return handle to cancel the operation, NULL on error
471  */
472 struct GNUNET_TRANSPORT_AddressToStringContext *
473 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
474                                     const struct GNUNET_HELLO_Address *address,
475                                     int numeric,
476                                     struct GNUNET_TIME_Relative timeout,
477                                     GNUNET_TRANSPORT_AddressToStringCallback aluc,
478                                     void *aluc_cls);
479
480
481 /**
482  * Cancel request for address conversion.
483  *
484  * @param pic the context handle
485  */
486 void
487 GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *pic);
488
489
490 /* *********************** Monitoring ************************** */
491
492
493 /**
494  * Possible state of a neighbour.  Initially, we are #GNUNET_TRANSPORT_PS_NOT_CONNECTED.
495  *
496  * Then, there are two main paths. If we receive a CONNECT message, we give
497  * the inbound address to ATS. After the check we ask ATS for a suggestion
498  * (#GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS). If ATS makes a suggestion, we
499  * send our CONNECT_ACK and go to #GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK.
500  * If we receive a SESSION_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
501  * (and notify everyone about the new connection). If the operation times out,
502  * we go to #GNUNET_TRANSPORT_PS_DISCONNECT.
503  *
504  * The other case is where we transmit a CONNECT message first.  We
505  * start with #GNUNET_TRANSPORT_PS_INIT_ATS.  If we get an address, we send
506  * the CONNECT message and go to state #GNUNET_TRANSPORT_PS_CONNECT_SENT.
507  * Once we receive a CONNECT_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
508  * (and notify everyone about the new connection and send
509  * back a SESSION_ACK).  If the operation times out, we go to
510  * #GNUNET_TRANSPORT_PS_DISCONNECT.
511  *
512  * If the session is in trouble (i.e. transport-level disconnect or
513  * timeout), we go to #GNUNET_TRANSPORT_PS_RECONNECT_ATS where we ask ATS for a new
514  * address (we don't notify anyone about the disconnect yet).  Once we
515  * have a new address, we enter #GNUNET_TRANSPORT_PS_RECONNECT_SENT and send a
516  * CONNECT message.  If we receive a
517  * CONNECT_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED and nobody noticed that we had
518  * trouble; we also send a SESSION_ACK at this time just in case.  If
519  * the operation times out, we go to #GNUNET_TRANSPORT_PS_DISCONNECT (and notify everyone
520  * about the lost connection).
521  *
522  * If ATS decides to switch addresses while we have a normal
523  * connection, we go to #GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT
524  * and send a SESSION_CONNECT.  If we get a SESSION_ACK back, we switch the
525  * primary connection to the suggested alternative from ATS, go back
526  * to #GNUNET_TRANSPORT_PS_CONNECTED and send a SESSION_ACK to the other peer just to be
527  * sure.  If the operation times out
528  * we go to #GNUNET_TRANSPORT_PS_CONNECTED (and notify ATS that the given alternative
529  * address is "invalid").
530  *
531  * Once a session is in #GNUNET_TRANSPORT_PS_DISCONNECT, it is cleaned up and then goes
532  * to (#GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED).  If we receive an explicit disconnect
533  * request, we can go from any state to #GNUNET_TRANSPORT_PS_DISCONNECT, possibly after
534  * generating disconnect notifications.
535  *
536  * Note that it is quite possible that while we are in any of these
537  * states, we could receive a 'CONNECT' request from the other peer.
538  * We then enter a 'weird' state where we pursue our own primary state
539  * machine (as described above), but with the 'send_connect_ack' flag
540  * set to 1.  If our state machine allows us to send a 'CONNECT_ACK'
541  * (because we have an acceptable address), we send the 'CONNECT_ACK'
542  * and set the 'send_connect_ack' to 2.  If we then receive a
543  * 'SESSION_ACK', we go to #GNUNET_TRANSPORT_PS_CONNECTED (and reset 'send_connect_ack'
544  * to 0).
545  *
546  */
547 enum GNUNET_TRANSPORT_PeerState
548 {
549   /**
550    * Fresh peer or completely disconnected
551    */
552   GNUNET_TRANSPORT_PS_NOT_CONNECTED = 0,
553
554   /**
555    * Asked to initiate connection, trying to get address from ATS
556    */
557   GNUNET_TRANSPORT_PS_INIT_ATS,
558
559   /**
560    * Sent CONNECT message to other peer, waiting for CONNECT_ACK
561    */
562   GNUNET_TRANSPORT_PS_CONNECT_SENT,
563
564   /**
565    * Received a CONNECT, asking ATS about address suggestions.
566    */
567   GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
568
569   /**
570    * CONNECT request from other peer was CONNECT_ACK'ed, waiting for
571    * SESSION_ACK.
572    */
573   GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
574
575   /**
576    * Got our CONNECT_ACK/SESSION_ACK, connection is up.
577    */
578   GNUNET_TRANSPORT_PS_CONNECTED,
579
580   /**
581    * Connection got into trouble, rest of the system still believes
582    * it to be up, but we're getting a new address from ATS.
583    */
584   GNUNET_TRANSPORT_PS_RECONNECT_ATS,
585
586   /**
587    * Sent CONNECT over new address (either by ATS telling us to switch
588    * addresses or from RECONNECT_ATS); if this fails, we need to tell
589    * the rest of the system about a disconnect.
590    */
591   GNUNET_TRANSPORT_PS_RECONNECT_SENT,
592
593   /**
594    * We have some primary connection, but ATS suggested we switch
595    * to some alternative; we now sent a CONNECT message for the
596    * alternative session to the other peer and waiting for a
597    * CONNECT_ACK to make this our primary connection.
598    */
599   GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT,
600
601   /**
602    * Disconnect in progress (we're sending the DISCONNECT message to the
603    * other peer; after that is finished, the state will be cleaned up).
604    */
605   GNUNET_TRANSPORT_PS_DISCONNECT,
606
607   /**
608    * We're finished with the disconnect; and are cleaning up the state
609    * now!  We put the struct into this state when we are really in the
610    * task that calls 'free' on it and are about to remove the record
611    * from the map.  We should never find a 'struct NeighbourMapEntry'
612    * in this state in the map.  Accessing a 'struct NeighbourMapEntry'
613    * in this state virtually always means using memory that has been
614    * freed (the exception being the cleanup code in #free_neighbour()).
615    */
616   GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED
617 };
618
619
620 /**
621  * Convert a transport state to a human readable string.
622  *
623  * @param state the state
624  */
625 const char *
626 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state);
627
628
629 /**
630  * Check if a state is defined as connected
631  *
632  * @param state the state value
633  * @return #GNUNET_YES or #GNUNET_NO
634  */
635 int
636 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state);
637
638
639 /**
640  * Handle for a #GNUNET_TRANSPORT_monitor_peers operation.
641  */
642 struct GNUNET_TRANSPORT_PeerMonitoringContext;
643
644
645 /**
646  * Function to call with information about a peer
647  *
648  * If one_shot was set to #GNUNET_YES to iterate over all peers once,
649  * a final call with NULL for peer and address will follow when done.
650  * In this case state and timeout do not contain valid values.
651  *
652  * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called from
653  * within this function!
654  *
655  *
656  * @param cls closure
657  * @param peer peer this update is about,
658  *      NULL if this is the final last callback for a iteration operation
659  * @param address address, NULL for disconnect notification in monitor mode
660  * @param state current state this peer is in
661  * @param state_timeout timeout for the current state of the peer
662  */
663 typedef void
664 (*GNUNET_TRANSPORT_PeerIterateCallback) (void *cls,
665                                          const struct GNUNET_PeerIdentity *peer,
666                                          const struct GNUNET_HELLO_Address *address,
667                                          enum GNUNET_TRANSPORT_PeerState state,
668                                          struct GNUNET_TIME_Absolute state_timeout);
669
670
671 /**
672  * Return information about a specific peer or all peers currently known to
673  * transport service once or in monitoring mode. To obtain information about
674  * a specific peer, a peer identity can be passed. To obtain information about
675  * all peers currently known to transport service, NULL can be passed as peer
676  * identity.
677  *
678  * For each peer, the callback is called with information about the address used
679  * to communicate with this peer, the state this peer is currently in and the
680  * the current timeout for this state.
681  *
682  * Upon completion, the 'GNUNET_TRANSPORT_PeerIterateCallback' is called one
683  * more time with 'NULL'. After this, the operation must no longer be
684  * explicitly canceled.
685  *
686  * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
687  * the peer_callback!
688  *
689  * @param cfg configuration to use
690  * @param peer a specific peer identity to obtain information for,
691  *      NULL for all peers
692  * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
693  *                 #GNUNET_NO to monitor peers continuously
694  * @param timeout how long is the lookup allowed to take at most
695  * @param peer_callback function to call with the results
696  * @param peer_callback_cls closure for @a peer_callback
697  */
698 struct GNUNET_TRANSPORT_PeerMonitoringContext *
699 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
700                                 const struct GNUNET_PeerIdentity *peer,
701                                 int one_shot,
702                                 struct GNUNET_TIME_Relative timeout,
703                                 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
704                                 void *peer_callback_cls);
705
706
707 /**
708  * Cancel request to monitor peers
709  *
710  * @param pic handle for the request to cancel
711  */
712 void
713 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic);
714
715
716 /**
717  * Handle for a #GNUNET_TRANSPORT_monitor_validation_entries() operation.
718  */
719 struct GNUNET_TRANSPORT_ValidationMonitoringContext;
720
721
722 /**
723  * Current state of a validation process.
724  *
725  * FIXME: what state is used to indicate that a validation
726  * was successful? If that is clarified/determined, "UGH" in
727  * ~gnunet-peerinfo-gtk.c:1103 should be resolved.
728  */
729 enum GNUNET_TRANSPORT_ValidationState
730 {
731   /**
732    * Undefined state
733    *
734    * Used for final callback indicating operation done
735    */
736   GNUNET_TRANSPORT_VS_NONE,
737
738   /**
739    * Fresh validation entry
740    *
741    * Entry was just created, no validation process was executed
742    */
743   GNUNET_TRANSPORT_VS_NEW,
744
745   /**
746    * Updated validation entry
747    *
748    * This is an update for an existing validation entry
749    */
750   GNUNET_TRANSPORT_VS_UPDATE,
751
752   /**
753    * Timeout for validation entry
754    *
755    * A timeout occured during the validation process
756    */
757   GNUNET_TRANSPORT_VS_TIMEOUT,
758
759   /**
760    * Validation entry is removed
761    *
762    * The validation entry is getting removed due to a failed validation
763    */
764   GNUNET_TRANSPORT_VS_REMOVE
765 };
766
767
768 /**
769  * Function to call with validation information about a peer
770  *
771  * This function is called by the transport validation monitoring api to
772  * indicate a change to a validation entry. The information included represent
773  * the current state of the validation entry,
774  *
775  * If the monitoring was called with one_shot=GNUNET_YES, a final callback
776  * with peer==NULL and address==NULL is executed.
777  *
778  * @param cls closure
779  * @param peer peer this update is about,
780  *      NULL if this is the final last callback for a iteration operation
781  * @param address address,
782  *      NULL for disconnect notification in monitor mode
783  * @param last_validation when was this address last validated
784  * @param valid_until when does this address expire
785  * @param next_validation time of the next validation operation
786  * @param state state in the validation state machine
787  */
788 typedef void
789 (*GNUNET_TRANSPORT_ValidationIterateCallback) (void *cls,
790                                                const struct GNUNET_PeerIdentity *peer,
791                                                const struct GNUNET_HELLO_Address *address,
792                                                struct GNUNET_TIME_Absolute last_validation,
793                                                struct GNUNET_TIME_Absolute valid_until,
794                                                struct GNUNET_TIME_Absolute next_validation,
795                                                enum GNUNET_TRANSPORT_ValidationState state);
796
797
798 /**
799  * Convert validation state to human-readable string.
800  *
801  * @param state the state value
802  * @return corresponding string
803  */
804 const char *
805 GNUNET_TRANSPORT_vs2s (enum GNUNET_TRANSPORT_ValidationState state);
806
807
808 /**
809  * Return information about pending address validation operations for a specific
810  * or all peers
811  *
812  * @param cfg configuration to use
813  * @param peer a specific peer identity to obtain validation entries for,
814  *      NULL for all peers
815  * @param one_shot #GNUNET_YES to return all entries and then end (with NULL+NULL),
816  *                 #GNUNET_NO to monitor validation entries continuously
817  * @param timeout how long is the lookup allowed to take at most
818  * @param validation_callback function to call with the results
819  * @param validation_callback_cls closure for @a validation_callback
820  */
821 struct GNUNET_TRANSPORT_ValidationMonitoringContext *
822 GNUNET_TRANSPORT_monitor_validation_entries (const struct GNUNET_CONFIGURATION_Handle *cfg,
823                                              const struct GNUNET_PeerIdentity *peer,
824                                              int one_shot,
825                                              struct GNUNET_TIME_Relative timeout,
826                                              GNUNET_TRANSPORT_ValidationIterateCallback validation_callback,
827                                              void *validation_callback_cls);
828
829
830 /**
831  * Return information about all current pending validation operations
832  *
833  * @param vic handle for the request to cancel
834  */
835 void
836 GNUNET_TRANSPORT_monitor_validation_entries_cancel (struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic);
837
838
839 /* *********************** Blacklisting ************************ */
840
841 /**
842  * Handle for blacklisting peers.
843  */
844 struct GNUNET_TRANSPORT_Blacklist;
845
846
847 /**
848  * Function that decides if a connection is acceptable or not.
849  *
850  * @param cls closure
851  * @param pid peer to approve or disapproave
852  * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
853  */
854 typedef int
855 (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
856                                        const struct GNUNET_PeerIdentity *pid);
857
858
859 /**
860  * Install a blacklist callback.  The service will be queried for all
861  * existing connections as well as any fresh connections to check if
862  * they are permitted.  If the blacklisting callback is unregistered,
863  * all hosts that were denied in the past will automatically be
864  * whitelisted again.  Cancelling the blacklist handle is also the
865  * only way to re-enable connections from peers that were previously
866  * blacklisted.
867  *
868  * @param cfg configuration to use
869  * @param cb callback to invoke to check if connections are allowed
870  * @param cb_cls closure for @a cb
871  * @return NULL on error, otherwise handle for cancellation
872  */
873 struct GNUNET_TRANSPORT_Blacklist *
874 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
875                             GNUNET_TRANSPORT_BlacklistCallback cb,
876                             void *cb_cls);
877
878
879 /**
880  * Abort the blacklist.  Note that this function is the only way for
881  * removing a peer from the blacklist.
882  *
883  * @param br handle of the request that is to be cancelled
884  */
885 void
886 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
887
888
889
890 #if 0                           /* keep Emacsens' auto-indent happy */
891 {
892 #endif
893 #ifdef __cplusplus
894 }
895 #endif
896
897 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
898 #endif
899 /* end of gnunet_transport_service.h */
900
901