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.
184 struct MeshPeerPath *path;
187 * Position of the local peer in the path.
189 unsigned int own_pos;
192 * Task to keep the used paths alive at the owner,
193 * time tunnel out on all the other peers.
195 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
198 * Task to keep the used paths alive at the destination,
199 * time tunnel out on all the other peers.
201 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
204 * Pending message count.
206 int pending_messages;
209 * Destroy flag: if true, destroy on last message.
215 * Handle for messages queued but not yet sent.
217 struct MeshConnectionQueue
220 * Peer queue handle, to cancel if necessary.
222 struct MeshPeerQueue *q;
225 * Continuation to call once sent.
230 * Closure for @c cont.
235 /******************************************************************************/
236 /******************************* GLOBALS ***********************************/
237 /******************************************************************************/
240 * Global handle to the statistics service.
242 extern struct GNUNET_STATISTICS_Handle *stats;
245 * Local peer own ID (memory efficient handle).
247 extern GNUNET_PEER_Id myid;
250 * Local peer own ID (full value).
252 extern struct GNUNET_PeerIdentity my_full_id;
255 * Connections known, indexed by cid (MeshConnection).
257 static struct GNUNET_CONTAINER_MultiHashMap *connections;
260 * How many connections are we willing to maintain.
261 * Local connections are always allowed, even if there are more connections than max.
263 static unsigned long long max_connections;
266 * How many messages *in total* are we willing to queue, divide by number of
267 * connections to get connection queue size.
269 static unsigned long long max_msgs_queue;
272 * How often to send path keepalives. Paths timeout after 4 missed.
274 static struct GNUNET_TIME_Relative refresh_connection_time;
277 * How often to send path create / ACKs.
279 static struct GNUNET_TIME_Relative create_connection_time;
282 /******************************************************************************/
283 /******************************** STATIC ***********************************/
284 /******************************************************************************/
286 #if 0 // avoid compiler warning for unused static function
288 fc_debug (struct MeshFlowControl *fc)
290 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
291 fc->last_pid_recv, fc->last_ack_sent);
292 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
293 fc->last_pid_sent, fc->last_ack_recv);
294 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
295 fc->queue_n, fc->queue_max);
299 connection_debug (struct MeshConnection *c)
303 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
306 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
307 peer2s (c->t->peer), GMC_2s (c));
308 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
309 c->state, c->pending_messages);
310 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
311 fc_debug (&c->fwd_fc);
312 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
313 fc_debug (&c->bck_fc);
318 * Get string description for tunnel state.
320 * @param s Tunnel state.
322 * @return String representation.
325 GMC_state2s (enum MeshConnectionState s)
329 case MESH_CONNECTION_NEW:
330 return "MESH_CONNECTION_NEW";
331 case MESH_CONNECTION_SENT:
332 return "MESH_CONNECTION_SENT";
333 case MESH_CONNECTION_ACK:
334 return "MESH_CONNECTION_ACK";
335 case MESH_CONNECTION_READY:
336 return "MESH_CONNECTION_READY";
338 return "MESH_CONNECTION_STATE_ERROR";
344 * Initialize a Flow Control structure to the initial state.
346 * @param fc Flow Control structure to initialize.
349 fc_init (struct MeshFlowControl *fc)
352 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
353 fc->last_pid_recv = (uint32_t) -1;
354 fc->last_ack_sent = (uint32_t) 0;
355 fc->last_ack_recv = (uint32_t) 0;
356 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
357 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
359 fc->queue_max = (max_msgs_queue / max_connections) + 1;
366 * @param cid Connection ID.
368 static struct MeshConnection *
369 connection_get (const struct GNUNET_HashCode *cid)
371 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
376 connection_change_state (struct MeshConnection* c,
377 enum MeshConnectionState state)
379 LOG (GNUNET_ERROR_TYPE_DEBUG,
380 "Connection %s state was %s\n",
381 GMC_2s (c), GMC_state2s (c->state));
382 LOG (GNUNET_ERROR_TYPE_DEBUG,
383 "Connection %s state is now %s\n",
384 GMC_2s (c), GMC_state2s (state));
390 * Callback called when a queued ACK message is sent.
392 * @param cls Closure (FC).
393 * @param c Connection this message was on.
394 * @param q Queue handler this call invalidates.
395 * @param type Type of message sent.
396 * @param fwd Was this a FWD going message?
397 * @param size Size of the message.
401 struct MeshConnection *c,
402 struct MeshConnectionQueue *q,
403 uint16_t type, int fwd, size_t size)
405 struct MeshFlowControl *fc = cls;
412 * Send an ACK on the connection, informing the predecessor about
413 * the available buffer space. Should not be called in case the peer
414 * is origin (no predecessor) in the @c fwd direction.
416 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
417 * the ACK itself goes "back" (dest->root).
419 * @param c Connection on which to send the ACK.
420 * @param buffer How much space free to advertise?
421 * @param fwd Is this FWD ACK? (Going dest -> root)
422 * @param force Don't optimize out.
425 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
427 struct MeshFlowControl *next_fc;
428 struct MeshFlowControl *prev_fc;
429 struct GNUNET_MESH_ACK msg;
433 /* If origin, there is no connection to send ACKs. Wrong function! */
434 if (GMC_is_origin (c, fwd))
440 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
441 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
443 LOG (GNUNET_ERROR_TYPE_DEBUG,
444 "connection send %s ack on %s\n",
445 fwd ? "FWD" : "BCK", GMC_2s (c));
447 /* Check if we need to transmit the ACK. */
448 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
449 if (3 < delta && buffer < delta && GNUNET_NO == force)
451 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
452 LOG (GNUNET_ERROR_TYPE_DEBUG,
453 " last pid recv: %u, last ack sent: %u\n",
454 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
458 /* Ok, ACK might be necessary, what PID to ACK? */
459 ack = prev_fc->last_pid_recv + buffer;
460 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
461 LOG (GNUNET_ERROR_TYPE_DEBUG,
462 " last pid %u, last ack %u, qmax %u, q %u\n",
463 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
464 next_fc->queue_max, next_fc->queue_n);
465 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
467 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
471 /* Check if message is already in queue */
472 if (NULL != prev_fc->ack_msg)
474 if (GMC_is_pid_bigger (ack, prev_fc->last_ack_sent))
476 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
477 GMC_cancel (prev_fc->ack_msg);
478 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
482 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
487 prev_fc->last_ack_sent = ack;
489 /* Build ACK message and send on connection */
490 msg.header.size = htons (sizeof (msg));
491 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
492 msg.ack = htonl (ack);
495 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c, !fwd,
501 * Callback called when a queued message is sent.
503 * Calculates the average time and connection packet tracking.
505 * @param cls Closure (ConnectionQueue Handle).
506 * @param c Connection this message was on.
507 * @param type Type of message sent.
508 * @param fwd Was this a FWD going message?
509 * @param size Size of the message.
510 * @param wait Time spent waiting for core (only the time for THIS message)
513 message_sent (void *cls,
514 struct MeshConnection *c, uint16_t type,
515 int fwd, size_t size,
516 struct GNUNET_TIME_Relative wait)
518 struct MeshConnectionPerformance *p;
519 struct MeshFlowControl *fc;
520 struct MeshConnectionQueue *q = cls;
523 fc = fwd ? &c->fwd_fc : &c->bck_fc;
524 LOG (GNUNET_ERROR_TYPE_DEBUG,
527 GNUNET_MESH_DEBUG_M2S (type));
528 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
533 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
534 q->cont (q->cont_cls, c, q, type, fwd, size);
538 c->pending_messages--;
539 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
541 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
545 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
548 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
550 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
552 LOG (GNUNET_ERROR_TYPE_DEBUG,
553 "! accounting pid %u\n",
555 GMC_send_ack (c, fwd, GNUNET_NO);
558 case GNUNET_MESSAGE_TYPE_MESH_POLL:
562 case GNUNET_MESSAGE_TYPE_MESH_ACK:
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
572 return; /* Only endpoints are interested in timing. */
575 usecsperbyte = ((double) wait.rel_value_us) / size;
576 if (p->size == AVG_MSGS)
578 /* Array is full. Substract oldest value, add new one and store. */
579 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
580 p->usecsperbyte[p->idx] = usecsperbyte;
581 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
585 /* Array not yet full. Add current value to avg and store. */
586 p->usecsperbyte[p->idx] = usecsperbyte;
588 p->avg += p->usecsperbyte[p->idx];
592 p->idx = (p->idx + 1) % AVG_MSGS;
597 * Get the previous hop in a connection
599 * @param c Connection.
601 * @return Previous peer in the connection.
603 static struct MeshPeer *
604 get_prev_hop (const struct MeshConnection *c)
608 LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos);
609 if (0 == c->own_pos || c->path->length < 2)
610 id = c->path->peers[0];
612 id = c->path->peers[c->own_pos - 1];
614 return GMP_get_short (id);
619 * Get the next hop in a connection
621 * @param c Connection.
623 * @return Next peer in the connection.
625 static struct MeshPeer *
626 get_next_hop (const struct MeshConnection *c)
630 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
631 id = c->path->peers[c->path->length - 1];
633 id = c->path->peers[c->own_pos + 1];
635 return GMP_get_short (id);
640 * Get the hop in a connection.
642 * @param c Connection.
643 * @param fwd Next hop?
645 * @return Next peer in the connection.
647 static struct MeshPeer *
648 get_hop (struct MeshConnection *c, int fwd)
651 return get_next_hop (c);
652 return get_prev_hop (c);
657 * Is traffic coming from this sender 'FWD' traffic?
659 * @param c Connection to check.
660 * @param sender Peer identity of neighbor.
662 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
663 * the traffic is 'FWD'.
664 * #GNUNET_NO for BCK.
665 * #GNUNET_SYSERR for errors.
668 is_fwd (const struct MeshConnection *c,
669 const struct GNUNET_PeerIdentity *sender)
673 id = GNUNET_PEER_search (sender);
674 if (GMP_get_short_id (get_prev_hop (c)) == id)
677 if (GMP_get_short_id (get_next_hop (c)) == id)
681 return GNUNET_SYSERR;
686 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
687 * or a first CONNECTION_ACK directed to us.
689 * @param connection Connection to confirm.
690 * @param fwd Should we send it FWD? (root->dest)
691 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
694 send_connection_ack (struct MeshConnection *connection, int fwd)
696 struct MeshTunnel3 *t;
699 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
700 !fwd ? "FWD" : "BCK");
701 GMP_queue_add (get_hop (connection, fwd), NULL,
702 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
703 sizeof (struct GNUNET_MESH_ConnectionACK),
704 connection, fwd, &message_sent, NULL);
705 connection->pending_messages++;
706 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
707 GMT_change_state (t, MESH_TUNNEL3_WAITING);
708 if (MESH_CONNECTION_READY != connection->state)
709 connection_change_state (connection, MESH_CONNECTION_SENT);
714 * Send a notification that a connection is broken.
716 * @param c Connection that is broken.
717 * @param id1 Peer that has disconnected.
718 * @param id2 Peer that has disconnected.
719 * @param fwd Direction towards which to send it.
722 send_broken (struct MeshConnection *c,
723 const struct GNUNET_PeerIdentity *id1,
724 const struct GNUNET_PeerIdentity *id2,
727 struct GNUNET_MESH_ConnectionBroken msg;
729 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
730 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
734 GMC_send_prebuilt_message (&msg.header, c, fwd, NULL, NULL);
740 * Send keepalive packets for a connection.
742 * @param c Connection to keep alive..
743 * @param fwd Is this a FWD keepalive? (owner -> dest).
746 connection_keepalive (struct MeshConnection *c, int fwd)
748 struct GNUNET_MESH_ConnectionKeepAlive *msg;
749 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
753 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
754 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
756 LOG (GNUNET_ERROR_TYPE_DEBUG,
757 "sending %s keepalive for connection %s]\n",
758 fwd ? "FWD" : "BCK", GMC_2s (c));
760 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
761 msg->header.size = htons (size);
762 msg->header.type = htons (type);
765 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
770 * Send CONNECTION_{CREATE/ACK} packets for a connection.
772 * @param c Connection for which to send the message.
773 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
776 connection_recreate (struct MeshConnection *c, int fwd)
778 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
782 send_connection_ack (c, GNUNET_NO);
787 * Generic connection timer management.
788 * Depending on the role of the peer in the connection will send the
789 * appropriate message (build or keepalive)
791 * @param c Conncetion to maintain.
795 connection_maintain (struct MeshConnection *c, int fwd)
797 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
799 /* TODO DHT GET with RO_BART */
804 case MESH_CONNECTION_NEW:
807 case MESH_CONNECTION_SENT:
808 connection_recreate (c, fwd);
810 case MESH_CONNECTION_READY:
811 connection_keepalive (c, fwd);
820 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
822 struct MeshConnection *c = cls;
823 struct GNUNET_TIME_Relative delay;
825 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
826 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
829 connection_maintain (c, GNUNET_YES);
830 delay = c->state == MESH_CONNECTION_READY ?
831 refresh_connection_time : create_connection_time;
832 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
833 &connection_fwd_keepalive,
839 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
841 struct MeshConnection *c = cls;
842 struct GNUNET_TIME_Relative delay;
844 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
845 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
848 connection_maintain (c, GNUNET_NO);
849 delay = c->state == MESH_CONNECTION_READY ?
850 refresh_connection_time : create_connection_time;
851 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
852 &connection_bck_keepalive,
858 * @brief Re-initiate traffic on this connection if necessary.
860 * Check if there is traffic queued towards this peer
861 * and the core transmit handle is NULL (traffic was stalled).
862 * If so, call core tmt rdy.
864 * @param c Connection on which initiate traffic.
865 * @param fwd Is this about fwd traffic?
868 connection_unlock_queue (struct MeshConnection *c, int fwd)
870 struct MeshPeer *peer;
872 LOG (GNUNET_ERROR_TYPE_DEBUG,
873 "connection_unlock_queue %s on %s\n",
874 fwd ? "FWD" : "BCK", GMC_2s (c));
876 if (GMC_is_terminal (c, fwd))
878 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
882 peer = get_hop (c, fwd);
883 GMP_queue_unlock (peer, c);
888 * Cancel all transmissions that belong to a certain connection.
890 * If the connection is scheduled for destruction and no more messages are left,
891 * the connection will be destroyed by the continuation call.
893 * @param c Connection which to cancel. Might be destroyed during this call.
894 * @param fwd Cancel fwd traffic?
897 connection_cancel_queues (struct MeshConnection *c, int fwd)
899 struct MeshFlowControl *fc;
900 struct MeshPeer *peer;
908 fc = fwd ? &c->fwd_fc : &c->bck_fc;
909 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
911 GNUNET_SCHEDULER_cancel (fc->poll_task);
912 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
914 peer = get_hop (c, fwd);
915 GMP_queue_cancel (peer, c);
920 * Function called if a connection has been stalled for a while,
921 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
923 * @param cls Closure (poll ctx).
924 * @param tc TaskContext.
927 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
931 * Callback called when a queued POLL message is sent.
933 * @param cls Closure (FC).
934 * @param c Connection this message was on.
935 * @param q Queue handler this call invalidates.
936 * @param type Type of message sent.
937 * @param fwd Was this a FWD going message?
938 * @param size Size of the message.
941 poll_sent (void *cls,
942 struct MeshConnection *c,
943 struct MeshConnectionQueue *q,
944 uint16_t type, int fwd, size_t size)
946 struct MeshFlowControl *fc = cls;
948 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL sent, scheduling new one!\n");
950 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
951 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
952 &connection_poll, fc);
956 * Function called if a connection has been stalled for a while,
957 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
959 * @param cls Closure (poll ctx).
960 * @param tc TaskContext.
963 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
965 struct MeshFlowControl *fc = cls;
966 struct GNUNET_MESH_Poll msg;
967 struct MeshConnection *c;
969 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
970 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
976 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
977 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
978 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
979 fc == &c->fwd_fc ? "FWD" : "BCK");
981 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
982 msg.header.size = htons (sizeof (msg));
983 msg.pid = htonl (fc->last_pid_sent);
984 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
985 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c, fc == &c->fwd_fc,
991 * Timeout function due to lack of keepalive/traffic from the owner.
992 * Destroys connection if called.
994 * @param cls Closure (connection to destroy).
995 * @param tc TaskContext.
998 connection_fwd_timeout (void *cls,
999 const struct GNUNET_SCHEDULER_TaskContext *tc)
1001 struct MeshConnection *c = cls;
1003 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1004 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1006 LOG (GNUNET_ERROR_TYPE_DEBUG,
1007 "Connection %s[%X] FWD timed out. Destroying.\n",
1011 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1019 * Timeout function due to lack of keepalive/traffic from the destination.
1020 * Destroys connection if called.
1022 * @param cls Closure (connection to destroy).
1023 * @param tc TaskContext
1026 connection_bck_timeout (void *cls,
1027 const struct GNUNET_SCHEDULER_TaskContext *tc)
1029 struct MeshConnection *c = cls;
1031 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1032 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1035 LOG (GNUNET_ERROR_TYPE_DEBUG,
1036 "Connection %s[%X] FWD timed out. Destroying.\n",
1037 GMT_2s (c->t), c->id);
1039 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1047 * Resets the connection timeout task, some other message has done the
1049 * - For the first peer on the direction this means to send
1050 * a keepalive or a path confirmation message (either create or ACK).
1051 * - For all other peers, this means to destroy the connection,
1052 * due to lack of activity.
1053 * Starts the tiemout if no timeout was running (connection just created).
1055 * @param c Connection whose timeout to reset.
1056 * @param fwd Is this forward?
1058 * TODO use heap to improve efficiency of scheduler.
1061 connection_reset_timeout (struct MeshConnection *c, int fwd)
1063 GNUNET_SCHEDULER_TaskIdentifier *ti;
1064 GNUNET_SCHEDULER_Task f;
1066 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1068 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1069 GNUNET_SCHEDULER_cancel (*ti);
1071 if (GMC_is_origin (c, fwd)) /* Startpoint */
1073 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1074 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1076 else /* Relay, endpoint. */
1078 struct GNUNET_TIME_Relative delay;
1080 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1081 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1082 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1088 * Add the connection to the list of both neighbors.
1090 * @param c Connection.
1092 * @return #GNUNET_OK if everything went fine
1093 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1096 register_neighbors (struct MeshConnection *c)
1098 struct MeshPeer *next_peer;
1099 struct MeshPeer *prev_peer;
1101 next_peer = get_next_hop (c);
1102 prev_peer = get_prev_hop (c);
1104 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1105 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1106 return GNUNET_SYSERR;
1108 GMP_add_connection (next_peer, c);
1109 GMP_add_connection (prev_peer, c);
1116 * Remove the connection from the list of both neighbors.
1118 * @param c Connection.
1121 unregister_neighbors (struct MeshConnection *c)
1123 struct MeshPeer *peer;
1125 peer = get_next_hop (c);
1126 GMP_remove_connection (peer, c);
1128 peer = get_prev_hop (c);
1129 GMP_remove_connection (peer, c);
1135 * Bind the connection to the peer and the tunnel to that peer.
1137 * If the peer has no tunnel, create one. Update tunnel and connection
1138 * data structres to reflect new status.
1140 * @param c Connection.
1144 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1146 GMP_add_tunnel (peer);
1147 c->t = GMP_get_tunnel (peer);
1148 GMT_add_connection (c->t, c);
1151 /******************************************************************************/
1152 /******************************** API ***********************************/
1153 /******************************************************************************/
1156 * Core handler for connection creation.
1158 * @param cls Closure (unused).
1159 * @param peer Sender (neighbor).
1160 * @param message Message.
1162 * @return GNUNET_OK to keep the connection open,
1163 * GNUNET_SYSERR to close it (signal serious error)
1166 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1167 const struct GNUNET_MessageHeader *message)
1169 struct GNUNET_MESH_ConnectionCreate *msg;
1170 struct GNUNET_PeerIdentity *id;
1171 struct GNUNET_HashCode *cid;
1172 struct MeshPeerPath *path;
1173 struct MeshPeer *dest_peer;
1174 struct MeshPeer *orig_peer;
1175 struct MeshConnection *c;
1176 unsigned int own_pos;
1180 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1184 size = ntohs (message->size);
1185 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1187 GNUNET_break_op (0);
1191 /* Calculate hops */
1192 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1193 if (size % sizeof (struct GNUNET_PeerIdentity))
1195 GNUNET_break_op (0);
1198 size /= sizeof (struct GNUNET_PeerIdentity);
1201 GNUNET_break_op (0);
1204 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1206 /* Get parameters */
1207 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1209 id = (struct GNUNET_PeerIdentity *) &msg[1];
1210 LOG (GNUNET_ERROR_TYPE_DEBUG,
1211 " connection %s (%s).\n",
1212 GNUNET_h2s (cid), GNUNET_i2s (id));
1214 /* Create connection */
1215 c = connection_get (cid);
1219 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1220 path = path_new (size);
1222 for (i = 0; i < size; i++)
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1225 GNUNET_i2s (&id[i]));
1226 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1227 if (path->peers[i] == myid)
1230 if (own_pos == 0 && path->peers[own_pos] != myid)
1232 /* create path: self not found in path through self */
1233 GNUNET_break_op (0);
1234 path_destroy (path);
1237 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1238 GMP_add_path_to_all (path, GNUNET_NO);
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1240 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1243 path_destroy (path);
1246 connection_reset_timeout (c, GNUNET_YES);
1252 if (MESH_CONNECTION_NEW == c->state)
1253 connection_change_state (c, MESH_CONNECTION_SENT);
1255 /* Remember peers */
1256 dest_peer = GMP_get (&id[size - 1]);
1257 orig_peer = GMP_get (&id[0]);
1259 /* Is it a connection to us? */
1260 if (c->own_pos == size - 1)
1262 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1263 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1265 add_to_peer (c, orig_peer);
1266 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1267 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1269 send_connection_ack (c, GNUNET_NO);
1270 if (MESH_CONNECTION_SENT == c->state)
1271 connection_change_state (c, MESH_CONNECTION_ACK);
1273 /* Keep tunnel alive in direction dest->owner*/
1274 c->bck_maintenance_task =
1275 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1276 &connection_bck_keepalive, c);
1280 /* It's for somebody else! Retransmit. */
1281 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1282 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1283 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1284 GMC_send_prebuilt_message (message, c, GNUNET_YES, NULL, NULL);
1291 * Core handler for path confirmations.
1293 * @param cls closure
1294 * @param message message
1295 * @param peer peer identity this notification is about
1297 * @return GNUNET_OK to keep the connection open,
1298 * GNUNET_SYSERR to close it (signal serious error)
1301 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1302 const struct GNUNET_MessageHeader *message)
1304 struct GNUNET_MESH_ConnectionACK *msg;
1305 struct MeshConnection *c;
1306 struct MeshPeerPath *p;
1307 struct MeshPeer *pi;
1308 enum MeshConnectionState oldstate;
1311 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1313 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1315 GNUNET_h2s (&msg->cid));
1316 c = connection_get (&msg->cid);
1319 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1321 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1325 oldstate = c->state;
1326 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1327 pi = GMP_get (peer);
1328 if (get_next_hop (c) == pi)
1330 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1332 if (MESH_CONNECTION_SENT == oldstate)
1333 connection_change_state (c, MESH_CONNECTION_ACK);
1335 else if (get_prev_hop (c) == pi)
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1339 connection_change_state (c, MESH_CONNECTION_READY);
1343 GNUNET_break_op (0);
1347 connection_reset_timeout (c, fwd);
1349 /* Add path to peers? */
1353 GMP_add_path_to_all (p, GNUNET_YES);
1360 /* Message for us as creator? */
1361 if (GMC_is_origin (c, GNUNET_YES))
1363 if (GNUNET_NO != fwd)
1365 GNUNET_break_op (0);
1368 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1370 /* If just created, cancel the short timeout and start a long one */
1371 if (MESH_CONNECTION_SENT == oldstate)
1372 connection_reset_timeout (c, GNUNET_YES);
1374 /* Change connection and tunnel state */
1375 connection_change_state (c, MESH_CONNECTION_READY);
1376 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1377 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1379 /* Send ACK (~TCP ACK)*/
1380 send_connection_ack (c, GNUNET_YES);
1383 /* Message for us as destination? */
1384 if (GMC_is_terminal (c, GNUNET_YES))
1386 if (GNUNET_YES != fwd)
1388 GNUNET_break_op (0);
1391 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1393 /* If just created, cancel the short timeout and start a long one */
1394 if (MESH_CONNECTION_ACK == oldstate)
1395 connection_reset_timeout (c, GNUNET_NO);
1397 /* Change tunnel state */
1398 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1399 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1404 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1405 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1411 * Core handler for notifications of broken paths
1413 * @param cls Closure (unused).
1414 * @param id Peer identity of sending neighbor.
1415 * @param message Message.
1417 * @return GNUNET_OK to keep the connection open,
1418 * GNUNET_SYSERR to close it (signal serious error)
1421 GMC_handle_broken (void* cls,
1422 const struct GNUNET_PeerIdentity* id,
1423 const struct GNUNET_MessageHeader* message)
1425 struct GNUNET_MESH_ConnectionBroken *msg;
1426 struct MeshConnection *c;
1429 LOG (GNUNET_ERROR_TYPE_DEBUG,
1430 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1431 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1433 GNUNET_i2s (&msg->peer1));
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1435 GNUNET_i2s (&msg->peer2));
1436 c = connection_get (&msg->cid);
1439 GNUNET_break_op (0);
1443 fwd = is_fwd (c, id);
1444 connection_cancel_queues (c, !fwd);
1445 if (GMC_is_terminal (c, fwd))
1447 if (0 < c->pending_messages)
1448 c->destroy = GNUNET_YES;
1454 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1455 c->destroy = GNUNET_YES;
1464 * Core handler for tunnel destruction
1466 * @param cls Closure (unused).
1467 * @param peer Peer identity of sending neighbor.
1468 * @param message Message.
1470 * @return GNUNET_OK to keep the connection open,
1471 * GNUNET_SYSERR to close it (signal serious error)
1474 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1475 const struct GNUNET_MessageHeader *message)
1477 struct GNUNET_MESH_ConnectionDestroy *msg;
1478 struct MeshConnection *c;
1481 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1482 LOG (GNUNET_ERROR_TYPE_DEBUG,
1483 "Got a CONNECTION DESTROY message from %s\n",
1485 LOG (GNUNET_ERROR_TYPE_DEBUG,
1486 " for connection %s\n",
1487 GNUNET_h2s (&msg->cid));
1488 c = connection_get (&msg->cid);
1491 /* Probably already got the message from another path,
1492 * destroyed the tunnel and retransmitted to children.
1495 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1499 fwd = is_fwd (c, peer);
1500 if (GNUNET_SYSERR == fwd)
1502 GNUNET_break_op (0);
1505 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1506 c->destroy = GNUNET_YES;
1512 * Generic handler for mesh network encrypted traffic.
1514 * @param peer Peer identity this notification is about.
1515 * @param msg Encrypted message.
1517 * @return GNUNET_OK to keep the connection open,
1518 * GNUNET_SYSERR to close it (signal serious error)
1521 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1522 const struct GNUNET_MESH_Encrypted *msg)
1524 struct MeshConnection *c;
1525 struct MeshPeer *neighbor;
1526 struct MeshFlowControl *fc;
1527 GNUNET_PEER_Id peer_id;
1535 size = ntohs (msg->header.size);
1537 sizeof (struct GNUNET_MESH_Encrypted) +
1538 sizeof (struct GNUNET_MessageHeader))
1540 GNUNET_break_op (0);
1543 type = ntohs (msg->header.type);
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1546 GNUNET_MESH_DEBUG_M2S (type), ntohl (msg->pid), GNUNET_i2s (peer));
1548 /* Check connection */
1549 c = connection_get (&msg->cid);
1552 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1553 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1557 /* Check if origin is as expected */
1558 neighbor = get_prev_hop (c);
1559 peer_id = GNUNET_PEER_search (peer);
1560 if (peer_id == GMP_get_short_id (neighbor))
1566 neighbor = get_next_hop (c);
1567 if (peer_id == GMP_get_short_id (neighbor))
1573 /* Unexpected peer sending traffic on a connection. */
1574 GNUNET_break_op (0);
1580 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1581 pid = ntohl (msg->pid);
1582 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1584 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1585 LOG (GNUNET_ERROR_TYPE_DEBUG,
1586 "WARNING Received PID %u, (prev %u), ACK %u\n",
1587 pid, fc->last_pid_recv, fc->last_ack_sent);
1590 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1592 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1593 LOG (GNUNET_ERROR_TYPE_DEBUG,
1594 " Pid %u not expected (%u+), dropping!\n",
1595 pid, fc->last_pid_recv + 1);
1598 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1599 connection_change_state (c, MESH_CONNECTION_READY);
1600 connection_reset_timeout (c, fwd);
1601 fc->last_pid_recv = pid;
1603 /* Is this message for us? */
1604 if (GMC_is_terminal (c, fwd))
1606 /* TODO signature verification */
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1608 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1615 fc->last_pid_recv = pid;
1616 GMT_handle_encrypted (c->t, msg);
1617 GMC_send_ack (c, fwd, GNUNET_NO);
1621 /* Message not for us: forward to next hop */
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1623 ttl = ntohl (msg->ttl);
1624 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1627 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1628 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1629 GMC_send_ack (c, fwd, GNUNET_NO);
1633 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1634 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
1640 * Generic handler for mesh network encrypted traffic.
1642 * @param peer Peer identity this notification is about.
1643 * @param msg Encrypted message.
1645 * @return GNUNET_OK to keep the connection open,
1646 * GNUNET_SYSERR to close it (signal serious error)
1649 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1650 const struct GNUNET_MESH_KX *msg)
1652 struct MeshConnection *c;
1653 struct MeshPeer *neighbor;
1654 GNUNET_PEER_Id peer_id;
1660 size = ntohs (msg->header.size);
1662 sizeof (struct GNUNET_MESH_Encrypted) +
1663 sizeof (struct GNUNET_MessageHeader))
1665 GNUNET_break_op (0);
1668 type = ntohs (msg->header.type);
1669 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1671 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1673 /* Check connection */
1674 c = connection_get (&msg->cid);
1677 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1678 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1682 /* Check if origin is as expected */
1683 neighbor = get_prev_hop (c);
1684 peer_id = GNUNET_PEER_search (peer);
1685 if (peer_id == GMP_get_short_id (neighbor))
1691 neighbor = get_next_hop (c);
1692 if (peer_id == GMP_get_short_id (neighbor))
1698 /* Unexpected peer sending traffic on a connection. */
1699 GNUNET_break_op (0);
1704 /* Count as connection confirmation. */
1705 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1706 connection_change_state (c, MESH_CONNECTION_READY);
1707 connection_reset_timeout (c, fwd);
1710 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1711 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1714 /* Is this message for us? */
1715 if (GMC_is_terminal (c, fwd))
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1718 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1724 GMT_handle_kx (c->t, &msg[1].header);
1728 /* Message not for us: forward to next hop */
1729 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1730 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1731 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
1738 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1740 * @param cls Closure (unused).
1741 * @param message Message received.
1742 * @param peer Peer who sent the message.
1744 * @return GNUNET_OK to keep the connection open,
1745 * GNUNET_SYSERR to close it (signal serious error)
1748 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1749 const struct GNUNET_MessageHeader *message)
1751 return handle_mesh_encrypted (peer,
1752 (struct GNUNET_MESH_Encrypted *)message);
1757 * Core handler for key exchange traffic (ephemeral key, ping, pong).
1759 * @param cls Closure (unused).
1760 * @param message Message received.
1761 * @param peer Peer who sent the message.
1763 * @return GNUNET_OK to keep the connection open,
1764 * GNUNET_SYSERR to close it (signal serious error)
1767 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1768 const struct GNUNET_MessageHeader *message)
1770 return handle_mesh_kx (peer,
1771 (struct GNUNET_MESH_KX *) message);
1776 * Core handler for mesh network traffic point-to-point acks.
1778 * @param cls closure
1779 * @param message message
1780 * @param peer peer identity this notification is about
1782 * @return GNUNET_OK to keep the connection open,
1783 * GNUNET_SYSERR to close it (signal serious error)
1786 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1787 const struct GNUNET_MessageHeader *message)
1789 struct GNUNET_MESH_ACK *msg;
1790 struct MeshConnection *c;
1791 struct MeshFlowControl *fc;
1796 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1797 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1799 msg = (struct GNUNET_MESH_ACK *) message;
1801 c = connection_get (&msg->cid);
1805 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1810 /* Is this a forward or backward ACK? */
1811 id = GNUNET_PEER_search (peer);
1812 if (GMP_get_short_id (get_next_hop (c)) == id)
1814 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1818 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1820 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1826 GNUNET_break_op (0);
1830 ack = ntohl (msg->ack);
1831 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1832 ack, fc->last_ack_recv);
1833 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1834 fc->last_ack_recv = ack;
1836 /* Cancel polling if the ACK is big enough. */
1837 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1838 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1840 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1841 GNUNET_SCHEDULER_cancel (fc->poll_task);
1842 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1843 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1846 connection_unlock_queue (c, fwd);
1853 * Core handler for mesh network traffic point-to-point ack polls.
1855 * @param cls closure
1856 * @param message message
1857 * @param peer peer identity this notification is about
1859 * @return GNUNET_OK to keep the connection open,
1860 * GNUNET_SYSERR to close it (signal serious error)
1863 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1864 const struct GNUNET_MessageHeader *message)
1866 struct GNUNET_MESH_Poll *msg;
1867 struct MeshConnection *c;
1868 struct MeshFlowControl *fc;
1873 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1874 LOG (GNUNET_ERROR_TYPE_DEBUG,
1875 "Got a POLL packet from %s!\n",
1878 msg = (struct GNUNET_MESH_Poll *) message;
1880 c = connection_get (&msg->cid);
1884 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1886 GNUNET_break_op (0);
1890 /* Is this a forward or backward ACK?
1891 * Note: a poll should never be needed in a loopback case,
1892 * since there is no possiblility of packet loss there, so
1893 * this way of discerining FWD/BCK should not be a problem.
1895 id = GNUNET_PEER_search (peer);
1896 if (GMP_get_short_id (get_next_hop (c)) == id)
1898 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
1901 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1903 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
1908 GNUNET_break_op (0);
1912 pid = ntohl (msg->pid);
1913 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
1914 fc->last_pid_recv = pid;
1915 fwd = fc == &c->bck_fc;
1916 GMC_send_ack (c, fwd, GNUNET_YES);
1923 * Core handler for mesh keepalives.
1925 * @param cls closure
1926 * @param message message
1927 * @param peer peer identity this notification is about
1928 * @return GNUNET_OK to keep the connection open,
1929 * GNUNET_SYSERR to close it (signal serious error)
1931 * TODO: Check who we got this from, to validate route.
1934 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1935 const struct GNUNET_MessageHeader *message)
1937 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1938 struct MeshConnection *c;
1939 struct MeshPeer *neighbor;
1942 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1943 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1946 c = connection_get (&msg->cid);
1949 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1954 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1955 GNUNET_YES : GNUNET_NO;
1957 /* Check if origin is as expected */
1958 neighbor = get_hop (c, fwd);
1959 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1961 GNUNET_break_op (0);
1965 connection_change_state (c, MESH_CONNECTION_READY);
1966 connection_reset_timeout (c, fwd);
1968 if (GMC_is_terminal (c, fwd))
1971 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1972 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1979 * Send an ACK on the appropriate connection/channel, depending on
1980 * the direction and the position of the peer.
1982 * @param c Which connection to send the hop-by-hop ACK.
1983 * @param fwd Is this a fwd ACK? (will go dest->root).
1984 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
1987 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
1989 unsigned int buffer;
1991 LOG (GNUNET_ERROR_TYPE_DEBUG,
1992 "GMC send %s ACK on %s\n",
1993 fwd ? "FWD" : "BCK", GMC_2s (c));
2001 /* Get available buffer space */
2002 if (GMC_is_terminal (c, fwd))
2004 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2005 buffer = GMT_get_channels_buffer (c->t);
2009 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2010 buffer = GMC_get_buffer (c, fwd);
2012 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2013 if (0 == buffer && GNUNET_NO == force)
2016 /* Send available buffer space */
2017 if (GMC_is_origin (c, fwd))
2019 GNUNET_assert (NULL != c->t);
2020 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2021 GMT_unchoke_channels (c->t);
2025 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2026 send_ack (c, buffer, fwd, force);
2032 * Initialize the connections subsystem
2034 * @param c Configuration handle.
2037 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2039 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2041 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2044 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2045 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2046 GNUNET_SCHEDULER_shutdown ();
2051 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2054 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2055 "MESH", "MAX_CONNECTIONS", "MISSING");
2056 GNUNET_SCHEDULER_shutdown ();
2061 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2062 &refresh_connection_time))
2064 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2065 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2066 GNUNET_SCHEDULER_shutdown ();
2069 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2070 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2074 * Shut down the connections subsystem.
2079 GNUNET_CONTAINER_multihashmap_destroy (connections);
2084 struct MeshConnection *
2085 GMC_new (const struct GNUNET_HashCode *cid,
2086 struct MeshTunnel3 *t,
2087 struct MeshPeerPath *p,
2088 unsigned int own_pos)
2090 struct MeshConnection *c;
2092 c = GNUNET_new (struct MeshConnection);
2094 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
2095 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2096 fc_init (&c->fwd_fc);
2097 fc_init (&c->bck_fc);
2102 if (own_pos > p->length - 1)
2109 c->own_pos = own_pos;
2114 c->fwd_maintenance_task =
2115 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2116 &connection_fwd_keepalive, c);
2118 if (GNUNET_OK != register_neighbors (c))
2129 GMC_destroy (struct MeshConnection *c)
2134 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2135 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2138 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2140 /* Cancel all traffic */
2141 connection_cancel_queues (c, GNUNET_YES);
2142 connection_cancel_queues (c, GNUNET_NO);
2144 /* Cancel maintainance task (keepalive/timeout) */
2145 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2146 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2147 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2148 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2149 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2150 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2151 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2152 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2154 /* Unregister from neighbors */
2155 unregister_neighbors (c);
2158 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2160 GMT_remove_connection (c->t, c);
2162 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES))
2163 path_destroy (c->path);
2165 GNUNET_break (GNUNET_YES ==
2166 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2172 * Get the connection ID.
2174 * @param c Connection to get the ID from.
2176 * @return ID of the connection.
2178 const struct GNUNET_HashCode *
2179 GMC_get_id (const struct MeshConnection *c)
2186 * Get the connection path.
2188 * @param c Connection to get the path from.
2190 * @return path used by the connection.
2192 const struct MeshPeerPath *
2193 GMC_get_path (const struct MeshConnection *c)
2200 * Get the connection state.
2202 * @param c Connection to get the state from.
2204 * @return state of the connection.
2206 enum MeshConnectionState
2207 GMC_get_state (const struct MeshConnection *c)
2213 * Get the connection tunnel.
2215 * @param c Connection to get the tunnel from.
2217 * @return tunnel of the connection.
2219 struct MeshTunnel3 *
2220 GMC_get_tunnel (const struct MeshConnection *c)
2227 * Get free buffer space in a connection.
2229 * @param c Connection.
2230 * @param fwd Is query about FWD traffic?
2232 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2235 GMC_get_buffer (struct MeshConnection *c, int fwd)
2237 struct MeshFlowControl *fc;
2239 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2241 return (fc->queue_max - fc->queue_n);
2245 * Get how many messages have we allowed to send to us from a direction.
2247 * @param c Connection.
2248 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2250 * @return last_ack_sent - last_pid_recv
2253 GMC_get_allowed (struct MeshConnection *c, int fwd)
2255 struct MeshFlowControl *fc;
2257 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2258 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2262 return (fc->last_ack_sent - fc->last_pid_recv);
2266 * Get messages queued in a connection.
2268 * @param c Connection.
2269 * @param fwd Is query about FWD traffic?
2271 * @return Number of messages queued.
2274 GMC_get_qn (struct MeshConnection *c, int fwd)
2276 struct MeshFlowControl *fc;
2278 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2285 * Allow the connection to advertise a buffer of the given size.
2287 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2288 * allowing up to last_pid_recv + buffer.
2290 * @param c Connection.
2291 * @param buffer How many more messages the connection can accept.
2292 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2295 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2297 send_ack (c, buffer, fwd, GNUNET_NO);
2302 * Notify other peers on a connection of a broken link. Mark connections
2303 * to destroy after all traffic has been sent.
2305 * @param c Connection on which there has been a disconnection.
2306 * @param peer Peer that disconnected.
2309 GMC_notify_broken (struct MeshConnection *c,
2310 struct MeshPeer *peer)
2314 LOG (GNUNET_ERROR_TYPE_DEBUG,
2315 " notify broken on %s due to %s disconnect\n",
2316 GMC_2s (c), GMP_2s (peer));
2318 fwd = peer == get_prev_hop (c);
2320 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2322 /* Local shutdown, no one to notify about this. */
2326 if (GNUNET_NO == c->destroy)
2327 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2329 /* Connection will have at least one pending message
2330 * (the one we just scheduled), so no point in checking whether to
2331 * destroy immediately. */
2332 c->destroy = GNUNET_YES;
2335 * Cancel all queues, if no message is left, connection will be destroyed.
2337 connection_cancel_queues (c, !fwd);
2344 * Is this peer the first one on the connection?
2346 * @param c Connection.
2347 * @param fwd Is this about fwd traffic?
2349 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2352 GMC_is_origin (struct MeshConnection *c, int fwd)
2354 if (!fwd && c->path->length - 1 == c->own_pos )
2356 if (fwd && 0 == c->own_pos)
2363 * Is this peer the last one on the connection?
2365 * @param c Connection.
2366 * @param fwd Is this about fwd traffic?
2367 * Note that the ROOT is the terminal for BCK traffic!
2369 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2372 GMC_is_terminal (struct MeshConnection *c, int fwd)
2374 return GMC_is_origin (c, !fwd);
2379 * See if we are allowed to send by the next hop in the given direction.
2381 * @param c Connection.
2382 * @param fwd Is this about fwd traffic?
2384 * @return #GNUNET_YES in case it's OK to send.
2387 GMC_is_sendable (struct MeshConnection *c, int fwd)
2389 struct MeshFlowControl *fc;
2391 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2392 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2398 * Sends an already built message on a connection, properly registering
2399 * all used resources.
2401 * @param message Message to send. Function makes a copy of it.
2402 * If message is not hop-by-hop, decrements TTL of copy.
2403 * @param c Connection on which this message is transmitted.
2404 * @param fwd Is this a fwd message?
2405 * @param cont Continuation called once message is sent. Can be NULL.
2406 * @param cont_cls Closure for @c cont.
2408 * @return Handle to cancel the message before it's sent.
2409 * NULL on error or if @c cont is NULL.
2410 * Invalid on @c cont call.
2412 struct MeshConnectionQueue *
2413 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2414 struct MeshConnection *c, int fwd,
2415 GMC_sent cont, void *cont_cls)
2417 struct MeshFlowControl *fc;
2418 struct MeshConnectionQueue *q;
2424 size = ntohs (message->size);
2425 data = GNUNET_malloc (size);
2426 memcpy (data, message, size);
2427 type = ntohs (message->type);
2428 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2429 GNUNET_MESH_DEBUG_M2S (type), size, GMC_2s (c));
2431 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2432 droppable = GNUNET_YES;
2435 struct GNUNET_MESH_Encrypted *emsg;
2436 struct GNUNET_MESH_KX *kmsg;
2437 struct GNUNET_MESH_ACK *amsg;
2438 struct GNUNET_MESH_Poll *pmsg;
2439 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2440 struct GNUNET_MESH_ConnectionBroken *bmsg;
2443 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2444 emsg = (struct GNUNET_MESH_Encrypted *) data;
2445 ttl = ntohl (emsg->ttl);
2448 GNUNET_break_op (0);
2453 emsg->ttl = htonl (ttl - 1);
2454 emsg->pid = htonl (fc->next_pid++);
2455 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2457 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2458 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2459 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2460 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2462 GMC_start_poll (c, fwd);
2466 case GNUNET_MESSAGE_TYPE_MESH_KX:
2467 kmsg = (struct GNUNET_MESH_KX *) data;
2471 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2472 amsg = (struct GNUNET_MESH_ACK *) data;
2474 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2475 droppable = GNUNET_NO;
2478 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2479 pmsg = (struct GNUNET_MESH_Poll *) data;
2481 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2482 droppable = GNUNET_NO;
2485 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2486 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2491 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2492 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2497 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2498 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2505 if (fc->queue_n > fc->queue_max && droppable)
2507 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2510 LOG (GNUNET_ERROR_TYPE_DEBUG,
2511 "queue full: %u/%u\n",
2512 fc->queue_n, fc->queue_max);
2513 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2519 return NULL; /* Drop this message */
2522 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2523 c->pending_messages++;
2527 (void) GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2528 &message_sent, NULL);
2532 q = GNUNET_new (struct MeshConnectionQueue);
2533 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2543 q->cont_cls = cont_cls;
2549 * Cancel a previously sent message while it's in the queue.
2551 * ONLY can be called before the continuation given to the send function
2552 * is called. Once the continuation is called, the message is no longer in the
2555 * @param q Handle to the queue.
2558 GMC_cancel (struct MeshConnectionQueue *q)
2560 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2562 /* queue destroy calls message_sent, which calls q->cont and frees q */
2563 GMP_queue_destroy (q->q, GNUNET_YES);
2568 * Sends a CREATE CONNECTION message for a path to a peer.
2569 * Changes the connection and tunnel states if necessary.
2571 * @param connection Connection to create.
2574 GMC_send_create (struct MeshConnection *connection)
2576 enum MeshTunnel3State state;
2579 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2580 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2581 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2582 GMP_queue_add (get_next_hop (connection), NULL,
2583 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2584 size, connection, GNUNET_YES, &message_sent, NULL);
2585 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2586 connection, connection->pending_messages);
2587 connection->pending_messages++;
2588 state = GMT_get_state (connection->t);
2589 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2590 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2591 if (MESH_CONNECTION_NEW == connection->state)
2592 connection_change_state (connection, MESH_CONNECTION_SENT);
2597 * Send a message to all peers in this connection that the connection
2598 * is no longer valid.
2600 * If some peer should not receive the message, it should be zero'ed out
2601 * before calling this function.
2603 * @param c The connection whose peers to notify.
2606 GMC_send_destroy (struct MeshConnection *c)
2608 struct GNUNET_MESH_ConnectionDestroy msg;
2610 if (GNUNET_YES == c->destroy)
2613 msg.header.size = htons (sizeof (msg));
2614 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2616 LOG (GNUNET_ERROR_TYPE_DEBUG,
2617 " sending connection destroy for connection %s\n",
2620 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2621 GMC_send_prebuilt_message (&msg.header, c, GNUNET_YES, NULL, NULL);
2622 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2623 GMC_send_prebuilt_message (&msg.header, c, GNUNET_NO, NULL, NULL);
2624 c->destroy = GNUNET_YES;
2629 * @brief Start a polling timer for the connection.
2631 * When a neighbor does not accept more traffic on the connection it could be
2632 * caused by a simple congestion or by a lost ACK. Polling enables to check
2633 * for the lastest ACK status for a connection.
2635 * @param c Connection.
2636 * @param fwd Should we poll in the FWD direction?
2639 GMC_start_poll (struct MeshConnection *c, int fwd)
2641 struct MeshFlowControl *fc;
2643 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2644 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task && NULL != fc->poll_msg)
2648 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2655 * @brief Stop polling a connection for ACKs.
2657 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2659 * @param c Connection.
2660 * @param fwd Should we stop the poll in the FWD direction?
2663 GMC_stop_poll (struct MeshConnection *c, int fwd)
2665 struct MeshFlowControl *fc;
2667 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2668 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2670 GNUNET_SCHEDULER_cancel (fc->poll_task);
2671 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2676 * Get a (static) string for a connection.
2678 * @param c Connection.
2681 GMC_2s (struct MeshConnection *c)
2685 static char buf[128];
2687 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2690 return GNUNET_h2s (&c->id);