2 This file is part of GNUnet.
3 (C) 2001-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.c
23 * @brief GNUnet MESH service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "gnunet-service-mesh_connection.h"
33 #include "gnunet-service-mesh_peer.h"
34 #include "gnunet-service-mesh_tunnel.h"
35 #include "gnunet-service-mesh_channel.h"
36 #include "mesh_protocol_enc.h"
37 #include "mesh_path.h"
40 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
41 GNUNET_TIME_UNIT_MINUTES,\
43 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
45 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct containing info about a queued transmission to this peer
60 struct MeshPeerQueue *next;
65 struct MeshPeerQueue *prev;
68 * Peer this transmission is directed to.
70 struct MeshPeer *peer;
73 * Connection this message belongs to.
75 struct MeshConnection *c;
83 * Channel this message belongs to, if known.
85 struct MeshChannel *ch;
88 * Pointer to info stucture used as cls.
105 * Struct to encapsulate all the Flow Control information to a peer to which
106 * we are directly connected (on a core level).
108 struct MeshFlowControl
111 * Connection this controls.
113 struct MeshConnection *c;
116 * How many messages are in the queue on this connection.
118 unsigned int queue_n;
121 * How many messages do we accept in the queue.
123 unsigned int queue_max;
131 * ID of the last packet sent towards the peer.
133 uint32_t last_pid_sent;
136 * ID of the last packet received from the peer.
138 uint32_t last_pid_recv;
141 * Last ACK sent to the peer (peer can't send more than this PID).
143 uint32_t last_ack_sent;
146 * Last ACK sent towards the origin (for traffic towards leaf node).
148 uint32_t last_ack_recv;
151 * Task to poll the peer in case of a lost ACK causes stall.
153 GNUNET_SCHEDULER_TaskIdentifier poll_task;
156 * How frequently to poll for ACKs.
158 struct GNUNET_TIME_Relative poll_time;
163 * Struct containing all information regarding a connection to a peer.
165 struct MeshConnection
168 * Tunnel this connection is part of.
170 struct MeshTunnel3 *t;
173 * Flow control information for traffic fwd.
175 struct MeshFlowControl fwd_fc;
178 * Flow control information for traffic bck.
180 struct MeshFlowControl bck_fc;
183 * ID of the connection.
185 struct GNUNET_HashCode id;
188 * State of the connection.
190 enum MeshConnectionState state;
193 * Path being used for the tunnel.
195 struct MeshPeerPath *path;
198 * Position of the local peer in the path.
200 unsigned int own_pos;
203 * Task to keep the used paths alive at the owner,
204 * time tunnel out on all the other peers.
206 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
209 * Task to keep the used paths alive at the destination,
210 * time tunnel out on all the other peers.
212 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
215 * Pending message count.
217 int pending_messages;
220 * Destroy flag: if true, destroy on last message.
225 /******************************************************************************/
226 /******************************* GLOBALS ***********************************/
227 /******************************************************************************/
230 * Global handle to the statistics service.
232 extern struct GNUNET_STATISTICS_Handle *stats;
235 * Connections known, indexed by cid (MeshConnection).
237 static struct GNUNET_CONTAINER_MultiHashMap *connections;
240 * How many connections are we willing to maintain.
241 * Local connections are always allowed, even if there are more connections than max.
243 static unsigned long long max_connections;
246 * How many messages *in total* are we willing to queue, divide by number of
247 * connections to get connection queue size.
249 static unsigned long long max_msgs_queue;
252 * How often to send path keepalives. Paths timeout after 4 missed.
254 static struct GNUNET_TIME_Relative refresh_connection_time;
257 /******************************************************************************/
258 /******************************** STATIC ***********************************/
259 /******************************************************************************/
261 #if 0 // avoid compiler warning for unused static function
263 fc_debug (struct MeshFlowControl *fc)
265 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
266 fc->last_pid_recv, fc->last_ack_sent);
267 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
268 fc->last_pid_sent, fc->last_ack_recv);
269 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
270 fc->queue_n, fc->queue_max);
274 connection_debug (struct MeshConnection *c)
278 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
282 peer2s (c->t->peer), GNUNET_h2s (&c->id));
283 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
284 c->state, c->pending_messages);
285 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
286 fc_debug (&c->fwd_fc);
287 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
288 fc_debug (&c->bck_fc);
293 * Get string description for tunnel state.
295 * @param s Tunnel state.
297 * @return String representation.
300 GMC_state2s (enum MeshConnectionState s)
304 case MESH_CONNECTION_NEW:
305 return "MESH_CONNECTION_NEW";
306 case MESH_CONNECTION_SENT:
307 return "MESH_CONNECTION_SENT";
308 case MESH_CONNECTION_ACK:
309 return "MESH_CONNECTION_ACK";
310 case MESH_CONNECTION_READY:
311 return "MESH_CONNECTION_READY";
313 return "MESH_CONNECTION_STATE_ERROR";
319 * Initialize a Flow Control structure to the initial state.
321 * @param fc Flow Control structure to initialize.
324 fc_init (struct MeshFlowControl *fc)
327 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
328 fc->last_pid_recv = (uint32_t) -1;
329 fc->last_ack_sent = (uint32_t) 0;
330 fc->last_ack_recv = (uint32_t) 0;
331 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
332 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
334 fc->queue_max = (max_msgs_queue / max_connections) + 1;
341 * @param cid Connection ID.
343 static struct MeshConnection *
344 connection_get (const struct GNUNET_HashCode *cid)
346 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
351 * Send an ACK informing the predecessor about the available buffer space.
353 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
354 * the ACK itself goes "back" (dest->root).
356 * @param c Connection on which to send the ACK.
357 * @param buffer How much space free to advertise?
358 * @param fwd Is this FWD ACK? (Going dest->owner)
361 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
363 struct MeshFlowControl *next_fc;
364 struct MeshFlowControl *prev_fc;
365 struct GNUNET_MESH_ACK msg;
369 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
370 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
372 LOG (GNUNET_ERROR_TYPE_DEBUG,
373 "connection send %s ack on %s\n",
374 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
376 /* Check if we need to transmit the ACK */
377 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
379 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
380 LOG (GNUNET_ERROR_TYPE_DEBUG,
381 " last pid recv: %u, last ack sent: %u\n",
382 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
386 /* Ok, ACK might be necessary, what PID to ACK? */
387 delta = next_fc->queue_max - next_fc->queue_n;
388 ack = prev_fc->last_pid_recv + delta;
389 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
390 LOG (GNUNET_ERROR_TYPE_DEBUG,
391 " last pid %u, last ack %u, qmax %u, q %u\n",
392 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
393 next_fc->queue_max, next_fc->queue_n);
394 if (ack == prev_fc->last_ack_sent)
396 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
400 prev_fc->last_ack_sent = ack;
402 /* Build ACK message and send on connection */
403 msg.header.size = htons (sizeof (msg));
404 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
405 msg.ack = htonl (ack);
408 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
413 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
416 * @param connection Connection to confirm.
417 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
420 send_connection_ack (struct MeshConnection *connection, int fwd)
422 struct MeshTunnel3 *t;
425 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
427 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
428 sizeof (struct GNUNET_MESH_ConnectionACK),
432 if (MESH_TUNNEL_NEW == t->state)
433 tunnel_change_state (t, MESH_TUNNEL_WAITING);
434 if (MESH_CONNECTION_READY != connection->state)
435 connection_change_state (connection, MESH_CONNECTION_SENT);
440 * Sends a CREATE CONNECTION message for a path to a peer.
441 * Changes the connection and tunnel states if necessary.
443 * @param connection Connection to create.
446 send_connection_create (struct MeshConnection *connection)
448 struct MeshTunnel3 *t;
451 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
453 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
454 sizeof (struct GNUNET_MESH_ConnectionCreate) +
455 (connection->path->length *
456 sizeof (struct GNUNET_PeerIdentity)),
461 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
462 tunnel_change_state (t, MESH_TUNNEL_WAITING);
463 if (MESH_CONNECTION_NEW == connection->state)
464 connection_change_state (connection, MESH_CONNECTION_SENT);
469 connection_change_state (struct MeshConnection* c,
470 enum MeshConnectionState state)
472 LOG (GNUNET_ERROR_TYPE_DEBUG,
473 "Connection %s state was %s\n",
474 GNUNET_h2s (&c->id), GMC_state2s (c->state));
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "Connection %s state is now %s\n",
477 GNUNET_h2s (&c->id), GMC_state2s (state));
484 * Send keepalive packets for a connection.
486 * @param c Connection to keep alive..
487 * @param fwd Is this a FWD keepalive? (owner -> dest).
490 connection_keepalive (struct MeshConnection *c, int fwd)
492 struct GNUNET_MESH_ConnectionKeepAlive *msg;
493 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
497 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
498 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
500 LOG (GNUNET_ERROR_TYPE_DEBUG,
501 "sending %s keepalive for connection %s[%d]\n",
506 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
507 msg->header.size = htons (size);
508 msg->header.type = htons (type);
511 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
516 * Send CONNECTION_{CREATE/ACK} packets for a connection.
518 * @param c Connection for which to send the message.
519 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
522 connection_recreate (struct MeshConnection *c, int fwd)
524 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
526 send_connection_create (c);
528 send_connection_ack (c, GNUNET_NO);
533 * Generic connection timer management.
534 * Depending on the role of the peer in the connection will send the
535 * appropriate message (build or keepalive)
537 * @param c Conncetion to maintain.
541 connection_maintain (struct MeshConnection *c, int fwd)
543 if (MESH_TUNNEL_SEARCHING == c->t->state)
545 /* TODO DHT GET with RO_BART */
550 case MESH_CONNECTION_NEW:
552 case MESH_CONNECTION_SENT:
553 connection_recreate (c, fwd);
555 case MESH_CONNECTION_READY:
556 connection_keepalive (c, fwd);
565 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
567 struct MeshConnection *c = cls;
569 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
570 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
573 connection_maintain (c, GNUNET_YES);
574 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
575 &connection_fwd_keepalive,
581 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
583 struct MeshConnection *c = cls;
585 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
586 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
589 connection_maintain (c, GNUNET_NO);
590 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
591 &connection_bck_keepalive,
598 * Get the first transmittable message for a connection.
600 * @param c Connection.
601 * @param fwd Is this FWD?
603 * @return First transmittable message.
605 static struct MeshPeerQueue *
606 connection_get_first_message (struct MeshConnection *c, int fwd)
608 struct MeshPeerQueue *q;
611 p = connection_get_hop (c, fwd);
613 for (q = p->queue_head; NULL != q; q = q->next)
617 if (queue_is_sendable (q))
626 * @brief Re-initiate traffic on this connection if necessary.
628 * Check if there is traffic queued towards this peer
629 * and the core transmit handle is NULL (traffic was stalled).
630 * If so, call core tmt rdy.
632 * @param c Connection on which initiate traffic.
633 * @param fwd Is this about fwd traffic?
636 connection_unlock_queue (struct MeshConnection *c, int fwd)
638 struct MeshPeer *peer;
639 struct MeshPeerQueue *q;
642 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 "connection_unlock_queue %s on %s\n",
644 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
646 if (GMC_is_terminal (c, fwd))
648 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
652 peer = connection_get_hop (c, fwd);
654 if (NULL != peer->core_transmit)
656 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
657 return; /* Already unlocked */
660 q = connection_get_first_message (c, fwd);
663 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
664 return; /* Nothing to transmit */
668 peer->core_transmit =
669 GNUNET_CORE_notify_transmit_ready (core_handle,
672 GNUNET_TIME_UNIT_FOREVER_REL,
673 GNUNET_PEER_resolve2 (peer->id),
681 * Cancel all transmissions that belong to a certain connection.
683 * @param c Connection which to cancel.
684 * @param fwd Cancel fwd traffic?
687 connection_cancel_queues (struct MeshConnection *c, int fwd)
689 struct MeshPeerQueue *q;
690 struct MeshPeerQueue *next;
691 struct MeshFlowControl *fc;
692 struct MeshPeer *peer;
699 fc = fwd ? &c->fwd_fc : &c->bck_fc;
700 peer = connection_get_hop (c, fwd);
702 for (q = peer->queue_head; NULL != q; q = next)
707 LOG (GNUNET_ERROR_TYPE_DEBUG,
708 "connection_cancel_queue %s\n",
709 GNUNET_MESH_DEBUG_M2S (q->type));
710 queue_destroy (q, GNUNET_YES);
713 if (NULL == peer->queue_head)
715 if (NULL != peer->core_transmit)
717 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
718 peer->core_transmit = NULL;
720 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
722 GNUNET_SCHEDULER_cancel (fc->poll_task);
723 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
730 * Function called if a connection has been stalled for a while,
731 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
733 * @param cls Closure (poll ctx).
734 * @param tc TaskContext.
737 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
739 struct MeshFlowControl *fc = cls;
740 struct GNUNET_MESH_Poll msg;
741 struct MeshConnection *c;
743 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
744 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
750 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
751 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
752 GNUNET_h2s (&c->id));
753 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
754 fc == &c->fwd_fc ? "FWD" : "BCK");
756 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
757 msg.header.size = htons (sizeof (msg));
758 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
759 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
760 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
761 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
762 &connection_poll, fc);
769 * Get the previous hop in a connection
771 * @param c Connection.
773 * @return Previous peer in the connection.
775 static struct MeshPeer *
776 connection_get_prev_hop (struct MeshConnection *c)
780 if (0 == c->own_pos || c->path->length < 2)
781 id = c->path->peers[0];
783 id = c->path->peers[c->own_pos - 1];
785 return peer_get_short (id);
790 * Get the next hop in a connection
792 * @param c Connection.
794 * @return Next peer in the connection.
796 static struct MeshPeer *
797 connection_get_next_hop (struct MeshConnection *c)
801 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
802 id = c->path->peers[c->path->length - 1];
804 id = c->path->peers[c->own_pos + 1];
806 return peer_get_short (id);
811 * Get the hop in a connection.
813 * @param c Connection.
814 * @param fwd Next hop?
816 * @return Next peer in the connection.
818 static struct MeshPeer *
819 connection_get_hop (struct MeshConnection *c, int fwd)
822 return connection_get_next_hop (c);
823 return connection_get_prev_hop (c);
830 * Timeout function due to lack of keepalive/traffic from the owner.
831 * Destroys connection if called.
833 * @param cls Closure (connection to destroy).
834 * @param tc TaskContext.
837 connection_fwd_timeout (void *cls,
838 const struct GNUNET_SCHEDULER_TaskContext *tc)
840 struct MeshConnection *c = cls;
842 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
843 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
845 LOG (GNUNET_ERROR_TYPE_DEBUG,
846 "Connection %s[%X] FWD timed out. Destroying.\n",
850 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
858 * Timeout function due to lack of keepalive/traffic from the destination.
859 * Destroys connection if called.
861 * @param cls Closure (connection to destroy).
862 * @param tc TaskContext
865 connection_bck_timeout (void *cls,
866 const struct GNUNET_SCHEDULER_TaskContext *tc)
868 struct MeshConnection *c = cls;
870 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
871 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
874 LOG (GNUNET_ERROR_TYPE_DEBUG,
875 "Connection %s[%X] FWD timed out. Destroying.\n",
879 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
887 * Resets the connection timeout task, some other message has done the
889 * - For the first peer on the direction this means to send
890 * a keepalive or a path confirmation message (either create or ACK).
891 * - For all other peers, this means to destroy the connection,
892 * due to lack of activity.
893 * Starts the tiemout if no timeout was running (connection just created).
895 * @param c Connection whose timeout to reset.
896 * @param fwd Is this forward?
898 * TODO use heap to improve efficiency of scheduler.
901 connection_reset_timeout (struct MeshConnection *c, int fwd)
903 GNUNET_SCHEDULER_TaskIdentifier *ti;
904 GNUNET_SCHEDULER_Task f;
906 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
908 if (GNUNET_SCHEDULER_NO_TASK != *ti)
909 GNUNET_SCHEDULER_cancel (*ti);
911 if (GMC_is_origin (c, fwd)) /* Endpoint */
913 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
914 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
918 struct GNUNET_TIME_Relative delay;
920 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
921 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
922 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
931 register_neighbors (struct MeshConnection *c)
933 struct MeshPeer *peer;
935 peer = connection_get_next_hop (c);
936 if (GNUNET_NO == GMP_is_neighbor (peer))
941 GMP_add_connection (peer, c);
942 peer = connection_get_prev_hop (c);
943 if (GNUNET_NO == GMP_is_neighbor (peer))
948 GMP_add_connection (peer, c);
956 unregister_neighbors (struct MeshConnection *c)
958 struct MeshPeer *peer;
960 peer = connection_get_next_hop (c);
961 GMP_remove_connection (peer, c);
963 peer = connection_get_prev_hop (c);
964 GMP_remove_connection (peer, c);
969 /******************************************************************************/
970 /******************************** API ***********************************/
971 /******************************************************************************/
974 * Core handler for connection creation.
976 * @param cls Closure (unused).
977 * @param peer Sender (neighbor).
978 * @param message Message.
980 * @return GNUNET_OK to keep the connection open,
981 * GNUNET_SYSERR to close it (signal serious error)
984 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
985 const struct GNUNET_MessageHeader *message)
987 struct GNUNET_MESH_ConnectionCreate *msg;
988 struct GNUNET_PeerIdentity *id;
989 struct GNUNET_HashCode *cid;
990 struct MeshPeerPath *path;
991 struct MeshPeer *dest_peer;
992 struct MeshPeer *orig_peer;
993 struct MeshConnection *c;
994 unsigned int own_pos;
998 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
999 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1002 size = ntohs (message->size);
1003 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1005 GNUNET_break_op (0);
1009 /* Calculate hops */
1010 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1011 if (size % sizeof (struct GNUNET_PeerIdentity))
1013 GNUNET_break_op (0);
1016 size /= sizeof (struct GNUNET_PeerIdentity);
1019 GNUNET_break_op (0);
1022 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1024 /* Get parameters */
1025 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1027 id = (struct GNUNET_PeerIdentity *) &msg[1];
1028 LOG (GNUNET_ERROR_TYPE_DEBUG,
1029 " connection %s (%s).\n",
1030 GNUNET_h2s (cid), GNUNET_i2s (id));
1032 /* Create connection */
1033 c = connection_get (cid);
1036 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1037 c = connection_new (cid);
1040 connection_reset_timeout (c, GNUNET_YES);
1043 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1044 path = path_new (size);
1046 for (i = 0; i < size; i++)
1048 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1049 GNUNET_i2s (&id[i]));
1050 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1051 if (path->peers[i] == my_short_id)
1054 if (own_pos == 0 && path->peers[own_pos] != myid)
1056 /* create path: self not found in path through self */
1057 GNUNET_break_op (0);
1058 path_destroy (path);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1063 path_add_to_peers (path, GNUNET_NO);
1064 c->path = path_duplicate (path);
1065 c->own_pos = own_pos;
1071 if (MESH_CONNECTION_NEW == c->state)
1072 connection_change_state (c, MESH_CONNECTION_SENT);
1074 /* Remember peers */
1075 dest_peer = peer_get (&id[size - 1]);
1076 orig_peer = peer_get (&id[0]);
1078 /* Is it a connection to us? */
1079 if (c->own_pos == size - 1)
1081 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1082 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
1084 if (NULL == orig_peer->tunnel)
1086 orig_peer->tunnel = tunnel_new ();
1087 orig_peer->tunnel->peer = orig_peer;
1089 GMT_add_connection (orig_peer->tunnel, c);
1090 if (MESH_TUNNEL_NEW == GMT_get_state (c->t))
1091 GMT_change_state (c->t, MESH_TUNNEL_WAITING);
1093 send_connection_ack (c, GNUNET_NO);
1094 if (MESH_CONNECTION_SENT == c->state)
1095 connection_change_state (c, MESH_CONNECTION_ACK);
1097 /* Keep tunnel alive in direction dest->owner*/
1098 connection_reset_timeout (c, GNUNET_NO);
1102 /* It's for somebody else! Retransmit. */
1103 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1104 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1105 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
1106 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
1113 * Core handler for path confirmations.
1115 * @param cls closure
1116 * @param message message
1117 * @param peer peer identity this notification is about
1119 * @return GNUNET_OK to keep the connection open,
1120 * GNUNET_SYSERR to close it (signal serious error)
1123 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1124 const struct GNUNET_MessageHeader *message)
1126 struct GNUNET_MESH_ConnectionACK *msg;
1127 struct MeshConnection *c;
1128 struct MeshPeerPath *p;
1129 struct MeshPeer *pi;
1132 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1133 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1134 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1135 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1136 GNUNET_h2s (&msg->cid));
1137 c = connection_get (&msg->cid);
1140 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1142 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1149 pi = peer_get (peer);
1150 if (connection_get_next_hop (c) == pi)
1152 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1154 if (MESH_CONNECTION_SENT == c->state)
1155 connection_change_state (c, MESH_CONNECTION_ACK);
1157 else if (connection_get_prev_hop (c) == pi)
1159 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1161 connection_change_state (c, MESH_CONNECTION_READY);
1165 GNUNET_break_op (0);
1168 connection_reset_timeout (c, fwd);
1170 /* Add path to peers? */
1174 path_add_to_peers (p, GNUNET_YES);
1181 /* Message for us as creator? */
1182 if (connection_is_origin (c, GNUNET_YES))
1184 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1185 connection_change_state (c, MESH_CONNECTION_READY);
1186 if (MESH_TUNNEL_READY != c->t->state)
1187 tunnel_change_state (c->t, MESH_TUNNEL_READY);
1188 send_connection_ack (c, GNUNET_YES);
1189 tunnel_send_queued_data (c->t, GNUNET_YES);
1190 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
1192 GNUNET_DHT_get_stop (c->t->peer->dhtget);
1193 c->t->peer->dhtget = NULL;
1198 /* Message for us as destination? */
1199 if (GMC_is_terminal (c, GNUNET_YES))
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1202 GMC_change_state (c, MESH_CONNECTION_READY);
1203 GMT_change_state (c->t, MESH_TUNNEL_READY);
1204 GMT_send_queued_data (c->t, GNUNET_NO);
1208 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1209 GMC_send_prebuilt_message (message, c, NULL, fwd);
1215 * Core handler for notifications of broken paths
1217 * @param cls Closure (unused).
1218 * @param peer Peer identity of sending neighbor.
1219 * @param message Message.
1221 * @return GNUNET_OK to keep the connection open,
1222 * GNUNET_SYSERR to close it (signal serious error)
1225 GMC_handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
1226 const struct GNUNET_MessageHeader *message)
1228 struct GNUNET_MESH_ConnectionBroken *msg;
1229 struct MeshConnection *c;
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
1233 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1235 GNUNET_i2s (&msg->peer1));
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1237 GNUNET_i2s (&msg->peer2));
1238 c = connection_get (&msg->cid);
1241 GNUNET_break_op (0);
1244 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
1245 GNUNET_PEER_search (&msg->peer2));
1252 * Core handler for tunnel destruction
1254 * @param cls Closure (unused).
1255 * @param peer Peer identity of sending neighbor.
1256 * @param message Message.
1258 * @return GNUNET_OK to keep the connection open,
1259 * GNUNET_SYSERR to close it (signal serious error)
1262 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1263 const struct GNUNET_MessageHeader *message)
1265 struct GNUNET_MESH_ConnectionDestroy *msg;
1266 struct MeshConnection *c;
1270 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1271 LOG (GNUNET_ERROR_TYPE_DEBUG,
1272 "Got a CONNECTION DESTROY message from %s\n",
1274 LOG (GNUNET_ERROR_TYPE_DEBUG,
1275 " for connection %s\n",
1276 GNUNET_h2s (&msg->cid));
1277 c = connection_get (&msg->cid);
1280 /* Probably already got the message from another path,
1281 * destroyed the tunnel and retransmitted to children.
1284 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1288 id = GNUNET_PEER_search (peer);
1289 if (id == GMP_get_short_id (connection_get_prev_hop (c)))
1291 else if (id == GMP_get_short_id (connection_get_next_hop (c)))
1295 GNUNET_break_op (0);
1298 GMC_send_prebuilt_message (message, c, NULL, fwd);
1299 c->destroy = GNUNET_YES;
1305 * Generic handler for mesh network encrypted traffic.
1307 * @param peer Peer identity this notification is about.
1308 * @param message Encrypted message.
1309 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1311 * @return GNUNET_OK to keep the connection open,
1312 * GNUNET_SYSERR to close it (signal serious error)
1315 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1316 const struct GNUNET_MESH_Encrypted *msg,
1319 struct MeshConnection *c;
1320 struct MeshTunnel3 *t;
1321 struct MeshPeer *neighbor;
1322 struct MeshFlowControl *fc;
1329 size = ntohs (msg->header.size);
1331 sizeof (struct GNUNET_MESH_Encrypted) +
1332 sizeof (struct GNUNET_MessageHeader))
1334 GNUNET_break_op (0);
1337 type = ntohs (msg->header.type);
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1339 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1340 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1342 /* Check connection */
1343 c = connection_get (&msg->cid);
1346 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1347 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1351 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1353 /* Check if origin is as expected */
1354 neighbor = connection_get_hop (c, !fwd);
1355 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1357 GNUNET_break_op (0);
1362 pid = ntohl (msg->pid);
1363 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1365 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1366 LOG (GNUNET_ERROR_TYPE_DEBUG,
1367 "WARNING Received PID %u, (prev %u), ACK %u\n",
1368 pid, fc->last_pid_recv, fc->last_ack_sent);
1371 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1373 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1374 LOG (GNUNET_ERROR_TYPE_DEBUG,
1375 " Pid %u not expected (%u+), dropping!\n",
1376 pid, fc->last_pid_recv + 1);
1379 if (MESH_CONNECTION_SENT == c->state)
1380 connection_change_state (c, MESH_CONNECTION_READY);
1381 connection_reset_timeout (c, fwd);
1382 fc->last_pid_recv = pid;
1384 /* Is this message for us? */
1385 if (GMC_is_terminal (c, fwd))
1387 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
1389 struct GNUNET_MessageHeader *msgh;
1392 /* TODO signature verification */
1393 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1394 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1396 fc->last_pid_recv = pid;
1397 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
1401 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1402 handle_decrypted (t, msgh, fwd);
1403 off += ntohs (msgh->size);
1405 send_ack (c, NULL, fwd);
1409 /* Message not for us: forward to next hop */
1410 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1411 ttl = ntohl (msg->ttl);
1412 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1415 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1416 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1417 send_ack (c, NULL, fwd);
1420 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1422 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1429 * Core handler for mesh network traffic going orig->dest.
1431 * @param cls Closure (unused).
1432 * @param message Message received.
1433 * @param peer Peer who sent the message.
1435 * @return GNUNET_OK to keep the connection open,
1436 * GNUNET_SYSERR to close it (signal serious error)
1439 GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1440 const struct GNUNET_MessageHeader *message)
1442 return handle_mesh_encrypted (peer,
1443 (struct GNUNET_MESH_Encrypted *)message,
1448 * Core handler for mesh network traffic going dest->orig.
1450 * @param cls Closure (unused).
1451 * @param message Message received.
1452 * @param peer Peer who sent the message.
1454 * @return GNUNET_OK to keep the connection open,
1455 * GNUNET_SYSERR to close it (signal serious error)
1458 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1459 const struct GNUNET_MessageHeader *message)
1461 return handle_mesh_encrypted (peer,
1462 (struct GNUNET_MESH_Encrypted *)message,
1468 * Core handler for mesh network traffic point-to-point acks.
1470 * @param cls closure
1471 * @param message message
1472 * @param peer peer identity this notification is about
1474 * @return GNUNET_OK to keep the connection open,
1475 * GNUNET_SYSERR to close it (signal serious error)
1478 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1479 const struct GNUNET_MessageHeader *message)
1481 struct GNUNET_MESH_ACK *msg;
1482 struct MeshConnection *c;
1483 struct MeshFlowControl *fc;
1488 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1489 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1491 msg = (struct GNUNET_MESH_ACK *) message;
1493 c = connection_get (&msg->cid);
1497 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1502 /* Is this a forward or backward ACK? */
1503 id = GNUNET_PEER_search (peer);
1504 if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1506 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1510 else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1512 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1518 GNUNET_break_op (0);
1522 ack = ntohl (msg->ack);
1523 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1524 ack, fc->last_ack_recv);
1525 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1526 fc->last_ack_recv = ack;
1528 /* Cancel polling if the ACK is big enough. */
1529 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1530 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1532 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1533 GNUNET_SCHEDULER_cancel (fc->poll_task);
1534 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1535 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1538 connection_unlock_queue (c, fwd);
1545 * Core handler for mesh network traffic point-to-point ack polls.
1547 * @param cls closure
1548 * @param message message
1549 * @param peer peer identity this notification is about
1551 * @return GNUNET_OK to keep the connection open,
1552 * GNUNET_SYSERR to close it (signal serious error)
1555 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1556 const struct GNUNET_MessageHeader *message)
1558 struct GNUNET_MESH_Poll *msg;
1559 struct MeshConnection *c;
1560 struct MeshFlowControl *fc;
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1569 msg = (struct GNUNET_MESH_Poll *) message;
1571 c = connection_get (&msg->cid);
1575 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1577 GNUNET_break_op (0);
1581 /* Is this a forward or backward ACK?
1582 * Note: a poll should never be needed in a loopback case,
1583 * since there is no possiblility of packet loss there, so
1584 * this way of discerining FWD/BCK should not be a problem.
1586 id = GNUNET_PEER_search (peer);
1587 if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1589 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1592 else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1594 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1599 GNUNET_break_op (0);
1603 pid = ntohl (msg->pid);
1604 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1605 pid, fc->last_pid_recv);
1606 fc->last_pid_recv = pid;
1607 fwd = fc == &c->fwd_fc;
1608 send_ack (c, NULL, fwd);
1615 * Core handler for mesh keepalives.
1617 * @param cls closure
1618 * @param message message
1619 * @param peer peer identity this notification is about
1620 * @return GNUNET_OK to keep the connection open,
1621 * GNUNET_SYSERR to close it (signal serious error)
1623 * TODO: Check who we got this from, to validate route.
1626 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1627 const struct GNUNET_MessageHeader *message)
1629 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1630 struct MeshConnection *c;
1631 struct MeshPeer *neighbor;
1634 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1635 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1638 c = connection_get (&msg->cid);
1641 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1646 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1647 GNUNET_YES : GNUNET_NO;
1649 /* Check if origin is as expected */
1650 neighbor = connection_get_hop (c, fwd);
1651 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1653 GNUNET_break_op (0);
1657 connection_change_state (c, MESH_CONNECTION_READY);
1658 connection_reset_timeout (c, fwd);
1660 if (GMC_is_terminal (c, fwd))
1663 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1664 GMC_send_prebuilt_message (message, c, NULL, fwd);
1671 * Send an ACK on the appropriate connection/channel, depending on
1672 * the direction and the position of the peer.
1674 * @param c Which connection to send the hop-by-hop ACK.
1675 * @param ch Channel, if any.
1676 * @param fwd Is this a fwd ACK? (will go dest->root)
1679 GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1681 unsigned int buffer;
1683 LOG (GNUNET_ERROR_TYPE_DEBUG,
1684 "send ack %s on %p %p\n",
1685 fwd ? "FWD" : "BCK", c, ch);
1686 if (NULL == c || GMC_is_terminal (c, fwd))
1688 struct MeshTunnel3 *t;
1689 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n");
1690 t = (NULL == c) ? GMCH_get_tunnel (ch) : GMC_get_tunnel (c);
1691 buffer = GMT_get_buffer (t, fwd);
1695 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1696 buffer = GMC_get_buffer (c, fwd);
1698 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1700 if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1701 (NULL != c && GMC_is_origin (c, fwd)) )
1703 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1706 GNUNET_assert (NULL != ch);
1707 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1708 send_local_ack (ch, fwd);
1713 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
1714 GNUNET_assert (NULL != ch);
1715 channel_send_connections_ack (ch, buffer, fwd);
1719 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1720 connection_send_ack (c, buffer, fwd);
1726 * Initialize the connections subsystem
1728 * @param c Configuration handle.
1731 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1734 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1737 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1738 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1739 GNUNET_SCHEDULER_shutdown ();
1744 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1747 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1748 "MESH", "MAX_CONNECTIONS", "MISSING");
1749 GNUNET_SCHEDULER_shutdown ();
1754 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1755 &refresh_connection_time))
1757 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1758 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1759 GNUNET_SCHEDULER_shutdown ();
1762 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1766 * Shut down the connections subsystem.
1774 struct MeshConnection *
1775 GMC_new (const struct GNUNET_HashCode *cid,
1776 struct MeshTunnel3 *t,
1777 struct MeshPeerPath *p,
1778 unsigned int own_pos)
1780 struct MeshConnection *c;
1782 c = GNUNET_new (struct MeshConnection);
1784 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1785 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1786 fc_init (&c->fwd_fc);
1787 fc_init (&c->bck_fc);
1792 if (own_pos > p->length - 1)
1798 c->own_pos = own_pos;
1803 c->fwd_maintenance_task =
1804 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1805 &connection_fwd_keepalive, c);
1807 register_neighbors (c);
1813 GMC_destroy (struct MeshConnection *c)
1815 struct MeshPeer *peer;
1820 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n",
1821 GNUNET_h2s (&c->id));
1823 /* Cancel all traffic */
1824 connection_cancel_queues (c, GNUNET_YES);
1825 connection_cancel_queues (c, GNUNET_NO);
1827 /* Cancel maintainance task (keepalive/timeout) */
1828 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1829 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1830 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1831 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1833 /* Unregister from neighbors */
1834 unregister_neighbors (c);
1837 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1838 GMT_remove_connection (c->t, c);
1843 * Get the connection ID.
1845 * @param c Connection to get the ID from.
1847 * @return ID of the connection.
1849 const struct GNUNET_HashCode *
1850 GMC_get_id (const struct MeshConnection *c)
1857 * Get the connection state.
1859 * @param c Connection to get the state from.
1861 * @return state of the connection.
1863 enum MeshConnectionState
1864 GMC_get_state (const struct MeshConnection *c)
1870 * Get the connection tunnel.
1872 * @param c Connection to get the tunnel from.
1874 * @return tunnel of the connection.
1876 struct MeshTunnel3 *
1877 GMC_get_tunnel (const struct MeshConnection *c)
1884 * Get free buffer space in a connection.
1886 * @param c Connection.
1887 * @param fwd Is query about FWD traffic?
1889 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1892 GMC_get_buffer (struct MeshConnection *c, int fwd)
1894 struct MeshFlowControl *fc;
1896 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1898 return (fc->queue_max - fc->queue_n);
1902 * Get messages queued in a connection.
1904 * @param c Connection.
1905 * @param fwd Is query about FWD traffic?
1907 * @return Number of messages queued.
1910 GMC_get_qn (struct MeshConnection *c, int fwd)
1912 struct MeshFlowControl *fc;
1914 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1921 * Notify other peers on a connection of a broken link. Mark connections
1922 * to destroy after all traffic has been sent.
1924 * @param c Connection on which there has been a disconnection.
1925 * @param peer Peer that disconnected.
1926 * @param my_full_id My ID (to send to other peers).
1929 GMC_notify_broken (struct MeshConnection *c,
1930 struct MeshPeer *peer,
1931 struct GNUNET_PeerIdentity *my_full_id)
1933 struct GNUNET_MESH_ConnectionBroken msg;
1936 fwd = peer == connection_get_prev_hop (c);
1938 connection_cancel_queues (c, !fwd);
1939 if (GMC_is_terminal (c, fwd))
1941 /* Local shutdown, no one to notify about this. */
1946 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
1947 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
1949 msg.peer1 = *my_full_id;
1950 msg.peer2 = *GMP_get_id (peer);
1951 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
1952 c->destroy = GNUNET_YES;
1959 * Is this peer the first one on the connection?
1961 * @param c Connection.
1962 * @param fwd Is this about fwd traffic?
1964 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1967 GMC_is_origin (struct MeshConnection *c, int fwd)
1969 if (!fwd && c->path->length - 1 == c->own_pos )
1971 if (fwd && 0 == c->own_pos)
1978 * Is this peer the last one on the connection?
1980 * @param c Connection.
1981 * @param fwd Is this about fwd traffic?
1982 * Note that the ROOT is the terminal for BCK traffic!
1984 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1987 GMC_is_terminal (struct MeshConnection *c, int fwd)
1989 return GMC_is_origin (c, !fwd);
1994 * Sends an already built message on a connection, properly registering
1995 * all used resources.
1997 * @param message Message to send. Function makes a copy of it.
1998 * If message is not hop-by-hop, decrements TTL of copy.
1999 * @param c Connection on which this message is transmitted.
2000 * @param ch Channel on which this message is transmitted, or NULL.
2001 * @param fwd Is this a fwd message?
2004 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2005 struct MeshConnection *c,
2006 struct MeshChannel *ch,
2013 size = ntohs (message->size);
2014 data = GNUNET_malloc (size);
2015 memcpy (data, message, size);
2016 type = ntohs (message->type);
2017 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2018 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
2022 struct GNUNET_MESH_Encrypted *emsg;
2023 struct GNUNET_MESH_ACK *amsg;
2024 struct GNUNET_MESH_Poll *pmsg;
2025 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2026 struct GNUNET_MESH_ConnectionBroken *bmsg;
2029 case GNUNET_MESSAGE_TYPE_MESH_FWD:
2030 case GNUNET_MESSAGE_TYPE_MESH_BCK:
2031 emsg = (struct GNUNET_MESH_Encrypted *) data;
2032 ttl = ntohl (emsg->ttl);
2035 GNUNET_break_op (0);
2039 emsg->ttl = htonl (ttl - 1);
2040 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2041 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
2044 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2045 amsg = (struct GNUNET_MESH_ACK *) data;
2047 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2050 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2051 pmsg = (struct GNUNET_MESH_Poll *) data;
2053 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2054 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2057 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2058 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2063 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2064 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2069 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2070 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2077 GMP_queue_add (data,
2087 * Send a message to all peers in this connection that the connection
2088 * is no longer valid.
2090 * If some peer should not receive the message, it should be zero'ed out
2091 * before calling this function.
2093 * @param c The connection whose peers to notify.
2096 GMC_send_destroy (struct MeshConnection *c)
2098 struct GNUNET_MESH_ConnectionDestroy msg;
2100 if (GNUNET_YES == c->destroy)
2103 msg.header.size = htons (sizeof (msg));
2104 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2106 LOG (GNUNET_ERROR_TYPE_DEBUG,
2107 " sending connection destroy for connection %s\n",
2108 GNUNET_h2s (&c->id));
2110 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2111 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2112 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2113 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2114 c->destroy = GNUNET_YES;