- refactor GCP_get_peer, fix get peer info
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_peer.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 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 cadet/gnunet-service-cadet_peer.h
23  * @brief cadet service; dealing with remote peers
24  * @author Bartlomiej Polot
25  *
26  * All functions in this file should use the prefix GMP (Gnunet Cadet Peer)
27  */
28
29 #ifndef GNUNET_SERVICE_CADET_PEER_H
30 #define GNUNET_SERVICE_CADET_PEER_H
31
32 #ifdef __cplusplus
33 extern "C"
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
44 /**
45  * Struct containing all information regarding a given peer
46  */
47 struct CadetPeer;
48
49 /**
50  * Struct containing info about a queued transmission to this peer
51  */
52 struct 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 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.
69  */
70 typedef int
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);
75
76 /**
77  * Peer path iterator.
78  *
79  * @param cls Closure.
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.
84  */
85 typedef int
86 (*GCP_path_iterator) (void *cls,
87                       struct CadetPeer *peer,
88                       struct CadetPeerPath *path);
89
90
91 /******************************************************************************/
92 /********************************    API    ***********************************/
93 /******************************************************************************/
94
95 /**
96  * Initialize peer subsystem.
97  *
98  * @param c Configuration.
99  */
100 void
101 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c);
102
103 /**
104  * Shut down the peer subsystem.
105  */
106 void
107 GCP_shutdown (void);
108
109
110 /**
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.
113  *
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.
117  *
118  * @return Existing or newly created peer structure.
119  *         NULL if unknown and not requested @a create
120  */
121 struct CadetPeer *
122 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create);
123
124 /**
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.
127  *
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.
131  *
132  * @return Existing or newly created peer structure.
133  *         NULL if unknown and not requested @a create
134  */
135 struct CadetPeer *
136 GCP_get_short (const GNUNET_PEER_Id peer, int create);
137
138 /**
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.
142  *
143  * @param peer Peer to connect to.
144  */
145 void
146 GCP_connect (struct CadetPeer *peer);
147
148 /**
149  * Free a transmission that was already queued with all resources
150  * associated to the request.
151  *
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.
154  *
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).
159  *
160  * @return #GNUNET_YES if connection was destroyed as a result,
161  *         #GNUNET_NO otherwise.
162  */
163 int
164 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
165                    int sent, uint32_t pid);
166
167 /**
168  * @brief Queue and pass message to core when possible.
169  *
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.
179  *
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.
182  */
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);
188
189 /**
190  * Cancel all queued messages to a peer that belong to a certain connection.
191  *
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.
195  */
196 void
197 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c);
198
199 /**
200  * Get the first message for a connection and unqueue it.
201  *
202  * Only tunnel (or higher) level messages are unqueued. Connection specific
203  * messages are silently destroyed upon encounter.
204  *
205  * @param peer Neighboring peer.
206  * @param c Connection.
207  * @param destroyed[in/out] Was the connection destroyed as a result?.
208  *                          Can NOT be NULL.
209  *
210  *
211  * @return First message for this connection.
212  */
213 struct GNUNET_MessageHeader *
214 GCP_connection_pop (struct CadetPeer *peer,
215                     struct CadetConnection *c,
216                     int *destroyed);
217
218 /**
219  * Unlock a possibly locked queue for a connection.
220  *
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.
224  *
225  * @param peer Peer who keeps the queue.
226  * @param c Connection whose messages to unlock.
227  */
228 void
229 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c);
230
231 /**
232  * Set tunnel.
233  *
234  * @param peer Peer.
235  * @param t Tunnel.
236  */
237 void
238 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t);
239
240
241 /**
242  * Check whether there is a direct (core level)  connection to peer.
243  *
244  * @param peer Peer to check.
245  *
246  * @return #GNUNET_YES if there is a direct connection.
247  */
248 int
249 GCP_is_neighbor (const struct CadetPeer *peer);
250
251
252 /**
253  * Create and initialize a new tunnel towards a peer, in case it has none.
254  *
255  * Does not generate any traffic, just creates the local data structures.
256  *
257  * @param peer Peer towards which to create the tunnel.
258  */
259 void
260 GCP_add_tunnel (struct CadetPeer *peer);
261
262
263 /**
264  * Add a connection to a neighboring peer.
265  *
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.
269  *
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
273  */
274 void
275 GCP_add_connection (struct CadetPeer *peer,
276                     struct CadetConnection *c,
277                     int pred);
278
279
280 /**
281  * Add the path to the peer and update the path used to reach it in case this
282  * is the shortest.
283  *
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?
288  *
289  * @return path if path was taken, pointer to existing duplicate if exists
290  *         NULL on error.
291  */
292 struct CadetPeerPath *
293 GCP_add_path (struct CadetPeer *peer,
294               struct CadetPeerPath *p,
295               int trusted);
296
297
298 /**
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
302  * upside down first.
303  *
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?
308  *
309  * @return path if path was taken, pointer to existing duplicate if exists
310  *         NULL on error.
311  */
312 struct CadetPeerPath *
313 GCP_add_path_to_origin (struct CadetPeer *peer,
314                         struct CadetPeerPath *path,
315                         int trusted);
316
317 /**
318  * Adds a path to the info of all the peers in the path
319  *
320  * @param p Path to process.
321  * @param confirmed Whether we know if the path works or not.
322  */
323 void
324 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed);
325
326
327 /**
328  * Remove any path to the peer that has the extact same peers as the one given.
329  *
330  * @param peer Peer to remove the path from.
331  * @param path Path to remove. Is always destroyed .
332  */
333 void
334 GCP_remove_path (struct CadetPeer *peer,
335                  struct CadetPeerPath *path);
336
337
338 /**
339  * Check that we are aware of a connection from a neighboring peer.
340  *
341  * @param peer Peer to the connection is with
342  * @param c Connection that should be in the map with this peer.
343  */
344 void
345 GCP_check_connection (const struct CadetPeer *peer,
346                       const struct CadetConnection *c);
347
348
349 /**
350  * Remove a connection from a neighboring peer.
351  *
352  * @param peer Peer to remove connection from.
353  * @param c Connection to remove.
354  */
355 void
356 GCP_remove_connection (struct CadetPeer *peer,
357                        const struct CadetConnection *c);
358
359
360 /**
361  * Start the DHT search for new paths towards the peer: we don't have
362  * enough good connections.
363  *
364  * @param peer Destination peer.
365  */
366 void
367 GCP_start_search (struct CadetPeer *peer);
368
369
370 /**
371  * Stop the DHT search for new paths towards the peer: we already have
372  * enough good connections.
373  *
374  * @param peer Destination peer.
375  */
376 void
377 GCP_stop_search (struct CadetPeer *peer);
378
379
380 /**
381  * Get the Full ID of a peer.
382  *
383  * @param peer Peer to get from.
384  *
385  * @return Full ID of peer.
386  */
387 const struct GNUNET_PeerIdentity *
388 GCP_get_id (const struct CadetPeer *peer);
389
390
391 /**
392  * Get the Short ID of a peer.
393  *
394  * @param peer Peer to get from.
395  *
396  * @return Short ID of peer.
397  */
398 GNUNET_PEER_Id
399 GCP_get_short_id (const struct CadetPeer *peer);
400
401
402 /**
403  * Get the tunnel towards a peer.
404  *
405  * @param peer Peer to get from.
406  *
407  * @return Tunnel towards peer.
408  */
409 struct CadetTunnel *
410 GCP_get_tunnel (const struct CadetPeer *peer);
411
412
413 /**
414  * Set the hello message.
415  *
416  * @param peer Peer whose message to set.
417  * @param hello Hello message.
418  */
419 void
420 GCP_set_hello (struct CadetPeer *peer,
421                const struct GNUNET_HELLO_Message *hello);
422
423
424 /**
425  * Get the hello message.
426  *
427  * @param peer Peer whose message to get.
428  *
429  * @return Hello message.
430  */
431 struct GNUNET_HELLO_Message *
432 GCP_get_hello (struct CadetPeer *peer);
433
434
435 /**
436  * Try to connect to a peer on TRANSPORT level.
437  *
438  * @param peer Peer to whom to connect.
439  */
440 void
441 GCP_try_connect (struct CadetPeer *peer);
442
443 /**
444  * Notify a peer that a link between two other peers is broken. If any path
445  * used that link, eliminate it.
446  *
447  * @param peer Peer affected by the change.
448  * @param peer1 Peer whose link is broken.
449  * @param peer2 Peer whose link is broken.
450  */
451 void
452 GCP_notify_broken_link (struct CadetPeer *peer,
453                         struct GNUNET_PeerIdentity *peer1,
454                         struct GNUNET_PeerIdentity *peer2);
455
456
457 /**
458  * Count the number of known paths toward the peer.
459  *
460  * @param peer Peer to get path info.
461  *
462  * @return Number of known paths.
463  */
464 unsigned int
465 GCP_count_paths (const struct CadetPeer *peer);
466
467 /**
468  * Iterate over the paths to a peer.
469  *
470  * @param peer Peer to get path info.
471  * @param callback Function to call for every path.
472  * @param cls Closure for @a callback.
473  *
474  * @return Number of iterated paths.
475  */
476 unsigned int
477 GCP_iterate_paths (struct CadetPeer *peer,
478                    GCP_path_iterator callback,
479                    void *cls);
480
481
482 /**
483  * Iterate all known peers.
484  *
485  * @param iter Iterator.
486  * @param cls Closure for @c iter.
487  */
488 void
489 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
490
491
492 /**
493  * Get the static string for a peer ID.
494  *
495  * @param peer Peer.
496  *
497  * @return Static string for it's ID.
498  */
499 const char *
500 GCP_2s (const struct CadetPeer *peer);
501
502
503 /**
504  * Log all kinds of info about a peer.
505  *
506  * @param peer Peer.
507  */
508 void
509 GCP_debug (const struct CadetPeer *p,
510            enum GNUNET_ErrorType level);
511
512
513 #if 0                           /* keep Emacsens' auto-indent happy */
514 {
515 #endif
516 #ifdef __cplusplus
517 }
518 #endif
519
520 /* ifndef GNUNET_CADET_SERVICE_PEER_H */
521 #endif
522 /* end of gnunet-cadet-service_peer.h */