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