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
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;
377 return; /* Only endpoints are interested in this. */
379 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
381 usecsperbyte = ((double) wait.rel_value_us) / size;
382 if (p->size == AVG_MSGS)
384 /* Array is full. Substract oldest value, add new one and store. */
385 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
386 p->usecsperbyte[p->idx] = usecsperbyte;
387 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
391 /* Array not yet full. Add current value to avg and store. */
392 p->usecsperbyte[p->idx] = usecsperbyte;
394 p->avg += p->usecsperbyte[p->idx];
398 p->idx = (p->idx + 1) % AVG_MSGS;
400 fc = fwd ? &c->fwd_fc : &c->bck_fc;
401 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
403 c->pending_messages--;
404 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
406 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
409 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
412 case GNUNET_MESSAGE_TYPE_MESH_FWD:
413 case GNUNET_MESSAGE_TYPE_MESH_BCK:
415 LOG (GNUNET_ERROR_TYPE_DEBUG, "! accounting pid %u\n", fc->last_pid_sent);
416 // send_ack (c, ch, fwd);
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 * Send an ACK informing the predecessor about the available buffer space.
495 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
496 * the ACK itself goes "back" (dest->root).
498 * @param c Connection on which to send the ACK.
499 * @param buffer How much space free to advertise?
500 * @param fwd Is this FWD ACK? (Going dest->owner)
503 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
505 struct MeshFlowControl *next_fc;
506 struct MeshFlowControl *prev_fc;
507 struct GNUNET_MESH_ACK msg;
511 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
512 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
514 LOG (GNUNET_ERROR_TYPE_DEBUG,
515 "connection send %s ack on %s\n",
516 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
518 /* Check if we need to transmit the ACK */
519 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
520 if (3 < delta && buffer < delta)
522 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
523 LOG (GNUNET_ERROR_TYPE_DEBUG,
524 " last pid recv: %u, last ack sent: %u\n",
525 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
529 /* Ok, ACK might be necessary, what PID to ACK? */
530 ack = prev_fc->last_pid_recv + buffer;
531 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
532 LOG (GNUNET_ERROR_TYPE_DEBUG,
533 " last pid %u, last ack %u, qmax %u, q %u\n",
534 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
535 next_fc->queue_max, next_fc->queue_n);
536 if (ack == prev_fc->last_ack_sent)
538 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
542 prev_fc->last_ack_sent = ack;
544 /* Build ACK message and send on connection */
545 msg.header.size = htons (sizeof (msg));
546 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
547 msg.ack = htonl (ack);
550 GMC_send_prebuilt_message (&msg.header, c, NULL, !fwd);
555 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
556 * or a first CONNECTION_ACK directed to us.
558 * @param connection Connection to confirm.
559 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
562 send_connection_ack (struct MeshConnection *connection, int fwd)
564 struct MeshTunnel3 *t;
567 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
568 GMP_queue_add (get_hop (connection, fwd), NULL,
569 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
570 sizeof (struct GNUNET_MESH_ConnectionACK),
571 connection, NULL, fwd,
572 &message_sent, NULL);
573 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
574 GMT_change_state (t, MESH_TUNNEL3_WAITING);
575 if (MESH_CONNECTION_READY != connection->state)
576 connection_change_state (connection, MESH_CONNECTION_SENT);
581 * Send keepalive packets for a connection.
583 * @param c Connection to keep alive..
584 * @param fwd Is this a FWD keepalive? (owner -> dest).
587 connection_keepalive (struct MeshConnection *c, int fwd)
589 struct GNUNET_MESH_ConnectionKeepAlive *msg;
590 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
594 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
595 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
597 LOG (GNUNET_ERROR_TYPE_DEBUG,
598 "sending %s keepalive for connection %s[%d]\n",
599 fwd ? "FWD" : "BCK", GMT_2s (c->t), c->id);
601 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
602 msg->header.size = htons (size);
603 msg->header.type = htons (type);
606 GMC_send_prebuilt_message (&msg->header, c, NULL, fwd);
611 * Send CONNECTION_{CREATE/ACK} packets for a connection.
613 * @param c Connection for which to send the message.
614 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
617 connection_recreate (struct MeshConnection *c, int fwd)
619 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
623 send_connection_ack (c, GNUNET_NO);
628 * Generic connection timer management.
629 * Depending on the role of the peer in the connection will send the
630 * appropriate message (build or keepalive)
632 * @param c Conncetion to maintain.
636 connection_maintain (struct MeshConnection *c, int fwd)
638 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
640 /* TODO DHT GET with RO_BART */
645 case MESH_CONNECTION_NEW:
647 case MESH_CONNECTION_SENT:
648 connection_recreate (c, fwd);
650 case MESH_CONNECTION_READY:
651 connection_keepalive (c, fwd);
660 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
662 struct MeshConnection *c = cls;
664 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
665 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
668 connection_maintain (c, GNUNET_YES);
669 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
670 &connection_fwd_keepalive,
676 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
678 struct MeshConnection *c = cls;
680 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
681 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
684 connection_maintain (c, GNUNET_NO);
685 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
686 &connection_bck_keepalive,
692 * @brief Re-initiate traffic on this connection if necessary.
694 * Check if there is traffic queued towards this peer
695 * and the core transmit handle is NULL (traffic was stalled).
696 * If so, call core tmt rdy.
698 * @param c Connection on which initiate traffic.
699 * @param fwd Is this about fwd traffic?
702 connection_unlock_queue (struct MeshConnection *c, int fwd)
704 struct MeshPeer *peer;
706 LOG (GNUNET_ERROR_TYPE_DEBUG,
707 "connection_unlock_queue %s on %s\n",
708 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
710 if (GMC_is_terminal (c, fwd))
712 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
716 peer = get_hop (c, fwd);
717 GMP_queue_unlock (peer, c);
722 * Cancel all transmissions that belong to a certain connection.
724 * @param c Connection which to cancel.
725 * @param fwd Cancel fwd traffic?
728 connection_cancel_queues (struct MeshConnection *c, int fwd)
731 struct MeshFlowControl *fc;
732 struct MeshPeer *peer;
740 peer = get_hop (c, fwd);
741 GMP_queue_cancel (peer, c);
743 fc = fwd ? &c->fwd_fc : &c->bck_fc;
744 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
746 GNUNET_SCHEDULER_cancel (fc->poll_task);
747 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
753 * Function called if a connection has been stalled for a while,
754 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
756 * @param cls Closure (poll ctx).
757 * @param tc TaskContext.
760 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
762 struct MeshFlowControl *fc = cls;
763 struct GNUNET_MESH_Poll msg;
764 struct MeshConnection *c;
766 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
767 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
773 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
774 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
775 GNUNET_h2s (&c->id));
776 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
777 fc == &c->fwd_fc ? "FWD" : "BCK");
779 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
780 msg.header.size = htons (sizeof (msg));
781 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
782 GMC_send_prebuilt_message (&msg.header, c, NULL, fc == &c->fwd_fc);
783 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
784 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
785 &connection_poll, fc);
790 * Timeout function due to lack of keepalive/traffic from the owner.
791 * Destroys connection if called.
793 * @param cls Closure (connection to destroy).
794 * @param tc TaskContext.
797 connection_fwd_timeout (void *cls,
798 const struct GNUNET_SCHEDULER_TaskContext *tc)
800 struct MeshConnection *c = cls;
802 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
803 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
805 LOG (GNUNET_ERROR_TYPE_DEBUG,
806 "Connection %s[%X] FWD timed out. Destroying.\n",
810 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
818 * Timeout function due to lack of keepalive/traffic from the destination.
819 * Destroys connection if called.
821 * @param cls Closure (connection to destroy).
822 * @param tc TaskContext
825 connection_bck_timeout (void *cls,
826 const struct GNUNET_SCHEDULER_TaskContext *tc)
828 struct MeshConnection *c = cls;
830 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
831 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
834 LOG (GNUNET_ERROR_TYPE_DEBUG,
835 "Connection %s[%X] FWD timed out. Destroying.\n",
836 GMT_2s (c->t), c->id);
838 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
846 * Resets the connection timeout task, some other message has done the
848 * - For the first peer on the direction this means to send
849 * a keepalive or a path confirmation message (either create or ACK).
850 * - For all other peers, this means to destroy the connection,
851 * due to lack of activity.
852 * Starts the tiemout if no timeout was running (connection just created).
854 * @param c Connection whose timeout to reset.
855 * @param fwd Is this forward?
857 * TODO use heap to improve efficiency of scheduler.
860 connection_reset_timeout (struct MeshConnection *c, int fwd)
862 GNUNET_SCHEDULER_TaskIdentifier *ti;
863 GNUNET_SCHEDULER_Task f;
865 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
867 if (GNUNET_SCHEDULER_NO_TASK != *ti)
868 GNUNET_SCHEDULER_cancel (*ti);
870 if (GMC_is_origin (c, fwd)) /* Endpoint */
872 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
873 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
877 struct GNUNET_TIME_Relative delay;
879 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
880 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
881 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
887 * Add the connection to the list of both neighbors.
889 * @param c Connection.
892 register_neighbors (struct MeshConnection *c)
894 struct MeshPeer *peer;
896 peer = get_next_hop (c);
897 if (GNUNET_NO == GMP_is_neighbor (peer))
902 GMP_add_connection (peer, c);
903 peer = get_prev_hop (c);
904 if (GNUNET_NO == GMP_is_neighbor (peer))
909 GMP_add_connection (peer, c);
914 * Remove the connection from the list of both neighbors.
916 * @param c Connection.
919 unregister_neighbors (struct MeshConnection *c)
921 struct MeshPeer *peer;
923 peer = get_next_hop (c);
924 GMP_remove_connection (peer, c);
926 peer = get_prev_hop (c);
927 GMP_remove_connection (peer, c);
932 /******************************************************************************/
933 /******************************** API ***********************************/
934 /******************************************************************************/
937 * Core handler for connection creation.
939 * @param cls Closure (unused).
940 * @param peer Sender (neighbor).
941 * @param message Message.
943 * @return GNUNET_OK to keep the connection open,
944 * GNUNET_SYSERR to close it (signal serious error)
947 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
948 const struct GNUNET_MessageHeader *message)
950 struct GNUNET_MESH_ConnectionCreate *msg;
951 struct GNUNET_PeerIdentity *id;
952 struct GNUNET_HashCode *cid;
953 struct MeshPeerPath *path;
954 struct MeshPeer *dest_peer;
955 struct MeshPeer *orig_peer;
956 struct MeshConnection *c;
957 unsigned int own_pos;
961 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
962 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
965 size = ntohs (message->size);
966 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
973 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
974 if (size % sizeof (struct GNUNET_PeerIdentity))
979 size /= sizeof (struct GNUNET_PeerIdentity);
985 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
988 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
990 id = (struct GNUNET_PeerIdentity *) &msg[1];
991 LOG (GNUNET_ERROR_TYPE_DEBUG,
992 " connection %s (%s).\n",
993 GNUNET_h2s (cid), GNUNET_i2s (id));
995 /* Create connection */
996 c = connection_get (cid);
1000 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1001 path = path_new (size);
1003 for (i = 0; i < size; i++)
1005 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1006 GNUNET_i2s (&id[i]));
1007 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1008 if (path->peers[i] == myid)
1011 if (own_pos == 0 && path->peers[own_pos] != myid)
1013 /* create path: self not found in path through self */
1014 GNUNET_break_op (0);
1015 path_destroy (path);
1018 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1019 GMP_add_path_to_all (path, GNUNET_NO);
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1021 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1024 connection_reset_timeout (c, GNUNET_YES);
1030 if (MESH_CONNECTION_NEW == c->state)
1031 connection_change_state (c, MESH_CONNECTION_SENT);
1033 /* Remember peers */
1034 dest_peer = GMP_get (&id[size - 1]);
1035 orig_peer = GMP_get (&id[0]);
1037 /* Is it a connection to us? */
1038 if (c->own_pos == size - 1)
1040 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1041 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1043 GMP_add_tunnel (orig_peer);
1044 GMP_add_connection (orig_peer, c);
1045 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1046 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1048 send_connection_ack (c, GNUNET_NO);
1049 if (MESH_CONNECTION_SENT == c->state)
1050 connection_change_state (c, MESH_CONNECTION_ACK);
1052 /* Keep tunnel alive in direction dest->owner*/
1053 connection_reset_timeout (c, GNUNET_NO);
1057 /* It's for somebody else! Retransmit. */
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1059 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1060 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1061 GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES);
1068 * Core handler for path confirmations.
1070 * @param cls closure
1071 * @param message message
1072 * @param peer peer identity this notification is about
1074 * @return GNUNET_OK to keep the connection open,
1075 * GNUNET_SYSERR to close it (signal serious error)
1078 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1079 const struct GNUNET_MessageHeader *message)
1081 struct GNUNET_MESH_ConnectionACK *msg;
1082 struct MeshConnection *c;
1083 struct MeshPeerPath *p;
1084 struct MeshPeer *pi;
1087 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1088 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1089 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1090 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1091 GNUNET_h2s (&msg->cid));
1092 c = connection_get (&msg->cid);
1095 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1097 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1104 pi = GMP_get (peer);
1105 if (get_next_hop (c) == pi)
1107 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1109 if (MESH_CONNECTION_SENT == c->state)
1110 connection_change_state (c, MESH_CONNECTION_ACK);
1112 else if (get_prev_hop (c) == pi)
1114 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1116 connection_change_state (c, MESH_CONNECTION_READY);
1120 GNUNET_break_op (0);
1123 connection_reset_timeout (c, fwd);
1125 /* Add path to peers? */
1129 GMP_add_path_to_all (p, GNUNET_YES);
1136 /* Message for us as creator? */
1137 if (GMC_is_origin (c, GNUNET_YES))
1139 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1140 connection_change_state (c, MESH_CONNECTION_READY);
1141 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1142 send_connection_ack (c, GNUNET_YES);
1143 GMT_send_queued_data (c->t, GNUNET_YES);
1147 /* Message for us as destination? */
1148 if (GMC_is_terminal (c, GNUNET_YES))
1150 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1151 connection_change_state (c, MESH_CONNECTION_READY);
1152 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1153 GMT_send_queued_data (c->t, GNUNET_NO);
1157 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1158 GMC_send_prebuilt_message (message, c, NULL, fwd);
1164 * Is traffic coming from this sender 'FWD' traffic?
1166 * @param c Connection to check.
1167 * @param sender Peer identity of neighbor.
1169 * @return GNUNET_YES in case the sender is the 'prev' hop and therefore
1170 * the traffic is 'FWD'. GNUNET_NO for BCK. GNUNET_SYSERR for errors.
1173 is_fwd (const struct MeshConnection *c,
1174 const struct GNUNET_PeerIdentity *sender)
1178 id = GNUNET_PEER_search (sender);
1179 if (GMP_get_short_id (get_prev_hop (c)) == id)
1182 if (GMP_get_short_id (get_next_hop (c)) == id)
1186 return GNUNET_SYSERR;
1191 * Core handler for notifications of broken paths
1193 * @param cls Closure (unused).
1194 * @param id Peer identity of sending neighbor.
1195 * @param message Message.
1197 * @return GNUNET_OK to keep the connection open,
1198 * GNUNET_SYSERR to close it (signal serious error)
1201 GMC_handle_broken (void* cls,
1202 const struct GNUNET_PeerIdentity* id,
1203 const struct GNUNET_MessageHeader* message)
1205 struct GNUNET_MESH_ConnectionBroken *msg;
1206 struct MeshConnection *c;
1209 LOG (GNUNET_ERROR_TYPE_DEBUG,
1210 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1211 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1212 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1213 GNUNET_i2s (&msg->peer1));
1214 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1215 GNUNET_i2s (&msg->peer2));
1216 c = connection_get (&msg->cid);
1219 GNUNET_break_op (0);
1223 fwd = is_fwd (c, id);
1224 connection_cancel_queues (c, !fwd);
1225 if (GMC_is_terminal (c, fwd))
1227 if (0 < c->pending_messages)
1228 c->destroy = GNUNET_YES;
1234 GMC_send_prebuilt_message (message, c, NULL, fwd);
1235 c->destroy = GNUNET_YES;
1244 * Core handler for tunnel destruction
1246 * @param cls Closure (unused).
1247 * @param peer Peer identity of sending neighbor.
1248 * @param message Message.
1250 * @return GNUNET_OK to keep the connection open,
1251 * GNUNET_SYSERR to close it (signal serious error)
1254 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1255 const struct GNUNET_MessageHeader *message)
1257 struct GNUNET_MESH_ConnectionDestroy *msg;
1258 struct MeshConnection *c;
1261 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1262 LOG (GNUNET_ERROR_TYPE_DEBUG,
1263 "Got a CONNECTION DESTROY message from %s\n",
1265 LOG (GNUNET_ERROR_TYPE_DEBUG,
1266 " for connection %s\n",
1267 GNUNET_h2s (&msg->cid));
1268 c = connection_get (&msg->cid);
1271 /* Probably already got the message from another path,
1272 * destroyed the tunnel and retransmitted to children.
1275 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1279 fwd = is_fwd (c, peer);
1280 if (GNUNET_SYSERR == fwd)
1282 GNUNET_break_op (0);
1285 GMC_send_prebuilt_message (message, c, NULL, fwd);
1286 c->destroy = GNUNET_YES;
1292 * Generic handler for mesh network encrypted traffic.
1294 * @param peer Peer identity this notification is about.
1295 * @param message Encrypted message.
1296 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1298 * @return GNUNET_OK to keep the connection open,
1299 * GNUNET_SYSERR to close it (signal serious error)
1302 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1303 const struct GNUNET_MESH_Encrypted *msg,
1306 struct MeshConnection *c;
1307 struct MeshPeer *neighbor;
1308 struct MeshFlowControl *fc;
1315 size = ntohs (msg->header.size);
1317 sizeof (struct GNUNET_MESH_Encrypted) +
1318 sizeof (struct GNUNET_MessageHeader))
1320 GNUNET_break_op (0);
1323 type = ntohs (msg->header.type);
1324 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1325 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1326 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1328 /* Check connection */
1329 c = connection_get (&msg->cid);
1332 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1333 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1337 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1339 /* Check if origin is as expected */
1340 neighbor = get_hop (c, !fwd);
1341 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1343 GNUNET_break_op (0);
1348 pid = ntohl (msg->pid);
1349 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1351 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1352 LOG (GNUNET_ERROR_TYPE_DEBUG,
1353 "WARNING Received PID %u, (prev %u), ACK %u\n",
1354 pid, fc->last_pid_recv, fc->last_ack_sent);
1357 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1359 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1360 LOG (GNUNET_ERROR_TYPE_DEBUG,
1361 " Pid %u not expected (%u+), dropping!\n",
1362 pid, fc->last_pid_recv + 1);
1365 if (MESH_CONNECTION_SENT == c->state)
1366 connection_change_state (c, MESH_CONNECTION_READY);
1367 connection_reset_timeout (c, fwd);
1368 fc->last_pid_recv = pid;
1370 /* Is this message for us? */
1371 if (GMC_is_terminal (c, fwd))
1373 /* TODO signature verification */
1374 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1375 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1382 fc->last_pid_recv = pid;
1383 GMT_handle_encrypted (c->t, msg, fwd);
1384 GMC_send_ack (c, NULL, fwd);
1388 /* Message not for us: forward to next hop */
1389 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1390 ttl = ntohl (msg->ttl);
1391 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1394 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1395 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1396 GMC_send_ack (c, NULL, fwd);
1399 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1401 GMC_send_prebuilt_message (&msg->header, c, NULL, fwd);
1408 * Core handler for mesh network traffic going orig->dest.
1410 * @param cls Closure (unused).
1411 * @param message Message received.
1412 * @param peer Peer who sent the message.
1414 * @return GNUNET_OK to keep the connection open,
1415 * GNUNET_SYSERR to close it (signal serious error)
1418 GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1419 const struct GNUNET_MessageHeader *message)
1421 return handle_mesh_encrypted (peer,
1422 (struct GNUNET_MESH_Encrypted *)message,
1427 * Core handler for mesh network traffic going dest->orig.
1429 * @param cls Closure (unused).
1430 * @param message Message received.
1431 * @param peer Peer who sent the message.
1433 * @return GNUNET_OK to keep the connection open,
1434 * GNUNET_SYSERR to close it (signal serious error)
1437 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1438 const struct GNUNET_MessageHeader *message)
1440 return handle_mesh_encrypted (peer,
1441 (struct GNUNET_MESH_Encrypted *)message,
1447 * Core handler for mesh network traffic point-to-point acks.
1449 * @param cls closure
1450 * @param message message
1451 * @param peer peer identity this notification is about
1453 * @return GNUNET_OK to keep the connection open,
1454 * GNUNET_SYSERR to close it (signal serious error)
1457 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1458 const struct GNUNET_MessageHeader *message)
1460 struct GNUNET_MESH_ACK *msg;
1461 struct MeshConnection *c;
1462 struct MeshFlowControl *fc;
1467 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1468 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1470 msg = (struct GNUNET_MESH_ACK *) message;
1472 c = connection_get (&msg->cid);
1476 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1481 /* Is this a forward or backward ACK? */
1482 id = GNUNET_PEER_search (peer);
1483 if (GMP_get_short_id (get_next_hop (c)) == id)
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1489 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1491 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1497 GNUNET_break_op (0);
1501 ack = ntohl (msg->ack);
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1503 ack, fc->last_ack_recv);
1504 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1505 fc->last_ack_recv = ack;
1507 /* Cancel polling if the ACK is big enough. */
1508 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1509 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1511 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1512 GNUNET_SCHEDULER_cancel (fc->poll_task);
1513 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1514 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1517 connection_unlock_queue (c, fwd);
1524 * Core handler for mesh network traffic point-to-point ack polls.
1526 * @param cls closure
1527 * @param message message
1528 * @param peer peer identity this notification is about
1530 * @return GNUNET_OK to keep the connection open,
1531 * GNUNET_SYSERR to close it (signal serious error)
1534 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1535 const struct GNUNET_MessageHeader *message)
1537 struct GNUNET_MESH_Poll *msg;
1538 struct MeshConnection *c;
1539 struct MeshFlowControl *fc;
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1548 msg = (struct GNUNET_MESH_Poll *) message;
1550 c = connection_get (&msg->cid);
1554 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1556 GNUNET_break_op (0);
1560 /* Is this a forward or backward ACK?
1561 * Note: a poll should never be needed in a loopback case,
1562 * since there is no possiblility of packet loss there, so
1563 * this way of discerining FWD/BCK should not be a problem.
1565 id = GNUNET_PEER_search (peer);
1566 if (GMP_get_short_id (get_next_hop (c)) == id)
1568 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1571 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1573 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1578 GNUNET_break_op (0);
1582 pid = ntohl (msg->pid);
1583 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1584 pid, fc->last_pid_recv);
1585 fc->last_pid_recv = pid;
1586 fwd = fc == &c->fwd_fc;
1587 GMC_send_ack (c, NULL, fwd);
1594 * Core handler for mesh keepalives.
1596 * @param cls closure
1597 * @param message message
1598 * @param peer peer identity this notification is about
1599 * @return GNUNET_OK to keep the connection open,
1600 * GNUNET_SYSERR to close it (signal serious error)
1602 * TODO: Check who we got this from, to validate route.
1605 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1606 const struct GNUNET_MessageHeader *message)
1608 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1609 struct MeshConnection *c;
1610 struct MeshPeer *neighbor;
1613 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1614 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1617 c = connection_get (&msg->cid);
1620 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1625 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1626 GNUNET_YES : GNUNET_NO;
1628 /* Check if origin is as expected */
1629 neighbor = get_hop (c, fwd);
1630 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1632 GNUNET_break_op (0);
1636 connection_change_state (c, MESH_CONNECTION_READY);
1637 connection_reset_timeout (c, fwd);
1639 if (GMC_is_terminal (c, fwd))
1642 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1643 GMC_send_prebuilt_message (message, c, NULL, fwd);
1650 * Send an ACK on the appropriate connection/channel, depending on
1651 * the direction and the position of the peer.
1653 * @param c Which connection to send the hop-by-hop ACK.
1654 * @param ch Channel, if any.
1655 * @param fwd Is this a fwd ACK? (will go dest->root)
1658 GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1660 unsigned int buffer;
1662 LOG (GNUNET_ERROR_TYPE_DEBUG,
1663 "send ack %s on %p %p\n",
1664 fwd ? "FWD" : "BCK", c, ch);
1666 /* Get available bufffer space */
1667 if (NULL == c || GMC_is_terminal (c, fwd))
1669 struct MeshTunnel3 *t;
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n");
1671 t = (NULL == c) ? GMCH_get_tunnel (ch) : GMC_get_tunnel (c);
1672 buffer = GMT_get_buffer (t, fwd);
1676 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1677 buffer = GMC_get_buffer (c, fwd);
1679 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1681 /* Send available buffer space */
1682 if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1683 (NULL != c && GMC_is_origin (c, fwd)) )
1685 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1688 GNUNET_assert (NULL != ch);
1689 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1690 GMCH_send_data_ack (ch, fwd);
1695 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
1696 GNUNET_assert (NULL != ch);
1697 GMT_send_acks (GMCH_get_tunnel (ch), buffer, fwd);
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1702 connection_send_ack (c, buffer, fwd);
1708 * Initialize the connections subsystem
1710 * @param c Configuration handle.
1713 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1715 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1717 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1720 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1721 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1722 GNUNET_SCHEDULER_shutdown ();
1727 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1730 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1731 "MESH", "MAX_CONNECTIONS", "MISSING");
1732 GNUNET_SCHEDULER_shutdown ();
1737 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1738 &refresh_connection_time))
1740 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1741 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1742 GNUNET_SCHEDULER_shutdown ();
1745 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1749 * Shut down the connections subsystem.
1754 GNUNET_CONTAINER_multihashmap_destroy (connections);
1758 struct MeshConnection *
1759 GMC_new (const struct GNUNET_HashCode *cid,
1760 struct MeshTunnel3 *t,
1761 struct MeshPeerPath *p,
1762 unsigned int own_pos)
1764 struct MeshConnection *c;
1766 c = GNUNET_new (struct MeshConnection);
1768 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1769 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1770 fc_init (&c->fwd_fc);
1771 fc_init (&c->bck_fc);
1776 if (own_pos > p->length - 1)
1782 c->own_pos = own_pos;
1787 c->fwd_maintenance_task =
1788 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1789 &connection_fwd_keepalive, c);
1791 register_neighbors (c);
1797 GMC_destroy (struct MeshConnection *c)
1802 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n",
1803 GNUNET_h2s (&c->id));
1805 /* Cancel all traffic */
1806 connection_cancel_queues (c, GNUNET_YES);
1807 connection_cancel_queues (c, GNUNET_NO);
1809 /* Cancel maintainance task (keepalive/timeout) */
1810 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1811 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1812 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1813 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1815 /* Unregister from neighbors */
1816 unregister_neighbors (c);
1819 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1820 GMT_remove_connection (c->t, c);
1825 * Get the connection ID.
1827 * @param c Connection to get the ID from.
1829 * @return ID of the connection.
1831 const struct GNUNET_HashCode *
1832 GMC_get_id (const struct MeshConnection *c)
1839 * Get the connection path.
1841 * @param c Connection to get the path from.
1843 * @return path used by the connection.
1845 const struct MeshPeerPath *
1846 GMC_get_path (const struct MeshConnection *c)
1853 * Get the connection state.
1855 * @param c Connection to get the state from.
1857 * @return state of the connection.
1859 enum MeshConnectionState
1860 GMC_get_state (const struct MeshConnection *c)
1866 * Get the connection tunnel.
1868 * @param c Connection to get the tunnel from.
1870 * @return tunnel of the connection.
1872 struct MeshTunnel3 *
1873 GMC_get_tunnel (const struct MeshConnection *c)
1880 * Get free buffer space in a connection.
1882 * @param c Connection.
1883 * @param fwd Is query about FWD traffic?
1885 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1888 GMC_get_buffer (struct MeshConnection *c, int fwd)
1890 struct MeshFlowControl *fc;
1892 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1894 return (fc->queue_max - fc->queue_n);
1898 * Get how many messages have we allowed to send to us from a direction..
1900 * @param c Connection.
1901 * @param fwd Are we asking about traffic from FWD (BCK messages)?
1903 * @return last_ack_sent - last_pid_recv
1906 GMC_get_allowed (struct MeshConnection *c, int fwd)
1908 struct MeshFlowControl *fc;
1910 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1911 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
1915 return (fc->last_ack_sent - fc->last_pid_recv);
1919 * Get messages queued in a connection.
1921 * @param c Connection.
1922 * @param fwd Is query about FWD traffic?
1924 * @return Number of messages queued.
1927 GMC_get_qn (struct MeshConnection *c, int fwd)
1929 struct MeshFlowControl *fc;
1931 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1938 * Allow the connection to advertise a buffer of the given size.
1940 * The connection will send an @c fwd ACK message (so: in direction !fwd)
1941 * allowing up to last_pid_recv + buffer.
1943 * @param c Connection.
1944 * @param buffer How many more messages the connection can accept.
1945 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
1948 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
1950 connection_send_ack (c, buffer, fwd);
1955 * Send a notification that a connection is broken.
1957 * @param c Connection that is broken.
1958 * @param id1 Peer that has disconnected.
1959 * @param id2 Peer that has disconnected.
1960 * @param fwd Direction towards which to send it.
1963 send_broken (struct MeshConnection *c,
1964 const struct GNUNET_PeerIdentity *id1,
1965 const struct GNUNET_PeerIdentity *id2,
1968 struct GNUNET_MESH_ConnectionBroken msg;
1970 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
1971 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
1975 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
1979 * Notify other peers on a connection of a broken link. Mark connections
1980 * to destroy after all traffic has been sent.
1982 * @param c Connection on which there has been a disconnection.
1983 * @param peer Peer that disconnected.
1986 GMC_notify_broken (struct MeshConnection *c,
1987 struct MeshPeer *peer)
1991 fwd = peer == get_prev_hop (c);
1993 connection_cancel_queues (c, !fwd);
1994 if (GMC_is_terminal (c, fwd))
1996 /* Local shutdown, no one to notify about this. */
2001 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2003 /* Connection will have at least one pending message
2004 * (the one we just scheduled), so no point in checking whether to
2005 * destroy immediately. */
2006 c->destroy = GNUNET_YES;
2013 * Is this peer the first one on the connection?
2015 * @param c Connection.
2016 * @param fwd Is this about fwd traffic?
2018 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2021 GMC_is_origin (struct MeshConnection *c, int fwd)
2023 if (!fwd && c->path->length - 1 == c->own_pos )
2025 if (fwd && 0 == c->own_pos)
2032 * Is this peer the last one on the connection?
2034 * @param c Connection.
2035 * @param fwd Is this about fwd traffic?
2036 * Note that the ROOT is the terminal for BCK traffic!
2038 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2041 GMC_is_terminal (struct MeshConnection *c, int fwd)
2043 return GMC_is_origin (c, !fwd);
2048 * See if we are allowed to send by the next hop in the given direction.
2050 * @param c Connection.
2051 * @param fwd Is this about fwd traffic?
2053 * @return GNUNET_YES in case it's OK.
2056 GMC_is_sendable (struct MeshConnection *c, int fwd)
2058 struct MeshFlowControl *fc;
2060 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2061 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2067 * Sends an already built message on a connection, properly registering
2068 * all used resources.
2070 * @param message Message to send. Function makes a copy of it.
2071 * If message is not hop-by-hop, decrements TTL of copy.
2072 * @param c Connection on which this message is transmitted.
2073 * @param ch Channel on which this message is transmitted, or NULL.
2074 * @param fwd Is this a fwd message?
2077 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2078 struct MeshConnection *c,
2079 struct MeshChannel *ch,
2082 struct MeshFlowControl *fc;
2088 size = ntohs (message->size);
2089 data = GNUNET_malloc (size);
2090 memcpy (data, message, size);
2091 type = ntohs (message->type);
2092 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2093 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
2095 droppable = GNUNET_YES;
2098 struct GNUNET_MESH_Encrypted *emsg;
2099 struct GNUNET_MESH_ACK *amsg;
2100 struct GNUNET_MESH_Poll *pmsg;
2101 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2102 struct GNUNET_MESH_ConnectionBroken *bmsg;
2105 case GNUNET_MESSAGE_TYPE_MESH_FWD:
2106 case GNUNET_MESSAGE_TYPE_MESH_BCK:
2107 emsg = (struct GNUNET_MESH_Encrypted *) data;
2108 ttl = ntohl (emsg->ttl);
2111 GNUNET_break_op (0);
2115 emsg->ttl = htonl (ttl - 1);
2116 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2117 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
2120 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2121 amsg = (struct GNUNET_MESH_ACK *) data;
2123 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2124 droppable = GNUNET_NO;
2127 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2128 pmsg = (struct GNUNET_MESH_Poll *) data;
2130 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2131 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2132 droppable = GNUNET_NO;
2135 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2136 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2141 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2142 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2147 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2148 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2155 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2156 if (fc->queue_n >= fc->queue_max && droppable)
2158 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2161 LOG (GNUNET_ERROR_TYPE_DEBUG,
2162 "queue full: %u/%u\n",
2163 fc->queue_n, fc->queue_max);
2164 return; /* Drop this message */
2167 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
2168 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
2169 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2170 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2172 GMC_start_poll (c, fwd);
2175 c->pending_messages++;
2177 GMP_queue_add (get_hop (c, fwd), data, type, size, c, ch, fwd,
2178 &message_sent, NULL);
2183 * Sends a CREATE CONNECTION message for a path to a peer.
2184 * Changes the connection and tunnel states if necessary.
2186 * @param connection Connection to create.
2189 GMC_send_create (struct MeshConnection *connection)
2191 enum MeshTunnel3State state;
2194 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2195 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2196 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2197 GMP_queue_add (get_next_hop (connection), NULL,
2198 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2199 size, connection, NULL,
2200 GNUNET_YES, &message_sent, NULL);
2201 state = GMT_get_state (connection->t);
2202 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2203 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2204 if (MESH_CONNECTION_NEW == connection->state)
2205 connection_change_state (connection, MESH_CONNECTION_SENT);
2210 * Send a message to all peers in this connection that the connection
2211 * is no longer valid.
2213 * If some peer should not receive the message, it should be zero'ed out
2214 * before calling this function.
2216 * @param c The connection whose peers to notify.
2219 GMC_send_destroy (struct MeshConnection *c)
2221 struct GNUNET_MESH_ConnectionDestroy msg;
2223 if (GNUNET_YES == c->destroy)
2226 msg.header.size = htons (sizeof (msg));
2227 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2229 LOG (GNUNET_ERROR_TYPE_DEBUG,
2230 " sending connection destroy for connection %s\n",
2231 GNUNET_h2s (&c->id));
2233 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2234 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2235 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2236 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2237 c->destroy = GNUNET_YES;
2242 * @brief Start a polling timer for the connection.
2244 * When a neighbor does not accept more traffic on the connection it could be
2245 * caused by a simple congestion or by a lost ACK. Polling enables to check
2246 * for the lastest ACK status for a connection.
2248 * @param c Connection.
2249 * @param fwd Should we poll in the FWD direction?
2252 GMC_start_poll (struct MeshConnection *c, int fwd)
2254 struct MeshFlowControl *fc;
2256 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2257 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2261 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2268 * @brief Stop polling a connection for ACKs.
2270 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2272 * @param c Connection.
2273 * @param fwd Should we stop the poll in the FWD direction?
2276 GMC_stop_poll (struct MeshConnection *c, int fwd)
2278 struct MeshFlowControl *fc;
2280 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2281 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2283 GNUNET_SCHEDULER_cancel (fc->poll_task);
2284 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;