2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh_connection.c
23 * @brief GNUnet MESH service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "mesh_path.h"
33 #include "mesh_protocol.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
42 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
43 GNUNET_TIME_UNIT_MINUTES,\
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct to encapsulate all the Flow Control information to a peer to which
54 * we are directly connected (on a core level).
56 struct MeshFlowControl
59 * Connection this controls.
61 struct MeshConnection *c;
64 * How many messages are in the queue on this connection.
69 * How many messages do we accept in the queue.
71 unsigned int queue_max;
79 * ID of the last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 GNUNET_SCHEDULER_TaskIdentifier poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
109 * Queued poll message, to cancel if not necessary anymore (got ACK).
111 struct MeshConnectionQueue *poll_msg;
114 * Queued poll message, to cancel if not necessary anymore (got ACK).
116 struct MeshConnectionQueue *ack_msg;
120 * Keep a record of the last messages sent on this connection.
122 struct MeshConnectionPerformance
125 * Circular buffer for storing measurements.
127 double usecsperbyte[AVG_MSGS];
130 * Running average of @c usecsperbyte.
135 * How many values of @c usecsperbyte are valid.
140 * Index of the next "free" position in @c usecsperbyte.
147 * Struct containing all information regarding a connection to a peer.
149 struct MeshConnection
152 * Tunnel this connection is part of.
154 struct MeshTunnel3 *t;
157 * Flow control information for traffic fwd.
159 struct MeshFlowControl fwd_fc;
162 * Flow control information for traffic bck.
164 struct MeshFlowControl bck_fc;
167 * Measure connection performance on the endpoint.
169 struct MeshConnectionPerformance *perf;
172 * ID of the connection.
174 struct GNUNET_HashCode id;
177 * State of the connection.
179 enum MeshConnectionState state;
182 * Path being used for the tunnel. At the origin of the connection
183 * it's a pointer to the destination's path pool, otherwise just a copy.
185 struct MeshPeerPath *path;
188 * Position of the local peer in the path.
190 unsigned int own_pos;
193 * Task to keep the used paths alive at the owner,
194 * time tunnel out on all the other peers.
196 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
199 * Task to keep the used paths alive at the destination,
200 * time tunnel out on all the other peers.
202 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
205 * Pending message count.
207 int pending_messages;
210 * Destroy flag: if true, destroy on last message.
216 * Handle for messages queued but not yet sent.
218 struct MeshConnectionQueue
221 * Peer queue handle, to cancel if necessary.
223 struct MeshPeerQueue *q;
226 * Was this a forced message? (Do not account for it)
231 * Continuation to call once sent.
236 * Closure for @c cont.
241 /******************************************************************************/
242 /******************************* GLOBALS ***********************************/
243 /******************************************************************************/
246 * Global handle to the statistics service.
248 extern struct GNUNET_STATISTICS_Handle *stats;
251 * Local peer own ID (memory efficient handle).
253 extern GNUNET_PEER_Id myid;
256 * Local peer own ID (full value).
258 extern struct GNUNET_PeerIdentity my_full_id;
261 * Connections known, indexed by cid (MeshConnection).
263 static struct GNUNET_CONTAINER_MultiHashMap *connections;
266 * How many connections are we willing to maintain.
267 * Local connections are always allowed, even if there are more connections than max.
269 static unsigned long long max_connections;
272 * How many messages *in total* are we willing to queue, divide by number of
273 * connections to get connection queue size.
275 static unsigned long long max_msgs_queue;
278 * How often to send path keepalives. Paths timeout after 4 missed.
280 static struct GNUNET_TIME_Relative refresh_connection_time;
283 * How often to send path create / ACKs.
285 static struct GNUNET_TIME_Relative create_connection_time;
288 /******************************************************************************/
289 /******************************** STATIC ***********************************/
290 /******************************************************************************/
292 #if 0 // avoid compiler warning for unused static function
294 fc_debug (struct MeshFlowControl *fc)
296 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
297 fc->last_pid_recv, fc->last_ack_sent);
298 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
299 fc->last_pid_sent, fc->last_ack_recv);
300 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
301 fc->queue_n, fc->queue_max);
305 connection_debug (struct MeshConnection *c)
309 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
312 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
313 peer2s (c->t->peer), GMC_2s (c));
314 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
315 c->state, c->pending_messages);
316 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
317 fc_debug (&c->fwd_fc);
318 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
319 fc_debug (&c->bck_fc);
324 * Get string description for tunnel state.
326 * @param s Tunnel state.
328 * @return String representation.
331 GMC_state2s (enum MeshConnectionState s)
335 case MESH_CONNECTION_NEW:
336 return "MESH_CONNECTION_NEW";
337 case MESH_CONNECTION_SENT:
338 return "MESH_CONNECTION_SENT";
339 case MESH_CONNECTION_ACK:
340 return "MESH_CONNECTION_ACK";
341 case MESH_CONNECTION_READY:
342 return "MESH_CONNECTION_READY";
343 case MESH_CONNECTION_DESTROYED:
344 return "MESH_CONNECTION_DESTROYED";
346 return "MESH_CONNECTION_STATE_ERROR";
352 * Initialize a Flow Control structure to the initial state.
354 * @param fc Flow Control structure to initialize.
357 fc_init (struct MeshFlowControl *fc)
360 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
361 fc->last_pid_recv = (uint32_t) -1;
362 fc->last_ack_sent = (uint32_t) 0;
363 fc->last_ack_recv = (uint32_t) 0;
364 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
365 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
367 fc->queue_max = (max_msgs_queue / max_connections) + 1;
374 * @param cid Connection ID.
376 static struct MeshConnection *
377 connection_get (const struct GNUNET_HashCode *cid)
379 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
384 connection_change_state (struct MeshConnection* c,
385 enum MeshConnectionState state)
387 LOG (GNUNET_ERROR_TYPE_DEBUG,
388 "Connection %s state was %s\n",
389 GMC_2s (c), GMC_state2s (c->state));
390 if (MESH_CONNECTION_DESTROYED == c->state)
392 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
395 LOG (GNUNET_ERROR_TYPE_DEBUG,
396 "Connection %s state is now %s\n",
397 GMC_2s (c), GMC_state2s (state));
403 * Callback called when a queued ACK message is sent.
405 * @param cls Closure (FC).
406 * @param c Connection this message was on.
407 * @param q Queue handler this call invalidates.
408 * @param type Type of message sent.
409 * @param fwd Was this a FWD going message?
410 * @param size Size of the message.
414 struct MeshConnection *c,
415 struct MeshConnectionQueue *q,
416 uint16_t type, int fwd, size_t size)
418 struct MeshFlowControl *fc = cls;
425 * Send an ACK on the connection, informing the predecessor about
426 * the available buffer space. Should not be called in case the peer
427 * is origin (no predecessor) in the @c fwd direction.
429 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
430 * the ACK itself goes "back" (dest->root).
432 * @param c Connection on which to send the ACK.
433 * @param buffer How much space free to advertise?
434 * @param fwd Is this FWD ACK? (Going dest -> root)
435 * @param force Don't optimize out.
438 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
440 struct MeshFlowControl *next_fc;
441 struct MeshFlowControl *prev_fc;
442 struct GNUNET_MESH_ACK msg;
446 /* If origin, there is no connection to send ACKs. Wrong function! */
447 if (GMC_is_origin (c, fwd))
453 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
454 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
456 LOG (GNUNET_ERROR_TYPE_DEBUG,
457 "connection send %s ack on %s\n",
458 GM_f2s (fwd), GMC_2s (c));
460 /* Check if we need to transmit the ACK. */
461 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
462 if (3 < delta && buffer < delta && GNUNET_NO == force)
464 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
465 LOG (GNUNET_ERROR_TYPE_DEBUG,
466 " last pid recv: %u, last ack sent: %u\n",
467 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
471 /* Ok, ACK might be necessary, what PID to ACK? */
472 ack = prev_fc->last_pid_recv + buffer;
473 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
474 LOG (GNUNET_ERROR_TYPE_DEBUG,
475 " last pid %u, last ack %u, qmax %u, q %u\n",
476 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
477 next_fc->queue_max, next_fc->queue_n);
478 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
484 /* Check if message is already in queue */
485 if (NULL != prev_fc->ack_msg)
487 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
489 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
490 GMC_cancel (prev_fc->ack_msg);
491 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
495 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
500 prev_fc->last_ack_sent = ack;
502 /* Build ACK message and send on connection */
503 msg.header.size = htons (sizeof (msg));
504 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
505 msg.ack = htonl (ack);
508 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
515 * Callback called when a queued message is sent.
517 * Calculates the average time and connection packet tracking.
519 * @param cls Closure (ConnectionQueue Handle).
520 * @param c Connection this message was on.
521 * @param type Type of message sent.
522 * @param fwd Was this a FWD going message?
523 * @param size Size of the message.
524 * @param wait Time spent waiting for core (only the time for THIS message)
527 message_sent (void *cls,
528 struct MeshConnection *c, uint16_t type,
529 int fwd, size_t size,
530 struct GNUNET_TIME_Relative wait)
532 struct MeshConnectionPerformance *p;
533 struct MeshFlowControl *fc;
534 struct MeshConnectionQueue *q = cls;
538 fc = fwd ? &c->fwd_fc : &c->bck_fc;
539 LOG (GNUNET_ERROR_TYPE_DEBUG,
543 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
549 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
550 q->cont (q->cont_cls, c, q, type, fwd, size);
554 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
556 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
563 c->pending_messages--;
564 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
566 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
570 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
573 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
576 if (GNUNET_NO == forced)
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 "! accounting pid %u\n",
585 LOG (GNUNET_ERROR_TYPE_DEBUG,
586 "! forced, Q_N not accounting pid %u\n",
589 GMC_send_ack (c, fwd, GNUNET_NO);
592 case GNUNET_MESSAGE_TYPE_MESH_POLL:
596 case GNUNET_MESSAGE_TYPE_MESH_ACK:
603 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
606 return; /* Only endpoints are interested in timing. */
609 usecsperbyte = ((double) wait.rel_value_us) / size;
610 if (p->size == AVG_MSGS)
612 /* Array is full. Substract oldest value, add new one and store. */
613 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
614 p->usecsperbyte[p->idx] = usecsperbyte;
615 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
619 /* Array not yet full. Add current value to avg and store. */
620 p->usecsperbyte[p->idx] = usecsperbyte;
622 p->avg += p->usecsperbyte[p->idx];
626 p->idx = (p->idx + 1) % AVG_MSGS;
631 * Get the previous hop in a connection
633 * @param c Connection.
635 * @return Previous peer in the connection.
637 static struct MeshPeer *
638 get_prev_hop (const struct MeshConnection *c)
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, neighbor: %d\n",
1157 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1158 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor: %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))
1183 GNUNET_break (MESH_CONNECTION_NEW == c->state);
1184 LOG (GNUNET_ERROR_TYPE_ERROR, " cstate: %u\n", c->state);
1187 peer = get_prev_hop (c);
1188 if (GNUNET_OK != GMP_remove_connection (peer, c))
1190 GNUNET_break (MESH_CONNECTION_NEW == c->state);
1191 LOG (GNUNET_ERROR_TYPE_ERROR, " cstate: %u\n", c->state);
1197 * Bind the connection to the peer and the tunnel to that peer.
1199 * If the peer has no tunnel, create one. Update tunnel and connection
1200 * data structres to reflect new status.
1202 * @param c Connection.
1206 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1208 GMP_add_tunnel (peer);
1209 c->t = GMP_get_tunnel (peer);
1210 GMT_add_connection (c->t, c);
1213 /******************************************************************************/
1214 /******************************** API ***********************************/
1215 /******************************************************************************/
1218 * Core handler for connection creation.
1220 * @param cls Closure (unused).
1221 * @param peer Sender (neighbor).
1222 * @param message Message.
1224 * @return GNUNET_OK to keep the connection open,
1225 * GNUNET_SYSERR to close it (signal serious error)
1228 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1229 const struct GNUNET_MessageHeader *message)
1231 struct GNUNET_MESH_ConnectionCreate *msg;
1232 struct GNUNET_PeerIdentity *id;
1233 struct GNUNET_HashCode *cid;
1234 struct MeshPeerPath *path;
1235 struct MeshPeer *dest_peer;
1236 struct MeshPeer *orig_peer;
1237 struct MeshConnection *c;
1238 unsigned int own_pos;
1242 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1246 size = ntohs (message->size);
1247 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1249 GNUNET_break_op (0);
1253 /* Calculate hops */
1254 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1255 if (size % sizeof (struct GNUNET_PeerIdentity))
1257 GNUNET_break_op (0);
1260 size /= sizeof (struct GNUNET_PeerIdentity);
1263 GNUNET_break_op (0);
1266 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1268 /* Get parameters */
1269 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1271 id = (struct GNUNET_PeerIdentity *) &msg[1];
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 " connection %s (%s).\n",
1274 GNUNET_h2s (cid), GNUNET_i2s (id));
1276 /* Create connection */
1277 c = connection_get (cid);
1281 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1282 path = path_new (size);
1284 for (i = 0; i < size; i++)
1286 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1287 GNUNET_i2s (&id[i]));
1288 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1289 if (path->peers[i] == myid)
1292 if (own_pos == 0 && path->peers[own_pos] != myid)
1294 /* create path: self not found in path through self */
1295 GNUNET_break_op (0);
1296 path_destroy (path);
1299 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1300 GMP_add_path_to_all (path, GNUNET_NO);
1301 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1302 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1305 path_destroy (path);
1308 connection_reset_timeout (c, GNUNET_YES);
1312 path = path_duplicate (c->path);
1314 if (MESH_CONNECTION_NEW == c->state)
1315 connection_change_state (c, MESH_CONNECTION_SENT);
1317 /* Remember peers */
1318 dest_peer = GMP_get (&id[size - 1]);
1319 orig_peer = GMP_get (&id[0]);
1321 /* Is it a connection to us? */
1322 if (c->own_pos == size - 1)
1324 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1325 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1327 add_to_peer (c, orig_peer);
1328 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1329 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1331 send_connection_ack (c, GNUNET_NO);
1332 if (MESH_CONNECTION_SENT == c->state)
1333 connection_change_state (c, MESH_CONNECTION_ACK);
1335 /* Keep tunnel alive in direction dest->owner*/
1336 if (GNUNET_SCHEDULER_NO_TASK == c->bck_maintenance_task)
1338 c->bck_maintenance_task =
1339 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1340 &connection_bck_keepalive, c);
1345 /* It's for somebody else! Retransmit. */
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1347 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1348 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1349 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES, NULL, NULL);
1351 path_destroy (path);
1357 * Core handler for path confirmations.
1359 * @param cls closure
1360 * @param message message
1361 * @param peer peer identity this notification is about
1363 * @return GNUNET_OK to keep the connection open,
1364 * GNUNET_SYSERR to close it (signal serious error)
1367 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1368 const struct GNUNET_MessageHeader *message)
1370 struct GNUNET_MESH_ConnectionACK *msg;
1371 struct MeshConnection *c;
1372 struct MeshPeerPath *p;
1373 struct MeshPeer *pi;
1374 enum MeshConnectionState oldstate;
1377 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1378 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1379 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1380 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1381 GNUNET_h2s (&msg->cid));
1382 c = connection_get (&msg->cid);
1385 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1387 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1391 oldstate = c->state;
1392 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1393 pi = GMP_get (peer);
1394 if (get_next_hop (c) == pi)
1396 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1398 if (MESH_CONNECTION_SENT == oldstate)
1399 connection_change_state (c, MESH_CONNECTION_ACK);
1401 else if (get_prev_hop (c) == pi)
1403 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1405 connection_change_state (c, MESH_CONNECTION_READY);
1409 GNUNET_break_op (0);
1413 connection_reset_timeout (c, fwd);
1415 /* Add path to peers? */
1419 GMP_add_path_to_all (p, GNUNET_YES);
1426 /* Message for us as creator? */
1427 if (GMC_is_origin (c, GNUNET_YES))
1429 if (GNUNET_NO != fwd)
1431 GNUNET_break_op (0);
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1436 /* If just created, cancel the short timeout and start a long one */
1437 if (MESH_CONNECTION_SENT == oldstate)
1438 connection_reset_timeout (c, GNUNET_YES);
1440 /* Change connection state */
1441 connection_change_state (c, MESH_CONNECTION_READY);
1442 send_connection_ack (c, GNUNET_YES);
1444 /* Change tunnel state, trigger KX */
1445 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1446 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1451 /* Message for us as destination? */
1452 if (GMC_is_terminal (c, GNUNET_YES))
1454 if (GNUNET_YES != fwd)
1456 GNUNET_break_op (0);
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1461 /* If just created, cancel the short timeout and start a long one */
1462 if (MESH_CONNECTION_ACK == oldstate)
1463 connection_reset_timeout (c, GNUNET_NO);
1465 /* Change tunnel state */
1466 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1467 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1472 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1473 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1479 * Core handler for notifications of broken paths
1481 * @param cls Closure (unused).
1482 * @param id Peer identity of sending neighbor.
1483 * @param message Message.
1485 * @return GNUNET_OK to keep the connection open,
1486 * GNUNET_SYSERR to close it (signal serious error)
1489 GMC_handle_broken (void* cls,
1490 const struct GNUNET_PeerIdentity* id,
1491 const struct GNUNET_MessageHeader* message)
1493 struct GNUNET_MESH_ConnectionBroken *msg;
1494 struct MeshConnection *c;
1497 LOG (GNUNET_ERROR_TYPE_DEBUG,
1498 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1499 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1500 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1501 GNUNET_i2s (&msg->peer1));
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1503 GNUNET_i2s (&msg->peer2));
1504 c = connection_get (&msg->cid);
1507 GNUNET_break_op (0);
1511 fwd = is_fwd (c, id);
1512 if (GMC_is_terminal (c, fwd))
1514 if (0 < c->pending_messages)
1515 c->destroy = GNUNET_YES;
1521 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1522 c->destroy = GNUNET_YES;
1523 connection_cancel_queues (c, !fwd);
1532 * Core handler for tunnel destruction
1534 * @param cls Closure (unused).
1535 * @param peer Peer identity of sending neighbor.
1536 * @param message Message.
1538 * @return GNUNET_OK to keep the connection open,
1539 * GNUNET_SYSERR to close it (signal serious error)
1542 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1543 const struct GNUNET_MessageHeader *message)
1545 struct GNUNET_MESH_ConnectionDestroy *msg;
1546 struct MeshConnection *c;
1549 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1550 LOG (GNUNET_ERROR_TYPE_DEBUG,
1551 "Got a CONNECTION DESTROY message from %s\n",
1553 LOG (GNUNET_ERROR_TYPE_DEBUG,
1554 " for connection %s\n",
1555 GNUNET_h2s (&msg->cid));
1556 c = connection_get (&msg->cid);
1559 /* Probably already got the message from another path,
1560 * destroyed the tunnel and retransmitted to children.
1563 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1568 fwd = is_fwd (c, peer);
1569 if (GNUNET_SYSERR == fwd)
1571 GNUNET_break_op (0);
1574 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1575 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1576 else if (0 == c->pending_messages)
1578 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1582 c->destroy = GNUNET_YES;
1583 c->state = MESH_CONNECTION_DESTROYED;
1586 GMT_remove_connection (c->t);
1594 * Generic handler for mesh network encrypted traffic.
1596 * @param peer Peer identity this notification is about.
1597 * @param msg Encrypted message.
1599 * @return GNUNET_OK to keep the connection open,
1600 * GNUNET_SYSERR to close it (signal serious error)
1603 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1604 const struct GNUNET_MESH_Encrypted *msg)
1606 struct MeshConnection *c;
1607 struct MeshPeer *neighbor;
1608 struct MeshFlowControl *fc;
1609 GNUNET_PEER_Id peer_id;
1617 size = ntohs (msg->header.size);
1619 sizeof (struct GNUNET_MESH_Encrypted) +
1620 sizeof (struct GNUNET_MessageHeader))
1622 GNUNET_break_op (0);
1625 type = ntohs (msg->header.type);
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1627 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1628 GM_m2s (type), ntohl (msg->pid), GNUNET_i2s (peer));
1630 /* Check connection */
1631 c = connection_get (&msg->cid);
1634 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1635 LOG (GNUNET_ERROR_TYPE_DEBUG,
1636 "WARNING connection %s unknown\n",
1637 GNUNET_h2s (&msg->cid));
1641 /* Check if origin is as expected */
1642 neighbor = get_prev_hop (c);
1643 peer_id = GNUNET_PEER_search (peer);
1644 if (peer_id == GMP_get_short_id (neighbor))
1650 neighbor = get_next_hop (c);
1651 if (peer_id == GMP_get_short_id (neighbor))
1657 /* Unexpected peer sending traffic on a connection. */
1658 GNUNET_break_op (0);
1664 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1665 pid = ntohl (msg->pid);
1666 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1668 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1669 LOG (GNUNET_ERROR_TYPE_DEBUG,
1670 "WARNING Received PID %u, (prev %u), ACK %u\n",
1671 pid, fc->last_pid_recv, fc->last_ack_sent);
1674 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1676 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1677 LOG (GNUNET_ERROR_TYPE_DEBUG,
1678 " Pid %u not expected (%u+), dropping!\n",
1679 pid, fc->last_pid_recv + 1);
1682 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1683 connection_change_state (c, MESH_CONNECTION_READY);
1684 connection_reset_timeout (c, fwd);
1685 fc->last_pid_recv = pid;
1687 /* Is this message for us? */
1688 if (GMC_is_terminal (c, fwd))
1690 /* TODO signature verification */
1691 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1692 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1699 fc->last_pid_recv = pid;
1700 GMT_handle_encrypted (c->t, msg);
1701 GMC_send_ack (c, fwd, GNUNET_NO);
1705 /* Message not for us: forward to next hop */
1706 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1707 ttl = ntohl (msg->ttl);
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1711 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1712 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1713 GMC_send_ack (c, fwd, GNUNET_NO);
1717 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1718 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1724 * Generic handler for mesh network encrypted traffic.
1726 * @param peer Peer identity this notification is about.
1727 * @param msg Encrypted message.
1729 * @return GNUNET_OK to keep the connection open,
1730 * GNUNET_SYSERR to close it (signal serious error)
1733 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1734 const struct GNUNET_MESH_KX *msg)
1736 struct MeshConnection *c;
1737 struct MeshPeer *neighbor;
1738 GNUNET_PEER_Id peer_id;
1744 size = ntohs (msg->header.size);
1746 sizeof (struct GNUNET_MESH_Encrypted) +
1747 sizeof (struct GNUNET_MessageHeader))
1749 GNUNET_break_op (0);
1752 type = ntohs (msg->header.type);
1753 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1754 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1755 GM_m2s (type), GNUNET_i2s (peer));
1757 /* Check connection */
1758 c = connection_get (&msg->cid);
1761 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1762 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1765 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1767 /* Check if origin is as expected */
1768 neighbor = get_prev_hop (c);
1769 peer_id = GNUNET_PEER_search (peer);
1770 if (peer_id == GMP_get_short_id (neighbor))
1776 neighbor = get_next_hop (c);
1777 if (peer_id == GMP_get_short_id (neighbor))
1783 /* Unexpected peer sending traffic on a connection. */
1784 GNUNET_break_op (0);
1789 /* Count as connection confirmation. */
1790 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1791 connection_change_state (c, MESH_CONNECTION_READY);
1792 connection_reset_timeout (c, fwd);
1795 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1796 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1799 /* Is this message for us? */
1800 if (GMC_is_terminal (c, fwd))
1802 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1803 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1809 GMT_handle_kx (c->t, &msg[1].header);
1813 /* Message not for us: forward to next hop */
1814 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1815 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1816 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1823 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1825 * @param cls Closure (unused).
1826 * @param message Message received.
1827 * @param peer Peer who sent the message.
1829 * @return GNUNET_OK to keep the connection open,
1830 * GNUNET_SYSERR to close it (signal serious error)
1833 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1834 const struct GNUNET_MessageHeader *message)
1836 return handle_mesh_encrypted (peer,
1837 (struct GNUNET_MESH_Encrypted *)message);
1842 * Core handler for key exchange traffic (ephemeral key, ping, pong).
1844 * @param cls Closure (unused).
1845 * @param message Message received.
1846 * @param peer Peer who sent the message.
1848 * @return GNUNET_OK to keep the connection open,
1849 * GNUNET_SYSERR to close it (signal serious error)
1852 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1853 const struct GNUNET_MessageHeader *message)
1855 return handle_mesh_kx (peer,
1856 (struct GNUNET_MESH_KX *) message);
1861 * Core handler for mesh network traffic point-to-point acks.
1863 * @param cls closure
1864 * @param message message
1865 * @param peer peer identity this notification is about
1867 * @return GNUNET_OK to keep the connection open,
1868 * GNUNET_SYSERR to close it (signal serious error)
1871 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1872 const struct GNUNET_MessageHeader *message)
1874 struct GNUNET_MESH_ACK *msg;
1875 struct MeshConnection *c;
1876 struct MeshFlowControl *fc;
1881 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1882 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1884 msg = (struct GNUNET_MESH_ACK *) message;
1886 c = connection_get (&msg->cid);
1890 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1895 /* Is this a forward or backward ACK? */
1896 id = GNUNET_PEER_search (peer);
1897 if (GMP_get_short_id (get_next_hop (c)) == id)
1899 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1903 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1905 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1911 GNUNET_break_op (0);
1915 ack = ntohl (msg->ack);
1916 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1917 ack, fc->last_ack_recv);
1918 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
1919 fc->last_ack_recv = ack;
1921 /* Cancel polling if the ACK is big enough. */
1922 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1923 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1925 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1926 GNUNET_SCHEDULER_cancel (fc->poll_task);
1927 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1928 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1931 connection_unlock_queue (c, fwd);
1938 * Core handler for mesh network traffic point-to-point ack polls.
1940 * @param cls closure
1941 * @param message message
1942 * @param peer peer identity this notification is about
1944 * @return GNUNET_OK to keep the connection open,
1945 * GNUNET_SYSERR to close it (signal serious error)
1948 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1949 const struct GNUNET_MessageHeader *message)
1951 struct GNUNET_MESH_Poll *msg;
1952 struct MeshConnection *c;
1953 struct MeshFlowControl *fc;
1958 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1959 LOG (GNUNET_ERROR_TYPE_DEBUG,
1960 "Got a POLL packet from %s!\n",
1963 msg = (struct GNUNET_MESH_Poll *) message;
1965 c = connection_get (&msg->cid);
1969 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1971 GNUNET_break_op (0);
1975 /* Is this a forward or backward ACK?
1976 * Note: a poll should never be needed in a loopback case,
1977 * since there is no possiblility of packet loss there, so
1978 * this way of discerining FWD/BCK should not be a problem.
1980 id = GNUNET_PEER_search (peer);
1981 if (GMP_get_short_id (get_next_hop (c)) == id)
1983 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
1986 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1988 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
1993 GNUNET_break_op (0);
1997 pid = ntohl (msg->pid);
1998 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
1999 fc->last_pid_recv = pid;
2000 fwd = fc == &c->bck_fc;
2001 GMC_send_ack (c, fwd, GNUNET_YES);
2008 * Core handler for mesh keepalives.
2010 * @param cls closure
2011 * @param message message
2012 * @param peer peer identity this notification is about
2013 * @return GNUNET_OK to keep the connection open,
2014 * GNUNET_SYSERR to close it (signal serious error)
2016 * TODO: Check who we got this from, to validate route.
2019 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2020 const struct GNUNET_MessageHeader *message)
2022 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2023 struct MeshConnection *c;
2024 struct MeshPeer *neighbor;
2025 GNUNET_PEER_Id peer_id;
2028 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2029 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
2032 c = connection_get (&msg->cid);
2035 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2040 /* Check if origin is as expected TODO refactor and reuse */
2041 peer_id = GNUNET_PEER_search (peer);
2042 neighbor = get_prev_hop (c);
2043 if (peer_id == GMP_get_short_id (neighbor))
2049 neighbor = get_next_hop (c);
2050 if (peer_id == GMP_get_short_id (neighbor))
2056 GNUNET_break_op (0);
2061 connection_change_state (c, MESH_CONNECTION_READY);
2062 connection_reset_timeout (c, fwd);
2064 if (GMC_is_terminal (c, fwd))
2067 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2068 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2075 * Send an ACK on the appropriate connection/channel, depending on
2076 * the direction and the position of the peer.
2078 * @param c Which connection to send the hop-by-hop ACK.
2079 * @param fwd Is this a fwd ACK? (will go dest->root).
2080 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2083 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2085 unsigned int buffer;
2087 LOG (GNUNET_ERROR_TYPE_DEBUG,
2088 "GMC send %s ACK on %s\n",
2089 GM_f2s (fwd), GMC_2s (c));
2097 if (GNUNET_NO != c->destroy)
2099 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2103 /* Get available buffer space */
2104 if (GMC_is_terminal (c, fwd))
2106 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2107 buffer = GMT_get_channels_buffer (c->t);
2111 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2112 buffer = GMC_get_buffer (c, fwd);
2114 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2115 if (0 == buffer && GNUNET_NO == force)
2118 /* Send available buffer space */
2119 if (GMC_is_origin (c, fwd))
2121 GNUNET_assert (NULL != c->t);
2122 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2123 GMT_unchoke_channels (c->t);
2127 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2128 send_ack (c, buffer, fwd, force);
2134 * Initialize the connections subsystem
2136 * @param c Configuration handle.
2139 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2141 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2143 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2146 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2147 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2148 GNUNET_SCHEDULER_shutdown ();
2153 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2156 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2157 "MESH", "MAX_CONNECTIONS", "MISSING");
2158 GNUNET_SCHEDULER_shutdown ();
2163 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2164 &refresh_connection_time))
2166 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2167 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2168 GNUNET_SCHEDULER_shutdown ();
2171 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2172 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2177 * Destroy each connection on shutdown.
2179 * @param cls Closure (unused).
2180 * @param key Current key code (CID, unused).
2181 * @param value Value in the hash map (connection)
2183 * @return #GNUNET_YES, because we should continue to iterate,
2186 shutdown_iterator (void *cls,
2187 const struct GNUNET_HashCode *key,
2190 struct MeshConnection *c = value;
2198 * Shut down the connections subsystem.
2203 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2204 GNUNET_CONTAINER_multihashmap_destroy (connections);
2209 struct MeshConnection *
2210 GMC_new (const struct GNUNET_HashCode *cid,
2211 struct MeshTunnel3 *t,
2212 struct MeshPeerPath *p,
2213 unsigned int own_pos)
2215 struct MeshConnection *c;
2217 c = GNUNET_new (struct MeshConnection);
2219 GNUNET_assert (GNUNET_OK ==
2220 GNUNET_CONTAINER_multihashmap_put (connections,
2222 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2223 fc_init (&c->fwd_fc);
2224 fc_init (&c->bck_fc);
2229 GNUNET_assert (own_pos <= p->length - 1);
2230 c->own_pos = own_pos;
2233 if (GNUNET_OK != register_neighbors (c))
2237 GMT_remove_path (c->t, p);
2247 c->fwd_maintenance_task =
2248 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2249 &connection_fwd_keepalive, c);
2257 GMC_destroy (struct MeshConnection *c)
2265 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2266 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2269 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2270 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2271 &c->fwd_fc, &c->bck_fc);
2272 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2273 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2275 /* Cancel all traffic */
2276 if (NULL != c->path)
2278 connection_cancel_queues (c, GNUNET_YES);
2279 connection_cancel_queues (c, GNUNET_NO);
2280 unregister_neighbors (c);
2283 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2284 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2286 /* Cancel maintainance task (keepalive/timeout) */
2287 if (NULL != c->fwd_fc.poll_msg)
2289 GMC_cancel (c->fwd_fc.poll_msg);
2290 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2292 if (NULL != c->bck_fc.poll_msg)
2294 GMC_cancel (c->bck_fc.poll_msg);
2295 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2298 /* Delete from tunnel */
2300 GMT_remove_connection (c->t, c);
2302 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2303 path_destroy (c->path);
2304 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2305 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2306 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2307 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2308 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2310 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2311 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2313 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2315 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2316 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2319 GNUNET_break (GNUNET_YES ==
2320 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2322 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2327 * Get the connection ID.
2329 * @param c Connection to get the ID from.
2331 * @return ID of the connection.
2333 const struct GNUNET_HashCode *
2334 GMC_get_id (const struct MeshConnection *c)
2341 * Get the connection path.
2343 * @param c Connection to get the path from.
2345 * @return path used by the connection.
2347 const struct MeshPeerPath *
2348 GMC_get_path (const struct MeshConnection *c)
2350 if (GNUNET_NO == c->destroy)
2357 * Get the connection state.
2359 * @param c Connection to get the state from.
2361 * @return state of the connection.
2363 enum MeshConnectionState
2364 GMC_get_state (const struct MeshConnection *c)
2370 * Get the connection tunnel.
2372 * @param c Connection to get the tunnel from.
2374 * @return tunnel of the connection.
2376 struct MeshTunnel3 *
2377 GMC_get_tunnel (const struct MeshConnection *c)
2384 * Get free buffer space in a connection.
2386 * @param c Connection.
2387 * @param fwd Is query about FWD traffic?
2389 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2392 GMC_get_buffer (struct MeshConnection *c, int fwd)
2394 struct MeshFlowControl *fc;
2396 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2398 return (fc->queue_max - fc->queue_n);
2402 * Get how many messages have we allowed to send to us from a direction.
2404 * @param c Connection.
2405 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2407 * @return last_ack_sent - last_pid_recv
2410 GMC_get_allowed (struct MeshConnection *c, int fwd)
2412 struct MeshFlowControl *fc;
2414 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2415 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2419 return (fc->last_ack_sent - fc->last_pid_recv);
2423 * Get messages queued in a connection.
2425 * @param c Connection.
2426 * @param fwd Is query about FWD traffic?
2428 * @return Number of messages queued.
2431 GMC_get_qn (struct MeshConnection *c, int fwd)
2433 struct MeshFlowControl *fc;
2435 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2442 * Allow the connection to advertise a buffer of the given size.
2444 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2445 * allowing up to last_pid_recv + buffer.
2447 * @param c Connection.
2448 * @param buffer How many more messages the connection can accept.
2449 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2452 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2454 send_ack (c, buffer, fwd, GNUNET_NO);
2459 * Notify other peers on a connection of a broken link. Mark connections
2460 * to destroy after all traffic has been sent.
2462 * @param c Connection on which there has been a disconnection.
2463 * @param peer Peer that disconnected.
2466 GMC_notify_broken (struct MeshConnection *c,
2467 struct MeshPeer *peer)
2471 LOG (GNUNET_ERROR_TYPE_DEBUG,
2472 " notify broken on %s due to %s disconnect\n",
2473 GMC_2s (c), GMP_2s (peer));
2475 fwd = peer == get_prev_hop (c);
2477 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2479 /* Local shutdown, no one to notify about this. */
2483 if (GNUNET_NO == c->destroy)
2484 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2486 /* Connection will have at least one pending message
2487 * (the one we just scheduled), so no point in checking whether to
2488 * destroy immediately. */
2489 c->destroy = GNUNET_YES;
2490 c->state = MESH_CONNECTION_DESTROYED;
2493 * Cancel all queues, if no message is left, connection will be destroyed.
2495 connection_cancel_queues (c, !fwd);
2502 * Is this peer the first one on the connection?
2504 * @param c Connection.
2505 * @param fwd Is this about fwd traffic?
2507 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2510 GMC_is_origin (struct MeshConnection *c, int fwd)
2512 if (!fwd && c->path->length - 1 == c->own_pos )
2514 if (fwd && 0 == c->own_pos)
2521 * Is this peer the last one on the connection?
2523 * @param c Connection.
2524 * @param fwd Is this about fwd traffic?
2525 * Note that the ROOT is the terminal for BCK traffic!
2527 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2530 GMC_is_terminal (struct MeshConnection *c, int fwd)
2532 return GMC_is_origin (c, !fwd);
2537 * See if we are allowed to send by the next hop in the given direction.
2539 * @param c Connection.
2540 * @param fwd Is this about fwd traffic?
2542 * @return #GNUNET_YES in case it's OK to send.
2545 GMC_is_sendable (struct MeshConnection *c, int fwd)
2547 struct MeshFlowControl *fc;
2549 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2550 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2556 * Sends an already built message on a connection, properly registering
2557 * all used resources.
2559 * @param message Message to send. Function makes a copy of it.
2560 * If message is not hop-by-hop, decrements TTL of copy.
2561 * @param c Connection on which this message is transmitted.
2562 * @param fwd Is this a fwd message?
2563 * @param force Force the connection to accept the message (buffer overfill).
2564 * @param cont Continuation called once message is sent. Can be NULL.
2565 * @param cont_cls Closure for @c cont.
2567 * @return Handle to cancel the message before it's sent.
2568 * NULL on error or if @c cont is NULL.
2569 * Invalid on @c cont call.
2571 struct MeshConnectionQueue *
2572 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2573 struct MeshConnection *c, int fwd, int force,
2574 GMC_sent cont, void *cont_cls)
2576 struct MeshFlowControl *fc;
2577 struct MeshConnectionQueue *q;
2583 size = ntohs (message->size);
2584 data = GNUNET_malloc (size);
2585 memcpy (data, message, size);
2586 type = ntohs (message->type);
2587 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2588 GM_m2s (type), size, GMC_2s (c));
2590 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2591 droppable = GNUNET_NO == force;
2594 struct GNUNET_MESH_Encrypted *emsg;
2595 struct GNUNET_MESH_KX *kmsg;
2596 struct GNUNET_MESH_ACK *amsg;
2597 struct GNUNET_MESH_Poll *pmsg;
2598 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2599 struct GNUNET_MESH_ConnectionBroken *bmsg;
2602 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2603 emsg = (struct GNUNET_MESH_Encrypted *) data;
2604 ttl = ntohl (emsg->ttl);
2607 GNUNET_break_op (0);
2612 emsg->ttl = htonl (ttl - 1);
2613 emsg->pid = htonl (fc->next_pid++);
2614 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2615 if (GNUNET_YES == droppable)
2618 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2619 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2620 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2624 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2626 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2628 GMC_start_poll (c, fwd);
2632 case GNUNET_MESSAGE_TYPE_MESH_KX:
2633 kmsg = (struct GNUNET_MESH_KX *) data;
2637 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2638 amsg = (struct GNUNET_MESH_ACK *) data;
2640 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2641 droppable = GNUNET_NO;
2644 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2645 pmsg = (struct GNUNET_MESH_Poll *) data;
2647 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2648 droppable = GNUNET_NO;
2651 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2652 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2657 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2658 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2663 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2664 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2665 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2674 if (fc->queue_n > fc->queue_max && droppable)
2676 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2679 LOG (GNUNET_ERROR_TYPE_DEBUG,
2680 "queue full: %u/%u\n",
2681 fc->queue_n, fc->queue_max);
2682 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2688 return NULL; /* Drop this message */
2691 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2692 c->pending_messages++;
2694 q = GNUNET_new (struct MeshConnectionQueue);
2695 q->forced = !droppable;
2696 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2706 q->cont_cls = cont_cls;
2712 * Cancel a previously sent message while it's in the queue.
2714 * ONLY can be called before the continuation given to the send function
2715 * is called. Once the continuation is called, the message is no longer in the
2718 * @param q Handle to the queue.
2721 GMC_cancel (struct MeshConnectionQueue *q)
2723 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2725 /* queue destroy calls message_sent, which calls q->cont and frees q */
2726 GMP_queue_destroy (q->q, GNUNET_YES);
2731 * Sends a CREATE CONNECTION message for a path to a peer.
2732 * Changes the connection and tunnel states if necessary.
2734 * @param connection Connection to create.
2737 GMC_send_create (struct MeshConnection *connection)
2739 enum MeshTunnel3CState state;
2742 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2743 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2745 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2746 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2747 connection, connection->pending_messages);
2748 connection->pending_messages++;
2750 GMP_queue_add (get_next_hop (connection), NULL,
2751 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2752 size, connection, GNUNET_YES, &message_sent, NULL);
2754 state = GMT_get_cstate (connection->t);
2755 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2756 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2757 if (MESH_CONNECTION_NEW == connection->state)
2758 connection_change_state (connection, MESH_CONNECTION_SENT);
2763 * Send a message to all peers in this connection that the connection
2764 * is no longer valid.
2766 * If some peer should not receive the message, it should be zero'ed out
2767 * before calling this function.
2769 * @param c The connection whose peers to notify.
2772 GMC_send_destroy (struct MeshConnection *c)
2774 struct GNUNET_MESH_ConnectionDestroy msg;
2776 if (GNUNET_YES == c->destroy)
2779 msg.header.size = htons (sizeof (msg));
2780 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2782 LOG (GNUNET_ERROR_TYPE_DEBUG,
2783 " sending connection destroy for connection %s\n",
2786 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2787 GMC_send_prebuilt_message (&msg.header, c,
2788 GNUNET_YES, GNUNET_YES, NULL, NULL);
2789 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2790 GMC_send_prebuilt_message (&msg.header, c,
2791 GNUNET_NO, GNUNET_YES, NULL, NULL);
2792 c->destroy = GNUNET_YES;
2793 c->state = MESH_CONNECTION_DESTROYED;
2798 * @brief Start a polling timer for the connection.
2800 * When a neighbor does not accept more traffic on the connection it could be
2801 * caused by a simple congestion or by a lost ACK. Polling enables to check
2802 * for the lastest ACK status for a connection.
2804 * @param c Connection.
2805 * @param fwd Should we poll in the FWD direction?
2808 GMC_start_poll (struct MeshConnection *c, int fwd)
2810 struct MeshFlowControl *fc;
2812 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2813 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
2815 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
2817 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
2818 fc->poll_task, fc->poll_msg);
2821 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
2822 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2829 * @brief Stop polling a connection for ACKs.
2831 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2833 * @param c Connection.
2834 * @param fwd Should we stop the poll in the FWD direction?
2837 GMC_stop_poll (struct MeshConnection *c, int fwd)
2839 struct MeshFlowControl *fc;
2841 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2842 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2844 GNUNET_SCHEDULER_cancel (fc->poll_task);
2845 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2850 * Get a (static) string for a connection.
2852 * @param c Connection.
2855 GMC_2s (struct MeshConnection *c)
2862 static char buf[128];
2864 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2867 return GNUNET_h2s (&c->id);