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.h"
35 #include "gnunet-service-mesh_connection.h"
36 #include "gnunet-service-mesh_peer.h"
37 #include "gnunet-service-mesh_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
42 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
43 GNUNET_TIME_UNIT_MINUTES,\
48 /******************************************************************************/
49 /******************************** STRUCTS **********************************/
50 /******************************************************************************/
53 * Struct to encapsulate all the Flow Control information to a peer to which
54 * we are directly connected (on a core level).
56 struct MeshFlowControl
59 * Connection this controls.
61 struct MeshConnection *c;
64 * How many messages are in the queue on this connection.
69 * How many messages do we accept in the queue.
71 unsigned int queue_max;
79 * ID of the last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 GNUNET_SCHEDULER_TaskIdentifier poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
109 * Queued poll message, to cancel if not necessary anymore (got ACK).
111 struct MeshConnectionQueue *poll_msg;
114 * Queued poll message, to cancel if not necessary anymore (got ACK).
116 struct MeshConnectionQueue *ack_msg;
120 * Keep a record of the last messages sent on this connection.
122 struct MeshConnectionPerformance
125 * Circular buffer for storing measurements.
127 double usecsperbyte[AVG_MSGS];
130 * Running average of @c usecsperbyte.
135 * How many values of @c usecsperbyte are valid.
140 * Index of the next "free" position in @c usecsperbyte.
147 * Struct containing all information regarding a connection to a peer.
149 struct MeshConnection
152 * Tunnel this connection is part of.
154 struct MeshTunnel3 *t;
157 * Flow control information for traffic fwd.
159 struct MeshFlowControl fwd_fc;
162 * Flow control information for traffic bck.
164 struct MeshFlowControl bck_fc;
167 * Measure connection performance on the endpoint.
169 struct MeshConnectionPerformance *perf;
172 * ID of the connection.
174 struct GNUNET_HashCode id;
177 * State of the connection.
179 enum MeshConnectionState state;
182 * Path being used for the tunnel. At the origin of the connection
183 * it's a pointer to the destination's path pool, otherwise just a copy.
185 struct MeshPeerPath *path;
188 * Position of the local peer in the path.
190 unsigned int own_pos;
193 * Task to keep the used paths alive at the owner,
194 * time tunnel out on all the other peers.
196 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
199 * Task to keep the used paths alive at the destination,
200 * time tunnel out on all the other peers.
202 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
205 * Pending message count.
207 int pending_messages;
210 * Destroy flag: if true, destroy on last message.
216 * Handle for messages queued but not yet sent.
218 struct MeshConnectionQueue
221 * Peer queue handle, to cancel if necessary.
223 struct MeshPeerQueue *q;
226 * Was this a forced message? (Do not account for it)
231 * Continuation to call once sent.
236 * Closure for @c cont.
241 /******************************************************************************/
242 /******************************* GLOBALS ***********************************/
243 /******************************************************************************/
246 * Global handle to the statistics service.
248 extern struct GNUNET_STATISTICS_Handle *stats;
251 * Local peer own ID (memory efficient handle).
253 extern GNUNET_PEER_Id myid;
256 * Local peer own ID (full value).
258 extern struct GNUNET_PeerIdentity my_full_id;
261 * Connections known, indexed by cid (MeshConnection).
263 static struct GNUNET_CONTAINER_MultiHashMap *connections;
266 * How many connections are we willing to maintain.
267 * Local connections are always allowed, even if there are more connections than max.
269 static unsigned long long max_connections;
272 * How many messages *in total* are we willing to queue, divide by number of
273 * connections to get connection queue size.
275 static unsigned long long max_msgs_queue;
278 * How often to send path keepalives. Paths timeout after 4 missed.
280 static struct GNUNET_TIME_Relative refresh_connection_time;
283 * How often to send path create / ACKs.
285 static struct GNUNET_TIME_Relative create_connection_time;
288 /******************************************************************************/
289 /******************************** STATIC ***********************************/
290 /******************************************************************************/
292 #if 0 // avoid compiler warning for unused static function
294 fc_debug (struct MeshFlowControl *fc)
296 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
297 fc->last_pid_recv, fc->last_ack_sent);
298 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
299 fc->last_pid_sent, fc->last_ack_recv);
300 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
301 fc->queue_n, fc->queue_max);
305 connection_debug (struct MeshConnection *c)
309 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
312 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
313 peer2s (c->t->peer), GMC_2s (c));
314 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
315 c->state, c->pending_messages);
316 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
317 fc_debug (&c->fwd_fc);
318 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
319 fc_debug (&c->bck_fc);
324 * Get string description for tunnel state.
326 * @param s Tunnel state.
328 * @return String representation.
331 GMC_state2s (enum MeshConnectionState s)
335 case MESH_CONNECTION_NEW:
336 return "MESH_CONNECTION_NEW";
337 case MESH_CONNECTION_SENT:
338 return "MESH_CONNECTION_SENT";
339 case MESH_CONNECTION_ACK:
340 return "MESH_CONNECTION_ACK";
341 case MESH_CONNECTION_READY:
342 return "MESH_CONNECTION_READY";
343 case MESH_CONNECTION_DESTROYED:
344 return "MESH_CONNECTION_DESTROYED";
346 return "MESH_CONNECTION_STATE_ERROR";
352 * Initialize a Flow Control structure to the initial state.
354 * @param fc Flow Control structure to initialize.
357 fc_init (struct MeshFlowControl *fc)
360 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
361 fc->last_pid_recv = (uint32_t) -1;
362 fc->last_ack_sent = (uint32_t) 0;
363 fc->last_ack_recv = (uint32_t) 0;
364 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
365 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
367 fc->queue_max = (max_msgs_queue / max_connections) + 1;
374 * @param cid Connection ID.
376 static struct MeshConnection *
377 connection_get (const struct GNUNET_HashCode *cid)
379 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
384 connection_change_state (struct MeshConnection* c,
385 enum MeshConnectionState state)
387 LOG (GNUNET_ERROR_TYPE_DEBUG,
388 "Connection %s state was %s\n",
389 GMC_2s (c), GMC_state2s (c->state));
390 if (MESH_CONNECTION_DESTROYED == c->state)
392 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
395 LOG (GNUNET_ERROR_TYPE_DEBUG,
396 "Connection %s state is now %s\n",
397 GMC_2s (c), GMC_state2s (state));
403 * Callback called when a queued ACK message is sent.
405 * @param cls Closure (FC).
406 * @param c Connection this message was on.
407 * @param q Queue handler this call invalidates.
408 * @param type Type of message sent.
409 * @param fwd Was this a FWD going message?
410 * @param size Size of the message.
414 struct MeshConnection *c,
415 struct MeshConnectionQueue *q,
416 uint16_t type, int fwd, size_t size)
418 struct MeshFlowControl *fc = cls;
425 * Send an ACK on the connection, informing the predecessor about
426 * the available buffer space. Should not be called in case the peer
427 * is origin (no predecessor) in the @c fwd direction.
429 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
430 * the ACK itself goes "back" (dest->root).
432 * @param c Connection on which to send the ACK.
433 * @param buffer How much space free to advertise?
434 * @param fwd Is this FWD ACK? (Going dest -> root)
435 * @param force Don't optimize out.
438 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
440 struct MeshFlowControl *next_fc;
441 struct MeshFlowControl *prev_fc;
442 struct GNUNET_MESH_ACK msg;
446 /* If origin, there is no connection to send ACKs. Wrong function! */
447 if (GMC_is_origin (c, fwd))
453 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
454 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
456 LOG (GNUNET_ERROR_TYPE_DEBUG,
457 "connection send %s ack on %s\n",
458 GM_f2s (fwd), GMC_2s (c));
460 /* Check if we need to transmit the ACK. */
461 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
462 if (3 < delta && buffer < delta && GNUNET_NO == force)
464 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
465 LOG (GNUNET_ERROR_TYPE_DEBUG,
466 " last pid recv: %u, last ack sent: %u\n",
467 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
471 /* Ok, ACK might be necessary, what PID to ACK? */
472 ack = prev_fc->last_pid_recv + buffer;
473 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
474 LOG (GNUNET_ERROR_TYPE_DEBUG,
475 " last pid %u, last ack %u, qmax %u, q %u\n",
476 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
477 next_fc->queue_max, next_fc->queue_n);
478 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
484 /* Check if message is already in queue */
485 if (NULL != prev_fc->ack_msg)
487 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
489 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
490 GMC_cancel (prev_fc->ack_msg);
491 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
495 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
500 prev_fc->last_ack_sent = ack;
502 /* Build ACK message and send on connection */
503 msg.header.size = htons (sizeof (msg));
504 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
505 msg.ack = htonl (ack);
508 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
515 * Callback called when a queued message is sent.
517 * Calculates the average time and connection packet tracking.
519 * @param cls Closure (ConnectionQueue Handle).
520 * @param c Connection this message was on.
521 * @param type Type of message sent.
522 * @param fwd Was this a FWD going message?
523 * @param size Size of the message.
524 * @param wait Time spent waiting for core (only the time for THIS message)
527 message_sent (void *cls,
528 struct MeshConnection *c, uint16_t type,
529 int fwd, size_t size,
530 struct GNUNET_TIME_Relative wait)
532 struct MeshConnectionPerformance *p;
533 struct MeshFlowControl *fc;
534 struct MeshConnectionQueue *q = cls;
538 fc = fwd ? &c->fwd_fc : &c->bck_fc;
539 LOG (GNUNET_ERROR_TYPE_DEBUG,
543 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
549 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
550 q->cont (q->cont_cls, c, q, type, fwd, size);
554 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
556 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
563 c->pending_messages--;
564 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
566 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
570 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
573 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
576 if (GNUNET_NO == forced)
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 "! accounting pid %u\n",
585 LOG (GNUNET_ERROR_TYPE_DEBUG,
586 "! forced, Q_N not accounting pid %u\n",
589 GMC_send_ack (c, fwd, GNUNET_NO);
592 case GNUNET_MESSAGE_TYPE_MESH_POLL:
596 case GNUNET_MESSAGE_TYPE_MESH_ACK:
603 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
606 return; /* Only endpoints are interested in timing. */
609 usecsperbyte = ((double) wait.rel_value_us) / size;
610 if (p->size == AVG_MSGS)
612 /* Array is full. Substract oldest value, add new one and store. */
613 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
614 p->usecsperbyte[p->idx] = usecsperbyte;
615 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
619 /* Array not yet full. Add current value to avg and store. */
620 p->usecsperbyte[p->idx] = usecsperbyte;
622 p->avg += p->usecsperbyte[p->idx];
626 p->idx = (p->idx + 1) % AVG_MSGS;
631 * Get the previous hop in a connection
633 * @param c Connection.
635 * @return Previous peer in the connection.
637 static struct MeshPeer *
638 get_prev_hop (const struct MeshConnection *c)
642 if (0 == c->own_pos || c->path->length < 2)
643 id = c->path->peers[0];
645 id = c->path->peers[c->own_pos - 1];
647 return GMP_get_short (id);
652 * Get the next hop in a connection
654 * @param c Connection.
656 * @return Next peer in the connection.
658 static struct MeshPeer *
659 get_next_hop (const struct MeshConnection *c)
663 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
664 id = c->path->peers[c->path->length - 1];
666 id = c->path->peers[c->own_pos + 1];
668 return GMP_get_short (id);
673 * Get the hop in a connection.
675 * @param c Connection.
676 * @param fwd Next hop?
678 * @return Next peer in the connection.
680 static struct MeshPeer *
681 get_hop (struct MeshConnection *c, int fwd)
684 return get_next_hop (c);
685 return get_prev_hop (c);
690 * Is traffic coming from this sender 'FWD' traffic?
692 * @param c Connection to check.
693 * @param sender Peer identity of neighbor.
695 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
696 * the traffic is 'FWD'.
697 * #GNUNET_NO for BCK.
698 * #GNUNET_SYSERR for errors.
701 is_fwd (const struct MeshConnection *c,
702 const struct GNUNET_PeerIdentity *sender)
706 id = GNUNET_PEER_search (sender);
707 if (GMP_get_short_id (get_prev_hop (c)) == id)
710 if (GMP_get_short_id (get_next_hop (c)) == id)
714 return GNUNET_SYSERR;
719 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
720 * or a first CONNECTION_ACK directed to us.
722 * @param connection Connection to confirm.
723 * @param fwd Should we send it FWD? (root->dest)
724 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
727 send_connection_ack (struct MeshConnection *connection, int fwd)
729 struct MeshTunnel3 *t;
732 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
734 GMP_queue_add (get_hop (connection, fwd), NULL,
735 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
736 sizeof (struct GNUNET_MESH_ConnectionACK),
737 connection, fwd, &message_sent, NULL);
738 connection->pending_messages++;
739 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
740 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
741 if (MESH_CONNECTION_READY != connection->state)
742 connection_change_state (connection, MESH_CONNECTION_SENT);
747 * Send a notification that a connection is broken.
749 * @param c Connection that is broken.
750 * @param id1 Peer that has disconnected.
751 * @param id2 Peer that has disconnected.
752 * @param fwd Direction towards which to send it.
755 send_broken (struct MeshConnection *c,
756 const struct GNUNET_PeerIdentity *id1,
757 const struct GNUNET_PeerIdentity *id2,
760 struct GNUNET_MESH_ConnectionBroken msg;
762 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
763 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
767 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
772 * Send keepalive packets for a connection.
774 * @param c Connection to keep alive..
775 * @param fwd Is this a FWD keepalive? (owner -> dest).
778 connection_keepalive (struct MeshConnection *c, int fwd)
780 struct GNUNET_MESH_ConnectionKeepAlive *msg;
781 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
785 "sending %s keepalive for connection %s]\n",
786 GM_f2s (fwd), GMC_2s (c));
788 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
789 msg->header.size = htons (size);
790 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
792 msg->reserved = htonl (0);
794 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
799 * Send CONNECTION_{CREATE/ACK} packets for a connection.
801 * @param c Connection for which to send the message.
802 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
805 connection_recreate (struct MeshConnection *c, int fwd)
807 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
811 send_connection_ack (c, GNUNET_NO);
816 * Generic connection timer management.
817 * Depending on the role of the peer in the connection will send the
818 * appropriate message (build or keepalive)
820 * @param c Conncetion to maintain.
824 connection_maintain (struct MeshConnection *c, int fwd)
826 if (GNUNET_NO != c->destroy)
829 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
831 /* TODO DHT GET with RO_BART */
836 case MESH_CONNECTION_NEW:
839 case MESH_CONNECTION_SENT:
840 connection_recreate (c, fwd);
842 case MESH_CONNECTION_READY:
843 connection_keepalive (c, fwd);
852 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
854 struct MeshConnection *c = cls;
855 struct GNUNET_TIME_Relative delay;
857 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
858 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
861 connection_maintain (c, GNUNET_YES);
862 delay = c->state == MESH_CONNECTION_READY ?
863 refresh_connection_time : create_connection_time;
864 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
865 &connection_fwd_keepalive,
871 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
873 struct MeshConnection *c = cls;
874 struct GNUNET_TIME_Relative delay;
876 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
877 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
880 connection_maintain (c, GNUNET_NO);
881 delay = c->state == MESH_CONNECTION_READY ?
882 refresh_connection_time : create_connection_time;
883 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
884 &connection_bck_keepalive,
890 * @brief Re-initiate traffic on this connection if necessary.
892 * Check if there is traffic queued towards this peer
893 * and the core transmit handle is NULL (traffic was stalled).
894 * If so, call core tmt rdy.
896 * @param c Connection on which initiate traffic.
897 * @param fwd Is this about fwd traffic?
900 connection_unlock_queue (struct MeshConnection *c, int fwd)
902 struct MeshPeer *peer;
904 LOG (GNUNET_ERROR_TYPE_DEBUG,
905 "connection_unlock_queue %s on %s\n",
906 GM_f2s (fwd), GMC_2s (c));
908 if (GMC_is_terminal (c, fwd))
910 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
914 peer = get_hop (c, fwd);
915 GMP_queue_unlock (peer, c);
920 * Cancel all transmissions that belong to a certain connection.
922 * If the connection is scheduled for destruction and no more messages are left,
923 * the connection will be destroyed by the continuation call.
925 * @param c Connection which to cancel. Might be destroyed during this call.
926 * @param fwd Cancel fwd traffic?
929 connection_cancel_queues (struct MeshConnection *c, int fwd)
931 struct MeshFlowControl *fc;
932 struct MeshPeer *peer;
934 LOG (GNUNET_ERROR_TYPE_DEBUG,
935 " *** Cancel %s queues for connection %s\n",
936 GM_f2s (fwd), GMC_2s (c));
943 fc = fwd ? &c->fwd_fc : &c->bck_fc;
944 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
946 GNUNET_SCHEDULER_cancel (fc->poll_task);
947 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
948 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
950 peer = get_hop (c, fwd);
951 GMP_queue_cancel (peer, c);
956 * Function called if a connection has been stalled for a while,
957 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
959 * @param cls Closure (poll ctx).
960 * @param tc TaskContext.
963 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
967 * Callback called when a queued POLL message is sent.
969 * @param cls Closure (FC).
970 * @param c Connection this message was on.
971 * @param q Queue handler this call invalidates.
972 * @param type Type of message sent.
973 * @param fwd Was this a FWD going message?
974 * @param size Size of the message.
977 poll_sent (void *cls,
978 struct MeshConnection *c,
979 struct MeshConnectionQueue *q,
980 uint16_t type, int fwd, size_t size)
982 struct MeshFlowControl *fc = cls;
986 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
989 LOG (GNUNET_ERROR_TYPE_DEBUG,
990 " *** POLL sent for , scheduling new one!\n");
992 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
993 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
994 &connection_poll, fc);
995 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1000 * Function called if a connection has been stalled for a while,
1001 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1003 * @param cls Closure (poll ctx).
1004 * @param tc TaskContext.
1007 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1009 struct MeshFlowControl *fc = cls;
1010 struct GNUNET_MESH_Poll msg;
1011 struct MeshConnection *c;
1013 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1014 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1021 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1022 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1023 fc == &c->fwd_fc ? "FWD" : "BCK");
1025 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1026 msg.header.size = htons (sizeof (msg));
1027 msg.pid = htonl (fc->last_pid_sent);
1028 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1029 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1030 fc == &c->fwd_fc, GNUNET_YES,
1036 * Timeout function due to lack of keepalive/traffic from the owner.
1037 * Destroys connection if called.
1039 * @param cls Closure (connection to destroy).
1040 * @param tc TaskContext.
1043 connection_fwd_timeout (void *cls,
1044 const struct GNUNET_SCHEDULER_TaskContext *tc)
1046 struct MeshConnection *c = cls;
1048 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1049 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1054 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1065 * Timeout function due to lack of keepalive/traffic from the destination.
1066 * Destroys connection if called.
1068 * @param cls Closure (connection to destroy).
1069 * @param tc TaskContext
1072 connection_bck_timeout (void *cls,
1073 const struct GNUNET_SCHEDULER_TaskContext *tc)
1075 struct MeshConnection *c = cls;
1077 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1078 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1081 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1084 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1095 * Resets the connection timeout task, some other message has done the
1097 * - For the first peer on the direction this means to send
1098 * a keepalive or a path confirmation message (either create or ACK).
1099 * - For all other peers, this means to destroy the connection,
1100 * due to lack of activity.
1101 * Starts the timeout if no timeout was running (connection just created).
1103 * @param c Connection whose timeout to reset.
1104 * @param fwd Is this forward?
1106 * TODO use heap to improve efficiency of scheduler.
1109 connection_reset_timeout (struct MeshConnection *c, int fwd)
1111 GNUNET_SCHEDULER_TaskIdentifier *ti;
1112 GNUNET_SCHEDULER_Task f;
1114 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1116 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1118 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1119 GNUNET_SCHEDULER_cancel (*ti);
1121 if (GMC_is_origin (c, fwd)) /* Startpoint */
1123 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1124 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1126 else /* Relay, endpoint. */
1128 struct GNUNET_TIME_Relative delay;
1130 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1131 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1132 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1138 * Add the connection to the list of both neighbors.
1140 * @param c Connection.
1142 * @return #GNUNET_OK if everything went fine
1143 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1146 register_neighbors (struct MeshConnection *c)
1148 struct MeshPeer *next_peer;
1149 struct MeshPeer *prev_peer;
1151 next_peer = get_next_hop (c);
1152 prev_peer = get_prev_hop (c);
1154 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1155 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1157 if (GMC_is_origin (c, GNUNET_YES))
1158 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1159 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1162 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor: %d\n",
1163 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1164 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor: %d\n",
1165 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1166 return GNUNET_SYSERR;
1169 GMP_add_connection (next_peer, c);
1170 GMP_add_connection (prev_peer, c);
1177 * Remove the connection from the list of both neighbors.
1179 * @param c Connection.
1182 unregister_neighbors (struct MeshConnection *c)
1184 struct MeshPeer *peer;
1186 peer = get_next_hop (c);
1187 if (GNUNET_OK != GMP_remove_connection (peer, c))
1189 GNUNET_break (MESH_CONNECTION_NEW == c->state);
1190 LOG (GNUNET_ERROR_TYPE_ERROR, " cstate: %u\n", c->state);
1193 peer = get_prev_hop (c);
1194 if (GNUNET_OK != GMP_remove_connection (peer, c))
1196 GNUNET_break (MESH_CONNECTION_NEW == c->state);
1197 LOG (GNUNET_ERROR_TYPE_ERROR, " cstate: %u\n", c->state);
1203 * Bind the connection to the peer and the tunnel to that peer.
1205 * If the peer has no tunnel, create one. Update tunnel and connection
1206 * data structres to reflect new status.
1208 * @param c Connection.
1212 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1214 GMP_add_tunnel (peer);
1215 c->t = GMP_get_tunnel (peer);
1216 GMT_add_connection (c->t, c);
1219 /******************************************************************************/
1220 /******************************** API ***********************************/
1221 /******************************************************************************/
1224 * Core handler for connection creation.
1226 * @param cls Closure (unused).
1227 * @param peer Sender (neighbor).
1228 * @param message Message.
1230 * @return GNUNET_OK to keep the connection open,
1231 * GNUNET_SYSERR to close it (signal serious error)
1234 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1235 const struct GNUNET_MessageHeader *message)
1237 struct GNUNET_MESH_ConnectionCreate *msg;
1238 struct GNUNET_PeerIdentity *id;
1239 struct GNUNET_HashCode *cid;
1240 struct MeshPeerPath *path;
1241 struct MeshPeer *dest_peer;
1242 struct MeshPeer *orig_peer;
1243 struct MeshConnection *c;
1244 unsigned int own_pos;
1248 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1249 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1252 size = ntohs (message->size);
1253 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1255 GNUNET_break_op (0);
1259 /* Calculate hops */
1260 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1261 if (size % sizeof (struct GNUNET_PeerIdentity))
1263 GNUNET_break_op (0);
1266 size /= sizeof (struct GNUNET_PeerIdentity);
1269 GNUNET_break_op (0);
1272 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1274 /* Get parameters */
1275 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1277 id = (struct GNUNET_PeerIdentity *) &msg[1];
1278 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s (%s->).\n",
1279 GNUNET_h2s (cid), GNUNET_i2s (id));
1281 /* Create connection */
1282 c = connection_get (cid);
1286 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1287 path = path_new (size);
1289 for (i = 0; i < size; i++)
1291 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1292 GNUNET_i2s (&id[i]));
1293 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1294 if (path->peers[i] == myid)
1297 if (own_pos == 0 && path->peers[own_pos] != myid)
1299 /* create path: self not found in path through self */
1300 GNUNET_break_op (0);
1301 path_destroy (path);
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1305 GMP_add_path_to_all (path, GNUNET_NO);
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1307 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1310 path_destroy (path);
1313 connection_reset_timeout (c, GNUNET_YES);
1317 path = path_duplicate (c->path);
1319 if (MESH_CONNECTION_NEW == c->state)
1320 connection_change_state (c, MESH_CONNECTION_SENT);
1322 /* Remember peers */
1323 dest_peer = GMP_get (&id[size - 1]);
1324 orig_peer = GMP_get (&id[0]);
1326 /* Is it a connection to us? */
1327 if (c->own_pos == size - 1)
1329 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1330 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1332 add_to_peer (c, orig_peer);
1333 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1334 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1336 send_connection_ack (c, GNUNET_NO);
1337 if (MESH_CONNECTION_SENT == c->state)
1338 connection_change_state (c, MESH_CONNECTION_ACK);
1340 /* Keep tunnel alive in direction dest->owner*/
1341 if (GNUNET_SCHEDULER_NO_TASK == c->bck_maintenance_task)
1343 c->bck_maintenance_task =
1344 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1345 &connection_bck_keepalive, c);
1350 /* It's for somebody else! Retransmit. */
1351 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1352 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1353 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1354 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1357 path_destroy (path);
1363 * Core handler for path confirmations.
1365 * @param cls closure
1366 * @param message message
1367 * @param peer peer identity this notification is about
1369 * @return GNUNET_OK to keep the connection open,
1370 * GNUNET_SYSERR to close it (signal serious error)
1373 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1374 const struct GNUNET_MessageHeader *message)
1376 struct GNUNET_MESH_ConnectionACK *msg;
1377 struct MeshConnection *c;
1378 struct MeshPeerPath *p;
1379 struct MeshPeer *pi;
1380 enum MeshConnectionState oldstate;
1383 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1384 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1385 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1386 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1387 GNUNET_h2s (&msg->cid));
1388 c = connection_get (&msg->cid);
1391 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1393 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1397 oldstate = c->state;
1398 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1399 pi = GMP_get (peer);
1400 if (get_next_hop (c) == pi)
1402 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1404 if (MESH_CONNECTION_SENT == oldstate)
1405 connection_change_state (c, MESH_CONNECTION_ACK);
1407 else if (get_prev_hop (c) == pi)
1409 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1411 connection_change_state (c, MESH_CONNECTION_READY);
1415 GNUNET_break_op (0);
1419 connection_reset_timeout (c, fwd);
1421 /* Add path to peers? */
1425 GMP_add_path_to_all (p, GNUNET_YES);
1432 /* Message for us as creator? */
1433 if (GMC_is_origin (c, GNUNET_YES))
1435 if (GNUNET_NO != fwd)
1437 GNUNET_break_op (0);
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1442 /* If just created, cancel the short timeout and start a long one */
1443 if (MESH_CONNECTION_SENT == oldstate)
1444 connection_reset_timeout (c, GNUNET_YES);
1446 /* Change connection state */
1447 connection_change_state (c, MESH_CONNECTION_READY);
1448 send_connection_ack (c, GNUNET_YES);
1450 /* Change tunnel state, trigger KX */
1451 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1452 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1457 /* Message for us as destination? */
1458 if (GMC_is_terminal (c, GNUNET_YES))
1460 if (GNUNET_YES != fwd)
1462 GNUNET_break_op (0);
1465 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1467 /* If just created, cancel the short timeout and start a long one */
1468 if (MESH_CONNECTION_ACK == oldstate)
1469 connection_reset_timeout (c, GNUNET_NO);
1471 /* Change tunnel state */
1472 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1473 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1478 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1479 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1485 * Core handler for notifications of broken paths
1487 * @param cls Closure (unused).
1488 * @param id Peer identity of sending neighbor.
1489 * @param message Message.
1491 * @return GNUNET_OK to keep the connection open,
1492 * GNUNET_SYSERR to close it (signal serious error)
1495 GMC_handle_broken (void* cls,
1496 const struct GNUNET_PeerIdentity* id,
1497 const struct GNUNET_MessageHeader* message)
1499 struct GNUNET_MESH_ConnectionBroken *msg;
1500 struct MeshConnection *c;
1503 LOG (GNUNET_ERROR_TYPE_DEBUG,
1504 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1505 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1506 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1507 GNUNET_i2s (&msg->peer1));
1508 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1509 GNUNET_i2s (&msg->peer2));
1510 c = connection_get (&msg->cid);
1513 GNUNET_break_op (0);
1517 fwd = is_fwd (c, id);
1518 if (GMC_is_terminal (c, fwd))
1520 if (0 < c->pending_messages)
1521 c->destroy = GNUNET_YES;
1527 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1528 c->destroy = GNUNET_YES;
1529 connection_cancel_queues (c, !fwd);
1538 * Core handler for tunnel destruction
1540 * @param cls Closure (unused).
1541 * @param peer Peer identity of sending neighbor.
1542 * @param message Message.
1544 * @return GNUNET_OK to keep the connection open,
1545 * GNUNET_SYSERR to close it (signal serious error)
1548 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1549 const struct GNUNET_MessageHeader *message)
1551 struct GNUNET_MESH_ConnectionDestroy *msg;
1552 struct MeshConnection *c;
1555 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1556 LOG (GNUNET_ERROR_TYPE_DEBUG,
1557 "Got a CONNECTION DESTROY message from %s\n",
1559 LOG (GNUNET_ERROR_TYPE_DEBUG,
1560 " for connection %s\n",
1561 GNUNET_h2s (&msg->cid));
1562 c = connection_get (&msg->cid);
1565 /* Probably already got the message from another path,
1566 * destroyed the tunnel and retransmitted to children.
1569 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1571 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1574 fwd = is_fwd (c, peer);
1575 if (GNUNET_SYSERR == fwd)
1577 GNUNET_break_op (0);
1580 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1581 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1582 else if (0 == c->pending_messages)
1584 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1588 c->destroy = GNUNET_YES;
1589 c->state = MESH_CONNECTION_DESTROYED;
1592 GMT_remove_connection (c->t, c);
1600 * Generic handler for mesh network encrypted traffic.
1602 * @param peer Peer identity this notification is about.
1603 * @param msg Encrypted message.
1605 * @return GNUNET_OK to keep the connection open,
1606 * GNUNET_SYSERR to close it (signal serious error)
1609 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1610 const struct GNUNET_MESH_Encrypted *msg)
1612 struct MeshConnection *c;
1613 struct MeshPeer *neighbor;
1614 struct MeshFlowControl *fc;
1615 GNUNET_PEER_Id peer_id;
1623 size = ntohs (msg->header.size);
1625 sizeof (struct GNUNET_MESH_Encrypted) +
1626 sizeof (struct GNUNET_MessageHeader))
1628 GNUNET_break_op (0);
1631 type = ntohs (msg->header.type);
1632 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1633 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1634 GM_m2s (type), ntohl (msg->pid), GNUNET_i2s (peer));
1636 /* Check connection */
1637 c = connection_get (&msg->cid);
1640 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1641 LOG (GNUNET_ERROR_TYPE_DEBUG,
1642 "WARNING connection %s unknown\n",
1643 GNUNET_h2s (&msg->cid));
1647 /* Check if origin is as expected */
1648 neighbor = get_prev_hop (c);
1649 peer_id = GNUNET_PEER_search (peer);
1650 if (peer_id == GMP_get_short_id (neighbor))
1656 neighbor = get_next_hop (c);
1657 if (peer_id == GMP_get_short_id (neighbor))
1663 /* Unexpected peer sending traffic on a connection. */
1664 GNUNET_break_op (0);
1670 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1671 pid = ntohl (msg->pid);
1672 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1674 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1675 LOG (GNUNET_ERROR_TYPE_DEBUG,
1676 "WARNING Received PID %u, (prev %u), ACK %u\n",
1677 pid, fc->last_pid_recv, fc->last_ack_sent);
1680 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1682 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1683 LOG (GNUNET_ERROR_TYPE_DEBUG,
1684 " Pid %u not expected (%u+), dropping!\n",
1685 pid, fc->last_pid_recv + 1);
1688 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1689 connection_change_state (c, MESH_CONNECTION_READY);
1690 connection_reset_timeout (c, fwd);
1691 fc->last_pid_recv = pid;
1693 /* Is this message for us? */
1694 if (GMC_is_terminal (c, fwd))
1696 /* TODO signature verification */
1697 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1698 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1705 fc->last_pid_recv = pid;
1706 GMT_handle_encrypted (c->t, msg);
1707 GMC_send_ack (c, fwd, GNUNET_NO);
1711 /* Message not for us: forward to next hop */
1712 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1713 ttl = ntohl (msg->ttl);
1714 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1717 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1718 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1719 GMC_send_ack (c, fwd, GNUNET_NO);
1723 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1724 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1730 * Generic handler for mesh network encrypted traffic.
1732 * @param peer Peer identity this notification is about.
1733 * @param msg Encrypted message.
1735 * @return GNUNET_OK to keep the connection open,
1736 * GNUNET_SYSERR to close it (signal serious error)
1739 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1740 const struct GNUNET_MESH_KX *msg)
1742 struct MeshConnection *c;
1743 struct MeshPeer *neighbor;
1744 GNUNET_PEER_Id peer_id;
1750 size = ntohs (msg->header.size);
1752 sizeof (struct GNUNET_MESH_Encrypted) +
1753 sizeof (struct GNUNET_MessageHeader))
1755 GNUNET_break_op (0);
1758 type = ntohs (msg->header.type);
1759 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1760 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1761 GM_m2s (type), GNUNET_i2s (peer));
1763 /* Check connection */
1764 c = connection_get (&msg->cid);
1767 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1768 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1771 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1773 /* Check if origin is as expected */
1774 neighbor = get_prev_hop (c);
1775 peer_id = GNUNET_PEER_search (peer);
1776 if (peer_id == GMP_get_short_id (neighbor))
1782 neighbor = get_next_hop (c);
1783 if (peer_id == GMP_get_short_id (neighbor))
1789 /* Unexpected peer sending traffic on a connection. */
1790 GNUNET_break_op (0);
1795 /* Count as connection confirmation. */
1796 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1798 connection_change_state (c, MESH_CONNECTION_READY);
1801 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1802 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1805 connection_reset_timeout (c, fwd);
1807 /* Is this message for us? */
1808 if (GMC_is_terminal (c, fwd))
1810 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1811 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1817 GMT_handle_kx (c->t, &msg[1].header);
1821 /* Message not for us: forward to next hop */
1822 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1823 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1824 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1831 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1833 * @param cls Closure (unused).
1834 * @param message Message received.
1835 * @param peer Peer who sent the message.
1837 * @return GNUNET_OK to keep the connection open,
1838 * GNUNET_SYSERR to close it (signal serious error)
1841 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1842 const struct GNUNET_MessageHeader *message)
1844 return handle_mesh_encrypted (peer,
1845 (struct GNUNET_MESH_Encrypted *)message);
1850 * Core handler for key exchange traffic (ephemeral key, ping, pong).
1852 * @param cls Closure (unused).
1853 * @param message Message received.
1854 * @param peer Peer who sent the message.
1856 * @return GNUNET_OK to keep the connection open,
1857 * GNUNET_SYSERR to close it (signal serious error)
1860 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1861 const struct GNUNET_MessageHeader *message)
1863 return handle_mesh_kx (peer,
1864 (struct GNUNET_MESH_KX *) message);
1869 * Core handler for mesh network traffic point-to-point acks.
1871 * @param cls closure
1872 * @param message message
1873 * @param peer peer identity this notification is about
1875 * @return GNUNET_OK to keep the connection open,
1876 * GNUNET_SYSERR to close it (signal serious error)
1879 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1880 const struct GNUNET_MessageHeader *message)
1882 struct GNUNET_MESH_ACK *msg;
1883 struct MeshConnection *c;
1884 struct MeshFlowControl *fc;
1889 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1890 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1892 msg = (struct GNUNET_MESH_ACK *) message;
1894 c = connection_get (&msg->cid);
1898 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1903 /* Is this a forward or backward ACK? */
1904 id = GNUNET_PEER_search (peer);
1905 if (GMP_get_short_id (get_next_hop (c)) == id)
1907 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1911 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1913 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1919 GNUNET_break_op (0);
1923 ack = ntohl (msg->ack);
1924 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1925 ack, fc->last_ack_recv);
1926 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
1927 fc->last_ack_recv = ack;
1929 /* Cancel polling if the ACK is big enough. */
1930 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1931 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1933 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1934 GNUNET_SCHEDULER_cancel (fc->poll_task);
1935 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1936 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1939 connection_unlock_queue (c, fwd);
1946 * Core handler for mesh network traffic point-to-point ack polls.
1948 * @param cls closure
1949 * @param message message
1950 * @param peer peer identity this notification is about
1952 * @return GNUNET_OK to keep the connection open,
1953 * GNUNET_SYSERR to close it (signal serious error)
1956 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1957 const struct GNUNET_MessageHeader *message)
1959 struct GNUNET_MESH_Poll *msg;
1960 struct MeshConnection *c;
1961 struct MeshFlowControl *fc;
1966 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1967 LOG (GNUNET_ERROR_TYPE_DEBUG,
1968 "Got a POLL packet from %s!\n",
1971 msg = (struct GNUNET_MESH_Poll *) message;
1973 c = connection_get (&msg->cid);
1977 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1979 GNUNET_break_op (0);
1983 /* Is this a forward or backward ACK?
1984 * Note: a poll should never be needed in a loopback case,
1985 * since there is no possiblility of packet loss there, so
1986 * this way of discerining FWD/BCK should not be a problem.
1988 id = GNUNET_PEER_search (peer);
1989 if (GMP_get_short_id (get_next_hop (c)) == id)
1991 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
1994 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1996 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2001 GNUNET_break_op (0);
2005 pid = ntohl (msg->pid);
2006 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2007 fc->last_pid_recv = pid;
2008 fwd = fc == &c->bck_fc;
2009 GMC_send_ack (c, fwd, GNUNET_YES);
2016 * Core handler for mesh keepalives.
2018 * @param cls closure
2019 * @param message message
2020 * @param peer peer identity this notification is about
2021 * @return GNUNET_OK to keep the connection open,
2022 * GNUNET_SYSERR to close it (signal serious error)
2024 * TODO: Check who we got this from, to validate route.
2027 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2028 const struct GNUNET_MessageHeader *message)
2030 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2031 struct MeshConnection *c;
2032 struct MeshPeer *neighbor;
2033 GNUNET_PEER_Id peer_id;
2036 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2037 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
2040 c = connection_get (&msg->cid);
2043 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2048 /* Check if origin is as expected TODO refactor and reuse */
2049 peer_id = GNUNET_PEER_search (peer);
2050 neighbor = get_prev_hop (c);
2051 if (peer_id == GMP_get_short_id (neighbor))
2057 neighbor = get_next_hop (c);
2058 if (peer_id == GMP_get_short_id (neighbor))
2064 GNUNET_break_op (0);
2069 connection_change_state (c, MESH_CONNECTION_READY);
2070 connection_reset_timeout (c, fwd);
2072 if (GMC_is_terminal (c, fwd))
2075 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2076 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2083 * Send an ACK on the appropriate connection/channel, depending on
2084 * the direction and the position of the peer.
2086 * @param c Which connection to send the hop-by-hop ACK.
2087 * @param fwd Is this a fwd ACK? (will go dest->root).
2088 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2091 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2093 unsigned int buffer;
2095 LOG (GNUNET_ERROR_TYPE_DEBUG,
2096 "GMC send %s ACK on %s\n",
2097 GM_f2s (fwd), GMC_2s (c));
2105 if (GNUNET_NO != c->destroy)
2107 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2111 /* Get available buffer space */
2112 if (GMC_is_terminal (c, fwd))
2114 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2115 buffer = GMT_get_channels_buffer (c->t);
2119 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2120 buffer = GMC_get_buffer (c, fwd);
2122 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2123 if (0 == buffer && GNUNET_NO == force)
2126 /* Send available buffer space */
2127 if (GMC_is_origin (c, fwd))
2129 GNUNET_assert (NULL != c->t);
2130 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2131 GMT_unchoke_channels (c->t);
2135 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2136 send_ack (c, buffer, fwd, force);
2142 * Initialize the connections subsystem
2144 * @param c Configuration handle.
2147 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2149 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2151 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2154 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2155 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2156 GNUNET_SCHEDULER_shutdown ();
2161 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2164 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2165 "MESH", "MAX_CONNECTIONS", "MISSING");
2166 GNUNET_SCHEDULER_shutdown ();
2171 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2172 &refresh_connection_time))
2174 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2175 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2176 GNUNET_SCHEDULER_shutdown ();
2179 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2180 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2185 * Destroy each connection on shutdown.
2187 * @param cls Closure (unused).
2188 * @param key Current key code (CID, unused).
2189 * @param value Value in the hash map (connection)
2191 * @return #GNUNET_YES, because we should continue to iterate,
2194 shutdown_iterator (void *cls,
2195 const struct GNUNET_HashCode *key,
2198 struct MeshConnection *c = value;
2206 * Shut down the connections subsystem.
2211 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2212 GNUNET_CONTAINER_multihashmap_destroy (connections);
2217 struct MeshConnection *
2218 GMC_new (const struct GNUNET_HashCode *cid,
2219 struct MeshTunnel3 *t,
2220 struct MeshPeerPath *p,
2221 unsigned int own_pos)
2223 struct MeshConnection *c;
2225 c = GNUNET_new (struct MeshConnection);
2227 GNUNET_assert (GNUNET_OK ==
2228 GNUNET_CONTAINER_multihashmap_put (connections,
2230 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2231 fc_init (&c->fwd_fc);
2232 fc_init (&c->bck_fc);
2237 GNUNET_assert (own_pos <= p->length - 1);
2238 c->own_pos = own_pos;
2241 if (GNUNET_OK != register_neighbors (c))
2245 GMT_remove_path (c->t, p);
2255 c->fwd_maintenance_task =
2256 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2257 &connection_fwd_keepalive, c);
2265 GMC_destroy (struct MeshConnection *c)
2273 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2274 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2277 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2278 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2279 &c->fwd_fc, &c->bck_fc);
2280 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2281 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2283 /* Cancel all traffic */
2284 if (NULL != c->path)
2286 connection_cancel_queues (c, GNUNET_YES);
2287 connection_cancel_queues (c, GNUNET_NO);
2288 unregister_neighbors (c);
2291 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2292 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2294 /* Cancel maintainance task (keepalive/timeout) */
2295 if (NULL != c->fwd_fc.poll_msg)
2297 GMC_cancel (c->fwd_fc.poll_msg);
2298 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2300 if (NULL != c->bck_fc.poll_msg)
2302 GMC_cancel (c->bck_fc.poll_msg);
2303 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2306 /* Delete from tunnel */
2308 GMT_remove_connection (c->t, c);
2310 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2311 path_destroy (c->path);
2312 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2313 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2314 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2315 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2316 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2318 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2319 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2321 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2323 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2324 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2327 GNUNET_break (GNUNET_YES ==
2328 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2330 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2335 * Get the connection ID.
2337 * @param c Connection to get the ID from.
2339 * @return ID of the connection.
2341 const struct GNUNET_HashCode *
2342 GMC_get_id (const struct MeshConnection *c)
2349 * Get the connection path.
2351 * @param c Connection to get the path from.
2353 * @return path used by the connection.
2355 const struct MeshPeerPath *
2356 GMC_get_path (const struct MeshConnection *c)
2358 if (GNUNET_NO == c->destroy)
2365 * Get the connection state.
2367 * @param c Connection to get the state from.
2369 * @return state of the connection.
2371 enum MeshConnectionState
2372 GMC_get_state (const struct MeshConnection *c)
2378 * Get the connection tunnel.
2380 * @param c Connection to get the tunnel from.
2382 * @return tunnel of the connection.
2384 struct MeshTunnel3 *
2385 GMC_get_tunnel (const struct MeshConnection *c)
2392 * Get free buffer space in a connection.
2394 * @param c Connection.
2395 * @param fwd Is query about FWD traffic?
2397 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2400 GMC_get_buffer (struct MeshConnection *c, int fwd)
2402 struct MeshFlowControl *fc;
2404 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2406 return (fc->queue_max - fc->queue_n);
2410 * Get how many messages have we allowed to send to us from a direction.
2412 * @param c Connection.
2413 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2415 * @return last_ack_sent - last_pid_recv
2418 GMC_get_allowed (struct MeshConnection *c, int fwd)
2420 struct MeshFlowControl *fc;
2422 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2423 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2427 return (fc->last_ack_sent - fc->last_pid_recv);
2431 * Get messages queued in a connection.
2433 * @param c Connection.
2434 * @param fwd Is query about FWD traffic?
2436 * @return Number of messages queued.
2439 GMC_get_qn (struct MeshConnection *c, int fwd)
2441 struct MeshFlowControl *fc;
2443 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2450 * Allow the connection to advertise a buffer of the given size.
2452 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2453 * allowing up to last_pid_recv + buffer.
2455 * @param c Connection.
2456 * @param buffer How many more messages the connection can accept.
2457 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2460 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2462 send_ack (c, buffer, fwd, GNUNET_NO);
2467 * Notify other peers on a connection of a broken link. Mark connections
2468 * to destroy after all traffic has been sent.
2470 * @param c Connection on which there has been a disconnection.
2471 * @param peer Peer that disconnected.
2474 GMC_notify_broken (struct MeshConnection *c,
2475 struct MeshPeer *peer)
2479 LOG (GNUNET_ERROR_TYPE_DEBUG,
2480 " notify broken on %s due to %s disconnect\n",
2481 GMC_2s (c), GMP_2s (peer));
2483 fwd = peer == get_prev_hop (c);
2485 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2487 /* Local shutdown, no one to notify about this. */
2491 if (GNUNET_NO == c->destroy)
2492 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2494 /* Connection will have at least one pending message
2495 * (the one we just scheduled), so no point in checking whether to
2496 * destroy immediately. */
2497 c->destroy = GNUNET_YES;
2498 c->state = MESH_CONNECTION_DESTROYED;
2501 * Cancel all queues, if no message is left, connection will be destroyed.
2503 connection_cancel_queues (c, !fwd);
2510 * Is this peer the first one on the connection?
2512 * @param c Connection.
2513 * @param fwd Is this about fwd traffic?
2515 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2518 GMC_is_origin (struct MeshConnection *c, int fwd)
2520 if (!fwd && c->path->length - 1 == c->own_pos )
2522 if (fwd && 0 == c->own_pos)
2529 * Is this peer the last one on the connection?
2531 * @param c Connection.
2532 * @param fwd Is this about fwd traffic?
2533 * Note that the ROOT is the terminal for BCK traffic!
2535 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2538 GMC_is_terminal (struct MeshConnection *c, int fwd)
2540 return GMC_is_origin (c, !fwd);
2545 * See if we are allowed to send by the next hop in the given direction.
2547 * @param c Connection.
2548 * @param fwd Is this about fwd traffic?
2550 * @return #GNUNET_YES in case it's OK to send.
2553 GMC_is_sendable (struct MeshConnection *c, int fwd)
2555 struct MeshFlowControl *fc;
2557 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2558 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2564 * Sends an already built message on a connection, properly registering
2565 * all used resources.
2567 * @param message Message to send. Function makes a copy of it.
2568 * If message is not hop-by-hop, decrements TTL of copy.
2569 * @param c Connection on which this message is transmitted.
2570 * @param fwd Is this a fwd message?
2571 * @param force Force the connection to accept the message (buffer overfill).
2572 * @param cont Continuation called once message is sent. Can be NULL.
2573 * @param cont_cls Closure for @c cont.
2575 * @return Handle to cancel the message before it's sent.
2576 * NULL on error or if @c cont is NULL.
2577 * Invalid on @c cont call.
2579 struct MeshConnectionQueue *
2580 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2581 struct MeshConnection *c, int fwd, int force,
2582 GMC_sent cont, void *cont_cls)
2584 struct MeshFlowControl *fc;
2585 struct MeshConnectionQueue *q;
2591 size = ntohs (message->size);
2592 data = GNUNET_malloc (size);
2593 memcpy (data, message, size);
2594 type = ntohs (message->type);
2595 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2596 GM_m2s (type), size, GMC_2s (c));
2598 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2599 droppable = GNUNET_NO == force;
2602 struct GNUNET_MESH_Encrypted *emsg;
2603 struct GNUNET_MESH_KX *kmsg;
2604 struct GNUNET_MESH_ACK *amsg;
2605 struct GNUNET_MESH_Poll *pmsg;
2606 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2607 struct GNUNET_MESH_ConnectionBroken *bmsg;
2610 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2611 emsg = (struct GNUNET_MESH_Encrypted *) data;
2612 ttl = ntohl (emsg->ttl);
2615 GNUNET_break_op (0);
2620 emsg->ttl = htonl (ttl - 1);
2621 emsg->pid = htonl (fc->next_pid++);
2622 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2623 if (GNUNET_YES == droppable)
2626 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2627 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2628 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2632 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2634 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2636 GMC_start_poll (c, fwd);
2640 case GNUNET_MESSAGE_TYPE_MESH_KX:
2641 kmsg = (struct GNUNET_MESH_KX *) data;
2645 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2646 amsg = (struct GNUNET_MESH_ACK *) data;
2648 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2649 droppable = GNUNET_NO;
2652 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2653 pmsg = (struct GNUNET_MESH_Poll *) data;
2655 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2656 droppable = GNUNET_NO;
2659 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2660 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2665 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2666 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2671 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2672 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2673 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2682 if (fc->queue_n > fc->queue_max && droppable)
2684 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2687 LOG (GNUNET_ERROR_TYPE_DEBUG,
2688 "queue full: %u/%u\n",
2689 fc->queue_n, fc->queue_max);
2690 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2696 return NULL; /* Drop this message */
2699 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2700 c->pending_messages++;
2702 q = GNUNET_new (struct MeshConnectionQueue);
2703 q->forced = !droppable;
2704 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2708 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2714 q->cont_cls = cont_cls;
2720 * Cancel a previously sent message while it's in the queue.
2722 * ONLY can be called before the continuation given to the send function
2723 * is called. Once the continuation is called, the message is no longer in the
2726 * @param q Handle to the queue.
2729 GMC_cancel (struct MeshConnectionQueue *q)
2731 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2733 /* queue destroy calls message_sent, which calls q->cont and frees q */
2734 GMP_queue_destroy (q->q, GNUNET_YES);
2739 * Sends a CREATE CONNECTION message for a path to a peer.
2740 * Changes the connection and tunnel states if necessary.
2742 * @param connection Connection to create.
2745 GMC_send_create (struct MeshConnection *connection)
2747 enum MeshTunnel3CState state;
2750 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2751 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2753 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2754 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2755 connection, connection->pending_messages);
2756 connection->pending_messages++;
2758 GMP_queue_add (get_next_hop (connection), NULL,
2759 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2760 size, connection, GNUNET_YES, &message_sent, NULL);
2762 state = GMT_get_cstate (connection->t);
2763 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2764 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2765 if (MESH_CONNECTION_NEW == connection->state)
2766 connection_change_state (connection, MESH_CONNECTION_SENT);
2771 * Send a message to all peers in this connection that the connection
2772 * is no longer valid.
2774 * If some peer should not receive the message, it should be zero'ed out
2775 * before calling this function.
2777 * @param c The connection whose peers to notify.
2780 GMC_send_destroy (struct MeshConnection *c)
2782 struct GNUNET_MESH_ConnectionDestroy msg;
2784 if (GNUNET_YES == c->destroy)
2787 msg.header.size = htons (sizeof (msg));
2788 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2790 LOG (GNUNET_ERROR_TYPE_DEBUG,
2791 " sending connection destroy for connection %s\n",
2794 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2795 GMC_send_prebuilt_message (&msg.header, c,
2796 GNUNET_YES, GNUNET_YES, NULL, NULL);
2797 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2798 GMC_send_prebuilt_message (&msg.header, c,
2799 GNUNET_NO, GNUNET_YES, NULL, NULL);
2800 c->destroy = GNUNET_YES;
2801 c->state = MESH_CONNECTION_DESTROYED;
2806 * @brief Start a polling timer for the connection.
2808 * When a neighbor does not accept more traffic on the connection it could be
2809 * caused by a simple congestion or by a lost ACK. Polling enables to check
2810 * for the lastest ACK status for a connection.
2812 * @param c Connection.
2813 * @param fwd Should we poll in the FWD direction?
2816 GMC_start_poll (struct MeshConnection *c, int fwd)
2818 struct MeshFlowControl *fc;
2820 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2821 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
2823 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
2825 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
2826 fc->poll_task, fc->poll_msg);
2829 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
2830 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2837 * @brief Stop polling a connection for ACKs.
2839 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2841 * @param c Connection.
2842 * @param fwd Should we stop the poll in the FWD direction?
2845 GMC_stop_poll (struct MeshConnection *c, int fwd)
2847 struct MeshFlowControl *fc;
2849 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2850 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2852 GNUNET_SCHEDULER_cancel (fc->poll_task);
2853 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2858 * Get a (static) string for a connection.
2860 * @param c Connection.
2863 GMC_2s (struct MeshConnection *c)
2870 static char buf[128];
2872 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2875 return GNUNET_h2s (&c->id);