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