cancel existing last_control_qe handle before scheduling a new one, always
[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,
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
81 /**
82  * Peer path iterator.
83  *
84  * @param cls Closure.
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  */
90 typedef 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  *
103  * @param c Configuration.
104  */
105 void
106 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c);
107
108 /**
109  * Shut down the peer subsystem.
110  */
111 void
112 GCP_shutdown (void);
113
114
115 /**
116  * Retrieve the CadetPeer stucture associated with the peer. Optionally create
117  * one and insert it in the appropriate structures if the peer is not known yet.
118  *
119  * @param peer_id Full identity of the peer.
120  * @param create #GNUNET_YES if a new peer should be created if unknown.
121  *               #GNUNET_NO otherwise.
122  *
123  * @return Existing or newly created peer structure.
124  *         NULL if unknown and not requested @a create
125  */
126 struct CadetPeer *
127 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create);
128
129
130 /**
131  * Retrieve the CadetPeer stucture associated with the peer. Optionally create
132  * one and insert it in the appropriate structures if the peer is not known yet.
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  *
138  * @return Existing or newly created peer structure.
139  *         NULL if unknown and not requested @a create
140  */
141 struct CadetPeer *
142 GCP_get_short (const GNUNET_PEER_Id peer, int create);
143
144
145 /**
146  * Try to establish a new connection to this peer (in its tunnel).
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  */
152 void
153 GCP_connect (struct CadetPeer *peer);
154
155 /**
156  * @brief Send a message to another peer (using CORE).
157  *
158  * @param peer Peer towards which to queue the message.
159  * @param message Message to send.
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  */
169 struct CadetPeerQueue *
170 GCP_send (struct CadetPeer *peer,
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
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  */
188 void
189 GCP_send_cancel (struct CadetPeerQueue *q);
190
191 /**
192  * Set tunnel.
193  *
194  * @param peer Peer.
195  * @param t Tunnel.
196  */
197 void
198 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t);
199
200
201 /**
202  * Check whether there is a direct (core level)  connection to peer.
203  *
204  * @param peer Peer to check.
205  *
206  * @return #GNUNET_YES if there is a direct connection.
207  */
208 int
209 GCP_is_neighbor (const struct CadetPeer *peer);
210
211
212 /**
213  * Create and initialize a new tunnel towards a peer, in case it has none.
214  *
215  * Does not generate any traffic, just creates the local data structures.
216  *
217  * @param peer Peer towards which to create the tunnel.
218  */
219 void
220 GCP_add_tunnel (struct CadetPeer *peer);
221
222
223 /**
224  * Add a connection to a neighboring peer.
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  *
230  * @param peer Peer to add connection to.
231  * @param c Connection to add.
232  * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
233  */
234 void
235 GCP_add_connection (struct CadetPeer *peer,
236                     struct CadetConnection *c,
237                     int pred);
238
239
240 /**
241  * Add the path to the peer and update the path used to reach it in case this
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  *
249  * @return path if path was taken, pointer to existing duplicate if exists
250  *         NULL on error.
251  */
252 struct CadetPeerPath *
253 GCP_add_path (struct CadetPeer *peer,
254               struct CadetPeerPath *p,
255               int trusted);
256
257
258 /**
259  * Add the path to the origin peer and update the path used to reach it in case
260  * this is the shortest.
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  *
269  * @return path if path was taken, pointer to existing duplicate if exists
270  *         NULL on error.
271  */
272 struct CadetPeerPath *
273 GCP_add_path_to_origin (struct CadetPeer *peer,
274                         struct CadetPeerPath *path,
275                         int trusted);
276
277 /**
278  * Adds a path to the info of all the peers in the path
279  *
280  * @param p Path to process.
281  * @param confirmed Whether we know if the path works or not.
282  */
283 void
284 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed);
285
286
287 /**
288  * Remove any path to the peer that has the extact same peers as the one given.
289  *
290  * @param peer Peer to remove the path from.
291  * @param path Path to remove. Is always destroyed .
292  */
293 void
294 GCP_remove_path (struct CadetPeer *peer,
295                  struct CadetPeerPath *path);
296
297
298 /**
299  * Check that we are aware of a connection from a neighboring peer.
300  *
301  * @param peer Peer to the connection is with
302  * @param c Connection that should be in the map with this peer.
303  */
304 void
305 GCP_check_connection (const struct CadetPeer *peer,
306                       const struct CadetConnection *c);
307
308
309 /**
310  * Remove a connection from a neighboring peer.
311  *
312  * @param peer Peer to remove connection from.
313  * @param c Connection to remove.
314  */
315 void
316 GCP_remove_connection (struct CadetPeer *peer,
317                        const struct CadetConnection *c);
318
319
320 /**
321  * Start the DHT search for new paths towards the peer: we don't have
322  * enough good connections.
323  *
324  * @param peer Destination peer.
325  */
326 void
327 GCP_start_search (struct CadetPeer *peer);
328
329
330 /**
331  * Stop the DHT search for new paths towards the peer: we already have
332  * enough good connections.
333  *
334  * @param peer Destination peer.
335  */
336 void
337 GCP_stop_search (struct CadetPeer *peer);
338
339
340 /**
341  * Get the Full ID of a peer.
342  *
343  * @param peer Peer to get from.
344  *
345  * @return Full ID of peer.
346  */
347 const struct GNUNET_PeerIdentity *
348 GCP_get_id (const struct CadetPeer *peer);
349
350
351 /**
352  * Get the Short ID of a peer.
353  *
354  * @param peer Peer to get from.
355  *
356  * @return Short ID of peer.
357  */
358 GNUNET_PEER_Id
359 GCP_get_short_id (const struct CadetPeer *peer);
360
361
362 /**
363  * Get the tunnel towards a peer.
364  *
365  * @param peer Peer to get from.
366  *
367  * @return Tunnel towards peer.
368  */
369 struct CadetTunnel *
370 GCP_get_tunnel (const struct CadetPeer *peer);
371
372
373 /**
374  * Set the hello message.
375  *
376  * @param peer Peer whose message to set.
377  * @param hello Hello message.
378  */
379 void
380 GCP_set_hello (struct CadetPeer *peer,
381                const struct GNUNET_HELLO_Message *hello);
382
383
384 /**
385  * Get the hello message.
386  *
387  * @param peer Peer whose message to get.
388  *
389  * @return Hello message.
390  */
391 struct GNUNET_HELLO_Message *
392 GCP_get_hello (struct CadetPeer *peer);
393
394
395 /**
396  * Try to connect to a peer on TRANSPORT level.
397  *
398  * @param peer Peer to whom to connect.
399  */
400 void
401 GCP_try_connect (struct CadetPeer *peer);
402
403 /**
404  * Notify a peer that a link between two other peers is broken. If any path
405  * used that link, eliminate it.
406  *
407  * @param peer Peer affected by the change.
408  * @param peer1 Peer whose link is broken.
409  * @param peer2 Peer whose link is broken.
410  */
411 void
412 GCP_notify_broken_link (struct CadetPeer *peer,
413                         const struct GNUNET_PeerIdentity *peer1,
414                         const struct GNUNET_PeerIdentity *peer2);
415
416
417 /**
418  * Count the number of known paths toward the peer.
419  *
420  * @param peer Peer to get path info.
421  *
422  * @return Number of known paths.
423  */
424 unsigned int
425 GCP_count_paths (const struct CadetPeer *peer);
426
427 /**
428  * Iterate over the paths to a peer.
429  *
430  * @param peer Peer to get path info.
431  * @param callback Function to call for every path.
432  * @param cls Closure for @a callback.
433  *
434  * @return Number of iterated paths.
435  */
436 unsigned int
437 GCP_iterate_paths (struct CadetPeer *peer,
438                    GCP_path_iterator callback,
439                    void *cls);
440
441
442 /**
443  * Iterate all known peers.
444  *
445  * @param iter Iterator.
446  * @param cls Closure for @c iter.
447  */
448 void
449 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
450                  void *cls);
451
452
453 /**
454  * Get the static string for a peer ID.
455  *
456  * @param peer Peer.
457  *
458  * @return Static string for it's ID.
459  */
460 const char *
461 GCP_2s (const struct CadetPeer *peer);
462
463
464 /**
465  * Log all kinds of info about a peer.
466  *
467  * @param peer Peer.
468  */
469 void
470 GCP_debug (const struct CadetPeer *p,
471            enum GNUNET_ErrorType level);
472
473
474 #if 0                           /* keep Emacsens' auto-indent happy */
475 {
476 #endif
477 #ifdef __cplusplus
478 }
479 #endif
480
481 /* ifndef GNUNET_CADET_SERVICE_PEER_H */
482 #endif
483 /* end of gnunet-cadet-service_peer.h */