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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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.
105 typedef void (*GCC_sent) (void *cls,
106 struct CadetConnection *c,
107 struct CadetConnectionQueue *q,
108 uint16_t type, int fwd, size_t size);
111 * Core handler for connection creation.
113 * @param cls Closure (unused).
114 * @param peer Sender (neighbor).
115 * @param message Message.
117 * @return GNUNET_OK to keep the connection open,
118 * GNUNET_SYSERR to close it (signal serious error)
121 GCC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
122 const struct GNUNET_MessageHeader *message);
125 * Core handler for path confirmations.
128 * @param message message
129 * @param peer peer identity this notification is about
131 * @return GNUNET_OK to keep the connection open,
132 * GNUNET_SYSERR to close it (signal serious error)
135 GCC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
136 const struct GNUNET_MessageHeader *message);
139 * Core handler for notifications of broken paths
141 * @param cls Closure (unused).
142 * @param id Peer identity of sending neighbor.
143 * @param message Message.
145 * @return GNUNET_OK to keep the connection open,
146 * GNUNET_SYSERR to close it (signal serious error)
149 GCC_handle_broken (void* cls,
150 const struct GNUNET_PeerIdentity* id,
151 const struct GNUNET_MessageHeader* message);
154 * Core handler for tunnel destruction
156 * @param cls Closure (unused).
157 * @param peer Peer identity of sending neighbor.
158 * @param message Message.
160 * @return GNUNET_OK to keep the connection open,
161 * GNUNET_SYSERR to close it (signal serious error)
164 GCC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
165 const struct GNUNET_MessageHeader *message);
168 * Core handler for key exchange traffic (ephemeral key, ping, pong).
170 * @param cls Closure (unused).
171 * @param message Message received.
172 * @param peer Peer who sent the message.
174 * @return GNUNET_OK to keep the connection open,
175 * GNUNET_SYSERR to close it (signal serious error)
178 GCC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
179 const struct GNUNET_MessageHeader *message);
182 * Core handler for encrypted cadet network traffic (channel mgmt, data).
184 * @param cls Closure (unused).
185 * @param message Message received.
186 * @param peer Peer who sent the message.
188 * @return GNUNET_OK to keep the connection open,
189 * GNUNET_SYSERR to close it (signal serious error)
192 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
193 const struct GNUNET_MessageHeader *message);
196 * Core handler for axolotl key exchange traffic.
198 * @param cls Closure (unused).
199 * @param message Message received.
200 * @param peer Neighbor who sent the message.
202 * @return GNUNET_OK, to keep the connection open.
205 GCC_handle_ax_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
206 const struct GNUNET_MessageHeader *message);
209 * Core handler for axolotl encrypted cadet network traffic.
211 * @param cls Closure (unused).
212 * @param message Message received.
213 * @param peer Neighbor who sent the message.
215 * @return GNUNET_OK, to keep the connection open.
218 GCC_handle_ax (void *cls, const struct GNUNET_PeerIdentity *peer,
219 struct GNUNET_MessageHeader *message);
222 * Core handler for cadet network traffic point-to-point acks.
225 * @param message message
226 * @param peer peer identity this notification is about
228 * @return GNUNET_OK to keep the connection open,
229 * GNUNET_SYSERR to close it (signal serious error)
232 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
233 const struct GNUNET_MessageHeader *message);
236 * Core handler for cadet network traffic point-to-point ack polls.
239 * @param message message
240 * @param peer peer identity this notification is about
242 * @return GNUNET_OK to keep the connection open,
243 * GNUNET_SYSERR to close it (signal serious error)
246 GCC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
247 const struct GNUNET_MessageHeader *message);
250 * Core handler for cadet keepalives.
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)
258 * TODO: Check who we got this from, to validate route.
261 GCC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
262 const struct GNUNET_MessageHeader *message);
265 * Send an ACK on the appropriate connection/channel, depending on
266 * the direction and the position of the peer.
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).
273 GCC_send_ack (struct CadetConnection *c, int fwd, int force);
276 * Initialize the connections subsystem
278 * @param c Configuration handle.
281 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c);
284 * Shut down the connections subsystem.
290 * Create a connection.
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.
297 * @return Newly created connection, NULL in case of error (own id not in path).
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);
306 * Connection is no longer needed: destroy it.
308 * Cancels all pending traffic (including possible DESTROY messages), all
309 * maintenance tasks and removes the connection from neighbor peers and tunnel.
311 * @param c Connection to destroy.
314 GCC_destroy (struct CadetConnection *c);
317 * Get the connection ID.
319 * @param c Connection to get the ID from.
321 * @return ID of the connection.
323 const struct GNUNET_CADET_Hash *
324 GCC_get_id (const struct CadetConnection *c);
328 * Get a hash for the connection ID.
330 * @param c Connection to get the hash.
332 * @return Hash expanded from the ID of the connection.
334 const struct GNUNET_HashCode *
335 GCC_get_h (const struct CadetConnection *c);
339 * Get the connection path.
341 * @param c Connection to get the path from.
343 * @return path used by the connection.
345 const struct CadetPeerPath *
346 GCC_get_path (const struct CadetConnection *c);
349 * Get the connection state.
351 * @param c Connection to get the state from.
353 * @return state of the connection.
355 enum CadetConnectionState
356 GCC_get_state (const struct CadetConnection *c);
359 * Get the connection tunnel.
361 * @param c Connection to get the tunnel from.
363 * @return tunnel of the connection.
366 GCC_get_tunnel (const struct CadetConnection *c);
369 * Get free buffer space in a connection.
371 * @param c Connection.
372 * @param fwd Is query about FWD traffic?
374 * @return Free buffer space [0 - max_msgs_queue/max_connections]
377 GCC_get_buffer (struct CadetConnection *c, int fwd);
380 * Get how many messages have we allowed to send to us from a direction.
382 * @param c Connection.
383 * @param fwd Are we asking about traffic from FWD (BCK messages)?
385 * @return last_ack_sent - last_pid_recv
388 GCC_get_allowed (struct CadetConnection *c, int fwd);
391 * Get messages queued in a connection.
393 * @param c Connection.
394 * @param fwd Is query about FWD traffic?
396 * @return Number of messages queued.
399 GCC_get_qn (struct CadetConnection *c, int fwd);
402 * Get next PID to use.
404 * @param c Connection.
405 * @param fwd Is query about FWD traffic?
407 * @return Last PID used + 1.
410 GCC_get_pid (struct CadetConnection *c, int fwd);
413 * Allow the connection to advertise a buffer of the given size.
415 * The connection will send an @c fwd ACK message (so: in direction !fwd)
416 * allowing up to last_pid_recv + buffer.
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).
423 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd);
426 * Send FWD keepalive packets for a connection.
428 * @param cls Closure (connection for which to send the keepalive).
429 * @param tc Notification context.
432 GCC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
435 * Send BCK keepalive packets for a connection.
437 * @param cls Closure (connection for which to send the keepalive).
438 * @param tc Notification context.
441 GCC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
445 * Notify other peers on a connection of a broken link. Mark connections
446 * to destroy after all traffic has been sent.
448 * @param c Connection on which there has been a disconnection.
449 * @param peer Peer that disconnected.
452 GCC_notify_broken (struct CadetConnection *c,
453 struct CadetPeer *peer);
456 * Is this peer the first one on the connection?
458 * @param c Connection.
459 * @param fwd Is this about fwd traffic?
461 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
464 GCC_is_origin (struct CadetConnection *c, int fwd);
467 * Is this peer the last one on the connection?
469 * @param c Connection.
470 * @param fwd Is this about fwd traffic?
471 * Note that the ROOT is the terminal for BCK traffic!
473 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
476 GCC_is_terminal (struct CadetConnection *c, int fwd);
479 * See if we are allowed to send by the next hop in the given direction.
481 * @param c Connection.
482 * @param fwd Is this about fwd traffic?
484 * @return #GNUNET_YES in case it's OK to send.
487 GCC_is_sendable (struct CadetConnection *c, int fwd);
490 * Check if this connection is a direct one (never trim a direct connection).
492 * @param c Connection.
494 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
497 GCC_is_direct (struct CadetConnection *c);
500 * Cancel a previously sent message while it's in the queue.
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
506 * @param q Handle to the queue.
509 GCC_cancel (struct CadetConnectionQueue *q);
512 * Sends an already built message on a connection, properly registering
513 * all used resources.
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.
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.
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);
535 * Sends a CREATE CONNECTION message for a path to a peer.
536 * Changes the connection and tunnel states if necessary.
538 * @param connection Connection to create.
541 GCC_send_create (struct CadetConnection *connection);
544 * Send a message to all peers in this connection that the connection
545 * is no longer valid.
547 * If some peer should not receive the message, it should be zero'ed out
548 * before calling this function.
550 * @param c The connection whose peers to notify.
553 GCC_send_destroy (struct CadetConnection *c);
556 * @brief Start a polling timer for the connection.
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.
562 * @param c Connection.
563 * @param fwd Should we poll in the FWD direction?
566 GCC_start_poll (struct CadetConnection *c, int fwd);
570 * @brief Stop polling a connection for ACKs.
572 * Once we have enough ACKs for future traffic, polls are no longer necessary.
574 * @param c Connection.
575 * @param fwd Should we stop the poll in the FWD direction?
578 GCC_stop_poll (struct CadetConnection *c, int fwd);
581 * Get a (static) string for a connection.
583 * @param c Connection.
586 GCC_2s (const struct CadetConnection *c);
589 * Log all possible info about the connection state.
591 * @param c Connection to debug.
592 * @param level Debug level to use.
595 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level);
597 #if 0 /* keep Emacsens' auto-indent happy */
604 /* ifndef GNUNET_SERVICE_CADET_CONNECTION_H */
606 /* end of gnunet-service-cadet_connection.h */