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;
394 * Send an ACK informing the predecessor about the available buffer space.
396 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
397 * the ACK itself goes "back" (dest->root).
399 * @param c Connection on which to send the ACK.
400 * @param buffer How much space free to advertise?
401 * @param fwd Is this FWD ACK? (Going dest->owner)
404 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
406 struct MeshFlowControl *next_fc;
407 struct MeshFlowControl *prev_fc;
408 struct GNUNET_MESH_ACK msg;
412 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
413 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
415 LOG (GNUNET_ERROR_TYPE_DEBUG,
416 "connection send %s ack on %s\n",
417 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
419 /* Check if we need to transmit the ACK */
420 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
422 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
423 LOG (GNUNET_ERROR_TYPE_DEBUG,
424 " last pid recv: %u, last ack sent: %u\n",
425 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
429 /* Ok, ACK might be necessary, what PID to ACK? */
430 delta = next_fc->queue_max - next_fc->queue_n;
431 ack = prev_fc->last_pid_recv + delta;
432 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
434 " last pid %u, last ack %u, qmax %u, q %u\n",
435 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
436 next_fc->queue_max, next_fc->queue_n);
437 if (ack == prev_fc->last_ack_sent)
439 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
443 prev_fc->last_ack_sent = ack;
445 /* Build ACK message and send on connection */
446 msg.header.size = htons (sizeof (msg));
447 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
448 msg.ack = htonl (ack);
451 GMC_send_prebuilt_message (&msg.header, c, NULL, !fwd);
456 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
457 * or a first CONNECTION_ACK directed to us.
459 * @param connection Connection to confirm.
460 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
463 send_connection_ack (struct MeshConnection *connection, int fwd)
465 struct MeshTunnel3 *t;
468 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
470 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
471 sizeof (struct GNUNET_MESH_ConnectionACK),
472 connection, NULL, fwd,
474 (void *) sizeof (struct GNUNET_MESH_ConnectionACK));
475 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
476 GMT_change_state (t, MESH_TUNNEL3_WAITING);
477 if (MESH_CONNECTION_READY != connection->state)
478 GMC_change_state (connection, MESH_CONNECTION_SENT);
483 * Send keepalive packets for a connection.
485 * @param c Connection to keep alive..
486 * @param fwd Is this a FWD keepalive? (owner -> dest).
489 connection_keepalive (struct MeshConnection *c, int fwd)
491 struct GNUNET_MESH_ConnectionKeepAlive *msg;
492 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
496 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
497 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
499 LOG (GNUNET_ERROR_TYPE_DEBUG,
500 "sending %s keepalive for connection %s[%d]\n",
501 fwd ? "FWD" : "BCK", GMT_2s (c->t), c->id);
503 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
504 msg->header.size = htons (size);
505 msg->header.type = htons (type);
508 GMC_send_prebuilt_message (&msg->header, c, NULL, fwd);
513 * Send CONNECTION_{CREATE/ACK} packets for a connection.
515 * @param c Connection for which to send the message.
516 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
519 connection_recreate (struct MeshConnection *c, int fwd)
521 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
523 send_connection_create (c);
525 send_connection_ack (c, GNUNET_NO);
530 * Generic connection timer management.
531 * Depending on the role of the peer in the connection will send the
532 * appropriate message (build or keepalive)
534 * @param c Conncetion to maintain.
538 connection_maintain (struct MeshConnection *c, int fwd)
540 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
542 /* TODO DHT GET with RO_BART */
547 case MESH_CONNECTION_NEW:
549 case MESH_CONNECTION_SENT:
550 connection_recreate (c, fwd);
552 case MESH_CONNECTION_READY:
553 connection_keepalive (c, fwd);
562 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
564 struct MeshConnection *c = cls;
566 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
567 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
570 connection_maintain (c, GNUNET_YES);
571 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
572 &connection_fwd_keepalive,
578 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
580 struct MeshConnection *c = cls;
582 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
583 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
586 connection_maintain (c, GNUNET_NO);
587 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
588 &connection_bck_keepalive,
595 * Get the previous hop in a connection
597 * @param c Connection.
599 * @return Previous peer in the connection.
601 static struct MeshPeer *
602 connection_get_prev_hop (struct MeshConnection *c)
606 if (0 == c->own_pos || c->path->length < 2)
607 id = c->path->peers[0];
609 id = c->path->peers[c->own_pos - 1];
611 return peer_get_short (id);
616 * Get the next hop in a connection
618 * @param c Connection.
620 * @return Next peer in the connection.
622 static struct MeshPeer *
623 connection_get_next_hop (struct MeshConnection *c)
627 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
628 id = c->path->peers[c->path->length - 1];
630 id = c->path->peers[c->own_pos + 1];
632 return peer_get_short (id);
637 * Get the hop in a connection.
639 * @param c Connection.
640 * @param fwd Next hop?
642 * @return Next peer in the connection.
644 static struct MeshPeer *
645 connection_get_hop (struct MeshConnection *c, int fwd)
648 return connection_get_next_hop (c);
649 return connection_get_prev_hop (c);
654 * @brief Re-initiate traffic on this connection if necessary.
656 * Check if there is traffic queued towards this peer
657 * and the core transmit handle is NULL (traffic was stalled).
658 * If so, call core tmt rdy.
660 * @param c Connection on which initiate traffic.
661 * @param fwd Is this about fwd traffic?
664 connection_unlock_queue (struct MeshConnection *c, int fwd)
666 struct MeshPeer *peer;
668 LOG (GNUNET_ERROR_TYPE_DEBUG,
669 "connection_unlock_queue %s on %s\n",
670 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
672 if (GMC_is_terminal (c, fwd))
674 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
678 peer = connection_get_hop (c, fwd);
679 GMP_queue_unlock (peer, c);
684 * Cancel all transmissions that belong to a certain connection.
686 * @param c Connection which to cancel.
687 * @param fwd Cancel fwd traffic?
690 connection_cancel_queues (struct MeshConnection *c, int fwd)
693 struct MeshFlowControl *fc;
694 struct MeshPeer *peer;
702 peer = connection_get_hop (c, fwd);
703 GMP_queue_cancel (peer, c);
705 fc = fwd ? &c->fwd_fc : &c->bck_fc;
706 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
708 GNUNET_SCHEDULER_cancel (fc->poll_task);
709 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
715 * Function called if a connection has been stalled for a while,
716 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
718 * @param cls Closure (poll ctx).
719 * @param tc TaskContext.
722 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
724 struct MeshFlowControl *fc = cls;
725 struct GNUNET_MESH_Poll msg;
726 struct MeshConnection *c;
728 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
729 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
735 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
736 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
737 GNUNET_h2s (&c->id));
738 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
739 fc == &c->fwd_fc ? "FWD" : "BCK");
741 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
742 msg.header.size = htons (sizeof (msg));
743 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
744 GMC_send_prebuilt_message (&msg.header, c, NULL, fc == &c->fwd_fc);
745 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
746 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
747 &connection_poll, fc);
752 * Timeout function due to lack of keepalive/traffic from the owner.
753 * Destroys connection if called.
755 * @param cls Closure (connection to destroy).
756 * @param tc TaskContext.
759 connection_fwd_timeout (void *cls,
760 const struct GNUNET_SCHEDULER_TaskContext *tc)
762 struct MeshConnection *c = cls;
764 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
765 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
767 LOG (GNUNET_ERROR_TYPE_DEBUG,
768 "Connection %s[%X] FWD timed out. Destroying.\n",
772 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
780 * Timeout function due to lack of keepalive/traffic from the destination.
781 * Destroys connection if called.
783 * @param cls Closure (connection to destroy).
784 * @param tc TaskContext
787 connection_bck_timeout (void *cls,
788 const struct GNUNET_SCHEDULER_TaskContext *tc)
790 struct MeshConnection *c = cls;
792 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
793 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "Connection %s[%X] FWD timed out. Destroying.\n",
798 GMT_2s (c->t), c->id);
800 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
808 * Resets the connection timeout task, some other message has done the
810 * - For the first peer on the direction this means to send
811 * a keepalive or a path confirmation message (either create or ACK).
812 * - For all other peers, this means to destroy the connection,
813 * due to lack of activity.
814 * Starts the tiemout if no timeout was running (connection just created).
816 * @param c Connection whose timeout to reset.
817 * @param fwd Is this forward?
819 * TODO use heap to improve efficiency of scheduler.
822 connection_reset_timeout (struct MeshConnection *c, int fwd)
824 GNUNET_SCHEDULER_TaskIdentifier *ti;
825 GNUNET_SCHEDULER_Task f;
827 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
829 if (GNUNET_SCHEDULER_NO_TASK != *ti)
830 GNUNET_SCHEDULER_cancel (*ti);
832 if (GMC_is_origin (c, fwd)) /* Endpoint */
834 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
835 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
839 struct GNUNET_TIME_Relative delay;
841 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
842 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
843 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
849 * Add the connection to the list of both neighbors.
851 * @param c Connection.
854 register_neighbors (struct MeshConnection *c)
856 struct MeshPeer *peer;
858 peer = connection_get_next_hop (c);
859 if (GNUNET_NO == GMP_is_neighbor (peer))
864 GMP_add_connection (peer, c);
865 peer = connection_get_prev_hop (c);
866 if (GNUNET_NO == GMP_is_neighbor (peer))
871 GMP_add_connection (peer, c);
876 * Remove the connection from the list of both neighbors.
878 * @param c Connection.
881 unregister_neighbors (struct MeshConnection *c)
883 struct MeshPeer *peer;
885 peer = connection_get_next_hop (c);
886 GMP_remove_connection (peer, c);
888 peer = connection_get_prev_hop (c);
889 GMP_remove_connection (peer, c);
894 /******************************************************************************/
895 /******************************** API ***********************************/
896 /******************************************************************************/
899 * Core handler for connection creation.
901 * @param cls Closure (unused).
902 * @param peer Sender (neighbor).
903 * @param message Message.
905 * @return GNUNET_OK to keep the connection open,
906 * GNUNET_SYSERR to close it (signal serious error)
909 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
910 const struct GNUNET_MessageHeader *message)
912 struct GNUNET_MESH_ConnectionCreate *msg;
913 struct GNUNET_PeerIdentity *id;
914 struct GNUNET_HashCode *cid;
915 struct MeshPeerPath *path;
916 struct MeshPeer *dest_peer;
917 struct MeshPeer *orig_peer;
918 struct MeshConnection *c;
919 unsigned int own_pos;
923 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
924 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
927 size = ntohs (message->size);
928 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
935 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
936 if (size % sizeof (struct GNUNET_PeerIdentity))
941 size /= sizeof (struct GNUNET_PeerIdentity);
947 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
950 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
952 id = (struct GNUNET_PeerIdentity *) &msg[1];
953 LOG (GNUNET_ERROR_TYPE_DEBUG,
954 " connection %s (%s).\n",
955 GNUNET_h2s (cid), GNUNET_i2s (id));
957 /* Create connection */
958 c = connection_get (cid);
962 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
963 path = path_new (size);
965 for (i = 0; i < size; i++)
967 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
968 GNUNET_i2s (&id[i]));
969 path->peers[i] = GNUNET_PEER_intern (&id[i]);
970 if (path->peers[i] == myid)
973 if (own_pos == 0 && path->peers[own_pos] != myid)
975 /* create path: self not found in path through self */
981 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
982 GMP_add_path_to_all (path, GNUNET_NO);
983 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
984 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
987 connection_reset_timeout (c, GNUNET_YES);
993 if (MESH_CONNECTION_NEW == c->state)
994 connection_change_state (c, MESH_CONNECTION_SENT);
997 dest_peer = GMP_get (&id[size - 1]);
998 orig_peer = GMP_get (&id[0]);
1000 /* Is it a connection to us? */
1001 if (c->own_pos == size - 1)
1003 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1004 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1006 GMP_add_tunnel (orig_peer);
1007 GMP_add_connection (orig_peer, c);
1008 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1009 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1011 send_connection_ack (c, GNUNET_NO);
1012 if (MESH_CONNECTION_SENT == c->state)
1013 connection_change_state (c, MESH_CONNECTION_ACK);
1015 /* Keep tunnel alive in direction dest->owner*/
1016 connection_reset_timeout (c, GNUNET_NO);
1020 /* It's for somebody else! Retransmit. */
1021 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1022 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1023 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1024 GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES);
1031 * Core handler for path confirmations.
1033 * @param cls closure
1034 * @param message message
1035 * @param peer peer identity this notification is about
1037 * @return GNUNET_OK to keep the connection open,
1038 * GNUNET_SYSERR to close it (signal serious error)
1041 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1042 const struct GNUNET_MessageHeader *message)
1044 struct GNUNET_MESH_ConnectionACK *msg;
1045 struct MeshConnection *c;
1046 struct MeshPeerPath *p;
1047 struct MeshPeer *pi;
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1051 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1052 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1053 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1054 GNUNET_h2s (&msg->cid));
1055 c = connection_get (&msg->cid);
1058 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1065 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1067 pi = peer_get (peer);
1068 if (connection_get_next_hop (c) == pi)
1070 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1072 if (MESH_CONNECTION_SENT == c->state)
1073 connection_change_state (c, MESH_CONNECTION_ACK);
1075 else if (connection_get_prev_hop (c) == pi)
1077 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1079 connection_change_state (c, MESH_CONNECTION_READY);
1083 GNUNET_break_op (0);
1086 connection_reset_timeout (c, fwd);
1088 /* Add path to peers? */
1092 path_add_to_peers (p, GNUNET_YES);
1099 /* Message for us as creator? */
1100 if (GMC_is_origin (c, GNUNET_YES))
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1103 connection_change_state (c, MESH_CONNECTION_READY);
1104 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1105 send_connection_ack (c, GNUNET_YES);
1106 GMT_send_queued_data (c->t, GNUNET_YES);
1110 /* Message for us as destination? */
1111 if (GMC_is_terminal (c, GNUNET_YES))
1113 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1114 GMC_change_state (c, MESH_CONNECTION_READY);
1115 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1116 GMT_send_queued_data (c->t, GNUNET_NO);
1120 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1121 GMC_send_prebuilt_message (message, c, NULL, fwd);
1127 * Core handler for notifications of broken paths
1129 * @param cls Closure (unused).
1130 * @param peer Peer identity of sending neighbor.
1131 * @param message Message.
1133 * @return GNUNET_OK to keep the connection open,
1134 * GNUNET_SYSERR to close it (signal serious error)
1137 GMC_handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
1138 const struct GNUNET_MessageHeader *message)
1140 struct GNUNET_MESH_ConnectionBroken *msg;
1141 struct MeshConnection *c;
1143 LOG (GNUNET_ERROR_TYPE_DEBUG,
1144 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
1145 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1147 GNUNET_i2s (&msg->peer1));
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1149 GNUNET_i2s (&msg->peer2));
1150 c = connection_get (&msg->cid);
1153 GNUNET_break_op (0);
1156 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
1157 GNUNET_PEER_search (&msg->peer2));
1164 * Core handler for tunnel destruction
1166 * @param cls Closure (unused).
1167 * @param peer Peer identity of sending neighbor.
1168 * @param message Message.
1170 * @return GNUNET_OK to keep the connection open,
1171 * GNUNET_SYSERR to close it (signal serious error)
1174 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1175 const struct GNUNET_MessageHeader *message)
1177 struct GNUNET_MESH_ConnectionDestroy *msg;
1178 struct MeshConnection *c;
1182 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "Got a CONNECTION DESTROY message from %s\n",
1186 LOG (GNUNET_ERROR_TYPE_DEBUG,
1187 " for connection %s\n",
1188 GNUNET_h2s (&msg->cid));
1189 c = connection_get (&msg->cid);
1192 /* Probably already got the message from another path,
1193 * destroyed the tunnel and retransmitted to children.
1196 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1200 id = GNUNET_PEER_search (peer);
1201 if (id == GMP_get_short_id (connection_get_prev_hop (c)))
1203 else if (id == GMP_get_short_id (connection_get_next_hop (c)))
1207 GNUNET_break_op (0);
1210 GMC_send_prebuilt_message (message, c, NULL, fwd);
1211 c->destroy = GNUNET_YES;
1217 * Generic handler for mesh network encrypted traffic.
1219 * @param peer Peer identity this notification is about.
1220 * @param message Encrypted message.
1221 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1223 * @return GNUNET_OK to keep the connection open,
1224 * GNUNET_SYSERR to close it (signal serious error)
1227 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1228 const struct GNUNET_MESH_Encrypted *msg,
1231 struct MeshConnection *c;
1232 struct MeshTunnel3 *t;
1233 struct MeshPeer *neighbor;
1234 struct MeshFlowControl *fc;
1241 size = ntohs (msg->header.size);
1243 sizeof (struct GNUNET_MESH_Encrypted) +
1244 sizeof (struct GNUNET_MessageHeader))
1246 GNUNET_break_op (0);
1249 type = ntohs (msg->header.type);
1250 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1251 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1252 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1254 /* Check connection */
1255 c = connection_get (&msg->cid);
1258 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1259 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1263 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1265 /* Check if origin is as expected */
1266 neighbor = connection_get_hop (c, !fwd);
1267 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1269 GNUNET_break_op (0);
1274 pid = ntohl (msg->pid);
1275 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1277 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1278 LOG (GNUNET_ERROR_TYPE_DEBUG,
1279 "WARNING Received PID %u, (prev %u), ACK %u\n",
1280 pid, fc->last_pid_recv, fc->last_ack_sent);
1283 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1285 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1286 LOG (GNUNET_ERROR_TYPE_DEBUG,
1287 " Pid %u not expected (%u+), dropping!\n",
1288 pid, fc->last_pid_recv + 1);
1291 if (MESH_CONNECTION_SENT == c->state)
1292 connection_change_state (c, MESH_CONNECTION_READY);
1293 connection_reset_timeout (c, fwd);
1294 fc->last_pid_recv = pid;
1296 /* Is this message for us? */
1297 if (GMC_is_terminal (c, fwd))
1299 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
1301 struct GNUNET_MessageHeader *msgh;
1304 /* TODO signature verification */
1305 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1306 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1308 fc->last_pid_recv = pid;
1309 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
1313 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1314 handle_decrypted (t, msgh, fwd);
1315 off += ntohs (msgh->size);
1317 send_ack (c, NULL, fwd);
1321 /* Message not for us: forward to next hop */
1322 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1323 ttl = ntohl (msg->ttl);
1324 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1327 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1328 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1329 send_ack (c, NULL, fwd);
1332 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1334 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1341 * Core handler for mesh network traffic going orig->dest.
1343 * @param cls Closure (unused).
1344 * @param message Message received.
1345 * @param peer Peer who sent the message.
1347 * @return GNUNET_OK to keep the connection open,
1348 * GNUNET_SYSERR to close it (signal serious error)
1351 GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1352 const struct GNUNET_MessageHeader *message)
1354 return handle_mesh_encrypted (peer,
1355 (struct GNUNET_MESH_Encrypted *)message,
1360 * Core handler for mesh network traffic going dest->orig.
1362 * @param cls Closure (unused).
1363 * @param message Message received.
1364 * @param peer Peer who sent the message.
1366 * @return GNUNET_OK to keep the connection open,
1367 * GNUNET_SYSERR to close it (signal serious error)
1370 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1371 const struct GNUNET_MessageHeader *message)
1373 return handle_mesh_encrypted (peer,
1374 (struct GNUNET_MESH_Encrypted *)message,
1380 * Core handler for mesh network traffic point-to-point acks.
1382 * @param cls closure
1383 * @param message message
1384 * @param peer peer identity this notification is about
1386 * @return GNUNET_OK to keep the connection open,
1387 * GNUNET_SYSERR to close it (signal serious error)
1390 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1391 const struct GNUNET_MessageHeader *message)
1393 struct GNUNET_MESH_ACK *msg;
1394 struct MeshConnection *c;
1395 struct MeshFlowControl *fc;
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1401 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1403 msg = (struct GNUNET_MESH_ACK *) message;
1405 c = connection_get (&msg->cid);
1409 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1414 /* Is this a forward or backward ACK? */
1415 id = GNUNET_PEER_search (peer);
1416 if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1418 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1422 else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1424 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1430 GNUNET_break_op (0);
1434 ack = ntohl (msg->ack);
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1436 ack, fc->last_ack_recv);
1437 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1438 fc->last_ack_recv = ack;
1440 /* Cancel polling if the ACK is big enough. */
1441 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1442 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1444 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1445 GNUNET_SCHEDULER_cancel (fc->poll_task);
1446 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1447 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1450 connection_unlock_queue (c, fwd);
1457 * Core handler for mesh network traffic point-to-point ack polls.
1459 * @param cls closure
1460 * @param message message
1461 * @param peer peer identity this notification is about
1463 * @return GNUNET_OK to keep the connection open,
1464 * GNUNET_SYSERR to close it (signal serious error)
1467 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1468 const struct GNUNET_MessageHeader *message)
1470 struct GNUNET_MESH_Poll *msg;
1471 struct MeshConnection *c;
1472 struct MeshFlowControl *fc;
1477 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1478 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1481 msg = (struct GNUNET_MESH_Poll *) message;
1483 c = connection_get (&msg->cid);
1487 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1489 GNUNET_break_op (0);
1493 /* Is this a forward or backward ACK?
1494 * Note: a poll should never be needed in a loopback case,
1495 * since there is no possiblility of packet loss there, so
1496 * this way of discerining FWD/BCK should not be a problem.
1498 id = GNUNET_PEER_search (peer);
1499 if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1501 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1504 else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1506 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1511 GNUNET_break_op (0);
1515 pid = ntohl (msg->pid);
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1517 pid, fc->last_pid_recv);
1518 fc->last_pid_recv = pid;
1519 fwd = fc == &c->fwd_fc;
1520 send_ack (c, NULL, fwd);
1527 * Core handler for mesh keepalives.
1529 * @param cls closure
1530 * @param message message
1531 * @param peer peer identity this notification is about
1532 * @return GNUNET_OK to keep the connection open,
1533 * GNUNET_SYSERR to close it (signal serious error)
1535 * TODO: Check who we got this from, to validate route.
1538 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1539 const struct GNUNET_MessageHeader *message)
1541 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1542 struct MeshConnection *c;
1543 struct MeshPeer *neighbor;
1546 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1547 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1550 c = connection_get (&msg->cid);
1553 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1558 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1559 GNUNET_YES : GNUNET_NO;
1561 /* Check if origin is as expected */
1562 neighbor = connection_get_hop (c, fwd);
1563 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1565 GNUNET_break_op (0);
1569 connection_change_state (c, MESH_CONNECTION_READY);
1570 connection_reset_timeout (c, fwd);
1572 if (GMC_is_terminal (c, fwd))
1575 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1576 GMC_send_prebuilt_message (message, c, NULL, fwd);
1583 * Send an ACK on the appropriate connection/channel, depending on
1584 * the direction and the position of the peer.
1586 * @param c Which connection to send the hop-by-hop ACK.
1587 * @param ch Channel, if any.
1588 * @param fwd Is this a fwd ACK? (will go dest->root)
1591 GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1593 unsigned int buffer;
1595 LOG (GNUNET_ERROR_TYPE_DEBUG,
1596 "send ack %s on %p %p\n",
1597 fwd ? "FWD" : "BCK", c, ch);
1598 if (NULL == c || GMC_is_terminal (c, fwd))
1600 struct MeshTunnel3 *t;
1601 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n");
1602 t = (NULL == c) ? GMCH_get_tunnel (ch) : GMC_get_tunnel (c);
1603 buffer = GMT_get_buffer (t, fwd);
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1608 buffer = GMC_get_buffer (c, fwd);
1610 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1612 if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1613 (NULL != c && GMC_is_origin (c, fwd)) )
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1618 GNUNET_assert (NULL != ch);
1619 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1620 send_local_ack (ch, fwd);
1625 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
1626 GNUNET_assert (NULL != ch);
1627 channel_send_connections_ack (ch, buffer, fwd);
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1632 connection_send_ack (c, buffer, fwd);
1638 * Initialize the connections subsystem
1640 * @param c Configuration handle.
1643 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1646 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1649 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1650 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1651 GNUNET_SCHEDULER_shutdown ();
1656 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1659 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1660 "MESH", "MAX_CONNECTIONS", "MISSING");
1661 GNUNET_SCHEDULER_shutdown ();
1666 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1667 &refresh_connection_time))
1669 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1670 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1671 GNUNET_SCHEDULER_shutdown ();
1674 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1678 * Shut down the connections subsystem.
1686 struct MeshConnection *
1687 GMC_new (const struct GNUNET_HashCode *cid,
1688 struct MeshTunnel3 *t,
1689 struct MeshPeerPath *p,
1690 unsigned int own_pos)
1692 struct MeshConnection *c;
1694 c = GNUNET_new (struct MeshConnection);
1696 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1697 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1698 fc_init (&c->fwd_fc);
1699 fc_init (&c->bck_fc);
1704 if (own_pos > p->length - 1)
1710 c->own_pos = own_pos;
1715 c->fwd_maintenance_task =
1716 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1717 &connection_fwd_keepalive, c);
1719 register_neighbors (c);
1725 GMC_destroy (struct MeshConnection *c)
1730 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n",
1731 GNUNET_h2s (&c->id));
1733 /* Cancel all traffic */
1734 connection_cancel_queues (c, GNUNET_YES);
1735 connection_cancel_queues (c, GNUNET_NO);
1737 /* Cancel maintainance task (keepalive/timeout) */
1738 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1739 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1740 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1741 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1743 /* Unregister from neighbors */
1744 unregister_neighbors (c);
1747 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1748 GMT_remove_connection (c->t, c);
1753 * Get the connection ID.
1755 * @param c Connection to get the ID from.
1757 * @return ID of the connection.
1759 const struct GNUNET_HashCode *
1760 GMC_get_id (const struct MeshConnection *c)
1767 * Get the connection state.
1769 * @param c Connection to get the state from.
1771 * @return state of the connection.
1773 enum MeshConnectionState
1774 GMC_get_state (const struct MeshConnection *c)
1780 * Get the connection tunnel.
1782 * @param c Connection to get the tunnel from.
1784 * @return tunnel of the connection.
1786 struct MeshTunnel3 *
1787 GMC_get_tunnel (const struct MeshConnection *c)
1794 * Get free buffer space in a connection.
1796 * @param c Connection.
1797 * @param fwd Is query about FWD traffic?
1799 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1802 GMC_get_buffer (struct MeshConnection *c, int fwd)
1804 struct MeshFlowControl *fc;
1806 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1808 return (fc->queue_max - fc->queue_n);
1812 * Get messages queued in a connection.
1814 * @param c Connection.
1815 * @param fwd Is query about FWD traffic?
1817 * @return Number of messages queued.
1820 GMC_get_qn (struct MeshConnection *c, int fwd)
1822 struct MeshFlowControl *fc;
1824 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1831 * Notify other peers on a connection of a broken link. Mark connections
1832 * to destroy after all traffic has been sent.
1834 * @param c Connection on which there has been a disconnection.
1835 * @param peer Peer that disconnected.
1836 * @param my_full_id My ID (to send to other peers).
1839 GMC_notify_broken (struct MeshConnection *c,
1840 struct MeshPeer *peer,
1841 struct GNUNET_PeerIdentity *my_full_id)
1843 struct GNUNET_MESH_ConnectionBroken msg;
1846 fwd = peer == connection_get_prev_hop (c);
1848 connection_cancel_queues (c, !fwd);
1849 if (GMC_is_terminal (c, fwd))
1851 /* Local shutdown, no one to notify about this. */
1856 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
1857 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
1859 msg.peer1 = *my_full_id;
1860 msg.peer2 = *GMP_get_id (peer);
1861 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
1862 c->destroy = GNUNET_YES;
1869 * Is this peer the first one on the connection?
1871 * @param c Connection.
1872 * @param fwd Is this about fwd traffic?
1874 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1877 GMC_is_origin (struct MeshConnection *c, int fwd)
1879 if (!fwd && c->path->length - 1 == c->own_pos )
1881 if (fwd && 0 == c->own_pos)
1888 * Is this peer the last one on the connection?
1890 * @param c Connection.
1891 * @param fwd Is this about fwd traffic?
1892 * Note that the ROOT is the terminal for BCK traffic!
1894 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1897 GMC_is_terminal (struct MeshConnection *c, int fwd)
1899 return GMC_is_origin (c, !fwd);
1904 * See if we are allowed to send by the next hop in the given direction.
1906 * @param c Connection.
1907 * @param fwd Is this about fwd traffic?
1909 * @return GNUNET_YES in case it's OK.
1912 GMC_is_sendable (struct MeshConnection *c, int fwd)
1914 struct MeshFlowControl *fc;
1916 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1917 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1923 * Sends an already built message on a connection, properly registering
1924 * all used resources.
1926 * @param message Message to send. Function makes a copy of it.
1927 * If message is not hop-by-hop, decrements TTL of copy.
1928 * @param c Connection on which this message is transmitted.
1929 * @param ch Channel on which this message is transmitted, or NULL.
1930 * @param fwd Is this a fwd message?
1933 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1934 struct MeshConnection *c,
1935 struct MeshChannel *ch,
1942 size = ntohs (message->size);
1943 data = GNUNET_malloc (size);
1944 memcpy (data, message, size);
1945 type = ntohs (message->type);
1946 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
1947 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1951 struct GNUNET_MESH_Encrypted *emsg;
1952 struct GNUNET_MESH_ACK *amsg;
1953 struct GNUNET_MESH_Poll *pmsg;
1954 struct GNUNET_MESH_ConnectionDestroy *dmsg;
1955 struct GNUNET_MESH_ConnectionBroken *bmsg;
1958 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1959 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1960 emsg = (struct GNUNET_MESH_Encrypted *) data;
1961 ttl = ntohl (emsg->ttl);
1964 GNUNET_break_op (0);
1968 emsg->ttl = htonl (ttl - 1);
1969 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1970 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1973 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1974 amsg = (struct GNUNET_MESH_ACK *) data;
1976 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1979 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1980 pmsg = (struct GNUNET_MESH_Poll *) data;
1982 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1983 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1986 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
1987 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
1992 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1993 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
1998 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1999 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2006 GMP_queue_add (data, type, size, c, ch, fwd, &message_sent, (void *) size);
2011 * Sends a CREATE CONNECTION message for a path to a peer.
2012 * Changes the connection and tunnel states if necessary.
2014 * @param connection Connection to create.
2017 GMC_send_create (struct MeshConnection *connection)
2019 enum MeshTunnel3State state;
2022 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2023 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2024 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2025 GMP_queue_add (NULL,
2026 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2030 GNUNET_YES, &message_sent, (void *) size);
2031 state = GMT_get_state (connection->t);
2032 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2033 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2034 if (MESH_CONNECTION_NEW == connection->state)
2035 GMC_change_state (connection, MESH_CONNECTION_SENT);
2040 * Send a message to all peers in this connection that the connection
2041 * is no longer valid.
2043 * If some peer should not receive the message, it should be zero'ed out
2044 * before calling this function.
2046 * @param c The connection whose peers to notify.
2049 GMC_send_destroy (struct MeshConnection *c)
2051 struct GNUNET_MESH_ConnectionDestroy msg;
2053 if (GNUNET_YES == c->destroy)
2056 msg.header.size = htons (sizeof (msg));
2057 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2059 LOG (GNUNET_ERROR_TYPE_DEBUG,
2060 " sending connection destroy for connection %s\n",
2061 GNUNET_h2s (&c->id));
2063 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2064 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2065 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2066 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2067 c->destroy = GNUNET_YES;
2072 * @brief Start a polling timer for the connection.
2074 * When a neighbor does not accept more traffic on the connection it could be
2075 * caused by a simple congestion or by a lost ACK. Polling enables to check
2076 * for the lastest ACK status for a connection.
2078 * @param c Connection.
2079 * @param fwd Should we poll in the FWD direction?
2082 GMC_start_poll (struct MeshConnection *c, int fwd)
2084 struct MeshFlowControl *fc;
2086 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2087 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2091 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2098 * @brief Stop polling a connection for ACKs.
2100 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2102 * @param c Connection.
2103 * @param fwd Should we stop the poll in the FWD direction?
2106 GMC_stop_poll (struct MeshConnection *c, int fwd)
2108 struct MeshFlowControl *fc;
2110 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2111 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2113 GNUNET_SCHEDULER_cancel (fc->poll_task);
2114 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;