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 "gnunet-service-mesh_connection.h"
33 #include "gnunet-service-mesh_peer.h"
34 #include "gnunet-service-mesh_tunnel.h"
35 #include "gnunet-service-mesh_channel.h"
36 #include "mesh_protocol_enc.h"
37 #include "mesh_path.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 * Connections known, indexed by cid (MeshConnection).
216 static struct GNUNET_CONTAINER_MultiHashMap *connections;
219 * How many connections are we willing to maintain.
220 * Local connections are always allowed, even if there are more connections than max.
222 static unsigned long long max_connections;
225 * How many messages *in total* are we willing to queue, divide by number of
226 * connections to get connection queue size.
228 static unsigned long long max_msgs_queue;
231 * How often to send path keepalives. Paths timeout after 4 missed.
233 static struct GNUNET_TIME_Relative refresh_connection_time;
236 /******************************************************************************/
237 /******************************** STATIC ***********************************/
238 /******************************************************************************/
240 #if 0 // avoid compiler warning for unused static function
242 fc_debug (struct MeshFlowControl *fc)
244 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
245 fc->last_pid_recv, fc->last_ack_sent);
246 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
247 fc->last_pid_sent, fc->last_ack_recv);
248 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
249 fc->queue_n, fc->queue_max);
253 connection_debug (struct MeshConnection *c)
257 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
260 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
261 peer2s (c->t->peer), GNUNET_h2s (&c->id));
262 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
263 c->state, c->pending_messages);
264 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
265 fc_debug (&c->fwd_fc);
266 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
267 fc_debug (&c->bck_fc);
272 * Get string description for tunnel state.
274 * @param s Tunnel state.
276 * @return String representation.
279 GMC_state2s (enum MeshConnectionState s)
283 case MESH_CONNECTION_NEW:
284 return "MESH_CONNECTION_NEW";
285 case MESH_CONNECTION_SENT:
286 return "MESH_CONNECTION_SENT";
287 case MESH_CONNECTION_ACK:
288 return "MESH_CONNECTION_ACK";
289 case MESH_CONNECTION_READY:
290 return "MESH_CONNECTION_READY";
292 return "MESH_CONNECTION_STATE_ERROR";
298 * Initialize a Flow Control structure to the initial state.
300 * @param fc Flow Control structure to initialize.
303 fc_init (struct MeshFlowControl *fc)
306 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
307 fc->last_pid_recv = (uint32_t) -1;
308 fc->last_ack_sent = (uint32_t) 0;
309 fc->last_ack_recv = (uint32_t) 0;
310 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
311 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
313 fc->queue_max = (max_msgs_queue / max_connections) + 1;
320 * @param cid Connection ID.
322 static struct MeshConnection *
323 connection_get (const struct GNUNET_HashCode *cid)
325 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
330 * Send an ACK informing the predecessor about the available buffer space.
332 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
333 * the ACK itself goes "back" (dest->root).
335 * @param c Connection on which to send the ACK.
336 * @param buffer How much space free to advertise?
337 * @param fwd Is this FWD ACK? (Going dest->owner)
340 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
342 struct MeshFlowControl *next_fc;
343 struct MeshFlowControl *prev_fc;
344 struct GNUNET_MESH_ACK msg;
348 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
349 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
351 LOG (GNUNET_ERROR_TYPE_DEBUG,
352 "connection send %s ack on %s\n",
353 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
355 /* Check if we need to transmit the ACK */
356 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
358 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
359 LOG (GNUNET_ERROR_TYPE_DEBUG,
360 " last pid recv: %u, last ack sent: %u\n",
361 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
365 /* Ok, ACK might be necessary, what PID to ACK? */
366 delta = next_fc->queue_max - next_fc->queue_n;
367 ack = prev_fc->last_pid_recv + delta;
368 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
369 LOG (GNUNET_ERROR_TYPE_DEBUG,
370 " last pid %u, last ack %u, qmax %u, q %u\n",
371 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
372 next_fc->queue_max, next_fc->queue_n);
373 if (ack == prev_fc->last_ack_sent)
375 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
379 prev_fc->last_ack_sent = ack;
381 /* Build ACK message and send on connection */
382 msg.header.size = htons (sizeof (msg));
383 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
384 msg.ack = htonl (ack);
387 GMC_send_prebuilt_message (&msg.header, c, NULL, !fwd);
392 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
393 * or a first CONNECTION_ACK directed to us.
395 * @param connection Connection to confirm.
396 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
399 send_connection_ack (struct MeshConnection *connection, int fwd)
401 struct MeshTunnel3 *t;
404 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
406 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
407 sizeof (struct GNUNET_MESH_ConnectionACK),
408 connection, NULL, fwd,
409 &message_sent, sizeof (struct GNUNET_MESH_ConnectionACK));
410 if (MESH_TUNNEL_NEW == t->state)
411 GMT_change_state (t, MESH_TUNNEL_WAITING);
412 if (MESH_CONNECTION_READY != connection->state)
413 GMC_change_state (connection, MESH_CONNECTION_SENT);
418 * Sends a CREATE CONNECTION message for a path to a peer.
419 * Changes the connection and tunnel states if necessary.
421 * @param connection Connection to create.
424 send_connection_create (struct MeshConnection *connection)
426 struct MeshTunnel3 *t;
429 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
431 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
432 sizeof (struct GNUNET_MESH_ConnectionCreate) +
433 (connection->path->length *
434 sizeof (struct GNUNET_PeerIdentity)),
439 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
440 tunnel_change_state (t, MESH_TUNNEL_WAITING);
441 if (MESH_CONNECTION_NEW == connection->state)
442 connection_change_state (connection, MESH_CONNECTION_SENT);
447 connection_change_state (struct MeshConnection* c,
448 enum MeshConnectionState state)
450 LOG (GNUNET_ERROR_TYPE_DEBUG,
451 "Connection %s state was %s\n",
452 GNUNET_h2s (&c->id), GMC_state2s (c->state));
453 LOG (GNUNET_ERROR_TYPE_DEBUG,
454 "Connection %s state is now %s\n",
455 GNUNET_h2s (&c->id), GMC_state2s (state));
462 * Send keepalive packets for a connection.
464 * @param c Connection to keep alive..
465 * @param fwd Is this a FWD keepalive? (owner -> dest).
468 connection_keepalive (struct MeshConnection *c, int fwd)
470 struct GNUNET_MESH_ConnectionKeepAlive *msg;
471 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
475 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
476 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
478 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 "sending %s keepalive for connection %s[%d]\n",
484 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
485 msg->header.size = htons (size);
486 msg->header.type = htons (type);
489 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
494 * Send CONNECTION_{CREATE/ACK} packets for a connection.
496 * @param c Connection for which to send the message.
497 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
500 connection_recreate (struct MeshConnection *c, int fwd)
502 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
504 send_connection_create (c);
506 send_connection_ack (c, GNUNET_NO);
511 * Generic connection timer management.
512 * Depending on the role of the peer in the connection will send the
513 * appropriate message (build or keepalive)
515 * @param c Conncetion to maintain.
519 connection_maintain (struct MeshConnection *c, int fwd)
521 if (MESH_TUNNEL_SEARCHING == c->t->state)
523 /* TODO DHT GET with RO_BART */
528 case MESH_CONNECTION_NEW:
530 case MESH_CONNECTION_SENT:
531 connection_recreate (c, fwd);
533 case MESH_CONNECTION_READY:
534 connection_keepalive (c, fwd);
543 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
545 struct MeshConnection *c = cls;
547 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
548 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
551 connection_maintain (c, GNUNET_YES);
552 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
553 &connection_fwd_keepalive,
559 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
561 struct MeshConnection *c = cls;
563 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
564 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
567 connection_maintain (c, GNUNET_NO);
568 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
569 &connection_bck_keepalive,
576 * Get the previous hop in a connection
578 * @param c Connection.
580 * @return Previous peer in the connection.
582 static struct MeshPeer *
583 connection_get_prev_hop (struct MeshConnection *c)
587 if (0 == c->own_pos || c->path->length < 2)
588 id = c->path->peers[0];
590 id = c->path->peers[c->own_pos - 1];
592 return peer_get_short (id);
597 * Get the next hop in a connection
599 * @param c Connection.
601 * @return Next peer in the connection.
603 static struct MeshPeer *
604 connection_get_next_hop (struct MeshConnection *c)
608 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
609 id = c->path->peers[c->path->length - 1];
611 id = c->path->peers[c->own_pos + 1];
613 return peer_get_short (id);
618 * Get the hop in a connection.
620 * @param c Connection.
621 * @param fwd Next hop?
623 * @return Next peer in the connection.
625 static struct MeshPeer *
626 connection_get_hop (struct MeshConnection *c, int fwd)
629 return connection_get_next_hop (c);
630 return connection_get_prev_hop (c);
635 * Get the first transmittable message for a connection.
637 * @param c Connection.
638 * @param fwd Is this FWD?
640 * @return First transmittable message.
642 static struct MeshPeerQueue *
643 connection_get_first_message (struct MeshConnection *c, int fwd)
645 struct MeshPeerQueue *q;
648 p = connection_get_hop (c, fwd);
650 for (q = p->queue_head; NULL != q; q = q->next)
654 if (queue_is_sendable (q))
663 * @brief Re-initiate traffic on this connection if necessary.
665 * Check if there is traffic queued towards this peer
666 * and the core transmit handle is NULL (traffic was stalled).
667 * If so, call core tmt rdy.
669 * @param c Connection on which initiate traffic.
670 * @param fwd Is this about fwd traffic?
673 connection_unlock_queue (struct MeshConnection *c, int fwd)
675 struct MeshPeer *peer;
676 struct MeshPeerQueue *q;
679 LOG (GNUNET_ERROR_TYPE_DEBUG,
680 "connection_unlock_queue %s on %s\n",
681 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
683 if (GMC_is_terminal (c, fwd))
685 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
689 peer = connection_get_hop (c, fwd);
691 if (NULL != peer->core_transmit)
693 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
694 return; /* Already unlocked */
697 q = connection_get_first_message (c, fwd);
700 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
701 return; /* Nothing to transmit */
705 peer->core_transmit =
706 GNUNET_CORE_notify_transmit_ready (core_handle,
709 GNUNET_TIME_UNIT_FOREVER_REL,
710 GNUNET_PEER_resolve2 (peer->id),
718 * Cancel all transmissions that belong to a certain connection.
720 * @param c Connection which to cancel.
721 * @param fwd Cancel fwd traffic?
724 connection_cancel_queues (struct MeshConnection *c, int fwd)
727 struct MeshFlowControl *fc;
728 struct MeshPeer *peer;
736 peer = connection_get_hop (c, fwd);
737 GMP_queue_cancel (peer, c);
739 fc = fwd ? &c->fwd_fc : &c->bck_fc;
740 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
742 GNUNET_SCHEDULER_cancel (fc->poll_task);
743 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
749 * Function called if a connection has been stalled for a while,
750 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
752 * @param cls Closure (poll ctx).
753 * @param tc TaskContext.
756 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
758 struct MeshFlowControl *fc = cls;
759 struct GNUNET_MESH_Poll msg;
760 struct MeshConnection *c;
762 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
763 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
769 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
770 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
771 GNUNET_h2s (&c->id));
772 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
773 fc == &c->fwd_fc ? "FWD" : "BCK");
775 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
776 msg.header.size = htons (sizeof (msg));
777 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
778 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
779 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
780 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
781 &connection_poll, fc);
786 * Timeout function due to lack of keepalive/traffic from the owner.
787 * Destroys connection if called.
789 * @param cls Closure (connection to destroy).
790 * @param tc TaskContext.
793 connection_fwd_timeout (void *cls,
794 const struct GNUNET_SCHEDULER_TaskContext *tc)
796 struct MeshConnection *c = cls;
798 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
799 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
801 LOG (GNUNET_ERROR_TYPE_DEBUG,
802 "Connection %s[%X] FWD timed out. Destroying.\n",
806 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
814 * Timeout function due to lack of keepalive/traffic from the destination.
815 * Destroys connection if called.
817 * @param cls Closure (connection to destroy).
818 * @param tc TaskContext
821 connection_bck_timeout (void *cls,
822 const struct GNUNET_SCHEDULER_TaskContext *tc)
824 struct MeshConnection *c = cls;
826 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
827 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
830 LOG (GNUNET_ERROR_TYPE_DEBUG,
831 "Connection %s[%X] FWD timed out. Destroying.\n",
832 GMT_2s (c->t), c->id);
834 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
842 * Resets the connection timeout task, some other message has done the
844 * - For the first peer on the direction this means to send
845 * a keepalive or a path confirmation message (either create or ACK).
846 * - For all other peers, this means to destroy the connection,
847 * due to lack of activity.
848 * Starts the tiemout if no timeout was running (connection just created).
850 * @param c Connection whose timeout to reset.
851 * @param fwd Is this forward?
853 * TODO use heap to improve efficiency of scheduler.
856 connection_reset_timeout (struct MeshConnection *c, int fwd)
858 GNUNET_SCHEDULER_TaskIdentifier *ti;
859 GNUNET_SCHEDULER_Task f;
861 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
863 if (GNUNET_SCHEDULER_NO_TASK != *ti)
864 GNUNET_SCHEDULER_cancel (*ti);
866 if (GMC_is_origin (c, fwd)) /* Endpoint */
868 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
869 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
873 struct GNUNET_TIME_Relative delay;
875 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
876 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
877 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
883 * Add the connection to the list of both neighbors.
885 * @param c Connection.
888 register_neighbors (struct MeshConnection *c)
890 struct MeshPeer *peer;
892 peer = connection_get_next_hop (c);
893 if (GNUNET_NO == GMP_is_neighbor (peer))
898 GMP_add_connection (peer, c);
899 peer = connection_get_prev_hop (c);
900 if (GNUNET_NO == GMP_is_neighbor (peer))
905 GMP_add_connection (peer, c);
910 * Remove the connection from the list of both neighbors.
912 * @param c Connection.
915 unregister_neighbors (struct MeshConnection *c)
917 struct MeshPeer *peer;
919 peer = connection_get_next_hop (c);
920 GMP_remove_connection (peer, c);
922 peer = connection_get_prev_hop (c);
923 GMP_remove_connection (peer, c);
929 * Callback called when a queued message is sent.
931 * Calculates the average time
933 * @param cls Closure.
934 * @param c Connection this message was on.
935 * @param wait Time spent waiting for core (only the time for THIS message)
938 message_sent (void *cls,
939 struct MeshConnection *c,
940 struct GNUNET_TIME_Relative wait)
942 struct MeshConnectionPerformance *p;
943 size_t size = (size_t) cls;
947 return; /* Only endpoints are interested in this. */
950 usecsperbyte = ((double) wait.rel_value_us) / size;
951 if (p->size == AVG_MSGS)
953 /* Array is full. Substract oldest value, add new one and store. */
954 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
955 p->usecsperbyte[p->idx] = usecsperbyte;
956 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
960 /* Array not yet full. Add current value to avg and store. */
961 p->usecsperbyte[p->idx] = usecsperbyte;
963 p->avg += p->usecsperbyte[p->idx];
967 p->idx = (p->idx + 1) % AVG_MSGS;
971 /******************************************************************************/
972 /******************************** API ***********************************/
973 /******************************************************************************/
976 * Core handler for connection creation.
978 * @param cls Closure (unused).
979 * @param peer Sender (neighbor).
980 * @param message Message.
982 * @return GNUNET_OK to keep the connection open,
983 * GNUNET_SYSERR to close it (signal serious error)
986 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
987 const struct GNUNET_MessageHeader *message)
989 struct GNUNET_MESH_ConnectionCreate *msg;
990 struct GNUNET_PeerIdentity *id;
991 struct GNUNET_HashCode *cid;
992 struct MeshPeerPath *path;
993 struct MeshPeer *dest_peer;
994 struct MeshPeer *orig_peer;
995 struct MeshConnection *c;
996 unsigned int own_pos;
1000 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1004 size = ntohs (message->size);
1005 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1007 GNUNET_break_op (0);
1011 /* Calculate hops */
1012 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1013 if (size % sizeof (struct GNUNET_PeerIdentity))
1015 GNUNET_break_op (0);
1018 size /= sizeof (struct GNUNET_PeerIdentity);
1021 GNUNET_break_op (0);
1024 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1026 /* Get parameters */
1027 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1029 id = (struct GNUNET_PeerIdentity *) &msg[1];
1030 LOG (GNUNET_ERROR_TYPE_DEBUG,
1031 " connection %s (%s).\n",
1032 GNUNET_h2s (cid), GNUNET_i2s (id));
1034 /* Create connection */
1035 c = connection_get (cid);
1039 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1040 path = path_new (size);
1042 for (i = 0; i < size; i++)
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1045 GNUNET_i2s (&id[i]));
1046 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1047 if (path->peers[i] == my_short_id)
1050 if (own_pos == 0 && path->peers[own_pos] != myid)
1052 /* create path: self not found in path through self */
1053 GNUNET_break_op (0);
1054 path_destroy (path);
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1059 path_add_to_peers (path, GNUNET_NO);
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1061 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1064 connection_reset_timeout (c, GNUNET_YES);
1070 if (MESH_CONNECTION_NEW == c->state)
1071 connection_change_state (c, MESH_CONNECTION_SENT);
1073 /* Remember peers */
1074 dest_peer = peer_get (&id[size - 1]);
1075 orig_peer = peer_get (&id[0]);
1077 /* Is it a connection to us? */
1078 if (c->own_pos == size - 1)
1080 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1081 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
1083 if (NULL == orig_peer->tunnel)
1085 orig_peer->tunnel = tunnel_new ();
1086 orig_peer->tunnel->peer = orig_peer;
1088 GMT_add_connection (orig_peer->tunnel, c);
1089 if (MESH_TUNNEL_NEW == GMT_get_state (c->t))
1090 GMT_change_state (c->t, MESH_TUNNEL_WAITING);
1092 send_connection_ack (c, GNUNET_NO);
1093 if (MESH_CONNECTION_SENT == c->state)
1094 connection_change_state (c, MESH_CONNECTION_ACK);
1096 /* Keep tunnel alive in direction dest->owner*/
1097 connection_reset_timeout (c, GNUNET_NO);
1101 /* It's for somebody else! Retransmit. */
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1103 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1104 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
1105 GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES);
1112 * Core handler for path confirmations.
1114 * @param cls closure
1115 * @param message message
1116 * @param peer peer identity this notification is about
1118 * @return GNUNET_OK to keep the connection open,
1119 * GNUNET_SYSERR to close it (signal serious error)
1122 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1123 const struct GNUNET_MessageHeader *message)
1125 struct GNUNET_MESH_ConnectionACK *msg;
1126 struct MeshConnection *c;
1127 struct MeshPeerPath *p;
1128 struct MeshPeer *pi;
1131 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1132 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1133 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1135 GNUNET_h2s (&msg->cid));
1136 c = connection_get (&msg->cid);
1139 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1148 pi = peer_get (peer);
1149 if (connection_get_next_hop (c) == pi)
1151 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1153 if (MESH_CONNECTION_SENT == c->state)
1154 connection_change_state (c, MESH_CONNECTION_ACK);
1156 else if (connection_get_prev_hop (c) == pi)
1158 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1160 connection_change_state (c, MESH_CONNECTION_READY);
1164 GNUNET_break_op (0);
1167 connection_reset_timeout (c, fwd);
1169 /* Add path to peers? */
1173 path_add_to_peers (p, GNUNET_YES);
1180 /* Message for us as creator? */
1181 if (GMC_is_origin (c, GNUNET_YES))
1183 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1184 connection_change_state (c, MESH_CONNECTION_READY);
1185 GMT_change_state (c->t, MESH_TUNNEL_READY);
1186 send_connection_ack (c, GNUNET_YES);
1187 GMT_send_queued_data (c->t, GNUNET_YES);
1191 /* Message for us as destination? */
1192 if (GMC_is_terminal (c, GNUNET_YES))
1194 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1195 GMC_change_state (c, MESH_CONNECTION_READY);
1196 GMT_change_state (c->t, MESH_TUNNEL_READY);
1197 GMT_send_queued_data (c->t, GNUNET_NO);
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1202 GMC_send_prebuilt_message (message, c, NULL, fwd);
1208 * Core handler for notifications of broken paths
1210 * @param cls Closure (unused).
1211 * @param peer Peer identity of sending neighbor.
1212 * @param message Message.
1214 * @return GNUNET_OK to keep the connection open,
1215 * GNUNET_SYSERR to close it (signal serious error)
1218 GMC_handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
1219 const struct GNUNET_MessageHeader *message)
1221 struct GNUNET_MESH_ConnectionBroken *msg;
1222 struct MeshConnection *c;
1224 LOG (GNUNET_ERROR_TYPE_DEBUG,
1225 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
1226 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1227 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1228 GNUNET_i2s (&msg->peer1));
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1230 GNUNET_i2s (&msg->peer2));
1231 c = connection_get (&msg->cid);
1234 GNUNET_break_op (0);
1237 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
1238 GNUNET_PEER_search (&msg->peer2));
1245 * Core handler for tunnel destruction
1247 * @param cls Closure (unused).
1248 * @param peer Peer identity of sending neighbor.
1249 * @param message Message.
1251 * @return GNUNET_OK to keep the connection open,
1252 * GNUNET_SYSERR to close it (signal serious error)
1255 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1256 const struct GNUNET_MessageHeader *message)
1258 struct GNUNET_MESH_ConnectionDestroy *msg;
1259 struct MeshConnection *c;
1263 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1264 LOG (GNUNET_ERROR_TYPE_DEBUG,
1265 "Got a CONNECTION DESTROY message from %s\n",
1267 LOG (GNUNET_ERROR_TYPE_DEBUG,
1268 " for connection %s\n",
1269 GNUNET_h2s (&msg->cid));
1270 c = connection_get (&msg->cid);
1273 /* Probably already got the message from another path,
1274 * destroyed the tunnel and retransmitted to children.
1277 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1281 id = GNUNET_PEER_search (peer);
1282 if (id == GMP_get_short_id (connection_get_prev_hop (c)))
1284 else if (id == GMP_get_short_id (connection_get_next_hop (c)))
1288 GNUNET_break_op (0);
1291 GMC_send_prebuilt_message (message, c, NULL, fwd);
1292 c->destroy = GNUNET_YES;
1298 * Generic handler for mesh network encrypted traffic.
1300 * @param peer Peer identity this notification is about.
1301 * @param message Encrypted message.
1302 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1304 * @return GNUNET_OK to keep the connection open,
1305 * GNUNET_SYSERR to close it (signal serious error)
1308 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1309 const struct GNUNET_MESH_Encrypted *msg,
1312 struct MeshConnection *c;
1313 struct MeshTunnel3 *t;
1314 struct MeshPeer *neighbor;
1315 struct MeshFlowControl *fc;
1322 size = ntohs (msg->header.size);
1324 sizeof (struct GNUNET_MESH_Encrypted) +
1325 sizeof (struct GNUNET_MessageHeader))
1327 GNUNET_break_op (0);
1330 type = ntohs (msg->header.type);
1331 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1333 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1335 /* Check connection */
1336 c = connection_get (&msg->cid);
1339 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1340 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1344 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1346 /* Check if origin is as expected */
1347 neighbor = connection_get_hop (c, !fwd);
1348 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1350 GNUNET_break_op (0);
1355 pid = ntohl (msg->pid);
1356 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1358 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1359 LOG (GNUNET_ERROR_TYPE_DEBUG,
1360 "WARNING Received PID %u, (prev %u), ACK %u\n",
1361 pid, fc->last_pid_recv, fc->last_ack_sent);
1364 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1366 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1367 LOG (GNUNET_ERROR_TYPE_DEBUG,
1368 " Pid %u not expected (%u+), dropping!\n",
1369 pid, fc->last_pid_recv + 1);
1372 if (MESH_CONNECTION_SENT == c->state)
1373 connection_change_state (c, MESH_CONNECTION_READY);
1374 connection_reset_timeout (c, fwd);
1375 fc->last_pid_recv = pid;
1377 /* Is this message for us? */
1378 if (GMC_is_terminal (c, fwd))
1380 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
1382 struct GNUNET_MessageHeader *msgh;
1385 /* TODO signature verification */
1386 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1387 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1389 fc->last_pid_recv = pid;
1390 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
1394 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1395 handle_decrypted (t, msgh, fwd);
1396 off += ntohs (msgh->size);
1398 send_ack (c, NULL, fwd);
1402 /* Message not for us: forward to next hop */
1403 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1404 ttl = ntohl (msg->ttl);
1405 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1408 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1409 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1410 send_ack (c, NULL, fwd);
1413 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1415 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1422 * Core handler for mesh network traffic going orig->dest.
1424 * @param cls Closure (unused).
1425 * @param message Message received.
1426 * @param peer Peer who sent the message.
1428 * @return GNUNET_OK to keep the connection open,
1429 * GNUNET_SYSERR to close it (signal serious error)
1432 GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1433 const struct GNUNET_MessageHeader *message)
1435 return handle_mesh_encrypted (peer,
1436 (struct GNUNET_MESH_Encrypted *)message,
1441 * Core handler for mesh network traffic going dest->orig.
1443 * @param cls Closure (unused).
1444 * @param message Message received.
1445 * @param peer Peer who sent the message.
1447 * @return GNUNET_OK to keep the connection open,
1448 * GNUNET_SYSERR to close it (signal serious error)
1451 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1452 const struct GNUNET_MessageHeader *message)
1454 return handle_mesh_encrypted (peer,
1455 (struct GNUNET_MESH_Encrypted *)message,
1461 * Core handler for mesh network traffic point-to-point acks.
1463 * @param cls closure
1464 * @param message message
1465 * @param peer peer identity this notification is about
1467 * @return GNUNET_OK to keep the connection open,
1468 * GNUNET_SYSERR to close it (signal serious error)
1471 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1472 const struct GNUNET_MessageHeader *message)
1474 struct GNUNET_MESH_ACK *msg;
1475 struct MeshConnection *c;
1476 struct MeshFlowControl *fc;
1481 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1482 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1484 msg = (struct GNUNET_MESH_ACK *) message;
1486 c = connection_get (&msg->cid);
1490 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1495 /* Is this a forward or backward ACK? */
1496 id = GNUNET_PEER_search (peer);
1497 if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1499 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1503 else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1505 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1511 GNUNET_break_op (0);
1515 ack = ntohl (msg->ack);
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1517 ack, fc->last_ack_recv);
1518 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1519 fc->last_ack_recv = ack;
1521 /* Cancel polling if the ACK is big enough. */
1522 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1523 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1525 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1526 GNUNET_SCHEDULER_cancel (fc->poll_task);
1527 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1528 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1531 connection_unlock_queue (c, fwd);
1538 * Core handler for mesh network traffic point-to-point ack polls.
1540 * @param cls closure
1541 * @param message message
1542 * @param peer peer identity this notification is about
1544 * @return GNUNET_OK to keep the connection open,
1545 * GNUNET_SYSERR to close it (signal serious error)
1548 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1549 const struct GNUNET_MessageHeader *message)
1551 struct GNUNET_MESH_Poll *msg;
1552 struct MeshConnection *c;
1553 struct MeshFlowControl *fc;
1558 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1559 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1562 msg = (struct GNUNET_MESH_Poll *) message;
1564 c = connection_get (&msg->cid);
1568 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1570 GNUNET_break_op (0);
1574 /* Is this a forward or backward ACK?
1575 * Note: a poll should never be needed in a loopback case,
1576 * since there is no possiblility of packet loss there, so
1577 * this way of discerining FWD/BCK should not be a problem.
1579 id = GNUNET_PEER_search (peer);
1580 if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1582 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1585 else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1592 GNUNET_break_op (0);
1596 pid = ntohl (msg->pid);
1597 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1598 pid, fc->last_pid_recv);
1599 fc->last_pid_recv = pid;
1600 fwd = fc == &c->fwd_fc;
1601 send_ack (c, NULL, fwd);
1608 * Core handler for mesh keepalives.
1610 * @param cls closure
1611 * @param message message
1612 * @param peer peer identity this notification is about
1613 * @return GNUNET_OK to keep the connection open,
1614 * GNUNET_SYSERR to close it (signal serious error)
1616 * TODO: Check who we got this from, to validate route.
1619 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1620 const struct GNUNET_MessageHeader *message)
1622 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1623 struct MeshConnection *c;
1624 struct MeshPeer *neighbor;
1627 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1628 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1631 c = connection_get (&msg->cid);
1634 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1639 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1640 GNUNET_YES : GNUNET_NO;
1642 /* Check if origin is as expected */
1643 neighbor = connection_get_hop (c, fwd);
1644 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1646 GNUNET_break_op (0);
1650 connection_change_state (c, MESH_CONNECTION_READY);
1651 connection_reset_timeout (c, fwd);
1653 if (GMC_is_terminal (c, fwd))
1656 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1657 GMC_send_prebuilt_message (message, c, NULL, fwd);
1664 * Send an ACK on the appropriate connection/channel, depending on
1665 * the direction and the position of the peer.
1667 * @param c Which connection to send the hop-by-hop ACK.
1668 * @param ch Channel, if any.
1669 * @param fwd Is this a fwd ACK? (will go dest->root)
1672 GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1674 unsigned int buffer;
1676 LOG (GNUNET_ERROR_TYPE_DEBUG,
1677 "send ack %s on %p %p\n",
1678 fwd ? "FWD" : "BCK", c, ch);
1679 if (NULL == c || GMC_is_terminal (c, fwd))
1681 struct MeshTunnel3 *t;
1682 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n");
1683 t = (NULL == c) ? GMCH_get_tunnel (ch) : GMC_get_tunnel (c);
1684 buffer = GMT_get_buffer (t, fwd);
1688 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1689 buffer = GMC_get_buffer (c, fwd);
1691 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1693 if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1694 (NULL != c && GMC_is_origin (c, fwd)) )
1696 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1699 GNUNET_assert (NULL != ch);
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1701 send_local_ack (ch, fwd);
1706 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
1707 GNUNET_assert (NULL != ch);
1708 channel_send_connections_ack (ch, buffer, fwd);
1712 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1713 connection_send_ack (c, buffer, fwd);
1719 * Initialize the connections subsystem
1721 * @param c Configuration handle.
1724 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1727 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1730 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1731 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1732 GNUNET_SCHEDULER_shutdown ();
1737 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1740 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1741 "MESH", "MAX_CONNECTIONS", "MISSING");
1742 GNUNET_SCHEDULER_shutdown ();
1747 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1748 &refresh_connection_time))
1750 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1751 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1752 GNUNET_SCHEDULER_shutdown ();
1755 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1759 * Shut down the connections subsystem.
1767 struct MeshConnection *
1768 GMC_new (const struct GNUNET_HashCode *cid,
1769 struct MeshTunnel3 *t,
1770 struct MeshPeerPath *p,
1771 unsigned int own_pos)
1773 struct MeshConnection *c;
1775 c = GNUNET_new (struct MeshConnection);
1777 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1778 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1779 fc_init (&c->fwd_fc);
1780 fc_init (&c->bck_fc);
1785 if (own_pos > p->length - 1)
1791 c->own_pos = own_pos;
1796 c->fwd_maintenance_task =
1797 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1798 &connection_fwd_keepalive, c);
1800 register_neighbors (c);
1806 GMC_destroy (struct MeshConnection *c)
1808 struct MeshPeer *peer;
1813 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n",
1814 GNUNET_h2s (&c->id));
1816 /* Cancel all traffic */
1817 connection_cancel_queues (c, GNUNET_YES);
1818 connection_cancel_queues (c, GNUNET_NO);
1820 /* Cancel maintainance task (keepalive/timeout) */
1821 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1822 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1823 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1824 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1826 /* Unregister from neighbors */
1827 unregister_neighbors (c);
1830 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1831 GMT_remove_connection (c->t, c);
1836 * Get the connection ID.
1838 * @param c Connection to get the ID from.
1840 * @return ID of the connection.
1842 const struct GNUNET_HashCode *
1843 GMC_get_id (const struct MeshConnection *c)
1850 * Get the connection state.
1852 * @param c Connection to get the state from.
1854 * @return state of the connection.
1856 enum MeshConnectionState
1857 GMC_get_state (const struct MeshConnection *c)
1863 * Get the connection tunnel.
1865 * @param c Connection to get the tunnel from.
1867 * @return tunnel of the connection.
1869 struct MeshTunnel3 *
1870 GMC_get_tunnel (const struct MeshConnection *c)
1877 * Get free buffer space in a connection.
1879 * @param c Connection.
1880 * @param fwd Is query about FWD traffic?
1882 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1885 GMC_get_buffer (struct MeshConnection *c, int fwd)
1887 struct MeshFlowControl *fc;
1889 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1891 return (fc->queue_max - fc->queue_n);
1895 * Get messages queued in a connection.
1897 * @param c Connection.
1898 * @param fwd Is query about FWD traffic?
1900 * @return Number of messages queued.
1903 GMC_get_qn (struct MeshConnection *c, int fwd)
1905 struct MeshFlowControl *fc;
1907 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1914 * Notify other peers on a connection of a broken link. Mark connections
1915 * to destroy after all traffic has been sent.
1917 * @param c Connection on which there has been a disconnection.
1918 * @param peer Peer that disconnected.
1919 * @param my_full_id My ID (to send to other peers).
1922 GMC_notify_broken (struct MeshConnection *c,
1923 struct MeshPeer *peer,
1924 struct GNUNET_PeerIdentity *my_full_id)
1926 struct GNUNET_MESH_ConnectionBroken msg;
1929 fwd = peer == connection_get_prev_hop (c);
1931 connection_cancel_queues (c, !fwd);
1932 if (GMC_is_terminal (c, fwd))
1934 /* Local shutdown, no one to notify about this. */
1939 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
1940 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
1942 msg.peer1 = *my_full_id;
1943 msg.peer2 = *GMP_get_id (peer);
1944 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
1945 c->destroy = GNUNET_YES;
1952 * Is this peer the first one on the connection?
1954 * @param c Connection.
1955 * @param fwd Is this about fwd traffic?
1957 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1960 GMC_is_origin (struct MeshConnection *c, int fwd)
1962 if (!fwd && c->path->length - 1 == c->own_pos )
1964 if (fwd && 0 == c->own_pos)
1971 * Is this peer the last one on the connection?
1973 * @param c Connection.
1974 * @param fwd Is this about fwd traffic?
1975 * Note that the ROOT is the terminal for BCK traffic!
1977 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1980 GMC_is_terminal (struct MeshConnection *c, int fwd)
1982 return GMC_is_origin (c, !fwd);
1987 * Sends an already built message on a connection, properly registering
1988 * all used resources.
1990 * @param message Message to send. Function makes a copy of it.
1991 * If message is not hop-by-hop, decrements TTL of copy.
1992 * @param c Connection on which this message is transmitted.
1993 * @param ch Channel on which this message is transmitted, or NULL.
1994 * @param fwd Is this a fwd message?
1997 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1998 struct MeshConnection *c,
1999 struct MeshChannel *ch,
2006 size = ntohs (message->size);
2007 data = GNUNET_malloc (size);
2008 memcpy (data, message, size);
2009 type = ntohs (message->type);
2010 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2011 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
2015 struct GNUNET_MESH_Encrypted *emsg;
2016 struct GNUNET_MESH_ACK *amsg;
2017 struct GNUNET_MESH_Poll *pmsg;
2018 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2019 struct GNUNET_MESH_ConnectionBroken *bmsg;
2022 case GNUNET_MESSAGE_TYPE_MESH_FWD:
2023 case GNUNET_MESSAGE_TYPE_MESH_BCK:
2024 emsg = (struct GNUNET_MESH_Encrypted *) data;
2025 ttl = ntohl (emsg->ttl);
2028 GNUNET_break_op (0);
2032 emsg->ttl = htonl (ttl - 1);
2033 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2034 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
2037 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2038 amsg = (struct GNUNET_MESH_ACK *) data;
2040 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2043 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2044 pmsg = (struct GNUNET_MESH_Poll *) data;
2046 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2047 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2050 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2051 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2056 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2057 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2062 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2063 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2070 GMP_queue_add (data, type, size, c, ch, fwd, &message_sent, (void *) size);
2075 * Send a message to all peers in this connection that the connection
2076 * is no longer valid.
2078 * If some peer should not receive the message, it should be zero'ed out
2079 * before calling this function.
2081 * @param c The connection whose peers to notify.
2084 GMC_send_destroy (struct MeshConnection *c)
2086 struct GNUNET_MESH_ConnectionDestroy msg;
2088 if (GNUNET_YES == c->destroy)
2091 msg.header.size = htons (sizeof (msg));
2092 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2094 LOG (GNUNET_ERROR_TYPE_DEBUG,
2095 " sending connection destroy for connection %s\n",
2096 GNUNET_h2s (&c->id));
2098 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2099 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2100 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2101 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2102 c->destroy = GNUNET_YES;