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);
335 * Get string description for tunnel state.
337 * @param s Tunnel state.
339 * @return String representation.
342 GMC_state2s (enum MeshConnectionState s)
346 case MESH_CONNECTION_NEW:
347 return "MESH_CONNECTION_NEW";
348 case MESH_CONNECTION_SENT:
349 return "MESH_CONNECTION_SENT";
350 case MESH_CONNECTION_ACK:
351 return "MESH_CONNECTION_ACK";
352 case MESH_CONNECTION_READY:
353 return "MESH_CONNECTION_READY";
354 case MESH_CONNECTION_DESTROYED:
355 return "MESH_CONNECTION_DESTROYED";
357 return "MESH_CONNECTION_STATE_ERROR";
363 * Initialize a Flow Control structure to the initial state.
365 * @param fc Flow Control structure to initialize.
368 fc_init (struct MeshFlowControl *fc)
371 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
372 fc->last_pid_recv = (uint32_t) -1;
373 fc->last_ack_sent = (uint32_t) 0;
374 fc->last_ack_recv = (uint32_t) 0;
375 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
376 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
378 fc->queue_max = (max_msgs_queue / max_connections) + 1;
385 * @param cid Connection ID.
387 static struct MeshConnection *
388 connection_get (const struct GNUNET_HashCode *cid)
390 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
395 connection_change_state (struct MeshConnection* c,
396 enum MeshConnectionState state)
398 LOG (GNUNET_ERROR_TYPE_DEBUG,
399 "Connection %s state was %s\n",
400 GMC_2s (c), GMC_state2s (c->state));
401 if (MESH_CONNECTION_DESTROYED == c->state)
403 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
406 LOG (GNUNET_ERROR_TYPE_DEBUG,
407 "Connection %s state is now %s\n",
408 GMC_2s (c), GMC_state2s (state));
410 if (MESH_CONNECTION_READY == state)
416 * Callback called when a queued ACK message is sent.
418 * @param cls Closure (FC).
419 * @param c Connection this message was on.
420 * @param q Queue handler this call invalidates.
421 * @param type Type of message sent.
422 * @param fwd Was this a FWD going message?
423 * @param size Size of the message.
427 struct MeshConnection *c,
428 struct MeshConnectionQueue *q,
429 uint16_t type, int fwd, size_t size)
431 struct MeshFlowControl *fc = cls;
438 * Send an ACK on the connection, informing the predecessor about
439 * the available buffer space. Should not be called in case the peer
440 * is origin (no predecessor) in the @c fwd direction.
442 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
443 * the ACK itself goes "back" (dest->root).
445 * @param c Connection on which to send the ACK.
446 * @param buffer How much space free to advertise?
447 * @param fwd Is this FWD ACK? (Going dest -> root)
448 * @param force Don't optimize out.
451 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
453 struct MeshFlowControl *next_fc;
454 struct MeshFlowControl *prev_fc;
455 struct GNUNET_MESH_ACK msg;
459 /* If origin, there is no connection to send ACKs. Wrong function! */
460 if (GMC_is_origin (c, fwd))
462 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
463 GMC_2s (c), GM_f2s (fwd));
468 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
469 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
471 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
472 GM_f2s (fwd), GMC_2s (c));
474 /* Check if we need to transmit the ACK. */
475 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
476 if (3 < delta && buffer < delta && GNUNET_NO == force)
478 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
479 LOG (GNUNET_ERROR_TYPE_DEBUG,
480 " last pid recv: %u, last ack sent: %u\n",
481 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
485 /* Ok, ACK might be necessary, what PID to ACK? */
486 ack = prev_fc->last_pid_recv + buffer;
487 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
488 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 " last pid %u, last ack %u, qmax %u, q %u\n",
490 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
491 next_fc->queue_max, next_fc->queue_n);
492 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
494 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
498 /* Check if message is already in queue */
499 if (NULL != prev_fc->ack_msg)
501 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
503 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
504 GMC_cancel (prev_fc->ack_msg);
505 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
509 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
514 prev_fc->last_ack_sent = ack;
516 /* Build ACK message and send on connection */
517 msg.header.size = htons (sizeof (msg));
518 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
519 msg.ack = htonl (ack);
522 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
529 * Callback called when a queued message is sent.
531 * Calculates the average time and connection packet tracking.
533 * @param cls Closure (ConnectionQueue Handle).
534 * @param c Connection this message was on.
535 * @param type Type of message sent.
536 * @param fwd Was this a FWD going message?
537 * @param size Size of the message.
538 * @param wait Time spent waiting for core (only the time for THIS message)
541 message_sent (void *cls,
542 struct MeshConnection *c, uint16_t type,
543 int fwd, size_t size,
544 struct GNUNET_TIME_Relative wait)
546 struct MeshConnectionPerformance *p;
547 struct MeshFlowControl *fc;
548 struct MeshConnectionQueue *q = cls;
552 fc = fwd ? &c->fwd_fc : &c->bck_fc;
553 LOG (GNUNET_ERROR_TYPE_DEBUG,
557 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
563 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
564 q->cont (q->cont_cls, c, q, type, fwd, size);
568 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
570 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
577 c->pending_messages--;
578 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
580 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
584 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
587 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
589 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
590 if (GNUNET_NO == forced)
593 LOG (GNUNET_ERROR_TYPE_DEBUG,
594 "! accounting pid %u\n",
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "! forced, Q_N not accounting pid %u\n",
603 GMC_send_ack (c, fwd, GNUNET_NO);
606 case GNUNET_MESSAGE_TYPE_MESH_POLL:
610 case GNUNET_MESSAGE_TYPE_MESH_ACK:
617 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
620 return; /* Only endpoints are interested in timing. */
623 usecsperbyte = ((double) wait.rel_value_us) / size;
624 if (p->size == AVG_MSGS)
626 /* Array is full. Substract oldest value, add new one and store. */
627 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
628 p->usecsperbyte[p->idx] = usecsperbyte;
629 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
633 /* Array not yet full. Add current value to avg and store. */
634 p->usecsperbyte[p->idx] = usecsperbyte;
636 p->avg += p->usecsperbyte[p->idx];
640 p->idx = (p->idx + 1) % AVG_MSGS;
645 * Get the previous hop in a connection
647 * @param c Connection.
649 * @return Previous peer in the connection.
651 static struct MeshPeer *
652 get_prev_hop (const struct MeshConnection *c)
656 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
657 GMC_2s (c), c->own_pos, c->path->length);
658 if (0 == c->own_pos || c->path->length < 2)
660 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is zero\n");
661 id = c->path->peers[0];
665 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is NOT zero\n");
666 id = c->path->peers[c->own_pos - 1];
669 LOG (GNUNET_ERROR_TYPE_DEBUG, " id: %u\n", id);
670 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s\n", GNUNET_i2s (GNUNET_PEER_resolve2 (id)));
671 return GMP_get_short (id);
676 * Get the next hop in a connection
678 * @param c Connection.
680 * @return Next peer in the connection.
682 static struct MeshPeer *
683 get_next_hop (const struct MeshConnection *c)
687 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
688 GMC_2s (c), c->own_pos, c->path->length);
689 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
691 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is end\n");
692 id = c->path->peers[c->path->length - 1];
696 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is NOT end\n");
697 id = c->path->peers[c->own_pos + 1];
700 LOG (GNUNET_ERROR_TYPE_DEBUG, " id: %u\n", id);
701 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s\n", GNUNET_i2s (GNUNET_PEER_resolve2 (id)));
703 return GMP_get_short (id);
708 * Get the hop in a connection.
710 * @param c Connection.
711 * @param fwd Next hop?
713 * @return Next peer in the connection.
715 static struct MeshPeer *
716 get_hop (struct MeshConnection *c, int fwd)
719 return get_next_hop (c);
720 return get_prev_hop (c);
725 * Is traffic coming from this sender 'FWD' traffic?
727 * @param c Connection to check.
728 * @param sender Peer identity of neighbor.
730 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
731 * the traffic is 'FWD'.
732 * #GNUNET_NO for BCK.
733 * #GNUNET_SYSERR for errors.
736 is_fwd (const struct MeshConnection *c,
737 const struct GNUNET_PeerIdentity *sender)
741 id = GNUNET_PEER_search (sender);
742 if (GMP_get_short_id (get_prev_hop (c)) == id)
745 if (GMP_get_short_id (get_next_hop (c)) == id)
749 return GNUNET_SYSERR;
754 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
755 * or a first CONNECTION_ACK directed to us.
757 * @param connection Connection to confirm.
758 * @param fwd Should we send it FWD? (root->dest)
759 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
762 send_connection_ack (struct MeshConnection *connection, int fwd)
764 struct MeshTunnel3 *t;
767 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
769 GMP_queue_add (get_hop (connection, fwd), NULL,
770 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
771 sizeof (struct GNUNET_MESH_ConnectionACK),
772 connection, fwd, &message_sent, NULL);
773 connection->pending_messages++;
774 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
775 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
776 if (MESH_CONNECTION_READY != connection->state)
777 connection_change_state (connection, MESH_CONNECTION_SENT);
782 * Send a notification that a connection is broken.
784 * @param c Connection that is broken.
785 * @param id1 Peer that has disconnected.
786 * @param id2 Peer that has disconnected.
787 * @param fwd Direction towards which to send it.
790 send_broken (struct MeshConnection *c,
791 const struct GNUNET_PeerIdentity *id1,
792 const struct GNUNET_PeerIdentity *id2,
795 struct GNUNET_MESH_ConnectionBroken msg;
797 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
798 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
802 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
807 * Send a notification that a connection is broken, when a connection
808 * isn't even created.
810 * @param connection_id Connection ID.
811 * @param id1 Peer that has disconnected.
812 * @param id2 Peer that has disconnected.
813 * @param peer Peer to notify (neighbor who sent the connection).
816 send_broken2 (struct GNUNET_HashCode *connection_id,
817 const struct GNUNET_PeerIdentity *id1,
818 const struct GNUNET_PeerIdentity *id2,
819 GNUNET_PEER_Id peer_id)
821 struct GNUNET_MESH_ConnectionBroken *msg;
822 struct MeshPeer *neighbor;
824 msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
825 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
826 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
827 msg->cid = *connection_id;
830 neighbor = GMP_get_short (peer_id);
831 GMP_queue_add (neighbor, msg,
832 GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED,
833 sizeof (struct GNUNET_MESH_ConnectionBroken),
834 NULL, GNUNET_SYSERR, /* connection, fwd */
835 NULL, NULL); /* continuation */
840 * Send keepalive packets for a connection.
842 * @param c Connection to keep alive..
843 * @param fwd Is this a FWD keepalive? (owner -> dest).
846 connection_keepalive (struct MeshConnection *c, int fwd)
848 struct GNUNET_MESH_ConnectionKeepAlive *msg;
849 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
852 LOG (GNUNET_ERROR_TYPE_DEBUG,
853 "sending %s keepalive for connection %s]\n",
854 GM_f2s (fwd), GMC_2s (c));
856 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
857 msg->header.size = htons (size);
858 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
860 msg->reserved = htonl (0);
862 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
867 * Send CONNECTION_{CREATE/ACK} packets for a connection.
869 * @param c Connection for which to send the message.
870 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
873 connection_recreate (struct MeshConnection *c, int fwd)
875 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
879 send_connection_ack (c, GNUNET_NO);
884 * Generic connection timer management.
885 * Depending on the role of the peer in the connection will send the
886 * appropriate message (build or keepalive)
888 * @param c Conncetion to maintain.
892 connection_maintain (struct MeshConnection *c, int fwd)
894 if (GNUNET_NO != c->destroy)
897 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
899 /* TODO DHT GET with RO_BART */
904 case MESH_CONNECTION_NEW:
907 case MESH_CONNECTION_SENT:
908 connection_recreate (c, fwd);
910 case MESH_CONNECTION_READY:
911 connection_keepalive (c, fwd);
920 * Keep the connection alive in the FWD direction.
922 * Call connection_maintain do to the work and schedule the next execution,
923 * taking in consideration the connection state and number of retries.
925 * @param cls Closure (connection to keepalive).
926 * @param tc TaskContext.
929 connection_fwd_keepalive (void *cls,
930 const struct GNUNET_SCHEDULER_TaskContext *tc)
932 struct MeshConnection *c = cls;
933 struct GNUNET_TIME_Relative delay;
935 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
936 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
939 connection_maintain (c, GNUNET_YES);
940 if (MESH_CONNECTION_READY == c->state)
942 delay = refresh_connection_time;
946 if (1 > c->create_retry)
948 delay = GNUNET_TIME_relative_multiply (create_connection_time,
950 if (c->create_retry < 64)
951 c->create_retry *= 2;
953 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
954 &connection_fwd_keepalive,
960 * Keep the connection alive in the BCK direction.
962 * Call connection_maintain do to the work and schedule the next execution,
963 * taking in consideration the connection state and number of retries.
965 * TODO refactor and merge with connection_fwd_keepalive.
967 * @param cls Closure (connection to keepalive).
968 * @param tc TaskContext.
971 connection_bck_keepalive (void *cls,
972 const struct GNUNET_SCHEDULER_TaskContext *tc)
974 struct MeshConnection *c = cls;
975 struct GNUNET_TIME_Relative delay;
977 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
978 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
981 connection_maintain (c, GNUNET_NO);
982 if (MESH_CONNECTION_READY == c->state)
984 delay = refresh_connection_time;
988 if (1 > c->create_retry)
990 delay = GNUNET_TIME_relative_multiply (create_connection_time,
992 if (c->create_retry < 64)
993 c->create_retry *= 2;
995 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
996 &connection_bck_keepalive,
1002 * @brief Re-initiate traffic on this connection if necessary.
1004 * Check if there is traffic queued towards this peer
1005 * and the core transmit handle is NULL (traffic was stalled).
1006 * If so, call core tmt rdy.
1008 * @param c Connection on which initiate traffic.
1009 * @param fwd Is this about fwd traffic?
1012 connection_unlock_queue (struct MeshConnection *c, int fwd)
1014 struct MeshPeer *peer;
1016 LOG (GNUNET_ERROR_TYPE_DEBUG,
1017 "connection_unlock_queue %s on %s\n",
1018 GM_f2s (fwd), GMC_2s (c));
1020 if (GMC_is_terminal (c, fwd))
1022 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1026 peer = get_hop (c, fwd);
1027 GMP_queue_unlock (peer, c);
1032 * Cancel all transmissions that belong to a certain connection.
1034 * If the connection is scheduled for destruction and no more messages are left,
1035 * the connection will be destroyed by the continuation call.
1037 * @param c Connection which to cancel. Might be destroyed during this call.
1038 * @param fwd Cancel fwd traffic?
1041 connection_cancel_queues (struct MeshConnection *c, int fwd)
1043 struct MeshFlowControl *fc;
1044 struct MeshPeer *peer;
1046 LOG (GNUNET_ERROR_TYPE_DEBUG,
1047 " *** Cancel %s queues for connection %s\n",
1048 GM_f2s (fwd), GMC_2s (c));
1055 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1056 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1058 GNUNET_SCHEDULER_cancel (fc->poll_task);
1059 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1062 peer = get_hop (c, fwd);
1063 GMP_queue_cancel (peer, c);
1068 * Function called if a connection has been stalled for a while,
1069 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1071 * @param cls Closure (poll ctx).
1072 * @param tc TaskContext.
1075 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1079 * Callback called when a queued POLL message is sent.
1081 * @param cls Closure (FC).
1082 * @param c Connection this message was on.
1083 * @param q Queue handler this call invalidates.
1084 * @param type Type of message sent.
1085 * @param fwd Was this a FWD going message?
1086 * @param size Size of the message.
1089 poll_sent (void *cls,
1090 struct MeshConnection *c,
1091 struct MeshConnectionQueue *q,
1092 uint16_t type, int fwd, size_t size)
1094 struct MeshFlowControl *fc = cls;
1096 if (2 == c->destroy)
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1101 LOG (GNUNET_ERROR_TYPE_DEBUG,
1102 " *** POLL sent for , scheduling new one!\n");
1103 fc->poll_msg = NULL;
1104 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1105 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1106 &connection_poll, fc);
1107 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1112 * Function called if a connection has been stalled for a while,
1113 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1115 * @param cls Closure (poll ctx).
1116 * @param tc TaskContext.
1119 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1121 struct MeshFlowControl *fc = cls;
1122 struct GNUNET_MESH_Poll msg;
1123 struct MeshConnection *c;
1125 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1126 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1132 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1133 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1135 fc == &c->fwd_fc ? "FWD" : "BCK");
1137 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1138 msg.header.size = htons (sizeof (msg));
1139 msg.pid = htonl (fc->last_pid_sent);
1140 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1141 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1142 fc == &c->fwd_fc, GNUNET_YES,
1148 * Timeout function due to lack of keepalive/traffic from the owner.
1149 * Destroys connection if called.
1151 * @param cls Closure (connection to destroy).
1152 * @param tc TaskContext.
1155 connection_fwd_timeout (void *cls,
1156 const struct GNUNET_SCHEDULER_TaskContext *tc)
1158 struct MeshConnection *c = cls;
1160 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1161 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1164 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1166 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1177 * Timeout function due to lack of keepalive/traffic from the destination.
1178 * Destroys connection if called.
1180 * @param cls Closure (connection to destroy).
1181 * @param tc TaskContext
1184 connection_bck_timeout (void *cls,
1185 const struct GNUNET_SCHEDULER_TaskContext *tc)
1187 struct MeshConnection *c = cls;
1189 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1190 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1193 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1196 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1207 * Resets the connection timeout task, some other message has done the
1209 * - For the first peer on the direction this means to send
1210 * a keepalive or a path confirmation message (either create or ACK).
1211 * - For all other peers, this means to destroy the connection,
1212 * due to lack of activity.
1213 * Starts the timeout if no timeout was running (connection just created).
1215 * @param c Connection whose timeout to reset.
1216 * @param fwd Is this forward?
1218 * TODO use heap to improve efficiency of scheduler.
1221 connection_reset_timeout (struct MeshConnection *c, int fwd)
1223 GNUNET_SCHEDULER_TaskIdentifier *ti;
1224 GNUNET_SCHEDULER_Task f;
1226 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1228 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1230 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1231 GNUNET_SCHEDULER_cancel (*ti);
1233 if (GMC_is_origin (c, fwd)) /* Startpoint */
1235 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1236 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1238 else /* Relay, endpoint. */
1240 struct GNUNET_TIME_Relative delay;
1242 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1243 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1244 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1250 * Add the connection to the list of both neighbors.
1252 * @param c Connection.
1254 * @return #GNUNET_OK if everything went fine
1255 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1258 register_neighbors (struct MeshConnection *c)
1260 struct MeshPeer *next_peer;
1261 struct MeshPeer *prev_peer;
1263 next_peer = get_next_hop (c);
1264 prev_peer = get_prev_hop (c);
1266 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1268 path_debug (c->path);
1269 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1270 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1271 GMC_2s (c), next_peer);
1272 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GMP_2s (next_peer));
1273 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1274 GMC_2s (c), prev_peer);
1275 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GMP_2s (prev_peer));
1277 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1278 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1280 if (GMC_is_origin (c, GNUNET_YES))
1281 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1282 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1284 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1285 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1286 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1287 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1288 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1289 return GNUNET_SYSERR;
1292 GMP_add_connection (next_peer, c);
1293 GMP_add_connection (prev_peer, c);
1300 * Remove the connection from the list of both neighbors.
1302 * @param c Connection.
1305 unregister_neighbors (struct MeshConnection *c)
1307 struct MeshPeer *peer;
1309 peer = get_next_hop (c);
1310 if (GNUNET_OK != GMP_remove_connection (peer, c))
1312 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1313 || MESH_CONNECTION_DESTROYED == c->state);
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1315 if (NULL != c->t) GMT_debug (c->t);
1318 peer = get_prev_hop (c);
1319 if (GNUNET_OK != GMP_remove_connection (peer, c))
1321 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1322 || MESH_CONNECTION_DESTROYED == c->state);
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1324 if (NULL != c->t) GMT_debug (c->t);
1330 * Bind the connection to the peer and the tunnel to that peer.
1332 * If the peer has no tunnel, create one. Update tunnel and connection
1333 * data structres to reflect new status.
1335 * @param c Connection.
1339 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1341 GMP_add_tunnel (peer);
1342 c->t = GMP_get_tunnel (peer);
1343 GMT_add_connection (c->t, c);
1348 * Builds a path from a PeerIdentity array.
1350 * @param peers PeerIdentity array.
1351 * @param size Size of the @c peers array.
1352 * @param own_pos Output parameter: own position in the path.
1354 * @return Fixed and shortened path.
1356 static struct MeshPeerPath *
1357 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1359 unsigned int *own_pos)
1361 struct MeshPeerPath *path;
1362 GNUNET_PEER_Id shortid;
1365 unsigned int offset;
1368 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1369 path = path_new (size);
1372 for (i = 0; i < size; i++)
1374 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1375 i, GNUNET_i2s (&peers[i]));
1376 shortid = GNUNET_PEER_intern (&peers[i]);
1378 /* Check for loops / duplicates */
1379 for (j = 0; j < i - offset; j++)
1381 if (path->peers[j] == shortid)
1383 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1385 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset);
1386 GNUNET_PEER_change_rc (shortid, -1);
1389 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1390 path->peers[i - offset] = shortid;
1391 if (path->peers[i] == myid)
1394 path->length -= offset;
1396 if (path->peers[*own_pos] != myid)
1398 /* create path: self not found in path through self */
1399 GNUNET_break_op (0);
1400 path_destroy (path);
1407 /******************************************************************************/
1408 /******************************** API ***********************************/
1409 /******************************************************************************/
1412 * Core handler for connection creation.
1414 * @param cls Closure (unused).
1415 * @param peer Sender (neighbor).
1416 * @param message Message.
1418 * @return GNUNET_OK to keep the connection open,
1419 * GNUNET_SYSERR to close it (signal serious error)
1422 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1423 const struct GNUNET_MessageHeader *message)
1425 struct GNUNET_MESH_ConnectionCreate *msg;
1426 struct GNUNET_PeerIdentity *id;
1427 struct GNUNET_HashCode *cid;
1428 struct MeshPeerPath *path;
1429 struct MeshPeer *dest_peer;
1430 struct MeshPeer *orig_peer;
1431 struct MeshConnection *c;
1432 unsigned int own_pos;
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1439 size = ntohs (message->size);
1440 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1442 GNUNET_break_op (0);
1446 /* Calculate hops */
1447 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1448 if (size % sizeof (struct GNUNET_PeerIdentity))
1450 GNUNET_break_op (0);
1453 size /= sizeof (struct GNUNET_PeerIdentity);
1456 GNUNET_break_op (0);
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1461 /* Get parameters */
1462 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1464 id = (struct GNUNET_PeerIdentity *) &msg[1];
1465 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s (%s->).\n",
1466 GNUNET_h2s (cid), GNUNET_i2s (id));
1468 /* Create connection */
1469 c = connection_get (cid);
1472 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1478 GNUNET_break_op (0);
1479 path_destroy (path);
1482 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1483 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1484 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1487 if (path->length - 1 == own_pos)
1489 /* If we are destination, why did the creation fail? */
1493 send_broken2 (cid, &my_full_id,
1494 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1495 path->peers[own_pos - 1]);
1496 path_destroy (path);
1499 GMP_add_path_to_all (path, GNUNET_NO);
1500 connection_reset_timeout (c, GNUNET_YES);
1504 path = path_duplicate (c->path);
1506 if (MESH_CONNECTION_NEW == c->state)
1507 connection_change_state (c, MESH_CONNECTION_SENT);
1509 /* Remember peers */
1510 dest_peer = GMP_get (&id[size - 1]);
1511 orig_peer = GMP_get (&id[0]);
1513 /* Is it a connection to us? */
1514 if (c->own_pos == size - 1)
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1517 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1519 add_to_peer (c, orig_peer);
1520 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1521 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1523 send_connection_ack (c, GNUNET_NO);
1524 if (MESH_CONNECTION_SENT == c->state)
1525 connection_change_state (c, MESH_CONNECTION_ACK);
1527 /* Keep tunnel alive in direction dest->owner*/
1528 if (GNUNET_SCHEDULER_NO_TASK == c->bck_maintenance_task)
1530 c->bck_maintenance_task =
1531 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1532 &connection_bck_keepalive, c);
1537 /* It's for somebody else! Retransmit. */
1538 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1539 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1540 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1541 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1544 path_destroy (path);
1550 * Core handler for path confirmations.
1552 * @param cls closure
1553 * @param message message
1554 * @param peer peer identity this notification is about
1556 * @return GNUNET_OK to keep the connection open,
1557 * GNUNET_SYSERR to close it (signal serious error)
1560 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1561 const struct GNUNET_MessageHeader *message)
1563 struct GNUNET_MESH_ConnectionACK *msg;
1564 struct MeshConnection *c;
1565 struct MeshPeerPath *p;
1566 struct MeshPeer *pi;
1567 enum MeshConnectionState oldstate;
1570 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1571 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1572 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1573 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1574 GNUNET_h2s (&msg->cid));
1575 c = connection_get (&msg->cid);
1578 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1584 if (GNUNET_NO != c->destroy)
1586 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1590 oldstate = c->state;
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1592 pi = GMP_get (peer);
1593 if (get_next_hop (c) == pi)
1595 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1597 if (MESH_CONNECTION_SENT == oldstate)
1598 connection_change_state (c, MESH_CONNECTION_ACK);
1600 else if (get_prev_hop (c) == pi)
1602 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1604 connection_change_state (c, MESH_CONNECTION_READY);
1608 GNUNET_break_op (0);
1612 connection_reset_timeout (c, fwd);
1614 /* Add path to peers? */
1618 GMP_add_path_to_all (p, GNUNET_YES);
1625 /* Message for us as creator? */
1626 if (GMC_is_origin (c, GNUNET_YES))
1628 if (GNUNET_NO != fwd)
1630 GNUNET_break_op (0);
1633 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1635 /* If just created, cancel the short timeout and start a long one */
1636 if (MESH_CONNECTION_SENT == oldstate)
1637 connection_reset_timeout (c, GNUNET_YES);
1639 /* Change connection state */
1640 connection_change_state (c, MESH_CONNECTION_READY);
1641 send_connection_ack (c, GNUNET_YES);
1643 /* Change tunnel state, trigger KX */
1644 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1645 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1650 /* Message for us as destination? */
1651 if (GMC_is_terminal (c, GNUNET_YES))
1653 if (GNUNET_YES != fwd)
1655 GNUNET_break_op (0);
1658 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1660 /* If just created, cancel the short timeout and start a long one */
1661 if (MESH_CONNECTION_ACK == oldstate)
1662 connection_reset_timeout (c, GNUNET_NO);
1664 /* Change tunnel state */
1665 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1666 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1671 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1672 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1678 * Core handler for notifications of broken paths
1680 * @param cls Closure (unused).
1681 * @param id Peer identity of sending neighbor.
1682 * @param message Message.
1684 * @return GNUNET_OK to keep the connection open,
1685 * GNUNET_SYSERR to close it (signal serious error)
1688 GMC_handle_broken (void* cls,
1689 const struct GNUNET_PeerIdentity* id,
1690 const struct GNUNET_MessageHeader* message)
1692 struct GNUNET_MESH_ConnectionBroken *msg;
1693 struct MeshConnection *c;
1696 LOG (GNUNET_ERROR_TYPE_DEBUG,
1697 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1698 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1699 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1700 GNUNET_i2s (&msg->peer1));
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1702 GNUNET_i2s (&msg->peer2));
1703 c = connection_get (&msg->cid);
1706 GNUNET_break_op (0);
1710 fwd = is_fwd (c, id);
1711 if (GMC_is_terminal (c, fwd))
1713 path_invalidate (c->path);
1714 if (0 < c->pending_messages)
1715 c->destroy = GNUNET_YES;
1721 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1722 c->destroy = GNUNET_YES;
1723 connection_cancel_queues (c, !fwd);
1732 * Core handler for tunnel destruction
1734 * @param cls Closure (unused).
1735 * @param peer Peer identity of sending neighbor.
1736 * @param message Message.
1738 * @return GNUNET_OK to keep the connection open,
1739 * GNUNET_SYSERR to close it (signal serious error)
1742 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1743 const struct GNUNET_MessageHeader *message)
1745 struct GNUNET_MESH_ConnectionDestroy *msg;
1746 struct MeshConnection *c;
1749 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1750 LOG (GNUNET_ERROR_TYPE_DEBUG,
1751 "Got a CONNECTION DESTROY message from %s\n",
1753 LOG (GNUNET_ERROR_TYPE_DEBUG,
1754 " for connection %s\n",
1755 GNUNET_h2s (&msg->cid));
1756 c = connection_get (&msg->cid);
1759 /* Probably already got the message from another path,
1760 * destroyed the tunnel and retransmitted to children.
1763 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1765 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1768 fwd = is_fwd (c, peer);
1769 if (GNUNET_SYSERR == fwd)
1771 GNUNET_break_op (0); /* FIXME */
1774 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1775 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1776 else if (0 == c->pending_messages)
1778 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1782 c->destroy = GNUNET_YES;
1783 c->state = MESH_CONNECTION_DESTROYED;
1786 GMT_remove_connection (c->t, c);
1794 * Generic handler for mesh network encrypted traffic.
1796 * @param peer Peer identity this notification is about.
1797 * @param msg Encrypted message.
1799 * @return GNUNET_OK to keep the connection open,
1800 * GNUNET_SYSERR to close it (signal serious error)
1803 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1804 const struct GNUNET_MESH_Encrypted *msg)
1806 struct MeshConnection *c;
1807 struct MeshPeer *neighbor;
1808 struct MeshFlowControl *fc;
1809 GNUNET_PEER_Id peer_id;
1817 size = ntohs (msg->header.size);
1819 sizeof (struct GNUNET_MESH_Encrypted) +
1820 sizeof (struct GNUNET_MessageHeader))
1822 GNUNET_break_op (0);
1825 type = ntohs (msg->header.type);
1826 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1827 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1828 GM_m2s (type), ntohl (msg->pid), GNUNET_i2s (peer));
1830 /* Check connection */
1831 c = connection_get (&msg->cid);
1834 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1835 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
1836 GNUNET_h2s (&msg->cid));
1840 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1842 /* Check if origin is as expected */
1843 neighbor = get_prev_hop (c);
1844 peer_id = GNUNET_PEER_search (peer);
1845 if (peer_id == GMP_get_short_id (neighbor))
1851 neighbor = get_next_hop (c);
1852 if (peer_id == GMP_get_short_id (neighbor))
1858 /* Unexpected peer sending traffic on a connection. */
1859 GNUNET_break_op (0);
1865 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1866 pid = ntohl (msg->pid);
1867 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1869 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1870 LOG (GNUNET_ERROR_TYPE_DEBUG,
1871 "WARNING Received PID %u, (prev %u), ACK %u\n",
1872 pid, fc->last_pid_recv, fc->last_ack_sent);
1875 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1877 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1878 LOG (GNUNET_ERROR_TYPE_DEBUG,
1879 " Pid %u not expected (%u+), dropping!\n",
1880 pid, fc->last_pid_recv + 1);
1883 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1884 connection_change_state (c, MESH_CONNECTION_READY);
1885 connection_reset_timeout (c, fwd);
1886 fc->last_pid_recv = pid;
1888 /* Is this message for us? */
1889 if (GMC_is_terminal (c, fwd))
1891 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1892 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1896 GNUNET_break (GNUNET_NO != c->destroy);
1899 fc->last_pid_recv = pid;
1900 GMT_handle_encrypted (c->t, msg);
1901 GMC_send_ack (c, fwd, GNUNET_NO);
1905 /* Message not for us: forward to next hop */
1906 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1907 ttl = ntohl (msg->ttl);
1908 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1911 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1912 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1913 GMC_send_ack (c, fwd, GNUNET_NO);
1917 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1918 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1924 * Generic handler for mesh network encrypted traffic.
1926 * @param peer Peer identity this notification is about.
1927 * @param msg Encrypted message.
1929 * @return GNUNET_OK to keep the connection open,
1930 * GNUNET_SYSERR to close it (signal serious error)
1933 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1934 const struct GNUNET_MESH_KX *msg)
1936 struct MeshConnection *c;
1937 struct MeshPeer *neighbor;
1938 GNUNET_PEER_Id peer_id;
1944 size = ntohs (msg->header.size);
1946 sizeof (struct GNUNET_MESH_Encrypted) +
1947 sizeof (struct GNUNET_MessageHeader))
1949 GNUNET_break_op (0);
1952 type = ntohs (msg->header.type);
1953 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1954 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1955 GM_m2s (type), GNUNET_i2s (peer));
1957 /* Check connection */
1958 c = connection_get (&msg->cid);
1961 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1962 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING kx on unknown connection %s\n",
1963 GNUNET_h2s (&msg->cid));
1966 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1968 /* Check if origin is as expected */
1969 neighbor = get_prev_hop (c);
1970 peer_id = GNUNET_PEER_search (peer);
1971 if (peer_id == GMP_get_short_id (neighbor))
1977 neighbor = get_next_hop (c);
1978 if (peer_id == GMP_get_short_id (neighbor))
1984 /* Unexpected peer sending traffic on a connection. */
1985 GNUNET_break_op (0);
1990 /* Count as connection confirmation. */
1991 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1993 connection_change_state (c, MESH_CONNECTION_READY);
1996 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1997 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
2000 connection_reset_timeout (c, fwd);
2002 /* Is this message for us? */
2003 if (GMC_is_terminal (c, fwd))
2005 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2006 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2012 GMT_handle_kx (c->t, &msg[1].header);
2016 /* Message not for us: forward to next hop */
2017 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2018 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2019 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
2026 * Core handler for encrypted mesh network traffic (channel mgmt, data).
2028 * @param cls Closure (unused).
2029 * @param message Message received.
2030 * @param peer Peer who sent the message.
2032 * @return GNUNET_OK to keep the connection open,
2033 * GNUNET_SYSERR to close it (signal serious error)
2036 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2037 const struct GNUNET_MessageHeader *message)
2039 return handle_mesh_encrypted (peer,
2040 (struct GNUNET_MESH_Encrypted *)message);
2045 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2047 * @param cls Closure (unused).
2048 * @param message Message received.
2049 * @param peer Peer who sent the message.
2051 * @return GNUNET_OK to keep the connection open,
2052 * GNUNET_SYSERR to close it (signal serious error)
2055 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2056 const struct GNUNET_MessageHeader *message)
2058 return handle_mesh_kx (peer,
2059 (struct GNUNET_MESH_KX *) message);
2064 * Core handler for mesh network traffic point-to-point acks.
2066 * @param cls closure
2067 * @param message message
2068 * @param peer peer identity this notification is about
2070 * @return GNUNET_OK to keep the connection open,
2071 * GNUNET_SYSERR to close it (signal serious error)
2074 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2075 const struct GNUNET_MessageHeader *message)
2077 struct GNUNET_MESH_ACK *msg;
2078 struct MeshConnection *c;
2079 struct MeshFlowControl *fc;
2084 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2085 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
2087 msg = (struct GNUNET_MESH_ACK *) message;
2089 c = connection_get (&msg->cid);
2093 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2098 /* Is this a forward or backward ACK? */
2099 id = GNUNET_PEER_search (peer);
2100 if (GMP_get_short_id (get_next_hop (c)) == id)
2102 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2106 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2108 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2114 GNUNET_break_op (0);
2118 ack = ntohl (msg->ack);
2119 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2120 ack, fc->last_ack_recv);
2121 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
2122 fc->last_ack_recv = ack;
2124 /* Cancel polling if the ACK is big enough. */
2125 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2126 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2128 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2129 GNUNET_SCHEDULER_cancel (fc->poll_task);
2130 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2131 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2134 connection_unlock_queue (c, fwd);
2141 * Core handler for mesh network traffic point-to-point ack polls.
2143 * @param cls closure
2144 * @param message message
2145 * @param peer peer identity this notification is about
2147 * @return GNUNET_OK to keep the connection open,
2148 * GNUNET_SYSERR to close it (signal serious error)
2151 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2152 const struct GNUNET_MessageHeader *message)
2154 struct GNUNET_MESH_Poll *msg;
2155 struct MeshConnection *c;
2156 struct MeshFlowControl *fc;
2161 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2162 LOG (GNUNET_ERROR_TYPE_DEBUG,
2163 "Got a POLL message from %s!\n",
2166 msg = (struct GNUNET_MESH_Poll *) message;
2168 c = connection_get (&msg->cid);
2172 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2174 LOG (GNUNET_ERROR_TYPE_DEBUG,
2175 "WARNING POLL message on unknown connection %s!\n",
2176 GNUNET_h2s (&msg->cid));
2180 /* Is this a forward or backward ACK?
2181 * Note: a poll should never be needed in a loopback case,
2182 * since there is no possiblility of packet loss there, so
2183 * this way of discerining FWD/BCK should not be a problem.
2185 id = GNUNET_PEER_search (peer);
2186 if (GMP_get_short_id (get_next_hop (c)) == id)
2188 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2191 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2193 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2198 GNUNET_break_op (0);
2202 pid = ntohl (msg->pid);
2203 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2204 fc->last_pid_recv = pid;
2205 fwd = fc == &c->bck_fc;
2206 GMC_send_ack (c, fwd, GNUNET_YES);
2213 * Core handler for mesh keepalives.
2215 * @param cls closure
2216 * @param message message
2217 * @param peer peer identity this notification is about
2218 * @return GNUNET_OK to keep the connection open,
2219 * GNUNET_SYSERR to close it (signal serious error)
2221 * TODO: Check who we got this from, to validate route.
2224 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2225 const struct GNUNET_MessageHeader *message)
2227 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2228 struct MeshConnection *c;
2229 struct MeshPeer *neighbor;
2230 GNUNET_PEER_Id peer_id;
2233 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2234 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
2237 c = connection_get (&msg->cid);
2240 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2245 /* Check if origin is as expected TODO refactor and reuse */
2246 peer_id = GNUNET_PEER_search (peer);
2247 neighbor = get_prev_hop (c);
2248 if (peer_id == GMP_get_short_id (neighbor))
2254 neighbor = get_next_hop (c);
2255 if (peer_id == GMP_get_short_id (neighbor))
2261 GNUNET_break_op (0);
2266 connection_change_state (c, MESH_CONNECTION_READY);
2267 connection_reset_timeout (c, fwd);
2269 if (GMC_is_terminal (c, fwd))
2272 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2273 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2280 * Send an ACK on the appropriate connection/channel, depending on
2281 * the direction and the position of the peer.
2283 * @param c Which connection to send the hop-by-hop ACK.
2284 * @param fwd Is this a fwd ACK? (will go dest->root).
2285 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2288 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2290 unsigned int buffer;
2292 LOG (GNUNET_ERROR_TYPE_DEBUG,
2293 "GMC send %s ACK on %s\n",
2294 GM_f2s (fwd), GMC_2s (c));
2302 if (GNUNET_NO != c->destroy)
2304 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2308 /* Get available buffer space */
2309 if (GMC_is_terminal (c, fwd))
2311 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2312 buffer = GMT_get_channels_buffer (c->t);
2316 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2317 buffer = GMC_get_buffer (c, fwd);
2319 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2320 if (0 == buffer && GNUNET_NO == force)
2323 /* Send available buffer space */
2324 if (GMC_is_origin (c, fwd))
2326 GNUNET_assert (NULL != c->t);
2327 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2328 GMT_unchoke_channels (c->t);
2332 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2333 send_ack (c, buffer, fwd, force);
2339 * Initialize the connections subsystem
2341 * @param c Configuration handle.
2344 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2346 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2348 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2351 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2352 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2353 GNUNET_SCHEDULER_shutdown ();
2358 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2361 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2362 "MESH", "MAX_CONNECTIONS", "MISSING");
2363 GNUNET_SCHEDULER_shutdown ();
2368 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2369 &refresh_connection_time))
2371 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2372 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2373 GNUNET_SCHEDULER_shutdown ();
2376 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2377 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2382 * Destroy each connection on shutdown.
2384 * @param cls Closure (unused).
2385 * @param key Current key code (CID, unused).
2386 * @param value Value in the hash map (connection)
2388 * @return #GNUNET_YES, because we should continue to iterate,
2391 shutdown_iterator (void *cls,
2392 const struct GNUNET_HashCode *key,
2395 struct MeshConnection *c = value;
2403 * Shut down the connections subsystem.
2408 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2409 GNUNET_CONTAINER_multihashmap_destroy (connections);
2414 struct MeshConnection *
2415 GMC_new (const struct GNUNET_HashCode *cid,
2416 struct MeshTunnel3 *t,
2417 struct MeshPeerPath *p,
2418 unsigned int own_pos)
2420 struct MeshConnection *c;
2422 c = GNUNET_new (struct MeshConnection);
2424 GNUNET_assert (GNUNET_OK ==
2425 GNUNET_CONTAINER_multihashmap_put (connections,
2427 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2428 fc_init (&c->fwd_fc);
2429 fc_init (&c->bck_fc);
2434 GNUNET_assert (own_pos <= p->length - 1);
2435 c->own_pos = own_pos;
2438 if (GNUNET_OK != register_neighbors (c))
2442 path_invalidate (c->path);
2452 c->fwd_maintenance_task =
2453 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2454 &connection_fwd_keepalive, c);
2462 GMC_destroy (struct MeshConnection *c)
2470 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2471 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2474 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2475 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2476 &c->fwd_fc, &c->bck_fc);
2477 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2478 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2480 /* Cancel all traffic */
2481 if (NULL != c->path)
2483 connection_cancel_queues (c, GNUNET_YES);
2484 connection_cancel_queues (c, GNUNET_NO);
2485 unregister_neighbors (c);
2488 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2489 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2491 /* Cancel maintainance task (keepalive/timeout) */
2492 if (NULL != c->fwd_fc.poll_msg)
2494 GMC_cancel (c->fwd_fc.poll_msg);
2495 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2497 if (NULL != c->bck_fc.poll_msg)
2499 GMC_cancel (c->bck_fc.poll_msg);
2500 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2503 /* Delete from tunnel */
2505 GMT_remove_connection (c->t, c);
2507 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2508 path_destroy (c->path);
2509 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2510 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2511 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2512 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2513 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2515 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2516 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2518 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2520 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2521 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2524 GNUNET_break (GNUNET_YES ==
2525 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2527 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2532 * Get the connection ID.
2534 * @param c Connection to get the ID from.
2536 * @return ID of the connection.
2538 const struct GNUNET_HashCode *
2539 GMC_get_id (const struct MeshConnection *c)
2546 * Get the connection path.
2548 * @param c Connection to get the path from.
2550 * @return path used by the connection.
2552 const struct MeshPeerPath *
2553 GMC_get_path (const struct MeshConnection *c)
2555 if (GNUNET_NO == c->destroy)
2562 * Get the connection state.
2564 * @param c Connection to get the state from.
2566 * @return state of the connection.
2568 enum MeshConnectionState
2569 GMC_get_state (const struct MeshConnection *c)
2575 * Get the connection tunnel.
2577 * @param c Connection to get the tunnel from.
2579 * @return tunnel of the connection.
2581 struct MeshTunnel3 *
2582 GMC_get_tunnel (const struct MeshConnection *c)
2589 * Get free buffer space in a connection.
2591 * @param c Connection.
2592 * @param fwd Is query about FWD traffic?
2594 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2597 GMC_get_buffer (struct MeshConnection *c, int fwd)
2599 struct MeshFlowControl *fc;
2601 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2603 return (fc->queue_max - fc->queue_n);
2607 * Get how many messages have we allowed to send to us from a direction.
2609 * @param c Connection.
2610 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2612 * @return last_ack_sent - last_pid_recv
2615 GMC_get_allowed (struct MeshConnection *c, int fwd)
2617 struct MeshFlowControl *fc;
2619 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2620 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2624 return (fc->last_ack_sent - fc->last_pid_recv);
2628 * Get messages queued in a connection.
2630 * @param c Connection.
2631 * @param fwd Is query about FWD traffic?
2633 * @return Number of messages queued.
2636 GMC_get_qn (struct MeshConnection *c, int fwd)
2638 struct MeshFlowControl *fc;
2640 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2647 * Allow the connection to advertise a buffer of the given size.
2649 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2650 * allowing up to last_pid_recv + buffer.
2652 * @param c Connection.
2653 * @param buffer How many more messages the connection can accept.
2654 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2657 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2659 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2660 GMC_2s (c), buffer, GM_f2s (fwd));
2661 send_ack (c, buffer, fwd, GNUNET_NO);
2666 * Notify other peers on a connection of a broken link. Mark connections
2667 * to destroy after all traffic has been sent.
2669 * @param c Connection on which there has been a disconnection.
2670 * @param peer Peer that disconnected.
2673 GMC_notify_broken (struct MeshConnection *c,
2674 struct MeshPeer *peer)
2678 LOG (GNUNET_ERROR_TYPE_DEBUG,
2679 " notify broken on %s due to %s disconnect\n",
2680 GMC_2s (c), GMP_2s (peer));
2682 fwd = peer == get_prev_hop (c);
2684 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2686 /* Local shutdown, no one to notify about this. */
2690 if (GNUNET_NO == c->destroy)
2691 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2693 /* Connection will have at least one pending message
2694 * (the one we just scheduled), so no point in checking whether to
2695 * destroy immediately. */
2696 c->destroy = GNUNET_YES;
2697 c->state = MESH_CONNECTION_DESTROYED;
2700 * Cancel all queues, if no message is left, connection will be destroyed.
2702 connection_cancel_queues (c, !fwd);
2709 * Is this peer the first one on the connection?
2711 * @param c Connection.
2712 * @param fwd Is this about fwd traffic?
2714 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2717 GMC_is_origin (struct MeshConnection *c, int fwd)
2719 if (!fwd && c->path->length - 1 == c->own_pos )
2721 if (fwd && 0 == c->own_pos)
2728 * Is this peer the last one on the connection?
2730 * @param c Connection.
2731 * @param fwd Is this about fwd traffic?
2732 * Note that the ROOT is the terminal for BCK traffic!
2734 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2737 GMC_is_terminal (struct MeshConnection *c, int fwd)
2739 return GMC_is_origin (c, !fwd);
2744 * See if we are allowed to send by the next hop in the given direction.
2746 * @param c Connection.
2747 * @param fwd Is this about fwd traffic?
2749 * @return #GNUNET_YES in case it's OK to send.
2752 GMC_is_sendable (struct MeshConnection *c, int fwd)
2754 struct MeshFlowControl *fc;
2761 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2762 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2768 * Sends an already built message on a connection, properly registering
2769 * all used resources.
2771 * @param message Message to send. Function makes a copy of it.
2772 * If message is not hop-by-hop, decrements TTL of copy.
2773 * @param c Connection on which this message is transmitted.
2774 * @param fwd Is this a fwd message?
2775 * @param force Force the connection to accept the message (buffer overfill).
2776 * @param cont Continuation called once message is sent. Can be NULL.
2777 * @param cont_cls Closure for @c cont.
2779 * @return Handle to cancel the message before it's sent.
2780 * NULL on error or if @c cont is NULL.
2781 * Invalid on @c cont call.
2783 struct MeshConnectionQueue *
2784 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2785 struct MeshConnection *c, int fwd, int force,
2786 GMC_sent cont, void *cont_cls)
2788 struct MeshFlowControl *fc;
2789 struct MeshConnectionQueue *q;
2795 size = ntohs (message->size);
2796 data = GNUNET_malloc (size);
2797 memcpy (data, message, size);
2798 type = ntohs (message->type);
2799 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2800 GM_m2s (type), size, GMC_2s (c));
2802 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2803 droppable = GNUNET_NO == force;
2806 struct GNUNET_MESH_Encrypted *emsg;
2807 struct GNUNET_MESH_KX *kmsg;
2808 struct GNUNET_MESH_ACK *amsg;
2809 struct GNUNET_MESH_Poll *pmsg;
2810 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2811 struct GNUNET_MESH_ConnectionBroken *bmsg;
2814 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2815 emsg = (struct GNUNET_MESH_Encrypted *) data;
2816 ttl = ntohl (emsg->ttl);
2819 GNUNET_break_op (0);
2824 emsg->ttl = htonl (ttl - 1);
2825 emsg->pid = htonl (fc->next_pid++);
2826 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2827 if (GNUNET_YES == droppable)
2830 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2831 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2832 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2836 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2838 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2840 GMC_start_poll (c, fwd);
2844 case GNUNET_MESSAGE_TYPE_MESH_KX:
2845 kmsg = (struct GNUNET_MESH_KX *) data;
2849 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2850 amsg = (struct GNUNET_MESH_ACK *) data;
2852 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2853 droppable = GNUNET_NO;
2856 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2857 pmsg = (struct GNUNET_MESH_Poll *) data;
2859 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2860 droppable = GNUNET_NO;
2863 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2864 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2869 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2870 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2875 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2876 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2877 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2886 if (fc->queue_n > fc->queue_max && droppable)
2888 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2891 LOG (GNUNET_ERROR_TYPE_DEBUG,
2892 "queue full: %u/%u\n",
2893 fc->queue_n, fc->queue_max);
2894 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2900 return NULL; /* Drop this message */
2903 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2904 c->pending_messages++;
2906 q = GNUNET_new (struct MeshConnectionQueue);
2907 q->forced = !droppable;
2908 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2912 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2918 q->cont_cls = cont_cls;
2924 * Cancel a previously sent message while it's in the queue.
2926 * ONLY can be called before the continuation given to the send function
2927 * is called. Once the continuation is called, the message is no longer in the
2930 * @param q Handle to the queue.
2933 GMC_cancel (struct MeshConnectionQueue *q)
2935 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2937 /* queue destroy calls message_sent, which calls q->cont and frees q */
2938 GMP_queue_destroy (q->q, GNUNET_YES);
2943 * Sends a CREATE CONNECTION message for a path to a peer.
2944 * Changes the connection and tunnel states if necessary.
2946 * @param connection Connection to create.
2949 GMC_send_create (struct MeshConnection *connection)
2951 enum MeshTunnel3CState state;
2954 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2955 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2957 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2958 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2959 connection, connection->pending_messages);
2960 connection->pending_messages++;
2962 GMP_queue_add (get_next_hop (connection), NULL,
2963 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2964 size, connection, GNUNET_YES, &message_sent, NULL);
2966 state = GMT_get_cstate (connection->t);
2967 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2968 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2969 if (MESH_CONNECTION_NEW == connection->state)
2970 connection_change_state (connection, MESH_CONNECTION_SENT);
2975 * Send a message to all peers in this connection that the connection
2976 * is no longer valid.
2978 * If some peer should not receive the message, it should be zero'ed out
2979 * before calling this function.
2981 * @param c The connection whose peers to notify.
2984 GMC_send_destroy (struct MeshConnection *c)
2986 struct GNUNET_MESH_ConnectionDestroy msg;
2988 if (GNUNET_YES == c->destroy)
2991 msg.header.size = htons (sizeof (msg));
2992 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2994 LOG (GNUNET_ERROR_TYPE_DEBUG,
2995 " sending connection destroy for connection %s\n",
2998 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2999 GMC_send_prebuilt_message (&msg.header, c,
3000 GNUNET_YES, GNUNET_YES, NULL, NULL);
3001 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
3002 GMC_send_prebuilt_message (&msg.header, c,
3003 GNUNET_NO, GNUNET_YES, NULL, NULL);
3004 c->destroy = GNUNET_YES;
3005 c->state = MESH_CONNECTION_DESTROYED;
3010 * @brief Start a polling timer for the connection.
3012 * When a neighbor does not accept more traffic on the connection it could be
3013 * caused by a simple congestion or by a lost ACK. Polling enables to check
3014 * for the lastest ACK status for a connection.
3016 * @param c Connection.
3017 * @param fwd Should we poll in the FWD direction?
3020 GMC_start_poll (struct MeshConnection *c, int fwd)
3022 struct MeshFlowControl *fc;
3024 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3025 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3027 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3029 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3030 fc->poll_task, fc->poll_msg);
3033 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3034 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3041 * @brief Stop polling a connection for ACKs.
3043 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3045 * @param c Connection.
3046 * @param fwd Should we stop the poll in the FWD direction?
3049 GMC_stop_poll (struct MeshConnection *c, int fwd)
3051 struct MeshFlowControl *fc;
3053 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3054 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3056 GNUNET_SCHEDULER_cancel (fc->poll_task);
3057 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3062 * Get a (static) string for a connection.
3064 * @param c Connection.
3067 GMC_2s (const struct MeshConnection *c)
3074 static char buf[128];
3076 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
3079 return GNUNET_h2s (&c->id);