2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh_connection.c
23 * @brief GNUnet MESH service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "mesh_path.h"
33 #include "mesh_protocol_enc.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
42 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
43 GNUNET_TIME_UNIT_MINUTES,\
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct to encapsulate all the Flow Control information to a peer to which
54 * we are directly connected (on a core level).
56 struct MeshFlowControl
59 * Connection this controls.
61 struct MeshConnection *c;
64 * How many messages are in the queue on this connection.
69 * How many messages do we accept in the queue.
71 unsigned int queue_max;
79 * ID of the last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 GNUNET_SCHEDULER_TaskIdentifier poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
110 * Keep a record of the last messages sent on this connection.
112 struct MeshConnectionPerformance
115 * Circular buffer for storing measurements.
117 double usecsperbyte[AVG_MSGS];
120 * Running average of @c usecsperbyte.
125 * How many values of @c usecsperbyte are valid.
130 * Index of the next "free" position in @c usecsperbyte.
137 * Struct containing all information regarding a connection to a peer.
139 struct MeshConnection
142 * Tunnel this connection is part of.
144 struct MeshTunnel3 *t;
147 * Flow control information for traffic fwd.
149 struct MeshFlowControl fwd_fc;
152 * Flow control information for traffic bck.
154 struct MeshFlowControl bck_fc;
157 * Measure connection performance on the endpoint.
159 struct MeshConnectionPerformance *perf;
162 * ID of the connection.
164 struct GNUNET_HashCode id;
167 * State of the connection.
169 enum MeshConnectionState state;
172 * Path being used for the tunnel.
174 struct MeshPeerPath *path;
177 * Position of the local peer in the path.
179 unsigned int own_pos;
182 * Task to keep the used paths alive at the owner,
183 * time tunnel out on all the other peers.
185 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
188 * Task to keep the used paths alive at the destination,
189 * time tunnel out on all the other peers.
191 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
194 * Pending message count.
196 int pending_messages;
199 * Destroy flag: if true, destroy on last message.
204 /******************************************************************************/
205 /******************************* GLOBALS ***********************************/
206 /******************************************************************************/
209 * Global handle to the statistics service.
211 extern struct GNUNET_STATISTICS_Handle *stats;
214 * Local peer own ID (memory efficient handle).
216 extern GNUNET_PEER_Id myid;
219 * Local peer own ID (full value).
221 extern struct GNUNET_PeerIdentity my_full_id;
224 * Connections known, indexed by cid (MeshConnection).
226 static struct GNUNET_CONTAINER_MultiHashMap *connections;
229 * How many connections are we willing to maintain.
230 * Local connections are always allowed, even if there are more connections than max.
232 static unsigned long long max_connections;
235 * How many messages *in total* are we willing to queue, divide by number of
236 * connections to get connection queue size.
238 static unsigned long long max_msgs_queue;
241 * How often to send path keepalives. Paths timeout after 4 missed.
243 static struct GNUNET_TIME_Relative refresh_connection_time;
246 /******************************************************************************/
247 /******************************** STATIC ***********************************/
248 /******************************************************************************/
250 #if 0 // avoid compiler warning for unused static function
252 fc_debug (struct MeshFlowControl *fc)
254 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
255 fc->last_pid_recv, fc->last_ack_sent);
256 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
257 fc->last_pid_sent, fc->last_ack_recv);
258 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
259 fc->queue_n, fc->queue_max);
263 connection_debug (struct MeshConnection *c)
267 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
270 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
271 peer2s (c->t->peer), GMC_2s (c));
272 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
273 c->state, c->pending_messages);
274 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
275 fc_debug (&c->fwd_fc);
276 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
277 fc_debug (&c->bck_fc);
282 * Get string description for tunnel state.
284 * @param s Tunnel state.
286 * @return String representation.
289 GMC_state2s (enum MeshConnectionState s)
293 case MESH_CONNECTION_NEW:
294 return "MESH_CONNECTION_NEW";
295 case MESH_CONNECTION_SENT:
296 return "MESH_CONNECTION_SENT";
297 case MESH_CONNECTION_ACK:
298 return "MESH_CONNECTION_ACK";
299 case MESH_CONNECTION_READY:
300 return "MESH_CONNECTION_READY";
302 return "MESH_CONNECTION_STATE_ERROR";
308 * Initialize a Flow Control structure to the initial state.
310 * @param fc Flow Control structure to initialize.
313 fc_init (struct MeshFlowControl *fc)
316 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
317 fc->last_pid_recv = (uint32_t) -1;
318 fc->last_ack_sent = (uint32_t) 0;
319 fc->last_ack_recv = (uint32_t) 0;
320 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
321 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
323 fc->queue_max = (max_msgs_queue / max_connections) + 1;
330 * @param cid Connection ID.
332 static struct MeshConnection *
333 connection_get (const struct GNUNET_HashCode *cid)
335 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
340 connection_change_state (struct MeshConnection* c,
341 enum MeshConnectionState state)
343 LOG (GNUNET_ERROR_TYPE_DEBUG,
344 "Connection %s state was %s\n",
345 GMC_2s (c), GMC_state2s (c->state));
346 LOG (GNUNET_ERROR_TYPE_DEBUG,
347 "Connection %s state is now %s\n",
348 GMC_2s (c), GMC_state2s (state));
354 * Send an ACK on the connection, informing the predecessor about
355 * the available buffer space. Should not be called in case the peer
356 * is origin (no predecessor).
358 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
359 * the ACK itself goes "back" (dest->root).
361 * @param c Connection on which to send the ACK.
362 * @param buffer How much space free to advertise?
363 * @param fwd Is this FWD ACK? (Going dest->owner)
366 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
368 struct MeshFlowControl *next_fc;
369 struct MeshFlowControl *prev_fc;
370 struct GNUNET_MESH_ACK msg;
374 /* If origin, there is no connection to send ACKs. Wrong function! */
375 if (GMC_is_origin (c, fwd))
381 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
382 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 "connection send %s ack on %s\n",
386 fwd ? "FWD" : "BCK", GMC_2s (c));
388 /* Check if we need to transmit the ACK */
389 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
390 if (3 < delta && buffer < delta)
392 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 " last pid recv: %u, last ack sent: %u\n",
395 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
399 /* Ok, ACK might be necessary, what PID to ACK? */
400 ack = prev_fc->last_pid_recv + buffer;
401 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
402 LOG (GNUNET_ERROR_TYPE_DEBUG,
403 " last pid %u, last ack %u, qmax %u, q %u\n",
404 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
405 next_fc->queue_max, next_fc->queue_n);
406 if (ack == prev_fc->last_ack_sent)
408 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
412 prev_fc->last_ack_sent = ack;
414 /* Build ACK message and send on connection */
415 msg.header.size = htons (sizeof (msg));
416 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
417 msg.ack = htonl (ack);
420 GMC_send_prebuilt_message (&msg.header, c, !fwd);
425 * Callback called when a queued message is sent.
427 * Calculates the average time and connection packet tracking.
429 * @param cls Closure.
430 * @param c Connection this message was on.
431 * @param type Type of message sent.
432 * @param fwd Was this a FWD going message?
433 * @param size Size of the message.
434 * @param wait Time spent waiting for core (only the time for THIS message)
437 message_sent (void *cls,
438 struct MeshConnection *c, uint16_t type,
439 int fwd, size_t size,
440 struct GNUNET_TIME_Relative wait)
442 struct MeshConnectionPerformance *p;
443 struct MeshFlowControl *fc;
446 fc = fwd ? &c->fwd_fc : &c->bck_fc;
447 LOG (GNUNET_ERROR_TYPE_DEBUG, "! sent %s\n", GNUNET_MESH_DEBUG_M2S (type));
448 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
449 c->pending_messages--;
450 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
452 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
456 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
459 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
462 LOG (GNUNET_ERROR_TYPE_DEBUG,
463 "! accounting pid %u\n",
465 GMC_send_ack (c, fwd);
470 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
473 return; /* Only endpoints are interested in timing. */
476 usecsperbyte = ((double) wait.rel_value_us) / size;
477 if (p->size == AVG_MSGS)
479 /* Array is full. Substract oldest value, add new one and store. */
480 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
481 p->usecsperbyte[p->idx] = usecsperbyte;
482 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
486 /* Array not yet full. Add current value to avg and store. */
487 p->usecsperbyte[p->idx] = usecsperbyte;
489 p->avg += p->usecsperbyte[p->idx];
493 p->idx = (p->idx + 1) % AVG_MSGS;
495 // if (NULL != c->t) FIXME
497 // c->t->pending_messages--;
498 // if (GNUNET_YES == c->t->destroy && 0 == t->pending_messages)
500 // LOG (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
501 // GMT_destroy (c->t);
508 * Get the previous hop in a connection
510 * @param c Connection.
512 * @return Previous peer in the connection.
514 static struct MeshPeer *
515 get_prev_hop (const struct MeshConnection *c)
519 LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos);
520 path_debug (c->path);
521 if (0 == c->own_pos || c->path->length < 2)
522 id = c->path->peers[0];
524 id = c->path->peers[c->own_pos - 1];
526 return GMP_get_short (id);
531 * Get the next hop in a connection
533 * @param c Connection.
535 * @return Next peer in the connection.
537 static struct MeshPeer *
538 get_next_hop (const struct MeshConnection *c)
542 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
543 id = c->path->peers[c->path->length - 1];
545 id = c->path->peers[c->own_pos + 1];
547 return GMP_get_short (id);
552 * Get the hop in a connection.
554 * @param c Connection.
555 * @param fwd Next hop?
557 * @return Next peer in the connection.
559 static struct MeshPeer *
560 get_hop (struct MeshConnection *c, int fwd)
563 return get_next_hop (c);
564 return get_prev_hop (c);
569 * Is traffic coming from this sender 'FWD' traffic?
571 * @param c Connection to check.
572 * @param sender Peer identity of neighbor.
574 * @return GNUNET_YES in case the sender is the 'prev' hop and therefore
575 * the traffic is 'FWD'. GNUNET_NO for BCK. GNUNET_SYSERR for errors.
578 is_fwd (const struct MeshConnection *c,
579 const struct GNUNET_PeerIdentity *sender)
583 id = GNUNET_PEER_search (sender);
584 if (GMP_get_short_id (get_prev_hop (c)) == id)
587 if (GMP_get_short_id (get_next_hop (c)) == id)
591 return GNUNET_SYSERR;
596 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
597 * or a first CONNECTION_ACK directed to us.
599 * @param connection Connection to confirm.
600 * @param fwd Should we send it FWD? (root->dest)
601 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
604 send_connection_ack (struct MeshConnection *connection, int fwd)
606 struct MeshTunnel3 *t;
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
610 !fwd ? "FWD" : "BCK");
611 GMP_queue_add (get_hop (connection, fwd), NULL,
612 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
613 sizeof (struct GNUNET_MESH_ConnectionACK),
614 connection, fwd, &message_sent, NULL);
615 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
616 GMT_change_state (t, MESH_TUNNEL3_WAITING);
617 if (MESH_CONNECTION_READY != connection->state)
618 connection_change_state (connection, MESH_CONNECTION_SENT);
623 * Send a notification that a connection is broken.
625 * @param c Connection that is broken.
626 * @param id1 Peer that has disconnected.
627 * @param id2 Peer that has disconnected.
628 * @param fwd Direction towards which to send it.
631 send_broken (struct MeshConnection *c,
632 const struct GNUNET_PeerIdentity *id1,
633 const struct GNUNET_PeerIdentity *id2,
636 struct GNUNET_MESH_ConnectionBroken msg;
638 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
639 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
643 GMC_send_prebuilt_message (&msg.header, c, fwd);
649 * Send keepalive packets for a connection.
651 * @param c Connection to keep alive..
652 * @param fwd Is this a FWD keepalive? (owner -> dest).
655 connection_keepalive (struct MeshConnection *c, int fwd)
657 struct GNUNET_MESH_ConnectionKeepAlive *msg;
658 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
662 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
663 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
665 LOG (GNUNET_ERROR_TYPE_DEBUG,
666 "sending %s keepalive for connection %s]\n",
667 fwd ? "FWD" : "BCK", GMC_2s (c));
669 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
670 msg->header.size = htons (size);
671 msg->header.type = htons (type);
674 GMC_send_prebuilt_message (&msg->header, c, fwd);
679 * Send CONNECTION_{CREATE/ACK} packets for a connection.
681 * @param c Connection for which to send the message.
682 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
685 connection_recreate (struct MeshConnection *c, int fwd)
687 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
691 send_connection_ack (c, GNUNET_NO);
696 * Generic connection timer management.
697 * Depending on the role of the peer in the connection will send the
698 * appropriate message (build or keepalive)
700 * @param c Conncetion to maintain.
704 connection_maintain (struct MeshConnection *c, int fwd)
706 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
708 /* TODO DHT GET with RO_BART */
713 case MESH_CONNECTION_NEW:
715 case MESH_CONNECTION_SENT:
716 connection_recreate (c, fwd);
718 case MESH_CONNECTION_READY:
719 connection_keepalive (c, fwd);
728 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
730 struct MeshConnection *c = cls;
732 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
733 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
736 connection_maintain (c, GNUNET_YES);
737 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
738 &connection_fwd_keepalive,
744 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
746 struct MeshConnection *c = cls;
748 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
749 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
752 connection_maintain (c, GNUNET_NO);
753 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
754 &connection_bck_keepalive,
760 * @brief Re-initiate traffic on this connection if necessary.
762 * Check if there is traffic queued towards this peer
763 * and the core transmit handle is NULL (traffic was stalled).
764 * If so, call core tmt rdy.
766 * @param c Connection on which initiate traffic.
767 * @param fwd Is this about fwd traffic?
770 connection_unlock_queue (struct MeshConnection *c, int fwd)
772 struct MeshPeer *peer;
774 LOG (GNUNET_ERROR_TYPE_DEBUG,
775 "connection_unlock_queue %s on %s\n",
776 fwd ? "FWD" : "BCK", GMC_2s (c));
778 if (GMC_is_terminal (c, fwd))
780 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
784 peer = get_hop (c, fwd);
785 GMP_queue_unlock (peer, c);
790 * Cancel all transmissions that belong to a certain connection.
792 * If the connection is scheduled for destruction and no more messages are left,
793 * the connection will be destroyed by the continuation call.
795 * @param c Connection which to cancel. Might be destroyed during this call.
796 * @param fwd Cancel fwd traffic?
799 connection_cancel_queues (struct MeshConnection *c, int fwd)
801 struct MeshFlowControl *fc;
802 struct MeshPeer *peer;
810 fc = fwd ? &c->fwd_fc : &c->bck_fc;
811 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
813 GNUNET_SCHEDULER_cancel (fc->poll_task);
814 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
816 peer = get_hop (c, fwd);
817 GMP_queue_cancel (peer, c);
822 * Function called if a connection has been stalled for a while,
823 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
825 * @param cls Closure (poll ctx).
826 * @param tc TaskContext.
829 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
831 struct MeshFlowControl *fc = cls;
832 struct GNUNET_MESH_Poll msg;
833 struct MeshConnection *c;
835 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
836 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
842 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
843 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
844 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
845 fc == &c->fwd_fc ? "FWD" : "BCK");
847 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
848 msg.header.size = htons (sizeof (msg));
849 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
850 GMC_send_prebuilt_message (&msg.header, c, fc == &c->fwd_fc);
851 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
852 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
853 &connection_poll, fc);
858 * Timeout function due to lack of keepalive/traffic from the owner.
859 * Destroys connection if called.
861 * @param cls Closure (connection to destroy).
862 * @param tc TaskContext.
865 connection_fwd_timeout (void *cls,
866 const struct GNUNET_SCHEDULER_TaskContext *tc)
868 struct MeshConnection *c = cls;
870 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
871 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
873 LOG (GNUNET_ERROR_TYPE_DEBUG,
874 "Connection %s[%X] FWD timed out. Destroying.\n",
878 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
886 * Timeout function due to lack of keepalive/traffic from the destination.
887 * Destroys connection if called.
889 * @param cls Closure (connection to destroy).
890 * @param tc TaskContext
893 connection_bck_timeout (void *cls,
894 const struct GNUNET_SCHEDULER_TaskContext *tc)
896 struct MeshConnection *c = cls;
898 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
899 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
902 LOG (GNUNET_ERROR_TYPE_DEBUG,
903 "Connection %s[%X] FWD timed out. Destroying.\n",
904 GMT_2s (c->t), c->id);
906 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
914 * Resets the connection timeout task, some other message has done the
916 * - For the first peer on the direction this means to send
917 * a keepalive or a path confirmation message (either create or ACK).
918 * - For all other peers, this means to destroy the connection,
919 * due to lack of activity.
920 * Starts the tiemout if no timeout was running (connection just created).
922 * @param c Connection whose timeout to reset.
923 * @param fwd Is this forward?
925 * TODO use heap to improve efficiency of scheduler.
928 connection_reset_timeout (struct MeshConnection *c, int fwd)
930 GNUNET_SCHEDULER_TaskIdentifier *ti;
931 GNUNET_SCHEDULER_Task f;
933 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
935 if (GNUNET_SCHEDULER_NO_TASK != *ti)
936 GNUNET_SCHEDULER_cancel (*ti);
938 if (GMC_is_origin (c, fwd)) /* Endpoint */
940 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
941 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
945 struct GNUNET_TIME_Relative delay;
947 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
948 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
949 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
955 * Add the connection to the list of both neighbors.
957 * @param c Connection.
960 register_neighbors (struct MeshConnection *c)
962 struct MeshPeer *peer;
964 peer = get_next_hop (c);
965 if (GNUNET_NO == GMP_is_neighbor (peer))
970 GMP_add_connection (peer, c);
971 peer = get_prev_hop (c);
972 if (GNUNET_NO == GMP_is_neighbor (peer))
977 GMP_add_connection (peer, c);
982 * Remove the connection from the list of both neighbors.
984 * @param c Connection.
987 unregister_neighbors (struct MeshConnection *c)
989 struct MeshPeer *peer;
991 peer = get_next_hop (c);
992 GMP_remove_connection (peer, c);
994 peer = get_prev_hop (c);
995 GMP_remove_connection (peer, c);
1001 * Bind the connection to the peer and the tunnel to that peer.
1003 * If the peer has no tunnel, create one. Update tunnel and connection
1004 * data structres to reflect new status.
1006 * @param c Connection.
1010 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1012 GMP_add_tunnel (peer);
1013 c->t = GMP_get_tunnel (peer);
1014 GMT_add_connection (c->t, c);
1017 /******************************************************************************/
1018 /******************************** API ***********************************/
1019 /******************************************************************************/
1022 * Core handler for connection creation.
1024 * @param cls Closure (unused).
1025 * @param peer Sender (neighbor).
1026 * @param message Message.
1028 * @return GNUNET_OK to keep the connection open,
1029 * GNUNET_SYSERR to close it (signal serious error)
1032 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1033 const struct GNUNET_MessageHeader *message)
1035 struct GNUNET_MESH_ConnectionCreate *msg;
1036 struct GNUNET_PeerIdentity *id;
1037 struct GNUNET_HashCode *cid;
1038 struct MeshPeerPath *path;
1039 struct MeshPeer *dest_peer;
1040 struct MeshPeer *orig_peer;
1041 struct MeshConnection *c;
1042 unsigned int own_pos;
1046 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1047 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1050 size = ntohs (message->size);
1051 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1053 GNUNET_break_op (0);
1057 /* Calculate hops */
1058 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1059 if (size % sizeof (struct GNUNET_PeerIdentity))
1061 GNUNET_break_op (0);
1064 size /= sizeof (struct GNUNET_PeerIdentity);
1067 GNUNET_break_op (0);
1070 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1072 /* Get parameters */
1073 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1075 id = (struct GNUNET_PeerIdentity *) &msg[1];
1076 LOG (GNUNET_ERROR_TYPE_DEBUG,
1077 " connection %s (%s).\n",
1078 GNUNET_h2s (cid), GNUNET_i2s (id));
1080 /* Create connection */
1081 c = connection_get (cid);
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1086 path = path_new (size);
1088 for (i = 0; i < size; i++)
1090 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1091 GNUNET_i2s (&id[i]));
1092 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1093 if (path->peers[i] == myid)
1096 if (own_pos == 0 && path->peers[own_pos] != myid)
1098 /* create path: self not found in path through self */
1099 GNUNET_break_op (0);
1100 path_destroy (path);
1103 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1104 GMP_add_path_to_all (path, GNUNET_NO);
1105 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1106 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1109 connection_reset_timeout (c, GNUNET_YES);
1115 if (MESH_CONNECTION_NEW == c->state)
1116 connection_change_state (c, MESH_CONNECTION_SENT);
1118 /* Remember peers */
1119 dest_peer = GMP_get (&id[size - 1]);
1120 orig_peer = GMP_get (&id[0]);
1122 /* Is it a connection to us? */
1123 if (c->own_pos == size - 1)
1125 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1126 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1128 add_to_peer (c, orig_peer);
1129 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1130 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1132 send_connection_ack (c, GNUNET_NO);
1133 if (MESH_CONNECTION_SENT == c->state)
1134 connection_change_state (c, MESH_CONNECTION_ACK);
1136 /* Keep tunnel alive in direction dest->owner*/
1137 connection_reset_timeout (c, GNUNET_NO);
1141 /* It's for somebody else! Retransmit. */
1142 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1143 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1144 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1145 GMC_send_prebuilt_message (message, c, GNUNET_YES);
1152 * Core handler for path confirmations.
1154 * @param cls closure
1155 * @param message message
1156 * @param peer peer identity this notification is about
1158 * @return GNUNET_OK to keep the connection open,
1159 * GNUNET_SYSERR to close it (signal serious error)
1162 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1163 const struct GNUNET_MessageHeader *message)
1165 struct GNUNET_MESH_ConnectionACK *msg;
1166 struct MeshConnection *c;
1167 struct MeshPeerPath *p;
1168 struct MeshPeer *pi;
1171 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1173 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1174 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1175 GNUNET_h2s (&msg->cid));
1176 c = connection_get (&msg->cid);
1179 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1186 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1188 pi = GMP_get (peer);
1189 if (get_next_hop (c) == pi)
1191 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1193 if (MESH_CONNECTION_SENT == c->state)
1194 connection_change_state (c, MESH_CONNECTION_ACK);
1196 else if (get_prev_hop (c) == pi)
1198 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1200 connection_change_state (c, MESH_CONNECTION_READY);
1204 GNUNET_break_op (0);
1207 connection_reset_timeout (c, fwd);
1209 /* Add path to peers? */
1213 GMP_add_path_to_all (p, GNUNET_YES);
1220 /* Message for us as creator? */
1221 if (GMC_is_origin (c, GNUNET_YES))
1223 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1224 connection_change_state (c, MESH_CONNECTION_READY);
1225 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1226 send_connection_ack (c, GNUNET_YES);
1227 GMT_send_queued_data (c->t, GNUNET_YES);
1231 /* Message for us as destination? */
1232 if (GMC_is_terminal (c, GNUNET_YES))
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1235 connection_change_state (c, MESH_CONNECTION_READY);
1236 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1237 GMT_send_queued_data (c->t, GNUNET_NO);
1241 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1242 GMC_send_prebuilt_message (message, c, fwd);
1248 * Core handler for notifications of broken paths
1250 * @param cls Closure (unused).
1251 * @param id Peer identity of sending neighbor.
1252 * @param message Message.
1254 * @return GNUNET_OK to keep the connection open,
1255 * GNUNET_SYSERR to close it (signal serious error)
1258 GMC_handle_broken (void* cls,
1259 const struct GNUNET_PeerIdentity* id,
1260 const struct GNUNET_MessageHeader* message)
1262 struct GNUNET_MESH_ConnectionBroken *msg;
1263 struct MeshConnection *c;
1266 LOG (GNUNET_ERROR_TYPE_DEBUG,
1267 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1268 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1269 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1270 GNUNET_i2s (&msg->peer1));
1271 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1272 GNUNET_i2s (&msg->peer2));
1273 c = connection_get (&msg->cid);
1276 GNUNET_break_op (0);
1280 fwd = is_fwd (c, id);
1281 connection_cancel_queues (c, !fwd);
1282 if (GMC_is_terminal (c, fwd))
1284 if (0 < c->pending_messages)
1285 c->destroy = GNUNET_YES;
1291 GMC_send_prebuilt_message (message, c, fwd);
1292 c->destroy = GNUNET_YES;
1301 * Core handler for tunnel destruction
1303 * @param cls Closure (unused).
1304 * @param peer Peer identity of sending neighbor.
1305 * @param message Message.
1307 * @return GNUNET_OK to keep the connection open,
1308 * GNUNET_SYSERR to close it (signal serious error)
1311 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1312 const struct GNUNET_MessageHeader *message)
1314 struct GNUNET_MESH_ConnectionDestroy *msg;
1315 struct MeshConnection *c;
1318 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1319 LOG (GNUNET_ERROR_TYPE_DEBUG,
1320 "Got a CONNECTION DESTROY message from %s\n",
1322 LOG (GNUNET_ERROR_TYPE_DEBUG,
1323 " for connection %s\n",
1324 GNUNET_h2s (&msg->cid));
1325 c = connection_get (&msg->cid);
1328 /* Probably already got the message from another path,
1329 * destroyed the tunnel and retransmitted to children.
1332 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1336 fwd = is_fwd (c, peer);
1337 if (GNUNET_SYSERR == fwd)
1339 GNUNET_break_op (0);
1342 GMC_send_prebuilt_message (message, c, fwd);
1343 c->destroy = GNUNET_YES;
1349 * Generic handler for mesh network encrypted traffic.
1351 * @param peer Peer identity this notification is about.
1352 * @param msg Encrypted message.
1354 * @return GNUNET_OK to keep the connection open,
1355 * GNUNET_SYSERR to close it (signal serious error)
1358 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1359 const struct GNUNET_MESH_Encrypted *msg)
1361 struct MeshConnection *c;
1362 struct MeshPeer *neighbor;
1363 struct MeshFlowControl *fc;
1364 GNUNET_PEER_Id peer_id;
1372 size = ntohs (msg->header.size);
1374 sizeof (struct GNUNET_MESH_Encrypted) +
1375 sizeof (struct GNUNET_MessageHeader))
1377 GNUNET_break_op (0);
1380 type = ntohs (msg->header.type);
1381 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1382 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1383 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1385 /* Check connection */
1386 c = connection_get (&msg->cid);
1389 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1390 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1394 /* Check if origin is as expected */
1395 neighbor = get_prev_hop (c);
1396 peer_id = GNUNET_PEER_search (peer);
1397 if (peer_id == GMP_get_short_id (neighbor))
1403 neighbor = get_next_hop (c);
1404 if (peer_id == GMP_get_short_id (neighbor))
1410 /* Unexpected peer sending traffic on a connection. */
1411 GNUNET_break_op (0);
1417 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1418 pid = ntohl (msg->pid);
1419 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1421 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1422 LOG (GNUNET_ERROR_TYPE_DEBUG,
1423 "WARNING Received PID %u, (prev %u), ACK %u\n",
1424 pid, fc->last_pid_recv, fc->last_ack_sent);
1427 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1429 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1430 LOG (GNUNET_ERROR_TYPE_DEBUG,
1431 " Pid %u not expected (%u+), dropping!\n",
1432 pid, fc->last_pid_recv + 1);
1435 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1436 connection_change_state (c, MESH_CONNECTION_READY);
1437 connection_reset_timeout (c, fwd);
1438 fc->last_pid_recv = pid;
1440 /* Is this message for us? */
1441 if (GMC_is_terminal (c, fwd))
1443 /* TODO signature verification */
1444 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1445 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1452 fc->last_pid_recv = pid;
1453 GMT_handle_encrypted (c->t, msg, fwd);
1454 GMC_send_ack (c, fwd);
1458 /* Message not for us: forward to next hop */
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1460 ttl = ntohl (msg->ttl);
1461 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1464 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1465 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1466 GMC_send_ack (c, fwd);
1469 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1471 GMC_send_prebuilt_message (&msg->header, c, fwd);
1478 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1480 * @param cls Closure (unused).
1481 * @param message Message received.
1482 * @param peer Peer who sent the message.
1484 * @return GNUNET_OK to keep the connection open,
1485 * GNUNET_SYSERR to close it (signal serious error)
1488 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1489 const struct GNUNET_MessageHeader *message)
1491 return handle_mesh_encrypted (peer,
1492 (struct GNUNET_MESH_Encrypted *)message);
1497 * Core handler for mesh network traffic point-to-point acks.
1499 * @param cls closure
1500 * @param message message
1501 * @param peer peer identity this notification is about
1503 * @return GNUNET_OK to keep the connection open,
1504 * GNUNET_SYSERR to close it (signal serious error)
1507 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1508 const struct GNUNET_MessageHeader *message)
1510 struct GNUNET_MESH_ACK *msg;
1511 struct MeshConnection *c;
1512 struct MeshFlowControl *fc;
1517 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1518 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1520 msg = (struct GNUNET_MESH_ACK *) message;
1522 c = connection_get (&msg->cid);
1526 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1531 /* Is this a forward or backward ACK? */
1532 id = GNUNET_PEER_search (peer);
1533 if (GMP_get_short_id (get_next_hop (c)) == id)
1535 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1539 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1541 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1547 GNUNET_break_op (0);
1551 ack = ntohl (msg->ack);
1552 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1553 ack, fc->last_ack_recv);
1554 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1555 fc->last_ack_recv = ack;
1557 /* Cancel polling if the ACK is big enough. */
1558 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1559 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1561 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1562 GNUNET_SCHEDULER_cancel (fc->poll_task);
1563 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1564 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1567 connection_unlock_queue (c, fwd);
1574 * Core handler for mesh network traffic point-to-point ack polls.
1576 * @param cls closure
1577 * @param message message
1578 * @param peer peer identity this notification is about
1580 * @return GNUNET_OK to keep the connection open,
1581 * GNUNET_SYSERR to close it (signal serious error)
1584 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1585 const struct GNUNET_MessageHeader *message)
1587 struct GNUNET_MESH_Poll *msg;
1588 struct MeshConnection *c;
1589 struct MeshFlowControl *fc;
1594 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1595 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1598 msg = (struct GNUNET_MESH_Poll *) message;
1600 c = connection_get (&msg->cid);
1604 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1606 GNUNET_break_op (0);
1610 /* Is this a forward or backward ACK?
1611 * Note: a poll should never be needed in a loopback case,
1612 * since there is no possiblility of packet loss there, so
1613 * this way of discerining FWD/BCK should not be a problem.
1615 id = GNUNET_PEER_search (peer);
1616 if (GMP_get_short_id (get_next_hop (c)) == id)
1618 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1621 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1623 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1628 GNUNET_break_op (0);
1632 pid = ntohl (msg->pid);
1633 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1634 pid, fc->last_pid_recv);
1635 fc->last_pid_recv = pid;
1636 fwd = fc == &c->fwd_fc;
1637 GMC_send_ack (c, fwd);
1644 * Core handler for mesh keepalives.
1646 * @param cls closure
1647 * @param message message
1648 * @param peer peer identity this notification is about
1649 * @return GNUNET_OK to keep the connection open,
1650 * GNUNET_SYSERR to close it (signal serious error)
1652 * TODO: Check who we got this from, to validate route.
1655 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1656 const struct GNUNET_MessageHeader *message)
1658 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1659 struct MeshConnection *c;
1660 struct MeshPeer *neighbor;
1663 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1664 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1667 c = connection_get (&msg->cid);
1670 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1675 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1676 GNUNET_YES : GNUNET_NO;
1678 /* Check if origin is as expected */
1679 neighbor = get_hop (c, fwd);
1680 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1682 GNUNET_break_op (0);
1686 connection_change_state (c, MESH_CONNECTION_READY);
1687 connection_reset_timeout (c, fwd);
1689 if (GMC_is_terminal (c, fwd))
1692 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1693 GMC_send_prebuilt_message (message, c, fwd);
1700 * Send an ACK on the appropriate connection/channel, depending on
1701 * the direction and the position of the peer.
1703 * @param c Which connection to send the hop-by-hop ACK.
1704 * @param fwd Is this a fwd ACK? (will go dest->root)
1707 GMC_send_ack (struct MeshConnection *c, int fwd)
1709 unsigned int buffer;
1711 LOG (GNUNET_ERROR_TYPE_DEBUG,
1712 "GMC send %s ACK on %s\n",
1713 fwd ? "FWD" : "BCK", GMC_2s (c));
1721 /* Get available buffer space */
1722 if (GMC_is_terminal (c, fwd))
1724 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
1725 buffer = GMT_get_buffer (c->t, fwd);
1729 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1730 buffer = GMC_get_buffer (c, fwd);
1732 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1734 /* Send available buffer space */
1735 if (GMC_is_origin (c, fwd))
1737 GNUNET_assert (NULL != c->t);
1738 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
1741 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1742 GMT_unchoke_channels (c->t, fwd);
1747 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1748 send_ack (c, buffer, fwd);
1754 * Initialize the connections subsystem
1756 * @param c Configuration handle.
1759 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1761 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1763 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1766 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1767 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1768 GNUNET_SCHEDULER_shutdown ();
1773 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1776 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1777 "MESH", "MAX_CONNECTIONS", "MISSING");
1778 GNUNET_SCHEDULER_shutdown ();
1783 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1784 &refresh_connection_time))
1786 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1787 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1788 GNUNET_SCHEDULER_shutdown ();
1791 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1795 * Shut down the connections subsystem.
1800 GNUNET_CONTAINER_multihashmap_destroy (connections);
1804 struct MeshConnection *
1805 GMC_new (const struct GNUNET_HashCode *cid,
1806 struct MeshTunnel3 *t,
1807 struct MeshPeerPath *p,
1808 unsigned int own_pos)
1810 struct MeshConnection *c;
1812 c = GNUNET_new (struct MeshConnection);
1814 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1815 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1816 fc_init (&c->fwd_fc);
1817 fc_init (&c->bck_fc);
1822 if (own_pos > p->length - 1)
1828 c->own_pos = own_pos;
1833 c->fwd_maintenance_task =
1834 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1835 &connection_fwd_keepalive, c);
1837 register_neighbors (c);
1843 GMC_destroy (struct MeshConnection *c)
1848 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
1849 return; /* -> message_sent -> GMC_destroy. Don't loop. */
1852 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
1854 /* Cancel all traffic */
1855 connection_cancel_queues (c, GNUNET_YES);
1856 connection_cancel_queues (c, GNUNET_NO);
1858 /* Cancel maintainance task (keepalive/timeout) */
1859 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1860 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1861 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1862 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1864 /* Unregister from neighbors */
1865 unregister_neighbors (c);
1868 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1870 GMT_remove_connection (c->t, c);
1872 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES))
1873 path_destroy (c->path);
1879 * Get the connection ID.
1881 * @param c Connection to get the ID from.
1883 * @return ID of the connection.
1885 const struct GNUNET_HashCode *
1886 GMC_get_id (const struct MeshConnection *c)
1893 * Get the connection path.
1895 * @param c Connection to get the path from.
1897 * @return path used by the connection.
1899 const struct MeshPeerPath *
1900 GMC_get_path (const struct MeshConnection *c)
1907 * Get the connection state.
1909 * @param c Connection to get the state from.
1911 * @return state of the connection.
1913 enum MeshConnectionState
1914 GMC_get_state (const struct MeshConnection *c)
1920 * Get the connection tunnel.
1922 * @param c Connection to get the tunnel from.
1924 * @return tunnel of the connection.
1926 struct MeshTunnel3 *
1927 GMC_get_tunnel (const struct MeshConnection *c)
1934 * Get free buffer space in a connection.
1936 * @param c Connection.
1937 * @param fwd Is query about FWD traffic?
1939 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1942 GMC_get_buffer (struct MeshConnection *c, int fwd)
1944 struct MeshFlowControl *fc;
1946 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1948 return (fc->queue_max - fc->queue_n);
1952 * Get how many messages have we allowed to send to us from a direction.
1954 * @param c Connection.
1955 * @param fwd Are we asking about traffic from FWD (BCK messages)?
1957 * @return last_ack_sent - last_pid_recv
1960 GMC_get_allowed (struct MeshConnection *c, int fwd)
1962 struct MeshFlowControl *fc;
1964 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1965 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
1969 return (fc->last_ack_sent - fc->last_pid_recv);
1973 * Get messages queued in a connection.
1975 * @param c Connection.
1976 * @param fwd Is query about FWD traffic?
1978 * @return Number of messages queued.
1981 GMC_get_qn (struct MeshConnection *c, int fwd)
1983 struct MeshFlowControl *fc;
1985 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1992 * Allow the connection to advertise a buffer of the given size.
1994 * The connection will send an @c fwd ACK message (so: in direction !fwd)
1995 * allowing up to last_pid_recv + buffer.
1997 * @param c Connection.
1998 * @param buffer How many more messages the connection can accept.
1999 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2002 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2004 send_ack (c, buffer, fwd);
2009 * Notify other peers on a connection of a broken link. Mark connections
2010 * to destroy after all traffic has been sent.
2012 * @param c Connection on which there has been a disconnection.
2013 * @param peer Peer that disconnected.
2016 GMC_notify_broken (struct MeshConnection *c,
2017 struct MeshPeer *peer)
2021 fwd = peer == get_prev_hop (c);
2023 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2025 /* Local shutdown, no one to notify about this. */
2029 if (GNUNET_NO == c->destroy)
2030 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2032 /* Connection will have at least one pending message
2033 * (the one we just scheduled), so no point in checking whether to
2034 * destroy immediately. */
2035 c->destroy = GNUNET_YES;
2038 * Cancel all queues, if no message is left, connection will be destroyed.
2040 connection_cancel_queues (c, !fwd);
2047 * Is this peer the first one on the connection?
2049 * @param c Connection.
2050 * @param fwd Is this about fwd traffic?
2052 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2055 GMC_is_origin (struct MeshConnection *c, int fwd)
2057 if (!fwd && c->path->length - 1 == c->own_pos )
2059 if (fwd && 0 == c->own_pos)
2066 * Is this peer the last one on the connection?
2068 * @param c Connection.
2069 * @param fwd Is this about fwd traffic?
2070 * Note that the ROOT is the terminal for BCK traffic!
2072 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2075 GMC_is_terminal (struct MeshConnection *c, int fwd)
2077 return GMC_is_origin (c, !fwd);
2082 * See if we are allowed to send by the next hop in the given direction.
2084 * @param c Connection.
2085 * @param fwd Is this about fwd traffic?
2087 * @return GNUNET_YES in case it's OK.
2090 GMC_is_sendable (struct MeshConnection *c, int fwd)
2092 struct MeshFlowControl *fc;
2094 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2095 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2101 * Sends an already built message on a connection, properly registering
2102 * all used resources.
2104 * @param message Message to send. Function makes a copy of it.
2105 * If message is not hop-by-hop, decrements TTL of copy.
2106 * @param c Connection on which this message is transmitted.
2107 * @param fwd Is this a fwd message?
2110 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2111 struct MeshConnection *c,
2114 struct MeshFlowControl *fc;
2120 size = ntohs (message->size);
2121 data = GNUNET_malloc (size);
2122 memcpy (data, message, size);
2123 type = ntohs (message->type);
2124 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2125 GNUNET_MESH_DEBUG_M2S (type), size, GMC_2s (c));
2127 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2128 droppable = GNUNET_YES;
2131 struct GNUNET_MESH_Encrypted *emsg;
2132 struct GNUNET_MESH_ACK *amsg;
2133 struct GNUNET_MESH_Poll *pmsg;
2134 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2135 struct GNUNET_MESH_ConnectionBroken *bmsg;
2138 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2139 emsg = (struct GNUNET_MESH_Encrypted *) data;
2140 ttl = ntohl (emsg->ttl);
2143 GNUNET_break_op (0);
2147 emsg->ttl = htonl (ttl - 1);
2148 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2149 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2151 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2152 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
2153 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
2154 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2156 GMC_start_poll (c, fwd);
2160 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2161 amsg = (struct GNUNET_MESH_ACK *) data;
2163 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2164 droppable = GNUNET_NO;
2167 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2168 pmsg = (struct GNUNET_MESH_Poll *) data;
2170 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2171 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2172 droppable = GNUNET_NO;
2175 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2176 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2181 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2182 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2187 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2188 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2195 if (fc->queue_n > fc->queue_max && droppable)
2197 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2200 LOG (GNUNET_ERROR_TYPE_DEBUG,
2201 "queue full: %u/%u\n",
2202 fc->queue_n, fc->queue_max);
2203 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2205 return; /* Drop this message */
2208 c->pending_messages++;
2210 GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2211 &message_sent, NULL);
2216 * Sends a CREATE CONNECTION message for a path to a peer.
2217 * Changes the connection and tunnel states if necessary.
2219 * @param connection Connection to create.
2222 GMC_send_create (struct MeshConnection *connection)
2224 enum MeshTunnel3State state;
2227 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2228 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2229 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2230 GMP_queue_add (get_next_hop (connection), NULL,
2231 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2232 size, connection, GNUNET_YES, &message_sent, NULL);
2233 state = GMT_get_state (connection->t);
2234 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2235 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2236 if (MESH_CONNECTION_NEW == connection->state)
2237 connection_change_state (connection, MESH_CONNECTION_SENT);
2242 * Send a message to all peers in this connection that the connection
2243 * is no longer valid.
2245 * If some peer should not receive the message, it should be zero'ed out
2246 * before calling this function.
2248 * @param c The connection whose peers to notify.
2251 GMC_send_destroy (struct MeshConnection *c)
2253 struct GNUNET_MESH_ConnectionDestroy msg;
2255 if (GNUNET_YES == c->destroy)
2258 msg.header.size = htons (sizeof (msg));
2259 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2261 LOG (GNUNET_ERROR_TYPE_DEBUG,
2262 " sending connection destroy for connection %s\n",
2265 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2266 GMC_send_prebuilt_message (&msg.header, c, GNUNET_YES);
2267 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2268 GMC_send_prebuilt_message (&msg.header, c, GNUNET_NO);
2269 c->destroy = GNUNET_YES;
2274 * @brief Start a polling timer for the connection.
2276 * When a neighbor does not accept more traffic on the connection it could be
2277 * caused by a simple congestion or by a lost ACK. Polling enables to check
2278 * for the lastest ACK status for a connection.
2280 * @param c Connection.
2281 * @param fwd Should we poll in the FWD direction?
2284 GMC_start_poll (struct MeshConnection *c, int fwd)
2286 struct MeshFlowControl *fc;
2288 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2289 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2293 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2300 * @brief Stop polling a connection for ACKs.
2302 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2304 * @param c Connection.
2305 * @param fwd Should we stop the poll in the FWD direction?
2308 GMC_stop_poll (struct MeshConnection *c, int fwd)
2310 struct MeshFlowControl *fc;
2312 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2313 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2315 GNUNET_SCHEDULER_cancel (fc->poll_task);
2316 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2321 * Get a (static) string for a connection.
2323 * @param c Connection.
2326 GMC_2s (struct MeshConnection *c)
2330 static char buf[128];
2332 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2335 return GNUNET_h2s (&c->id);