make some functions static, ensure shutdown tasks could be run repeatedly if 1st...
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_peer.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2017 GNUnet e.V.
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 cadet/gnunet-service-cadet_peer.h
23  * @brief Information we track per peer.
24  * @author Bartlomiej Polot
25  * @author Christian Grothoff
26  */
27 #ifndef GNUNET_SERVICE_CADET_PEER_H
28 #define GNUNET_SERVICE_CADET_PEER_H
29
30 #include "gnunet-service-cadet.h"
31 #include "gnunet_hello_lib.h"
32
33
34 /**
35  * Get the static string for a peer ID.
36  *
37  * @param peer Peer.
38  *
39  * @return Static string for it's ID.
40  */
41 const char *
42 GCP_2s (const struct CadetPeer *peer);
43
44
45 /**
46  * Retrieve the CadetPeer stucture associated with the
47  * peer. Optionally create one and insert it in the appropriate
48  * structures if the peer is not known yet.
49  *
50  * @param peer_id Full identity of the peer.
51  * @param create #GNUNET_YES if a new peer should be created if unknown.
52  *               #GNUNET_NO to return NULL if peer is unknown.
53  * @return Existing or newly created peer structure.
54  *         NULL if unknown and not requested @a create
55  */
56 struct CadetPeer *
57 GCP_get (const struct GNUNET_PeerIdentity *peer_id,
58          int create);
59
60
61 /**
62  * Calculate how desirable a path is for @a cp if
63  * @a cp is at offset @a off in the path.
64  *
65  * @param cp a peer reachable via a path
66  * @param off offset of @a cp in a path
67  * @return score how useful a path is to reach @a cp,
68  *         positive scores mean path is more desirable
69  */
70 double
71 GCP_get_desirability_of_path (struct CadetPeer *cp,
72                               unsigned int off);
73
74
75 /**
76  * Obtain the peer identity for a `struct CadetPeer`.
77  *
78  * @param cp our peer handle
79  * @return the peer identity
80  */
81 const struct GNUNET_PeerIdentity *
82 GCP_get_id (struct CadetPeer *cp);
83
84
85 /**
86  * Iterate over all known peers.
87  *
88  * @param iter Iterator.
89  * @param cls Closure for @c iter.
90  */
91 void
92 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
93                  void *cls);
94
95
96 /**
97  * Count the number of known paths toward the peer.
98  *
99  * @param cp Peer to get path info.
100  * @return Number of known paths.
101  */
102 unsigned int
103 GCP_count_paths (const struct CadetPeer *cp);
104
105
106 /**
107  * Drop all paths owned by this peer, and do not
108  * allow new ones to be added: We are shutting down.
109  *
110  * @param cp peer to drop paths to
111  */
112 void
113 GCP_drop_owned_paths (struct CadetPeer *cp);
114
115
116 /**
117  * Peer path iterator.
118  *
119  * @param cls Closure.
120  * @param path Path itself
121  * @param off offset of the target peer in @a path
122  * @return #GNUNET_YES if should keep iterating.
123  *         #GNUNET_NO otherwise.
124  */
125 typedef int
126 (*GCP_PathIterator) (void *cls,
127                      struct CadetPeerPath *path,
128                      unsigned int off);
129
130
131 /**
132  * Iterate over the paths to a peer.
133  *
134  * @param cp Peer to get path info.
135  * @param callback Function to call for every path.
136  * @param callback_cls Closure for @a callback.
137  * @return Number of iterated paths.
138  */
139 unsigned int
140 GCP_iterate_paths (struct CadetPeer *cp,
141                    GCP_PathIterator callback,
142                    void *callback_cls);
143
144
145 /**
146  * Iterate over the paths to @a peer where
147  * @a peer is at distance @a dist from us.
148  *
149  * @param cp Peer to get path info.
150  * @param dist desired distance of @a peer to us on the path
151  * @param callback Function to call for every path.
152  * @param callback_cls Closure for @a callback.
153  * @return Number of iterated paths.
154  */
155 unsigned int
156 GCP_iterate_paths_at (struct CadetPeer *cp,
157                       unsigned int dist,
158                       GCP_PathIterator callback,
159                       void *callback_cls);
160
161
162 /**
163  * Remove an entry from the DLL of all of the paths that this peer is on.
164  *
165  * @param cp peer to modify
166  * @param entry an entry on a path
167  * @param off offset of this peer on the path
168  */
169 void
170 GCP_path_entry_remove (struct CadetPeer *cp,
171                        struct CadetPeerPathEntry *entry,
172                        unsigned int off);
173
174
175 /**
176  * Add an entry to the DLL of all of the paths that this peer is on.
177  *
178  * @param cp peer to modify
179  * @param entry an entry on a path
180  * @param off offset of this peer on the path
181  */
182 void
183 GCP_path_entry_add (struct CadetPeer *cp,
184                     struct CadetPeerPathEntry *entry,
185                     unsigned int off);
186
187
188 /**
189  * Get the tunnel towards a peer.
190  *
191  * @param cp Peer to get from.
192  * @param create #GNUNET_YES to create a tunnel if we do not have one
193  * @return Tunnel towards peer.
194  */
195 struct CadetTunnel *
196 GCP_get_tunnel (struct CadetPeer *cp,
197                 int create);
198
199
200 /**
201  * The tunnel to the given peer no longer exists, remove it from our
202  * data structures, and possibly clean up the peer itself.
203  *
204  * @param cp the peer affected
205  * @param t the dead tunnel
206  */
207 void
208 GCP_drop_tunnel (struct CadetPeer *cp,
209                  struct CadetTunnel *t);
210
211
212 /**
213  * Try adding a @a path to this @a cp.  If the peer already
214  * has plenty of paths, return NULL.
215  *
216  * @param cp peer to which the @a path leads to
217  * @param path a path looking for an owner; may not be fully initialized yet!
218  * @param off offset of @a cp in @a path
219  * @param force for attaching the path
220  * @return NULL if this peer does not care to become a new owner,
221  *         otherwise the node in the peer's path heap for the @a path.
222  */
223 struct GNUNET_CONTAINER_HeapNode *
224 GCP_attach_path (struct CadetPeer *cp,
225                  struct CadetPeerPath *path,
226                  unsigned int off,
227                  int force);
228
229
230 /**
231  * This peer can no longer own @a path as the path
232  * has been extended and a peer further down the line
233  * is now the new owner.
234  *
235  * @param cp old owner of the @a path
236  * @param path path where the ownership is lost
237  * @param hn note in @a cp's path heap that must be deleted
238  */
239 void
240 GCP_detach_path (struct CadetPeer *cp,
241                  struct CadetPeerPath *path,
242                  struct GNUNET_CONTAINER_HeapNode *hn);
243
244
245 /**
246  * Add a @a connection to this @a cp.
247  *
248  * @param cp peer via which the @a connection goes
249  * @param cc the connection to add
250  */
251 void
252 GCP_add_connection (struct CadetPeer *cp,
253                     struct CadetConnection *cc);
254
255
256 /**
257  * Remove a @a connection that went via this @a cp.
258  *
259  * @param cp peer via which the @a connection went
260  * @param cc the connection to remove
261  */
262 void
263 GCP_remove_connection (struct CadetPeer *cp,
264                        struct CadetConnection *cc);
265
266
267 /**
268  * We got a HELLO for a @a cp, remember it, and possibly
269  * trigger adequate actions (like trying to connect).
270  *
271  * @param cp the peer we got a HELLO for
272  * @param hello the HELLO to remember
273  */
274 void
275 GCP_set_hello (struct CadetPeer *cp,
276                const struct GNUNET_HELLO_Message *hello);
277
278
279 /**
280  * Clean up all entries about all peers.
281  * Must only be called after all tunnels, CORE-connections and
282  * connections are down.
283  */
284 void
285 GCP_destroy_all_peers (void);
286
287
288 /**
289  * Data structure used to track whom we have to notify about changes
290  * in our ability to transmit to a given peer.
291  *
292  * All queue managers will be given equal chance for sending messages
293  * to @a cp.  This construct this guarantees fairness for access to @a
294  * cp among the different message queues.  Each connection or route
295  * will have its respective message queue managers for each direction.
296  */
297 struct GCP_MessageQueueManager;
298
299
300 /**
301  * Function to call with updated message queue object.
302  *
303  * @param cls closure
304  * @param available #GNUNET_YES if sending is now possible,
305  *                  #GNUNET_NO if sending is no longer possible
306  *                  #GNUNET_SYSERR if sending is no longer possible
307  *                                 and the last envelope was discarded
308  */
309 typedef void
310 (*GCP_MessageQueueNotificationCallback)(void *cls,
311                                         int available);
312
313
314 /**
315  * Start message queue change notifications.  Will create a new slot
316  * to manage the message queue to the given @a cp.
317  *
318  * @param cp peer to notify for
319  * @param cb function to call if mq becomes available or unavailable
320  * @param cb_cls closure for @a cb
321  * @return handle to cancel request
322  */
323 struct GCP_MessageQueueManager *
324 GCP_request_mq (struct CadetPeer *cp,
325                 GCP_MessageQueueNotificationCallback cb,
326                 void *cb_cls);
327
328
329 /**
330  * Test if @a cp has a core-level connection
331  *
332  * @param cp peer to test
333  * @return #GNUNET_YES if @a cp has a core-level connection
334  */
335 int
336 GCP_has_core_connection (struct CadetPeer *cp);
337
338
339 /**
340  * Send the message in @a env via a @a mqm.  Must only be called at
341  * most once after the respective
342  * #GCP_MessageQueueNotificationCallback was called with `available`
343  * set to #GNUNET_YES, and not after the callback was called with
344  * `available` set to #GNUNET_NO or #GNUNET_SYSERR.
345  *
346  * @param mqm message queue manager for the transmission
347  * @param env envelope with the message to send; must NOT
348  *            yet have a #GNUNET_MQ_notify_sent() callback attached to it
349  */
350 void
351 GCP_send (struct GCP_MessageQueueManager *mqm,
352           struct GNUNET_MQ_Envelope *env);
353
354
355 /**
356  * Send the message in @a env to @a cp, overriding queueing logic.
357  * This function should only be used to send error messages outside
358  * of flow and congestion control, similar to ICMP.  Note that
359  * the envelope may be silently discarded as well.
360  *
361  * @param cp peer to send the message to
362  * @param env envelope with the message to send
363  */
364 void
365 GCP_send_ooo (struct CadetPeer *cp,
366               struct GNUNET_MQ_Envelope *env);
367
368
369 /**
370  * Stops message queue change notifications and sends a last message.
371  * In practice, this is implemented by sending that @a last_env
372  * message immediately (if any), ignoring queue order.
373  *
374  * @param mqm handle matching request to cancel
375  * @param last_env final message to transmit, or NULL
376  */
377 void
378 GCP_request_mq_cancel (struct GCP_MessageQueueManager *mqm,
379                        struct GNUNET_MQ_Envelope *last_env);
380
381
382 /**
383  * Set the message queue to @a mq for peer @a cp and notify watchers.
384  *
385  * @param cp peer to modify
386  * @param mq message queue to set (can be NULL)
387  */
388 void
389 GCP_set_mq (struct CadetPeer *cp,
390             struct GNUNET_MQ_Handle *mq);
391
392
393 #endif