2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh_connection.c
23 * @brief GNUnet MESH service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "mesh_path.h"
33 #include "mesh_protocol.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
42 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
43 GNUNET_TIME_UNIT_MINUTES,\
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct to encapsulate all the Flow Control information to a peer to which
54 * we are directly connected (on a core level).
56 struct MeshFlowControl
59 * Connection this controls.
61 struct MeshConnection *c;
64 * How many messages are in the queue on this connection.
69 * How many messages do we accept in the queue.
71 unsigned int queue_max;
79 * ID of the last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 GNUNET_SCHEDULER_TaskIdentifier poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
109 * Queued poll message, to cancel if not necessary anymore (got ACK).
111 struct MeshConnectionQueue *poll_msg;
114 * Queued poll message, to cancel if not necessary anymore (got ACK).
116 struct MeshConnectionQueue *ack_msg;
120 * Keep a record of the last messages sent on this connection.
122 struct MeshConnectionPerformance
125 * Circular buffer for storing measurements.
127 double usecsperbyte[AVG_MSGS];
130 * Running average of @c usecsperbyte.
135 * How many values of @c usecsperbyte are valid.
140 * Index of the next "free" position in @c usecsperbyte.
147 * Struct containing all information regarding a connection to a peer.
149 struct MeshConnection
152 * Tunnel this connection is part of.
154 struct MeshTunnel3 *t;
157 * Flow control information for traffic fwd.
159 struct MeshFlowControl fwd_fc;
162 * Flow control information for traffic bck.
164 struct MeshFlowControl bck_fc;
167 * Measure connection performance on the endpoint.
169 struct MeshConnectionPerformance *perf;
172 * ID of the connection.
174 struct GNUNET_MeshHash id;
177 * State of the connection.
179 enum MeshConnectionState state;
182 * Path being used for the tunnel. At the origin of the connection
183 * it's a pointer to the destination's path pool, otherwise just a copy.
185 struct MeshPeerPath *path;
188 * Position of the local peer in the path.
190 unsigned int own_pos;
193 * Task to keep the used paths alive at the owner,
194 * time tunnel out on all the other peers.
196 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
199 * Task to keep the used paths alive at the destination,
200 * time tunnel out on all the other peers.
202 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
205 * Queue handle for maintainance traffic. One handle for FWD and BCK since
206 * one peer never needs to maintain both directions (no loopback connections).
208 struct MeshPeerQueue *maintenance_q;
211 * Counter to do exponential backoff when creating a connection (max 64).
213 unsigned short create_retry;
216 * Pending message count.
218 int pending_messages;
221 * Destroy flag: if true, destroy on last message.
227 * Handle for messages queued but not yet sent.
229 struct MeshConnectionQueue
232 * Peer queue handle, to cancel if necessary.
234 struct MeshPeerQueue *q;
237 * Was this a forced message? (Do not account for it)
242 * Continuation to call once sent.
247 * Closure for @c cont.
252 /******************************************************************************/
253 /******************************* GLOBALS ***********************************/
254 /******************************************************************************/
257 * Global handle to the statistics service.
259 extern struct GNUNET_STATISTICS_Handle *stats;
262 * Local peer own ID (memory efficient handle).
264 extern GNUNET_PEER_Id myid;
267 * Local peer own ID (full value).
269 extern struct GNUNET_PeerIdentity my_full_id;
272 * Connections known, indexed by cid (MeshConnection).
274 static struct GNUNET_CONTAINER_MultiHashMap *connections;
277 * How many connections are we willing to maintain.
278 * Local connections are always allowed, even if there are more connections than max.
280 static unsigned long long max_connections;
283 * How many messages *in total* are we willing to queue, divide by number of
284 * connections to get connection queue size.
286 static unsigned long long max_msgs_queue;
289 * How often to send path keepalives. Paths timeout after 4 missed.
291 static struct GNUNET_TIME_Relative refresh_connection_time;
294 * How often to send path create / ACKs.
296 static struct GNUNET_TIME_Relative create_connection_time;
299 /******************************************************************************/
300 /******************************** STATIC ***********************************/
301 /******************************************************************************/
303 #if 0 // avoid compiler warning for unused static function
305 fc_debug (struct MeshFlowControl *fc)
307 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
308 fc->last_pid_recv, fc->last_ack_sent);
309 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
310 fc->last_pid_sent, fc->last_ack_recv);
311 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
312 fc->queue_n, fc->queue_max);
316 connection_debug (struct MeshConnection *c)
320 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
324 peer2s (c->t->peer), GMC_2s (c));
325 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
326 c->state, c->pending_messages);
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
328 fc_debug (&c->fwd_fc);
329 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
330 fc_debug (&c->bck_fc);
336 * Schedule next keepalive task, taking in consideration
337 * the connection state and number of retries.
339 * @param c Connection for which to schedule the next keepalive.
340 * @param fwd Direction for the next keepalive.
343 schedule_next_keepalive (struct MeshConnection *c, int fwd);
347 * Resets the connection timeout task, some other message has done the
349 * - For the first peer on the direction this means to send
350 * a keepalive or a path confirmation message (either create or ACK).
351 * - For all other peers, this means to destroy the connection,
352 * due to lack of activity.
353 * Starts the timeout if no timeout was running (connection just created).
355 * @param c Connection whose timeout to reset.
356 * @param fwd Is this forward?
359 connection_reset_timeout (struct MeshConnection *c, int fwd);
363 * Get string description for tunnel state.
365 * @param s Tunnel state.
367 * @return String representation.
370 GMC_state2s (enum MeshConnectionState s)
374 case MESH_CONNECTION_NEW:
375 return "MESH_CONNECTION_NEW";
376 case MESH_CONNECTION_SENT:
377 return "MESH_CONNECTION_SENT";
378 case MESH_CONNECTION_ACK:
379 return "MESH_CONNECTION_ACK";
380 case MESH_CONNECTION_READY:
381 return "MESH_CONNECTION_READY";
382 case MESH_CONNECTION_DESTROYED:
383 return "MESH_CONNECTION_DESTROYED";
385 return "MESH_CONNECTION_STATE_ERROR";
391 * Initialize a Flow Control structure to the initial state.
393 * @param fc Flow Control structure to initialize.
396 fc_init (struct MeshFlowControl *fc)
399 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
400 fc->last_pid_recv = (uint32_t) -1;
401 fc->last_ack_sent = (uint32_t) 0;
402 fc->last_ack_recv = (uint32_t) 0;
403 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
404 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
406 fc->queue_max = (max_msgs_queue / max_connections) + 1;
413 * @param cid Connection ID.
415 static struct MeshConnection *
416 connection_get (const struct GNUNET_MeshHash *cid)
418 struct GNUNET_HashCode hash;
419 struct GNUNET_MeshHash *aux;
421 memcpy (&hash, cid, sizeof (cid));
422 aux = (struct GNUNET_MeshHash *) &hash;
423 memset (&aux[1], 0, sizeof (hash) - sizeof (*cid));
425 return GNUNET_CONTAINER_multihashmap_get (connections, &hash);
430 connection_change_state (struct MeshConnection* c,
431 enum MeshConnectionState state)
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
434 "Connection %s state was %s\n",
435 GMC_2s (c), GMC_state2s (c->state));
436 if (MESH_CONNECTION_DESTROYED == c->state)
438 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "Connection %s state is now %s\n",
443 GMC_2s (c), GMC_state2s (state));
445 if (MESH_CONNECTION_READY == state)
451 * Callback called when a queued ACK message is sent.
453 * @param cls Closure (FC).
454 * @param c Connection this message was on.
455 * @param q Queue handler this call invalidates.
456 * @param type Type of message sent.
457 * @param fwd Was this a FWD going message?
458 * @param size Size of the message.
462 struct MeshConnection *c,
463 struct MeshConnectionQueue *q,
464 uint16_t type, int fwd, size_t size)
466 struct MeshFlowControl *fc = cls;
473 * Send an ACK on the connection, informing the predecessor about
474 * the available buffer space. Should not be called in case the peer
475 * is origin (no predecessor) in the @c fwd direction.
477 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
478 * the ACK itself goes "back" (dest->root).
480 * @param c Connection on which to send the ACK.
481 * @param buffer How much space free to advertise?
482 * @param fwd Is this FWD ACK? (Going dest -> root)
483 * @param force Don't optimize out.
486 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
488 struct MeshFlowControl *next_fc;
489 struct MeshFlowControl *prev_fc;
490 struct GNUNET_MESH_ACK msg;
494 /* If origin, there is no connection to send ACKs. Wrong function! */
495 if (GMC_is_origin (c, fwd))
497 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
498 GMC_2s (c), GM_f2s (fwd));
503 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
504 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
507 GM_f2s (fwd), GMC_2s (c));
509 /* Check if we need to transmit the ACK. */
510 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
511 if (3 < delta && buffer < delta && GNUNET_NO == force)
513 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
514 LOG (GNUNET_ERROR_TYPE_DEBUG,
515 " last pid recv: %u, last ack sent: %u\n",
516 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
520 /* Ok, ACK might be necessary, what PID to ACK? */
521 ack = prev_fc->last_pid_recv + buffer;
522 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
523 LOG (GNUNET_ERROR_TYPE_DEBUG,
524 " last pid %u, last ack %u, qmax %u, q %u\n",
525 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
526 next_fc->queue_max, next_fc->queue_n);
527 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
529 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
533 /* Check if message is already in queue */
534 if (NULL != prev_fc->ack_msg)
536 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
538 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
539 GMC_cancel (prev_fc->ack_msg);
540 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
544 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
549 prev_fc->last_ack_sent = ack;
551 /* Build ACK message and send on connection */
552 msg.header.size = htons (sizeof (msg));
553 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
554 msg.ack = htonl (ack);
557 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
564 * Callback called when a queued message is sent.
566 * Calculates the average time and connection packet tracking.
568 * @param cls Closure (ConnectionQueue Handle).
569 * @param c Connection this message was on.
570 * @param type Type of message sent.
571 * @param fwd Was this a FWD going message?
572 * @param size Size of the message.
573 * @param wait Time spent waiting for core (only the time for THIS message)
576 message_sent (void *cls,
577 struct MeshConnection *c, uint16_t type,
578 int fwd, size_t size,
579 struct GNUNET_TIME_Relative wait)
581 struct MeshConnectionPerformance *p;
582 struct MeshFlowControl *fc;
583 struct MeshConnectionQueue *q = cls;
587 fc = fwd ? &c->fwd_fc : &c->bck_fc;
588 LOG (GNUNET_ERROR_TYPE_DEBUG,
592 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
598 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
599 q->cont (q->cont_cls, c, q, type, fwd, size);
603 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
605 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
612 c->pending_messages--;
613 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
615 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
619 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
622 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
623 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
624 c->maintenance_q = NULL;
625 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
626 if (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE == type || !fwd)
627 schedule_next_keepalive (c, fwd);
630 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
632 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
633 if (GNUNET_NO == forced)
636 LOG (GNUNET_ERROR_TYPE_DEBUG,
637 "! accounting pid %u\n",
642 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 "! forced, Q_N not accounting pid %u\n",
646 GMC_send_ack (c, fwd, GNUNET_NO);
647 connection_reset_timeout (c, fwd);
650 case GNUNET_MESSAGE_TYPE_MESH_POLL:
654 case GNUNET_MESSAGE_TYPE_MESH_ACK:
661 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
664 return; /* Only endpoints are interested in timing. */
667 usecsperbyte = ((double) wait.rel_value_us) / size;
668 if (p->size == AVG_MSGS)
670 /* Array is full. Substract oldest value, add new one and store. */
671 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
672 p->usecsperbyte[p->idx] = usecsperbyte;
673 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
677 /* Array not yet full. Add current value to avg and store. */
678 p->usecsperbyte[p->idx] = usecsperbyte;
680 p->avg += p->usecsperbyte[p->idx];
684 p->idx = (p->idx + 1) % AVG_MSGS;
689 * Get the previous hop in a connection
691 * @param c Connection.
693 * @return Previous peer in the connection.
695 static struct MeshPeer *
696 get_prev_hop (const struct MeshConnection *c)
700 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
701 GMC_2s (c), c->own_pos, c->path->length);
702 if (0 == c->own_pos || c->path->length < 2)
703 id = c->path->peers[0];
705 id = c->path->peers[c->own_pos - 1];
707 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
708 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
710 return GMP_get_short (id);
715 * Get the next hop in a connection
717 * @param c Connection.
719 * @return Next peer in the connection.
721 static struct MeshPeer *
722 get_next_hop (const struct MeshConnection *c)
726 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
727 GMC_2s (c), c->own_pos, c->path->length);
728 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
729 id = c->path->peers[c->path->length - 1];
731 id = c->path->peers[c->own_pos + 1];
733 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
734 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
736 return GMP_get_short (id);
741 * Get the hop in a connection.
743 * @param c Connection.
744 * @param fwd Next hop?
746 * @return Next peer in the connection.
748 static struct MeshPeer *
749 get_hop (struct MeshConnection *c, int fwd)
752 return get_next_hop (c);
753 return get_prev_hop (c);
758 * Is traffic coming from this sender 'FWD' traffic?
760 * @param c Connection to check.
761 * @param sender Peer identity of neighbor.
763 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
764 * the traffic is 'FWD'.
765 * #GNUNET_NO for BCK.
766 * #GNUNET_SYSERR for errors.
769 is_fwd (const struct MeshConnection *c,
770 const struct GNUNET_PeerIdentity *sender)
774 id = GNUNET_PEER_search (sender);
775 if (GMP_get_short_id (get_prev_hop (c)) == id)
778 if (GMP_get_short_id (get_next_hop (c)) == id)
782 return GNUNET_SYSERR;
787 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
788 * or a first CONNECTION_ACK directed to us.
790 * @param connection Connection to confirm.
791 * @param fwd Should we send it FWD? (root->dest)
792 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
795 send_connection_ack (struct MeshConnection *connection, int fwd)
797 struct MeshTunnel3 *t;
800 LOG (GNUNET_ERROR_TYPE_INFO, "=> {%18s ACK} on connection %s\n",
801 GM_f2s (!fwd), GMC_2s (connection));
802 GMP_queue_add (get_hop (connection, fwd), NULL,
803 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
804 sizeof (struct GNUNET_MESH_ConnectionACK),
805 connection, fwd, &message_sent, NULL);
806 connection->pending_messages++;
807 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
808 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
809 if (MESH_CONNECTION_READY != connection->state)
810 connection_change_state (connection, MESH_CONNECTION_SENT);
815 * Send a notification that a connection is broken.
817 * @param c Connection that is broken.
818 * @param id1 Peer that has disconnected.
819 * @param id2 Peer that has disconnected.
820 * @param fwd Direction towards which to send it.
823 send_broken (struct MeshConnection *c,
824 const struct GNUNET_PeerIdentity *id1,
825 const struct GNUNET_PeerIdentity *id2,
828 struct GNUNET_MESH_ConnectionBroken msg;
830 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
831 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
835 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
840 * Send a notification that a connection is broken, when a connection
841 * isn't even created.
843 * @param connection_id Connection ID.
844 * @param id1 Peer that has disconnected.
845 * @param id2 Peer that has disconnected.
846 * @param peer Peer to notify (neighbor who sent the connection).
849 send_broken2 (struct GNUNET_HashCode *connection_id,
850 const struct GNUNET_PeerIdentity *id1,
851 const struct GNUNET_PeerIdentity *id2,
852 GNUNET_PEER_Id peer_id)
854 struct GNUNET_MESH_ConnectionBroken *msg;
855 struct MeshPeer *neighbor;
857 LOG (GNUNET_ERROR_TYPE_INFO,
858 "Send BROKEN on unknown connection %s\n", GNUNET_h2s (connection_id));
860 msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
861 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
862 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
863 msg->cid = *connection_id;
866 neighbor = GMP_get_short (peer_id);
867 GMP_queue_add (neighbor, msg,
868 GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED,
869 sizeof (struct GNUNET_MESH_ConnectionBroken),
870 NULL, GNUNET_SYSERR, /* connection, fwd */
871 NULL, NULL); /* continuation */
876 * Send keepalive packets for a connection.
878 * @param c Connection to keep alive..
879 * @param fwd Is this a FWD keepalive? (owner -> dest).
882 send_connection_keepalive (struct MeshConnection *c, int fwd)
884 struct GNUNET_MessageHeader msg;
886 LOG (GNUNET_ERROR_TYPE_INFO,
887 "keepalive %s for connection %s\n",
888 GM_f2s (fwd), GMC_2s (c));
889 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
891 GNUNET_assert (NULL != c->t);
892 msg.size = htons (sizeof (msg));
893 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
895 GNUNET_assert (NULL ==
896 GMT_send_prebuilt_message (&msg, c->t, c,
897 GNUNET_NO, NULL, NULL));
902 * Send CONNECTION_{CREATE/ACK} packets for a connection.
904 * @param c Connection for which to send the message.
905 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
908 connection_recreate (struct MeshConnection *c, int fwd)
910 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
914 send_connection_ack (c, GNUNET_NO);
919 * Generic connection timer management.
920 * Depending on the role of the peer in the connection will send the
921 * appropriate message (build or keepalive)
923 * @param c Conncetion to maintain.
927 connection_maintain (struct MeshConnection *c, int fwd)
929 if (GNUNET_NO != c->destroy)
932 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
934 /* TODO DHT GET with RO_BART */
939 case MESH_CONNECTION_NEW:
942 case MESH_CONNECTION_SENT:
943 connection_recreate (c, fwd);
945 case MESH_CONNECTION_READY:
946 send_connection_keepalive (c, fwd);
956 * Keep the connection alive.
958 * @param c Connection to keep alive.
959 * @param fwd Direction.
960 * @param shutdown Are we shutting down? (Don't send traffic)
961 * Non-zero value for true, not necessarily GNUNET_YES.
964 connection_keepalive (struct MeshConnection *c, int fwd, int shutdown)
966 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
967 GM_f2s (fwd), GMC_2s (c));
970 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
972 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
974 if (GNUNET_NO != shutdown)
977 connection_maintain (c, fwd);
979 /* Next execution will be scheduled by message_sent */
984 * Keep the connection alive in the FWD direction.
986 * @param cls Closure (connection to keepalive).
987 * @param tc TaskContext.
990 connection_fwd_keepalive (void *cls,
991 const struct GNUNET_SCHEDULER_TaskContext *tc)
993 connection_keepalive ((struct MeshConnection *) cls,
995 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1000 * Keep the connection alive in the BCK direction.
1002 * @param cls Closure (connection to keepalive).
1003 * @param tc TaskContext.
1006 connection_bck_keepalive (void *cls,
1007 const struct GNUNET_SCHEDULER_TaskContext *tc)
1009 connection_keepalive ((struct MeshConnection *) cls,
1011 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1016 * Schedule next keepalive task, taking in consideration
1017 * the connection state and number of retries.
1019 * If the peer is not the origin, do nothing.
1021 * @param c Connection for which to schedule the next keepalive.
1022 * @param fwd Direction for the next keepalive.
1025 schedule_next_keepalive (struct MeshConnection *c, int fwd)
1027 struct GNUNET_TIME_Relative delay;
1028 GNUNET_SCHEDULER_TaskIdentifier *task_id;
1029 GNUNET_SCHEDULER_Task keepalive_task;
1031 if (GNUNET_NO == GMC_is_origin (c, fwd))
1034 /* Calculate delay to use, depending on the state of the connection */
1035 if (MESH_CONNECTION_READY == c->state)
1037 delay = refresh_connection_time;
1041 if (1 > c->create_retry)
1042 c->create_retry = 1;
1043 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1045 if (c->create_retry < 64)
1046 c->create_retry *= 2;
1049 /* Select direction-dependent parameters */
1050 if (GNUNET_YES == fwd)
1052 task_id = &c->fwd_maintenance_task;
1053 keepalive_task = &connection_fwd_keepalive;
1057 task_id = &c->bck_maintenance_task;
1058 keepalive_task = &connection_bck_keepalive;
1061 /* Check that no one scheduled it before us */
1062 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1064 /* No need for a _break. It can happen for instance when sending a SYNACK
1065 * for a duplicate SYN: the first SYNACK scheduled the task. */
1066 GNUNET_SCHEDULER_cancel (*task_id);
1069 /* Schedule the task */
1070 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1072 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1077 * @brief Re-initiate traffic on this connection if necessary.
1079 * Check if there is traffic queued towards this peer
1080 * and the core transmit handle is NULL (traffic was stalled).
1081 * If so, call core tmt rdy.
1083 * @param c Connection on which initiate traffic.
1084 * @param fwd Is this about fwd traffic?
1087 connection_unlock_queue (struct MeshConnection *c, int fwd)
1089 struct MeshPeer *peer;
1091 LOG (GNUNET_ERROR_TYPE_DEBUG,
1092 "connection_unlock_queue %s on %s\n",
1093 GM_f2s (fwd), GMC_2s (c));
1095 if (GMC_is_terminal (c, fwd))
1097 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1101 peer = get_hop (c, fwd);
1102 GMP_queue_unlock (peer, c);
1107 * Cancel all transmissions that belong to a certain connection.
1109 * If the connection is scheduled for destruction and no more messages are left,
1110 * the connection will be destroyed by the continuation call.
1112 * @param c Connection which to cancel. Might be destroyed during this call.
1113 * @param fwd Cancel fwd traffic?
1116 connection_cancel_queues (struct MeshConnection *c, int fwd)
1118 struct MeshFlowControl *fc;
1119 struct MeshPeer *peer;
1121 LOG (GNUNET_ERROR_TYPE_DEBUG,
1122 " *** Cancel %s queues for connection %s\n",
1123 GM_f2s (fwd), GMC_2s (c));
1130 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1131 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1133 GNUNET_SCHEDULER_cancel (fc->poll_task);
1134 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1135 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1137 peer = get_hop (c, fwd);
1138 GMP_queue_cancel (peer, c);
1143 * Function called if a connection has been stalled for a while,
1144 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1146 * @param cls Closure (poll ctx).
1147 * @param tc TaskContext.
1150 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1154 * Callback called when a queued POLL message is sent.
1156 * @param cls Closure (FC).
1157 * @param c Connection this message was on.
1158 * @param q Queue handler this call invalidates.
1159 * @param type Type of message sent.
1160 * @param fwd Was this a FWD going message?
1161 * @param size Size of the message.
1164 poll_sent (void *cls,
1165 struct MeshConnection *c,
1166 struct MeshConnectionQueue *q,
1167 uint16_t type, int fwd, size_t size)
1169 struct MeshFlowControl *fc = cls;
1171 if (2 == c->destroy)
1173 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1176 LOG (GNUNET_ERROR_TYPE_DEBUG,
1177 " *** POLL sent for , scheduling new one!\n");
1178 fc->poll_msg = NULL;
1179 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1180 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1181 &connection_poll, fc);
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1187 * Function called if a connection has been stalled for a while,
1188 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1190 * @param cls Closure (poll ctx).
1191 * @param tc TaskContext.
1194 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1196 struct MeshFlowControl *fc = cls;
1197 struct GNUNET_MESH_Poll msg;
1198 struct MeshConnection *c;
1200 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1201 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1207 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1208 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1209 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1210 fc == &c->fwd_fc ? "FWD" : "BCK");
1212 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1213 msg.header.size = htons (sizeof (msg));
1214 msg.pid = htonl (fc->last_pid_sent);
1215 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1216 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1217 fc == &c->fwd_fc, GNUNET_YES,
1223 * Timeout function due to lack of keepalive/traffic from the owner.
1224 * Destroys connection if called.
1226 * @param cls Closure (connection to destroy).
1227 * @param tc TaskContext.
1230 connection_fwd_timeout (void *cls,
1231 const struct GNUNET_SCHEDULER_TaskContext *tc)
1233 struct MeshConnection *c = cls;
1235 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1236 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1241 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1252 * Timeout function due to lack of keepalive/traffic from the destination.
1253 * Destroys connection if called.
1255 * @param cls Closure (connection to destroy).
1256 * @param tc TaskContext
1259 connection_bck_timeout (void *cls,
1260 const struct GNUNET_SCHEDULER_TaskContext *tc)
1262 struct MeshConnection *c = cls;
1264 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1265 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1268 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1271 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1282 * Resets the connection timeout task, some other message has done the
1284 * - For the first peer on the direction this means to send
1285 * a keepalive or a path confirmation message (either create or ACK).
1286 * - For all other peers, this means to destroy the connection,
1287 * due to lack of activity.
1288 * Starts the timeout if no timeout was running (connection just created).
1290 * @param c Connection whose timeout to reset.
1291 * @param fwd Is this forward?
1293 * TODO use heap to improve efficiency of scheduler.
1296 connection_reset_timeout (struct MeshConnection *c, int fwd)
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1300 if (GMC_is_origin (c, fwd)) /* Startpoint */
1302 schedule_next_keepalive (c, fwd);
1304 else /* Relay, endpoint. */
1306 struct GNUNET_TIME_Relative delay;
1307 GNUNET_SCHEDULER_TaskIdentifier *ti;
1308 GNUNET_SCHEDULER_Task f;
1310 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1312 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1313 GNUNET_SCHEDULER_cancel (*ti);
1314 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1315 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1316 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1322 * Add the connection to the list of both neighbors.
1324 * @param c Connection.
1326 * @return #GNUNET_OK if everything went fine
1327 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1330 register_neighbors (struct MeshConnection *c)
1332 struct MeshPeer *next_peer;
1333 struct MeshPeer *prev_peer;
1335 next_peer = get_next_hop (c);
1336 prev_peer = get_prev_hop (c);
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1340 path_debug (c->path);
1341 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1342 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1343 GMC_2s (c), next_peer);
1344 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GMP_2s (next_peer));
1345 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1346 GMC_2s (c), prev_peer);
1347 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GMP_2s (prev_peer));
1349 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1350 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1352 if (GMC_is_origin (c, GNUNET_YES))
1353 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1354 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1356 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1357 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1358 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1359 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1360 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1361 return GNUNET_SYSERR;
1364 GMP_add_connection (next_peer, c);
1365 GMP_add_connection (prev_peer, c);
1372 * Remove the connection from the list of both neighbors.
1374 * @param c Connection.
1377 unregister_neighbors (struct MeshConnection *c)
1379 struct MeshPeer *peer;
1381 peer = get_next_hop (c);
1382 if (GNUNET_OK != GMP_remove_connection (peer, c))
1384 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1385 || MESH_CONNECTION_DESTROYED == c->state);
1386 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1387 if (NULL != c->t) GMT_debug (c->t);
1390 peer = get_prev_hop (c);
1391 if (GNUNET_OK != GMP_remove_connection (peer, c))
1393 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1394 || MESH_CONNECTION_DESTROYED == c->state);
1395 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1396 if (NULL != c->t) GMT_debug (c->t);
1402 * Bind the connection to the peer and the tunnel to that peer.
1404 * If the peer has no tunnel, create one. Update tunnel and connection
1405 * data structres to reflect new status.
1407 * @param c Connection.
1411 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1413 GMP_add_tunnel (peer);
1414 c->t = GMP_get_tunnel (peer);
1415 GMT_add_connection (c->t, c);
1420 * Builds a path from a PeerIdentity array.
1422 * @param peers PeerIdentity array.
1423 * @param size Size of the @c peers array.
1424 * @param own_pos Output parameter: own position in the path.
1426 * @return Fixed and shortened path.
1428 static struct MeshPeerPath *
1429 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1431 unsigned int *own_pos)
1433 struct MeshPeerPath *path;
1434 GNUNET_PEER_Id shortid;
1437 unsigned int offset;
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1441 path = path_new (size);
1444 for (i = 0; i < size; i++)
1446 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1447 i, GNUNET_i2s (&peers[i]));
1448 shortid = GNUNET_PEER_intern (&peers[i]);
1450 /* Check for loops / duplicates */
1451 for (j = 0; j < i - offset; j++)
1453 if (path->peers[j] == shortid)
1455 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1457 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset);
1458 GNUNET_PEER_change_rc (shortid, -1);
1461 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1462 path->peers[i - offset] = shortid;
1463 if (path->peers[i] == myid)
1466 path->length -= offset;
1468 if (path->peers[*own_pos] != myid)
1470 /* create path: self not found in path through self */
1471 GNUNET_break_op (0);
1472 path_destroy (path);
1481 * Log receipt of message on stderr (INFO level).
1483 * @param message Message received.
1484 * @param peer Peer who sent the message.
1485 * @param hash Connection ID.
1488 log_message (const struct GNUNET_MessageHeader *message,
1489 const struct GNUNET_PeerIdentity *peer,
1490 const struct GNUNET_HashCode *hash)
1492 LOG (GNUNET_ERROR_TYPE_INFO, "<- %s on connection %s from %s\n",
1493 GM_m2s (ntohs (message->type)), GNUNET_h2s (hash), GNUNET_i2s (peer));
1496 /******************************************************************************/
1497 /******************************** API ***********************************/
1498 /******************************************************************************/
1501 * Core handler for connection creation.
1503 * @param cls Closure (unused).
1504 * @param peer Sender (neighbor).
1505 * @param message Message.
1507 * @return GNUNET_OK to keep the connection open,
1508 * GNUNET_SYSERR to close it (signal serious error)
1511 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1512 const struct GNUNET_MessageHeader *message)
1514 struct GNUNET_MESH_ConnectionCreate *msg;
1515 struct GNUNET_PeerIdentity *id;
1516 struct GNUNET_HashCode *cid;
1517 struct MeshPeerPath *path;
1518 struct MeshPeer *dest_peer;
1519 struct MeshPeer *orig_peer;
1520 struct MeshConnection *c;
1521 unsigned int own_pos;
1525 size = ntohs (message->size);
1526 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1528 GNUNET_break_op (0);
1532 /* Calculate hops */
1533 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1534 if (size % sizeof (struct GNUNET_PeerIdentity))
1536 GNUNET_break_op (0);
1539 size /= sizeof (struct GNUNET_PeerIdentity);
1542 GNUNET_break_op (0);
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1547 /* Get parameters */
1548 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1550 log_message (message, peer, cid);
1551 id = (struct GNUNET_PeerIdentity *) &msg[1];
1552 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1554 /* Create connection */
1555 c = connection_get (cid);
1558 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1564 GNUNET_break_op (0);
1565 path_destroy (path);
1568 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1569 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1570 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1573 if (path->length - 1 == own_pos)
1575 /* If we are destination, why did the creation fail? */
1579 send_broken2 (cid, &my_full_id,
1580 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1581 path->peers[own_pos - 1]);
1582 path_destroy (path);
1585 GMP_add_path_to_all (path, GNUNET_NO);
1586 connection_reset_timeout (c, GNUNET_YES);
1590 path = path_duplicate (c->path);
1592 if (MESH_CONNECTION_NEW == c->state)
1593 connection_change_state (c, MESH_CONNECTION_SENT);
1595 /* Remember peers */
1596 dest_peer = GMP_get (&id[size - 1]);
1597 orig_peer = GMP_get (&id[0]);
1599 /* Is it a connection to us? */
1600 if (c->own_pos == size - 1)
1602 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1603 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1605 add_to_peer (c, orig_peer);
1606 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1607 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1609 send_connection_ack (c, GNUNET_NO);
1610 if (MESH_CONNECTION_SENT == c->state)
1611 connection_change_state (c, MESH_CONNECTION_ACK);
1615 /* It's for somebody else! Retransmit. */
1616 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1617 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1618 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1619 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1622 path_destroy (path);
1628 * Core handler for path confirmations.
1630 * @param cls closure
1631 * @param message message
1632 * @param peer peer identity this notification is about
1634 * @return GNUNET_OK to keep the connection open,
1635 * GNUNET_SYSERR to close it (signal serious error)
1638 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1639 const struct GNUNET_MessageHeader *message)
1641 struct GNUNET_MESH_ConnectionACK *msg;
1642 struct MeshConnection *c;
1643 struct MeshPeerPath *p;
1644 struct MeshPeer *pi;
1645 enum MeshConnectionState oldstate;
1648 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1649 log_message (message, peer, &msg->cid);
1650 c = connection_get (&msg->cid);
1653 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1655 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1659 if (GNUNET_NO != c->destroy)
1661 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1665 oldstate = c->state;
1666 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1667 pi = GMP_get (peer);
1668 if (get_next_hop (c) == pi)
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1672 if (MESH_CONNECTION_SENT == oldstate)
1673 connection_change_state (c, MESH_CONNECTION_ACK);
1675 else if (get_prev_hop (c) == pi)
1677 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1679 connection_change_state (c, MESH_CONNECTION_READY);
1683 GNUNET_break_op (0);
1687 connection_reset_timeout (c, fwd);
1689 /* Add path to peers? */
1693 GMP_add_path_to_all (p, GNUNET_YES);
1700 /* Message for us as creator? */
1701 if (GMC_is_origin (c, GNUNET_YES))
1703 if (GNUNET_NO != fwd)
1705 GNUNET_break_op (0);
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1710 /* If just created, cancel the short timeout and start a long one */
1711 if (MESH_CONNECTION_SENT == oldstate)
1712 connection_reset_timeout (c, GNUNET_YES);
1714 /* Change connection state */
1715 connection_change_state (c, MESH_CONNECTION_READY);
1716 send_connection_ack (c, GNUNET_YES);
1718 /* Change tunnel state, trigger KX */
1719 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1720 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1725 /* Message for us as destination? */
1726 if (GMC_is_terminal (c, GNUNET_YES))
1728 if (GNUNET_YES != fwd)
1730 GNUNET_break_op (0);
1733 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1735 /* If just created, cancel the short timeout and start a long one */
1736 if (MESH_CONNECTION_ACK == oldstate)
1737 connection_reset_timeout (c, GNUNET_NO);
1739 /* Change tunnel state */
1740 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1741 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1746 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1747 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1753 * Core handler for notifications of broken paths
1755 * @param cls Closure (unused).
1756 * @param id Peer identity of sending neighbor.
1757 * @param message Message.
1759 * @return GNUNET_OK to keep the connection open,
1760 * GNUNET_SYSERR to close it (signal serious error)
1763 GMC_handle_broken (void* cls,
1764 const struct GNUNET_PeerIdentity* id,
1765 const struct GNUNET_MessageHeader* message)
1767 struct GNUNET_MESH_ConnectionBroken *msg;
1768 struct MeshConnection *c;
1771 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1772 log_message (message, id, &msg->cid);
1773 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1774 GNUNET_i2s (&msg->peer1));
1775 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1776 GNUNET_i2s (&msg->peer2));
1777 c = connection_get (&msg->cid);
1780 GNUNET_break_op (0);
1784 fwd = is_fwd (c, id);
1785 if (GMC_is_terminal (c, fwd))
1787 path_invalidate (c->path);
1788 if (0 < c->pending_messages)
1789 c->destroy = GNUNET_YES;
1795 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1796 c->destroy = GNUNET_YES;
1797 connection_cancel_queues (c, !fwd);
1806 * Core handler for tunnel destruction
1808 * @param cls Closure (unused).
1809 * @param peer Peer identity of sending neighbor.
1810 * @param message Message.
1812 * @return GNUNET_OK to keep the connection open,
1813 * GNUNET_SYSERR to close it (signal serious error)
1816 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1817 const struct GNUNET_MessageHeader *message)
1819 struct GNUNET_MESH_ConnectionDestroy *msg;
1820 struct MeshConnection *c;
1823 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1824 log_message (message, peer, &msg->cid);
1825 c = connection_get (&msg->cid);
1828 /* Probably already got the message from another path,
1829 * destroyed the tunnel and retransmitted to children.
1832 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1834 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1837 fwd = is_fwd (c, peer);
1838 if (GNUNET_SYSERR == fwd)
1840 GNUNET_break_op (0); /* FIXME */
1843 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1844 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1845 else if (0 == c->pending_messages)
1847 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1851 c->destroy = GNUNET_YES;
1852 c->state = MESH_CONNECTION_DESTROYED;
1855 GMT_remove_connection (c->t, c);
1863 * Generic handler for mesh network encrypted traffic.
1865 * @param peer Peer identity this notification is about.
1866 * @param msg Encrypted message.
1868 * @return GNUNET_OK to keep the connection open,
1869 * GNUNET_SYSERR to close it (signal serious error)
1872 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1873 const struct GNUNET_MESH_Encrypted *msg)
1875 struct MeshConnection *c;
1876 struct MeshPeer *neighbor;
1877 struct MeshFlowControl *fc;
1878 GNUNET_PEER_Id peer_id;
1884 log_message (&msg->header, peer, &msg->cid);
1887 size = ntohs (msg->header.size);
1889 sizeof (struct GNUNET_MESH_Encrypted) +
1890 sizeof (struct GNUNET_MessageHeader))
1892 GNUNET_break_op (0);
1896 /* Check connection */
1897 c = connection_get (&msg->cid);
1900 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1901 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
1902 GNUNET_h2s (&msg->cid));
1906 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s\n", GMC_2s (c));
1908 /* Check if origin is as expected */
1909 neighbor = get_prev_hop (c);
1910 peer_id = GNUNET_PEER_search (peer);
1911 if (peer_id == GMP_get_short_id (neighbor))
1917 neighbor = get_next_hop (c);
1918 if (peer_id == GMP_get_short_id (neighbor))
1924 /* Unexpected peer sending traffic on a connection. */
1925 GNUNET_break_op (0);
1931 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1932 pid = ntohl (msg->pid);
1933 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1935 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1936 LOG (GNUNET_ERROR_TYPE_DEBUG,
1937 "WARNING Received PID %u, (prev %u), ACK %u\n",
1938 pid, fc->last_pid_recv, fc->last_ack_sent);
1941 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1943 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1944 LOG (GNUNET_ERROR_TYPE_DEBUG,
1945 " Pid %u not expected (%u+), dropping!\n",
1946 pid, fc->last_pid_recv + 1);
1949 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1950 connection_change_state (c, MESH_CONNECTION_READY);
1951 connection_reset_timeout (c, fwd);
1952 fc->last_pid_recv = pid;
1954 /* Is this message for us? */
1955 if (GMC_is_terminal (c, fwd))
1957 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1958 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1962 GNUNET_break (GNUNET_NO != c->destroy);
1965 fc->last_pid_recv = pid;
1966 GMT_handle_encrypted (c->t, msg);
1967 GMC_send_ack (c, fwd, GNUNET_NO);
1971 /* Message not for us: forward to next hop */
1972 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1973 ttl = ntohl (msg->ttl);
1974 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1977 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1978 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1979 GMC_send_ack (c, fwd, GNUNET_NO);
1983 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1984 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1990 * Generic handler for mesh network encrypted traffic.
1992 * @param peer Peer identity this notification is about.
1993 * @param msg Encrypted message.
1995 * @return GNUNET_OK to keep the connection open,
1996 * GNUNET_SYSERR to close it (signal serious error)
1999 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
2000 const struct GNUNET_MESH_KX *msg)
2002 struct MeshConnection *c;
2003 struct MeshPeer *neighbor;
2004 GNUNET_PEER_Id peer_id;
2008 log_message (&msg->header, peer, &msg->cid);
2011 size = ntohs (msg->header.size);
2013 sizeof (struct GNUNET_MESH_KX) +
2014 sizeof (struct GNUNET_MessageHeader))
2016 GNUNET_break_op (0);
2020 /* Check connection */
2021 c = connection_get (&msg->cid);
2024 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2025 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx on unknown connection %s\n",
2026 GNUNET_h2s (&msg->cid));
2029 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
2031 /* Check if origin is as expected */
2032 neighbor = get_prev_hop (c);
2033 peer_id = GNUNET_PEER_search (peer);
2034 if (peer_id == GMP_get_short_id (neighbor))
2040 neighbor = get_next_hop (c);
2041 if (peer_id == GMP_get_short_id (neighbor))
2047 /* Unexpected peer sending traffic on a connection. */
2048 GNUNET_break_op (0);
2053 /* Count as connection confirmation. */
2054 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
2056 connection_change_state (c, MESH_CONNECTION_READY);
2059 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
2060 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
2063 connection_reset_timeout (c, fwd);
2065 /* Is this message for us? */
2066 if (GMC_is_terminal (c, fwd))
2068 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2069 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2075 GMT_handle_kx (c->t, &msg[1].header);
2079 /* Message not for us: forward to next hop */
2080 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2081 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2082 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
2089 * Core handler for encrypted mesh network traffic (channel mgmt, data).
2091 * @param cls Closure (unused).
2092 * @param message Message received.
2093 * @param peer Peer who sent the message.
2095 * @return GNUNET_OK to keep the connection open,
2096 * GNUNET_SYSERR to close it (signal serious error)
2099 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2100 const struct GNUNET_MessageHeader *message)
2102 return handle_mesh_encrypted (peer,
2103 (struct GNUNET_MESH_Encrypted *)message);
2108 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2110 * @param cls Closure (unused).
2111 * @param message Message received.
2112 * @param peer Peer who sent the message.
2114 * @return GNUNET_OK to keep the connection open,
2115 * GNUNET_SYSERR to close it (signal serious error)
2118 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2119 const struct GNUNET_MessageHeader *message)
2121 return handle_mesh_kx (peer,
2122 (struct GNUNET_MESH_KX *) message);
2127 * Core handler for mesh network traffic point-to-point acks.
2129 * @param cls closure
2130 * @param message message
2131 * @param peer peer identity this notification is about
2133 * @return GNUNET_OK to keep the connection open,
2134 * GNUNET_SYSERR to close it (signal serious error)
2137 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2138 const struct GNUNET_MessageHeader *message)
2140 struct GNUNET_MESH_ACK *msg;
2141 struct MeshConnection *c;
2142 struct MeshFlowControl *fc;
2147 msg = (struct GNUNET_MESH_ACK *) message;
2148 log_message (message, peer, &msg->cid);
2149 c = connection_get (&msg->cid);
2152 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2157 /* Is this a forward or backward ACK? */
2158 id = GNUNET_PEER_search (peer);
2159 if (GMP_get_short_id (get_next_hop (c)) == id)
2161 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2165 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2167 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2173 GNUNET_break_op (0);
2177 ack = ntohl (msg->ack);
2178 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2179 ack, fc->last_ack_recv);
2180 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
2181 fc->last_ack_recv = ack;
2183 /* Cancel polling if the ACK is big enough. */
2184 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2185 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2187 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2188 GNUNET_SCHEDULER_cancel (fc->poll_task);
2189 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2190 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2193 connection_unlock_queue (c, fwd);
2200 * Core handler for mesh network traffic point-to-point ack polls.
2202 * @param cls closure
2203 * @param message message
2204 * @param peer peer identity this notification is about
2206 * @return GNUNET_OK to keep the connection open,
2207 * GNUNET_SYSERR to close it (signal serious error)
2210 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2211 const struct GNUNET_MessageHeader *message)
2213 struct GNUNET_MESH_Poll *msg;
2214 struct MeshConnection *c;
2215 struct MeshFlowControl *fc;
2220 msg = (struct GNUNET_MESH_Poll *) message;
2221 log_message (message, peer, &msg->cid);
2222 c = connection_get (&msg->cid);
2225 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2227 LOG (GNUNET_ERROR_TYPE_DEBUG,
2228 "WARNING POLL message on unknown connection %s!\n",
2229 GNUNET_h2s (&msg->cid));
2233 /* Is this a forward or backward ACK?
2234 * Note: a poll should never be needed in a loopback case,
2235 * since there is no possiblility of packet loss there, so
2236 * this way of discerining FWD/BCK should not be a problem.
2238 id = GNUNET_PEER_search (peer);
2239 if (GMP_get_short_id (get_next_hop (c)) == id)
2241 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2244 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2246 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2251 GNUNET_break_op (0);
2255 pid = ntohl (msg->pid);
2256 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2257 fc->last_pid_recv = pid;
2258 fwd = fc == &c->bck_fc;
2259 GMC_send_ack (c, fwd, GNUNET_YES);
2266 * Send an ACK on the appropriate connection/channel, depending on
2267 * the direction and the position of the peer.
2269 * @param c Which connection to send the hop-by-hop ACK.
2270 * @param fwd Is this a fwd ACK? (will go dest->root).
2271 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2274 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2276 unsigned int buffer;
2278 LOG (GNUNET_ERROR_TYPE_DEBUG,
2279 "GMC send %s ACK on %s\n",
2280 GM_f2s (fwd), GMC_2s (c));
2288 if (GNUNET_NO != c->destroy)
2290 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2294 /* Get available buffer space */
2295 if (GMC_is_terminal (c, fwd))
2297 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2298 buffer = GMT_get_channels_buffer (c->t);
2302 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2303 buffer = GMC_get_buffer (c, fwd);
2305 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2306 if (0 == buffer && GNUNET_NO == force)
2309 /* Send available buffer space */
2310 if (GMC_is_origin (c, fwd))
2312 GNUNET_assert (NULL != c->t);
2313 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2314 GMT_unchoke_channels (c->t);
2318 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2319 send_ack (c, buffer, fwd, force);
2325 * Initialize the connections subsystem
2327 * @param c Configuration handle.
2330 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2332 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2334 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2337 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2338 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2339 GNUNET_SCHEDULER_shutdown ();
2344 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2347 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2348 "MESH", "MAX_CONNECTIONS", "MISSING");
2349 GNUNET_SCHEDULER_shutdown ();
2354 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2355 &refresh_connection_time))
2357 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2358 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2359 GNUNET_SCHEDULER_shutdown ();
2362 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2363 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2368 * Destroy each connection on shutdown.
2370 * @param cls Closure (unused).
2371 * @param key Current key code (CID, unused).
2372 * @param value Value in the hash map (connection)
2374 * @return #GNUNET_YES, because we should continue to iterate,
2377 shutdown_iterator (void *cls,
2378 const struct GNUNET_HashCode *key,
2381 struct MeshConnection *c = value;
2389 * Shut down the connections subsystem.
2394 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2395 GNUNET_CONTAINER_multihashmap_destroy (connections);
2400 struct MeshConnection *
2401 GMC_new (const struct GNUNET_HashCode *cid,
2402 struct MeshTunnel3 *t,
2403 struct MeshPeerPath *p,
2404 unsigned int own_pos)
2406 struct MeshConnection *c;
2408 c = GNUNET_new (struct MeshConnection);
2410 GNUNET_assert (GNUNET_OK ==
2411 GNUNET_CONTAINER_multihashmap_put (connections,
2413 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2414 fc_init (&c->fwd_fc);
2415 fc_init (&c->bck_fc);
2420 GNUNET_assert (own_pos <= p->length - 1);
2421 c->own_pos = own_pos;
2424 if (GNUNET_OK != register_neighbors (c))
2428 path_invalidate (c->path);
2441 GMC_destroy (struct MeshConnection *c)
2449 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2450 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2453 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2454 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2455 &c->fwd_fc, &c->bck_fc);
2456 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2457 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2459 /* Cancel all traffic */
2460 if (NULL != c->path)
2462 connection_cancel_queues (c, GNUNET_YES);
2463 connection_cancel_queues (c, GNUNET_NO);
2464 unregister_neighbors (c);
2467 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2468 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2470 /* Cancel maintainance task (keepalive/timeout) */
2471 if (NULL != c->fwd_fc.poll_msg)
2473 GMC_cancel (c->fwd_fc.poll_msg);
2474 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2476 if (NULL != c->bck_fc.poll_msg)
2478 GMC_cancel (c->bck_fc.poll_msg);
2479 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2482 /* Delete from tunnel */
2484 GMT_remove_connection (c->t, c);
2486 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2487 path_destroy (c->path);
2488 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2489 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2490 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2491 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2492 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2494 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2495 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2497 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2499 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2500 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2503 GNUNET_break (GNUNET_YES ==
2504 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2506 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2511 * Get the connection ID.
2513 * @param c Connection to get the ID from.
2515 * @return ID of the connection.
2517 const struct GNUNET_MeshHash *
2518 GMC_get_id (const struct MeshConnection *c)
2525 * Get the connection path.
2527 * @param c Connection to get the path from.
2529 * @return path used by the connection.
2531 const struct MeshPeerPath *
2532 GMC_get_path (const struct MeshConnection *c)
2534 if (GNUNET_NO == c->destroy)
2541 * Get the connection state.
2543 * @param c Connection to get the state from.
2545 * @return state of the connection.
2547 enum MeshConnectionState
2548 GMC_get_state (const struct MeshConnection *c)
2554 * Get the connection tunnel.
2556 * @param c Connection to get the tunnel from.
2558 * @return tunnel of the connection.
2560 struct MeshTunnel3 *
2561 GMC_get_tunnel (const struct MeshConnection *c)
2568 * Get free buffer space in a connection.
2570 * @param c Connection.
2571 * @param fwd Is query about FWD traffic?
2573 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2576 GMC_get_buffer (struct MeshConnection *c, int fwd)
2578 struct MeshFlowControl *fc;
2580 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2582 return (fc->queue_max - fc->queue_n);
2586 * Get how many messages have we allowed to send to us from a direction.
2588 * @param c Connection.
2589 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2591 * @return last_ack_sent - last_pid_recv
2594 GMC_get_allowed (struct MeshConnection *c, int fwd)
2596 struct MeshFlowControl *fc;
2598 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2599 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2603 return (fc->last_ack_sent - fc->last_pid_recv);
2607 * Get messages queued in a connection.
2609 * @param c Connection.
2610 * @param fwd Is query about FWD traffic?
2612 * @return Number of messages queued.
2615 GMC_get_qn (struct MeshConnection *c, int fwd)
2617 struct MeshFlowControl *fc;
2619 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2626 * Allow the connection to advertise a buffer of the given size.
2628 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2629 * allowing up to last_pid_recv + buffer.
2631 * @param c Connection.
2632 * @param buffer How many more messages the connection can accept.
2633 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2636 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2638 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2639 GMC_2s (c), buffer, GM_f2s (fwd));
2640 send_ack (c, buffer, fwd, GNUNET_NO);
2645 * Notify other peers on a connection of a broken link. Mark connections
2646 * to destroy after all traffic has been sent.
2648 * @param c Connection on which there has been a disconnection.
2649 * @param peer Peer that disconnected.
2652 GMC_notify_broken (struct MeshConnection *c,
2653 struct MeshPeer *peer)
2657 LOG (GNUNET_ERROR_TYPE_DEBUG,
2658 " notify broken on %s due to %s disconnect\n",
2659 GMC_2s (c), GMP_2s (peer));
2661 fwd = peer == get_prev_hop (c);
2663 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2665 /* Local shutdown, no one to notify about this. */
2669 if (GNUNET_NO == c->destroy)
2670 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2672 /* Connection will have at least one pending message
2673 * (the one we just scheduled), so no point in checking whether to
2674 * destroy immediately. */
2675 c->destroy = GNUNET_YES;
2676 c->state = MESH_CONNECTION_DESTROYED;
2679 * Cancel all queues, if no message is left, connection will be destroyed.
2681 connection_cancel_queues (c, !fwd);
2688 * Is this peer the first one on the connection?
2690 * @param c Connection.
2691 * @param fwd Is this about fwd traffic?
2693 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2696 GMC_is_origin (struct MeshConnection *c, int fwd)
2698 if (!fwd && c->path->length - 1 == c->own_pos )
2700 if (fwd && 0 == c->own_pos)
2707 * Is this peer the last one on the connection?
2709 * @param c Connection.
2710 * @param fwd Is this about fwd traffic?
2711 * Note that the ROOT is the terminal for BCK traffic!
2713 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2716 GMC_is_terminal (struct MeshConnection *c, int fwd)
2718 return GMC_is_origin (c, !fwd);
2723 * See if we are allowed to send by the next hop in the given direction.
2725 * @param c Connection.
2726 * @param fwd Is this about fwd traffic?
2728 * @return #GNUNET_YES in case it's OK to send.
2731 GMC_is_sendable (struct MeshConnection *c, int fwd)
2733 struct MeshFlowControl *fc;
2740 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2741 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2747 * Sends an already built message on a connection, properly registering
2748 * all used resources.
2750 * @param message Message to send. Function makes a copy of it.
2751 * If message is not hop-by-hop, decrements TTL of copy.
2752 * @param c Connection on which this message is transmitted.
2753 * @param fwd Is this a fwd message?
2754 * @param force Force the connection to accept the message (buffer overfill).
2755 * @param cont Continuation called once message is sent. Can be NULL.
2756 * @param cont_cls Closure for @c cont.
2758 * @return Handle to cancel the message before it's sent.
2759 * NULL on error or if @c cont is NULL.
2760 * Invalid on @c cont call.
2762 struct MeshConnectionQueue *
2763 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2764 struct MeshConnection *c, int fwd, int force,
2765 GMC_sent cont, void *cont_cls)
2767 struct MeshFlowControl *fc;
2768 struct MeshConnectionQueue *q;
2774 size = ntohs (message->size);
2775 data = GNUNET_malloc (size);
2776 memcpy (data, message, size);
2777 type = ntohs (message->type);
2778 LOG (GNUNET_ERROR_TYPE_INFO, "-> %s on connection %s (%u bytes)\n",
2779 GM_m2s (type), GMC_2s (c), size);
2781 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2782 droppable = GNUNET_NO == force;
2785 struct GNUNET_MESH_Encrypted *emsg;
2786 struct GNUNET_MESH_KX *kmsg;
2787 struct GNUNET_MESH_ACK *amsg;
2788 struct GNUNET_MESH_Poll *pmsg;
2789 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2790 struct GNUNET_MESH_ConnectionBroken *bmsg;
2793 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2794 emsg = (struct GNUNET_MESH_Encrypted *) data;
2795 ttl = ntohl (emsg->ttl);
2798 GNUNET_break_op (0);
2803 emsg->ttl = htonl (ttl - 1);
2804 emsg->pid = htonl (fc->next_pid++);
2805 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2806 if (GNUNET_YES == droppable)
2809 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2810 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2811 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2815 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2817 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2819 GMC_start_poll (c, fwd);
2823 case GNUNET_MESSAGE_TYPE_MESH_KX:
2824 kmsg = (struct GNUNET_MESH_KX *) data;
2828 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2829 amsg = (struct GNUNET_MESH_ACK *) data;
2831 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2832 droppable = GNUNET_NO;
2835 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2836 pmsg = (struct GNUNET_MESH_Poll *) data;
2838 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2839 droppable = GNUNET_NO;
2842 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2843 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2847 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2848 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2852 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2855 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2856 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2865 if (fc->queue_n > fc->queue_max && droppable)
2867 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2870 LOG (GNUNET_ERROR_TYPE_DEBUG,
2871 "queue full: %u/%u\n",
2872 fc->queue_n, fc->queue_max);
2873 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2879 return NULL; /* Drop this message */
2882 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2883 c->pending_messages++;
2885 q = GNUNET_new (struct MeshConnectionQueue);
2886 q->forced = !droppable;
2887 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2891 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2897 q->cont_cls = cont_cls;
2903 * Cancel a previously sent message while it's in the queue.
2905 * ONLY can be called before the continuation given to the send function
2906 * is called. Once the continuation is called, the message is no longer in the
2909 * @param q Handle to the queue.
2912 GMC_cancel (struct MeshConnectionQueue *q)
2914 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2916 /* queue destroy calls message_sent, which calls q->cont and frees q */
2917 GMP_queue_destroy (q->q, GNUNET_YES);
2922 * Sends a CREATE CONNECTION message for a path to a peer.
2923 * Changes the connection and tunnel states if necessary.
2925 * @param connection Connection to create.
2928 GMC_send_create (struct MeshConnection *connection)
2930 enum MeshTunnel3CState state;
2933 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2934 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2936 LOG (GNUNET_ERROR_TYPE_INFO, "=> %s on connection %s (%u bytes)\n",
2937 GM_m2s (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE),
2938 GMC_2s (connection), size);
2939 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2940 connection, connection->pending_messages);
2941 connection->pending_messages++;
2943 connection->maintenance_q =
2944 GMP_queue_add (get_next_hop (connection), NULL,
2945 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2946 size, connection, GNUNET_YES, &message_sent, NULL);
2948 state = GMT_get_cstate (connection->t);
2949 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2950 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2951 if (MESH_CONNECTION_NEW == connection->state)
2952 connection_change_state (connection, MESH_CONNECTION_SENT);
2957 * Send a message to all peers in this connection that the connection
2958 * is no longer valid.
2960 * If some peer should not receive the message, it should be zero'ed out
2961 * before calling this function.
2963 * @param c The connection whose peers to notify.
2966 GMC_send_destroy (struct MeshConnection *c)
2968 struct GNUNET_MESH_ConnectionDestroy msg;
2970 if (GNUNET_YES == c->destroy)
2973 msg.header.size = htons (sizeof (msg));
2974 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2976 LOG (GNUNET_ERROR_TYPE_DEBUG,
2977 " sending connection destroy for connection %s\n",
2980 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2981 GMC_send_prebuilt_message (&msg.header, c,
2982 GNUNET_YES, GNUNET_YES, NULL, NULL);
2983 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2984 GMC_send_prebuilt_message (&msg.header, c,
2985 GNUNET_NO, GNUNET_YES, NULL, NULL);
2986 c->destroy = GNUNET_YES;
2987 c->state = MESH_CONNECTION_DESTROYED;
2992 * @brief Start a polling timer for the connection.
2994 * When a neighbor does not accept more traffic on the connection it could be
2995 * caused by a simple congestion or by a lost ACK. Polling enables to check
2996 * for the lastest ACK status for a connection.
2998 * @param c Connection.
2999 * @param fwd Should we poll in the FWD direction?
3002 GMC_start_poll (struct MeshConnection *c, int fwd)
3004 struct MeshFlowControl *fc;
3006 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3007 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3009 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3011 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3012 fc->poll_task, fc->poll_msg);
3015 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3016 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3023 * @brief Stop polling a connection for ACKs.
3025 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3027 * @param c Connection.
3028 * @param fwd Should we stop the poll in the FWD direction?
3031 GMC_stop_poll (struct MeshConnection *c, int fwd)
3033 struct MeshFlowControl *fc;
3035 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3036 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3038 GNUNET_SCHEDULER_cancel (fc->poll_task);
3039 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3044 * Get a (static) string for a connection.
3046 * @param c Connection.
3049 GMC_2s (const struct MeshConnection *c)
3056 static char buf[128];
3058 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
3061 return GNUNET_h2s (&c->id);