Fix #3929: cancel poll towards disconnected neighbor
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_connection.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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file cadet/gnunet-service-cadet_connection.h
23  * @brief cadet service; dealing with connections
24  * @author Bartlomiej Polot
25  *
26  * All functions in this file should use the prefix GMC (Gnunet Cadet Connection)
27  */
28
29 #ifndef GNUNET_SERVICE_CADET_CONNECTION_H
30 #define GNUNET_SERVICE_CADET_CONNECTION_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 "gnunet_util_lib.h"
41
42
43 /**
44  * All the states a connection can be in.
45  */
46 enum CadetConnectionState
47 {
48   /**
49    * Uninitialized status, should never appear in operation.
50    */
51   CADET_CONNECTION_NEW,
52
53   /**
54    * Connection create message sent, waiting for ACK.
55    */
56   CADET_CONNECTION_SENT,
57
58   /**
59    * Connection ACK sent, waiting for ACK.
60    */
61   CADET_CONNECTION_ACK,
62
63   /**
64    * Connection confirmed, ready to carry traffic.
65    */
66   CADET_CONNECTION_READY,
67
68   /**
69    * Connection to be destroyed, just waiting to empty queues.
70    */
71   CADET_CONNECTION_DESTROYED,
72
73   /**
74    * Connection to be destroyed because of a distant peer, same as DESTROYED.
75    */
76   CADET_CONNECTION_BROKEN,
77 };
78
79
80 /**
81  * Struct containing all information regarding a connection to a peer.
82  */
83 struct CadetConnection;
84
85 /**
86  * Handle for messages queued but not yet sent.
87  */
88 struct CadetConnectionQueue;
89
90 #include "cadet_path.h"
91 #include "gnunet-service-cadet_channel.h"
92 #include "gnunet-service-cadet_peer.h"
93
94
95 /**
96  * Check invariants for all connections using #check_neighbours().
97  */
98 void
99 GCC_check_connections ();
100
101
102 /**
103  * Callback called when a queued message is sent.
104  *
105  * @param cls Closure.
106  * @param c Connection this message was on.
107  * @param type Type of message sent.
108  * @param fwd Was this a FWD going message?
109  * @param size Size of the message.
110  */
111 typedef void
112 (*GCC_sent) (void *cls,
113              struct CadetConnection *c,
114              struct CadetConnectionQueue *q,
115              uint16_t type,
116              int fwd,
117              size_t size);
118
119
120 /**
121  * Core handler for connection creation.
122  *
123  * @param cls Closure (unused).
124  * @param peer Sender (neighbor).
125  * @param message Message.
126  * @return #GNUNET_OK to keep the connection open,
127  *         #GNUNET_SYSERR to close it (signal serious error)
128  */
129 int
130 GCC_handle_create (void *cls,
131                    const struct GNUNET_PeerIdentity *peer,
132                    const struct GNUNET_MessageHeader *message);
133
134
135 /**
136  * Core handler for path confirmations.
137  *
138  * @param cls closure
139  * @param message message
140  * @param peer peer identity this notification is about
141  * @return #GNUNET_OK to keep the connection open,
142  *         #GNUNET_SYSERR to close it (signal serious error)
143  */
144 int
145 GCC_handle_confirm (void *cls,
146                     const struct GNUNET_PeerIdentity *peer,
147                     const struct GNUNET_MessageHeader *message);
148
149
150 /**
151  * Core handler for notifications of broken paths
152  *
153  * @param cls Closure (unused).
154  * @param id Peer identity of sending neighbor.
155  * @param message Message.
156  * @return #GNUNET_OK to keep the connection open,
157  *         #GNUNET_SYSERR to close it (signal serious error)
158  */
159 int
160 GCC_handle_broken (void* cls,
161                    const struct GNUNET_PeerIdentity* id,
162                    const struct GNUNET_MessageHeader* message);
163
164 /**
165  * Core handler for tunnel destruction
166  *
167  * @param cls Closure (unused).
168  * @param peer Peer identity of sending neighbor.
169  * @param message Message.
170  *
171  * @return GNUNET_OK to keep the connection open,
172  *         GNUNET_SYSERR to close it (signal serious error)
173  */
174 int
175 GCC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
176                     const struct GNUNET_MessageHeader *message);
177
178 /**
179  * Core handler for key exchange traffic (ephemeral key, ping, pong).
180  *
181  * @param cls Closure (unused).
182  * @param message Message received.
183  * @param peer Peer who sent the message.
184  *
185  * @return GNUNET_OK to keep the connection open,
186  *         GNUNET_SYSERR to close it (signal serious error)
187  */
188 int
189 GCC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
190                const struct GNUNET_MessageHeader *message);
191
192 /**
193  * Core handler for encrypted cadet network traffic (channel mgmt, data).
194  *
195  * @param cls Closure (unused).
196  * @param message Message received.
197  * @param peer Peer who sent the message.
198  *
199  * @return GNUNET_OK to keep the connection open,
200  *         GNUNET_SYSERR to close it (signal serious error)
201  */
202 int
203 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
204                       const struct GNUNET_MessageHeader *message);
205
206 /**
207  * Core handler for axolotl key exchange traffic.
208  *
209  * @param cls Closure (unused).
210  * @param message Message received.
211  * @param peer Neighbor who sent the message.
212  *
213  * @return GNUNET_OK, to keep the connection open.
214  */
215 int
216 GCC_handle_ax_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
217                   const struct GNUNET_MessageHeader *message);
218
219 /**
220  * Core handler for axolotl encrypted cadet network traffic.
221  *
222  * @param cls Closure (unused).
223  * @param message Message received.
224  * @param peer Neighbor who sent the message.
225  *
226  * @return GNUNET_OK, to keep the connection open.
227  */
228 int
229 GCC_handle_ax (void *cls, const struct GNUNET_PeerIdentity *peer,
230                struct GNUNET_MessageHeader *message);
231
232 /**
233  * Core handler for cadet network traffic point-to-point acks.
234  *
235  * @param cls closure
236  * @param message message
237  * @param peer peer identity this notification is about
238  *
239  * @return GNUNET_OK to keep the connection open,
240  *         GNUNET_SYSERR to close it (signal serious error)
241  */
242 int
243 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
244                 const struct GNUNET_MessageHeader *message);
245
246 /**
247  * Core handler for cadet network traffic point-to-point ack polls.
248  *
249  * @param cls closure
250  * @param message message
251  * @param peer peer identity this notification is about
252  *
253  * @return GNUNET_OK to keep the connection open,
254  *         GNUNET_SYSERR to close it (signal serious error)
255  */
256 int
257 GCC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
258                  const struct GNUNET_MessageHeader *message);
259
260 /**
261  * Core handler for cadet keepalives.
262  *
263  * @param cls closure
264  * @param message message
265  * @param peer peer identity this notification is about
266  * @return GNUNET_OK to keep the connection open,
267  *         GNUNET_SYSERR to close it (signal serious error)
268  *
269  * TODO: Check who we got this from, to validate route.
270  */
271 int
272 GCC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
273                       const struct GNUNET_MessageHeader *message);
274
275 /**
276  * Send an ACK on the appropriate connection/channel, depending on
277  * the direction and the position of the peer.
278  *
279  * @param c Which connection to send the hop-by-hop ACK.
280  * @param fwd Is this a fwd ACK? (will go dest->root).
281  * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
282  */
283 void
284 GCC_send_ack (struct CadetConnection *c, int fwd, int force);
285
286 /**
287  * Initialize the connections subsystem
288  *
289  * @param c Configuration handle.
290  */
291 void
292 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c);
293
294 /**
295  * Shut down the connections subsystem.
296  */
297 void
298 GCC_shutdown (void);
299
300 /**
301  * Create a connection.
302  *
303  * @param cid Connection ID (either created locally or imposed remotely).
304  * @param t Tunnel this connection belongs to (or NULL);
305  * @param path Path this connection has to use (copy is made).
306  * @param own_pos Own position in the @c path path.
307  *
308  * @return Newly created connection, NULL in case of error (own id not in path).
309  */
310 struct CadetConnection *
311 GCC_new (const struct GNUNET_CADET_Hash *cid,
312          struct CadetTunnel *t,
313          struct CadetPeerPath *path,
314          unsigned int own_pos);
315
316 /**
317  * Connection is no longer needed: destroy it.
318  *
319  * Cancels all pending traffic (including possible DESTROY messages), all
320  * maintenance tasks and removes the connection from neighbor peers and tunnel.
321  *
322  * @param c Connection to destroy.
323  */
324 void
325 GCC_destroy (struct CadetConnection *c);
326
327 /**
328  * Get the connection ID.
329  *
330  * @param c Connection to get the ID from.
331  *
332  * @return ID of the connection.
333  */
334 const struct GNUNET_CADET_Hash *
335 GCC_get_id (const struct CadetConnection *c);
336
337
338 /**
339  * Get a hash for the connection ID.
340  *
341  * @param c Connection to get the hash.
342  *
343  * @return Hash expanded from the ID of the connection.
344  */
345 const struct GNUNET_HashCode *
346 GCC_get_h (const struct CadetConnection *c);
347
348
349 /**
350  * Get the connection path.
351  *
352  * @param c Connection to get the path from.
353  *
354  * @return path used by the connection.
355  */
356 const struct CadetPeerPath *
357 GCC_get_path (const struct CadetConnection *c);
358
359 /**
360  * Get the connection state.
361  *
362  * @param c Connection to get the state from.
363  *
364  * @return state of the connection.
365  */
366 enum CadetConnectionState
367 GCC_get_state (const struct CadetConnection *c);
368
369 /**
370  * Get the connection tunnel.
371  *
372  * @param c Connection to get the tunnel from.
373  *
374  * @return tunnel of the connection.
375  */
376 struct CadetTunnel *
377 GCC_get_tunnel (const struct CadetConnection *c);
378
379 /**
380  * Get free buffer space in a connection.
381  *
382  * @param c Connection.
383  * @param fwd Is query about FWD traffic?
384  *
385  * @return Free buffer space [0 - max_msgs_queue/max_connections]
386  */
387 unsigned int
388 GCC_get_buffer (struct CadetConnection *c, int fwd);
389
390 /**
391  * Get how many messages have we allowed to send to us from a direction.
392  *
393  * @param c Connection.
394  * @param fwd Are we asking about traffic from FWD (BCK messages)?
395  *
396  * @return last_ack_sent - last_pid_recv
397  */
398 unsigned int
399 GCC_get_allowed (struct CadetConnection *c, int fwd);
400
401 /**
402  * Get messages queued in a connection.
403  *
404  * @param c Connection.
405  * @param fwd Is query about FWD traffic?
406  *
407  * @return Number of messages queued.
408  */
409 unsigned int
410 GCC_get_qn (struct CadetConnection *c, int fwd);
411
412 /**
413  * Get next PID to use.
414  *
415  * @param c Connection.
416  * @param fwd Is query about FWD traffic?
417  *
418  * @return Last PID used + 1.
419  */
420 unsigned int
421 GCC_get_pid (struct CadetConnection *c, int fwd);
422
423 /**
424  * Allow the connection to advertise a buffer of the given size.
425  *
426  * The connection will send an @c fwd ACK message (so: in direction !fwd)
427  * allowing up to last_pid_recv + buffer.
428  *
429  * @param c Connection.
430  * @param buffer How many more messages the connection can accept.
431  * @param fwd Is this about FWD traffic? (The ack will go dest->root).
432  */
433 void
434 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd);
435
436 /**
437  * Send FWD keepalive packets for a connection.
438  *
439  * @param cls Closure (connection for which to send the keepalive).
440  * @param tc Notification context.
441  */
442 void
443 GCC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
444
445 /**
446  * Send BCK keepalive packets for a connection.
447  *
448  * @param cls Closure (connection for which to send the keepalive).
449  * @param tc Notification context.
450  */
451 void
452 GCC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
453
454
455 /**
456  * Notify other peers on a connection of a broken link. Mark connections
457  * to destroy after all traffic has been sent.
458  *
459  * @param c Connection on which there has been a disconnection.
460  * @param peer Peer that disconnected.
461  */
462 void
463 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer);
464
465 /**
466  * Is this peer the first one on the connection?
467  *
468  * @param c Connection.
469  * @param fwd Is this about fwd traffic?
470  *
471  * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
472  */
473 int
474 GCC_is_origin (struct CadetConnection *c, int fwd);
475
476 /**
477  * Is this peer the last one on the connection?
478  *
479  * @param c Connection.
480  * @param fwd Is this about fwd traffic?
481  *            Note that the ROOT is the terminal for BCK traffic!
482  *
483  * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
484  */
485 int
486 GCC_is_terminal (struct CadetConnection *c, int fwd);
487
488 /**
489  * See if we are allowed to send by the next hop in the given direction.
490  *
491  * @param c Connection.
492  * @param fwd Is this about fwd traffic?
493  *
494  * @return #GNUNET_YES in case it's OK to send.
495  */
496 int
497 GCC_is_sendable (struct CadetConnection *c, int fwd);
498
499 /**
500  * Check if this connection is a direct one (never trim a direct connection).
501  *
502  * @param c Connection.
503  *
504  * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
505  */
506 int
507 GCC_is_direct (struct CadetConnection *c);
508
509 /**
510  * Cancel a previously sent message while it's in the queue.
511  *
512  * ONLY can be called before the continuation given to the send function
513  * is called. Once the continuation is called, the message is no longer in the
514  * queue.
515  *
516  * @param q Handle to the queue.
517  */
518 void
519 GCC_cancel (struct CadetConnectionQueue *q);
520
521 /**
522  * Sends an already built message on a connection, properly registering
523  * all used resources.
524  *
525  * @param message Message to send. Function makes a copy of it.
526  *                If message is not hop-by-hop, decrements TTL of copy.
527  * @param payload_type Type of payload, in case the message is encrypted.
528  * @param c Connection on which this message is transmitted.
529  * @param fwd Is this a fwd message?
530  * @param force Force the connection to accept the message (buffer overfill).
531  * @param cont Continuation called once message is sent. Can be NULL.
532  * @param cont_cls Closure for @c cont.
533  *
534  * @return Handle to cancel the message before it's sent.
535  *         NULL on error or if @c cont is NULL.
536  *         Invalid on @c cont call.
537  */
538 struct CadetConnectionQueue *
539 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
540                            uint16_t payload_type, uint32_t payload_id,
541                            struct CadetConnection *c, int fwd, int force,
542                            GCC_sent cont, void *cont_cls);
543
544 /**
545  * Sends a CREATE CONNECTION message for a path to a peer.
546  * Changes the connection and tunnel states if necessary.
547  *
548  * @param connection Connection to create.
549  */
550 void
551 GCC_send_create (struct CadetConnection *connection);
552
553 /**
554  * Send a message to all peers in this connection that the connection
555  * is no longer valid.
556  *
557  * If some peer should not receive the message, it should be zero'ed out
558  * before calling this function.
559  *
560  * @param c The connection whose peers to notify.
561  */
562 void
563 GCC_send_destroy (struct CadetConnection *c);
564
565 /**
566  * @brief Start a polling timer for the connection.
567  *
568  * When a neighbor does not accept more traffic on the connection it could be
569  * caused by a simple congestion or by a lost ACK. Polling enables to check
570  * for the lastest ACK status for a connection.
571  *
572  * @param c Connection.
573  * @param fwd Should we poll in the FWD direction?
574  */
575 void
576 GCC_start_poll (struct CadetConnection *c, int fwd);
577
578
579 /**
580  * @brief Stop polling a connection for ACKs.
581  *
582  * Once we have enough ACKs for future traffic, polls are no longer necessary.
583  *
584  * @param c Connection.
585  * @param fwd Should we stop the poll in the FWD direction?
586  */
587 void
588 GCC_stop_poll (struct CadetConnection *c, int fwd);
589
590 /**
591  * Get a (static) string for a connection.
592  *
593  * @param c Connection.
594  */
595 const char *
596 GCC_2s (const struct CadetConnection *c);
597
598 /**
599  * Log all possible info about the connection state.
600  *
601  * @param c Connection to debug.
602  * @param level Debug level to use.
603  */
604 void
605 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level);
606
607 #if 0                           /* keep Emacsens' auto-indent happy */
608 {
609 #endif
610 #ifdef __cplusplus
611 }
612 #endif
613
614 /* ifndef GNUNET_SERVICE_CADET_CONNECTION_H */
615 #endif
616 /* end of gnunet-service-cadet_connection.h */