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_MeshHash 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. Reentrant.
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_MeshHash *cid)
418 return GNUNET_CONTAINER_multihashmap_get (connections, GM_h2hc (cid));
423 connection_change_state (struct MeshConnection* c,
424 enum MeshConnectionState state)
426 LOG (GNUNET_ERROR_TYPE_DEBUG,
427 "Connection %s state %s -> %s\n",
428 GMC_2s (c), GMC_state2s (c->state), GMC_state2s (state));
429 if (MESH_CONNECTION_DESTROYED == c->state)
431 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
435 if (MESH_CONNECTION_READY == state)
441 * Callback called when a queued ACK message is sent.
443 * @param cls Closure (FC).
444 * @param c Connection this message was on.
445 * @param q Queue handler this call invalidates.
446 * @param type Type of message sent.
447 * @param fwd Was this a FWD going message?
448 * @param size Size of the message.
452 struct MeshConnection *c,
453 struct MeshConnectionQueue *q,
454 uint16_t type, int fwd, size_t size)
456 struct MeshFlowControl *fc = cls;
463 * Send an ACK on the connection, informing the predecessor about
464 * the available buffer space. Should not be called in case the peer
465 * is origin (no predecessor) in the @c fwd direction.
467 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
468 * the ACK itself goes "back" (dest->root).
470 * @param c Connection on which to send the ACK.
471 * @param buffer How much space free to advertise?
472 * @param fwd Is this FWD ACK? (Going dest -> root)
473 * @param force Don't optimize out.
476 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
478 struct MeshFlowControl *next_fc;
479 struct MeshFlowControl *prev_fc;
480 struct GNUNET_MESH_ACK msg;
484 /* If origin, there is no connection to send ACKs. Wrong function! */
485 if (GMC_is_origin (c, fwd))
487 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
488 GMC_2s (c), GM_f2s (fwd));
493 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
494 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
496 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
497 GM_f2s (fwd), GMC_2s (c));
499 /* Check if we need to transmit the ACK. */
500 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
501 if (3 < delta && buffer < delta && GNUNET_NO == force)
503 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
504 LOG (GNUNET_ERROR_TYPE_DEBUG,
505 " last pid recv: %u, last ack sent: %u\n",
506 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
510 /* Ok, ACK might be necessary, what PID to ACK? */
511 ack = prev_fc->last_pid_recv + buffer;
512 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 " last pid %u, last ack %u, qmax %u, q %u\n",
515 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
516 next_fc->queue_max, next_fc->queue_n);
517 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
519 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
523 /* Check if message is already in queue */
524 if (NULL != prev_fc->ack_msg)
526 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
528 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
529 GMC_cancel (prev_fc->ack_msg);
530 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
534 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
539 prev_fc->last_ack_sent = ack;
541 /* Build ACK message and send on connection */
542 msg.header.size = htons (sizeof (msg));
543 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
544 msg.ack = htonl (ack);
547 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
554 * Callback called when a queued message is sent.
556 * Calculates the average time and connection packet tracking.
558 * @param cls Closure (ConnectionQueue Handle).
559 * @param c Connection this message was on.
560 * @param sent Was it really sent? (Could have been canceled)
561 * @param type Type of message sent.
562 * @param fwd Was this a FWD going message?
563 * @param size Size of the message.
564 * @param wait Time spent waiting for core (only the time for THIS message)
567 message_sent (void *cls,
568 struct MeshConnection *c, int sent,
569 uint16_t type, int fwd, size_t size,
570 struct GNUNET_TIME_Relative wait)
572 struct MeshConnectionPerformance *p;
573 struct MeshFlowControl *fc;
574 struct MeshConnectionQueue *q = cls;
578 fc = fwd ? &c->fwd_fc : &c->bck_fc;
579 LOG (GNUNET_ERROR_TYPE_DEBUG, "! %ssent %s %s\n",
580 sent ? "" : "not ", GM_f2s (fwd), GM_m2s (type));
581 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
587 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
588 q->cont (q->cont_cls, c, q, type, fwd, size);
592 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
594 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
601 c->pending_messages--;
602 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
604 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
608 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
611 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
612 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
613 c->maintenance_q = NULL;
614 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
615 if (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE == type || !fwd)
616 schedule_next_keepalive (c, fwd);
619 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
620 if (GNUNET_YES == sent)
623 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
624 if (GNUNET_NO == forced)
627 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 "! accounting pid %u\n",
633 LOG (GNUNET_ERROR_TYPE_DEBUG,
634 "! forced, Q_N not accounting pid %u\n",
637 if (GNUNET_YES == sent)
639 GMC_send_ack (c, fwd, GNUNET_NO);
640 connection_reset_timeout (c, fwd);
644 case GNUNET_MESSAGE_TYPE_MESH_POLL:
648 case GNUNET_MESSAGE_TYPE_MESH_ACK:
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
658 return; /* Only endpoints are interested in timing. */
661 usecsperbyte = ((double) wait.rel_value_us) / size;
662 if (p->size == AVG_MSGS)
664 /* Array is full. Substract oldest value, add new one and store. */
665 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
666 p->usecsperbyte[p->idx] = usecsperbyte;
667 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
671 /* Array not yet full. Add current value to avg and store. */
672 p->usecsperbyte[p->idx] = usecsperbyte;
674 p->avg += p->usecsperbyte[p->idx];
678 p->idx = (p->idx + 1) % AVG_MSGS;
683 * Get the previous hop in a connection
685 * @param c Connection.
687 * @return Previous peer in the connection.
689 static struct MeshPeer *
690 get_prev_hop (const struct MeshConnection *c)
694 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
695 GMC_2s (c), c->own_pos, c->path->length);
696 if (0 == c->own_pos || c->path->length < 2)
697 id = c->path->peers[0];
699 id = c->path->peers[c->own_pos - 1];
701 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
702 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
704 return GMP_get_short (id);
709 * Get the next hop in a connection
711 * @param c Connection.
713 * @return Next peer in the connection.
715 static struct MeshPeer *
716 get_next_hop (const struct MeshConnection *c)
720 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
721 GMC_2s (c), c->own_pos, c->path->length);
722 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
723 id = c->path->peers[c->path->length - 1];
725 id = c->path->peers[c->own_pos + 1];
727 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
728 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
730 return GMP_get_short (id);
735 * Get the hop in a connection.
737 * @param c Connection.
738 * @param fwd Next hop?
740 * @return Next peer in the connection.
742 static struct MeshPeer *
743 get_hop (struct MeshConnection *c, int fwd)
746 return get_next_hop (c);
747 return get_prev_hop (c);
752 * Is traffic coming from this sender 'FWD' traffic?
754 * @param c Connection to check.
755 * @param sender Peer identity of neighbor.
757 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
758 * the traffic is 'FWD'.
759 * #GNUNET_NO for BCK.
760 * #GNUNET_SYSERR for errors.
763 is_fwd (const struct MeshConnection *c,
764 const struct GNUNET_PeerIdentity *sender)
768 id = GNUNET_PEER_search (sender);
769 if (GMP_get_short_id (get_prev_hop (c)) == id)
772 if (GMP_get_short_id (get_next_hop (c)) == id)
776 return GNUNET_SYSERR;
781 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
782 * or a first CONNECTION_ACK directed to us.
784 * @param connection Connection to confirm.
785 * @param fwd Should we send it FWD? (root->dest)
786 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
789 send_connection_ack (struct MeshConnection *connection, int fwd)
791 struct MeshTunnel3 *t;
794 LOG (GNUNET_ERROR_TYPE_INFO, "=> {%18s ACK} on connection %s\n",
795 GM_f2s (!fwd), GMC_2s (connection));
796 GMP_queue_add (get_hop (connection, fwd), NULL,
797 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
798 sizeof (struct GNUNET_MESH_ConnectionACK),
799 connection, fwd, &message_sent, NULL);
800 connection->pending_messages++;
801 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
802 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
803 if (MESH_CONNECTION_READY != connection->state)
804 connection_change_state (connection, MESH_CONNECTION_SENT);
809 * Send a notification that a connection is broken.
811 * @param c Connection that is broken.
812 * @param id1 Peer that has disconnected.
813 * @param id2 Peer that has disconnected.
814 * @param fwd Direction towards which to send it.
817 send_broken (struct MeshConnection *c,
818 const struct GNUNET_PeerIdentity *id1,
819 const struct GNUNET_PeerIdentity *id2,
822 struct GNUNET_MESH_ConnectionBroken msg;
824 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
825 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
829 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
834 * Send a notification that a connection is broken, when a connection
835 * isn't even created.
837 * @param connection_id Connection ID.
838 * @param id1 Peer that has disconnected.
839 * @param id2 Peer that has disconnected.
840 * @param peer Peer to notify (neighbor who sent the connection).
843 send_broken2 (struct GNUNET_MeshHash *connection_id,
844 const struct GNUNET_PeerIdentity *id1,
845 const struct GNUNET_PeerIdentity *id2,
846 GNUNET_PEER_Id peer_id)
848 struct GNUNET_MESH_ConnectionBroken *msg;
849 struct MeshPeer *neighbor;
851 LOG (GNUNET_ERROR_TYPE_INFO, "=> BROKEN on unknown connection %s\n",
852 GNUNET_h2s (GM_h2hc (connection_id)));
854 msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
855 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
856 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
857 msg->cid = *connection_id;
860 neighbor = GMP_get_short (peer_id);
861 GMP_queue_add (neighbor, msg,
862 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
863 sizeof (struct GNUNET_MESH_ConnectionBroken),
864 NULL, GNUNET_SYSERR, /* connection, fwd */
865 NULL, NULL); /* continuation */
870 * Send keepalive packets for a connection.
872 * @param c Connection to keep alive..
873 * @param fwd Is this a FWD keepalive? (owner -> dest).
876 send_connection_keepalive (struct MeshConnection *c, int fwd)
878 struct GNUNET_MessageHeader msg;
880 LOG (GNUNET_ERROR_TYPE_INFO,
881 "keepalive %s for connection %s\n",
882 GM_f2s (fwd), GMC_2s (c));
883 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
885 GNUNET_assert (NULL != c->t);
886 msg.size = htons (sizeof (msg));
887 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
889 GNUNET_assert (NULL ==
890 GMT_send_prebuilt_message (&msg, c->t, c,
891 GNUNET_NO, NULL, NULL));
896 * Send CONNECTION_{CREATE/ACK} packets for a connection.
898 * @param c Connection for which to send the message.
899 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
902 connection_recreate (struct MeshConnection *c, int fwd)
904 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
908 send_connection_ack (c, GNUNET_NO);
913 * Generic connection timer management.
914 * Depending on the role of the peer in the connection will send the
915 * appropriate message (build or keepalive)
917 * @param c Conncetion to maintain.
921 connection_maintain (struct MeshConnection *c, int fwd)
923 if (GNUNET_NO != c->destroy)
926 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
928 /* TODO DHT GET with RO_BART */
933 case MESH_CONNECTION_NEW:
936 case MESH_CONNECTION_SENT:
937 connection_recreate (c, fwd);
939 case MESH_CONNECTION_READY:
940 send_connection_keepalive (c, fwd);
950 * Keep the connection alive.
952 * @param c Connection to keep alive.
953 * @param fwd Direction.
954 * @param shutdown Are we shutting down? (Don't send traffic)
955 * Non-zero value for true, not necessarily GNUNET_YES.
958 connection_keepalive (struct MeshConnection *c, int fwd, int shutdown)
960 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
961 GM_f2s (fwd), GMC_2s (c));
964 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
966 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
968 if (GNUNET_NO != shutdown)
971 connection_maintain (c, fwd);
973 /* Next execution will be scheduled by message_sent */
978 * Keep the connection alive in the FWD direction.
980 * @param cls Closure (connection to keepalive).
981 * @param tc TaskContext.
984 connection_fwd_keepalive (void *cls,
985 const struct GNUNET_SCHEDULER_TaskContext *tc)
987 connection_keepalive ((struct MeshConnection *) cls,
989 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
994 * Keep the connection alive in the BCK direction.
996 * @param cls Closure (connection to keepalive).
997 * @param tc TaskContext.
1000 connection_bck_keepalive (void *cls,
1001 const struct GNUNET_SCHEDULER_TaskContext *tc)
1003 connection_keepalive ((struct MeshConnection *) cls,
1005 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1010 * Schedule next keepalive task, taking in consideration
1011 * the connection state and number of retries.
1013 * If the peer is not the origin, do nothing.
1015 * @param c Connection for which to schedule the next keepalive.
1016 * @param fwd Direction for the next keepalive.
1019 schedule_next_keepalive (struct MeshConnection *c, int fwd)
1021 struct GNUNET_TIME_Relative delay;
1022 GNUNET_SCHEDULER_TaskIdentifier *task_id;
1023 GNUNET_SCHEDULER_Task keepalive_task;
1025 if (GNUNET_NO == GMC_is_origin (c, fwd))
1028 /* Calculate delay to use, depending on the state of the connection */
1029 if (MESH_CONNECTION_READY == c->state)
1031 delay = refresh_connection_time;
1035 if (1 > c->create_retry)
1036 c->create_retry = 1;
1037 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1039 if (c->create_retry < 64)
1040 c->create_retry *= 2;
1043 /* Select direction-dependent parameters */
1044 if (GNUNET_YES == fwd)
1046 task_id = &c->fwd_maintenance_task;
1047 keepalive_task = &connection_fwd_keepalive;
1051 task_id = &c->bck_maintenance_task;
1052 keepalive_task = &connection_bck_keepalive;
1055 /* Check that no one scheduled it before us */
1056 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1058 /* No need for a _break. It can happen for instance when sending a SYNACK
1059 * for a duplicate SYN: the first SYNACK scheduled the task. */
1060 GNUNET_SCHEDULER_cancel (*task_id);
1063 /* Schedule the task */
1064 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1066 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1071 * @brief Re-initiate traffic on this connection if necessary.
1073 * Check if there is traffic queued towards this peer
1074 * and the core transmit handle is NULL (traffic was stalled).
1075 * If so, call core tmt rdy.
1077 * @param c Connection on which initiate traffic.
1078 * @param fwd Is this about fwd traffic?
1081 connection_unlock_queue (struct MeshConnection *c, int fwd)
1083 struct MeshPeer *peer;
1085 LOG (GNUNET_ERROR_TYPE_DEBUG,
1086 "connection_unlock_queue %s on %s\n",
1087 GM_f2s (fwd), GMC_2s (c));
1089 if (GMC_is_terminal (c, fwd))
1091 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1095 peer = get_hop (c, fwd);
1096 GMP_queue_unlock (peer, c);
1101 * Cancel all transmissions that belong to a certain connection.
1103 * If the connection is scheduled for destruction and no more messages are left,
1104 * the connection will be destroyed by the continuation call.
1106 * @param c Connection which to cancel. Might be destroyed during this call.
1107 * @param fwd Cancel fwd traffic?
1110 connection_cancel_queues (struct MeshConnection *c, int fwd)
1112 struct MeshFlowControl *fc;
1113 struct MeshPeer *peer;
1115 LOG (GNUNET_ERROR_TYPE_DEBUG,
1116 " *** Cancel %s queues for connection %s\n",
1117 GM_f2s (fwd), GMC_2s (c));
1124 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1125 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1127 GNUNET_SCHEDULER_cancel (fc->poll_task);
1128 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1129 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1131 peer = get_hop (c, fwd);
1132 GMP_queue_cancel (peer, c);
1137 * Function called if a connection has been stalled for a while,
1138 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1140 * @param cls Closure (poll ctx).
1141 * @param tc TaskContext.
1144 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1148 * Callback called when a queued POLL message is sent.
1150 * @param cls Closure (FC).
1151 * @param c Connection this message was on.
1152 * @param q Queue handler this call invalidates.
1153 * @param type Type of message sent.
1154 * @param fwd Was this a FWD going message?
1155 * @param size Size of the message.
1158 poll_sent (void *cls,
1159 struct MeshConnection *c,
1160 struct MeshConnectionQueue *q,
1161 uint16_t type, int fwd, size_t size)
1163 struct MeshFlowControl *fc = cls;
1165 if (2 == c->destroy)
1167 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1170 LOG (GNUNET_ERROR_TYPE_DEBUG,
1171 " *** POLL sent for , scheduling new one!\n");
1172 fc->poll_msg = NULL;
1173 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1174 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1175 &connection_poll, fc);
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1181 * Function called if a connection has been stalled for a while,
1182 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1184 * @param cls Closure (poll ctx).
1185 * @param tc TaskContext.
1188 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1190 struct MeshFlowControl *fc = cls;
1191 struct GNUNET_MESH_Poll msg;
1192 struct MeshConnection *c;
1194 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1195 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1202 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1203 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1204 fc == &c->fwd_fc ? "FWD" : "BCK");
1206 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1207 msg.header.size = htons (sizeof (msg));
1208 msg.pid = htonl (fc->last_pid_sent);
1209 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1210 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1211 fc == &c->fwd_fc, GNUNET_YES,
1217 * Timeout function due to lack of keepalive/traffic from the owner.
1218 * Destroys connection if called.
1220 * @param cls Closure (connection to destroy).
1221 * @param tc TaskContext.
1224 connection_fwd_timeout (void *cls,
1225 const struct GNUNET_SCHEDULER_TaskContext *tc)
1227 struct MeshConnection *c = cls;
1229 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1230 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1235 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1246 * Timeout function due to lack of keepalive/traffic from the destination.
1247 * Destroys connection if called.
1249 * @param cls Closure (connection to destroy).
1250 * @param tc TaskContext
1253 connection_bck_timeout (void *cls,
1254 const struct GNUNET_SCHEDULER_TaskContext *tc)
1256 struct MeshConnection *c = cls;
1258 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1259 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1262 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1265 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1276 * Resets the connection timeout task, some other message has done the
1278 * - For the first peer on the direction this means to send
1279 * a keepalive or a path confirmation message (either create or ACK).
1280 * - For all other peers, this means to destroy the connection,
1281 * due to lack of activity.
1282 * Starts the timeout if no timeout was running (connection just created).
1284 * @param c Connection whose timeout to reset.
1285 * @param fwd Is this forward?
1287 * TODO use heap to improve efficiency of scheduler.
1290 connection_reset_timeout (struct MeshConnection *c, int fwd)
1292 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1294 if (GMC_is_origin (c, fwd)) /* Startpoint */
1296 schedule_next_keepalive (c, fwd);
1298 else /* Relay, endpoint. */
1300 struct GNUNET_TIME_Relative delay;
1301 GNUNET_SCHEDULER_TaskIdentifier *ti;
1302 GNUNET_SCHEDULER_Task f;
1304 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1306 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1307 GNUNET_SCHEDULER_cancel (*ti);
1308 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1309 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1310 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1316 * Add the connection to the list of both neighbors.
1318 * @param c Connection.
1320 * @return #GNUNET_OK if everything went fine
1321 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1324 register_neighbors (struct MeshConnection *c)
1326 struct MeshPeer *next_peer;
1327 struct MeshPeer *prev_peer;
1329 next_peer = get_next_hop (c);
1330 prev_peer = get_prev_hop (c);
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1334 path_debug (c->path);
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1337 GMC_2s (c), next_peer);
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GMP_2s (next_peer));
1339 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1340 GMC_2s (c), prev_peer);
1341 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GMP_2s (prev_peer));
1343 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1344 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1346 if (GMC_is_origin (c, GNUNET_YES))
1347 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1348 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1350 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1351 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1352 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1354 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1355 return GNUNET_SYSERR;
1358 GMP_add_connection (next_peer, c);
1359 GMP_add_connection (prev_peer, c);
1366 * Remove the connection from the list of both neighbors.
1368 * @param c Connection.
1371 unregister_neighbors (struct MeshConnection *c)
1373 struct MeshPeer *peer;
1375 peer = get_next_hop (c);
1376 if (GNUNET_OK != GMP_remove_connection (peer, c))
1378 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1379 || MESH_CONNECTION_DESTROYED == c->state);
1380 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1381 if (NULL != c->t) GMT_debug (c->t);
1384 peer = get_prev_hop (c);
1385 if (GNUNET_OK != GMP_remove_connection (peer, c))
1387 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1388 || MESH_CONNECTION_DESTROYED == c->state);
1389 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1390 if (NULL != c->t) GMT_debug (c->t);
1396 * Bind the connection to the peer and the tunnel to that peer.
1398 * If the peer has no tunnel, create one. Update tunnel and connection
1399 * data structres to reflect new status.
1401 * @param c Connection.
1405 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1407 GMP_add_tunnel (peer);
1408 c->t = GMP_get_tunnel (peer);
1409 GMT_add_connection (c->t, c);
1414 * Builds a path from a PeerIdentity array.
1416 * @param peers PeerIdentity array.
1417 * @param size Size of the @c peers array.
1418 * @param own_pos Output parameter: own position in the path.
1420 * @return Fixed and shortened path.
1422 static struct MeshPeerPath *
1423 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1425 unsigned int *own_pos)
1427 struct MeshPeerPath *path;
1428 GNUNET_PEER_Id shortid;
1431 unsigned int offset;
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1435 path = path_new (size);
1438 for (i = 0; i < size; i++)
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1441 i, GNUNET_i2s (&peers[i]));
1442 shortid = GNUNET_PEER_intern (&peers[i]);
1444 /* Check for loops / duplicates */
1445 for (j = 0; j < i - offset; j++)
1447 if (path->peers[j] == shortid)
1449 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now %u\n", offset);
1452 GNUNET_PEER_change_rc (shortid, -1);
1455 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1456 path->peers[i - offset] = shortid;
1457 if (path->peers[i - offset] == myid)
1458 *own_pos = i - offset;
1460 path->length -= offset;
1462 if (path->peers[*own_pos] != myid)
1464 /* create path: self not found in path through self */
1465 GNUNET_break_op (0);
1466 path_destroy (path);
1475 * Log receipt of message on stderr (INFO level).
1477 * @param message Message received.
1478 * @param peer Peer who sent the message.
1479 * @param hash Connection ID.
1482 log_message (const struct GNUNET_MessageHeader *message,
1483 const struct GNUNET_PeerIdentity *peer,
1484 const struct GNUNET_MeshHash *hash)
1486 LOG (GNUNET_ERROR_TYPE_INFO, "<- %s on connection %s from %s\n",
1487 GM_m2s (ntohs (message->type)), GNUNET_h2s (GM_h2hc (hash)),
1491 /******************************************************************************/
1492 /******************************** API ***********************************/
1493 /******************************************************************************/
1496 * Core handler for connection creation.
1498 * @param cls Closure (unused).
1499 * @param peer Sender (neighbor).
1500 * @param message Message.
1502 * @return GNUNET_OK to keep the connection open,
1503 * GNUNET_SYSERR to close it (signal serious error)
1506 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1507 const struct GNUNET_MessageHeader *message)
1509 struct GNUNET_MESH_ConnectionCreate *msg;
1510 struct GNUNET_PeerIdentity *id;
1511 struct GNUNET_MeshHash *cid;
1512 struct MeshPeerPath *path;
1513 struct MeshPeer *dest_peer;
1514 struct MeshPeer *orig_peer;
1515 struct MeshConnection *c;
1516 unsigned int own_pos;
1520 size = ntohs (message->size);
1521 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1523 GNUNET_break_op (0);
1527 /* Calculate hops */
1528 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1529 if (size % sizeof (struct GNUNET_PeerIdentity))
1531 GNUNET_break_op (0);
1534 size /= sizeof (struct GNUNET_PeerIdentity);
1537 GNUNET_break_op (0);
1540 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1542 /* Get parameters */
1543 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1545 log_message (message, peer, cid);
1546 id = (struct GNUNET_PeerIdentity *) &msg[1];
1547 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1549 /* Create connection */
1550 c = connection_get (cid);
1553 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1559 GNUNET_break_op (0);
1560 path_destroy (path);
1563 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1564 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1565 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1568 if (path->length - 1 == own_pos)
1570 /* If we are destination, why did the creation fail? */
1574 send_broken2 (cid, &my_full_id,
1575 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1576 path->peers[own_pos - 1]);
1577 path_destroy (path);
1580 GMP_add_path_to_all (path, GNUNET_NO);
1581 connection_reset_timeout (c, GNUNET_YES);
1585 path = path_duplicate (c->path);
1587 if (MESH_CONNECTION_NEW == c->state)
1588 connection_change_state (c, MESH_CONNECTION_SENT);
1590 /* Remember peers */
1591 dest_peer = GMP_get (&id[size - 1]);
1592 orig_peer = GMP_get (&id[0]);
1594 /* Is it a connection to us? */
1595 if (c->own_pos == path->length - 1)
1597 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1598 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1600 add_to_peer (c, orig_peer);
1601 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1602 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1604 send_connection_ack (c, GNUNET_NO);
1605 if (MESH_CONNECTION_SENT == c->state)
1606 connection_change_state (c, MESH_CONNECTION_ACK);
1610 /* It's for somebody else! Retransmit. */
1611 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1612 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1613 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1614 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1617 path_destroy (path);
1623 * Core handler for path confirmations.
1625 * @param cls closure
1626 * @param message message
1627 * @param peer peer identity this notification is about
1629 * @return GNUNET_OK to keep the connection open,
1630 * GNUNET_SYSERR to close it (signal serious error)
1633 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1634 const struct GNUNET_MessageHeader *message)
1636 struct GNUNET_MESH_ConnectionACK *msg;
1637 struct MeshConnection *c;
1638 struct MeshPeerPath *p;
1639 struct MeshPeer *pi;
1640 enum MeshConnectionState oldstate;
1643 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1644 log_message (message, peer, &msg->cid);
1645 c = connection_get (&msg->cid);
1648 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1650 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1654 if (GNUNET_NO != c->destroy)
1656 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1660 oldstate = c->state;
1661 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1662 pi = GMP_get (peer);
1663 if (get_next_hop (c) == pi)
1665 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1667 if (MESH_CONNECTION_SENT == oldstate)
1668 connection_change_state (c, MESH_CONNECTION_ACK);
1670 else if (get_prev_hop (c) == pi)
1672 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1674 connection_change_state (c, MESH_CONNECTION_READY);
1678 GNUNET_break_op (0);
1682 connection_reset_timeout (c, fwd);
1684 /* Add path to peers? */
1688 GMP_add_path_to_all (p, GNUNET_YES);
1695 /* Message for us as creator? */
1696 if (GMC_is_origin (c, GNUNET_YES))
1698 if (GNUNET_NO != fwd)
1700 GNUNET_break_op (0);
1703 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1705 /* If just created, cancel the short timeout and start a long one */
1706 if (MESH_CONNECTION_SENT == oldstate)
1707 connection_reset_timeout (c, GNUNET_YES);
1709 /* Change connection state */
1710 connection_change_state (c, MESH_CONNECTION_READY);
1711 send_connection_ack (c, GNUNET_YES);
1713 /* Change tunnel state, trigger KX */
1714 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1715 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1720 /* Message for us as destination? */
1721 if (GMC_is_terminal (c, GNUNET_YES))
1723 if (GNUNET_YES != fwd)
1725 GNUNET_break_op (0);
1728 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1730 /* If just created, cancel the short timeout and start a long one */
1731 if (MESH_CONNECTION_ACK == oldstate)
1732 connection_reset_timeout (c, GNUNET_NO);
1734 /* Change tunnel state */
1735 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1736 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1741 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1742 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1748 * Core handler for notifications of broken connections.
1750 * @param cls Closure (unused).
1751 * @param id Peer identity of sending neighbor.
1752 * @param message Message.
1754 * @return GNUNET_OK to keep the connection open,
1755 * GNUNET_SYSERR to close it (signal serious error)
1758 GMC_handle_broken (void* cls,
1759 const struct GNUNET_PeerIdentity* id,
1760 const struct GNUNET_MessageHeader* message)
1762 struct GNUNET_MESH_ConnectionBroken *msg;
1763 struct MeshConnection *c;
1766 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1767 log_message (message, id, &msg->cid);
1768 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1769 GNUNET_i2s (&msg->peer1));
1770 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1771 GNUNET_i2s (&msg->peer2));
1772 c = connection_get (&msg->cid);
1775 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
1779 fwd = is_fwd (c, id);
1780 if (GMC_is_terminal (c, fwd))
1782 struct GNUNET_MessageHeader *out_msg;
1783 struct MeshPeer *neighbor;
1784 struct MeshPeer *endpoint;
1786 neighbor = get_hop (c, !fwd);
1787 endpoint = GMP_get_short (c->path->peers[c->path->length - 1]);
1788 path_invalidate (c->path);
1789 GMP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
1790 c->state = MESH_CONNECTION_DESTROYED;
1791 while (NULL != (out_msg = GMP_connection_pop (neighbor, c)))
1793 GNUNET_assert (NULL ==
1794 GMT_send_prebuilt_message (out_msg, c->t, NULL, GNUNET_YES,
1802 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1803 c->destroy = GNUNET_YES;
1804 connection_cancel_queues (c, !fwd);
1813 * Core handler for tunnel destruction
1815 * @param cls Closure (unused).
1816 * @param peer Peer identity of sending neighbor.
1817 * @param message Message.
1819 * @return GNUNET_OK to keep the connection open,
1820 * GNUNET_SYSERR to close it (signal serious error)
1823 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1824 const struct GNUNET_MessageHeader *message)
1826 struct GNUNET_MESH_ConnectionDestroy *msg;
1827 struct MeshConnection *c;
1830 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1831 log_message (message, peer, &msg->cid);
1832 c = connection_get (&msg->cid);
1835 /* Probably already got the message from another path,
1836 * destroyed the tunnel and retransmitted to children.
1839 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1841 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1844 fwd = is_fwd (c, peer);
1845 if (GNUNET_SYSERR == fwd)
1847 GNUNET_break_op (0); /* FIXME */
1850 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1851 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1852 else if (0 == c->pending_messages)
1854 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1858 c->destroy = GNUNET_YES;
1859 c->state = MESH_CONNECTION_DESTROYED;
1862 GMT_remove_connection (c->t, c);
1870 * Generic handler for mesh network encrypted traffic.
1872 * @param peer Peer identity this notification is about.
1873 * @param msg Encrypted message.
1875 * @return GNUNET_OK to keep the connection open,
1876 * GNUNET_SYSERR to close it (signal serious error)
1879 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1880 const struct GNUNET_MESH_Encrypted *msg)
1882 struct MeshConnection *c;
1883 struct MeshPeer *neighbor;
1884 struct MeshFlowControl *fc;
1885 GNUNET_PEER_Id peer_id;
1891 log_message (&msg->header, peer, &msg->cid);
1894 size = ntohs (msg->header.size);
1896 sizeof (struct GNUNET_MESH_Encrypted) +
1897 sizeof (struct GNUNET_MessageHeader))
1899 GNUNET_break_op (0);
1903 /* Check connection */
1904 c = connection_get (&msg->cid);
1907 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1908 LOG (GNUNET_ERROR_TYPE_DEBUG, "enc on unknown connection %s\n",
1909 GNUNET_h2s (GM_h2hc (&msg->cid)));
1913 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s\n", GMC_2s (c));
1915 /* Check if origin is as expected */
1916 neighbor = get_prev_hop (c);
1917 peer_id = GNUNET_PEER_search (peer);
1918 if (peer_id == GMP_get_short_id (neighbor))
1924 neighbor = get_next_hop (c);
1925 if (peer_id == GMP_get_short_id (neighbor))
1931 /* Unexpected peer sending traffic on a connection. */
1932 GNUNET_break_op (0);
1938 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1939 pid = ntohl (msg->pid);
1940 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1942 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1943 LOG (GNUNET_ERROR_TYPE_DEBUG,
1944 "WARNING Received PID %u, (prev %u), ACK %u\n",
1945 pid, fc->last_pid_recv, fc->last_ack_sent);
1948 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1950 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1951 LOG (GNUNET_ERROR_TYPE_DEBUG,
1952 " Pid %u not expected (%u+), dropping!\n",
1953 pid, fc->last_pid_recv + 1);
1956 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1957 connection_change_state (c, MESH_CONNECTION_READY);
1958 connection_reset_timeout (c, fwd);
1959 fc->last_pid_recv = pid;
1961 /* Is this message for us? */
1962 if (GMC_is_terminal (c, fwd))
1964 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1965 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1969 GNUNET_break (GNUNET_NO != c->destroy);
1972 fc->last_pid_recv = pid;
1973 GMT_handle_encrypted (c->t, msg);
1974 GMC_send_ack (c, fwd, GNUNET_NO);
1978 /* Message not for us: forward to next hop */
1979 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1980 ttl = ntohl (msg->ttl);
1981 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1984 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1985 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1986 GMC_send_ack (c, fwd, GNUNET_NO);
1990 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1991 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1997 * Generic handler for mesh network encrypted traffic.
1999 * @param peer Peer identity this notification is about.
2000 * @param msg Encrypted message.
2002 * @return GNUNET_OK to keep the connection open,
2003 * GNUNET_SYSERR to close it (signal serious error)
2006 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
2007 const struct GNUNET_MESH_KX *msg)
2009 struct MeshConnection *c;
2010 struct MeshPeer *neighbor;
2011 GNUNET_PEER_Id peer_id;
2015 log_message (&msg->header, peer, &msg->cid);
2018 size = ntohs (msg->header.size);
2020 sizeof (struct GNUNET_MESH_KX) +
2021 sizeof (struct GNUNET_MessageHeader))
2023 GNUNET_break_op (0);
2027 /* Check connection */
2028 c = connection_get (&msg->cid);
2031 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2032 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx on unknown connection %s\n",
2033 GNUNET_h2s (GM_h2hc (&msg->cid)));
2036 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
2038 /* Check if origin is as expected */
2039 neighbor = get_prev_hop (c);
2040 peer_id = GNUNET_PEER_search (peer);
2041 if (peer_id == GMP_get_short_id (neighbor))
2047 neighbor = get_next_hop (c);
2048 if (peer_id == GMP_get_short_id (neighbor))
2054 /* Unexpected peer sending traffic on a connection. */
2055 GNUNET_break_op (0);
2060 /* Count as connection confirmation. */
2061 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
2063 connection_change_state (c, MESH_CONNECTION_READY);
2066 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
2067 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
2070 connection_reset_timeout (c, fwd);
2072 /* Is this message for us? */
2073 if (GMC_is_terminal (c, fwd))
2075 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2076 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2082 GMT_handle_kx (c->t, &msg[1].header);
2086 /* Message not for us: forward to next hop */
2087 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2088 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2089 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
2096 * Core handler for encrypted mesh network traffic (channel mgmt, data).
2098 * @param cls Closure (unused).
2099 * @param message Message received.
2100 * @param peer Peer who sent the message.
2102 * @return GNUNET_OK to keep the connection open,
2103 * GNUNET_SYSERR to close it (signal serious error)
2106 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2107 const struct GNUNET_MessageHeader *message)
2109 return handle_mesh_encrypted (peer,
2110 (struct GNUNET_MESH_Encrypted *)message);
2115 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2117 * @param cls Closure (unused).
2118 * @param message Message received.
2119 * @param peer Peer who sent the message.
2121 * @return GNUNET_OK to keep the connection open,
2122 * GNUNET_SYSERR to close it (signal serious error)
2125 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2126 const struct GNUNET_MessageHeader *message)
2128 return handle_mesh_kx (peer,
2129 (struct GNUNET_MESH_KX *) message);
2134 * Core handler for mesh network traffic point-to-point acks.
2136 * @param cls closure
2137 * @param message message
2138 * @param peer peer identity this notification is about
2140 * @return GNUNET_OK to keep the connection open,
2141 * GNUNET_SYSERR to close it (signal serious error)
2144 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2145 const struct GNUNET_MessageHeader *message)
2147 struct GNUNET_MESH_ACK *msg;
2148 struct MeshConnection *c;
2149 struct MeshFlowControl *fc;
2154 msg = (struct GNUNET_MESH_ACK *) message;
2155 log_message (message, peer, &msg->cid);
2156 c = connection_get (&msg->cid);
2159 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2164 /* Is this a forward or backward ACK? */
2165 id = GNUNET_PEER_search (peer);
2166 if (GMP_get_short_id (get_next_hop (c)) == id)
2168 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2172 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2174 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2180 GNUNET_break_op (0);
2184 ack = ntohl (msg->ack);
2185 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2186 ack, fc->last_ack_recv);
2187 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
2188 fc->last_ack_recv = ack;
2190 /* Cancel polling if the ACK is big enough. */
2191 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2192 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2194 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2195 GNUNET_SCHEDULER_cancel (fc->poll_task);
2196 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2197 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2200 connection_unlock_queue (c, fwd);
2207 * Core handler for mesh network traffic point-to-point ack polls.
2209 * @param cls closure
2210 * @param message message
2211 * @param peer peer identity this notification is about
2213 * @return GNUNET_OK to keep the connection open,
2214 * GNUNET_SYSERR to close it (signal serious error)
2217 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2218 const struct GNUNET_MessageHeader *message)
2220 struct GNUNET_MESH_Poll *msg;
2221 struct MeshConnection *c;
2222 struct MeshFlowControl *fc;
2227 msg = (struct GNUNET_MESH_Poll *) message;
2228 log_message (message, peer, &msg->cid);
2229 c = connection_get (&msg->cid);
2232 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2234 LOG (GNUNET_ERROR_TYPE_DEBUG,
2235 "WARNING POLL message on unknown connection %s!\n",
2236 GNUNET_h2s (GM_h2hc (&msg->cid)));
2240 /* Is this a forward or backward ACK?
2241 * Note: a poll should never be needed in a loopback case,
2242 * since there is no possiblility of packet loss there, so
2243 * this way of discerining FWD/BCK should not be a problem.
2245 id = GNUNET_PEER_search (peer);
2246 if (GMP_get_short_id (get_next_hop (c)) == id)
2248 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2251 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2253 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2258 GNUNET_break_op (0);
2262 pid = ntohl (msg->pid);
2263 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2264 fc->last_pid_recv = pid;
2265 fwd = fc == &c->bck_fc;
2266 GMC_send_ack (c, fwd, GNUNET_YES);
2273 * Send an ACK on the appropriate connection/channel, depending on
2274 * the direction and the position of the peer.
2276 * @param c Which connection to send the hop-by-hop ACK.
2277 * @param fwd Is this a fwd ACK? (will go dest->root).
2278 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2281 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2283 unsigned int buffer;
2285 LOG (GNUNET_ERROR_TYPE_DEBUG,
2286 "GMC send %s ACK on %s\n",
2287 GM_f2s (fwd), GMC_2s (c));
2295 if (GNUNET_NO != c->destroy)
2297 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2301 /* Get available buffer space */
2302 if (GMC_is_terminal (c, fwd))
2304 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2305 buffer = GMT_get_channels_buffer (c->t);
2309 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2310 buffer = GMC_get_buffer (c, fwd);
2312 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2313 if (0 == buffer && GNUNET_NO == force)
2316 /* Send available buffer space */
2317 if (GMC_is_origin (c, fwd))
2319 GNUNET_assert (NULL != c->t);
2320 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2321 GMT_unchoke_channels (c->t);
2325 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2326 send_ack (c, buffer, fwd, force);
2332 * Initialize the connections subsystem
2334 * @param c Configuration handle.
2337 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2339 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2341 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2344 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2345 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2346 GNUNET_SCHEDULER_shutdown ();
2351 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2354 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2355 "MESH", "MAX_CONNECTIONS", "MISSING");
2356 GNUNET_SCHEDULER_shutdown ();
2361 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2362 &refresh_connection_time))
2364 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2365 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2366 GNUNET_SCHEDULER_shutdown ();
2369 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2370 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2375 * Destroy each connection on shutdown.
2377 * @param cls Closure (unused).
2378 * @param key Current key code (CID, unused).
2379 * @param value Value in the hash map (connection)
2381 * @return #GNUNET_YES, because we should continue to iterate,
2384 shutdown_iterator (void *cls,
2385 const struct GNUNET_HashCode *key,
2388 struct MeshConnection *c = value;
2396 * Shut down the connections subsystem.
2401 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2402 GNUNET_CONTAINER_multihashmap_destroy (connections);
2407 struct MeshConnection *
2408 GMC_new (const struct GNUNET_MeshHash *cid,
2409 struct MeshTunnel3 *t,
2410 struct MeshPeerPath *p,
2411 unsigned int own_pos)
2413 struct MeshConnection *c;
2415 c = GNUNET_new (struct MeshConnection);
2417 GNUNET_assert (GNUNET_OK ==
2418 GNUNET_CONTAINER_multihashmap_put (connections,
2420 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2421 fc_init (&c->fwd_fc);
2422 fc_init (&c->bck_fc);
2427 GNUNET_assert (own_pos <= p->length - 1);
2428 c->own_pos = own_pos;
2431 if (GNUNET_OK != register_neighbors (c))
2435 path_invalidate (c->path);
2448 GMC_destroy (struct MeshConnection *c)
2456 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2457 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2460 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2461 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2462 &c->fwd_fc, &c->bck_fc);
2463 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2464 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2466 /* Cancel all traffic */
2467 if (NULL != c->path)
2469 connection_cancel_queues (c, GNUNET_YES);
2470 connection_cancel_queues (c, GNUNET_NO);
2471 unregister_neighbors (c);
2474 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2475 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2477 /* Cancel maintainance task (keepalive/timeout) */
2478 if (NULL != c->fwd_fc.poll_msg)
2480 GMC_cancel (c->fwd_fc.poll_msg);
2481 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2483 if (NULL != c->bck_fc.poll_msg)
2485 GMC_cancel (c->bck_fc.poll_msg);
2486 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2489 /* Delete from tunnel */
2491 GMT_remove_connection (c->t, c);
2493 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2494 path_destroy (c->path);
2495 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2496 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2497 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2498 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2499 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2501 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2502 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2504 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2506 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2507 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2510 GNUNET_break (GNUNET_YES ==
2511 GNUNET_CONTAINER_multihashmap_remove (connections,
2514 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2519 * Get the connection ID.
2521 * @param c Connection to get the ID from.
2523 * @return ID of the connection.
2525 const struct GNUNET_MeshHash *
2526 GMC_get_id (const struct MeshConnection *c)
2533 * Get the connection ID.
2535 * @param c Connection to get the ID from.
2537 * @return ID of the connection.
2539 const struct GNUNET_HashCode *
2540 GMC_get_h (const struct MeshConnection *c)
2542 return GM_h2hc (&c->id);
2547 * Get the connection path.
2549 * @param c Connection to get the path from.
2551 * @return path used by the connection.
2553 const struct MeshPeerPath *
2554 GMC_get_path (const struct MeshConnection *c)
2556 if (GNUNET_NO == c->destroy)
2563 * Get the connection state.
2565 * @param c Connection to get the state from.
2567 * @return state of the connection.
2569 enum MeshConnectionState
2570 GMC_get_state (const struct MeshConnection *c)
2576 * Get the connection tunnel.
2578 * @param c Connection to get the tunnel from.
2580 * @return tunnel of the connection.
2582 struct MeshTunnel3 *
2583 GMC_get_tunnel (const struct MeshConnection *c)
2590 * Get free buffer space in a connection.
2592 * @param c Connection.
2593 * @param fwd Is query about FWD traffic?
2595 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2598 GMC_get_buffer (struct MeshConnection *c, int fwd)
2600 struct MeshFlowControl *fc;
2602 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2604 return (fc->queue_max - fc->queue_n);
2608 * Get how many messages have we allowed to send to us from a direction.
2610 * @param c Connection.
2611 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2613 * @return last_ack_sent - last_pid_recv
2616 GMC_get_allowed (struct MeshConnection *c, int fwd)
2618 struct MeshFlowControl *fc;
2620 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2621 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2625 return (fc->last_ack_sent - fc->last_pid_recv);
2629 * Get messages queued in a connection.
2631 * @param c Connection.
2632 * @param fwd Is query about FWD traffic?
2634 * @return Number of messages queued.
2637 GMC_get_qn (struct MeshConnection *c, int fwd)
2639 struct MeshFlowControl *fc;
2641 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2648 * Allow the connection to advertise a buffer of the given size.
2650 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2651 * allowing up to last_pid_recv + buffer.
2653 * @param c Connection.
2654 * @param buffer How many more messages the connection can accept.
2655 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2658 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2660 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2661 GMC_2s (c), buffer, GM_f2s (fwd));
2662 send_ack (c, buffer, fwd, GNUNET_NO);
2667 * Notify other peers on a connection of a broken link. Mark connections
2668 * to destroy after all traffic has been sent.
2670 * @param c Connection on which there has been a disconnection.
2671 * @param peer Peer that disconnected.
2674 GMC_notify_broken (struct MeshConnection *c,
2675 struct MeshPeer *peer)
2679 LOG (GNUNET_ERROR_TYPE_DEBUG,
2680 " notify broken on %s due to %s disconnect\n",
2681 GMC_2s (c), GMP_2s (peer));
2683 fwd = peer == get_prev_hop (c);
2685 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2687 /* Local shutdown, no one to notify about this. */
2691 if (GNUNET_NO == c->destroy)
2692 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2694 /* Connection will have at least one pending message
2695 * (the one we just scheduled), so no point in checking whether to
2696 * destroy immediately. */
2697 c->destroy = GNUNET_YES;
2698 c->state = MESH_CONNECTION_DESTROYED;
2701 * Cancel all queues, if no message is left, connection will be destroyed.
2703 connection_cancel_queues (c, !fwd);
2710 * Is this peer the first one on the connection?
2712 * @param c Connection.
2713 * @param fwd Is this about fwd traffic?
2715 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2718 GMC_is_origin (struct MeshConnection *c, int fwd)
2720 if (!fwd && c->path->length - 1 == c->own_pos )
2722 if (fwd && 0 == c->own_pos)
2729 * Is this peer the last one on the connection?
2731 * @param c Connection.
2732 * @param fwd Is this about fwd traffic?
2733 * Note that the ROOT is the terminal for BCK traffic!
2735 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2738 GMC_is_terminal (struct MeshConnection *c, int fwd)
2740 return GMC_is_origin (c, !fwd);
2745 * See if we are allowed to send by the next hop in the given direction.
2747 * @param c Connection.
2748 * @param fwd Is this about fwd traffic?
2750 * @return #GNUNET_YES in case it's OK to send.
2753 GMC_is_sendable (struct MeshConnection *c, int fwd)
2755 struct MeshFlowControl *fc;
2757 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking sendability of %s traffic on %s\n",
2758 GM_f2s (fwd), GMC_2s (c));
2764 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2765 LOG (GNUNET_ERROR_TYPE_DEBUG, " last ack recv: %u, last pid sent: %u\n",
2766 fc->last_ack_recv, fc->last_pid_sent);
2767 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2773 * Sends an already built message on a connection, properly registering
2774 * all used resources.
2776 * @param message Message to send. Function makes a copy of it.
2777 * If message is not hop-by-hop, decrements TTL of copy.
2778 * @param c Connection on which this message is transmitted.
2779 * @param fwd Is this a fwd message?
2780 * @param force Force the connection to accept the message (buffer overfill).
2781 * @param cont Continuation called once message is sent. Can be NULL.
2782 * @param cont_cls Closure for @c cont.
2784 * @return Handle to cancel the message before it's sent.
2785 * NULL on error or if @c cont is NULL.
2786 * Invalid on @c cont call.
2788 struct MeshConnectionQueue *
2789 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2790 struct MeshConnection *c, int fwd, int force,
2791 GMC_sent cont, void *cont_cls)
2793 struct MeshFlowControl *fc;
2794 struct MeshConnectionQueue *q;
2800 size = ntohs (message->size);
2801 data = GNUNET_malloc (size);
2802 memcpy (data, message, size);
2803 type = ntohs (message->type);
2804 LOG (GNUNET_ERROR_TYPE_INFO, "-> %s on connection %s (%u bytes)\n",
2805 GM_m2s (type), GMC_2s (c), size);
2807 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2808 droppable = GNUNET_NO == force;
2811 struct GNUNET_MESH_Encrypted *emsg;
2812 struct GNUNET_MESH_KX *kmsg;
2813 struct GNUNET_MESH_ACK *amsg;
2814 struct GNUNET_MESH_Poll *pmsg;
2815 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2816 struct GNUNET_MESH_ConnectionBroken *bmsg;
2819 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2820 emsg = (struct GNUNET_MESH_Encrypted *) data;
2821 ttl = ntohl (emsg->ttl);
2824 GNUNET_break_op (0);
2829 emsg->ttl = htonl (ttl - 1);
2830 emsg->pid = htonl (fc->next_pid++);
2831 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2832 if (GNUNET_YES == droppable)
2835 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2836 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2837 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2841 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2843 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2845 GMC_start_poll (c, fwd);
2849 case GNUNET_MESSAGE_TYPE_MESH_KX:
2850 kmsg = (struct GNUNET_MESH_KX *) data;
2854 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2855 amsg = (struct GNUNET_MESH_ACK *) data;
2857 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2858 droppable = GNUNET_NO;
2861 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2862 pmsg = (struct GNUNET_MESH_Poll *) data;
2864 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2865 droppable = GNUNET_NO;
2868 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2869 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2873 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2874 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2878 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2881 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2882 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2891 if (fc->queue_n > fc->queue_max && droppable)
2893 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2896 LOG (GNUNET_ERROR_TYPE_DEBUG,
2897 "queue full: %u/%u\n",
2898 fc->queue_n, fc->queue_max);
2899 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2905 return NULL; /* Drop this message */
2908 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2909 c->pending_messages++;
2911 q = GNUNET_new (struct MeshConnectionQueue);
2912 q->forced = !droppable;
2913 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2917 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2923 q->cont_cls = cont_cls;
2929 * Cancel a previously sent message while it's in the queue.
2931 * ONLY can be called before the continuation given to the send function
2932 * is called. Once the continuation is called, the message is no longer in the
2935 * @param q Handle to the queue.
2938 GMC_cancel (struct MeshConnectionQueue *q)
2940 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2942 /* queue destroy calls message_sent, which calls q->cont and frees q */
2943 GMP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO);
2948 * Sends a CREATE CONNECTION message for a path to a peer.
2949 * Changes the connection and tunnel states if necessary.
2951 * @param connection Connection to create.
2954 GMC_send_create (struct MeshConnection *connection)
2956 enum MeshTunnel3CState state;
2959 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2960 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2962 LOG (GNUNET_ERROR_TYPE_INFO, "=> %s on connection %s (%u bytes)\n",
2963 GM_m2s (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE),
2964 GMC_2s (connection), size);
2965 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2966 connection, connection->pending_messages);
2967 connection->pending_messages++;
2969 connection->maintenance_q =
2970 GMP_queue_add (get_next_hop (connection), NULL,
2971 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2972 size, connection, GNUNET_YES, &message_sent, NULL);
2974 state = GMT_get_cstate (connection->t);
2975 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2976 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2977 if (MESH_CONNECTION_NEW == connection->state)
2978 connection_change_state (connection, MESH_CONNECTION_SENT);
2983 * Send a message to all peers in this connection that the connection
2984 * is no longer valid.
2986 * If some peer should not receive the message, it should be zero'ed out
2987 * before calling this function.
2989 * @param c The connection whose peers to notify.
2992 GMC_send_destroy (struct MeshConnection *c)
2994 struct GNUNET_MESH_ConnectionDestroy msg;
2996 if (GNUNET_YES == c->destroy)
2999 msg.header.size = htons (sizeof (msg));
3000 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
3002 LOG (GNUNET_ERROR_TYPE_DEBUG,
3003 " sending connection destroy for connection %s\n",
3006 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
3007 GMC_send_prebuilt_message (&msg.header, c,
3008 GNUNET_YES, GNUNET_YES, NULL, NULL);
3009 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
3010 GMC_send_prebuilt_message (&msg.header, c,
3011 GNUNET_NO, GNUNET_YES, NULL, NULL);
3012 c->destroy = GNUNET_YES;
3013 c->state = MESH_CONNECTION_DESTROYED;
3018 * @brief Start a polling timer for the connection.
3020 * When a neighbor does not accept more traffic on the connection it could be
3021 * caused by a simple congestion or by a lost ACK. Polling enables to check
3022 * for the lastest ACK status for a connection.
3024 * @param c Connection.
3025 * @param fwd Should we poll in the FWD direction?
3028 GMC_start_poll (struct MeshConnection *c, int fwd)
3030 struct MeshFlowControl *fc;
3032 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3033 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3035 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3037 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3038 fc->poll_task, fc->poll_msg);
3041 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3042 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3049 * @brief Stop polling a connection for ACKs.
3051 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3053 * @param c Connection.
3054 * @param fwd Should we stop the poll in the FWD direction?
3057 GMC_stop_poll (struct MeshConnection *c, int fwd)
3059 struct MeshFlowControl *fc;
3061 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3062 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3064 GNUNET_SCHEDULER_cancel (fc->poll_task);
3065 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3070 * Get a (static) string for a connection.
3072 * @param c Connection.
3075 GMC_2s (const struct MeshConnection *c)
3082 static char buf[128];
3084 sprintf (buf, "%s (->%s)", GNUNET_h2s (GM_h2hc (GMC_get_id(c))), GMT_2s (c->t));
3087 return GNUNET_h2s (GM_h2hc (&c->id));