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