2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh_connection.c
23 * @brief GNUnet MESH service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "mesh_path.h"
33 #include "mesh_protocol_enc.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
42 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
43 GNUNET_TIME_UNIT_MINUTES,\
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct to encapsulate all the Flow Control information to a peer to which
54 * we are directly connected (on a core level).
56 struct MeshFlowControl
59 * Connection this controls.
61 struct MeshConnection *c;
64 * How many messages are in the queue on this connection.
69 * How many messages do we accept in the queue.
71 unsigned int queue_max;
79 * ID of the last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 GNUNET_SCHEDULER_TaskIdentifier poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
110 * Keep a record of the last messages sent on this connection.
112 struct MeshConnectionPerformance
115 * Circular buffer for storing measurements.
117 double usecsperbyte[AVG_MSGS];
120 * Running average of @c usecsperbyte.
125 * How many values of @c usecsperbyte are valid.
130 * Index of the next "free" position in @c usecsperbyte.
137 * Struct containing all information regarding a connection to a peer.
139 struct MeshConnection
142 * Tunnel this connection is part of.
144 struct MeshTunnel3 *t;
147 * Flow control information for traffic fwd.
149 struct MeshFlowControl fwd_fc;
152 * Flow control information for traffic bck.
154 struct MeshFlowControl bck_fc;
157 * Measure connection performance on the endpoint.
159 struct MeshConnectionPerformance *perf;
162 * ID of the connection.
164 struct GNUNET_HashCode id;
167 * State of the connection.
169 enum MeshConnectionState state;
172 * Path being used for the tunnel.
174 struct MeshPeerPath *path;
177 * Position of the local peer in the path.
179 unsigned int own_pos;
182 * Task to keep the used paths alive at the owner,
183 * time tunnel out on all the other peers.
185 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
188 * Task to keep the used paths alive at the destination,
189 * time tunnel out on all the other peers.
191 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
194 * Pending message count.
196 int pending_messages;
199 * Destroy flag: if true, destroy on last message.
204 /******************************************************************************/
205 /******************************* GLOBALS ***********************************/
206 /******************************************************************************/
209 * Global handle to the statistics service.
211 extern struct GNUNET_STATISTICS_Handle *stats;
214 * Local peer own ID (memory efficient handle).
216 extern GNUNET_PEER_Id myid;
219 * Local peer own ID (full value).
221 extern struct GNUNET_PeerIdentity my_full_id;
224 * Connections known, indexed by cid (MeshConnection).
226 static struct GNUNET_CONTAINER_MultiHashMap *connections;
229 * How many connections are we willing to maintain.
230 * Local connections are always allowed, even if there are more connections than max.
232 static unsigned long long max_connections;
235 * How many messages *in total* are we willing to queue, divide by number of
236 * connections to get connection queue size.
238 static unsigned long long max_msgs_queue;
241 * How often to send path keepalives. Paths timeout after 4 missed.
243 static struct GNUNET_TIME_Relative refresh_connection_time;
246 /******************************************************************************/
247 /******************************** STATIC ***********************************/
248 /******************************************************************************/
250 #if 0 // avoid compiler warning for unused static function
252 fc_debug (struct MeshFlowControl *fc)
254 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
255 fc->last_pid_recv, fc->last_ack_sent);
256 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
257 fc->last_pid_sent, fc->last_ack_recv);
258 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
259 fc->queue_n, fc->queue_max);
263 connection_debug (struct MeshConnection *c)
267 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
270 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
271 peer2s (c->t->peer), GMC_2s (c));
272 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
273 c->state, c->pending_messages);
274 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
275 fc_debug (&c->fwd_fc);
276 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
277 fc_debug (&c->bck_fc);
282 * Get string description for tunnel state.
284 * @param s Tunnel state.
286 * @return String representation.
289 GMC_state2s (enum MeshConnectionState s)
293 case MESH_CONNECTION_NEW:
294 return "MESH_CONNECTION_NEW";
295 case MESH_CONNECTION_SENT:
296 return "MESH_CONNECTION_SENT";
297 case MESH_CONNECTION_ACK:
298 return "MESH_CONNECTION_ACK";
299 case MESH_CONNECTION_READY:
300 return "MESH_CONNECTION_READY";
302 return "MESH_CONNECTION_STATE_ERROR";
308 * Initialize a Flow Control structure to the initial state.
310 * @param fc Flow Control structure to initialize.
313 fc_init (struct MeshFlowControl *fc)
316 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
317 fc->last_pid_recv = (uint32_t) -1;
318 fc->last_ack_sent = (uint32_t) 0;
319 fc->last_ack_recv = (uint32_t) 0;
320 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
321 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
323 fc->queue_max = (max_msgs_queue / max_connections) + 1;
330 * @param cid Connection ID.
332 static struct MeshConnection *
333 connection_get (const struct GNUNET_HashCode *cid)
335 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
340 connection_change_state (struct MeshConnection* c,
341 enum MeshConnectionState state)
343 LOG (GNUNET_ERROR_TYPE_DEBUG,
344 "Connection %s state was %s\n",
345 GMC_2s (c), GMC_state2s (c->state));
346 LOG (GNUNET_ERROR_TYPE_DEBUG,
347 "Connection %s state is now %s\n",
348 GMC_2s (c), GMC_state2s (state));
354 * Send an ACK on the connection, informing the predecessor about
355 * the available buffer space. Should not be called in case the peer
356 * is origin (no predecessor).
358 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
359 * the ACK itself goes "back" (dest->root).
361 * @param c Connection on which to send the ACK.
362 * @param buffer How much space free to advertise?
363 * @param fwd Is this FWD ACK? (Going dest->owner)
366 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
368 struct MeshFlowControl *next_fc;
369 struct MeshFlowControl *prev_fc;
370 struct GNUNET_MESH_ACK msg;
374 /* If origin, there is no connection to send ACKs. Wrong function! */
375 if (GMC_is_origin (c, fwd))
381 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
382 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 "connection send %s ack on %s\n",
386 fwd ? "FWD" : "BCK", GMC_2s (c));
388 /* Check if we need to transmit the ACK */
389 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
390 if (3 < delta && buffer < delta)
392 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 " last pid recv: %u, last ack sent: %u\n",
395 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
399 /* Ok, ACK might be necessary, what PID to ACK? */
400 ack = prev_fc->last_pid_recv + buffer;
401 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
402 LOG (GNUNET_ERROR_TYPE_DEBUG,
403 " last pid %u, last ack %u, qmax %u, q %u\n",
404 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
405 next_fc->queue_max, next_fc->queue_n);
406 if (ack == prev_fc->last_ack_sent)
408 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
412 prev_fc->last_ack_sent = ack;
414 /* Build ACK message and send on connection */
415 msg.header.size = htons (sizeof (msg));
416 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
417 msg.ack = htonl (ack);
420 GMC_send_prebuilt_message (&msg.header, c, !fwd);
425 * Callback called when a queued message is sent.
427 * Calculates the average time and connection packet tracking.
429 * @param cls Closure.
430 * @param c Connection this message was on.
431 * @param type Type of message sent.
432 * @param fwd Was this a FWD going message?
433 * @param size Size of the message.
434 * @param wait Time spent waiting for core (only the time for THIS message)
437 message_sent (void *cls,
438 struct MeshConnection *c, uint16_t type,
439 int fwd, size_t size,
440 struct GNUNET_TIME_Relative wait)
442 struct MeshConnectionPerformance *p;
443 struct MeshFlowControl *fc;
446 fc = fwd ? &c->fwd_fc : &c->bck_fc;
447 LOG (GNUNET_ERROR_TYPE_DEBUG, "! sent %s\n", GNUNET_MESH_DEBUG_M2S (type));
448 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
449 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
450 c->pending_messages--;
451 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
453 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
457 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
460 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
463 LOG (GNUNET_ERROR_TYPE_DEBUG,
464 "! accounting pid %u\n",
466 GMC_send_ack (c, fwd);
471 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
474 return; /* Only endpoints are interested in timing. */
477 usecsperbyte = ((double) wait.rel_value_us) / size;
478 if (p->size == AVG_MSGS)
480 /* Array is full. Substract oldest value, add new one and store. */
481 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
482 p->usecsperbyte[p->idx] = usecsperbyte;
483 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
487 /* Array not yet full. Add current value to avg and store. */
488 p->usecsperbyte[p->idx] = usecsperbyte;
490 p->avg += p->usecsperbyte[p->idx];
494 p->idx = (p->idx + 1) % AVG_MSGS;
499 * Get the previous hop in a connection
501 * @param c Connection.
503 * @return Previous peer in the connection.
505 static struct MeshPeer *
506 get_prev_hop (const struct MeshConnection *c)
510 LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos);
511 if (0 == c->own_pos || c->path->length < 2)
512 id = c->path->peers[0];
514 id = c->path->peers[c->own_pos - 1];
516 return GMP_get_short (id);
521 * Get the next hop in a connection
523 * @param c Connection.
525 * @return Next peer in the connection.
527 static struct MeshPeer *
528 get_next_hop (const struct MeshConnection *c)
532 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
533 id = c->path->peers[c->path->length - 1];
535 id = c->path->peers[c->own_pos + 1];
537 return GMP_get_short (id);
542 * Get the hop in a connection.
544 * @param c Connection.
545 * @param fwd Next hop?
547 * @return Next peer in the connection.
549 static struct MeshPeer *
550 get_hop (struct MeshConnection *c, int fwd)
553 return get_next_hop (c);
554 return get_prev_hop (c);
559 * Is traffic coming from this sender 'FWD' traffic?
561 * @param c Connection to check.
562 * @param sender Peer identity of neighbor.
564 * @return GNUNET_YES in case the sender is the 'prev' hop and therefore
565 * the traffic is 'FWD'. GNUNET_NO for BCK. GNUNET_SYSERR for errors.
568 is_fwd (const struct MeshConnection *c,
569 const struct GNUNET_PeerIdentity *sender)
573 id = GNUNET_PEER_search (sender);
574 if (GMP_get_short_id (get_prev_hop (c)) == id)
577 if (GMP_get_short_id (get_next_hop (c)) == id)
581 return GNUNET_SYSERR;
586 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
587 * or a first CONNECTION_ACK directed to us.
589 * @param connection Connection to confirm.
590 * @param fwd Should we send it FWD? (root->dest)
591 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
594 send_connection_ack (struct MeshConnection *connection, int fwd)
596 struct MeshTunnel3 *t;
599 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
600 !fwd ? "FWD" : "BCK");
601 GMP_queue_add (get_hop (connection, fwd), NULL,
602 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
603 sizeof (struct GNUNET_MESH_ConnectionACK),
604 connection, fwd, &message_sent, NULL);
605 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
606 GMT_change_state (t, MESH_TUNNEL3_WAITING);
607 if (MESH_CONNECTION_READY != connection->state)
608 connection_change_state (connection, MESH_CONNECTION_SENT);
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, 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]\n",
657 fwd ? "FWD" : "BCK", GMC_2s (c));
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, 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", GMC_2s (c));
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 * If the connection is scheduled for destruction and no more messages are left,
783 * the connection will be destroyed by the continuation call.
785 * @param c Connection which to cancel. Might be destroyed during this call.
786 * @param fwd Cancel fwd traffic?
789 connection_cancel_queues (struct MeshConnection *c, int fwd)
791 struct MeshFlowControl *fc;
792 struct MeshPeer *peer;
800 fc = fwd ? &c->fwd_fc : &c->bck_fc;
801 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
803 GNUNET_SCHEDULER_cancel (fc->poll_task);
804 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
806 peer = get_hop (c, fwd);
807 GMP_queue_cancel (peer, c);
812 * Function called if a connection has been stalled for a while,
813 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
815 * @param cls Closure (poll ctx).
816 * @param tc TaskContext.
819 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
821 struct MeshFlowControl *fc = cls;
822 struct GNUNET_MESH_Poll msg;
823 struct MeshConnection *c;
825 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
826 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
832 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
833 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
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, 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, 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, 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, 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, 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 msg 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 /* Unexpected peer sending traffic on a connection. */
1401 GNUNET_break_op (0);
1407 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1408 pid = ntohl (msg->pid);
1409 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1411 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1412 LOG (GNUNET_ERROR_TYPE_DEBUG,
1413 "WARNING Received PID %u, (prev %u), ACK %u\n",
1414 pid, fc->last_pid_recv, fc->last_ack_sent);
1417 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1419 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1420 LOG (GNUNET_ERROR_TYPE_DEBUG,
1421 " Pid %u not expected (%u+), dropping!\n",
1422 pid, fc->last_pid_recv + 1);
1425 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1426 connection_change_state (c, MESH_CONNECTION_READY);
1427 connection_reset_timeout (c, fwd);
1428 fc->last_pid_recv = pid;
1430 /* Is this message for us? */
1431 if (GMC_is_terminal (c, fwd))
1433 /* TODO signature verification */
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1435 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1442 fc->last_pid_recv = pid;
1443 GMT_handle_encrypted (c->t, msg, fwd);
1444 GMC_send_ack (c, fwd);
1448 /* Message not for us: forward to next hop */
1449 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1450 ttl = ntohl (msg->ttl);
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1454 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1455 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1456 GMC_send_ack (c, fwd);
1459 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1461 GMC_send_prebuilt_message (&msg->header, c, fwd);
1468 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1470 * @param cls Closure (unused).
1471 * @param message Message received.
1472 * @param peer Peer who sent the message.
1474 * @return GNUNET_OK to keep the connection open,
1475 * GNUNET_SYSERR to close it (signal serious error)
1478 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1479 const struct GNUNET_MessageHeader *message)
1481 return handle_mesh_encrypted (peer,
1482 (struct GNUNET_MESH_Encrypted *)message);
1487 * Core handler for mesh network traffic point-to-point acks.
1489 * @param cls closure
1490 * @param message message
1491 * @param peer peer identity this notification is about
1493 * @return GNUNET_OK to keep the connection open,
1494 * GNUNET_SYSERR to close it (signal serious error)
1497 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1498 const struct GNUNET_MessageHeader *message)
1500 struct GNUNET_MESH_ACK *msg;
1501 struct MeshConnection *c;
1502 struct MeshFlowControl *fc;
1507 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1508 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1510 msg = (struct GNUNET_MESH_ACK *) message;
1512 c = connection_get (&msg->cid);
1516 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1521 /* Is this a forward or backward ACK? */
1522 id = GNUNET_PEER_search (peer);
1523 if (GMP_get_short_id (get_next_hop (c)) == id)
1525 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1529 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1531 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1537 GNUNET_break_op (0);
1541 ack = ntohl (msg->ack);
1542 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1543 ack, fc->last_ack_recv);
1544 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1545 fc->last_ack_recv = ack;
1547 /* Cancel polling if the ACK is big enough. */
1548 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1549 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1551 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1552 GNUNET_SCHEDULER_cancel (fc->poll_task);
1553 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1554 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1557 connection_unlock_queue (c, fwd);
1564 * Core handler for mesh network traffic point-to-point ack polls.
1566 * @param cls closure
1567 * @param message message
1568 * @param peer peer identity this notification is about
1570 * @return GNUNET_OK to keep the connection open,
1571 * GNUNET_SYSERR to close it (signal serious error)
1574 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1575 const struct GNUNET_MessageHeader *message)
1577 struct GNUNET_MESH_Poll *msg;
1578 struct MeshConnection *c;
1579 struct MeshFlowControl *fc;
1584 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1585 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1588 msg = (struct GNUNET_MESH_Poll *) message;
1590 c = connection_get (&msg->cid);
1594 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1596 GNUNET_break_op (0);
1600 /* Is this a forward or backward ACK?
1601 * Note: a poll should never be needed in a loopback case,
1602 * since there is no possiblility of packet loss there, so
1603 * this way of discerining FWD/BCK should not be a problem.
1605 id = GNUNET_PEER_search (peer);
1606 if (GMP_get_short_id (get_next_hop (c)) == id)
1608 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1611 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1613 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1618 GNUNET_break_op (0);
1622 pid = ntohl (msg->pid);
1623 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1624 pid, fc->last_pid_recv);
1625 fc->last_pid_recv = pid;
1626 fwd = fc == &c->fwd_fc;
1627 GMC_send_ack (c, fwd);
1634 * Core handler for mesh keepalives.
1636 * @param cls closure
1637 * @param message message
1638 * @param peer peer identity this notification is about
1639 * @return GNUNET_OK to keep the connection open,
1640 * GNUNET_SYSERR to close it (signal serious error)
1642 * TODO: Check who we got this from, to validate route.
1645 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1646 const struct GNUNET_MessageHeader *message)
1648 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1649 struct MeshConnection *c;
1650 struct MeshPeer *neighbor;
1653 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1654 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1657 c = connection_get (&msg->cid);
1660 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1665 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1666 GNUNET_YES : GNUNET_NO;
1668 /* Check if origin is as expected */
1669 neighbor = get_hop (c, fwd);
1670 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1672 GNUNET_break_op (0);
1676 connection_change_state (c, MESH_CONNECTION_READY);
1677 connection_reset_timeout (c, fwd);
1679 if (GMC_is_terminal (c, fwd))
1682 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1683 GMC_send_prebuilt_message (message, c, fwd);
1690 * Send an ACK on the appropriate connection/channel, depending on
1691 * the direction and the position of the peer.
1693 * @param c Which connection to send the hop-by-hop ACK.
1694 * @param fwd Is this a fwd ACK? (will go dest->root)
1697 GMC_send_ack (struct MeshConnection *c, int fwd)
1699 unsigned int buffer;
1701 LOG (GNUNET_ERROR_TYPE_DEBUG,
1702 "GMC send %s ACK on %s\n",
1703 fwd ? "FWD" : "BCK", GMC_2s (c));
1711 /* Get available buffer space */
1712 if (GMC_is_terminal (c, fwd))
1714 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
1715 buffer = GMT_get_buffer (c->t, fwd);
1719 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1720 buffer = GMC_get_buffer (c, fwd);
1722 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1724 /* Send available buffer space */
1725 if (GMC_is_origin (c, fwd))
1727 GNUNET_assert (NULL != c->t);
1728 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
1731 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1732 GMT_unchoke_channels (c->t, fwd);
1737 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1738 send_ack (c, buffer, fwd);
1744 * Initialize the connections subsystem
1746 * @param c Configuration handle.
1749 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1751 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1753 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1756 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1757 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1758 GNUNET_SCHEDULER_shutdown ();
1763 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1766 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1767 "MESH", "MAX_CONNECTIONS", "MISSING");
1768 GNUNET_SCHEDULER_shutdown ();
1773 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1774 &refresh_connection_time))
1776 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1777 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1778 GNUNET_SCHEDULER_shutdown ();
1781 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1785 * Shut down the connections subsystem.
1790 GNUNET_CONTAINER_multihashmap_destroy (connections);
1794 struct MeshConnection *
1795 GMC_new (const struct GNUNET_HashCode *cid,
1796 struct MeshTunnel3 *t,
1797 struct MeshPeerPath *p,
1798 unsigned int own_pos)
1800 struct MeshConnection *c;
1802 c = GNUNET_new (struct MeshConnection);
1804 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1805 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1806 fc_init (&c->fwd_fc);
1807 fc_init (&c->bck_fc);
1812 if (own_pos > p->length - 1)
1818 c->own_pos = own_pos;
1823 c->fwd_maintenance_task =
1824 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1825 &connection_fwd_keepalive, c);
1827 register_neighbors (c);
1833 GMC_destroy (struct MeshConnection *c)
1838 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
1839 return; /* -> message_sent -> GMC_destroy. Don't loop. */
1842 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
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);
1862 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES))
1863 path_destroy (c->path);
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 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2015 /* Local shutdown, no one to notify about this. */
2019 if (GNUNET_NO == c->destroy)
2020 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2022 /* Connection will have at least one pending message
2023 * (the one we just scheduled), so no point in checking whether to
2024 * destroy immediately. */
2025 c->destroy = GNUNET_YES;
2028 * Cancel all queues, if no message is left, connection will be destroyed.
2030 connection_cancel_queues (c, !fwd);
2037 * Is this peer the first one on the connection?
2039 * @param c Connection.
2040 * @param fwd Is this about fwd traffic?
2042 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2045 GMC_is_origin (struct MeshConnection *c, int fwd)
2047 if (!fwd && c->path->length - 1 == c->own_pos )
2049 if (fwd && 0 == c->own_pos)
2056 * Is this peer the last one on the connection?
2058 * @param c Connection.
2059 * @param fwd Is this about fwd traffic?
2060 * Note that the ROOT is the terminal for BCK traffic!
2062 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2065 GMC_is_terminal (struct MeshConnection *c, int fwd)
2067 return GMC_is_origin (c, !fwd);
2072 * See if we are allowed to send by the next hop in the given direction.
2074 * @param c Connection.
2075 * @param fwd Is this about fwd traffic?
2077 * @return GNUNET_YES in case it's OK.
2080 GMC_is_sendable (struct MeshConnection *c, int fwd)
2082 struct MeshFlowControl *fc;
2084 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2085 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2091 * Sends an already built message on a connection, properly registering
2092 * all used resources.
2094 * @param message Message to send. Function makes a copy of it.
2095 * If message is not hop-by-hop, decrements TTL of copy.
2096 * @param c Connection on which this message is transmitted.
2097 * @param fwd Is this a fwd message?
2100 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2101 struct MeshConnection *c,
2104 struct MeshFlowControl *fc;
2110 size = ntohs (message->size);
2111 data = GNUNET_malloc (size);
2112 memcpy (data, message, size);
2113 type = ntohs (message->type);
2114 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2115 GNUNET_MESH_DEBUG_M2S (type), size, GMC_2s (c));
2117 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2118 droppable = GNUNET_YES;
2121 struct GNUNET_MESH_Encrypted *emsg;
2122 struct GNUNET_MESH_ACK *amsg;
2123 struct GNUNET_MESH_Poll *pmsg;
2124 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2125 struct GNUNET_MESH_ConnectionBroken *bmsg;
2128 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2129 emsg = (struct GNUNET_MESH_Encrypted *) data;
2130 ttl = ntohl (emsg->ttl);
2133 GNUNET_break_op (0);
2137 emsg->ttl = htonl (ttl - 1);
2138 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2139 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2141 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2142 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
2143 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
2144 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2146 GMC_start_poll (c, fwd);
2150 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2151 amsg = (struct GNUNET_MESH_ACK *) data;
2153 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2154 droppable = GNUNET_NO;
2157 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2158 pmsg = (struct GNUNET_MESH_Poll *) data;
2160 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2161 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2162 droppable = GNUNET_NO;
2165 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2166 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2171 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2172 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2177 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2178 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2185 if (fc->queue_n > fc->queue_max && droppable)
2187 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2190 LOG (GNUNET_ERROR_TYPE_DEBUG,
2191 "queue full: %u/%u\n",
2192 fc->queue_n, fc->queue_max);
2193 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2195 return; /* Drop this message */
2198 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2199 c->pending_messages++;
2201 GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2202 &message_sent, NULL);
2207 * Sends a CREATE CONNECTION message for a path to a peer.
2208 * Changes the connection and tunnel states if necessary.
2210 * @param connection Connection to create.
2213 GMC_send_create (struct MeshConnection *connection)
2215 enum MeshTunnel3State state;
2218 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2219 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2220 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2221 GMP_queue_add (get_next_hop (connection), NULL,
2222 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2223 size, connection, GNUNET_YES, &message_sent, NULL);
2224 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2225 connection, connection->pending_messages);
2226 connection->pending_messages++;
2227 state = GMT_get_state (connection->t);
2228 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2229 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2230 if (MESH_CONNECTION_NEW == connection->state)
2231 connection_change_state (connection, MESH_CONNECTION_SENT);
2236 * Send a message to all peers in this connection that the connection
2237 * is no longer valid.
2239 * If some peer should not receive the message, it should be zero'ed out
2240 * before calling this function.
2242 * @param c The connection whose peers to notify.
2245 GMC_send_destroy (struct MeshConnection *c)
2247 struct GNUNET_MESH_ConnectionDestroy msg;
2249 if (GNUNET_YES == c->destroy)
2252 msg.header.size = htons (sizeof (msg));
2253 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2255 LOG (GNUNET_ERROR_TYPE_DEBUG,
2256 " sending connection destroy for connection %s\n",
2259 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2260 GMC_send_prebuilt_message (&msg.header, c, GNUNET_YES);
2261 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2262 GMC_send_prebuilt_message (&msg.header, c, GNUNET_NO);
2263 c->destroy = GNUNET_YES;
2268 * @brief Start a polling timer for the connection.
2270 * When a neighbor does not accept more traffic on the connection it could be
2271 * caused by a simple congestion or by a lost ACK. Polling enables to check
2272 * for the lastest ACK status for a connection.
2274 * @param c Connection.
2275 * @param fwd Should we poll in the FWD direction?
2278 GMC_start_poll (struct MeshConnection *c, int fwd)
2280 struct MeshFlowControl *fc;
2282 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2283 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2287 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2294 * @brief Stop polling a connection for ACKs.
2296 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2298 * @param c Connection.
2299 * @param fwd Should we stop the poll in the FWD direction?
2302 GMC_stop_poll (struct MeshConnection *c, int fwd)
2304 struct MeshFlowControl *fc;
2306 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2307 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2309 GNUNET_SCHEDULER_cancel (fc->poll_task);
2310 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2315 * Get a (static) string for a connection.
2317 * @param c Connection.
2320 GMC_2s (struct MeshConnection *c)
2324 static char buf[128];
2326 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2329 return GNUNET_h2s (&c->id);