-remove deprecated try_disconnect API
[oweals/gnunet.git] / src / include / gnunet_transport_service.h
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 0x00000002
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                                         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  * @deprecated
194  */
195 struct GNUNET_TRANSPORT_TryConnectHandle *
196 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
197                               const struct GNUNET_PeerIdentity *target,
198                               GNUNET_TRANSPORT_TryConnectCallback cb,
199                               void *cb_cls);
200
201
202 /**
203  * Cancel the request to transport to try a connect
204  * Callback will not be called
205  *
206  * @param tch handle to cancel
207  */
208 void
209 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch);
210
211
212 /* ************************* Sending *************************** */
213
214 /**
215  * Opaque handle for a transmission-ready request.
216  */
217 struct GNUNET_TRANSPORT_TransmitHandle;
218
219
220 /**
221  * Function called to notify a client about the connection begin ready
222  * to queue more data.  @a buf will be NULL and @a size zero if the
223  * connection was closed for writing in the meantime.
224  *
225  * @param cls closure
226  * @param size number of bytes available in @a buf
227  * @param buf where the callee should write the message
228  * @return number of bytes written to @a buf
229  */
230 typedef size_t
231 (*GNUNET_TRANSPORT_TransmitReadyNotify) (void *cls,
232                                          size_t size,
233                                          void *buf);
234
235
236 /**
237  * Check if we could queue a message of the given size for
238  * transmission.  The transport service will take both its internal
239  * buffers and bandwidth limits imposed by the other peer into
240  * consideration when answering this query.
241  *
242  * @param handle connection to transport service
243  * @param target who should receive the message
244  * @param size how big is the message we want to transmit?
245  * @param timeout after how long should we give up (and call
246  *        notify with buf NULL and size 0)?
247  * @param notify function to call when we are ready to
248  *        send such a message
249  * @param notify_cls closure for @a notify
250  * @return NULL if someone else is already waiting to be notified
251  *         non-NULL if the notify callback was queued (can be used to cancel
252  *         using #GNUNET_TRANSPORT_notify_transmit_ready_cancel())
253  */
254 struct GNUNET_TRANSPORT_TransmitHandle *
255 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
256                                         const struct GNUNET_PeerIdentity *target,
257                                         size_t size,
258                                         struct GNUNET_TIME_Relative timeout,
259                                         GNUNET_TRANSPORT_TransmitReadyNotify notify,
260                                         void *notify_cls);
261
262
263 /**
264  * Cancel the specified transmission-ready notification.
265  *
266  * @param th handle of the transmission notification request to cancel
267  */
268 void
269 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct GNUNET_TRANSPORT_TransmitHandle *th);
270
271
272 /**
273  * Checks if a given peer is connected to us
274  *
275  * @param handle connection to transport service
276  * @param peer the peer to check
277  * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
278  */
279 int
280 GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
281                                        const struct GNUNET_PeerIdentity *peer);
282
283
284
285 /* *********************** Metric manipulation ***************** */
286
287 /**
288  * Set transport metrics for a peer and a direction
289  *
290  * @param handle transport handle
291  * @param peer the peer to set the metric for
292  * @param prop the performance metrics to set
293  * @param delay_in inbound delay to introduce
294  * @param delay_out outbound delay to introduce
295  *
296  * Note: Delay restrictions in receiving direction will be enforced
297  * with one message delay.
298  */
299 void
300 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
301                                      const struct GNUNET_PeerIdentity *peer,
302                                      const struct GNUNET_ATS_Properties *prop,
303                                      struct GNUNET_TIME_Relative delay_in,
304                                      struct GNUNET_TIME_Relative delay_out);
305
306
307 /* *************************** HELLO *************************** */
308
309
310 /**
311  * Function called whenever there is an update to the
312  * HELLO of this peer.
313  *
314  * @param cls closure
315  * @param hello our updated HELLO
316  */
317 typedef void
318 (*GNUNET_TRANSPORT_HelloUpdateCallback) (void *cls,
319                                          const struct GNUNET_MessageHeader *hello);
320
321
322 /**
323  * Handle to cancel a #GNUNET_TRANSPORT_get_hello() operation.
324  */
325 struct GNUNET_TRANSPORT_GetHelloHandle;
326
327
328 /**
329  * Obtain updates on changes to the HELLO message for this peer. The callback
330  * given in this function is never called synchronously.
331  *
332  * @param handle connection to transport service
333  * @param rec function to call with the HELLO
334  * @param rec_cls closure for @a rec
335  * @return handle to cancel the operation
336  */
337 struct GNUNET_TRANSPORT_GetHelloHandle *
338 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
339                             GNUNET_TRANSPORT_HelloUpdateCallback rec,
340                             void *rec_cls);
341
342
343 /**
344  * Stop receiving updates about changes to our HELLO message.
345  *
346  * @param ghh handle to cancel
347  */
348 void
349 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh);
350
351
352 /**
353  * Handle for a #GNUNET_TRANSPORT_offer_hello operation
354  */
355 struct GNUNET_TRANSPORT_OfferHelloHandle;
356
357
358 /**
359  * Offer the transport service the HELLO of another peer.  Note that
360  * the transport service may just ignore this message if the HELLO is
361  * malformed or useless due to our local configuration.
362  *
363  * @param handle connection to transport service
364  * @param hello the hello message
365  * @param cont continuation to call when HELLO has been sent,
366  *      tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
367  *      tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
368  * @param cont_cls closure for @a cont
369  * @return a GNUNET_TRANSPORT_OfferHelloHandle handle or NULL on failure,
370  *      in case of failure @a cont will not be called
371  *
372  */
373 struct GNUNET_TRANSPORT_OfferHelloHandle *
374 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
375                               const struct GNUNET_MessageHeader *hello,
376                               GNUNET_SCHEDULER_TaskCallback cont,
377                               void *cont_cls);
378
379
380 /**
381  * Cancel the request to transport to offer the HELLO message
382  *
383  * @param ohh the `struct GNUNET_TRANSPORT_OfferHelloHandle` to cancel
384  */
385 void
386 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh);
387
388
389 /* *********************** Address to String ******************* */
390
391 /**
392  * Handle to cancel a pending address lookup.
393  */
394 struct GNUNET_TRANSPORT_AddressToStringContext;
395
396
397 /**
398  * Function to call with a textual representation of an address.  This
399  * function will be called several times with different possible
400  * textual representations, and a last time with @a address being NULL
401  * to signal the end of the iteration.  Note that @a address NULL
402  * always is the last call, regardless of the value in @a res.
403  *
404  * @param cls closure
405  * @param address NULL on end of iteration,
406  *        otherwise 0-terminated printable UTF-8 string,
407  *        in particular an empty string if @a res is #GNUNET_NO
408  * @param res result of the address to string conversion:
409  *        if #GNUNET_OK: conversion successful
410  *        if #GNUNET_NO: address was invalid (or not supported)
411  *        if #GNUNET_SYSERR: communication error (IPC error)
412  */
413 typedef void
414 (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls,
415                                              const char *address,
416                                              int res);
417
418
419 /**
420  * Convert a binary address into a human readable address.
421  *
422  * @param cfg configuration to use
423  * @param address address to convert (binary format)
424  * @param numeric should (IP) addresses be displayed in numeric form
425  *                (otherwise do reverse DNS lookup)
426  * @param timeout how long is the lookup allowed to take at most
427  * @param aluc function to call with the results
428  * @param aluc_cls closure for @a aluc
429  * @return handle to cancel the operation, NULL on error
430  */
431 struct GNUNET_TRANSPORT_AddressToStringContext *
432 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
433                                     const struct GNUNET_HELLO_Address *address,
434                                     int numeric,
435                                     struct GNUNET_TIME_Relative timeout,
436                                     GNUNET_TRANSPORT_AddressToStringCallback aluc,
437                                     void *aluc_cls);
438
439
440 /**
441  * Cancel request for address conversion.
442  *
443  * @param pic the context handle
444  */
445 void
446 GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *pic);
447
448
449 /* *********************** Monitoring ************************** */
450
451
452 /**
453  * Possible state of a neighbour.  Initially, we are #GNUNET_TRANSPORT_PS_NOT_CONNECTED.
454  *
455  * Then, there are two main paths. If we receive a SYN message, we give
456  * the inbound address to ATS. After the check we ask ATS for a suggestion
457  * (#GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS). If ATS makes a suggestion, we
458  * send our SYN_ACK and go to #GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK.
459  * If we receive a ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
460  * (and notify everyone about the new connection). If the operation times out,
461  * we go to #GNUNET_TRANSPORT_PS_DISCONNECT.
462  *
463  * The other case is where we transmit a SYN message first.  We
464  * start with #GNUNET_TRANSPORT_PS_INIT_ATS.  If we get an address, we send
465  * the SYN message and go to state #GNUNET_TRANSPORT_PS_CONNECT_SENT.
466  * Once we receive a SYN_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
467  * (and notify everyone about the new connection and send
468  * back a ACK).  If the operation times out, we go to
469  * #GNUNET_TRANSPORT_PS_DISCONNECT.
470  *
471  * If the session is in trouble (i.e. transport-level disconnect or
472  * timeout), we go to #GNUNET_TRANSPORT_PS_RECONNECT_ATS where we ask ATS for a new
473  * address (we don't notify anyone about the disconnect yet).  Once we
474  * have a new address, we enter #GNUNET_TRANSPORT_PS_RECONNECT_SENT and send a
475  * SYN message.  If we receive a
476  * SYN_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED and nobody noticed that we had
477  * trouble; we also send a ACK at this time just in case.  If
478  * the operation times out, we go to #GNUNET_TRANSPORT_PS_DISCONNECT (and notify everyone
479  * about the lost connection).
480  *
481  * If ATS decides to switch addresses while we have a normal
482  * connection, we go to #GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT
483  * and send a SESSION_CONNECT.  If we get a ACK back, we switch the
484  * primary connection to the suggested alternative from ATS, go back
485  * to #GNUNET_TRANSPORT_PS_CONNECTED and send a ACK to the other peer just to be
486  * sure.  If the operation times out
487  * we go to #GNUNET_TRANSPORT_PS_CONNECTED (and notify ATS that the given alternative
488  * address is "invalid").
489  *
490  * Once a session is in #GNUNET_TRANSPORT_PS_DISCONNECT, it is cleaned up and then goes
491  * to (#GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED).  If we receive an explicit disconnect
492  * request, we can go from any state to #GNUNET_TRANSPORT_PS_DISCONNECT, possibly after
493  * generating disconnect notifications.
494  *
495  * Note that it is quite possible that while we are in any of these
496  * states, we could receive a 'SYN' request from the other peer.
497  * We then enter a 'weird' state where we pursue our own primary state
498  * machine (as described above), but with the 'send_connect_ack' flag
499  * set to 1.  If our state machine allows us to send a 'SYN_ACK'
500  * (because we have an acceptable address), we send the 'SYN_ACK'
501  * and set the 'send_connect_ack' to 2.  If we then receive a
502  * 'ACK', we go to #GNUNET_TRANSPORT_PS_CONNECTED (and reset 'send_connect_ack'
503  * to 0).
504  *
505  */
506 enum GNUNET_TRANSPORT_PeerState
507 {
508   /**
509    * Fresh peer or completely disconnected
510    */
511   GNUNET_TRANSPORT_PS_NOT_CONNECTED = 0,
512
513   /**
514    * Asked to initiate connection, trying to get address from ATS
515    */
516   GNUNET_TRANSPORT_PS_INIT_ATS,
517
518   /**
519    * Sent SYN message to other peer, waiting for SYN_ACK
520    */
521   GNUNET_TRANSPORT_PS_SYN_SENT,
522
523   /**
524    * Received a SYN, asking ATS about address suggestions.
525    */
526   GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
527
528   /**
529    * SYN request from other peer was SYN_ACK'ed, waiting for ACK.
530    */
531   GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
532
533   /**
534    * Got our SYN_ACK/ACK, connection is up.
535    */
536   GNUNET_TRANSPORT_PS_CONNECTED,
537
538   /**
539    * Connection got into trouble, rest of the system still believes
540    * it to be up, but we're getting a new address from ATS.
541    */
542   GNUNET_TRANSPORT_PS_RECONNECT_ATS,
543
544   /**
545    * Sent SYN over new address (either by ATS telling us to switch
546    * addresses or from RECONNECT_ATS); if this fails, we need to tell
547    * the rest of the system about a disconnect.
548    */
549   GNUNET_TRANSPORT_PS_RECONNECT_SENT,
550
551   /**
552    * We have some primary connection, but ATS suggested we switch
553    * to some alternative; we now sent a SYN message for the
554    * alternative session to the other peer and waiting for a
555    * SYN_ACK to make this our primary connection.
556    */
557   GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
558
559   /**
560    * Disconnect in progress (we're sending the DISCONNECT message to the
561    * other peer; after that is finished, the state will be cleaned up).
562    */
563   GNUNET_TRANSPORT_PS_DISCONNECT,
564
565   /**
566    * We're finished with the disconnect; and are cleaning up the state
567    * now!  We put the struct into this state when we are really in the
568    * task that calls 'free' on it and are about to remove the record
569    * from the map.  We should never find a 'struct NeighbourMapEntry'
570    * in this state in the map.  Accessing a 'struct NeighbourMapEntry'
571    * in this state virtually always means using memory that has been
572    * freed (the exception being the cleanup code in #free_neighbour()).
573    */
574   GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED
575 };
576
577
578 /**
579  * Convert a transport state to a human readable string.
580  *
581  * @param state the state
582  */
583 const char *
584 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state);
585
586
587 /**
588  * Check if a state is defined as connected
589  *
590  * @param state the state value
591  * @return #GNUNET_YES or #GNUNET_NO
592  */
593 int
594 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state);
595
596
597 /**
598  * Handle for a #GNUNET_TRANSPORT_monitor_peers operation.
599  */
600 struct GNUNET_TRANSPORT_PeerMonitoringContext;
601
602
603 /**
604  * Function to call with information about a peer
605  *
606  * If one_shot was set to #GNUNET_YES to iterate over all peers once,
607  * a final call with NULL for peer and address will follow when done.
608  * In this case state and timeout do not contain valid values.
609  *
610  * The #GNUNET_TRANSPORT_monitor_peers_cancel() call MUST not be called from
611  * within this function!
612  *
613  *
614  * @param cls closure
615  * @param peer peer this update is about,
616  *      NULL if this is the final last callback for a iteration operation
617  * @param address address, NULL if this is the final callback for iteration op
618  * @param state current state this peer is in
619  * @param state_timeout timeout for the current state of the peer
620  */
621 typedef void
622 (*GNUNET_TRANSPORT_PeerIterateCallback) (void *cls,
623                                          const struct GNUNET_PeerIdentity *peer,
624                                          const struct GNUNET_HELLO_Address *address,
625                                          enum GNUNET_TRANSPORT_PeerState state,
626                                          struct GNUNET_TIME_Absolute state_timeout);
627
628
629 /**
630  * Return information about a specific peer or all peers currently known to
631  * transport service once or in monitoring mode. To obtain information about
632  * a specific peer, a peer identity can be passed. To obtain information about
633  * all peers currently known to transport service, NULL can be passed as peer
634  * identity.
635  *
636  * For each peer, the callback is called with information about the address used
637  * to communicate with this peer, the state this peer is currently in and the
638  * the current timeout for this state.
639  *
640  * Upon completion, the #GNUNET_TRANSPORT_PeerIterateCallback is called one
641  * more time with `NULL`. After this, the operation must no longer be
642  * explicitly canceled.
643  *
644  * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
645  * the peer_callback!
646  *
647  * @param cfg configuration to use
648  * @param peer a specific peer identity to obtain information for,
649  *      NULL for all peers
650  * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
651  *                 #GNUNET_NO to monitor peers continuously
652  * @param timeout how long is the lookup allowed to take at most
653  * @param peer_callback function to call with the results
654  * @param peer_callback_cls closure for @a peer_callback
655  */
656 struct GNUNET_TRANSPORT_PeerMonitoringContext *
657 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
658                                 const struct GNUNET_PeerIdentity *peer,
659                                 int one_shot,
660                                 struct GNUNET_TIME_Relative timeout,
661                                 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
662                                 void *peer_callback_cls);
663
664
665 /**
666  * Cancel request to monitor peers
667  *
668  * @param pic handle for the request to cancel
669  */
670 void
671 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic);
672
673
674 /**
675  * Handle for a #GNUNET_TRANSPORT_monitor_validation_entries() operation.
676  */
677 struct GNUNET_TRANSPORT_ValidationMonitoringContext;
678
679
680 /**
681  * Current state of a validation process.
682  *
683  * FIXME: what state is used to indicate that a validation
684  * was successful? If that is clarified/determined, "UGH" in
685  * ~gnunet-peerinfo-gtk.c:1103 should be resolved.
686  */
687 enum GNUNET_TRANSPORT_ValidationState
688 {
689   /**
690    * Undefined state
691    *
692    * Used for final callback indicating operation done
693    */
694   GNUNET_TRANSPORT_VS_NONE,
695
696   /**
697    * Fresh validation entry
698    *
699    * Entry was just created, no validation process was executed
700    */
701   GNUNET_TRANSPORT_VS_NEW,
702
703   /**
704    * Updated validation entry
705    *
706    * This is an update for an existing validation entry
707    */
708   GNUNET_TRANSPORT_VS_UPDATE,
709
710   /**
711    * Timeout for validation entry
712    *
713    * A timeout occured during the validation process
714    */
715   GNUNET_TRANSPORT_VS_TIMEOUT,
716
717   /**
718    * Validation entry is removed
719    *
720    * The validation entry is getting removed due to a failed validation
721    */
722   GNUNET_TRANSPORT_VS_REMOVE
723 };
724
725
726 /**
727  * Function to call with validation information about a peer
728  *
729  * This function is called by the transport validation monitoring api to
730  * indicate a change to a validation entry. The information included represent
731  * the current state of the validation entry,
732  *
733  * If the monitoring was called with `one_shot==GNUNET_YES`, a final callback
734  * with `address==NULL` is executed.
735  *
736  * @param cls closure
737  * @param address address this update is about,
738  *      NULL if this is the final last callback for a iteration operation
739  * @param last_validation when was this address last validated
740  * @param valid_until when does this address expire
741  * @param next_validation time of the next validation operation
742  * @param state state in the validation state machine
743  */
744 typedef void
745 (*GNUNET_TRANSPORT_ValidationIterateCallback) (void *cls,
746                                                const struct GNUNET_HELLO_Address *address,
747                                                struct GNUNET_TIME_Absolute last_validation,
748                                                struct GNUNET_TIME_Absolute valid_until,
749                                                struct GNUNET_TIME_Absolute next_validation,
750                                                enum GNUNET_TRANSPORT_ValidationState state);
751
752
753 /**
754  * Convert validation state to human-readable string.
755  *
756  * @param state the state value
757  * @return corresponding string
758  */
759 const char *
760 GNUNET_TRANSPORT_vs2s (enum GNUNET_TRANSPORT_ValidationState state);
761
762
763 /**
764  * Return information about pending address validation operations for a specific
765  * or all peers
766  *
767  * @param cfg configuration to use
768  * @param peer a specific peer identity to obtain validation entries for,
769  *      NULL for all peers
770  * @param one_shot #GNUNET_YES to return all entries and then end (with NULL+NULL),
771  *                 #GNUNET_NO to monitor validation entries continuously
772  * @param timeout how long is the lookup allowed to take at most
773  * @param validation_callback function to call with the results
774  * @param validation_callback_cls closure for @a validation_callback
775  */
776 struct GNUNET_TRANSPORT_ValidationMonitoringContext *
777 GNUNET_TRANSPORT_monitor_validation_entries (const struct GNUNET_CONFIGURATION_Handle *cfg,
778                                              const struct GNUNET_PeerIdentity *peer,
779                                              int one_shot,
780                                              struct GNUNET_TIME_Relative timeout,
781                                              GNUNET_TRANSPORT_ValidationIterateCallback validation_callback,
782                                              void *validation_callback_cls);
783
784
785 /**
786  * Return information about all current pending validation operations
787  *
788  * @param vic handle for the request to cancel
789  */
790 void
791 GNUNET_TRANSPORT_monitor_validation_entries_cancel (struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic);
792
793
794 /* *********************** Blacklisting ************************ */
795
796 /**
797  * Handle for blacklisting peers.
798  */
799 struct GNUNET_TRANSPORT_Blacklist;
800
801
802 /**
803  * Function that decides if a connection is acceptable or not.
804  *
805  * @param cls closure
806  * @param pid peer to approve or disapproave
807  * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
808  */
809 typedef int
810 (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
811                                        const struct GNUNET_PeerIdentity *pid);
812
813
814 /**
815  * Install a blacklist callback.  The service will be queried for all
816  * existing connections as well as any fresh connections to check if
817  * they are permitted.  If the blacklisting callback is unregistered,
818  * all hosts that were denied in the past will automatically be
819  * whitelisted again.  Cancelling the blacklist handle is also the
820  * only way to re-enable connections from peers that were previously
821  * blacklisted.
822  *
823  * @param cfg configuration to use
824  * @param cb callback to invoke to check if connections are allowed
825  * @param cb_cls closure for @a cb
826  * @return NULL on error, otherwise handle for cancellation
827  */
828 struct GNUNET_TRANSPORT_Blacklist *
829 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
830                             GNUNET_TRANSPORT_BlacklistCallback cb,
831                             void *cb_cls);
832
833
834 /**
835  * Abort the blacklist.  Note that this function is the only way for
836  * removing a peer from the blacklist.
837  *
838  * @param br handle of the request that is to be cancelled
839  */
840 void
841 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
842
843
844 /**
845  * Handle for a plugin session state monitor.
846  */
847 struct GNUNET_TRANSPORT_PluginMonitor;
848
849 /**
850  * Abstract representation of a plugin's session.
851  * Corresponds to the `struct GNUNET_ATS_Session` within the TRANSPORT service.
852  */
853 struct GNUNET_TRANSPORT_PluginSession;
854
855
856 /**
857  * Possible states of a session in a plugin.
858  */
859 enum GNUNET_TRANSPORT_SessionState
860 {
861
862   /**
863    * The session was created (first call for each session object).
864    */
865   GNUNET_TRANSPORT_SS_INIT,
866
867   /**
868    * Initial session handshake is in progress.
869    */
870   GNUNET_TRANSPORT_SS_HANDSHAKE,
871
872   /**
873    * Session is fully UP.
874    */
875   GNUNET_TRANSPORT_SS_UP,
876
877   /**
878    * This is just an update about the session,
879    * the state did not change.
880    */
881   GNUNET_TRANSPORT_SS_UPDATE,
882
883   /**
884    * Session is being torn down and about to disappear.
885    * Last call for each session object.
886    */
887   GNUNET_TRANSPORT_SS_DONE
888
889 };
890
891
892 /**
893  * Information about a plugin's session.
894  */
895 struct GNUNET_TRANSPORT_SessionInfo
896 {
897
898   /**
899    * New state of the session.
900    */
901   enum GNUNET_TRANSPORT_SessionState state;
902
903   /**
904    * #GNUNET_YES if this is an inbound connection,
905    * #GNUNET_NO if this is an outbound connection,
906    * #GNUNET_SYSERR if connections of this plugin
907    *             are so fundamentally bidirectional
908    *             that they have no 'initiator'
909    */
910   int is_inbound;
911
912   /**
913    * Number of messages pending transmission for this session.
914    */
915   uint32_t num_msg_pending;
916
917   /**
918    * Number of bytes pending transmission for this session.
919    */
920   uint32_t num_bytes_pending;
921
922   /**
923    * Until when does this plugin refuse to receive to manage
924    * staying within the inbound quota?  ZERO if receive is
925    * active.
926    */
927   struct GNUNET_TIME_Absolute receive_delay;
928
929   /**
930    * At what time will this session timeout (unless activity
931    * happens)?
932    */
933   struct GNUNET_TIME_Absolute session_timeout;
934
935   /**
936    * Address used by the session.  Can be NULL if none is available.
937    */
938   const struct GNUNET_HELLO_Address *address;
939 };
940
941
942 /**
943  * Function called by the plugin with information about the
944  * current sessions managed by the plugin (for monitoring).
945  *
946  * @param cls closure
947  * @param session session handle this information is about,
948  *        NULL to indicate that we are "in sync" (initial
949  *        iteration complete)
950  * @param session_ctx storage location where the application
951  *        can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
952  *        and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
953  * @param info information about the state of the session,
954  *        NULL if @a session is also NULL and we are
955  *        merely signalling that the initial iteration is over;
956  *        NULL with @a session being non-NULL if the monitor
957  *        was being cancelled while sessions were active
958  */
959 typedef void
960 (*GNUNET_TRANSPORT_SessionMonitorCallback) (void *cls,
961                                             struct GNUNET_TRANSPORT_PluginSession *session,
962                                             void **session_ctx,
963                                             const struct GNUNET_TRANSPORT_SessionInfo *info);
964
965
966 /**
967  * Install a plugin session state monitor callback.  The callback
968  * will be notified whenever the session changes.
969  *
970  * @param cfg configuration to use
971  * @param cb callback to invoke on events
972  * @param cb_cls closure for @a cb
973  * @return NULL on error, otherwise handle for cancellation
974  */
975 struct GNUNET_TRANSPORT_PluginMonitor *
976 GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
977                                   GNUNET_TRANSPORT_SessionMonitorCallback cb,
978                                   void *cb_cls);
979
980
981 /**
982  * Cancel monitoring the plugin session state.  The callback will be
983  * called once for each session that is up with the "info" argument
984  * being NULL (this is just to enable client-side cleanup).
985  *
986  * @param pm handle of the request that is to be cancelled
987  */
988 void
989 GNUNET_TRANSPORT_monitor_plugins_cancel (struct GNUNET_TRANSPORT_PluginMonitor *pm);
990
991
992
993 #if 0                           /* keep Emacsens' auto-indent happy */
994 {
995 #endif
996 #ifdef __cplusplus
997 }
998 #endif
999
1000 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
1001 #endif
1002 /* end of gnunet_transport_service.h */