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
219 struct MeshPeerQueue *q;
224 /******************************************************************************/
225 /******************************* GLOBALS ***********************************/
226 /******************************************************************************/
229 * Global handle to the statistics service.
231 extern struct GNUNET_STATISTICS_Handle *stats;
234 * Local peer own ID (memory efficient handle).
236 extern GNUNET_PEER_Id myid;
239 * Local peer own ID (full value).
241 extern struct GNUNET_PeerIdentity my_full_id;
244 * Connections known, indexed by cid (MeshConnection).
246 static struct GNUNET_CONTAINER_MultiHashMap *connections;
249 * How many connections are we willing to maintain.
250 * Local connections are always allowed, even if there are more connections than max.
252 static unsigned long long max_connections;
255 * How many messages *in total* are we willing to queue, divide by number of
256 * connections to get connection queue size.
258 static unsigned long long max_msgs_queue;
261 * How often to send path keepalives. Paths timeout after 4 missed.
263 static struct GNUNET_TIME_Relative refresh_connection_time;
266 * How often to send path create / ACKs.
268 static struct GNUNET_TIME_Relative create_connection_time;
271 /******************************************************************************/
272 /******************************** STATIC ***********************************/
273 /******************************************************************************/
275 #if 0 // avoid compiler warning for unused static function
277 fc_debug (struct MeshFlowControl *fc)
279 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
280 fc->last_pid_recv, fc->last_ack_sent);
281 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
282 fc->last_pid_sent, fc->last_ack_recv);
283 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
284 fc->queue_n, fc->queue_max);
288 connection_debug (struct MeshConnection *c)
292 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
295 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
296 peer2s (c->t->peer), GMC_2s (c));
297 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
298 c->state, c->pending_messages);
299 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
300 fc_debug (&c->fwd_fc);
301 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
302 fc_debug (&c->bck_fc);
307 * Get string description for tunnel state.
309 * @param s Tunnel state.
311 * @return String representation.
314 GMC_state2s (enum MeshConnectionState s)
318 case MESH_CONNECTION_NEW:
319 return "MESH_CONNECTION_NEW";
320 case MESH_CONNECTION_SENT:
321 return "MESH_CONNECTION_SENT";
322 case MESH_CONNECTION_ACK:
323 return "MESH_CONNECTION_ACK";
324 case MESH_CONNECTION_READY:
325 return "MESH_CONNECTION_READY";
327 return "MESH_CONNECTION_STATE_ERROR";
333 * Initialize a Flow Control structure to the initial state.
335 * @param fc Flow Control structure to initialize.
338 fc_init (struct MeshFlowControl *fc)
341 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
342 fc->last_pid_recv = (uint32_t) -1;
343 fc->last_ack_sent = (uint32_t) 0;
344 fc->last_ack_recv = (uint32_t) 0;
345 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
346 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
348 fc->queue_max = (max_msgs_queue / max_connections) + 1;
355 * @param cid Connection ID.
357 static struct MeshConnection *
358 connection_get (const struct GNUNET_HashCode *cid)
360 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
365 connection_change_state (struct MeshConnection* c,
366 enum MeshConnectionState state)
368 LOG (GNUNET_ERROR_TYPE_DEBUG,
369 "Connection %s state was %s\n",
370 GMC_2s (c), GMC_state2s (c->state));
371 LOG (GNUNET_ERROR_TYPE_DEBUG,
372 "Connection %s state is now %s\n",
373 GMC_2s (c), GMC_state2s (state));
379 * Callback called when a queued ACK message is sent.
381 * @param cls Closure (FC).
382 * @param c Connection this message was on.
383 * @param q Queue handler this call invalidates.
384 * @param type Type of message sent.
385 * @param fwd Was this a FWD going message?
386 * @param size Size of the message.
390 struct MeshConnection *c,
391 struct MeshConnectionQueue *q,
392 uint16_t type, int fwd, size_t size)
394 struct MeshFlowControl *fc = cls;
401 * Send an ACK on the connection, informing the predecessor about
402 * the available buffer space. Should not be called in case the peer
403 * is origin (no predecessor) in the @c fwd direction.
405 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
406 * the ACK itself goes "back" (dest->root).
408 * @param c Connection on which to send the ACK.
409 * @param buffer How much space free to advertise?
410 * @param fwd Is this FWD ACK? (Going dest -> root)
411 * @param force Don't optimize out.
414 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
416 struct MeshFlowControl *next_fc;
417 struct MeshFlowControl *prev_fc;
418 struct GNUNET_MESH_ACK msg;
422 /* If origin, there is no connection to send ACKs. Wrong function! */
423 if (GMC_is_origin (c, fwd))
429 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
430 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
432 LOG (GNUNET_ERROR_TYPE_DEBUG,
433 "connection send %s ack on %s\n",
434 fwd ? "FWD" : "BCK", GMC_2s (c));
436 /* Check if we need to transmit the ACK. */
437 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
438 if (3 < delta && buffer < delta && GNUNET_NO == force)
440 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 " last pid recv: %u, last ack sent: %u\n",
443 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
447 /* Ok, ACK might be necessary, what PID to ACK? */
448 ack = prev_fc->last_pid_recv + buffer;
449 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
450 LOG (GNUNET_ERROR_TYPE_DEBUG,
451 " last pid %u, last ack %u, qmax %u, q %u\n",
452 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
453 next_fc->queue_max, next_fc->queue_n);
454 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
456 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
460 /* Check if message is already in queue */
461 if (NULL != prev_fc->ack_msg)
463 if (GMC_is_pid_bigger (ack, prev_fc->last_ack_sent))
465 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
466 GMC_cancel (prev_fc->ack_msg);
467 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
471 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
476 prev_fc->last_ack_sent = ack;
478 /* Build ACK message and send on connection */
479 msg.header.size = htons (sizeof (msg));
480 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
481 msg.ack = htonl (ack);
484 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c, !fwd,
490 * Callback called when a queued message is sent.
492 * Calculates the average time and connection packet tracking.
494 * @param cls Closure (ConnectionQueue Handle).
495 * @param c Connection this message was on.
496 * @param type Type of message sent.
497 * @param fwd Was this a FWD going message?
498 * @param size Size of the message.
499 * @param wait Time spent waiting for core (only the time for THIS message)
502 message_sent (void *cls,
503 struct MeshConnection *c, uint16_t type,
504 int fwd, size_t size,
505 struct GNUNET_TIME_Relative wait)
507 struct MeshConnectionPerformance *p;
508 struct MeshFlowControl *fc;
509 struct MeshConnectionQueue *q = cls;
512 fc = fwd ? &c->fwd_fc : &c->bck_fc;
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
516 GNUNET_MESH_DEBUG_M2S (type));
517 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
522 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
523 q->cont (q->cont_cls, c, q, type, fwd, size);
527 c->pending_messages--;
528 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
530 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
534 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
537 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
539 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
541 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 "! accounting pid %u\n",
544 GMC_send_ack (c, fwd, GNUNET_NO);
547 case GNUNET_MESSAGE_TYPE_MESH_POLL:
551 case GNUNET_MESSAGE_TYPE_MESH_ACK:
558 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
561 return; /* Only endpoints are interested in timing. */
564 usecsperbyte = ((double) wait.rel_value_us) / size;
565 if (p->size == AVG_MSGS)
567 /* Array is full. Substract oldest value, add new one and store. */
568 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
569 p->usecsperbyte[p->idx] = usecsperbyte;
570 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
574 /* Array not yet full. Add current value to avg and store. */
575 p->usecsperbyte[p->idx] = usecsperbyte;
577 p->avg += p->usecsperbyte[p->idx];
581 p->idx = (p->idx + 1) % AVG_MSGS;
586 * Get the previous hop in a connection
588 * @param c Connection.
590 * @return Previous peer in the connection.
592 static struct MeshPeer *
593 get_prev_hop (const struct MeshConnection *c)
597 LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos);
598 if (0 == c->own_pos || c->path->length < 2)
599 id = c->path->peers[0];
601 id = c->path->peers[c->own_pos - 1];
603 return GMP_get_short (id);
608 * Get the next hop in a connection
610 * @param c Connection.
612 * @return Next peer in the connection.
614 static struct MeshPeer *
615 get_next_hop (const struct MeshConnection *c)
619 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
620 id = c->path->peers[c->path->length - 1];
622 id = c->path->peers[c->own_pos + 1];
624 return GMP_get_short (id);
629 * Get the hop in a connection.
631 * @param c Connection.
632 * @param fwd Next hop?
634 * @return Next peer in the connection.
636 static struct MeshPeer *
637 get_hop (struct MeshConnection *c, int fwd)
640 return get_next_hop (c);
641 return get_prev_hop (c);
646 * Is traffic coming from this sender 'FWD' traffic?
648 * @param c Connection to check.
649 * @param sender Peer identity of neighbor.
651 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
652 * the traffic is 'FWD'.
653 * #GNUNET_NO for BCK.
654 * #GNUNET_SYSERR for errors.
657 is_fwd (const struct MeshConnection *c,
658 const struct GNUNET_PeerIdentity *sender)
662 id = GNUNET_PEER_search (sender);
663 if (GMP_get_short_id (get_prev_hop (c)) == id)
666 if (GMP_get_short_id (get_next_hop (c)) == id)
670 return GNUNET_SYSERR;
675 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
676 * or a first CONNECTION_ACK directed to us.
678 * @param connection Connection to confirm.
679 * @param fwd Should we send it FWD? (root->dest)
680 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
683 send_connection_ack (struct MeshConnection *connection, int fwd)
685 struct MeshTunnel3 *t;
688 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
689 !fwd ? "FWD" : "BCK");
690 GMP_queue_add (get_hop (connection, fwd), NULL,
691 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
692 sizeof (struct GNUNET_MESH_ConnectionACK),
693 connection, fwd, &message_sent, NULL);
694 connection->pending_messages++;
695 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
696 GMT_change_state (t, MESH_TUNNEL3_WAITING);
697 if (MESH_CONNECTION_READY != connection->state)
698 connection_change_state (connection, MESH_CONNECTION_SENT);
703 * Send a notification that a connection is broken.
705 * @param c Connection that is broken.
706 * @param id1 Peer that has disconnected.
707 * @param id2 Peer that has disconnected.
708 * @param fwd Direction towards which to send it.
711 send_broken (struct MeshConnection *c,
712 const struct GNUNET_PeerIdentity *id1,
713 const struct GNUNET_PeerIdentity *id2,
716 struct GNUNET_MESH_ConnectionBroken msg;
718 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
719 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
723 GMC_send_prebuilt_message (&msg.header, c, fwd, NULL, NULL);
729 * Send keepalive packets for a connection.
731 * @param c Connection to keep alive..
732 * @param fwd Is this a FWD keepalive? (owner -> dest).
735 connection_keepalive (struct MeshConnection *c, int fwd)
737 struct GNUNET_MESH_ConnectionKeepAlive *msg;
738 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
742 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
743 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
745 LOG (GNUNET_ERROR_TYPE_DEBUG,
746 "sending %s keepalive for connection %s]\n",
747 fwd ? "FWD" : "BCK", GMC_2s (c));
749 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
750 msg->header.size = htons (size);
751 msg->header.type = htons (type);
754 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
759 * Send CONNECTION_{CREATE/ACK} packets for a connection.
761 * @param c Connection for which to send the message.
762 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
765 connection_recreate (struct MeshConnection *c, int fwd)
767 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
771 send_connection_ack (c, GNUNET_NO);
776 * Generic connection timer management.
777 * Depending on the role of the peer in the connection will send the
778 * appropriate message (build or keepalive)
780 * @param c Conncetion to maintain.
784 connection_maintain (struct MeshConnection *c, int fwd)
786 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
788 /* TODO DHT GET with RO_BART */
793 case MESH_CONNECTION_NEW:
795 case MESH_CONNECTION_SENT:
796 connection_recreate (c, fwd);
798 case MESH_CONNECTION_READY:
799 connection_keepalive (c, fwd);
808 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
810 struct MeshConnection *c = cls;
811 struct GNUNET_TIME_Relative delay;
813 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
814 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
817 connection_maintain (c, GNUNET_YES);
818 delay = c->state == MESH_CONNECTION_READY ?
819 refresh_connection_time : create_connection_time;
820 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
821 &connection_fwd_keepalive,
827 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
829 struct MeshConnection *c = cls;
830 struct GNUNET_TIME_Relative delay;
832 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
833 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
836 connection_maintain (c, GNUNET_NO);
837 delay = c->state == MESH_CONNECTION_READY ?
838 refresh_connection_time : create_connection_time;
839 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
840 &connection_bck_keepalive,
846 * @brief Re-initiate traffic on this connection if necessary.
848 * Check if there is traffic queued towards this peer
849 * and the core transmit handle is NULL (traffic was stalled).
850 * If so, call core tmt rdy.
852 * @param c Connection on which initiate traffic.
853 * @param fwd Is this about fwd traffic?
856 connection_unlock_queue (struct MeshConnection *c, int fwd)
858 struct MeshPeer *peer;
860 LOG (GNUNET_ERROR_TYPE_DEBUG,
861 "connection_unlock_queue %s on %s\n",
862 fwd ? "FWD" : "BCK", GMC_2s (c));
864 if (GMC_is_terminal (c, fwd))
866 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
870 peer = get_hop (c, fwd);
871 GMP_queue_unlock (peer, c);
876 * Cancel all transmissions that belong to a certain connection.
878 * If the connection is scheduled for destruction and no more messages are left,
879 * the connection will be destroyed by the continuation call.
881 * @param c Connection which to cancel. Might be destroyed during this call.
882 * @param fwd Cancel fwd traffic?
885 connection_cancel_queues (struct MeshConnection *c, int fwd)
887 struct MeshFlowControl *fc;
888 struct MeshPeer *peer;
896 fc = fwd ? &c->fwd_fc : &c->bck_fc;
897 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
899 GNUNET_SCHEDULER_cancel (fc->poll_task);
900 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
902 peer = get_hop (c, fwd);
903 GMP_queue_cancel (peer, c);
908 * Function called if a connection has been stalled for a while,
909 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
911 * @param cls Closure (poll ctx).
912 * @param tc TaskContext.
915 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
919 * Callback called when a queued POLL message is sent.
921 * @param cls Closure (FC).
922 * @param c Connection this message was on.
923 * @param q Queue handler this call invalidates.
924 * @param type Type of message sent.
925 * @param fwd Was this a FWD going message?
926 * @param size Size of the message.
929 poll_sent (void *cls,
930 struct MeshConnection *c,
931 struct MeshConnectionQueue *q,
932 uint16_t type, int fwd, size_t size)
934 struct MeshFlowControl *fc = cls;
936 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL sent, scheduling new one!\n");
938 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
939 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
940 &connection_poll, fc);
944 * Function called if a connection has been stalled for a while,
945 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
947 * @param cls Closure (poll ctx).
948 * @param tc TaskContext.
951 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
953 struct MeshFlowControl *fc = cls;
954 struct GNUNET_MESH_Poll msg;
955 struct MeshConnection *c;
957 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
958 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
964 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
965 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
966 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
967 fc == &c->fwd_fc ? "FWD" : "BCK");
969 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
970 msg.header.size = htons (sizeof (msg));
971 msg.pid = htonl (fc->last_pid_sent);
972 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
973 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c, fc == &c->fwd_fc,
979 * Timeout function due to lack of keepalive/traffic from the owner.
980 * Destroys connection if called.
982 * @param cls Closure (connection to destroy).
983 * @param tc TaskContext.
986 connection_fwd_timeout (void *cls,
987 const struct GNUNET_SCHEDULER_TaskContext *tc)
989 struct MeshConnection *c = cls;
991 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
992 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
994 LOG (GNUNET_ERROR_TYPE_DEBUG,
995 "Connection %s[%X] FWD timed out. Destroying.\n",
999 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1007 * Timeout function due to lack of keepalive/traffic from the destination.
1008 * Destroys connection if called.
1010 * @param cls Closure (connection to destroy).
1011 * @param tc TaskContext
1014 connection_bck_timeout (void *cls,
1015 const struct GNUNET_SCHEDULER_TaskContext *tc)
1017 struct MeshConnection *c = cls;
1019 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1020 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1023 LOG (GNUNET_ERROR_TYPE_DEBUG,
1024 "Connection %s[%X] FWD timed out. Destroying.\n",
1025 GMT_2s (c->t), c->id);
1027 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1035 * Resets the connection timeout task, some other message has done the
1037 * - For the first peer on the direction this means to send
1038 * a keepalive or a path confirmation message (either create or ACK).
1039 * - For all other peers, this means to destroy the connection,
1040 * due to lack of activity.
1041 * Starts the tiemout if no timeout was running (connection just created).
1043 * @param c Connection whose timeout to reset.
1044 * @param fwd Is this forward?
1046 * TODO use heap to improve efficiency of scheduler.
1049 connection_reset_timeout (struct MeshConnection *c, int fwd)
1051 GNUNET_SCHEDULER_TaskIdentifier *ti;
1052 GNUNET_SCHEDULER_Task f;
1054 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1056 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1057 GNUNET_SCHEDULER_cancel (*ti);
1059 if (GMC_is_origin (c, fwd)) /* Startpoint */
1061 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1062 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1064 else /* Relay, endpoint. */
1066 struct GNUNET_TIME_Relative delay;
1068 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1069 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1070 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1076 * Add the connection to the list of both neighbors.
1078 * @param c Connection.
1081 register_neighbors (struct MeshConnection *c)
1083 struct MeshPeer *peer;
1085 peer = get_next_hop (c);
1086 if (GNUNET_NO == GMP_is_neighbor (peer))
1091 GMP_add_connection (peer, c);
1092 peer = get_prev_hop (c);
1093 if (GNUNET_NO == GMP_is_neighbor (peer))
1098 GMP_add_connection (peer, c);
1103 * Remove the connection from the list of both neighbors.
1105 * @param c Connection.
1108 unregister_neighbors (struct MeshConnection *c)
1110 struct MeshPeer *peer;
1112 peer = get_next_hop (c);
1113 GMP_remove_connection (peer, c);
1115 peer = get_prev_hop (c);
1116 GMP_remove_connection (peer, c);
1122 * Bind the connection to the peer and the tunnel to that peer.
1124 * If the peer has no tunnel, create one. Update tunnel and connection
1125 * data structres to reflect new status.
1127 * @param c Connection.
1131 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1133 GMP_add_tunnel (peer);
1134 c->t = GMP_get_tunnel (peer);
1135 GMT_add_connection (c->t, c);
1138 /******************************************************************************/
1139 /******************************** API ***********************************/
1140 /******************************************************************************/
1143 * Core handler for connection creation.
1145 * @param cls Closure (unused).
1146 * @param peer Sender (neighbor).
1147 * @param message Message.
1149 * @return GNUNET_OK to keep the connection open,
1150 * GNUNET_SYSERR to close it (signal serious error)
1153 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1154 const struct GNUNET_MessageHeader *message)
1156 struct GNUNET_MESH_ConnectionCreate *msg;
1157 struct GNUNET_PeerIdentity *id;
1158 struct GNUNET_HashCode *cid;
1159 struct MeshPeerPath *path;
1160 struct MeshPeer *dest_peer;
1161 struct MeshPeer *orig_peer;
1162 struct MeshConnection *c;
1163 unsigned int own_pos;
1167 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1168 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1171 size = ntohs (message->size);
1172 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1174 GNUNET_break_op (0);
1178 /* Calculate hops */
1179 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1180 if (size % sizeof (struct GNUNET_PeerIdentity))
1182 GNUNET_break_op (0);
1185 size /= sizeof (struct GNUNET_PeerIdentity);
1188 GNUNET_break_op (0);
1191 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1193 /* Get parameters */
1194 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1196 id = (struct GNUNET_PeerIdentity *) &msg[1];
1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 " connection %s (%s).\n",
1199 GNUNET_h2s (cid), GNUNET_i2s (id));
1201 /* Create connection */
1202 c = connection_get (cid);
1206 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1207 path = path_new (size);
1209 for (i = 0; i < size; i++)
1211 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1212 GNUNET_i2s (&id[i]));
1213 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1214 if (path->peers[i] == myid)
1217 if (own_pos == 0 && path->peers[own_pos] != myid)
1219 /* create path: self not found in path through self */
1220 GNUNET_break_op (0);
1221 path_destroy (path);
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1225 GMP_add_path_to_all (path, GNUNET_NO);
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1227 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1230 connection_reset_timeout (c, GNUNET_YES);
1236 if (MESH_CONNECTION_NEW == c->state)
1237 connection_change_state (c, MESH_CONNECTION_SENT);
1239 /* Remember peers */
1240 dest_peer = GMP_get (&id[size - 1]);
1241 orig_peer = GMP_get (&id[0]);
1243 /* Is it a connection to us? */
1244 if (c->own_pos == size - 1)
1246 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1247 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1249 add_to_peer (c, orig_peer);
1250 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1251 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1253 send_connection_ack (c, GNUNET_NO);
1254 if (MESH_CONNECTION_SENT == c->state)
1255 connection_change_state (c, MESH_CONNECTION_ACK);
1257 /* Keep tunnel alive in direction dest->owner*/
1258 c->bck_maintenance_task =
1259 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1260 &connection_bck_keepalive, c);
1264 /* It's for somebody else! Retransmit. */
1265 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1266 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1267 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1268 GMC_send_prebuilt_message (message, c, GNUNET_YES, NULL, NULL);
1275 * Core handler for path confirmations.
1277 * @param cls closure
1278 * @param message message
1279 * @param peer peer identity this notification is about
1281 * @return GNUNET_OK to keep the connection open,
1282 * GNUNET_SYSERR to close it (signal serious error)
1285 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1286 const struct GNUNET_MessageHeader *message)
1288 struct GNUNET_MESH_ConnectionACK *msg;
1289 struct MeshConnection *c;
1290 struct MeshPeerPath *p;
1291 struct MeshPeer *pi;
1292 enum MeshConnectionState oldstate;
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1296 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1297 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1299 GNUNET_h2s (&msg->cid));
1300 c = connection_get (&msg->cid);
1303 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1305 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1309 oldstate = c->state;
1310 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1311 pi = GMP_get (peer);
1312 if (get_next_hop (c) == pi)
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1316 if (MESH_CONNECTION_SENT == oldstate)
1317 connection_change_state (c, MESH_CONNECTION_ACK);
1319 else if (get_prev_hop (c) == pi)
1321 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1323 connection_change_state (c, MESH_CONNECTION_READY);
1327 GNUNET_break_op (0);
1331 connection_reset_timeout (c, fwd);
1333 /* Add path to peers? */
1337 GMP_add_path_to_all (p, GNUNET_YES);
1344 /* Message for us as creator? */
1345 if (GMC_is_origin (c, GNUNET_YES))
1347 if (GNUNET_NO != fwd)
1349 GNUNET_break_op (0);
1352 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1354 /* If just created, cancel the short timeout and start a long one */
1355 if (MESH_CONNECTION_SENT == oldstate)
1356 connection_reset_timeout (c, GNUNET_YES);
1358 /* Change tunnel state */
1359 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1360 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1362 /* Send ACK (~TCP ACK)*/
1363 send_connection_ack (c, GNUNET_YES);
1366 /* Message for us as destination? */
1367 if (GMC_is_terminal (c, GNUNET_YES))
1369 if (GNUNET_YES != fwd)
1371 GNUNET_break_op (0);
1374 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1376 /* If just created, cancel the short timeout and start a long one */
1377 if (MESH_CONNECTION_ACK == oldstate)
1378 connection_reset_timeout (c, GNUNET_NO);
1380 /* Change tunnel state */
1381 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1382 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1387 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1388 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1394 * Core handler for notifications of broken paths
1396 * @param cls Closure (unused).
1397 * @param id Peer identity of sending neighbor.
1398 * @param message Message.
1400 * @return GNUNET_OK to keep the connection open,
1401 * GNUNET_SYSERR to close it (signal serious error)
1404 GMC_handle_broken (void* cls,
1405 const struct GNUNET_PeerIdentity* id,
1406 const struct GNUNET_MessageHeader* message)
1408 struct GNUNET_MESH_ConnectionBroken *msg;
1409 struct MeshConnection *c;
1412 LOG (GNUNET_ERROR_TYPE_DEBUG,
1413 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1414 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1415 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1416 GNUNET_i2s (&msg->peer1));
1417 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1418 GNUNET_i2s (&msg->peer2));
1419 c = connection_get (&msg->cid);
1422 GNUNET_break_op (0);
1426 fwd = is_fwd (c, id);
1427 connection_cancel_queues (c, !fwd);
1428 if (GMC_is_terminal (c, fwd))
1430 if (0 < c->pending_messages)
1431 c->destroy = GNUNET_YES;
1437 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1438 c->destroy = GNUNET_YES;
1447 * Core handler for tunnel destruction
1449 * @param cls Closure (unused).
1450 * @param peer Peer identity of sending neighbor.
1451 * @param message Message.
1453 * @return GNUNET_OK to keep the connection open,
1454 * GNUNET_SYSERR to close it (signal serious error)
1457 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1458 const struct GNUNET_MessageHeader *message)
1460 struct GNUNET_MESH_ConnectionDestroy *msg;
1461 struct MeshConnection *c;
1464 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1465 LOG (GNUNET_ERROR_TYPE_DEBUG,
1466 "Got a CONNECTION DESTROY message from %s\n",
1468 LOG (GNUNET_ERROR_TYPE_DEBUG,
1469 " for connection %s\n",
1470 GNUNET_h2s (&msg->cid));
1471 c = connection_get (&msg->cid);
1474 /* Probably already got the message from another path,
1475 * destroyed the tunnel and retransmitted to children.
1478 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1482 fwd = is_fwd (c, peer);
1483 if (GNUNET_SYSERR == fwd)
1485 GNUNET_break_op (0);
1488 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1489 c->destroy = GNUNET_YES;
1495 * Generic handler for mesh network encrypted traffic.
1497 * @param peer Peer identity this notification is about.
1498 * @param msg Encrypted message.
1500 * @return GNUNET_OK to keep the connection open,
1501 * GNUNET_SYSERR to close it (signal serious error)
1504 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1505 const struct GNUNET_MESH_Encrypted *msg)
1507 struct MeshConnection *c;
1508 struct MeshPeer *neighbor;
1509 struct MeshFlowControl *fc;
1510 GNUNET_PEER_Id peer_id;
1518 size = ntohs (msg->header.size);
1520 sizeof (struct GNUNET_MESH_Encrypted) +
1521 sizeof (struct GNUNET_MessageHeader))
1523 GNUNET_break_op (0);
1526 type = ntohs (msg->header.type);
1527 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1529 GNUNET_MESH_DEBUG_M2S (type), ntohl (msg->pid), GNUNET_i2s (peer));
1531 /* Check connection */
1532 c = connection_get (&msg->cid);
1535 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1536 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1540 /* Check if origin is as expected */
1541 neighbor = get_prev_hop (c);
1542 peer_id = GNUNET_PEER_search (peer);
1543 if (peer_id == GMP_get_short_id (neighbor))
1549 neighbor = get_next_hop (c);
1550 if (peer_id == GMP_get_short_id (neighbor))
1556 /* Unexpected peer sending traffic on a connection. */
1557 GNUNET_break_op (0);
1563 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1564 pid = ntohl (msg->pid);
1565 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1567 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1568 LOG (GNUNET_ERROR_TYPE_DEBUG,
1569 "WARNING Received PID %u, (prev %u), ACK %u\n",
1570 pid, fc->last_pid_recv, fc->last_ack_sent);
1573 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1575 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1576 LOG (GNUNET_ERROR_TYPE_DEBUG,
1577 " Pid %u not expected (%u+), dropping!\n",
1578 pid, fc->last_pid_recv + 1);
1581 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1582 connection_change_state (c, MESH_CONNECTION_READY);
1583 connection_reset_timeout (c, fwd);
1584 fc->last_pid_recv = pid;
1586 /* Is this message for us? */
1587 if (GMC_is_terminal (c, fwd))
1589 /* TODO signature verification */
1590 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1591 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1598 fc->last_pid_recv = pid;
1599 GMT_handle_encrypted (c->t, msg);
1600 GMC_send_ack (c, fwd, GNUNET_NO);
1604 /* Message not for us: forward to next hop */
1605 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1606 ttl = ntohl (msg->ttl);
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1610 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1611 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1612 GMC_send_ack (c, fwd, GNUNET_NO);
1616 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1617 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
1623 * Generic handler for mesh network encrypted traffic.
1625 * @param peer Peer identity this notification is about.
1626 * @param msg Encrypted message.
1628 * @return GNUNET_OK to keep the connection open,
1629 * GNUNET_SYSERR to close it (signal serious error)
1632 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1633 const struct GNUNET_MESH_KX *msg)
1635 struct MeshConnection *c;
1636 struct MeshPeer *neighbor;
1637 GNUNET_PEER_Id peer_id;
1643 size = ntohs (msg->header.size);
1645 sizeof (struct GNUNET_MESH_Encrypted) +
1646 sizeof (struct GNUNET_MessageHeader))
1648 GNUNET_break_op (0);
1651 type = ntohs (msg->header.type);
1652 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1653 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1654 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1656 /* Check connection */
1657 c = connection_get (&msg->cid);
1660 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1661 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1665 /* Check if origin is as expected */
1666 neighbor = get_prev_hop (c);
1667 peer_id = GNUNET_PEER_search (peer);
1668 if (peer_id == GMP_get_short_id (neighbor))
1674 neighbor = get_next_hop (c);
1675 if (peer_id == GMP_get_short_id (neighbor))
1681 /* Unexpected peer sending traffic on a connection. */
1682 GNUNET_break_op (0);
1687 /* Count as connection confirmation. */
1688 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1689 connection_change_state (c, MESH_CONNECTION_READY);
1690 connection_reset_timeout (c, fwd);
1693 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1694 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1697 /* Is this message for us? */
1698 if (GMC_is_terminal (c, fwd))
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1701 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1707 GMT_handle_kx (c->t, &msg[1].header);
1711 /* Message not for us: forward to next hop */
1712 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1713 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1714 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
1721 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1723 * @param cls Closure (unused).
1724 * @param message Message received.
1725 * @param peer Peer who sent the message.
1727 * @return GNUNET_OK to keep the connection open,
1728 * GNUNET_SYSERR to close it (signal serious error)
1731 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1732 const struct GNUNET_MessageHeader *message)
1734 return handle_mesh_encrypted (peer,
1735 (struct GNUNET_MESH_Encrypted *)message);
1740 * Core handler for key exchange traffic (ephemeral key, ping, pong).
1742 * @param cls Closure (unused).
1743 * @param message Message received.
1744 * @param peer Peer who sent the message.
1746 * @return GNUNET_OK to keep the connection open,
1747 * GNUNET_SYSERR to close it (signal serious error)
1750 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1751 const struct GNUNET_MessageHeader *message)
1753 return handle_mesh_kx (peer,
1754 (struct GNUNET_MESH_KX *) message);
1759 * Core handler for mesh network traffic point-to-point acks.
1761 * @param cls closure
1762 * @param message message
1763 * @param peer peer identity this notification is about
1765 * @return GNUNET_OK to keep the connection open,
1766 * GNUNET_SYSERR to close it (signal serious error)
1769 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1770 const struct GNUNET_MessageHeader *message)
1772 struct GNUNET_MESH_ACK *msg;
1773 struct MeshConnection *c;
1774 struct MeshFlowControl *fc;
1779 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1780 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1782 msg = (struct GNUNET_MESH_ACK *) message;
1784 c = connection_get (&msg->cid);
1788 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1793 /* Is this a forward or backward ACK? */
1794 id = GNUNET_PEER_search (peer);
1795 if (GMP_get_short_id (get_next_hop (c)) == id)
1797 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1801 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1803 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1809 GNUNET_break_op (0);
1813 ack = ntohl (msg->ack);
1814 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1815 ack, fc->last_ack_recv);
1816 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1817 fc->last_ack_recv = ack;
1819 /* Cancel polling if the ACK is big enough. */
1820 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1821 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1823 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1824 GNUNET_SCHEDULER_cancel (fc->poll_task);
1825 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1826 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1829 connection_unlock_queue (c, fwd);
1836 * Core handler for mesh network traffic point-to-point ack polls.
1838 * @param cls closure
1839 * @param message message
1840 * @param peer peer identity this notification is about
1842 * @return GNUNET_OK to keep the connection open,
1843 * GNUNET_SYSERR to close it (signal serious error)
1846 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1847 const struct GNUNET_MessageHeader *message)
1849 struct GNUNET_MESH_Poll *msg;
1850 struct MeshConnection *c;
1851 struct MeshFlowControl *fc;
1856 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1857 LOG (GNUNET_ERROR_TYPE_DEBUG,
1858 "Got a POLL packet from %s!\n",
1861 msg = (struct GNUNET_MESH_Poll *) message;
1863 c = connection_get (&msg->cid);
1867 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1869 GNUNET_break_op (0);
1873 /* Is this a forward or backward ACK?
1874 * Note: a poll should never be needed in a loopback case,
1875 * since there is no possiblility of packet loss there, so
1876 * this way of discerining FWD/BCK should not be a problem.
1878 id = GNUNET_PEER_search (peer);
1879 if (GMP_get_short_id (get_next_hop (c)) == id)
1881 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
1884 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1886 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
1891 GNUNET_break_op (0);
1895 pid = ntohl (msg->pid);
1896 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
1897 fc->last_pid_recv = pid;
1898 fwd = fc == &c->bck_fc;
1899 GMC_send_ack (c, fwd, GNUNET_YES);
1906 * Core handler for mesh keepalives.
1908 * @param cls closure
1909 * @param message message
1910 * @param peer peer identity this notification is about
1911 * @return GNUNET_OK to keep the connection open,
1912 * GNUNET_SYSERR to close it (signal serious error)
1914 * TODO: Check who we got this from, to validate route.
1917 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1918 const struct GNUNET_MessageHeader *message)
1920 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1921 struct MeshConnection *c;
1922 struct MeshPeer *neighbor;
1925 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1926 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1929 c = connection_get (&msg->cid);
1932 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1937 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1938 GNUNET_YES : GNUNET_NO;
1940 /* Check if origin is as expected */
1941 neighbor = get_hop (c, fwd);
1942 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1944 GNUNET_break_op (0);
1948 connection_change_state (c, MESH_CONNECTION_READY);
1949 connection_reset_timeout (c, fwd);
1951 if (GMC_is_terminal (c, fwd))
1954 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1955 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1962 * Send an ACK on the appropriate connection/channel, depending on
1963 * the direction and the position of the peer.
1965 * @param c Which connection to send the hop-by-hop ACK.
1966 * @param fwd Is this a fwd ACK? (will go dest->root).
1967 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
1970 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
1972 unsigned int buffer;
1974 LOG (GNUNET_ERROR_TYPE_DEBUG,
1975 "GMC send %s ACK on %s\n",
1976 fwd ? "FWD" : "BCK", GMC_2s (c));
1984 /* Get available buffer space */
1985 if (GMC_is_terminal (c, fwd))
1987 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
1988 buffer = GMT_get_channels_buffer (c->t);
1992 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1993 buffer = GMC_get_buffer (c, fwd);
1995 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1996 if (0 == buffer && GNUNET_NO == force)
1999 /* Send available buffer space */
2000 if (GMC_is_origin (c, fwd))
2002 GNUNET_assert (NULL != c->t);
2003 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2004 GMT_unchoke_channels (c->t);
2008 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2009 send_ack (c, buffer, fwd, force);
2015 * Initialize the connections subsystem
2017 * @param c Configuration handle.
2020 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2022 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2024 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2027 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2028 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2029 GNUNET_SCHEDULER_shutdown ();
2034 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2037 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2038 "MESH", "MAX_CONNECTIONS", "MISSING");
2039 GNUNET_SCHEDULER_shutdown ();
2044 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2045 &refresh_connection_time))
2047 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2048 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2049 GNUNET_SCHEDULER_shutdown ();
2052 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2053 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2057 * Shut down the connections subsystem.
2062 GNUNET_CONTAINER_multihashmap_destroy (connections);
2066 struct MeshConnection *
2067 GMC_new (const struct GNUNET_HashCode *cid,
2068 struct MeshTunnel3 *t,
2069 struct MeshPeerPath *p,
2070 unsigned int own_pos)
2072 struct MeshConnection *c;
2074 c = GNUNET_new (struct MeshConnection);
2076 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
2077 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2078 fc_init (&c->fwd_fc);
2079 fc_init (&c->bck_fc);
2084 if (own_pos > p->length - 1)
2090 c->own_pos = own_pos;
2095 c->fwd_maintenance_task =
2096 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2097 &connection_fwd_keepalive, c);
2099 register_neighbors (c);
2105 GMC_destroy (struct MeshConnection *c)
2110 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2111 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2114 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2116 /* Cancel all traffic */
2117 connection_cancel_queues (c, GNUNET_YES);
2118 connection_cancel_queues (c, GNUNET_NO);
2120 /* Cancel maintainance task (keepalive/timeout) */
2121 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2122 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2123 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2124 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2126 /* Unregister from neighbors */
2127 unregister_neighbors (c);
2130 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2132 GMT_remove_connection (c->t, c);
2134 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES))
2135 path_destroy (c->path);
2137 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c);
2143 * Get the connection ID.
2145 * @param c Connection to get the ID from.
2147 * @return ID of the connection.
2149 const struct GNUNET_HashCode *
2150 GMC_get_id (const struct MeshConnection *c)
2157 * Get the connection path.
2159 * @param c Connection to get the path from.
2161 * @return path used by the connection.
2163 const struct MeshPeerPath *
2164 GMC_get_path (const struct MeshConnection *c)
2171 * Get the connection state.
2173 * @param c Connection to get the state from.
2175 * @return state of the connection.
2177 enum MeshConnectionState
2178 GMC_get_state (const struct MeshConnection *c)
2184 * Get the connection tunnel.
2186 * @param c Connection to get the tunnel from.
2188 * @return tunnel of the connection.
2190 struct MeshTunnel3 *
2191 GMC_get_tunnel (const struct MeshConnection *c)
2198 * Get free buffer space in a connection.
2200 * @param c Connection.
2201 * @param fwd Is query about FWD traffic?
2203 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2206 GMC_get_buffer (struct MeshConnection *c, int fwd)
2208 struct MeshFlowControl *fc;
2210 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2212 return (fc->queue_max - fc->queue_n);
2216 * Get how many messages have we allowed to send to us from a direction.
2218 * @param c Connection.
2219 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2221 * @return last_ack_sent - last_pid_recv
2224 GMC_get_allowed (struct MeshConnection *c, int fwd)
2226 struct MeshFlowControl *fc;
2228 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2229 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2233 return (fc->last_ack_sent - fc->last_pid_recv);
2237 * Get messages queued in a connection.
2239 * @param c Connection.
2240 * @param fwd Is query about FWD traffic?
2242 * @return Number of messages queued.
2245 GMC_get_qn (struct MeshConnection *c, int fwd)
2247 struct MeshFlowControl *fc;
2249 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2256 * Allow the connection to advertise a buffer of the given size.
2258 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2259 * allowing up to last_pid_recv + buffer.
2261 * @param c Connection.
2262 * @param buffer How many more messages the connection can accept.
2263 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2266 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2268 send_ack (c, buffer, fwd, GNUNET_NO);
2273 * Notify other peers on a connection of a broken link. Mark connections
2274 * to destroy after all traffic has been sent.
2276 * @param c Connection on which there has been a disconnection.
2277 * @param peer Peer that disconnected.
2280 GMC_notify_broken (struct MeshConnection *c,
2281 struct MeshPeer *peer)
2285 fwd = peer == get_prev_hop (c);
2287 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2289 /* Local shutdown, no one to notify about this. */
2293 if (GNUNET_NO == c->destroy)
2294 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2296 /* Connection will have at least one pending message
2297 * (the one we just scheduled), so no point in checking whether to
2298 * destroy immediately. */
2299 c->destroy = GNUNET_YES;
2302 * Cancel all queues, if no message is left, connection will be destroyed.
2304 connection_cancel_queues (c, !fwd);
2311 * Is this peer the first one on the connection?
2313 * @param c Connection.
2314 * @param fwd Is this about fwd traffic?
2316 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2319 GMC_is_origin (struct MeshConnection *c, int fwd)
2321 if (!fwd && c->path->length - 1 == c->own_pos )
2323 if (fwd && 0 == c->own_pos)
2330 * Is this peer the last one on the connection?
2332 * @param c Connection.
2333 * @param fwd Is this about fwd traffic?
2334 * Note that the ROOT is the terminal for BCK traffic!
2336 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2339 GMC_is_terminal (struct MeshConnection *c, int fwd)
2341 return GMC_is_origin (c, !fwd);
2346 * See if we are allowed to send by the next hop in the given direction.
2348 * @param c Connection.
2349 * @param fwd Is this about fwd traffic?
2351 * @return #GNUNET_YES in case it's OK to send.
2354 GMC_is_sendable (struct MeshConnection *c, int fwd)
2356 struct MeshFlowControl *fc;
2358 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2359 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2365 * Sends an already built message on a connection, properly registering
2366 * all used resources.
2368 * @param message Message to send. Function makes a copy of it.
2369 * If message is not hop-by-hop, decrements TTL of copy.
2370 * @param c Connection on which this message is transmitted.
2371 * @param fwd Is this a fwd message?
2372 * @param cont Continuation called once message is sent. Can be NULL.
2373 * @param cont_cls Closure for @c cont.
2375 * @return Handle to cancel the message before it's sent. NULL on error.
2376 * Invalid on @c cont call.
2378 struct MeshConnectionQueue *
2379 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2380 struct MeshConnection *c, int fwd,
2381 GMC_sent cont, void *cont_cls)
2383 struct MeshFlowControl *fc;
2384 struct MeshConnectionQueue *q;
2390 size = ntohs (message->size);
2391 data = GNUNET_malloc (size);
2392 memcpy (data, message, size);
2393 type = ntohs (message->type);
2394 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2395 GNUNET_MESH_DEBUG_M2S (type), size, GMC_2s (c));
2397 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2398 droppable = GNUNET_YES;
2401 struct GNUNET_MESH_Encrypted *emsg;
2402 struct GNUNET_MESH_KX *kmsg;
2403 struct GNUNET_MESH_ACK *amsg;
2404 struct GNUNET_MESH_Poll *pmsg;
2405 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2406 struct GNUNET_MESH_ConnectionBroken *bmsg;
2409 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2410 emsg = (struct GNUNET_MESH_Encrypted *) data;
2411 ttl = ntohl (emsg->ttl);
2414 GNUNET_break_op (0);
2419 emsg->ttl = htonl (ttl - 1);
2420 emsg->pid = htonl (fc->next_pid++);
2421 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2423 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2424 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2425 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2426 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2428 GMC_start_poll (c, fwd);
2432 case GNUNET_MESSAGE_TYPE_MESH_KX:
2433 kmsg = (struct GNUNET_MESH_KX *) data;
2437 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2438 amsg = (struct GNUNET_MESH_ACK *) data;
2440 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2441 droppable = GNUNET_NO;
2444 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2445 pmsg = (struct GNUNET_MESH_Poll *) data;
2447 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2448 droppable = GNUNET_NO;
2451 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2452 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2457 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2458 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2463 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2464 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2471 if (fc->queue_n > fc->queue_max && droppable)
2473 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2476 LOG (GNUNET_ERROR_TYPE_DEBUG,
2477 "queue full: %u/%u\n",
2478 fc->queue_n, fc->queue_max);
2479 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2485 return NULL; /* Drop this message */
2488 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2489 c->pending_messages++;
2491 q = GNUNET_new (struct MeshConnectionQueue);
2492 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2502 q->cont_cls = cont_cls;
2508 * Cancel a previously sent message while it's in the queue.
2510 * ONLY can be called before the continuation given to the send function
2511 * is called. Once the continuation is called, the message is no longer in the
2514 * If the send function was given no continuation, GMC_cancel should
2515 * NOT be called, since it's not possible to determine if the message has
2516 * already been sent.
2518 * @param q Handle to the queue.
2521 GMC_cancel (struct MeshConnectionQueue *q)
2523 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2525 /* queue destroy calls message_sent, which calls q->cont and frees q */
2526 GMP_queue_destroy (q->q, GNUNET_YES);
2531 * Sends a CREATE CONNECTION message for a path to a peer.
2532 * Changes the connection and tunnel states if necessary.
2534 * @param connection Connection to create.
2537 GMC_send_create (struct MeshConnection *connection)
2539 enum MeshTunnel3State state;
2542 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2543 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2544 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2545 GMP_queue_add (get_next_hop (connection), NULL,
2546 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2547 size, connection, GNUNET_YES, &message_sent, NULL);
2548 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2549 connection, connection->pending_messages);
2550 connection->pending_messages++;
2551 state = GMT_get_state (connection->t);
2552 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2553 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2554 if (MESH_CONNECTION_NEW == connection->state)
2555 connection_change_state (connection, MESH_CONNECTION_SENT);
2560 * Send a message to all peers in this connection that the connection
2561 * is no longer valid.
2563 * If some peer should not receive the message, it should be zero'ed out
2564 * before calling this function.
2566 * @param c The connection whose peers to notify.
2569 GMC_send_destroy (struct MeshConnection *c)
2571 struct GNUNET_MESH_ConnectionDestroy msg;
2573 if (GNUNET_YES == c->destroy)
2576 msg.header.size = htons (sizeof (msg));
2577 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2579 LOG (GNUNET_ERROR_TYPE_DEBUG,
2580 " sending connection destroy for connection %s\n",
2583 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2584 GMC_send_prebuilt_message (&msg.header, c, GNUNET_YES, NULL, NULL);
2585 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2586 GMC_send_prebuilt_message (&msg.header, c, GNUNET_NO, NULL, NULL);
2587 c->destroy = GNUNET_YES;
2592 * @brief Start a polling timer for the connection.
2594 * When a neighbor does not accept more traffic on the connection it could be
2595 * caused by a simple congestion or by a lost ACK. Polling enables to check
2596 * for the lastest ACK status for a connection.
2598 * @param c Connection.
2599 * @param fwd Should we poll in the FWD direction?
2602 GMC_start_poll (struct MeshConnection *c, int fwd)
2604 struct MeshFlowControl *fc;
2606 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2607 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task && NULL != fc->poll_msg)
2611 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2618 * @brief Stop polling a connection for ACKs.
2620 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2622 * @param c Connection.
2623 * @param fwd Should we stop the poll in the FWD direction?
2626 GMC_stop_poll (struct MeshConnection *c, int fwd)
2628 struct MeshFlowControl *fc;
2630 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2631 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2633 GNUNET_SCHEDULER_cancel (fc->poll_task);
2634 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2639 * Get a (static) string for a connection.
2641 * @param c Connection.
2644 GMC_2s (struct MeshConnection *c)
2648 static char buf[128];
2650 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2653 return GNUNET_h2s (&c->id);