2 This file is part of GNUnet.
3 Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file cadet/gnunet-service-cadet_peer.h
23 * @brief cadet service; dealing with remote peers
24 * @author Bartlomiej Polot
26 * All functions in this file should use the prefix GMP (Gnunet Cadet Peer)
29 #ifndef GNUNET_SERVICE_CADET_PEER_H
30 #define GNUNET_SERVICE_CADET_PEER_H
35 #if 0 /* keep Emacsens' auto-indent happy */
41 #include "gnunet_util_lib.h"
42 #include "cadet_path.h"
45 * Struct containing all information regarding a given peer
50 * Struct containing info about a queued transmission to this peer
52 struct CadetPeerQueue;
54 #include "gnunet-service-cadet_connection.h"
58 * Callback called when a queued message is sent.
61 * @param c Connection this message was on.
62 * @param sent Was it really sent? (Could have been canceled)
63 * @param type Type of message sent.
64 * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
65 * @param fwd Was this a FWD going message?
66 * @param size Size of the message.
67 * @param wait Time spent waiting for core (only the time for THIS message)
68 * @return #GNUNET_YES if connection was destroyed, #GNUNET_NO otherwise.
71 (*GCP_sent) (void *cls,
72 struct CadetConnection *c, int sent,
73 uint16_t type, uint32_t pid, int fwd, size_t size,
74 struct GNUNET_TIME_Relative wait);
80 * @param peer Peer this path is towards.
81 * @param path Path itself
82 * @return #GNUNET_YES if should keep iterating.
83 * #GNUNET_NO otherwise.
86 (*GCP_path_iterator) (void *cls,
87 struct CadetPeer *peer,
88 struct CadetPeerPath *path);
91 /******************************************************************************/
92 /******************************** API ***********************************/
93 /******************************************************************************/
96 * Initialize peer subsystem.
98 * @param c Configuration.
101 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c);
104 * Shut down the peer subsystem.
111 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
112 * one and insert it in the appropriate structures if the peer is not known yet.
114 * @param peer_id Full identity of the peer.
115 * @param create #GNUNET_YES if a new peer should be created if unknown.
116 * #GNUNET_NO otherwise.
118 * @return Existing or newly created peer structure.
119 * NULL if unknown and not requested @a create
122 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create);
125 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
126 * one and insert it in the appropriate structures if the peer is not known yet.
128 * @param peer Short identity of the peer.
129 * @param create #GNUNET_YES if a new peer should be created if unknown.
130 * #GNUNET_NO otherwise.
132 * @return Existing or newly created peer structure.
133 * NULL if unknown and not requested @a create
136 GCP_get_short (const GNUNET_PEER_Id peer, int create);
139 * Try to establish a new connection to this peer (in its tunnel).
140 * If the peer doesn't have any path to it yet, try to get one.
141 * If the peer already has some path, send a CREATE CONNECTION towards it.
143 * @param peer Peer to connect to.
146 GCP_connect (struct CadetPeer *peer);
149 * Free a transmission that was already queued with all resources
150 * associated to the request.
152 * If connection was marked to be destroyed, and this was the last queued
153 * message on it, the connection will be free'd as a result.
155 * @param queue Queue handler to cancel.
156 * @param clear_cls Is it necessary to free associated cls?
157 * @param sent Was it really sent? (Could have been canceled)
158 * @param pid PID, if relevant (was sent and was a payload message).
160 * @return #GNUNET_YES if connection was destroyed as a result,
161 * #GNUNET_NO otherwise.
164 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
165 int sent, uint32_t pid);
168 * @brief Queue and pass message to core when possible.
170 * @param peer Peer towards which to queue the message.
171 * @param cls Closure (@c type dependant). It will be used by queue_send to
172 * build the message to be sent if not already prebuilt.
173 * @param type Type of the message, 0 for a raw message.
174 * @param size Size of the message.
175 * @param c Connection this message belongs to (cannot be NULL).
176 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
177 * @param cont Continuation to be called once CORE has taken the message.
178 * @param cont_cls Closure for @c cont.
180 * @return Handle to cancel the message before it is sent. Once cont is called
181 * message has been sent and therefore the handle is no longer valid.
183 struct CadetPeerQueue *
184 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
185 uint16_t payload_type, uint32_t payload_id,
186 size_t size, struct CadetConnection *c, int fwd,
187 GCP_sent cont, void *cont_cls);
190 * Cancel all queued messages to a peer that belong to a certain connection.
192 * @param peer Peer towards whom to cancel.
193 * @param c Connection whose queued messages to cancel. Might be destroyed by
194 * the sent continuation call.
197 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c);
200 * Get the first message for a connection and unqueue it.
202 * Only tunnel (or higher) level messages are unqueued. Connection specific
203 * messages are silently destroyed upon encounter.
205 * @param peer Neighboring peer.
206 * @param c Connection.
207 * @param destroyed[in/out] Was the connection destroyed as a result?.
211 * @return First message for this connection.
213 struct GNUNET_MessageHeader *
214 GCP_connection_pop (struct CadetPeer *peer,
215 struct CadetConnection *c,
219 * Unlock a possibly locked queue for a connection.
221 * If there is a message that can be sent on this connection, call core for it.
222 * Otherwise (if core transmit is already called or there is no sendable
223 * message) do nothing.
225 * @param peer Peer who keeps the queue.
226 * @param c Connection whose messages to unlock.
229 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c);
238 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t);
242 * Check whether there is a direct (core level) connection to peer.
244 * @param peer Peer to check.
246 * @return #GNUNET_YES if there is a direct connection.
249 GCP_is_neighbor (const struct CadetPeer *peer);
253 * Create and initialize a new tunnel towards a peer, in case it has none.
255 * Does not generate any traffic, just creates the local data structures.
257 * @param peer Peer towards which to create the tunnel.
260 GCP_add_tunnel (struct CadetPeer *peer);
264 * Add a connection to a neighboring peer.
266 * Store that the peer is the first hop of the connection in one
267 * direction and that on peer disconnect the connection must be
268 * notified and destroyed, for it will no longer be valid.
270 * @param peer Peer to add connection to.
271 * @param c Connection to add.
272 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
275 GCP_add_connection (struct CadetPeer *peer,
276 struct CadetConnection *c,
281 * Add the path to the peer and update the path used to reach it in case this
284 * @param peer Destination peer to add the path to.
285 * @param path New path to add. Last peer must be the peer in arg 1.
286 * Path will be either used of freed if already known.
287 * @param trusted Do we trust that this path is real?
289 * @return path if path was taken, pointer to existing duplicate if exists
292 struct CadetPeerPath *
293 GCP_add_path (struct CadetPeer *peer,
294 struct CadetPeerPath *p,
299 * Add the path to the origin peer and update the path used to reach it in case
300 * this is the shortest.
301 * The path is given in peer_info -> destination, therefore we turn the path
304 * @param peer Peer to add the path to, being the origin of the path.
305 * @param path New path to add after being inversed.
306 * Path will be either used or freed.
307 * @param trusted Do we trust that this path is real?
309 * @return path if path was taken, pointer to existing duplicate if exists
312 struct CadetPeerPath *
313 GCP_add_path_to_origin (struct CadetPeer *peer,
314 struct CadetPeerPath *path,
318 * Adds a path to the info of all the peers in the path
320 * @param p Path to process.
321 * @param confirmed Whether we know if the path works or not.
324 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed);
328 * Remove any path to the peer that has the extact same peers as the one given.
330 * @param peer Peer to remove the path from.
331 * @param path Path to remove. Is always destroyed .
334 GCP_remove_path (struct CadetPeer *peer,
335 struct CadetPeerPath *path);
339 * Check that we are aware of a connection from a neighboring peer.
341 * @param peer Peer to the connection is with
342 * @param c Connection that should be in the map with this peer.
345 GCP_check_connection (const struct CadetPeer *peer,
346 const struct CadetConnection *c);
350 * Remove a connection from a neighboring peer.
352 * @param peer Peer to remove connection from.
353 * @param c Connection to remove.
356 GCP_remove_connection (struct CadetPeer *peer,
357 const struct CadetConnection *c);
361 * Start the DHT search for new paths towards the peer: we don't have
362 * enough good connections.
364 * @param peer Destination peer.
367 GCP_start_search (struct CadetPeer *peer);
371 * Stop the DHT search for new paths towards the peer: we already have
372 * enough good connections.
374 * @param peer Destination peer.
377 GCP_stop_search (struct CadetPeer *peer);
381 * Get the Full ID of a peer.
383 * @param peer Peer to get from.
385 * @return Full ID of peer.
387 const struct GNUNET_PeerIdentity *
388 GCP_get_id (const struct CadetPeer *peer);
392 * Get the Short ID of a peer.
394 * @param peer Peer to get from.
396 * @return Short ID of peer.
399 GCP_get_short_id (const struct CadetPeer *peer);
403 * Get the tunnel towards a peer.
405 * @param peer Peer to get from.
407 * @return Tunnel towards peer.
410 GCP_get_tunnel (const struct CadetPeer *peer);
414 * Set the hello message.
416 * @param peer Peer whose message to set.
417 * @param hello Hello message.
420 GCP_set_hello (struct CadetPeer *peer,
421 const struct GNUNET_HELLO_Message *hello);
425 * Get the hello message.
427 * @param peer Peer whose message to get.
429 * @return Hello message.
431 struct GNUNET_HELLO_Message *
432 GCP_get_hello (struct CadetPeer *peer);
436 * Try to connect to a peer on TRANSPORT level.
438 * @param peer Peer to whom to connect.
441 GCP_try_connect (struct CadetPeer *peer);
444 * Notify a peer that a link between two other peers is broken. If any path
445 * used that link, eliminate it.
447 * @param peer Peer affected by the change.
448 * @param peer1 Peer whose link is broken.
449 * @param peer2 Peer whose link is broken.
452 GCP_notify_broken_link (struct CadetPeer *peer,
453 struct GNUNET_PeerIdentity *peer1,
454 struct GNUNET_PeerIdentity *peer2);
458 * Count the number of known paths toward the peer.
460 * @param peer Peer to get path info.
462 * @return Number of known paths.
465 GCP_count_paths (const struct CadetPeer *peer);
468 * Iterate over the paths to a peer.
470 * @param peer Peer to get path info.
471 * @param callback Function to call for every path.
472 * @param cls Closure for @a callback.
474 * @return Number of iterated paths.
477 GCP_iterate_paths (struct CadetPeer *peer,
478 GCP_path_iterator callback,
483 * Iterate all known peers.
485 * @param iter Iterator.
486 * @param cls Closure for @c iter.
489 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
493 * Get the static string for a peer ID.
497 * @return Static string for it's ID.
500 GCP_2s (const struct CadetPeer *peer);
504 * Log all kinds of info about a peer.
509 GCP_debug (const struct CadetPeer *p,
510 enum GNUNET_ErrorType level);
513 #if 0 /* keep Emacsens' auto-indent happy */
520 /* ifndef GNUNET_CADET_SERVICE_PEER_H */
522 /* end of gnunet-cadet-service_peer.h */