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 mesh/gnunet-service-mesh_connection.h
23 * @brief mesh service; dealing with connections
24 * @author Bartlomiej Polot
26 * All functions in this file should use the prefix GMC (Gnunet Mesh Connection)
29 #ifndef GNUNET_SERVICE_MESH_CONNECTION_H
30 #define GNUNET_SERVICE_MESH_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 MeshConnectionState
49 * Uninitialized status, should never appear in operation.
54 * Connection create message sent, waiting for ACK.
59 * Connection ACK sent, waiting for ACK.
64 * Connection confirmed, ready to carry traffic.
66 MESH_CONNECTION_READY,
69 * Connection to be destroyed, just waiting to empty queues.
71 MESH_CONNECTION_DESTROYED,
76 * Struct containing all information regarding a connection to a peer.
78 struct MeshConnection;
81 * Handle for messages queued but not yet sent.
83 struct MeshConnectionQueue;
85 #include "mesh_path.h"
86 #include "gnunet-service-mesh_channel.h"
87 #include "gnunet-service-mesh_peer.h"
92 * Callback called when a queued message is sent.
95 * @param c Connection this message was on.
96 * @param type Type of message sent.
97 * @param fwd Was this a FWD going message?
98 * @param size Size of the message.
100 typedef void (*GMC_sent) (void *cls,
101 struct MeshConnection *c,
102 struct MeshConnectionQueue *q,
103 uint16_t type, int fwd, size_t size);
106 * Core handler for connection creation.
108 * @param cls Closure (unused).
109 * @param peer Sender (neighbor).
110 * @param message Message.
112 * @return GNUNET_OK to keep the connection open,
113 * GNUNET_SYSERR to close it (signal serious error)
116 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
117 const struct GNUNET_MessageHeader *message);
120 * Core handler for path confirmations.
123 * @param message message
124 * @param peer peer identity this notification is about
126 * @return GNUNET_OK to keep the connection open,
127 * GNUNET_SYSERR to close it (signal serious error)
130 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
131 const struct GNUNET_MessageHeader *message);
134 * Core handler for notifications of broken paths
136 * @param cls Closure (unused).
137 * @param id Peer identity of sending neighbor.
138 * @param message Message.
140 * @return GNUNET_OK to keep the connection open,
141 * GNUNET_SYSERR to close it (signal serious error)
144 GMC_handle_broken (void* cls,
145 const struct GNUNET_PeerIdentity* id,
146 const struct GNUNET_MessageHeader* message);
149 * Core handler for tunnel destruction
151 * @param cls Closure (unused).
152 * @param peer Peer identity of sending neighbor.
153 * @param message Message.
155 * @return GNUNET_OK to keep the connection open,
156 * GNUNET_SYSERR to close it (signal serious error)
159 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
160 const struct GNUNET_MessageHeader *message);
163 * Core handler for encrypted mesh network traffic (channel mgmt, data).
165 * @param cls Closure (unused).
166 * @param message Message received.
167 * @param peer Peer who sent the message.
169 * @return GNUNET_OK to keep the connection open,
170 * GNUNET_SYSERR to close it (signal serious error)
173 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
174 const struct GNUNET_MessageHeader *message);
177 * Core handler for key exchange traffic (ephemeral key, ping, pong).
179 * @param cls Closure (unused).
180 * @param message Message received.
181 * @param peer Peer who sent the message.
183 * @return GNUNET_OK to keep the connection open,
184 * GNUNET_SYSERR to close it (signal serious error)
187 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
188 const struct GNUNET_MessageHeader *message);
191 * Core handler for mesh network traffic point-to-point acks.
194 * @param message message
195 * @param peer peer identity this notification is about
197 * @return GNUNET_OK to keep the connection open,
198 * GNUNET_SYSERR to close it (signal serious error)
201 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
202 const struct GNUNET_MessageHeader *message);
205 * Core handler for mesh network traffic point-to-point ack polls.
208 * @param message message
209 * @param peer peer identity this notification is about
211 * @return GNUNET_OK to keep the connection open,
212 * GNUNET_SYSERR to close it (signal serious error)
215 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
216 const struct GNUNET_MessageHeader *message);
219 * Core handler for mesh keepalives.
222 * @param message message
223 * @param peer peer identity this notification is about
224 * @return GNUNET_OK to keep the connection open,
225 * GNUNET_SYSERR to close it (signal serious error)
227 * TODO: Check who we got this from, to validate route.
230 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
231 const struct GNUNET_MessageHeader *message);
234 * Send an ACK on the appropriate connection/channel, depending on
235 * the direction and the position of the peer.
237 * @param c Which connection to send the hop-by-hop ACK.
238 * @param fwd Is this a fwd ACK? (will go dest->root).
239 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
242 GMC_send_ack (struct MeshConnection *c, int fwd, int force);
245 * Initialize the connections subsystem
247 * @param c Configuration handle.
250 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c);
253 * Shut down the connections subsystem.
259 * Create a connection.
261 * @param cid Connection ID (either created locally or imposed remotely).
262 * @param t Tunnel this connection belongs to (or NULL);
263 * @param p Path this connection has to use.
264 * @param own_pos Own position in the @c p path.
266 * @return Newly created connection, NULL in case of error (own id not in path).
268 struct MeshConnection *
269 GMC_new (const struct GNUNET_MESH_Hash *cid,
270 struct MeshTunnel3 *t,
271 struct MeshPeerPath *p,
272 unsigned int own_pos);
275 * Connection is no longer needed: destroy it.
277 * Cancels all pending traffic (including possible DESTROY messages), all
278 * maintenance tasks and removes the connection from neighbor peers and tunnel.
280 * @param c Connection to destroy.
283 GMC_destroy (struct MeshConnection *c);
286 * Get the connection ID.
288 * @param c Connection to get the ID from.
290 * @return ID of the connection.
292 const struct GNUNET_MESH_Hash *
293 GMC_get_id (const struct MeshConnection *c);
297 * Get a hash for the connection ID.
299 * @param c Connection to get the hash.
301 * @return Hash expanded from the ID of the connection.
303 const struct GNUNET_HashCode *
304 GMC_get_h (const struct MeshConnection *c);
308 * Get the connection path.
310 * @param c Connection to get the path from.
312 * @return path used by the connection.
314 const struct MeshPeerPath *
315 GMC_get_path (const struct MeshConnection *c);
318 * Get the connection state.
320 * @param c Connection to get the state from.
322 * @return state of the connection.
324 enum MeshConnectionState
325 GMC_get_state (const struct MeshConnection *c);
328 * Get the connection tunnel.
330 * @param c Connection to get the tunnel from.
332 * @return tunnel of the connection.
335 GMC_get_tunnel (const struct MeshConnection *c);
338 * Get free buffer space in a connection.
340 * @param c Connection.
341 * @param fwd Is query about FWD traffic?
343 * @return Free buffer space [0 - max_msgs_queue/max_connections]
346 GMC_get_buffer (struct MeshConnection *c, int fwd);
349 * Get how many messages have we allowed to send to us from a direction.
351 * @param c Connection.
352 * @param fwd Are we asking about traffic from FWD (BCK messages)?
354 * @return last_ack_sent - last_pid_recv
357 GMC_get_allowed (struct MeshConnection *c, int fwd);
360 * Get messages queued in a connection.
362 * @param c Connection.
363 * @param fwd Is query about FWD traffic?
365 * @return Number of messages queued.
368 GMC_get_qn (struct MeshConnection *c, int fwd);
371 * Allow the connection to advertise a buffer of the given size.
373 * The connection will send an @c fwd ACK message (so: in direction !fwd)
374 * allowing up to last_pid_recv + buffer.
376 * @param c Connection.
377 * @param buffer How many more messages the connection can accept.
378 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
381 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd);
384 * Send FWD keepalive packets for a connection.
386 * @param cls Closure (connection for which to send the keepalive).
387 * @param tc Notification context.
390 GMC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
393 * Send BCK keepalive packets for a connection.
395 * @param cls Closure (connection for which to send the keepalive).
396 * @param tc Notification context.
399 GMC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
403 * Notify other peers on a connection of a broken link. Mark connections
404 * to destroy after all traffic has been sent.
406 * @param c Connection on which there has been a disconnection.
407 * @param peer Peer that disconnected.
410 GMC_notify_broken (struct MeshConnection *c,
411 struct MeshPeer *peer);
414 * Is this peer the first one on the connection?
416 * @param c Connection.
417 * @param fwd Is this about fwd traffic?
419 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
422 GMC_is_origin (struct MeshConnection *c, int fwd);
425 * Is this peer the last one on the connection?
427 * @param c Connection.
428 * @param fwd Is this about fwd traffic?
429 * Note that the ROOT is the terminal for BCK traffic!
431 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
434 GMC_is_terminal (struct MeshConnection *c, int fwd);
437 * See if we are allowed to send by the next hop in the given direction.
439 * @param c Connection.
440 * @param fwd Is this about fwd traffic?
442 * @return #GNUNET_YES in case it's OK to send.
445 GMC_is_sendable (struct MeshConnection *c, int fwd);
448 * Cancel a previously sent message while it's in the queue.
450 * ONLY can be called before the continuation given to the send function
451 * is called. Once the continuation is called, the message is no longer in the
454 * @param q Handle to the queue.
457 GMC_cancel (struct MeshConnectionQueue *q);
460 * Sends an already built message on a connection, properly registering
461 * all used resources.
463 * @param message Message to send. Function makes a copy of it.
464 * If message is not hop-by-hop, decrements TTL of copy.
465 * @param c Connection on which this message is transmitted.
466 * @param fwd Is this a fwd message?
467 * @param force Force the connection to accept the message (buffer overfill).
468 * @param cont Continuation called once message is sent. Can be NULL.
469 * @param cont_cls Closure for @c cont.
471 * @return Handle to cancel the message before it's sent.
472 * NULL on error or if @c cont is NULL.
473 * Invalid on @c cont call.
475 struct MeshConnectionQueue *
476 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
477 struct MeshConnection *c, int fwd, int force,
478 GMC_sent cont, void *cont_cls);
481 * Sends a CREATE CONNECTION message for a path to a peer.
482 * Changes the connection and tunnel states if necessary.
484 * @param connection Connection to create.
487 GMC_send_create (struct MeshConnection *connection);
490 * Send a message to all peers in this connection that the connection
491 * is no longer valid.
493 * If some peer should not receive the message, it should be zero'ed out
494 * before calling this function.
496 * @param c The connection whose peers to notify.
499 GMC_send_destroy (struct MeshConnection *c);
502 * @brief Start a polling timer for the connection.
504 * When a neighbor does not accept more traffic on the connection it could be
505 * caused by a simple congestion or by a lost ACK. Polling enables to check
506 * for the lastest ACK status for a connection.
508 * @param c Connection.
509 * @param fwd Should we poll in the FWD direction?
512 GMC_start_poll (struct MeshConnection *c, int fwd);
516 * @brief Stop polling a connection for ACKs.
518 * Once we have enough ACKs for future traffic, polls are no longer necessary.
520 * @param c Connection.
521 * @param fwd Should we stop the poll in the FWD direction?
524 GMC_stop_poll (struct MeshConnection *c, int fwd);
527 * Get a (static) string for a connection.
529 * @param c Connection.
532 GMC_2s (const struct MeshConnection *c);
534 #if 0 /* keep Emacsens' auto-indent happy */
541 /* ifndef GNUNET_SERVICE_MESH_CONNECTION_H */
543 /* end of gnunet-service-mesh_connection.h */