2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh_connection.c
23 * @brief GNUnet MESH service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "mesh_path.h"
33 #include "mesh_protocol_enc.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
38 #include "gnunet-service-mesh_channel.h"
41 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
43 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
44 GNUNET_TIME_UNIT_MINUTES,\
49 /******************************************************************************/
50 /******************************** STRUCTS **********************************/
51 /******************************************************************************/
54 * Struct to encapsulate all the Flow Control information to a peer to which
55 * we are directly connected (on a core level).
57 struct MeshFlowControl
60 * Connection this controls.
62 struct MeshConnection *c;
65 * How many messages are in the queue on this connection.
70 * How many messages do we accept in the queue.
72 unsigned int queue_max;
80 * ID of the last packet sent towards the peer.
82 uint32_t last_pid_sent;
85 * ID of the last packet received from the peer.
87 uint32_t last_pid_recv;
90 * Last ACK sent to the peer (peer can't send more than this PID).
92 uint32_t last_ack_sent;
95 * Last ACK sent towards the origin (for traffic towards leaf node).
97 uint32_t last_ack_recv;
100 * Task to poll the peer in case of a lost ACK causes stall.
102 GNUNET_SCHEDULER_TaskIdentifier poll_task;
105 * How frequently to poll for ACKs.
107 struct GNUNET_TIME_Relative poll_time;
111 * Keep a record of the last messages sent on this connection.
113 struct MeshConnectionPerformance
116 * Circular buffer for storing measurements.
118 double usecsperbyte[AVG_MSGS];
121 * Running average of @c usecsperbyte.
126 * How many values of @c usecsperbyte are valid.
131 * Index of the next "free" position in @c usecsperbyte.
138 * Struct containing all information regarding a connection to a peer.
140 struct MeshConnection
143 * Tunnel this connection is part of.
145 struct MeshTunnel3 *t;
148 * Flow control information for traffic fwd.
150 struct MeshFlowControl fwd_fc;
153 * Flow control information for traffic bck.
155 struct MeshFlowControl bck_fc;
158 * Measure connection performance on the endpoint.
160 struct MeshConnectionPerformance *perf;
163 * ID of the connection.
165 struct GNUNET_HashCode id;
168 * State of the connection.
170 enum MeshConnectionState state;
173 * Path being used for the tunnel.
175 struct MeshPeerPath *path;
178 * Position of the local peer in the path.
180 unsigned int own_pos;
183 * Task to keep the used paths alive at the owner,
184 * time tunnel out on all the other peers.
186 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
189 * Task to keep the used paths alive at the destination,
190 * time tunnel out on all the other peers.
192 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
195 * Pending message count.
197 int pending_messages;
200 * Destroy flag: if true, destroy on last message.
205 /******************************************************************************/
206 /******************************* GLOBALS ***********************************/
207 /******************************************************************************/
210 * Global handle to the statistics service.
212 extern struct GNUNET_STATISTICS_Handle *stats;
215 * Local peer own ID (memory efficient handle).
217 extern GNUNET_PEER_Id myid;
220 * Connections known, indexed by cid (MeshConnection).
222 static struct GNUNET_CONTAINER_MultiHashMap *connections;
225 * How many connections are we willing to maintain.
226 * Local connections are always allowed, even if there are more connections than max.
228 static unsigned long long max_connections;
231 * How many messages *in total* are we willing to queue, divide by number of
232 * connections to get connection queue size.
234 static unsigned long long max_msgs_queue;
237 * How often to send path keepalives. Paths timeout after 4 missed.
239 static struct GNUNET_TIME_Relative refresh_connection_time;
242 /******************************************************************************/
243 /******************************** STATIC ***********************************/
244 /******************************************************************************/
246 #if 0 // avoid compiler warning for unused static function
248 fc_debug (struct MeshFlowControl *fc)
250 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
251 fc->last_pid_recv, fc->last_ack_sent);
252 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
253 fc->last_pid_sent, fc->last_ack_recv);
254 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
255 fc->queue_n, fc->queue_max);
259 connection_debug (struct MeshConnection *c)
263 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
266 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
267 peer2s (c->t->peer), GNUNET_h2s (&c->id));
268 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
269 c->state, c->pending_messages);
270 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
271 fc_debug (&c->fwd_fc);
272 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
273 fc_debug (&c->bck_fc);
278 * Get string description for tunnel state.
280 * @param s Tunnel state.
282 * @return String representation.
285 GMC_state2s (enum MeshConnectionState s)
289 case MESH_CONNECTION_NEW:
290 return "MESH_CONNECTION_NEW";
291 case MESH_CONNECTION_SENT:
292 return "MESH_CONNECTION_SENT";
293 case MESH_CONNECTION_ACK:
294 return "MESH_CONNECTION_ACK";
295 case MESH_CONNECTION_READY:
296 return "MESH_CONNECTION_READY";
298 return "MESH_CONNECTION_STATE_ERROR";
304 * Initialize a Flow Control structure to the initial state.
306 * @param fc Flow Control structure to initialize.
309 fc_init (struct MeshFlowControl *fc)
312 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
313 fc->last_pid_recv = (uint32_t) -1;
314 fc->last_ack_sent = (uint32_t) 0;
315 fc->last_ack_recv = (uint32_t) 0;
316 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
317 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
319 fc->queue_max = (max_msgs_queue / max_connections) + 1;
326 * @param cid Connection ID.
328 static struct MeshConnection *
329 connection_get (const struct GNUNET_HashCode *cid)
331 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
336 connection_change_state (struct MeshConnection* c,
337 enum MeshConnectionState state)
339 LOG (GNUNET_ERROR_TYPE_DEBUG,
340 "Connection %s state was %s\n",
341 GNUNET_h2s (&c->id), GMC_state2s (c->state));
342 LOG (GNUNET_ERROR_TYPE_DEBUG,
343 "Connection %s state is now %s\n",
344 GNUNET_h2s (&c->id), GMC_state2s (state));
350 * Callback called when a queued message is sent.
352 * Calculates the average time
354 * @param cls Closure.
355 * @param c Connection this message was on.
356 * @param wait Time spent waiting for core (only the time for THIS message)
359 message_sent (void *cls,
360 struct MeshConnection *c,
361 struct GNUNET_TIME_Relative wait)
363 struct MeshConnectionPerformance *p;
364 size_t size = (size_t) cls;
368 return; /* Only endpoints are interested in this. */
371 usecsperbyte = ((double) wait.rel_value_us) / size;
372 if (p->size == AVG_MSGS)
374 /* Array is full. Substract oldest value, add new one and store. */
375 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
376 p->usecsperbyte[p->idx] = usecsperbyte;
377 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
381 /* Array not yet full. Add current value to avg and store. */
382 p->usecsperbyte[p->idx] = usecsperbyte;
384 p->avg += p->usecsperbyte[p->idx];
388 p->idx = (p->idx + 1) % AVG_MSGS;
393 * Get the previous hop in a connection
395 * @param c Connection.
397 * @return Previous peer in the connection.
399 static struct MeshPeer *
400 get_prev_hop (struct MeshConnection *c)
404 if (0 == c->own_pos || c->path->length < 2)
405 id = c->path->peers[0];
407 id = c->path->peers[c->own_pos - 1];
409 return GMP_get_short (id);
414 * Get the next hop in a connection
416 * @param c Connection.
418 * @return Next peer in the connection.
420 static struct MeshPeer *
421 get_next_hop (struct MeshConnection *c)
425 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
426 id = c->path->peers[c->path->length - 1];
428 id = c->path->peers[c->own_pos + 1];
430 return GMP_get_short (id);
435 * Get the hop in a connection.
437 * @param c Connection.
438 * @param fwd Next hop?
440 * @return Next peer in the connection.
442 static struct MeshPeer *
443 get_hop (struct MeshConnection *c, int fwd)
446 return get_next_hop (c);
447 return get_prev_hop (c);
452 * Send an ACK informing the predecessor about the available buffer space.
454 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
455 * the ACK itself goes "back" (dest->root).
457 * @param c Connection on which to send the ACK.
458 * @param buffer How much space free to advertise?
459 * @param fwd Is this FWD ACK? (Going dest->owner)
462 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
464 struct MeshFlowControl *next_fc;
465 struct MeshFlowControl *prev_fc;
466 struct GNUNET_MESH_ACK msg;
470 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
471 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
473 LOG (GNUNET_ERROR_TYPE_DEBUG,
474 "connection send %s ack on %s\n",
475 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
477 /* Check if we need to transmit the ACK */
478 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
481 LOG (GNUNET_ERROR_TYPE_DEBUG,
482 " last pid recv: %u, last ack sent: %u\n",
483 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
487 /* Ok, ACK might be necessary, what PID to ACK? */
488 delta = next_fc->queue_max - next_fc->queue_n;
489 ack = prev_fc->last_pid_recv + delta;
490 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 " last pid %u, last ack %u, qmax %u, q %u\n",
493 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
494 next_fc->queue_max, next_fc->queue_n);
495 if (ack == prev_fc->last_ack_sent)
497 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
501 prev_fc->last_ack_sent = ack;
503 /* Build ACK message and send on connection */
504 msg.header.size = htons (sizeof (msg));
505 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
506 msg.ack = htonl (ack);
509 GMC_send_prebuilt_message (&msg.header, c, NULL, !fwd);
514 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
515 * or a first CONNECTION_ACK directed to us.
517 * @param connection Connection to confirm.
518 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
521 send_connection_ack (struct MeshConnection *connection, int fwd)
523 struct MeshTunnel3 *t;
526 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
527 GMP_queue_add (get_hop (connection, fwd), NULL,
528 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
529 sizeof (struct GNUNET_MESH_ConnectionACK),
530 connection, NULL, fwd,
532 (void *) sizeof (struct GNUNET_MESH_ConnectionACK));
533 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
534 GMT_change_state (t, MESH_TUNNEL3_WAITING);
535 if (MESH_CONNECTION_READY != connection->state)
536 GMC_change_state (connection, MESH_CONNECTION_SENT);
541 * Send keepalive packets for a connection.
543 * @param c Connection to keep alive..
544 * @param fwd Is this a FWD keepalive? (owner -> dest).
547 connection_keepalive (struct MeshConnection *c, int fwd)
549 struct GNUNET_MESH_ConnectionKeepAlive *msg;
550 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
554 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
555 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
557 LOG (GNUNET_ERROR_TYPE_DEBUG,
558 "sending %s keepalive for connection %s[%d]\n",
559 fwd ? "FWD" : "BCK", GMT_2s (c->t), c->id);
561 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
562 msg->header.size = htons (size);
563 msg->header.type = htons (type);
566 GMC_send_prebuilt_message (&msg->header, c, NULL, fwd);
571 * Send CONNECTION_{CREATE/ACK} packets for a connection.
573 * @param c Connection for which to send the message.
574 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
577 connection_recreate (struct MeshConnection *c, int fwd)
579 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
581 send_connection_create (c);
583 send_connection_ack (c, GNUNET_NO);
588 * Generic connection timer management.
589 * Depending on the role of the peer in the connection will send the
590 * appropriate message (build or keepalive)
592 * @param c Conncetion to maintain.
596 connection_maintain (struct MeshConnection *c, int fwd)
598 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
600 /* TODO DHT GET with RO_BART */
605 case MESH_CONNECTION_NEW:
607 case MESH_CONNECTION_SENT:
608 connection_recreate (c, fwd);
610 case MESH_CONNECTION_READY:
611 connection_keepalive (c, fwd);
620 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
622 struct MeshConnection *c = cls;
624 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
625 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
628 connection_maintain (c, GNUNET_YES);
629 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
630 &connection_fwd_keepalive,
636 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
638 struct MeshConnection *c = cls;
640 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
641 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
644 connection_maintain (c, GNUNET_NO);
645 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
646 &connection_bck_keepalive,
652 * @brief Re-initiate traffic on this connection if necessary.
654 * Check if there is traffic queued towards this peer
655 * and the core transmit handle is NULL (traffic was stalled).
656 * If so, call core tmt rdy.
658 * @param c Connection on which initiate traffic.
659 * @param fwd Is this about fwd traffic?
662 connection_unlock_queue (struct MeshConnection *c, int fwd)
664 struct MeshPeer *peer;
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "connection_unlock_queue %s on %s\n",
668 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
670 if (GMC_is_terminal (c, fwd))
672 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
676 peer = get_hop (c, fwd);
677 GMP_queue_unlock (peer, c);
682 * Cancel all transmissions that belong to a certain connection.
684 * @param c Connection which to cancel.
685 * @param fwd Cancel fwd traffic?
688 connection_cancel_queues (struct MeshConnection *c, int fwd)
691 struct MeshFlowControl *fc;
692 struct MeshPeer *peer;
700 peer = get_hop (c, fwd);
701 GMP_queue_cancel (peer, c);
703 fc = fwd ? &c->fwd_fc : &c->bck_fc;
704 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
706 GNUNET_SCHEDULER_cancel (fc->poll_task);
707 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
713 * Function called if a connection has been stalled for a while,
714 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
716 * @param cls Closure (poll ctx).
717 * @param tc TaskContext.
720 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
722 struct MeshFlowControl *fc = cls;
723 struct GNUNET_MESH_Poll msg;
724 struct MeshConnection *c;
726 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
727 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
733 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
734 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
735 GNUNET_h2s (&c->id));
736 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
737 fc == &c->fwd_fc ? "FWD" : "BCK");
739 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
740 msg.header.size = htons (sizeof (msg));
741 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
742 GMC_send_prebuilt_message (&msg.header, c, NULL, fc == &c->fwd_fc);
743 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
744 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
745 &connection_poll, fc);
750 * Timeout function due to lack of keepalive/traffic from the owner.
751 * Destroys connection if called.
753 * @param cls Closure (connection to destroy).
754 * @param tc TaskContext.
757 connection_fwd_timeout (void *cls,
758 const struct GNUNET_SCHEDULER_TaskContext *tc)
760 struct MeshConnection *c = cls;
762 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
763 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
765 LOG (GNUNET_ERROR_TYPE_DEBUG,
766 "Connection %s[%X] FWD timed out. Destroying.\n",
770 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
778 * Timeout function due to lack of keepalive/traffic from the destination.
779 * Destroys connection if called.
781 * @param cls Closure (connection to destroy).
782 * @param tc TaskContext
785 connection_bck_timeout (void *cls,
786 const struct GNUNET_SCHEDULER_TaskContext *tc)
788 struct MeshConnection *c = cls;
790 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
791 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
794 LOG (GNUNET_ERROR_TYPE_DEBUG,
795 "Connection %s[%X] FWD timed out. Destroying.\n",
796 GMT_2s (c->t), c->id);
798 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
806 * Resets the connection timeout task, some other message has done the
808 * - For the first peer on the direction this means to send
809 * a keepalive or a path confirmation message (either create or ACK).
810 * - For all other peers, this means to destroy the connection,
811 * due to lack of activity.
812 * Starts the tiemout if no timeout was running (connection just created).
814 * @param c Connection whose timeout to reset.
815 * @param fwd Is this forward?
817 * TODO use heap to improve efficiency of scheduler.
820 connection_reset_timeout (struct MeshConnection *c, int fwd)
822 GNUNET_SCHEDULER_TaskIdentifier *ti;
823 GNUNET_SCHEDULER_Task f;
825 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
827 if (GNUNET_SCHEDULER_NO_TASK != *ti)
828 GNUNET_SCHEDULER_cancel (*ti);
830 if (GMC_is_origin (c, fwd)) /* Endpoint */
832 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
833 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
837 struct GNUNET_TIME_Relative delay;
839 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
840 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
841 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
847 * Add the connection to the list of both neighbors.
849 * @param c Connection.
852 register_neighbors (struct MeshConnection *c)
854 struct MeshPeer *peer;
856 peer = get_next_hop (c);
857 if (GNUNET_NO == GMP_is_neighbor (peer))
862 GMP_add_connection (peer, c);
863 peer = get_prev_hop (c);
864 if (GNUNET_NO == GMP_is_neighbor (peer))
869 GMP_add_connection (peer, c);
874 * Remove the connection from the list of both neighbors.
876 * @param c Connection.
879 unregister_neighbors (struct MeshConnection *c)
881 struct MeshPeer *peer;
883 peer = get_next_hop (c);
884 GMP_remove_connection (peer, c);
886 peer = get_prev_hop (c);
887 GMP_remove_connection (peer, c);
892 /******************************************************************************/
893 /******************************** API ***********************************/
894 /******************************************************************************/
897 * Core handler for connection creation.
899 * @param cls Closure (unused).
900 * @param peer Sender (neighbor).
901 * @param message Message.
903 * @return GNUNET_OK to keep the connection open,
904 * GNUNET_SYSERR to close it (signal serious error)
907 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
908 const struct GNUNET_MessageHeader *message)
910 struct GNUNET_MESH_ConnectionCreate *msg;
911 struct GNUNET_PeerIdentity *id;
912 struct GNUNET_HashCode *cid;
913 struct MeshPeerPath *path;
914 struct MeshPeer *dest_peer;
915 struct MeshPeer *orig_peer;
916 struct MeshConnection *c;
917 unsigned int own_pos;
921 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
922 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
925 size = ntohs (message->size);
926 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
933 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
934 if (size % sizeof (struct GNUNET_PeerIdentity))
939 size /= sizeof (struct GNUNET_PeerIdentity);
945 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
948 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
950 id = (struct GNUNET_PeerIdentity *) &msg[1];
951 LOG (GNUNET_ERROR_TYPE_DEBUG,
952 " connection %s (%s).\n",
953 GNUNET_h2s (cid), GNUNET_i2s (id));
955 /* Create connection */
956 c = connection_get (cid);
960 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
961 path = path_new (size);
963 for (i = 0; i < size; i++)
965 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
966 GNUNET_i2s (&id[i]));
967 path->peers[i] = GNUNET_PEER_intern (&id[i]);
968 if (path->peers[i] == myid)
971 if (own_pos == 0 && path->peers[own_pos] != myid)
973 /* create path: self not found in path through self */
979 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
980 GMP_add_path_to_all (path, GNUNET_NO);
981 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
982 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
985 connection_reset_timeout (c, GNUNET_YES);
991 if (MESH_CONNECTION_NEW == c->state)
992 connection_change_state (c, MESH_CONNECTION_SENT);
995 dest_peer = GMP_get (&id[size - 1]);
996 orig_peer = GMP_get (&id[0]);
998 /* Is it a connection to us? */
999 if (c->own_pos == size - 1)
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1002 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1004 GMP_add_tunnel (orig_peer);
1005 GMP_add_connection (orig_peer, c);
1006 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1007 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1009 send_connection_ack (c, GNUNET_NO);
1010 if (MESH_CONNECTION_SENT == c->state)
1011 connection_change_state (c, MESH_CONNECTION_ACK);
1013 /* Keep tunnel alive in direction dest->owner*/
1014 connection_reset_timeout (c, GNUNET_NO);
1018 /* It's for somebody else! Retransmit. */
1019 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1020 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1021 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1022 GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES);
1029 * Core handler for path confirmations.
1031 * @param cls closure
1032 * @param message message
1033 * @param peer peer identity this notification is about
1035 * @return GNUNET_OK to keep the connection open,
1036 * GNUNET_SYSERR to close it (signal serious error)
1039 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1040 const struct GNUNET_MessageHeader *message)
1042 struct GNUNET_MESH_ConnectionACK *msg;
1043 struct MeshConnection *c;
1044 struct MeshPeerPath *p;
1045 struct MeshPeer *pi;
1048 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1049 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1050 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1051 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1052 GNUNET_h2s (&msg->cid));
1053 c = connection_get (&msg->cid);
1056 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1063 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1065 pi = peer_get (peer);
1066 if (get_next_hop (c) == pi)
1068 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1070 if (MESH_CONNECTION_SENT == c->state)
1071 connection_change_state (c, MESH_CONNECTION_ACK);
1073 else if (get_prev_hop (c) == pi)
1075 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1077 connection_change_state (c, MESH_CONNECTION_READY);
1081 GNUNET_break_op (0);
1084 connection_reset_timeout (c, fwd);
1086 /* Add path to peers? */
1090 path_add_to_peers (p, GNUNET_YES);
1097 /* Message for us as creator? */
1098 if (GMC_is_origin (c, GNUNET_YES))
1100 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1101 connection_change_state (c, MESH_CONNECTION_READY);
1102 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1103 send_connection_ack (c, GNUNET_YES);
1104 GMT_send_queued_data (c->t, GNUNET_YES);
1108 /* Message for us as destination? */
1109 if (GMC_is_terminal (c, GNUNET_YES))
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1112 GMC_change_state (c, MESH_CONNECTION_READY);
1113 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1114 GMT_send_queued_data (c->t, GNUNET_NO);
1118 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1119 GMC_send_prebuilt_message (message, c, NULL, fwd);
1125 * Core handler for notifications of broken paths
1127 * @param cls Closure (unused).
1128 * @param peer Peer identity of sending neighbor.
1129 * @param message Message.
1131 * @return GNUNET_OK to keep the connection open,
1132 * GNUNET_SYSERR to close it (signal serious error)
1135 GMC_handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
1136 const struct GNUNET_MessageHeader *message)
1138 struct GNUNET_MESH_ConnectionBroken *msg;
1139 struct MeshConnection *c;
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
1143 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1145 GNUNET_i2s (&msg->peer1));
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1147 GNUNET_i2s (&msg->peer2));
1148 c = connection_get (&msg->cid);
1151 GNUNET_break_op (0);
1154 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
1155 GNUNET_PEER_search (&msg->peer2));
1162 * Core handler for tunnel destruction
1164 * @param cls Closure (unused).
1165 * @param peer Peer identity of sending neighbor.
1166 * @param message Message.
1168 * @return GNUNET_OK to keep the connection open,
1169 * GNUNET_SYSERR to close it (signal serious error)
1172 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1173 const struct GNUNET_MessageHeader *message)
1175 struct GNUNET_MESH_ConnectionDestroy *msg;
1176 struct MeshConnection *c;
1180 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1181 LOG (GNUNET_ERROR_TYPE_DEBUG,
1182 "Got a CONNECTION DESTROY message from %s\n",
1184 LOG (GNUNET_ERROR_TYPE_DEBUG,
1185 " for connection %s\n",
1186 GNUNET_h2s (&msg->cid));
1187 c = connection_get (&msg->cid);
1190 /* Probably already got the message from another path,
1191 * destroyed the tunnel and retransmitted to children.
1194 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1198 id = GNUNET_PEER_search (peer);
1199 if (id == GMP_get_short_id (get_prev_hop (c)))
1201 else if (id == GMP_get_short_id (get_next_hop (c)))
1205 GNUNET_break_op (0);
1208 GMC_send_prebuilt_message (message, c, NULL, fwd);
1209 c->destroy = GNUNET_YES;
1215 * Generic handler for mesh network encrypted traffic.
1217 * @param peer Peer identity this notification is about.
1218 * @param message Encrypted message.
1219 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1221 * @return GNUNET_OK to keep the connection open,
1222 * GNUNET_SYSERR to close it (signal serious error)
1225 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1226 const struct GNUNET_MESH_Encrypted *msg,
1229 struct MeshConnection *c;
1230 struct MeshTunnel3 *t;
1231 struct MeshPeer *neighbor;
1232 struct MeshFlowControl *fc;
1239 size = ntohs (msg->header.size);
1241 sizeof (struct GNUNET_MESH_Encrypted) +
1242 sizeof (struct GNUNET_MessageHeader))
1244 GNUNET_break_op (0);
1247 type = ntohs (msg->header.type);
1248 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1249 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1250 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1252 /* Check connection */
1253 c = connection_get (&msg->cid);
1256 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1257 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1261 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1263 /* Check if origin is as expected */
1264 neighbor = get_hop (c, !fwd);
1265 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1267 GNUNET_break_op (0);
1272 pid = ntohl (msg->pid);
1273 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1275 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1276 LOG (GNUNET_ERROR_TYPE_DEBUG,
1277 "WARNING Received PID %u, (prev %u), ACK %u\n",
1278 pid, fc->last_pid_recv, fc->last_ack_sent);
1281 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1283 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1284 LOG (GNUNET_ERROR_TYPE_DEBUG,
1285 " Pid %u not expected (%u+), dropping!\n",
1286 pid, fc->last_pid_recv + 1);
1289 if (MESH_CONNECTION_SENT == c->state)
1290 connection_change_state (c, MESH_CONNECTION_READY);
1291 connection_reset_timeout (c, fwd);
1292 fc->last_pid_recv = pid;
1294 /* Is this message for us? */
1295 if (GMC_is_terminal (c, fwd))
1297 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
1299 struct GNUNET_MessageHeader *msgh;
1302 /* TODO signature verification */
1303 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1304 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1306 fc->last_pid_recv = pid;
1307 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
1311 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1312 handle_decrypted (t, msgh, fwd);
1313 off += ntohs (msgh->size);
1315 send_ack (c, NULL, fwd);
1319 /* Message not for us: forward to next hop */
1320 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1321 ttl = ntohl (msg->ttl);
1322 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1325 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1326 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1327 send_ack (c, NULL, fwd);
1330 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1332 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1339 * Core handler for mesh network traffic going orig->dest.
1341 * @param cls Closure (unused).
1342 * @param message Message received.
1343 * @param peer Peer who sent the message.
1345 * @return GNUNET_OK to keep the connection open,
1346 * GNUNET_SYSERR to close it (signal serious error)
1349 GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1350 const struct GNUNET_MessageHeader *message)
1352 return handle_mesh_encrypted (peer,
1353 (struct GNUNET_MESH_Encrypted *)message,
1358 * Core handler for mesh network traffic going dest->orig.
1360 * @param cls Closure (unused).
1361 * @param message Message received.
1362 * @param peer Peer who sent the message.
1364 * @return GNUNET_OK to keep the connection open,
1365 * GNUNET_SYSERR to close it (signal serious error)
1368 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1369 const struct GNUNET_MessageHeader *message)
1371 return handle_mesh_encrypted (peer,
1372 (struct GNUNET_MESH_Encrypted *)message,
1378 * Core handler for mesh network traffic point-to-point acks.
1380 * @param cls closure
1381 * @param message message
1382 * @param peer peer identity this notification is about
1384 * @return GNUNET_OK to keep the connection open,
1385 * GNUNET_SYSERR to close it (signal serious error)
1388 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1389 const struct GNUNET_MessageHeader *message)
1391 struct GNUNET_MESH_ACK *msg;
1392 struct MeshConnection *c;
1393 struct MeshFlowControl *fc;
1398 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1399 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1401 msg = (struct GNUNET_MESH_ACK *) message;
1403 c = connection_get (&msg->cid);
1407 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1412 /* Is this a forward or backward ACK? */
1413 id = GNUNET_PEER_search (peer);
1414 if (GMP_get_short_id (get_next_hop (c)) == id)
1416 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1420 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1422 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1428 GNUNET_break_op (0);
1432 ack = ntohl (msg->ack);
1433 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1434 ack, fc->last_ack_recv);
1435 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1436 fc->last_ack_recv = ack;
1438 /* Cancel polling if the ACK is big enough. */
1439 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1440 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1442 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1443 GNUNET_SCHEDULER_cancel (fc->poll_task);
1444 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1445 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1448 connection_unlock_queue (c, fwd);
1455 * Core handler for mesh network traffic point-to-point ack polls.
1457 * @param cls closure
1458 * @param message message
1459 * @param peer peer identity this notification is about
1461 * @return GNUNET_OK to keep the connection open,
1462 * GNUNET_SYSERR to close it (signal serious error)
1465 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1466 const struct GNUNET_MessageHeader *message)
1468 struct GNUNET_MESH_Poll *msg;
1469 struct MeshConnection *c;
1470 struct MeshFlowControl *fc;
1475 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1476 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1479 msg = (struct GNUNET_MESH_Poll *) message;
1481 c = connection_get (&msg->cid);
1485 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1487 GNUNET_break_op (0);
1491 /* Is this a forward or backward ACK?
1492 * Note: a poll should never be needed in a loopback case,
1493 * since there is no possiblility of packet loss there, so
1494 * this way of discerining FWD/BCK should not be a problem.
1496 id = GNUNET_PEER_search (peer);
1497 if (GMP_get_short_id (get_next_hop (c)) == id)
1499 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1502 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1504 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1509 GNUNET_break_op (0);
1513 pid = ntohl (msg->pid);
1514 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1515 pid, fc->last_pid_recv);
1516 fc->last_pid_recv = pid;
1517 fwd = fc == &c->fwd_fc;
1518 send_ack (c, NULL, fwd);
1525 * Core handler for mesh keepalives.
1527 * @param cls closure
1528 * @param message message
1529 * @param peer peer identity this notification is about
1530 * @return GNUNET_OK to keep the connection open,
1531 * GNUNET_SYSERR to close it (signal serious error)
1533 * TODO: Check who we got this from, to validate route.
1536 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1537 const struct GNUNET_MessageHeader *message)
1539 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1540 struct MeshConnection *c;
1541 struct MeshPeer *neighbor;
1544 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1548 c = connection_get (&msg->cid);
1551 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1556 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1557 GNUNET_YES : GNUNET_NO;
1559 /* Check if origin is as expected */
1560 neighbor = get_hop (c, fwd);
1561 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1563 GNUNET_break_op (0);
1567 connection_change_state (c, MESH_CONNECTION_READY);
1568 connection_reset_timeout (c, fwd);
1570 if (GMC_is_terminal (c, fwd))
1573 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1574 GMC_send_prebuilt_message (message, c, NULL, fwd);
1581 * Send an ACK on the appropriate connection/channel, depending on
1582 * the direction and the position of the peer.
1584 * @param c Which connection to send the hop-by-hop ACK.
1585 * @param ch Channel, if any.
1586 * @param fwd Is this a fwd ACK? (will go dest->root)
1589 GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1591 unsigned int buffer;
1593 LOG (GNUNET_ERROR_TYPE_DEBUG,
1594 "send ack %s on %p %p\n",
1595 fwd ? "FWD" : "BCK", c, ch);
1596 if (NULL == c || GMC_is_terminal (c, fwd))
1598 struct MeshTunnel3 *t;
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n");
1600 t = (NULL == c) ? GMCH_get_tunnel (ch) : GMC_get_tunnel (c);
1601 buffer = GMT_get_buffer (t, fwd);
1605 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1606 buffer = GMC_get_buffer (c, fwd);
1608 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1610 if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1611 (NULL != c && GMC_is_origin (c, fwd)) )
1613 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1616 GNUNET_assert (NULL != ch);
1617 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1618 send_local_ack (ch, fwd);
1623 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
1624 GNUNET_assert (NULL != ch);
1625 channel_send_connections_ack (ch, buffer, fwd);
1629 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1630 connection_send_ack (c, buffer, fwd);
1636 * Initialize the connections subsystem
1638 * @param c Configuration handle.
1641 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1644 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1647 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1648 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1649 GNUNET_SCHEDULER_shutdown ();
1654 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1657 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1658 "MESH", "MAX_CONNECTIONS", "MISSING");
1659 GNUNET_SCHEDULER_shutdown ();
1664 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1665 &refresh_connection_time))
1667 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1668 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1669 GNUNET_SCHEDULER_shutdown ();
1672 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1676 * Shut down the connections subsystem.
1684 struct MeshConnection *
1685 GMC_new (const struct GNUNET_HashCode *cid,
1686 struct MeshTunnel3 *t,
1687 struct MeshPeerPath *p,
1688 unsigned int own_pos)
1690 struct MeshConnection *c;
1692 c = GNUNET_new (struct MeshConnection);
1694 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1695 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1696 fc_init (&c->fwd_fc);
1697 fc_init (&c->bck_fc);
1702 if (own_pos > p->length - 1)
1708 c->own_pos = own_pos;
1713 c->fwd_maintenance_task =
1714 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1715 &connection_fwd_keepalive, c);
1717 register_neighbors (c);
1723 GMC_destroy (struct MeshConnection *c)
1728 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n",
1729 GNUNET_h2s (&c->id));
1731 /* Cancel all traffic */
1732 connection_cancel_queues (c, GNUNET_YES);
1733 connection_cancel_queues (c, GNUNET_NO);
1735 /* Cancel maintainance task (keepalive/timeout) */
1736 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1737 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1738 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1739 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1741 /* Unregister from neighbors */
1742 unregister_neighbors (c);
1745 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1746 GMT_remove_connection (c->t, c);
1751 * Get the connection ID.
1753 * @param c Connection to get the ID from.
1755 * @return ID of the connection.
1757 const struct GNUNET_HashCode *
1758 GMC_get_id (const struct MeshConnection *c)
1765 * Get the connection state.
1767 * @param c Connection to get the state from.
1769 * @return state of the connection.
1771 enum MeshConnectionState
1772 GMC_get_state (const struct MeshConnection *c)
1778 * Get the connection tunnel.
1780 * @param c Connection to get the tunnel from.
1782 * @return tunnel of the connection.
1784 struct MeshTunnel3 *
1785 GMC_get_tunnel (const struct MeshConnection *c)
1792 * Get free buffer space in a connection.
1794 * @param c Connection.
1795 * @param fwd Is query about FWD traffic?
1797 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1800 GMC_get_buffer (struct MeshConnection *c, int fwd)
1802 struct MeshFlowControl *fc;
1804 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1806 return (fc->queue_max - fc->queue_n);
1810 * Get messages queued in a connection.
1812 * @param c Connection.
1813 * @param fwd Is query about FWD traffic?
1815 * @return Number of messages queued.
1818 GMC_get_qn (struct MeshConnection *c, int fwd)
1820 struct MeshFlowControl *fc;
1822 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1829 * Notify other peers on a connection of a broken link. Mark connections
1830 * to destroy after all traffic has been sent.
1832 * @param c Connection on which there has been a disconnection.
1833 * @param peer Peer that disconnected.
1834 * @param my_full_id My ID (to send to other peers).
1837 GMC_notify_broken (struct MeshConnection *c,
1838 struct MeshPeer *peer,
1839 struct GNUNET_PeerIdentity *my_full_id)
1841 struct GNUNET_MESH_ConnectionBroken msg;
1844 fwd = peer == get_prev_hop (c);
1846 connection_cancel_queues (c, !fwd);
1847 if (GMC_is_terminal (c, fwd))
1849 /* Local shutdown, no one to notify about this. */
1854 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
1855 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
1857 msg.peer1 = *my_full_id;
1858 msg.peer2 = *GMP_get_id (peer);
1859 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
1860 c->destroy = GNUNET_YES;
1867 * Is this peer the first one on the connection?
1869 * @param c Connection.
1870 * @param fwd Is this about fwd traffic?
1872 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1875 GMC_is_origin (struct MeshConnection *c, int fwd)
1877 if (!fwd && c->path->length - 1 == c->own_pos )
1879 if (fwd && 0 == c->own_pos)
1886 * Is this peer the last one on the connection?
1888 * @param c Connection.
1889 * @param fwd Is this about fwd traffic?
1890 * Note that the ROOT is the terminal for BCK traffic!
1892 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1895 GMC_is_terminal (struct MeshConnection *c, int fwd)
1897 return GMC_is_origin (c, !fwd);
1902 * See if we are allowed to send by the next hop in the given direction.
1904 * @param c Connection.
1905 * @param fwd Is this about fwd traffic?
1907 * @return GNUNET_YES in case it's OK.
1910 GMC_is_sendable (struct MeshConnection *c, int fwd)
1912 struct MeshFlowControl *fc;
1914 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1915 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1921 * Sends an already built message on a connection, properly registering
1922 * all used resources.
1924 * @param message Message to send. Function makes a copy of it.
1925 * If message is not hop-by-hop, decrements TTL of copy.
1926 * @param c Connection on which this message is transmitted.
1927 * @param ch Channel on which this message is transmitted, or NULL.
1928 * @param fwd Is this a fwd message?
1931 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1932 struct MeshConnection *c,
1933 struct MeshChannel *ch,
1940 size = ntohs (message->size);
1941 data = GNUNET_malloc (size);
1942 memcpy (data, message, size);
1943 type = ntohs (message->type);
1944 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
1945 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1949 struct GNUNET_MESH_Encrypted *emsg;
1950 struct GNUNET_MESH_ACK *amsg;
1951 struct GNUNET_MESH_Poll *pmsg;
1952 struct GNUNET_MESH_ConnectionDestroy *dmsg;
1953 struct GNUNET_MESH_ConnectionBroken *bmsg;
1956 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1957 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1958 emsg = (struct GNUNET_MESH_Encrypted *) data;
1959 ttl = ntohl (emsg->ttl);
1962 GNUNET_break_op (0);
1966 emsg->ttl = htonl (ttl - 1);
1967 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1968 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1971 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1972 amsg = (struct GNUNET_MESH_ACK *) data;
1974 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1977 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1978 pmsg = (struct GNUNET_MESH_Poll *) data;
1980 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1981 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1984 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
1985 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
1990 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1991 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
1996 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1997 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2004 GMP_queue_add (get_hop (c, fwd), data, type, size, c, ch, fwd,
2005 &message_sent, (void *) size);
2010 * Sends a CREATE CONNECTION message for a path to a peer.
2011 * Changes the connection and tunnel states if necessary.
2013 * @param connection Connection to create.
2016 GMC_send_create (struct MeshConnection *connection)
2018 enum MeshTunnel3State state;
2021 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2022 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2023 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2024 GMP_queue_add (get_next_hop (connection), NULL,
2025 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2029 GNUNET_YES, &message_sent, (void *) size);
2030 state = GMT_get_state (connection->t);
2031 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2032 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2033 if (MESH_CONNECTION_NEW == connection->state)
2034 GMC_change_state (connection, MESH_CONNECTION_SENT);
2039 * Send a message to all peers in this connection that the connection
2040 * is no longer valid.
2042 * If some peer should not receive the message, it should be zero'ed out
2043 * before calling this function.
2045 * @param c The connection whose peers to notify.
2048 GMC_send_destroy (struct MeshConnection *c)
2050 struct GNUNET_MESH_ConnectionDestroy msg;
2052 if (GNUNET_YES == c->destroy)
2055 msg.header.size = htons (sizeof (msg));
2056 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2058 LOG (GNUNET_ERROR_TYPE_DEBUG,
2059 " sending connection destroy for connection %s\n",
2060 GNUNET_h2s (&c->id));
2062 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2063 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2064 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2065 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2066 c->destroy = GNUNET_YES;
2071 * @brief Start a polling timer for the connection.
2073 * When a neighbor does not accept more traffic on the connection it could be
2074 * caused by a simple congestion or by a lost ACK. Polling enables to check
2075 * for the lastest ACK status for a connection.
2077 * @param c Connection.
2078 * @param fwd Should we poll in the FWD direction?
2081 GMC_start_poll (struct MeshConnection *c, int fwd)
2083 struct MeshFlowControl *fc;
2085 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2086 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2090 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2097 * @brief Stop polling a connection for ACKs.
2099 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2101 * @param c Connection.
2102 * @param fwd Should we stop the poll in the FWD direction?
2105 GMC_stop_poll (struct MeshConnection *c, int fwd)
2107 struct MeshFlowControl *fc;
2109 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2110 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2112 GNUNET_SCHEDULER_cancel (fc->poll_task);
2113 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;