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 * Was this a forced message? (Do not account for it)
230 * Continuation to call once sent.
235 * Closure for @c cont.
240 /******************************************************************************/
241 /******************************* GLOBALS ***********************************/
242 /******************************************************************************/
245 * Global handle to the statistics service.
247 extern struct GNUNET_STATISTICS_Handle *stats;
250 * Local peer own ID (memory efficient handle).
252 extern GNUNET_PEER_Id myid;
255 * Local peer own ID (full value).
257 extern struct GNUNET_PeerIdentity my_full_id;
260 * Connections known, indexed by cid (MeshConnection).
262 static struct GNUNET_CONTAINER_MultiHashMap *connections;
265 * How many connections are we willing to maintain.
266 * Local connections are always allowed, even if there are more connections than max.
268 static unsigned long long max_connections;
271 * How many messages *in total* are we willing to queue, divide by number of
272 * connections to get connection queue size.
274 static unsigned long long max_msgs_queue;
277 * How often to send path keepalives. Paths timeout after 4 missed.
279 static struct GNUNET_TIME_Relative refresh_connection_time;
282 * How often to send path create / ACKs.
284 static struct GNUNET_TIME_Relative create_connection_time;
287 /******************************************************************************/
288 /******************************** STATIC ***********************************/
289 /******************************************************************************/
291 #if 0 // avoid compiler warning for unused static function
293 fc_debug (struct MeshFlowControl *fc)
295 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
296 fc->last_pid_recv, fc->last_ack_sent);
297 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
298 fc->last_pid_sent, fc->last_ack_recv);
299 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
300 fc->queue_n, fc->queue_max);
304 connection_debug (struct MeshConnection *c)
308 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
311 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
312 peer2s (c->t->peer), GMC_2s (c));
313 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
314 c->state, c->pending_messages);
315 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
316 fc_debug (&c->fwd_fc);
317 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
318 fc_debug (&c->bck_fc);
323 * Get string description for tunnel state.
325 * @param s Tunnel state.
327 * @return String representation.
330 GMC_state2s (enum MeshConnectionState s)
334 case MESH_CONNECTION_NEW:
335 return "MESH_CONNECTION_NEW";
336 case MESH_CONNECTION_SENT:
337 return "MESH_CONNECTION_SENT";
338 case MESH_CONNECTION_ACK:
339 return "MESH_CONNECTION_ACK";
340 case MESH_CONNECTION_READY:
341 return "MESH_CONNECTION_READY";
342 case MESH_CONNECTION_DESTROYED:
343 return "MESH_CONNECTION_DESTROYED";
345 return "MESH_CONNECTION_STATE_ERROR";
351 * Initialize a Flow Control structure to the initial state.
353 * @param fc Flow Control structure to initialize.
356 fc_init (struct MeshFlowControl *fc)
359 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
360 fc->last_pid_recv = (uint32_t) -1;
361 fc->last_ack_sent = (uint32_t) 0;
362 fc->last_ack_recv = (uint32_t) 0;
363 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
364 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
366 fc->queue_max = (max_msgs_queue / max_connections) + 1;
373 * @param cid Connection ID.
375 static struct MeshConnection *
376 connection_get (const struct GNUNET_HashCode *cid)
378 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
383 connection_change_state (struct MeshConnection* c,
384 enum MeshConnectionState state)
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "Connection %s state was %s\n",
388 GMC_2s (c), GMC_state2s (c->state));
389 if (MESH_CONNECTION_DESTROYED == c->state)
391 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
394 LOG (GNUNET_ERROR_TYPE_DEBUG,
395 "Connection %s state is now %s\n",
396 GMC_2s (c), GMC_state2s (state));
402 * Callback called when a queued ACK message is sent.
404 * @param cls Closure (FC).
405 * @param c Connection this message was on.
406 * @param q Queue handler this call invalidates.
407 * @param type Type of message sent.
408 * @param fwd Was this a FWD going message?
409 * @param size Size of the message.
413 struct MeshConnection *c,
414 struct MeshConnectionQueue *q,
415 uint16_t type, int fwd, size_t size)
417 struct MeshFlowControl *fc = cls;
424 * Send an ACK on the connection, informing the predecessor about
425 * the available buffer space. Should not be called in case the peer
426 * is origin (no predecessor) in the @c fwd direction.
428 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
429 * the ACK itself goes "back" (dest->root).
431 * @param c Connection on which to send the ACK.
432 * @param buffer How much space free to advertise?
433 * @param fwd Is this FWD ACK? (Going dest -> root)
434 * @param force Don't optimize out.
437 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
439 struct MeshFlowControl *next_fc;
440 struct MeshFlowControl *prev_fc;
441 struct GNUNET_MESH_ACK msg;
445 /* If origin, there is no connection to send ACKs. Wrong function! */
446 if (GMC_is_origin (c, fwd))
452 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
453 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
455 LOG (GNUNET_ERROR_TYPE_DEBUG,
456 "connection send %s ack on %s\n",
457 GM_f2s (fwd), GMC_2s (c));
459 /* Check if we need to transmit the ACK. */
460 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
461 if (3 < delta && buffer < delta && GNUNET_NO == force)
463 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
464 LOG (GNUNET_ERROR_TYPE_DEBUG,
465 " last pid recv: %u, last ack sent: %u\n",
466 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
470 /* Ok, ACK might be necessary, what PID to ACK? */
471 ack = prev_fc->last_pid_recv + buffer;
472 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
473 LOG (GNUNET_ERROR_TYPE_DEBUG,
474 " last pid %u, last ack %u, qmax %u, q %u\n",
475 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
476 next_fc->queue_max, next_fc->queue_n);
477 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
479 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
483 /* Check if message is already in queue */
484 if (NULL != prev_fc->ack_msg)
486 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
488 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
489 GMC_cancel (prev_fc->ack_msg);
490 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
494 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
499 prev_fc->last_ack_sent = ack;
501 /* Build ACK message and send on connection */
502 msg.header.size = htons (sizeof (msg));
503 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
504 msg.ack = htonl (ack);
507 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
514 * Callback called when a queued message is sent.
516 * Calculates the average time and connection packet tracking.
518 * @param cls Closure (ConnectionQueue Handle).
519 * @param c Connection this message was on.
520 * @param type Type of message sent.
521 * @param fwd Was this a FWD going message?
522 * @param size Size of the message.
523 * @param wait Time spent waiting for core (only the time for THIS message)
526 message_sent (void *cls,
527 struct MeshConnection *c, uint16_t type,
528 int fwd, size_t size,
529 struct GNUNET_TIME_Relative wait)
531 struct MeshConnectionPerformance *p;
532 struct MeshFlowControl *fc;
533 struct MeshConnectionQueue *q = cls;
537 fc = fwd ? &c->fwd_fc : &c->bck_fc;
538 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
548 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
549 q->cont (q->cont_cls, c, q, type, fwd, size);
553 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
555 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
562 c->pending_messages--;
563 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
565 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
569 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
572 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
574 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
575 if (GNUNET_NO == forced)
578 LOG (GNUNET_ERROR_TYPE_DEBUG,
579 "! accounting pid %u\n",
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
585 "! forced, Q_N not accounting pid %u\n",
588 GMC_send_ack (c, fwd, GNUNET_NO);
591 case GNUNET_MESSAGE_TYPE_MESH_POLL:
595 case GNUNET_MESSAGE_TYPE_MESH_ACK:
602 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
605 return; /* Only endpoints are interested in timing. */
608 usecsperbyte = ((double) wait.rel_value_us) / size;
609 if (p->size == AVG_MSGS)
611 /* Array is full. Substract oldest value, add new one and store. */
612 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
613 p->usecsperbyte[p->idx] = usecsperbyte;
614 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
618 /* Array not yet full. Add current value to avg and store. */
619 p->usecsperbyte[p->idx] = usecsperbyte;
621 p->avg += p->usecsperbyte[p->idx];
625 p->idx = (p->idx + 1) % AVG_MSGS;
630 * Get the previous hop in a connection
632 * @param c Connection.
634 * @return Previous peer in the connection.
636 static struct MeshPeer *
637 get_prev_hop (const struct MeshConnection *c)
641 LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos);
642 if (0 == c->own_pos || c->path->length < 2)
643 id = c->path->peers[0];
645 id = c->path->peers[c->own_pos - 1];
647 return GMP_get_short (id);
652 * Get the next hop in a connection
654 * @param c Connection.
656 * @return Next peer in the connection.
658 static struct MeshPeer *
659 get_next_hop (const struct MeshConnection *c)
663 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
664 id = c->path->peers[c->path->length - 1];
666 id = c->path->peers[c->own_pos + 1];
668 return GMP_get_short (id);
673 * Get the hop in a connection.
675 * @param c Connection.
676 * @param fwd Next hop?
678 * @return Next peer in the connection.
680 static struct MeshPeer *
681 get_hop (struct MeshConnection *c, int fwd)
684 return get_next_hop (c);
685 return get_prev_hop (c);
690 * Is traffic coming from this sender 'FWD' traffic?
692 * @param c Connection to check.
693 * @param sender Peer identity of neighbor.
695 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
696 * the traffic is 'FWD'.
697 * #GNUNET_NO for BCK.
698 * #GNUNET_SYSERR for errors.
701 is_fwd (const struct MeshConnection *c,
702 const struct GNUNET_PeerIdentity *sender)
706 id = GNUNET_PEER_search (sender);
707 if (GMP_get_short_id (get_prev_hop (c)) == id)
710 if (GMP_get_short_id (get_next_hop (c)) == id)
714 return GNUNET_SYSERR;
719 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
720 * or a first CONNECTION_ACK directed to us.
722 * @param connection Connection to confirm.
723 * @param fwd Should we send it FWD? (root->dest)
724 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
727 send_connection_ack (struct MeshConnection *connection, int fwd)
729 struct MeshTunnel3 *t;
732 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
734 GMP_queue_add (get_hop (connection, fwd), NULL,
735 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
736 sizeof (struct GNUNET_MESH_ConnectionACK),
737 connection, fwd, &message_sent, NULL);
738 connection->pending_messages++;
739 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
740 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
741 if (MESH_CONNECTION_READY != connection->state)
742 connection_change_state (connection, MESH_CONNECTION_SENT);
747 * Send a notification that a connection is broken.
749 * @param c Connection that is broken.
750 * @param id1 Peer that has disconnected.
751 * @param id2 Peer that has disconnected.
752 * @param fwd Direction towards which to send it.
755 send_broken (struct MeshConnection *c,
756 const struct GNUNET_PeerIdentity *id1,
757 const struct GNUNET_PeerIdentity *id2,
760 struct GNUNET_MESH_ConnectionBroken msg;
762 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
763 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
767 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
772 * Send keepalive packets for a connection.
774 * @param c Connection to keep alive..
775 * @param fwd Is this a FWD keepalive? (owner -> dest).
778 connection_keepalive (struct MeshConnection *c, int fwd)
780 struct GNUNET_MESH_ConnectionKeepAlive *msg;
781 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
785 "sending %s keepalive for connection %s]\n",
786 GM_f2s (fwd), GMC_2s (c));
788 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
789 msg->header.size = htons (size);
790 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
792 msg->reserved = htonl (0);
794 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
799 * Send CONNECTION_{CREATE/ACK} packets for a connection.
801 * @param c Connection for which to send the message.
802 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
805 connection_recreate (struct MeshConnection *c, int fwd)
807 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
811 send_connection_ack (c, GNUNET_NO);
816 * Generic connection timer management.
817 * Depending on the role of the peer in the connection will send the
818 * appropriate message (build or keepalive)
820 * @param c Conncetion to maintain.
824 connection_maintain (struct MeshConnection *c, int fwd)
826 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
828 /* TODO DHT GET with RO_BART */
833 case MESH_CONNECTION_NEW:
836 case MESH_CONNECTION_SENT:
837 connection_recreate (c, fwd);
839 case MESH_CONNECTION_READY:
840 connection_keepalive (c, fwd);
849 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
851 struct MeshConnection *c = cls;
852 struct GNUNET_TIME_Relative delay;
854 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
855 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
858 connection_maintain (c, GNUNET_YES);
859 delay = c->state == MESH_CONNECTION_READY ?
860 refresh_connection_time : create_connection_time;
861 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
862 &connection_fwd_keepalive,
868 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
870 struct MeshConnection *c = cls;
871 struct GNUNET_TIME_Relative delay;
873 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
874 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
877 connection_maintain (c, GNUNET_NO);
878 delay = c->state == MESH_CONNECTION_READY ?
879 refresh_connection_time : create_connection_time;
880 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
881 &connection_bck_keepalive,
887 * @brief Re-initiate traffic on this connection if necessary.
889 * Check if there is traffic queued towards this peer
890 * and the core transmit handle is NULL (traffic was stalled).
891 * If so, call core tmt rdy.
893 * @param c Connection on which initiate traffic.
894 * @param fwd Is this about fwd traffic?
897 connection_unlock_queue (struct MeshConnection *c, int fwd)
899 struct MeshPeer *peer;
901 LOG (GNUNET_ERROR_TYPE_DEBUG,
902 "connection_unlock_queue %s on %s\n",
903 GM_f2s (fwd), GMC_2s (c));
905 if (GMC_is_terminal (c, fwd))
907 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
911 peer = get_hop (c, fwd);
912 GMP_queue_unlock (peer, c);
917 * Cancel all transmissions that belong to a certain connection.
919 * If the connection is scheduled for destruction and no more messages are left,
920 * the connection will be destroyed by the continuation call.
922 * @param c Connection which to cancel. Might be destroyed during this call.
923 * @param fwd Cancel fwd traffic?
926 connection_cancel_queues (struct MeshConnection *c, int fwd)
928 struct MeshFlowControl *fc;
929 struct MeshPeer *peer;
931 LOG (GNUNET_ERROR_TYPE_DEBUG,
932 " *** Cancel %s queues for connection %s\n",
933 GM_f2s (fwd), GMC_2s (c));
940 fc = fwd ? &c->fwd_fc : &c->bck_fc;
941 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
943 GNUNET_SCHEDULER_cancel (fc->poll_task);
944 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
945 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
947 peer = get_hop (c, fwd);
948 GMP_queue_cancel (peer, c);
953 * Function called if a connection has been stalled for a while,
954 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
956 * @param cls Closure (poll ctx).
957 * @param tc TaskContext.
960 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
964 * Callback called when a queued POLL message is sent.
966 * @param cls Closure (FC).
967 * @param c Connection this message was on.
968 * @param q Queue handler this call invalidates.
969 * @param type Type of message sent.
970 * @param fwd Was this a FWD going message?
971 * @param size Size of the message.
974 poll_sent (void *cls,
975 struct MeshConnection *c,
976 struct MeshConnectionQueue *q,
977 uint16_t type, int fwd, size_t size)
979 struct MeshFlowControl *fc = cls;
983 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
986 LOG (GNUNET_ERROR_TYPE_DEBUG,
987 " *** POLL sent for , scheduling new one!\n");
989 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
990 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
991 &connection_poll, fc);
992 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
997 * Function called if a connection has been stalled for a while,
998 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1000 * @param cls Closure (poll ctx).
1001 * @param tc TaskContext.
1004 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1006 struct MeshFlowControl *fc = cls;
1007 struct GNUNET_MESH_Poll msg;
1008 struct MeshConnection *c;
1010 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1011 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1017 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1018 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1019 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1020 fc == &c->fwd_fc ? "FWD" : "BCK");
1022 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1023 msg.header.size = htons (sizeof (msg));
1024 msg.pid = htonl (fc->last_pid_sent);
1025 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1026 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1027 fc == &c->fwd_fc, GNUNET_YES,
1033 * Timeout function due to lack of keepalive/traffic from the owner.
1034 * Destroys connection if called.
1036 * @param cls Closure (connection to destroy).
1037 * @param tc TaskContext.
1040 connection_fwd_timeout (void *cls,
1041 const struct GNUNET_SCHEDULER_TaskContext *tc)
1043 struct MeshConnection *c = cls;
1045 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1046 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1048 LOG (GNUNET_ERROR_TYPE_DEBUG,
1049 "Connection %s[%X] FWD timed out. Destroying.\n",
1053 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1061 * Timeout function due to lack of keepalive/traffic from the destination.
1062 * Destroys connection if called.
1064 * @param cls Closure (connection to destroy).
1065 * @param tc TaskContext
1068 connection_bck_timeout (void *cls,
1069 const struct GNUNET_SCHEDULER_TaskContext *tc)
1071 struct MeshConnection *c = cls;
1073 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1074 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1077 LOG (GNUNET_ERROR_TYPE_DEBUG,
1078 "Connection %s[%X] FWD timed out. Destroying.\n",
1079 GMT_2s (c->t), c->id);
1081 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1089 * Resets the connection timeout task, some other message has done the
1091 * - For the first peer on the direction this means to send
1092 * a keepalive or a path confirmation message (either create or ACK).
1093 * - For all other peers, this means to destroy the connection,
1094 * due to lack of activity.
1095 * Starts the timeout if no timeout was running (connection just created).
1097 * @param c Connection whose timeout to reset.
1098 * @param fwd Is this forward?
1100 * TODO use heap to improve efficiency of scheduler.
1103 connection_reset_timeout (struct MeshConnection *c, int fwd)
1105 GNUNET_SCHEDULER_TaskIdentifier *ti;
1106 GNUNET_SCHEDULER_Task f;
1108 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1110 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1112 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1113 GNUNET_SCHEDULER_cancel (*ti);
1115 if (GMC_is_origin (c, fwd)) /* Startpoint */
1117 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1118 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1120 else /* Relay, endpoint. */
1122 struct GNUNET_TIME_Relative delay;
1124 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1125 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1126 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1132 * Add the connection to the list of both neighbors.
1134 * @param c Connection.
1136 * @return #GNUNET_OK if everything went fine
1137 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1140 register_neighbors (struct MeshConnection *c)
1142 struct MeshPeer *next_peer;
1143 struct MeshPeer *prev_peer;
1145 next_peer = get_next_hop (c);
1146 prev_peer = get_prev_hop (c);
1148 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1149 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1151 if (GMC_is_origin (c, GNUNET_YES))
1152 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1153 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1155 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1156 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, %d\n",
1157 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1158 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, %d\n",
1159 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1160 return GNUNET_SYSERR;
1163 GMP_add_connection (next_peer, c);
1164 GMP_add_connection (prev_peer, c);
1171 * Remove the connection from the list of both neighbors.
1173 * @param c Connection.
1176 unregister_neighbors (struct MeshConnection *c)
1178 struct MeshPeer *peer;
1180 peer = get_next_hop (c);
1181 if (GNUNET_OK != GMP_remove_connection (peer, c))
1182 GNUNET_break (MESH_CONNECTION_NEW == c->state);
1184 peer = get_prev_hop (c);
1185 if (GNUNET_OK != GMP_remove_connection (peer, c))
1186 GNUNET_break (MESH_CONNECTION_NEW == c->state);
1191 * Bind the connection to the peer and the tunnel to that peer.
1193 * If the peer has no tunnel, create one. Update tunnel and connection
1194 * data structres to reflect new status.
1196 * @param c Connection.
1200 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1202 GMP_add_tunnel (peer);
1203 c->t = GMP_get_tunnel (peer);
1204 GMT_add_connection (c->t, c);
1207 /******************************************************************************/
1208 /******************************** API ***********************************/
1209 /******************************************************************************/
1212 * Core handler for connection creation.
1214 * @param cls Closure (unused).
1215 * @param peer Sender (neighbor).
1216 * @param message Message.
1218 * @return GNUNET_OK to keep the connection open,
1219 * GNUNET_SYSERR to close it (signal serious error)
1222 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1223 const struct GNUNET_MessageHeader *message)
1225 struct GNUNET_MESH_ConnectionCreate *msg;
1226 struct GNUNET_PeerIdentity *id;
1227 struct GNUNET_HashCode *cid;
1228 struct MeshPeerPath *path;
1229 struct MeshPeer *dest_peer;
1230 struct MeshPeer *orig_peer;
1231 struct MeshConnection *c;
1232 unsigned int own_pos;
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1237 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1240 size = ntohs (message->size);
1241 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1243 GNUNET_break_op (0);
1247 /* Calculate hops */
1248 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1249 if (size % sizeof (struct GNUNET_PeerIdentity))
1251 GNUNET_break_op (0);
1254 size /= sizeof (struct GNUNET_PeerIdentity);
1257 GNUNET_break_op (0);
1260 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1262 /* Get parameters */
1263 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1265 id = (struct GNUNET_PeerIdentity *) &msg[1];
1266 LOG (GNUNET_ERROR_TYPE_DEBUG,
1267 " connection %s (%s).\n",
1268 GNUNET_h2s (cid), GNUNET_i2s (id));
1270 /* Create connection */
1271 c = connection_get (cid);
1275 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1276 path = path_new (size);
1278 for (i = 0; i < size; i++)
1280 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1281 GNUNET_i2s (&id[i]));
1282 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1283 if (path->peers[i] == myid)
1286 if (own_pos == 0 && path->peers[own_pos] != myid)
1288 /* create path: self not found in path through self */
1289 GNUNET_break_op (0);
1290 path_destroy (path);
1293 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1294 GMP_add_path_to_all (path, GNUNET_NO);
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1296 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1299 path_destroy (path);
1302 connection_reset_timeout (c, GNUNET_YES);
1306 path = path_duplicate (c->path);
1308 if (MESH_CONNECTION_NEW == c->state)
1309 connection_change_state (c, MESH_CONNECTION_SENT);
1311 /* Remember peers */
1312 dest_peer = GMP_get (&id[size - 1]);
1313 orig_peer = GMP_get (&id[0]);
1315 /* Is it a connection to us? */
1316 if (c->own_pos == size - 1)
1318 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1319 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1321 add_to_peer (c, orig_peer);
1322 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1323 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1325 send_connection_ack (c, GNUNET_NO);
1326 if (MESH_CONNECTION_SENT == c->state)
1327 connection_change_state (c, MESH_CONNECTION_ACK);
1329 /* Keep tunnel alive in direction dest->owner*/
1330 if (GNUNET_SCHEDULER_NO_TASK == c->bck_maintenance_task)
1332 c->bck_maintenance_task =
1333 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1334 &connection_bck_keepalive, c);
1339 /* It's for somebody else! Retransmit. */
1340 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1341 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1342 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1343 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES, NULL, NULL);
1345 path_destroy (path);
1351 * Core handler for path confirmations.
1353 * @param cls closure
1354 * @param message message
1355 * @param peer peer identity this notification is about
1357 * @return GNUNET_OK to keep the connection open,
1358 * GNUNET_SYSERR to close it (signal serious error)
1361 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1362 const struct GNUNET_MessageHeader *message)
1364 struct GNUNET_MESH_ConnectionACK *msg;
1365 struct MeshConnection *c;
1366 struct MeshPeerPath *p;
1367 struct MeshPeer *pi;
1368 enum MeshConnectionState oldstate;
1371 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1372 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1373 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1374 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1375 GNUNET_h2s (&msg->cid));
1376 c = connection_get (&msg->cid);
1379 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1381 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1385 oldstate = c->state;
1386 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1387 pi = GMP_get (peer);
1388 if (get_next_hop (c) == pi)
1390 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1392 if (MESH_CONNECTION_SENT == oldstate)
1393 connection_change_state (c, MESH_CONNECTION_ACK);
1395 else if (get_prev_hop (c) == pi)
1397 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1399 connection_change_state (c, MESH_CONNECTION_READY);
1403 GNUNET_break_op (0);
1407 connection_reset_timeout (c, fwd);
1409 /* Add path to peers? */
1413 GMP_add_path_to_all (p, GNUNET_YES);
1420 /* Message for us as creator? */
1421 if (GMC_is_origin (c, GNUNET_YES))
1423 if (GNUNET_NO != fwd)
1425 GNUNET_break_op (0);
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1430 /* If just created, cancel the short timeout and start a long one */
1431 if (MESH_CONNECTION_SENT == oldstate)
1432 connection_reset_timeout (c, GNUNET_YES);
1434 /* Change connection state */
1435 connection_change_state (c, MESH_CONNECTION_READY);
1436 send_connection_ack (c, GNUNET_YES);
1438 /* Change tunnel state, trigger KX */
1439 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1440 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1445 /* Message for us as destination? */
1446 if (GMC_is_terminal (c, GNUNET_YES))
1448 if (GNUNET_YES != fwd)
1450 GNUNET_break_op (0);
1453 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1455 /* If just created, cancel the short timeout and start a long one */
1456 if (MESH_CONNECTION_ACK == oldstate)
1457 connection_reset_timeout (c, GNUNET_NO);
1459 /* Change tunnel state */
1460 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1461 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1466 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1467 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1473 * Core handler for notifications of broken paths
1475 * @param cls Closure (unused).
1476 * @param id Peer identity of sending neighbor.
1477 * @param message Message.
1479 * @return GNUNET_OK to keep the connection open,
1480 * GNUNET_SYSERR to close it (signal serious error)
1483 GMC_handle_broken (void* cls,
1484 const struct GNUNET_PeerIdentity* id,
1485 const struct GNUNET_MessageHeader* message)
1487 struct GNUNET_MESH_ConnectionBroken *msg;
1488 struct MeshConnection *c;
1491 LOG (GNUNET_ERROR_TYPE_DEBUG,
1492 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1493 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1495 GNUNET_i2s (&msg->peer1));
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1497 GNUNET_i2s (&msg->peer2));
1498 c = connection_get (&msg->cid);
1501 GNUNET_break_op (0);
1505 fwd = is_fwd (c, id);
1506 if (GMC_is_terminal (c, fwd))
1508 if (0 < c->pending_messages)
1509 c->destroy = GNUNET_YES;
1515 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1516 c->destroy = GNUNET_YES;
1517 connection_cancel_queues (c, !fwd);
1526 * Core handler for tunnel destruction
1528 * @param cls Closure (unused).
1529 * @param peer Peer identity of sending neighbor.
1530 * @param message Message.
1532 * @return GNUNET_OK to keep the connection open,
1533 * GNUNET_SYSERR to close it (signal serious error)
1536 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1537 const struct GNUNET_MessageHeader *message)
1539 struct GNUNET_MESH_ConnectionDestroy *msg;
1540 struct MeshConnection *c;
1543 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1544 LOG (GNUNET_ERROR_TYPE_DEBUG,
1545 "Got a CONNECTION DESTROY message from %s\n",
1547 LOG (GNUNET_ERROR_TYPE_DEBUG,
1548 " for connection %s\n",
1549 GNUNET_h2s (&msg->cid));
1550 c = connection_get (&msg->cid);
1553 /* Probably already got the message from another path,
1554 * destroyed the tunnel and retransmitted to children.
1557 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1559 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1562 fwd = is_fwd (c, peer);
1563 if (GNUNET_SYSERR == fwd)
1565 GNUNET_break_op (0);
1568 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1569 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1570 else if (0 == c->pending_messages)
1572 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1576 c->destroy = GNUNET_YES;
1577 c->state = MESH_CONNECTION_DESTROYED;
1583 * Generic handler for mesh network encrypted traffic.
1585 * @param peer Peer identity this notification is about.
1586 * @param msg Encrypted message.
1588 * @return GNUNET_OK to keep the connection open,
1589 * GNUNET_SYSERR to close it (signal serious error)
1592 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1593 const struct GNUNET_MESH_Encrypted *msg)
1595 struct MeshConnection *c;
1596 struct MeshPeer *neighbor;
1597 struct MeshFlowControl *fc;
1598 GNUNET_PEER_Id peer_id;
1606 size = ntohs (msg->header.size);
1608 sizeof (struct GNUNET_MESH_Encrypted) +
1609 sizeof (struct GNUNET_MessageHeader))
1611 GNUNET_break_op (0);
1614 type = ntohs (msg->header.type);
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1616 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1617 GM_m2s (type), ntohl (msg->pid), GNUNET_i2s (peer));
1619 /* Check connection */
1620 c = connection_get (&msg->cid);
1623 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1624 LOG (GNUNET_ERROR_TYPE_DEBUG,
1625 "WARNING connection %s unknown\n",
1626 GNUNET_h2s (&msg->cid));
1630 /* Check if origin is as expected */
1631 neighbor = get_prev_hop (c);
1632 peer_id = GNUNET_PEER_search (peer);
1633 if (peer_id == GMP_get_short_id (neighbor))
1639 neighbor = get_next_hop (c);
1640 if (peer_id == GMP_get_short_id (neighbor))
1646 /* Unexpected peer sending traffic on a connection. */
1647 GNUNET_break_op (0);
1653 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1654 pid = ntohl (msg->pid);
1655 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1657 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1658 LOG (GNUNET_ERROR_TYPE_DEBUG,
1659 "WARNING Received PID %u, (prev %u), ACK %u\n",
1660 pid, fc->last_pid_recv, fc->last_ack_sent);
1663 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1665 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1666 LOG (GNUNET_ERROR_TYPE_DEBUG,
1667 " Pid %u not expected (%u+), dropping!\n",
1668 pid, fc->last_pid_recv + 1);
1671 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1672 connection_change_state (c, MESH_CONNECTION_READY);
1673 connection_reset_timeout (c, fwd);
1674 fc->last_pid_recv = pid;
1676 /* Is this message for us? */
1677 if (GMC_is_terminal (c, fwd))
1679 /* TODO signature verification */
1680 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1681 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1688 fc->last_pid_recv = pid;
1689 GMT_handle_encrypted (c->t, msg);
1690 GMC_send_ack (c, fwd, GNUNET_NO);
1694 /* Message not for us: forward to next hop */
1695 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1696 ttl = ntohl (msg->ttl);
1697 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1700 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1701 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1702 GMC_send_ack (c, fwd, GNUNET_NO);
1706 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1707 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1713 * Generic handler for mesh network encrypted traffic.
1715 * @param peer Peer identity this notification is about.
1716 * @param msg Encrypted message.
1718 * @return GNUNET_OK to keep the connection open,
1719 * GNUNET_SYSERR to close it (signal serious error)
1722 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1723 const struct GNUNET_MESH_KX *msg)
1725 struct MeshConnection *c;
1726 struct MeshPeer *neighbor;
1727 GNUNET_PEER_Id peer_id;
1733 size = ntohs (msg->header.size);
1735 sizeof (struct GNUNET_MESH_Encrypted) +
1736 sizeof (struct GNUNET_MessageHeader))
1738 GNUNET_break_op (0);
1741 type = ntohs (msg->header.type);
1742 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1743 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1744 GM_m2s (type), GNUNET_i2s (peer));
1746 /* Check connection */
1747 c = connection_get (&msg->cid);
1750 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1751 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1755 /* Check if origin is as expected */
1756 neighbor = get_prev_hop (c);
1757 peer_id = GNUNET_PEER_search (peer);
1758 if (peer_id == GMP_get_short_id (neighbor))
1764 neighbor = get_next_hop (c);
1765 if (peer_id == GMP_get_short_id (neighbor))
1771 /* Unexpected peer sending traffic on a connection. */
1772 GNUNET_break_op (0);
1777 /* Count as connection confirmation. */
1778 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1779 connection_change_state (c, MESH_CONNECTION_READY);
1780 connection_reset_timeout (c, fwd);
1783 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1784 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1787 /* Is this message for us? */
1788 if (GMC_is_terminal (c, fwd))
1790 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1791 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1797 GMT_handle_kx (c->t, &msg[1].header);
1801 /* Message not for us: forward to next hop */
1802 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1803 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1804 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1811 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1813 * @param cls Closure (unused).
1814 * @param message Message received.
1815 * @param peer Peer who sent the message.
1817 * @return GNUNET_OK to keep the connection open,
1818 * GNUNET_SYSERR to close it (signal serious error)
1821 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1822 const struct GNUNET_MessageHeader *message)
1824 return handle_mesh_encrypted (peer,
1825 (struct GNUNET_MESH_Encrypted *)message);
1830 * Core handler for key exchange traffic (ephemeral key, ping, pong).
1832 * @param cls Closure (unused).
1833 * @param message Message received.
1834 * @param peer Peer who sent the message.
1836 * @return GNUNET_OK to keep the connection open,
1837 * GNUNET_SYSERR to close it (signal serious error)
1840 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1841 const struct GNUNET_MessageHeader *message)
1843 return handle_mesh_kx (peer,
1844 (struct GNUNET_MESH_KX *) message);
1849 * Core handler for mesh network traffic point-to-point acks.
1851 * @param cls closure
1852 * @param message message
1853 * @param peer peer identity this notification is about
1855 * @return GNUNET_OK to keep the connection open,
1856 * GNUNET_SYSERR to close it (signal serious error)
1859 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1860 const struct GNUNET_MessageHeader *message)
1862 struct GNUNET_MESH_ACK *msg;
1863 struct MeshConnection *c;
1864 struct MeshFlowControl *fc;
1869 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1870 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1872 msg = (struct GNUNET_MESH_ACK *) message;
1874 c = connection_get (&msg->cid);
1878 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1883 /* Is this a forward or backward ACK? */
1884 id = GNUNET_PEER_search (peer);
1885 if (GMP_get_short_id (get_next_hop (c)) == id)
1887 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1891 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1893 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1899 GNUNET_break_op (0);
1903 ack = ntohl (msg->ack);
1904 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1905 ack, fc->last_ack_recv);
1906 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
1907 fc->last_ack_recv = ack;
1909 /* Cancel polling if the ACK is big enough. */
1910 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1911 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1913 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1914 GNUNET_SCHEDULER_cancel (fc->poll_task);
1915 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1916 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1919 connection_unlock_queue (c, fwd);
1926 * Core handler for mesh network traffic point-to-point ack polls.
1928 * @param cls closure
1929 * @param message message
1930 * @param peer peer identity this notification is about
1932 * @return GNUNET_OK to keep the connection open,
1933 * GNUNET_SYSERR to close it (signal serious error)
1936 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1937 const struct GNUNET_MessageHeader *message)
1939 struct GNUNET_MESH_Poll *msg;
1940 struct MeshConnection *c;
1941 struct MeshFlowControl *fc;
1946 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1947 LOG (GNUNET_ERROR_TYPE_DEBUG,
1948 "Got a POLL packet from %s!\n",
1951 msg = (struct GNUNET_MESH_Poll *) message;
1953 c = connection_get (&msg->cid);
1957 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1959 GNUNET_break_op (0);
1963 /* Is this a forward or backward ACK?
1964 * Note: a poll should never be needed in a loopback case,
1965 * since there is no possiblility of packet loss there, so
1966 * this way of discerining FWD/BCK should not be a problem.
1968 id = GNUNET_PEER_search (peer);
1969 if (GMP_get_short_id (get_next_hop (c)) == id)
1971 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
1974 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1976 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
1981 GNUNET_break_op (0);
1985 pid = ntohl (msg->pid);
1986 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
1987 fc->last_pid_recv = pid;
1988 fwd = fc == &c->bck_fc;
1989 GMC_send_ack (c, fwd, GNUNET_YES);
1996 * Core handler for mesh keepalives.
1998 * @param cls closure
1999 * @param message message
2000 * @param peer peer identity this notification is about
2001 * @return GNUNET_OK to keep the connection open,
2002 * GNUNET_SYSERR to close it (signal serious error)
2004 * TODO: Check who we got this from, to validate route.
2007 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2008 const struct GNUNET_MessageHeader *message)
2010 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2011 struct MeshConnection *c;
2012 struct MeshPeer *neighbor;
2013 GNUNET_PEER_Id peer_id;
2016 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2017 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
2020 c = connection_get (&msg->cid);
2023 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2028 /* Check if origin is as expected TODO refactor and reuse */
2029 peer_id = GNUNET_PEER_search (peer);
2030 neighbor = get_prev_hop (c);
2031 if (peer_id == GMP_get_short_id (neighbor))
2037 neighbor = get_next_hop (c);
2038 if (peer_id == GMP_get_short_id (neighbor))
2044 GNUNET_break_op (0);
2049 connection_change_state (c, MESH_CONNECTION_READY);
2050 connection_reset_timeout (c, fwd);
2052 if (GMC_is_terminal (c, fwd))
2055 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2056 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2063 * Send an ACK on the appropriate connection/channel, depending on
2064 * the direction and the position of the peer.
2066 * @param c Which connection to send the hop-by-hop ACK.
2067 * @param fwd Is this a fwd ACK? (will go dest->root).
2068 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2071 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2073 unsigned int buffer;
2075 LOG (GNUNET_ERROR_TYPE_DEBUG,
2076 "GMC send %s ACK on %s\n",
2077 GM_f2s (fwd), GMC_2s (c));
2085 if (GNUNET_NO != c->destroy)
2087 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2091 /* Get available buffer space */
2092 if (GMC_is_terminal (c, fwd))
2094 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2095 buffer = GMT_get_channels_buffer (c->t);
2099 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2100 buffer = GMC_get_buffer (c, fwd);
2102 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2103 if (0 == buffer && GNUNET_NO == force)
2106 /* Send available buffer space */
2107 if (GMC_is_origin (c, fwd))
2109 GNUNET_assert (NULL != c->t);
2110 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2111 GMT_unchoke_channels (c->t);
2115 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2116 send_ack (c, buffer, fwd, force);
2122 * Initialize the connections subsystem
2124 * @param c Configuration handle.
2127 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2129 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2131 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2134 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2135 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2136 GNUNET_SCHEDULER_shutdown ();
2141 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2144 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2145 "MESH", "MAX_CONNECTIONS", "MISSING");
2146 GNUNET_SCHEDULER_shutdown ();
2151 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2152 &refresh_connection_time))
2154 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2155 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2156 GNUNET_SCHEDULER_shutdown ();
2159 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2160 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2165 * Destroy each connection on shutdown.
2167 * @param cls Closure (unused).
2168 * @param key Current key code (CID, unused).
2169 * @param value Value in the hash map (connection)
2171 * @return #GNUNET_YES, because we should continue to iterate,
2174 shutdown_iterator (void *cls,
2175 const struct GNUNET_HashCode *key,
2178 struct MeshConnection *c = value;
2186 * Shut down the connections subsystem.
2191 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2192 GNUNET_CONTAINER_multihashmap_destroy (connections);
2197 struct MeshConnection *
2198 GMC_new (const struct GNUNET_HashCode *cid,
2199 struct MeshTunnel3 *t,
2200 struct MeshPeerPath *p,
2201 unsigned int own_pos)
2203 struct MeshConnection *c;
2205 c = GNUNET_new (struct MeshConnection);
2207 GNUNET_assert (GNUNET_OK ==
2208 GNUNET_CONTAINER_multihashmap_put (connections,
2210 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2211 fc_init (&c->fwd_fc);
2212 fc_init (&c->bck_fc);
2217 GNUNET_assert (own_pos <= p->length - 1);
2218 c->own_pos = own_pos;
2221 if (GNUNET_OK != register_neighbors (c))
2229 c->fwd_maintenance_task =
2230 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2231 &connection_fwd_keepalive, c);
2239 GMC_destroy (struct MeshConnection *c)
2247 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2248 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2251 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2252 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2253 &c->fwd_fc, &c->bck_fc);
2254 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2255 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2257 /* Cancel all traffic */
2258 connection_cancel_queues (c, GNUNET_YES);
2259 connection_cancel_queues (c, GNUNET_NO);
2261 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2262 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2264 /* Cancel maintainance task (keepalive/timeout) */
2265 if (NULL != c->fwd_fc.poll_msg)
2267 GMC_cancel (c->fwd_fc.poll_msg);
2268 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2270 if (NULL != c->bck_fc.poll_msg)
2272 GMC_cancel (c->bck_fc.poll_msg);
2273 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2276 /* Unregister from neighbors */
2277 unregister_neighbors (c);
2280 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2282 GMT_remove_connection (c->t, c);
2286 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES))
2287 path_destroy (c->path);
2288 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2289 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2290 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2291 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2292 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2294 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2295 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2297 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2299 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2300 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2303 GNUNET_break (GNUNET_YES ==
2304 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2310 * Get the connection ID.
2312 * @param c Connection to get the ID from.
2314 * @return ID of the connection.
2316 const struct GNUNET_HashCode *
2317 GMC_get_id (const struct MeshConnection *c)
2324 * Get the connection path.
2326 * @param c Connection to get the path from.
2328 * @return path used by the connection.
2330 const struct MeshPeerPath *
2331 GMC_get_path (const struct MeshConnection *c)
2333 if (GNUNET_NO == c->destroy)
2340 * Get the connection state.
2342 * @param c Connection to get the state from.
2344 * @return state of the connection.
2346 enum MeshConnectionState
2347 GMC_get_state (const struct MeshConnection *c)
2353 * Get the connection tunnel.
2355 * @param c Connection to get the tunnel from.
2357 * @return tunnel of the connection.
2359 struct MeshTunnel3 *
2360 GMC_get_tunnel (const struct MeshConnection *c)
2367 * Get free buffer space in a connection.
2369 * @param c Connection.
2370 * @param fwd Is query about FWD traffic?
2372 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2375 GMC_get_buffer (struct MeshConnection *c, int fwd)
2377 struct MeshFlowControl *fc;
2379 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2381 return (fc->queue_max - fc->queue_n);
2385 * Get how many messages have we allowed to send to us from a direction.
2387 * @param c Connection.
2388 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2390 * @return last_ack_sent - last_pid_recv
2393 GMC_get_allowed (struct MeshConnection *c, int fwd)
2395 struct MeshFlowControl *fc;
2397 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2398 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2402 return (fc->last_ack_sent - fc->last_pid_recv);
2406 * Get messages queued in a connection.
2408 * @param c Connection.
2409 * @param fwd Is query about FWD traffic?
2411 * @return Number of messages queued.
2414 GMC_get_qn (struct MeshConnection *c, int fwd)
2416 struct MeshFlowControl *fc;
2418 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2425 * Allow the connection to advertise a buffer of the given size.
2427 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2428 * allowing up to last_pid_recv + buffer.
2430 * @param c Connection.
2431 * @param buffer How many more messages the connection can accept.
2432 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2435 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2437 send_ack (c, buffer, fwd, GNUNET_NO);
2442 * Notify other peers on a connection of a broken link. Mark connections
2443 * to destroy after all traffic has been sent.
2445 * @param c Connection on which there has been a disconnection.
2446 * @param peer Peer that disconnected.
2449 GMC_notify_broken (struct MeshConnection *c,
2450 struct MeshPeer *peer)
2454 LOG (GNUNET_ERROR_TYPE_DEBUG,
2455 " notify broken on %s due to %s disconnect\n",
2456 GMC_2s (c), GMP_2s (peer));
2458 fwd = peer == get_prev_hop (c);
2460 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2462 /* Local shutdown, no one to notify about this. */
2466 if (GNUNET_NO == c->destroy)
2467 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2469 /* Connection will have at least one pending message
2470 * (the one we just scheduled), so no point in checking whether to
2471 * destroy immediately. */
2472 c->destroy = GNUNET_YES;
2473 c->state = MESH_CONNECTION_DESTROYED;
2476 * Cancel all queues, if no message is left, connection will be destroyed.
2478 connection_cancel_queues (c, !fwd);
2485 * Is this peer the first one on the connection?
2487 * @param c Connection.
2488 * @param fwd Is this about fwd traffic?
2490 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2493 GMC_is_origin (struct MeshConnection *c, int fwd)
2495 if (!fwd && c->path->length - 1 == c->own_pos )
2497 if (fwd && 0 == c->own_pos)
2504 * Is this peer the last one on the connection?
2506 * @param c Connection.
2507 * @param fwd Is this about fwd traffic?
2508 * Note that the ROOT is the terminal for BCK traffic!
2510 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2513 GMC_is_terminal (struct MeshConnection *c, int fwd)
2515 return GMC_is_origin (c, !fwd);
2520 * See if we are allowed to send by the next hop in the given direction.
2522 * @param c Connection.
2523 * @param fwd Is this about fwd traffic?
2525 * @return #GNUNET_YES in case it's OK to send.
2528 GMC_is_sendable (struct MeshConnection *c, int fwd)
2530 struct MeshFlowControl *fc;
2532 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2533 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2539 * Sends an already built message on a connection, properly registering
2540 * all used resources.
2542 * @param message Message to send. Function makes a copy of it.
2543 * If message is not hop-by-hop, decrements TTL of copy.
2544 * @param c Connection on which this message is transmitted.
2545 * @param fwd Is this a fwd message?
2546 * @param force Force the connection to accept the message (buffer overfill).
2547 * @param cont Continuation called once message is sent. Can be NULL.
2548 * @param cont_cls Closure for @c cont.
2550 * @return Handle to cancel the message before it's sent.
2551 * NULL on error or if @c cont is NULL.
2552 * Invalid on @c cont call.
2554 struct MeshConnectionQueue *
2555 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2556 struct MeshConnection *c, int fwd, int force,
2557 GMC_sent cont, void *cont_cls)
2559 struct MeshFlowControl *fc;
2560 struct MeshConnectionQueue *q;
2566 size = ntohs (message->size);
2567 data = GNUNET_malloc (size);
2568 memcpy (data, message, size);
2569 type = ntohs (message->type);
2570 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2571 GM_m2s (type), size, GMC_2s (c));
2573 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2574 droppable = GNUNET_NO == force;
2577 struct GNUNET_MESH_Encrypted *emsg;
2578 struct GNUNET_MESH_KX *kmsg;
2579 struct GNUNET_MESH_ACK *amsg;
2580 struct GNUNET_MESH_Poll *pmsg;
2581 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2582 struct GNUNET_MESH_ConnectionBroken *bmsg;
2585 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2586 emsg = (struct GNUNET_MESH_Encrypted *) data;
2587 ttl = ntohl (emsg->ttl);
2590 GNUNET_break_op (0);
2595 emsg->ttl = htonl (ttl - 1);
2596 emsg->pid = htonl (fc->next_pid++);
2597 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2598 if (GNUNET_YES == droppable)
2601 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2602 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2603 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2607 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2609 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2611 GMC_start_poll (c, fwd);
2615 case GNUNET_MESSAGE_TYPE_MESH_KX:
2616 kmsg = (struct GNUNET_MESH_KX *) data;
2620 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2621 amsg = (struct GNUNET_MESH_ACK *) data;
2623 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2624 droppable = GNUNET_NO;
2627 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2628 pmsg = (struct GNUNET_MESH_Poll *) data;
2630 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2631 droppable = GNUNET_NO;
2634 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2635 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2640 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2641 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2646 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2647 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2648 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2657 if (fc->queue_n > fc->queue_max && droppable)
2659 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2662 LOG (GNUNET_ERROR_TYPE_DEBUG,
2663 "queue full: %u/%u\n",
2664 fc->queue_n, fc->queue_max);
2665 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2671 return NULL; /* Drop this message */
2674 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2675 c->pending_messages++;
2677 q = GNUNET_new (struct MeshConnectionQueue);
2678 q->forced = !droppable;
2679 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2689 q->cont_cls = cont_cls;
2695 * Cancel a previously sent message while it's in the queue.
2697 * ONLY can be called before the continuation given to the send function
2698 * is called. Once the continuation is called, the message is no longer in the
2701 * @param q Handle to the queue.
2704 GMC_cancel (struct MeshConnectionQueue *q)
2706 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2708 /* queue destroy calls message_sent, which calls q->cont and frees q */
2709 GMP_queue_destroy (q->q, GNUNET_YES);
2714 * Sends a CREATE CONNECTION message for a path to a peer.
2715 * Changes the connection and tunnel states if necessary.
2717 * @param connection Connection to create.
2720 GMC_send_create (struct MeshConnection *connection)
2722 enum MeshTunnel3CState state;
2725 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2726 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2728 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2729 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2730 connection, connection->pending_messages);
2731 connection->pending_messages++;
2733 GMP_queue_add (get_next_hop (connection), NULL,
2734 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2735 size, connection, GNUNET_YES, &message_sent, NULL);
2737 state = GMT_get_cstate (connection->t);
2738 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2739 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2740 if (MESH_CONNECTION_NEW == connection->state)
2741 connection_change_state (connection, MESH_CONNECTION_SENT);
2746 * Send a message to all peers in this connection that the connection
2747 * is no longer valid.
2749 * If some peer should not receive the message, it should be zero'ed out
2750 * before calling this function.
2752 * @param c The connection whose peers to notify.
2755 GMC_send_destroy (struct MeshConnection *c)
2757 struct GNUNET_MESH_ConnectionDestroy msg;
2759 if (GNUNET_YES == c->destroy)
2762 msg.header.size = htons (sizeof (msg));
2763 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2765 LOG (GNUNET_ERROR_TYPE_DEBUG,
2766 " sending connection destroy for connection %s\n",
2769 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2770 GMC_send_prebuilt_message (&msg.header, c,
2771 GNUNET_YES, GNUNET_YES, NULL, NULL);
2772 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2773 GMC_send_prebuilt_message (&msg.header, c,
2774 GNUNET_NO, GNUNET_YES, NULL, NULL);
2775 c->destroy = GNUNET_YES;
2776 c->state = MESH_CONNECTION_DESTROYED;
2781 * @brief Start a polling timer for the connection.
2783 * When a neighbor does not accept more traffic on the connection it could be
2784 * caused by a simple congestion or by a lost ACK. Polling enables to check
2785 * for the lastest ACK status for a connection.
2787 * @param c Connection.
2788 * @param fwd Should we poll in the FWD direction?
2791 GMC_start_poll (struct MeshConnection *c, int fwd)
2793 struct MeshFlowControl *fc;
2795 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2796 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
2798 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
2800 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
2801 fc->poll_task, fc->poll_msg);
2804 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
2805 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2812 * @brief Stop polling a connection for ACKs.
2814 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2816 * @param c Connection.
2817 * @param fwd Should we stop the poll in the FWD direction?
2820 GMC_stop_poll (struct MeshConnection *c, int fwd)
2822 struct MeshFlowControl *fc;
2824 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2825 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2827 GNUNET_SCHEDULER_cancel (fc->poll_task);
2828 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2833 * Get a (static) string for a connection.
2835 * @param c Connection.
2838 GMC_2s (struct MeshConnection *c)
2845 static char buf[128];
2847 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2850 return GNUNET_h2s (&c->id);