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_enc.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;
110 * Keep a record of the last messages sent on this connection.
112 struct MeshConnectionPerformance
115 * Circular buffer for storing measurements.
117 double usecsperbyte[AVG_MSGS];
120 * Running average of @c usecsperbyte.
125 * How many values of @c usecsperbyte are valid.
130 * Index of the next "free" position in @c usecsperbyte.
137 * Struct containing all information regarding a connection to a peer.
139 struct MeshConnection
142 * Tunnel this connection is part of.
144 struct MeshTunnel3 *t;
147 * Flow control information for traffic fwd.
149 struct MeshFlowControl fwd_fc;
152 * Flow control information for traffic bck.
154 struct MeshFlowControl bck_fc;
157 * Measure connection performance on the endpoint.
159 struct MeshConnectionPerformance *perf;
162 * ID of the connection.
164 struct GNUNET_HashCode id;
167 * State of the connection.
169 enum MeshConnectionState state;
172 * Path being used for the tunnel.
174 struct MeshPeerPath *path;
177 * Position of the local peer in the path.
179 unsigned int own_pos;
182 * Task to keep the used paths alive at the owner,
183 * time tunnel out on all the other peers.
185 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
188 * Task to keep the used paths alive at the destination,
189 * time tunnel out on all the other peers.
191 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
194 * Pending message count.
196 int pending_messages;
199 * Destroy flag: if true, destroy on last message.
204 /******************************************************************************/
205 /******************************* GLOBALS ***********************************/
206 /******************************************************************************/
209 * Global handle to the statistics service.
211 extern struct GNUNET_STATISTICS_Handle *stats;
214 * Local peer own ID (memory efficient handle).
216 extern GNUNET_PEER_Id myid;
219 * Local peer own ID (full value).
221 extern struct GNUNET_PeerIdentity my_full_id;
224 * Connections known, indexed by cid (MeshConnection).
226 static struct GNUNET_CONTAINER_MultiHashMap *connections;
229 * How many connections are we willing to maintain.
230 * Local connections are always allowed, even if there are more connections than max.
232 static unsigned long long max_connections;
235 * How many messages *in total* are we willing to queue, divide by number of
236 * connections to get connection queue size.
238 static unsigned long long max_msgs_queue;
241 * How often to send path keepalives. Paths timeout after 4 missed.
243 static struct GNUNET_TIME_Relative refresh_connection_time;
246 /******************************************************************************/
247 /******************************** STATIC ***********************************/
248 /******************************************************************************/
250 #if 0 // avoid compiler warning for unused static function
252 fc_debug (struct MeshFlowControl *fc)
254 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
255 fc->last_pid_recv, fc->last_ack_sent);
256 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
257 fc->last_pid_sent, fc->last_ack_recv);
258 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
259 fc->queue_n, fc->queue_max);
263 connection_debug (struct MeshConnection *c)
267 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
270 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
271 peer2s (c->t->peer), GMC_2s (c));
272 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
273 c->state, c->pending_messages);
274 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
275 fc_debug (&c->fwd_fc);
276 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
277 fc_debug (&c->bck_fc);
282 * Get string description for tunnel state.
284 * @param s Tunnel state.
286 * @return String representation.
289 GMC_state2s (enum MeshConnectionState s)
293 case MESH_CONNECTION_NEW:
294 return "MESH_CONNECTION_NEW";
295 case MESH_CONNECTION_SENT:
296 return "MESH_CONNECTION_SENT";
297 case MESH_CONNECTION_ACK:
298 return "MESH_CONNECTION_ACK";
299 case MESH_CONNECTION_READY:
300 return "MESH_CONNECTION_READY";
302 return "MESH_CONNECTION_STATE_ERROR";
308 * Initialize a Flow Control structure to the initial state.
310 * @param fc Flow Control structure to initialize.
313 fc_init (struct MeshFlowControl *fc)
316 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
317 fc->last_pid_recv = (uint32_t) -1;
318 fc->last_ack_sent = (uint32_t) 0;
319 fc->last_ack_recv = (uint32_t) 0;
320 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
321 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
323 fc->queue_max = (max_msgs_queue / max_connections) + 1;
330 * @param cid Connection ID.
332 static struct MeshConnection *
333 connection_get (const struct GNUNET_HashCode *cid)
335 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
340 connection_change_state (struct MeshConnection* c,
341 enum MeshConnectionState state)
343 LOG (GNUNET_ERROR_TYPE_DEBUG,
344 "Connection %s state was %s\n",
345 GMC_2s (c), GMC_state2s (c->state));
346 LOG (GNUNET_ERROR_TYPE_DEBUG,
347 "Connection %s state is now %s\n",
348 GMC_2s (c), GMC_state2s (state));
354 * Send an ACK on the connection, informing the predecessor about
355 * the available buffer space. Should not be called in case the peer
356 * is origin (no predecessor).
358 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
359 * the ACK itself goes "back" (dest->root).
361 * @param c Connection on which to send the ACK.
362 * @param buffer How much space free to advertise?
363 * @param fwd Is this FWD ACK? (Going dest->owner)
366 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
368 struct MeshFlowControl *next_fc;
369 struct MeshFlowControl *prev_fc;
370 struct GNUNET_MESH_ACK msg;
374 /* If origin, there is no connection to send ACKs. Wrong function! */
375 if (GMC_is_origin (c, fwd))
381 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
382 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 "connection send %s ack on %s\n",
386 fwd ? "FWD" : "BCK", GMC_2s (c));
388 /* Check if we need to transmit the ACK */
389 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
390 if (3 < delta && buffer < delta)
392 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 " last pid recv: %u, last ack sent: %u\n",
395 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
399 /* Ok, ACK might be necessary, what PID to ACK? */
400 ack = prev_fc->last_pid_recv + buffer;
401 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
402 LOG (GNUNET_ERROR_TYPE_DEBUG,
403 " last pid %u, last ack %u, qmax %u, q %u\n",
404 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
405 next_fc->queue_max, next_fc->queue_n);
406 if (ack == prev_fc->last_ack_sent)
408 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
412 prev_fc->last_ack_sent = ack;
414 /* Build ACK message and send on connection */
415 msg.header.size = htons (sizeof (msg));
416 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
417 msg.ack = htonl (ack);
420 GMC_send_prebuilt_message (&msg.header, c, !fwd);
425 * Callback called when a queued message is sent.
427 * Calculates the average time and connection packet tracking.
429 * @param cls Closure.
430 * @param c Connection this message was on.
431 * @param type Type of message sent.
432 * @param fwd Was this a FWD going message?
433 * @param size Size of the message.
434 * @param wait Time spent waiting for core (only the time for THIS message)
437 message_sent (void *cls,
438 struct MeshConnection *c, uint16_t type,
439 int fwd, size_t size,
440 struct GNUNET_TIME_Relative wait)
442 struct MeshConnectionPerformance *p;
443 struct MeshFlowControl *fc;
446 fc = fwd ? &c->fwd_fc : &c->bck_fc;
447 LOG (GNUNET_ERROR_TYPE_DEBUG, "! sent %s\n", GNUNET_MESH_DEBUG_M2S (type));
448 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
449 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
450 c->pending_messages--;
451 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
453 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
457 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
460 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
463 LOG (GNUNET_ERROR_TYPE_DEBUG,
464 "! accounting pid %u\n",
466 GMC_send_ack (c, fwd);
471 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
474 return; /* Only endpoints are interested in timing. */
477 usecsperbyte = ((double) wait.rel_value_us) / size;
478 if (p->size == AVG_MSGS)
480 /* Array is full. Substract oldest value, add new one and store. */
481 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
482 p->usecsperbyte[p->idx] = usecsperbyte;
483 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
487 /* Array not yet full. Add current value to avg and store. */
488 p->usecsperbyte[p->idx] = usecsperbyte;
490 p->avg += p->usecsperbyte[p->idx];
494 p->idx = (p->idx + 1) % AVG_MSGS;
499 * Get the previous hop in a connection
501 * @param c Connection.
503 * @return Previous peer in the connection.
505 static struct MeshPeer *
506 get_prev_hop (const struct MeshConnection *c)
510 LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos);
511 if (0 == c->own_pos || c->path->length < 2)
512 id = c->path->peers[0];
514 id = c->path->peers[c->own_pos - 1];
516 return GMP_get_short (id);
521 * Get the next hop in a connection
523 * @param c Connection.
525 * @return Next peer in the connection.
527 static struct MeshPeer *
528 get_next_hop (const struct MeshConnection *c)
532 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
533 id = c->path->peers[c->path->length - 1];
535 id = c->path->peers[c->own_pos + 1];
537 return GMP_get_short (id);
542 * Get the hop in a connection.
544 * @param c Connection.
545 * @param fwd Next hop?
547 * @return Next peer in the connection.
549 static struct MeshPeer *
550 get_hop (struct MeshConnection *c, int fwd)
553 return get_next_hop (c);
554 return get_prev_hop (c);
559 * Is traffic coming from this sender 'FWD' traffic?
561 * @param c Connection to check.
562 * @param sender Peer identity of neighbor.
564 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
565 * the traffic is 'FWD'.
566 * #GNUNET_NO for BCK.
567 * #GNUNET_SYSERR for errors.
570 is_fwd (const struct MeshConnection *c,
571 const struct GNUNET_PeerIdentity *sender)
575 id = GNUNET_PEER_search (sender);
576 if (GMP_get_short_id (get_prev_hop (c)) == id)
579 if (GMP_get_short_id (get_next_hop (c)) == id)
583 return GNUNET_SYSERR;
588 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
589 * or a first CONNECTION_ACK directed to us.
591 * @param connection Connection to confirm.
592 * @param fwd Should we send it FWD? (root->dest)
593 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
596 send_connection_ack (struct MeshConnection *connection, int fwd)
598 struct MeshTunnel3 *t;
601 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
602 !fwd ? "FWD" : "BCK");
603 GMP_queue_add (get_hop (connection, fwd), NULL,
604 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
605 sizeof (struct GNUNET_MESH_ConnectionACK),
606 connection, fwd, &message_sent, NULL);
607 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
608 GMT_change_state (t, MESH_TUNNEL3_WAITING);
609 if (MESH_CONNECTION_READY != connection->state)
610 connection_change_state (connection, MESH_CONNECTION_SENT);
615 * Send a notification that a connection is broken.
617 * @param c Connection that is broken.
618 * @param id1 Peer that has disconnected.
619 * @param id2 Peer that has disconnected.
620 * @param fwd Direction towards which to send it.
623 send_broken (struct MeshConnection *c,
624 const struct GNUNET_PeerIdentity *id1,
625 const struct GNUNET_PeerIdentity *id2,
628 struct GNUNET_MESH_ConnectionBroken msg;
630 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
631 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
635 GMC_send_prebuilt_message (&msg.header, c, fwd);
641 * Send keepalive packets for a connection.
643 * @param c Connection to keep alive..
644 * @param fwd Is this a FWD keepalive? (owner -> dest).
647 connection_keepalive (struct MeshConnection *c, int fwd)
649 struct GNUNET_MESH_ConnectionKeepAlive *msg;
650 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
654 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
655 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
657 LOG (GNUNET_ERROR_TYPE_DEBUG,
658 "sending %s keepalive for connection %s]\n",
659 fwd ? "FWD" : "BCK", GMC_2s (c));
661 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
662 msg->header.size = htons (size);
663 msg->header.type = htons (type);
666 GMC_send_prebuilt_message (&msg->header, c, fwd);
671 * Send CONNECTION_{CREATE/ACK} packets for a connection.
673 * @param c Connection for which to send the message.
674 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
677 connection_recreate (struct MeshConnection *c, int fwd)
679 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
683 send_connection_ack (c, GNUNET_NO);
688 * Generic connection timer management.
689 * Depending on the role of the peer in the connection will send the
690 * appropriate message (build or keepalive)
692 * @param c Conncetion to maintain.
696 connection_maintain (struct MeshConnection *c, int fwd)
698 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
700 /* TODO DHT GET with RO_BART */
705 case MESH_CONNECTION_NEW:
707 case MESH_CONNECTION_SENT:
708 connection_recreate (c, fwd);
710 case MESH_CONNECTION_READY:
711 connection_keepalive (c, fwd);
720 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
722 struct MeshConnection *c = cls;
724 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
725 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
728 connection_maintain (c, GNUNET_YES);
729 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
730 &connection_fwd_keepalive,
736 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
738 struct MeshConnection *c = cls;
740 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
741 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
744 connection_maintain (c, GNUNET_NO);
745 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
746 &connection_bck_keepalive,
752 * @brief Re-initiate traffic on this connection if necessary.
754 * Check if there is traffic queued towards this peer
755 * and the core transmit handle is NULL (traffic was stalled).
756 * If so, call core tmt rdy.
758 * @param c Connection on which initiate traffic.
759 * @param fwd Is this about fwd traffic?
762 connection_unlock_queue (struct MeshConnection *c, int fwd)
764 struct MeshPeer *peer;
766 LOG (GNUNET_ERROR_TYPE_DEBUG,
767 "connection_unlock_queue %s on %s\n",
768 fwd ? "FWD" : "BCK", GMC_2s (c));
770 if (GMC_is_terminal (c, fwd))
772 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
776 peer = get_hop (c, fwd);
777 GMP_queue_unlock (peer, c);
782 * Cancel all transmissions that belong to a certain connection.
784 * If the connection is scheduled for destruction and no more messages are left,
785 * the connection will be destroyed by the continuation call.
787 * @param c Connection which to cancel. Might be destroyed during this call.
788 * @param fwd Cancel fwd traffic?
791 connection_cancel_queues (struct MeshConnection *c, int fwd)
793 struct MeshFlowControl *fc;
794 struct MeshPeer *peer;
802 fc = fwd ? &c->fwd_fc : &c->bck_fc;
803 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
805 GNUNET_SCHEDULER_cancel (fc->poll_task);
806 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
808 peer = get_hop (c, fwd);
809 GMP_queue_cancel (peer, c);
814 * Function called if a connection has been stalled for a while,
815 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
817 * @param cls Closure (poll ctx).
818 * @param tc TaskContext.
821 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
823 struct MeshFlowControl *fc = cls;
824 struct GNUNET_MESH_Poll msg;
825 struct MeshConnection *c;
827 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
828 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
834 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
835 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
836 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
837 fc == &c->fwd_fc ? "FWD" : "BCK");
839 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
840 msg.header.size = htons (sizeof (msg));
841 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
842 GMC_send_prebuilt_message (&msg.header, c, fc == &c->fwd_fc);
843 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
844 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
845 &connection_poll, fc);
850 * Timeout function due to lack of keepalive/traffic from the owner.
851 * Destroys connection if called.
853 * @param cls Closure (connection to destroy).
854 * @param tc TaskContext.
857 connection_fwd_timeout (void *cls,
858 const struct GNUNET_SCHEDULER_TaskContext *tc)
860 struct MeshConnection *c = cls;
862 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
863 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
865 LOG (GNUNET_ERROR_TYPE_DEBUG,
866 "Connection %s[%X] FWD timed out. Destroying.\n",
870 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
878 * Timeout function due to lack of keepalive/traffic from the destination.
879 * Destroys connection if called.
881 * @param cls Closure (connection to destroy).
882 * @param tc TaskContext
885 connection_bck_timeout (void *cls,
886 const struct GNUNET_SCHEDULER_TaskContext *tc)
888 struct MeshConnection *c = cls;
890 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
891 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
894 LOG (GNUNET_ERROR_TYPE_DEBUG,
895 "Connection %s[%X] FWD timed out. Destroying.\n",
896 GMT_2s (c->t), c->id);
898 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
906 * Resets the connection timeout task, some other message has done the
908 * - For the first peer on the direction this means to send
909 * a keepalive or a path confirmation message (either create or ACK).
910 * - For all other peers, this means to destroy the connection,
911 * due to lack of activity.
912 * Starts the tiemout if no timeout was running (connection just created).
914 * @param c Connection whose timeout to reset.
915 * @param fwd Is this forward?
917 * TODO use heap to improve efficiency of scheduler.
920 connection_reset_timeout (struct MeshConnection *c, int fwd)
922 GNUNET_SCHEDULER_TaskIdentifier *ti;
923 GNUNET_SCHEDULER_Task f;
925 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
927 if (GNUNET_SCHEDULER_NO_TASK != *ti)
928 GNUNET_SCHEDULER_cancel (*ti);
930 if (GMC_is_origin (c, fwd)) /* Endpoint */
932 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
933 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
937 struct GNUNET_TIME_Relative delay;
939 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
940 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
941 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
947 * Add the connection to the list of both neighbors.
949 * @param c Connection.
952 register_neighbors (struct MeshConnection *c)
954 struct MeshPeer *peer;
956 peer = get_next_hop (c);
957 if (GNUNET_NO == GMP_is_neighbor (peer))
962 GMP_add_connection (peer, c);
963 peer = get_prev_hop (c);
964 if (GNUNET_NO == GMP_is_neighbor (peer))
969 GMP_add_connection (peer, c);
974 * Remove the connection from the list of both neighbors.
976 * @param c Connection.
979 unregister_neighbors (struct MeshConnection *c)
981 struct MeshPeer *peer;
983 peer = get_next_hop (c);
984 GMP_remove_connection (peer, c);
986 peer = get_prev_hop (c);
987 GMP_remove_connection (peer, c);
993 * Bind the connection to the peer and the tunnel to that peer.
995 * If the peer has no tunnel, create one. Update tunnel and connection
996 * data structres to reflect new status.
998 * @param c Connection.
1002 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1004 GMP_add_tunnel (peer);
1005 c->t = GMP_get_tunnel (peer);
1006 GMT_add_connection (c->t, c);
1009 /******************************************************************************/
1010 /******************************** API ***********************************/
1011 /******************************************************************************/
1014 * Core handler for connection creation.
1016 * @param cls Closure (unused).
1017 * @param peer Sender (neighbor).
1018 * @param message Message.
1020 * @return GNUNET_OK to keep the connection open,
1021 * GNUNET_SYSERR to close it (signal serious error)
1024 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1025 const struct GNUNET_MessageHeader *message)
1027 struct GNUNET_MESH_ConnectionCreate *msg;
1028 struct GNUNET_PeerIdentity *id;
1029 struct GNUNET_HashCode *cid;
1030 struct MeshPeerPath *path;
1031 struct MeshPeer *dest_peer;
1032 struct MeshPeer *orig_peer;
1033 struct MeshConnection *c;
1034 unsigned int own_pos;
1038 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1039 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1042 size = ntohs (message->size);
1043 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1045 GNUNET_break_op (0);
1049 /* Calculate hops */
1050 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1051 if (size % sizeof (struct GNUNET_PeerIdentity))
1053 GNUNET_break_op (0);
1056 size /= sizeof (struct GNUNET_PeerIdentity);
1059 GNUNET_break_op (0);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1064 /* Get parameters */
1065 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1067 id = (struct GNUNET_PeerIdentity *) &msg[1];
1068 LOG (GNUNET_ERROR_TYPE_DEBUG,
1069 " connection %s (%s).\n",
1070 GNUNET_h2s (cid), GNUNET_i2s (id));
1072 /* Create connection */
1073 c = connection_get (cid);
1077 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1078 path = path_new (size);
1080 for (i = 0; i < size; i++)
1082 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1083 GNUNET_i2s (&id[i]));
1084 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1085 if (path->peers[i] == myid)
1088 if (own_pos == 0 && path->peers[own_pos] != myid)
1090 /* create path: self not found in path through self */
1091 GNUNET_break_op (0);
1092 path_destroy (path);
1095 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1096 GMP_add_path_to_all (path, GNUNET_NO);
1097 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1098 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1101 connection_reset_timeout (c, GNUNET_YES);
1107 if (MESH_CONNECTION_NEW == c->state)
1108 connection_change_state (c, MESH_CONNECTION_SENT);
1110 /* Remember peers */
1111 dest_peer = GMP_get (&id[size - 1]);
1112 orig_peer = GMP_get (&id[0]);
1114 /* Is it a connection to us? */
1115 if (c->own_pos == size - 1)
1117 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1118 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1120 add_to_peer (c, orig_peer);
1121 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1122 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1124 send_connection_ack (c, GNUNET_NO);
1125 if (MESH_CONNECTION_SENT == c->state)
1126 connection_change_state (c, MESH_CONNECTION_ACK);
1128 /* Keep tunnel alive in direction dest->owner*/
1129 connection_reset_timeout (c, GNUNET_NO);
1133 /* It's for somebody else! Retransmit. */
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1135 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1136 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1137 GMC_send_prebuilt_message (message, c, GNUNET_YES);
1144 * Core handler for path confirmations.
1146 * @param cls closure
1147 * @param message message
1148 * @param peer peer identity this notification is about
1150 * @return GNUNET_OK to keep the connection open,
1151 * GNUNET_SYSERR to close it (signal serious error)
1154 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1155 const struct GNUNET_MessageHeader *message)
1157 struct GNUNET_MESH_ConnectionACK *msg;
1158 struct MeshConnection *c;
1159 struct MeshPeerPath *p;
1160 struct MeshPeer *pi;
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1164 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1165 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1166 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1167 GNUNET_h2s (&msg->cid));
1168 c = connection_get (&msg->cid);
1171 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1173 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1178 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1180 pi = GMP_get (peer);
1181 if (get_next_hop (c) == pi)
1183 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1185 if (MESH_CONNECTION_SENT == c->state)
1186 connection_change_state (c, MESH_CONNECTION_ACK);
1188 else if (get_prev_hop (c) == pi)
1190 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1192 connection_change_state (c, MESH_CONNECTION_READY);
1196 GNUNET_break_op (0);
1199 connection_reset_timeout (c, fwd);
1201 /* Add path to peers? */
1205 GMP_add_path_to_all (p, GNUNET_YES);
1212 /* Message for us as creator? */
1213 if (GMC_is_origin (c, GNUNET_YES))
1215 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1216 connection_change_state (c, MESH_CONNECTION_READY);
1217 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1218 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1219 send_connection_ack (c, GNUNET_YES);
1223 /* Message for us as destination? */
1224 if (GMC_is_terminal (c, GNUNET_YES))
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1227 connection_change_state (c, MESH_CONNECTION_READY);
1228 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1229 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1234 GMC_send_prebuilt_message (message, c, fwd);
1240 * Core handler for notifications of broken paths
1242 * @param cls Closure (unused).
1243 * @param id Peer identity of sending neighbor.
1244 * @param message Message.
1246 * @return GNUNET_OK to keep the connection open,
1247 * GNUNET_SYSERR to close it (signal serious error)
1250 GMC_handle_broken (void* cls,
1251 const struct GNUNET_PeerIdentity* id,
1252 const struct GNUNET_MessageHeader* message)
1254 struct GNUNET_MESH_ConnectionBroken *msg;
1255 struct MeshConnection *c;
1258 LOG (GNUNET_ERROR_TYPE_DEBUG,
1259 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1260 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1261 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1262 GNUNET_i2s (&msg->peer1));
1263 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1264 GNUNET_i2s (&msg->peer2));
1265 c = connection_get (&msg->cid);
1268 GNUNET_break_op (0);
1272 fwd = is_fwd (c, id);
1273 connection_cancel_queues (c, !fwd);
1274 if (GMC_is_terminal (c, fwd))
1276 if (0 < c->pending_messages)
1277 c->destroy = GNUNET_YES;
1283 GMC_send_prebuilt_message (message, c, fwd);
1284 c->destroy = GNUNET_YES;
1293 * Core handler for tunnel destruction
1295 * @param cls Closure (unused).
1296 * @param peer Peer identity of sending neighbor.
1297 * @param message Message.
1299 * @return GNUNET_OK to keep the connection open,
1300 * GNUNET_SYSERR to close it (signal serious error)
1303 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1304 const struct GNUNET_MessageHeader *message)
1306 struct GNUNET_MESH_ConnectionDestroy *msg;
1307 struct MeshConnection *c;
1310 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1311 LOG (GNUNET_ERROR_TYPE_DEBUG,
1312 "Got a CONNECTION DESTROY message from %s\n",
1314 LOG (GNUNET_ERROR_TYPE_DEBUG,
1315 " for connection %s\n",
1316 GNUNET_h2s (&msg->cid));
1317 c = connection_get (&msg->cid);
1320 /* Probably already got the message from another path,
1321 * destroyed the tunnel and retransmitted to children.
1324 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1328 fwd = is_fwd (c, peer);
1329 if (GNUNET_SYSERR == fwd)
1331 GNUNET_break_op (0);
1334 GMC_send_prebuilt_message (message, c, fwd);
1335 c->destroy = GNUNET_YES;
1341 * Generic handler for mesh network encrypted traffic.
1343 * @param peer Peer identity this notification is about.
1344 * @param msg Encrypted message.
1346 * @return GNUNET_OK to keep the connection open,
1347 * GNUNET_SYSERR to close it (signal serious error)
1350 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1351 const struct GNUNET_MESH_Encrypted *msg)
1353 struct MeshConnection *c;
1354 struct MeshPeer *neighbor;
1355 struct MeshFlowControl *fc;
1356 GNUNET_PEER_Id peer_id;
1364 size = ntohs (msg->header.size);
1366 sizeof (struct GNUNET_MESH_Encrypted) +
1367 sizeof (struct GNUNET_MessageHeader))
1369 GNUNET_break_op (0);
1372 type = ntohs (msg->header.type);
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1374 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1375 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1377 /* Check connection */
1378 c = connection_get (&msg->cid);
1381 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1382 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1386 /* Check if origin is as expected */
1387 neighbor = get_prev_hop (c);
1388 peer_id = GNUNET_PEER_search (peer);
1389 if (peer_id == GMP_get_short_id (neighbor))
1395 neighbor = get_next_hop (c);
1396 if (peer_id == GMP_get_short_id (neighbor))
1402 /* Unexpected peer sending traffic on a connection. */
1403 GNUNET_break_op (0);
1409 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1410 pid = ntohl (msg->pid);
1411 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1413 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1414 LOG (GNUNET_ERROR_TYPE_DEBUG,
1415 "WARNING Received PID %u, (prev %u), ACK %u\n",
1416 pid, fc->last_pid_recv, fc->last_ack_sent);
1419 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1421 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1422 LOG (GNUNET_ERROR_TYPE_DEBUG,
1423 " Pid %u not expected (%u+), dropping!\n",
1424 pid, fc->last_pid_recv + 1);
1427 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1428 connection_change_state (c, MESH_CONNECTION_READY);
1429 connection_reset_timeout (c, fwd);
1430 fc->last_pid_recv = pid;
1432 /* Is this message for us? */
1433 if (GMC_is_terminal (c, fwd))
1435 /* TODO signature verification */
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1437 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1444 fc->last_pid_recv = pid;
1445 GMT_handle_encrypted (c->t, msg);
1446 GMC_send_ack (c, fwd);
1450 /* Message not for us: forward to next hop */
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1452 ttl = ntohl (msg->ttl);
1453 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1456 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1457 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1458 GMC_send_ack (c, fwd);
1461 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1463 GMC_send_prebuilt_message (&msg->header, c, fwd);
1469 * Generic handler for mesh network encrypted traffic.
1471 * @param peer Peer identity this notification is about.
1472 * @param msg Encrypted message.
1474 * @return GNUNET_OK to keep the connection open,
1475 * GNUNET_SYSERR to close it (signal serious error)
1478 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1479 const struct GNUNET_MESH_KX *msg)
1481 struct MeshConnection *c;
1482 struct MeshPeer *neighbor;
1483 GNUNET_PEER_Id peer_id;
1489 size = ntohs (msg->header.size);
1491 sizeof (struct GNUNET_MESH_Encrypted) +
1492 sizeof (struct GNUNET_MessageHeader))
1494 GNUNET_break_op (0);
1497 type = ntohs (msg->header.type);
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1499 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1500 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1502 /* Check connection */
1503 c = connection_get (&msg->cid);
1506 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1507 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1511 /* Check if origin is as expected */
1512 neighbor = get_prev_hop (c);
1513 peer_id = GNUNET_PEER_search (peer);
1514 if (peer_id == GMP_get_short_id (neighbor))
1520 neighbor = get_next_hop (c);
1521 if (peer_id == GMP_get_short_id (neighbor))
1527 /* Unexpected peer sending traffic on a connection. */
1528 GNUNET_break_op (0);
1533 /* Count as connection confirmation. */
1534 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1535 connection_change_state (c, MESH_CONNECTION_READY);
1536 connection_reset_timeout (c, fwd);
1539 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1540 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1543 /* Is this message for us? */
1544 if (GMC_is_terminal (c, fwd))
1546 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1547 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1553 GMT_handle_kx (c->t, &msg[1].header);
1557 /* Message not for us: forward to next hop */
1558 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1559 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1561 GMC_send_prebuilt_message (&msg->header, c, fwd);
1568 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1570 * @param cls Closure (unused).
1571 * @param message Message received.
1572 * @param peer Peer who sent the message.
1574 * @return GNUNET_OK to keep the connection open,
1575 * GNUNET_SYSERR to close it (signal serious error)
1578 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1579 const struct GNUNET_MessageHeader *message)
1581 return handle_mesh_encrypted (peer,
1582 (struct GNUNET_MESH_Encrypted *)message);
1587 * Core handler for key exchange traffic (ephemeral key, ping, pong).
1589 * @param cls Closure (unused).
1590 * @param message Message received.
1591 * @param peer Peer who sent the message.
1593 * @return GNUNET_OK to keep the connection open,
1594 * GNUNET_SYSERR to close it (signal serious error)
1597 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1598 const struct GNUNET_MessageHeader *message)
1600 return handle_mesh_kx (peer,
1601 (struct GNUNET_MESH_KX *) message);
1606 * Core handler for mesh network traffic point-to-point acks.
1608 * @param cls closure
1609 * @param message message
1610 * @param peer peer identity this notification is about
1612 * @return GNUNET_OK to keep the connection open,
1613 * GNUNET_SYSERR to close it (signal serious error)
1616 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1617 const struct GNUNET_MessageHeader *message)
1619 struct GNUNET_MESH_ACK *msg;
1620 struct MeshConnection *c;
1621 struct MeshFlowControl *fc;
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1627 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1629 msg = (struct GNUNET_MESH_ACK *) message;
1631 c = connection_get (&msg->cid);
1635 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1640 /* Is this a forward or backward ACK? */
1641 id = GNUNET_PEER_search (peer);
1642 if (GMP_get_short_id (get_next_hop (c)) == id)
1644 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1648 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1650 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1656 GNUNET_break_op (0);
1660 ack = ntohl (msg->ack);
1661 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1662 ack, fc->last_ack_recv);
1663 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1664 fc->last_ack_recv = ack;
1666 /* Cancel polling if the ACK is big enough. */
1667 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1668 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1671 GNUNET_SCHEDULER_cancel (fc->poll_task);
1672 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1673 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1676 connection_unlock_queue (c, fwd);
1683 * Core handler for mesh network traffic point-to-point ack polls.
1685 * @param cls closure
1686 * @param message message
1687 * @param peer peer identity this notification is about
1689 * @return GNUNET_OK to keep the connection open,
1690 * GNUNET_SYSERR to close it (signal serious error)
1693 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1694 const struct GNUNET_MessageHeader *message)
1696 struct GNUNET_MESH_Poll *msg;
1697 struct MeshConnection *c;
1698 struct MeshFlowControl *fc;
1703 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1704 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1707 msg = (struct GNUNET_MESH_Poll *) message;
1709 c = connection_get (&msg->cid);
1713 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1715 GNUNET_break_op (0);
1719 /* Is this a forward or backward ACK?
1720 * Note: a poll should never be needed in a loopback case,
1721 * since there is no possiblility of packet loss there, so
1722 * this way of discerining FWD/BCK should not be a problem.
1724 id = GNUNET_PEER_search (peer);
1725 if (GMP_get_short_id (get_next_hop (c)) == id)
1727 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1730 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1732 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1737 GNUNET_break_op (0);
1741 pid = ntohl (msg->pid);
1742 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1743 pid, fc->last_pid_recv);
1744 fc->last_pid_recv = pid;
1745 fwd = fc == &c->fwd_fc;
1746 GMC_send_ack (c, fwd);
1753 * Core handler for mesh keepalives.
1755 * @param cls closure
1756 * @param message message
1757 * @param peer peer identity this notification is about
1758 * @return GNUNET_OK to keep the connection open,
1759 * GNUNET_SYSERR to close it (signal serious error)
1761 * TODO: Check who we got this from, to validate route.
1764 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1765 const struct GNUNET_MessageHeader *message)
1767 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1768 struct MeshConnection *c;
1769 struct MeshPeer *neighbor;
1772 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1773 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1776 c = connection_get (&msg->cid);
1779 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1784 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1785 GNUNET_YES : GNUNET_NO;
1787 /* Check if origin is as expected */
1788 neighbor = get_hop (c, fwd);
1789 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1791 GNUNET_break_op (0);
1795 connection_change_state (c, MESH_CONNECTION_READY);
1796 connection_reset_timeout (c, fwd);
1798 if (GMC_is_terminal (c, fwd))
1801 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1802 GMC_send_prebuilt_message (message, c, fwd);
1809 * Send an ACK on the appropriate connection/channel, depending on
1810 * the direction and the position of the peer.
1812 * @param c Which connection to send the hop-by-hop ACK.
1813 * @param fwd Is this a fwd ACK? (will go dest->root)
1816 GMC_send_ack (struct MeshConnection *c, int fwd)
1818 unsigned int buffer;
1820 LOG (GNUNET_ERROR_TYPE_DEBUG,
1821 "GMC send %s ACK on %s\n",
1822 fwd ? "FWD" : "BCK", GMC_2s (c));
1830 /* Get available buffer space */
1831 if (GMC_is_terminal (c, fwd))
1833 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
1834 buffer = GMT_get_channels_buffer (c->t);
1838 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1839 buffer = GMC_get_buffer (c, fwd);
1841 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1843 /* Send available buffer space */
1844 if (GMC_is_origin (c, fwd))
1846 GNUNET_assert (NULL != c->t);
1847 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
1850 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1851 GMT_unchoke_channels (c->t);
1856 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1857 send_ack (c, buffer, fwd);
1863 * Initialize the connections subsystem
1865 * @param c Configuration handle.
1868 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1870 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1872 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1875 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1876 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1877 GNUNET_SCHEDULER_shutdown ();
1882 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1885 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1886 "MESH", "MAX_CONNECTIONS", "MISSING");
1887 GNUNET_SCHEDULER_shutdown ();
1892 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1893 &refresh_connection_time))
1895 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1896 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1897 GNUNET_SCHEDULER_shutdown ();
1900 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1904 * Shut down the connections subsystem.
1909 GNUNET_CONTAINER_multihashmap_destroy (connections);
1913 struct MeshConnection *
1914 GMC_new (const struct GNUNET_HashCode *cid,
1915 struct MeshTunnel3 *t,
1916 struct MeshPeerPath *p,
1917 unsigned int own_pos)
1919 struct MeshConnection *c;
1921 c = GNUNET_new (struct MeshConnection);
1923 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1924 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1925 fc_init (&c->fwd_fc);
1926 fc_init (&c->bck_fc);
1931 if (own_pos > p->length - 1)
1937 c->own_pos = own_pos;
1942 c->fwd_maintenance_task =
1943 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1944 &connection_fwd_keepalive, c);
1946 register_neighbors (c);
1952 GMC_destroy (struct MeshConnection *c)
1957 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
1958 return; /* -> message_sent -> GMC_destroy. Don't loop. */
1961 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
1963 /* Cancel all traffic */
1964 connection_cancel_queues (c, GNUNET_YES);
1965 connection_cancel_queues (c, GNUNET_NO);
1967 /* Cancel maintainance task (keepalive/timeout) */
1968 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1969 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1970 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1971 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1973 /* Unregister from neighbors */
1974 unregister_neighbors (c);
1977 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1979 GMT_remove_connection (c->t, c);
1981 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES))
1982 path_destroy (c->path);
1988 * Get the connection ID.
1990 * @param c Connection to get the ID from.
1992 * @return ID of the connection.
1994 const struct GNUNET_HashCode *
1995 GMC_get_id (const struct MeshConnection *c)
2002 * Get the connection path.
2004 * @param c Connection to get the path from.
2006 * @return path used by the connection.
2008 const struct MeshPeerPath *
2009 GMC_get_path (const struct MeshConnection *c)
2016 * Get the connection state.
2018 * @param c Connection to get the state from.
2020 * @return state of the connection.
2022 enum MeshConnectionState
2023 GMC_get_state (const struct MeshConnection *c)
2029 * Get the connection tunnel.
2031 * @param c Connection to get the tunnel from.
2033 * @return tunnel of the connection.
2035 struct MeshTunnel3 *
2036 GMC_get_tunnel (const struct MeshConnection *c)
2043 * Get free buffer space in a connection.
2045 * @param c Connection.
2046 * @param fwd Is query about FWD traffic?
2048 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2051 GMC_get_buffer (struct MeshConnection *c, int fwd)
2053 struct MeshFlowControl *fc;
2055 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2057 return (fc->queue_max - fc->queue_n);
2061 * Get how many messages have we allowed to send to us from a direction.
2063 * @param c Connection.
2064 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2066 * @return last_ack_sent - last_pid_recv
2069 GMC_get_allowed (struct MeshConnection *c, int fwd)
2071 struct MeshFlowControl *fc;
2073 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2074 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2078 return (fc->last_ack_sent - fc->last_pid_recv);
2082 * Get messages queued in a connection.
2084 * @param c Connection.
2085 * @param fwd Is query about FWD traffic?
2087 * @return Number of messages queued.
2090 GMC_get_qn (struct MeshConnection *c, int fwd)
2092 struct MeshFlowControl *fc;
2094 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2101 * Allow the connection to advertise a buffer of the given size.
2103 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2104 * allowing up to last_pid_recv + buffer.
2106 * @param c Connection.
2107 * @param buffer How many more messages the connection can accept.
2108 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2111 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2113 send_ack (c, buffer, fwd);
2118 * Notify other peers on a connection of a broken link. Mark connections
2119 * to destroy after all traffic has been sent.
2121 * @param c Connection on which there has been a disconnection.
2122 * @param peer Peer that disconnected.
2125 GMC_notify_broken (struct MeshConnection *c,
2126 struct MeshPeer *peer)
2130 fwd = peer == get_prev_hop (c);
2132 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2134 /* Local shutdown, no one to notify about this. */
2138 if (GNUNET_NO == c->destroy)
2139 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2141 /* Connection will have at least one pending message
2142 * (the one we just scheduled), so no point in checking whether to
2143 * destroy immediately. */
2144 c->destroy = GNUNET_YES;
2147 * Cancel all queues, if no message is left, connection will be destroyed.
2149 connection_cancel_queues (c, !fwd);
2156 * Is this peer the first one on the connection?
2158 * @param c Connection.
2159 * @param fwd Is this about fwd traffic?
2161 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2164 GMC_is_origin (struct MeshConnection *c, int fwd)
2166 if (!fwd && c->path->length - 1 == c->own_pos )
2168 if (fwd && 0 == c->own_pos)
2175 * Is this peer the last one on the connection?
2177 * @param c Connection.
2178 * @param fwd Is this about fwd traffic?
2179 * Note that the ROOT is the terminal for BCK traffic!
2181 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2184 GMC_is_terminal (struct MeshConnection *c, int fwd)
2186 return GMC_is_origin (c, !fwd);
2191 * See if we are allowed to send by the next hop in the given direction.
2193 * @param c Connection.
2194 * @param fwd Is this about fwd traffic?
2196 * @return #GNUNET_YES in case it's OK to send.
2199 GMC_is_sendable (struct MeshConnection *c, int fwd)
2201 struct MeshFlowControl *fc;
2203 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2204 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2210 * Sends an already built message on a connection, properly registering
2211 * all used resources.
2213 * @param message Message to send. Function makes a copy of it.
2214 * If message is not hop-by-hop, decrements TTL of copy.
2215 * @param c Connection on which this message is transmitted.
2216 * @param fwd Is this a fwd message?
2219 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2220 struct MeshConnection *c,
2223 struct MeshFlowControl *fc;
2229 size = ntohs (message->size);
2230 data = GNUNET_malloc (size);
2231 memcpy (data, message, size);
2232 type = ntohs (message->type);
2233 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2234 GNUNET_MESH_DEBUG_M2S (type), size, GMC_2s (c));
2236 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2237 droppable = GNUNET_YES;
2240 struct GNUNET_MESH_Encrypted *emsg;
2241 struct GNUNET_MESH_KX *kmsg;
2242 struct GNUNET_MESH_ACK *amsg;
2243 struct GNUNET_MESH_Poll *pmsg;
2244 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2245 struct GNUNET_MESH_ConnectionBroken *bmsg;
2248 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2249 emsg = (struct GNUNET_MESH_Encrypted *) data;
2250 ttl = ntohl (emsg->ttl);
2253 GNUNET_break_op (0);
2257 emsg->ttl = htonl (ttl - 1);
2258 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2259 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2261 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2262 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
2263 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
2264 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2266 GMC_start_poll (c, fwd);
2270 case GNUNET_MESSAGE_TYPE_MESH_KX:
2271 kmsg = (struct GNUNET_MESH_KX *) data;
2275 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2276 amsg = (struct GNUNET_MESH_ACK *) data;
2278 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2279 droppable = GNUNET_NO;
2282 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2283 pmsg = (struct GNUNET_MESH_Poll *) data;
2285 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2286 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2287 droppable = GNUNET_NO;
2290 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2291 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2296 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2297 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2302 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2303 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2310 if (fc->queue_n > fc->queue_max && droppable)
2312 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2315 LOG (GNUNET_ERROR_TYPE_DEBUG,
2316 "queue full: %u/%u\n",
2317 fc->queue_n, fc->queue_max);
2318 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2320 return; /* Drop this message */
2323 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2324 c->pending_messages++;
2326 GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2327 &message_sent, NULL);
2332 * Sends a CREATE CONNECTION message for a path to a peer.
2333 * Changes the connection and tunnel states if necessary.
2335 * @param connection Connection to create.
2338 GMC_send_create (struct MeshConnection *connection)
2340 enum MeshTunnel3State state;
2343 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2344 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2345 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2346 GMP_queue_add (get_next_hop (connection), NULL,
2347 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2348 size, connection, GNUNET_YES, &message_sent, NULL);
2349 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2350 connection, connection->pending_messages);
2351 connection->pending_messages++;
2352 state = GMT_get_state (connection->t);
2353 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2354 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2355 if (MESH_CONNECTION_NEW == connection->state)
2356 connection_change_state (connection, MESH_CONNECTION_SENT);
2361 * Send a message to all peers in this connection that the connection
2362 * is no longer valid.
2364 * If some peer should not receive the message, it should be zero'ed out
2365 * before calling this function.
2367 * @param c The connection whose peers to notify.
2370 GMC_send_destroy (struct MeshConnection *c)
2372 struct GNUNET_MESH_ConnectionDestroy msg;
2374 if (GNUNET_YES == c->destroy)
2377 msg.header.size = htons (sizeof (msg));
2378 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2380 LOG (GNUNET_ERROR_TYPE_DEBUG,
2381 " sending connection destroy for connection %s\n",
2384 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2385 GMC_send_prebuilt_message (&msg.header, c, GNUNET_YES);
2386 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2387 GMC_send_prebuilt_message (&msg.header, c, GNUNET_NO);
2388 c->destroy = GNUNET_YES;
2393 * @brief Start a polling timer for the connection.
2395 * When a neighbor does not accept more traffic on the connection it could be
2396 * caused by a simple congestion or by a lost ACK. Polling enables to check
2397 * for the lastest ACK status for a connection.
2399 * @param c Connection.
2400 * @param fwd Should we poll in the FWD direction?
2403 GMC_start_poll (struct MeshConnection *c, int fwd)
2405 struct MeshFlowControl *fc;
2407 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2408 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2412 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2419 * @brief Stop polling a connection for ACKs.
2421 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2423 * @param c Connection.
2424 * @param fwd Should we stop the poll in the FWD direction?
2427 GMC_stop_poll (struct MeshConnection *c, int fwd)
2429 struct MeshFlowControl *fc;
2431 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2432 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2434 GNUNET_SCHEDULER_cancel (fc->poll_task);
2435 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2440 * Get a (static) string for a connection.
2442 * @param c Connection.
2445 GMC_2s (struct MeshConnection *c)
2449 static char buf[128];
2451 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2454 return GNUNET_h2s (&c->id);