using tcp terminology for 3 way handshake: syn/syn_ack/ack
[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 SYN 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 SYN_ACK and go to #GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK.
500  * If we receive a 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 SYN message first.  We
505  * start with #GNUNET_TRANSPORT_PS_INIT_ATS.  If we get an address, we send
506  * the SYN message and go to state #GNUNET_TRANSPORT_PS_CONNECT_SENT.
507  * Once we receive a SYN_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
508  * (and notify everyone about the new connection and send
509  * back a 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  * SYN message.  If we receive a
517  * SYN_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED and nobody noticed that we had
518  * trouble; we also send a 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_SYN_SENT
524  * and send a SESSION_CONNECT.  If we get a 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 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 'SYN' 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 'SYN_ACK'
541  * (because we have an acceptable address), we send the 'SYN_ACK'
542  * and set the 'send_connect_ack' to 2.  If we then receive a
543  * '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 SYN message to other peer, waiting for SYN_ACK
561    */
562   GNUNET_TRANSPORT_PS_SYN_SENT,
563
564   /**
565    * Received a SYN, asking ATS about address suggestions.
566    */
567   GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
568
569   /**
570    * SYN request from other peer was SYN_ACK'ed, waiting for ACK.
571    */
572   GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
573
574   /**
575    * Got our SYN_ACK/ACK, connection is up.
576    */
577   GNUNET_TRANSPORT_PS_CONNECTED,
578
579   /**
580    * Connection got into trouble, rest of the system still believes
581    * it to be up, but we're getting a new address from ATS.
582    */
583   GNUNET_TRANSPORT_PS_RECONNECT_ATS,
584
585   /**
586    * Sent SYN over new address (either by ATS telling us to switch
587    * addresses or from RECONNECT_ATS); if this fails, we need to tell
588    * the rest of the system about a disconnect.
589    */
590   GNUNET_TRANSPORT_PS_RECONNECT_SENT,
591
592   /**
593    * We have some primary connection, but ATS suggested we switch
594    * to some alternative; we now sent a SYN message for the
595    * alternative session to the other peer and waiting for a
596    * SYN_ACK to make this our primary connection.
597    */
598   GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT,
599
600   /**
601    * Disconnect in progress (we're sending the DISCONNECT message to the
602    * other peer; after that is finished, the state will be cleaned up).
603    */
604   GNUNET_TRANSPORT_PS_DISCONNECT,
605
606   /**
607    * We're finished with the disconnect; and are cleaning up the state
608    * now!  We put the struct into this state when we are really in the
609    * task that calls 'free' on it and are about to remove the record
610    * from the map.  We should never find a 'struct NeighbourMapEntry'
611    * in this state in the map.  Accessing a 'struct NeighbourMapEntry'
612    * in this state virtually always means using memory that has been
613    * freed (the exception being the cleanup code in #free_neighbour()).
614    */
615   GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED
616 };
617
618
619 /**
620  * Convert a transport state to a human readable string.
621  *
622  * @param state the state
623  */
624 const char *
625 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state);
626
627
628 /**
629  * Check if a state is defined as connected
630  *
631  * @param state the state value
632  * @return #GNUNET_YES or #GNUNET_NO
633  */
634 int
635 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state);
636
637
638 /**
639  * Handle for a #GNUNET_TRANSPORT_monitor_peers operation.
640  */
641 struct GNUNET_TRANSPORT_PeerMonitoringContext;
642
643
644 /**
645  * Function to call with information about a peer
646  *
647  * If one_shot was set to #GNUNET_YES to iterate over all peers once,
648  * a final call with NULL for peer and address will follow when done.
649  * In this case state and timeout do not contain valid values.
650  *
651  * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called from
652  * within this function!
653  *
654  *
655  * @param cls closure
656  * @param peer peer this update is about,
657  *      NULL if this is the final last callback for a iteration operation
658  * @param address address, NULL for disconnect notification in monitor mode
659  * @param state current state this peer is in
660  * @param state_timeout timeout for the current state of the peer
661  */
662 typedef void
663 (*GNUNET_TRANSPORT_PeerIterateCallback) (void *cls,
664                                          const struct GNUNET_PeerIdentity *peer,
665                                          const struct GNUNET_HELLO_Address *address,
666                                          enum GNUNET_TRANSPORT_PeerState state,
667                                          struct GNUNET_TIME_Absolute state_timeout);
668
669
670 /**
671  * Return information about a specific peer or all peers currently known to
672  * transport service once or in monitoring mode. To obtain information about
673  * a specific peer, a peer identity can be passed. To obtain information about
674  * all peers currently known to transport service, NULL can be passed as peer
675  * identity.
676  *
677  * For each peer, the callback is called with information about the address used
678  * to communicate with this peer, the state this peer is currently in and the
679  * the current timeout for this state.
680  *
681  * Upon completion, the 'GNUNET_TRANSPORT_PeerIterateCallback' is called one
682  * more time with 'NULL'. After this, the operation must no longer be
683  * explicitly canceled.
684  *
685  * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
686  * the peer_callback!
687  *
688  * @param cfg configuration to use
689  * @param peer a specific peer identity to obtain information for,
690  *      NULL for all peers
691  * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
692  *                 #GNUNET_NO to monitor peers continuously
693  * @param timeout how long is the lookup allowed to take at most
694  * @param peer_callback function to call with the results
695  * @param peer_callback_cls closure for @a peer_callback
696  */
697 struct GNUNET_TRANSPORT_PeerMonitoringContext *
698 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
699                                 const struct GNUNET_PeerIdentity *peer,
700                                 int one_shot,
701                                 struct GNUNET_TIME_Relative timeout,
702                                 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
703                                 void *peer_callback_cls);
704
705
706 /**
707  * Cancel request to monitor peers
708  *
709  * @param pic handle for the request to cancel
710  */
711 void
712 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic);
713
714
715 /**
716  * Handle for a #GNUNET_TRANSPORT_monitor_validation_entries() operation.
717  */
718 struct GNUNET_TRANSPORT_ValidationMonitoringContext;
719
720
721 /**
722  * Current state of a validation process.
723  *
724  * FIXME: what state is used to indicate that a validation
725  * was successful? If that is clarified/determined, "UGH" in
726  * ~gnunet-peerinfo-gtk.c:1103 should be resolved.
727  */
728 enum GNUNET_TRANSPORT_ValidationState
729 {
730   /**
731    * Undefined state
732    *
733    * Used for final callback indicating operation done
734    */
735   GNUNET_TRANSPORT_VS_NONE,
736
737   /**
738    * Fresh validation entry
739    *
740    * Entry was just created, no validation process was executed
741    */
742   GNUNET_TRANSPORT_VS_NEW,
743
744   /**
745    * Updated validation entry
746    *
747    * This is an update for an existing validation entry
748    */
749   GNUNET_TRANSPORT_VS_UPDATE,
750
751   /**
752    * Timeout for validation entry
753    *
754    * A timeout occured during the validation process
755    */
756   GNUNET_TRANSPORT_VS_TIMEOUT,
757
758   /**
759    * Validation entry is removed
760    *
761    * The validation entry is getting removed due to a failed validation
762    */
763   GNUNET_TRANSPORT_VS_REMOVE
764 };
765
766
767 /**
768  * Function to call with validation information about a peer
769  *
770  * This function is called by the transport validation monitoring api to
771  * indicate a change to a validation entry. The information included represent
772  * the current state of the validation entry,
773  *
774  * If the monitoring was called with one_shot=GNUNET_YES, a final callback
775  * with peer==NULL and address==NULL is executed.
776  *
777  * @param cls closure
778  * @param peer peer this update is about,
779  *      NULL if this is the final last callback for a iteration operation
780  * @param address address,
781  *      NULL for disconnect notification in monitor mode
782  * @param last_validation when was this address last validated
783  * @param valid_until when does this address expire
784  * @param next_validation time of the next validation operation
785  * @param state state in the validation state machine
786  */
787 typedef void
788 (*GNUNET_TRANSPORT_ValidationIterateCallback) (void *cls,
789                                                const struct GNUNET_PeerIdentity *peer,
790                                                const struct GNUNET_HELLO_Address *address,
791                                                struct GNUNET_TIME_Absolute last_validation,
792                                                struct GNUNET_TIME_Absolute valid_until,
793                                                struct GNUNET_TIME_Absolute next_validation,
794                                                enum GNUNET_TRANSPORT_ValidationState state);
795
796
797 /**
798  * Convert validation state to human-readable string.
799  *
800  * @param state the state value
801  * @return corresponding string
802  */
803 const char *
804 GNUNET_TRANSPORT_vs2s (enum GNUNET_TRANSPORT_ValidationState state);
805
806
807 /**
808  * Return information about pending address validation operations for a specific
809  * or all peers
810  *
811  * @param cfg configuration to use
812  * @param peer a specific peer identity to obtain validation entries for,
813  *      NULL for all peers
814  * @param one_shot #GNUNET_YES to return all entries and then end (with NULL+NULL),
815  *                 #GNUNET_NO to monitor validation entries continuously
816  * @param timeout how long is the lookup allowed to take at most
817  * @param validation_callback function to call with the results
818  * @param validation_callback_cls closure for @a validation_callback
819  */
820 struct GNUNET_TRANSPORT_ValidationMonitoringContext *
821 GNUNET_TRANSPORT_monitor_validation_entries (const struct GNUNET_CONFIGURATION_Handle *cfg,
822                                              const struct GNUNET_PeerIdentity *peer,
823                                              int one_shot,
824                                              struct GNUNET_TIME_Relative timeout,
825                                              GNUNET_TRANSPORT_ValidationIterateCallback validation_callback,
826                                              void *validation_callback_cls);
827
828
829 /**
830  * Return information about all current pending validation operations
831  *
832  * @param vic handle for the request to cancel
833  */
834 void
835 GNUNET_TRANSPORT_monitor_validation_entries_cancel (struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic);
836
837
838 /* *********************** Blacklisting ************************ */
839
840 /**
841  * Handle for blacklisting peers.
842  */
843 struct GNUNET_TRANSPORT_Blacklist;
844
845
846 /**
847  * Function that decides if a connection is acceptable or not.
848  *
849  * @param cls closure
850  * @param pid peer to approve or disapproave
851  * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
852  */
853 typedef int
854 (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
855                                        const struct GNUNET_PeerIdentity *pid);
856
857
858 /**
859  * Install a blacklist callback.  The service will be queried for all
860  * existing connections as well as any fresh connections to check if
861  * they are permitted.  If the blacklisting callback is unregistered,
862  * all hosts that were denied in the past will automatically be
863  * whitelisted again.  Cancelling the blacklist handle is also the
864  * only way to re-enable connections from peers that were previously
865  * blacklisted.
866  *
867  * @param cfg configuration to use
868  * @param cb callback to invoke to check if connections are allowed
869  * @param cb_cls closure for @a cb
870  * @return NULL on error, otherwise handle for cancellation
871  */
872 struct GNUNET_TRANSPORT_Blacklist *
873 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
874                             GNUNET_TRANSPORT_BlacklistCallback cb,
875                             void *cb_cls);
876
877
878 /**
879  * Abort the blacklist.  Note that this function is the only way for
880  * removing a peer from the blacklist.
881  *
882  * @param br handle of the request that is to be cancelled
883  */
884 void
885 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
886
887
888
889 #if 0                           /* keep Emacsens' auto-indent happy */
890 {
891 #endif
892 #ifdef __cplusplus
893 }
894 #endif
895
896 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
897 #endif
898 /* end of gnunet_transport_service.h */
899
900