2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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"
96 * Check invariants for all connections using #check_neighbours().
99 GCC_check_connections (void);
103 * Callback called when a queued message is sent.
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.
112 (*GCC_sent) (void *cls,
113 struct CadetConnection *c,
114 struct CadetConnectionQueue *q,
121 * Handler for connection creation.
123 * @param peer Message sender (neighbor).
124 * @param msg Message itself.
127 GCC_handle_create (struct CadetPeer *peer,
128 const struct GNUNET_CADET_ConnectionCreate *msg);
132 * Handler for connection confirmations.
134 * @param peer Message sender (neighbor).
135 * @param msg Message itself.
138 GCC_handle_confirm (struct CadetPeer *peer,
139 const struct GNUNET_CADET_ConnectionACK *msg);
143 * Handler for notifications of broken connections.
145 * @param peer Message sender (neighbor).
146 * @param msg Message itself.
149 GCC_handle_broken (struct CadetPeer *peer,
150 const struct GNUNET_CADET_ConnectionBroken *msg);
153 * Handler for notifications of destroyed connections.
155 * @param peer Message sender (neighbor).
156 * @param msg Message itself.
159 GCC_handle_destroy (struct CadetPeer *peer,
160 const struct GNUNET_CADET_ConnectionDestroy *msg);
163 * Handler for cadet network traffic hop-by-hop acks.
165 * @param peer Message sender (neighbor).
166 * @param msg Message itself.
169 GCC_handle_ack (struct CadetPeer *peer,
170 const struct GNUNET_CADET_ACK *msg);
173 * Handler for cadet network traffic hop-by-hop data counter polls.
175 * @param peer Message sender (neighbor).
176 * @param msg Message itself.
179 GCC_handle_poll (struct CadetPeer *peer,
180 const struct GNUNET_CADET_Poll *msg);
183 * Handler for key exchange traffic (Axolotl KX).
185 * @param peer Message sender (neighbor).
186 * @param msg Message itself.
189 GCC_handle_kx (struct CadetPeer *peer,
190 const struct GNUNET_CADET_KX *msg);
193 * Handler for encrypted cadet network traffic (channel mgmt, data).
195 * @param peer Message sender (neighbor).
196 * @param msg Message itself.
199 GCC_handle_encrypted (struct CadetPeer *peer,
200 const struct GNUNET_CADET_AX *msg);
203 * Core handler for axolotl key exchange traffic.
205 * @param cls Closure (unused).
206 * @param message Message received.
207 * @param peer Neighbor who sent the message.
209 * @return GNUNET_OK, to keep the connection open.
212 GCC_handle_ax_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
213 const struct GNUNET_MessageHeader *message);
216 * Core handler for axolotl encrypted cadet network traffic.
218 * @param cls Closure (unused).
219 * @param message Message received.
220 * @param peer Neighbor who sent the message.
222 * @return GNUNET_OK, to keep the connection open.
225 GCC_handle_ax (void *cls, const struct GNUNET_PeerIdentity *peer,
226 struct GNUNET_MessageHeader *message);
229 * Core handler for cadet keepalives.
232 * @param message message
233 * @param peer peer identity this notification is about
234 * @return GNUNET_OK to keep the connection open,
235 * GNUNET_SYSERR to close it (signal serious error)
237 * TODO: Check who we got this from, to validate route.
240 GCC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
241 const struct GNUNET_MessageHeader *message);
244 * Send an ACK on the appropriate connection/channel, depending on
245 * the direction and the position of the peer.
247 * @param c Which connection to send the hop-by-hop ACK.
248 * @param fwd Is this a fwd ACK? (will go dest->root).
249 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
252 GCC_send_ack (struct CadetConnection *c, int fwd, int force);
255 * Initialize the connections subsystem
257 * @param c Configuration handle.
260 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c);
263 * Shut down the connections subsystem.
269 * Create a connection.
271 * @param cid Connection ID (either created locally or imposed remotely).
272 * @param t Tunnel this connection belongs to (or NULL for transit connections);
273 * @param path Path this connection has to use (copy is made).
274 * @param own_pos Own position in the @c path path.
276 * @return Newly created connection.
277 * NULL in case of error: own id not in path, wrong neighbors, ...
279 struct CadetConnection *
280 GCC_new (const struct GNUNET_CADET_Hash *cid,
281 struct CadetTunnel *t,
282 struct CadetPeerPath *path,
283 unsigned int own_pos);
286 * Connection is no longer needed: destroy it.
288 * Cancels all pending traffic (including possible DESTROY messages), all
289 * maintenance tasks and removes the connection from neighbor peers and tunnel.
291 * @param c Connection to destroy.
294 GCC_destroy (struct CadetConnection *c);
297 * Get the connection ID.
299 * @param c Connection to get the ID from.
301 * @return ID of the connection.
303 const struct GNUNET_CADET_Hash *
304 GCC_get_id (const struct CadetConnection *c);
308 * Get a hash for the connection ID.
310 * @param c Connection to get the hash.
312 * @return Hash expanded from the ID of the connection.
314 const struct GNUNET_HashCode *
315 GCC_get_h (const struct CadetConnection *c);
319 * Get the connection path.
321 * @param c Connection to get the path from.
323 * @return path used by the connection.
325 const struct CadetPeerPath *
326 GCC_get_path (const struct CadetConnection *c);
329 * Get the connection state.
331 * @param c Connection to get the state from.
333 * @return state of the connection.
335 enum CadetConnectionState
336 GCC_get_state (const struct CadetConnection *c);
339 * Get the connection tunnel.
341 * @param c Connection to get the tunnel from.
343 * @return tunnel of the connection.
346 GCC_get_tunnel (const struct CadetConnection *c);
349 * Get free buffer space in a connection.
351 * @param c Connection.
352 * @param fwd Is query about FWD traffic?
354 * @return Free buffer space [0 - max_msgs_queue/max_connections]
357 GCC_get_buffer (struct CadetConnection *c, int fwd);
360 * Get how many messages have we allowed to send to us from a direction.
362 * @param c Connection.
363 * @param fwd Are we asking about traffic from FWD (BCK messages)?
365 * @return last_ack_sent - last_pid_recv
368 GCC_get_allowed (struct CadetConnection *c, int fwd);
371 * Get messages queued in a connection.
373 * @param c Connection.
374 * @param fwd Is query about FWD traffic?
376 * @return Number of messages queued.
379 GCC_get_qn (struct CadetConnection *c, int fwd);
382 * Get next PID to use.
384 * @param c Connection.
385 * @param fwd Is query about FWD traffic?
387 * @return Next PID to use.
390 GCC_get_pid (struct CadetConnection *c, int fwd);
393 * Allow the connection to advertise a buffer of the given size.
395 * The connection will send an @c fwd ACK message (so: in direction !fwd)
396 * allowing up to last_pid_recv + buffer.
398 * @param c Connection.
399 * @param buffer How many more messages the connection can accept.
400 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
403 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd);
406 * Send FWD keepalive packets for a connection.
408 * @param cls Closure (connection for which to send the keepalive).
409 * @param tc Notification context.
412 GCC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
415 * Send BCK keepalive packets for a connection.
417 * @param cls Closure (connection for which to send the keepalive).
418 * @param tc Notification context.
421 GCC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
425 * Notify other peers on a connection of a broken link. Mark connections
426 * to destroy after all traffic has been sent.
428 * @param c Connection on which there has been a disconnection.
429 * @param peer Peer that disconnected.
432 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer);
435 * Is this peer the first one on the connection?
437 * @param c Connection.
438 * @param fwd Is this about fwd traffic?
440 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
443 GCC_is_origin (struct CadetConnection *c, int fwd);
446 * Is this peer the last one on the connection?
448 * @param c Connection.
449 * @param fwd Is this about fwd traffic?
450 * Note that the ROOT is the terminal for BCK traffic!
452 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
455 GCC_is_terminal (struct CadetConnection *c, int fwd);
458 * See if we are allowed to send by the next hop in the given direction.
460 * @param c Connection.
461 * @param fwd Is this about fwd traffic?
463 * @return #GNUNET_YES in case it's OK to send.
466 GCC_is_sendable (struct CadetConnection *c, int fwd);
469 * Check if this connection is a direct one (never trim a direct connection).
471 * @param c Connection.
473 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
476 GCC_is_direct (struct CadetConnection *c);
479 * Cancel a previously sent message while it's in the queue.
481 * ONLY can be called before the continuation given to the send function
482 * is called. Once the continuation is called, the message is no longer in the
485 * @param q Handle to the queue.
488 GCC_cancel (struct CadetConnectionQueue *q);
491 * Sends an already built message on a connection, properly registering
492 * all used resources.
494 * @param message Message to send.
495 * @param payload_type Type of payload, in case the message is encrypted.
496 * @param payload_id ID of the payload (PID, ACK, ...).
497 * @param c Connection on which this message is transmitted.
498 * @param fwd Is this a fwd message?
499 * @param force Force the connection to accept the message (buffer overfill).
500 * @param cont Continuation called once message is sent. Can be NULL.
501 * @param cont_cls Closure for @c cont.
503 * @return Handle to cancel the message before it's sent.
504 * NULL on error or if @c cont is NULL.
505 * Invalid on @c cont call.
507 struct CadetConnectionQueue *
508 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
509 uint16_t payload_type, uint32_t payload_id,
510 struct CadetConnection *c, int fwd, int force,
511 GCC_sent cont, void *cont_cls);
514 * Sends a CREATE CONNECTION message for a path to a peer.
515 * Changes the connection and tunnel states if necessary.
517 * @param connection Connection to create.
520 GCC_send_create (struct CadetConnection *connection);
523 * Send a message to all peers in this connection that the connection
524 * is no longer valid.
526 * If some peer should not receive the message, it should be zero'ed out
527 * before calling this function.
529 * @param c The connection whose peers to notify.
532 GCC_send_destroy (struct CadetConnection *c);
535 * @brief Start a polling timer for the connection.
537 * When a neighbor does not accept more traffic on the connection it could be
538 * caused by a simple congestion or by a lost ACK. Polling enables to check
539 * for the lastest ACK status for a connection.
541 * @param c Connection.
542 * @param fwd Should we poll in the FWD direction?
545 GCC_start_poll (struct CadetConnection *c, int fwd);
549 * @brief Stop polling a connection for ACKs.
551 * Once we have enough ACKs for future traffic, polls are no longer necessary.
553 * @param c Connection.
554 * @param fwd Should we stop the poll in the FWD direction?
557 GCC_stop_poll (struct CadetConnection *c, int fwd);
560 * Get a (static) string for a connection.
562 * @param c Connection.
565 GCC_2s (const struct CadetConnection *c);
568 * Log all possible info about the connection state.
570 * @param c Connection to debug.
571 * @param level Debug level to use.
574 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level);
576 #if 0 /* keep Emacsens' auto-indent happy */
583 /* ifndef GNUNET_SERVICE_CADET_CONNECTION_H */
585 /* end of gnunet-service-cadet_connection.h */