-fix (C) notices
[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  * 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.
174  * @param payload_type Type of the message's payload
175  *                     0 if the message is a retransmission (unknown payload).
176  *                     UINT16_MAX if the message does not have payload.
177  * @param payload_id ID of the payload (MID, ACK #, etc)
178  * @param size Size of the message.
179  * @param c Connection this message belongs to (can be NULL).
180  * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
181  * @param cont Continuation to be called once CORE has taken the message.
182  * @param cont_cls Closure for @c cont.
183  *
184  * @return Handle to cancel the message before it is sent. Once cont is called
185  *         message has been sent and therefore the handle is no longer valid.
186  */
187 struct CadetPeerQueue *
188 GCP_queue_add (struct CadetPeer *peer,
189                void *cls,
190                uint16_t type,
191                uint16_t payload_type,
192                uint32_t payload_id,
193                size_t size,
194                struct CadetConnection *c,
195                int fwd,
196                GCP_sent cont,
197                void *cont_cls);
198
199 /**
200  * Cancel all queued messages to a peer that belong to a certain connection.
201  *
202  * @param peer Peer towards whom to cancel.
203  * @param c Connection whose queued messages to cancel. Might be destroyed by
204  *          the sent continuation call.
205  */
206 void
207 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c);
208
209 /**
210  * Get the first message for a connection and unqueue it.
211  *
212  * Only tunnel (or higher) level messages are unqueued. Connection specific
213  * messages are silently destroyed upon encounter.
214  *
215  * @param peer Neighboring peer.
216  * @param c Connection.
217  * @param destroyed[in/out] Was the connection destroyed as a result?.
218  *                          Can NOT be NULL.
219  *
220  *
221  * @return First message for this connection.
222  */
223 struct GNUNET_MessageHeader *
224 GCP_connection_pop (struct CadetPeer *peer,
225                     struct CadetConnection *c,
226                     int *destroyed);
227
228 /**
229  * Unlock a possibly locked queue for a connection.
230  *
231  * If there is a message that can be sent on this connection, call core for it.
232  * Otherwise (if core transmit is already called or there is no sendable
233  * message) do nothing.
234  *
235  * @param peer Peer who keeps the queue.
236  * @param c Connection whose messages to unlock.
237  */
238 void
239 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c);
240
241 /**
242  * Set tunnel.
243  *
244  * @param peer Peer.
245  * @param t Tunnel.
246  */
247 void
248 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t);
249
250
251 /**
252  * Check whether there is a direct (core level)  connection to peer.
253  *
254  * @param peer Peer to check.
255  *
256  * @return #GNUNET_YES if there is a direct connection.
257  */
258 int
259 GCP_is_neighbor (const struct CadetPeer *peer);
260
261
262 /**
263  * Create and initialize a new tunnel towards a peer, in case it has none.
264  *
265  * Does not generate any traffic, just creates the local data structures.
266  *
267  * @param peer Peer towards which to create the tunnel.
268  */
269 void
270 GCP_add_tunnel (struct CadetPeer *peer);
271
272
273 /**
274  * Add a connection to a neighboring peer.
275  *
276  * Store that the peer is the first hop of the connection in one
277  * direction and that on peer disconnect the connection must be
278  * notified and destroyed, for it will no longer be valid.
279  *
280  * @param peer Peer to add connection to.
281  * @param c Connection to add.
282  * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
283  */
284 void
285 GCP_add_connection (struct CadetPeer *peer,
286                     struct CadetConnection *c,
287                     int pred);
288
289
290 /**
291  * Add the path to the peer and update the path used to reach it in case this
292  * is the shortest.
293  *
294  * @param peer Destination peer to add the path to.
295  * @param path New path to add. Last peer must be the peer in arg 1.
296  *             Path will be either used of freed if already known.
297  * @param trusted Do we trust that this path is real?
298  *
299  * @return path if path was taken, pointer to existing duplicate if exists
300  *         NULL on error.
301  */
302 struct CadetPeerPath *
303 GCP_add_path (struct CadetPeer *peer,
304               struct CadetPeerPath *p,
305               int trusted);
306
307
308 /**
309  * Add the path to the origin peer and update the path used to reach it in case
310  * this is the shortest.
311  * The path is given in peer_info -> destination, therefore we turn the path
312  * upside down first.
313  *
314  * @param peer Peer to add the path to, being the origin of the path.
315  * @param path New path to add after being inversed.
316  *             Path will be either used or freed.
317  * @param trusted Do we trust that this path is real?
318  *
319  * @return path if path was taken, pointer to existing duplicate if exists
320  *         NULL on error.
321  */
322 struct CadetPeerPath *
323 GCP_add_path_to_origin (struct CadetPeer *peer,
324                         struct CadetPeerPath *path,
325                         int trusted);
326
327 /**
328  * Adds a path to the info of all the peers in the path
329  *
330  * @param p Path to process.
331  * @param confirmed Whether we know if the path works or not.
332  */
333 void
334 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed);
335
336
337 /**
338  * Remove any path to the peer that has the extact same peers as the one given.
339  *
340  * @param peer Peer to remove the path from.
341  * @param path Path to remove. Is always destroyed .
342  */
343 void
344 GCP_remove_path (struct CadetPeer *peer,
345                  struct CadetPeerPath *path);
346
347
348 /**
349  * Check that we are aware of a connection from a neighboring peer.
350  *
351  * @param peer Peer to the connection is with
352  * @param c Connection that should be in the map with this peer.
353  */
354 void
355 GCP_check_connection (const struct CadetPeer *peer,
356                       const struct CadetConnection *c);
357
358
359 /**
360  * Remove a connection from a neighboring peer.
361  *
362  * @param peer Peer to remove connection from.
363  * @param c Connection to remove.
364  */
365 void
366 GCP_remove_connection (struct CadetPeer *peer,
367                        const struct CadetConnection *c);
368
369
370 /**
371  * Start the DHT search for new paths towards the peer: we don't have
372  * enough good connections.
373  *
374  * @param peer Destination peer.
375  */
376 void
377 GCP_start_search (struct CadetPeer *peer);
378
379
380 /**
381  * Stop the DHT search for new paths towards the peer: we already have
382  * enough good connections.
383  *
384  * @param peer Destination peer.
385  */
386 void
387 GCP_stop_search (struct CadetPeer *peer);
388
389
390 /**
391  * Get the Full ID of a peer.
392  *
393  * @param peer Peer to get from.
394  *
395  * @return Full ID of peer.
396  */
397 const struct GNUNET_PeerIdentity *
398 GCP_get_id (const struct CadetPeer *peer);
399
400
401 /**
402  * Get the Short ID of a peer.
403  *
404  * @param peer Peer to get from.
405  *
406  * @return Short ID of peer.
407  */
408 GNUNET_PEER_Id
409 GCP_get_short_id (const struct CadetPeer *peer);
410
411
412 /**
413  * Get the tunnel towards a peer.
414  *
415  * @param peer Peer to get from.
416  *
417  * @return Tunnel towards peer.
418  */
419 struct CadetTunnel *
420 GCP_get_tunnel (const struct CadetPeer *peer);
421
422
423 /**
424  * Set the hello message.
425  *
426  * @param peer Peer whose message to set.
427  * @param hello Hello message.
428  */
429 void
430 GCP_set_hello (struct CadetPeer *peer,
431                const struct GNUNET_HELLO_Message *hello);
432
433
434 /**
435  * Get the hello message.
436  *
437  * @param peer Peer whose message to get.
438  *
439  * @return Hello message.
440  */
441 struct GNUNET_HELLO_Message *
442 GCP_get_hello (struct CadetPeer *peer);
443
444
445 /**
446  * Try to connect to a peer on TRANSPORT level.
447  *
448  * @param peer Peer to whom to connect.
449  */
450 void
451 GCP_try_connect (struct CadetPeer *peer);
452
453 /**
454  * Notify a peer that a link between two other peers is broken. If any path
455  * used that link, eliminate it.
456  *
457  * @param peer Peer affected by the change.
458  * @param peer1 Peer whose link is broken.
459  * @param peer2 Peer whose link is broken.
460  */
461 void
462 GCP_notify_broken_link (struct CadetPeer *peer,
463                         const struct GNUNET_PeerIdentity *peer1,
464                         const struct GNUNET_PeerIdentity *peer2);
465
466
467 /**
468  * Count the number of known paths toward the peer.
469  *
470  * @param peer Peer to get path info.
471  *
472  * @return Number of known paths.
473  */
474 unsigned int
475 GCP_count_paths (const struct CadetPeer *peer);
476
477 /**
478  * Iterate over the paths to a peer.
479  *
480  * @param peer Peer to get path info.
481  * @param callback Function to call for every path.
482  * @param cls Closure for @a callback.
483  *
484  * @return Number of iterated paths.
485  */
486 unsigned int
487 GCP_iterate_paths (struct CadetPeer *peer,
488                    GCP_path_iterator callback,
489                    void *cls);
490
491
492 /**
493  * Iterate all known peers.
494  *
495  * @param iter Iterator.
496  * @param cls Closure for @c iter.
497  */
498 void
499 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
500
501
502 /**
503  * Get the static string for a peer ID.
504  *
505  * @param peer Peer.
506  *
507  * @return Static string for it's ID.
508  */
509 const char *
510 GCP_2s (const struct CadetPeer *peer);
511
512
513 /**
514  * Log all kinds of info about a peer.
515  *
516  * @param peer Peer.
517  */
518 void
519 GCP_debug (const struct CadetPeer *p,
520            enum GNUNET_ErrorType level);
521
522
523 #if 0                           /* keep Emacsens' auto-indent happy */
524 {
525 #endif
526 #ifdef __cplusplus
527 }
528 #endif
529
530 /* ifndef GNUNET_CADET_SERVICE_PEER_H */
531 #endif
532 /* end of gnunet-cadet-service_peer.h */