2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh_connection.c
23 * @brief GNUnet MESH service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "mesh_path.h"
33 #include "mesh_protocol.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
42 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
43 GNUNET_TIME_UNIT_MINUTES,\
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct to encapsulate all the Flow Control information to a peer to which
54 * we are directly connected (on a core level).
56 struct MeshFlowControl
59 * Connection this controls.
61 struct MeshConnection *c;
64 * How many messages are in the queue on this connection.
69 * How many messages do we accept in the queue.
71 unsigned int queue_max;
79 * ID of the last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 GNUNET_SCHEDULER_TaskIdentifier poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
109 * Queued poll message, to cancel if not necessary anymore (got ACK).
111 struct MeshConnectionQueue *poll_msg;
114 * Queued poll message, to cancel if not necessary anymore (got ACK).
116 struct MeshConnectionQueue *ack_msg;
120 * Keep a record of the last messages sent on this connection.
122 struct MeshConnectionPerformance
125 * Circular buffer for storing measurements.
127 double usecsperbyte[AVG_MSGS];
130 * Running average of @c usecsperbyte.
135 * How many values of @c usecsperbyte are valid.
140 * Index of the next "free" position in @c usecsperbyte.
147 * Struct containing all information regarding a connection to a peer.
149 struct MeshConnection
152 * Tunnel this connection is part of.
154 struct MeshTunnel3 *t;
157 * Flow control information for traffic fwd.
159 struct MeshFlowControl fwd_fc;
162 * Flow control information for traffic bck.
164 struct MeshFlowControl bck_fc;
167 * Measure connection performance on the endpoint.
169 struct MeshConnectionPerformance *perf;
172 * ID of the connection.
174 struct GNUNET_HashCode id;
177 * State of the connection.
179 enum MeshConnectionState state;
182 * Path being used for the tunnel. At the origin of the connection
183 * it's a pointer to the destination's path pool, otherwise just a copy.
185 struct MeshPeerPath *path;
188 * Position of the local peer in the path.
190 unsigned int own_pos;
193 * Task to keep the used paths alive at the owner,
194 * time tunnel out on all the other peers.
196 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
199 * Task to keep the used paths alive at the destination,
200 * time tunnel out on all the other peers.
202 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
205 * Queue handle for maintainance traffic. One handle for FWD and BCK since
206 * one peer never needs to maintain both directions (no loopback connections).
208 struct MeshPeerQueue *maintenance_q;
211 * Counter to do exponential backoff when creating a connection (max 64).
213 unsigned short create_retry;
216 * Pending message count.
218 int pending_messages;
221 * Destroy flag: if true, destroy on last message.
227 * Handle for messages queued but not yet sent.
229 struct MeshConnectionQueue
232 * Peer queue handle, to cancel if necessary.
234 struct MeshPeerQueue *q;
237 * Was this a forced message? (Do not account for it)
242 * Continuation to call once sent.
247 * Closure for @c cont.
252 /******************************************************************************/
253 /******************************* GLOBALS ***********************************/
254 /******************************************************************************/
257 * Global handle to the statistics service.
259 extern struct GNUNET_STATISTICS_Handle *stats;
262 * Local peer own ID (memory efficient handle).
264 extern GNUNET_PEER_Id myid;
267 * Local peer own ID (full value).
269 extern struct GNUNET_PeerIdentity my_full_id;
272 * Connections known, indexed by cid (MeshConnection).
274 static struct GNUNET_CONTAINER_MultiHashMap *connections;
277 * How many connections are we willing to maintain.
278 * Local connections are always allowed, even if there are more connections than max.
280 static unsigned long long max_connections;
283 * How many messages *in total* are we willing to queue, divide by number of
284 * connections to get connection queue size.
286 static unsigned long long max_msgs_queue;
289 * How often to send path keepalives. Paths timeout after 4 missed.
291 static struct GNUNET_TIME_Relative refresh_connection_time;
294 * How often to send path create / ACKs.
296 static struct GNUNET_TIME_Relative create_connection_time;
299 /******************************************************************************/
300 /******************************** STATIC ***********************************/
301 /******************************************************************************/
303 #if 0 // avoid compiler warning for unused static function
305 fc_debug (struct MeshFlowControl *fc)
307 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
308 fc->last_pid_recv, fc->last_ack_sent);
309 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
310 fc->last_pid_sent, fc->last_ack_recv);
311 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
312 fc->queue_n, fc->queue_max);
316 connection_debug (struct MeshConnection *c)
320 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
324 peer2s (c->t->peer), GMC_2s (c));
325 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
326 c->state, c->pending_messages);
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
328 fc_debug (&c->fwd_fc);
329 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
330 fc_debug (&c->bck_fc);
336 * Schedule next keepalive task, taking in consideration
337 * the connection state and number of retries.
339 * @param c Connection for which to schedule the next keepalive.
340 * @param fwd Direction for the next keepalive.
343 schedule_next_keepalive (struct MeshConnection *c, int fwd);
347 * Get string description for tunnel state.
349 * @param s Tunnel state.
351 * @return String representation.
354 GMC_state2s (enum MeshConnectionState s)
358 case MESH_CONNECTION_NEW:
359 return "MESH_CONNECTION_NEW";
360 case MESH_CONNECTION_SENT:
361 return "MESH_CONNECTION_SENT";
362 case MESH_CONNECTION_ACK:
363 return "MESH_CONNECTION_ACK";
364 case MESH_CONNECTION_READY:
365 return "MESH_CONNECTION_READY";
366 case MESH_CONNECTION_DESTROYED:
367 return "MESH_CONNECTION_DESTROYED";
369 return "MESH_CONNECTION_STATE_ERROR";
375 * Initialize a Flow Control structure to the initial state.
377 * @param fc Flow Control structure to initialize.
380 fc_init (struct MeshFlowControl *fc)
383 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
384 fc->last_pid_recv = (uint32_t) -1;
385 fc->last_ack_sent = (uint32_t) 0;
386 fc->last_ack_recv = (uint32_t) 0;
387 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
388 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
390 fc->queue_max = (max_msgs_queue / max_connections) + 1;
397 * @param cid Connection ID.
399 static struct MeshConnection *
400 connection_get (const struct GNUNET_HashCode *cid)
402 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
407 connection_change_state (struct MeshConnection* c,
408 enum MeshConnectionState state)
410 LOG (GNUNET_ERROR_TYPE_DEBUG,
411 "Connection %s state was %s\n",
412 GMC_2s (c), GMC_state2s (c->state));
413 if (MESH_CONNECTION_DESTROYED == c->state)
415 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
418 LOG (GNUNET_ERROR_TYPE_DEBUG,
419 "Connection %s state is now %s\n",
420 GMC_2s (c), GMC_state2s (state));
422 if (MESH_CONNECTION_READY == state)
428 * Callback called when a queued ACK message is sent.
430 * @param cls Closure (FC).
431 * @param c Connection this message was on.
432 * @param q Queue handler this call invalidates.
433 * @param type Type of message sent.
434 * @param fwd Was this a FWD going message?
435 * @param size Size of the message.
439 struct MeshConnection *c,
440 struct MeshConnectionQueue *q,
441 uint16_t type, int fwd, size_t size)
443 struct MeshFlowControl *fc = cls;
450 * Send an ACK on the connection, informing the predecessor about
451 * the available buffer space. Should not be called in case the peer
452 * is origin (no predecessor) in the @c fwd direction.
454 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
455 * the ACK itself goes "back" (dest->root).
457 * @param c Connection on which to send the ACK.
458 * @param buffer How much space free to advertise?
459 * @param fwd Is this FWD ACK? (Going dest -> root)
460 * @param force Don't optimize out.
463 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
465 struct MeshFlowControl *next_fc;
466 struct MeshFlowControl *prev_fc;
467 struct GNUNET_MESH_ACK msg;
471 /* If origin, there is no connection to send ACKs. Wrong function! */
472 if (GMC_is_origin (c, fwd))
474 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
475 GMC_2s (c), GM_f2s (fwd));
480 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
481 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
483 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
484 GM_f2s (fwd), GMC_2s (c));
486 /* Check if we need to transmit the ACK. */
487 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
488 if (3 < delta && buffer < delta && GNUNET_NO == force)
490 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 " last pid recv: %u, last ack sent: %u\n",
493 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
497 /* Ok, ACK might be necessary, what PID to ACK? */
498 ack = prev_fc->last_pid_recv + buffer;
499 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
500 LOG (GNUNET_ERROR_TYPE_DEBUG,
501 " last pid %u, last ack %u, qmax %u, q %u\n",
502 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
503 next_fc->queue_max, next_fc->queue_n);
504 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
510 /* Check if message is already in queue */
511 if (NULL != prev_fc->ack_msg)
513 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
515 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
516 GMC_cancel (prev_fc->ack_msg);
517 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
521 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
526 prev_fc->last_ack_sent = ack;
528 /* Build ACK message and send on connection */
529 msg.header.size = htons (sizeof (msg));
530 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
531 msg.ack = htonl (ack);
534 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
541 * Callback called when a queued message is sent.
543 * Calculates the average time and connection packet tracking.
545 * @param cls Closure (ConnectionQueue Handle).
546 * @param c Connection this message was on.
547 * @param type Type of message sent.
548 * @param fwd Was this a FWD going message?
549 * @param size Size of the message.
550 * @param wait Time spent waiting for core (only the time for THIS message)
553 message_sent (void *cls,
554 struct MeshConnection *c, uint16_t type,
555 int fwd, size_t size,
556 struct GNUNET_TIME_Relative wait)
558 struct MeshConnectionPerformance *p;
559 struct MeshFlowControl *fc;
560 struct MeshConnectionQueue *q = cls;
564 fc = fwd ? &c->fwd_fc : &c->bck_fc;
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
576 q->cont (q->cont_cls, c, q, type, fwd, size);
580 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
582 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
589 c->pending_messages--;
590 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
592 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
596 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
599 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
600 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
601 c->maintenance_q = NULL;
602 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
603 if (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE == type || !fwd)
604 schedule_next_keepalive (c, fwd);
607 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
610 if (GNUNET_NO == forced)
613 LOG (GNUNET_ERROR_TYPE_DEBUG,
614 "! accounting pid %u\n",
619 LOG (GNUNET_ERROR_TYPE_DEBUG,
620 "! forced, Q_N not accounting pid %u\n",
623 GMC_send_ack (c, fwd, GNUNET_NO);
626 case GNUNET_MESSAGE_TYPE_MESH_POLL:
630 case GNUNET_MESSAGE_TYPE_MESH_ACK:
637 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
640 return; /* Only endpoints are interested in timing. */
643 usecsperbyte = ((double) wait.rel_value_us) / size;
644 if (p->size == AVG_MSGS)
646 /* Array is full. Substract oldest value, add new one and store. */
647 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
648 p->usecsperbyte[p->idx] = usecsperbyte;
649 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
653 /* Array not yet full. Add current value to avg and store. */
654 p->usecsperbyte[p->idx] = usecsperbyte;
656 p->avg += p->usecsperbyte[p->idx];
660 p->idx = (p->idx + 1) % AVG_MSGS;
665 * Get the previous hop in a connection
667 * @param c Connection.
669 * @return Previous peer in the connection.
671 static struct MeshPeer *
672 get_prev_hop (const struct MeshConnection *c)
676 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
677 GMC_2s (c), c->own_pos, c->path->length);
678 if (0 == c->own_pos || c->path->length < 2)
680 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is zero\n");
681 id = c->path->peers[0];
685 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is NOT zero\n");
686 id = c->path->peers[c->own_pos - 1];
689 LOG (GNUNET_ERROR_TYPE_DEBUG, " id: %u\n", id);
690 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s\n", GNUNET_i2s (GNUNET_PEER_resolve2 (id)));
691 return GMP_get_short (id);
696 * Get the next hop in a connection
698 * @param c Connection.
700 * @return Next peer in the connection.
702 static struct MeshPeer *
703 get_next_hop (const struct MeshConnection *c)
707 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
708 GMC_2s (c), c->own_pos, c->path->length);
709 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
711 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is end\n");
712 id = c->path->peers[c->path->length - 1];
716 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is NOT end\n");
717 id = c->path->peers[c->own_pos + 1];
720 LOG (GNUNET_ERROR_TYPE_DEBUG, " id: %u\n", id);
721 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s\n", GNUNET_i2s (GNUNET_PEER_resolve2 (id)));
723 return GMP_get_short (id);
728 * Get the hop in a connection.
730 * @param c Connection.
731 * @param fwd Next hop?
733 * @return Next peer in the connection.
735 static struct MeshPeer *
736 get_hop (struct MeshConnection *c, int fwd)
739 return get_next_hop (c);
740 return get_prev_hop (c);
745 * Is traffic coming from this sender 'FWD' traffic?
747 * @param c Connection to check.
748 * @param sender Peer identity of neighbor.
750 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
751 * the traffic is 'FWD'.
752 * #GNUNET_NO for BCK.
753 * #GNUNET_SYSERR for errors.
756 is_fwd (const struct MeshConnection *c,
757 const struct GNUNET_PeerIdentity *sender)
761 id = GNUNET_PEER_search (sender);
762 if (GMP_get_short_id (get_prev_hop (c)) == id)
765 if (GMP_get_short_id (get_next_hop (c)) == id)
769 return GNUNET_SYSERR;
774 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
775 * or a first CONNECTION_ACK directed to us.
777 * @param connection Connection to confirm.
778 * @param fwd Should we send it FWD? (root->dest)
779 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
782 send_connection_ack (struct MeshConnection *connection, int fwd)
784 struct MeshTunnel3 *t;
787 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
789 GMP_queue_add (get_hop (connection, fwd), NULL,
790 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
791 sizeof (struct GNUNET_MESH_ConnectionACK),
792 connection, fwd, &message_sent, NULL);
793 connection->pending_messages++;
794 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
795 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
796 if (MESH_CONNECTION_READY != connection->state)
797 connection_change_state (connection, MESH_CONNECTION_SENT);
802 * Send a notification that a connection is broken.
804 * @param c Connection that is broken.
805 * @param id1 Peer that has disconnected.
806 * @param id2 Peer that has disconnected.
807 * @param fwd Direction towards which to send it.
810 send_broken (struct MeshConnection *c,
811 const struct GNUNET_PeerIdentity *id1,
812 const struct GNUNET_PeerIdentity *id2,
815 struct GNUNET_MESH_ConnectionBroken msg;
817 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
818 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
822 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
827 * Send a notification that a connection is broken, when a connection
828 * isn't even created.
830 * @param connection_id Connection ID.
831 * @param id1 Peer that has disconnected.
832 * @param id2 Peer that has disconnected.
833 * @param peer Peer to notify (neighbor who sent the connection).
836 send_broken2 (struct GNUNET_HashCode *connection_id,
837 const struct GNUNET_PeerIdentity *id1,
838 const struct GNUNET_PeerIdentity *id2,
839 GNUNET_PEER_Id peer_id)
841 struct GNUNET_MESH_ConnectionBroken *msg;
842 struct MeshPeer *neighbor;
844 msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
845 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
846 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
847 msg->cid = *connection_id;
850 neighbor = GMP_get_short (peer_id);
851 GMP_queue_add (neighbor, msg,
852 GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED,
853 sizeof (struct GNUNET_MESH_ConnectionBroken),
854 NULL, GNUNET_SYSERR, /* connection, fwd */
855 NULL, NULL); /* continuation */
860 * Send keepalive packets for a connection.
862 * @param c Connection to keep alive..
863 * @param fwd Is this a FWD keepalive? (owner -> dest).
866 connection_keepalive (struct MeshConnection *c, int fwd)
868 struct GNUNET_MESH_ConnectionKeepAlive *msg;
869 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
872 LOG (GNUNET_ERROR_TYPE_DEBUG,
873 "sending %s keepalive for connection %s]\n",
874 GM_f2s (fwd), GMC_2s (c));
876 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
877 msg->header.size = htons (size);
878 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
880 msg->reserved = htonl (0);
882 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
887 * Send CONNECTION_{CREATE/ACK} packets for a connection.
889 * @param c Connection for which to send the message.
890 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
893 connection_recreate (struct MeshConnection *c, int fwd)
895 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
899 send_connection_ack (c, GNUNET_NO);
904 * Generic connection timer management.
905 * Depending on the role of the peer in the connection will send the
906 * appropriate message (build or keepalive)
908 * @param c Conncetion to maintain.
912 connection_maintain (struct MeshConnection *c, int fwd)
914 if (GNUNET_NO != c->destroy)
917 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
919 /* TODO DHT GET with RO_BART */
924 case MESH_CONNECTION_NEW:
927 case MESH_CONNECTION_SENT:
928 connection_recreate (c, fwd);
930 case MESH_CONNECTION_READY:
931 connection_keepalive (c, fwd);
940 * Keep the connection alive in the FWD direction.
942 * @param cls Closure (connection to keepalive).
943 * @param tc TaskContext.
946 connection_fwd_keepalive (void *cls,
947 const struct GNUNET_SCHEDULER_TaskContext *tc)
949 struct MeshConnection *c = cls;
951 LOG (GNUNET_ERROR_TYPE_DEBUG, "FWD keepalive for %s\n", GMC_2s (c));
952 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
953 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
956 connection_maintain (c, GNUNET_YES);
958 /* Next execution will be scheduled by message_sent */
963 * Keep the connection alive in the BCK direction.
965 * TODO refactor and merge with connection_fwd_keepalive.
967 * @param cls Closure (connection to keepalive).
968 * @param tc TaskContext.
971 connection_bck_keepalive (void *cls,
972 const struct GNUNET_SCHEDULER_TaskContext *tc)
974 struct MeshConnection *c = cls;
976 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
977 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
980 connection_maintain (c, GNUNET_NO);
982 /* Next execution will be scheduled by message_sent */
987 * Schedule next keepalive task, taking in consideration
988 * the connection state and number of retries.
990 * If the peer is not the origin, do nothing.
992 * @param c Connection for which to schedule the next keepalive.
993 * @param fwd Direction for the next keepalive.
996 schedule_next_keepalive (struct MeshConnection *c, int fwd)
998 struct GNUNET_TIME_Relative delay;
999 GNUNET_SCHEDULER_TaskIdentifier *task_id;
1000 GNUNET_SCHEDULER_Task keepalive_task;
1002 if (GNUNET_NO == GMC_is_origin (c, fwd))
1005 /* Calculate delay to use, depending on the state of the connection */
1006 if (MESH_CONNECTION_READY == c->state)
1008 delay = refresh_connection_time;
1012 if (1 > c->create_retry)
1013 c->create_retry = 1;
1014 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1016 if (c->create_retry < 64)
1017 c->create_retry *= 2;
1020 /* Select direction-dependent parameters */
1021 if (GNUNET_YES == fwd)
1023 task_id = &c->fwd_maintenance_task;
1024 keepalive_task = &connection_fwd_keepalive;
1028 task_id = &c->bck_maintenance_task;
1029 keepalive_task = &connection_bck_keepalive;
1032 /* Check that no one scheduled it before us (and alert in that case) */
1033 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1036 GNUNET_SCHEDULER_cancel (*task_id);
1039 /* Schedule the task */
1040 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1045 * @brief Re-initiate traffic on this connection if necessary.
1047 * Check if there is traffic queued towards this peer
1048 * and the core transmit handle is NULL (traffic was stalled).
1049 * If so, call core tmt rdy.
1051 * @param c Connection on which initiate traffic.
1052 * @param fwd Is this about fwd traffic?
1055 connection_unlock_queue (struct MeshConnection *c, int fwd)
1057 struct MeshPeer *peer;
1059 LOG (GNUNET_ERROR_TYPE_DEBUG,
1060 "connection_unlock_queue %s on %s\n",
1061 GM_f2s (fwd), GMC_2s (c));
1063 if (GMC_is_terminal (c, fwd))
1065 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1069 peer = get_hop (c, fwd);
1070 GMP_queue_unlock (peer, c);
1075 * Cancel all transmissions that belong to a certain connection.
1077 * If the connection is scheduled for destruction and no more messages are left,
1078 * the connection will be destroyed by the continuation call.
1080 * @param c Connection which to cancel. Might be destroyed during this call.
1081 * @param fwd Cancel fwd traffic?
1084 connection_cancel_queues (struct MeshConnection *c, int fwd)
1086 struct MeshFlowControl *fc;
1087 struct MeshPeer *peer;
1089 LOG (GNUNET_ERROR_TYPE_DEBUG,
1090 " *** Cancel %s queues for connection %s\n",
1091 GM_f2s (fwd), GMC_2s (c));
1098 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1099 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1101 GNUNET_SCHEDULER_cancel (fc->poll_task);
1102 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1103 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1105 peer = get_hop (c, fwd);
1106 GMP_queue_cancel (peer, c);
1111 * Function called if a connection has been stalled for a while,
1112 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1114 * @param cls Closure (poll ctx).
1115 * @param tc TaskContext.
1118 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1122 * Callback called when a queued POLL message is sent.
1124 * @param cls Closure (FC).
1125 * @param c Connection this message was on.
1126 * @param q Queue handler this call invalidates.
1127 * @param type Type of message sent.
1128 * @param fwd Was this a FWD going message?
1129 * @param size Size of the message.
1132 poll_sent (void *cls,
1133 struct MeshConnection *c,
1134 struct MeshConnectionQueue *q,
1135 uint16_t type, int fwd, size_t size)
1137 struct MeshFlowControl *fc = cls;
1139 if (2 == c->destroy)
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1144 LOG (GNUNET_ERROR_TYPE_DEBUG,
1145 " *** POLL sent for , scheduling new one!\n");
1146 fc->poll_msg = NULL;
1147 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1148 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1149 &connection_poll, fc);
1150 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1155 * Function called if a connection has been stalled for a while,
1156 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1158 * @param cls Closure (poll ctx).
1159 * @param tc TaskContext.
1162 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1164 struct MeshFlowControl *fc = cls;
1165 struct GNUNET_MESH_Poll msg;
1166 struct MeshConnection *c;
1168 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1169 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1175 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1177 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1178 fc == &c->fwd_fc ? "FWD" : "BCK");
1180 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1181 msg.header.size = htons (sizeof (msg));
1182 msg.pid = htonl (fc->last_pid_sent);
1183 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1184 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1185 fc == &c->fwd_fc, GNUNET_YES,
1191 * Timeout function due to lack of keepalive/traffic from the owner.
1192 * Destroys connection if called.
1194 * @param cls Closure (connection to destroy).
1195 * @param tc TaskContext.
1198 connection_fwd_timeout (void *cls,
1199 const struct GNUNET_SCHEDULER_TaskContext *tc)
1201 struct MeshConnection *c = cls;
1203 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1204 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1207 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1209 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1220 * Timeout function due to lack of keepalive/traffic from the destination.
1221 * Destroys connection if called.
1223 * @param cls Closure (connection to destroy).
1224 * @param tc TaskContext
1227 connection_bck_timeout (void *cls,
1228 const struct GNUNET_SCHEDULER_TaskContext *tc)
1230 struct MeshConnection *c = cls;
1232 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1233 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1239 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1250 * Resets the connection timeout task, some other message has done the
1252 * - For the first peer on the direction this means to send
1253 * a keepalive or a path confirmation message (either create or ACK).
1254 * - For all other peers, this means to destroy the connection,
1255 * due to lack of activity.
1256 * Starts the timeout if no timeout was running (connection just created).
1258 * @param c Connection whose timeout to reset.
1259 * @param fwd Is this forward?
1261 * TODO use heap to improve efficiency of scheduler.
1264 connection_reset_timeout (struct MeshConnection *c, int fwd)
1266 GNUNET_SCHEDULER_TaskIdentifier *ti;
1267 GNUNET_SCHEDULER_Task f;
1269 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1271 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1273 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1274 GNUNET_SCHEDULER_cancel (*ti);
1276 if (GMC_is_origin (c, fwd)) /* Startpoint */
1278 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1279 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1281 else /* Relay, endpoint. */
1283 struct GNUNET_TIME_Relative delay;
1285 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1286 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1287 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1293 * Add the connection to the list of both neighbors.
1295 * @param c Connection.
1297 * @return #GNUNET_OK if everything went fine
1298 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1301 register_neighbors (struct MeshConnection *c)
1303 struct MeshPeer *next_peer;
1304 struct MeshPeer *prev_peer;
1306 next_peer = get_next_hop (c);
1307 prev_peer = get_prev_hop (c);
1309 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1311 path_debug (c->path);
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1313 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1314 GMC_2s (c), next_peer);
1315 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GMP_2s (next_peer));
1316 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1317 GMC_2s (c), prev_peer);
1318 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GMP_2s (prev_peer));
1320 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1321 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1323 if (GMC_is_origin (c, GNUNET_YES))
1324 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1325 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1327 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1328 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1329 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1330 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1331 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1332 return GNUNET_SYSERR;
1335 GMP_add_connection (next_peer, c);
1336 GMP_add_connection (prev_peer, c);
1343 * Remove the connection from the list of both neighbors.
1345 * @param c Connection.
1348 unregister_neighbors (struct MeshConnection *c)
1350 struct MeshPeer *peer;
1352 peer = get_next_hop (c);
1353 if (GNUNET_OK != GMP_remove_connection (peer, c))
1355 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1356 || MESH_CONNECTION_DESTROYED == c->state);
1357 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1358 if (NULL != c->t) GMT_debug (c->t);
1361 peer = get_prev_hop (c);
1362 if (GNUNET_OK != GMP_remove_connection (peer, c))
1364 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1365 || MESH_CONNECTION_DESTROYED == c->state);
1366 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1367 if (NULL != c->t) GMT_debug (c->t);
1373 * Bind the connection to the peer and the tunnel to that peer.
1375 * If the peer has no tunnel, create one. Update tunnel and connection
1376 * data structres to reflect new status.
1378 * @param c Connection.
1382 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1384 GMP_add_tunnel (peer);
1385 c->t = GMP_get_tunnel (peer);
1386 GMT_add_connection (c->t, c);
1391 * Builds a path from a PeerIdentity array.
1393 * @param peers PeerIdentity array.
1394 * @param size Size of the @c peers array.
1395 * @param own_pos Output parameter: own position in the path.
1397 * @return Fixed and shortened path.
1399 static struct MeshPeerPath *
1400 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1402 unsigned int *own_pos)
1404 struct MeshPeerPath *path;
1405 GNUNET_PEER_Id shortid;
1408 unsigned int offset;
1411 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1412 path = path_new (size);
1415 for (i = 0; i < size; i++)
1417 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1418 i, GNUNET_i2s (&peers[i]));
1419 shortid = GNUNET_PEER_intern (&peers[i]);
1421 /* Check for loops / duplicates */
1422 for (j = 0; j < i - offset; j++)
1424 if (path->peers[j] == shortid)
1426 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset);
1429 GNUNET_PEER_change_rc (shortid, -1);
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1433 path->peers[i - offset] = shortid;
1434 if (path->peers[i] == myid)
1437 path->length -= offset;
1439 if (path->peers[*own_pos] != myid)
1441 /* create path: self not found in path through self */
1442 GNUNET_break_op (0);
1443 path_destroy (path);
1450 /******************************************************************************/
1451 /******************************** API ***********************************/
1452 /******************************************************************************/
1455 * Core handler for connection creation.
1457 * @param cls Closure (unused).
1458 * @param peer Sender (neighbor).
1459 * @param message Message.
1461 * @return GNUNET_OK to keep the connection open,
1462 * GNUNET_SYSERR to close it (signal serious error)
1465 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1466 const struct GNUNET_MessageHeader *message)
1468 struct GNUNET_MESH_ConnectionCreate *msg;
1469 struct GNUNET_PeerIdentity *id;
1470 struct GNUNET_HashCode *cid;
1471 struct MeshPeerPath *path;
1472 struct MeshPeer *dest_peer;
1473 struct MeshPeer *orig_peer;
1474 struct MeshConnection *c;
1475 unsigned int own_pos;
1478 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1479 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1482 size = ntohs (message->size);
1483 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1485 GNUNET_break_op (0);
1489 /* Calculate hops */
1490 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1491 if (size % sizeof (struct GNUNET_PeerIdentity))
1493 GNUNET_break_op (0);
1496 size /= sizeof (struct GNUNET_PeerIdentity);
1499 GNUNET_break_op (0);
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1504 /* Get parameters */
1505 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1507 id = (struct GNUNET_PeerIdentity *) &msg[1];
1508 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s (%s->).\n",
1509 GNUNET_h2s (cid), GNUNET_i2s (id));
1511 /* Create connection */
1512 c = connection_get (cid);
1515 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1521 GNUNET_break_op (0);
1522 path_destroy (path);
1525 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1526 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1527 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1530 if (path->length - 1 == own_pos)
1532 /* If we are destination, why did the creation fail? */
1536 send_broken2 (cid, &my_full_id,
1537 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1538 path->peers[own_pos - 1]);
1539 path_destroy (path);
1542 GMP_add_path_to_all (path, GNUNET_NO);
1543 connection_reset_timeout (c, GNUNET_YES);
1547 path = path_duplicate (c->path);
1549 if (MESH_CONNECTION_NEW == c->state)
1550 connection_change_state (c, MESH_CONNECTION_SENT);
1552 /* Remember peers */
1553 dest_peer = GMP_get (&id[size - 1]);
1554 orig_peer = GMP_get (&id[0]);
1556 /* Is it a connection to us? */
1557 if (c->own_pos == size - 1)
1559 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1560 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1562 add_to_peer (c, orig_peer);
1563 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1564 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1566 send_connection_ack (c, GNUNET_NO);
1567 if (MESH_CONNECTION_SENT == c->state)
1568 connection_change_state (c, MESH_CONNECTION_ACK);
1572 /* It's for somebody else! Retransmit. */
1573 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1574 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1575 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1576 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1579 path_destroy (path);
1585 * Core handler for path confirmations.
1587 * @param cls closure
1588 * @param message message
1589 * @param peer peer identity this notification is about
1591 * @return GNUNET_OK to keep the connection open,
1592 * GNUNET_SYSERR to close it (signal serious error)
1595 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1596 const struct GNUNET_MessageHeader *message)
1598 struct GNUNET_MESH_ConnectionACK *msg;
1599 struct MeshConnection *c;
1600 struct MeshPeerPath *p;
1601 struct MeshPeer *pi;
1602 enum MeshConnectionState oldstate;
1605 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1606 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1607 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1608 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1609 GNUNET_h2s (&msg->cid));
1610 c = connection_get (&msg->cid);
1613 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1619 if (GNUNET_NO != c->destroy)
1621 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1625 oldstate = c->state;
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1627 pi = GMP_get (peer);
1628 if (get_next_hop (c) == pi)
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1632 if (MESH_CONNECTION_SENT == oldstate)
1633 connection_change_state (c, MESH_CONNECTION_ACK);
1635 else if (get_prev_hop (c) == pi)
1637 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1639 connection_change_state (c, MESH_CONNECTION_READY);
1643 GNUNET_break_op (0);
1647 connection_reset_timeout (c, fwd);
1649 /* Add path to peers? */
1653 GMP_add_path_to_all (p, GNUNET_YES);
1660 /* Message for us as creator? */
1661 if (GMC_is_origin (c, GNUNET_YES))
1663 if (GNUNET_NO != fwd)
1665 GNUNET_break_op (0);
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1670 /* If just created, cancel the short timeout and start a long one */
1671 if (MESH_CONNECTION_SENT == oldstate)
1672 connection_reset_timeout (c, GNUNET_YES);
1674 /* Change connection state */
1675 connection_change_state (c, MESH_CONNECTION_READY);
1676 send_connection_ack (c, GNUNET_YES);
1678 /* Change tunnel state, trigger KX */
1679 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1680 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1685 /* Message for us as destination? */
1686 if (GMC_is_terminal (c, GNUNET_YES))
1688 if (GNUNET_YES != fwd)
1690 GNUNET_break_op (0);
1693 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1695 /* If just created, cancel the short timeout and start a long one */
1696 if (MESH_CONNECTION_ACK == oldstate)
1697 connection_reset_timeout (c, GNUNET_NO);
1699 /* Change tunnel state */
1700 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1701 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1706 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1707 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1713 * Core handler for notifications of broken paths
1715 * @param cls Closure (unused).
1716 * @param id Peer identity of sending neighbor.
1717 * @param message Message.
1719 * @return GNUNET_OK to keep the connection open,
1720 * GNUNET_SYSERR to close it (signal serious error)
1723 GMC_handle_broken (void* cls,
1724 const struct GNUNET_PeerIdentity* id,
1725 const struct GNUNET_MessageHeader* message)
1727 struct GNUNET_MESH_ConnectionBroken *msg;
1728 struct MeshConnection *c;
1731 LOG (GNUNET_ERROR_TYPE_DEBUG,
1732 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1733 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1734 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1735 GNUNET_i2s (&msg->peer1));
1736 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1737 GNUNET_i2s (&msg->peer2));
1738 c = connection_get (&msg->cid);
1741 GNUNET_break_op (0);
1745 fwd = is_fwd (c, id);
1746 if (GMC_is_terminal (c, fwd))
1748 path_invalidate (c->path);
1749 if (0 < c->pending_messages)
1750 c->destroy = GNUNET_YES;
1756 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1757 c->destroy = GNUNET_YES;
1758 connection_cancel_queues (c, !fwd);
1767 * Core handler for tunnel destruction
1769 * @param cls Closure (unused).
1770 * @param peer Peer identity of sending neighbor.
1771 * @param message Message.
1773 * @return GNUNET_OK to keep the connection open,
1774 * GNUNET_SYSERR to close it (signal serious error)
1777 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1778 const struct GNUNET_MessageHeader *message)
1780 struct GNUNET_MESH_ConnectionDestroy *msg;
1781 struct MeshConnection *c;
1784 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1785 LOG (GNUNET_ERROR_TYPE_DEBUG,
1786 "Got a CONNECTION DESTROY message from %s\n",
1788 LOG (GNUNET_ERROR_TYPE_DEBUG,
1789 " for connection %s\n",
1790 GNUNET_h2s (&msg->cid));
1791 c = connection_get (&msg->cid);
1794 /* Probably already got the message from another path,
1795 * destroyed the tunnel and retransmitted to children.
1798 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1800 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1803 fwd = is_fwd (c, peer);
1804 if (GNUNET_SYSERR == fwd)
1806 GNUNET_break_op (0); /* FIXME */
1809 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1810 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1811 else if (0 == c->pending_messages)
1813 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1817 c->destroy = GNUNET_YES;
1818 c->state = MESH_CONNECTION_DESTROYED;
1821 GMT_remove_connection (c->t, c);
1829 * Generic handler for mesh network encrypted traffic.
1831 * @param peer Peer identity this notification is about.
1832 * @param msg Encrypted message.
1834 * @return GNUNET_OK to keep the connection open,
1835 * GNUNET_SYSERR to close it (signal serious error)
1838 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1839 const struct GNUNET_MESH_Encrypted *msg)
1841 struct MeshConnection *c;
1842 struct MeshPeer *neighbor;
1843 struct MeshFlowControl *fc;
1844 GNUNET_PEER_Id peer_id;
1852 size = ntohs (msg->header.size);
1854 sizeof (struct GNUNET_MESH_Encrypted) +
1855 sizeof (struct GNUNET_MessageHeader))
1857 GNUNET_break_op (0);
1860 type = ntohs (msg->header.type);
1861 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1862 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1863 GM_m2s (type), ntohl (msg->pid), GNUNET_i2s (peer));
1865 /* Check connection */
1866 c = connection_get (&msg->cid);
1869 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1870 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
1871 GNUNET_h2s (&msg->cid));
1875 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1877 /* Check if origin is as expected */
1878 neighbor = get_prev_hop (c);
1879 peer_id = GNUNET_PEER_search (peer);
1880 if (peer_id == GMP_get_short_id (neighbor))
1886 neighbor = get_next_hop (c);
1887 if (peer_id == GMP_get_short_id (neighbor))
1893 /* Unexpected peer sending traffic on a connection. */
1894 GNUNET_break_op (0);
1900 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1901 pid = ntohl (msg->pid);
1902 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1904 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1905 LOG (GNUNET_ERROR_TYPE_DEBUG,
1906 "WARNING Received PID %u, (prev %u), ACK %u\n",
1907 pid, fc->last_pid_recv, fc->last_ack_sent);
1910 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1912 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1913 LOG (GNUNET_ERROR_TYPE_DEBUG,
1914 " Pid %u not expected (%u+), dropping!\n",
1915 pid, fc->last_pid_recv + 1);
1918 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1919 connection_change_state (c, MESH_CONNECTION_READY);
1920 connection_reset_timeout (c, fwd);
1921 fc->last_pid_recv = pid;
1923 /* Is this message for us? */
1924 if (GMC_is_terminal (c, fwd))
1926 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1927 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1931 GNUNET_break (GNUNET_NO != c->destroy);
1934 fc->last_pid_recv = pid;
1935 GMT_handle_encrypted (c->t, msg);
1936 GMC_send_ack (c, fwd, GNUNET_NO);
1940 /* Message not for us: forward to next hop */
1941 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1942 ttl = ntohl (msg->ttl);
1943 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1946 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1947 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1948 GMC_send_ack (c, fwd, GNUNET_NO);
1952 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1953 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1959 * Generic handler for mesh network encrypted traffic.
1961 * @param peer Peer identity this notification is about.
1962 * @param msg Encrypted message.
1964 * @return GNUNET_OK to keep the connection open,
1965 * GNUNET_SYSERR to close it (signal serious error)
1968 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1969 const struct GNUNET_MESH_KX *msg)
1971 struct MeshConnection *c;
1972 struct MeshPeer *neighbor;
1973 GNUNET_PEER_Id peer_id;
1979 size = ntohs (msg->header.size);
1981 sizeof (struct GNUNET_MESH_Encrypted) +
1982 sizeof (struct GNUNET_MessageHeader))
1984 GNUNET_break_op (0);
1987 type = ntohs (msg->header.type);
1988 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1989 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1990 GM_m2s (type), GNUNET_i2s (peer));
1992 /* Check connection */
1993 c = connection_get (&msg->cid);
1996 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1997 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING kx on unknown connection %s\n",
1998 GNUNET_h2s (&msg->cid));
2001 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
2003 /* Check if origin is as expected */
2004 neighbor = get_prev_hop (c);
2005 peer_id = GNUNET_PEER_search (peer);
2006 if (peer_id == GMP_get_short_id (neighbor))
2012 neighbor = get_next_hop (c);
2013 if (peer_id == GMP_get_short_id (neighbor))
2019 /* Unexpected peer sending traffic on a connection. */
2020 GNUNET_break_op (0);
2025 /* Count as connection confirmation. */
2026 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
2028 connection_change_state (c, MESH_CONNECTION_READY);
2031 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
2032 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
2035 connection_reset_timeout (c, fwd);
2037 /* Is this message for us? */
2038 if (GMC_is_terminal (c, fwd))
2040 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2041 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2047 GMT_handle_kx (c->t, &msg[1].header);
2051 /* Message not for us: forward to next hop */
2052 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2053 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2054 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
2061 * Core handler for encrypted mesh network traffic (channel mgmt, data).
2063 * @param cls Closure (unused).
2064 * @param message Message received.
2065 * @param peer Peer who sent the message.
2067 * @return GNUNET_OK to keep the connection open,
2068 * GNUNET_SYSERR to close it (signal serious error)
2071 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2072 const struct GNUNET_MessageHeader *message)
2074 return handle_mesh_encrypted (peer,
2075 (struct GNUNET_MESH_Encrypted *)message);
2080 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2082 * @param cls Closure (unused).
2083 * @param message Message received.
2084 * @param peer Peer who sent the message.
2086 * @return GNUNET_OK to keep the connection open,
2087 * GNUNET_SYSERR to close it (signal serious error)
2090 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2091 const struct GNUNET_MessageHeader *message)
2093 return handle_mesh_kx (peer,
2094 (struct GNUNET_MESH_KX *) message);
2099 * Core handler for mesh network traffic point-to-point acks.
2101 * @param cls closure
2102 * @param message message
2103 * @param peer peer identity this notification is about
2105 * @return GNUNET_OK to keep the connection open,
2106 * GNUNET_SYSERR to close it (signal serious error)
2109 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2110 const struct GNUNET_MessageHeader *message)
2112 struct GNUNET_MESH_ACK *msg;
2113 struct MeshConnection *c;
2114 struct MeshFlowControl *fc;
2119 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2120 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
2122 msg = (struct GNUNET_MESH_ACK *) message;
2124 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 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2197 LOG (GNUNET_ERROR_TYPE_DEBUG,
2198 "Got a POLL message from %s!\n",
2201 msg = (struct GNUNET_MESH_Poll *) message;
2203 c = connection_get (&msg->cid);
2207 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2209 LOG (GNUNET_ERROR_TYPE_DEBUG,
2210 "WARNING POLL message on unknown connection %s!\n",
2211 GNUNET_h2s (&msg->cid));
2215 /* Is this a forward or backward ACK?
2216 * Note: a poll should never be needed in a loopback case,
2217 * since there is no possiblility of packet loss there, so
2218 * this way of discerining FWD/BCK should not be a problem.
2220 id = GNUNET_PEER_search (peer);
2221 if (GMP_get_short_id (get_next_hop (c)) == id)
2223 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2226 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2228 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2233 GNUNET_break_op (0);
2237 pid = ntohl (msg->pid);
2238 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2239 fc->last_pid_recv = pid;
2240 fwd = fc == &c->bck_fc;
2241 GMC_send_ack (c, fwd, GNUNET_YES);
2248 * Core handler for mesh keepalives.
2250 * @param cls closure
2251 * @param message message
2252 * @param peer peer identity this notification is about
2253 * @return GNUNET_OK to keep the connection open,
2254 * GNUNET_SYSERR to close it (signal serious error)
2256 * TODO: Check who we got this from, to validate route.
2259 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2260 const struct GNUNET_MessageHeader *message)
2262 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2263 struct MeshConnection *c;
2264 struct MeshPeer *neighbor;
2265 GNUNET_PEER_Id peer_id;
2268 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2269 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
2272 c = connection_get (&msg->cid);
2275 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2280 /* Check if origin is as expected TODO refactor and reuse */
2281 peer_id = GNUNET_PEER_search (peer);
2282 neighbor = get_prev_hop (c);
2283 if (peer_id == GMP_get_short_id (neighbor))
2289 neighbor = get_next_hop (c);
2290 if (peer_id == GMP_get_short_id (neighbor))
2296 GNUNET_break_op (0);
2301 connection_change_state (c, MESH_CONNECTION_READY);
2302 connection_reset_timeout (c, fwd);
2304 if (GMC_is_terminal (c, fwd))
2307 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2308 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2315 * Send an ACK on the appropriate connection/channel, depending on
2316 * the direction and the position of the peer.
2318 * @param c Which connection to send the hop-by-hop ACK.
2319 * @param fwd Is this a fwd ACK? (will go dest->root).
2320 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2323 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2325 unsigned int buffer;
2327 LOG (GNUNET_ERROR_TYPE_DEBUG,
2328 "GMC send %s ACK on %s\n",
2329 GM_f2s (fwd), GMC_2s (c));
2337 if (GNUNET_NO != c->destroy)
2339 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2343 /* Get available buffer space */
2344 if (GMC_is_terminal (c, fwd))
2346 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2347 buffer = GMT_get_channels_buffer (c->t);
2351 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2352 buffer = GMC_get_buffer (c, fwd);
2354 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2355 if (0 == buffer && GNUNET_NO == force)
2358 /* Send available buffer space */
2359 if (GMC_is_origin (c, fwd))
2361 GNUNET_assert (NULL != c->t);
2362 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2363 GMT_unchoke_channels (c->t);
2367 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2368 send_ack (c, buffer, fwd, force);
2374 * Initialize the connections subsystem
2376 * @param c Configuration handle.
2379 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2381 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2383 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2386 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2387 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2388 GNUNET_SCHEDULER_shutdown ();
2393 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2396 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2397 "MESH", "MAX_CONNECTIONS", "MISSING");
2398 GNUNET_SCHEDULER_shutdown ();
2403 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2404 &refresh_connection_time))
2406 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2407 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2408 GNUNET_SCHEDULER_shutdown ();
2411 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2412 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2417 * Destroy each connection on shutdown.
2419 * @param cls Closure (unused).
2420 * @param key Current key code (CID, unused).
2421 * @param value Value in the hash map (connection)
2423 * @return #GNUNET_YES, because we should continue to iterate,
2426 shutdown_iterator (void *cls,
2427 const struct GNUNET_HashCode *key,
2430 struct MeshConnection *c = value;
2438 * Shut down the connections subsystem.
2443 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2444 GNUNET_CONTAINER_multihashmap_destroy (connections);
2449 struct MeshConnection *
2450 GMC_new (const struct GNUNET_HashCode *cid,
2451 struct MeshTunnel3 *t,
2452 struct MeshPeerPath *p,
2453 unsigned int own_pos)
2455 struct MeshConnection *c;
2457 c = GNUNET_new (struct MeshConnection);
2459 GNUNET_assert (GNUNET_OK ==
2460 GNUNET_CONTAINER_multihashmap_put (connections,
2462 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2463 fc_init (&c->fwd_fc);
2464 fc_init (&c->bck_fc);
2469 GNUNET_assert (own_pos <= p->length - 1);
2470 c->own_pos = own_pos;
2473 if (GNUNET_OK != register_neighbors (c))
2477 path_invalidate (c->path);
2490 GMC_destroy (struct MeshConnection *c)
2498 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2499 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2502 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2503 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2504 &c->fwd_fc, &c->bck_fc);
2505 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2506 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2508 /* Cancel all traffic */
2509 if (NULL != c->path)
2511 connection_cancel_queues (c, GNUNET_YES);
2512 connection_cancel_queues (c, GNUNET_NO);
2513 unregister_neighbors (c);
2516 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2517 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2519 /* Cancel maintainance task (keepalive/timeout) */
2520 if (NULL != c->fwd_fc.poll_msg)
2522 GMC_cancel (c->fwd_fc.poll_msg);
2523 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2525 if (NULL != c->bck_fc.poll_msg)
2527 GMC_cancel (c->bck_fc.poll_msg);
2528 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2531 /* Delete from tunnel */
2533 GMT_remove_connection (c->t, c);
2535 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2536 path_destroy (c->path);
2537 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2538 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2539 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2540 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2541 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2543 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2544 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2546 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2548 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2549 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2552 GNUNET_break (GNUNET_YES ==
2553 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2555 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2560 * Get the connection ID.
2562 * @param c Connection to get the ID from.
2564 * @return ID of the connection.
2566 const struct GNUNET_HashCode *
2567 GMC_get_id (const struct MeshConnection *c)
2574 * Get the connection path.
2576 * @param c Connection to get the path from.
2578 * @return path used by the connection.
2580 const struct MeshPeerPath *
2581 GMC_get_path (const struct MeshConnection *c)
2583 if (GNUNET_NO == c->destroy)
2590 * Get the connection state.
2592 * @param c Connection to get the state from.
2594 * @return state of the connection.
2596 enum MeshConnectionState
2597 GMC_get_state (const struct MeshConnection *c)
2603 * Get the connection tunnel.
2605 * @param c Connection to get the tunnel from.
2607 * @return tunnel of the connection.
2609 struct MeshTunnel3 *
2610 GMC_get_tunnel (const struct MeshConnection *c)
2617 * Get free buffer space in a connection.
2619 * @param c Connection.
2620 * @param fwd Is query about FWD traffic?
2622 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2625 GMC_get_buffer (struct MeshConnection *c, int fwd)
2627 struct MeshFlowControl *fc;
2629 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2631 return (fc->queue_max - fc->queue_n);
2635 * Get how many messages have we allowed to send to us from a direction.
2637 * @param c Connection.
2638 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2640 * @return last_ack_sent - last_pid_recv
2643 GMC_get_allowed (struct MeshConnection *c, int fwd)
2645 struct MeshFlowControl *fc;
2647 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2648 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2652 return (fc->last_ack_sent - fc->last_pid_recv);
2656 * Get messages queued in a connection.
2658 * @param c Connection.
2659 * @param fwd Is query about FWD traffic?
2661 * @return Number of messages queued.
2664 GMC_get_qn (struct MeshConnection *c, int fwd)
2666 struct MeshFlowControl *fc;
2668 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2675 * Allow the connection to advertise a buffer of the given size.
2677 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2678 * allowing up to last_pid_recv + buffer.
2680 * @param c Connection.
2681 * @param buffer How many more messages the connection can accept.
2682 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2685 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2687 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2688 GMC_2s (c), buffer, GM_f2s (fwd));
2689 send_ack (c, buffer, fwd, GNUNET_NO);
2694 * Notify other peers on a connection of a broken link. Mark connections
2695 * to destroy after all traffic has been sent.
2697 * @param c Connection on which there has been a disconnection.
2698 * @param peer Peer that disconnected.
2701 GMC_notify_broken (struct MeshConnection *c,
2702 struct MeshPeer *peer)
2706 LOG (GNUNET_ERROR_TYPE_DEBUG,
2707 " notify broken on %s due to %s disconnect\n",
2708 GMC_2s (c), GMP_2s (peer));
2710 fwd = peer == get_prev_hop (c);
2712 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2714 /* Local shutdown, no one to notify about this. */
2718 if (GNUNET_NO == c->destroy)
2719 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2721 /* Connection will have at least one pending message
2722 * (the one we just scheduled), so no point in checking whether to
2723 * destroy immediately. */
2724 c->destroy = GNUNET_YES;
2725 c->state = MESH_CONNECTION_DESTROYED;
2728 * Cancel all queues, if no message is left, connection will be destroyed.
2730 connection_cancel_queues (c, !fwd);
2737 * Is this peer the first one on the connection?
2739 * @param c Connection.
2740 * @param fwd Is this about fwd traffic?
2742 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2745 GMC_is_origin (struct MeshConnection *c, int fwd)
2747 if (!fwd && c->path->length - 1 == c->own_pos )
2749 if (fwd && 0 == c->own_pos)
2756 * Is this peer the last one on the connection?
2758 * @param c Connection.
2759 * @param fwd Is this about fwd traffic?
2760 * Note that the ROOT is the terminal for BCK traffic!
2762 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2765 GMC_is_terminal (struct MeshConnection *c, int fwd)
2767 return GMC_is_origin (c, !fwd);
2772 * See if we are allowed to send by the next hop in the given direction.
2774 * @param c Connection.
2775 * @param fwd Is this about fwd traffic?
2777 * @return #GNUNET_YES in case it's OK to send.
2780 GMC_is_sendable (struct MeshConnection *c, int fwd)
2782 struct MeshFlowControl *fc;
2789 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2790 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2796 * Sends an already built message on a connection, properly registering
2797 * all used resources.
2799 * @param message Message to send. Function makes a copy of it.
2800 * If message is not hop-by-hop, decrements TTL of copy.
2801 * @param c Connection on which this message is transmitted.
2802 * @param fwd Is this a fwd message?
2803 * @param force Force the connection to accept the message (buffer overfill).
2804 * @param cont Continuation called once message is sent. Can be NULL.
2805 * @param cont_cls Closure for @c cont.
2807 * @return Handle to cancel the message before it's sent.
2808 * NULL on error or if @c cont is NULL.
2809 * Invalid on @c cont call.
2811 struct MeshConnectionQueue *
2812 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2813 struct MeshConnection *c, int fwd, int force,
2814 GMC_sent cont, void *cont_cls)
2816 struct MeshFlowControl *fc;
2817 struct MeshConnectionQueue *q;
2823 size = ntohs (message->size);
2824 data = GNUNET_malloc (size);
2825 memcpy (data, message, size);
2826 type = ntohs (message->type);
2827 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2828 GM_m2s (type), size, GMC_2s (c));
2830 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2831 droppable = GNUNET_NO == force;
2834 struct GNUNET_MESH_Encrypted *emsg;
2835 struct GNUNET_MESH_KX *kmsg;
2836 struct GNUNET_MESH_ACK *amsg;
2837 struct GNUNET_MESH_Poll *pmsg;
2838 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2839 struct GNUNET_MESH_ConnectionBroken *bmsg;
2842 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2843 emsg = (struct GNUNET_MESH_Encrypted *) data;
2844 ttl = ntohl (emsg->ttl);
2847 GNUNET_break_op (0);
2852 emsg->ttl = htonl (ttl - 1);
2853 emsg->pid = htonl (fc->next_pid++);
2854 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2855 if (GNUNET_YES == droppable)
2858 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2859 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2860 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2864 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2866 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2868 GMC_start_poll (c, fwd);
2872 case GNUNET_MESSAGE_TYPE_MESH_KX:
2873 kmsg = (struct GNUNET_MESH_KX *) data;
2877 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2878 amsg = (struct GNUNET_MESH_ACK *) data;
2880 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2881 droppable = GNUNET_NO;
2884 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2885 pmsg = (struct GNUNET_MESH_Poll *) data;
2887 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2888 droppable = GNUNET_NO;
2891 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2892 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2897 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2898 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2903 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2904 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2905 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2914 if (fc->queue_n > fc->queue_max && droppable)
2916 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2919 LOG (GNUNET_ERROR_TYPE_DEBUG,
2920 "queue full: %u/%u\n",
2921 fc->queue_n, fc->queue_max);
2922 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2928 return NULL; /* Drop this message */
2931 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2932 c->pending_messages++;
2934 q = GNUNET_new (struct MeshConnectionQueue);
2935 q->forced = !droppable;
2936 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2940 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2946 q->cont_cls = cont_cls;
2952 * Cancel a previously sent message while it's in the queue.
2954 * ONLY can be called before the continuation given to the send function
2955 * is called. Once the continuation is called, the message is no longer in the
2958 * @param q Handle to the queue.
2961 GMC_cancel (struct MeshConnectionQueue *q)
2963 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2965 /* queue destroy calls message_sent, which calls q->cont and frees q */
2966 GMP_queue_destroy (q->q, GNUNET_YES);
2971 * Sends a CREATE CONNECTION message for a path to a peer.
2972 * Changes the connection and tunnel states if necessary.
2974 * @param connection Connection to create.
2977 GMC_send_create (struct MeshConnection *connection)
2979 enum MeshTunnel3CState state;
2982 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2983 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2985 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2986 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2987 connection, connection->pending_messages);
2988 connection->pending_messages++;
2990 connection->maintenance_q =
2991 GMP_queue_add (get_next_hop (connection), NULL,
2992 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2993 size, connection, GNUNET_YES, &message_sent, NULL);
2995 state = GMT_get_cstate (connection->t);
2996 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2997 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2998 if (MESH_CONNECTION_NEW == connection->state)
2999 connection_change_state (connection, MESH_CONNECTION_SENT);
3004 * Send a message to all peers in this connection that the connection
3005 * is no longer valid.
3007 * If some peer should not receive the message, it should be zero'ed out
3008 * before calling this function.
3010 * @param c The connection whose peers to notify.
3013 GMC_send_destroy (struct MeshConnection *c)
3015 struct GNUNET_MESH_ConnectionDestroy msg;
3017 if (GNUNET_YES == c->destroy)
3020 msg.header.size = htons (sizeof (msg));
3021 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
3023 LOG (GNUNET_ERROR_TYPE_DEBUG,
3024 " sending connection destroy for connection %s\n",
3027 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
3028 GMC_send_prebuilt_message (&msg.header, c,
3029 GNUNET_YES, GNUNET_YES, NULL, NULL);
3030 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
3031 GMC_send_prebuilt_message (&msg.header, c,
3032 GNUNET_NO, GNUNET_YES, NULL, NULL);
3033 c->destroy = GNUNET_YES;
3034 c->state = MESH_CONNECTION_DESTROYED;
3039 * @brief Start a polling timer for the connection.
3041 * When a neighbor does not accept more traffic on the connection it could be
3042 * caused by a simple congestion or by a lost ACK. Polling enables to check
3043 * for the lastest ACK status for a connection.
3045 * @param c Connection.
3046 * @param fwd Should we poll in the FWD direction?
3049 GMC_start_poll (struct MeshConnection *c, int fwd)
3051 struct MeshFlowControl *fc;
3053 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3054 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3056 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3058 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3059 fc->poll_task, fc->poll_msg);
3062 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3063 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3070 * @brief Stop polling a connection for ACKs.
3072 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3074 * @param c Connection.
3075 * @param fwd Should we stop the poll in the FWD direction?
3078 GMC_stop_poll (struct MeshConnection *c, int fwd)
3080 struct MeshFlowControl *fc;
3082 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3083 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3085 GNUNET_SCHEDULER_cancel (fc->poll_task);
3086 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3091 * Get a (static) string for a connection.
3093 * @param c Connection.
3096 GMC_2s (const struct MeshConnection *c)
3103 static char buf[128];
3105 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
3108 return GNUNET_h2s (&c->id);