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 LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
46 /******************************************************************************/
47 /******************************** STRUCTS **********************************/
48 /******************************************************************************/
51 * Struct containing info about a queued transmission to this peer
58 struct MeshPeerQueue *next;
63 struct MeshPeerQueue *prev;
66 * Peer this transmission is directed to.
68 struct MeshPeer *peer;
71 * Connection this message belongs to.
73 struct MeshConnection *c;
81 * Channel this message belongs to, if known.
83 struct MeshChannel *ch;
86 * Pointer to info stucture used as cls.
103 * Struct to encapsulate all the Flow Control information to a peer to which
104 * we are directly connected (on a core level).
106 struct MeshFlowControl
109 * Connection this controls.
111 struct MeshConnection *c;
114 * How many messages are in the queue on this connection.
116 unsigned int queue_n;
119 * How many messages do we accept in the queue.
121 unsigned int queue_max;
129 * ID of the last packet sent towards the peer.
131 uint32_t last_pid_sent;
134 * ID of the last packet received from the peer.
136 uint32_t last_pid_recv;
139 * Last ACK sent to the peer (peer can't send more than this PID).
141 uint32_t last_ack_sent;
144 * Last ACK sent towards the origin (for traffic towards leaf node).
146 uint32_t last_ack_recv;
149 * Task to poll the peer in case of a lost ACK causes stall.
151 GNUNET_SCHEDULER_TaskIdentifier poll_task;
154 * How frequently to poll for ACKs.
156 struct GNUNET_TIME_Relative poll_time;
161 * Struct containing all information regarding a connection to a peer.
163 struct MeshConnection
166 * Tunnel this connection is part of.
168 struct MeshTunnel3 *t;
171 * Flow control information for traffic fwd.
173 struct MeshFlowControl fwd_fc;
176 * Flow control information for traffic bck.
178 struct MeshFlowControl bck_fc;
181 * ID of the connection.
183 struct GNUNET_HashCode id;
186 * State of the connection.
188 enum MeshConnectionState state;
191 * Path being used for the tunnel.
193 struct MeshPeerPath *path;
196 * Position of the local peer in the path.
198 unsigned int own_pos;
201 * Task to keep the used paths alive at the owner,
202 * time tunnel out on all the other peers.
204 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
207 * Task to keep the used paths alive at the destination,
208 * time tunnel out on all the other peers.
210 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
213 * Pending message count.
215 int pending_messages;
218 * Destroy flag: if true, destroy on last message.
223 /******************************************************************************/
224 /******************************* GLOBALS ***********************************/
225 /******************************************************************************/
228 * Global handle to the statistics service.
230 extern struct GNUNET_STATISTICS_Handle *stats;
233 * Connections known, indexed by cid (MeshConnection).
235 static struct GNUNET_CONTAINER_MultiHashMap *connections;
238 * How many connections are we willing to maintain.
239 * Local connections are always allowed, even if there are more connections than max.
241 static unsigned long long max_connections;
244 * How many messages *in total* are we willing to queue, divide by number of
245 * connections to get connection queue size.
247 static unsigned long long max_msgs_queue;
250 * How often to send path keepalives. Paths timeout after 4 missed.
252 static struct GNUNET_TIME_Relative refresh_connection_time;
255 /******************************************************************************/
256 /******************************** STATIC ***********************************/
257 /******************************************************************************/
259 #if 0 // avoid compiler warning for unused static function
261 fc_debug (struct MeshFlowControl *fc)
263 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
264 fc->last_pid_recv, fc->last_ack_sent);
265 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
266 fc->last_pid_sent, fc->last_ack_recv);
267 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
268 fc->queue_n, fc->queue_max);
272 connection_debug (struct MeshConnection *c)
276 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
279 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
280 peer2s (c->t->peer), GNUNET_h2s (&c->id));
281 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
282 c->state, c->pending_messages);
283 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
284 fc_debug (&c->fwd_fc);
285 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
286 fc_debug (&c->bck_fc);
291 * Get string description for tunnel state.
293 * @param s Tunnel state.
295 * @return String representation.
298 GMC_state2s (enum MeshConnectionState s)
302 case MESH_CONNECTION_NEW:
303 return "MESH_CONNECTION_NEW";
304 case MESH_CONNECTION_SENT:
305 return "MESH_CONNECTION_SENT";
306 case MESH_CONNECTION_ACK:
307 return "MESH_CONNECTION_ACK";
308 case MESH_CONNECTION_READY:
309 return "MESH_CONNECTION_READY";
311 return "MESH_CONNECTION_STATE_ERROR";
317 * Initialize a Flow Control structure to the initial state.
319 * @param fc Flow Control structure to initialize.
322 fc_init (struct MeshFlowControl *fc)
325 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
326 fc->last_pid_recv = (uint32_t) -1;
327 fc->last_ack_sent = (uint32_t) 0;
328 fc->last_ack_recv = (uint32_t) 0;
329 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
330 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
332 fc->queue_max = (max_msgs_queue / max_connections) + 1;
339 * @param cid Connection ID.
341 static struct MeshConnection *
342 connection_get (const struct GNUNET_HashCode *cid)
344 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
349 * Send an ACK informing the predecessor about the available buffer space.
351 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
352 * the ACK itself goes "back" (dest->root).
354 * @param c Connection on which to send the ACK.
355 * @param buffer How much space free to advertise?
356 * @param fwd Is this FWD ACK? (Going dest->owner)
359 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
361 struct MeshFlowControl *next_fc;
362 struct MeshFlowControl *prev_fc;
363 struct GNUNET_MESH_ACK msg;
367 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
368 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
370 LOG (GNUNET_ERROR_TYPE_DEBUG,
371 "connection send %s ack on %s\n",
372 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
374 /* Check if we need to transmit the ACK */
375 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
377 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
378 LOG (GNUNET_ERROR_TYPE_DEBUG,
379 " last pid recv: %u, last ack sent: %u\n",
380 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
384 /* Ok, ACK might be necessary, what PID to ACK? */
385 delta = next_fc->queue_max - next_fc->queue_n;
386 ack = prev_fc->last_pid_recv + delta;
387 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
388 LOG (GNUNET_ERROR_TYPE_DEBUG,
389 " last pid %u, last ack %u, qmax %u, q %u\n",
390 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
391 next_fc->queue_max, next_fc->queue_n);
392 if (ack == prev_fc->last_ack_sent)
394 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
398 prev_fc->last_ack_sent = ack;
400 /* Build ACK message and send on connection */
401 msg.header.size = htons (sizeof (msg));
402 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
403 msg.ack = htonl (ack);
406 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
411 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
414 * @param connection Connection to confirm.
415 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
418 send_connection_ack (struct MeshConnection *connection, int fwd)
420 struct MeshTunnel3 *t;
423 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
425 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
426 sizeof (struct GNUNET_MESH_ConnectionACK),
430 if (MESH_TUNNEL_NEW == t->state)
431 tunnel_change_state (t, MESH_TUNNEL_WAITING);
432 if (MESH_CONNECTION_READY != connection->state)
433 connection_change_state (connection, MESH_CONNECTION_SENT);
438 * Sends a CREATE CONNECTION message for a path to a peer.
439 * Changes the connection and tunnel states if necessary.
441 * @param connection Connection to create.
444 send_connection_create (struct MeshConnection *connection)
446 struct MeshTunnel3 *t;
449 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
451 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
452 sizeof (struct GNUNET_MESH_ConnectionCreate) +
453 (connection->path->length *
454 sizeof (struct GNUNET_PeerIdentity)),
459 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
460 tunnel_change_state (t, MESH_TUNNEL_WAITING);
461 if (MESH_CONNECTION_NEW == connection->state)
462 connection_change_state (connection, MESH_CONNECTION_SENT);
467 connection_change_state (struct MeshConnection* c,
468 enum MeshConnectionState state)
470 LOG (GNUNET_ERROR_TYPE_DEBUG,
471 "Connection %s state was %s\n",
472 GNUNET_h2s (&c->id), GMC_state2s (c->state));
473 LOG (GNUNET_ERROR_TYPE_DEBUG,
474 "Connection %s state is now %s\n",
475 GNUNET_h2s (&c->id), GMC_state2s (state));
482 * Send keepalive packets for a connection.
484 * @param c Connection to keep alive..
485 * @param fwd Is this a FWD keepalive? (owner -> dest).
488 connection_keepalive (struct MeshConnection *c, int fwd)
490 struct GNUNET_MESH_ConnectionKeepAlive *msg;
491 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
495 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
496 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
498 LOG (GNUNET_ERROR_TYPE_DEBUG,
499 "sending %s keepalive for connection %s[%d]\n",
504 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
505 msg->header.size = htons (size);
506 msg->header.type = htons (type);
509 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
514 * Send CONNECTION_{CREATE/ACK} packets for a connection.
516 * @param c Connection for which to send the message.
517 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
520 connection_recreate (struct MeshConnection *c, int fwd)
522 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
524 send_connection_create (c);
526 send_connection_ack (c, GNUNET_NO);
531 * Generic connection timer management.
532 * Depending on the role of the peer in the connection will send the
533 * appropriate message (build or keepalive)
535 * @param c Conncetion to maintain.
539 connection_maintain (struct MeshConnection *c, int fwd)
541 if (MESH_TUNNEL_SEARCHING == c->t->state)
543 /* TODO DHT GET with RO_BART */
548 case MESH_CONNECTION_NEW:
550 case MESH_CONNECTION_SENT:
551 connection_recreate (c, fwd);
553 case MESH_CONNECTION_READY:
554 connection_keepalive (c, fwd);
563 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
565 struct MeshConnection *c = cls;
567 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
568 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
571 connection_maintain (c, GNUNET_YES);
572 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
573 &connection_fwd_keepalive,
579 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
581 struct MeshConnection *c = cls;
583 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
584 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
587 connection_maintain (c, GNUNET_NO);
588 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
589 &connection_bck_keepalive,
596 * Get the first transmittable message for a connection.
598 * @param c Connection.
599 * @param fwd Is this FWD?
601 * @return First transmittable message.
603 static struct MeshPeerQueue *
604 connection_get_first_message (struct MeshConnection *c, int fwd)
606 struct MeshPeerQueue *q;
609 p = connection_get_hop (c, fwd);
611 for (q = p->queue_head; NULL != q; q = q->next)
615 if (queue_is_sendable (q))
624 * @brief Re-initiate traffic on this connection if necessary.
626 * Check if there is traffic queued towards this peer
627 * and the core transmit handle is NULL (traffic was stalled).
628 * If so, call core tmt rdy.
630 * @param c Connection on which initiate traffic.
631 * @param fwd Is this about fwd traffic?
634 connection_unlock_queue (struct MeshConnection *c, int fwd)
636 struct MeshPeer *peer;
637 struct MeshPeerQueue *q;
640 LOG (GNUNET_ERROR_TYPE_DEBUG,
641 "connection_unlock_queue %s on %s\n",
642 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
644 if (GMC_is_terminal (c, fwd))
646 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
650 peer = connection_get_hop (c, fwd);
652 if (NULL != peer->core_transmit)
654 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
655 return; /* Already unlocked */
658 q = connection_get_first_message (c, fwd);
661 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
662 return; /* Nothing to transmit */
666 peer->core_transmit =
667 GNUNET_CORE_notify_transmit_ready (core_handle,
670 GNUNET_TIME_UNIT_FOREVER_REL,
671 GNUNET_PEER_resolve2 (peer->id),
679 * Cancel all transmissions that belong to a certain connection.
681 * @param c Connection which to cancel.
682 * @param fwd Cancel fwd traffic?
685 connection_cancel_queues (struct MeshConnection *c, int fwd)
687 struct MeshPeerQueue *q;
688 struct MeshPeerQueue *next;
689 struct MeshFlowControl *fc;
690 struct MeshPeer *peer;
697 fc = fwd ? &c->fwd_fc : &c->bck_fc;
698 peer = connection_get_hop (c, fwd);
700 for (q = peer->queue_head; NULL != q; q = next)
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "connection_cancel_queue %s\n",
707 GNUNET_MESH_DEBUG_M2S (q->type));
708 queue_destroy (q, GNUNET_YES);
711 if (NULL == peer->queue_head)
713 if (NULL != peer->core_transmit)
715 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
716 peer->core_transmit = NULL;
718 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
720 GNUNET_SCHEDULER_cancel (fc->poll_task);
721 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
728 * Function called if a connection has been stalled for a while,
729 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
731 * @param cls Closure (poll ctx).
732 * @param tc TaskContext.
735 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
737 struct MeshFlowControl *fc = cls;
738 struct GNUNET_MESH_Poll msg;
739 struct MeshConnection *c;
741 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
742 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
748 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
749 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
750 GNUNET_h2s (&c->id));
751 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
752 fc == &c->fwd_fc ? "FWD" : "BCK");
754 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
755 msg.header.size = htons (sizeof (msg));
756 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
757 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
758 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
759 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
760 &connection_poll, fc);
767 * Get the previous hop in a connection
769 * @param c Connection.
771 * @return Previous peer in the connection.
773 static struct MeshPeer *
774 connection_get_prev_hop (struct MeshConnection *c)
778 if (0 == c->own_pos || c->path->length < 2)
779 id = c->path->peers[0];
781 id = c->path->peers[c->own_pos - 1];
783 return peer_get_short (id);
788 * Get the next hop in a connection
790 * @param c Connection.
792 * @return Next peer in the connection.
794 static struct MeshPeer *
795 connection_get_next_hop (struct MeshConnection *c)
799 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
800 id = c->path->peers[c->path->length - 1];
802 id = c->path->peers[c->own_pos + 1];
804 return peer_get_short (id);
809 * Get the hop in a connection.
811 * @param c Connection.
812 * @param fwd Next hop?
814 * @return Next peer in the connection.
816 static struct MeshPeer *
817 connection_get_hop (struct MeshConnection *c, int fwd)
820 return connection_get_next_hop (c);
821 return connection_get_prev_hop (c);
828 * Timeout function due to lack of keepalive/traffic from the owner.
829 * Destroys connection if called.
831 * @param cls Closure (connection to destroy).
832 * @param tc TaskContext.
835 connection_fwd_timeout (void *cls,
836 const struct GNUNET_SCHEDULER_TaskContext *tc)
838 struct MeshConnection *c = cls;
840 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
841 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
843 LOG (GNUNET_ERROR_TYPE_DEBUG,
844 "Connection %s[%X] FWD timed out. Destroying.\n",
848 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
856 * Timeout function due to lack of keepalive/traffic from the destination.
857 * Destroys connection if called.
859 * @param cls Closure (connection to destroy).
860 * @param tc TaskContext
863 connection_bck_timeout (void *cls,
864 const struct GNUNET_SCHEDULER_TaskContext *tc)
866 struct MeshConnection *c = cls;
868 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
869 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
872 LOG (GNUNET_ERROR_TYPE_DEBUG,
873 "Connection %s[%X] FWD timed out. Destroying.\n",
877 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
885 * Resets the connection timeout task, some other message has done the
887 * - For the first peer on the direction this means to send
888 * a keepalive or a path confirmation message (either create or ACK).
889 * - For all other peers, this means to destroy the connection,
890 * due to lack of activity.
891 * Starts the tiemout if no timeout was running (connection just created).
893 * @param c Connection whose timeout to reset.
894 * @param fwd Is this forward?
896 * TODO use heap to improve efficiency of scheduler.
899 connection_reset_timeout (struct MeshConnection *c, int fwd)
901 GNUNET_SCHEDULER_TaskIdentifier *ti;
902 GNUNET_SCHEDULER_Task f;
904 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
906 if (GNUNET_SCHEDULER_NO_TASK != *ti)
907 GNUNET_SCHEDULER_cancel (*ti);
909 if (GMC_is_origin (c, fwd)) /* Endpoint */
911 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
912 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
916 struct GNUNET_TIME_Relative delay;
918 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
919 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
920 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
929 register_neighbors (struct MeshConnection *c)
931 struct MeshPeer *peer;
933 peer = connection_get_next_hop (c);
934 if (GNUNET_NO == GMP_is_neighbor (peer))
939 GMP_add_connection (peer, c);
940 peer = connection_get_prev_hop (c);
941 if (GNUNET_NO == GMP_is_neighbor (peer))
946 GMP_add_connection (peer, c);
954 unregister_neighbors (struct MeshConnection *c)
956 struct MeshPeer *peer;
958 peer = connection_get_next_hop (c);
959 GMP_remove_connection (peer, c);
961 peer = connection_get_prev_hop (c);
962 GMP_remove_connection (peer, c);
967 /******************************************************************************/
968 /******************************** API ***********************************/
969 /******************************************************************************/
972 * Core handler for connection creation.
974 * @param cls Closure (unused).
975 * @param peer Sender (neighbor).
976 * @param message Message.
978 * @return GNUNET_OK to keep the connection open,
979 * GNUNET_SYSERR to close it (signal serious error)
982 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
983 const struct GNUNET_MessageHeader *message)
985 struct GNUNET_MESH_ConnectionCreate *msg;
986 struct GNUNET_PeerIdentity *id;
987 struct GNUNET_HashCode *cid;
988 struct MeshPeerPath *path;
989 struct MeshPeer *dest_peer;
990 struct MeshPeer *orig_peer;
991 struct MeshConnection *c;
992 unsigned int own_pos;
996 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
997 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1000 size = ntohs (message->size);
1001 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1003 GNUNET_break_op (0);
1007 /* Calculate hops */
1008 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1009 if (size % sizeof (struct GNUNET_PeerIdentity))
1011 GNUNET_break_op (0);
1014 size /= sizeof (struct GNUNET_PeerIdentity);
1017 GNUNET_break_op (0);
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1022 /* Get parameters */
1023 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1025 id = (struct GNUNET_PeerIdentity *) &msg[1];
1026 LOG (GNUNET_ERROR_TYPE_DEBUG,
1027 " connection %s (%s).\n",
1028 GNUNET_h2s (cid), GNUNET_i2s (id));
1030 /* Create connection */
1031 c = connection_get (cid);
1034 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1035 c = connection_new (cid);
1038 connection_reset_timeout (c, GNUNET_YES);
1041 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1042 path = path_new (size);
1044 for (i = 0; i < size; i++)
1046 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1047 GNUNET_i2s (&id[i]));
1048 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1049 if (path->peers[i] == my_short_id)
1052 if (own_pos == 0 && path->peers[own_pos] != myid)
1054 /* create path: self not found in path through self */
1055 GNUNET_break_op (0);
1056 path_destroy (path);
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1061 path_add_to_peers (path, GNUNET_NO);
1062 c->path = path_duplicate (path);
1063 c->own_pos = own_pos;
1069 if (MESH_CONNECTION_NEW == c->state)
1070 connection_change_state (c, MESH_CONNECTION_SENT);
1072 /* Remember peers */
1073 dest_peer = peer_get (&id[size - 1]);
1074 orig_peer = peer_get (&id[0]);
1076 /* Is it a connection to us? */
1077 if (c->own_pos == size - 1)
1079 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1080 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
1082 if (NULL == orig_peer->tunnel)
1084 orig_peer->tunnel = tunnel_new ();
1085 orig_peer->tunnel->peer = orig_peer;
1087 GMT_add_connection (orig_peer->tunnel, c);
1088 if (MESH_TUNNEL_NEW == GMT_get_state (c->t))
1089 GMT_change_state (c->t, MESH_TUNNEL_WAITING);
1091 send_connection_ack (c, GNUNET_NO);
1092 if (MESH_CONNECTION_SENT == c->state)
1093 connection_change_state (c, MESH_CONNECTION_ACK);
1095 /* Keep tunnel alive in direction dest->owner*/
1096 connection_reset_timeout (c, GNUNET_NO);
1100 /* It's for somebody else! Retransmit. */
1101 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1102 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1103 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
1104 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
1111 * Core handler for path confirmations.
1113 * @param cls closure
1114 * @param message message
1115 * @param peer peer identity this notification is about
1117 * @return GNUNET_OK to keep the connection open,
1118 * GNUNET_SYSERR to close it (signal serious error)
1121 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1122 const struct GNUNET_MessageHeader *message)
1124 struct GNUNET_MESH_ConnectionACK *msg;
1125 struct MeshConnection *c;
1126 struct MeshPeerPath *p;
1127 struct MeshPeer *pi;
1130 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1131 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1132 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1133 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1134 GNUNET_h2s (&msg->cid));
1135 c = connection_get (&msg->cid);
1138 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1140 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1147 pi = peer_get (peer);
1148 if (connection_get_next_hop (c) == pi)
1150 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1152 if (MESH_CONNECTION_SENT == c->state)
1153 connection_change_state (c, MESH_CONNECTION_ACK);
1155 else if (connection_get_prev_hop (c) == pi)
1157 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1159 connection_change_state (c, MESH_CONNECTION_READY);
1163 GNUNET_break_op (0);
1166 connection_reset_timeout (c, fwd);
1168 /* Add path to peers? */
1172 path_add_to_peers (p, GNUNET_YES);
1179 /* Message for us as creator? */
1180 if (connection_is_origin (c, GNUNET_YES))
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1183 connection_change_state (c, MESH_CONNECTION_READY);
1184 if (MESH_TUNNEL_READY != c->t->state)
1185 tunnel_change_state (c->t, MESH_TUNNEL_READY);
1186 send_connection_ack (c, GNUNET_YES);
1187 tunnel_send_queued_data (c->t, GNUNET_YES);
1188 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
1190 GNUNET_DHT_get_stop (c->t->peer->dhtget);
1191 c->t->peer->dhtget = NULL;
1196 /* Message for us as destination? */
1197 if (GMC_is_terminal (c, GNUNET_YES))
1199 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1200 GMC_change_state (c, MESH_CONNECTION_READY);
1201 GMT_change_state (c->t, MESH_TUNNEL_READY);
1202 GMT_send_queued_data (c->t, GNUNET_NO);
1206 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1207 GMC_send_prebuilt_message (message, c, NULL, fwd);
1213 * Core handler for notifications of broken paths
1215 * @param cls Closure (unused).
1216 * @param peer Peer identity of sending neighbor.
1217 * @param message Message.
1219 * @return GNUNET_OK to keep the connection open,
1220 * GNUNET_SYSERR to close it (signal serious error)
1223 GMC_handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
1224 const struct GNUNET_MessageHeader *message)
1226 struct GNUNET_MESH_ConnectionBroken *msg;
1227 struct MeshConnection *c;
1229 LOG (GNUNET_ERROR_TYPE_DEBUG,
1230 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
1231 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1232 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1233 GNUNET_i2s (&msg->peer1));
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1235 GNUNET_i2s (&msg->peer2));
1236 c = connection_get (&msg->cid);
1239 GNUNET_break_op (0);
1242 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
1243 GNUNET_PEER_search (&msg->peer2));
1250 * Core handler for tunnel destruction
1252 * @param cls Closure (unused).
1253 * @param peer Peer identity of sending neighbor.
1254 * @param message Message.
1256 * @return GNUNET_OK to keep the connection open,
1257 * GNUNET_SYSERR to close it (signal serious error)
1260 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1261 const struct GNUNET_MessageHeader *message)
1263 struct GNUNET_MESH_ConnectionDestroy *msg;
1264 struct MeshConnection *c;
1268 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1269 LOG (GNUNET_ERROR_TYPE_DEBUG,
1270 "Got a CONNECTION DESTROY message from %s\n",
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 " for connection %s\n",
1274 GNUNET_h2s (&msg->cid));
1275 c = connection_get (&msg->cid);
1278 /* Probably already got the message from another path,
1279 * destroyed the tunnel and retransmitted to children.
1282 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1286 id = GNUNET_PEER_search (peer);
1287 if (id == GMP_get_short_id (connection_get_prev_hop (c)))
1289 else if (id == GMP_get_short_id (connection_get_next_hop (c)))
1293 GNUNET_break_op (0);
1296 GMC_send_prebuilt_message (message, c, NULL, fwd);
1297 c->destroy = GNUNET_YES;
1303 * Generic handler for mesh network encrypted traffic.
1305 * @param peer Peer identity this notification is about.
1306 * @param message Encrypted message.
1307 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1309 * @return GNUNET_OK to keep the connection open,
1310 * GNUNET_SYSERR to close it (signal serious error)
1313 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1314 const struct GNUNET_MESH_Encrypted *msg,
1317 struct MeshConnection *c;
1318 struct MeshTunnel3 *t;
1319 struct MeshPeer *neighbor;
1320 struct MeshFlowControl *fc;
1327 size = ntohs (msg->header.size);
1329 sizeof (struct GNUNET_MESH_Encrypted) +
1330 sizeof (struct GNUNET_MessageHeader))
1332 GNUNET_break_op (0);
1335 type = ntohs (msg->header.type);
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1338 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1340 /* Check connection */
1341 c = connection_get (&msg->cid);
1344 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1345 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1349 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1351 /* Check if origin is as expected */
1352 neighbor = connection_get_hop (c, !fwd);
1353 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1355 GNUNET_break_op (0);
1360 pid = ntohl (msg->pid);
1361 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1363 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1364 LOG (GNUNET_ERROR_TYPE_DEBUG,
1365 "WARNING Received PID %u, (prev %u), ACK %u\n",
1366 pid, fc->last_pid_recv, fc->last_ack_sent);
1369 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1371 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1372 LOG (GNUNET_ERROR_TYPE_DEBUG,
1373 " Pid %u not expected (%u+), dropping!\n",
1374 pid, fc->last_pid_recv + 1);
1377 if (MESH_CONNECTION_SENT == c->state)
1378 connection_change_state (c, MESH_CONNECTION_READY);
1379 connection_reset_timeout (c, fwd);
1380 fc->last_pid_recv = pid;
1382 /* Is this message for us? */
1383 if (GMC_is_terminal (c, fwd))
1385 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
1387 struct GNUNET_MessageHeader *msgh;
1390 /* TODO signature verification */
1391 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1392 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1394 fc->last_pid_recv = pid;
1395 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
1399 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1400 handle_decrypted (t, msgh, fwd);
1401 off += ntohs (msgh->size);
1403 send_ack (c, NULL, fwd);
1407 /* Message not for us: forward to next hop */
1408 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1409 ttl = ntohl (msg->ttl);
1410 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1413 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1414 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1415 send_ack (c, NULL, fwd);
1418 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1420 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1427 * Core handler for mesh network traffic going orig->dest.
1429 * @param cls Closure (unused).
1430 * @param message Message received.
1431 * @param peer Peer who sent the message.
1433 * @return GNUNET_OK to keep the connection open,
1434 * GNUNET_SYSERR to close it (signal serious error)
1437 GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1438 const struct GNUNET_MessageHeader *message)
1440 return handle_mesh_encrypted (peer,
1441 (struct GNUNET_MESH_Encrypted *)message,
1446 * Core handler for mesh network traffic going dest->orig.
1448 * @param cls Closure (unused).
1449 * @param message Message received.
1450 * @param peer Peer who sent the message.
1452 * @return GNUNET_OK to keep the connection open,
1453 * GNUNET_SYSERR to close it (signal serious error)
1456 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1457 const struct GNUNET_MessageHeader *message)
1459 return handle_mesh_encrypted (peer,
1460 (struct GNUNET_MESH_Encrypted *)message,
1466 * Core handler for mesh network traffic point-to-point acks.
1468 * @param cls closure
1469 * @param message message
1470 * @param peer peer identity this notification is about
1472 * @return GNUNET_OK to keep the connection open,
1473 * GNUNET_SYSERR to close it (signal serious error)
1476 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1477 const struct GNUNET_MessageHeader *message)
1479 struct GNUNET_MESH_ACK *msg;
1480 struct MeshConnection *c;
1481 struct MeshFlowControl *fc;
1486 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1487 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1489 msg = (struct GNUNET_MESH_ACK *) message;
1491 c = connection_get (&msg->cid);
1495 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1500 /* Is this a forward or backward ACK? */
1501 id = GNUNET_PEER_search (peer);
1502 if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1504 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1508 else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1510 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1516 GNUNET_break_op (0);
1520 ack = ntohl (msg->ack);
1521 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1522 ack, fc->last_ack_recv);
1523 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1524 fc->last_ack_recv = ack;
1526 /* Cancel polling if the ACK is big enough. */
1527 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1528 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1530 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1531 GNUNET_SCHEDULER_cancel (fc->poll_task);
1532 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1533 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1536 connection_unlock_queue (c, fwd);
1543 * Core handler for mesh network traffic point-to-point ack polls.
1545 * @param cls closure
1546 * @param message message
1547 * @param peer peer identity this notification is about
1549 * @return GNUNET_OK to keep the connection open,
1550 * GNUNET_SYSERR to close it (signal serious error)
1553 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1554 const struct GNUNET_MessageHeader *message)
1556 struct GNUNET_MESH_Poll *msg;
1557 struct MeshConnection *c;
1558 struct MeshFlowControl *fc;
1563 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1564 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1567 msg = (struct GNUNET_MESH_Poll *) message;
1569 c = connection_get (&msg->cid);
1573 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1575 GNUNET_break_op (0);
1579 /* Is this a forward or backward ACK?
1580 * Note: a poll should never be needed in a loopback case,
1581 * since there is no possiblility of packet loss there, so
1582 * this way of discerining FWD/BCK should not be a problem.
1584 id = GNUNET_PEER_search (peer);
1585 if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1590 else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1592 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1597 GNUNET_break_op (0);
1601 pid = ntohl (msg->pid);
1602 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1603 pid, fc->last_pid_recv);
1604 fc->last_pid_recv = pid;
1605 fwd = fc == &c->fwd_fc;
1606 send_ack (c, NULL, fwd);
1613 * Core handler for mesh keepalives.
1615 * @param cls closure
1616 * @param message message
1617 * @param peer peer identity this notification is about
1618 * @return GNUNET_OK to keep the connection open,
1619 * GNUNET_SYSERR to close it (signal serious error)
1621 * TODO: Check who we got this from, to validate route.
1624 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1625 const struct GNUNET_MessageHeader *message)
1627 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1628 struct MeshConnection *c;
1629 struct MeshPeer *neighbor;
1632 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1633 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1636 c = connection_get (&msg->cid);
1639 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1644 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1645 GNUNET_YES : GNUNET_NO;
1647 /* Check if origin is as expected */
1648 neighbor = connection_get_hop (c, fwd);
1649 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1651 GNUNET_break_op (0);
1655 connection_change_state (c, MESH_CONNECTION_READY);
1656 connection_reset_timeout (c, fwd);
1658 if (GMC_is_terminal (c, fwd))
1661 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1662 GMC_send_prebuilt_message (message, c, NULL, fwd);
1669 * Send an ACK on the appropriate connection/channel, depending on
1670 * the direction and the position of the peer.
1672 * @param c Which connection to send the hop-by-hop ACK.
1673 * @param ch Channel, if any.
1674 * @param fwd Is this a fwd ACK? (will go dest->root)
1677 GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1679 unsigned int buffer;
1681 LOG (GNUNET_ERROR_TYPE_DEBUG,
1682 "send ack %s on %p %p\n",
1683 fwd ? "FWD" : "BCK", c, ch);
1684 if (NULL == c || GMC_is_terminal (c, fwd))
1686 struct MeshTunnel3 *t;
1687 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n");
1688 t = (NULL == c) ? GMCH_get_tunnel (ch) : GMC_get_tunnel (c);
1689 buffer = GMT_get_buffer (t, fwd);
1693 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1694 buffer = GMC_get_buffer (c, fwd);
1696 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1698 if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1699 (NULL != c && GMC_is_origin (c, fwd)) )
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1704 GNUNET_assert (NULL != ch);
1705 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1706 send_local_ack (ch, fwd);
1711 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
1712 GNUNET_assert (NULL != ch);
1713 channel_send_connections_ack (ch, buffer, fwd);
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1718 connection_send_ack (c, buffer, fwd);
1724 * Initialize the connections subsystem
1726 * @param c Configuration handle.
1729 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1732 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1735 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1736 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1737 GNUNET_SCHEDULER_shutdown ();
1742 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1745 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1746 "MESH", "MAX_CONNECTIONS", "MISSING");
1747 GNUNET_SCHEDULER_shutdown ();
1752 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1753 &refresh_connection_time))
1755 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1756 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1757 GNUNET_SCHEDULER_shutdown ();
1760 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1764 * Shut down the connections subsystem.
1772 struct MeshConnection *
1773 GMC_new (const struct GNUNET_HashCode *cid,
1774 struct MeshTunnel3 *t,
1775 struct MeshPeerPath *p,
1776 unsigned int own_pos)
1778 struct MeshConnection *c;
1780 c = GNUNET_new (struct MeshConnection);
1782 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1783 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1784 fc_init (&c->fwd_fc);
1785 fc_init (&c->bck_fc);
1790 if (own_pos > p->length - 1)
1796 c->own_pos = own_pos;
1801 c->fwd_maintenance_task =
1802 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1803 &connection_fwd_keepalive, c);
1805 register_neighbors (c);
1811 GMC_destroy (struct MeshConnection *c)
1813 struct MeshPeer *peer;
1818 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n",
1819 GNUNET_h2s (&c->id));
1821 /* Cancel all traffic */
1822 connection_cancel_queues (c, GNUNET_YES);
1823 connection_cancel_queues (c, GNUNET_NO);
1825 /* Cancel maintainance task (keepalive/timeout) */
1826 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1827 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1828 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1829 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1831 /* Unregister from neighbors */
1832 unregister_neighbors (c);
1835 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1836 GMT_remove_connection (c->t, c);
1841 * Get the connection ID.
1843 * @param c Connection to get the ID from.
1845 * @return ID of the connection.
1847 const struct GNUNET_HashCode *
1848 GMC_get_id (const struct MeshConnection *c)
1855 * Get the connection state.
1857 * @param c Connection to get the state from.
1859 * @return state of the connection.
1861 enum MeshConnectionState
1862 GMC_get_state (const struct MeshConnection *c)
1868 * Get the connection tunnel.
1870 * @param c Connection to get the tunnel from.
1872 * @return tunnel of the connection.
1874 struct MeshTunnel3 *
1875 GMC_get_tunnel (const struct MeshConnection *c)
1882 * Get free buffer space in a connection.
1884 * @param c Connection.
1885 * @param fwd Is query about FWD traffic?
1887 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1890 GMC_get_buffer (struct MeshConnection *c, int fwd)
1892 struct MeshFlowControl *fc;
1894 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1896 return (fc->queue_max - fc->queue_n);
1900 * Get messages queued in a connection.
1902 * @param c Connection.
1903 * @param fwd Is query about FWD traffic?
1905 * @return Number of messages queued.
1908 GMC_get_qn (struct MeshConnection *c, int fwd)
1910 struct MeshFlowControl *fc;
1912 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1919 * Notify other peers on a connection of a broken link. Mark connections
1920 * to destroy after all traffic has been sent.
1922 * @param c Connection on which there has been a disconnection.
1923 * @param peer Peer that disconnected.
1924 * @param my_full_id My ID (to send to other peers).
1927 GMC_notify_broken (struct MeshConnection *c,
1928 struct MeshPeer *peer,
1929 struct GNUNET_PeerIdentity *my_full_id)
1931 struct GNUNET_MESH_ConnectionBroken msg;
1934 fwd = peer == connection_get_prev_hop (c);
1936 connection_cancel_queues (c, !fwd);
1937 if (GMC_is_terminal (c, fwd))
1939 /* Local shutdown, no one to notify about this. */
1944 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
1945 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
1947 msg.peer1 = *my_full_id;
1948 msg.peer2 = *GMP_get_id (peer);
1949 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
1950 c->destroy = GNUNET_YES;
1957 * Is this peer the first one on the connection?
1959 * @param c Connection.
1960 * @param fwd Is this about fwd traffic?
1962 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1965 GMC_is_origin (struct MeshConnection *c, int fwd)
1967 if (!fwd && c->path->length - 1 == c->own_pos )
1969 if (fwd && 0 == c->own_pos)
1976 * Is this peer the last one on the connection?
1978 * @param c Connection.
1979 * @param fwd Is this about fwd traffic?
1980 * Note that the ROOT is the terminal for BCK traffic!
1982 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1985 GMC_is_terminal (struct MeshConnection *c, int fwd)
1987 return GMC_is_origin (c, !fwd);
1992 * Sends an already built message on a connection, properly registering
1993 * all used resources.
1995 * @param message Message to send. Function makes a copy of it.
1996 * If message is not hop-by-hop, decrements TTL of copy.
1997 * @param c Connection on which this message is transmitted.
1998 * @param ch Channel on which this message is transmitted, or NULL.
1999 * @param fwd Is this a fwd message?
2002 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2003 struct MeshConnection *c,
2004 struct MeshChannel *ch,
2011 size = ntohs (message->size);
2012 data = GNUNET_malloc (size);
2013 memcpy (data, message, size);
2014 type = ntohs (message->type);
2015 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2016 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
2020 struct GNUNET_MESH_Encrypted *emsg;
2021 struct GNUNET_MESH_ACK *amsg;
2022 struct GNUNET_MESH_Poll *pmsg;
2023 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2024 struct GNUNET_MESH_ConnectionBroken *bmsg;
2027 case GNUNET_MESSAGE_TYPE_MESH_FWD:
2028 case GNUNET_MESSAGE_TYPE_MESH_BCK:
2029 emsg = (struct GNUNET_MESH_Encrypted *) data;
2030 ttl = ntohl (emsg->ttl);
2033 GNUNET_break_op (0);
2037 emsg->ttl = htonl (ttl - 1);
2038 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2039 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
2042 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2043 amsg = (struct GNUNET_MESH_ACK *) data;
2045 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2048 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2049 pmsg = (struct GNUNET_MESH_Poll *) data;
2051 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2052 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2055 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2056 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2061 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2062 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2067 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2068 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2075 GMP_queue_add (data,
2085 * Send a message to all peers in this connection that the connection
2086 * is no longer valid.
2088 * If some peer should not receive the message, it should be zero'ed out
2089 * before calling this function.
2091 * @param c The connection whose peers to notify.
2094 GMC_send_destroy (struct MeshConnection *c)
2096 struct GNUNET_MESH_ConnectionDestroy msg;
2098 if (GNUNET_YES == c->destroy)
2101 msg.header.size = htons (sizeof (msg));
2102 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2104 LOG (GNUNET_ERROR_TYPE_DEBUG,
2105 " sending connection destroy for connection %s\n",
2106 GNUNET_h2s (&c->id));
2108 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2109 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2110 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2111 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2112 c->destroy = GNUNET_YES;