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