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 * Pending message count.
207 int pending_messages;
210 * Destroy flag: if true, destroy on last message.
216 * Handle for messages queued but not yet sent.
218 struct MeshConnectionQueue
221 * Peer queue handle, to cancel if necessary.
223 struct MeshPeerQueue *q;
226 * Was this a forced message? (Do not account for it)
231 * Continuation to call once sent.
236 * Closure for @c cont.
241 /******************************************************************************/
242 /******************************* GLOBALS ***********************************/
243 /******************************************************************************/
246 * Global handle to the statistics service.
248 extern struct GNUNET_STATISTICS_Handle *stats;
251 * Local peer own ID (memory efficient handle).
253 extern GNUNET_PEER_Id myid;
256 * Local peer own ID (full value).
258 extern struct GNUNET_PeerIdentity my_full_id;
261 * Connections known, indexed by cid (MeshConnection).
263 static struct GNUNET_CONTAINER_MultiHashMap *connections;
266 * How many connections are we willing to maintain.
267 * Local connections are always allowed, even if there are more connections than max.
269 static unsigned long long max_connections;
272 * How many messages *in total* are we willing to queue, divide by number of
273 * connections to get connection queue size.
275 static unsigned long long max_msgs_queue;
278 * How often to send path keepalives. Paths timeout after 4 missed.
280 static struct GNUNET_TIME_Relative refresh_connection_time;
283 * How often to send path create / ACKs.
285 static struct GNUNET_TIME_Relative create_connection_time;
288 /******************************************************************************/
289 /******************************** STATIC ***********************************/
290 /******************************************************************************/
292 #if 0 // avoid compiler warning for unused static function
294 fc_debug (struct MeshFlowControl *fc)
296 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
297 fc->last_pid_recv, fc->last_ack_sent);
298 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
299 fc->last_pid_sent, fc->last_ack_recv);
300 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
301 fc->queue_n, fc->queue_max);
305 connection_debug (struct MeshConnection *c)
309 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
312 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
313 peer2s (c->t->peer), GMC_2s (c));
314 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
315 c->state, c->pending_messages);
316 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
317 fc_debug (&c->fwd_fc);
318 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
319 fc_debug (&c->bck_fc);
324 * Get string description for tunnel state.
326 * @param s Tunnel state.
328 * @return String representation.
331 GMC_state2s (enum MeshConnectionState s)
335 case MESH_CONNECTION_NEW:
336 return "MESH_CONNECTION_NEW";
337 case MESH_CONNECTION_SENT:
338 return "MESH_CONNECTION_SENT";
339 case MESH_CONNECTION_ACK:
340 return "MESH_CONNECTION_ACK";
341 case MESH_CONNECTION_READY:
342 return "MESH_CONNECTION_READY";
343 case MESH_CONNECTION_DESTROYED:
344 return "MESH_CONNECTION_DESTROYED";
346 return "MESH_CONNECTION_STATE_ERROR";
352 * Initialize a Flow Control structure to the initial state.
354 * @param fc Flow Control structure to initialize.
357 fc_init (struct MeshFlowControl *fc)
360 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
361 fc->last_pid_recv = (uint32_t) -1;
362 fc->last_ack_sent = (uint32_t) 0;
363 fc->last_ack_recv = (uint32_t) 0;
364 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
365 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
367 fc->queue_max = (max_msgs_queue / max_connections) + 1;
374 * @param cid Connection ID.
376 static struct MeshConnection *
377 connection_get (const struct GNUNET_HashCode *cid)
379 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
384 connection_change_state (struct MeshConnection* c,
385 enum MeshConnectionState state)
387 LOG (GNUNET_ERROR_TYPE_DEBUG,
388 "Connection %s state was %s\n",
389 GMC_2s (c), GMC_state2s (c->state));
390 if (MESH_CONNECTION_DESTROYED == c->state)
392 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
395 LOG (GNUNET_ERROR_TYPE_DEBUG,
396 "Connection %s state is now %s\n",
397 GMC_2s (c), GMC_state2s (state));
403 * Callback called when a queued ACK message is sent.
405 * @param cls Closure (FC).
406 * @param c Connection this message was on.
407 * @param q Queue handler this call invalidates.
408 * @param type Type of message sent.
409 * @param fwd Was this a FWD going message?
410 * @param size Size of the message.
414 struct MeshConnection *c,
415 struct MeshConnectionQueue *q,
416 uint16_t type, int fwd, size_t size)
418 struct MeshFlowControl *fc = cls;
425 * Send an ACK on the connection, informing the predecessor about
426 * the available buffer space. Should not be called in case the peer
427 * is origin (no predecessor) in the @c fwd direction.
429 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
430 * the ACK itself goes "back" (dest->root).
432 * @param c Connection on which to send the ACK.
433 * @param buffer How much space free to advertise?
434 * @param fwd Is this FWD ACK? (Going dest -> root)
435 * @param force Don't optimize out.
438 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
440 struct MeshFlowControl *next_fc;
441 struct MeshFlowControl *prev_fc;
442 struct GNUNET_MESH_ACK msg;
446 /* If origin, there is no connection to send ACKs. Wrong function! */
447 if (GMC_is_origin (c, fwd))
449 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
450 GMC_2s (c), GM_f2s (fwd));
455 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
456 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
458 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
459 GM_f2s (fwd), GMC_2s (c));
461 /* Check if we need to transmit the ACK. */
462 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
463 if (3 < delta && buffer < delta && GNUNET_NO == force)
465 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
466 LOG (GNUNET_ERROR_TYPE_DEBUG,
467 " last pid recv: %u, last ack sent: %u\n",
468 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
472 /* Ok, ACK might be necessary, what PID to ACK? */
473 ack = prev_fc->last_pid_recv + buffer;
474 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 " last pid %u, last ack %u, qmax %u, q %u\n",
477 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
478 next_fc->queue_max, next_fc->queue_n);
479 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
485 /* Check if message is already in queue */
486 if (NULL != prev_fc->ack_msg)
488 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
490 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
491 GMC_cancel (prev_fc->ack_msg);
492 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
496 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
501 prev_fc->last_ack_sent = ack;
503 /* Build ACK message and send on connection */
504 msg.header.size = htons (sizeof (msg));
505 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
506 msg.ack = htonl (ack);
509 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
516 * Callback called when a queued message is sent.
518 * Calculates the average time and connection packet tracking.
520 * @param cls Closure (ConnectionQueue Handle).
521 * @param c Connection this message was on.
522 * @param type Type of message sent.
523 * @param fwd Was this a FWD going message?
524 * @param size Size of the message.
525 * @param wait Time spent waiting for core (only the time for THIS message)
528 message_sent (void *cls,
529 struct MeshConnection *c, uint16_t type,
530 int fwd, size_t size,
531 struct GNUNET_TIME_Relative wait)
533 struct MeshConnectionPerformance *p;
534 struct MeshFlowControl *fc;
535 struct MeshConnectionQueue *q = cls;
539 fc = fwd ? &c->fwd_fc : &c->bck_fc;
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
544 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
550 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
551 q->cont (q->cont_cls, c, q, type, fwd, size);
555 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
557 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
564 c->pending_messages--;
565 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
567 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
571 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
574 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
576 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
577 if (GNUNET_NO == forced)
580 LOG (GNUNET_ERROR_TYPE_DEBUG,
581 "! accounting pid %u\n",
586 LOG (GNUNET_ERROR_TYPE_DEBUG,
587 "! forced, Q_N not accounting pid %u\n",
590 GMC_send_ack (c, fwd, GNUNET_NO);
593 case GNUNET_MESSAGE_TYPE_MESH_POLL:
597 case GNUNET_MESSAGE_TYPE_MESH_ACK:
604 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
607 return; /* Only endpoints are interested in timing. */
610 usecsperbyte = ((double) wait.rel_value_us) / size;
611 if (p->size == AVG_MSGS)
613 /* Array is full. Substract oldest value, add new one and store. */
614 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
615 p->usecsperbyte[p->idx] = usecsperbyte;
616 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
620 /* Array not yet full. Add current value to avg and store. */
621 p->usecsperbyte[p->idx] = usecsperbyte;
623 p->avg += p->usecsperbyte[p->idx];
627 p->idx = (p->idx + 1) % AVG_MSGS;
632 * Get the previous hop in a connection
634 * @param c Connection.
636 * @return Previous peer in the connection.
638 static struct MeshPeer *
639 get_prev_hop (const struct MeshConnection *c)
643 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
644 GMC_2s (c), c->own_pos, c->path->length);
645 if (0 == c->own_pos || c->path->length < 2)
647 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is zero\n");
648 id = c->path->peers[0];
652 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is NOT zero\n");
653 id = c->path->peers[c->own_pos - 1];
656 LOG (GNUNET_ERROR_TYPE_DEBUG, " id: %u\n", id);
657 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s\n", GNUNET_i2s (GNUNET_PEER_resolve2 (id)));
658 return GMP_get_short (id);
663 * Get the next hop in a connection
665 * @param c Connection.
667 * @return Next peer in the connection.
669 static struct MeshPeer *
670 get_next_hop (const struct MeshConnection *c)
674 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
675 GMC_2s (c), c->own_pos, c->path->length);
676 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
678 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is end\n");
679 id = c->path->peers[c->path->length - 1];
683 LOG (GNUNET_ERROR_TYPE_DEBUG, " own pos is NOT end\n");
684 id = c->path->peers[c->own_pos + 1];
687 LOG (GNUNET_ERROR_TYPE_DEBUG, " id: %u\n", id);
688 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s\n", GNUNET_i2s (GNUNET_PEER_resolve2 (id)));
690 return GMP_get_short (id);
695 * Get the hop in a connection.
697 * @param c Connection.
698 * @param fwd Next hop?
700 * @return Next peer in the connection.
702 static struct MeshPeer *
703 get_hop (struct MeshConnection *c, int fwd)
706 return get_next_hop (c);
707 return get_prev_hop (c);
712 * Is traffic coming from this sender 'FWD' traffic?
714 * @param c Connection to check.
715 * @param sender Peer identity of neighbor.
717 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
718 * the traffic is 'FWD'.
719 * #GNUNET_NO for BCK.
720 * #GNUNET_SYSERR for errors.
723 is_fwd (const struct MeshConnection *c,
724 const struct GNUNET_PeerIdentity *sender)
728 id = GNUNET_PEER_search (sender);
729 if (GMP_get_short_id (get_prev_hop (c)) == id)
732 if (GMP_get_short_id (get_next_hop (c)) == id)
736 return GNUNET_SYSERR;
741 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
742 * or a first CONNECTION_ACK directed to us.
744 * @param connection Connection to confirm.
745 * @param fwd Should we send it FWD? (root->dest)
746 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
749 send_connection_ack (struct MeshConnection *connection, int fwd)
751 struct MeshTunnel3 *t;
754 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
756 GMP_queue_add (get_hop (connection, fwd), NULL,
757 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
758 sizeof (struct GNUNET_MESH_ConnectionACK),
759 connection, fwd, &message_sent, NULL);
760 connection->pending_messages++;
761 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
762 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
763 if (MESH_CONNECTION_READY != connection->state)
764 connection_change_state (connection, MESH_CONNECTION_SENT);
769 * Send a notification that a connection is broken.
771 * @param c Connection that is broken.
772 * @param id1 Peer that has disconnected.
773 * @param id2 Peer that has disconnected.
774 * @param fwd Direction towards which to send it.
777 send_broken (struct MeshConnection *c,
778 const struct GNUNET_PeerIdentity *id1,
779 const struct GNUNET_PeerIdentity *id2,
782 struct GNUNET_MESH_ConnectionBroken msg;
784 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
785 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
789 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
794 * Send a notification that a connection is broken, when a connection
795 * isn't even created.
797 * @param connection_id Connection ID.
798 * @param id1 Peer that has disconnected.
799 * @param id2 Peer that has disconnected.
800 * @param peer Peer to notify (neighbor who sent the connection).
803 send_broken2 (struct GNUNET_HashCode *connection_id,
804 const struct GNUNET_PeerIdentity *id1,
805 const struct GNUNET_PeerIdentity *id2,
806 GNUNET_PEER_Id peer_id)
808 struct GNUNET_MESH_ConnectionBroken *msg;
809 struct MeshPeer *neighbor;
811 msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
812 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
813 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
814 msg->cid = *connection_id;
817 neighbor = GMP_get_short (peer_id);
818 GMP_queue_add (neighbor, msg,
819 GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED,
820 sizeof (struct GNUNET_MESH_ConnectionBroken),
821 NULL, GNUNET_SYSERR, /* connection, fwd */
822 NULL, NULL); /* continuation */
827 * Send keepalive packets for a connection.
829 * @param c Connection to keep alive..
830 * @param fwd Is this a FWD keepalive? (owner -> dest).
833 connection_keepalive (struct MeshConnection *c, int fwd)
835 struct GNUNET_MESH_ConnectionKeepAlive *msg;
836 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
839 LOG (GNUNET_ERROR_TYPE_DEBUG,
840 "sending %s keepalive for connection %s]\n",
841 GM_f2s (fwd), GMC_2s (c));
843 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
844 msg->header.size = htons (size);
845 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
847 msg->reserved = htonl (0);
849 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
854 * Send CONNECTION_{CREATE/ACK} packets for a connection.
856 * @param c Connection for which to send the message.
857 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
860 connection_recreate (struct MeshConnection *c, int fwd)
862 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
866 send_connection_ack (c, GNUNET_NO);
871 * Generic connection timer management.
872 * Depending on the role of the peer in the connection will send the
873 * appropriate message (build or keepalive)
875 * @param c Conncetion to maintain.
879 connection_maintain (struct MeshConnection *c, int fwd)
881 if (GNUNET_NO != c->destroy)
884 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
886 /* TODO DHT GET with RO_BART */
891 case MESH_CONNECTION_NEW:
894 case MESH_CONNECTION_SENT:
895 connection_recreate (c, fwd);
897 case MESH_CONNECTION_READY:
898 connection_keepalive (c, fwd);
907 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
909 struct MeshConnection *c = cls;
910 struct GNUNET_TIME_Relative delay;
912 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
913 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
916 connection_maintain (c, GNUNET_YES);
917 delay = c->state == MESH_CONNECTION_READY ?
918 refresh_connection_time : create_connection_time;
919 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
920 &connection_fwd_keepalive,
926 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
928 struct MeshConnection *c = cls;
929 struct GNUNET_TIME_Relative delay;
931 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
932 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
935 connection_maintain (c, GNUNET_NO);
936 delay = c->state == MESH_CONNECTION_READY ?
937 refresh_connection_time : create_connection_time;
938 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
939 &connection_bck_keepalive,
945 * @brief Re-initiate traffic on this connection if necessary.
947 * Check if there is traffic queued towards this peer
948 * and the core transmit handle is NULL (traffic was stalled).
949 * If so, call core tmt rdy.
951 * @param c Connection on which initiate traffic.
952 * @param fwd Is this about fwd traffic?
955 connection_unlock_queue (struct MeshConnection *c, int fwd)
957 struct MeshPeer *peer;
959 LOG (GNUNET_ERROR_TYPE_DEBUG,
960 "connection_unlock_queue %s on %s\n",
961 GM_f2s (fwd), GMC_2s (c));
963 if (GMC_is_terminal (c, fwd))
965 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
969 peer = get_hop (c, fwd);
970 GMP_queue_unlock (peer, c);
975 * Cancel all transmissions that belong to a certain connection.
977 * If the connection is scheduled for destruction and no more messages are left,
978 * the connection will be destroyed by the continuation call.
980 * @param c Connection which to cancel. Might be destroyed during this call.
981 * @param fwd Cancel fwd traffic?
984 connection_cancel_queues (struct MeshConnection *c, int fwd)
986 struct MeshFlowControl *fc;
987 struct MeshPeer *peer;
989 LOG (GNUNET_ERROR_TYPE_DEBUG,
990 " *** Cancel %s queues for connection %s\n",
991 GM_f2s (fwd), GMC_2s (c));
998 fc = fwd ? &c->fwd_fc : &c->bck_fc;
999 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1001 GNUNET_SCHEDULER_cancel (fc->poll_task);
1002 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1003 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1005 peer = get_hop (c, fwd);
1006 GMP_queue_cancel (peer, c);
1011 * Function called if a connection has been stalled for a while,
1012 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1014 * @param cls Closure (poll ctx).
1015 * @param tc TaskContext.
1018 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1022 * Callback called when a queued POLL message is sent.
1024 * @param cls Closure (FC).
1025 * @param c Connection this message was on.
1026 * @param q Queue handler this call invalidates.
1027 * @param type Type of message sent.
1028 * @param fwd Was this a FWD going message?
1029 * @param size Size of the message.
1032 poll_sent (void *cls,
1033 struct MeshConnection *c,
1034 struct MeshConnectionQueue *q,
1035 uint16_t type, int fwd, size_t size)
1037 struct MeshFlowControl *fc = cls;
1039 if (2 == c->destroy)
1041 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1044 LOG (GNUNET_ERROR_TYPE_DEBUG,
1045 " *** POLL sent for , scheduling new one!\n");
1046 fc->poll_msg = NULL;
1047 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1048 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1049 &connection_poll, fc);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1055 * Function called if a connection has been stalled for a while,
1056 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1058 * @param cls Closure (poll ctx).
1059 * @param tc TaskContext.
1062 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1064 struct MeshFlowControl *fc = cls;
1065 struct GNUNET_MESH_Poll msg;
1066 struct MeshConnection *c;
1068 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1069 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1075 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1076 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1077 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1078 fc == &c->fwd_fc ? "FWD" : "BCK");
1080 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1081 msg.header.size = htons (sizeof (msg));
1082 msg.pid = htonl (fc->last_pid_sent);
1083 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1084 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1085 fc == &c->fwd_fc, GNUNET_YES,
1091 * Timeout function due to lack of keepalive/traffic from the owner.
1092 * Destroys connection if called.
1094 * @param cls Closure (connection to destroy).
1095 * @param tc TaskContext.
1098 connection_fwd_timeout (void *cls,
1099 const struct GNUNET_SCHEDULER_TaskContext *tc)
1101 struct MeshConnection *c = cls;
1103 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1104 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1107 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1109 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1120 * Timeout function due to lack of keepalive/traffic from the destination.
1121 * Destroys connection if called.
1123 * @param cls Closure (connection to destroy).
1124 * @param tc TaskContext
1127 connection_bck_timeout (void *cls,
1128 const struct GNUNET_SCHEDULER_TaskContext *tc)
1130 struct MeshConnection *c = cls;
1132 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1133 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1136 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1139 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1150 * Resets the connection timeout task, some other message has done the
1152 * - For the first peer on the direction this means to send
1153 * a keepalive or a path confirmation message (either create or ACK).
1154 * - For all other peers, this means to destroy the connection,
1155 * due to lack of activity.
1156 * Starts the timeout if no timeout was running (connection just created).
1158 * @param c Connection whose timeout to reset.
1159 * @param fwd Is this forward?
1161 * TODO use heap to improve efficiency of scheduler.
1164 connection_reset_timeout (struct MeshConnection *c, int fwd)
1166 GNUNET_SCHEDULER_TaskIdentifier *ti;
1167 GNUNET_SCHEDULER_Task f;
1169 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1171 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1173 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1174 GNUNET_SCHEDULER_cancel (*ti);
1176 if (GMC_is_origin (c, fwd)) /* Startpoint */
1178 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1179 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1181 else /* Relay, endpoint. */
1183 struct GNUNET_TIME_Relative delay;
1185 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1186 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1187 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1193 * Add the connection to the list of both neighbors.
1195 * @param c Connection.
1197 * @return #GNUNET_OK if everything went fine
1198 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1201 register_neighbors (struct MeshConnection *c)
1203 struct MeshPeer *next_peer;
1204 struct MeshPeer *prev_peer;
1206 next_peer = get_next_hop (c);
1207 prev_peer = get_prev_hop (c);
1209 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1211 path_debug (c->path);
1212 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1213 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1214 GMC_2s (c), next_peer);
1215 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GMP_2s (next_peer));
1216 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1217 GMC_2s (c), prev_peer);
1218 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GMP_2s (prev_peer));
1220 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1221 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1223 if (GMC_is_origin (c, GNUNET_YES))
1224 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1225 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1227 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1228 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1229 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1230 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1231 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1232 return GNUNET_SYSERR;
1235 GMP_add_connection (next_peer, c);
1236 GMP_add_connection (prev_peer, c);
1243 * Remove the connection from the list of both neighbors.
1245 * @param c Connection.
1248 unregister_neighbors (struct MeshConnection *c)
1250 struct MeshPeer *peer;
1252 peer = get_next_hop (c);
1253 if (GNUNET_OK != GMP_remove_connection (peer, c))
1255 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1256 || MESH_CONNECTION_DESTROYED == c->state);
1257 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1258 if (NULL != c->t) GMT_debug (c->t);
1261 peer = get_prev_hop (c);
1262 if (GNUNET_OK != GMP_remove_connection (peer, c))
1264 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1265 || MESH_CONNECTION_DESTROYED == c->state);
1266 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1267 if (NULL != c->t) GMT_debug (c->t);
1273 * Bind the connection to the peer and the tunnel to that peer.
1275 * If the peer has no tunnel, create one. Update tunnel and connection
1276 * data structres to reflect new status.
1278 * @param c Connection.
1282 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1284 GMP_add_tunnel (peer);
1285 c->t = GMP_get_tunnel (peer);
1286 GMT_add_connection (c->t, c);
1291 * Builds a path from a PeerIdentity array.
1293 * @param peers PeerIdentity array.
1294 * @param size Size of the @c peers array.
1295 * @param own_pos Output parameter: own position in the path.
1297 * @return Fixed and shortened path.
1299 static struct MeshPeerPath *
1300 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1302 unsigned int *own_pos)
1304 struct MeshPeerPath *path;
1305 GNUNET_PEER_Id shortid;
1308 unsigned int offset;
1311 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1312 path = path_new (size);
1315 for (i = 0; i < size; i++)
1317 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1318 i, GNUNET_i2s (&peers[i]));
1319 shortid = GNUNET_PEER_intern (&peers[i]);
1321 /* Check for loops / duplicates */
1322 for (j = 0; j < i - offset; j++)
1324 if (path->peers[j] == shortid)
1326 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1328 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset);
1329 GNUNET_PEER_change_rc (shortid, -1);
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1333 path->peers[i - offset] = shortid;
1334 if (path->peers[i] == myid)
1337 path->length -= offset;
1339 if (path->peers[*own_pos] != myid)
1341 /* create path: self not found in path through self */
1342 GNUNET_break_op (0);
1343 path_destroy (path);
1350 /******************************************************************************/
1351 /******************************** API ***********************************/
1352 /******************************************************************************/
1355 * Core handler for connection creation.
1357 * @param cls Closure (unused).
1358 * @param peer Sender (neighbor).
1359 * @param message Message.
1361 * @return GNUNET_OK to keep the connection open,
1362 * GNUNET_SYSERR to close it (signal serious error)
1365 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1366 const struct GNUNET_MessageHeader *message)
1368 struct GNUNET_MESH_ConnectionCreate *msg;
1369 struct GNUNET_PeerIdentity *id;
1370 struct GNUNET_HashCode *cid;
1371 struct MeshPeerPath *path;
1372 struct MeshPeer *dest_peer;
1373 struct MeshPeer *orig_peer;
1374 struct MeshConnection *c;
1375 unsigned int own_pos;
1378 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1382 size = ntohs (message->size);
1383 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1385 GNUNET_break_op (0);
1389 /* Calculate hops */
1390 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1391 if (size % sizeof (struct GNUNET_PeerIdentity))
1393 GNUNET_break_op (0);
1396 size /= sizeof (struct GNUNET_PeerIdentity);
1399 GNUNET_break_op (0);
1402 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1404 /* Get parameters */
1405 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1407 id = (struct GNUNET_PeerIdentity *) &msg[1];
1408 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s (%s->).\n",
1409 GNUNET_h2s (cid), GNUNET_i2s (id));
1411 /* Create connection */
1412 c = connection_get (cid);
1415 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1421 GNUNET_break_op (0);
1424 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1425 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1426 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1429 if (path->length - 1 == own_pos)
1431 /* If we are destination, why did the creation fail? */
1435 send_broken2 (cid, &my_full_id,
1436 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1437 path->peers[own_pos - 1]);
1438 path_destroy (path);
1441 GMP_add_path_to_all (path, GNUNET_NO);
1442 connection_reset_timeout (c, GNUNET_YES);
1446 path = path_duplicate (c->path);
1448 if (MESH_CONNECTION_NEW == c->state)
1449 connection_change_state (c, MESH_CONNECTION_SENT);
1451 /* Remember peers */
1452 dest_peer = GMP_get (&id[size - 1]);
1453 orig_peer = GMP_get (&id[0]);
1455 /* Is it a connection to us? */
1456 if (c->own_pos == size - 1)
1458 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1459 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1461 add_to_peer (c, orig_peer);
1462 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1463 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1465 send_connection_ack (c, GNUNET_NO);
1466 if (MESH_CONNECTION_SENT == c->state)
1467 connection_change_state (c, MESH_CONNECTION_ACK);
1469 /* Keep tunnel alive in direction dest->owner*/
1470 if (GNUNET_SCHEDULER_NO_TASK == c->bck_maintenance_task)
1472 c->bck_maintenance_task =
1473 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1474 &connection_bck_keepalive, c);
1479 /* It's for somebody else! Retransmit. */
1480 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1481 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1482 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1483 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1486 path_destroy (path);
1492 * Core handler for path confirmations.
1494 * @param cls closure
1495 * @param message message
1496 * @param peer peer identity this notification is about
1498 * @return GNUNET_OK to keep the connection open,
1499 * GNUNET_SYSERR to close it (signal serious error)
1502 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1503 const struct GNUNET_MessageHeader *message)
1505 struct GNUNET_MESH_ConnectionACK *msg;
1506 struct MeshConnection *c;
1507 struct MeshPeerPath *p;
1508 struct MeshPeer *pi;
1509 enum MeshConnectionState oldstate;
1512 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1513 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1514 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1515 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1516 GNUNET_h2s (&msg->cid));
1517 c = connection_get (&msg->cid);
1520 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1522 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1526 if (GNUNET_NO != c->destroy)
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1532 oldstate = c->state;
1533 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1534 pi = GMP_get (peer);
1535 if (get_next_hop (c) == pi)
1537 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1539 if (MESH_CONNECTION_SENT == oldstate)
1540 connection_change_state (c, MESH_CONNECTION_ACK);
1542 else if (get_prev_hop (c) == pi)
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1546 connection_change_state (c, MESH_CONNECTION_READY);
1550 GNUNET_break_op (0);
1554 connection_reset_timeout (c, fwd);
1556 /* Add path to peers? */
1560 GMP_add_path_to_all (p, GNUNET_YES);
1567 /* Message for us as creator? */
1568 if (GMC_is_origin (c, GNUNET_YES))
1570 if (GNUNET_NO != fwd)
1572 GNUNET_break_op (0);
1575 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1577 /* If just created, cancel the short timeout and start a long one */
1578 if (MESH_CONNECTION_SENT == oldstate)
1579 connection_reset_timeout (c, GNUNET_YES);
1581 /* Change connection state */
1582 connection_change_state (c, MESH_CONNECTION_READY);
1583 send_connection_ack (c, GNUNET_YES);
1585 /* Change tunnel state, trigger KX */
1586 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1587 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1592 /* Message for us as destination? */
1593 if (GMC_is_terminal (c, GNUNET_YES))
1595 if (GNUNET_YES != fwd)
1597 GNUNET_break_op (0);
1600 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1602 /* If just created, cancel the short timeout and start a long one */
1603 if (MESH_CONNECTION_ACK == oldstate)
1604 connection_reset_timeout (c, GNUNET_NO);
1606 /* Change tunnel state */
1607 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1608 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1613 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1614 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1620 * Core handler for notifications of broken paths
1622 * @param cls Closure (unused).
1623 * @param id Peer identity of sending neighbor.
1624 * @param message Message.
1626 * @return GNUNET_OK to keep the connection open,
1627 * GNUNET_SYSERR to close it (signal serious error)
1630 GMC_handle_broken (void* cls,
1631 const struct GNUNET_PeerIdentity* id,
1632 const struct GNUNET_MessageHeader* message)
1634 struct GNUNET_MESH_ConnectionBroken *msg;
1635 struct MeshConnection *c;
1638 LOG (GNUNET_ERROR_TYPE_DEBUG,
1639 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1640 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1641 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1642 GNUNET_i2s (&msg->peer1));
1643 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1644 GNUNET_i2s (&msg->peer2));
1645 c = connection_get (&msg->cid);
1648 GNUNET_break_op (0);
1652 fwd = is_fwd (c, id);
1653 if (GMC_is_terminal (c, fwd))
1655 path_invalidate (c->path);
1656 if (0 < c->pending_messages)
1657 c->destroy = GNUNET_YES;
1663 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1664 c->destroy = GNUNET_YES;
1665 connection_cancel_queues (c, !fwd);
1674 * Core handler for tunnel destruction
1676 * @param cls Closure (unused).
1677 * @param peer Peer identity of sending neighbor.
1678 * @param message Message.
1680 * @return GNUNET_OK to keep the connection open,
1681 * GNUNET_SYSERR to close it (signal serious error)
1684 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1685 const struct GNUNET_MessageHeader *message)
1687 struct GNUNET_MESH_ConnectionDestroy *msg;
1688 struct MeshConnection *c;
1691 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1692 LOG (GNUNET_ERROR_TYPE_DEBUG,
1693 "Got a CONNECTION DESTROY message from %s\n",
1695 LOG (GNUNET_ERROR_TYPE_DEBUG,
1696 " for connection %s\n",
1697 GNUNET_h2s (&msg->cid));
1698 c = connection_get (&msg->cid);
1701 /* Probably already got the message from another path,
1702 * destroyed the tunnel and retransmitted to children.
1705 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1707 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1710 fwd = is_fwd (c, peer);
1711 if (GNUNET_SYSERR == fwd)
1713 GNUNET_break_op (0); /* FIXME */
1716 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1717 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1718 else if (0 == c->pending_messages)
1720 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1724 c->destroy = GNUNET_YES;
1725 c->state = MESH_CONNECTION_DESTROYED;
1728 GMT_remove_connection (c->t, c);
1736 * Generic handler for mesh network encrypted traffic.
1738 * @param peer Peer identity this notification is about.
1739 * @param msg Encrypted message.
1741 * @return GNUNET_OK to keep the connection open,
1742 * GNUNET_SYSERR to close it (signal serious error)
1745 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1746 const struct GNUNET_MESH_Encrypted *msg)
1748 struct MeshConnection *c;
1749 struct MeshPeer *neighbor;
1750 struct MeshFlowControl *fc;
1751 GNUNET_PEER_Id peer_id;
1759 size = ntohs (msg->header.size);
1761 sizeof (struct GNUNET_MESH_Encrypted) +
1762 sizeof (struct GNUNET_MessageHeader))
1764 GNUNET_break_op (0);
1767 type = ntohs (msg->header.type);
1768 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1769 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1770 GM_m2s (type), ntohl (msg->pid), GNUNET_i2s (peer));
1772 /* Check connection */
1773 c = connection_get (&msg->cid);
1776 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1777 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
1778 GNUNET_h2s (&msg->cid));
1782 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1784 /* Check if origin is as expected */
1785 neighbor = get_prev_hop (c);
1786 peer_id = GNUNET_PEER_search (peer);
1787 if (peer_id == GMP_get_short_id (neighbor))
1793 neighbor = get_next_hop (c);
1794 if (peer_id == GMP_get_short_id (neighbor))
1800 /* Unexpected peer sending traffic on a connection. */
1801 GNUNET_break_op (0);
1807 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1808 pid = ntohl (msg->pid);
1809 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1811 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1812 LOG (GNUNET_ERROR_TYPE_DEBUG,
1813 "WARNING Received PID %u, (prev %u), ACK %u\n",
1814 pid, fc->last_pid_recv, fc->last_ack_sent);
1817 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1819 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1820 LOG (GNUNET_ERROR_TYPE_DEBUG,
1821 " Pid %u not expected (%u+), dropping!\n",
1822 pid, fc->last_pid_recv + 1);
1825 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1826 connection_change_state (c, MESH_CONNECTION_READY);
1827 connection_reset_timeout (c, fwd);
1828 fc->last_pid_recv = pid;
1830 /* Is this message for us? */
1831 if (GMC_is_terminal (c, fwd))
1833 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1834 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1838 GNUNET_break (GNUNET_NO != c->destroy);
1841 fc->last_pid_recv = pid;
1842 GMT_handle_encrypted (c->t, msg);
1843 GMC_send_ack (c, fwd, GNUNET_NO);
1847 /* Message not for us: forward to next hop */
1848 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1849 ttl = ntohl (msg->ttl);
1850 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1853 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1854 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1855 GMC_send_ack (c, fwd, GNUNET_NO);
1859 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1860 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1866 * Generic handler for mesh network encrypted traffic.
1868 * @param peer Peer identity this notification is about.
1869 * @param msg Encrypted message.
1871 * @return GNUNET_OK to keep the connection open,
1872 * GNUNET_SYSERR to close it (signal serious error)
1875 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1876 const struct GNUNET_MESH_KX *msg)
1878 struct MeshConnection *c;
1879 struct MeshPeer *neighbor;
1880 GNUNET_PEER_Id peer_id;
1886 size = ntohs (msg->header.size);
1888 sizeof (struct GNUNET_MESH_Encrypted) +
1889 sizeof (struct GNUNET_MessageHeader))
1891 GNUNET_break_op (0);
1894 type = ntohs (msg->header.type);
1895 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1896 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1897 GM_m2s (type), GNUNET_i2s (peer));
1899 /* Check connection */
1900 c = connection_get (&msg->cid);
1903 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1904 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING kx on unknown connection %s\n",
1905 GNUNET_h2s (&msg->cid));
1908 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1910 /* Check if origin is as expected */
1911 neighbor = get_prev_hop (c);
1912 peer_id = GNUNET_PEER_search (peer);
1913 if (peer_id == GMP_get_short_id (neighbor))
1919 neighbor = get_next_hop (c);
1920 if (peer_id == GMP_get_short_id (neighbor))
1926 /* Unexpected peer sending traffic on a connection. */
1927 GNUNET_break_op (0);
1932 /* Count as connection confirmation. */
1933 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1935 connection_change_state (c, MESH_CONNECTION_READY);
1938 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1939 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1942 connection_reset_timeout (c, fwd);
1944 /* Is this message for us? */
1945 if (GMC_is_terminal (c, fwd))
1947 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1948 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1954 GMT_handle_kx (c->t, &msg[1].header);
1958 /* Message not for us: forward to next hop */
1959 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1960 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1961 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1968 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1970 * @param cls Closure (unused).
1971 * @param message Message received.
1972 * @param peer Peer who sent the message.
1974 * @return GNUNET_OK to keep the connection open,
1975 * GNUNET_SYSERR to close it (signal serious error)
1978 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1979 const struct GNUNET_MessageHeader *message)
1981 return handle_mesh_encrypted (peer,
1982 (struct GNUNET_MESH_Encrypted *)message);
1987 * Core handler for key exchange traffic (ephemeral key, ping, pong).
1989 * @param cls Closure (unused).
1990 * @param message Message received.
1991 * @param peer Peer who sent the message.
1993 * @return GNUNET_OK to keep the connection open,
1994 * GNUNET_SYSERR to close it (signal serious error)
1997 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1998 const struct GNUNET_MessageHeader *message)
2000 return handle_mesh_kx (peer,
2001 (struct GNUNET_MESH_KX *) message);
2006 * Core handler for mesh network traffic point-to-point acks.
2008 * @param cls closure
2009 * @param message message
2010 * @param peer peer identity this notification is about
2012 * @return GNUNET_OK to keep the connection open,
2013 * GNUNET_SYSERR to close it (signal serious error)
2016 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2017 const struct GNUNET_MessageHeader *message)
2019 struct GNUNET_MESH_ACK *msg;
2020 struct MeshConnection *c;
2021 struct MeshFlowControl *fc;
2026 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2027 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
2029 msg = (struct GNUNET_MESH_ACK *) message;
2031 c = connection_get (&msg->cid);
2035 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2040 /* Is this a forward or backward ACK? */
2041 id = GNUNET_PEER_search (peer);
2042 if (GMP_get_short_id (get_next_hop (c)) == id)
2044 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2048 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2050 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2056 GNUNET_break_op (0);
2060 ack = ntohl (msg->ack);
2061 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2062 ack, fc->last_ack_recv);
2063 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
2064 fc->last_ack_recv = ack;
2066 /* Cancel polling if the ACK is big enough. */
2067 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2068 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2070 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2071 GNUNET_SCHEDULER_cancel (fc->poll_task);
2072 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2073 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2076 connection_unlock_queue (c, fwd);
2083 * Core handler for mesh network traffic point-to-point ack polls.
2085 * @param cls closure
2086 * @param message message
2087 * @param peer peer identity this notification is about
2089 * @return GNUNET_OK to keep the connection open,
2090 * GNUNET_SYSERR to close it (signal serious error)
2093 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2094 const struct GNUNET_MessageHeader *message)
2096 struct GNUNET_MESH_Poll *msg;
2097 struct MeshConnection *c;
2098 struct MeshFlowControl *fc;
2103 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2104 LOG (GNUNET_ERROR_TYPE_DEBUG,
2105 "Got a POLL message from %s!\n",
2108 msg = (struct GNUNET_MESH_Poll *) message;
2110 c = connection_get (&msg->cid);
2114 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2116 LOG (GNUNET_ERROR_TYPE_DEBUG,
2117 "WARNING POLL message on unknown connection %s!\n",
2118 GNUNET_h2s (&msg->cid));
2122 /* Is this a forward or backward ACK?
2123 * Note: a poll should never be needed in a loopback case,
2124 * since there is no possiblility of packet loss there, so
2125 * this way of discerining FWD/BCK should not be a problem.
2127 id = GNUNET_PEER_search (peer);
2128 if (GMP_get_short_id (get_next_hop (c)) == id)
2130 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2133 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2135 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2140 GNUNET_break_op (0);
2144 pid = ntohl (msg->pid);
2145 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2146 fc->last_pid_recv = pid;
2147 fwd = fc == &c->bck_fc;
2148 GMC_send_ack (c, fwd, GNUNET_YES);
2155 * Core handler for mesh keepalives.
2157 * @param cls closure
2158 * @param message message
2159 * @param peer peer identity this notification is about
2160 * @return GNUNET_OK to keep the connection open,
2161 * GNUNET_SYSERR to close it (signal serious error)
2163 * TODO: Check who we got this from, to validate route.
2166 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2167 const struct GNUNET_MessageHeader *message)
2169 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2170 struct MeshConnection *c;
2171 struct MeshPeer *neighbor;
2172 GNUNET_PEER_Id peer_id;
2175 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2176 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
2179 c = connection_get (&msg->cid);
2182 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2187 /* Check if origin is as expected TODO refactor and reuse */
2188 peer_id = GNUNET_PEER_search (peer);
2189 neighbor = get_prev_hop (c);
2190 if (peer_id == GMP_get_short_id (neighbor))
2196 neighbor = get_next_hop (c);
2197 if (peer_id == GMP_get_short_id (neighbor))
2203 GNUNET_break_op (0);
2208 connection_change_state (c, MESH_CONNECTION_READY);
2209 connection_reset_timeout (c, fwd);
2211 if (GMC_is_terminal (c, fwd))
2214 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2215 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2222 * Send an ACK on the appropriate connection/channel, depending on
2223 * the direction and the position of the peer.
2225 * @param c Which connection to send the hop-by-hop ACK.
2226 * @param fwd Is this a fwd ACK? (will go dest->root).
2227 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2230 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2232 unsigned int buffer;
2234 LOG (GNUNET_ERROR_TYPE_DEBUG,
2235 "GMC send %s ACK on %s\n",
2236 GM_f2s (fwd), GMC_2s (c));
2244 if (GNUNET_NO != c->destroy)
2246 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2250 /* Get available buffer space */
2251 if (GMC_is_terminal (c, fwd))
2253 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2254 buffer = GMT_get_channels_buffer (c->t);
2258 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2259 buffer = GMC_get_buffer (c, fwd);
2261 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2262 if (0 == buffer && GNUNET_NO == force)
2265 /* Send available buffer space */
2266 if (GMC_is_origin (c, fwd))
2268 GNUNET_assert (NULL != c->t);
2269 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2270 GMT_unchoke_channels (c->t);
2274 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2275 send_ack (c, buffer, fwd, force);
2281 * Initialize the connections subsystem
2283 * @param c Configuration handle.
2286 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2288 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2290 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2293 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2294 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2295 GNUNET_SCHEDULER_shutdown ();
2300 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2303 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2304 "MESH", "MAX_CONNECTIONS", "MISSING");
2305 GNUNET_SCHEDULER_shutdown ();
2310 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2311 &refresh_connection_time))
2313 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2314 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2315 GNUNET_SCHEDULER_shutdown ();
2318 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2319 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2324 * Destroy each connection on shutdown.
2326 * @param cls Closure (unused).
2327 * @param key Current key code (CID, unused).
2328 * @param value Value in the hash map (connection)
2330 * @return #GNUNET_YES, because we should continue to iterate,
2333 shutdown_iterator (void *cls,
2334 const struct GNUNET_HashCode *key,
2337 struct MeshConnection *c = value;
2345 * Shut down the connections subsystem.
2350 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2351 GNUNET_CONTAINER_multihashmap_destroy (connections);
2356 struct MeshConnection *
2357 GMC_new (const struct GNUNET_HashCode *cid,
2358 struct MeshTunnel3 *t,
2359 struct MeshPeerPath *p,
2360 unsigned int own_pos)
2362 struct MeshConnection *c;
2364 c = GNUNET_new (struct MeshConnection);
2366 GNUNET_assert (GNUNET_OK ==
2367 GNUNET_CONTAINER_multihashmap_put (connections,
2369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2370 fc_init (&c->fwd_fc);
2371 fc_init (&c->bck_fc);
2376 GNUNET_assert (own_pos <= p->length - 1);
2377 c->own_pos = own_pos;
2380 if (GNUNET_OK != register_neighbors (c))
2384 path_invalidate (c->path);
2394 c->fwd_maintenance_task =
2395 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2396 &connection_fwd_keepalive, c);
2404 GMC_destroy (struct MeshConnection *c)
2412 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2413 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2416 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2417 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2418 &c->fwd_fc, &c->bck_fc);
2419 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2420 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2422 /* Cancel all traffic */
2423 if (NULL != c->path)
2425 connection_cancel_queues (c, GNUNET_YES);
2426 connection_cancel_queues (c, GNUNET_NO);
2427 unregister_neighbors (c);
2430 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2431 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2433 /* Cancel maintainance task (keepalive/timeout) */
2434 if (NULL != c->fwd_fc.poll_msg)
2436 GMC_cancel (c->fwd_fc.poll_msg);
2437 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2439 if (NULL != c->bck_fc.poll_msg)
2441 GMC_cancel (c->bck_fc.poll_msg);
2442 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2445 /* Delete from tunnel */
2447 GMT_remove_connection (c->t, c);
2449 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2450 path_destroy (c->path);
2451 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2452 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2453 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2454 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2455 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2457 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2458 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2460 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2462 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2463 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2466 GNUNET_break (GNUNET_YES ==
2467 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2469 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2474 * Get the connection ID.
2476 * @param c Connection to get the ID from.
2478 * @return ID of the connection.
2480 const struct GNUNET_HashCode *
2481 GMC_get_id (const struct MeshConnection *c)
2488 * Get the connection path.
2490 * @param c Connection to get the path from.
2492 * @return path used by the connection.
2494 const struct MeshPeerPath *
2495 GMC_get_path (const struct MeshConnection *c)
2497 if (GNUNET_NO == c->destroy)
2504 * Get the connection state.
2506 * @param c Connection to get the state from.
2508 * @return state of the connection.
2510 enum MeshConnectionState
2511 GMC_get_state (const struct MeshConnection *c)
2517 * Get the connection tunnel.
2519 * @param c Connection to get the tunnel from.
2521 * @return tunnel of the connection.
2523 struct MeshTunnel3 *
2524 GMC_get_tunnel (const struct MeshConnection *c)
2531 * Get free buffer space in a connection.
2533 * @param c Connection.
2534 * @param fwd Is query about FWD traffic?
2536 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2539 GMC_get_buffer (struct MeshConnection *c, int fwd)
2541 struct MeshFlowControl *fc;
2543 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2545 return (fc->queue_max - fc->queue_n);
2549 * Get how many messages have we allowed to send to us from a direction.
2551 * @param c Connection.
2552 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2554 * @return last_ack_sent - last_pid_recv
2557 GMC_get_allowed (struct MeshConnection *c, int fwd)
2559 struct MeshFlowControl *fc;
2561 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2562 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2566 return (fc->last_ack_sent - fc->last_pid_recv);
2570 * Get messages queued in a connection.
2572 * @param c Connection.
2573 * @param fwd Is query about FWD traffic?
2575 * @return Number of messages queued.
2578 GMC_get_qn (struct MeshConnection *c, int fwd)
2580 struct MeshFlowControl *fc;
2582 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2589 * Allow the connection to advertise a buffer of the given size.
2591 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2592 * allowing up to last_pid_recv + buffer.
2594 * @param c Connection.
2595 * @param buffer How many more messages the connection can accept.
2596 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2599 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2601 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2602 GMC_2s (c), buffer, GM_f2s (fwd));
2603 send_ack (c, buffer, fwd, GNUNET_NO);
2608 * Notify other peers on a connection of a broken link. Mark connections
2609 * to destroy after all traffic has been sent.
2611 * @param c Connection on which there has been a disconnection.
2612 * @param peer Peer that disconnected.
2615 GMC_notify_broken (struct MeshConnection *c,
2616 struct MeshPeer *peer)
2620 LOG (GNUNET_ERROR_TYPE_DEBUG,
2621 " notify broken on %s due to %s disconnect\n",
2622 GMC_2s (c), GMP_2s (peer));
2624 fwd = peer == get_prev_hop (c);
2626 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2628 /* Local shutdown, no one to notify about this. */
2632 if (GNUNET_NO == c->destroy)
2633 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2635 /* Connection will have at least one pending message
2636 * (the one we just scheduled), so no point in checking whether to
2637 * destroy immediately. */
2638 c->destroy = GNUNET_YES;
2639 c->state = MESH_CONNECTION_DESTROYED;
2642 * Cancel all queues, if no message is left, connection will be destroyed.
2644 connection_cancel_queues (c, !fwd);
2651 * Is this peer the first one on the connection?
2653 * @param c Connection.
2654 * @param fwd Is this about fwd traffic?
2656 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2659 GMC_is_origin (struct MeshConnection *c, int fwd)
2661 if (!fwd && c->path->length - 1 == c->own_pos )
2663 if (fwd && 0 == c->own_pos)
2670 * Is this peer the last one on the connection?
2672 * @param c Connection.
2673 * @param fwd Is this about fwd traffic?
2674 * Note that the ROOT is the terminal for BCK traffic!
2676 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2679 GMC_is_terminal (struct MeshConnection *c, int fwd)
2681 return GMC_is_origin (c, !fwd);
2686 * See if we are allowed to send by the next hop in the given direction.
2688 * @param c Connection.
2689 * @param fwd Is this about fwd traffic?
2691 * @return #GNUNET_YES in case it's OK to send.
2694 GMC_is_sendable (struct MeshConnection *c, int fwd)
2696 struct MeshFlowControl *fc;
2698 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2699 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2705 * Sends an already built message on a connection, properly registering
2706 * all used resources.
2708 * @param message Message to send. Function makes a copy of it.
2709 * If message is not hop-by-hop, decrements TTL of copy.
2710 * @param c Connection on which this message is transmitted.
2711 * @param fwd Is this a fwd message?
2712 * @param force Force the connection to accept the message (buffer overfill).
2713 * @param cont Continuation called once message is sent. Can be NULL.
2714 * @param cont_cls Closure for @c cont.
2716 * @return Handle to cancel the message before it's sent.
2717 * NULL on error or if @c cont is NULL.
2718 * Invalid on @c cont call.
2720 struct MeshConnectionQueue *
2721 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2722 struct MeshConnection *c, int fwd, int force,
2723 GMC_sent cont, void *cont_cls)
2725 struct MeshFlowControl *fc;
2726 struct MeshConnectionQueue *q;
2732 size = ntohs (message->size);
2733 data = GNUNET_malloc (size);
2734 memcpy (data, message, size);
2735 type = ntohs (message->type);
2736 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2737 GM_m2s (type), size, GMC_2s (c));
2739 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2740 droppable = GNUNET_NO == force;
2743 struct GNUNET_MESH_Encrypted *emsg;
2744 struct GNUNET_MESH_KX *kmsg;
2745 struct GNUNET_MESH_ACK *amsg;
2746 struct GNUNET_MESH_Poll *pmsg;
2747 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2748 struct GNUNET_MESH_ConnectionBroken *bmsg;
2751 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2752 emsg = (struct GNUNET_MESH_Encrypted *) data;
2753 ttl = ntohl (emsg->ttl);
2756 GNUNET_break_op (0);
2761 emsg->ttl = htonl (ttl - 1);
2762 emsg->pid = htonl (fc->next_pid++);
2763 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2764 if (GNUNET_YES == droppable)
2767 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2768 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2769 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2773 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2775 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2777 GMC_start_poll (c, fwd);
2781 case GNUNET_MESSAGE_TYPE_MESH_KX:
2782 kmsg = (struct GNUNET_MESH_KX *) data;
2786 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2787 amsg = (struct GNUNET_MESH_ACK *) data;
2789 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2790 droppable = GNUNET_NO;
2793 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2794 pmsg = (struct GNUNET_MESH_Poll *) data;
2796 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2797 droppable = GNUNET_NO;
2800 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2801 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2806 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2807 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2812 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2813 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2814 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2823 if (fc->queue_n > fc->queue_max && droppable)
2825 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2828 LOG (GNUNET_ERROR_TYPE_DEBUG,
2829 "queue full: %u/%u\n",
2830 fc->queue_n, fc->queue_max);
2831 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2837 return NULL; /* Drop this message */
2840 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2841 c->pending_messages++;
2843 q = GNUNET_new (struct MeshConnectionQueue);
2844 q->forced = !droppable;
2845 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2849 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2855 q->cont_cls = cont_cls;
2861 * Cancel a previously sent message while it's in the queue.
2863 * ONLY can be called before the continuation given to the send function
2864 * is called. Once the continuation is called, the message is no longer in the
2867 * @param q Handle to the queue.
2870 GMC_cancel (struct MeshConnectionQueue *q)
2872 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2874 /* queue destroy calls message_sent, which calls q->cont and frees q */
2875 GMP_queue_destroy (q->q, GNUNET_YES);
2880 * Sends a CREATE CONNECTION message for a path to a peer.
2881 * Changes the connection and tunnel states if necessary.
2883 * @param connection Connection to create.
2886 GMC_send_create (struct MeshConnection *connection)
2888 enum MeshTunnel3CState state;
2891 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2892 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2894 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2895 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2896 connection, connection->pending_messages);
2897 connection->pending_messages++;
2899 GMP_queue_add (get_next_hop (connection), NULL,
2900 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2901 size, connection, GNUNET_YES, &message_sent, NULL);
2903 state = GMT_get_cstate (connection->t);
2904 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2905 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2906 if (MESH_CONNECTION_NEW == connection->state)
2907 connection_change_state (connection, MESH_CONNECTION_SENT);
2912 * Send a message to all peers in this connection that the connection
2913 * is no longer valid.
2915 * If some peer should not receive the message, it should be zero'ed out
2916 * before calling this function.
2918 * @param c The connection whose peers to notify.
2921 GMC_send_destroy (struct MeshConnection *c)
2923 struct GNUNET_MESH_ConnectionDestroy msg;
2925 if (GNUNET_YES == c->destroy)
2928 msg.header.size = htons (sizeof (msg));
2929 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2931 LOG (GNUNET_ERROR_TYPE_DEBUG,
2932 " sending connection destroy for connection %s\n",
2935 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2936 GMC_send_prebuilt_message (&msg.header, c,
2937 GNUNET_YES, GNUNET_YES, NULL, NULL);
2938 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2939 GMC_send_prebuilt_message (&msg.header, c,
2940 GNUNET_NO, GNUNET_YES, NULL, NULL);
2941 c->destroy = GNUNET_YES;
2942 c->state = MESH_CONNECTION_DESTROYED;
2947 * @brief Start a polling timer for the connection.
2949 * When a neighbor does not accept more traffic on the connection it could be
2950 * caused by a simple congestion or by a lost ACK. Polling enables to check
2951 * for the lastest ACK status for a connection.
2953 * @param c Connection.
2954 * @param fwd Should we poll in the FWD direction?
2957 GMC_start_poll (struct MeshConnection *c, int fwd)
2959 struct MeshFlowControl *fc;
2961 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2962 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
2964 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
2966 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
2967 fc->poll_task, fc->poll_msg);
2970 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
2971 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2978 * @brief Stop polling a connection for ACKs.
2980 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2982 * @param c Connection.
2983 * @param fwd Should we stop the poll in the FWD direction?
2986 GMC_stop_poll (struct MeshConnection *c, int fwd)
2988 struct MeshFlowControl *fc;
2990 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2991 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2993 GNUNET_SCHEDULER_cancel (fc->poll_task);
2994 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2999 * Get a (static) string for a connection.
3001 * @param c Connection.
3004 GMC_2s (const struct MeshConnection *c)
3011 static char buf[128];
3013 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
3016 return GNUNET_h2s (&c->id);