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)
679 id = c->path->peers[0];
681 id = c->path->peers[c->own_pos - 1];
683 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
684 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
686 return GMP_get_short (id);
691 * Get the next hop in a connection
693 * @param c Connection.
695 * @return Next peer in the connection.
697 static struct MeshPeer *
698 get_next_hop (const struct MeshConnection *c)
702 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
703 GMC_2s (c), c->own_pos, c->path->length);
704 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
705 id = c->path->peers[c->path->length - 1];
707 id = c->path->peers[c->own_pos + 1];
709 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
710 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
712 return GMP_get_short (id);
717 * Get the hop in a connection.
719 * @param c Connection.
720 * @param fwd Next hop?
722 * @return Next peer in the connection.
724 static struct MeshPeer *
725 get_hop (struct MeshConnection *c, int fwd)
728 return get_next_hop (c);
729 return get_prev_hop (c);
734 * Is traffic coming from this sender 'FWD' traffic?
736 * @param c Connection to check.
737 * @param sender Peer identity of neighbor.
739 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
740 * the traffic is 'FWD'.
741 * #GNUNET_NO for BCK.
742 * #GNUNET_SYSERR for errors.
745 is_fwd (const struct MeshConnection *c,
746 const struct GNUNET_PeerIdentity *sender)
750 id = GNUNET_PEER_search (sender);
751 if (GMP_get_short_id (get_prev_hop (c)) == id)
754 if (GMP_get_short_id (get_next_hop (c)) == id)
758 return GNUNET_SYSERR;
763 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
764 * or a first CONNECTION_ACK directed to us.
766 * @param connection Connection to confirm.
767 * @param fwd Should we send it FWD? (root->dest)
768 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
771 send_connection_ack (struct MeshConnection *connection, int fwd)
773 struct MeshTunnel3 *t;
776 LOG (GNUNET_ERROR_TYPE_INFO, "=> {%18s ACK} on connection %s\n",
777 GM_f2s (!fwd), GMC_2s (connection));
778 GMP_queue_add (get_hop (connection, fwd), NULL,
779 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
780 sizeof (struct GNUNET_MESH_ConnectionACK),
781 connection, fwd, &message_sent, NULL);
782 connection->pending_messages++;
783 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
784 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
785 if (MESH_CONNECTION_READY != connection->state)
786 connection_change_state (connection, MESH_CONNECTION_SENT);
791 * Send a notification that a connection is broken.
793 * @param c Connection that is broken.
794 * @param id1 Peer that has disconnected.
795 * @param id2 Peer that has disconnected.
796 * @param fwd Direction towards which to send it.
799 send_broken (struct MeshConnection *c,
800 const struct GNUNET_PeerIdentity *id1,
801 const struct GNUNET_PeerIdentity *id2,
804 struct GNUNET_MESH_ConnectionBroken msg;
806 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
807 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
811 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
816 * Send a notification that a connection is broken, when a connection
817 * isn't even created.
819 * @param connection_id Connection ID.
820 * @param id1 Peer that has disconnected.
821 * @param id2 Peer that has disconnected.
822 * @param peer Peer to notify (neighbor who sent the connection).
825 send_broken2 (struct GNUNET_HashCode *connection_id,
826 const struct GNUNET_PeerIdentity *id1,
827 const struct GNUNET_PeerIdentity *id2,
828 GNUNET_PEER_Id peer_id)
830 struct GNUNET_MESH_ConnectionBroken *msg;
831 struct MeshPeer *neighbor;
833 LOG (GNUNET_ERROR_TYPE_INFO,
834 "Send BROKEN on unknown connection %s\n", GNUNET_h2s (connection_id));
836 msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
837 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
838 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
839 msg->cid = *connection_id;
842 neighbor = GMP_get_short (peer_id);
843 GMP_queue_add (neighbor, msg,
844 GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED,
845 sizeof (struct GNUNET_MESH_ConnectionBroken),
846 NULL, GNUNET_SYSERR, /* connection, fwd */
847 NULL, NULL); /* continuation */
852 * Send keepalive packets for a connection.
854 * @param c Connection to keep alive..
855 * @param fwd Is this a FWD keepalive? (owner -> dest).
858 send_connection_keepalive (struct MeshConnection *c, int fwd)
860 struct GNUNET_MessageHeader msg;
862 LOG (GNUNET_ERROR_TYPE_INFO,
863 "keepalive %s for connection %s\n",
864 GM_f2s (fwd), GMC_2s (c));
866 GNUNET_assert (NULL != c->t);
867 msg.size = htons (sizeof (msg));
868 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
870 GNUNET_assert (NULL ==
871 GMT_send_prebuilt_message (&msg, c->t, c,
872 GNUNET_NO, NULL, NULL));
877 * Send CONNECTION_{CREATE/ACK} packets for a connection.
879 * @param c Connection for which to send the message.
880 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
883 connection_recreate (struct MeshConnection *c, int fwd)
885 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
889 send_connection_ack (c, GNUNET_NO);
894 * Generic connection timer management.
895 * Depending on the role of the peer in the connection will send the
896 * appropriate message (build or keepalive)
898 * @param c Conncetion to maintain.
902 connection_maintain (struct MeshConnection *c, int fwd)
904 if (GNUNET_NO != c->destroy)
907 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
909 /* TODO DHT GET with RO_BART */
914 case MESH_CONNECTION_NEW:
917 case MESH_CONNECTION_SENT:
918 connection_recreate (c, fwd);
920 case MESH_CONNECTION_READY:
921 send_connection_keepalive (c, fwd);
931 * Keep the connection alive.
933 * @param c Connection to keep alive.
934 * @param fwd Direction.
935 * @param shutdown Are we shutting down? (Don't send traffic)
936 * Non-zero value for true, not necessarily GNUNET_YES.
939 connection_keepalive (struct MeshConnection *c, int fwd, int shutdown)
941 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
942 GM_f2s (fwd), GMC_2s (c));
945 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
947 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
949 if (GNUNET_NO != shutdown)
952 connection_maintain (c, fwd);
954 /* Next execution will be scheduled by message_sent */
959 * Keep the connection alive in the FWD direction.
961 * @param cls Closure (connection to keepalive).
962 * @param tc TaskContext.
965 connection_fwd_keepalive (void *cls,
966 const struct GNUNET_SCHEDULER_TaskContext *tc)
968 connection_keepalive ((struct MeshConnection *) cls,
970 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
975 * Keep the connection alive in the BCK direction.
977 * @param cls Closure (connection to keepalive).
978 * @param tc TaskContext.
981 connection_bck_keepalive (void *cls,
982 const struct GNUNET_SCHEDULER_TaskContext *tc)
984 connection_keepalive ((struct MeshConnection *) cls,
986 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
991 * Schedule next keepalive task, taking in consideration
992 * the connection state and number of retries.
994 * If the peer is not the origin, do nothing.
996 * @param c Connection for which to schedule the next keepalive.
997 * @param fwd Direction for the next keepalive.
1000 schedule_next_keepalive (struct MeshConnection *c, int fwd)
1002 struct GNUNET_TIME_Relative delay;
1003 GNUNET_SCHEDULER_TaskIdentifier *task_id;
1004 GNUNET_SCHEDULER_Task keepalive_task;
1006 if (GNUNET_NO == GMC_is_origin (c, fwd))
1009 /* Calculate delay to use, depending on the state of the connection */
1010 if (MESH_CONNECTION_READY == c->state)
1012 delay = refresh_connection_time;
1016 if (1 > c->create_retry)
1017 c->create_retry = 1;
1018 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1020 if (c->create_retry < 64)
1021 c->create_retry *= 2;
1024 /* Select direction-dependent parameters */
1025 if (GNUNET_YES == fwd)
1027 task_id = &c->fwd_maintenance_task;
1028 keepalive_task = &connection_fwd_keepalive;
1032 task_id = &c->bck_maintenance_task;
1033 keepalive_task = &connection_bck_keepalive;
1036 /* Check that no one scheduled it before us */
1037 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1039 /* No need for a _break. It can happen for instance when sending a SYNACK
1040 * for a duplicate SYN: the first SYNACK scheduled the task. */
1041 GNUNET_SCHEDULER_cancel (*task_id);
1044 /* Schedule the task */
1045 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1050 * @brief Re-initiate traffic on this connection if necessary.
1052 * Check if there is traffic queued towards this peer
1053 * and the core transmit handle is NULL (traffic was stalled).
1054 * If so, call core tmt rdy.
1056 * @param c Connection on which initiate traffic.
1057 * @param fwd Is this about fwd traffic?
1060 connection_unlock_queue (struct MeshConnection *c, int fwd)
1062 struct MeshPeer *peer;
1064 LOG (GNUNET_ERROR_TYPE_DEBUG,
1065 "connection_unlock_queue %s on %s\n",
1066 GM_f2s (fwd), GMC_2s (c));
1068 if (GMC_is_terminal (c, fwd))
1070 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1074 peer = get_hop (c, fwd);
1075 GMP_queue_unlock (peer, c);
1080 * Cancel all transmissions that belong to a certain connection.
1082 * If the connection is scheduled for destruction and no more messages are left,
1083 * the connection will be destroyed by the continuation call.
1085 * @param c Connection which to cancel. Might be destroyed during this call.
1086 * @param fwd Cancel fwd traffic?
1089 connection_cancel_queues (struct MeshConnection *c, int fwd)
1091 struct MeshFlowControl *fc;
1092 struct MeshPeer *peer;
1094 LOG (GNUNET_ERROR_TYPE_DEBUG,
1095 " *** Cancel %s queues for connection %s\n",
1096 GM_f2s (fwd), GMC_2s (c));
1103 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1104 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1106 GNUNET_SCHEDULER_cancel (fc->poll_task);
1107 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1108 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1110 peer = get_hop (c, fwd);
1111 GMP_queue_cancel (peer, c);
1116 * Function called if a connection has been stalled for a while,
1117 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1119 * @param cls Closure (poll ctx).
1120 * @param tc TaskContext.
1123 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1127 * Callback called when a queued POLL message is sent.
1129 * @param cls Closure (FC).
1130 * @param c Connection this message was on.
1131 * @param q Queue handler this call invalidates.
1132 * @param type Type of message sent.
1133 * @param fwd Was this a FWD going message?
1134 * @param size Size of the message.
1137 poll_sent (void *cls,
1138 struct MeshConnection *c,
1139 struct MeshConnectionQueue *q,
1140 uint16_t type, int fwd, size_t size)
1142 struct MeshFlowControl *fc = cls;
1144 if (2 == c->destroy)
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1149 LOG (GNUNET_ERROR_TYPE_DEBUG,
1150 " *** POLL sent for , scheduling new one!\n");
1151 fc->poll_msg = NULL;
1152 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1153 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1154 &connection_poll, fc);
1155 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1160 * Function called if a connection has been stalled for a while,
1161 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1163 * @param cls Closure (poll ctx).
1164 * @param tc TaskContext.
1167 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1169 struct MeshFlowControl *fc = cls;
1170 struct GNUNET_MESH_Poll msg;
1171 struct MeshConnection *c;
1173 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1174 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1180 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1183 fc == &c->fwd_fc ? "FWD" : "BCK");
1185 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1186 msg.header.size = htons (sizeof (msg));
1187 msg.pid = htonl (fc->last_pid_sent);
1188 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1189 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1190 fc == &c->fwd_fc, GNUNET_YES,
1196 * Timeout function due to lack of keepalive/traffic from the owner.
1197 * Destroys connection if called.
1199 * @param cls Closure (connection to destroy).
1200 * @param tc TaskContext.
1203 connection_fwd_timeout (void *cls,
1204 const struct GNUNET_SCHEDULER_TaskContext *tc)
1206 struct MeshConnection *c = cls;
1208 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1209 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1212 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1214 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1225 * Timeout function due to lack of keepalive/traffic from the destination.
1226 * Destroys connection if called.
1228 * @param cls Closure (connection to destroy).
1229 * @param tc TaskContext
1232 connection_bck_timeout (void *cls,
1233 const struct GNUNET_SCHEDULER_TaskContext *tc)
1235 struct MeshConnection *c = cls;
1237 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1238 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1241 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1244 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1255 * Resets the connection timeout task, some other message has done the
1257 * - For the first peer on the direction this means to send
1258 * a keepalive or a path confirmation message (either create or ACK).
1259 * - For all other peers, this means to destroy the connection,
1260 * due to lack of activity.
1261 * Starts the timeout if no timeout was running (connection just created).
1263 * @param c Connection whose timeout to reset.
1264 * @param fwd Is this forward?
1266 * TODO use heap to improve efficiency of scheduler.
1269 connection_reset_timeout (struct MeshConnection *c, int fwd)
1271 GNUNET_SCHEDULER_TaskIdentifier *ti;
1272 GNUNET_SCHEDULER_Task f;
1274 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1276 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1278 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1279 GNUNET_SCHEDULER_cancel (*ti);
1281 if (GMC_is_origin (c, fwd)) /* Startpoint */
1283 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1284 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1286 else /* Relay, endpoint. */
1288 struct GNUNET_TIME_Relative delay;
1290 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1291 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1292 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1298 * Add the connection to the list of both neighbors.
1300 * @param c Connection.
1302 * @return #GNUNET_OK if everything went fine
1303 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1306 register_neighbors (struct MeshConnection *c)
1308 struct MeshPeer *next_peer;
1309 struct MeshPeer *prev_peer;
1311 next_peer = get_next_hop (c);
1312 prev_peer = get_prev_hop (c);
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1316 path_debug (c->path);
1317 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1318 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1319 GMC_2s (c), next_peer);
1320 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GMP_2s (next_peer));
1321 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1322 GMC_2s (c), prev_peer);
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GMP_2s (prev_peer));
1325 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1326 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1328 if (GMC_is_origin (c, GNUNET_YES))
1329 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1330 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1333 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1334 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1336 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1337 return GNUNET_SYSERR;
1340 GMP_add_connection (next_peer, c);
1341 GMP_add_connection (prev_peer, c);
1348 * Remove the connection from the list of both neighbors.
1350 * @param c Connection.
1353 unregister_neighbors (struct MeshConnection *c)
1355 struct MeshPeer *peer;
1357 peer = get_next_hop (c);
1358 if (GNUNET_OK != GMP_remove_connection (peer, c))
1360 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1361 || MESH_CONNECTION_DESTROYED == c->state);
1362 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1363 if (NULL != c->t) GMT_debug (c->t);
1366 peer = get_prev_hop (c);
1367 if (GNUNET_OK != GMP_remove_connection (peer, c))
1369 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1370 || MESH_CONNECTION_DESTROYED == c->state);
1371 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1372 if (NULL != c->t) GMT_debug (c->t);
1378 * Bind the connection to the peer and the tunnel to that peer.
1380 * If the peer has no tunnel, create one. Update tunnel and connection
1381 * data structres to reflect new status.
1383 * @param c Connection.
1387 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1389 GMP_add_tunnel (peer);
1390 c->t = GMP_get_tunnel (peer);
1391 GMT_add_connection (c->t, c);
1396 * Builds a path from a PeerIdentity array.
1398 * @param peers PeerIdentity array.
1399 * @param size Size of the @c peers array.
1400 * @param own_pos Output parameter: own position in the path.
1402 * @return Fixed and shortened path.
1404 static struct MeshPeerPath *
1405 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1407 unsigned int *own_pos)
1409 struct MeshPeerPath *path;
1410 GNUNET_PEER_Id shortid;
1413 unsigned int offset;
1416 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1417 path = path_new (size);
1420 for (i = 0; i < size; i++)
1422 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1423 i, GNUNET_i2s (&peers[i]));
1424 shortid = GNUNET_PEER_intern (&peers[i]);
1426 /* Check for loops / duplicates */
1427 for (j = 0; j < i - offset; j++)
1429 if (path->peers[j] == shortid)
1431 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1433 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset);
1434 GNUNET_PEER_change_rc (shortid, -1);
1437 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1438 path->peers[i - offset] = shortid;
1439 if (path->peers[i] == myid)
1442 path->length -= offset;
1444 if (path->peers[*own_pos] != myid)
1446 /* create path: self not found in path through self */
1447 GNUNET_break_op (0);
1448 path_destroy (path);
1457 * Log receipt of message on stderr (INFO level).
1459 * @param message Message received.
1460 * @param peer Peer who sent the message.
1461 * @param hash Connection ID.
1464 log_message (const struct GNUNET_MessageHeader *message,
1465 const struct GNUNET_PeerIdentity *peer,
1466 const struct GNUNET_HashCode *hash)
1468 LOG (GNUNET_ERROR_TYPE_INFO, "<- %s on connection %s from %s\n",
1469 GM_m2s (ntohs (message->type)), GNUNET_h2s (hash), GNUNET_i2s (peer));
1472 /******************************************************************************/
1473 /******************************** API ***********************************/
1474 /******************************************************************************/
1477 * Core handler for connection creation.
1479 * @param cls Closure (unused).
1480 * @param peer Sender (neighbor).
1481 * @param message Message.
1483 * @return GNUNET_OK to keep the connection open,
1484 * GNUNET_SYSERR to close it (signal serious error)
1487 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1488 const struct GNUNET_MessageHeader *message)
1490 struct GNUNET_MESH_ConnectionCreate *msg;
1491 struct GNUNET_PeerIdentity *id;
1492 struct GNUNET_HashCode *cid;
1493 struct MeshPeerPath *path;
1494 struct MeshPeer *dest_peer;
1495 struct MeshPeer *orig_peer;
1496 struct MeshConnection *c;
1497 unsigned int own_pos;
1501 size = ntohs (message->size);
1502 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1504 GNUNET_break_op (0);
1508 /* Calculate hops */
1509 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1510 if (size % sizeof (struct GNUNET_PeerIdentity))
1512 GNUNET_break_op (0);
1515 size /= sizeof (struct GNUNET_PeerIdentity);
1518 GNUNET_break_op (0);
1521 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1523 /* Get parameters */
1524 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1526 log_message (message, peer, cid);
1527 id = (struct GNUNET_PeerIdentity *) &msg[1];
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1530 /* Create connection */
1531 c = connection_get (cid);
1534 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1540 GNUNET_break_op (0);
1541 path_destroy (path);
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1546 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1549 if (path->length - 1 == own_pos)
1551 /* If we are destination, why did the creation fail? */
1555 send_broken2 (cid, &my_full_id,
1556 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1557 path->peers[own_pos - 1]);
1558 path_destroy (path);
1561 GMP_add_path_to_all (path, GNUNET_NO);
1562 connection_reset_timeout (c, GNUNET_YES);
1566 path = path_duplicate (c->path);
1568 if (MESH_CONNECTION_NEW == c->state)
1569 connection_change_state (c, MESH_CONNECTION_SENT);
1571 /* Remember peers */
1572 dest_peer = GMP_get (&id[size - 1]);
1573 orig_peer = GMP_get (&id[0]);
1575 /* Is it a connection to us? */
1576 if (c->own_pos == size - 1)
1578 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1579 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1581 add_to_peer (c, orig_peer);
1582 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1583 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1585 send_connection_ack (c, GNUNET_NO);
1586 if (MESH_CONNECTION_SENT == c->state)
1587 connection_change_state (c, MESH_CONNECTION_ACK);
1591 /* It's for somebody else! Retransmit. */
1592 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1593 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1594 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1595 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1598 path_destroy (path);
1604 * Core handler for path confirmations.
1606 * @param cls closure
1607 * @param message message
1608 * @param peer peer identity this notification is about
1610 * @return GNUNET_OK to keep the connection open,
1611 * GNUNET_SYSERR to close it (signal serious error)
1614 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1615 const struct GNUNET_MessageHeader *message)
1617 struct GNUNET_MESH_ConnectionACK *msg;
1618 struct MeshConnection *c;
1619 struct MeshPeerPath *p;
1620 struct MeshPeer *pi;
1621 enum MeshConnectionState oldstate;
1624 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1625 log_message (message, peer, &msg->cid);
1626 c = connection_get (&msg->cid);
1629 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1635 if (GNUNET_NO != c->destroy)
1637 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1641 oldstate = c->state;
1642 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1643 pi = GMP_get (peer);
1644 if (get_next_hop (c) == pi)
1646 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1648 if (MESH_CONNECTION_SENT == oldstate)
1649 connection_change_state (c, MESH_CONNECTION_ACK);
1651 else if (get_prev_hop (c) == pi)
1653 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1655 connection_change_state (c, MESH_CONNECTION_READY);
1659 GNUNET_break_op (0);
1663 connection_reset_timeout (c, fwd);
1665 /* Add path to peers? */
1669 GMP_add_path_to_all (p, GNUNET_YES);
1676 /* Message for us as creator? */
1677 if (GMC_is_origin (c, GNUNET_YES))
1679 if (GNUNET_NO != fwd)
1681 GNUNET_break_op (0);
1684 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1686 /* If just created, cancel the short timeout and start a long one */
1687 if (MESH_CONNECTION_SENT == oldstate)
1688 connection_reset_timeout (c, GNUNET_YES);
1690 /* Change connection state */
1691 connection_change_state (c, MESH_CONNECTION_READY);
1692 send_connection_ack (c, GNUNET_YES);
1694 /* Change tunnel state, trigger KX */
1695 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1696 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1701 /* Message for us as destination? */
1702 if (GMC_is_terminal (c, GNUNET_YES))
1704 if (GNUNET_YES != fwd)
1706 GNUNET_break_op (0);
1709 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1711 /* If just created, cancel the short timeout and start a long one */
1712 if (MESH_CONNECTION_ACK == oldstate)
1713 connection_reset_timeout (c, GNUNET_NO);
1715 /* Change tunnel state */
1716 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1717 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1722 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1723 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1729 * Core handler for notifications of broken paths
1731 * @param cls Closure (unused).
1732 * @param id Peer identity of sending neighbor.
1733 * @param message Message.
1735 * @return GNUNET_OK to keep the connection open,
1736 * GNUNET_SYSERR to close it (signal serious error)
1739 GMC_handle_broken (void* cls,
1740 const struct GNUNET_PeerIdentity* id,
1741 const struct GNUNET_MessageHeader* message)
1743 struct GNUNET_MESH_ConnectionBroken *msg;
1744 struct MeshConnection *c;
1747 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1748 log_message (message, id, &msg->cid);
1749 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1750 GNUNET_i2s (&msg->peer1));
1751 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1752 GNUNET_i2s (&msg->peer2));
1753 c = connection_get (&msg->cid);
1756 GNUNET_break_op (0);
1760 fwd = is_fwd (c, id);
1761 if (GMC_is_terminal (c, fwd))
1763 path_invalidate (c->path);
1764 if (0 < c->pending_messages)
1765 c->destroy = GNUNET_YES;
1771 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1772 c->destroy = GNUNET_YES;
1773 connection_cancel_queues (c, !fwd);
1782 * Core handler for tunnel destruction
1784 * @param cls Closure (unused).
1785 * @param peer Peer identity of sending neighbor.
1786 * @param message Message.
1788 * @return GNUNET_OK to keep the connection open,
1789 * GNUNET_SYSERR to close it (signal serious error)
1792 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1793 const struct GNUNET_MessageHeader *message)
1795 struct GNUNET_MESH_ConnectionDestroy *msg;
1796 struct MeshConnection *c;
1799 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1800 log_message (message, peer, &msg->cid);
1801 c = connection_get (&msg->cid);
1804 /* Probably already got the message from another path,
1805 * destroyed the tunnel and retransmitted to children.
1808 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1810 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1813 fwd = is_fwd (c, peer);
1814 if (GNUNET_SYSERR == fwd)
1816 GNUNET_break_op (0); /* FIXME */
1819 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1820 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1821 else if (0 == c->pending_messages)
1823 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1827 c->destroy = GNUNET_YES;
1828 c->state = MESH_CONNECTION_DESTROYED;
1831 GMT_remove_connection (c->t, c);
1839 * Generic handler for mesh network encrypted traffic.
1841 * @param peer Peer identity this notification is about.
1842 * @param msg Encrypted message.
1844 * @return GNUNET_OK to keep the connection open,
1845 * GNUNET_SYSERR to close it (signal serious error)
1848 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1849 const struct GNUNET_MESH_Encrypted *msg)
1851 struct MeshConnection *c;
1852 struct MeshPeer *neighbor;
1853 struct MeshFlowControl *fc;
1854 GNUNET_PEER_Id peer_id;
1860 log_message (&msg->header, peer, &msg->cid);
1863 size = ntohs (msg->header.size);
1865 sizeof (struct GNUNET_MESH_Encrypted) +
1866 sizeof (struct GNUNET_MessageHeader))
1868 GNUNET_break_op (0);
1872 /* Check connection */
1873 c = connection_get (&msg->cid);
1876 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1877 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
1878 GNUNET_h2s (&msg->cid));
1882 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s\n", GMC_2s (c));
1884 /* Check if origin is as expected */
1885 neighbor = get_prev_hop (c);
1886 peer_id = GNUNET_PEER_search (peer);
1887 if (peer_id == GMP_get_short_id (neighbor))
1893 neighbor = get_next_hop (c);
1894 if (peer_id == GMP_get_short_id (neighbor))
1900 /* Unexpected peer sending traffic on a connection. */
1901 GNUNET_break_op (0);
1907 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1908 pid = ntohl (msg->pid);
1909 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1911 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1912 LOG (GNUNET_ERROR_TYPE_DEBUG,
1913 "WARNING Received PID %u, (prev %u), ACK %u\n",
1914 pid, fc->last_pid_recv, fc->last_ack_sent);
1917 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1919 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1920 LOG (GNUNET_ERROR_TYPE_DEBUG,
1921 " Pid %u not expected (%u+), dropping!\n",
1922 pid, fc->last_pid_recv + 1);
1925 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1926 connection_change_state (c, MESH_CONNECTION_READY);
1927 connection_reset_timeout (c, fwd);
1928 fc->last_pid_recv = pid;
1930 /* Is this message for us? */
1931 if (GMC_is_terminal (c, fwd))
1933 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1934 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1938 GNUNET_break (GNUNET_NO != c->destroy);
1941 fc->last_pid_recv = pid;
1942 GMT_handle_encrypted (c->t, msg);
1943 GMC_send_ack (c, fwd, GNUNET_NO);
1947 /* Message not for us: forward to next hop */
1948 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1949 ttl = ntohl (msg->ttl);
1950 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1953 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1954 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1955 GMC_send_ack (c, fwd, GNUNET_NO);
1959 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1960 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1966 * Generic handler for mesh network encrypted traffic.
1968 * @param peer Peer identity this notification is about.
1969 * @param msg Encrypted message.
1971 * @return GNUNET_OK to keep the connection open,
1972 * GNUNET_SYSERR to close it (signal serious error)
1975 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1976 const struct GNUNET_MESH_KX *msg)
1978 struct MeshConnection *c;
1979 struct MeshPeer *neighbor;
1980 GNUNET_PEER_Id peer_id;
1984 log_message (&msg->header, peer, &msg->cid);
1987 size = ntohs (msg->header.size);
1989 sizeof (struct GNUNET_MESH_Encrypted) +
1990 sizeof (struct GNUNET_MessageHeader))
1992 GNUNET_break_op (0);
1996 /* Check connection */
1997 c = connection_get (&msg->cid);
2000 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2001 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING kx on unknown connection %s\n",
2002 GNUNET_h2s (&msg->cid));
2005 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
2007 /* Check if origin is as expected */
2008 neighbor = get_prev_hop (c);
2009 peer_id = GNUNET_PEER_search (peer);
2010 if (peer_id == GMP_get_short_id (neighbor))
2016 neighbor = get_next_hop (c);
2017 if (peer_id == GMP_get_short_id (neighbor))
2023 /* Unexpected peer sending traffic on a connection. */
2024 GNUNET_break_op (0);
2029 /* Count as connection confirmation. */
2030 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
2032 connection_change_state (c, MESH_CONNECTION_READY);
2035 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
2036 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
2039 connection_reset_timeout (c, fwd);
2041 /* Is this message for us? */
2042 if (GMC_is_terminal (c, fwd))
2044 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2045 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2051 GMT_handle_kx (c->t, &msg[1].header);
2055 /* Message not for us: forward to next hop */
2056 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2057 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2058 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
2065 * Core handler for encrypted mesh network traffic (channel mgmt, data).
2067 * @param cls Closure (unused).
2068 * @param message Message received.
2069 * @param peer Peer who sent the message.
2071 * @return GNUNET_OK to keep the connection open,
2072 * GNUNET_SYSERR to close it (signal serious error)
2075 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2076 const struct GNUNET_MessageHeader *message)
2078 return handle_mesh_encrypted (peer,
2079 (struct GNUNET_MESH_Encrypted *)message);
2084 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2086 * @param cls Closure (unused).
2087 * @param message Message received.
2088 * @param peer Peer who sent the message.
2090 * @return GNUNET_OK to keep the connection open,
2091 * GNUNET_SYSERR to close it (signal serious error)
2094 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2095 const struct GNUNET_MessageHeader *message)
2097 return handle_mesh_kx (peer,
2098 (struct GNUNET_MESH_KX *) message);
2103 * Core handler for mesh network traffic point-to-point acks.
2105 * @param cls closure
2106 * @param message message
2107 * @param peer peer identity this notification is about
2109 * @return GNUNET_OK to keep the connection open,
2110 * GNUNET_SYSERR to close it (signal serious error)
2113 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2114 const struct GNUNET_MessageHeader *message)
2116 struct GNUNET_MESH_ACK *msg;
2117 struct MeshConnection *c;
2118 struct MeshFlowControl *fc;
2123 msg = (struct GNUNET_MESH_ACK *) message;
2124 log_message (message, peer, &msg->cid);
2125 c = connection_get (&msg->cid);
2128 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2133 /* Is this a forward or backward ACK? */
2134 id = GNUNET_PEER_search (peer);
2135 if (GMP_get_short_id (get_next_hop (c)) == id)
2137 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2141 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2143 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2149 GNUNET_break_op (0);
2153 ack = ntohl (msg->ack);
2154 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2155 ack, fc->last_ack_recv);
2156 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
2157 fc->last_ack_recv = ack;
2159 /* Cancel polling if the ACK is big enough. */
2160 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2161 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2163 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2164 GNUNET_SCHEDULER_cancel (fc->poll_task);
2165 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2166 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2169 connection_unlock_queue (c, fwd);
2176 * Core handler for mesh network traffic point-to-point ack polls.
2178 * @param cls closure
2179 * @param message message
2180 * @param peer peer identity this notification is about
2182 * @return GNUNET_OK to keep the connection open,
2183 * GNUNET_SYSERR to close it (signal serious error)
2186 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2187 const struct GNUNET_MessageHeader *message)
2189 struct GNUNET_MESH_Poll *msg;
2190 struct MeshConnection *c;
2191 struct MeshFlowControl *fc;
2196 msg = (struct GNUNET_MESH_Poll *) message;
2197 log_message (message, peer, &msg->cid);
2198 c = connection_get (&msg->cid);
2201 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2203 LOG (GNUNET_ERROR_TYPE_DEBUG,
2204 "WARNING POLL message on unknown connection %s!\n",
2205 GNUNET_h2s (&msg->cid));
2209 /* Is this a forward or backward ACK?
2210 * Note: a poll should never be needed in a loopback case,
2211 * since there is no possiblility of packet loss there, so
2212 * this way of discerining FWD/BCK should not be a problem.
2214 id = GNUNET_PEER_search (peer);
2215 if (GMP_get_short_id (get_next_hop (c)) == id)
2217 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2220 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2222 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2227 GNUNET_break_op (0);
2231 pid = ntohl (msg->pid);
2232 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2233 fc->last_pid_recv = pid;
2234 fwd = fc == &c->bck_fc;
2235 GMC_send_ack (c, fwd, GNUNET_YES);
2242 * Send an ACK on the appropriate connection/channel, depending on
2243 * the direction and the position of the peer.
2245 * @param c Which connection to send the hop-by-hop ACK.
2246 * @param fwd Is this a fwd ACK? (will go dest->root).
2247 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2250 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2252 unsigned int buffer;
2254 LOG (GNUNET_ERROR_TYPE_DEBUG,
2255 "GMC send %s ACK on %s\n",
2256 GM_f2s (fwd), GMC_2s (c));
2264 if (GNUNET_NO != c->destroy)
2266 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2270 /* Get available buffer space */
2271 if (GMC_is_terminal (c, fwd))
2273 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2274 buffer = GMT_get_channels_buffer (c->t);
2278 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2279 buffer = GMC_get_buffer (c, fwd);
2281 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2282 if (0 == buffer && GNUNET_NO == force)
2285 /* Send available buffer space */
2286 if (GMC_is_origin (c, fwd))
2288 GNUNET_assert (NULL != c->t);
2289 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2290 GMT_unchoke_channels (c->t);
2294 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2295 send_ack (c, buffer, fwd, force);
2301 * Initialize the connections subsystem
2303 * @param c Configuration handle.
2306 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2308 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2310 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2313 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2314 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2315 GNUNET_SCHEDULER_shutdown ();
2320 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2323 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2324 "MESH", "MAX_CONNECTIONS", "MISSING");
2325 GNUNET_SCHEDULER_shutdown ();
2330 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2331 &refresh_connection_time))
2333 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2334 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2335 GNUNET_SCHEDULER_shutdown ();
2338 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2339 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2344 * Destroy each connection on shutdown.
2346 * @param cls Closure (unused).
2347 * @param key Current key code (CID, unused).
2348 * @param value Value in the hash map (connection)
2350 * @return #GNUNET_YES, because we should continue to iterate,
2353 shutdown_iterator (void *cls,
2354 const struct GNUNET_HashCode *key,
2357 struct MeshConnection *c = value;
2365 * Shut down the connections subsystem.
2370 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2371 GNUNET_CONTAINER_multihashmap_destroy (connections);
2376 struct MeshConnection *
2377 GMC_new (const struct GNUNET_HashCode *cid,
2378 struct MeshTunnel3 *t,
2379 struct MeshPeerPath *p,
2380 unsigned int own_pos)
2382 struct MeshConnection *c;
2384 c = GNUNET_new (struct MeshConnection);
2386 GNUNET_assert (GNUNET_OK ==
2387 GNUNET_CONTAINER_multihashmap_put (connections,
2389 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2390 fc_init (&c->fwd_fc);
2391 fc_init (&c->bck_fc);
2396 GNUNET_assert (own_pos <= p->length - 1);
2397 c->own_pos = own_pos;
2400 if (GNUNET_OK != register_neighbors (c))
2404 path_invalidate (c->path);
2417 GMC_destroy (struct MeshConnection *c)
2425 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2426 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2429 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2430 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2431 &c->fwd_fc, &c->bck_fc);
2432 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2433 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2435 /* Cancel all traffic */
2436 if (NULL != c->path)
2438 connection_cancel_queues (c, GNUNET_YES);
2439 connection_cancel_queues (c, GNUNET_NO);
2440 unregister_neighbors (c);
2443 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2444 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2446 /* Cancel maintainance task (keepalive/timeout) */
2447 if (NULL != c->fwd_fc.poll_msg)
2449 GMC_cancel (c->fwd_fc.poll_msg);
2450 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2452 if (NULL != c->bck_fc.poll_msg)
2454 GMC_cancel (c->bck_fc.poll_msg);
2455 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2458 /* Delete from tunnel */
2460 GMT_remove_connection (c->t, c);
2462 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2463 path_destroy (c->path);
2464 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2465 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2466 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2467 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2468 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2470 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2471 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2473 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2475 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2476 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2479 GNUNET_break (GNUNET_YES ==
2480 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2482 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2487 * Get the connection ID.
2489 * @param c Connection to get the ID from.
2491 * @return ID of the connection.
2493 const struct GNUNET_HashCode *
2494 GMC_get_id (const struct MeshConnection *c)
2501 * Get the connection path.
2503 * @param c Connection to get the path from.
2505 * @return path used by the connection.
2507 const struct MeshPeerPath *
2508 GMC_get_path (const struct MeshConnection *c)
2510 if (GNUNET_NO == c->destroy)
2517 * Get the connection state.
2519 * @param c Connection to get the state from.
2521 * @return state of the connection.
2523 enum MeshConnectionState
2524 GMC_get_state (const struct MeshConnection *c)
2530 * Get the connection tunnel.
2532 * @param c Connection to get the tunnel from.
2534 * @return tunnel of the connection.
2536 struct MeshTunnel3 *
2537 GMC_get_tunnel (const struct MeshConnection *c)
2544 * Get free buffer space in a connection.
2546 * @param c Connection.
2547 * @param fwd Is query about FWD traffic?
2549 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2552 GMC_get_buffer (struct MeshConnection *c, int fwd)
2554 struct MeshFlowControl *fc;
2556 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2558 return (fc->queue_max - fc->queue_n);
2562 * Get how many messages have we allowed to send to us from a direction.
2564 * @param c Connection.
2565 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2567 * @return last_ack_sent - last_pid_recv
2570 GMC_get_allowed (struct MeshConnection *c, int fwd)
2572 struct MeshFlowControl *fc;
2574 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2575 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2579 return (fc->last_ack_sent - fc->last_pid_recv);
2583 * Get messages queued in a connection.
2585 * @param c Connection.
2586 * @param fwd Is query about FWD traffic?
2588 * @return Number of messages queued.
2591 GMC_get_qn (struct MeshConnection *c, int fwd)
2593 struct MeshFlowControl *fc;
2595 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2602 * Allow the connection to advertise a buffer of the given size.
2604 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2605 * allowing up to last_pid_recv + buffer.
2607 * @param c Connection.
2608 * @param buffer How many more messages the connection can accept.
2609 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2612 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2614 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2615 GMC_2s (c), buffer, GM_f2s (fwd));
2616 send_ack (c, buffer, fwd, GNUNET_NO);
2621 * Notify other peers on a connection of a broken link. Mark connections
2622 * to destroy after all traffic has been sent.
2624 * @param c Connection on which there has been a disconnection.
2625 * @param peer Peer that disconnected.
2628 GMC_notify_broken (struct MeshConnection *c,
2629 struct MeshPeer *peer)
2633 LOG (GNUNET_ERROR_TYPE_DEBUG,
2634 " notify broken on %s due to %s disconnect\n",
2635 GMC_2s (c), GMP_2s (peer));
2637 fwd = peer == get_prev_hop (c);
2639 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2641 /* Local shutdown, no one to notify about this. */
2645 if (GNUNET_NO == c->destroy)
2646 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2648 /* Connection will have at least one pending message
2649 * (the one we just scheduled), so no point in checking whether to
2650 * destroy immediately. */
2651 c->destroy = GNUNET_YES;
2652 c->state = MESH_CONNECTION_DESTROYED;
2655 * Cancel all queues, if no message is left, connection will be destroyed.
2657 connection_cancel_queues (c, !fwd);
2664 * Is this peer the first one on the connection?
2666 * @param c Connection.
2667 * @param fwd Is this about fwd traffic?
2669 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2672 GMC_is_origin (struct MeshConnection *c, int fwd)
2674 if (!fwd && c->path->length - 1 == c->own_pos )
2676 if (fwd && 0 == c->own_pos)
2683 * Is this peer the last one on the connection?
2685 * @param c Connection.
2686 * @param fwd Is this about fwd traffic?
2687 * Note that the ROOT is the terminal for BCK traffic!
2689 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2692 GMC_is_terminal (struct MeshConnection *c, int fwd)
2694 return GMC_is_origin (c, !fwd);
2699 * See if we are allowed to send by the next hop in the given direction.
2701 * @param c Connection.
2702 * @param fwd Is this about fwd traffic?
2704 * @return #GNUNET_YES in case it's OK to send.
2707 GMC_is_sendable (struct MeshConnection *c, int fwd)
2709 struct MeshFlowControl *fc;
2716 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2717 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2723 * Sends an already built message on a connection, properly registering
2724 * all used resources.
2726 * @param message Message to send. Function makes a copy of it.
2727 * If message is not hop-by-hop, decrements TTL of copy.
2728 * @param c Connection on which this message is transmitted.
2729 * @param fwd Is this a fwd message?
2730 * @param force Force the connection to accept the message (buffer overfill).
2731 * @param cont Continuation called once message is sent. Can be NULL.
2732 * @param cont_cls Closure for @c cont.
2734 * @return Handle to cancel the message before it's sent.
2735 * NULL on error or if @c cont is NULL.
2736 * Invalid on @c cont call.
2738 struct MeshConnectionQueue *
2739 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2740 struct MeshConnection *c, int fwd, int force,
2741 GMC_sent cont, void *cont_cls)
2743 struct MeshFlowControl *fc;
2744 struct MeshConnectionQueue *q;
2750 size = ntohs (message->size);
2751 data = GNUNET_malloc (size);
2752 memcpy (data, message, size);
2753 type = ntohs (message->type);
2754 LOG (GNUNET_ERROR_TYPE_INFO, "-> %s on connection %s (%u bytes)\n",
2755 GM_m2s (type), GMC_2s (c), size);
2757 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2758 droppable = GNUNET_NO == force;
2761 struct GNUNET_MESH_Encrypted *emsg;
2762 struct GNUNET_MESH_KX *kmsg;
2763 struct GNUNET_MESH_ACK *amsg;
2764 struct GNUNET_MESH_Poll *pmsg;
2765 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2766 struct GNUNET_MESH_ConnectionBroken *bmsg;
2769 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2770 emsg = (struct GNUNET_MESH_Encrypted *) data;
2771 ttl = ntohl (emsg->ttl);
2774 GNUNET_break_op (0);
2779 emsg->ttl = htonl (ttl - 1);
2780 emsg->pid = htonl (fc->next_pid++);
2781 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2782 if (GNUNET_YES == droppable)
2785 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2786 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2787 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2791 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2793 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2795 GMC_start_poll (c, fwd);
2799 case GNUNET_MESSAGE_TYPE_MESH_KX:
2800 kmsg = (struct GNUNET_MESH_KX *) data;
2804 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2805 amsg = (struct GNUNET_MESH_ACK *) data;
2807 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2808 droppable = GNUNET_NO;
2811 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2812 pmsg = (struct GNUNET_MESH_Poll *) data;
2814 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2815 droppable = GNUNET_NO;
2818 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2819 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2824 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2825 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2830 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2831 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2832 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2841 if (fc->queue_n > fc->queue_max && droppable)
2843 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2846 LOG (GNUNET_ERROR_TYPE_DEBUG,
2847 "queue full: %u/%u\n",
2848 fc->queue_n, fc->queue_max);
2849 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2855 return NULL; /* Drop this message */
2858 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2859 c->pending_messages++;
2861 q = GNUNET_new (struct MeshConnectionQueue);
2862 q->forced = !droppable;
2863 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2867 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2873 q->cont_cls = cont_cls;
2879 * Cancel a previously sent message while it's in the queue.
2881 * ONLY can be called before the continuation given to the send function
2882 * is called. Once the continuation is called, the message is no longer in the
2885 * @param q Handle to the queue.
2888 GMC_cancel (struct MeshConnectionQueue *q)
2890 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2892 /* queue destroy calls message_sent, which calls q->cont and frees q */
2893 GMP_queue_destroy (q->q, GNUNET_YES);
2898 * Sends a CREATE CONNECTION message for a path to a peer.
2899 * Changes the connection and tunnel states if necessary.
2901 * @param connection Connection to create.
2904 GMC_send_create (struct MeshConnection *connection)
2906 enum MeshTunnel3CState state;
2909 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2910 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2912 LOG (GNUNET_ERROR_TYPE_INFO, "=> %s on connection %s (%u bytes)\n",
2913 GM_m2s (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE),
2914 GMC_2s (connection), size);
2915 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2916 connection, connection->pending_messages);
2917 connection->pending_messages++;
2919 connection->maintenance_q =
2920 GMP_queue_add (get_next_hop (connection), NULL,
2921 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2922 size, connection, GNUNET_YES, &message_sent, NULL);
2924 state = GMT_get_cstate (connection->t);
2925 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2926 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2927 if (MESH_CONNECTION_NEW == connection->state)
2928 connection_change_state (connection, MESH_CONNECTION_SENT);
2933 * Send a message to all peers in this connection that the connection
2934 * is no longer valid.
2936 * If some peer should not receive the message, it should be zero'ed out
2937 * before calling this function.
2939 * @param c The connection whose peers to notify.
2942 GMC_send_destroy (struct MeshConnection *c)
2944 struct GNUNET_MESH_ConnectionDestroy msg;
2946 if (GNUNET_YES == c->destroy)
2949 msg.header.size = htons (sizeof (msg));
2950 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2952 LOG (GNUNET_ERROR_TYPE_DEBUG,
2953 " sending connection destroy for connection %s\n",
2956 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2957 GMC_send_prebuilt_message (&msg.header, c,
2958 GNUNET_YES, GNUNET_YES, NULL, NULL);
2959 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2960 GMC_send_prebuilt_message (&msg.header, c,
2961 GNUNET_NO, GNUNET_YES, NULL, NULL);
2962 c->destroy = GNUNET_YES;
2963 c->state = MESH_CONNECTION_DESTROYED;
2968 * @brief Start a polling timer for the connection.
2970 * When a neighbor does not accept more traffic on the connection it could be
2971 * caused by a simple congestion or by a lost ACK. Polling enables to check
2972 * for the lastest ACK status for a connection.
2974 * @param c Connection.
2975 * @param fwd Should we poll in the FWD direction?
2978 GMC_start_poll (struct MeshConnection *c, int fwd)
2980 struct MeshFlowControl *fc;
2982 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2983 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
2985 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
2987 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
2988 fc->poll_task, fc->poll_msg);
2991 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
2992 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2999 * @brief Stop polling a connection for ACKs.
3001 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3003 * @param c Connection.
3004 * @param fwd Should we stop the poll in the FWD direction?
3007 GMC_stop_poll (struct MeshConnection *c, int fwd)
3009 struct MeshFlowControl *fc;
3011 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3012 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3014 GNUNET_SCHEDULER_cancel (fc->poll_task);
3015 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3020 * Get a (static) string for a connection.
3022 * @param c Connection.
3025 GMC_2s (const struct MeshConnection *c)
3032 static char buf[128];
3034 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
3037 return GNUNET_h2s (&c->id);