2 This file is part of GNUnet.
3 Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file cadet/gnunet-service-cadet_connection.h
23 * @brief cadet service; dealing with connections
24 * @author Bartlomiej Polot
26 * All functions in this file should use the prefix GMC (Gnunet Cadet Connection)
29 #ifndef GNUNET_SERVICE_CADET_CONNECTION_H
30 #define GNUNET_SERVICE_CADET_CONNECTION_H
35 #if 0 /* keep Emacsens' auto-indent happy */
40 #include "gnunet_util_lib.h"
44 * All the states a connection can be in.
46 enum CadetConnectionState
49 * Uninitialized status, should never appear in operation.
54 * Connection create message sent, waiting for ACK.
56 CADET_CONNECTION_SENT,
59 * Connection ACK sent, waiting for ACK.
64 * Connection confirmed, ready to carry traffic.
66 CADET_CONNECTION_READY,
69 * Connection to be destroyed, just waiting to empty queues.
71 CADET_CONNECTION_DESTROYED,
74 * Connection to be destroyed because of a distant peer, same as DESTROYED.
76 CADET_CONNECTION_BROKEN,
81 * Struct containing all information regarding a connection to a peer.
83 struct CadetConnection;
86 * Handle for messages queued but not yet sent.
88 struct CadetConnectionQueue;
90 #include "cadet_path.h"
91 #include "gnunet-service-cadet_channel.h"
92 #include "gnunet-service-cadet_peer.h"
97 * Callback called when a queued message is sent.
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.
106 (*GCC_sent) (void *cls,
107 struct CadetConnection *c,
108 struct CadetConnectionQueue *q,
115 * Core handler for connection creation.
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)
124 GCC_handle_create (void *cls,
125 const struct GNUNET_PeerIdentity *peer,
126 const struct GNUNET_MessageHeader *message);
130 * Core handler for path confirmations.
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)
139 GCC_handle_confirm (void *cls,
140 const struct GNUNET_PeerIdentity *peer,
141 const struct GNUNET_MessageHeader *message);
145 * Core handler for notifications of broken paths
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)
154 GCC_handle_broken (void* cls,
155 const struct GNUNET_PeerIdentity* id,
156 const struct GNUNET_MessageHeader* message);
159 * Core handler for tunnel destruction
161 * @param cls Closure (unused).
162 * @param peer Peer identity of sending neighbor.
163 * @param message Message.
165 * @return GNUNET_OK to keep the connection open,
166 * GNUNET_SYSERR to close it (signal serious error)
169 GCC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
170 const struct GNUNET_MessageHeader *message);
173 * Core handler for key exchange traffic (ephemeral key, ping, pong).
175 * @param cls Closure (unused).
176 * @param message Message received.
177 * @param peer Peer who sent the message.
179 * @return GNUNET_OK to keep the connection open,
180 * GNUNET_SYSERR to close it (signal serious error)
183 GCC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
184 const struct GNUNET_MessageHeader *message);
187 * Core handler for encrypted cadet network traffic (channel mgmt, data).
189 * @param cls Closure (unused).
190 * @param message Message received.
191 * @param peer Peer who sent the message.
193 * @return GNUNET_OK to keep the connection open,
194 * GNUNET_SYSERR to close it (signal serious error)
197 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
198 const struct GNUNET_MessageHeader *message);
201 * Core handler for axolotl key exchange traffic.
203 * @param cls Closure (unused).
204 * @param message Message received.
205 * @param peer Neighbor who sent the message.
207 * @return GNUNET_OK, to keep the connection open.
210 GCC_handle_ax_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
211 const struct GNUNET_MessageHeader *message);
214 * Core handler for axolotl encrypted cadet network traffic.
216 * @param cls Closure (unused).
217 * @param message Message received.
218 * @param peer Neighbor who sent the message.
220 * @return GNUNET_OK, to keep the connection open.
223 GCC_handle_ax (void *cls, const struct GNUNET_PeerIdentity *peer,
224 struct GNUNET_MessageHeader *message);
227 * Core handler for cadet network traffic point-to-point acks.
230 * @param message message
231 * @param peer peer identity this notification is about
233 * @return GNUNET_OK to keep the connection open,
234 * GNUNET_SYSERR to close it (signal serious error)
237 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
238 const struct GNUNET_MessageHeader *message);
241 * Core handler for cadet network traffic point-to-point ack polls.
244 * @param message message
245 * @param peer peer identity this notification is about
247 * @return GNUNET_OK to keep the connection open,
248 * GNUNET_SYSERR to close it (signal serious error)
251 GCC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
252 const struct GNUNET_MessageHeader *message);
255 * Core handler for cadet keepalives.
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)
263 * TODO: Check who we got this from, to validate route.
266 GCC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
267 const struct GNUNET_MessageHeader *message);
270 * Send an ACK on the appropriate connection/channel, depending on
271 * the direction and the position of the peer.
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).
278 GCC_send_ack (struct CadetConnection *c, int fwd, int force);
281 * Initialize the connections subsystem
283 * @param c Configuration handle.
286 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c);
289 * Shut down the connections subsystem.
295 * Create a connection.
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.
302 * @return Newly created connection, NULL in case of error (own id not in path).
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);
311 * Connection is no longer needed: destroy it.
313 * Cancels all pending traffic (including possible DESTROY messages), all
314 * maintenance tasks and removes the connection from neighbor peers and tunnel.
316 * @param c Connection to destroy.
319 GCC_destroy (struct CadetConnection *c);
322 * Get the connection ID.
324 * @param c Connection to get the ID from.
326 * @return ID of the connection.
328 const struct GNUNET_CADET_Hash *
329 GCC_get_id (const struct CadetConnection *c);
333 * Get a hash for the connection ID.
335 * @param c Connection to get the hash.
337 * @return Hash expanded from the ID of the connection.
339 const struct GNUNET_HashCode *
340 GCC_get_h (const struct CadetConnection *c);
344 * Get the connection path.
346 * @param c Connection to get the path from.
348 * @return path used by the connection.
350 const struct CadetPeerPath *
351 GCC_get_path (const struct CadetConnection *c);
354 * Get the connection state.
356 * @param c Connection to get the state from.
358 * @return state of the connection.
360 enum CadetConnectionState
361 GCC_get_state (const struct CadetConnection *c);
364 * Get the connection tunnel.
366 * @param c Connection to get the tunnel from.
368 * @return tunnel of the connection.
371 GCC_get_tunnel (const struct CadetConnection *c);
374 * Get free buffer space in a connection.
376 * @param c Connection.
377 * @param fwd Is query about FWD traffic?
379 * @return Free buffer space [0 - max_msgs_queue/max_connections]
382 GCC_get_buffer (struct CadetConnection *c, int fwd);
385 * Get how many messages have we allowed to send to us from a direction.
387 * @param c Connection.
388 * @param fwd Are we asking about traffic from FWD (BCK messages)?
390 * @return last_ack_sent - last_pid_recv
393 GCC_get_allowed (struct CadetConnection *c, int fwd);
396 * Get messages queued in a connection.
398 * @param c Connection.
399 * @param fwd Is query about FWD traffic?
401 * @return Number of messages queued.
404 GCC_get_qn (struct CadetConnection *c, int fwd);
407 * Get next PID to use.
409 * @param c Connection.
410 * @param fwd Is query about FWD traffic?
412 * @return Last PID used + 1.
415 GCC_get_pid (struct CadetConnection *c, int fwd);
418 * Allow the connection to advertise a buffer of the given size.
420 * The connection will send an @c fwd ACK message (so: in direction !fwd)
421 * allowing up to last_pid_recv + buffer.
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).
428 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd);
431 * Send FWD keepalive packets for a connection.
433 * @param cls Closure (connection for which to send the keepalive).
434 * @param tc Notification context.
437 GCC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
440 * Send BCK keepalive packets for a connection.
442 * @param cls Closure (connection for which to send the keepalive).
443 * @param tc Notification context.
446 GCC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
450 * Notify other peers on a connection of a broken link. Mark connections
451 * to destroy after all traffic has been sent.
453 * @param c Connection on which there has been a disconnection.
454 * @param peer Peer that disconnected.
457 GCC_notify_broken (struct CadetConnection *c,
458 struct CadetPeer *peer);
461 * Is this peer the first one on the connection?
463 * @param c Connection.
464 * @param fwd Is this about fwd traffic?
466 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
469 GCC_is_origin (struct CadetConnection *c, int fwd);
472 * Is this peer the last one on the connection?
474 * @param c Connection.
475 * @param fwd Is this about fwd traffic?
476 * Note that the ROOT is the terminal for BCK traffic!
478 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
481 GCC_is_terminal (struct CadetConnection *c, int fwd);
484 * See if we are allowed to send by the next hop in the given direction.
486 * @param c Connection.
487 * @param fwd Is this about fwd traffic?
489 * @return #GNUNET_YES in case it's OK to send.
492 GCC_is_sendable (struct CadetConnection *c, int fwd);
495 * Check if this connection is a direct one (never trim a direct connection).
497 * @param c Connection.
499 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
502 GCC_is_direct (struct CadetConnection *c);
505 * Cancel a previously sent message while it's in the queue.
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
511 * @param q Handle to the queue.
514 GCC_cancel (struct CadetConnectionQueue *q);
517 * Sends an already built message on a connection, properly registering
518 * all used resources.
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.
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.
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);
540 * Sends a CREATE CONNECTION message for a path to a peer.
541 * Changes the connection and tunnel states if necessary.
543 * @param connection Connection to create.
546 GCC_send_create (struct CadetConnection *connection);
549 * Send a message to all peers in this connection that the connection
550 * is no longer valid.
552 * If some peer should not receive the message, it should be zero'ed out
553 * before calling this function.
555 * @param c The connection whose peers to notify.
558 GCC_send_destroy (struct CadetConnection *c);
561 * @brief Start a polling timer for the connection.
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.
567 * @param c Connection.
568 * @param fwd Should we poll in the FWD direction?
571 GCC_start_poll (struct CadetConnection *c, int fwd);
575 * @brief Stop polling a connection for ACKs.
577 * Once we have enough ACKs for future traffic, polls are no longer necessary.
579 * @param c Connection.
580 * @param fwd Should we stop the poll in the FWD direction?
583 GCC_stop_poll (struct CadetConnection *c, int fwd);
586 * Get a (static) string for a connection.
588 * @param c Connection.
591 GCC_2s (const struct CadetConnection *c);
594 * Log all possible info about the connection state.
596 * @param c Connection to debug.
597 * @param level Debug level to use.
600 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level);
602 #if 0 /* keep Emacsens' auto-indent happy */
609 /* ifndef GNUNET_SERVICE_CADET_CONNECTION_H */
611 /* end of gnunet-service-cadet_connection.h */