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"
38 #include "gnunet-service-mesh_channel.h"
41 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
43 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
44 GNUNET_TIME_UNIT_MINUTES,\
49 /******************************************************************************/
50 /******************************** STRUCTS **********************************/
51 /******************************************************************************/
54 * Struct to encapsulate all the Flow Control information to a peer to which
55 * we are directly connected (on a core level).
57 struct MeshFlowControl
60 * Connection this controls.
62 struct MeshConnection *c;
65 * How many messages are in the queue on this connection.
70 * How many messages do we accept in the queue.
72 unsigned int queue_max;
80 * ID of the last packet sent towards the peer.
82 uint32_t last_pid_sent;
85 * ID of the last packet received from the peer.
87 uint32_t last_pid_recv;
90 * Last ACK sent to the peer (peer can't send more than this PID).
92 uint32_t last_ack_sent;
95 * Last ACK sent towards the origin (for traffic towards leaf node).
97 uint32_t last_ack_recv;
100 * Task to poll the peer in case of a lost ACK causes stall.
102 GNUNET_SCHEDULER_TaskIdentifier poll_task;
105 * How frequently to poll for ACKs.
107 struct GNUNET_TIME_Relative poll_time;
111 * Keep a record of the last messages sent on this connection.
113 struct MeshConnectionPerformance
116 * Circular buffer for storing measurements.
118 double usecsperbyte[AVG_MSGS];
121 * Running average of @c usecsperbyte.
126 * How many values of @c usecsperbyte are valid.
131 * Index of the next "free" position in @c usecsperbyte.
138 * Struct containing all information regarding a connection to a peer.
140 struct MeshConnection
143 * Tunnel this connection is part of.
145 struct MeshTunnel3 *t;
148 * Flow control information for traffic fwd.
150 struct MeshFlowControl fwd_fc;
153 * Flow control information for traffic bck.
155 struct MeshFlowControl bck_fc;
158 * Measure connection performance on the endpoint.
160 struct MeshConnectionPerformance *perf;
163 * ID of the connection.
165 struct GNUNET_HashCode id;
168 * State of the connection.
170 enum MeshConnectionState state;
173 * Path being used for the tunnel.
175 struct MeshPeerPath *path;
178 * Position of the local peer in the path.
180 unsigned int own_pos;
183 * Task to keep the used paths alive at the owner,
184 * time tunnel out on all the other peers.
186 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
189 * Task to keep the used paths alive at the destination,
190 * time tunnel out on all the other peers.
192 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
195 * Pending message count.
197 int pending_messages;
200 * Destroy flag: if true, destroy on last message.
205 /******************************************************************************/
206 /******************************* GLOBALS ***********************************/
207 /******************************************************************************/
210 * Global handle to the statistics service.
212 extern struct GNUNET_STATISTICS_Handle *stats;
215 * Local peer own ID (memory efficient handle).
217 extern GNUNET_PEER_Id myid;
220 * Local peer own ID (full value).
222 extern struct GNUNET_PeerIdentity my_full_id;
225 * Connections known, indexed by cid (MeshConnection).
227 static struct GNUNET_CONTAINER_MultiHashMap *connections;
230 * How many connections are we willing to maintain.
231 * Local connections are always allowed, even if there are more connections than max.
233 static unsigned long long max_connections;
236 * How many messages *in total* are we willing to queue, divide by number of
237 * connections to get connection queue size.
239 static unsigned long long max_msgs_queue;
242 * How often to send path keepalives. Paths timeout after 4 missed.
244 static struct GNUNET_TIME_Relative refresh_connection_time;
247 /******************************************************************************/
248 /******************************** STATIC ***********************************/
249 /******************************************************************************/
251 #if 0 // avoid compiler warning for unused static function
253 fc_debug (struct MeshFlowControl *fc)
255 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
256 fc->last_pid_recv, fc->last_ack_sent);
257 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
258 fc->last_pid_sent, fc->last_ack_recv);
259 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
260 fc->queue_n, fc->queue_max);
264 connection_debug (struct MeshConnection *c)
268 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
271 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
272 peer2s (c->t->peer), GMC_2s (c));
273 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
274 c->state, c->pending_messages);
275 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
276 fc_debug (&c->fwd_fc);
277 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
278 fc_debug (&c->bck_fc);
283 * Get string description for tunnel state.
285 * @param s Tunnel state.
287 * @return String representation.
290 GMC_state2s (enum MeshConnectionState s)
294 case MESH_CONNECTION_NEW:
295 return "MESH_CONNECTION_NEW";
296 case MESH_CONNECTION_SENT:
297 return "MESH_CONNECTION_SENT";
298 case MESH_CONNECTION_ACK:
299 return "MESH_CONNECTION_ACK";
300 case MESH_CONNECTION_READY:
301 return "MESH_CONNECTION_READY";
303 return "MESH_CONNECTION_STATE_ERROR";
309 * Initialize a Flow Control structure to the initial state.
311 * @param fc Flow Control structure to initialize.
314 fc_init (struct MeshFlowControl *fc)
317 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
318 fc->last_pid_recv = (uint32_t) -1;
319 fc->last_ack_sent = (uint32_t) 0;
320 fc->last_ack_recv = (uint32_t) 0;
321 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
322 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
324 fc->queue_max = (max_msgs_queue / max_connections) + 1;
331 * @param cid Connection ID.
333 static struct MeshConnection *
334 connection_get (const struct GNUNET_HashCode *cid)
336 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
341 connection_change_state (struct MeshConnection* c,
342 enum MeshConnectionState state)
344 LOG (GNUNET_ERROR_TYPE_DEBUG,
345 "Connection %s state was %s\n",
346 GMC_2s (c), GMC_state2s (c->state));
347 LOG (GNUNET_ERROR_TYPE_DEBUG,
348 "Connection %s state is now %s\n",
349 GMC_2s (c), GMC_state2s (state));
355 * Send an ACK on the connection, informing the predecessor about
356 * the available buffer space. Should not be called in case the peer
357 * is origin (no predecessor).
359 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
360 * the ACK itself goes "back" (dest->root).
362 * @param c Connection on which to send the ACK.
363 * @param buffer How much space free to advertise?
364 * @param fwd Is this FWD ACK? (Going dest->owner)
367 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
369 struct MeshFlowControl *next_fc;
370 struct MeshFlowControl *prev_fc;
371 struct GNUNET_MESH_ACK msg;
375 /* If origin, there is no connection to send ACKs. Wrong function! */
376 if (GMC_is_origin (c, fwd))
382 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
383 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
385 LOG (GNUNET_ERROR_TYPE_DEBUG,
386 "connection send %s ack on %s\n",
387 fwd ? "FWD" : "BCK", GMC_2s (c));
389 /* Check if we need to transmit the ACK */
390 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
391 if (3 < delta && buffer < delta)
393 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
394 LOG (GNUNET_ERROR_TYPE_DEBUG,
395 " last pid recv: %u, last ack sent: %u\n",
396 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
400 /* Ok, ACK might be necessary, what PID to ACK? */
401 ack = prev_fc->last_pid_recv + buffer;
402 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
403 LOG (GNUNET_ERROR_TYPE_DEBUG,
404 " last pid %u, last ack %u, qmax %u, q %u\n",
405 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
406 next_fc->queue_max, next_fc->queue_n);
407 if (ack == prev_fc->last_ack_sent)
409 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
413 prev_fc->last_ack_sent = ack;
415 /* Build ACK message and send on connection */
416 msg.header.size = htons (sizeof (msg));
417 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
418 msg.ack = htonl (ack);
421 GMC_send_prebuilt_message (&msg.header, c, NULL, !fwd);
426 * Callback called when a queued message is sent.
428 * Calculates the average time and connection packet tracking.
430 * @param cls Closure.
431 * @param c Connection this message was on.
432 * @param type Type of message sent.
433 * @param fwd Was this a FWD going message?
434 * @param size Size of the message.
435 * @param wait Time spent waiting for core (only the time for THIS message)
438 message_sent (void *cls,
439 struct MeshConnection *c, uint16_t type,
440 int fwd, size_t size,
441 struct GNUNET_TIME_Relative wait)
443 struct MeshConnectionPerformance *p;
444 struct MeshFlowControl *fc;
447 fc = fwd ? &c->fwd_fc : &c->bck_fc;
448 LOG (GNUNET_ERROR_TYPE_DEBUG, "! sent %s\n", GNUNET_MESH_DEBUG_M2S (type));
449 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
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;
498 // c->t->pending_messages--;
499 // if (GNUNET_YES == c->t->destroy && 0 == t->pending_messages)
501 // LOG (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
502 // GMT_destroy (c->t);
509 * Get the previous hop in a connection
511 * @param c Connection.
513 * @return Previous peer in the connection.
515 static struct MeshPeer *
516 get_prev_hop (const struct MeshConnection *c)
520 if (0 == c->own_pos || c->path->length < 2)
521 id = c->path->peers[0];
523 id = c->path->peers[c->own_pos - 1];
525 return GMP_get_short (id);
530 * Get the next hop in a connection
532 * @param c Connection.
534 * @return Next peer in the connection.
536 static struct MeshPeer *
537 get_next_hop (const struct MeshConnection *c)
541 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
542 id = c->path->peers[c->path->length - 1];
544 id = c->path->peers[c->own_pos + 1];
546 return GMP_get_short (id);
551 * Get the hop in a connection.
553 * @param c Connection.
554 * @param fwd Next hop?
556 * @return Next peer in the connection.
558 static struct MeshPeer *
559 get_hop (struct MeshConnection *c, int fwd)
562 return get_next_hop (c);
563 return get_prev_hop (c);
568 * Is traffic coming from this sender 'FWD' traffic?
570 * @param c Connection to check.
571 * @param sender Peer identity of neighbor.
573 * @return GNUNET_YES in case the sender is the 'prev' hop and therefore
574 * the traffic is 'FWD'. GNUNET_NO for BCK. GNUNET_SYSERR for errors.
577 is_fwd (const struct MeshConnection *c,
578 const struct GNUNET_PeerIdentity *sender)
582 id = GNUNET_PEER_search (sender);
583 if (GMP_get_short_id (get_prev_hop (c)) == id)
586 if (GMP_get_short_id (get_next_hop (c)) == id)
590 return GNUNET_SYSERR;
595 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
596 * or a first CONNECTION_ACK directed to us.
598 * @param connection Connection to confirm.
599 * @param fwd Should we send it FWD?
600 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
603 send_connection_ack (struct MeshConnection *connection, int fwd)
605 struct MeshTunnel3 *t;
608 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
609 GMP_queue_add (get_hop (connection, fwd), NULL,
610 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
611 sizeof (struct GNUNET_MESH_ConnectionACK),
612 connection, NULL, fwd,
613 &message_sent, NULL);
614 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
615 GMT_change_state (t, MESH_TUNNEL3_WAITING);
616 if (MESH_CONNECTION_READY != connection->state)
617 connection_change_state (connection, MESH_CONNECTION_SENT);
622 * Send a notification that a connection is broken.
624 * @param c Connection that is broken.
625 * @param id1 Peer that has disconnected.
626 * @param id2 Peer that has disconnected.
627 * @param fwd Direction towards which to send it.
630 send_broken (struct MeshConnection *c,
631 const struct GNUNET_PeerIdentity *id1,
632 const struct GNUNET_PeerIdentity *id2,
635 struct GNUNET_MESH_ConnectionBroken msg;
637 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
638 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
642 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
648 * Send keepalive packets for a connection.
650 * @param c Connection to keep alive..
651 * @param fwd Is this a FWD keepalive? (owner -> dest).
654 connection_keepalive (struct MeshConnection *c, int fwd)
656 struct GNUNET_MESH_ConnectionKeepAlive *msg;
657 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
661 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
662 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
664 LOG (GNUNET_ERROR_TYPE_DEBUG,
665 "sending %s keepalive for connection %s]\n",
666 fwd ? "FWD" : "BCK", GMC_2s (c));
668 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
669 msg->header.size = htons (size);
670 msg->header.type = htons (type);
673 GMC_send_prebuilt_message (&msg->header, c, NULL, fwd);
678 * Send CONNECTION_{CREATE/ACK} packets for a connection.
680 * @param c Connection for which to send the message.
681 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
684 connection_recreate (struct MeshConnection *c, int fwd)
686 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
690 send_connection_ack (c, GNUNET_NO);
695 * Generic connection timer management.
696 * Depending on the role of the peer in the connection will send the
697 * appropriate message (build or keepalive)
699 * @param c Conncetion to maintain.
703 connection_maintain (struct MeshConnection *c, int fwd)
705 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
707 /* TODO DHT GET with RO_BART */
712 case MESH_CONNECTION_NEW:
714 case MESH_CONNECTION_SENT:
715 connection_recreate (c, fwd);
717 case MESH_CONNECTION_READY:
718 connection_keepalive (c, fwd);
727 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
729 struct MeshConnection *c = cls;
731 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
732 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
735 connection_maintain (c, GNUNET_YES);
736 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
737 &connection_fwd_keepalive,
743 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
745 struct MeshConnection *c = cls;
747 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
748 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
751 connection_maintain (c, GNUNET_NO);
752 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
753 &connection_bck_keepalive,
759 * @brief Re-initiate traffic on this connection if necessary.
761 * Check if there is traffic queued towards this peer
762 * and the core transmit handle is NULL (traffic was stalled).
763 * If so, call core tmt rdy.
765 * @param c Connection on which initiate traffic.
766 * @param fwd Is this about fwd traffic?
769 connection_unlock_queue (struct MeshConnection *c, int fwd)
771 struct MeshPeer *peer;
773 LOG (GNUNET_ERROR_TYPE_DEBUG,
774 "connection_unlock_queue %s on %s\n",
775 fwd ? "FWD" : "BCK", GMC_2s (c));
777 if (GMC_is_terminal (c, fwd))
779 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
783 peer = get_hop (c, fwd);
784 GMP_queue_unlock (peer, c);
789 * Cancel all transmissions that belong to a certain connection.
791 * @param c Connection which to cancel.
792 * @param fwd Cancel fwd traffic?
795 connection_cancel_queues (struct MeshConnection *c, int fwd)
798 struct MeshFlowControl *fc;
799 struct MeshPeer *peer;
807 peer = get_hop (c, fwd);
808 GMP_queue_cancel (peer, c);
810 fc = fwd ? &c->fwd_fc : &c->bck_fc;
811 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
813 GNUNET_SCHEDULER_cancel (fc->poll_task);
814 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
820 * Function called if a connection has been stalled for a while,
821 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
823 * @param cls Closure (poll ctx).
824 * @param tc TaskContext.
827 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
829 struct MeshFlowControl *fc = cls;
830 struct GNUNET_MESH_Poll msg;
831 struct MeshConnection *c;
833 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
834 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
840 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
841 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
842 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
843 fc == &c->fwd_fc ? "FWD" : "BCK");
845 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
846 msg.header.size = htons (sizeof (msg));
847 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
848 GMC_send_prebuilt_message (&msg.header, c, NULL, fc == &c->fwd_fc);
849 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
850 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
851 &connection_poll, fc);
856 * Timeout function due to lack of keepalive/traffic from the owner.
857 * Destroys connection if called.
859 * @param cls Closure (connection to destroy).
860 * @param tc TaskContext.
863 connection_fwd_timeout (void *cls,
864 const struct GNUNET_SCHEDULER_TaskContext *tc)
866 struct MeshConnection *c = cls;
868 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
869 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
871 LOG (GNUNET_ERROR_TYPE_DEBUG,
872 "Connection %s[%X] FWD timed out. Destroying.\n",
876 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
884 * Timeout function due to lack of keepalive/traffic from the destination.
885 * Destroys connection if called.
887 * @param cls Closure (connection to destroy).
888 * @param tc TaskContext
891 connection_bck_timeout (void *cls,
892 const struct GNUNET_SCHEDULER_TaskContext *tc)
894 struct MeshConnection *c = cls;
896 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
897 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
900 LOG (GNUNET_ERROR_TYPE_DEBUG,
901 "Connection %s[%X] FWD timed out. Destroying.\n",
902 GMT_2s (c->t), c->id);
904 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
912 * Resets the connection timeout task, some other message has done the
914 * - For the first peer on the direction this means to send
915 * a keepalive or a path confirmation message (either create or ACK).
916 * - For all other peers, this means to destroy the connection,
917 * due to lack of activity.
918 * Starts the tiemout if no timeout was running (connection just created).
920 * @param c Connection whose timeout to reset.
921 * @param fwd Is this forward?
923 * TODO use heap to improve efficiency of scheduler.
926 connection_reset_timeout (struct MeshConnection *c, int fwd)
928 GNUNET_SCHEDULER_TaskIdentifier *ti;
929 GNUNET_SCHEDULER_Task f;
931 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
933 if (GNUNET_SCHEDULER_NO_TASK != *ti)
934 GNUNET_SCHEDULER_cancel (*ti);
936 if (GMC_is_origin (c, fwd)) /* Endpoint */
938 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
939 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
943 struct GNUNET_TIME_Relative delay;
945 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
946 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
947 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
953 * Add the connection to the list of both neighbors.
955 * @param c Connection.
958 register_neighbors (struct MeshConnection *c)
960 struct MeshPeer *peer;
962 peer = get_next_hop (c);
963 if (GNUNET_NO == GMP_is_neighbor (peer))
968 GMP_add_connection (peer, c);
969 peer = get_prev_hop (c);
970 if (GNUNET_NO == GMP_is_neighbor (peer))
975 GMP_add_connection (peer, c);
980 * Remove the connection from the list of both neighbors.
982 * @param c Connection.
985 unregister_neighbors (struct MeshConnection *c)
987 struct MeshPeer *peer;
989 peer = get_next_hop (c);
990 GMP_remove_connection (peer, c);
992 peer = get_prev_hop (c);
993 GMP_remove_connection (peer, c);
999 * Bind the connection to the peer and the tunnel to that peer.
1001 * If the peer has no tunnel, create one. Update tunnel and connection
1002 * data structres to reflect new status.
1004 * @param c Connection.
1008 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1010 GMP_add_tunnel (peer);
1011 c->t = GMP_get_tunnel (peer);
1012 GMT_add_connection (c->t, c);
1015 /******************************************************************************/
1016 /******************************** API ***********************************/
1017 /******************************************************************************/
1020 * Core handler for connection creation.
1022 * @param cls Closure (unused).
1023 * @param peer Sender (neighbor).
1024 * @param message Message.
1026 * @return GNUNET_OK to keep the connection open,
1027 * GNUNET_SYSERR to close it (signal serious error)
1030 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1031 const struct GNUNET_MessageHeader *message)
1033 struct GNUNET_MESH_ConnectionCreate *msg;
1034 struct GNUNET_PeerIdentity *id;
1035 struct GNUNET_HashCode *cid;
1036 struct MeshPeerPath *path;
1037 struct MeshPeer *dest_peer;
1038 struct MeshPeer *orig_peer;
1039 struct MeshConnection *c;
1040 unsigned int own_pos;
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1045 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1048 size = ntohs (message->size);
1049 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1051 GNUNET_break_op (0);
1055 /* Calculate hops */
1056 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1057 if (size % sizeof (struct GNUNET_PeerIdentity))
1059 GNUNET_break_op (0);
1062 size /= sizeof (struct GNUNET_PeerIdentity);
1065 GNUNET_break_op (0);
1068 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1070 /* Get parameters */
1071 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1073 id = (struct GNUNET_PeerIdentity *) &msg[1];
1074 LOG (GNUNET_ERROR_TYPE_DEBUG,
1075 " connection %s (%s).\n",
1076 GNUNET_h2s (cid), GNUNET_i2s (id));
1078 /* Create connection */
1079 c = connection_get (cid);
1083 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1084 path = path_new (size);
1086 for (i = 0; i < size; i++)
1088 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1089 GNUNET_i2s (&id[i]));
1090 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1091 if (path->peers[i] == myid)
1094 if (own_pos == 0 && path->peers[own_pos] != myid)
1096 /* create path: self not found in path through self */
1097 GNUNET_break_op (0);
1098 path_destroy (path);
1101 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1102 GMP_add_path_to_all (path, GNUNET_NO);
1103 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1104 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1107 connection_reset_timeout (c, GNUNET_YES);
1113 if (MESH_CONNECTION_NEW == c->state)
1114 connection_change_state (c, MESH_CONNECTION_SENT);
1116 /* Remember peers */
1117 dest_peer = GMP_get (&id[size - 1]);
1118 orig_peer = GMP_get (&id[0]);
1120 /* Is it a connection to us? */
1121 if (c->own_pos == size - 1)
1123 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1124 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1126 add_to_peer (c, orig_peer);
1127 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1128 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1130 send_connection_ack (c, GNUNET_NO);
1131 if (MESH_CONNECTION_SENT == c->state)
1132 connection_change_state (c, MESH_CONNECTION_ACK);
1134 /* Keep tunnel alive in direction dest->owner*/
1135 connection_reset_timeout (c, GNUNET_NO);
1139 /* It's for somebody else! Retransmit. */
1140 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1141 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1142 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1143 GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES);
1150 * Core handler for path confirmations.
1152 * @param cls closure
1153 * @param message message
1154 * @param peer peer identity this notification is about
1156 * @return GNUNET_OK to keep the connection open,
1157 * GNUNET_SYSERR to close it (signal serious error)
1160 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1161 const struct GNUNET_MessageHeader *message)
1163 struct GNUNET_MESH_ConnectionACK *msg;
1164 struct MeshConnection *c;
1165 struct MeshPeerPath *p;
1166 struct MeshPeer *pi;
1169 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1171 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1173 GNUNET_h2s (&msg->cid));
1174 c = connection_get (&msg->cid);
1177 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1179 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1184 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1186 pi = GMP_get (peer);
1187 if (get_next_hop (c) == pi)
1189 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1191 if (MESH_CONNECTION_SENT == c->state)
1192 connection_change_state (c, MESH_CONNECTION_ACK);
1194 else if (get_prev_hop (c) == pi)
1196 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1198 connection_change_state (c, MESH_CONNECTION_READY);
1202 GNUNET_break_op (0);
1205 connection_reset_timeout (c, fwd);
1207 /* Add path to peers? */
1211 GMP_add_path_to_all (p, GNUNET_YES);
1218 /* Message for us as creator? */
1219 if (GMC_is_origin (c, GNUNET_YES))
1221 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1222 connection_change_state (c, MESH_CONNECTION_READY);
1223 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1224 send_connection_ack (c, GNUNET_YES);
1225 GMT_send_queued_data (c->t, GNUNET_YES);
1229 /* Message for us as destination? */
1230 if (GMC_is_terminal (c, GNUNET_YES))
1232 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1233 connection_change_state (c, MESH_CONNECTION_READY);
1234 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1235 GMT_send_queued_data (c->t, GNUNET_NO);
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1240 GMC_send_prebuilt_message (message, c, NULL, fwd);
1246 * Core handler for notifications of broken paths
1248 * @param cls Closure (unused).
1249 * @param id Peer identity of sending neighbor.
1250 * @param message Message.
1252 * @return GNUNET_OK to keep the connection open,
1253 * GNUNET_SYSERR to close it (signal serious error)
1256 GMC_handle_broken (void* cls,
1257 const struct GNUNET_PeerIdentity* id,
1258 const struct GNUNET_MessageHeader* message)
1260 struct GNUNET_MESH_ConnectionBroken *msg;
1261 struct MeshConnection *c;
1264 LOG (GNUNET_ERROR_TYPE_DEBUG,
1265 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1266 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1267 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1268 GNUNET_i2s (&msg->peer1));
1269 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1270 GNUNET_i2s (&msg->peer2));
1271 c = connection_get (&msg->cid);
1274 GNUNET_break_op (0);
1278 fwd = is_fwd (c, id);
1279 connection_cancel_queues (c, !fwd);
1280 if (GMC_is_terminal (c, fwd))
1282 if (0 < c->pending_messages)
1283 c->destroy = GNUNET_YES;
1289 GMC_send_prebuilt_message (message, c, NULL, fwd);
1290 c->destroy = GNUNET_YES;
1299 * Core handler for tunnel destruction
1301 * @param cls Closure (unused).
1302 * @param peer Peer identity of sending neighbor.
1303 * @param message Message.
1305 * @return GNUNET_OK to keep the connection open,
1306 * GNUNET_SYSERR to close it (signal serious error)
1309 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1310 const struct GNUNET_MessageHeader *message)
1312 struct GNUNET_MESH_ConnectionDestroy *msg;
1313 struct MeshConnection *c;
1316 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1317 LOG (GNUNET_ERROR_TYPE_DEBUG,
1318 "Got a CONNECTION DESTROY message from %s\n",
1320 LOG (GNUNET_ERROR_TYPE_DEBUG,
1321 " for connection %s\n",
1322 GNUNET_h2s (&msg->cid));
1323 c = connection_get (&msg->cid);
1326 /* Probably already got the message from another path,
1327 * destroyed the tunnel and retransmitted to children.
1330 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1334 fwd = is_fwd (c, peer);
1335 if (GNUNET_SYSERR == fwd)
1337 GNUNET_break_op (0);
1340 GMC_send_prebuilt_message (message, c, NULL, fwd);
1341 c->destroy = GNUNET_YES;
1347 * Generic handler for mesh network encrypted traffic.
1349 * @param peer Peer identity this notification is about.
1350 * @param message Encrypted message.
1352 * @return GNUNET_OK to keep the connection open,
1353 * GNUNET_SYSERR to close it (signal serious error)
1356 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1357 const struct GNUNET_MESH_Encrypted *msg)
1359 struct MeshConnection *c;
1360 struct MeshPeer *neighbor;
1361 struct MeshFlowControl *fc;
1362 GNUNET_PEER_Id peer_id;
1370 size = ntohs (msg->header.size);
1372 sizeof (struct GNUNET_MESH_Encrypted) +
1373 sizeof (struct GNUNET_MessageHeader))
1375 GNUNET_break_op (0);
1378 type = ntohs (msg->header.type);
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1380 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1381 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1383 /* Check connection */
1384 c = connection_get (&msg->cid);
1387 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1388 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1392 /* Check if origin is as expected */
1393 neighbor = get_prev_hop (c);
1394 peer_id = GNUNET_PEER_search (peer);
1395 if (peer_id == GMP_get_short_id (neighbor))
1401 neighbor = get_next_hop (c);
1402 if (peer_id == GMP_get_short_id (neighbor))
1408 /* Unexpected peer sending traffic on a connection. */
1409 GNUNET_break_op (0);
1415 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1416 pid = ntohl (msg->pid);
1417 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1419 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1420 LOG (GNUNET_ERROR_TYPE_DEBUG,
1421 "WARNING Received PID %u, (prev %u), ACK %u\n",
1422 pid, fc->last_pid_recv, fc->last_ack_sent);
1425 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1427 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1428 LOG (GNUNET_ERROR_TYPE_DEBUG,
1429 " Pid %u not expected (%u+), dropping!\n",
1430 pid, fc->last_pid_recv + 1);
1433 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1434 connection_change_state (c, MESH_CONNECTION_READY);
1435 connection_reset_timeout (c, fwd);
1436 fc->last_pid_recv = pid;
1438 /* Is this message for us? */
1439 if (GMC_is_terminal (c, fwd))
1441 /* TODO signature verification */
1442 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1443 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1450 fc->last_pid_recv = pid;
1451 GMT_handle_encrypted (c->t, msg, fwd);
1452 GMC_send_ack (c, fwd);
1456 /* Message not for us: forward to next hop */
1457 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1458 ttl = ntohl (msg->ttl);
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1462 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1463 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1464 GMC_send_ack (c, fwd);
1467 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1469 GMC_send_prebuilt_message (&msg->header, c, NULL, fwd);
1476 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1478 * @param cls Closure (unused).
1479 * @param message Message received.
1480 * @param peer Peer who sent the message.
1482 * @return GNUNET_OK to keep the connection open,
1483 * GNUNET_SYSERR to close it (signal serious error)
1486 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1487 const struct GNUNET_MessageHeader *message)
1489 return handle_mesh_encrypted (peer,
1490 (struct GNUNET_MESH_Encrypted *)message);
1495 * Core handler for mesh network traffic point-to-point acks.
1497 * @param cls closure
1498 * @param message message
1499 * @param peer peer identity this notification is about
1501 * @return GNUNET_OK to keep the connection open,
1502 * GNUNET_SYSERR to close it (signal serious error)
1505 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1506 const struct GNUNET_MessageHeader *message)
1508 struct GNUNET_MESH_ACK *msg;
1509 struct MeshConnection *c;
1510 struct MeshFlowControl *fc;
1515 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1518 msg = (struct GNUNET_MESH_ACK *) message;
1520 c = connection_get (&msg->cid);
1524 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1529 /* Is this a forward or backward ACK? */
1530 id = GNUNET_PEER_search (peer);
1531 if (GMP_get_short_id (get_next_hop (c)) == id)
1533 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1537 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1539 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1545 GNUNET_break_op (0);
1549 ack = ntohl (msg->ack);
1550 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1551 ack, fc->last_ack_recv);
1552 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1553 fc->last_ack_recv = ack;
1555 /* Cancel polling if the ACK is big enough. */
1556 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1557 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1559 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1560 GNUNET_SCHEDULER_cancel (fc->poll_task);
1561 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1562 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1565 connection_unlock_queue (c, fwd);
1572 * Core handler for mesh network traffic point-to-point ack polls.
1574 * @param cls closure
1575 * @param message message
1576 * @param peer peer identity this notification is about
1578 * @return GNUNET_OK to keep the connection open,
1579 * GNUNET_SYSERR to close it (signal serious error)
1582 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1583 const struct GNUNET_MessageHeader *message)
1585 struct GNUNET_MESH_Poll *msg;
1586 struct MeshConnection *c;
1587 struct MeshFlowControl *fc;
1592 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1593 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1596 msg = (struct GNUNET_MESH_Poll *) message;
1598 c = connection_get (&msg->cid);
1602 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1604 GNUNET_break_op (0);
1608 /* Is this a forward or backward ACK?
1609 * Note: a poll should never be needed in a loopback case,
1610 * since there is no possiblility of packet loss there, so
1611 * this way of discerining FWD/BCK should not be a problem.
1613 id = GNUNET_PEER_search (peer);
1614 if (GMP_get_short_id (get_next_hop (c)) == id)
1616 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1619 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1621 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1626 GNUNET_break_op (0);
1630 pid = ntohl (msg->pid);
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1632 pid, fc->last_pid_recv);
1633 fc->last_pid_recv = pid;
1634 fwd = fc == &c->fwd_fc;
1635 GMC_send_ack (c, fwd);
1642 * Core handler for mesh keepalives.
1644 * @param cls closure
1645 * @param message message
1646 * @param peer peer identity this notification is about
1647 * @return GNUNET_OK to keep the connection open,
1648 * GNUNET_SYSERR to close it (signal serious error)
1650 * TODO: Check who we got this from, to validate route.
1653 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1654 const struct GNUNET_MessageHeader *message)
1656 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1657 struct MeshConnection *c;
1658 struct MeshPeer *neighbor;
1661 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1662 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1665 c = connection_get (&msg->cid);
1668 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1673 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1674 GNUNET_YES : GNUNET_NO;
1676 /* Check if origin is as expected */
1677 neighbor = get_hop (c, fwd);
1678 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1680 GNUNET_break_op (0);
1684 connection_change_state (c, MESH_CONNECTION_READY);
1685 connection_reset_timeout (c, fwd);
1687 if (GMC_is_terminal (c, fwd))
1690 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1691 GMC_send_prebuilt_message (message, c, NULL, fwd);
1698 * Send an ACK on the appropriate connection/channel, depending on
1699 * the direction and the position of the peer.
1701 * @param c Which connection to send the hop-by-hop ACK.
1702 * @param fwd Is this a fwd ACK? (will go dest->root)
1705 GMC_send_ack (struct MeshConnection *c, int fwd)
1707 unsigned int buffer;
1709 LOG (GNUNET_ERROR_TYPE_DEBUG,
1710 "GMC send %s ACK on %s\n",
1711 fwd ? "FWD" : "BCK", GMC_2s (c));
1719 /* Get available buffer space */
1720 if (GMC_is_terminal (c, fwd))
1722 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
1723 buffer = GMT_get_buffer (c->t, fwd);
1727 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1728 buffer = GMC_get_buffer (c, fwd);
1730 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1732 /* Send available buffer space */
1733 if (GMC_is_origin (c, fwd))
1735 GNUNET_assert (NULL != c->t);
1736 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
1739 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1740 GMT_unchoke_channels (c->t, fwd);
1745 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1746 send_ack (c, buffer, fwd);
1752 * Initialize the connections subsystem
1754 * @param c Configuration handle.
1757 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1759 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1761 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1764 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1765 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1766 GNUNET_SCHEDULER_shutdown ();
1771 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1774 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1775 "MESH", "MAX_CONNECTIONS", "MISSING");
1776 GNUNET_SCHEDULER_shutdown ();
1781 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1782 &refresh_connection_time))
1784 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1785 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1786 GNUNET_SCHEDULER_shutdown ();
1789 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1793 * Shut down the connections subsystem.
1798 GNUNET_CONTAINER_multihashmap_destroy (connections);
1802 struct MeshConnection *
1803 GMC_new (const struct GNUNET_HashCode *cid,
1804 struct MeshTunnel3 *t,
1805 struct MeshPeerPath *p,
1806 unsigned int own_pos)
1808 struct MeshConnection *c;
1810 c = GNUNET_new (struct MeshConnection);
1812 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1813 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1814 fc_init (&c->fwd_fc);
1815 fc_init (&c->bck_fc);
1820 if (own_pos > p->length - 1)
1826 c->own_pos = own_pos;
1831 c->fwd_maintenance_task =
1832 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1833 &connection_fwd_keepalive, c);
1835 register_neighbors (c);
1841 GMC_destroy (struct MeshConnection *c)
1846 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
1848 /* Cancel all traffic */
1849 connection_cancel_queues (c, GNUNET_YES);
1850 connection_cancel_queues (c, GNUNET_NO);
1852 /* Cancel maintainance task (keepalive/timeout) */
1853 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1854 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1855 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1856 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1858 /* Unregister from neighbors */
1859 unregister_neighbors (c);
1862 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1864 GMT_remove_connection (c->t, c);
1869 * Get the connection ID.
1871 * @param c Connection to get the ID from.
1873 * @return ID of the connection.
1875 const struct GNUNET_HashCode *
1876 GMC_get_id (const struct MeshConnection *c)
1883 * Get the connection path.
1885 * @param c Connection to get the path from.
1887 * @return path used by the connection.
1889 const struct MeshPeerPath *
1890 GMC_get_path (const struct MeshConnection *c)
1897 * Get the connection state.
1899 * @param c Connection to get the state from.
1901 * @return state of the connection.
1903 enum MeshConnectionState
1904 GMC_get_state (const struct MeshConnection *c)
1910 * Get the connection tunnel.
1912 * @param c Connection to get the tunnel from.
1914 * @return tunnel of the connection.
1916 struct MeshTunnel3 *
1917 GMC_get_tunnel (const struct MeshConnection *c)
1924 * Get free buffer space in a connection.
1926 * @param c Connection.
1927 * @param fwd Is query about FWD traffic?
1929 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1932 GMC_get_buffer (struct MeshConnection *c, int fwd)
1934 struct MeshFlowControl *fc;
1936 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1938 return (fc->queue_max - fc->queue_n);
1942 * Get how many messages have we allowed to send to us from a direction..
1944 * @param c Connection.
1945 * @param fwd Are we asking about traffic from FWD (BCK messages)?
1947 * @return last_ack_sent - last_pid_recv
1950 GMC_get_allowed (struct MeshConnection *c, int fwd)
1952 struct MeshFlowControl *fc;
1954 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1955 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
1959 return (fc->last_ack_sent - fc->last_pid_recv);
1963 * Get messages queued in a connection.
1965 * @param c Connection.
1966 * @param fwd Is query about FWD traffic?
1968 * @return Number of messages queued.
1971 GMC_get_qn (struct MeshConnection *c, int fwd)
1973 struct MeshFlowControl *fc;
1975 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1982 * Allow the connection to advertise a buffer of the given size.
1984 * The connection will send an @c fwd ACK message (so: in direction !fwd)
1985 * allowing up to last_pid_recv + buffer.
1987 * @param c Connection.
1988 * @param buffer How many more messages the connection can accept.
1989 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
1992 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
1994 send_ack (c, buffer, fwd);
1999 * Notify other peers on a connection of a broken link. Mark connections
2000 * to destroy after all traffic has been sent.
2002 * @param c Connection on which there has been a disconnection.
2003 * @param peer Peer that disconnected.
2006 GMC_notify_broken (struct MeshConnection *c,
2007 struct MeshPeer *peer)
2011 fwd = peer == get_prev_hop (c);
2013 connection_cancel_queues (c, !fwd);
2014 if (GMC_is_terminal (c, fwd))
2016 /* Local shutdown, no one to notify about this. */
2021 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2023 /* Connection will have at least one pending message
2024 * (the one we just scheduled), so no point in checking whether to
2025 * destroy immediately. */
2026 c->destroy = GNUNET_YES;
2033 * Is this peer the first one on the connection?
2035 * @param c Connection.
2036 * @param fwd Is this about fwd traffic?
2038 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2041 GMC_is_origin (struct MeshConnection *c, int fwd)
2043 if (!fwd && c->path->length - 1 == c->own_pos )
2045 if (fwd && 0 == c->own_pos)
2052 * Is this peer the last one on the connection?
2054 * @param c Connection.
2055 * @param fwd Is this about fwd traffic?
2056 * Note that the ROOT is the terminal for BCK traffic!
2058 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2061 GMC_is_terminal (struct MeshConnection *c, int fwd)
2063 return GMC_is_origin (c, !fwd);
2068 * See if we are allowed to send by the next hop in the given direction.
2070 * @param c Connection.
2071 * @param fwd Is this about fwd traffic?
2073 * @return GNUNET_YES in case it's OK.
2076 GMC_is_sendable (struct MeshConnection *c, int fwd)
2078 struct MeshFlowControl *fc;
2080 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2081 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2087 * Sends an already built message on a connection, properly registering
2088 * all used resources.
2090 * @param message Message to send. Function makes a copy of it.
2091 * If message is not hop-by-hop, decrements TTL of copy.
2092 * @param c Connection on which this message is transmitted.
2093 * @param ch Channel on which this message is transmitted, or NULL.
2094 * @param fwd Is this a fwd message?
2097 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2098 struct MeshConnection *c,
2099 struct MeshChannel *ch,
2102 struct MeshFlowControl *fc;
2108 size = ntohs (message->size);
2109 data = GNUNET_malloc (size);
2110 memcpy (data, message, size);
2111 type = ntohs (message->type);
2112 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2113 GNUNET_MESH_DEBUG_M2S (type), size, GMC_2s (c));
2115 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2116 droppable = GNUNET_YES;
2119 struct GNUNET_MESH_Encrypted *emsg;
2120 struct GNUNET_MESH_ACK *amsg;
2121 struct GNUNET_MESH_Poll *pmsg;
2122 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2123 struct GNUNET_MESH_ConnectionBroken *bmsg;
2126 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2127 emsg = (struct GNUNET_MESH_Encrypted *) data;
2128 ttl = ntohl (emsg->ttl);
2131 GNUNET_break_op (0);
2135 emsg->ttl = htonl (ttl - 1);
2136 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2137 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2139 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2140 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
2141 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
2142 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2144 GMC_start_poll (c, fwd);
2148 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2149 amsg = (struct GNUNET_MESH_ACK *) data;
2151 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2152 droppable = GNUNET_NO;
2155 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2156 pmsg = (struct GNUNET_MESH_Poll *) data;
2158 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2159 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2160 droppable = GNUNET_NO;
2163 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2164 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2169 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2170 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2175 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2176 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2183 if (fc->queue_n > fc->queue_max && droppable)
2185 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2188 LOG (GNUNET_ERROR_TYPE_DEBUG,
2189 "queue full: %u/%u\n",
2190 fc->queue_n, fc->queue_max);
2191 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2193 return; /* Drop this message */
2196 c->pending_messages++;
2198 GMP_queue_add (get_hop (c, fwd), data, type, size, c, ch, fwd,
2199 &message_sent, NULL);
2204 * Sends a CREATE CONNECTION message for a path to a peer.
2205 * Changes the connection and tunnel states if necessary.
2207 * @param connection Connection to create.
2210 GMC_send_create (struct MeshConnection *connection)
2212 enum MeshTunnel3State state;
2215 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2216 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2217 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2218 GMP_queue_add (get_next_hop (connection), NULL,
2219 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2220 size, connection, NULL,
2221 GNUNET_YES, &message_sent, NULL);
2222 state = GMT_get_state (connection->t);
2223 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2224 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2225 if (MESH_CONNECTION_NEW == connection->state)
2226 connection_change_state (connection, MESH_CONNECTION_SENT);
2231 * Send a message to all peers in this connection that the connection
2232 * is no longer valid.
2234 * If some peer should not receive the message, it should be zero'ed out
2235 * before calling this function.
2237 * @param c The connection whose peers to notify.
2240 GMC_send_destroy (struct MeshConnection *c)
2242 struct GNUNET_MESH_ConnectionDestroy msg;
2244 if (GNUNET_YES == c->destroy)
2247 msg.header.size = htons (sizeof (msg));
2248 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2250 LOG (GNUNET_ERROR_TYPE_DEBUG,
2251 " sending connection destroy for connection %s\n",
2254 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2255 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2256 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2257 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2258 c->destroy = GNUNET_YES;
2263 * @brief Start a polling timer for the connection.
2265 * When a neighbor does not accept more traffic on the connection it could be
2266 * caused by a simple congestion or by a lost ACK. Polling enables to check
2267 * for the lastest ACK status for a connection.
2269 * @param c Connection.
2270 * @param fwd Should we poll in the FWD direction?
2273 GMC_start_poll (struct MeshConnection *c, int fwd)
2275 struct MeshFlowControl *fc;
2277 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2278 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2282 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2289 * @brief Stop polling a connection for ACKs.
2291 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2293 * @param c Connection.
2294 * @param fwd Should we stop the poll in the FWD direction?
2297 GMC_stop_poll (struct MeshConnection *c, int fwd)
2299 struct MeshFlowControl *fc;
2301 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2302 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2304 GNUNET_SCHEDULER_cancel (fc->poll_task);
2305 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2310 * Get a (static) string for a connection.
2312 * @param c Connection.
2315 GMC_2s (struct MeshConnection *c)
2319 static char buf[128];
2321 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2324 return GNUNET_h2s (&c->id);