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), GNUNET_h2s (&c->id));
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 GNUNET_h2s (&c->id), GMC_state2s (c->state));
347 LOG (GNUNET_ERROR_TYPE_DEBUG,
348 "Connection %s state is now %s\n",
349 GNUNET_h2s (&c->id), GMC_state2s (state));
355 * Callback called when a queued message is sent.
357 * Calculates the average time and connection packet tracking.
359 * @param cls Closure.
360 * @param c Connection this message was on.
361 * @param type Type of message sent.
362 * @param fwd Was this a FWD going message?
363 * @param size Size of the message.
364 * @param wait Time spent waiting for core (only the time for THIS message)
367 message_sent (void *cls,
368 struct MeshConnection *c, uint16_t type,
369 int fwd, size_t size,
370 struct GNUNET_TIME_Relative wait)
372 struct MeshConnectionPerformance *p;
373 struct MeshFlowControl *fc;
376 fc = fwd ? &c->fwd_fc : &c->bck_fc;
377 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
379 c->pending_messages--;
380 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
382 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
385 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
388 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
390 LOG (GNUNET_ERROR_TYPE_DEBUG, "! accounting pid %u\n", fc->last_pid_sent);
391 // send_ack (c, ch, fwd);
398 return; /* Only endpoints are interested in timing. */
400 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
402 usecsperbyte = ((double) wait.rel_value_us) / size;
403 if (p->size == AVG_MSGS)
405 /* Array is full. Substract oldest value, add new one and store. */
406 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
407 p->usecsperbyte[p->idx] = usecsperbyte;
408 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
412 /* Array not yet full. Add current value to avg and store. */
413 p->usecsperbyte[p->idx] = usecsperbyte;
415 p->avg += p->usecsperbyte[p->idx];
419 p->idx = (p->idx + 1) % AVG_MSGS;
423 // c->t->pending_messages--;
424 // if (GNUNET_YES == c->t->destroy && 0 == t->pending_messages)
426 // LOG (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
427 // GMT_destroy (c->t);
434 * Get the previous hop in a connection
436 * @param c Connection.
438 * @return Previous peer in the connection.
440 static struct MeshPeer *
441 get_prev_hop (const struct MeshConnection *c)
445 if (0 == c->own_pos || c->path->length < 2)
446 id = c->path->peers[0];
448 id = c->path->peers[c->own_pos - 1];
450 return GMP_get_short (id);
455 * Get the next hop in a connection
457 * @param c Connection.
459 * @return Next peer in the connection.
461 static struct MeshPeer *
462 get_next_hop (const struct MeshConnection *c)
466 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
467 id = c->path->peers[c->path->length - 1];
469 id = c->path->peers[c->own_pos + 1];
471 return GMP_get_short (id);
476 * Get the hop in a connection.
478 * @param c Connection.
479 * @param fwd Next hop?
481 * @return Next peer in the connection.
483 static struct MeshPeer *
484 get_hop (struct MeshConnection *c, int fwd)
487 return get_next_hop (c);
488 return get_prev_hop (c);
493 * Is traffic coming from this sender 'FWD' traffic?
495 * @param c Connection to check.
496 * @param sender Peer identity of neighbor.
498 * @return GNUNET_YES in case the sender is the 'prev' hop and therefore
499 * the traffic is 'FWD'. GNUNET_NO for BCK. GNUNET_SYSERR for errors.
502 is_fwd (const struct MeshConnection *c,
503 const struct GNUNET_PeerIdentity *sender)
507 id = GNUNET_PEER_search (sender);
508 if (GMP_get_short_id (get_prev_hop (c)) == id)
511 if (GMP_get_short_id (get_next_hop (c)) == id)
515 return GNUNET_SYSERR;
521 * Send an ACK informing the predecessor about the available buffer space.
523 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
524 * the ACK itself goes "back" (dest->root).
526 * @param c Connection on which to send the ACK.
527 * @param buffer How much space free to advertise?
528 * @param fwd Is this FWD ACK? (Going dest->owner)
531 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
533 struct MeshFlowControl *next_fc;
534 struct MeshFlowControl *prev_fc;
535 struct GNUNET_MESH_ACK msg;
539 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
540 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
542 LOG (GNUNET_ERROR_TYPE_DEBUG,
543 "connection send %s ack on %s\n",
544 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
546 /* Check if we need to transmit the ACK */
547 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
548 if (3 < delta && buffer < delta)
550 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
551 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 " last pid recv: %u, last ack sent: %u\n",
553 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
557 /* Ok, ACK might be necessary, what PID to ACK? */
558 ack = prev_fc->last_pid_recv + buffer;
559 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
560 LOG (GNUNET_ERROR_TYPE_DEBUG,
561 " last pid %u, last ack %u, qmax %u, q %u\n",
562 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
563 next_fc->queue_max, next_fc->queue_n);
564 if (ack == prev_fc->last_ack_sent)
566 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
570 prev_fc->last_ack_sent = ack;
572 /* Build ACK message and send on connection */
573 msg.header.size = htons (sizeof (msg));
574 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
575 msg.ack = htonl (ack);
578 GMC_send_prebuilt_message (&msg.header, c, NULL, !fwd);
583 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
584 * or a first CONNECTION_ACK directed to us.
586 * @param connection Connection to confirm.
587 * @param fwd Should we send it FWD?
588 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
591 send_connection_ack (struct MeshConnection *connection, int fwd)
593 struct MeshFlowControl *fc;
594 struct MeshTunnel3 *t;
597 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
598 GMP_queue_add (get_hop (connection, fwd), NULL,
599 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
600 sizeof (struct GNUNET_MESH_ConnectionACK),
601 connection, NULL, fwd,
602 &message_sent, NULL);
603 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
604 GMT_change_state (t, MESH_TUNNEL3_WAITING);
605 if (MESH_CONNECTION_READY != connection->state)
606 connection_change_state (connection, MESH_CONNECTION_SENT);
607 fc = fwd ? &connection->fwd_fc : &connection->bck_fc;
613 * Send a notification that a connection is broken.
615 * @param c Connection that is broken.
616 * @param id1 Peer that has disconnected.
617 * @param id2 Peer that has disconnected.
618 * @param fwd Direction towards which to send it.
621 send_broken (struct MeshConnection *c,
622 const struct GNUNET_PeerIdentity *id1,
623 const struct GNUNET_PeerIdentity *id2,
626 struct GNUNET_MESH_ConnectionBroken msg;
628 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
629 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
633 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
639 * Send keepalive packets for a connection.
641 * @param c Connection to keep alive..
642 * @param fwd Is this a FWD keepalive? (owner -> dest).
645 connection_keepalive (struct MeshConnection *c, int fwd)
647 struct GNUNET_MESH_ConnectionKeepAlive *msg;
648 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
652 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
653 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
655 LOG (GNUNET_ERROR_TYPE_DEBUG,
656 "sending %s keepalive for connection %s[%d]\n",
657 fwd ? "FWD" : "BCK", GMT_2s (c->t), c->id);
659 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
660 msg->header.size = htons (size);
661 msg->header.type = htons (type);
664 GMC_send_prebuilt_message (&msg->header, c, NULL, fwd);
669 * Send CONNECTION_{CREATE/ACK} packets for a connection.
671 * @param c Connection for which to send the message.
672 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
675 connection_recreate (struct MeshConnection *c, int fwd)
677 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
681 send_connection_ack (c, GNUNET_NO);
686 * Generic connection timer management.
687 * Depending on the role of the peer in the connection will send the
688 * appropriate message (build or keepalive)
690 * @param c Conncetion to maintain.
694 connection_maintain (struct MeshConnection *c, int fwd)
696 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
698 /* TODO DHT GET with RO_BART */
703 case MESH_CONNECTION_NEW:
705 case MESH_CONNECTION_SENT:
706 connection_recreate (c, fwd);
708 case MESH_CONNECTION_READY:
709 connection_keepalive (c, fwd);
718 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
720 struct MeshConnection *c = cls;
722 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
723 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
726 connection_maintain (c, GNUNET_YES);
727 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
728 &connection_fwd_keepalive,
734 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
736 struct MeshConnection *c = cls;
738 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
739 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
742 connection_maintain (c, GNUNET_NO);
743 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
744 &connection_bck_keepalive,
750 * @brief Re-initiate traffic on this connection if necessary.
752 * Check if there is traffic queued towards this peer
753 * and the core transmit handle is NULL (traffic was stalled).
754 * If so, call core tmt rdy.
756 * @param c Connection on which initiate traffic.
757 * @param fwd Is this about fwd traffic?
760 connection_unlock_queue (struct MeshConnection *c, int fwd)
762 struct MeshPeer *peer;
764 LOG (GNUNET_ERROR_TYPE_DEBUG,
765 "connection_unlock_queue %s on %s\n",
766 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
768 if (GMC_is_terminal (c, fwd))
770 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
774 peer = get_hop (c, fwd);
775 GMP_queue_unlock (peer, c);
780 * Cancel all transmissions that belong to a certain connection.
782 * @param c Connection which to cancel.
783 * @param fwd Cancel fwd traffic?
786 connection_cancel_queues (struct MeshConnection *c, int fwd)
789 struct MeshFlowControl *fc;
790 struct MeshPeer *peer;
798 peer = get_hop (c, fwd);
799 GMP_queue_cancel (peer, c);
801 fc = fwd ? &c->fwd_fc : &c->bck_fc;
802 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
804 GNUNET_SCHEDULER_cancel (fc->poll_task);
805 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
811 * Function called if a connection has been stalled for a while,
812 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
814 * @param cls Closure (poll ctx).
815 * @param tc TaskContext.
818 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
820 struct MeshFlowControl *fc = cls;
821 struct GNUNET_MESH_Poll msg;
822 struct MeshConnection *c;
824 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
825 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
831 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
832 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
833 GNUNET_h2s (&c->id));
834 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
835 fc == &c->fwd_fc ? "FWD" : "BCK");
837 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
838 msg.header.size = htons (sizeof (msg));
839 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
840 GMC_send_prebuilt_message (&msg.header, c, NULL, fc == &c->fwd_fc);
841 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
842 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
843 &connection_poll, fc);
848 * Timeout function due to lack of keepalive/traffic from the owner.
849 * Destroys connection if called.
851 * @param cls Closure (connection to destroy).
852 * @param tc TaskContext.
855 connection_fwd_timeout (void *cls,
856 const struct GNUNET_SCHEDULER_TaskContext *tc)
858 struct MeshConnection *c = cls;
860 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
861 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
863 LOG (GNUNET_ERROR_TYPE_DEBUG,
864 "Connection %s[%X] FWD timed out. Destroying.\n",
868 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
876 * Timeout function due to lack of keepalive/traffic from the destination.
877 * Destroys connection if called.
879 * @param cls Closure (connection to destroy).
880 * @param tc TaskContext
883 connection_bck_timeout (void *cls,
884 const struct GNUNET_SCHEDULER_TaskContext *tc)
886 struct MeshConnection *c = cls;
888 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
889 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
892 LOG (GNUNET_ERROR_TYPE_DEBUG,
893 "Connection %s[%X] FWD timed out. Destroying.\n",
894 GMT_2s (c->t), c->id);
896 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
904 * Resets the connection timeout task, some other message has done the
906 * - For the first peer on the direction this means to send
907 * a keepalive or a path confirmation message (either create or ACK).
908 * - For all other peers, this means to destroy the connection,
909 * due to lack of activity.
910 * Starts the tiemout if no timeout was running (connection just created).
912 * @param c Connection whose timeout to reset.
913 * @param fwd Is this forward?
915 * TODO use heap to improve efficiency of scheduler.
918 connection_reset_timeout (struct MeshConnection *c, int fwd)
920 GNUNET_SCHEDULER_TaskIdentifier *ti;
921 GNUNET_SCHEDULER_Task f;
923 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
925 if (GNUNET_SCHEDULER_NO_TASK != *ti)
926 GNUNET_SCHEDULER_cancel (*ti);
928 if (GMC_is_origin (c, fwd)) /* Endpoint */
930 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
931 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
935 struct GNUNET_TIME_Relative delay;
937 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
938 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
939 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
945 * Add the connection to the list of both neighbors.
947 * @param c Connection.
950 register_neighbors (struct MeshConnection *c)
952 struct MeshPeer *peer;
954 peer = get_next_hop (c);
955 if (GNUNET_NO == GMP_is_neighbor (peer))
960 GMP_add_connection (peer, c);
961 peer = get_prev_hop (c);
962 if (GNUNET_NO == GMP_is_neighbor (peer))
967 GMP_add_connection (peer, c);
972 * Remove the connection from the list of both neighbors.
974 * @param c Connection.
977 unregister_neighbors (struct MeshConnection *c)
979 struct MeshPeer *peer;
981 peer = get_next_hop (c);
982 GMP_remove_connection (peer, c);
984 peer = get_prev_hop (c);
985 GMP_remove_connection (peer, c);
991 * Bind the connection to the peer and the tunnel to that peer.
993 * If the peer has no tunnel, create one. Update tunnel and connection
994 * data structres to reflect new status.
996 * @param c Connection.
1000 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1002 GMP_add_tunnel (peer);
1003 c->t = GMP_get_tunnel (peer);
1004 GMT_add_connection (c->t, c);
1007 /******************************************************************************/
1008 /******************************** API ***********************************/
1009 /******************************************************************************/
1012 * Core handler for connection creation.
1014 * @param cls Closure (unused).
1015 * @param peer Sender (neighbor).
1016 * @param message Message.
1018 * @return GNUNET_OK to keep the connection open,
1019 * GNUNET_SYSERR to close it (signal serious error)
1022 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1023 const struct GNUNET_MessageHeader *message)
1025 struct GNUNET_MESH_ConnectionCreate *msg;
1026 struct GNUNET_PeerIdentity *id;
1027 struct GNUNET_HashCode *cid;
1028 struct MeshPeerPath *path;
1029 struct MeshPeer *dest_peer;
1030 struct MeshPeer *orig_peer;
1031 struct MeshConnection *c;
1032 unsigned int own_pos;
1036 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1037 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1040 size = ntohs (message->size);
1041 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1043 GNUNET_break_op (0);
1047 /* Calculate hops */
1048 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1049 if (size % sizeof (struct GNUNET_PeerIdentity))
1051 GNUNET_break_op (0);
1054 size /= sizeof (struct GNUNET_PeerIdentity);
1057 GNUNET_break_op (0);
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1062 /* Get parameters */
1063 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1065 id = (struct GNUNET_PeerIdentity *) &msg[1];
1066 LOG (GNUNET_ERROR_TYPE_DEBUG,
1067 " connection %s (%s).\n",
1068 GNUNET_h2s (cid), GNUNET_i2s (id));
1070 /* Create connection */
1071 c = connection_get (cid);
1075 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1076 path = path_new (size);
1078 for (i = 0; i < size; i++)
1080 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1081 GNUNET_i2s (&id[i]));
1082 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1083 if (path->peers[i] == myid)
1086 if (own_pos == 0 && path->peers[own_pos] != myid)
1088 /* create path: self not found in path through self */
1089 GNUNET_break_op (0);
1090 path_destroy (path);
1093 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1094 GMP_add_path_to_all (path, GNUNET_NO);
1095 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1096 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1099 connection_reset_timeout (c, GNUNET_YES);
1105 if (MESH_CONNECTION_NEW == c->state)
1106 connection_change_state (c, MESH_CONNECTION_SENT);
1108 /* Remember peers */
1109 dest_peer = GMP_get (&id[size - 1]);
1110 orig_peer = GMP_get (&id[0]);
1112 /* Is it a connection to us? */
1113 if (c->own_pos == size - 1)
1115 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1116 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1118 add_to_peer (c, orig_peer);
1119 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1120 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1122 send_connection_ack (c, GNUNET_NO);
1123 if (MESH_CONNECTION_SENT == c->state)
1124 connection_change_state (c, MESH_CONNECTION_ACK);
1126 /* Keep tunnel alive in direction dest->owner*/
1127 connection_reset_timeout (c, GNUNET_NO);
1131 /* It's for somebody else! Retransmit. */
1132 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1133 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1134 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1135 GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES);
1142 * Core handler for path confirmations.
1144 * @param cls closure
1145 * @param message message
1146 * @param peer peer identity this notification is about
1148 * @return GNUNET_OK to keep the connection open,
1149 * GNUNET_SYSERR to close it (signal serious error)
1152 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1153 const struct GNUNET_MessageHeader *message)
1155 struct GNUNET_MESH_ConnectionACK *msg;
1156 struct MeshConnection *c;
1157 struct MeshPeerPath *p;
1158 struct MeshPeer *pi;
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1162 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1163 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1164 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1165 GNUNET_h2s (&msg->cid));
1166 c = connection_get (&msg->cid);
1169 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1171 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1178 pi = GMP_get (peer);
1179 if (get_next_hop (c) == pi)
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1183 if (MESH_CONNECTION_SENT == c->state)
1184 connection_change_state (c, MESH_CONNECTION_ACK);
1186 else if (get_prev_hop (c) == pi)
1188 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1190 connection_change_state (c, MESH_CONNECTION_READY);
1194 GNUNET_break_op (0);
1197 connection_reset_timeout (c, fwd);
1199 /* Add path to peers? */
1203 GMP_add_path_to_all (p, GNUNET_YES);
1210 /* Message for us as creator? */
1211 if (GMC_is_origin (c, GNUNET_YES))
1213 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1214 connection_change_state (c, MESH_CONNECTION_READY);
1215 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1216 send_connection_ack (c, GNUNET_YES);
1217 GMT_send_queued_data (c->t, GNUNET_YES);
1221 /* Message for us as destination? */
1222 if (GMC_is_terminal (c, GNUNET_YES))
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1225 connection_change_state (c, MESH_CONNECTION_READY);
1226 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1227 GMT_send_queued_data (c->t, GNUNET_NO);
1231 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1232 GMC_send_prebuilt_message (message, c, NULL, fwd);
1238 * Core handler for notifications of broken paths
1240 * @param cls Closure (unused).
1241 * @param id Peer identity of sending neighbor.
1242 * @param message Message.
1244 * @return GNUNET_OK to keep the connection open,
1245 * GNUNET_SYSERR to close it (signal serious error)
1248 GMC_handle_broken (void* cls,
1249 const struct GNUNET_PeerIdentity* id,
1250 const struct GNUNET_MessageHeader* message)
1252 struct GNUNET_MESH_ConnectionBroken *msg;
1253 struct MeshConnection *c;
1256 LOG (GNUNET_ERROR_TYPE_DEBUG,
1257 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1258 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1259 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1260 GNUNET_i2s (&msg->peer1));
1261 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1262 GNUNET_i2s (&msg->peer2));
1263 c = connection_get (&msg->cid);
1266 GNUNET_break_op (0);
1270 fwd = is_fwd (c, id);
1271 connection_cancel_queues (c, !fwd);
1272 if (GMC_is_terminal (c, fwd))
1274 if (0 < c->pending_messages)
1275 c->destroy = GNUNET_YES;
1281 GMC_send_prebuilt_message (message, c, NULL, fwd);
1282 c->destroy = GNUNET_YES;
1291 * Core handler for tunnel destruction
1293 * @param cls Closure (unused).
1294 * @param peer Peer identity of sending neighbor.
1295 * @param message Message.
1297 * @return GNUNET_OK to keep the connection open,
1298 * GNUNET_SYSERR to close it (signal serious error)
1301 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1302 const struct GNUNET_MessageHeader *message)
1304 struct GNUNET_MESH_ConnectionDestroy *msg;
1305 struct MeshConnection *c;
1308 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1309 LOG (GNUNET_ERROR_TYPE_DEBUG,
1310 "Got a CONNECTION DESTROY message from %s\n",
1312 LOG (GNUNET_ERROR_TYPE_DEBUG,
1313 " for connection %s\n",
1314 GNUNET_h2s (&msg->cid));
1315 c = connection_get (&msg->cid);
1318 /* Probably already got the message from another path,
1319 * destroyed the tunnel and retransmitted to children.
1322 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1326 fwd = is_fwd (c, peer);
1327 if (GNUNET_SYSERR == fwd)
1329 GNUNET_break_op (0);
1332 GMC_send_prebuilt_message (message, c, NULL, fwd);
1333 c->destroy = GNUNET_YES;
1339 * Generic handler for mesh network encrypted traffic.
1341 * @param peer Peer identity this notification is about.
1342 * @param message Encrypted message.
1344 * @return GNUNET_OK to keep the connection open,
1345 * GNUNET_SYSERR to close it (signal serious error)
1348 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1349 const struct GNUNET_MESH_Encrypted *msg)
1351 struct MeshConnection *c;
1352 struct MeshPeer *neighbor;
1353 struct MeshFlowControl *fc;
1354 GNUNET_PEER_Id peer_id;
1362 size = ntohs (msg->header.size);
1364 sizeof (struct GNUNET_MESH_Encrypted) +
1365 sizeof (struct GNUNET_MessageHeader))
1367 GNUNET_break_op (0);
1370 type = ntohs (msg->header.type);
1371 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1372 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1373 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1375 /* Check connection */
1376 c = connection_get (&msg->cid);
1379 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1380 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1384 /* Check if origin is as expected */
1385 neighbor = get_prev_hop (c);
1386 peer_id = GNUNET_PEER_search (peer);
1387 if (peer_id == GMP_get_short_id (neighbor))
1393 neighbor = get_next_hop (c);
1394 if (peer_id == GMP_get_short_id (neighbor))
1400 GNUNET_break_op (0);
1404 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1407 pid = ntohl (msg->pid);
1408 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1410 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1411 LOG (GNUNET_ERROR_TYPE_DEBUG,
1412 "WARNING Received PID %u, (prev %u), ACK %u\n",
1413 pid, fc->last_pid_recv, fc->last_ack_sent);
1416 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1418 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1419 LOG (GNUNET_ERROR_TYPE_DEBUG,
1420 " Pid %u not expected (%u+), dropping!\n",
1421 pid, fc->last_pid_recv + 1);
1424 if (MESH_CONNECTION_SENT == c->state)
1425 connection_change_state (c, MESH_CONNECTION_READY);
1426 connection_reset_timeout (c, fwd);
1427 fc->last_pid_recv = pid;
1429 /* Is this message for us? */
1430 if (GMC_is_terminal (c, fwd))
1432 /* TODO signature verification */
1433 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1434 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1441 fc->last_pid_recv = pid;
1442 GMT_handle_encrypted (c->t, msg, fwd);
1443 GMC_send_ack (c, NULL, fwd);
1447 /* Message not for us: forward to next hop */
1448 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1449 ttl = ntohl (msg->ttl);
1450 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1453 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1454 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1455 GMC_send_ack (c, NULL, fwd);
1458 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1460 GMC_send_prebuilt_message (&msg->header, c, NULL, fwd);
1467 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1469 * @param cls Closure (unused).
1470 * @param message Message received.
1471 * @param peer Peer who sent the message.
1473 * @return GNUNET_OK to keep the connection open,
1474 * GNUNET_SYSERR to close it (signal serious error)
1477 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1478 const struct GNUNET_MessageHeader *message)
1480 return handle_mesh_encrypted (peer,
1481 (struct GNUNET_MESH_Encrypted *)message);
1486 * Core handler for mesh network traffic point-to-point acks.
1488 * @param cls closure
1489 * @param message message
1490 * @param peer peer identity this notification is about
1492 * @return GNUNET_OK to keep the connection open,
1493 * GNUNET_SYSERR to close it (signal serious error)
1496 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1497 const struct GNUNET_MessageHeader *message)
1499 struct GNUNET_MESH_ACK *msg;
1500 struct MeshConnection *c;
1501 struct MeshFlowControl *fc;
1506 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1507 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1509 msg = (struct GNUNET_MESH_ACK *) message;
1511 c = connection_get (&msg->cid);
1515 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1520 /* Is this a forward or backward ACK? */
1521 id = GNUNET_PEER_search (peer);
1522 if (GMP_get_short_id (get_next_hop (c)) == id)
1524 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1528 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1530 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1536 GNUNET_break_op (0);
1540 ack = ntohl (msg->ack);
1541 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1542 ack, fc->last_ack_recv);
1543 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1544 fc->last_ack_recv = ack;
1546 /* Cancel polling if the ACK is big enough. */
1547 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1548 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1550 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1551 GNUNET_SCHEDULER_cancel (fc->poll_task);
1552 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1553 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1556 connection_unlock_queue (c, fwd);
1563 * Core handler for mesh network traffic point-to-point ack polls.
1565 * @param cls closure
1566 * @param message message
1567 * @param peer peer identity this notification is about
1569 * @return GNUNET_OK to keep the connection open,
1570 * GNUNET_SYSERR to close it (signal serious error)
1573 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1574 const struct GNUNET_MessageHeader *message)
1576 struct GNUNET_MESH_Poll *msg;
1577 struct MeshConnection *c;
1578 struct MeshFlowControl *fc;
1583 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1584 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1587 msg = (struct GNUNET_MESH_Poll *) message;
1589 c = connection_get (&msg->cid);
1593 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1595 GNUNET_break_op (0);
1599 /* Is this a forward or backward ACK?
1600 * Note: a poll should never be needed in a loopback case,
1601 * since there is no possiblility of packet loss there, so
1602 * this way of discerining FWD/BCK should not be a problem.
1604 id = GNUNET_PEER_search (peer);
1605 if (GMP_get_short_id (get_next_hop (c)) == id)
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1610 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1612 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1617 GNUNET_break_op (0);
1621 pid = ntohl (msg->pid);
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1623 pid, fc->last_pid_recv);
1624 fc->last_pid_recv = pid;
1625 fwd = fc == &c->fwd_fc;
1626 GMC_send_ack (c, NULL, fwd);
1633 * Core handler for mesh keepalives.
1635 * @param cls closure
1636 * @param message message
1637 * @param peer peer identity this notification is about
1638 * @return GNUNET_OK to keep the connection open,
1639 * GNUNET_SYSERR to close it (signal serious error)
1641 * TODO: Check who we got this from, to validate route.
1644 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1645 const struct GNUNET_MessageHeader *message)
1647 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1648 struct MeshConnection *c;
1649 struct MeshPeer *neighbor;
1652 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1653 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1656 c = connection_get (&msg->cid);
1659 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1664 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1665 GNUNET_YES : GNUNET_NO;
1667 /* Check if origin is as expected */
1668 neighbor = get_hop (c, fwd);
1669 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1671 GNUNET_break_op (0);
1675 connection_change_state (c, MESH_CONNECTION_READY);
1676 connection_reset_timeout (c, fwd);
1678 if (GMC_is_terminal (c, fwd))
1681 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1682 GMC_send_prebuilt_message (message, c, NULL, fwd);
1689 * Send an ACK on the appropriate connection/channel, depending on
1690 * the direction and the position of the peer.
1692 * @param c Which connection to send the hop-by-hop ACK.
1693 * @param ch Channel, if any.
1694 * @param fwd Is this a fwd ACK? (will go dest->root)
1697 GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1699 unsigned int buffer;
1701 LOG (GNUNET_ERROR_TYPE_DEBUG,
1702 "send ack %s on %p %p\n",
1703 fwd ? "FWD" : "BCK", c, ch);
1705 /* Get available bufffer space */
1706 if (NULL == c || GMC_is_terminal (c, fwd))
1708 struct MeshTunnel3 *t;
1709 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n");
1710 t = (NULL == c) ? GMCH_get_tunnel (ch) : GMC_get_tunnel (c);
1711 buffer = GMT_get_buffer (t, fwd);
1715 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1716 buffer = GMC_get_buffer (c, fwd);
1718 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1720 /* Send available buffer space */
1721 if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1722 (NULL != c && GMC_is_origin (c, fwd)) )
1724 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1727 GNUNET_assert (NULL != ch);
1728 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1729 GMCH_send_data_ack (ch, fwd);
1734 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
1735 GNUNET_assert (NULL != ch);
1736 GMT_send_acks (GMCH_get_tunnel (ch), buffer, fwd);
1740 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1741 send_ack (c, buffer, fwd);
1747 * Initialize the connections subsystem
1749 * @param c Configuration handle.
1752 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1754 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1756 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1759 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1760 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1761 GNUNET_SCHEDULER_shutdown ();
1766 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1769 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1770 "MESH", "MAX_CONNECTIONS", "MISSING");
1771 GNUNET_SCHEDULER_shutdown ();
1776 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1777 &refresh_connection_time))
1779 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1780 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1781 GNUNET_SCHEDULER_shutdown ();
1784 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1788 * Shut down the connections subsystem.
1793 GNUNET_CONTAINER_multihashmap_destroy (connections);
1797 struct MeshConnection *
1798 GMC_new (const struct GNUNET_HashCode *cid,
1799 struct MeshTunnel3 *t,
1800 struct MeshPeerPath *p,
1801 unsigned int own_pos)
1803 struct MeshConnection *c;
1805 c = GNUNET_new (struct MeshConnection);
1807 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1808 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1809 fc_init (&c->fwd_fc);
1810 fc_init (&c->bck_fc);
1815 if (own_pos > p->length - 1)
1821 c->own_pos = own_pos;
1826 c->fwd_maintenance_task =
1827 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1828 &connection_fwd_keepalive, c);
1830 register_neighbors (c);
1836 GMC_destroy (struct MeshConnection *c)
1841 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n",
1842 GNUNET_h2s (&c->id));
1844 /* Cancel all traffic */
1845 connection_cancel_queues (c, GNUNET_YES);
1846 connection_cancel_queues (c, GNUNET_NO);
1848 /* Cancel maintainance task (keepalive/timeout) */
1849 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1850 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1851 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1852 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1854 /* Unregister from neighbors */
1855 unregister_neighbors (c);
1858 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1860 GMT_remove_connection (c->t, c);
1865 * Get the connection ID.
1867 * @param c Connection to get the ID from.
1869 * @return ID of the connection.
1871 const struct GNUNET_HashCode *
1872 GMC_get_id (const struct MeshConnection *c)
1879 * Get the connection path.
1881 * @param c Connection to get the path from.
1883 * @return path used by the connection.
1885 const struct MeshPeerPath *
1886 GMC_get_path (const struct MeshConnection *c)
1893 * Get the connection state.
1895 * @param c Connection to get the state from.
1897 * @return state of the connection.
1899 enum MeshConnectionState
1900 GMC_get_state (const struct MeshConnection *c)
1906 * Get the connection tunnel.
1908 * @param c Connection to get the tunnel from.
1910 * @return tunnel of the connection.
1912 struct MeshTunnel3 *
1913 GMC_get_tunnel (const struct MeshConnection *c)
1920 * Get free buffer space in a connection.
1922 * @param c Connection.
1923 * @param fwd Is query about FWD traffic?
1925 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1928 GMC_get_buffer (struct MeshConnection *c, int fwd)
1930 struct MeshFlowControl *fc;
1932 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1934 return (fc->queue_max - fc->queue_n);
1938 * Get how many messages have we allowed to send to us from a direction..
1940 * @param c Connection.
1941 * @param fwd Are we asking about traffic from FWD (BCK messages)?
1943 * @return last_ack_sent - last_pid_recv
1946 GMC_get_allowed (struct MeshConnection *c, int fwd)
1948 struct MeshFlowControl *fc;
1950 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1951 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
1955 return (fc->last_ack_sent - fc->last_pid_recv);
1959 * Get messages queued in a connection.
1961 * @param c Connection.
1962 * @param fwd Is query about FWD traffic?
1964 * @return Number of messages queued.
1967 GMC_get_qn (struct MeshConnection *c, int fwd)
1969 struct MeshFlowControl *fc;
1971 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1978 * Allow the connection to advertise a buffer of the given size.
1980 * The connection will send an @c fwd ACK message (so: in direction !fwd)
1981 * allowing up to last_pid_recv + buffer.
1983 * @param c Connection.
1984 * @param buffer How many more messages the connection can accept.
1985 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
1988 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
1990 send_ack (c, buffer, fwd);
1995 * Notify other peers on a connection of a broken link. Mark connections
1996 * to destroy after all traffic has been sent.
1998 * @param c Connection on which there has been a disconnection.
1999 * @param peer Peer that disconnected.
2002 GMC_notify_broken (struct MeshConnection *c,
2003 struct MeshPeer *peer)
2007 fwd = peer == get_prev_hop (c);
2009 connection_cancel_queues (c, !fwd);
2010 if (GMC_is_terminal (c, fwd))
2012 /* Local shutdown, no one to notify about this. */
2017 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2019 /* Connection will have at least one pending message
2020 * (the one we just scheduled), so no point in checking whether to
2021 * destroy immediately. */
2022 c->destroy = GNUNET_YES;
2029 * Is this peer the first one on the connection?
2031 * @param c Connection.
2032 * @param fwd Is this about fwd traffic?
2034 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2037 GMC_is_origin (struct MeshConnection *c, int fwd)
2039 if (!fwd && c->path->length - 1 == c->own_pos )
2041 if (fwd && 0 == c->own_pos)
2048 * Is this peer the last one on the connection?
2050 * @param c Connection.
2051 * @param fwd Is this about fwd traffic?
2052 * Note that the ROOT is the terminal for BCK traffic!
2054 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2057 GMC_is_terminal (struct MeshConnection *c, int fwd)
2059 return GMC_is_origin (c, !fwd);
2064 * See if we are allowed to send by the next hop in the given direction.
2066 * @param c Connection.
2067 * @param fwd Is this about fwd traffic?
2069 * @return GNUNET_YES in case it's OK.
2072 GMC_is_sendable (struct MeshConnection *c, int fwd)
2074 struct MeshFlowControl *fc;
2076 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2077 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2083 * Sends an already built message on a connection, properly registering
2084 * all used resources.
2086 * @param message Message to send. Function makes a copy of it.
2087 * If message is not hop-by-hop, decrements TTL of copy.
2088 * @param c Connection on which this message is transmitted.
2089 * @param ch Channel on which this message is transmitted, or NULL.
2090 * @param fwd Is this a fwd message?
2093 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2094 struct MeshConnection *c,
2095 struct MeshChannel *ch,
2098 struct MeshFlowControl *fc;
2104 size = ntohs (message->size);
2105 data = GNUNET_malloc (size);
2106 memcpy (data, message, size);
2107 type = ntohs (message->type);
2108 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2109 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
2111 droppable = GNUNET_YES;
2114 struct GNUNET_MESH_Encrypted *emsg;
2115 struct GNUNET_MESH_ACK *amsg;
2116 struct GNUNET_MESH_Poll *pmsg;
2117 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2118 struct GNUNET_MESH_ConnectionBroken *bmsg;
2121 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2122 emsg = (struct GNUNET_MESH_Encrypted *) data;
2123 ttl = ntohl (emsg->ttl);
2126 GNUNET_break_op (0);
2130 emsg->ttl = htonl (ttl - 1);
2131 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2132 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
2135 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2136 amsg = (struct GNUNET_MESH_ACK *) data;
2138 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2139 droppable = GNUNET_NO;
2142 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2143 pmsg = (struct GNUNET_MESH_Poll *) data;
2145 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2146 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2147 droppable = GNUNET_NO;
2150 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2151 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2156 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2157 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2162 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2163 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2170 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2171 if (fc->queue_n >= fc->queue_max && droppable)
2173 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2176 LOG (GNUNET_ERROR_TYPE_DEBUG,
2177 "queue full: %u/%u\n",
2178 fc->queue_n, fc->queue_max);
2179 return; /* Drop this message */
2182 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
2183 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
2184 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2185 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2187 GMC_start_poll (c, fwd);
2190 c->pending_messages++;
2192 GMP_queue_add (get_hop (c, fwd), data, type, size, c, ch, fwd,
2193 &message_sent, NULL);
2198 * Sends a CREATE CONNECTION message for a path to a peer.
2199 * Changes the connection and tunnel states if necessary.
2201 * @param connection Connection to create.
2204 GMC_send_create (struct MeshConnection *connection)
2206 enum MeshTunnel3State state;
2209 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2210 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2211 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2212 GMP_queue_add (get_next_hop (connection), NULL,
2213 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2214 size, connection, NULL,
2215 GNUNET_YES, &message_sent, NULL);
2216 state = GMT_get_state (connection->t);
2217 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2218 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2219 if (MESH_CONNECTION_NEW == connection->state)
2220 connection_change_state (connection, MESH_CONNECTION_SENT);
2221 connection->fwd_fc.queue_n++;
2226 * Send a message to all peers in this connection that the connection
2227 * is no longer valid.
2229 * If some peer should not receive the message, it should be zero'ed out
2230 * before calling this function.
2232 * @param c The connection whose peers to notify.
2235 GMC_send_destroy (struct MeshConnection *c)
2237 struct GNUNET_MESH_ConnectionDestroy msg;
2239 if (GNUNET_YES == c->destroy)
2242 msg.header.size = htons (sizeof (msg));
2243 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2245 LOG (GNUNET_ERROR_TYPE_DEBUG,
2246 " sending connection destroy for connection %s\n",
2247 GNUNET_h2s (&c->id));
2249 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2250 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2251 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2252 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2253 c->destroy = GNUNET_YES;
2258 * @brief Start a polling timer for the connection.
2260 * When a neighbor does not accept more traffic on the connection it could be
2261 * caused by a simple congestion or by a lost ACK. Polling enables to check
2262 * for the lastest ACK status for a connection.
2264 * @param c Connection.
2265 * @param fwd Should we poll in the FWD direction?
2268 GMC_start_poll (struct MeshConnection *c, int fwd)
2270 struct MeshFlowControl *fc;
2272 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2273 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2277 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2284 * @brief Stop polling a connection for ACKs.
2286 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2288 * @param c Connection.
2289 * @param fwd Should we stop the poll in the FWD direction?
2292 GMC_stop_poll (struct MeshConnection *c, int fwd)
2294 struct MeshFlowControl *fc;
2296 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2297 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2299 GNUNET_SCHEDULER_cancel (fc->poll_task);
2300 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;