2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh_connection.c
23 * @brief GNUnet MESH service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "mesh_path.h"
33 #include "mesh_protocol.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
42 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
43 GNUNET_TIME_UNIT_MINUTES,\
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct to encapsulate all the Flow Control information to a peer to which
54 * we are directly connected (on a core level).
56 struct MeshFlowControl
59 * Connection this controls.
61 struct MeshConnection *c;
64 * How many messages are in the queue on this connection.
69 * How many messages do we accept in the queue.
71 unsigned int queue_max;
79 * ID of the last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 GNUNET_SCHEDULER_TaskIdentifier poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
109 * Queued poll message, to cancel if not necessary anymore (got ACK).
111 struct MeshConnectionQueue *poll_msg;
114 * Queued poll message, to cancel if not necessary anymore (got ACK).
116 struct MeshConnectionQueue *ack_msg;
120 * Keep a record of the last messages sent on this connection.
122 struct MeshConnectionPerformance
125 * Circular buffer for storing measurements.
127 double usecsperbyte[AVG_MSGS];
130 * Running average of @c usecsperbyte.
135 * How many values of @c usecsperbyte are valid.
140 * Index of the next "free" position in @c usecsperbyte.
147 * Struct containing all information regarding a connection to a peer.
149 struct MeshConnection
152 * Tunnel this connection is part of.
154 struct MeshTunnel3 *t;
157 * Flow control information for traffic fwd.
159 struct MeshFlowControl fwd_fc;
162 * Flow control information for traffic bck.
164 struct MeshFlowControl bck_fc;
167 * Measure connection performance on the endpoint.
169 struct MeshConnectionPerformance *perf;
172 * ID of the connection.
174 struct GNUNET_HashCode id;
177 * State of the connection.
179 enum MeshConnectionState state;
182 * Path being used for the tunnel. At the origin of the connection
183 * it's a pointer to the destination's path pool, otherwise just a copy.
185 struct MeshPeerPath *path;
188 * Position of the local peer in the path.
190 unsigned int own_pos;
193 * Task to keep the used paths alive at the owner,
194 * time tunnel out on all the other peers.
196 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
199 * Task to keep the used paths alive at the destination,
200 * time tunnel out on all the other peers.
202 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
205 * Queue handle for maintainance traffic. One handle for FWD and BCK since
206 * one peer never needs to maintain both directions (no loopback connections).
208 struct MeshPeerQueue *maintenance_q;
211 * Counter to do exponential backoff when creating a connection (max 64).
213 unsigned short create_retry;
216 * Pending message count.
218 int pending_messages;
221 * Destroy flag: if true, destroy on last message.
227 * Handle for messages queued but not yet sent.
229 struct MeshConnectionQueue
232 * Peer queue handle, to cancel if necessary.
234 struct MeshPeerQueue *q;
237 * Was this a forced message? (Do not account for it)
242 * Continuation to call once sent.
247 * Closure for @c cont.
252 /******************************************************************************/
253 /******************************* GLOBALS ***********************************/
254 /******************************************************************************/
257 * Global handle to the statistics service.
259 extern struct GNUNET_STATISTICS_Handle *stats;
262 * Local peer own ID (memory efficient handle).
264 extern GNUNET_PEER_Id myid;
267 * Local peer own ID (full value).
269 extern struct GNUNET_PeerIdentity my_full_id;
272 * Connections known, indexed by cid (MeshConnection).
274 static struct GNUNET_CONTAINER_MultiHashMap *connections;
277 * How many connections are we willing to maintain.
278 * Local connections are always allowed, even if there are more connections than max.
280 static unsigned long long max_connections;
283 * How many messages *in total* are we willing to queue, divide by number of
284 * connections to get connection queue size.
286 static unsigned long long max_msgs_queue;
289 * How often to send path keepalives. Paths timeout after 4 missed.
291 static struct GNUNET_TIME_Relative refresh_connection_time;
294 * How often to send path create / ACKs.
296 static struct GNUNET_TIME_Relative create_connection_time;
299 /******************************************************************************/
300 /******************************** STATIC ***********************************/
301 /******************************************************************************/
303 #if 0 // avoid compiler warning for unused static function
305 fc_debug (struct MeshFlowControl *fc)
307 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
308 fc->last_pid_recv, fc->last_ack_sent);
309 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
310 fc->last_pid_sent, fc->last_ack_recv);
311 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
312 fc->queue_n, fc->queue_max);
316 connection_debug (struct MeshConnection *c)
320 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
324 peer2s (c->t->peer), GMC_2s (c));
325 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
326 c->state, c->pending_messages);
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
328 fc_debug (&c->fwd_fc);
329 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
330 fc_debug (&c->bck_fc);
336 * Schedule next keepalive task, taking in consideration
337 * the connection state and number of retries.
339 * @param c Connection for which to schedule the next keepalive.
340 * @param fwd Direction for the next keepalive.
343 schedule_next_keepalive (struct MeshConnection *c, int fwd);
347 * Get string description for tunnel state.
349 * @param s Tunnel state.
351 * @return String representation.
354 GMC_state2s (enum MeshConnectionState s)
358 case MESH_CONNECTION_NEW:
359 return "MESH_CONNECTION_NEW";
360 case MESH_CONNECTION_SENT:
361 return "MESH_CONNECTION_SENT";
362 case MESH_CONNECTION_ACK:
363 return "MESH_CONNECTION_ACK";
364 case MESH_CONNECTION_READY:
365 return "MESH_CONNECTION_READY";
366 case MESH_CONNECTION_DESTROYED:
367 return "MESH_CONNECTION_DESTROYED";
369 return "MESH_CONNECTION_STATE_ERROR";
375 * Initialize a Flow Control structure to the initial state.
377 * @param fc Flow Control structure to initialize.
380 fc_init (struct MeshFlowControl *fc)
383 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
384 fc->last_pid_recv = (uint32_t) -1;
385 fc->last_ack_sent = (uint32_t) 0;
386 fc->last_ack_recv = (uint32_t) 0;
387 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
388 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
390 fc->queue_max = (max_msgs_queue / max_connections) + 1;
397 * @param cid Connection ID.
399 static struct MeshConnection *
400 connection_get (const struct GNUNET_HashCode *cid)
402 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
407 connection_change_state (struct MeshConnection* c,
408 enum MeshConnectionState state)
410 LOG (GNUNET_ERROR_TYPE_DEBUG,
411 "Connection %s state was %s\n",
412 GMC_2s (c), GMC_state2s (c->state));
413 if (MESH_CONNECTION_DESTROYED == c->state)
415 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
418 LOG (GNUNET_ERROR_TYPE_DEBUG,
419 "Connection %s state is now %s\n",
420 GMC_2s (c), GMC_state2s (state));
422 if (MESH_CONNECTION_READY == state)
428 * Callback called when a queued ACK message is sent.
430 * @param cls Closure (FC).
431 * @param c Connection this message was on.
432 * @param q Queue handler this call invalidates.
433 * @param type Type of message sent.
434 * @param fwd Was this a FWD going message?
435 * @param size Size of the message.
439 struct MeshConnection *c,
440 struct MeshConnectionQueue *q,
441 uint16_t type, int fwd, size_t size)
443 struct MeshFlowControl *fc = cls;
450 * Send an ACK on the connection, informing the predecessor about
451 * the available buffer space. Should not be called in case the peer
452 * is origin (no predecessor) in the @c fwd direction.
454 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
455 * the ACK itself goes "back" (dest->root).
457 * @param c Connection on which to send the ACK.
458 * @param buffer How much space free to advertise?
459 * @param fwd Is this FWD ACK? (Going dest -> root)
460 * @param force Don't optimize out.
463 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
465 struct MeshFlowControl *next_fc;
466 struct MeshFlowControl *prev_fc;
467 struct GNUNET_MESH_ACK msg;
471 /* If origin, there is no connection to send ACKs. Wrong function! */
472 if (GMC_is_origin (c, fwd))
474 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
475 GMC_2s (c), GM_f2s (fwd));
480 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
481 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
483 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
484 GM_f2s (fwd), GMC_2s (c));
486 /* Check if we need to transmit the ACK. */
487 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
488 if (3 < delta && buffer < delta && GNUNET_NO == force)
490 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 " last pid recv: %u, last ack sent: %u\n",
493 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
497 /* Ok, ACK might be necessary, what PID to ACK? */
498 ack = prev_fc->last_pid_recv + buffer;
499 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
500 LOG (GNUNET_ERROR_TYPE_DEBUG,
501 " last pid %u, last ack %u, qmax %u, q %u\n",
502 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
503 next_fc->queue_max, next_fc->queue_n);
504 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
510 /* Check if message is already in queue */
511 if (NULL != prev_fc->ack_msg)
513 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
515 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
516 GMC_cancel (prev_fc->ack_msg);
517 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
521 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
526 prev_fc->last_ack_sent = ack;
528 /* Build ACK message and send on connection */
529 msg.header.size = htons (sizeof (msg));
530 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
531 msg.ack = htonl (ack);
534 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
541 * Callback called when a queued message is sent.
543 * Calculates the average time and connection packet tracking.
545 * @param cls Closure (ConnectionQueue Handle).
546 * @param c Connection this message was on.
547 * @param type Type of message sent.
548 * @param fwd Was this a FWD going message?
549 * @param size Size of the message.
550 * @param wait Time spent waiting for core (only the time for THIS message)
553 message_sent (void *cls,
554 struct MeshConnection *c, uint16_t type,
555 int fwd, size_t size,
556 struct GNUNET_TIME_Relative wait)
558 struct MeshConnectionPerformance *p;
559 struct MeshFlowControl *fc;
560 struct MeshConnectionQueue *q = cls;
564 fc = fwd ? &c->fwd_fc : &c->bck_fc;
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
576 q->cont (q->cont_cls, c, q, type, fwd, size);
580 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
582 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
589 c->pending_messages--;
590 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
592 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
596 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
599 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
600 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
601 c->maintenance_q = NULL;
602 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
603 if (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE == type || !fwd)
604 schedule_next_keepalive (c, fwd);
607 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
610 if (GNUNET_NO == forced)
613 LOG (GNUNET_ERROR_TYPE_DEBUG,
614 "! accounting pid %u\n",
619 LOG (GNUNET_ERROR_TYPE_DEBUG,
620 "! forced, Q_N not accounting pid %u\n",
623 GMC_send_ack (c, fwd, GNUNET_NO);
626 case GNUNET_MESSAGE_TYPE_MESH_POLL:
630 case GNUNET_MESSAGE_TYPE_MESH_ACK:
637 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
640 return; /* Only endpoints are interested in timing. */
643 usecsperbyte = ((double) wait.rel_value_us) / size;
644 if (p->size == AVG_MSGS)
646 /* Array is full. Substract oldest value, add new one and store. */
647 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
648 p->usecsperbyte[p->idx] = usecsperbyte;
649 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
653 /* Array not yet full. Add current value to avg and store. */
654 p->usecsperbyte[p->idx] = usecsperbyte;
656 p->avg += p->usecsperbyte[p->idx];
660 p->idx = (p->idx + 1) % AVG_MSGS;
665 * Get the previous hop in a connection
667 * @param c Connection.
669 * @return Previous peer in the connection.
671 static struct MeshPeer *
672 get_prev_hop (const struct MeshConnection *c)
676 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
677 GMC_2s (c), c->own_pos, c->path->length);
678 if (0 == c->own_pos || c->path->length < 2)
679 id = c->path->peers[0];
681 id = c->path->peers[c->own_pos - 1];
683 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
684 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
686 return GMP_get_short (id);
691 * Get the next hop in a connection
693 * @param c Connection.
695 * @return Next peer in the connection.
697 static struct MeshPeer *
698 get_next_hop (const struct MeshConnection *c)
702 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
703 GMC_2s (c), c->own_pos, c->path->length);
704 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
705 id = c->path->peers[c->path->length - 1];
707 id = c->path->peers[c->own_pos + 1];
709 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
710 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
712 return GMP_get_short (id);
717 * Get the hop in a connection.
719 * @param c Connection.
720 * @param fwd Next hop?
722 * @return Next peer in the connection.
724 static struct MeshPeer *
725 get_hop (struct MeshConnection *c, int fwd)
728 return get_next_hop (c);
729 return get_prev_hop (c);
734 * Is traffic coming from this sender 'FWD' traffic?
736 * @param c Connection to check.
737 * @param sender Peer identity of neighbor.
739 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
740 * the traffic is 'FWD'.
741 * #GNUNET_NO for BCK.
742 * #GNUNET_SYSERR for errors.
745 is_fwd (const struct MeshConnection *c,
746 const struct GNUNET_PeerIdentity *sender)
750 id = GNUNET_PEER_search (sender);
751 if (GMP_get_short_id (get_prev_hop (c)) == id)
754 if (GMP_get_short_id (get_next_hop (c)) == id)
758 return GNUNET_SYSERR;
763 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
764 * or a first CONNECTION_ACK directed to us.
766 * @param connection Connection to confirm.
767 * @param fwd Should we send it FWD? (root->dest)
768 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
771 send_connection_ack (struct MeshConnection *connection, int fwd)
773 struct MeshTunnel3 *t;
776 LOG (GNUNET_ERROR_TYPE_INFO, "=> {%18s ACK} on connection %s\n",
777 GM_f2s (!fwd), GMC_2s (connection));
778 GMP_queue_add (get_hop (connection, fwd), NULL,
779 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
780 sizeof (struct GNUNET_MESH_ConnectionACK),
781 connection, fwd, &message_sent, NULL);
782 connection->pending_messages++;
783 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
784 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
785 if (MESH_CONNECTION_READY != connection->state)
786 connection_change_state (connection, MESH_CONNECTION_SENT);
791 * Send a notification that a connection is broken.
793 * @param c Connection that is broken.
794 * @param id1 Peer that has disconnected.
795 * @param id2 Peer that has disconnected.
796 * @param fwd Direction towards which to send it.
799 send_broken (struct MeshConnection *c,
800 const struct GNUNET_PeerIdentity *id1,
801 const struct GNUNET_PeerIdentity *id2,
804 struct GNUNET_MESH_ConnectionBroken msg;
806 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
807 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
811 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
816 * Send a notification that a connection is broken, when a connection
817 * isn't even created.
819 * @param connection_id Connection ID.
820 * @param id1 Peer that has disconnected.
821 * @param id2 Peer that has disconnected.
822 * @param peer Peer to notify (neighbor who sent the connection).
825 send_broken2 (struct GNUNET_HashCode *connection_id,
826 const struct GNUNET_PeerIdentity *id1,
827 const struct GNUNET_PeerIdentity *id2,
828 GNUNET_PEER_Id peer_id)
830 struct GNUNET_MESH_ConnectionBroken *msg;
831 struct MeshPeer *neighbor;
833 LOG (GNUNET_ERROR_TYPE_INFO,
834 "Send BROKEN on unknown connection %s\n", GNUNET_h2s (connection_id));
836 msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
837 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
838 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
839 msg->cid = *connection_id;
842 neighbor = GMP_get_short (peer_id);
843 GMP_queue_add (neighbor, msg,
844 GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED,
845 sizeof (struct GNUNET_MESH_ConnectionBroken),
846 NULL, GNUNET_SYSERR, /* connection, fwd */
847 NULL, NULL); /* continuation */
852 * Send keepalive packets for a connection.
854 * @param c Connection to keep alive..
855 * @param fwd Is this a FWD keepalive? (owner -> dest).
858 send_connection_keepalive (struct MeshConnection *c, int fwd)
860 struct GNUNET_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 send_connection_keepalive (c, fwd);
933 * Keep the connection alive.
935 * @param c Connection to keep alive.
936 * @param fwd Direction.
937 * @param shutdown Are we shutting down? (Don't send traffic)
938 * Non-zero value for true, not necessarily GNUNET_YES.
941 connection_keepalive (struct MeshConnection *c, int fwd, int shutdown)
943 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
944 GM_f2s (fwd), GMC_2s (c));
947 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
949 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
951 if (GNUNET_NO != shutdown)
954 connection_maintain (c, fwd);
956 /* Next execution will be scheduled by message_sent */
961 * Keep the connection alive in the FWD direction.
963 * @param cls Closure (connection to keepalive).
964 * @param tc TaskContext.
967 connection_fwd_keepalive (void *cls,
968 const struct GNUNET_SCHEDULER_TaskContext *tc)
970 connection_keepalive ((struct MeshConnection *) cls,
972 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
977 * Keep the connection alive in the BCK direction.
979 * @param cls Closure (connection to keepalive).
980 * @param tc TaskContext.
983 connection_bck_keepalive (void *cls,
984 const struct GNUNET_SCHEDULER_TaskContext *tc)
986 connection_keepalive ((struct MeshConnection *) cls,
988 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
993 * Schedule next keepalive task, taking in consideration
994 * the connection state and number of retries.
996 * If the peer is not the origin, do nothing.
998 * @param c Connection for which to schedule the next keepalive.
999 * @param fwd Direction for the next keepalive.
1002 schedule_next_keepalive (struct MeshConnection *c, int fwd)
1004 struct GNUNET_TIME_Relative delay;
1005 GNUNET_SCHEDULER_TaskIdentifier *task_id;
1006 GNUNET_SCHEDULER_Task keepalive_task;
1008 if (GNUNET_NO == GMC_is_origin (c, fwd))
1011 /* Calculate delay to use, depending on the state of the connection */
1012 if (MESH_CONNECTION_READY == c->state)
1014 delay = refresh_connection_time;
1018 if (1 > c->create_retry)
1019 c->create_retry = 1;
1020 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1022 if (c->create_retry < 64)
1023 c->create_retry *= 2;
1026 /* Select direction-dependent parameters */
1027 if (GNUNET_YES == fwd)
1029 task_id = &c->fwd_maintenance_task;
1030 keepalive_task = &connection_fwd_keepalive;
1034 task_id = &c->bck_maintenance_task;
1035 keepalive_task = &connection_bck_keepalive;
1038 /* Check that no one scheduled it before us */
1039 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1041 /* No need for a _break. It can happen for instance when sending a SYNACK
1042 * for a duplicate SYN: the first SYNACK scheduled the task. */
1043 GNUNET_SCHEDULER_cancel (*task_id);
1046 /* Schedule the task */
1047 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1052 * @brief Re-initiate traffic on this connection if necessary.
1054 * Check if there is traffic queued towards this peer
1055 * and the core transmit handle is NULL (traffic was stalled).
1056 * If so, call core tmt rdy.
1058 * @param c Connection on which initiate traffic.
1059 * @param fwd Is this about fwd traffic?
1062 connection_unlock_queue (struct MeshConnection *c, int fwd)
1064 struct MeshPeer *peer;
1066 LOG (GNUNET_ERROR_TYPE_DEBUG,
1067 "connection_unlock_queue %s on %s\n",
1068 GM_f2s (fwd), GMC_2s (c));
1070 if (GMC_is_terminal (c, fwd))
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1076 peer = get_hop (c, fwd);
1077 GMP_queue_unlock (peer, c);
1082 * Cancel all transmissions that belong to a certain connection.
1084 * If the connection is scheduled for destruction and no more messages are left,
1085 * the connection will be destroyed by the continuation call.
1087 * @param c Connection which to cancel. Might be destroyed during this call.
1088 * @param fwd Cancel fwd traffic?
1091 connection_cancel_queues (struct MeshConnection *c, int fwd)
1093 struct MeshFlowControl *fc;
1094 struct MeshPeer *peer;
1096 LOG (GNUNET_ERROR_TYPE_DEBUG,
1097 " *** Cancel %s queues for connection %s\n",
1098 GM_f2s (fwd), GMC_2s (c));
1105 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1106 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1108 GNUNET_SCHEDULER_cancel (fc->poll_task);
1109 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1110 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1112 peer = get_hop (c, fwd);
1113 GMP_queue_cancel (peer, c);
1118 * Function called if a connection has been stalled for a while,
1119 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1121 * @param cls Closure (poll ctx).
1122 * @param tc TaskContext.
1125 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1129 * Callback called when a queued POLL message is sent.
1131 * @param cls Closure (FC).
1132 * @param c Connection this message was on.
1133 * @param q Queue handler this call invalidates.
1134 * @param type Type of message sent.
1135 * @param fwd Was this a FWD going message?
1136 * @param size Size of the message.
1139 poll_sent (void *cls,
1140 struct MeshConnection *c,
1141 struct MeshConnectionQueue *q,
1142 uint16_t type, int fwd, size_t size)
1144 struct MeshFlowControl *fc = cls;
1146 if (2 == c->destroy)
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 " *** POLL sent for , scheduling new one!\n");
1153 fc->poll_msg = NULL;
1154 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1155 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1156 &connection_poll, fc);
1157 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1162 * Function called if a connection has been stalled for a while,
1163 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1165 * @param cls Closure (poll ctx).
1166 * @param tc TaskContext.
1169 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1171 struct MeshFlowControl *fc = cls;
1172 struct GNUNET_MESH_Poll msg;
1173 struct MeshConnection *c;
1175 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1176 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1183 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1184 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1185 fc == &c->fwd_fc ? "FWD" : "BCK");
1187 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1188 msg.header.size = htons (sizeof (msg));
1189 msg.pid = htonl (fc->last_pid_sent);
1190 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1191 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1192 fc == &c->fwd_fc, GNUNET_YES,
1198 * Timeout function due to lack of keepalive/traffic from the owner.
1199 * Destroys connection if called.
1201 * @param cls Closure (connection to destroy).
1202 * @param tc TaskContext.
1205 connection_fwd_timeout (void *cls,
1206 const struct GNUNET_SCHEDULER_TaskContext *tc)
1208 struct MeshConnection *c = cls;
1210 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1211 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1214 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1216 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1227 * Timeout function due to lack of keepalive/traffic from the destination.
1228 * Destroys connection if called.
1230 * @param cls Closure (connection to destroy).
1231 * @param tc TaskContext
1234 connection_bck_timeout (void *cls,
1235 const struct GNUNET_SCHEDULER_TaskContext *tc)
1237 struct MeshConnection *c = cls;
1239 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1240 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1246 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1257 * Resets the connection timeout task, some other message has done the
1259 * - For the first peer on the direction this means to send
1260 * a keepalive or a path confirmation message (either create or ACK).
1261 * - For all other peers, this means to destroy the connection,
1262 * due to lack of activity.
1263 * Starts the timeout if no timeout was running (connection just created).
1265 * @param c Connection whose timeout to reset.
1266 * @param fwd Is this forward?
1268 * TODO use heap to improve efficiency of scheduler.
1271 connection_reset_timeout (struct MeshConnection *c, int fwd)
1273 GNUNET_SCHEDULER_TaskIdentifier *ti;
1274 GNUNET_SCHEDULER_Task f;
1276 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1278 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1280 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1281 GNUNET_SCHEDULER_cancel (*ti);
1283 if (GMC_is_origin (c, fwd)) /* Startpoint */
1285 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1286 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1288 else /* Relay, endpoint. */
1290 struct GNUNET_TIME_Relative delay;
1292 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1293 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1294 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1300 * Add the connection to the list of both neighbors.
1302 * @param c Connection.
1304 * @return #GNUNET_OK if everything went fine
1305 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1308 register_neighbors (struct MeshConnection *c)
1310 struct MeshPeer *next_peer;
1311 struct MeshPeer *prev_peer;
1313 next_peer = get_next_hop (c);
1314 prev_peer = get_prev_hop (c);
1316 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1318 path_debug (c->path);
1319 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1320 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1321 GMC_2s (c), next_peer);
1322 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GMP_2s (next_peer));
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1324 GMC_2s (c), prev_peer);
1325 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GMP_2s (prev_peer));
1327 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1328 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1330 if (GMC_is_origin (c, GNUNET_YES))
1331 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1332 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1334 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1336 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1338 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1339 return GNUNET_SYSERR;
1342 GMP_add_connection (next_peer, c);
1343 GMP_add_connection (prev_peer, c);
1350 * Remove the connection from the list of both neighbors.
1352 * @param c Connection.
1355 unregister_neighbors (struct MeshConnection *c)
1357 struct MeshPeer *peer;
1359 peer = get_next_hop (c);
1360 if (GNUNET_OK != GMP_remove_connection (peer, c))
1362 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1363 || MESH_CONNECTION_DESTROYED == c->state);
1364 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1365 if (NULL != c->t) GMT_debug (c->t);
1368 peer = get_prev_hop (c);
1369 if (GNUNET_OK != GMP_remove_connection (peer, c))
1371 GNUNET_assert (MESH_CONNECTION_NEW == c->state
1372 || MESH_CONNECTION_DESTROYED == c->state);
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1374 if (NULL != c->t) GMT_debug (c->t);
1380 * Bind the connection to the peer and the tunnel to that peer.
1382 * If the peer has no tunnel, create one. Update tunnel and connection
1383 * data structres to reflect new status.
1385 * @param c Connection.
1389 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1391 GMP_add_tunnel (peer);
1392 c->t = GMP_get_tunnel (peer);
1393 GMT_add_connection (c->t, c);
1398 * Builds a path from a PeerIdentity array.
1400 * @param peers PeerIdentity array.
1401 * @param size Size of the @c peers array.
1402 * @param own_pos Output parameter: own position in the path.
1404 * @return Fixed and shortened path.
1406 static struct MeshPeerPath *
1407 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1409 unsigned int *own_pos)
1411 struct MeshPeerPath *path;
1412 GNUNET_PEER_Id shortid;
1415 unsigned int offset;
1418 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1419 path = path_new (size);
1422 for (i = 0; i < size; i++)
1424 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1425 i, GNUNET_i2s (&peers[i]));
1426 shortid = GNUNET_PEER_intern (&peers[i]);
1428 /* Check for loops / duplicates */
1429 for (j = 0; j < i - offset; j++)
1431 if (path->peers[j] == shortid)
1433 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset);
1436 GNUNET_PEER_change_rc (shortid, -1);
1439 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1440 path->peers[i - offset] = shortid;
1441 if (path->peers[i] == myid)
1444 path->length -= offset;
1446 if (path->peers[*own_pos] != myid)
1448 /* create path: self not found in path through self */
1449 GNUNET_break_op (0);
1450 path_destroy (path);
1459 * Log receipt of message on stderr (INFO level).
1461 * @param message Message received.
1462 * @param peer Peer who sent the message.
1463 * @param hash Connection ID.
1466 log_message (const struct GNUNET_MessageHeader *message,
1467 const struct GNUNET_PeerIdentity *peer,
1468 const struct GNUNET_HashCode *hash)
1470 LOG (GNUNET_ERROR_TYPE_INFO, "<- %s on connection %s from %s\n",
1471 GM_m2s (ntohs (message->type)), GNUNET_h2s (hash), GNUNET_i2s (peer));
1474 /******************************************************************************/
1475 /******************************** API ***********************************/
1476 /******************************************************************************/
1479 * Core handler for connection creation.
1481 * @param cls Closure (unused).
1482 * @param peer Sender (neighbor).
1483 * @param message Message.
1485 * @return GNUNET_OK to keep the connection open,
1486 * GNUNET_SYSERR to close it (signal serious error)
1489 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1490 const struct GNUNET_MessageHeader *message)
1492 struct GNUNET_MESH_ConnectionCreate *msg;
1493 struct GNUNET_PeerIdentity *id;
1494 struct GNUNET_HashCode *cid;
1495 struct MeshPeerPath *path;
1496 struct MeshPeer *dest_peer;
1497 struct MeshPeer *orig_peer;
1498 struct MeshConnection *c;
1499 unsigned int own_pos;
1503 size = ntohs (message->size);
1504 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1506 GNUNET_break_op (0);
1510 /* Calculate hops */
1511 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1512 if (size % sizeof (struct GNUNET_PeerIdentity))
1514 GNUNET_break_op (0);
1517 size /= sizeof (struct GNUNET_PeerIdentity);
1520 GNUNET_break_op (0);
1523 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1525 /* Get parameters */
1526 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1528 log_message (message, peer, cid);
1529 id = (struct GNUNET_PeerIdentity *) &msg[1];
1530 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1532 /* Create connection */
1533 c = connection_get (cid);
1536 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1542 GNUNET_break_op (0);
1543 path_destroy (path);
1546 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1547 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1548 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1551 if (path->length - 1 == own_pos)
1553 /* If we are destination, why did the creation fail? */
1557 send_broken2 (cid, &my_full_id,
1558 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1559 path->peers[own_pos - 1]);
1560 path_destroy (path);
1563 GMP_add_path_to_all (path, GNUNET_NO);
1564 connection_reset_timeout (c, GNUNET_YES);
1568 path = path_duplicate (c->path);
1570 if (MESH_CONNECTION_NEW == c->state)
1571 connection_change_state (c, MESH_CONNECTION_SENT);
1573 /* Remember peers */
1574 dest_peer = GMP_get (&id[size - 1]);
1575 orig_peer = GMP_get (&id[0]);
1577 /* Is it a connection to us? */
1578 if (c->own_pos == size - 1)
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1581 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1583 add_to_peer (c, orig_peer);
1584 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1585 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1587 send_connection_ack (c, GNUNET_NO);
1588 if (MESH_CONNECTION_SENT == c->state)
1589 connection_change_state (c, MESH_CONNECTION_ACK);
1593 /* It's for somebody else! Retransmit. */
1594 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1595 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1596 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1597 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1600 path_destroy (path);
1606 * Core handler for path confirmations.
1608 * @param cls closure
1609 * @param message message
1610 * @param peer peer identity this notification is about
1612 * @return GNUNET_OK to keep the connection open,
1613 * GNUNET_SYSERR to close it (signal serious error)
1616 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1617 const struct GNUNET_MessageHeader *message)
1619 struct GNUNET_MESH_ConnectionACK *msg;
1620 struct MeshConnection *c;
1621 struct MeshPeerPath *p;
1622 struct MeshPeer *pi;
1623 enum MeshConnectionState oldstate;
1626 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1627 log_message (message, peer, &msg->cid);
1628 c = connection_get (&msg->cid);
1631 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1633 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1637 if (GNUNET_NO != c->destroy)
1639 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1643 oldstate = c->state;
1644 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1645 pi = GMP_get (peer);
1646 if (get_next_hop (c) == pi)
1648 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1650 if (MESH_CONNECTION_SENT == oldstate)
1651 connection_change_state (c, MESH_CONNECTION_ACK);
1653 else if (get_prev_hop (c) == pi)
1655 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1657 connection_change_state (c, MESH_CONNECTION_READY);
1661 GNUNET_break_op (0);
1665 connection_reset_timeout (c, fwd);
1667 /* Add path to peers? */
1671 GMP_add_path_to_all (p, GNUNET_YES);
1678 /* Message for us as creator? */
1679 if (GMC_is_origin (c, GNUNET_YES))
1681 if (GNUNET_NO != fwd)
1683 GNUNET_break_op (0);
1686 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1688 /* If just created, cancel the short timeout and start a long one */
1689 if (MESH_CONNECTION_SENT == oldstate)
1690 connection_reset_timeout (c, GNUNET_YES);
1692 /* Change connection state */
1693 connection_change_state (c, MESH_CONNECTION_READY);
1694 send_connection_ack (c, GNUNET_YES);
1696 /* Change tunnel state, trigger KX */
1697 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1698 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1703 /* Message for us as destination? */
1704 if (GMC_is_terminal (c, GNUNET_YES))
1706 if (GNUNET_YES != fwd)
1708 GNUNET_break_op (0);
1711 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1713 /* If just created, cancel the short timeout and start a long one */
1714 if (MESH_CONNECTION_ACK == oldstate)
1715 connection_reset_timeout (c, GNUNET_NO);
1717 /* Change tunnel state */
1718 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1719 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1724 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1725 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1731 * Core handler for notifications of broken paths
1733 * @param cls Closure (unused).
1734 * @param id Peer identity of sending neighbor.
1735 * @param message Message.
1737 * @return GNUNET_OK to keep the connection open,
1738 * GNUNET_SYSERR to close it (signal serious error)
1741 GMC_handle_broken (void* cls,
1742 const struct GNUNET_PeerIdentity* id,
1743 const struct GNUNET_MessageHeader* message)
1745 struct GNUNET_MESH_ConnectionBroken *msg;
1746 struct MeshConnection *c;
1749 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1750 log_message (message, id, &msg->cid);
1751 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1752 GNUNET_i2s (&msg->peer1));
1753 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1754 GNUNET_i2s (&msg->peer2));
1755 c = connection_get (&msg->cid);
1758 GNUNET_break_op (0);
1762 fwd = is_fwd (c, id);
1763 if (GMC_is_terminal (c, fwd))
1765 path_invalidate (c->path);
1766 if (0 < c->pending_messages)
1767 c->destroy = GNUNET_YES;
1773 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1774 c->destroy = GNUNET_YES;
1775 connection_cancel_queues (c, !fwd);
1784 * Core handler for tunnel destruction
1786 * @param cls Closure (unused).
1787 * @param peer Peer identity of sending neighbor.
1788 * @param message Message.
1790 * @return GNUNET_OK to keep the connection open,
1791 * GNUNET_SYSERR to close it (signal serious error)
1794 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1795 const struct GNUNET_MessageHeader *message)
1797 struct GNUNET_MESH_ConnectionDestroy *msg;
1798 struct MeshConnection *c;
1801 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1802 log_message (message, peer, &msg->cid);
1803 c = connection_get (&msg->cid);
1806 /* Probably already got the message from another path,
1807 * destroyed the tunnel and retransmitted to children.
1810 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1812 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1815 fwd = is_fwd (c, peer);
1816 if (GNUNET_SYSERR == fwd)
1818 GNUNET_break_op (0); /* FIXME */
1821 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1822 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1823 else if (0 == c->pending_messages)
1825 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1829 c->destroy = GNUNET_YES;
1830 c->state = MESH_CONNECTION_DESTROYED;
1833 GMT_remove_connection (c->t, c);
1841 * Generic handler for mesh network encrypted traffic.
1843 * @param peer Peer identity this notification is about.
1844 * @param msg Encrypted message.
1846 * @return GNUNET_OK to keep the connection open,
1847 * GNUNET_SYSERR to close it (signal serious error)
1850 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1851 const struct GNUNET_MESH_Encrypted *msg)
1853 struct MeshConnection *c;
1854 struct MeshPeer *neighbor;
1855 struct MeshFlowControl *fc;
1856 GNUNET_PEER_Id peer_id;
1862 log_message (&msg->header, peer, &msg->cid);
1865 size = ntohs (msg->header.size);
1867 sizeof (struct GNUNET_MESH_Encrypted) +
1868 sizeof (struct GNUNET_MessageHeader))
1870 GNUNET_break_op (0);
1874 /* Check connection */
1875 c = connection_get (&msg->cid);
1878 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1879 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
1880 GNUNET_h2s (&msg->cid));
1884 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s\n", GMC_2s (c));
1886 /* Check if origin is as expected */
1887 neighbor = get_prev_hop (c);
1888 peer_id = GNUNET_PEER_search (peer);
1889 if (peer_id == GMP_get_short_id (neighbor))
1895 neighbor = get_next_hop (c);
1896 if (peer_id == GMP_get_short_id (neighbor))
1902 /* Unexpected peer sending traffic on a connection. */
1903 GNUNET_break_op (0);
1909 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1910 pid = ntohl (msg->pid);
1911 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1913 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1914 LOG (GNUNET_ERROR_TYPE_DEBUG,
1915 "WARNING Received PID %u, (prev %u), ACK %u\n",
1916 pid, fc->last_pid_recv, fc->last_ack_sent);
1919 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1921 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1922 LOG (GNUNET_ERROR_TYPE_DEBUG,
1923 " Pid %u not expected (%u+), dropping!\n",
1924 pid, fc->last_pid_recv + 1);
1927 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1928 connection_change_state (c, MESH_CONNECTION_READY);
1929 connection_reset_timeout (c, fwd);
1930 fc->last_pid_recv = pid;
1932 /* Is this message for us? */
1933 if (GMC_is_terminal (c, fwd))
1935 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1936 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1940 GNUNET_break (GNUNET_NO != c->destroy);
1943 fc->last_pid_recv = pid;
1944 GMT_handle_encrypted (c->t, msg);
1945 GMC_send_ack (c, fwd, GNUNET_NO);
1949 /* Message not for us: forward to next hop */
1950 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1951 ttl = ntohl (msg->ttl);
1952 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1955 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1956 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1957 GMC_send_ack (c, fwd, GNUNET_NO);
1961 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1962 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1968 * Generic handler for mesh network encrypted traffic.
1970 * @param peer Peer identity this notification is about.
1971 * @param msg Encrypted message.
1973 * @return GNUNET_OK to keep the connection open,
1974 * GNUNET_SYSERR to close it (signal serious error)
1977 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1978 const struct GNUNET_MESH_KX *msg)
1980 struct MeshConnection *c;
1981 struct MeshPeer *neighbor;
1982 GNUNET_PEER_Id peer_id;
1986 log_message (&msg->header, peer, &msg->cid);
1989 size = ntohs (msg->header.size);
1991 sizeof (struct GNUNET_MESH_Encrypted) +
1992 sizeof (struct GNUNET_MessageHeader))
1994 GNUNET_break_op (0);
1998 /* Check connection */
1999 c = connection_get (&msg->cid);
2002 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2003 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING kx on unknown connection %s\n",
2004 GNUNET_h2s (&msg->cid));
2007 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
2009 /* Check if origin is as expected */
2010 neighbor = get_prev_hop (c);
2011 peer_id = GNUNET_PEER_search (peer);
2012 if (peer_id == GMP_get_short_id (neighbor))
2018 neighbor = get_next_hop (c);
2019 if (peer_id == GMP_get_short_id (neighbor))
2025 /* Unexpected peer sending traffic on a connection. */
2026 GNUNET_break_op (0);
2031 /* Count as connection confirmation. */
2032 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
2034 connection_change_state (c, MESH_CONNECTION_READY);
2037 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
2038 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
2041 connection_reset_timeout (c, fwd);
2043 /* Is this message for us? */
2044 if (GMC_is_terminal (c, fwd))
2046 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2047 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2053 GMT_handle_kx (c->t, &msg[1].header);
2057 /* Message not for us: forward to next hop */
2058 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2059 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2060 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
2067 * Core handler for encrypted mesh network traffic (channel mgmt, data).
2069 * @param cls Closure (unused).
2070 * @param message Message received.
2071 * @param peer Peer who sent the message.
2073 * @return GNUNET_OK to keep the connection open,
2074 * GNUNET_SYSERR to close it (signal serious error)
2077 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2078 const struct GNUNET_MessageHeader *message)
2080 return handle_mesh_encrypted (peer,
2081 (struct GNUNET_MESH_Encrypted *)message);
2086 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2088 * @param cls Closure (unused).
2089 * @param message Message received.
2090 * @param peer Peer who sent the message.
2092 * @return GNUNET_OK to keep the connection open,
2093 * GNUNET_SYSERR to close it (signal serious error)
2096 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2097 const struct GNUNET_MessageHeader *message)
2099 return handle_mesh_kx (peer,
2100 (struct GNUNET_MESH_KX *) message);
2105 * Core handler for mesh network traffic point-to-point acks.
2107 * @param cls closure
2108 * @param message message
2109 * @param peer peer identity this notification is about
2111 * @return GNUNET_OK to keep the connection open,
2112 * GNUNET_SYSERR to close it (signal serious error)
2115 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2116 const struct GNUNET_MessageHeader *message)
2118 struct GNUNET_MESH_ACK *msg;
2119 struct MeshConnection *c;
2120 struct MeshFlowControl *fc;
2125 msg = (struct GNUNET_MESH_ACK *) message;
2126 log_message (message, peer, &msg->cid);
2127 c = connection_get (&msg->cid);
2130 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2135 /* Is this a forward or backward ACK? */
2136 id = GNUNET_PEER_search (peer);
2137 if (GMP_get_short_id (get_next_hop (c)) == id)
2139 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2143 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2145 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2151 GNUNET_break_op (0);
2155 ack = ntohl (msg->ack);
2156 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2157 ack, fc->last_ack_recv);
2158 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
2159 fc->last_ack_recv = ack;
2161 /* Cancel polling if the ACK is big enough. */
2162 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2163 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2165 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2166 GNUNET_SCHEDULER_cancel (fc->poll_task);
2167 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2168 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2171 connection_unlock_queue (c, fwd);
2178 * Core handler for mesh network traffic point-to-point ack polls.
2180 * @param cls closure
2181 * @param message message
2182 * @param peer peer identity this notification is about
2184 * @return GNUNET_OK to keep the connection open,
2185 * GNUNET_SYSERR to close it (signal serious error)
2188 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2189 const struct GNUNET_MessageHeader *message)
2191 struct GNUNET_MESH_Poll *msg;
2192 struct MeshConnection *c;
2193 struct MeshFlowControl *fc;
2198 msg = (struct GNUNET_MESH_Poll *) message;
2199 log_message (message, peer, &msg->cid);
2200 c = connection_get (&msg->cid);
2203 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2205 LOG (GNUNET_ERROR_TYPE_DEBUG,
2206 "WARNING POLL message on unknown connection %s!\n",
2207 GNUNET_h2s (&msg->cid));
2211 /* Is this a forward or backward ACK?
2212 * Note: a poll should never be needed in a loopback case,
2213 * since there is no possiblility of packet loss there, so
2214 * this way of discerining FWD/BCK should not be a problem.
2216 id = GNUNET_PEER_search (peer);
2217 if (GMP_get_short_id (get_next_hop (c)) == id)
2219 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2222 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2224 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2229 GNUNET_break_op (0);
2233 pid = ntohl (msg->pid);
2234 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2235 fc->last_pid_recv = pid;
2236 fwd = fc == &c->bck_fc;
2237 GMC_send_ack (c, fwd, GNUNET_YES);
2244 * Core handler for mesh keepalives.
2246 * @param cls closure
2247 * @param message message
2248 * @param peer peer identity this notification is about
2249 * @return GNUNET_OK to keep the connection open,
2250 * GNUNET_SYSERR to close it (signal serious error)
2252 * TODO: Check who we got this from, to validate route.
2255 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2256 const struct GNUNET_MessageHeader *message)
2258 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2259 struct MeshConnection *c;
2260 struct MeshPeer *neighbor;
2261 GNUNET_PEER_Id peer_id;
2264 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2265 log_message (message, peer, &msg->cid);
2266 c = connection_get (&msg->cid);
2269 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2274 /* Check if origin is as expected TODO refactor and reuse */
2275 peer_id = GNUNET_PEER_search (peer);
2276 neighbor = get_prev_hop (c);
2277 if (peer_id == GMP_get_short_id (neighbor))
2283 neighbor = get_next_hop (c);
2284 if (peer_id == GMP_get_short_id (neighbor))
2290 GNUNET_break_op (0);
2295 connection_change_state (c, MESH_CONNECTION_READY);
2296 connection_reset_timeout (c, fwd);
2298 if (GMC_is_terminal (c, fwd))
2301 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2302 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2309 * Send an ACK on the appropriate connection/channel, depending on
2310 * the direction and the position of the peer.
2312 * @param c Which connection to send the hop-by-hop ACK.
2313 * @param fwd Is this a fwd ACK? (will go dest->root).
2314 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2317 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2319 unsigned int buffer;
2321 LOG (GNUNET_ERROR_TYPE_DEBUG,
2322 "GMC send %s ACK on %s\n",
2323 GM_f2s (fwd), GMC_2s (c));
2331 if (GNUNET_NO != c->destroy)
2333 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2337 /* Get available buffer space */
2338 if (GMC_is_terminal (c, fwd))
2340 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2341 buffer = GMT_get_channels_buffer (c->t);
2345 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2346 buffer = GMC_get_buffer (c, fwd);
2348 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2349 if (0 == buffer && GNUNET_NO == force)
2352 /* Send available buffer space */
2353 if (GMC_is_origin (c, fwd))
2355 GNUNET_assert (NULL != c->t);
2356 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2357 GMT_unchoke_channels (c->t);
2361 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2362 send_ack (c, buffer, fwd, force);
2368 * Initialize the connections subsystem
2370 * @param c Configuration handle.
2373 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2375 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2377 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2380 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2381 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2382 GNUNET_SCHEDULER_shutdown ();
2387 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2390 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2391 "MESH", "MAX_CONNECTIONS", "MISSING");
2392 GNUNET_SCHEDULER_shutdown ();
2397 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2398 &refresh_connection_time))
2400 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2401 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2402 GNUNET_SCHEDULER_shutdown ();
2405 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2406 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2411 * Destroy each connection on shutdown.
2413 * @param cls Closure (unused).
2414 * @param key Current key code (CID, unused).
2415 * @param value Value in the hash map (connection)
2417 * @return #GNUNET_YES, because we should continue to iterate,
2420 shutdown_iterator (void *cls,
2421 const struct GNUNET_HashCode *key,
2424 struct MeshConnection *c = value;
2432 * Shut down the connections subsystem.
2437 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2438 GNUNET_CONTAINER_multihashmap_destroy (connections);
2443 struct MeshConnection *
2444 GMC_new (const struct GNUNET_HashCode *cid,
2445 struct MeshTunnel3 *t,
2446 struct MeshPeerPath *p,
2447 unsigned int own_pos)
2449 struct MeshConnection *c;
2451 c = GNUNET_new (struct MeshConnection);
2453 GNUNET_assert (GNUNET_OK ==
2454 GNUNET_CONTAINER_multihashmap_put (connections,
2456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2457 fc_init (&c->fwd_fc);
2458 fc_init (&c->bck_fc);
2463 GNUNET_assert (own_pos <= p->length - 1);
2464 c->own_pos = own_pos;
2467 if (GNUNET_OK != register_neighbors (c))
2471 path_invalidate (c->path);
2484 GMC_destroy (struct MeshConnection *c)
2492 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2493 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2496 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2497 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2498 &c->fwd_fc, &c->bck_fc);
2499 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2500 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2502 /* Cancel all traffic */
2503 if (NULL != c->path)
2505 connection_cancel_queues (c, GNUNET_YES);
2506 connection_cancel_queues (c, GNUNET_NO);
2507 unregister_neighbors (c);
2510 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2511 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2513 /* Cancel maintainance task (keepalive/timeout) */
2514 if (NULL != c->fwd_fc.poll_msg)
2516 GMC_cancel (c->fwd_fc.poll_msg);
2517 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2519 if (NULL != c->bck_fc.poll_msg)
2521 GMC_cancel (c->bck_fc.poll_msg);
2522 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2525 /* Delete from tunnel */
2527 GMT_remove_connection (c->t, c);
2529 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2530 path_destroy (c->path);
2531 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2532 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2533 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2534 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2535 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2537 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2538 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2540 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2542 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2543 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2546 GNUNET_break (GNUNET_YES ==
2547 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2549 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2554 * Get the connection ID.
2556 * @param c Connection to get the ID from.
2558 * @return ID of the connection.
2560 const struct GNUNET_HashCode *
2561 GMC_get_id (const struct MeshConnection *c)
2568 * Get the connection path.
2570 * @param c Connection to get the path from.
2572 * @return path used by the connection.
2574 const struct MeshPeerPath *
2575 GMC_get_path (const struct MeshConnection *c)
2577 if (GNUNET_NO == c->destroy)
2584 * Get the connection state.
2586 * @param c Connection to get the state from.
2588 * @return state of the connection.
2590 enum MeshConnectionState
2591 GMC_get_state (const struct MeshConnection *c)
2597 * Get the connection tunnel.
2599 * @param c Connection to get the tunnel from.
2601 * @return tunnel of the connection.
2603 struct MeshTunnel3 *
2604 GMC_get_tunnel (const struct MeshConnection *c)
2611 * Get free buffer space in a connection.
2613 * @param c Connection.
2614 * @param fwd Is query about FWD traffic?
2616 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2619 GMC_get_buffer (struct MeshConnection *c, int fwd)
2621 struct MeshFlowControl *fc;
2623 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2625 return (fc->queue_max - fc->queue_n);
2629 * Get how many messages have we allowed to send to us from a direction.
2631 * @param c Connection.
2632 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2634 * @return last_ack_sent - last_pid_recv
2637 GMC_get_allowed (struct MeshConnection *c, int fwd)
2639 struct MeshFlowControl *fc;
2641 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2642 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2646 return (fc->last_ack_sent - fc->last_pid_recv);
2650 * Get messages queued in a connection.
2652 * @param c Connection.
2653 * @param fwd Is query about FWD traffic?
2655 * @return Number of messages queued.
2658 GMC_get_qn (struct MeshConnection *c, int fwd)
2660 struct MeshFlowControl *fc;
2662 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2669 * Allow the connection to advertise a buffer of the given size.
2671 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2672 * allowing up to last_pid_recv + buffer.
2674 * @param c Connection.
2675 * @param buffer How many more messages the connection can accept.
2676 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2679 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2681 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2682 GMC_2s (c), buffer, GM_f2s (fwd));
2683 send_ack (c, buffer, fwd, GNUNET_NO);
2688 * Notify other peers on a connection of a broken link. Mark connections
2689 * to destroy after all traffic has been sent.
2691 * @param c Connection on which there has been a disconnection.
2692 * @param peer Peer that disconnected.
2695 GMC_notify_broken (struct MeshConnection *c,
2696 struct MeshPeer *peer)
2700 LOG (GNUNET_ERROR_TYPE_DEBUG,
2701 " notify broken on %s due to %s disconnect\n",
2702 GMC_2s (c), GMP_2s (peer));
2704 fwd = peer == get_prev_hop (c);
2706 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2708 /* Local shutdown, no one to notify about this. */
2712 if (GNUNET_NO == c->destroy)
2713 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2715 /* Connection will have at least one pending message
2716 * (the one we just scheduled), so no point in checking whether to
2717 * destroy immediately. */
2718 c->destroy = GNUNET_YES;
2719 c->state = MESH_CONNECTION_DESTROYED;
2722 * Cancel all queues, if no message is left, connection will be destroyed.
2724 connection_cancel_queues (c, !fwd);
2731 * Is this peer the first one on the connection?
2733 * @param c Connection.
2734 * @param fwd Is this about fwd traffic?
2736 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2739 GMC_is_origin (struct MeshConnection *c, int fwd)
2741 if (!fwd && c->path->length - 1 == c->own_pos )
2743 if (fwd && 0 == c->own_pos)
2750 * Is this peer the last one on the connection?
2752 * @param c Connection.
2753 * @param fwd Is this about fwd traffic?
2754 * Note that the ROOT is the terminal for BCK traffic!
2756 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2759 GMC_is_terminal (struct MeshConnection *c, int fwd)
2761 return GMC_is_origin (c, !fwd);
2766 * See if we are allowed to send by the next hop in the given direction.
2768 * @param c Connection.
2769 * @param fwd Is this about fwd traffic?
2771 * @return #GNUNET_YES in case it's OK to send.
2774 GMC_is_sendable (struct MeshConnection *c, int fwd)
2776 struct MeshFlowControl *fc;
2783 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2784 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2790 * Sends an already built message on a connection, properly registering
2791 * all used resources.
2793 * @param message Message to send. Function makes a copy of it.
2794 * If message is not hop-by-hop, decrements TTL of copy.
2795 * @param c Connection on which this message is transmitted.
2796 * @param fwd Is this a fwd message?
2797 * @param force Force the connection to accept the message (buffer overfill).
2798 * @param cont Continuation called once message is sent. Can be NULL.
2799 * @param cont_cls Closure for @c cont.
2801 * @return Handle to cancel the message before it's sent.
2802 * NULL on error or if @c cont is NULL.
2803 * Invalid on @c cont call.
2805 struct MeshConnectionQueue *
2806 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2807 struct MeshConnection *c, int fwd, int force,
2808 GMC_sent cont, void *cont_cls)
2810 struct MeshFlowControl *fc;
2811 struct MeshConnectionQueue *q;
2817 size = ntohs (message->size);
2818 data = GNUNET_malloc (size);
2819 memcpy (data, message, size);
2820 type = ntohs (message->type);
2821 LOG (GNUNET_ERROR_TYPE_INFO, "-> %s on connection %s (%u bytes)\n",
2822 GM_m2s (type), GMC_2s (c), size);
2824 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2825 droppable = GNUNET_NO == force;
2828 struct GNUNET_MESH_Encrypted *emsg;
2829 struct GNUNET_MESH_KX *kmsg;
2830 struct GNUNET_MESH_ACK *amsg;
2831 struct GNUNET_MESH_Poll *pmsg;
2832 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2833 struct GNUNET_MESH_ConnectionBroken *bmsg;
2836 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2837 emsg = (struct GNUNET_MESH_Encrypted *) data;
2838 ttl = ntohl (emsg->ttl);
2841 GNUNET_break_op (0);
2846 emsg->ttl = htonl (ttl - 1);
2847 emsg->pid = htonl (fc->next_pid++);
2848 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2849 if (GNUNET_YES == droppable)
2852 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2853 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2854 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2858 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2860 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2862 GMC_start_poll (c, fwd);
2866 case GNUNET_MESSAGE_TYPE_MESH_KX:
2867 kmsg = (struct GNUNET_MESH_KX *) data;
2871 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2872 amsg = (struct GNUNET_MESH_ACK *) data;
2874 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2875 droppable = GNUNET_NO;
2878 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2879 pmsg = (struct GNUNET_MESH_Poll *) data;
2881 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2882 droppable = GNUNET_NO;
2885 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2886 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2891 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2892 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2897 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2898 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2899 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2908 if (fc->queue_n > fc->queue_max && droppable)
2910 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2913 LOG (GNUNET_ERROR_TYPE_DEBUG,
2914 "queue full: %u/%u\n",
2915 fc->queue_n, fc->queue_max);
2916 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2922 return NULL; /* Drop this message */
2925 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2926 c->pending_messages++;
2928 q = GNUNET_new (struct MeshConnectionQueue);
2929 q->forced = !droppable;
2930 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2934 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2940 q->cont_cls = cont_cls;
2946 * Cancel a previously sent message while it's in the queue.
2948 * ONLY can be called before the continuation given to the send function
2949 * is called. Once the continuation is called, the message is no longer in the
2952 * @param q Handle to the queue.
2955 GMC_cancel (struct MeshConnectionQueue *q)
2957 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2959 /* queue destroy calls message_sent, which calls q->cont and frees q */
2960 GMP_queue_destroy (q->q, GNUNET_YES);
2965 * Sends a CREATE CONNECTION message for a path to a peer.
2966 * Changes the connection and tunnel states if necessary.
2968 * @param connection Connection to create.
2971 GMC_send_create (struct MeshConnection *connection)
2973 enum MeshTunnel3CState state;
2976 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2977 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2979 LOG (GNUNET_ERROR_TYPE_INFO, "=> %s on connection %s (%u bytes)\n",
2980 GM_m2s (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE),
2981 GMC_2s (connection), size);
2982 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2983 connection, connection->pending_messages);
2984 connection->pending_messages++;
2986 connection->maintenance_q =
2987 GMP_queue_add (get_next_hop (connection), NULL,
2988 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2989 size, connection, GNUNET_YES, &message_sent, NULL);
2991 state = GMT_get_cstate (connection->t);
2992 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2993 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2994 if (MESH_CONNECTION_NEW == connection->state)
2995 connection_change_state (connection, MESH_CONNECTION_SENT);
3000 * Send a message to all peers in this connection that the connection
3001 * is no longer valid.
3003 * If some peer should not receive the message, it should be zero'ed out
3004 * before calling this function.
3006 * @param c The connection whose peers to notify.
3009 GMC_send_destroy (struct MeshConnection *c)
3011 struct GNUNET_MESH_ConnectionDestroy msg;
3013 if (GNUNET_YES == c->destroy)
3016 msg.header.size = htons (sizeof (msg));
3017 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
3019 LOG (GNUNET_ERROR_TYPE_DEBUG,
3020 " sending connection destroy for connection %s\n",
3023 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
3024 GMC_send_prebuilt_message (&msg.header, c,
3025 GNUNET_YES, GNUNET_YES, NULL, NULL);
3026 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
3027 GMC_send_prebuilt_message (&msg.header, c,
3028 GNUNET_NO, GNUNET_YES, NULL, NULL);
3029 c->destroy = GNUNET_YES;
3030 c->state = MESH_CONNECTION_DESTROYED;
3035 * @brief Start a polling timer for the connection.
3037 * When a neighbor does not accept more traffic on the connection it could be
3038 * caused by a simple congestion or by a lost ACK. Polling enables to check
3039 * for the lastest ACK status for a connection.
3041 * @param c Connection.
3042 * @param fwd Should we poll in the FWD direction?
3045 GMC_start_poll (struct MeshConnection *c, int fwd)
3047 struct MeshFlowControl *fc;
3049 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3050 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3052 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3054 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3055 fc->poll_task, fc->poll_msg);
3058 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3059 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3066 * @brief Stop polling a connection for ACKs.
3068 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3070 * @param c Connection.
3071 * @param fwd Should we stop the poll in the FWD direction?
3074 GMC_stop_poll (struct MeshConnection *c, int fwd)
3076 struct MeshFlowControl *fc;
3078 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3079 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3081 GNUNET_SCHEDULER_cancel (fc->poll_task);
3082 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3087 * Get a (static) string for a connection.
3089 * @param c Connection.
3092 GMC_2s (const struct MeshConnection *c)
3099 static char buf[128];
3101 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
3104 return GNUNET_h2s (&c->id);