2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh_connection.c
23 * @brief GNUnet MESH service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "mesh_path.h"
33 #include "mesh_protocol.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
42 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
43 GNUNET_TIME_UNIT_MINUTES,\
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct to encapsulate all the Flow Control information to a peer to which
54 * we are directly connected (on a core level).
56 struct MeshFlowControl
59 * Connection this controls.
61 struct MeshConnection *c;
64 * How many messages are in the queue on this connection.
69 * How many messages do we accept in the queue.
71 unsigned int queue_max;
79 * ID of the last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 GNUNET_SCHEDULER_TaskIdentifier poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
109 * Queued poll message, to cancel if not necessary anymore (got ACK).
111 struct MeshConnectionQueue *poll_msg;
114 * Queued poll message, to cancel if not necessary anymore (got ACK).
116 struct MeshConnectionQueue *ack_msg;
120 * Keep a record of the last messages sent on this connection.
122 struct MeshConnectionPerformance
125 * Circular buffer for storing measurements.
127 double usecsperbyte[AVG_MSGS];
130 * Running average of @c usecsperbyte.
135 * How many values of @c usecsperbyte are valid.
140 * Index of the next "free" position in @c usecsperbyte.
147 * Struct containing all information regarding a connection to a peer.
149 struct MeshConnection
152 * Tunnel this connection is part of.
154 struct MeshTunnel3 *t;
157 * Flow control information for traffic fwd.
159 struct MeshFlowControl fwd_fc;
162 * Flow control information for traffic bck.
164 struct MeshFlowControl bck_fc;
167 * Measure connection performance on the endpoint.
169 struct MeshConnectionPerformance *perf;
172 * ID of the connection.
174 struct GNUNET_HashCode id;
177 * State of the connection.
179 enum MeshConnectionState state;
182 * Path being used for the tunnel. At the origin of the connection
183 * it's a pointer to the destination's path pool, otherwise just a copy.
185 struct MeshPeerPath *path;
188 * Position of the local peer in the path.
190 unsigned int own_pos;
193 * Task to keep the used paths alive at the owner,
194 * time tunnel out on all the other peers.
196 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
199 * Task to keep the used paths alive at the destination,
200 * time tunnel out on all the other peers.
202 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
205 * Queue handle for maintainance traffic. One handle for FWD and BCK since
206 * one peer never needs to maintain both directions (no loopback connections).
208 struct MeshPeerQueue *maintenance_q;
211 * Counter to do exponential backoff when creating a connection (max 64).
213 unsigned short create_retry;
216 * Pending message count.
218 int pending_messages;
221 * Destroy flag: if true, destroy on last message.
227 * Handle for messages queued but not yet sent.
229 struct MeshConnectionQueue
232 * Peer queue handle, to cancel if necessary.
234 struct MeshPeerQueue *q;
237 * Was this a forced message? (Do not account for it)
242 * Continuation to call once sent.
247 * Closure for @c cont.
252 /******************************************************************************/
253 /******************************* GLOBALS ***********************************/
254 /******************************************************************************/
257 * Global handle to the statistics service.
259 extern struct GNUNET_STATISTICS_Handle *stats;
262 * Local peer own ID (memory efficient handle).
264 extern GNUNET_PEER_Id myid;
267 * Local peer own ID (full value).
269 extern struct GNUNET_PeerIdentity my_full_id;
272 * Connections known, indexed by cid (MeshConnection).
274 static struct GNUNET_CONTAINER_MultiHashMap *connections;
277 * How many connections are we willing to maintain.
278 * Local connections are always allowed, even if there are more connections than max.
280 static unsigned long long max_connections;
283 * How many messages *in total* are we willing to queue, divide by number of
284 * connections to get connection queue size.
286 static unsigned long long max_msgs_queue;
289 * How often to send path keepalives. Paths timeout after 4 missed.
291 static struct GNUNET_TIME_Relative refresh_connection_time;
294 * How often to send path create / ACKs.
296 static struct GNUNET_TIME_Relative create_connection_time;
299 /******************************************************************************/
300 /******************************** STATIC ***********************************/
301 /******************************************************************************/
303 #if 0 // avoid compiler warning for unused static function
305 fc_debug (struct MeshFlowControl *fc)
307 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
308 fc->last_pid_recv, fc->last_ack_sent);
309 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
310 fc->last_pid_sent, fc->last_ack_recv);
311 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
312 fc->queue_n, fc->queue_max);
316 connection_debug (struct MeshConnection *c)
320 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
324 peer2s (c->t->peer), GMC_2s (c));
325 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
326 c->state, c->pending_messages);
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
328 fc_debug (&c->fwd_fc);
329 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
330 fc_debug (&c->bck_fc);
336 * Schedule next keepalive task, taking in consideration
337 * the connection state and number of retries.
339 * @param c Connection for which to schedule the next keepalive.
340 * @param fwd Direction for the next keepalive.
343 schedule_next_keepalive (struct MeshConnection *c, int fwd);
347 * Get string description for tunnel state.
349 * @param s Tunnel state.
351 * @return String representation.
354 GMC_state2s (enum MeshConnectionState s)
358 case MESH_CONNECTION_NEW:
359 return "MESH_CONNECTION_NEW";
360 case MESH_CONNECTION_SENT:
361 return "MESH_CONNECTION_SENT";
362 case MESH_CONNECTION_ACK:
363 return "MESH_CONNECTION_ACK";
364 case MESH_CONNECTION_READY:
365 return "MESH_CONNECTION_READY";
366 case MESH_CONNECTION_DESTROYED:
367 return "MESH_CONNECTION_DESTROYED";
369 return "MESH_CONNECTION_STATE_ERROR";
375 * Initialize a Flow Control structure to the initial state.
377 * @param fc Flow Control structure to initialize.
380 fc_init (struct MeshFlowControl *fc)
383 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
384 fc->last_pid_recv = (uint32_t) -1;
385 fc->last_ack_sent = (uint32_t) 0;
386 fc->last_ack_recv = (uint32_t) 0;
387 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
388 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
390 fc->queue_max = (max_msgs_queue / max_connections) + 1;
397 * @param cid Connection ID.
399 static struct MeshConnection *
400 connection_get (const struct GNUNET_HashCode *cid)
402 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
407 connection_change_state (struct MeshConnection* c,
408 enum MeshConnectionState state)
410 LOG (GNUNET_ERROR_TYPE_DEBUG,
411 "Connection %s state was %s\n",
412 GMC_2s (c), GMC_state2s (c->state));
413 if (MESH_CONNECTION_DESTROYED == c->state)
415 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
418 LOG (GNUNET_ERROR_TYPE_DEBUG,
419 "Connection %s state is now %s\n",
420 GMC_2s (c), GMC_state2s (state));
422 if (MESH_CONNECTION_READY == state)
428 * Callback called when a queued ACK message is sent.
430 * @param cls Closure (FC).
431 * @param c Connection this message was on.
432 * @param q Queue handler this call invalidates.
433 * @param type Type of message sent.
434 * @param fwd Was this a FWD going message?
435 * @param size Size of the message.
439 struct MeshConnection *c,
440 struct MeshConnectionQueue *q,
441 uint16_t type, int fwd, size_t size)
443 struct MeshFlowControl *fc = cls;
450 * Send an ACK on the connection, informing the predecessor about
451 * the available buffer space. Should not be called in case the peer
452 * is origin (no predecessor) in the @c fwd direction.
454 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
455 * the ACK itself goes "back" (dest->root).
457 * @param c Connection on which to send the ACK.
458 * @param buffer How much space free to advertise?
459 * @param fwd Is this FWD ACK? (Going dest -> root)
460 * @param force Don't optimize out.
463 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
465 struct MeshFlowControl *next_fc;
466 struct MeshFlowControl *prev_fc;
467 struct GNUNET_MESH_ACK msg;
471 /* If origin, there is no connection to send ACKs. Wrong function! */
472 if (GMC_is_origin (c, fwd))
474 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
475 GMC_2s (c), GM_f2s (fwd));
480 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
481 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
483 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
484 GM_f2s (fwd), GMC_2s (c));
486 /* Check if we need to transmit the ACK. */
487 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
488 if (3 < delta && buffer < delta && GNUNET_NO == force)
490 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 " last pid recv: %u, last ack sent: %u\n",
493 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
497 /* Ok, ACK might be necessary, what PID to ACK? */
498 ack = prev_fc->last_pid_recv + buffer;
499 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
500 LOG (GNUNET_ERROR_TYPE_DEBUG,
501 " last pid %u, last ack %u, qmax %u, q %u\n",
502 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
503 next_fc->queue_max, next_fc->queue_n);
504 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
510 /* Check if message is already in queue */
511 if (NULL != prev_fc->ack_msg)
513 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
515 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
516 GMC_cancel (prev_fc->ack_msg);
517 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
521 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
526 prev_fc->last_ack_sent = ack;
528 /* Build ACK message and send on connection */
529 msg.header.size = htons (sizeof (msg));
530 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
531 msg.ack = htonl (ack);
534 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
541 * Callback called when a queued message is sent.
543 * Calculates the average time and connection packet tracking.
545 * @param cls Closure (ConnectionQueue Handle).
546 * @param c Connection this message was on.
547 * @param type Type of message sent.
548 * @param fwd Was this a FWD going message?
549 * @param size Size of the message.
550 * @param wait Time spent waiting for core (only the time for THIS message)
553 message_sent (void *cls,
554 struct MeshConnection *c, uint16_t type,
555 int fwd, size_t size,
556 struct GNUNET_TIME_Relative wait)
558 struct MeshConnectionPerformance *p;
559 struct MeshFlowControl *fc;
560 struct MeshConnectionQueue *q = cls;
564 fc = fwd ? &c->fwd_fc : &c->bck_fc;
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
576 q->cont (q->cont_cls, c, q, type, fwd, size);
580 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
582 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
589 c->pending_messages--;
590 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
592 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
596 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
599 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
600 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
601 c->maintenance_q = NULL;
602 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
603 if (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE == type || !fwd)
604 schedule_next_keepalive (c, fwd);
607 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
610 if (GNUNET_NO == forced)
613 LOG (GNUNET_ERROR_TYPE_DEBUG,
614 "! accounting pid %u\n",
619 LOG (GNUNET_ERROR_TYPE_DEBUG,
620 "! forced, Q_N not accounting pid %u\n",
623 GMC_send_ack (c, fwd, GNUNET_NO);
626 case GNUNET_MESSAGE_TYPE_MESH_POLL:
630 case GNUNET_MESSAGE_TYPE_MESH_ACK:
637 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
640 return; /* Only endpoints are interested in timing. */
643 usecsperbyte = ((double) wait.rel_value_us) / size;
644 if (p->size == AVG_MSGS)
646 /* Array is full. Substract oldest value, add new one and store. */
647 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
648 p->usecsperbyte[p->idx] = usecsperbyte;
649 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
653 /* Array not yet full. Add current value to avg and store. */
654 p->usecsperbyte[p->idx] = usecsperbyte;
656 p->avg += p->usecsperbyte[p->idx];
660 p->idx = (p->idx + 1) % AVG_MSGS;
665 * Get the previous hop in a connection
667 * @param c Connection.
669 * @return Previous peer in the connection.
671 static struct MeshPeer *
672 get_prev_hop (const struct MeshConnection *c)
676 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
677 GMC_2s (c), c->own_pos, c->path->length);
678 if (0 == c->own_pos || c->path->length < 2)
680 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is zero\n");
681 id = c->path->peers[0];
685 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is NOT zero\n");
686 id = c->path->peers[c->own_pos - 1];
689 LOG (GNUNET_ERROR_TYPE_DEBUG, " id: %u\n", id);
690 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s\n", GNUNET_i2s (GNUNET_PEER_resolve2 (id)));
691 return GMP_get_short (id);
696 * Get the next hop in a connection
698 * @param c Connection.
700 * @return Next peer in the connection.
702 static struct MeshPeer *
703 get_next_hop (const struct MeshConnection *c)
707 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
708 GMC_2s (c), c->own_pos, c->path->length);
709 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
711 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is end\n");
712 id = c->path->peers[c->path->length - 1];
716 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is NOT end\n");
717 id = c->path->peers[c->own_pos + 1];
720 LOG (GNUNET_ERROR_TYPE_DEBUG, " id: %u\n", id);
721 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s\n", GNUNET_i2s (GNUNET_PEER_resolve2 (id)));
723 return GMP_get_short (id);
728 * Get the hop in a connection.
730 * @param c Connection.
731 * @param fwd Next hop?
733 * @return Next peer in the connection.
735 static struct MeshPeer *
736 get_hop (struct MeshConnection *c, int fwd)
739 return get_next_hop (c);
740 return get_prev_hop (c);
745 * Is traffic coming from this sender 'FWD' traffic?
747 * @param c Connection to check.
748 * @param sender Peer identity of neighbor.
750 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
751 * the traffic is 'FWD'.
752 * #GNUNET_NO for BCK.
753 * #GNUNET_SYSERR for errors.
756 is_fwd (const struct MeshConnection *c,
757 const struct GNUNET_PeerIdentity *sender)
761 id = GNUNET_PEER_search (sender);
762 if (GMP_get_short_id (get_prev_hop (c)) == id)
765 if (GMP_get_short_id (get_next_hop (c)) == id)
769 return GNUNET_SYSERR;
774 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
775 * or a first CONNECTION_ACK directed to us.
777 * @param connection Connection to confirm.
778 * @param fwd Should we send it FWD? (root->dest)
779 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
782 send_connection_ack (struct MeshConnection *connection, int fwd)
784 struct MeshTunnel3 *t;
787 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
789 GMP_queue_add (get_hop (connection, fwd), NULL,
790 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
791 sizeof (struct GNUNET_MESH_ConnectionACK),
792 connection, fwd, &message_sent, NULL);
793 connection->pending_messages++;
794 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
795 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
796 if (MESH_CONNECTION_READY != connection->state)
797 connection_change_state (connection, MESH_CONNECTION_SENT);
802 * Send a notification that a connection is broken.
804 * @param c Connection that is broken.
805 * @param id1 Peer that has disconnected.
806 * @param id2 Peer that has disconnected.
807 * @param fwd Direction towards which to send it.
810 send_broken (struct MeshConnection *c,
811 const struct GNUNET_PeerIdentity *id1,
812 const struct GNUNET_PeerIdentity *id2,
815 struct GNUNET_MESH_ConnectionBroken msg;
817 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
818 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
822 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
827 * Send a notification that a connection is broken, when a connection
828 * isn't even created.
830 * @param connection_id Connection ID.
831 * @param id1 Peer that has disconnected.
832 * @param id2 Peer that has disconnected.
833 * @param peer Peer to notify (neighbor who sent the connection).
836 send_broken2 (struct GNUNET_HashCode *connection_id,
837 const struct GNUNET_PeerIdentity *id1,
838 const struct GNUNET_PeerIdentity *id2,
839 GNUNET_PEER_Id peer_id)
841 struct GNUNET_MESH_ConnectionBroken *msg;
842 struct MeshPeer *neighbor;
844 msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
845 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
846 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
847 msg->cid = *connection_id;
850 neighbor = GMP_get_short (peer_id);
851 GMP_queue_add (neighbor, msg,
852 GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED,
853 sizeof (struct GNUNET_MESH_ConnectionBroken),
854 NULL, GNUNET_SYSERR, /* connection, fwd */
855 NULL, NULL); /* continuation */
860 * Send keepalive packets for a connection.
862 * @param c Connection to keep alive..
863 * @param fwd Is this a FWD keepalive? (owner -> dest).
866 connection_keepalive (struct MeshConnection *c, int fwd)
868 struct GNUNET_MESH_ConnectionKeepAlive *msg;
869 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
872 LOG (GNUNET_ERROR_TYPE_DEBUG,
873 "sending %s keepalive for connection %s]\n",
874 GM_f2s (fwd), GMC_2s (c));
876 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
877 msg->header.size = htons (size);
878 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
880 msg->reserved = htonl (0);
882 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
887 * Send CONNECTION_{CREATE/ACK} packets for a connection.
889 * @param c Connection for which to send the message.
890 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
893 connection_recreate (struct MeshConnection *c, int fwd)
895 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
899 send_connection_ack (c, GNUNET_NO);
904 * Generic connection timer management.
905 * Depending on the role of the peer in the connection will send the
906 * appropriate message (build or keepalive)
908 * @param c Conncetion to maintain.
912 connection_maintain (struct MeshConnection *c, int fwd)
914 if (GNUNET_NO != c->destroy)
917 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
919 /* TODO DHT GET with RO_BART */
924 case MESH_CONNECTION_NEW:
927 case MESH_CONNECTION_SENT:
928 connection_recreate (c, fwd);
930 case MESH_CONNECTION_READY:
931 connection_keepalive (c, fwd);
940 * Keep the connection alive in the FWD direction.
942 * @param cls Closure (connection to keepalive).
943 * @param tc TaskContext.
946 connection_fwd_keepalive (void *cls,
947 const struct GNUNET_SCHEDULER_TaskContext *tc)
949 struct MeshConnection *c = cls;
951 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
952 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
955 connection_maintain (c, GNUNET_YES);
957 /* Next execution will be scheduled by message_sent */
962 * Keep the connection alive in the BCK direction.
964 * TODO refactor and merge with connection_fwd_keepalive.
966 * @param cls Closure (connection to keepalive).
967 * @param tc TaskContext.
970 connection_bck_keepalive (void *cls,
971 const struct GNUNET_SCHEDULER_TaskContext *tc)
973 struct MeshConnection *c = cls;
975 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
976 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
979 connection_maintain (c, GNUNET_NO);
981 /* Next execution will be scheduled by message_sent */
986 * Schedule next keepalive task, taking in consideration
987 * the connection state and number of retries.
989 * @param c Connection for which to schedule the next keepalive.
990 * @param fwd Direction for the next keepalive.
993 schedule_next_keepalive (struct MeshConnection *c, int fwd)
995 struct GNUNET_TIME_Relative delay;
996 GNUNET_SCHEDULER_TaskIdentifier *task_id;
997 GNUNET_SCHEDULER_Task keepalive_task;
999 /* Calculate delay to use, depending on the state of the connection */
1000 if (MESH_CONNECTION_READY == c->state)
1002 delay = refresh_connection_time;
1006 if (1 > c->create_retry)
1007 c->create_retry = 1;
1008 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1010 if (c->create_retry < 64)
1011 c->create_retry *= 2;
1014 /* Select direction-dependent parameters */
1015 if (GNUNET_YES == fwd)
1017 task_id = &c->fwd_maintenance_task;
1018 keepalive_task = &connection_fwd_keepalive;
1022 task_id = &c->bck_maintenance_task;
1023 keepalive_task = &connection_bck_keepalive;
1026 /* Check that no one scheduled it before us (and alert in that case) */
1027 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1030 GNUNET_SCHEDULER_cancel (*task_id);
1033 /* Schedule the task */
1034 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1039 * @brief Re-initiate traffic on this connection if necessary.
1041 * Check if there is traffic queued towards this peer
1042 * and the core transmit handle is NULL (traffic was stalled).
1043 * If so, call core tmt rdy.
1045 * @param c Connection on which initiate traffic.
1046 * @param fwd Is this about fwd traffic?
1049 connection_unlock_queue (struct MeshConnection *c, int fwd)
1051 struct MeshPeer *peer;
1053 LOG (GNUNET_ERROR_TYPE_DEBUG,
1054 "connection_unlock_queue %s on %s\n",
1055 GM_f2s (fwd), GMC_2s (c));
1057 if (GMC_is_terminal (c, fwd))
1059 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1063 peer = get_hop (c, fwd);
1064 GMP_queue_unlock (peer, c);
1069 * Cancel all transmissions that belong to a certain connection.
1071 * If the connection is scheduled for destruction and no more messages are left,
1072 * the connection will be destroyed by the continuation call.
1074 * @param c Connection which to cancel. Might be destroyed during this call.
1075 * @param fwd Cancel fwd traffic?
1078 connection_cancel_queues (struct MeshConnection *c, int fwd)
1080 struct MeshFlowControl *fc;
1081 struct MeshPeer *peer;
1083 LOG (GNUNET_ERROR_TYPE_DEBUG,
1084 " *** Cancel %s queues for connection %s\n",
1085 GM_f2s (fwd), GMC_2s (c));
1092 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1093 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1095 GNUNET_SCHEDULER_cancel (fc->poll_task);
1096 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1097 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1099 peer = get_hop (c, fwd);
1100 GMP_queue_cancel (peer, c);
1105 * Function called if a connection has been stalled for a while,
1106 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1108 * @param cls Closure (poll ctx).
1109 * @param tc TaskContext.
1112 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1116 * Callback called when a queued POLL message is sent.
1118 * @param cls Closure (FC).
1119 * @param c Connection this message was on.
1120 * @param q Queue handler this call invalidates.
1121 * @param type Type of message sent.
1122 * @param fwd Was this a FWD going message?
1123 * @param size Size of the message.
1126 poll_sent (void *cls,
1127 struct MeshConnection *c,
1128 struct MeshConnectionQueue *q,
1129 uint16_t type, int fwd, size_t size)
1131 struct MeshFlowControl *fc = cls;
1133 if (2 == c->destroy)
1135 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1138 LOG (GNUNET_ERROR_TYPE_DEBUG,
1139 " *** POLL sent for , scheduling new one!\n");
1140 fc->poll_msg = NULL;
1141 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1142 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1143 &connection_poll, fc);
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1149 * Function called if a connection has been stalled for a while,
1150 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1152 * @param cls Closure (poll ctx).
1153 * @param tc TaskContext.
1156 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1158 struct MeshFlowControl *fc = cls;
1159 struct GNUNET_MESH_Poll msg;
1160 struct MeshConnection *c;
1162 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1163 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1169 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1171 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1172 fc == &c->fwd_fc ? "FWD" : "BCK");
1174 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1175 msg.header.size = htons (sizeof (msg));
1176 msg.pid = htonl (fc->last_pid_sent);
1177 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1178 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1179 fc == &c->fwd_fc, GNUNET_YES,
1185 * Timeout function due to lack of keepalive/traffic from the owner.
1186 * Destroys connection if called.
1188 * @param cls Closure (connection to destroy).
1189 * @param tc TaskContext.
1192 connection_fwd_timeout (void *cls,
1193 const struct GNUNET_SCHEDULER_TaskContext *tc)
1195 struct MeshConnection *c = cls;
1197 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1198 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1203 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1214 * Timeout function due to lack of keepalive/traffic from the destination.
1215 * Destroys connection if called.
1217 * @param cls Closure (connection to destroy).
1218 * @param tc TaskContext
1221 connection_bck_timeout (void *cls,
1222 const struct GNUNET_SCHEDULER_TaskContext *tc)
1224 struct MeshConnection *c = cls;
1226 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1227 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1230 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1233 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1244 * Resets the connection timeout task, some other message has done the
1246 * - For the first peer on the direction this means to send
1247 * a keepalive or a path confirmation message (either create or ACK).
1248 * - For all other peers, this means to destroy the connection,
1249 * due to lack of activity.
1250 * Starts the timeout if no timeout was running (connection just created).
1252 * @param c Connection whose timeout to reset.
1253 * @param fwd Is this forward?
1255 * TODO use heap to improve efficiency of scheduler.
1258 connection_reset_timeout (struct MeshConnection *c, int fwd)
1260 GNUNET_SCHEDULER_TaskIdentifier *ti;
1261 GNUNET_SCHEDULER_Task f;
1263 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1265 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1267 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1268 GNUNET_SCHEDULER_cancel (*ti);
1270 if (GMC_is_origin (c, fwd)) /* Startpoint */
1272 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1273 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1275 else /* Relay, endpoint. */
1277 struct GNUNET_TIME_Relative delay;
1279 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1280 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1281 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1287 * Add the connection to the list of both neighbors.
1289 * @param c Connection.
1291 * @return #GNUNET_OK if everything went fine
1292 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1295 register_neighbors (struct MeshConnection *c)
1297 struct MeshPeer *next_peer;
1298 struct MeshPeer *prev_peer;
1300 next_peer = get_next_hop (c);
1301 prev_peer = get_prev_hop (c);
1303 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1305 path_debug (c->path);
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1307 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1308 GMC_2s (c), next_peer);
1309 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GMP_2s (next_peer));
1310 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1311 GMC_2s (c), prev_peer);
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GMP_2s (prev_peer));
1314 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1315 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1317 if (GMC_is_origin (c, GNUNET_YES))
1318 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1319 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1321 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1322 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1323 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1324 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1325 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1326 return GNUNET_SYSERR;
1329 GMP_add_connection (next_peer, c);
1330 GMP_add_connection (prev_peer, c);
1337 * Remove the connection from the list of both neighbors.
1339 * @param c Connection.
1342 unregister_neighbors (struct MeshConnection *c)
1344 struct MeshPeer *peer;
1346 peer = get_next_hop (c);
1347 if (GNUNET_OK != GMP_remove_connection (peer, c))
1349 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1350 || MESH_CONNECTION_DESTROYED == c->state);
1351 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1352 if (NULL != c->t) GMT_debug (c->t);
1355 peer = get_prev_hop (c);
1356 if (GNUNET_OK != GMP_remove_connection (peer, c))
1358 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1359 || MESH_CONNECTION_DESTROYED == c->state);
1360 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1361 if (NULL != c->t) GMT_debug (c->t);
1367 * Bind the connection to the peer and the tunnel to that peer.
1369 * If the peer has no tunnel, create one. Update tunnel and connection
1370 * data structres to reflect new status.
1372 * @param c Connection.
1376 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1378 GMP_add_tunnel (peer);
1379 c->t = GMP_get_tunnel (peer);
1380 GMT_add_connection (c->t, c);
1385 * Builds a path from a PeerIdentity array.
1387 * @param peers PeerIdentity array.
1388 * @param size Size of the @c peers array.
1389 * @param own_pos Output parameter: own position in the path.
1391 * @return Fixed and shortened path.
1393 static struct MeshPeerPath *
1394 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1396 unsigned int *own_pos)
1398 struct MeshPeerPath *path;
1399 GNUNET_PEER_Id shortid;
1402 unsigned int offset;
1405 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1406 path = path_new (size);
1409 for (i = 0; i < size; i++)
1411 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1412 i, GNUNET_i2s (&peers[i]));
1413 shortid = GNUNET_PEER_intern (&peers[i]);
1415 /* Check for loops / duplicates */
1416 for (j = 0; j < i - offset; j++)
1418 if (path->peers[j] == shortid)
1420 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1422 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset);
1423 GNUNET_PEER_change_rc (shortid, -1);
1426 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1427 path->peers[i - offset] = shortid;
1428 if (path->peers[i] == myid)
1431 path->length -= offset;
1433 if (path->peers[*own_pos] != myid)
1435 /* create path: self not found in path through self */
1436 GNUNET_break_op (0);
1437 path_destroy (path);
1444 /******************************************************************************/
1445 /******************************** API ***********************************/
1446 /******************************************************************************/
1449 * Core handler for connection creation.
1451 * @param cls Closure (unused).
1452 * @param peer Sender (neighbor).
1453 * @param message Message.
1455 * @return GNUNET_OK to keep the connection open,
1456 * GNUNET_SYSERR to close it (signal serious error)
1459 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1460 const struct GNUNET_MessageHeader *message)
1462 struct GNUNET_MESH_ConnectionCreate *msg;
1463 struct GNUNET_PeerIdentity *id;
1464 struct GNUNET_HashCode *cid;
1465 struct MeshPeerPath *path;
1466 struct MeshPeer *dest_peer;
1467 struct MeshPeer *orig_peer;
1468 struct MeshConnection *c;
1469 unsigned int own_pos;
1472 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1473 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1476 size = ntohs (message->size);
1477 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1479 GNUNET_break_op (0);
1483 /* Calculate hops */
1484 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1485 if (size % sizeof (struct GNUNET_PeerIdentity))
1487 GNUNET_break_op (0);
1490 size /= sizeof (struct GNUNET_PeerIdentity);
1493 GNUNET_break_op (0);
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1498 /* Get parameters */
1499 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1501 id = (struct GNUNET_PeerIdentity *) &msg[1];
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s (%s->).\n",
1503 GNUNET_h2s (cid), GNUNET_i2s (id));
1505 /* Create connection */
1506 c = connection_get (cid);
1509 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1515 GNUNET_break_op (0);
1516 path_destroy (path);
1519 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1520 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1521 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1524 if (path->length - 1 == own_pos)
1526 /* If we are destination, why did the creation fail? */
1530 send_broken2 (cid, &my_full_id,
1531 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1532 path->peers[own_pos - 1]);
1533 path_destroy (path);
1536 GMP_add_path_to_all (path, GNUNET_NO);
1537 connection_reset_timeout (c, GNUNET_YES);
1541 path = path_duplicate (c->path);
1543 if (MESH_CONNECTION_NEW == c->state)
1544 connection_change_state (c, MESH_CONNECTION_SENT);
1546 /* Remember peers */
1547 dest_peer = GMP_get (&id[size - 1]);
1548 orig_peer = GMP_get (&id[0]);
1550 /* Is it a connection to us? */
1551 if (c->own_pos == size - 1)
1553 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1554 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1556 add_to_peer (c, orig_peer);
1557 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1558 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1560 send_connection_ack (c, GNUNET_NO);
1561 if (MESH_CONNECTION_SENT == c->state)
1562 connection_change_state (c, MESH_CONNECTION_ACK);
1564 /* Keep tunnel alive in direction dest->owner*/
1565 if (GNUNET_SCHEDULER_NO_TASK == c->bck_maintenance_task)
1567 c->bck_maintenance_task =
1568 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1569 &connection_bck_keepalive, c);
1574 /* It's for somebody else! Retransmit. */
1575 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1576 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1577 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1578 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1581 path_destroy (path);
1587 * Core handler for path confirmations.
1589 * @param cls closure
1590 * @param message message
1591 * @param peer peer identity this notification is about
1593 * @return GNUNET_OK to keep the connection open,
1594 * GNUNET_SYSERR to close it (signal serious error)
1597 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1598 const struct GNUNET_MessageHeader *message)
1600 struct GNUNET_MESH_ConnectionACK *msg;
1601 struct MeshConnection *c;
1602 struct MeshPeerPath *p;
1603 struct MeshPeer *pi;
1604 enum MeshConnectionState oldstate;
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1608 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1609 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1610 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1611 GNUNET_h2s (&msg->cid));
1612 c = connection_get (&msg->cid);
1615 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1617 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1621 if (GNUNET_NO != c->destroy)
1623 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1627 oldstate = c->state;
1628 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1629 pi = GMP_get (peer);
1630 if (get_next_hop (c) == pi)
1632 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1634 if (MESH_CONNECTION_SENT == oldstate)
1635 connection_change_state (c, MESH_CONNECTION_ACK);
1637 else if (get_prev_hop (c) == pi)
1639 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1641 connection_change_state (c, MESH_CONNECTION_READY);
1645 GNUNET_break_op (0);
1649 connection_reset_timeout (c, fwd);
1651 /* Add path to peers? */
1655 GMP_add_path_to_all (p, GNUNET_YES);
1662 /* Message for us as creator? */
1663 if (GMC_is_origin (c, GNUNET_YES))
1665 if (GNUNET_NO != fwd)
1667 GNUNET_break_op (0);
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1672 /* If just created, cancel the short timeout and start a long one */
1673 if (MESH_CONNECTION_SENT == oldstate)
1674 connection_reset_timeout (c, GNUNET_YES);
1676 /* Change connection state */
1677 connection_change_state (c, MESH_CONNECTION_READY);
1678 send_connection_ack (c, GNUNET_YES);
1680 /* Change tunnel state, trigger KX */
1681 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1682 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1687 /* Message for us as destination? */
1688 if (GMC_is_terminal (c, GNUNET_YES))
1690 if (GNUNET_YES != fwd)
1692 GNUNET_break_op (0);
1695 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1697 /* If just created, cancel the short timeout and start a long one */
1698 if (MESH_CONNECTION_ACK == oldstate)
1699 connection_reset_timeout (c, GNUNET_NO);
1701 /* Change tunnel state */
1702 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1703 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1709 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1715 * Core handler for notifications of broken paths
1717 * @param cls Closure (unused).
1718 * @param id Peer identity of sending neighbor.
1719 * @param message Message.
1721 * @return GNUNET_OK to keep the connection open,
1722 * GNUNET_SYSERR to close it (signal serious error)
1725 GMC_handle_broken (void* cls,
1726 const struct GNUNET_PeerIdentity* id,
1727 const struct GNUNET_MessageHeader* message)
1729 struct GNUNET_MESH_ConnectionBroken *msg;
1730 struct MeshConnection *c;
1733 LOG (GNUNET_ERROR_TYPE_DEBUG,
1734 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1735 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1736 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1737 GNUNET_i2s (&msg->peer1));
1738 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1739 GNUNET_i2s (&msg->peer2));
1740 c = connection_get (&msg->cid);
1743 GNUNET_break_op (0);
1747 fwd = is_fwd (c, id);
1748 if (GMC_is_terminal (c, fwd))
1750 path_invalidate (c->path);
1751 if (0 < c->pending_messages)
1752 c->destroy = GNUNET_YES;
1758 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1759 c->destroy = GNUNET_YES;
1760 connection_cancel_queues (c, !fwd);
1769 * Core handler for tunnel destruction
1771 * @param cls Closure (unused).
1772 * @param peer Peer identity of sending neighbor.
1773 * @param message Message.
1775 * @return GNUNET_OK to keep the connection open,
1776 * GNUNET_SYSERR to close it (signal serious error)
1779 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1780 const struct GNUNET_MessageHeader *message)
1782 struct GNUNET_MESH_ConnectionDestroy *msg;
1783 struct MeshConnection *c;
1786 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1787 LOG (GNUNET_ERROR_TYPE_DEBUG,
1788 "Got a CONNECTION DESTROY message from %s\n",
1790 LOG (GNUNET_ERROR_TYPE_DEBUG,
1791 " for connection %s\n",
1792 GNUNET_h2s (&msg->cid));
1793 c = connection_get (&msg->cid);
1796 /* Probably already got the message from another path,
1797 * destroyed the tunnel and retransmitted to children.
1800 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1802 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1805 fwd = is_fwd (c, peer);
1806 if (GNUNET_SYSERR == fwd)
1808 GNUNET_break_op (0); /* FIXME */
1811 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1812 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1813 else if (0 == c->pending_messages)
1815 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1819 c->destroy = GNUNET_YES;
1820 c->state = MESH_CONNECTION_DESTROYED;
1823 GMT_remove_connection (c->t, c);
1831 * Generic handler for mesh network encrypted traffic.
1833 * @param peer Peer identity this notification is about.
1834 * @param msg Encrypted message.
1836 * @return GNUNET_OK to keep the connection open,
1837 * GNUNET_SYSERR to close it (signal serious error)
1840 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1841 const struct GNUNET_MESH_Encrypted *msg)
1843 struct MeshConnection *c;
1844 struct MeshPeer *neighbor;
1845 struct MeshFlowControl *fc;
1846 GNUNET_PEER_Id peer_id;
1854 size = ntohs (msg->header.size);
1856 sizeof (struct GNUNET_MESH_Encrypted) +
1857 sizeof (struct GNUNET_MessageHeader))
1859 GNUNET_break_op (0);
1862 type = ntohs (msg->header.type);
1863 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1864 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1865 GM_m2s (type), ntohl (msg->pid), GNUNET_i2s (peer));
1867 /* Check connection */
1868 c = connection_get (&msg->cid);
1871 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1872 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
1873 GNUNET_h2s (&msg->cid));
1877 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1879 /* Check if origin is as expected */
1880 neighbor = get_prev_hop (c);
1881 peer_id = GNUNET_PEER_search (peer);
1882 if (peer_id == GMP_get_short_id (neighbor))
1888 neighbor = get_next_hop (c);
1889 if (peer_id == GMP_get_short_id (neighbor))
1895 /* Unexpected peer sending traffic on a connection. */
1896 GNUNET_break_op (0);
1902 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1903 pid = ntohl (msg->pid);
1904 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1906 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1907 LOG (GNUNET_ERROR_TYPE_DEBUG,
1908 "WARNING Received PID %u, (prev %u), ACK %u\n",
1909 pid, fc->last_pid_recv, fc->last_ack_sent);
1912 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1914 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1915 LOG (GNUNET_ERROR_TYPE_DEBUG,
1916 " Pid %u not expected (%u+), dropping!\n",
1917 pid, fc->last_pid_recv + 1);
1920 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1921 connection_change_state (c, MESH_CONNECTION_READY);
1922 connection_reset_timeout (c, fwd);
1923 fc->last_pid_recv = pid;
1925 /* Is this message for us? */
1926 if (GMC_is_terminal (c, fwd))
1928 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1929 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1933 GNUNET_break (GNUNET_NO != c->destroy);
1936 fc->last_pid_recv = pid;
1937 GMT_handle_encrypted (c->t, msg);
1938 GMC_send_ack (c, fwd, GNUNET_NO);
1942 /* Message not for us: forward to next hop */
1943 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1944 ttl = ntohl (msg->ttl);
1945 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1948 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1949 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1950 GMC_send_ack (c, fwd, GNUNET_NO);
1954 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1955 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1961 * Generic handler for mesh network encrypted traffic.
1963 * @param peer Peer identity this notification is about.
1964 * @param msg Encrypted message.
1966 * @return GNUNET_OK to keep the connection open,
1967 * GNUNET_SYSERR to close it (signal serious error)
1970 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1971 const struct GNUNET_MESH_KX *msg)
1973 struct MeshConnection *c;
1974 struct MeshPeer *neighbor;
1975 GNUNET_PEER_Id peer_id;
1981 size = ntohs (msg->header.size);
1983 sizeof (struct GNUNET_MESH_Encrypted) +
1984 sizeof (struct GNUNET_MessageHeader))
1986 GNUNET_break_op (0);
1989 type = ntohs (msg->header.type);
1990 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1991 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1992 GM_m2s (type), GNUNET_i2s (peer));
1994 /* Check connection */
1995 c = connection_get (&msg->cid);
1998 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1999 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING kx on unknown connection %s\n",
2000 GNUNET_h2s (&msg->cid));
2003 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
2005 /* Check if origin is as expected */
2006 neighbor = get_prev_hop (c);
2007 peer_id = GNUNET_PEER_search (peer);
2008 if (peer_id == GMP_get_short_id (neighbor))
2014 neighbor = get_next_hop (c);
2015 if (peer_id == GMP_get_short_id (neighbor))
2021 /* Unexpected peer sending traffic on a connection. */
2022 GNUNET_break_op (0);
2027 /* Count as connection confirmation. */
2028 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
2030 connection_change_state (c, MESH_CONNECTION_READY);
2033 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
2034 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
2037 connection_reset_timeout (c, fwd);
2039 /* Is this message for us? */
2040 if (GMC_is_terminal (c, fwd))
2042 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2043 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2049 GMT_handle_kx (c->t, &msg[1].header);
2053 /* Message not for us: forward to next hop */
2054 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2055 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2056 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
2063 * Core handler for encrypted mesh network traffic (channel mgmt, data).
2065 * @param cls Closure (unused).
2066 * @param message Message received.
2067 * @param peer Peer who sent the message.
2069 * @return GNUNET_OK to keep the connection open,
2070 * GNUNET_SYSERR to close it (signal serious error)
2073 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2074 const struct GNUNET_MessageHeader *message)
2076 return handle_mesh_encrypted (peer,
2077 (struct GNUNET_MESH_Encrypted *)message);
2082 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2084 * @param cls Closure (unused).
2085 * @param message Message received.
2086 * @param peer Peer who sent the message.
2088 * @return GNUNET_OK to keep the connection open,
2089 * GNUNET_SYSERR to close it (signal serious error)
2092 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2093 const struct GNUNET_MessageHeader *message)
2095 return handle_mesh_kx (peer,
2096 (struct GNUNET_MESH_KX *) message);
2101 * Core handler for mesh network traffic point-to-point acks.
2103 * @param cls closure
2104 * @param message message
2105 * @param peer peer identity this notification is about
2107 * @return GNUNET_OK to keep the connection open,
2108 * GNUNET_SYSERR to close it (signal serious error)
2111 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2112 const struct GNUNET_MessageHeader *message)
2114 struct GNUNET_MESH_ACK *msg;
2115 struct MeshConnection *c;
2116 struct MeshFlowControl *fc;
2121 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2122 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
2124 msg = (struct GNUNET_MESH_ACK *) message;
2126 c = connection_get (&msg->cid);
2130 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2135 /* Is this a forward or backward ACK? */
2136 id = GNUNET_PEER_search (peer);
2137 if (GMP_get_short_id (get_next_hop (c)) == id)
2139 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2143 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2145 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2151 GNUNET_break_op (0);
2155 ack = ntohl (msg->ack);
2156 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2157 ack, fc->last_ack_recv);
2158 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
2159 fc->last_ack_recv = ack;
2161 /* Cancel polling if the ACK is big enough. */
2162 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2163 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2165 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2166 GNUNET_SCHEDULER_cancel (fc->poll_task);
2167 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2168 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2171 connection_unlock_queue (c, fwd);
2178 * Core handler for mesh network traffic point-to-point ack polls.
2180 * @param cls closure
2181 * @param message message
2182 * @param peer peer identity this notification is about
2184 * @return GNUNET_OK to keep the connection open,
2185 * GNUNET_SYSERR to close it (signal serious error)
2188 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2189 const struct GNUNET_MessageHeader *message)
2191 struct GNUNET_MESH_Poll *msg;
2192 struct MeshConnection *c;
2193 struct MeshFlowControl *fc;
2198 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2199 LOG (GNUNET_ERROR_TYPE_DEBUG,
2200 "Got a POLL message from %s!\n",
2203 msg = (struct GNUNET_MESH_Poll *) message;
2205 c = connection_get (&msg->cid);
2209 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2211 LOG (GNUNET_ERROR_TYPE_DEBUG,
2212 "WARNING POLL message on unknown connection %s!\n",
2213 GNUNET_h2s (&msg->cid));
2217 /* Is this a forward or backward ACK?
2218 * Note: a poll should never be needed in a loopback case,
2219 * since there is no possiblility of packet loss there, so
2220 * this way of discerining FWD/BCK should not be a problem.
2222 id = GNUNET_PEER_search (peer);
2223 if (GMP_get_short_id (get_next_hop (c)) == id)
2225 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2228 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2230 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2235 GNUNET_break_op (0);
2239 pid = ntohl (msg->pid);
2240 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2241 fc->last_pid_recv = pid;
2242 fwd = fc == &c->bck_fc;
2243 GMC_send_ack (c, fwd, GNUNET_YES);
2250 * Core handler for mesh keepalives.
2252 * @param cls closure
2253 * @param message message
2254 * @param peer peer identity this notification is about
2255 * @return GNUNET_OK to keep the connection open,
2256 * GNUNET_SYSERR to close it (signal serious error)
2258 * TODO: Check who we got this from, to validate route.
2261 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2262 const struct GNUNET_MessageHeader *message)
2264 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2265 struct MeshConnection *c;
2266 struct MeshPeer *neighbor;
2267 GNUNET_PEER_Id peer_id;
2270 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2271 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
2274 c = connection_get (&msg->cid);
2277 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2282 /* Check if origin is as expected TODO refactor and reuse */
2283 peer_id = GNUNET_PEER_search (peer);
2284 neighbor = get_prev_hop (c);
2285 if (peer_id == GMP_get_short_id (neighbor))
2291 neighbor = get_next_hop (c);
2292 if (peer_id == GMP_get_short_id (neighbor))
2298 GNUNET_break_op (0);
2303 connection_change_state (c, MESH_CONNECTION_READY);
2304 connection_reset_timeout (c, fwd);
2306 if (GMC_is_terminal (c, fwd))
2309 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2310 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2317 * Send an ACK on the appropriate connection/channel, depending on
2318 * the direction and the position of the peer.
2320 * @param c Which connection to send the hop-by-hop ACK.
2321 * @param fwd Is this a fwd ACK? (will go dest->root).
2322 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2325 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2327 unsigned int buffer;
2329 LOG (GNUNET_ERROR_TYPE_DEBUG,
2330 "GMC send %s ACK on %s\n",
2331 GM_f2s (fwd), GMC_2s (c));
2339 if (GNUNET_NO != c->destroy)
2341 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2345 /* Get available buffer space */
2346 if (GMC_is_terminal (c, fwd))
2348 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2349 buffer = GMT_get_channels_buffer (c->t);
2353 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2354 buffer = GMC_get_buffer (c, fwd);
2356 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2357 if (0 == buffer && GNUNET_NO == force)
2360 /* Send available buffer space */
2361 if (GMC_is_origin (c, fwd))
2363 GNUNET_assert (NULL != c->t);
2364 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2365 GMT_unchoke_channels (c->t);
2369 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2370 send_ack (c, buffer, fwd, force);
2376 * Initialize the connections subsystem
2378 * @param c Configuration handle.
2381 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2383 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2385 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2388 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2389 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2390 GNUNET_SCHEDULER_shutdown ();
2395 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2398 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2399 "MESH", "MAX_CONNECTIONS", "MISSING");
2400 GNUNET_SCHEDULER_shutdown ();
2405 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2406 &refresh_connection_time))
2408 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2409 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2410 GNUNET_SCHEDULER_shutdown ();
2413 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2414 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2419 * Destroy each connection on shutdown.
2421 * @param cls Closure (unused).
2422 * @param key Current key code (CID, unused).
2423 * @param value Value in the hash map (connection)
2425 * @return #GNUNET_YES, because we should continue to iterate,
2428 shutdown_iterator (void *cls,
2429 const struct GNUNET_HashCode *key,
2432 struct MeshConnection *c = value;
2440 * Shut down the connections subsystem.
2445 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2446 GNUNET_CONTAINER_multihashmap_destroy (connections);
2451 struct MeshConnection *
2452 GMC_new (const struct GNUNET_HashCode *cid,
2453 struct MeshTunnel3 *t,
2454 struct MeshPeerPath *p,
2455 unsigned int own_pos)
2457 struct MeshConnection *c;
2459 c = GNUNET_new (struct MeshConnection);
2461 GNUNET_assert (GNUNET_OK ==
2462 GNUNET_CONTAINER_multihashmap_put (connections,
2464 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2465 fc_init (&c->fwd_fc);
2466 fc_init (&c->bck_fc);
2471 GNUNET_assert (own_pos <= p->length - 1);
2472 c->own_pos = own_pos;
2475 if (GNUNET_OK != register_neighbors (c))
2479 path_invalidate (c->path);
2492 GMC_destroy (struct MeshConnection *c)
2500 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2501 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2504 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2505 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2506 &c->fwd_fc, &c->bck_fc);
2507 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2508 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2510 /* Cancel all traffic */
2511 if (NULL != c->path)
2513 connection_cancel_queues (c, GNUNET_YES);
2514 connection_cancel_queues (c, GNUNET_NO);
2515 unregister_neighbors (c);
2518 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2519 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2521 /* Cancel maintainance task (keepalive/timeout) */
2522 if (NULL != c->fwd_fc.poll_msg)
2524 GMC_cancel (c->fwd_fc.poll_msg);
2525 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2527 if (NULL != c->bck_fc.poll_msg)
2529 GMC_cancel (c->bck_fc.poll_msg);
2530 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2533 /* Delete from tunnel */
2535 GMT_remove_connection (c->t, c);
2537 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2538 path_destroy (c->path);
2539 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2540 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2541 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2542 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2543 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2545 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2546 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2548 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2550 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2551 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2554 GNUNET_break (GNUNET_YES ==
2555 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2557 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2562 * Get the connection ID.
2564 * @param c Connection to get the ID from.
2566 * @return ID of the connection.
2568 const struct GNUNET_HashCode *
2569 GMC_get_id (const struct MeshConnection *c)
2576 * Get the connection path.
2578 * @param c Connection to get the path from.
2580 * @return path used by the connection.
2582 const struct MeshPeerPath *
2583 GMC_get_path (const struct MeshConnection *c)
2585 if (GNUNET_NO == c->destroy)
2592 * Get the connection state.
2594 * @param c Connection to get the state from.
2596 * @return state of the connection.
2598 enum MeshConnectionState
2599 GMC_get_state (const struct MeshConnection *c)
2605 * Get the connection tunnel.
2607 * @param c Connection to get the tunnel from.
2609 * @return tunnel of the connection.
2611 struct MeshTunnel3 *
2612 GMC_get_tunnel (const struct MeshConnection *c)
2619 * Get free buffer space in a connection.
2621 * @param c Connection.
2622 * @param fwd Is query about FWD traffic?
2624 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2627 GMC_get_buffer (struct MeshConnection *c, int fwd)
2629 struct MeshFlowControl *fc;
2631 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2633 return (fc->queue_max - fc->queue_n);
2637 * Get how many messages have we allowed to send to us from a direction.
2639 * @param c Connection.
2640 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2642 * @return last_ack_sent - last_pid_recv
2645 GMC_get_allowed (struct MeshConnection *c, int fwd)
2647 struct MeshFlowControl *fc;
2649 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2650 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2654 return (fc->last_ack_sent - fc->last_pid_recv);
2658 * Get messages queued in a connection.
2660 * @param c Connection.
2661 * @param fwd Is query about FWD traffic?
2663 * @return Number of messages queued.
2666 GMC_get_qn (struct MeshConnection *c, int fwd)
2668 struct MeshFlowControl *fc;
2670 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2677 * Allow the connection to advertise a buffer of the given size.
2679 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2680 * allowing up to last_pid_recv + buffer.
2682 * @param c Connection.
2683 * @param buffer How many more messages the connection can accept.
2684 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2687 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2689 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2690 GMC_2s (c), buffer, GM_f2s (fwd));
2691 send_ack (c, buffer, fwd, GNUNET_NO);
2696 * Notify other peers on a connection of a broken link. Mark connections
2697 * to destroy after all traffic has been sent.
2699 * @param c Connection on which there has been a disconnection.
2700 * @param peer Peer that disconnected.
2703 GMC_notify_broken (struct MeshConnection *c,
2704 struct MeshPeer *peer)
2708 LOG (GNUNET_ERROR_TYPE_DEBUG,
2709 " notify broken on %s due to %s disconnect\n",
2710 GMC_2s (c), GMP_2s (peer));
2712 fwd = peer == get_prev_hop (c);
2714 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2716 /* Local shutdown, no one to notify about this. */
2720 if (GNUNET_NO == c->destroy)
2721 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2723 /* Connection will have at least one pending message
2724 * (the one we just scheduled), so no point in checking whether to
2725 * destroy immediately. */
2726 c->destroy = GNUNET_YES;
2727 c->state = MESH_CONNECTION_DESTROYED;
2730 * Cancel all queues, if no message is left, connection will be destroyed.
2732 connection_cancel_queues (c, !fwd);
2739 * Is this peer the first one on the connection?
2741 * @param c Connection.
2742 * @param fwd Is this about fwd traffic?
2744 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2747 GMC_is_origin (struct MeshConnection *c, int fwd)
2749 if (!fwd && c->path->length - 1 == c->own_pos )
2751 if (fwd && 0 == c->own_pos)
2758 * Is this peer the last one on the connection?
2760 * @param c Connection.
2761 * @param fwd Is this about fwd traffic?
2762 * Note that the ROOT is the terminal for BCK traffic!
2764 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2767 GMC_is_terminal (struct MeshConnection *c, int fwd)
2769 return GMC_is_origin (c, !fwd);
2774 * See if we are allowed to send by the next hop in the given direction.
2776 * @param c Connection.
2777 * @param fwd Is this about fwd traffic?
2779 * @return #GNUNET_YES in case it's OK to send.
2782 GMC_is_sendable (struct MeshConnection *c, int fwd)
2784 struct MeshFlowControl *fc;
2791 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2792 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2798 * Sends an already built message on a connection, properly registering
2799 * all used resources.
2801 * @param message Message to send. Function makes a copy of it.
2802 * If message is not hop-by-hop, decrements TTL of copy.
2803 * @param c Connection on which this message is transmitted.
2804 * @param fwd Is this a fwd message?
2805 * @param force Force the connection to accept the message (buffer overfill).
2806 * @param cont Continuation called once message is sent. Can be NULL.
2807 * @param cont_cls Closure for @c cont.
2809 * @return Handle to cancel the message before it's sent.
2810 * NULL on error or if @c cont is NULL.
2811 * Invalid on @c cont call.
2813 struct MeshConnectionQueue *
2814 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2815 struct MeshConnection *c, int fwd, int force,
2816 GMC_sent cont, void *cont_cls)
2818 struct MeshFlowControl *fc;
2819 struct MeshConnectionQueue *q;
2825 size = ntohs (message->size);
2826 data = GNUNET_malloc (size);
2827 memcpy (data, message, size);
2828 type = ntohs (message->type);
2829 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2830 GM_m2s (type), size, GMC_2s (c));
2832 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2833 droppable = GNUNET_NO == force;
2836 struct GNUNET_MESH_Encrypted *emsg;
2837 struct GNUNET_MESH_KX *kmsg;
2838 struct GNUNET_MESH_ACK *amsg;
2839 struct GNUNET_MESH_Poll *pmsg;
2840 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2841 struct GNUNET_MESH_ConnectionBroken *bmsg;
2844 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2845 emsg = (struct GNUNET_MESH_Encrypted *) data;
2846 ttl = ntohl (emsg->ttl);
2849 GNUNET_break_op (0);
2854 emsg->ttl = htonl (ttl - 1);
2855 emsg->pid = htonl (fc->next_pid++);
2856 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2857 if (GNUNET_YES == droppable)
2860 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2861 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2862 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2866 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2868 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2870 GMC_start_poll (c, fwd);
2874 case GNUNET_MESSAGE_TYPE_MESH_KX:
2875 kmsg = (struct GNUNET_MESH_KX *) data;
2879 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2880 amsg = (struct GNUNET_MESH_ACK *) data;
2882 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2883 droppable = GNUNET_NO;
2886 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2887 pmsg = (struct GNUNET_MESH_Poll *) data;
2889 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2890 droppable = GNUNET_NO;
2893 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2894 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2899 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2900 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2905 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2906 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2907 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2916 if (fc->queue_n > fc->queue_max && droppable)
2918 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2921 LOG (GNUNET_ERROR_TYPE_DEBUG,
2922 "queue full: %u/%u\n",
2923 fc->queue_n, fc->queue_max);
2924 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2930 return NULL; /* Drop this message */
2933 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2934 c->pending_messages++;
2936 q = GNUNET_new (struct MeshConnectionQueue);
2937 q->forced = !droppable;
2938 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2942 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2948 q->cont_cls = cont_cls;
2954 * Cancel a previously sent message while it's in the queue.
2956 * ONLY can be called before the continuation given to the send function
2957 * is called. Once the continuation is called, the message is no longer in the
2960 * @param q Handle to the queue.
2963 GMC_cancel (struct MeshConnectionQueue *q)
2965 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2967 /* queue destroy calls message_sent, which calls q->cont and frees q */
2968 GMP_queue_destroy (q->q, GNUNET_YES);
2973 * Sends a CREATE CONNECTION message for a path to a peer.
2974 * Changes the connection and tunnel states if necessary.
2976 * @param connection Connection to create.
2979 GMC_send_create (struct MeshConnection *connection)
2981 enum MeshTunnel3CState state;
2984 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2985 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2987 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2988 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2989 connection, connection->pending_messages);
2990 connection->pending_messages++;
2992 connection->maintenance_q =
2993 GMP_queue_add (get_next_hop (connection), NULL,
2994 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2995 size, connection, GNUNET_YES, &message_sent, NULL);
2997 state = GMT_get_cstate (connection->t);
2998 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2999 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
3000 if (MESH_CONNECTION_NEW == connection->state)
3001 connection_change_state (connection, MESH_CONNECTION_SENT);
3006 * Send a message to all peers in this connection that the connection
3007 * is no longer valid.
3009 * If some peer should not receive the message, it should be zero'ed out
3010 * before calling this function.
3012 * @param c The connection whose peers to notify.
3015 GMC_send_destroy (struct MeshConnection *c)
3017 struct GNUNET_MESH_ConnectionDestroy msg;
3019 if (GNUNET_YES == c->destroy)
3022 msg.header.size = htons (sizeof (msg));
3023 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
3025 LOG (GNUNET_ERROR_TYPE_DEBUG,
3026 " sending connection destroy for connection %s\n",
3029 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
3030 GMC_send_prebuilt_message (&msg.header, c,
3031 GNUNET_YES, GNUNET_YES, NULL, NULL);
3032 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
3033 GMC_send_prebuilt_message (&msg.header, c,
3034 GNUNET_NO, GNUNET_YES, NULL, NULL);
3035 c->destroy = GNUNET_YES;
3036 c->state = MESH_CONNECTION_DESTROYED;
3041 * @brief Start a polling timer for the connection.
3043 * When a neighbor does not accept more traffic on the connection it could be
3044 * caused by a simple congestion or by a lost ACK. Polling enables to check
3045 * for the lastest ACK status for a connection.
3047 * @param c Connection.
3048 * @param fwd Should we poll in the FWD direction?
3051 GMC_start_poll (struct MeshConnection *c, int fwd)
3053 struct MeshFlowControl *fc;
3055 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3056 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3058 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3060 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3061 fc->poll_task, fc->poll_msg);
3064 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3065 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3072 * @brief Stop polling a connection for ACKs.
3074 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3076 * @param c Connection.
3077 * @param fwd Should we stop the poll in the FWD direction?
3080 GMC_stop_poll (struct MeshConnection *c, int fwd)
3082 struct MeshFlowControl *fc;
3084 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3085 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3087 GNUNET_SCHEDULER_cancel (fc->poll_task);
3088 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3093 * Get a (static) string for a connection.
3095 * @param c Connection.
3098 GMC_2s (const struct MeshConnection *c)
3105 static char buf[128];
3107 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
3110 return GNUNET_h2s (&c->id);