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