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 type Type of message sent.
357 * @param fwd Was this a FWD going message?
358 * @param size Size of the message.
359 * @param wait Time spent waiting for core (only the time for THIS message)
362 message_sent (void *cls,
363 struct MeshConnection *c, uint16_t type,
364 int fwd, size_t size,
365 struct GNUNET_TIME_Relative wait)
367 struct MeshConnectionPerformance *p;
368 struct MeshFlowControl *fc;
372 return; /* Only endpoints are interested in this. */
374 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
376 usecsperbyte = ((double) wait.rel_value_us) / size;
377 if (p->size == AVG_MSGS)
379 /* Array is full. Substract oldest value, add new one and store. */
380 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
381 p->usecsperbyte[p->idx] = usecsperbyte;
382 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
386 /* Array not yet full. Add current value to avg and store. */
387 p->usecsperbyte[p->idx] = usecsperbyte;
389 p->avg += p->usecsperbyte[p->idx];
393 p->idx = (p->idx + 1) % AVG_MSGS;
395 fc = fwd ? &c->fwd_fc : &c->bck_fc;
396 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
398 c->pending_messages--;
399 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
401 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
408 * Get the previous hop in a connection
410 * @param c Connection.
412 * @return Previous peer in the connection.
414 static struct MeshPeer *
415 get_prev_hop (struct MeshConnection *c)
419 if (0 == c->own_pos || c->path->length < 2)
420 id = c->path->peers[0];
422 id = c->path->peers[c->own_pos - 1];
424 return GMP_get_short (id);
429 * Get the next hop in a connection
431 * @param c Connection.
433 * @return Next peer in the connection.
435 static struct MeshPeer *
436 get_next_hop (struct MeshConnection *c)
440 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
441 id = c->path->peers[c->path->length - 1];
443 id = c->path->peers[c->own_pos + 1];
445 return GMP_get_short (id);
450 * Get the hop in a connection.
452 * @param c Connection.
453 * @param fwd Next hop?
455 * @return Next peer in the connection.
457 static struct MeshPeer *
458 get_hop (struct MeshConnection *c, int fwd)
461 return get_next_hop (c);
462 return get_prev_hop (c);
467 * Send an ACK informing the predecessor about the available buffer space.
469 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
470 * the ACK itself goes "back" (dest->root).
472 * @param c Connection on which to send the ACK.
473 * @param buffer How much space free to advertise?
474 * @param fwd Is this FWD ACK? (Going dest->owner)
477 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
479 struct MeshFlowControl *next_fc;
480 struct MeshFlowControl *prev_fc;
481 struct GNUNET_MESH_ACK msg;
485 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
486 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
488 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 "connection send %s ack on %s\n",
490 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
492 /* Check if we need to transmit the ACK */
493 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
495 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
496 LOG (GNUNET_ERROR_TYPE_DEBUG,
497 " last pid recv: %u, last ack sent: %u\n",
498 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
502 /* Ok, ACK might be necessary, what PID to ACK? */
503 delta = next_fc->queue_max - next_fc->queue_n;
504 ack = prev_fc->last_pid_recv + delta;
505 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
506 LOG (GNUNET_ERROR_TYPE_DEBUG,
507 " last pid %u, last ack %u, qmax %u, q %u\n",
508 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
509 next_fc->queue_max, next_fc->queue_n);
510 if (ack == prev_fc->last_ack_sent)
512 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
516 prev_fc->last_ack_sent = ack;
518 /* Build ACK message and send on connection */
519 msg.header.size = htons (sizeof (msg));
520 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
521 msg.ack = htonl (ack);
524 GMC_send_prebuilt_message (&msg.header, c, NULL, !fwd);
529 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
530 * or a first CONNECTION_ACK directed to us.
532 * @param connection Connection to confirm.
533 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
536 send_connection_ack (struct MeshConnection *connection, int fwd)
538 struct MeshTunnel3 *t;
541 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
542 GMP_queue_add (get_hop (connection, fwd), NULL,
543 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
544 sizeof (struct GNUNET_MESH_ConnectionACK),
545 connection, NULL, fwd,
546 &message_sent, NULL);
547 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
548 GMT_change_state (t, MESH_TUNNEL3_WAITING);
549 if (MESH_CONNECTION_READY != connection->state)
550 GMC_change_state (connection, MESH_CONNECTION_SENT);
555 * Send keepalive packets for a connection.
557 * @param c Connection to keep alive..
558 * @param fwd Is this a FWD keepalive? (owner -> dest).
561 connection_keepalive (struct MeshConnection *c, int fwd)
563 struct GNUNET_MESH_ConnectionKeepAlive *msg;
564 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
568 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
569 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
571 LOG (GNUNET_ERROR_TYPE_DEBUG,
572 "sending %s keepalive for connection %s[%d]\n",
573 fwd ? "FWD" : "BCK", GMT_2s (c->t), c->id);
575 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
576 msg->header.size = htons (size);
577 msg->header.type = htons (type);
580 GMC_send_prebuilt_message (&msg->header, c, NULL, fwd);
585 * Send CONNECTION_{CREATE/ACK} packets for a connection.
587 * @param c Connection for which to send the message.
588 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
591 connection_recreate (struct MeshConnection *c, int fwd)
593 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
595 send_connection_create (c);
597 send_connection_ack (c, GNUNET_NO);
602 * Generic connection timer management.
603 * Depending on the role of the peer in the connection will send the
604 * appropriate message (build or keepalive)
606 * @param c Conncetion to maintain.
610 connection_maintain (struct MeshConnection *c, int fwd)
612 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
614 /* TODO DHT GET with RO_BART */
619 case MESH_CONNECTION_NEW:
621 case MESH_CONNECTION_SENT:
622 connection_recreate (c, fwd);
624 case MESH_CONNECTION_READY:
625 connection_keepalive (c, fwd);
634 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
636 struct MeshConnection *c = cls;
638 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
639 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
642 connection_maintain (c, GNUNET_YES);
643 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
644 &connection_fwd_keepalive,
650 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
652 struct MeshConnection *c = cls;
654 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
655 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
658 connection_maintain (c, GNUNET_NO);
659 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
660 &connection_bck_keepalive,
666 * @brief Re-initiate traffic on this connection if necessary.
668 * Check if there is traffic queued towards this peer
669 * and the core transmit handle is NULL (traffic was stalled).
670 * If so, call core tmt rdy.
672 * @param c Connection on which initiate traffic.
673 * @param fwd Is this about fwd traffic?
676 connection_unlock_queue (struct MeshConnection *c, int fwd)
678 struct MeshPeer *peer;
680 LOG (GNUNET_ERROR_TYPE_DEBUG,
681 "connection_unlock_queue %s on %s\n",
682 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
684 if (GMC_is_terminal (c, fwd))
686 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
690 peer = get_hop (c, fwd);
691 GMP_queue_unlock (peer, c);
696 * Cancel all transmissions that belong to a certain connection.
698 * @param c Connection which to cancel.
699 * @param fwd Cancel fwd traffic?
702 connection_cancel_queues (struct MeshConnection *c, int fwd)
705 struct MeshFlowControl *fc;
706 struct MeshPeer *peer;
714 peer = get_hop (c, fwd);
715 GMP_queue_cancel (peer, c);
717 fc = fwd ? &c->fwd_fc : &c->bck_fc;
718 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
720 GNUNET_SCHEDULER_cancel (fc->poll_task);
721 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
727 * Function called if a connection has been stalled for a while,
728 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
730 * @param cls Closure (poll ctx).
731 * @param tc TaskContext.
734 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
736 struct MeshFlowControl *fc = cls;
737 struct GNUNET_MESH_Poll msg;
738 struct MeshConnection *c;
740 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
741 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
747 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
748 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
749 GNUNET_h2s (&c->id));
750 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
751 fc == &c->fwd_fc ? "FWD" : "BCK");
753 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
754 msg.header.size = htons (sizeof (msg));
755 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
756 GMC_send_prebuilt_message (&msg.header, c, NULL, fc == &c->fwd_fc);
757 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
758 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
759 &connection_poll, fc);
764 * Timeout function due to lack of keepalive/traffic from the owner.
765 * Destroys connection if called.
767 * @param cls Closure (connection to destroy).
768 * @param tc TaskContext.
771 connection_fwd_timeout (void *cls,
772 const struct GNUNET_SCHEDULER_TaskContext *tc)
774 struct MeshConnection *c = cls;
776 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
777 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
779 LOG (GNUNET_ERROR_TYPE_DEBUG,
780 "Connection %s[%X] FWD timed out. Destroying.\n",
784 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
792 * Timeout function due to lack of keepalive/traffic from the destination.
793 * Destroys connection if called.
795 * @param cls Closure (connection to destroy).
796 * @param tc TaskContext
799 connection_bck_timeout (void *cls,
800 const struct GNUNET_SCHEDULER_TaskContext *tc)
802 struct MeshConnection *c = cls;
804 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
805 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
808 LOG (GNUNET_ERROR_TYPE_DEBUG,
809 "Connection %s[%X] FWD timed out. Destroying.\n",
810 GMT_2s (c->t), c->id);
812 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
820 * Resets the connection timeout task, some other message has done the
822 * - For the first peer on the direction this means to send
823 * a keepalive or a path confirmation message (either create or ACK).
824 * - For all other peers, this means to destroy the connection,
825 * due to lack of activity.
826 * Starts the tiemout if no timeout was running (connection just created).
828 * @param c Connection whose timeout to reset.
829 * @param fwd Is this forward?
831 * TODO use heap to improve efficiency of scheduler.
834 connection_reset_timeout (struct MeshConnection *c, int fwd)
836 GNUNET_SCHEDULER_TaskIdentifier *ti;
837 GNUNET_SCHEDULER_Task f;
839 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
841 if (GNUNET_SCHEDULER_NO_TASK != *ti)
842 GNUNET_SCHEDULER_cancel (*ti);
844 if (GMC_is_origin (c, fwd)) /* Endpoint */
846 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
847 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
851 struct GNUNET_TIME_Relative delay;
853 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
854 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
855 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
861 * Add the connection to the list of both neighbors.
863 * @param c Connection.
866 register_neighbors (struct MeshConnection *c)
868 struct MeshPeer *peer;
870 peer = get_next_hop (c);
871 if (GNUNET_NO == GMP_is_neighbor (peer))
876 GMP_add_connection (peer, c);
877 peer = get_prev_hop (c);
878 if (GNUNET_NO == GMP_is_neighbor (peer))
883 GMP_add_connection (peer, c);
888 * Remove the connection from the list of both neighbors.
890 * @param c Connection.
893 unregister_neighbors (struct MeshConnection *c)
895 struct MeshPeer *peer;
897 peer = get_next_hop (c);
898 GMP_remove_connection (peer, c);
900 peer = get_prev_hop (c);
901 GMP_remove_connection (peer, c);
906 /******************************************************************************/
907 /******************************** API ***********************************/
908 /******************************************************************************/
911 * Core handler for connection creation.
913 * @param cls Closure (unused).
914 * @param peer Sender (neighbor).
915 * @param message Message.
917 * @return GNUNET_OK to keep the connection open,
918 * GNUNET_SYSERR to close it (signal serious error)
921 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
922 const struct GNUNET_MessageHeader *message)
924 struct GNUNET_MESH_ConnectionCreate *msg;
925 struct GNUNET_PeerIdentity *id;
926 struct GNUNET_HashCode *cid;
927 struct MeshPeerPath *path;
928 struct MeshPeer *dest_peer;
929 struct MeshPeer *orig_peer;
930 struct MeshConnection *c;
931 unsigned int own_pos;
935 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
936 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
939 size = ntohs (message->size);
940 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
947 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
948 if (size % sizeof (struct GNUNET_PeerIdentity))
953 size /= sizeof (struct GNUNET_PeerIdentity);
959 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
962 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
964 id = (struct GNUNET_PeerIdentity *) &msg[1];
965 LOG (GNUNET_ERROR_TYPE_DEBUG,
966 " connection %s (%s).\n",
967 GNUNET_h2s (cid), GNUNET_i2s (id));
969 /* Create connection */
970 c = connection_get (cid);
974 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
975 path = path_new (size);
977 for (i = 0; i < size; i++)
979 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
980 GNUNET_i2s (&id[i]));
981 path->peers[i] = GNUNET_PEER_intern (&id[i]);
982 if (path->peers[i] == myid)
985 if (own_pos == 0 && path->peers[own_pos] != myid)
987 /* create path: self not found in path through self */
993 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
994 GMP_add_path_to_all (path, GNUNET_NO);
995 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
996 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
999 connection_reset_timeout (c, GNUNET_YES);
1005 if (MESH_CONNECTION_NEW == c->state)
1006 connection_change_state (c, MESH_CONNECTION_SENT);
1008 /* Remember peers */
1009 dest_peer = GMP_get (&id[size - 1]);
1010 orig_peer = GMP_get (&id[0]);
1012 /* Is it a connection to us? */
1013 if (c->own_pos == size - 1)
1015 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1016 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1018 GMP_add_tunnel (orig_peer);
1019 GMP_add_connection (orig_peer, c);
1020 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1021 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1023 send_connection_ack (c, GNUNET_NO);
1024 if (MESH_CONNECTION_SENT == c->state)
1025 connection_change_state (c, MESH_CONNECTION_ACK);
1027 /* Keep tunnel alive in direction dest->owner*/
1028 connection_reset_timeout (c, GNUNET_NO);
1032 /* It's for somebody else! Retransmit. */
1033 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1034 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1035 GMP_add_path_to_origin (orig_peer, path, GNUNET_NO);
1036 GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES);
1043 * Core handler for path confirmations.
1045 * @param cls closure
1046 * @param message message
1047 * @param peer peer identity this notification is about
1049 * @return GNUNET_OK to keep the connection open,
1050 * GNUNET_SYSERR to close it (signal serious error)
1053 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1054 const struct GNUNET_MessageHeader *message)
1056 struct GNUNET_MESH_ConnectionACK *msg;
1057 struct MeshConnection *c;
1058 struct MeshPeerPath *p;
1059 struct MeshPeer *pi;
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1063 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1064 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1065 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1066 GNUNET_h2s (&msg->cid));
1067 c = connection_get (&msg->cid);
1070 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1077 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1079 pi = peer_get (peer);
1080 if (get_next_hop (c) == pi)
1082 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1084 if (MESH_CONNECTION_SENT == c->state)
1085 connection_change_state (c, MESH_CONNECTION_ACK);
1087 else if (get_prev_hop (c) == pi)
1089 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1091 connection_change_state (c, MESH_CONNECTION_READY);
1095 GNUNET_break_op (0);
1098 connection_reset_timeout (c, fwd);
1100 /* Add path to peers? */
1104 path_add_to_peers (p, GNUNET_YES);
1111 /* Message for us as creator? */
1112 if (GMC_is_origin (c, GNUNET_YES))
1114 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1115 connection_change_state (c, MESH_CONNECTION_READY);
1116 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1117 send_connection_ack (c, GNUNET_YES);
1118 GMT_send_queued_data (c->t, GNUNET_YES);
1122 /* Message for us as destination? */
1123 if (GMC_is_terminal (c, GNUNET_YES))
1125 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1126 GMC_change_state (c, MESH_CONNECTION_READY);
1127 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1128 GMT_send_queued_data (c->t, GNUNET_NO);
1132 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1133 GMC_send_prebuilt_message (message, c, NULL, fwd);
1139 * Core handler for notifications of broken paths
1141 * @param cls Closure (unused).
1142 * @param peer Peer identity of sending neighbor.
1143 * @param message Message.
1145 * @return GNUNET_OK to keep the connection open,
1146 * GNUNET_SYSERR to close it (signal serious error)
1149 GMC_handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
1150 const struct GNUNET_MessageHeader *message)
1152 struct GNUNET_MESH_ConnectionBroken *msg;
1153 struct MeshConnection *c;
1155 LOG (GNUNET_ERROR_TYPE_DEBUG,
1156 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
1157 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1158 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1159 GNUNET_i2s (&msg->peer1));
1160 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1161 GNUNET_i2s (&msg->peer2));
1162 c = connection_get (&msg->cid);
1165 GNUNET_break_op (0);
1168 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
1169 GNUNET_PEER_search (&msg->peer2));
1176 * Core handler for tunnel destruction
1178 * @param cls Closure (unused).
1179 * @param peer Peer identity of sending neighbor.
1180 * @param message Message.
1182 * @return GNUNET_OK to keep the connection open,
1183 * GNUNET_SYSERR to close it (signal serious error)
1186 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1187 const struct GNUNET_MessageHeader *message)
1189 struct GNUNET_MESH_ConnectionDestroy *msg;
1190 struct MeshConnection *c;
1194 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1195 LOG (GNUNET_ERROR_TYPE_DEBUG,
1196 "Got a CONNECTION DESTROY message from %s\n",
1198 LOG (GNUNET_ERROR_TYPE_DEBUG,
1199 " for connection %s\n",
1200 GNUNET_h2s (&msg->cid));
1201 c = connection_get (&msg->cid);
1204 /* Probably already got the message from another path,
1205 * destroyed the tunnel and retransmitted to children.
1208 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1212 id = GNUNET_PEER_search (peer);
1213 if (id == GMP_get_short_id (get_prev_hop (c)))
1215 else if (id == GMP_get_short_id (get_next_hop (c)))
1219 GNUNET_break_op (0);
1222 GMC_send_prebuilt_message (message, c, NULL, fwd);
1223 c->destroy = GNUNET_YES;
1229 * Generic handler for mesh network encrypted traffic.
1231 * @param peer Peer identity this notification is about.
1232 * @param message Encrypted message.
1233 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1235 * @return GNUNET_OK to keep the connection open,
1236 * GNUNET_SYSERR to close it (signal serious error)
1239 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1240 const struct GNUNET_MESH_Encrypted *msg,
1243 struct MeshConnection *c;
1244 struct MeshTunnel3 *t;
1245 struct MeshPeer *neighbor;
1246 struct MeshFlowControl *fc;
1253 size = ntohs (msg->header.size);
1255 sizeof (struct GNUNET_MESH_Encrypted) +
1256 sizeof (struct GNUNET_MessageHeader))
1258 GNUNET_break_op (0);
1261 type = ntohs (msg->header.type);
1262 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1263 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1264 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1266 /* Check connection */
1267 c = connection_get (&msg->cid);
1270 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1271 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1275 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1277 /* Check if origin is as expected */
1278 neighbor = get_hop (c, !fwd);
1279 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1281 GNUNET_break_op (0);
1286 pid = ntohl (msg->pid);
1287 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1289 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1290 LOG (GNUNET_ERROR_TYPE_DEBUG,
1291 "WARNING Received PID %u, (prev %u), ACK %u\n",
1292 pid, fc->last_pid_recv, fc->last_ack_sent);
1295 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1297 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1298 LOG (GNUNET_ERROR_TYPE_DEBUG,
1299 " Pid %u not expected (%u+), dropping!\n",
1300 pid, fc->last_pid_recv + 1);
1303 if (MESH_CONNECTION_SENT == c->state)
1304 connection_change_state (c, MESH_CONNECTION_READY);
1305 connection_reset_timeout (c, fwd);
1306 fc->last_pid_recv = pid;
1308 /* Is this message for us? */
1309 if (GMC_is_terminal (c, fwd))
1311 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
1313 struct GNUNET_MessageHeader *msgh;
1316 /* TODO signature verification */
1317 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1318 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1320 fc->last_pid_recv = pid;
1321 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
1325 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1326 handle_decrypted (t, msgh, fwd);
1327 off += ntohs (msgh->size);
1329 send_ack (c, NULL, fwd);
1333 /* Message not for us: forward to next hop */
1334 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1335 ttl = ntohl (msg->ttl);
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1339 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1340 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1341 send_ack (c, NULL, fwd);
1344 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1346 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1353 * Core handler for mesh network traffic going orig->dest.
1355 * @param cls Closure (unused).
1356 * @param message Message received.
1357 * @param peer Peer who sent the message.
1359 * @return GNUNET_OK to keep the connection open,
1360 * GNUNET_SYSERR to close it (signal serious error)
1363 GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1364 const struct GNUNET_MessageHeader *message)
1366 return handle_mesh_encrypted (peer,
1367 (struct GNUNET_MESH_Encrypted *)message,
1372 * Core handler for mesh network traffic going dest->orig.
1374 * @param cls Closure (unused).
1375 * @param message Message received.
1376 * @param peer Peer who sent the message.
1378 * @return GNUNET_OK to keep the connection open,
1379 * GNUNET_SYSERR to close it (signal serious error)
1382 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1383 const struct GNUNET_MessageHeader *message)
1385 return handle_mesh_encrypted (peer,
1386 (struct GNUNET_MESH_Encrypted *)message,
1392 * Core handler for mesh network traffic point-to-point acks.
1394 * @param cls closure
1395 * @param message message
1396 * @param peer peer identity this notification is about
1398 * @return GNUNET_OK to keep the connection open,
1399 * GNUNET_SYSERR to close it (signal serious error)
1402 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1403 const struct GNUNET_MessageHeader *message)
1405 struct GNUNET_MESH_ACK *msg;
1406 struct MeshConnection *c;
1407 struct MeshFlowControl *fc;
1412 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1413 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1415 msg = (struct GNUNET_MESH_ACK *) message;
1417 c = connection_get (&msg->cid);
1421 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1426 /* Is this a forward or backward ACK? */
1427 id = GNUNET_PEER_search (peer);
1428 if (GMP_get_short_id (get_next_hop (c)) == id)
1430 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1434 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1442 GNUNET_break_op (0);
1446 ack = ntohl (msg->ack);
1447 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1448 ack, fc->last_ack_recv);
1449 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1450 fc->last_ack_recv = ack;
1452 /* Cancel polling if the ACK is big enough. */
1453 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1454 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1456 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1457 GNUNET_SCHEDULER_cancel (fc->poll_task);
1458 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1459 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1462 connection_unlock_queue (c, fwd);
1469 * Core handler for mesh network traffic point-to-point ack polls.
1471 * @param cls closure
1472 * @param message message
1473 * @param peer peer identity this notification is about
1475 * @return GNUNET_OK to keep the connection open,
1476 * GNUNET_SYSERR to close it (signal serious error)
1479 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1480 const struct GNUNET_MessageHeader *message)
1482 struct GNUNET_MESH_Poll *msg;
1483 struct MeshConnection *c;
1484 struct MeshFlowControl *fc;
1489 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1490 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1493 msg = (struct GNUNET_MESH_Poll *) message;
1495 c = connection_get (&msg->cid);
1499 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1501 GNUNET_break_op (0);
1505 /* Is this a forward or backward ACK?
1506 * Note: a poll should never be needed in a loopback case,
1507 * since there is no possiblility of packet loss there, so
1508 * this way of discerining FWD/BCK should not be a problem.
1510 id = GNUNET_PEER_search (peer);
1511 if (GMP_get_short_id (get_next_hop (c)) == id)
1513 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1516 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1518 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1523 GNUNET_break_op (0);
1527 pid = ntohl (msg->pid);
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
1529 pid, fc->last_pid_recv);
1530 fc->last_pid_recv = pid;
1531 fwd = fc == &c->fwd_fc;
1532 send_ack (c, NULL, fwd);
1539 * Core handler for mesh keepalives.
1541 * @param cls closure
1542 * @param message message
1543 * @param peer peer identity this notification is about
1544 * @return GNUNET_OK to keep the connection open,
1545 * GNUNET_SYSERR to close it (signal serious error)
1547 * TODO: Check who we got this from, to validate route.
1550 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1551 const struct GNUNET_MessageHeader *message)
1553 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1554 struct MeshConnection *c;
1555 struct MeshPeer *neighbor;
1558 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1559 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1562 c = connection_get (&msg->cid);
1565 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1570 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1571 GNUNET_YES : GNUNET_NO;
1573 /* Check if origin is as expected */
1574 neighbor = get_hop (c, fwd);
1575 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1577 GNUNET_break_op (0);
1581 connection_change_state (c, MESH_CONNECTION_READY);
1582 connection_reset_timeout (c, fwd);
1584 if (GMC_is_terminal (c, fwd))
1587 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1588 GMC_send_prebuilt_message (message, c, NULL, fwd);
1595 * Send an ACK on the appropriate connection/channel, depending on
1596 * the direction and the position of the peer.
1598 * @param c Which connection to send the hop-by-hop ACK.
1599 * @param ch Channel, if any.
1600 * @param fwd Is this a fwd ACK? (will go dest->root)
1603 GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1605 unsigned int buffer;
1607 LOG (GNUNET_ERROR_TYPE_DEBUG,
1608 "send ack %s on %p %p\n",
1609 fwd ? "FWD" : "BCK", c, ch);
1610 if (NULL == c || GMC_is_terminal (c, fwd))
1612 struct MeshTunnel3 *t;
1613 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n");
1614 t = (NULL == c) ? GMCH_get_tunnel (ch) : GMC_get_tunnel (c);
1615 buffer = GMT_get_buffer (t, fwd);
1619 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
1620 buffer = GMC_get_buffer (c, fwd);
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
1624 if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1625 (NULL != c && GMC_is_origin (c, fwd)) )
1627 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1630 GNUNET_assert (NULL != ch);
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
1632 send_local_ack (ch, fwd);
1637 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
1638 GNUNET_assert (NULL != ch);
1639 channel_send_connections_ack (ch, buffer, fwd);
1643 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
1644 connection_send_ack (c, buffer, fwd);
1650 * Initialize the connections subsystem
1652 * @param c Configuration handle.
1655 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1658 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1661 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1662 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1663 GNUNET_SCHEDULER_shutdown ();
1668 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1671 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1672 "MESH", "MAX_CONNECTIONS", "MISSING");
1673 GNUNET_SCHEDULER_shutdown ();
1678 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1679 &refresh_connection_time))
1681 LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1682 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1683 GNUNET_SCHEDULER_shutdown ();
1686 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1690 * Shut down the connections subsystem.
1698 struct MeshConnection *
1699 GMC_new (const struct GNUNET_HashCode *cid,
1700 struct MeshTunnel3 *t,
1701 struct MeshPeerPath *p,
1702 unsigned int own_pos)
1704 struct MeshConnection *c;
1706 c = GNUNET_new (struct MeshConnection);
1708 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1709 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1710 fc_init (&c->fwd_fc);
1711 fc_init (&c->bck_fc);
1716 if (own_pos > p->length - 1)
1722 c->own_pos = own_pos;
1727 c->fwd_maintenance_task =
1728 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1729 &connection_fwd_keepalive, c);
1731 register_neighbors (c);
1737 GMC_destroy (struct MeshConnection *c)
1742 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n",
1743 GNUNET_h2s (&c->id));
1745 /* Cancel all traffic */
1746 connection_cancel_queues (c, GNUNET_YES);
1747 connection_cancel_queues (c, GNUNET_NO);
1749 /* Cancel maintainance task (keepalive/timeout) */
1750 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1751 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1752 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1753 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1755 /* Unregister from neighbors */
1756 unregister_neighbors (c);
1759 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1760 GMT_remove_connection (c->t, c);
1765 * Get the connection ID.
1767 * @param c Connection to get the ID from.
1769 * @return ID of the connection.
1771 const struct GNUNET_HashCode *
1772 GMC_get_id (const struct MeshConnection *c)
1779 * Get the connection state.
1781 * @param c Connection to get the state from.
1783 * @return state of the connection.
1785 enum MeshConnectionState
1786 GMC_get_state (const struct MeshConnection *c)
1792 * Get the connection tunnel.
1794 * @param c Connection to get the tunnel from.
1796 * @return tunnel of the connection.
1798 struct MeshTunnel3 *
1799 GMC_get_tunnel (const struct MeshConnection *c)
1806 * Get free buffer space in a connection.
1808 * @param c Connection.
1809 * @param fwd Is query about FWD traffic?
1811 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1814 GMC_get_buffer (struct MeshConnection *c, int fwd)
1816 struct MeshFlowControl *fc;
1818 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1820 return (fc->queue_max - fc->queue_n);
1824 * Get messages queued in a connection.
1826 * @param c Connection.
1827 * @param fwd Is query about FWD traffic?
1829 * @return Number of messages queued.
1832 GMC_get_qn (struct MeshConnection *c, int fwd)
1834 struct MeshFlowControl *fc;
1836 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1843 * Notify other peers on a connection of a broken link. Mark connections
1844 * to destroy after all traffic has been sent.
1846 * @param c Connection on which there has been a disconnection.
1847 * @param peer Peer that disconnected.
1848 * @param my_full_id My ID (to send to other peers).
1851 GMC_notify_broken (struct MeshConnection *c,
1852 struct MeshPeer *peer,
1853 struct GNUNET_PeerIdentity *my_full_id)
1855 struct GNUNET_MESH_ConnectionBroken msg;
1858 fwd = peer == get_prev_hop (c);
1860 connection_cancel_queues (c, !fwd);
1861 if (GMC_is_terminal (c, fwd))
1863 /* Local shutdown, no one to notify about this. */
1868 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
1869 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
1871 msg.peer1 = *my_full_id;
1872 msg.peer2 = *GMP_get_id (peer);
1873 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
1874 c->destroy = GNUNET_YES;
1881 * Is this peer the first one on the connection?
1883 * @param c Connection.
1884 * @param fwd Is this about fwd traffic?
1886 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1889 GMC_is_origin (struct MeshConnection *c, int fwd)
1891 if (!fwd && c->path->length - 1 == c->own_pos )
1893 if (fwd && 0 == c->own_pos)
1900 * Is this peer the last one on the connection?
1902 * @param c Connection.
1903 * @param fwd Is this about fwd traffic?
1904 * Note that the ROOT is the terminal for BCK traffic!
1906 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1909 GMC_is_terminal (struct MeshConnection *c, int fwd)
1911 return GMC_is_origin (c, !fwd);
1916 * See if we are allowed to send by the next hop in the given direction.
1918 * @param c Connection.
1919 * @param fwd Is this about fwd traffic?
1921 * @return GNUNET_YES in case it's OK.
1924 GMC_is_sendable (struct MeshConnection *c, int fwd)
1926 struct MeshFlowControl *fc;
1928 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1929 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1935 * Sends an already built message on a connection, properly registering
1936 * all used resources.
1938 * @param message Message to send. Function makes a copy of it.
1939 * If message is not hop-by-hop, decrements TTL of copy.
1940 * @param c Connection on which this message is transmitted.
1941 * @param ch Channel on which this message is transmitted, or NULL.
1942 * @param fwd Is this a fwd message?
1945 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1946 struct MeshConnection *c,
1947 struct MeshChannel *ch,
1950 struct MeshFlowControl *fc;
1956 size = ntohs (message->size);
1957 data = GNUNET_malloc (size);
1958 memcpy (data, message, size);
1959 type = ntohs (message->type);
1960 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
1961 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1963 droppable = GNUNET_YES;
1966 struct GNUNET_MESH_Encrypted *emsg;
1967 struct GNUNET_MESH_ACK *amsg;
1968 struct GNUNET_MESH_Poll *pmsg;
1969 struct GNUNET_MESH_ConnectionDestroy *dmsg;
1970 struct GNUNET_MESH_ConnectionBroken *bmsg;
1973 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1974 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1975 emsg = (struct GNUNET_MESH_Encrypted *) data;
1976 ttl = ntohl (emsg->ttl);
1979 GNUNET_break_op (0);
1983 emsg->ttl = htonl (ttl - 1);
1984 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1985 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1988 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1989 amsg = (struct GNUNET_MESH_ACK *) data;
1991 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1992 droppable = GNUNET_NO;
1995 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1996 pmsg = (struct GNUNET_MESH_Poll *) data;
1998 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1999 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2000 droppable = GNUNET_NO;
2003 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2004 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2009 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2010 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2015 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2016 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2023 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2024 if (fc->queue_n >= fc->queue_max && droppable)
2026 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2029 LOG (GNUNET_ERROR_TYPE_DEBUG,
2030 "queue full: %u/%u\n",
2031 fc->queue_n, fc->queue_max);
2032 return; /* Drop this message */
2035 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
2036 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
2037 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2038 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2040 GMC_start_poll (c, fwd);
2043 c->pending_messages++;
2045 GMP_queue_add (get_hop (c, fwd), data, type, size, c, ch, fwd,
2046 &message_sent, NULL);
2051 * Sends a CREATE CONNECTION message for a path to a peer.
2052 * Changes the connection and tunnel states if necessary.
2054 * @param connection Connection to create.
2057 GMC_send_create (struct MeshConnection *connection)
2059 enum MeshTunnel3State state;
2062 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2063 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2064 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2065 GMP_queue_add (get_next_hop (connection), NULL,
2066 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2067 size, connection, NULL,
2068 GNUNET_YES, &message_sent, NULL);
2069 state = GMT_get_state (connection->t);
2070 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2071 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2072 if (MESH_CONNECTION_NEW == connection->state)
2073 GMC_change_state (connection, MESH_CONNECTION_SENT);
2078 * Send a message to all peers in this connection that the connection
2079 * is no longer valid.
2081 * If some peer should not receive the message, it should be zero'ed out
2082 * before calling this function.
2084 * @param c The connection whose peers to notify.
2087 GMC_send_destroy (struct MeshConnection *c)
2089 struct GNUNET_MESH_ConnectionDestroy msg;
2091 if (GNUNET_YES == c->destroy)
2094 msg.header.size = htons (sizeof (msg));
2095 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2097 LOG (GNUNET_ERROR_TYPE_DEBUG,
2098 " sending connection destroy for connection %s\n",
2099 GNUNET_h2s (&c->id));
2101 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2102 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2103 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2104 GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2105 c->destroy = GNUNET_YES;
2110 * @brief Start a polling timer for the connection.
2112 * When a neighbor does not accept more traffic on the connection it could be
2113 * caused by a simple congestion or by a lost ACK. Polling enables to check
2114 * for the lastest ACK status for a connection.
2116 * @param c Connection.
2117 * @param fwd Should we poll in the FWD direction?
2120 GMC_start_poll (struct MeshConnection *c, int fwd)
2122 struct MeshFlowControl *fc;
2124 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2125 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2129 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2136 * @brief Stop polling a connection for ACKs.
2138 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2140 * @param c Connection.
2141 * @param fwd Should we stop the poll in the FWD direction?
2144 GMC_stop_poll (struct MeshConnection *c, int fwd)
2146 struct MeshFlowControl *fc;
2148 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2149 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2151 GNUNET_SCHEDULER_cancel (fc->poll_task);
2152 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;