37d735bf52ff58d9a2768c2092d645be352d0763
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_peer.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 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 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  * Handle to queued messages on a peer level.
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 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  */
70 typedef void
71 (*GCP_sent) (void *cls,
72              struct CadetConnection *c, int fwd, int sent,
73              uint16_t type, uint16_t payload_type, uint32_t pid,
74              size_t size,
75              struct GNUNET_TIME_Relative wait);
76
77 /**
78  * Peer path iterator.
79  *
80  * @param cls Closure.
81  * @param peer Peer this path is towards.
82  * @param path Path itself
83  * @return #GNUNET_YES if should keep iterating.
84  *         #GNUNET_NO otherwise.
85  */
86 typedef int
87 (*GCP_path_iterator) (void *cls,
88                       struct CadetPeer *peer,
89                       struct CadetPeerPath *path);
90
91
92 /******************************************************************************/
93 /********************************    API    ***********************************/
94 /******************************************************************************/
95
96 /**
97  * Initialize peer subsystem.
98  *
99  * @param c Configuration.
100  */
101 void
102 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c);
103
104 /**
105  * Shut down the peer subsystem.
106  */
107 void
108 GCP_shutdown (void);
109
110
111 /**
112  * Retrieve the CadetPeer stucture associated with the peer. Optionally create
113  * one and insert it in the appropriate structures if the peer is not known yet.
114  *
115  * @param peer_id Full identity of the peer.
116  * @param create #GNUNET_YES if a new peer should be created if unknown.
117  *               #GNUNET_NO otherwise.
118  *
119  * @return Existing or newly created peer structure.
120  *         NULL if unknown and not requested @a create
121  */
122 struct CadetPeer *
123 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create);
124
125
126 /**
127  * Retrieve the CadetPeer stucture associated with the peer. Optionally create
128  * one and insert it in the appropriate structures if the peer is not known yet.
129  *
130  * @param peer Short identity of the peer.
131  * @param create #GNUNET_YES if a new peer should be created if unknown.
132  *               #GNUNET_NO otherwise.
133  *
134  * @return Existing or newly created peer structure.
135  *         NULL if unknown and not requested @a create
136  */
137 struct CadetPeer *
138 GCP_get_short (const GNUNET_PEER_Id peer, int create);
139
140
141 /**
142  * Try to establish a new connection to this peer (in its tunnel).
143  * If the peer doesn't have any path to it yet, try to get one.
144  * If the peer already has some path, send a CREATE CONNECTION towards it.
145  *
146  * @param peer Peer to connect to.
147  */
148 void
149 GCP_connect (struct CadetPeer *peer);
150
151 /**
152  * @brief Send a message to another peer (using CORE).
153  *
154  * @param peer Peer towards which to queue the message.
155  * @param message Message to send.
156  * @param payload_type Type of the message's payload, for debug messages.
157  *                     0 if the message is a retransmission (unknown payload).
158  *                     UINT16_MAX if the message does not have payload.
159  * @param payload_id ID of the payload (MID, ACK #, etc)
160  * @param c Connection this message belongs to (can be NULL).
161  * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
162  * @param cont Continuation to be called once CORE has sent the message.
163  * @param cont_cls Closure for @c cont.
164  */
165 struct CadetPeerQueue *
166 GCP_send (struct CadetPeer *peer,
167           const struct GNUNET_MessageHeader *message,
168           uint16_t payload_type,
169           uint32_t payload_id,
170           struct CadetConnection *c,
171           int fwd,
172           GCP_sent cont,
173           void *cont_cls);
174
175 /**
176  * Cancel sending a message. Message must have been sent with
177  * #GCP_send before.  May not be called after the notify sent
178  * callback has been called.
179  *
180  * It does NOT call the continuation given to #GCP_send.
181  *
182  * @param q Queue handle to cancel
183  */
184 void
185 GCP_send_cancel (struct CadetPeerQueue *q);
186
187 /**
188  * Set tunnel.
189  *
190  * @param peer Peer.
191  * @param t Tunnel.
192  */
193 void
194 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t);
195
196
197 /**
198  * Check whether there is a direct (core level)  connection to peer.
199  *
200  * @param peer Peer to check.
201  *
202  * @return #GNUNET_YES if there is a direct connection.
203  */
204 int
205 GCP_is_neighbor (const struct CadetPeer *peer);
206
207
208 /**
209  * Create and initialize a new tunnel towards a peer, in case it has none.
210  *
211  * Does not generate any traffic, just creates the local data structures.
212  *
213  * @param peer Peer towards which to create the tunnel.
214  */
215 void
216 GCP_add_tunnel (struct CadetPeer *peer);
217
218
219 /**
220  * Add a connection to a neighboring peer.
221  *
222  * Store that the peer is the first hop of the connection in one
223  * direction and that on peer disconnect the connection must be
224  * notified and destroyed, for it will no longer be valid.
225  *
226  * @param peer Peer to add connection to.
227  * @param c Connection to add.
228  * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
229  */
230 void
231 GCP_add_connection (struct CadetPeer *peer,
232                     struct CadetConnection *c,
233                     int pred);
234
235
236 /**
237  * Add the path to the peer and update the path used to reach it in case this
238  * is the shortest.
239  *
240  * @param peer Destination peer to add the path to.
241  * @param path New path to add. Last peer must be the peer in arg 1.
242  *             Path will be either used of freed if already known.
243  * @param trusted Do we trust that this path is real?
244  *
245  * @return path if path was taken, pointer to existing duplicate if exists
246  *         NULL on error.
247  */
248 struct CadetPeerPath *
249 GCP_add_path (struct CadetPeer *peer,
250               struct CadetPeerPath *p,
251               int trusted);
252
253
254 /**
255  * Add the path to the origin peer and update the path used to reach it in case
256  * this is the shortest.
257  * The path is given in peer_info -> destination, therefore we turn the path
258  * upside down first.
259  *
260  * @param peer Peer to add the path to, being the origin of the path.
261  * @param path New path to add after being inversed.
262  *             Path will be either used or freed.
263  * @param trusted Do we trust that this path is real?
264  *
265  * @return path if path was taken, pointer to existing duplicate if exists
266  *         NULL on error.
267  */
268 struct CadetPeerPath *
269 GCP_add_path_to_origin (struct CadetPeer *peer,
270                         struct CadetPeerPath *path,
271                         int trusted);
272
273 /**
274  * Adds a path to the info of all the peers in the path
275  *
276  * @param p Path to process.
277  * @param confirmed Whether we know if the path works or not.
278  */
279 void
280 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed);
281
282
283 /**
284  * Remove any path to the peer that has the extact same peers as the one given.
285  *
286  * @param peer Peer to remove the path from.
287  * @param path Path to remove. Is always destroyed .
288  */
289 void
290 GCP_remove_path (struct CadetPeer *peer,
291                  struct CadetPeerPath *path);
292
293
294 /**
295  * Check that we are aware of a connection from a neighboring peer.
296  *
297  * @param peer Peer to the connection is with
298  * @param c Connection that should be in the map with this peer.
299  */
300 void
301 GCP_check_connection (const struct CadetPeer *peer,
302                       const struct CadetConnection *c);
303
304
305 /**
306  * Remove a connection from a neighboring peer.
307  *
308  * @param peer Peer to remove connection from.
309  * @param c Connection to remove.
310  */
311 void
312 GCP_remove_connection (struct CadetPeer *peer,
313                        const struct CadetConnection *c);
314
315
316 /**
317  * Start the DHT search for new paths towards the peer: we don't have
318  * enough good connections.
319  *
320  * @param peer Destination peer.
321  */
322 void
323 GCP_start_search (struct CadetPeer *peer);
324
325
326 /**
327  * Stop the DHT search for new paths towards the peer: we already have
328  * enough good connections.
329  *
330  * @param peer Destination peer.
331  */
332 void
333 GCP_stop_search (struct CadetPeer *peer);
334
335
336 /**
337  * Get the Full ID of a peer.
338  *
339  * @param peer Peer to get from.
340  *
341  * @return Full ID of peer.
342  */
343 const struct GNUNET_PeerIdentity *
344 GCP_get_id (const struct CadetPeer *peer);
345
346
347 /**
348  * Get the Short ID of a peer.
349  *
350  * @param peer Peer to get from.
351  *
352  * @return Short ID of peer.
353  */
354 GNUNET_PEER_Id
355 GCP_get_short_id (const struct CadetPeer *peer);
356
357
358 /**
359  * Get the tunnel towards a peer.
360  *
361  * @param peer Peer to get from.
362  *
363  * @return Tunnel towards peer.
364  */
365 struct CadetTunnel *
366 GCP_get_tunnel (const struct CadetPeer *peer);
367
368
369 /**
370  * Set the hello message.
371  *
372  * @param peer Peer whose message to set.
373  * @param hello Hello message.
374  */
375 void
376 GCP_set_hello (struct CadetPeer *peer,
377                const struct GNUNET_HELLO_Message *hello);
378
379
380 /**
381  * Get the hello message.
382  *
383  * @param peer Peer whose message to get.
384  *
385  * @return Hello message.
386  */
387 struct GNUNET_HELLO_Message *
388 GCP_get_hello (struct CadetPeer *peer);
389
390
391 /**
392  * Try to connect to a peer on TRANSPORT level.
393  *
394  * @param peer Peer to whom to connect.
395  */
396 void
397 GCP_try_connect (struct CadetPeer *peer);
398
399 /**
400  * Notify a peer that a link between two other peers is broken. If any path
401  * used that link, eliminate it.
402  *
403  * @param peer Peer affected by the change.
404  * @param peer1 Peer whose link is broken.
405  * @param peer2 Peer whose link is broken.
406  */
407 void
408 GCP_notify_broken_link (struct CadetPeer *peer,
409                         const struct GNUNET_PeerIdentity *peer1,
410                         const struct GNUNET_PeerIdentity *peer2);
411
412
413 /**
414  * Count the number of known paths toward the peer.
415  *
416  * @param peer Peer to get path info.
417  *
418  * @return Number of known paths.
419  */
420 unsigned int
421 GCP_count_paths (const struct CadetPeer *peer);
422
423 /**
424  * Iterate over the paths to a peer.
425  *
426  * @param peer Peer to get path info.
427  * @param callback Function to call for every path.
428  * @param cls Closure for @a callback.
429  *
430  * @return Number of iterated paths.
431  */
432 unsigned int
433 GCP_iterate_paths (struct CadetPeer *peer,
434                    GCP_path_iterator callback,
435                    void *cls);
436
437
438 /**
439  * Iterate all known peers.
440  *
441  * @param iter Iterator.
442  * @param cls Closure for @c iter.
443  */
444 void
445 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
446                  void *cls);
447
448
449 /**
450  * Get the static string for a peer ID.
451  *
452  * @param peer Peer.
453  *
454  * @return Static string for it's ID.
455  */
456 const char *
457 GCP_2s (const struct CadetPeer *peer);
458
459
460 /**
461  * Log all kinds of info about a peer.
462  *
463  * @param peer Peer.
464  */
465 void
466 GCP_debug (const struct CadetPeer *p,
467            enum GNUNET_ErrorType level);
468
469
470 #if 0                           /* keep Emacsens' auto-indent happy */
471 {
472 #endif
473 #ifdef __cplusplus
474 }
475 #endif
476
477 /* ifndef GNUNET_CADET_SERVICE_PEER_H */
478 #endif
479 /* end of gnunet-cadet-service_peer.h */