2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh_connection.c
23 * @brief GNUnet MESH service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "mesh_path.h"
33 #include "mesh_protocol.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
42 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
43 GNUNET_TIME_UNIT_MINUTES,\
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct to encapsulate all the Flow Control information to a peer to which
54 * we are directly connected (on a core level).
56 struct MeshFlowControl
59 * Connection this controls.
61 struct MeshConnection *c;
64 * How many messages are in the queue on this connection.
69 * How many messages do we accept in the queue.
71 unsigned int queue_max;
79 * ID of the last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 GNUNET_SCHEDULER_TaskIdentifier poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
109 * Queued poll message, to cancel if not necessary anymore (got ACK).
111 struct MeshConnectionQueue *poll_msg;
114 * Queued poll message, to cancel if not necessary anymore (got ACK).
116 struct MeshConnectionQueue *ack_msg;
120 * Keep a record of the last messages sent on this connection.
122 struct MeshConnectionPerformance
125 * Circular buffer for storing measurements.
127 double usecsperbyte[AVG_MSGS];
130 * Running average of @c usecsperbyte.
135 * How many values of @c usecsperbyte are valid.
140 * Index of the next "free" position in @c usecsperbyte.
147 * Struct containing all information regarding a connection to a peer.
149 struct MeshConnection
152 * Tunnel this connection is part of.
154 struct MeshTunnel3 *t;
157 * Flow control information for traffic fwd.
159 struct MeshFlowControl fwd_fc;
162 * Flow control information for traffic bck.
164 struct MeshFlowControl bck_fc;
167 * Measure connection performance on the endpoint.
169 struct MeshConnectionPerformance *perf;
172 * ID of the connection.
174 struct GNUNET_HashCode id;
177 * State of the connection.
179 enum MeshConnectionState state;
182 * Path being used for the tunnel. At the origin of the connection
183 * it's a pointer to the destination's path pool, otherwise just a copy.
185 struct MeshPeerPath *path;
188 * Position of the local peer in the path.
190 unsigned int own_pos;
193 * Task to keep the used paths alive at the owner,
194 * time tunnel out on all the other peers.
196 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
199 * Task to keep the used paths alive at the destination,
200 * time tunnel out on all the other peers.
202 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
205 * Queue handle for maintainance traffic. One handle for FWD and BCK since
206 * one peer never needs to maintain both directions (no loopback connections).
208 struct MeshPeerQueue *maintenance_q;
211 * Counter to do exponential backoff when creating a connection (max 64).
213 unsigned short create_retry;
216 * Pending message count.
218 int pending_messages;
221 * Destroy flag: if true, destroy on last message.
227 * Handle for messages queued but not yet sent.
229 struct MeshConnectionQueue
232 * Peer queue handle, to cancel if necessary.
234 struct MeshPeerQueue *q;
237 * Was this a forced message? (Do not account for it)
242 * Continuation to call once sent.
247 * Closure for @c cont.
252 /******************************************************************************/
253 /******************************* GLOBALS ***********************************/
254 /******************************************************************************/
257 * Global handle to the statistics service.
259 extern struct GNUNET_STATISTICS_Handle *stats;
262 * Local peer own ID (memory efficient handle).
264 extern GNUNET_PEER_Id myid;
267 * Local peer own ID (full value).
269 extern struct GNUNET_PeerIdentity my_full_id;
272 * Connections known, indexed by cid (MeshConnection).
274 static struct GNUNET_CONTAINER_MultiHashMap *connections;
277 * How many connections are we willing to maintain.
278 * Local connections are always allowed, even if there are more connections than max.
280 static unsigned long long max_connections;
283 * How many messages *in total* are we willing to queue, divide by number of
284 * connections to get connection queue size.
286 static unsigned long long max_msgs_queue;
289 * How often to send path keepalives. Paths timeout after 4 missed.
291 static struct GNUNET_TIME_Relative refresh_connection_time;
294 * How often to send path create / ACKs.
296 static struct GNUNET_TIME_Relative create_connection_time;
299 /******************************************************************************/
300 /******************************** STATIC ***********************************/
301 /******************************************************************************/
303 #if 0 // avoid compiler warning for unused static function
305 fc_debug (struct MeshFlowControl *fc)
307 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
308 fc->last_pid_recv, fc->last_ack_sent);
309 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
310 fc->last_pid_sent, fc->last_ack_recv);
311 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
312 fc->queue_n, fc->queue_max);
316 connection_debug (struct MeshConnection *c)
320 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
324 peer2s (c->t->peer), GMC_2s (c));
325 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
326 c->state, c->pending_messages);
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
328 fc_debug (&c->fwd_fc);
329 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
330 fc_debug (&c->bck_fc);
336 * Schedule next keepalive task, taking in consideration
337 * the connection state and number of retries.
339 * @param c Connection for which to schedule the next keepalive.
340 * @param fwd Direction for the next keepalive.
343 schedule_next_keepalive (struct MeshConnection *c, int fwd);
347 * Get string description for tunnel state.
349 * @param s Tunnel state.
351 * @return String representation.
354 GMC_state2s (enum MeshConnectionState s)
358 case MESH_CONNECTION_NEW:
359 return "MESH_CONNECTION_NEW";
360 case MESH_CONNECTION_SENT:
361 return "MESH_CONNECTION_SENT";
362 case MESH_CONNECTION_ACK:
363 return "MESH_CONNECTION_ACK";
364 case MESH_CONNECTION_READY:
365 return "MESH_CONNECTION_READY";
366 case MESH_CONNECTION_DESTROYED:
367 return "MESH_CONNECTION_DESTROYED";
369 return "MESH_CONNECTION_STATE_ERROR";
375 * Initialize a Flow Control structure to the initial state.
377 * @param fc Flow Control structure to initialize.
380 fc_init (struct MeshFlowControl *fc)
383 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
384 fc->last_pid_recv = (uint32_t) -1;
385 fc->last_ack_sent = (uint32_t) 0;
386 fc->last_ack_recv = (uint32_t) 0;
387 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
388 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
390 fc->queue_max = (max_msgs_queue / max_connections) + 1;
397 * @param cid Connection ID.
399 static struct MeshConnection *
400 connection_get (const struct GNUNET_HashCode *cid)
402 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
407 connection_change_state (struct MeshConnection* c,
408 enum MeshConnectionState state)
410 LOG (GNUNET_ERROR_TYPE_DEBUG,
411 "Connection %s state was %s\n",
412 GMC_2s (c), GMC_state2s (c->state));
413 if (MESH_CONNECTION_DESTROYED == c->state)
415 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
418 LOG (GNUNET_ERROR_TYPE_DEBUG,
419 "Connection %s state is now %s\n",
420 GMC_2s (c), GMC_state2s (state));
422 if (MESH_CONNECTION_READY == state)
428 * Callback called when a queued ACK message is sent.
430 * @param cls Closure (FC).
431 * @param c Connection this message was on.
432 * @param q Queue handler this call invalidates.
433 * @param type Type of message sent.
434 * @param fwd Was this a FWD going message?
435 * @param size Size of the message.
439 struct MeshConnection *c,
440 struct MeshConnectionQueue *q,
441 uint16_t type, int fwd, size_t size)
443 struct MeshFlowControl *fc = cls;
450 * Send an ACK on the connection, informing the predecessor about
451 * the available buffer space. Should not be called in case the peer
452 * is origin (no predecessor) in the @c fwd direction.
454 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
455 * the ACK itself goes "back" (dest->root).
457 * @param c Connection on which to send the ACK.
458 * @param buffer How much space free to advertise?
459 * @param fwd Is this FWD ACK? (Going dest -> root)
460 * @param force Don't optimize out.
463 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
465 struct MeshFlowControl *next_fc;
466 struct MeshFlowControl *prev_fc;
467 struct GNUNET_MESH_ACK msg;
471 /* If origin, there is no connection to send ACKs. Wrong function! */
472 if (GMC_is_origin (c, fwd))
474 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
475 GMC_2s (c), GM_f2s (fwd));
480 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
481 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
483 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
484 GM_f2s (fwd), GMC_2s (c));
486 /* Check if we need to transmit the ACK. */
487 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
488 if (3 < delta && buffer < delta && GNUNET_NO == force)
490 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 " last pid recv: %u, last ack sent: %u\n",
493 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
497 /* Ok, ACK might be necessary, what PID to ACK? */
498 ack = prev_fc->last_pid_recv + buffer;
499 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
500 LOG (GNUNET_ERROR_TYPE_DEBUG,
501 " last pid %u, last ack %u, qmax %u, q %u\n",
502 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
503 next_fc->queue_max, next_fc->queue_n);
504 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
510 /* Check if message is already in queue */
511 if (NULL != prev_fc->ack_msg)
513 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
515 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
516 GMC_cancel (prev_fc->ack_msg);
517 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
521 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
526 prev_fc->last_ack_sent = ack;
528 /* Build ACK message and send on connection */
529 msg.header.size = htons (sizeof (msg));
530 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
531 msg.ack = htonl (ack);
534 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
541 * Callback called when a queued message is sent.
543 * Calculates the average time and connection packet tracking.
545 * @param cls Closure (ConnectionQueue Handle).
546 * @param c Connection this message was on.
547 * @param type Type of message sent.
548 * @param fwd Was this a FWD going message?
549 * @param size Size of the message.
550 * @param wait Time spent waiting for core (only the time for THIS message)
553 message_sent (void *cls,
554 struct MeshConnection *c, uint16_t type,
555 int fwd, size_t size,
556 struct GNUNET_TIME_Relative wait)
558 struct MeshConnectionPerformance *p;
559 struct MeshFlowControl *fc;
560 struct MeshConnectionQueue *q = cls;
564 fc = fwd ? &c->fwd_fc : &c->bck_fc;
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
576 q->cont (q->cont_cls, c, q, type, fwd, size);
580 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
582 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
589 c->pending_messages--;
590 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
592 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
596 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
599 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
600 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
601 c->maintenance_q = NULL;
602 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
603 if (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE == type || !fwd)
604 schedule_next_keepalive (c, fwd);
607 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
610 if (GNUNET_NO == forced)
613 LOG (GNUNET_ERROR_TYPE_DEBUG,
614 "! accounting pid %u\n",
619 LOG (GNUNET_ERROR_TYPE_DEBUG,
620 "! forced, Q_N not accounting pid %u\n",
623 GMC_send_ack (c, fwd, GNUNET_NO);
626 case GNUNET_MESSAGE_TYPE_MESH_POLL:
630 case GNUNET_MESSAGE_TYPE_MESH_ACK:
637 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
640 return; /* Only endpoints are interested in timing. */
643 usecsperbyte = ((double) wait.rel_value_us) / size;
644 if (p->size == AVG_MSGS)
646 /* Array is full. Substract oldest value, add new one and store. */
647 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
648 p->usecsperbyte[p->idx] = usecsperbyte;
649 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
653 /* Array not yet full. Add current value to avg and store. */
654 p->usecsperbyte[p->idx] = usecsperbyte;
656 p->avg += p->usecsperbyte[p->idx];
660 p->idx = (p->idx + 1) % AVG_MSGS;
665 * Get the previous hop in a connection
667 * @param c Connection.
669 * @return Previous peer in the connection.
671 static struct MeshPeer *
672 get_prev_hop (const struct MeshConnection *c)
676 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
677 GMC_2s (c), c->own_pos, c->path->length);
678 if (0 == c->own_pos || c->path->length < 2)
679 id = c->path->peers[0];
681 id = c->path->peers[c->own_pos - 1];
683 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
684 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
686 return GMP_get_short (id);
691 * Get the next hop in a connection
693 * @param c Connection.
695 * @return Next peer in the connection.
697 static struct MeshPeer *
698 get_next_hop (const struct MeshConnection *c)
702 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
703 GMC_2s (c), c->own_pos, c->path->length);
704 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
705 id = c->path->peers[c->path->length - 1];
707 id = c->path->peers[c->own_pos + 1];
709 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
710 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
712 return GMP_get_short (id);
717 * Get the hop in a connection.
719 * @param c Connection.
720 * @param fwd Next hop?
722 * @return Next peer in the connection.
724 static struct MeshPeer *
725 get_hop (struct MeshConnection *c, int fwd)
728 return get_next_hop (c);
729 return get_prev_hop (c);
734 * Is traffic coming from this sender 'FWD' traffic?
736 * @param c Connection to check.
737 * @param sender Peer identity of neighbor.
739 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
740 * the traffic is 'FWD'.
741 * #GNUNET_NO for BCK.
742 * #GNUNET_SYSERR for errors.
745 is_fwd (const struct MeshConnection *c,
746 const struct GNUNET_PeerIdentity *sender)
750 id = GNUNET_PEER_search (sender);
751 if (GMP_get_short_id (get_prev_hop (c)) == id)
754 if (GMP_get_short_id (get_next_hop (c)) == id)
758 return GNUNET_SYSERR;
763 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
764 * or a first CONNECTION_ACK directed to us.
766 * @param connection Connection to confirm.
767 * @param fwd Should we send it FWD? (root->dest)
768 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
771 send_connection_ack (struct MeshConnection *connection, int fwd)
773 struct MeshTunnel3 *t;
776 LOG (GNUNET_ERROR_TYPE_INFO, "Send %s ACK on connection %s\n",
777 GM_f2s (!fwd), GMC_2s (connection));
778 GMP_queue_add (get_hop (connection, fwd), NULL,
779 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
780 sizeof (struct GNUNET_MESH_ConnectionACK),
781 connection, fwd, &message_sent, NULL);
782 connection->pending_messages++;
783 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
784 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
785 if (MESH_CONNECTION_READY != connection->state)
786 connection_change_state (connection, MESH_CONNECTION_SENT);
791 * Send a notification that a connection is broken.
793 * @param c Connection that is broken.
794 * @param id1 Peer that has disconnected.
795 * @param id2 Peer that has disconnected.
796 * @param fwd Direction towards which to send it.
799 send_broken (struct MeshConnection *c,
800 const struct GNUNET_PeerIdentity *id1,
801 const struct GNUNET_PeerIdentity *id2,
804 struct GNUNET_MESH_ConnectionBroken msg;
806 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
807 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
811 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
816 * Send a notification that a connection is broken, when a connection
817 * isn't even created.
819 * @param connection_id Connection ID.
820 * @param id1 Peer that has disconnected.
821 * @param id2 Peer that has disconnected.
822 * @param peer Peer to notify (neighbor who sent the connection).
825 send_broken2 (struct GNUNET_HashCode *connection_id,
826 const struct GNUNET_PeerIdentity *id1,
827 const struct GNUNET_PeerIdentity *id2,
828 GNUNET_PEER_Id peer_id)
830 struct GNUNET_MESH_ConnectionBroken *msg;
831 struct MeshPeer *neighbor;
833 LOG (GNUNET_ERROR_TYPE_INFO,
834 "Send BROKEN on unknown connection %s\n", GNUNET_h2s (connection_id));
836 msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
837 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
838 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
839 msg->cid = *connection_id;
842 neighbor = GMP_get_short (peer_id);
843 GMP_queue_add (neighbor, msg,
844 GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED,
845 sizeof (struct GNUNET_MESH_ConnectionBroken),
846 NULL, GNUNET_SYSERR, /* connection, fwd */
847 NULL, NULL); /* continuation */
852 * Send keepalive packets for a connection.
854 * @param c Connection to keep alive..
855 * @param fwd Is this a FWD keepalive? (owner -> dest).
858 connection_keepalive (struct MeshConnection *c, int fwd)
860 struct GNUNET_MESH_ConnectionKeepAlive *msg;
861 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
864 LOG (GNUNET_ERROR_TYPE_DEBUG,
865 "sending %s keepalive for connection %s]\n",
866 GM_f2s (fwd), GMC_2s (c));
868 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
869 msg->header.size = htons (size);
870 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
872 msg->reserved = htonl (0);
874 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
879 * Send CONNECTION_{CREATE/ACK} packets for a connection.
881 * @param c Connection for which to send the message.
882 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
885 connection_recreate (struct MeshConnection *c, int fwd)
887 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
891 send_connection_ack (c, GNUNET_NO);
896 * Generic connection timer management.
897 * Depending on the role of the peer in the connection will send the
898 * appropriate message (build or keepalive)
900 * @param c Conncetion to maintain.
904 connection_maintain (struct MeshConnection *c, int fwd)
906 if (GNUNET_NO != c->destroy)
909 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
911 /* TODO DHT GET with RO_BART */
916 case MESH_CONNECTION_NEW:
919 case MESH_CONNECTION_SENT:
920 connection_recreate (c, fwd);
922 case MESH_CONNECTION_READY:
923 connection_keepalive (c, fwd);
932 * Keep the connection alive in the FWD direction.
934 * @param cls Closure (connection to keepalive).
935 * @param tc TaskContext.
938 connection_fwd_keepalive (void *cls,
939 const struct GNUNET_SCHEDULER_TaskContext *tc)
941 struct MeshConnection *c = cls;
943 LOG (GNUNET_ERROR_TYPE_DEBUG, "FWD keepalive for %s\n", GMC_2s (c));
944 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
945 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
948 connection_maintain (c, GNUNET_YES);
950 /* Next execution will be scheduled by message_sent */
955 * Keep the connection alive in the BCK direction.
957 * TODO refactor and merge with connection_fwd_keepalive.
959 * @param cls Closure (connection to keepalive).
960 * @param tc TaskContext.
963 connection_bck_keepalive (void *cls,
964 const struct GNUNET_SCHEDULER_TaskContext *tc)
966 struct MeshConnection *c = cls;
968 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
969 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
972 connection_maintain (c, GNUNET_NO);
974 /* Next execution will be scheduled by message_sent */
979 * Schedule next keepalive task, taking in consideration
980 * the connection state and number of retries.
982 * If the peer is not the origin, do nothing.
984 * @param c Connection for which to schedule the next keepalive.
985 * @param fwd Direction for the next keepalive.
988 schedule_next_keepalive (struct MeshConnection *c, int fwd)
990 struct GNUNET_TIME_Relative delay;
991 GNUNET_SCHEDULER_TaskIdentifier *task_id;
992 GNUNET_SCHEDULER_Task keepalive_task;
994 if (GNUNET_NO == GMC_is_origin (c, fwd))
997 /* Calculate delay to use, depending on the state of the connection */
998 if (MESH_CONNECTION_READY == c->state)
1000 delay = refresh_connection_time;
1004 if (1 > c->create_retry)
1005 c->create_retry = 1;
1006 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1008 if (c->create_retry < 64)
1009 c->create_retry *= 2;
1012 /* Select direction-dependent parameters */
1013 if (GNUNET_YES == fwd)
1015 task_id = &c->fwd_maintenance_task;
1016 keepalive_task = &connection_fwd_keepalive;
1020 task_id = &c->bck_maintenance_task;
1021 keepalive_task = &connection_bck_keepalive;
1024 /* Check that no one scheduled it before us */
1025 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1027 /* No need for a _break. It can happen for instance when sending a SYNACK
1028 * for a duplicate SYN: the first SYNACK scheduled the task. */
1029 GNUNET_SCHEDULER_cancel (*task_id);
1032 /* Schedule the task */
1033 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1038 * @brief Re-initiate traffic on this connection if necessary.
1040 * Check if there is traffic queued towards this peer
1041 * and the core transmit handle is NULL (traffic was stalled).
1042 * If so, call core tmt rdy.
1044 * @param c Connection on which initiate traffic.
1045 * @param fwd Is this about fwd traffic?
1048 connection_unlock_queue (struct MeshConnection *c, int fwd)
1050 struct MeshPeer *peer;
1052 LOG (GNUNET_ERROR_TYPE_DEBUG,
1053 "connection_unlock_queue %s on %s\n",
1054 GM_f2s (fwd), GMC_2s (c));
1056 if (GMC_is_terminal (c, fwd))
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1062 peer = get_hop (c, fwd);
1063 GMP_queue_unlock (peer, c);
1068 * Cancel all transmissions that belong to a certain connection.
1070 * If the connection is scheduled for destruction and no more messages are left,
1071 * the connection will be destroyed by the continuation call.
1073 * @param c Connection which to cancel. Might be destroyed during this call.
1074 * @param fwd Cancel fwd traffic?
1077 connection_cancel_queues (struct MeshConnection *c, int fwd)
1079 struct MeshFlowControl *fc;
1080 struct MeshPeer *peer;
1082 LOG (GNUNET_ERROR_TYPE_DEBUG,
1083 " *** Cancel %s queues for connection %s\n",
1084 GM_f2s (fwd), GMC_2s (c));
1091 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1092 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1094 GNUNET_SCHEDULER_cancel (fc->poll_task);
1095 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1096 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1098 peer = get_hop (c, fwd);
1099 GMP_queue_cancel (peer, c);
1104 * Function called if a connection has been stalled for a while,
1105 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1107 * @param cls Closure (poll ctx).
1108 * @param tc TaskContext.
1111 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1115 * Callback called when a queued POLL message is sent.
1117 * @param cls Closure (FC).
1118 * @param c Connection this message was on.
1119 * @param q Queue handler this call invalidates.
1120 * @param type Type of message sent.
1121 * @param fwd Was this a FWD going message?
1122 * @param size Size of the message.
1125 poll_sent (void *cls,
1126 struct MeshConnection *c,
1127 struct MeshConnectionQueue *q,
1128 uint16_t type, int fwd, size_t size)
1130 struct MeshFlowControl *fc = cls;
1132 if (2 == c->destroy)
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1137 LOG (GNUNET_ERROR_TYPE_DEBUG,
1138 " *** POLL sent for , scheduling new one!\n");
1139 fc->poll_msg = NULL;
1140 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1141 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1142 &connection_poll, fc);
1143 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1148 * Function called if a connection has been stalled for a while,
1149 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1151 * @param cls Closure (poll ctx).
1152 * @param tc TaskContext.
1155 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1157 struct MeshFlowControl *fc = cls;
1158 struct GNUNET_MESH_Poll msg;
1159 struct MeshConnection *c;
1161 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1162 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1168 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1169 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1171 fc == &c->fwd_fc ? "FWD" : "BCK");
1173 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1174 msg.header.size = htons (sizeof (msg));
1175 msg.pid = htonl (fc->last_pid_sent);
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1177 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1178 fc == &c->fwd_fc, GNUNET_YES,
1184 * Timeout function due to lack of keepalive/traffic from the owner.
1185 * Destroys connection if called.
1187 * @param cls Closure (connection to destroy).
1188 * @param tc TaskContext.
1191 connection_fwd_timeout (void *cls,
1192 const struct GNUNET_SCHEDULER_TaskContext *tc)
1194 struct MeshConnection *c = cls;
1196 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1197 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1200 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1202 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1213 * Timeout function due to lack of keepalive/traffic from the destination.
1214 * Destroys connection if called.
1216 * @param cls Closure (connection to destroy).
1217 * @param tc TaskContext
1220 connection_bck_timeout (void *cls,
1221 const struct GNUNET_SCHEDULER_TaskContext *tc)
1223 struct MeshConnection *c = cls;
1225 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1226 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1232 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1243 * Resets the connection timeout task, some other message has done the
1245 * - For the first peer on the direction this means to send
1246 * a keepalive or a path confirmation message (either create or ACK).
1247 * - For all other peers, this means to destroy the connection,
1248 * due to lack of activity.
1249 * Starts the timeout if no timeout was running (connection just created).
1251 * @param c Connection whose timeout to reset.
1252 * @param fwd Is this forward?
1254 * TODO use heap to improve efficiency of scheduler.
1257 connection_reset_timeout (struct MeshConnection *c, int fwd)
1259 GNUNET_SCHEDULER_TaskIdentifier *ti;
1260 GNUNET_SCHEDULER_Task f;
1262 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1264 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1266 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1267 GNUNET_SCHEDULER_cancel (*ti);
1269 if (GMC_is_origin (c, fwd)) /* Startpoint */
1271 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1272 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1274 else /* Relay, endpoint. */
1276 struct GNUNET_TIME_Relative delay;
1278 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1279 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1280 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1286 * Add the connection to the list of both neighbors.
1288 * @param c Connection.
1290 * @return #GNUNET_OK if everything went fine
1291 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1294 register_neighbors (struct MeshConnection *c)
1296 struct MeshPeer *next_peer;
1297 struct MeshPeer *prev_peer;
1299 next_peer = get_next_hop (c);
1300 prev_peer = get_prev_hop (c);
1302 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1304 path_debug (c->path);
1305 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1307 GMC_2s (c), next_peer);
1308 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GMP_2s (next_peer));
1309 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1310 GMC_2s (c), prev_peer);
1311 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GMP_2s (prev_peer));
1313 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1314 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1316 if (GMC_is_origin (c, GNUNET_YES))
1317 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1318 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1320 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1321 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1322 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1324 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1325 return GNUNET_SYSERR;
1328 GMP_add_connection (next_peer, c);
1329 GMP_add_connection (prev_peer, c);
1336 * Remove the connection from the list of both neighbors.
1338 * @param c Connection.
1341 unregister_neighbors (struct MeshConnection *c)
1343 struct MeshPeer *peer;
1345 peer = get_next_hop (c);
1346 if (GNUNET_OK != GMP_remove_connection (peer, c))
1348 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1349 || MESH_CONNECTION_DESTROYED == c->state);
1350 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1351 if (NULL != c->t) GMT_debug (c->t);
1354 peer = get_prev_hop (c);
1355 if (GNUNET_OK != GMP_remove_connection (peer, c))
1357 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1358 || MESH_CONNECTION_DESTROYED == c->state);
1359 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1360 if (NULL != c->t) GMT_debug (c->t);
1366 * Bind the connection to the peer and the tunnel to that peer.
1368 * If the peer has no tunnel, create one. Update tunnel and connection
1369 * data structres to reflect new status.
1371 * @param c Connection.
1375 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1377 GMP_add_tunnel (peer);
1378 c->t = GMP_get_tunnel (peer);
1379 GMT_add_connection (c->t, c);
1384 * Builds a path from a PeerIdentity array.
1386 * @param peers PeerIdentity array.
1387 * @param size Size of the @c peers array.
1388 * @param own_pos Output parameter: own position in the path.
1390 * @return Fixed and shortened path.
1392 static struct MeshPeerPath *
1393 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1395 unsigned int *own_pos)
1397 struct MeshPeerPath *path;
1398 GNUNET_PEER_Id shortid;
1401 unsigned int offset;
1404 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1405 path = path_new (size);
1408 for (i = 0; i < size; i++)
1410 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1411 i, GNUNET_i2s (&peers[i]));
1412 shortid = GNUNET_PEER_intern (&peers[i]);
1414 /* Check for loops / duplicates */
1415 for (j = 0; j < i - offset; j++)
1417 if (path->peers[j] == shortid)
1419 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1421 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset);
1422 GNUNET_PEER_change_rc (shortid, -1);
1425 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1426 path->peers[i - offset] = shortid;
1427 if (path->peers[i] == myid)
1430 path->length -= offset;
1432 if (path->peers[*own_pos] != myid)
1434 /* create path: self not found in path through self */
1435 GNUNET_break_op (0);
1436 path_destroy (path);
1445 * Log receipt of message on stderr (INFO level).
1447 * @param message Message received.
1448 * @param peer Peer who sent the message.
1451 log_message (const struct GNUNET_MessageHeader *message,
1452 const struct GNUNET_PeerIdentity *peer)
1454 LOG (GNUNET_ERROR_TYPE_INFO, "<- %s from %s\n",
1455 GM_m2s (ntohs (message->type)), GNUNET_i2s (peer));
1458 /******************************************************************************/
1459 /******************************** API ***********************************/
1460 /******************************************************************************/
1463 * Core handler for connection creation.
1465 * @param cls Closure (unused).
1466 * @param peer Sender (neighbor).
1467 * @param message Message.
1469 * @return GNUNET_OK to keep the connection open,
1470 * GNUNET_SYSERR to close it (signal serious error)
1473 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1474 const struct GNUNET_MessageHeader *message)
1476 struct GNUNET_MESH_ConnectionCreate *msg;
1477 struct GNUNET_PeerIdentity *id;
1478 struct GNUNET_HashCode *cid;
1479 struct MeshPeerPath *path;
1480 struct MeshPeer *dest_peer;
1481 struct MeshPeer *orig_peer;
1482 struct MeshConnection *c;
1483 unsigned int own_pos;
1486 log_message (message, peer);
1489 size = ntohs (message->size);
1490 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1492 GNUNET_break_op (0);
1496 /* Calculate hops */
1497 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1498 if (size % sizeof (struct GNUNET_PeerIdentity))
1500 GNUNET_break_op (0);
1503 size /= sizeof (struct GNUNET_PeerIdentity);
1506 GNUNET_break_op (0);
1509 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1511 /* Get parameters */
1512 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1514 id = (struct GNUNET_PeerIdentity *) &msg[1];
1515 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s (%s->).\n",
1516 GNUNET_h2s (cid), GNUNET_i2s (id));
1518 /* Create connection */
1519 c = connection_get (cid);
1522 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1528 GNUNET_break_op (0);
1529 path_destroy (path);
1532 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1533 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1534 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1537 if (path->length - 1 == own_pos)
1539 /* If we are destination, why did the creation fail? */
1543 send_broken2 (cid, &my_full_id,
1544 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1545 path->peers[own_pos - 1]);
1546 path_destroy (path);
1549 GMP_add_path_to_all (path, GNUNET_NO);
1550 connection_reset_timeout (c, GNUNET_YES);
1554 path = path_duplicate (c->path);
1556 if (MESH_CONNECTION_NEW == c->state)
1557 connection_change_state (c, MESH_CONNECTION_SENT);
1559 /* Remember peers */
1560 dest_peer = GMP_get (&id[size - 1]);
1561 orig_peer = GMP_get (&id[0]);
1563 /* Is it a connection to us? */
1564 if (c->own_pos == size - 1)
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1567 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1569 add_to_peer (c, orig_peer);
1570 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1571 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1573 send_connection_ack (c, GNUNET_NO);
1574 if (MESH_CONNECTION_SENT == c->state)
1575 connection_change_state (c, MESH_CONNECTION_ACK);
1579 /* It's for somebody else! Retransmit. */
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1581 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1582 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1583 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1586 path_destroy (path);
1592 * Core handler for path confirmations.
1594 * @param cls closure
1595 * @param message message
1596 * @param peer peer identity this notification is about
1598 * @return GNUNET_OK to keep the connection open,
1599 * GNUNET_SYSERR to close it (signal serious error)
1602 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1603 const struct GNUNET_MessageHeader *message)
1605 struct GNUNET_MESH_ConnectionACK *msg;
1606 struct MeshConnection *c;
1607 struct MeshPeerPath *p;
1608 struct MeshPeer *pi;
1609 enum MeshConnectionState oldstate;
1612 log_message (message, peer);
1614 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1616 GNUNET_h2s (&msg->cid));
1617 c = connection_get (&msg->cid);
1620 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1626 if (GNUNET_NO != c->destroy)
1628 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1632 oldstate = c->state;
1633 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1634 pi = GMP_get (peer);
1635 if (get_next_hop (c) == pi)
1637 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1639 if (MESH_CONNECTION_SENT == oldstate)
1640 connection_change_state (c, MESH_CONNECTION_ACK);
1642 else if (get_prev_hop (c) == pi)
1644 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1646 connection_change_state (c, MESH_CONNECTION_READY);
1650 GNUNET_break_op (0);
1654 connection_reset_timeout (c, fwd);
1656 /* Add path to peers? */
1660 GMP_add_path_to_all (p, GNUNET_YES);
1667 /* Message for us as creator? */
1668 if (GMC_is_origin (c, GNUNET_YES))
1670 if (GNUNET_NO != fwd)
1672 GNUNET_break_op (0);
1675 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1677 /* If just created, cancel the short timeout and start a long one */
1678 if (MESH_CONNECTION_SENT == oldstate)
1679 connection_reset_timeout (c, GNUNET_YES);
1681 /* Change connection state */
1682 connection_change_state (c, MESH_CONNECTION_READY);
1683 send_connection_ack (c, GNUNET_YES);
1685 /* Change tunnel state, trigger KX */
1686 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1687 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1692 /* Message for us as destination? */
1693 if (GMC_is_terminal (c, GNUNET_YES))
1695 if (GNUNET_YES != fwd)
1697 GNUNET_break_op (0);
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1702 /* If just created, cancel the short timeout and start a long one */
1703 if (MESH_CONNECTION_ACK == oldstate)
1704 connection_reset_timeout (c, GNUNET_NO);
1706 /* Change tunnel state */
1707 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1708 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1713 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1714 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1720 * Core handler for notifications of broken paths
1722 * @param cls Closure (unused).
1723 * @param id Peer identity of sending neighbor.
1724 * @param message Message.
1726 * @return GNUNET_OK to keep the connection open,
1727 * GNUNET_SYSERR to close it (signal serious error)
1730 GMC_handle_broken (void* cls,
1731 const struct GNUNET_PeerIdentity* id,
1732 const struct GNUNET_MessageHeader* message)
1734 struct GNUNET_MESH_ConnectionBroken *msg;
1735 struct MeshConnection *c;
1738 log_message (message, id);
1740 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1741 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1742 GNUNET_i2s (&msg->peer1));
1743 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1744 GNUNET_i2s (&msg->peer2));
1745 c = connection_get (&msg->cid);
1748 GNUNET_break_op (0);
1752 fwd = is_fwd (c, id);
1753 if (GMC_is_terminal (c, fwd))
1755 path_invalidate (c->path);
1756 if (0 < c->pending_messages)
1757 c->destroy = GNUNET_YES;
1763 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1764 c->destroy = GNUNET_YES;
1765 connection_cancel_queues (c, !fwd);
1774 * Core handler for tunnel destruction
1776 * @param cls Closure (unused).
1777 * @param peer Peer identity of sending neighbor.
1778 * @param message Message.
1780 * @return GNUNET_OK to keep the connection open,
1781 * GNUNET_SYSERR to close it (signal serious error)
1784 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1785 const struct GNUNET_MessageHeader *message)
1787 struct GNUNET_MESH_ConnectionDestroy *msg;
1788 struct MeshConnection *c;
1791 log_message (message, peer);
1793 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1794 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GNUNET_h2s (&msg->cid));
1795 c = connection_get (&msg->cid);
1798 /* Probably already got the message from another path,
1799 * destroyed the tunnel and retransmitted to children.
1802 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1804 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1807 fwd = is_fwd (c, peer);
1808 if (GNUNET_SYSERR == fwd)
1810 GNUNET_break_op (0); /* FIXME */
1813 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1814 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1815 else if (0 == c->pending_messages)
1817 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1821 c->destroy = GNUNET_YES;
1822 c->state = MESH_CONNECTION_DESTROYED;
1825 GMT_remove_connection (c->t, c);
1833 * Generic handler for mesh network encrypted traffic.
1835 * @param peer Peer identity this notification is about.
1836 * @param msg Encrypted message.
1838 * @return GNUNET_OK to keep the connection open,
1839 * GNUNET_SYSERR to close it (signal serious error)
1842 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1843 const struct GNUNET_MESH_Encrypted *msg)
1845 struct MeshConnection *c;
1846 struct MeshPeer *neighbor;
1847 struct MeshFlowControl *fc;
1848 GNUNET_PEER_Id peer_id;
1855 size = ntohs (msg->header.size);
1857 sizeof (struct GNUNET_MESH_Encrypted) +
1858 sizeof (struct GNUNET_MessageHeader))
1860 GNUNET_break_op (0);
1864 /* Check connection */
1865 c = connection_get (&msg->cid);
1868 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1869 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
1870 GNUNET_h2s (&msg->cid));
1874 LOG (GNUNET_ERROR_TYPE_INFO, " on connection %s\n", GMC_2s (c));
1876 /* Check if origin is as expected */
1877 neighbor = get_prev_hop (c);
1878 peer_id = GNUNET_PEER_search (peer);
1879 if (peer_id == GMP_get_short_id (neighbor))
1885 neighbor = get_next_hop (c);
1886 if (peer_id == GMP_get_short_id (neighbor))
1892 /* Unexpected peer sending traffic on a connection. */
1893 GNUNET_break_op (0);
1899 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1900 pid = ntohl (msg->pid);
1901 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1903 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1904 LOG (GNUNET_ERROR_TYPE_DEBUG,
1905 "WARNING Received PID %u, (prev %u), ACK %u\n",
1906 pid, fc->last_pid_recv, fc->last_ack_sent);
1909 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1911 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1912 LOG (GNUNET_ERROR_TYPE_DEBUG,
1913 " Pid %u not expected (%u+), dropping!\n",
1914 pid, fc->last_pid_recv + 1);
1917 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1918 connection_change_state (c, MESH_CONNECTION_READY);
1919 connection_reset_timeout (c, fwd);
1920 fc->last_pid_recv = pid;
1922 /* Is this message for us? */
1923 if (GMC_is_terminal (c, fwd))
1925 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1926 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1930 GNUNET_break (GNUNET_NO != c->destroy);
1933 fc->last_pid_recv = pid;
1934 GMT_handle_encrypted (c->t, msg);
1935 GMC_send_ack (c, fwd, GNUNET_NO);
1939 /* Message not for us: forward to next hop */
1940 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1941 ttl = ntohl (msg->ttl);
1942 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1945 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1946 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1947 GMC_send_ack (c, fwd, GNUNET_NO);
1951 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1952 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1958 * Generic handler for mesh network encrypted traffic.
1960 * @param peer Peer identity this notification is about.
1961 * @param msg Encrypted message.
1963 * @return GNUNET_OK to keep the connection open,
1964 * GNUNET_SYSERR to close it (signal serious error)
1967 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1968 const struct GNUNET_MESH_KX *msg)
1970 struct MeshConnection *c;
1971 struct MeshPeer *neighbor;
1972 GNUNET_PEER_Id peer_id;
1977 size = ntohs (msg->header.size);
1979 sizeof (struct GNUNET_MESH_Encrypted) +
1980 sizeof (struct GNUNET_MessageHeader))
1982 GNUNET_break_op (0);
1986 /* Check connection */
1987 c = connection_get (&msg->cid);
1990 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1991 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING kx on unknown connection %s\n",
1992 GNUNET_h2s (&msg->cid));
1995 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1997 /* Check if origin is as expected */
1998 neighbor = get_prev_hop (c);
1999 peer_id = GNUNET_PEER_search (peer);
2000 if (peer_id == GMP_get_short_id (neighbor))
2006 neighbor = get_next_hop (c);
2007 if (peer_id == GMP_get_short_id (neighbor))
2013 /* Unexpected peer sending traffic on a connection. */
2014 GNUNET_break_op (0);
2019 /* Count as connection confirmation. */
2020 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
2022 connection_change_state (c, MESH_CONNECTION_READY);
2025 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
2026 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
2029 connection_reset_timeout (c, fwd);
2031 /* Is this message for us? */
2032 if (GMC_is_terminal (c, fwd))
2034 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2035 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2041 GMT_handle_kx (c->t, &msg[1].header);
2045 /* Message not for us: forward to next hop */
2046 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2047 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2048 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
2055 * Core handler for encrypted mesh network traffic (channel mgmt, data).
2057 * @param cls Closure (unused).
2058 * @param message Message received.
2059 * @param peer Peer who sent the message.
2061 * @return GNUNET_OK to keep the connection open,
2062 * GNUNET_SYSERR to close it (signal serious error)
2065 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2066 const struct GNUNET_MessageHeader *message)
2068 log_message (message, peer);
2070 return handle_mesh_encrypted (peer,
2071 (struct GNUNET_MESH_Encrypted *)message);
2076 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2078 * @param cls Closure (unused).
2079 * @param message Message received.
2080 * @param peer Peer who sent the message.
2082 * @return GNUNET_OK to keep the connection open,
2083 * GNUNET_SYSERR to close it (signal serious error)
2086 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2087 const struct GNUNET_MessageHeader *message)
2089 log_message (message, peer);
2091 return handle_mesh_kx (peer,
2092 (struct GNUNET_MESH_KX *) message);
2097 * Core handler for mesh network traffic point-to-point acks.
2099 * @param cls closure
2100 * @param message message
2101 * @param peer peer identity this notification is about
2103 * @return GNUNET_OK to keep the connection open,
2104 * GNUNET_SYSERR to close it (signal serious error)
2107 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2108 const struct GNUNET_MessageHeader *message)
2110 struct GNUNET_MESH_ACK *msg;
2111 struct MeshConnection *c;
2112 struct MeshFlowControl *fc;
2117 log_message (message, peer);
2119 msg = (struct GNUNET_MESH_ACK *) message;
2120 c = connection_get (&msg->cid);
2123 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2128 /* Is this a forward or backward ACK? */
2129 id = GNUNET_PEER_search (peer);
2130 if (GMP_get_short_id (get_next_hop (c)) == id)
2132 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2136 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2138 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2144 GNUNET_break_op (0);
2148 ack = ntohl (msg->ack);
2149 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2150 ack, fc->last_ack_recv);
2151 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
2152 fc->last_ack_recv = ack;
2154 /* Cancel polling if the ACK is big enough. */
2155 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2156 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2158 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2159 GNUNET_SCHEDULER_cancel (fc->poll_task);
2160 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2161 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2164 connection_unlock_queue (c, fwd);
2171 * Core handler for mesh network traffic point-to-point ack polls.
2173 * @param cls closure
2174 * @param message message
2175 * @param peer peer identity this notification is about
2177 * @return GNUNET_OK to keep the connection open,
2178 * GNUNET_SYSERR to close it (signal serious error)
2181 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2182 const struct GNUNET_MessageHeader *message)
2184 struct GNUNET_MESH_Poll *msg;
2185 struct MeshConnection *c;
2186 struct MeshFlowControl *fc;
2191 log_message (message, peer);
2193 msg = (struct GNUNET_MESH_Poll *) message;
2194 c = connection_get (&msg->cid);
2197 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2199 LOG (GNUNET_ERROR_TYPE_DEBUG,
2200 "WARNING POLL message on unknown connection %s!\n",
2201 GNUNET_h2s (&msg->cid));
2205 /* Is this a forward or backward ACK?
2206 * Note: a poll should never be needed in a loopback case,
2207 * since there is no possiblility of packet loss there, so
2208 * this way of discerining FWD/BCK should not be a problem.
2210 id = GNUNET_PEER_search (peer);
2211 if (GMP_get_short_id (get_next_hop (c)) == id)
2213 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2216 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2218 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2223 GNUNET_break_op (0);
2227 pid = ntohl (msg->pid);
2228 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2229 fc->last_pid_recv = pid;
2230 fwd = fc == &c->bck_fc;
2231 GMC_send_ack (c, fwd, GNUNET_YES);
2238 * Core handler for mesh keepalives.
2240 * @param cls closure
2241 * @param message message
2242 * @param peer peer identity this notification is about
2243 * @return GNUNET_OK to keep the connection open,
2244 * GNUNET_SYSERR to close it (signal serious error)
2246 * TODO: Check who we got this from, to validate route.
2249 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2250 const struct GNUNET_MessageHeader *message)
2252 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2253 struct MeshConnection *c;
2254 struct MeshPeer *neighbor;
2255 GNUNET_PEER_Id peer_id;
2258 log_message (message, peer);
2260 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2261 c = connection_get (&msg->cid);
2264 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2269 /* Check if origin is as expected TODO refactor and reuse */
2270 peer_id = GNUNET_PEER_search (peer);
2271 neighbor = get_prev_hop (c);
2272 if (peer_id == GMP_get_short_id (neighbor))
2278 neighbor = get_next_hop (c);
2279 if (peer_id == GMP_get_short_id (neighbor))
2285 GNUNET_break_op (0);
2290 connection_change_state (c, MESH_CONNECTION_READY);
2291 connection_reset_timeout (c, fwd);
2293 if (GMC_is_terminal (c, fwd))
2296 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2297 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2304 * Send an ACK on the appropriate connection/channel, depending on
2305 * the direction and the position of the peer.
2307 * @param c Which connection to send the hop-by-hop ACK.
2308 * @param fwd Is this a fwd ACK? (will go dest->root).
2309 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2312 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2314 unsigned int buffer;
2316 LOG (GNUNET_ERROR_TYPE_DEBUG,
2317 "GMC send %s ACK on %s\n",
2318 GM_f2s (fwd), GMC_2s (c));
2326 if (GNUNET_NO != c->destroy)
2328 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2332 /* Get available buffer space */
2333 if (GMC_is_terminal (c, fwd))
2335 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2336 buffer = GMT_get_channels_buffer (c->t);
2340 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2341 buffer = GMC_get_buffer (c, fwd);
2343 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2344 if (0 == buffer && GNUNET_NO == force)
2347 /* Send available buffer space */
2348 if (GMC_is_origin (c, fwd))
2350 GNUNET_assert (NULL != c->t);
2351 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2352 GMT_unchoke_channels (c->t);
2356 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2357 send_ack (c, buffer, fwd, force);
2363 * Initialize the connections subsystem
2365 * @param c Configuration handle.
2368 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2370 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2372 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2375 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2376 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2377 GNUNET_SCHEDULER_shutdown ();
2382 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2385 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2386 "MESH", "MAX_CONNECTIONS", "MISSING");
2387 GNUNET_SCHEDULER_shutdown ();
2392 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2393 &refresh_connection_time))
2395 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2396 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2397 GNUNET_SCHEDULER_shutdown ();
2400 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2401 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2406 * Destroy each connection on shutdown.
2408 * @param cls Closure (unused).
2409 * @param key Current key code (CID, unused).
2410 * @param value Value in the hash map (connection)
2412 * @return #GNUNET_YES, because we should continue to iterate,
2415 shutdown_iterator (void *cls,
2416 const struct GNUNET_HashCode *key,
2419 struct MeshConnection *c = value;
2427 * Shut down the connections subsystem.
2432 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2433 GNUNET_CONTAINER_multihashmap_destroy (connections);
2438 struct MeshConnection *
2439 GMC_new (const struct GNUNET_HashCode *cid,
2440 struct MeshTunnel3 *t,
2441 struct MeshPeerPath *p,
2442 unsigned int own_pos)
2444 struct MeshConnection *c;
2446 c = GNUNET_new (struct MeshConnection);
2448 GNUNET_assert (GNUNET_OK ==
2449 GNUNET_CONTAINER_multihashmap_put (connections,
2451 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2452 fc_init (&c->fwd_fc);
2453 fc_init (&c->bck_fc);
2458 GNUNET_assert (own_pos <= p->length - 1);
2459 c->own_pos = own_pos;
2462 if (GNUNET_OK != register_neighbors (c))
2466 path_invalidate (c->path);
2479 GMC_destroy (struct MeshConnection *c)
2487 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2488 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2491 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2492 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2493 &c->fwd_fc, &c->bck_fc);
2494 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2495 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2497 /* Cancel all traffic */
2498 if (NULL != c->path)
2500 connection_cancel_queues (c, GNUNET_YES);
2501 connection_cancel_queues (c, GNUNET_NO);
2502 unregister_neighbors (c);
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 maintainance task (keepalive/timeout) */
2509 if (NULL != c->fwd_fc.poll_msg)
2511 GMC_cancel (c->fwd_fc.poll_msg);
2512 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2514 if (NULL != c->bck_fc.poll_msg)
2516 GMC_cancel (c->bck_fc.poll_msg);
2517 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2520 /* Delete from tunnel */
2522 GMT_remove_connection (c->t, c);
2524 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2525 path_destroy (c->path);
2526 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2527 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2528 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2529 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2530 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2532 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2533 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2535 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2537 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2538 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2541 GNUNET_break (GNUNET_YES ==
2542 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2544 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2549 * Get the connection ID.
2551 * @param c Connection to get the ID from.
2553 * @return ID of the connection.
2555 const struct GNUNET_HashCode *
2556 GMC_get_id (const struct MeshConnection *c)
2563 * Get the connection path.
2565 * @param c Connection to get the path from.
2567 * @return path used by the connection.
2569 const struct MeshPeerPath *
2570 GMC_get_path (const struct MeshConnection *c)
2572 if (GNUNET_NO == c->destroy)
2579 * Get the connection state.
2581 * @param c Connection to get the state from.
2583 * @return state of the connection.
2585 enum MeshConnectionState
2586 GMC_get_state (const struct MeshConnection *c)
2592 * Get the connection tunnel.
2594 * @param c Connection to get the tunnel from.
2596 * @return tunnel of the connection.
2598 struct MeshTunnel3 *
2599 GMC_get_tunnel (const struct MeshConnection *c)
2606 * Get free buffer space in a connection.
2608 * @param c Connection.
2609 * @param fwd Is query about FWD traffic?
2611 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2614 GMC_get_buffer (struct MeshConnection *c, int fwd)
2616 struct MeshFlowControl *fc;
2618 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2620 return (fc->queue_max - fc->queue_n);
2624 * Get how many messages have we allowed to send to us from a direction.
2626 * @param c Connection.
2627 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2629 * @return last_ack_sent - last_pid_recv
2632 GMC_get_allowed (struct MeshConnection *c, int fwd)
2634 struct MeshFlowControl *fc;
2636 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2637 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2641 return (fc->last_ack_sent - fc->last_pid_recv);
2645 * Get messages queued in a connection.
2647 * @param c Connection.
2648 * @param fwd Is query about FWD traffic?
2650 * @return Number of messages queued.
2653 GMC_get_qn (struct MeshConnection *c, int fwd)
2655 struct MeshFlowControl *fc;
2657 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2664 * Allow the connection to advertise a buffer of the given size.
2666 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2667 * allowing up to last_pid_recv + buffer.
2669 * @param c Connection.
2670 * @param buffer How many more messages the connection can accept.
2671 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2674 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2676 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2677 GMC_2s (c), buffer, GM_f2s (fwd));
2678 send_ack (c, buffer, fwd, GNUNET_NO);
2683 * Notify other peers on a connection of a broken link. Mark connections
2684 * to destroy after all traffic has been sent.
2686 * @param c Connection on which there has been a disconnection.
2687 * @param peer Peer that disconnected.
2690 GMC_notify_broken (struct MeshConnection *c,
2691 struct MeshPeer *peer)
2695 LOG (GNUNET_ERROR_TYPE_DEBUG,
2696 " notify broken on %s due to %s disconnect\n",
2697 GMC_2s (c), GMP_2s (peer));
2699 fwd = peer == get_prev_hop (c);
2701 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2703 /* Local shutdown, no one to notify about this. */
2707 if (GNUNET_NO == c->destroy)
2708 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2710 /* Connection will have at least one pending message
2711 * (the one we just scheduled), so no point in checking whether to
2712 * destroy immediately. */
2713 c->destroy = GNUNET_YES;
2714 c->state = MESH_CONNECTION_DESTROYED;
2717 * Cancel all queues, if no message is left, connection will be destroyed.
2719 connection_cancel_queues (c, !fwd);
2726 * Is this peer the first one on the connection?
2728 * @param c Connection.
2729 * @param fwd Is this about fwd traffic?
2731 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2734 GMC_is_origin (struct MeshConnection *c, int fwd)
2736 if (!fwd && c->path->length - 1 == c->own_pos )
2738 if (fwd && 0 == c->own_pos)
2745 * Is this peer the last one on the connection?
2747 * @param c Connection.
2748 * @param fwd Is this about fwd traffic?
2749 * Note that the ROOT is the terminal for BCK traffic!
2751 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2754 GMC_is_terminal (struct MeshConnection *c, int fwd)
2756 return GMC_is_origin (c, !fwd);
2761 * See if we are allowed to send by the next hop in the given direction.
2763 * @param c Connection.
2764 * @param fwd Is this about fwd traffic?
2766 * @return #GNUNET_YES in case it's OK to send.
2769 GMC_is_sendable (struct MeshConnection *c, int fwd)
2771 struct MeshFlowControl *fc;
2778 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2779 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2785 * Sends an already built message on a connection, properly registering
2786 * all used resources.
2788 * @param message Message to send. Function makes a copy of it.
2789 * If message is not hop-by-hop, decrements TTL of copy.
2790 * @param c Connection on which this message is transmitted.
2791 * @param fwd Is this a fwd message?
2792 * @param force Force the connection to accept the message (buffer overfill).
2793 * @param cont Continuation called once message is sent. Can be NULL.
2794 * @param cont_cls Closure for @c cont.
2796 * @return Handle to cancel the message before it's sent.
2797 * NULL on error or if @c cont is NULL.
2798 * Invalid on @c cont call.
2800 struct MeshConnectionQueue *
2801 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2802 struct MeshConnection *c, int fwd, int force,
2803 GMC_sent cont, void *cont_cls)
2805 struct MeshFlowControl *fc;
2806 struct MeshConnectionQueue *q;
2812 size = ntohs (message->size);
2813 data = GNUNET_malloc (size);
2814 memcpy (data, message, size);
2815 type = ntohs (message->type);
2816 LOG (GNUNET_ERROR_TYPE_INFO, "-> %s (%u bytes) on connection %s\n",
2817 GM_m2s (type), size, GMC_2s (c));
2819 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2820 droppable = GNUNET_NO == force;
2823 struct GNUNET_MESH_Encrypted *emsg;
2824 struct GNUNET_MESH_KX *kmsg;
2825 struct GNUNET_MESH_ACK *amsg;
2826 struct GNUNET_MESH_Poll *pmsg;
2827 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2828 struct GNUNET_MESH_ConnectionBroken *bmsg;
2831 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2832 emsg = (struct GNUNET_MESH_Encrypted *) data;
2833 ttl = ntohl (emsg->ttl);
2836 GNUNET_break_op (0);
2841 emsg->ttl = htonl (ttl - 1);
2842 emsg->pid = htonl (fc->next_pid++);
2843 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2844 if (GNUNET_YES == droppable)
2847 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2848 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2849 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2853 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2855 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2857 GMC_start_poll (c, fwd);
2861 case GNUNET_MESSAGE_TYPE_MESH_KX:
2862 kmsg = (struct GNUNET_MESH_KX *) data;
2866 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2867 amsg = (struct GNUNET_MESH_ACK *) data;
2869 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2870 droppable = GNUNET_NO;
2873 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2874 pmsg = (struct GNUNET_MESH_Poll *) data;
2876 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2877 droppable = GNUNET_NO;
2880 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2881 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2886 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2887 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2892 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2893 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2894 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2903 if (fc->queue_n > fc->queue_max && droppable)
2905 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2908 LOG (GNUNET_ERROR_TYPE_DEBUG,
2909 "queue full: %u/%u\n",
2910 fc->queue_n, fc->queue_max);
2911 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2917 return NULL; /* Drop this message */
2920 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2921 c->pending_messages++;
2923 q = GNUNET_new (struct MeshConnectionQueue);
2924 q->forced = !droppable;
2925 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2929 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2935 q->cont_cls = cont_cls;
2941 * Cancel a previously sent message while it's in the queue.
2943 * ONLY can be called before the continuation given to the send function
2944 * is called. Once the continuation is called, the message is no longer in the
2947 * @param q Handle to the queue.
2950 GMC_cancel (struct MeshConnectionQueue *q)
2952 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2954 /* queue destroy calls message_sent, which calls q->cont and frees q */
2955 GMP_queue_destroy (q->q, GNUNET_YES);
2960 * Sends a CREATE CONNECTION message for a path to a peer.
2961 * Changes the connection and tunnel states if necessary.
2963 * @param connection Connection to create.
2966 GMC_send_create (struct MeshConnection *connection)
2968 enum MeshTunnel3CState state;
2971 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2972 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2974 LOG (GNUNET_ERROR_TYPE_INFO, "Send %s (%u bytes) on connection %s\n",
2975 GM_m2s (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE),
2976 size, GMC_2s (connection));
2977 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2978 connection, connection->pending_messages);
2979 connection->pending_messages++;
2981 connection->maintenance_q =
2982 GMP_queue_add (get_next_hop (connection), NULL,
2983 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2984 size, connection, GNUNET_YES, &message_sent, NULL);
2986 state = GMT_get_cstate (connection->t);
2987 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2988 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2989 if (MESH_CONNECTION_NEW == connection->state)
2990 connection_change_state (connection, MESH_CONNECTION_SENT);
2995 * Send a message to all peers in this connection that the connection
2996 * is no longer valid.
2998 * If some peer should not receive the message, it should be zero'ed out
2999 * before calling this function.
3001 * @param c The connection whose peers to notify.
3004 GMC_send_destroy (struct MeshConnection *c)
3006 struct GNUNET_MESH_ConnectionDestroy msg;
3008 if (GNUNET_YES == c->destroy)
3011 msg.header.size = htons (sizeof (msg));
3012 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
3014 LOG (GNUNET_ERROR_TYPE_DEBUG,
3015 " sending connection destroy for connection %s\n",
3018 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
3019 GMC_send_prebuilt_message (&msg.header, c,
3020 GNUNET_YES, GNUNET_YES, NULL, NULL);
3021 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
3022 GMC_send_prebuilt_message (&msg.header, c,
3023 GNUNET_NO, GNUNET_YES, NULL, NULL);
3024 c->destroy = GNUNET_YES;
3025 c->state = MESH_CONNECTION_DESTROYED;
3030 * @brief Start a polling timer for the connection.
3032 * When a neighbor does not accept more traffic on the connection it could be
3033 * caused by a simple congestion or by a lost ACK. Polling enables to check
3034 * for the lastest ACK status for a connection.
3036 * @param c Connection.
3037 * @param fwd Should we poll in the FWD direction?
3040 GMC_start_poll (struct MeshConnection *c, int fwd)
3042 struct MeshFlowControl *fc;
3044 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3045 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3047 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3049 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3050 fc->poll_task, fc->poll_msg);
3053 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3054 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3061 * @brief Stop polling a connection for ACKs.
3063 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3065 * @param c Connection.
3066 * @param fwd Should we stop the poll in the FWD direction?
3069 GMC_stop_poll (struct MeshConnection *c, int fwd)
3071 struct MeshFlowControl *fc;
3073 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3074 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3076 GNUNET_SCHEDULER_cancel (fc->poll_task);
3077 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3082 * Get a (static) string for a connection.
3084 * @param c Connection.
3087 GMC_2s (const struct MeshConnection *c)
3094 static char buf[128];
3096 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
3099 return GNUNET_h2s (&c->id);