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