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,
473 &message_sent, sizeof (struct GNUNET_MESH_ConnectionACK));
474 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
475 GMT_change_state (t, MESH_TUNNEL3_WAITING);
476 if (MESH_CONNECTION_READY != connection->state)
477 GMC_change_state (connection, MESH_CONNECTION_SENT);
482 * Sends a CREATE CONNECTION message for a path to a peer.
483 * Changes the connection and tunnel states if necessary.
485 * @param connection Connection to create.
488 send_connection_create (struct MeshConnection *connection)
490 enum MeshTunnel3State state;
493 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
494 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
495 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
497 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
501 GNUNET_YES, &message_sent, size);
502 state = GMT_get_state (connection->t);
503 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
504 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
505 if (MESH_CONNECTION_NEW == connection->state)
506 connection_change_state (connection, MESH_CONNECTION_SENT);
511 * Send keepalive packets for a connection.
513 * @param c Connection to keep alive..
514 * @param fwd Is this a FWD keepalive? (owner -> dest).
517 connection_keepalive (struct MeshConnection *c, int fwd)
519 struct GNUNET_MESH_ConnectionKeepAlive *msg;
520 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
524 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
525 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
527 LOG (GNUNET_ERROR_TYPE_DEBUG,
528 "sending %s keepalive for connection %s[%d]\n",
529 fwd ? "FWD" : "BCK", GMT_2s (c->t), c->id);
531 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
532 msg->header.size = htons (size);
533 msg->header.type = htons (type);
536 GMC_send_prebuilt_message (&msg->header, c, NULL, fwd);
541 * Send CONNECTION_{CREATE/ACK} packets for a connection.
543 * @param c Connection for which to send the message.
544 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
547 connection_recreate (struct MeshConnection *c, int fwd)
549 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
551 send_connection_create (c);
553 send_connection_ack (c, GNUNET_NO);
558 * Generic connection timer management.
559 * Depending on the role of the peer in the connection will send the
560 * appropriate message (build or keepalive)
562 * @param c Conncetion to maintain.
566 connection_maintain (struct MeshConnection *c, int fwd)
568 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
570 /* TODO DHT GET with RO_BART */
575 case MESH_CONNECTION_NEW:
577 case MESH_CONNECTION_SENT:
578 connection_recreate (c, fwd);
580 case MESH_CONNECTION_READY:
581 connection_keepalive (c, fwd);
590 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
592 struct MeshConnection *c = cls;
594 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
595 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
598 connection_maintain (c, GNUNET_YES);
599 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
600 &connection_fwd_keepalive,
606 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
608 struct MeshConnection *c = cls;
610 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
611 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
614 connection_maintain (c, GNUNET_NO);
615 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
616 &connection_bck_keepalive,
623 * Get the previous hop in a connection
625 * @param c Connection.
627 * @return Previous peer in the connection.
629 static struct MeshPeer *
630 connection_get_prev_hop (struct MeshConnection *c)
634 if (0 == c->own_pos || c->path->length < 2)
635 id = c->path->peers[0];
637 id = c->path->peers[c->own_pos - 1];
639 return peer_get_short (id);
644 * Get the next hop in a connection
646 * @param c Connection.
648 * @return Next peer in the connection.
650 static struct MeshPeer *
651 connection_get_next_hop (struct MeshConnection *c)
655 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
656 id = c->path->peers[c->path->length - 1];
658 id = c->path->peers[c->own_pos + 1];
660 return peer_get_short (id);
665 * Get the hop in a connection.
667 * @param c Connection.
668 * @param fwd Next hop?
670 * @return Next peer in the connection.
672 static struct MeshPeer *
673 connection_get_hop (struct MeshConnection *c, int fwd)
676 return connection_get_next_hop (c);
677 return connection_get_prev_hop (c);
682 * Get the first transmittable message for a connection.
684 * @param c Connection.
685 * @param fwd Is this FWD?
687 * @return First transmittable message.
689 static struct MeshPeerQueue *
690 connection_get_first_message (struct MeshConnection *c, int fwd)
692 struct MeshPeerQueue *q;
695 p = connection_get_hop (c, fwd);
697 for (q = p->queue_head; NULL != q; q = q->next)
701 if (queue_is_sendable (q))
710 * @brief Re-initiate traffic on this connection if necessary.
712 * Check if there is traffic queued towards this peer
713 * and the core transmit handle is NULL (traffic was stalled).
714 * If so, call core tmt rdy.
716 * @param c Connection on which initiate traffic.
717 * @param fwd Is this about fwd traffic?
720 connection_unlock_queue (struct MeshConnection *c, int fwd)
722 struct MeshPeer *peer;
723 struct MeshPeerQueue *q;
726 LOG (GNUNET_ERROR_TYPE_DEBUG,
727 "connection_unlock_queue %s on %s\n",
728 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
730 if (GMC_is_terminal (c, fwd))
732 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
736 peer = connection_get_hop (c, fwd);
738 if (NULL != peer->core_transmit)
740 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
741 return; /* Already unlocked */
744 q = connection_get_first_message (c, fwd);
747 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
748 return; /* Nothing to transmit */
752 peer->core_transmit =
753 GNUNET_CORE_notify_transmit_ready (core_handle,
756 GNUNET_TIME_UNIT_FOREVER_REL,
757 GNUNET_PEER_resolve2 (peer->id),
765 * Cancel all transmissions that belong to a certain connection.
767 * @param c Connection which to cancel.
768 * @param fwd Cancel fwd traffic?
771 connection_cancel_queues (struct MeshConnection *c, int fwd)
774 struct MeshFlowControl *fc;
775 struct MeshPeer *peer;
783 peer = connection_get_hop (c, fwd);
784 GMP_queue_cancel (peer, c);
786 fc = fwd ? &c->fwd_fc : &c->bck_fc;
787 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
789 GNUNET_SCHEDULER_cancel (fc->poll_task);
790 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
796 * Function called if a connection has been stalled for a while,
797 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
799 * @param cls Closure (poll ctx).
800 * @param tc TaskContext.
803 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
805 struct MeshFlowControl *fc = cls;
806 struct GNUNET_MESH_Poll msg;
807 struct MeshConnection *c;
809 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
810 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
816 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
817 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
818 GNUNET_h2s (&c->id));
819 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
820 fc == &c->fwd_fc ? "FWD" : "BCK");
822 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
823 msg.header.size = htons (sizeof (msg));
824 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
825 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
826 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
827 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
828 &connection_poll, fc);
833 * Timeout function due to lack of keepalive/traffic from the owner.
834 * Destroys connection if called.
836 * @param cls Closure (connection to destroy).
837 * @param tc TaskContext.
840 connection_fwd_timeout (void *cls,
841 const struct GNUNET_SCHEDULER_TaskContext *tc)
843 struct MeshConnection *c = cls;
845 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
846 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
848 LOG (GNUNET_ERROR_TYPE_DEBUG,
849 "Connection %s[%X] FWD timed out. Destroying.\n",
853 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
861 * Timeout function due to lack of keepalive/traffic from the destination.
862 * Destroys connection if called.
864 * @param cls Closure (connection to destroy).
865 * @param tc TaskContext
868 connection_bck_timeout (void *cls,
869 const struct GNUNET_SCHEDULER_TaskContext *tc)
871 struct MeshConnection *c = cls;
873 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
874 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
877 LOG (GNUNET_ERROR_TYPE_DEBUG,
878 "Connection %s[%X] FWD timed out. Destroying.\n",
879 GMT_2s (c->t), c->id);
881 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
889 * Resets the connection timeout task, some other message has done the
891 * - For the first peer on the direction this means to send
892 * a keepalive or a path confirmation message (either create or ACK).
893 * - For all other peers, this means to destroy the connection,
894 * due to lack of activity.
895 * Starts the tiemout if no timeout was running (connection just created).
897 * @param c Connection whose timeout to reset.
898 * @param fwd Is this forward?
900 * TODO use heap to improve efficiency of scheduler.
903 connection_reset_timeout (struct MeshConnection *c, int fwd)
905 GNUNET_SCHEDULER_TaskIdentifier *ti;
906 GNUNET_SCHEDULER_Task f;
908 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
910 if (GNUNET_SCHEDULER_NO_TASK != *ti)
911 GNUNET_SCHEDULER_cancel (*ti);
913 if (GMC_is_origin (c, fwd)) /* Endpoint */
915 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
916 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
920 struct GNUNET_TIME_Relative delay;
922 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
923 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
924 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
930 * Add the connection to the list of both neighbors.
932 * @param c Connection.
935 register_neighbors (struct MeshConnection *c)
937 struct MeshPeer *peer;
939 peer = connection_get_next_hop (c);
940 if (GNUNET_NO == GMP_is_neighbor (peer))
945 GMP_add_connection (peer, c);
946 peer = connection_get_prev_hop (c);
947 if (GNUNET_NO == GMP_is_neighbor (peer))
952 GMP_add_connection (peer, c);
957 * Remove the connection from the list of both neighbors.
959 * @param c Connection.
962 unregister_neighbors (struct MeshConnection *c)
964 struct MeshPeer *peer;
966 peer = connection_get_next_hop (c);
967 GMP_remove_connection (peer, c);
969 peer = connection_get_prev_hop (c);
970 GMP_remove_connection (peer, c);
975 /******************************************************************************/
976 /******************************** API ***********************************/
977 /******************************************************************************/
980 * Core handler for connection creation.
982 * @param cls Closure (unused).
983 * @param peer Sender (neighbor).
984 * @param message Message.
986 * @return GNUNET_OK to keep the connection open,
987 * GNUNET_SYSERR to close it (signal serious error)
990 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
991 const struct GNUNET_MessageHeader *message)
993 struct GNUNET_MESH_ConnectionCreate *msg;
994 struct GNUNET_PeerIdentity *id;
995 struct GNUNET_HashCode *cid;
996 struct MeshPeerPath *path;
997 struct MeshPeer *dest_peer;
998 struct MeshPeer *orig_peer;
999 struct MeshConnection *c;
1000 unsigned int own_pos;
1004 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1005 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1008 size = ntohs (message->size);
1009 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1011 GNUNET_break_op (0);
1015 /* Calculate hops */
1016 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1017 if (size % sizeof (struct GNUNET_PeerIdentity))
1019 GNUNET_break_op (0);
1022 size /= sizeof (struct GNUNET_PeerIdentity);
1025 GNUNET_break_op (0);
1028 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1030 /* Get parameters */
1031 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1033 id = (struct GNUNET_PeerIdentity *) &msg[1];
1034 LOG (GNUNET_ERROR_TYPE_DEBUG,
1035 " connection %s (%s).\n",
1036 GNUNET_h2s (cid), GNUNET_i2s (id));
1038 /* Create connection */
1039 c = connection_get (cid);
1043 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1044 path = path_new (size);
1046 for (i = 0; i < size; i++)
1048 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1049 GNUNET_i2s (&id[i]));
1050 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1051 if (path->peers[i] == my_short_id)
1054 if (own_pos == 0 && path->peers[own_pos] != my_short_id)
1056 /* create path: self not found in path through self */
1057 GNUNET_break_op (0);
1058 path_destroy (path);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1063 GMP_add_path_to_all (path, GNUNET_NO);
1064 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1065 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1068 connection_reset_timeout (c, GNUNET_YES);
1074 if (MESH_CONNECTION_NEW == c->state)
1075 connection_change_state (c, MESH_CONNECTION_SENT);
1077 /* Remember peers */
1078 dest_peer = peer_get (&id[size - 1]);
1079 orig_peer = peer_get (&id[0]);
1081 /* Is it a connection to us? */
1082 if (c->own_pos == size - 1)
1084 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1085 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1087 if (NULL == orig_peer->tunnel)
1089 orig_peer->tunnel = tunnel_new ();
1090 orig_peer->tunnel->peer = orig_peer;
1092 GMT_add_connection (orig_peer->tunnel, c);
1093 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1094 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1096 send_connection_ack (c, GNUNET_NO);
1097 if (MESH_CONNECTION_SENT == c->state)
1098 connection_change_state (c, MESH_CONNECTION_ACK);
1100 /* Keep tunnel alive in direction dest->owner*/
1101 connection_reset_timeout (c, GNUNET_NO);
1105 /* It's for somebody else! Retransmit. */
1106 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1107 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1108 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1109 GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES);
1116 * Core handler for path confirmations.
1118 * @param cls closure
1119 * @param message message
1120 * @param peer peer identity this notification is about
1122 * @return GNUNET_OK to keep the connection open,
1123 * GNUNET_SYSERR to close it (signal serious error)
1126 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1127 const struct GNUNET_MessageHeader *message)
1129 struct GNUNET_MESH_ConnectionACK *msg;
1130 struct MeshConnection *c;
1131 struct MeshPeerPath *p;
1132 struct MeshPeer *pi;
1135 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1136 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1137 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1139 GNUNET_h2s (&msg->cid));
1140 c = connection_get (&msg->cid);
1143 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1150 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1152 pi = peer_get (peer);
1153 if (connection_get_next_hop (c) == pi)
1155 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1157 if (MESH_CONNECTION_SENT == c->state)
1158 connection_change_state (c, MESH_CONNECTION_ACK);
1160 else if (connection_get_prev_hop (c) == pi)
1162 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1164 connection_change_state (c, MESH_CONNECTION_READY);
1168 GNUNET_break_op (0);
1171 connection_reset_timeout (c, fwd);
1173 /* Add path to peers? */
1177 path_add_to_peers (p, GNUNET_YES);
1184 /* Message for us as creator? */
1185 if (GMC_is_origin (c, GNUNET_YES))
1187 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1188 connection_change_state (c, MESH_CONNECTION_READY);
1189 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1190 send_connection_ack (c, GNUNET_YES);
1191 GMT_send_queued_data (c->t, GNUNET_YES);
1195 /* Message for us as destination? */
1196 if (GMC_is_terminal (c, GNUNET_YES))
1198 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1199 GMC_change_state (c, MESH_CONNECTION_READY);
1200 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1201 GMT_send_queued_data (c->t, GNUNET_NO);
1205 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1206 GMC_send_prebuilt_message (message, c, NULL, fwd);
1212 * Core handler for notifications of broken paths
1214 * @param cls Closure (unused).
1215 * @param peer Peer identity of sending neighbor.
1216 * @param message Message.
1218 * @return GNUNET_OK to keep the connection open,
1219 * GNUNET_SYSERR to close it (signal serious error)
1222 GMC_handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
1223 const struct GNUNET_MessageHeader *message)
1225 struct GNUNET_MESH_ConnectionBroken *msg;
1226 struct MeshConnection *c;
1228 LOG (GNUNET_ERROR_TYPE_DEBUG,
1229 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
1230 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1231 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1232 GNUNET_i2s (&msg->peer1));
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1234 GNUNET_i2s (&msg->peer2));
1235 c = connection_get (&msg->cid);
1238 GNUNET_break_op (0);
1241 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
1242 GNUNET_PEER_search (&msg->peer2));
1249 * Core handler for tunnel destruction
1251 * @param cls Closure (unused).
1252 * @param peer Peer identity of sending neighbor.
1253 * @param message Message.
1255 * @return GNUNET_OK to keep the connection open,
1256 * GNUNET_SYSERR to close it (signal serious error)
1259 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1260 const struct GNUNET_MessageHeader *message)
1262 struct GNUNET_MESH_ConnectionDestroy *msg;
1263 struct MeshConnection *c;
1267 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1268 LOG (GNUNET_ERROR_TYPE_DEBUG,
1269 "Got a CONNECTION DESTROY message from %s\n",
1271 LOG (GNUNET_ERROR_TYPE_DEBUG,
1272 " for connection %s\n",
1273 GNUNET_h2s (&msg->cid));
1274 c = connection_get (&msg->cid);
1277 /* Probably already got the message from another path,
1278 * destroyed the tunnel and retransmitted to children.
1281 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1285 id = GNUNET_PEER_search (peer);
1286 if (id == GMP_get_short_id (connection_get_prev_hop (c)))
1288 else if (id == GMP_get_short_id (connection_get_next_hop (c)))
1292 GNUNET_break_op (0);
1295 GMC_send_prebuilt_message (message, c, NULL, fwd);
1296 c->destroy = GNUNET_YES;
1302 * Generic handler for mesh network encrypted traffic.
1304 * @param peer Peer identity this notification is about.
1305 * @param message Encrypted message.
1306 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1308 * @return GNUNET_OK to keep the connection open,
1309 * GNUNET_SYSERR to close it (signal serious error)
1312 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1313 const struct GNUNET_MESH_Encrypted *msg,
1316 struct MeshConnection *c;
1317 struct MeshTunnel3 *t;
1318 struct MeshPeer *neighbor;
1319 struct MeshFlowControl *fc;
1326 size = ntohs (msg->header.size);
1328 sizeof (struct GNUNET_MESH_Encrypted) +
1329 sizeof (struct GNUNET_MessageHeader))
1331 GNUNET_break_op (0);
1334 type = ntohs (msg->header.type);
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1337 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1339 /* Check connection */
1340 c = connection_get (&msg->cid);
1343 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1344 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1348 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1350 /* Check if origin is as expected */
1351 neighbor = connection_get_hop (c, !fwd);
1352 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1354 GNUNET_break_op (0);
1359 pid = ntohl (msg->pid);
1360 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1362 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1363 LOG (GNUNET_ERROR_TYPE_DEBUG,
1364 "WARNING Received PID %u, (prev %u), ACK %u\n",
1365 pid, fc->last_pid_recv, fc->last_ack_sent);
1368 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1370 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1371 LOG (GNUNET_ERROR_TYPE_DEBUG,
1372 " Pid %u not expected (%u+), dropping!\n",
1373 pid, fc->last_pid_recv + 1);
1376 if (MESH_CONNECTION_SENT == c->state)
1377 connection_change_state (c, MESH_CONNECTION_READY);
1378 connection_reset_timeout (c, fwd);
1379 fc->last_pid_recv = pid;
1381 /* Is this message for us? */
1382 if (GMC_is_terminal (c, fwd))
1384 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
1386 struct GNUNET_MessageHeader *msgh;
1389 /* TODO signature verification */
1390 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1391 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1393 fc->last_pid_recv = pid;
1394 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
1398 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1399 handle_decrypted (t, msgh, fwd);
1400 off += ntohs (msgh->size);
1402 send_ack (c, NULL, fwd);
1406 /* Message not for us: forward to next hop */
1407 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1408 ttl = ntohl (msg->ttl);
1409 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1412 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1413 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1414 send_ack (c, NULL, fwd);
1417 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1419 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1426 * Core handler for mesh network traffic going orig->dest.
1428 * @param cls Closure (unused).
1429 * @param message Message received.
1430 * @param peer Peer who sent the message.
1432 * @return GNUNET_OK to keep the connection open,
1433 * GNUNET_SYSERR to close it (signal serious error)
1436 GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1437 const struct GNUNET_MessageHeader *message)
1439 return handle_mesh_encrypted (peer,
1440 (struct GNUNET_MESH_Encrypted *)message,
1445 * Core handler for mesh network traffic going dest->orig.
1447 * @param cls Closure (unused).
1448 * @param message Message received.
1449 * @param peer Peer who sent the message.
1451 * @return GNUNET_OK to keep the connection open,
1452 * GNUNET_SYSERR to close it (signal serious error)
1455 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1456 const struct GNUNET_MessageHeader *message)
1458 return handle_mesh_encrypted (peer,
1459 (struct GNUNET_MESH_Encrypted *)message,
1465 * Core handler for mesh network traffic point-to-point acks.
1467 * @param cls closure
1468 * @param message message
1469 * @param peer peer identity this notification is about
1471 * @return GNUNET_OK to keep the connection open,
1472 * GNUNET_SYSERR to close it (signal serious error)
1475 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1476 const struct GNUNET_MessageHeader *message)
1478 struct GNUNET_MESH_ACK *msg;
1479 struct MeshConnection *c;
1480 struct MeshFlowControl *fc;
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1486 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1488 msg = (struct GNUNET_MESH_ACK *) message;
1490 c = connection_get (&msg->cid);
1494 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1499 /* Is this a forward or backward ACK? */
1500 id = GNUNET_PEER_search (peer);
1501 if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1503 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1507 else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1509 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1515 GNUNET_break_op (0);
1519 ack = ntohl (msg->ack);
1520 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1521 ack, fc->last_ack_recv);
1522 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1523 fc->last_ack_recv = ack;
1525 /* Cancel polling if the ACK is big enough. */
1526 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1527 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1529 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1530 GNUNET_SCHEDULER_cancel (fc->poll_task);
1531 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1532 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1535 connection_unlock_queue (c, fwd);
1542 * Core handler for mesh network traffic point-to-point ack polls.
1544 * @param cls closure
1545 * @param message message
1546 * @param peer peer identity this notification is about
1548 * @return GNUNET_OK to keep the connection open,
1549 * GNUNET_SYSERR to close it (signal serious error)
1552 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1553 const struct GNUNET_MessageHeader *message)
1555 struct GNUNET_MESH_Poll *msg;
1556 struct MeshConnection *c;
1557 struct MeshFlowControl *fc;
1562 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1563 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1566 msg = (struct GNUNET_MESH_Poll *) message;
1568 c = connection_get (&msg->cid);
1572 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1574 GNUNET_break_op (0);
1578 /* Is this a forward or backward ACK?
1579 * Note: a poll should never be needed in a loopback case,
1580 * since there is no possiblility of packet loss there, so
1581 * this way of discerining FWD/BCK should not be a problem.
1583 id = GNUNET_PEER_search (peer);
1584 if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1586 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1589 else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1596 GNUNET_break_op (0);
1600 pid = ntohl (msg->pid);
1601 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1602 pid, fc->last_pid_recv);
1603 fc->last_pid_recv = pid;
1604 fwd = fc == &c->fwd_fc;
1605 send_ack (c, NULL, fwd);
1612 * Core handler for mesh keepalives.
1614 * @param cls closure
1615 * @param message message
1616 * @param peer peer identity this notification is about
1617 * @return GNUNET_OK to keep the connection open,
1618 * GNUNET_SYSERR to close it (signal serious error)
1620 * TODO: Check who we got this from, to validate route.
1623 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1624 const struct GNUNET_MessageHeader *message)
1626 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1627 struct MeshConnection *c;
1628 struct MeshPeer *neighbor;
1631 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1632 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1635 c = connection_get (&msg->cid);
1638 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1643 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1644 GNUNET_YES : GNUNET_NO;
1646 /* Check if origin is as expected */
1647 neighbor = connection_get_hop (c, fwd);
1648 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1650 GNUNET_break_op (0);
1654 connection_change_state (c, MESH_CONNECTION_READY);
1655 connection_reset_timeout (c, fwd);
1657 if (GMC_is_terminal (c, fwd))
1660 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1661 GMC_send_prebuilt_message (message, c, NULL, fwd);
1668 * Send an ACK on the appropriate connection/channel, depending on
1669 * the direction and the position of the peer.
1671 * @param c Which connection to send the hop-by-hop ACK.
1672 * @param ch Channel, if any.
1673 * @param fwd Is this a fwd ACK? (will go dest->root)
1676 GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1678 unsigned int buffer;
1680 LOG (GNUNET_ERROR_TYPE_DEBUG,
1681 "send ack %s on %p %p\n",
1682 fwd ? "FWD" : "BCK", c, ch);
1683 if (NULL == c || GMC_is_terminal (c, fwd))
1685 struct MeshTunnel3 *t;
1686 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n");
1687 t = (NULL == c) ? GMCH_get_tunnel (ch) : GMC_get_tunnel (c);
1688 buffer = GMT_get_buffer (t, fwd);
1692 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1693 buffer = GMC_get_buffer (c, fwd);
1695 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1697 if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1698 (NULL != c && GMC_is_origin (c, fwd)) )
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1703 GNUNET_assert (NULL != ch);
1704 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1705 send_local_ack (ch, fwd);
1710 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
1711 GNUNET_assert (NULL != ch);
1712 channel_send_connections_ack (ch, buffer, fwd);
1716 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1717 connection_send_ack (c, buffer, fwd);
1723 * Initialize the connections subsystem
1725 * @param c Configuration handle.
1728 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1731 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1734 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1735 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1736 GNUNET_SCHEDULER_shutdown ();
1741 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1744 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1745 "MESH", "MAX_CONNECTIONS", "MISSING");
1746 GNUNET_SCHEDULER_shutdown ();
1751 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1752 &refresh_connection_time))
1754 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1755 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1756 GNUNET_SCHEDULER_shutdown ();
1759 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1763 * Shut down the connections subsystem.
1771 struct MeshConnection *
1772 GMC_new (const struct GNUNET_HashCode *cid,
1773 struct MeshTunnel3 *t,
1774 struct MeshPeerPath *p,
1775 unsigned int own_pos)
1777 struct MeshConnection *c;
1779 c = GNUNET_new (struct MeshConnection);
1781 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1782 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1783 fc_init (&c->fwd_fc);
1784 fc_init (&c->bck_fc);
1789 if (own_pos > p->length - 1)
1795 c->own_pos = own_pos;
1800 c->fwd_maintenance_task =
1801 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1802 &connection_fwd_keepalive, c);
1804 register_neighbors (c);
1810 GMC_destroy (struct MeshConnection *c)
1815 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n",
1816 GNUNET_h2s (&c->id));
1818 /* Cancel all traffic */
1819 connection_cancel_queues (c, GNUNET_YES);
1820 connection_cancel_queues (c, GNUNET_NO);
1822 /* Cancel maintainance task (keepalive/timeout) */
1823 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1824 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1825 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1826 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1828 /* Unregister from neighbors */
1829 unregister_neighbors (c);
1832 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1833 GMT_remove_connection (c->t, c);
1838 * Get the connection ID.
1840 * @param c Connection to get the ID from.
1842 * @return ID of the connection.
1844 const struct GNUNET_HashCode *
1845 GMC_get_id (const struct MeshConnection *c)
1852 * Get the connection state.
1854 * @param c Connection to get the state from.
1856 * @return state of the connection.
1858 enum MeshConnectionState
1859 GMC_get_state (const struct MeshConnection *c)
1865 * Get the connection tunnel.
1867 * @param c Connection to get the tunnel from.
1869 * @return tunnel of the connection.
1871 struct MeshTunnel3 *
1872 GMC_get_tunnel (const struct MeshConnection *c)
1879 * Get free buffer space in a connection.
1881 * @param c Connection.
1882 * @param fwd Is query about FWD traffic?
1884 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1887 GMC_get_buffer (struct MeshConnection *c, int fwd)
1889 struct MeshFlowControl *fc;
1891 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1893 return (fc->queue_max - fc->queue_n);
1897 * Get messages queued in a connection.
1899 * @param c Connection.
1900 * @param fwd Is query about FWD traffic?
1902 * @return Number of messages queued.
1905 GMC_get_qn (struct MeshConnection *c, int fwd)
1907 struct MeshFlowControl *fc;
1909 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1916 * Notify other peers on a connection of a broken link. Mark connections
1917 * to destroy after all traffic has been sent.
1919 * @param c Connection on which there has been a disconnection.
1920 * @param peer Peer that disconnected.
1921 * @param my_full_id My ID (to send to other peers).
1924 GMC_notify_broken (struct MeshConnection *c,
1925 struct MeshPeer *peer,
1926 struct GNUNET_PeerIdentity *my_full_id)
1928 struct GNUNET_MESH_ConnectionBroken msg;
1931 fwd = peer == connection_get_prev_hop (c);
1933 connection_cancel_queues (c, !fwd);
1934 if (GMC_is_terminal (c, fwd))
1936 /* Local shutdown, no one to notify about this. */
1941 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
1942 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
1944 msg.peer1 = *my_full_id;
1945 msg.peer2 = *GMP_get_id (peer);
1946 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
1947 c->destroy = GNUNET_YES;
1954 * Is this peer the first one on the connection?
1956 * @param c Connection.
1957 * @param fwd Is this about fwd traffic?
1959 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1962 GMC_is_origin (struct MeshConnection *c, int fwd)
1964 if (!fwd && c->path->length - 1 == c->own_pos )
1966 if (fwd && 0 == c->own_pos)
1973 * Is this peer the last one on the connection?
1975 * @param c Connection.
1976 * @param fwd Is this about fwd traffic?
1977 * Note that the ROOT is the terminal for BCK traffic!
1979 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1982 GMC_is_terminal (struct MeshConnection *c, int fwd)
1984 return GMC_is_origin (c, !fwd);
1989 * Sends an already built message on a connection, properly registering
1990 * all used resources.
1992 * @param message Message to send. Function makes a copy of it.
1993 * If message is not hop-by-hop, decrements TTL of copy.
1994 * @param c Connection on which this message is transmitted.
1995 * @param ch Channel on which this message is transmitted, or NULL.
1996 * @param fwd Is this a fwd message?
1999 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2000 struct MeshConnection *c,
2001 struct MeshChannel *ch,
2008 size = ntohs (message->size);
2009 data = GNUNET_malloc (size);
2010 memcpy (data, message, size);
2011 type = ntohs (message->type);
2012 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2013 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
2017 struct GNUNET_MESH_Encrypted *emsg;
2018 struct GNUNET_MESH_ACK *amsg;
2019 struct GNUNET_MESH_Poll *pmsg;
2020 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2021 struct GNUNET_MESH_ConnectionBroken *bmsg;
2024 case GNUNET_MESSAGE_TYPE_MESH_FWD:
2025 case GNUNET_MESSAGE_TYPE_MESH_BCK:
2026 emsg = (struct GNUNET_MESH_Encrypted *) data;
2027 ttl = ntohl (emsg->ttl);
2030 GNUNET_break_op (0);
2034 emsg->ttl = htonl (ttl - 1);
2035 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2036 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
2039 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2040 amsg = (struct GNUNET_MESH_ACK *) data;
2042 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2045 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2046 pmsg = (struct GNUNET_MESH_Poll *) data;
2048 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2049 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2052 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2053 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2058 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2059 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2064 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2065 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2072 GMP_queue_add (data, type, size, c, ch, fwd, &message_sent, (void *) size);
2077 * Send a message to all peers in this connection that the connection
2078 * is no longer valid.
2080 * If some peer should not receive the message, it should be zero'ed out
2081 * before calling this function.
2083 * @param c The connection whose peers to notify.
2086 GMC_send_destroy (struct MeshConnection *c)
2088 struct GNUNET_MESH_ConnectionDestroy msg;
2090 if (GNUNET_YES == c->destroy)
2093 msg.header.size = htons (sizeof (msg));
2094 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2096 LOG (GNUNET_ERROR_TYPE_DEBUG,
2097 " sending connection destroy for connection %s\n",
2098 GNUNET_h2s (&c->id));
2100 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2101 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2102 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2103 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2104 c->destroy = GNUNET_YES;