2 This file is part of GNUnet.
3 (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 encrypted cadet network traffic (channel mgmt, data).
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_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
179 const struct GNUNET_MessageHeader *message);
182 * Core handler for key exchange traffic (ephemeral key, ping, pong).
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_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
193 const struct GNUNET_MessageHeader *message);
196 * Core handler for cadet network traffic point-to-point acks.
199 * @param message message
200 * @param peer peer identity this notification is about
202 * @return GNUNET_OK to keep the connection open,
203 * GNUNET_SYSERR to close it (signal serious error)
206 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
207 const struct GNUNET_MessageHeader *message);
210 * Core handler for cadet network traffic point-to-point ack polls.
213 * @param message message
214 * @param peer peer identity this notification is about
216 * @return GNUNET_OK to keep the connection open,
217 * GNUNET_SYSERR to close it (signal serious error)
220 GCC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
221 const struct GNUNET_MessageHeader *message);
224 * Core handler for cadet keepalives.
227 * @param message message
228 * @param peer peer identity this notification is about
229 * @return GNUNET_OK to keep the connection open,
230 * GNUNET_SYSERR to close it (signal serious error)
232 * TODO: Check who we got this from, to validate route.
235 GCC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
236 const struct GNUNET_MessageHeader *message);
239 * Send an ACK on the appropriate connection/channel, depending on
240 * the direction and the position of the peer.
242 * @param c Which connection to send the hop-by-hop ACK.
243 * @param fwd Is this a fwd ACK? (will go dest->root).
244 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
247 GCC_send_ack (struct CadetConnection *c, int fwd, int force);
250 * Initialize the connections subsystem
252 * @param c Configuration handle.
255 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c);
258 * Shut down the connections subsystem.
264 * Create a connection.
266 * @param cid Connection ID (either created locally or imposed remotely).
267 * @param t Tunnel this connection belongs to (or NULL);
268 * @param path Path this connection has to use (copy is made).
269 * @param own_pos Own position in the @c path path.
271 * @return Newly created connection, NULL in case of error (own id not in path).
273 struct CadetConnection *
274 GCC_new (const struct GNUNET_CADET_Hash *cid,
275 struct CadetTunnel *t,
276 struct CadetPeerPath *path,
277 unsigned int own_pos);
280 * Connection is no longer needed: destroy it.
282 * Cancels all pending traffic (including possible DESTROY messages), all
283 * maintenance tasks and removes the connection from neighbor peers and tunnel.
285 * @param c Connection to destroy.
288 GCC_destroy (struct CadetConnection *c);
291 * Get the connection ID.
293 * @param c Connection to get the ID from.
295 * @return ID of the connection.
297 const struct GNUNET_CADET_Hash *
298 GCC_get_id (const struct CadetConnection *c);
302 * Get a hash for the connection ID.
304 * @param c Connection to get the hash.
306 * @return Hash expanded from the ID of the connection.
308 const struct GNUNET_HashCode *
309 GCC_get_h (const struct CadetConnection *c);
313 * Get the connection path.
315 * @param c Connection to get the path from.
317 * @return path used by the connection.
319 const struct CadetPeerPath *
320 GCC_get_path (const struct CadetConnection *c);
323 * Get the connection state.
325 * @param c Connection to get the state from.
327 * @return state of the connection.
329 enum CadetConnectionState
330 GCC_get_state (const struct CadetConnection *c);
333 * Get the connection tunnel.
335 * @param c Connection to get the tunnel from.
337 * @return tunnel of the connection.
340 GCC_get_tunnel (const struct CadetConnection *c);
343 * Get free buffer space in a connection.
345 * @param c Connection.
346 * @param fwd Is query about FWD traffic?
348 * @return Free buffer space [0 - max_msgs_queue/max_connections]
351 GCC_get_buffer (struct CadetConnection *c, int fwd);
354 * Get how many messages have we allowed to send to us from a direction.
356 * @param c Connection.
357 * @param fwd Are we asking about traffic from FWD (BCK messages)?
359 * @return last_ack_sent - last_pid_recv
362 GCC_get_allowed (struct CadetConnection *c, int fwd);
365 * Get messages queued in a connection.
367 * @param c Connection.
368 * @param fwd Is query about FWD traffic?
370 * @return Number of messages queued.
373 GCC_get_qn (struct CadetConnection *c, int fwd);
376 * Get next PID to use.
378 * @param c Connection.
379 * @param fwd Is query about FWD traffic?
381 * @return Last PID used + 1.
384 GCC_get_pid (struct CadetConnection *c, int fwd);
387 * Allow the connection to advertise a buffer of the given size.
389 * The connection will send an @c fwd ACK message (so: in direction !fwd)
390 * allowing up to last_pid_recv + buffer.
392 * @param c Connection.
393 * @param buffer How many more messages the connection can accept.
394 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
397 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd);
400 * Send FWD keepalive packets for a connection.
402 * @param cls Closure (connection for which to send the keepalive).
403 * @param tc Notification context.
406 GCC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
409 * Send BCK keepalive packets for a connection.
411 * @param cls Closure (connection for which to send the keepalive).
412 * @param tc Notification context.
415 GCC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
419 * Notify other peers on a connection of a broken link. Mark connections
420 * to destroy after all traffic has been sent.
422 * @param c Connection on which there has been a disconnection.
423 * @param peer Peer that disconnected.
426 GCC_notify_broken (struct CadetConnection *c,
427 struct CadetPeer *peer);
430 * Is this peer the first one on the connection?
432 * @param c Connection.
433 * @param fwd Is this about fwd traffic?
435 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
438 GCC_is_origin (struct CadetConnection *c, int fwd);
441 * Is this peer the last one on the connection?
443 * @param c Connection.
444 * @param fwd Is this about fwd traffic?
445 * Note that the ROOT is the terminal for BCK traffic!
447 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
450 GCC_is_terminal (struct CadetConnection *c, int fwd);
453 * See if we are allowed to send by the next hop in the given direction.
455 * @param c Connection.
456 * @param fwd Is this about fwd traffic?
458 * @return #GNUNET_YES in case it's OK to send.
461 GCC_is_sendable (struct CadetConnection *c, int fwd);
464 * Check if this connection is a direct one (never trim a direct connection).
466 * @param c Connection.
468 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
471 GCC_is_direct (struct CadetConnection *c);
474 * Cancel a previously sent message while it's in the queue.
476 * ONLY can be called before the continuation given to the send function
477 * is called. Once the continuation is called, the message is no longer in the
480 * @param q Handle to the queue.
483 GCC_cancel (struct CadetConnectionQueue *q);
486 * Sends an already built message on a connection, properly registering
487 * all used resources.
489 * @param message Message to send. Function makes a copy of it.
490 * If message is not hop-by-hop, decrements TTL of copy.
491 * @param payload_type Type of payload, in case the message is encrypted.
492 * @param c Connection on which this message is transmitted.
493 * @param fwd Is this a fwd message?
494 * @param force Force the connection to accept the message (buffer overfill).
495 * @param cont Continuation called once message is sent. Can be NULL.
496 * @param cont_cls Closure for @c cont.
498 * @return Handle to cancel the message before it's sent.
499 * NULL on error or if @c cont is NULL.
500 * Invalid on @c cont call.
502 struct CadetConnectionQueue *
503 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
504 uint16_t payload_type, uint32_t payload_id,
505 struct CadetConnection *c, int fwd, int force,
506 GCC_sent cont, void *cont_cls);
509 * Sends a CREATE CONNECTION message for a path to a peer.
510 * Changes the connection and tunnel states if necessary.
512 * @param connection Connection to create.
515 GCC_send_create (struct CadetConnection *connection);
518 * Send a message to all peers in this connection that the connection
519 * is no longer valid.
521 * If some peer should not receive the message, it should be zero'ed out
522 * before calling this function.
524 * @param c The connection whose peers to notify.
527 GCC_send_destroy (struct CadetConnection *c);
530 * @brief Start a polling timer for the connection.
532 * When a neighbor does not accept more traffic on the connection it could be
533 * caused by a simple congestion or by a lost ACK. Polling enables to check
534 * for the lastest ACK status for a connection.
536 * @param c Connection.
537 * @param fwd Should we poll in the FWD direction?
540 GCC_start_poll (struct CadetConnection *c, int fwd);
544 * @brief Stop polling a connection for ACKs.
546 * Once we have enough ACKs for future traffic, polls are no longer necessary.
548 * @param c Connection.
549 * @param fwd Should we stop the poll in the FWD direction?
552 GCC_stop_poll (struct CadetConnection *c, int fwd);
555 * Get a (static) string for a connection.
557 * @param c Connection.
560 GCC_2s (const struct CadetConnection *c);
563 * Log all possible info about the connection state.
565 * @param c Connection to debug.
566 * @param level Debug level to use.
569 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level);
571 #if 0 /* keep Emacsens' auto-indent happy */
578 /* ifndef GNUNET_SERVICE_CADET_CONNECTION_H */
580 /* end of gnunet-service-cadet_connection.h */