aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/gnunet-service-cadet_peer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/cadet/gnunet-service-cadet_peer.h')
-rw-r--r--src/cadet/gnunet-service-cadet_peer.h516
1 files changed, 213 insertions, 303 deletions
diff --git a/src/cadet/gnunet-service-cadet_peer.h b/src/cadet/gnunet-service-cadet_peer.h
index 1e206e10f..baa87ea87 100644
--- a/src/cadet/gnunet-service-cadet_peer.h
+++ b/src/cadet/gnunet-service-cadet_peer.h
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V. 3 Copyright (C) 2001-2017 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -20,464 +20,374 @@
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet_peer.h 22 * @file cadet/gnunet-service-cadet_peer.h
23 * @brief cadet service; dealing with remote peers 23 * @brief Information we track per peer.
24 * @author Bartlomiej Polot 24 * @author Bartlomiej Polot
25 * 25 * @author Christian Grothoff
26 * All functions in this file should use the prefix GMP (Gnunet Cadet Peer)
27 */ 26 */
28
29#ifndef GNUNET_SERVICE_CADET_PEER_H 27#ifndef GNUNET_SERVICE_CADET_PEER_H
30#define GNUNET_SERVICE_CADET_PEER_H 28#define GNUNET_SERVICE_CADET_PEER_H
31 29
32#ifdef __cplusplus 30#include "gnunet-service-cadet.h"
33extern "C" 31#include "gnunet_hello_lib.h"
34{
35#if 0 /* keep Emacsens' auto-indent happy */
36}
37#endif
38#endif
39
40#include "platform.h"
41#include "gnunet_util_lib.h"
42#include "cadet_path.h"
43 32
44/**
45 * Struct containing all information regarding a given peer
46 */
47struct CadetPeer;
48
49/**
50 * Handle to queued messages on a peer level.
51 */
52struct CadetPeerQueue;
53
54#include "gnunet-service-cadet_connection.h"
55
56
57/**
58 * Callback called when a queued message is sent.
59 *
60 * @param cls Closure.
61 * @param c Connection this message was on.
62 * @param fwd Was this a FWD going message?
63 * @param sent Was it really sent? (Could have been canceled)
64 * @param type Type of message sent.
65 * @param payload_type Type of payload, if applicable.
66 * @param pid Message ID, or 0 if not applicable (create, destroy, etc).
67 * @param size Size of the message.
68 * @param wait Time spent waiting for core (only the time for THIS message)
69 */
70typedef void
71(*GCP_sent) (void *cls,
72 struct CadetConnection *c,
73 int fwd,
74 int sent,
75 uint16_t type,
76 uint16_t payload_type,
77 struct CadetEncryptedMessageIdentifier pid,
78 size_t size,
79 struct GNUNET_TIME_Relative wait);
80 33
81/** 34/**
82 * Peer path iterator. 35 * Get the static string for a peer ID.
83 * 36 *
84 * @param cls Closure. 37 * @param peer Peer.
85 * @param peer Peer this path is towards.
86 * @param path Path itself
87 * @return #GNUNET_YES if should keep iterating.
88 * #GNUNET_NO otherwise.
89 */
90typedef int
91(*GCP_path_iterator) (void *cls,
92 struct CadetPeer *peer,
93 struct CadetPeerPath *path);
94
95
96/******************************************************************************/
97/******************************** API ***********************************/
98/******************************************************************************/
99
100/**
101 * Initialize peer subsystem.
102 * 38 *
103 * @param c Configuration. 39 * @return Static string for it's ID.
104 */
105void
106GCP_init (const struct GNUNET_CONFIGURATION_Handle *c);
107
108/**
109 * Shut down the peer subsystem.
110 */ 40 */
111void 41const char *
112GCP_shutdown (void); 42GCP_2s (const struct CadetPeer *peer);
113 43
114 44
115/** 45/**
116 * Retrieve the CadetPeer stucture associated with the peer. Optionally create 46 * Retrieve the CadetPeer stucture associated with the
117 * one and insert it in the appropriate structures if the peer is not known yet. 47 * peer. Optionally create one and insert it in the appropriate
48 * structures if the peer is not known yet.
118 * 49 *
119 * @param peer_id Full identity of the peer. 50 * @param peer_id Full identity of the peer.
120 * @param create #GNUNET_YES if a new peer should be created if unknown. 51 * @param create #GNUNET_YES if a new peer should be created if unknown.
121 * #GNUNET_NO otherwise. 52 * #GNUNET_NO to return NULL if peer is unknown.
122 *
123 * @return Existing or newly created peer structure. 53 * @return Existing or newly created peer structure.
124 * NULL if unknown and not requested @a create 54 * NULL if unknown and not requested @a create
125 */ 55 */
126struct CadetPeer * 56struct CadetPeer *
127GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create); 57GCP_get (const struct GNUNET_PeerIdentity *peer_id,
58 int create);
128 59
129 60
130/** 61/**
131 * Retrieve the CadetPeer stucture associated with the peer. Optionally create 62 * Calculate how desirable a path is for @a cp if
132 * one and insert it in the appropriate structures if the peer is not known yet. 63 * @a cp is at offset @a off in the path.
133 *
134 * @param peer Short identity of the peer.
135 * @param create #GNUNET_YES if a new peer should be created if unknown.
136 * #GNUNET_NO otherwise.
137 * 64 *
138 * @return Existing or newly created peer structure. 65 * @param cp a peer reachable via a path
139 * NULL if unknown and not requested @a create 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
140 */ 69 */
141struct CadetPeer * 70double
142GCP_get_short (const GNUNET_PEER_Id peer, int create); 71GCP_get_desirability_of_path (struct CadetPeer *cp,
72 unsigned int off);
143 73
144 74
145/** 75/**
146 * Try to establish a new connection to this peer (in its tunnel). 76 * Obtain the peer identity for a `struct CadetPeer`.
147 * If the peer doesn't have any path to it yet, try to get one.
148 * If the peer already has some path, send a CREATE CONNECTION towards it.
149 *
150 * @param peer Peer to connect to.
151 */
152void
153GCP_connect (struct CadetPeer *peer);
154
155/**
156 * @brief Send a message to another peer (using CORE).
157 * 77 *
158 * @param peer Peer towards which to queue the message. 78 * @param cp our peer handle
159 * @param message Message to send. 79 * @return the peer identity
160 * @param payload_type Type of the message's payload, for debug messages.
161 * 0 if the message is a retransmission (unknown payload).
162 * UINT16_MAX if the message does not have payload.
163 * @param payload_id ID of the payload (MID, ACK #, etc)
164 * @param c Connection this message belongs to (can be NULL).
165 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
166 * @param cont Continuation to be called once CORE has sent the message.
167 * @param cont_cls Closure for @c cont.
168 */ 80 */
169struct CadetPeerQueue * 81const struct GNUNET_PeerIdentity *
170GCP_send (struct CadetPeer *peer, 82GCP_get_id (struct CadetPeer *cp);
171 const struct GNUNET_MessageHeader *message,
172 uint16_t payload_type,
173 struct CadetEncryptedMessageIdentifier payload_id,
174 struct CadetConnection *c,
175 int fwd,
176 GCP_sent cont,
177 void *cont_cls);
178 83
179/**
180 * Cancel sending a message. Message must have been sent with
181 * #GCP_send before. May not be called after the notify sent
182 * callback has been called.
183 *
184 * It does NOT call the continuation given to #GCP_send.
185 *
186 * @param q Queue handle to cancel
187 */
188void
189GCP_send_cancel (struct CadetPeerQueue *q);
190 84
191/** 85/**
192 * Set tunnel. 86 * Iterate over all known peers.
193 * 87 *
194 * @param peer Peer. 88 * @param iter Iterator.
195 * @param t Tunnel. 89 * @param cls Closure for @c iter.
196 */ 90 */
197void 91void
198GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t); 92GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
93 void *cls);
199 94
200 95
201/** 96/**
202 * Check whether there is a direct (core level) connection to peer. 97 * Count the number of known paths toward the peer.
203 *
204 * @param peer Peer to check.
205 * 98 *
206 * @return #GNUNET_YES if there is a direct connection. 99 * @param cp Peer to get path info.
100 * @return Number of known paths.
207 */ 101 */
208int 102unsigned int
209GCP_is_neighbor (const struct CadetPeer *peer); 103GCP_count_paths (const struct CadetPeer *cp);
210 104
211 105
212/** 106/**
213 * Create and initialize a new tunnel towards a peer, in case it has none. 107 * Drop all paths owned by this peer, and do not
108 * allow new ones to be added: We are shutting down.
214 * 109 *
215 * Does not generate any traffic, just creates the local data structures. 110 * @param cp peer to drop paths to
216 *
217 * @param peer Peer towards which to create the tunnel.
218 */ 111 */
219void 112void
220GCP_add_tunnel (struct CadetPeer *peer); 113GCP_drop_owned_paths (struct CadetPeer *cp);
221 114
222 115
223/** 116/**
224 * Add a connection to a neighboring peer. 117 * Peer path iterator.
225 *
226 * Store that the peer is the first hop of the connection in one
227 * direction and that on peer disconnect the connection must be
228 * notified and destroyed, for it will no longer be valid.
229 * 118 *
230 * @param peer Peer to add connection to. 119 * @param cls Closure.
231 * @param c Connection to add. 120 * @param path Path itself
232 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor 121 * @param off offset of the target peer in @a path
122 * @return #GNUNET_YES if should keep iterating.
123 * #GNUNET_NO otherwise.
233 */ 124 */
234void 125typedef int
235GCP_add_connection (struct CadetPeer *peer, 126(*GCP_PathIterator) (void *cls,
236 struct CadetConnection *c, 127 struct CadetPeerPath *path,
237 int pred); 128 unsigned int off);
238 129
239 130
240/** 131/**
241 * Add the path to the peer and update the path used to reach it in case this 132 * Iterate over the paths to a peer.
242 * is the shortest.
243 *
244 * @param peer Destination peer to add the path to.
245 * @param path New path to add. Last peer must be the peer in arg 1.
246 * Path will be either used of freed if already known.
247 * @param trusted Do we trust that this path is real?
248 * 133 *
249 * @return path if path was taken, pointer to existing duplicate if exists 134 * @param cp Peer to get path info.
250 * NULL on error. 135 * @param callback Function to call for every path.
136 * @param callback_cls Closure for @a callback.
137 * @return Number of iterated paths.
251 */ 138 */
252struct CadetPeerPath * 139unsigned int
253GCP_add_path (struct CadetPeer *peer, 140GCP_iterate_paths (struct CadetPeer *cp,
254 struct CadetPeerPath *p, 141 GCP_PathIterator callback,
255 int trusted); 142 void *callback_cls);
256 143
257 144
258/** 145/**
259 * Add the path to the origin peer and update the path used to reach it in case 146 * Iterate over the paths to @a peer where
260 * this is the shortest. 147 * @a peer is at distance @a dist from us.
261 * The path is given in peer_info -> destination, therefore we turn the path
262 * upside down first.
263 *
264 * @param peer Peer to add the path to, being the origin of the path.
265 * @param path New path to add after being inversed.
266 * Path will be either used or freed.
267 * @param trusted Do we trust that this path is real?
268 * 148 *
269 * @return path if path was taken, pointer to existing duplicate if exists 149 * @param cp Peer to get path info.
270 * NULL on error. 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.
271 */ 154 */
272struct CadetPeerPath * 155unsigned int
273GCP_add_path_to_origin (struct CadetPeer *peer, 156GCP_iterate_paths_at (struct CadetPeer *cp,
274 struct CadetPeerPath *path, 157 unsigned int dist,
275 int trusted); 158 GCP_PathIterator callback,
159 void *callback_cls);
160
276 161
277/** 162/**
278 * Adds a path to the info of all the peers in the path 163 * Remove an entry from the DLL of all of the paths that this peer is on.
279 * 164 *
280 * @param p Path to process. 165 * @param cp peer to modify
281 * @param confirmed Whether we know if the path works or not. 166 * @param entry an entry on a path
167 * @param off offset of this peer on the path
282 */ 168 */
283void 169void
284GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed); 170GCP_path_entry_remove (struct CadetPeer *cp,
171 struct CadetPeerPathEntry *entry,
172 unsigned int off);
285 173
286 174
287/** 175/**
288 * Remove any path to the peer that has the extact same peers as the one given. 176 * Add an entry to the DLL of all of the paths that this peer is on.
289 * 177 *
290 * @param peer Peer to remove the path from. 178 * @param cp peer to modify
291 * @param path Path to remove. Is always destroyed . 179 * @param entry an entry on a path
180 * @param off offset of this peer on the path
292 */ 181 */
293void 182void
294GCP_remove_path (struct CadetPeer *peer, 183GCP_path_entry_add (struct CadetPeer *cp,
295 struct CadetPeerPath *path); 184 struct CadetPeerPathEntry *entry,
185 unsigned int off);
296 186
297 187
298/** 188/**
299 * Check that we are aware of a connection from a neighboring peer. 189 * Get the tunnel towards a peer.
300 * 190 *
301 * @param peer Peer to the connection is with 191 * @param cp Peer to get from.
302 * @param c Connection that should be in the map with this peer. 192 * @param create #GNUNET_YES to create a tunnel if we do not have one
193 * @return Tunnel towards peer.
303 */ 194 */
304void 195struct CadetTunnel *
305GCP_check_connection (const struct CadetPeer *peer, 196GCP_get_tunnel (struct CadetPeer *cp,
306 const struct CadetConnection *c); 197 int create);
307 198
308 199
309/** 200/**
310 * Remove a connection from a neighboring peer. 201 * The tunnel to the given peer no longer exists, remove it from our
202 * data structures, and possibly clean up the peer itself.
311 * 203 *
312 * @param peer Peer to remove connection from. 204 * @param cp the peer affected
313 * @param c Connection to remove. 205 * @param t the dead tunnel
314 */ 206 */
315void 207void
316GCP_remove_connection (struct CadetPeer *peer, 208GCP_drop_tunnel (struct CadetPeer *cp,
317 const struct CadetConnection *c); 209 struct CadetTunnel *t);
318 210
319 211
320/** 212/**
321 * Start the DHT search for new paths towards the peer: we don't have 213 * Try adding a @a path to this @a cp. If the peer already
322 * enough good connections. 214 * has plenty of paths, return NULL.
323 * 215 *
324 * @param peer Destination peer. 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.
325 */ 222 */
326void 223struct GNUNET_CONTAINER_HeapNode *
327GCP_start_search (struct CadetPeer *peer); 224GCP_attach_path (struct CadetPeer *cp,
225 struct CadetPeerPath *path,
226 unsigned int off,
227 int force);
328 228
329 229
330/** 230/**
331 * Stop the DHT search for new paths towards the peer: we already have 231 * This peer can no longer own @a path as the path
332 * enough good connections. 232 * has been extended and a peer further down the line
233 * is now the new owner.
333 * 234 *
334 * @param peer Destination peer. 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
335 */ 238 */
336void 239void
337GCP_stop_search (struct CadetPeer *peer); 240GCP_detach_path (struct CadetPeer *cp,
241 struct CadetPeerPath *path,
242 struct GNUNET_CONTAINER_HeapNode *hn);
338 243
339 244
340/** 245/**
341 * Get the Full ID of a peer. 246 * Add a @a connection to this @a cp.
342 * 247 *
343 * @param peer Peer to get from. 248 * @param cp peer via which the @a connection goes
344 * 249 * @param cc the connection to add
345 * @return Full ID of peer.
346 */ 250 */
347const struct GNUNET_PeerIdentity * 251void
348GCP_get_id (const struct CadetPeer *peer); 252GCP_add_connection (struct CadetPeer *cp,
253 struct CadetConnection *cc);
349 254
350 255
351/** 256/**
352 * Get the Short ID of a peer. 257 * Remove a @a connection that went via this @a cp.
353 *
354 * @param peer Peer to get from.
355 * 258 *
356 * @return Short ID of peer. 259 * @param cp peer via which the @a connection went
260 * @param cc the connection to remove
357 */ 261 */
358GNUNET_PEER_Id 262void
359GCP_get_short_id (const struct CadetPeer *peer); 263GCP_remove_connection (struct CadetPeer *cp,
264 struct CadetConnection *cc);
360 265
361 266
362/** 267/**
363 * Get the tunnel towards a peer. 268 * We got a HELLO for a @a cp, remember it, and possibly
269 * trigger adequate actions (like trying to connect).
364 * 270 *
365 * @param peer Peer to get from. 271 * @param cp the peer we got a HELLO for
366 * 272 * @param hello the HELLO to remember
367 * @return Tunnel towards peer.
368 */ 273 */
369struct CadetTunnel * 274void
370GCP_get_tunnel (const struct CadetPeer *peer); 275GCP_set_hello (struct CadetPeer *cp,
276 const struct GNUNET_HELLO_Message *hello);
371 277
372 278
373/** 279/**
374 * Set the hello message. 280 * Clean up all entries about all peers.
375 * 281 * Must only be called after all tunnels, CORE-connections and
376 * @param peer Peer whose message to set. 282 * connections are down.
377 * @param hello Hello message.
378 */ 283 */
379void 284void
380GCP_set_hello (struct CadetPeer *peer, 285GCP_destroy_all_peers (void);
381 const struct GNUNET_HELLO_Message *hello);
382 286
383 287
384/** 288/**
385 * Get the hello message. 289 * Data structure used to track whom we have to notify about changes
386 * 290 * in our ability to transmit to a given peer.
387 * @param peer Peer whose message to get.
388 * 291 *
389 * @return Hello message. 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.
390 */ 296 */
391struct GNUNET_HELLO_Message * 297struct GCP_MessageQueueManager;
392GCP_get_hello (struct CadetPeer *peer);
393 298
394 299
395/** 300/**
396 * Try to connect to a peer on TRANSPORT level. 301 * Function to call with updated message queue object.
397 * 302 *
398 * @param peer Peer to whom to connect. 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
399 */ 308 */
400void 309typedef void
401GCP_try_connect (struct CadetPeer *peer); 310(*GCP_MessageQueueNotificationCallback)(void *cls,
311 int available);
312
402 313
403/** 314/**
404 * Notify a peer that a link between two other peers is broken. If any path 315 * Start message queue change notifications. Will create a new slot
405 * used that link, eliminate it. 316 * to manage the message queue to the given @a cp.
406 * 317 *
407 * @param peer Peer affected by the change. 318 * @param cp peer to notify for
408 * @param peer1 Peer whose link is broken. 319 * @param cb function to call if mq becomes available or unavailable
409 * @param peer2 Peer whose link is broken. 320 * @param cb_cls closure for @a cb
321 * @return handle to cancel request
410 */ 322 */
411void 323struct GCP_MessageQueueManager *
412GCP_notify_broken_link (struct CadetPeer *peer, 324GCP_request_mq (struct CadetPeer *cp,
413 const struct GNUNET_PeerIdentity *peer1, 325 GCP_MessageQueueNotificationCallback cb,
414 const struct GNUNET_PeerIdentity *peer2); 326 void *cb_cls);
415 327
416 328
417/** 329/**
418 * Count the number of known paths toward the peer. 330 * Test if @a cp has a core-level connection
419 * 331 *
420 * @param peer Peer to get path info. 332 * @param cp peer to test
421 * 333 * @return #GNUNET_YES if @a cp has a core-level connection
422 * @return Number of known paths.
423 */ 334 */
424unsigned int 335int
425GCP_count_paths (const struct CadetPeer *peer); 336GCP_has_core_connection (struct CadetPeer *cp);
337
426 338
427/** 339/**
428 * Iterate over the paths to a peer. 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.
429 * 345 *
430 * @param peer Peer to get path info. 346 * @param mqm message queue manager for the transmission
431 * @param callback Function to call for every path. 347 * @param env envelope with the message to send; must NOT
432 * @param cls Closure for @a callback. 348 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
433 *
434 * @return Number of iterated paths.
435 */ 349 */
436unsigned int 350void
437GCP_iterate_paths (struct CadetPeer *peer, 351GCP_send (struct GCP_MessageQueueManager *mqm,
438 GCP_path_iterator callback, 352 struct GNUNET_MQ_Envelope *env);
439 void *cls);
440 353
441 354
442/** 355/**
443 * Iterate all known peers. 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.
444 * 360 *
445 * @param iter Iterator. 361 * @param cp peer to send the message to
446 * @param cls Closure for @c iter. 362 * @param env envelope with the message to send
447 */ 363 */
448void 364void
449GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, 365GCP_send_ooo (struct CadetPeer *cp,
450 void *cls); 366 struct GNUNET_MQ_Envelope *env);
451 367
452 368
453/** 369/**
454 * Get the static string for a peer ID. 370 * Stops message queue change notifications and sends a last message.
455 * 371 * In practice, this is implemented by sending that @a last_env
456 * @param peer Peer. 372 * message immediately (if any), ignoring queue order.
457 * 373 *
458 * @return Static string for it's ID. 374 * @param mqm handle matching request to cancel
375 * @param last_env final message to transmit, or NULL
459 */ 376 */
460const char * 377void
461GCP_2s (const struct CadetPeer *peer); 378GCP_request_mq_cancel (struct GCP_MessageQueueManager *mqm,
379 struct GNUNET_MQ_Envelope *last_env);
462 380
463 381
464/** 382/**
465 * Log all kinds of info about a peer. 383 * Set the message queue to @a mq for peer @a cp and notify watchers.
466 * 384 *
467 * @param peer Peer. 385 * @param cp peer to modify
386 * @param mq message queue to set (can be NULL)
468 */ 387 */
469void 388void
470GCP_debug (const struct CadetPeer *p, 389GCP_set_mq (struct CadetPeer *cp,
471 enum GNUNET_ErrorType level); 390 struct GNUNET_MQ_Handle *mq);
472
473 391
474#if 0 /* keep Emacsens' auto-indent happy */
475{
476#endif
477#ifdef __cplusplus
478}
479#endif
480 392
481/* ifndef GNUNET_CADET_SERVICE_PEER_H */
482#endif 393#endif
483/* end of gnunet-cadet-service_peer.h */