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 "mesh_path.h"
33 #include "mesh_protocol.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
42 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
43 GNUNET_TIME_UNIT_MINUTES,\
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct to encapsulate all the Flow Control information to a peer to which
54 * we are directly connected (on a core level).
56 struct MeshFlowControl
59 * Connection this controls.
61 struct MeshConnection *c;
64 * How many messages are in the queue on this connection.
69 * How many messages do we accept in the queue.
71 unsigned int queue_max;
79 * ID of the last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 GNUNET_SCHEDULER_TaskIdentifier poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
109 * Queued poll message, to cancel if not necessary anymore (got ACK).
111 struct MeshConnectionQueue *poll_msg;
114 * Queued poll message, to cancel if not necessary anymore (got ACK).
116 struct MeshConnectionQueue *ack_msg;
120 * Keep a record of the last messages sent on this connection.
122 struct MeshConnectionPerformance
125 * Circular buffer for storing measurements.
127 double usecsperbyte[AVG_MSGS];
130 * Running average of @c usecsperbyte.
135 * How many values of @c usecsperbyte are valid.
140 * Index of the next "free" position in @c usecsperbyte.
147 * Struct containing all information regarding a connection to a peer.
149 struct MeshConnection
152 * Tunnel this connection is part of.
154 struct MeshTunnel3 *t;
157 * Flow control information for traffic fwd.
159 struct MeshFlowControl fwd_fc;
162 * Flow control information for traffic bck.
164 struct MeshFlowControl bck_fc;
167 * Measure connection performance on the endpoint.
169 struct MeshConnectionPerformance *perf;
172 * ID of the connection.
174 struct GNUNET_HashCode id;
177 * State of the connection.
179 enum MeshConnectionState state;
182 * Path being used for the tunnel. At the origin of the connection
183 * it's a pointer to the destination's path pool, otherwise just a copy.
185 struct MeshPeerPath *path;
188 * Position of the local peer in the path.
190 unsigned int own_pos;
193 * Task to keep the used paths alive at the owner,
194 * time tunnel out on all the other peers.
196 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
199 * Task to keep the used paths alive at the destination,
200 * time tunnel out on all the other peers.
202 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
205 * Queue handle for maintainance traffic. One handle for FWD and BCK since
206 * one peer never needs to maintain both directions (no loopback connections).
208 struct MeshPeerQueue *maintenance_q;
211 * Counter to do exponential backoff when creating a connection (max 64).
213 unsigned short create_retry;
216 * Pending message count.
218 int pending_messages;
221 * Destroy flag: if true, destroy on last message.
227 * Handle for messages queued but not yet sent.
229 struct MeshConnectionQueue
232 * Peer queue handle, to cancel if necessary.
234 struct MeshPeerQueue *q;
237 * Was this a forced message? (Do not account for it)
242 * Continuation to call once sent.
247 * Closure for @c cont.
252 /******************************************************************************/
253 /******************************* GLOBALS ***********************************/
254 /******************************************************************************/
257 * Global handle to the statistics service.
259 extern struct GNUNET_STATISTICS_Handle *stats;
262 * Local peer own ID (memory efficient handle).
264 extern GNUNET_PEER_Id myid;
267 * Local peer own ID (full value).
269 extern struct GNUNET_PeerIdentity my_full_id;
272 * Connections known, indexed by cid (MeshConnection).
274 static struct GNUNET_CONTAINER_MultiHashMap *connections;
277 * How many connections are we willing to maintain.
278 * Local connections are always allowed, even if there are more connections than max.
280 static unsigned long long max_connections;
283 * How many messages *in total* are we willing to queue, divide by number of
284 * connections to get connection queue size.
286 static unsigned long long max_msgs_queue;
289 * How often to send path keepalives. Paths timeout after 4 missed.
291 static struct GNUNET_TIME_Relative refresh_connection_time;
294 * How often to send path create / ACKs.
296 static struct GNUNET_TIME_Relative create_connection_time;
299 /******************************************************************************/
300 /******************************** STATIC ***********************************/
301 /******************************************************************************/
303 #if 0 // avoid compiler warning for unused static function
305 fc_debug (struct MeshFlowControl *fc)
307 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
308 fc->last_pid_recv, fc->last_ack_sent);
309 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
310 fc->last_pid_sent, fc->last_ack_recv);
311 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
312 fc->queue_n, fc->queue_max);
316 connection_debug (struct MeshConnection *c)
320 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
324 peer2s (c->t->peer), GMC_2s (c));
325 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
326 c->state, c->pending_messages);
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
328 fc_debug (&c->fwd_fc);
329 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
330 fc_debug (&c->bck_fc);
336 * Schedule next keepalive task, taking in consideration
337 * the connection state and number of retries.
339 * @param c Connection for which to schedule the next keepalive.
340 * @param fwd Direction for the next keepalive.
343 schedule_next_keepalive (struct MeshConnection *c, int fwd);
347 * Resets the connection timeout task, some other message has done the
349 * - For the first peer on the direction this means to send
350 * a keepalive or a path confirmation message (either create or ACK).
351 * - For all other peers, this means to destroy the connection,
352 * due to lack of activity.
353 * Starts the timeout if no timeout was running (connection just created).
355 * @param c Connection whose timeout to reset.
356 * @param fwd Is this forward?
359 connection_reset_timeout (struct MeshConnection *c, int fwd);
363 * Get string description for tunnel state.
365 * @param s Tunnel state.
367 * @return String representation.
370 GMC_state2s (enum MeshConnectionState s)
374 case MESH_CONNECTION_NEW:
375 return "MESH_CONNECTION_NEW";
376 case MESH_CONNECTION_SENT:
377 return "MESH_CONNECTION_SENT";
378 case MESH_CONNECTION_ACK:
379 return "MESH_CONNECTION_ACK";
380 case MESH_CONNECTION_READY:
381 return "MESH_CONNECTION_READY";
382 case MESH_CONNECTION_DESTROYED:
383 return "MESH_CONNECTION_DESTROYED";
385 return "MESH_CONNECTION_STATE_ERROR";
391 * Initialize a Flow Control structure to the initial state.
393 * @param fc Flow Control structure to initialize.
396 fc_init (struct MeshFlowControl *fc)
399 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
400 fc->last_pid_recv = (uint32_t) -1;
401 fc->last_ack_sent = (uint32_t) 0;
402 fc->last_ack_recv = (uint32_t) 0;
403 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
404 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
406 fc->queue_max = (max_msgs_queue / max_connections) + 1;
413 * @param cid Connection ID.
415 static struct MeshConnection *
416 connection_get (const struct GNUNET_HashCode *cid)
418 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
423 connection_change_state (struct MeshConnection* c,
424 enum MeshConnectionState state)
426 LOG (GNUNET_ERROR_TYPE_DEBUG,
427 "Connection %s state was %s\n",
428 GMC_2s (c), GMC_state2s (c->state));
429 if (MESH_CONNECTION_DESTROYED == c->state)
431 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
434 LOG (GNUNET_ERROR_TYPE_DEBUG,
435 "Connection %s state is now %s\n",
436 GMC_2s (c), GMC_state2s (state));
438 if (MESH_CONNECTION_READY == state)
444 * Callback called when a queued ACK message is sent.
446 * @param cls Closure (FC).
447 * @param c Connection this message was on.
448 * @param q Queue handler this call invalidates.
449 * @param type Type of message sent.
450 * @param fwd Was this a FWD going message?
451 * @param size Size of the message.
455 struct MeshConnection *c,
456 struct MeshConnectionQueue *q,
457 uint16_t type, int fwd, size_t size)
459 struct MeshFlowControl *fc = cls;
466 * Send an ACK on the connection, informing the predecessor about
467 * the available buffer space. Should not be called in case the peer
468 * is origin (no predecessor) in the @c fwd direction.
470 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
471 * the ACK itself goes "back" (dest->root).
473 * @param c Connection on which to send the ACK.
474 * @param buffer How much space free to advertise?
475 * @param fwd Is this FWD ACK? (Going dest -> root)
476 * @param force Don't optimize out.
479 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
481 struct MeshFlowControl *next_fc;
482 struct MeshFlowControl *prev_fc;
483 struct GNUNET_MESH_ACK msg;
487 /* If origin, there is no connection to send ACKs. Wrong function! */
488 if (GMC_is_origin (c, fwd))
490 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
491 GMC_2s (c), GM_f2s (fwd));
496 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
497 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
499 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
500 GM_f2s (fwd), GMC_2s (c));
502 /* Check if we need to transmit the ACK. */
503 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
504 if (3 < delta && buffer < delta && GNUNET_NO == force)
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
507 LOG (GNUNET_ERROR_TYPE_DEBUG,
508 " last pid recv: %u, last ack sent: %u\n",
509 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
513 /* Ok, ACK might be necessary, what PID to ACK? */
514 ack = prev_fc->last_pid_recv + buffer;
515 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
516 LOG (GNUNET_ERROR_TYPE_DEBUG,
517 " last pid %u, last ack %u, qmax %u, q %u\n",
518 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
519 next_fc->queue_max, next_fc->queue_n);
520 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
522 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
526 /* Check if message is already in queue */
527 if (NULL != prev_fc->ack_msg)
529 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
531 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
532 GMC_cancel (prev_fc->ack_msg);
533 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
537 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
542 prev_fc->last_ack_sent = ack;
544 /* Build ACK message and send on connection */
545 msg.header.size = htons (sizeof (msg));
546 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
547 msg.ack = htonl (ack);
550 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
557 * Callback called when a queued message is sent.
559 * Calculates the average time and connection packet tracking.
561 * @param cls Closure (ConnectionQueue Handle).
562 * @param c Connection this message was on.
563 * @param type Type of message sent.
564 * @param fwd Was this a FWD going message?
565 * @param size Size of the message.
566 * @param wait Time spent waiting for core (only the time for THIS message)
569 message_sent (void *cls,
570 struct MeshConnection *c, uint16_t type,
571 int fwd, size_t size,
572 struct GNUNET_TIME_Relative wait)
574 struct MeshConnectionPerformance *p;
575 struct MeshFlowControl *fc;
576 struct MeshConnectionQueue *q = cls;
580 fc = fwd ? &c->fwd_fc : &c->bck_fc;
581 LOG (GNUNET_ERROR_TYPE_DEBUG,
585 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
591 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
592 q->cont (q->cont_cls, c, q, type, fwd, size);
596 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
598 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
605 c->pending_messages--;
606 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
608 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
612 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
615 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
616 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
617 c->maintenance_q = NULL;
618 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
619 if (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE == type || !fwd)
620 schedule_next_keepalive (c, fwd);
623 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
625 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
626 if (GNUNET_NO == forced)
629 LOG (GNUNET_ERROR_TYPE_DEBUG,
630 "! accounting pid %u\n",
635 LOG (GNUNET_ERROR_TYPE_DEBUG,
636 "! forced, Q_N not accounting pid %u\n",
639 GMC_send_ack (c, fwd, GNUNET_NO);
640 connection_reset_timeout (c, fwd);
643 case GNUNET_MESSAGE_TYPE_MESH_POLL:
647 case GNUNET_MESSAGE_TYPE_MESH_ACK:
654 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
657 return; /* Only endpoints are interested in timing. */
660 usecsperbyte = ((double) wait.rel_value_us) / size;
661 if (p->size == AVG_MSGS)
663 /* Array is full. Substract oldest value, add new one and store. */
664 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
665 p->usecsperbyte[p->idx] = usecsperbyte;
666 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
670 /* Array not yet full. Add current value to avg and store. */
671 p->usecsperbyte[p->idx] = usecsperbyte;
673 p->avg += p->usecsperbyte[p->idx];
677 p->idx = (p->idx + 1) % AVG_MSGS;
682 * Get the previous hop in a connection
684 * @param c Connection.
686 * @return Previous peer in the connection.
688 static struct MeshPeer *
689 get_prev_hop (const struct MeshConnection *c)
693 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
694 GMC_2s (c), c->own_pos, c->path->length);
695 if (0 == c->own_pos || c->path->length < 2)
696 id = c->path->peers[0];
698 id = c->path->peers[c->own_pos - 1];
700 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
701 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
703 return GMP_get_short (id);
708 * Get the next hop in a connection
710 * @param c Connection.
712 * @return Next peer in the connection.
714 static struct MeshPeer *
715 get_next_hop (const struct MeshConnection *c)
719 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
720 GMC_2s (c), c->own_pos, c->path->length);
721 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
722 id = c->path->peers[c->path->length - 1];
724 id = c->path->peers[c->own_pos + 1];
726 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
727 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
729 return GMP_get_short (id);
734 * Get the hop in a connection.
736 * @param c Connection.
737 * @param fwd Next hop?
739 * @return Next peer in the connection.
741 static struct MeshPeer *
742 get_hop (struct MeshConnection *c, int fwd)
745 return get_next_hop (c);
746 return get_prev_hop (c);
751 * Is traffic coming from this sender 'FWD' traffic?
753 * @param c Connection to check.
754 * @param sender Peer identity of neighbor.
756 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
757 * the traffic is 'FWD'.
758 * #GNUNET_NO for BCK.
759 * #GNUNET_SYSERR for errors.
762 is_fwd (const struct MeshConnection *c,
763 const struct GNUNET_PeerIdentity *sender)
767 id = GNUNET_PEER_search (sender);
768 if (GMP_get_short_id (get_prev_hop (c)) == id)
771 if (GMP_get_short_id (get_next_hop (c)) == id)
775 return GNUNET_SYSERR;
780 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
781 * or a first CONNECTION_ACK directed to us.
783 * @param connection Connection to confirm.
784 * @param fwd Should we send it FWD? (root->dest)
785 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
788 send_connection_ack (struct MeshConnection *connection, int fwd)
790 struct MeshTunnel3 *t;
793 LOG (GNUNET_ERROR_TYPE_INFO, "=> {%18s ACK} on connection %s\n",
794 GM_f2s (!fwd), GMC_2s (connection));
795 GMP_queue_add (get_hop (connection, fwd), NULL,
796 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
797 sizeof (struct GNUNET_MESH_ConnectionACK),
798 connection, fwd, &message_sent, NULL);
799 connection->pending_messages++;
800 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
801 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
802 if (MESH_CONNECTION_READY != connection->state)
803 connection_change_state (connection, MESH_CONNECTION_SENT);
808 * Send a notification that a connection is broken.
810 * @param c Connection that is broken.
811 * @param id1 Peer that has disconnected.
812 * @param id2 Peer that has disconnected.
813 * @param fwd Direction towards which to send it.
816 send_broken (struct MeshConnection *c,
817 const struct GNUNET_PeerIdentity *id1,
818 const struct GNUNET_PeerIdentity *id2,
821 struct GNUNET_MESH_ConnectionBroken msg;
823 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
824 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
828 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
833 * Send a notification that a connection is broken, when a connection
834 * isn't even created.
836 * @param connection_id Connection ID.
837 * @param id1 Peer that has disconnected.
838 * @param id2 Peer that has disconnected.
839 * @param peer Peer to notify (neighbor who sent the connection).
842 send_broken2 (struct GNUNET_HashCode *connection_id,
843 const struct GNUNET_PeerIdentity *id1,
844 const struct GNUNET_PeerIdentity *id2,
845 GNUNET_PEER_Id peer_id)
847 struct GNUNET_MESH_ConnectionBroken *msg;
848 struct MeshPeer *neighbor;
850 LOG (GNUNET_ERROR_TYPE_INFO,
851 "Send BROKEN on unknown connection %s\n", GNUNET_h2s (connection_id));
853 msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
854 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
855 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
856 msg->cid = *connection_id;
859 neighbor = GMP_get_short (peer_id);
860 GMP_queue_add (neighbor, msg,
861 GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED,
862 sizeof (struct GNUNET_MESH_ConnectionBroken),
863 NULL, GNUNET_SYSERR, /* connection, fwd */
864 NULL, NULL); /* continuation */
869 * Send keepalive packets for a connection.
871 * @param c Connection to keep alive..
872 * @param fwd Is this a FWD keepalive? (owner -> dest).
875 send_connection_keepalive (struct MeshConnection *c, int fwd)
877 struct GNUNET_MessageHeader msg;
879 LOG (GNUNET_ERROR_TYPE_INFO,
880 "keepalive %s for connection %s\n",
881 GM_f2s (fwd), GMC_2s (c));
882 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
884 GNUNET_assert (NULL != c->t);
885 msg.size = htons (sizeof (msg));
886 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
888 GNUNET_assert (NULL ==
889 GMT_send_prebuilt_message (&msg, c->t, c,
890 GNUNET_NO, NULL, NULL));
895 * Send CONNECTION_{CREATE/ACK} packets for a connection.
897 * @param c Connection for which to send the message.
898 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
901 connection_recreate (struct MeshConnection *c, int fwd)
903 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
907 send_connection_ack (c, GNUNET_NO);
912 * Generic connection timer management.
913 * Depending on the role of the peer in the connection will send the
914 * appropriate message (build or keepalive)
916 * @param c Conncetion to maintain.
920 connection_maintain (struct MeshConnection *c, int fwd)
922 if (GNUNET_NO != c->destroy)
925 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
927 /* TODO DHT GET with RO_BART */
932 case MESH_CONNECTION_NEW:
935 case MESH_CONNECTION_SENT:
936 connection_recreate (c, fwd);
938 case MESH_CONNECTION_READY:
939 send_connection_keepalive (c, fwd);
949 * Keep the connection alive.
951 * @param c Connection to keep alive.
952 * @param fwd Direction.
953 * @param shutdown Are we shutting down? (Don't send traffic)
954 * Non-zero value for true, not necessarily GNUNET_YES.
957 connection_keepalive (struct MeshConnection *c, int fwd, int shutdown)
959 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
960 GM_f2s (fwd), GMC_2s (c));
963 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
965 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
967 if (GNUNET_NO != shutdown)
970 connection_maintain (c, fwd);
972 /* Next execution will be scheduled by message_sent */
977 * Keep the connection alive in the FWD direction.
979 * @param cls Closure (connection to keepalive).
980 * @param tc TaskContext.
983 connection_fwd_keepalive (void *cls,
984 const struct GNUNET_SCHEDULER_TaskContext *tc)
986 connection_keepalive ((struct MeshConnection *) cls,
988 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
993 * Keep the connection alive in the BCK direction.
995 * @param cls Closure (connection to keepalive).
996 * @param tc TaskContext.
999 connection_bck_keepalive (void *cls,
1000 const struct GNUNET_SCHEDULER_TaskContext *tc)
1002 connection_keepalive ((struct MeshConnection *) cls,
1004 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1009 * Schedule next keepalive task, taking in consideration
1010 * the connection state and number of retries.
1012 * If the peer is not the origin, do nothing.
1014 * @param c Connection for which to schedule the next keepalive.
1015 * @param fwd Direction for the next keepalive.
1018 schedule_next_keepalive (struct MeshConnection *c, int fwd)
1020 struct GNUNET_TIME_Relative delay;
1021 GNUNET_SCHEDULER_TaskIdentifier *task_id;
1022 GNUNET_SCHEDULER_Task keepalive_task;
1024 if (GNUNET_NO == GMC_is_origin (c, fwd))
1027 /* Calculate delay to use, depending on the state of the connection */
1028 if (MESH_CONNECTION_READY == c->state)
1030 delay = refresh_connection_time;
1034 if (1 > c->create_retry)
1035 c->create_retry = 1;
1036 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1038 if (c->create_retry < 64)
1039 c->create_retry *= 2;
1042 /* Select direction-dependent parameters */
1043 if (GNUNET_YES == fwd)
1045 task_id = &c->fwd_maintenance_task;
1046 keepalive_task = &connection_fwd_keepalive;
1050 task_id = &c->bck_maintenance_task;
1051 keepalive_task = &connection_bck_keepalive;
1054 /* Check that no one scheduled it before us */
1055 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1057 /* No need for a _break. It can happen for instance when sending a SYNACK
1058 * for a duplicate SYN: the first SYNACK scheduled the task. */
1059 GNUNET_SCHEDULER_cancel (*task_id);
1062 /* Schedule the task */
1063 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1065 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1070 * @brief Re-initiate traffic on this connection if necessary.
1072 * Check if there is traffic queued towards this peer
1073 * and the core transmit handle is NULL (traffic was stalled).
1074 * If so, call core tmt rdy.
1076 * @param c Connection on which initiate traffic.
1077 * @param fwd Is this about fwd traffic?
1080 connection_unlock_queue (struct MeshConnection *c, int fwd)
1082 struct MeshPeer *peer;
1084 LOG (GNUNET_ERROR_TYPE_DEBUG,
1085 "connection_unlock_queue %s on %s\n",
1086 GM_f2s (fwd), GMC_2s (c));
1088 if (GMC_is_terminal (c, fwd))
1090 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1094 peer = get_hop (c, fwd);
1095 GMP_queue_unlock (peer, c);
1100 * Cancel all transmissions that belong to a certain connection.
1102 * If the connection is scheduled for destruction and no more messages are left,
1103 * the connection will be destroyed by the continuation call.
1105 * @param c Connection which to cancel. Might be destroyed during this call.
1106 * @param fwd Cancel fwd traffic?
1109 connection_cancel_queues (struct MeshConnection *c, int fwd)
1111 struct MeshFlowControl *fc;
1112 struct MeshPeer *peer;
1114 LOG (GNUNET_ERROR_TYPE_DEBUG,
1115 " *** Cancel %s queues for connection %s\n",
1116 GM_f2s (fwd), GMC_2s (c));
1123 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1124 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1126 GNUNET_SCHEDULER_cancel (fc->poll_task);
1127 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1128 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1130 peer = get_hop (c, fwd);
1131 GMP_queue_cancel (peer, c);
1136 * Function called if a connection has been stalled for a while,
1137 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1139 * @param cls Closure (poll ctx).
1140 * @param tc TaskContext.
1143 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1147 * Callback called when a queued POLL message is sent.
1149 * @param cls Closure (FC).
1150 * @param c Connection this message was on.
1151 * @param q Queue handler this call invalidates.
1152 * @param type Type of message sent.
1153 * @param fwd Was this a FWD going message?
1154 * @param size Size of the message.
1157 poll_sent (void *cls,
1158 struct MeshConnection *c,
1159 struct MeshConnectionQueue *q,
1160 uint16_t type, int fwd, size_t size)
1162 struct MeshFlowControl *fc = cls;
1164 if (2 == c->destroy)
1166 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1169 LOG (GNUNET_ERROR_TYPE_DEBUG,
1170 " *** POLL sent for , scheduling new one!\n");
1171 fc->poll_msg = NULL;
1172 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1173 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1174 &connection_poll, fc);
1175 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1180 * Function called if a connection has been stalled for a while,
1181 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1183 * @param cls Closure (poll ctx).
1184 * @param tc TaskContext.
1187 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1189 struct MeshFlowControl *fc = cls;
1190 struct GNUNET_MESH_Poll msg;
1191 struct MeshConnection *c;
1193 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1194 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1200 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1202 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1203 fc == &c->fwd_fc ? "FWD" : "BCK");
1205 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1206 msg.header.size = htons (sizeof (msg));
1207 msg.pid = htonl (fc->last_pid_sent);
1208 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1209 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1210 fc == &c->fwd_fc, GNUNET_YES,
1216 * Timeout function due to lack of keepalive/traffic from the owner.
1217 * Destroys connection if called.
1219 * @param cls Closure (connection to destroy).
1220 * @param tc TaskContext.
1223 connection_fwd_timeout (void *cls,
1224 const struct GNUNET_SCHEDULER_TaskContext *tc)
1226 struct MeshConnection *c = cls;
1228 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1229 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1232 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1234 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1245 * Timeout function due to lack of keepalive/traffic from the destination.
1246 * Destroys connection if called.
1248 * @param cls Closure (connection to destroy).
1249 * @param tc TaskContext
1252 connection_bck_timeout (void *cls,
1253 const struct GNUNET_SCHEDULER_TaskContext *tc)
1255 struct MeshConnection *c = cls;
1257 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1258 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1261 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1264 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1275 * Resets the connection timeout task, some other message has done the
1277 * - For the first peer on the direction this means to send
1278 * a keepalive or a path confirmation message (either create or ACK).
1279 * - For all other peers, this means to destroy the connection,
1280 * due to lack of activity.
1281 * Starts the timeout if no timeout was running (connection just created).
1283 * @param c Connection whose timeout to reset.
1284 * @param fwd Is this forward?
1286 * TODO use heap to improve efficiency of scheduler.
1289 connection_reset_timeout (struct MeshConnection *c, int fwd)
1291 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1293 if (GMC_is_origin (c, fwd)) /* Startpoint */
1295 schedule_next_keepalive (c, fwd);
1297 else /* Relay, endpoint. */
1299 struct GNUNET_TIME_Relative delay;
1300 GNUNET_SCHEDULER_TaskIdentifier *ti;
1301 GNUNET_SCHEDULER_Task f;
1303 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1305 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1306 GNUNET_SCHEDULER_cancel (*ti);
1307 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1308 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1309 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1315 * Add the connection to the list of both neighbors.
1317 * @param c Connection.
1319 * @return #GNUNET_OK if everything went fine
1320 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1323 register_neighbors (struct MeshConnection *c)
1325 struct MeshPeer *next_peer;
1326 struct MeshPeer *prev_peer;
1328 next_peer = get_next_hop (c);
1329 prev_peer = get_prev_hop (c);
1331 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1333 path_debug (c->path);
1334 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1336 GMC_2s (c), next_peer);
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GMP_2s (next_peer));
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1339 GMC_2s (c), prev_peer);
1340 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GMP_2s (prev_peer));
1342 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1343 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1345 if (GMC_is_origin (c, GNUNET_YES))
1346 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1347 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1349 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1350 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1351 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1352 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1353 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1354 return GNUNET_SYSERR;
1357 GMP_add_connection (next_peer, c);
1358 GMP_add_connection (prev_peer, c);
1365 * Remove the connection from the list of both neighbors.
1367 * @param c Connection.
1370 unregister_neighbors (struct MeshConnection *c)
1372 struct MeshPeer *peer;
1374 peer = get_next_hop (c);
1375 if (GNUNET_OK != GMP_remove_connection (peer, c))
1377 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1378 || MESH_CONNECTION_DESTROYED == c->state);
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1380 if (NULL != c->t) GMT_debug (c->t);
1383 peer = get_prev_hop (c);
1384 if (GNUNET_OK != GMP_remove_connection (peer, c))
1386 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1387 || MESH_CONNECTION_DESTROYED == c->state);
1388 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1389 if (NULL != c->t) GMT_debug (c->t);
1395 * Bind the connection to the peer and the tunnel to that peer.
1397 * If the peer has no tunnel, create one. Update tunnel and connection
1398 * data structres to reflect new status.
1400 * @param c Connection.
1404 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1406 GMP_add_tunnel (peer);
1407 c->t = GMP_get_tunnel (peer);
1408 GMT_add_connection (c->t, c);
1413 * Builds a path from a PeerIdentity array.
1415 * @param peers PeerIdentity array.
1416 * @param size Size of the @c peers array.
1417 * @param own_pos Output parameter: own position in the path.
1419 * @return Fixed and shortened path.
1421 static struct MeshPeerPath *
1422 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1424 unsigned int *own_pos)
1426 struct MeshPeerPath *path;
1427 GNUNET_PEER_Id shortid;
1430 unsigned int offset;
1433 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1434 path = path_new (size);
1437 for (i = 0; i < size; i++)
1439 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1440 i, GNUNET_i2s (&peers[i]));
1441 shortid = GNUNET_PEER_intern (&peers[i]);
1443 /* Check for loops / duplicates */
1444 for (j = 0; j < i - offset; j++)
1446 if (path->peers[j] == shortid)
1448 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1450 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset);
1451 GNUNET_PEER_change_rc (shortid, -1);
1454 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1455 path->peers[i - offset] = shortid;
1456 if (path->peers[i] == myid)
1459 path->length -= offset;
1461 if (path->peers[*own_pos] != myid)
1463 /* create path: self not found in path through self */
1464 GNUNET_break_op (0);
1465 path_destroy (path);
1474 * Log receipt of message on stderr (INFO level).
1476 * @param message Message received.
1477 * @param peer Peer who sent the message.
1478 * @param hash Connection ID.
1481 log_message (const struct GNUNET_MessageHeader *message,
1482 const struct GNUNET_PeerIdentity *peer,
1483 const struct GNUNET_HashCode *hash)
1485 LOG (GNUNET_ERROR_TYPE_INFO, "<- %s on connection %s from %s\n",
1486 GM_m2s (ntohs (message->type)), GNUNET_h2s (hash), GNUNET_i2s (peer));
1489 /******************************************************************************/
1490 /******************************** API ***********************************/
1491 /******************************************************************************/
1494 * Core handler for connection creation.
1496 * @param cls Closure (unused).
1497 * @param peer Sender (neighbor).
1498 * @param message Message.
1500 * @return GNUNET_OK to keep the connection open,
1501 * GNUNET_SYSERR to close it (signal serious error)
1504 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1505 const struct GNUNET_MessageHeader *message)
1507 struct GNUNET_MESH_ConnectionCreate *msg;
1508 struct GNUNET_PeerIdentity *id;
1509 struct GNUNET_HashCode *cid;
1510 struct MeshPeerPath *path;
1511 struct MeshPeer *dest_peer;
1512 struct MeshPeer *orig_peer;
1513 struct MeshConnection *c;
1514 unsigned int own_pos;
1518 size = ntohs (message->size);
1519 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1521 GNUNET_break_op (0);
1525 /* Calculate hops */
1526 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1527 if (size % sizeof (struct GNUNET_PeerIdentity))
1529 GNUNET_break_op (0);
1532 size /= sizeof (struct GNUNET_PeerIdentity);
1535 GNUNET_break_op (0);
1538 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1540 /* Get parameters */
1541 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1543 log_message (message, peer, cid);
1544 id = (struct GNUNET_PeerIdentity *) &msg[1];
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1547 /* Create connection */
1548 c = connection_get (cid);
1551 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1557 GNUNET_break_op (0);
1558 path_destroy (path);
1561 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1562 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1563 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1566 if (path->length - 1 == own_pos)
1568 /* If we are destination, why did the creation fail? */
1572 send_broken2 (cid, &my_full_id,
1573 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1574 path->peers[own_pos - 1]);
1575 path_destroy (path);
1578 GMP_add_path_to_all (path, GNUNET_NO);
1579 connection_reset_timeout (c, GNUNET_YES);
1583 path = path_duplicate (c->path);
1585 if (MESH_CONNECTION_NEW == c->state)
1586 connection_change_state (c, MESH_CONNECTION_SENT);
1588 /* Remember peers */
1589 dest_peer = GMP_get (&id[size - 1]);
1590 orig_peer = GMP_get (&id[0]);
1592 /* Is it a connection to us? */
1593 if (c->own_pos == size - 1)
1595 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1596 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1598 add_to_peer (c, orig_peer);
1599 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1600 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1602 send_connection_ack (c, GNUNET_NO);
1603 if (MESH_CONNECTION_SENT == c->state)
1604 connection_change_state (c, MESH_CONNECTION_ACK);
1608 /* It's for somebody else! Retransmit. */
1609 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1610 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1611 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1612 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1615 path_destroy (path);
1621 * Core handler for path confirmations.
1623 * @param cls closure
1624 * @param message message
1625 * @param peer peer identity this notification is about
1627 * @return GNUNET_OK to keep the connection open,
1628 * GNUNET_SYSERR to close it (signal serious error)
1631 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1632 const struct GNUNET_MessageHeader *message)
1634 struct GNUNET_MESH_ConnectionACK *msg;
1635 struct MeshConnection *c;
1636 struct MeshPeerPath *p;
1637 struct MeshPeer *pi;
1638 enum MeshConnectionState oldstate;
1641 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1642 log_message (message, peer, &msg->cid);
1643 c = connection_get (&msg->cid);
1646 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1648 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1652 if (GNUNET_NO != c->destroy)
1654 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1658 oldstate = c->state;
1659 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1660 pi = GMP_get (peer);
1661 if (get_next_hop (c) == pi)
1663 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1665 if (MESH_CONNECTION_SENT == oldstate)
1666 connection_change_state (c, MESH_CONNECTION_ACK);
1668 else if (get_prev_hop (c) == pi)
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1672 connection_change_state (c, MESH_CONNECTION_READY);
1676 GNUNET_break_op (0);
1680 connection_reset_timeout (c, fwd);
1682 /* Add path to peers? */
1686 GMP_add_path_to_all (p, GNUNET_YES);
1693 /* Message for us as creator? */
1694 if (GMC_is_origin (c, GNUNET_YES))
1696 if (GNUNET_NO != fwd)
1698 GNUNET_break_op (0);
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1703 /* If just created, cancel the short timeout and start a long one */
1704 if (MESH_CONNECTION_SENT == oldstate)
1705 connection_reset_timeout (c, GNUNET_YES);
1707 /* Change connection state */
1708 connection_change_state (c, MESH_CONNECTION_READY);
1709 send_connection_ack (c, GNUNET_YES);
1711 /* Change tunnel state, trigger KX */
1712 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1713 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1718 /* Message for us as destination? */
1719 if (GMC_is_terminal (c, GNUNET_YES))
1721 if (GNUNET_YES != fwd)
1723 GNUNET_break_op (0);
1726 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1728 /* If just created, cancel the short timeout and start a long one */
1729 if (MESH_CONNECTION_ACK == oldstate)
1730 connection_reset_timeout (c, GNUNET_NO);
1732 /* Change tunnel state */
1733 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1734 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1739 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1740 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1746 * Core handler for notifications of broken paths
1748 * @param cls Closure (unused).
1749 * @param id Peer identity of sending neighbor.
1750 * @param message Message.
1752 * @return GNUNET_OK to keep the connection open,
1753 * GNUNET_SYSERR to close it (signal serious error)
1756 GMC_handle_broken (void* cls,
1757 const struct GNUNET_PeerIdentity* id,
1758 const struct GNUNET_MessageHeader* message)
1760 struct GNUNET_MESH_ConnectionBroken *msg;
1761 struct MeshConnection *c;
1764 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1765 log_message (message, id, &msg->cid);
1766 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1767 GNUNET_i2s (&msg->peer1));
1768 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1769 GNUNET_i2s (&msg->peer2));
1770 c = connection_get (&msg->cid);
1773 GNUNET_break_op (0);
1777 fwd = is_fwd (c, id);
1778 if (GMC_is_terminal (c, fwd))
1780 path_invalidate (c->path);
1781 if (0 < c->pending_messages)
1782 c->destroy = GNUNET_YES;
1788 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1789 c->destroy = GNUNET_YES;
1790 connection_cancel_queues (c, !fwd);
1799 * Core handler for tunnel destruction
1801 * @param cls Closure (unused).
1802 * @param peer Peer identity of sending neighbor.
1803 * @param message Message.
1805 * @return GNUNET_OK to keep the connection open,
1806 * GNUNET_SYSERR to close it (signal serious error)
1809 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1810 const struct GNUNET_MessageHeader *message)
1812 struct GNUNET_MESH_ConnectionDestroy *msg;
1813 struct MeshConnection *c;
1816 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1817 log_message (message, peer, &msg->cid);
1818 c = connection_get (&msg->cid);
1821 /* Probably already got the message from another path,
1822 * destroyed the tunnel and retransmitted to children.
1825 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1827 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1830 fwd = is_fwd (c, peer);
1831 if (GNUNET_SYSERR == fwd)
1833 GNUNET_break_op (0); /* FIXME */
1836 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1837 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1838 else if (0 == c->pending_messages)
1840 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1844 c->destroy = GNUNET_YES;
1845 c->state = MESH_CONNECTION_DESTROYED;
1848 GMT_remove_connection (c->t, c);
1856 * Generic handler for mesh network encrypted traffic.
1858 * @param peer Peer identity this notification is about.
1859 * @param msg Encrypted message.
1861 * @return GNUNET_OK to keep the connection open,
1862 * GNUNET_SYSERR to close it (signal serious error)
1865 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1866 const struct GNUNET_MESH_Encrypted *msg)
1868 struct MeshConnection *c;
1869 struct MeshPeer *neighbor;
1870 struct MeshFlowControl *fc;
1871 GNUNET_PEER_Id peer_id;
1877 log_message (&msg->header, peer, &msg->cid);
1880 size = ntohs (msg->header.size);
1882 sizeof (struct GNUNET_MESH_Encrypted) +
1883 sizeof (struct GNUNET_MessageHeader))
1885 GNUNET_break_op (0);
1889 /* Check connection */
1890 c = connection_get (&msg->cid);
1893 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1894 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
1895 GNUNET_h2s (&msg->cid));
1899 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s\n", GMC_2s (c));
1901 /* Check if origin is as expected */
1902 neighbor = get_prev_hop (c);
1903 peer_id = GNUNET_PEER_search (peer);
1904 if (peer_id == GMP_get_short_id (neighbor))
1910 neighbor = get_next_hop (c);
1911 if (peer_id == GMP_get_short_id (neighbor))
1917 /* Unexpected peer sending traffic on a connection. */
1918 GNUNET_break_op (0);
1924 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1925 pid = ntohl (msg->pid);
1926 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1928 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1929 LOG (GNUNET_ERROR_TYPE_DEBUG,
1930 "WARNING Received PID %u, (prev %u), ACK %u\n",
1931 pid, fc->last_pid_recv, fc->last_ack_sent);
1934 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1936 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1937 LOG (GNUNET_ERROR_TYPE_DEBUG,
1938 " Pid %u not expected (%u+), dropping!\n",
1939 pid, fc->last_pid_recv + 1);
1942 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1943 connection_change_state (c, MESH_CONNECTION_READY);
1944 connection_reset_timeout (c, fwd);
1945 fc->last_pid_recv = pid;
1947 /* Is this message for us? */
1948 if (GMC_is_terminal (c, fwd))
1950 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1951 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1955 GNUNET_break (GNUNET_NO != c->destroy);
1958 fc->last_pid_recv = pid;
1959 GMT_handle_encrypted (c->t, msg);
1960 GMC_send_ack (c, fwd, GNUNET_NO);
1964 /* Message not for us: forward to next hop */
1965 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1966 ttl = ntohl (msg->ttl);
1967 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1970 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1971 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1972 GMC_send_ack (c, fwd, GNUNET_NO);
1976 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1977 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1983 * Generic handler for mesh network encrypted traffic.
1985 * @param peer Peer identity this notification is about.
1986 * @param msg Encrypted message.
1988 * @return GNUNET_OK to keep the connection open,
1989 * GNUNET_SYSERR to close it (signal serious error)
1992 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1993 const struct GNUNET_MESH_KX *msg)
1995 struct MeshConnection *c;
1996 struct MeshPeer *neighbor;
1997 GNUNET_PEER_Id peer_id;
2001 log_message (&msg->header, peer, &msg->cid);
2004 size = ntohs (msg->header.size);
2006 sizeof (struct GNUNET_MESH_KX) +
2007 sizeof (struct GNUNET_MessageHeader))
2009 GNUNET_break_op (0);
2013 /* Check connection */
2014 c = connection_get (&msg->cid);
2017 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2018 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx on unknown connection %s\n",
2019 GNUNET_h2s (&msg->cid));
2022 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
2024 /* Check if origin is as expected */
2025 neighbor = get_prev_hop (c);
2026 peer_id = GNUNET_PEER_search (peer);
2027 if (peer_id == GMP_get_short_id (neighbor))
2033 neighbor = get_next_hop (c);
2034 if (peer_id == GMP_get_short_id (neighbor))
2040 /* Unexpected peer sending traffic on a connection. */
2041 GNUNET_break_op (0);
2046 /* Count as connection confirmation. */
2047 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
2049 connection_change_state (c, MESH_CONNECTION_READY);
2052 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
2053 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
2056 connection_reset_timeout (c, fwd);
2058 /* Is this message for us? */
2059 if (GMC_is_terminal (c, fwd))
2061 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2062 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2068 GMT_handle_kx (c->t, &msg[1].header);
2072 /* Message not for us: forward to next hop */
2073 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2074 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2075 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
2082 * Core handler for encrypted mesh network traffic (channel mgmt, data).
2084 * @param cls Closure (unused).
2085 * @param message Message received.
2086 * @param peer Peer who sent the message.
2088 * @return GNUNET_OK to keep the connection open,
2089 * GNUNET_SYSERR to close it (signal serious error)
2092 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2093 const struct GNUNET_MessageHeader *message)
2095 return handle_mesh_encrypted (peer,
2096 (struct GNUNET_MESH_Encrypted *)message);
2101 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2103 * @param cls Closure (unused).
2104 * @param message Message received.
2105 * @param peer Peer who sent the message.
2107 * @return GNUNET_OK to keep the connection open,
2108 * GNUNET_SYSERR to close it (signal serious error)
2111 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2112 const struct GNUNET_MessageHeader *message)
2114 return handle_mesh_kx (peer,
2115 (struct GNUNET_MESH_KX *) message);
2120 * Core handler for mesh network traffic point-to-point acks.
2122 * @param cls closure
2123 * @param message message
2124 * @param peer peer identity this notification is about
2126 * @return GNUNET_OK to keep the connection open,
2127 * GNUNET_SYSERR to close it (signal serious error)
2130 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2131 const struct GNUNET_MessageHeader *message)
2133 struct GNUNET_MESH_ACK *msg;
2134 struct MeshConnection *c;
2135 struct MeshFlowControl *fc;
2140 msg = (struct GNUNET_MESH_ACK *) message;
2141 log_message (message, peer, &msg->cid);
2142 c = connection_get (&msg->cid);
2145 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2150 /* Is this a forward or backward ACK? */
2151 id = GNUNET_PEER_search (peer);
2152 if (GMP_get_short_id (get_next_hop (c)) == id)
2154 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2158 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2160 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2166 GNUNET_break_op (0);
2170 ack = ntohl (msg->ack);
2171 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2172 ack, fc->last_ack_recv);
2173 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
2174 fc->last_ack_recv = ack;
2176 /* Cancel polling if the ACK is big enough. */
2177 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2178 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2180 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2181 GNUNET_SCHEDULER_cancel (fc->poll_task);
2182 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2183 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2186 connection_unlock_queue (c, fwd);
2193 * Core handler for mesh network traffic point-to-point ack polls.
2195 * @param cls closure
2196 * @param message message
2197 * @param peer peer identity this notification is about
2199 * @return GNUNET_OK to keep the connection open,
2200 * GNUNET_SYSERR to close it (signal serious error)
2203 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2204 const struct GNUNET_MessageHeader *message)
2206 struct GNUNET_MESH_Poll *msg;
2207 struct MeshConnection *c;
2208 struct MeshFlowControl *fc;
2213 msg = (struct GNUNET_MESH_Poll *) message;
2214 log_message (message, peer, &msg->cid);
2215 c = connection_get (&msg->cid);
2218 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2220 LOG (GNUNET_ERROR_TYPE_DEBUG,
2221 "WARNING POLL message on unknown connection %s!\n",
2222 GNUNET_h2s (&msg->cid));
2226 /* Is this a forward or backward ACK?
2227 * Note: a poll should never be needed in a loopback case,
2228 * since there is no possiblility of packet loss there, so
2229 * this way of discerining FWD/BCK should not be a problem.
2231 id = GNUNET_PEER_search (peer);
2232 if (GMP_get_short_id (get_next_hop (c)) == id)
2234 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2237 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2239 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2244 GNUNET_break_op (0);
2248 pid = ntohl (msg->pid);
2249 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2250 fc->last_pid_recv = pid;
2251 fwd = fc == &c->bck_fc;
2252 GMC_send_ack (c, fwd, GNUNET_YES);
2259 * Send an ACK on the appropriate connection/channel, depending on
2260 * the direction and the position of the peer.
2262 * @param c Which connection to send the hop-by-hop ACK.
2263 * @param fwd Is this a fwd ACK? (will go dest->root).
2264 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2267 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2269 unsigned int buffer;
2271 LOG (GNUNET_ERROR_TYPE_DEBUG,
2272 "GMC send %s ACK on %s\n",
2273 GM_f2s (fwd), GMC_2s (c));
2281 if (GNUNET_NO != c->destroy)
2283 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2287 /* Get available buffer space */
2288 if (GMC_is_terminal (c, fwd))
2290 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2291 buffer = GMT_get_channels_buffer (c->t);
2295 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2296 buffer = GMC_get_buffer (c, fwd);
2298 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2299 if (0 == buffer && GNUNET_NO == force)
2302 /* Send available buffer space */
2303 if (GMC_is_origin (c, fwd))
2305 GNUNET_assert (NULL != c->t);
2306 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2307 GMT_unchoke_channels (c->t);
2311 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2312 send_ack (c, buffer, fwd, force);
2318 * Initialize the connections subsystem
2320 * @param c Configuration handle.
2323 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2325 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2327 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2330 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2331 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2332 GNUNET_SCHEDULER_shutdown ();
2337 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2340 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2341 "MESH", "MAX_CONNECTIONS", "MISSING");
2342 GNUNET_SCHEDULER_shutdown ();
2347 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2348 &refresh_connection_time))
2350 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2351 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2352 GNUNET_SCHEDULER_shutdown ();
2355 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2356 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2361 * Destroy each connection on shutdown.
2363 * @param cls Closure (unused).
2364 * @param key Current key code (CID, unused).
2365 * @param value Value in the hash map (connection)
2367 * @return #GNUNET_YES, because we should continue to iterate,
2370 shutdown_iterator (void *cls,
2371 const struct GNUNET_HashCode *key,
2374 struct MeshConnection *c = value;
2382 * Shut down the connections subsystem.
2387 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2388 GNUNET_CONTAINER_multihashmap_destroy (connections);
2393 struct MeshConnection *
2394 GMC_new (const struct GNUNET_HashCode *cid,
2395 struct MeshTunnel3 *t,
2396 struct MeshPeerPath *p,
2397 unsigned int own_pos)
2399 struct MeshConnection *c;
2401 c = GNUNET_new (struct MeshConnection);
2403 GNUNET_assert (GNUNET_OK ==
2404 GNUNET_CONTAINER_multihashmap_put (connections,
2406 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2407 fc_init (&c->fwd_fc);
2408 fc_init (&c->bck_fc);
2413 GNUNET_assert (own_pos <= p->length - 1);
2414 c->own_pos = own_pos;
2417 if (GNUNET_OK != register_neighbors (c))
2421 path_invalidate (c->path);
2434 GMC_destroy (struct MeshConnection *c)
2442 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2443 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2446 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2447 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2448 &c->fwd_fc, &c->bck_fc);
2449 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2450 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2452 /* Cancel all traffic */
2453 if (NULL != c->path)
2455 connection_cancel_queues (c, GNUNET_YES);
2456 connection_cancel_queues (c, GNUNET_NO);
2457 unregister_neighbors (c);
2460 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2461 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2463 /* Cancel maintainance task (keepalive/timeout) */
2464 if (NULL != c->fwd_fc.poll_msg)
2466 GMC_cancel (c->fwd_fc.poll_msg);
2467 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2469 if (NULL != c->bck_fc.poll_msg)
2471 GMC_cancel (c->bck_fc.poll_msg);
2472 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2475 /* Delete from tunnel */
2477 GMT_remove_connection (c->t, c);
2479 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2480 path_destroy (c->path);
2481 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2482 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2483 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2484 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2485 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2487 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2488 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2490 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2492 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2493 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2496 GNUNET_break (GNUNET_YES ==
2497 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2499 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2504 * Get the connection ID.
2506 * @param c Connection to get the ID from.
2508 * @return ID of the connection.
2510 const struct GNUNET_HashCode *
2511 GMC_get_id (const struct MeshConnection *c)
2518 * Get the connection path.
2520 * @param c Connection to get the path from.
2522 * @return path used by the connection.
2524 const struct MeshPeerPath *
2525 GMC_get_path (const struct MeshConnection *c)
2527 if (GNUNET_NO == c->destroy)
2534 * Get the connection state.
2536 * @param c Connection to get the state from.
2538 * @return state of the connection.
2540 enum MeshConnectionState
2541 GMC_get_state (const struct MeshConnection *c)
2547 * Get the connection tunnel.
2549 * @param c Connection to get the tunnel from.
2551 * @return tunnel of the connection.
2553 struct MeshTunnel3 *
2554 GMC_get_tunnel (const struct MeshConnection *c)
2561 * Get free buffer space in a connection.
2563 * @param c Connection.
2564 * @param fwd Is query about FWD traffic?
2566 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2569 GMC_get_buffer (struct MeshConnection *c, int fwd)
2571 struct MeshFlowControl *fc;
2573 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2575 return (fc->queue_max - fc->queue_n);
2579 * Get how many messages have we allowed to send to us from a direction.
2581 * @param c Connection.
2582 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2584 * @return last_ack_sent - last_pid_recv
2587 GMC_get_allowed (struct MeshConnection *c, int fwd)
2589 struct MeshFlowControl *fc;
2591 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2592 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2596 return (fc->last_ack_sent - fc->last_pid_recv);
2600 * Get messages queued in a connection.
2602 * @param c Connection.
2603 * @param fwd Is query about FWD traffic?
2605 * @return Number of messages queued.
2608 GMC_get_qn (struct MeshConnection *c, int fwd)
2610 struct MeshFlowControl *fc;
2612 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2619 * Allow the connection to advertise a buffer of the given size.
2621 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2622 * allowing up to last_pid_recv + buffer.
2624 * @param c Connection.
2625 * @param buffer How many more messages the connection can accept.
2626 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2629 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2631 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2632 GMC_2s (c), buffer, GM_f2s (fwd));
2633 send_ack (c, buffer, fwd, GNUNET_NO);
2638 * Notify other peers on a connection of a broken link. Mark connections
2639 * to destroy after all traffic has been sent.
2641 * @param c Connection on which there has been a disconnection.
2642 * @param peer Peer that disconnected.
2645 GMC_notify_broken (struct MeshConnection *c,
2646 struct MeshPeer *peer)
2650 LOG (GNUNET_ERROR_TYPE_DEBUG,
2651 " notify broken on %s due to %s disconnect\n",
2652 GMC_2s (c), GMP_2s (peer));
2654 fwd = peer == get_prev_hop (c);
2656 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2658 /* Local shutdown, no one to notify about this. */
2662 if (GNUNET_NO == c->destroy)
2663 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2665 /* Connection will have at least one pending message
2666 * (the one we just scheduled), so no point in checking whether to
2667 * destroy immediately. */
2668 c->destroy = GNUNET_YES;
2669 c->state = MESH_CONNECTION_DESTROYED;
2672 * Cancel all queues, if no message is left, connection will be destroyed.
2674 connection_cancel_queues (c, !fwd);
2681 * Is this peer the first one on the connection?
2683 * @param c Connection.
2684 * @param fwd Is this about fwd traffic?
2686 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2689 GMC_is_origin (struct MeshConnection *c, int fwd)
2691 if (!fwd && c->path->length - 1 == c->own_pos )
2693 if (fwd && 0 == c->own_pos)
2700 * Is this peer the last one on the connection?
2702 * @param c Connection.
2703 * @param fwd Is this about fwd traffic?
2704 * Note that the ROOT is the terminal for BCK traffic!
2706 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2709 GMC_is_terminal (struct MeshConnection *c, int fwd)
2711 return GMC_is_origin (c, !fwd);
2716 * See if we are allowed to send by the next hop in the given direction.
2718 * @param c Connection.
2719 * @param fwd Is this about fwd traffic?
2721 * @return #GNUNET_YES in case it's OK to send.
2724 GMC_is_sendable (struct MeshConnection *c, int fwd)
2726 struct MeshFlowControl *fc;
2733 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2734 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2740 * Sends an already built message on a connection, properly registering
2741 * all used resources.
2743 * @param message Message to send. Function makes a copy of it.
2744 * If message is not hop-by-hop, decrements TTL of copy.
2745 * @param c Connection on which this message is transmitted.
2746 * @param fwd Is this a fwd message?
2747 * @param force Force the connection to accept the message (buffer overfill).
2748 * @param cont Continuation called once message is sent. Can be NULL.
2749 * @param cont_cls Closure for @c cont.
2751 * @return Handle to cancel the message before it's sent.
2752 * NULL on error or if @c cont is NULL.
2753 * Invalid on @c cont call.
2755 struct MeshConnectionQueue *
2756 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2757 struct MeshConnection *c, int fwd, int force,
2758 GMC_sent cont, void *cont_cls)
2760 struct MeshFlowControl *fc;
2761 struct MeshConnectionQueue *q;
2767 size = ntohs (message->size);
2768 data = GNUNET_malloc (size);
2769 memcpy (data, message, size);
2770 type = ntohs (message->type);
2771 LOG (GNUNET_ERROR_TYPE_INFO, "-> %s on connection %s (%u bytes)\n",
2772 GM_m2s (type), GMC_2s (c), size);
2774 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2775 droppable = GNUNET_NO == force;
2778 struct GNUNET_MESH_Encrypted *emsg;
2779 struct GNUNET_MESH_KX *kmsg;
2780 struct GNUNET_MESH_ACK *amsg;
2781 struct GNUNET_MESH_Poll *pmsg;
2782 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2783 struct GNUNET_MESH_ConnectionBroken *bmsg;
2786 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2787 emsg = (struct GNUNET_MESH_Encrypted *) data;
2788 ttl = ntohl (emsg->ttl);
2791 GNUNET_break_op (0);
2796 emsg->ttl = htonl (ttl - 1);
2797 emsg->pid = htonl (fc->next_pid++);
2798 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2799 if (GNUNET_YES == droppable)
2802 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2803 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2804 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2808 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2810 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2812 GMC_start_poll (c, fwd);
2816 case GNUNET_MESSAGE_TYPE_MESH_KX:
2817 kmsg = (struct GNUNET_MESH_KX *) data;
2821 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2822 amsg = (struct GNUNET_MESH_ACK *) data;
2824 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2825 droppable = GNUNET_NO;
2828 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2829 pmsg = (struct GNUNET_MESH_Poll *) data;
2831 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2832 droppable = GNUNET_NO;
2835 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2836 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2840 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2841 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2845 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2848 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2849 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2858 if (fc->queue_n > fc->queue_max && droppable)
2860 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2863 LOG (GNUNET_ERROR_TYPE_DEBUG,
2864 "queue full: %u/%u\n",
2865 fc->queue_n, fc->queue_max);
2866 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2872 return NULL; /* Drop this message */
2875 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2876 c->pending_messages++;
2878 q = GNUNET_new (struct MeshConnectionQueue);
2879 q->forced = !droppable;
2880 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2884 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2890 q->cont_cls = cont_cls;
2896 * Cancel a previously sent message while it's in the queue.
2898 * ONLY can be called before the continuation given to the send function
2899 * is called. Once the continuation is called, the message is no longer in the
2902 * @param q Handle to the queue.
2905 GMC_cancel (struct MeshConnectionQueue *q)
2907 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2909 /* queue destroy calls message_sent, which calls q->cont and frees q */
2910 GMP_queue_destroy (q->q, GNUNET_YES);
2915 * Sends a CREATE CONNECTION message for a path to a peer.
2916 * Changes the connection and tunnel states if necessary.
2918 * @param connection Connection to create.
2921 GMC_send_create (struct MeshConnection *connection)
2923 enum MeshTunnel3CState state;
2926 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2927 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2929 LOG (GNUNET_ERROR_TYPE_INFO, "=> %s on connection %s (%u bytes)\n",
2930 GM_m2s (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE),
2931 GMC_2s (connection), size);
2932 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2933 connection, connection->pending_messages);
2934 connection->pending_messages++;
2936 connection->maintenance_q =
2937 GMP_queue_add (get_next_hop (connection), NULL,
2938 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2939 size, connection, GNUNET_YES, &message_sent, NULL);
2941 state = GMT_get_cstate (connection->t);
2942 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2943 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2944 if (MESH_CONNECTION_NEW == connection->state)
2945 connection_change_state (connection, MESH_CONNECTION_SENT);
2950 * Send a message to all peers in this connection that the connection
2951 * is no longer valid.
2953 * If some peer should not receive the message, it should be zero'ed out
2954 * before calling this function.
2956 * @param c The connection whose peers to notify.
2959 GMC_send_destroy (struct MeshConnection *c)
2961 struct GNUNET_MESH_ConnectionDestroy msg;
2963 if (GNUNET_YES == c->destroy)
2966 msg.header.size = htons (sizeof (msg));
2967 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2969 LOG (GNUNET_ERROR_TYPE_DEBUG,
2970 " sending connection destroy for connection %s\n",
2973 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2974 GMC_send_prebuilt_message (&msg.header, c,
2975 GNUNET_YES, GNUNET_YES, NULL, NULL);
2976 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2977 GMC_send_prebuilt_message (&msg.header, c,
2978 GNUNET_NO, GNUNET_YES, NULL, NULL);
2979 c->destroy = GNUNET_YES;
2980 c->state = MESH_CONNECTION_DESTROYED;
2985 * @brief Start a polling timer for the connection.
2987 * When a neighbor does not accept more traffic on the connection it could be
2988 * caused by a simple congestion or by a lost ACK. Polling enables to check
2989 * for the lastest ACK status for a connection.
2991 * @param c Connection.
2992 * @param fwd Should we poll in the FWD direction?
2995 GMC_start_poll (struct MeshConnection *c, int fwd)
2997 struct MeshFlowControl *fc;
2999 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3000 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3002 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3004 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3005 fc->poll_task, fc->poll_msg);
3008 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3009 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3016 * @brief Stop polling a connection for ACKs.
3018 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3020 * @param c Connection.
3021 * @param fwd Should we stop the poll in the FWD direction?
3024 GMC_stop_poll (struct MeshConnection *c, int fwd)
3026 struct MeshFlowControl *fc;
3028 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3029 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3031 GNUNET_SCHEDULER_cancel (fc->poll_task);
3032 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3037 * Get a (static) string for a connection.
3039 * @param c Connection.
3042 GMC_2s (const struct MeshConnection *c)
3049 static char buf[128];
3051 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
3054 return GNUNET_h2s (&c->id);