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))
449 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
450 GMC_2s (c), GM_f2s (fwd));
455 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
456 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
458 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
459 GM_f2s (fwd), GMC_2s (c));
461 /* Check if we need to transmit the ACK. */
462 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
463 if (3 < delta && buffer < delta && GNUNET_NO == force)
465 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
466 LOG (GNUNET_ERROR_TYPE_DEBUG,
467 " last pid recv: %u, last ack sent: %u\n",
468 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
472 /* Ok, ACK might be necessary, what PID to ACK? */
473 ack = prev_fc->last_pid_recv + buffer;
474 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 " last pid %u, last ack %u, qmax %u, q %u\n",
477 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
478 next_fc->queue_max, next_fc->queue_n);
479 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
485 /* Check if message is already in queue */
486 if (NULL != prev_fc->ack_msg)
488 if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
490 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
491 GMC_cancel (prev_fc->ack_msg);
492 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
496 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
501 prev_fc->last_ack_sent = ack;
503 /* Build ACK message and send on connection */
504 msg.header.size = htons (sizeof (msg));
505 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
506 msg.ack = htonl (ack);
509 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
516 * Callback called when a queued message is sent.
518 * Calculates the average time and connection packet tracking.
520 * @param cls Closure (ConnectionQueue Handle).
521 * @param c Connection this message was on.
522 * @param type Type of message sent.
523 * @param fwd Was this a FWD going message?
524 * @param size Size of the message.
525 * @param wait Time spent waiting for core (only the time for THIS message)
528 message_sent (void *cls,
529 struct MeshConnection *c, uint16_t type,
530 int fwd, size_t size,
531 struct GNUNET_TIME_Relative wait)
533 struct MeshConnectionPerformance *p;
534 struct MeshFlowControl *fc;
535 struct MeshConnectionQueue *q = cls;
539 fc = fwd ? &c->fwd_fc : &c->bck_fc;
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
544 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
550 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
551 q->cont (q->cont_cls, c, q, type, fwd, size);
555 else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
557 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
564 c->pending_messages--;
565 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
567 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
571 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
574 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
576 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
577 if (GNUNET_NO == forced)
580 LOG (GNUNET_ERROR_TYPE_DEBUG,
581 "! accounting pid %u\n",
586 LOG (GNUNET_ERROR_TYPE_DEBUG,
587 "! forced, Q_N not accounting pid %u\n",
590 GMC_send_ack (c, fwd, GNUNET_NO);
593 case GNUNET_MESSAGE_TYPE_MESH_POLL:
597 case GNUNET_MESSAGE_TYPE_MESH_ACK:
604 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
607 return; /* Only endpoints are interested in timing. */
610 usecsperbyte = ((double) wait.rel_value_us) / size;
611 if (p->size == AVG_MSGS)
613 /* Array is full. Substract oldest value, add new one and store. */
614 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
615 p->usecsperbyte[p->idx] = usecsperbyte;
616 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
620 /* Array not yet full. Add current value to avg and store. */
621 p->usecsperbyte[p->idx] = usecsperbyte;
623 p->avg += p->usecsperbyte[p->idx];
627 p->idx = (p->idx + 1) % AVG_MSGS;
632 * Get the previous hop in a connection
634 * @param c Connection.
636 * @return Previous peer in the connection.
638 static struct MeshPeer *
639 get_prev_hop (const struct MeshConnection *c)
643 if (0 == c->own_pos || c->path->length < 2)
644 id = c->path->peers[0];
646 id = c->path->peers[c->own_pos - 1];
648 return GMP_get_short (id);
653 * Get the next hop in a connection
655 * @param c Connection.
657 * @return Next peer in the connection.
659 static struct MeshPeer *
660 get_next_hop (const struct MeshConnection *c)
664 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
665 id = c->path->peers[c->path->length - 1];
667 id = c->path->peers[c->own_pos + 1];
669 return GMP_get_short (id);
674 * Get the hop in a connection.
676 * @param c Connection.
677 * @param fwd Next hop?
679 * @return Next peer in the connection.
681 static struct MeshPeer *
682 get_hop (struct MeshConnection *c, int fwd)
685 return get_next_hop (c);
686 return get_prev_hop (c);
691 * Is traffic coming from this sender 'FWD' traffic?
693 * @param c Connection to check.
694 * @param sender Peer identity of neighbor.
696 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
697 * the traffic is 'FWD'.
698 * #GNUNET_NO for BCK.
699 * #GNUNET_SYSERR for errors.
702 is_fwd (const struct MeshConnection *c,
703 const struct GNUNET_PeerIdentity *sender)
707 id = GNUNET_PEER_search (sender);
708 if (GMP_get_short_id (get_prev_hop (c)) == id)
711 if (GMP_get_short_id (get_next_hop (c)) == id)
715 return GNUNET_SYSERR;
720 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
721 * or a first CONNECTION_ACK directed to us.
723 * @param connection Connection to confirm.
724 * @param fwd Should we send it FWD? (root->dest)
725 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
728 send_connection_ack (struct MeshConnection *connection, int fwd)
730 struct MeshTunnel3 *t;
733 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
735 GMP_queue_add (get_hop (connection, fwd), NULL,
736 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
737 sizeof (struct GNUNET_MESH_ConnectionACK),
738 connection, fwd, &message_sent, NULL);
739 connection->pending_messages++;
740 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
741 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
742 if (MESH_CONNECTION_READY != connection->state)
743 connection_change_state (connection, MESH_CONNECTION_SENT);
748 * Send a notification that a connection is broken.
750 * @param c Connection that is broken.
751 * @param id1 Peer that has disconnected.
752 * @param id2 Peer that has disconnected.
753 * @param fwd Direction towards which to send it.
756 send_broken (struct MeshConnection *c,
757 const struct GNUNET_PeerIdentity *id1,
758 const struct GNUNET_PeerIdentity *id2,
761 struct GNUNET_MESH_ConnectionBroken msg;
763 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
764 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
768 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
773 * Send keepalive packets for a connection.
775 * @param c Connection to keep alive..
776 * @param fwd Is this a FWD keepalive? (owner -> dest).
779 connection_keepalive (struct MeshConnection *c, int fwd)
781 struct GNUNET_MESH_ConnectionKeepAlive *msg;
782 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
785 LOG (GNUNET_ERROR_TYPE_DEBUG,
786 "sending %s keepalive for connection %s]\n",
787 GM_f2s (fwd), GMC_2s (c));
789 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
790 msg->header.size = htons (size);
791 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
793 msg->reserved = htonl (0);
795 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
800 * Send CONNECTION_{CREATE/ACK} packets for a connection.
802 * @param c Connection for which to send the message.
803 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
806 connection_recreate (struct MeshConnection *c, int fwd)
808 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
812 send_connection_ack (c, GNUNET_NO);
817 * Generic connection timer management.
818 * Depending on the role of the peer in the connection will send the
819 * appropriate message (build or keepalive)
821 * @param c Conncetion to maintain.
825 connection_maintain (struct MeshConnection *c, int fwd)
827 if (GNUNET_NO != c->destroy)
830 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
832 /* TODO DHT GET with RO_BART */
837 case MESH_CONNECTION_NEW:
840 case MESH_CONNECTION_SENT:
841 connection_recreate (c, fwd);
843 case MESH_CONNECTION_READY:
844 connection_keepalive (c, fwd);
853 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
855 struct MeshConnection *c = cls;
856 struct GNUNET_TIME_Relative delay;
858 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
859 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
862 connection_maintain (c, GNUNET_YES);
863 delay = c->state == MESH_CONNECTION_READY ?
864 refresh_connection_time : create_connection_time;
865 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
866 &connection_fwd_keepalive,
872 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
874 struct MeshConnection *c = cls;
875 struct GNUNET_TIME_Relative delay;
877 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
878 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
881 connection_maintain (c, GNUNET_NO);
882 delay = c->state == MESH_CONNECTION_READY ?
883 refresh_connection_time : create_connection_time;
884 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
885 &connection_bck_keepalive,
891 * @brief Re-initiate traffic on this connection if necessary.
893 * Check if there is traffic queued towards this peer
894 * and the core transmit handle is NULL (traffic was stalled).
895 * If so, call core tmt rdy.
897 * @param c Connection on which initiate traffic.
898 * @param fwd Is this about fwd traffic?
901 connection_unlock_queue (struct MeshConnection *c, int fwd)
903 struct MeshPeer *peer;
905 LOG (GNUNET_ERROR_TYPE_DEBUG,
906 "connection_unlock_queue %s on %s\n",
907 GM_f2s (fwd), GMC_2s (c));
909 if (GMC_is_terminal (c, fwd))
911 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
915 peer = get_hop (c, fwd);
916 GMP_queue_unlock (peer, c);
921 * Cancel all transmissions that belong to a certain connection.
923 * If the connection is scheduled for destruction and no more messages are left,
924 * the connection will be destroyed by the continuation call.
926 * @param c Connection which to cancel. Might be destroyed during this call.
927 * @param fwd Cancel fwd traffic?
930 connection_cancel_queues (struct MeshConnection *c, int fwd)
932 struct MeshFlowControl *fc;
933 struct MeshPeer *peer;
935 LOG (GNUNET_ERROR_TYPE_DEBUG,
936 " *** Cancel %s queues for connection %s\n",
937 GM_f2s (fwd), GMC_2s (c));
944 fc = fwd ? &c->fwd_fc : &c->bck_fc;
945 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
947 GNUNET_SCHEDULER_cancel (fc->poll_task);
948 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
949 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
951 peer = get_hop (c, fwd);
952 GMP_queue_cancel (peer, c);
957 * Function called if a connection has been stalled for a while,
958 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
960 * @param cls Closure (poll ctx).
961 * @param tc TaskContext.
964 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
968 * Callback called when a queued POLL message is sent.
970 * @param cls Closure (FC).
971 * @param c Connection this message was on.
972 * @param q Queue handler this call invalidates.
973 * @param type Type of message sent.
974 * @param fwd Was this a FWD going message?
975 * @param size Size of the message.
978 poll_sent (void *cls,
979 struct MeshConnection *c,
980 struct MeshConnectionQueue *q,
981 uint16_t type, int fwd, size_t size)
983 struct MeshFlowControl *fc = cls;
987 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
990 LOG (GNUNET_ERROR_TYPE_DEBUG,
991 " *** POLL sent for , scheduling new one!\n");
993 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
994 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
995 &connection_poll, fc);
996 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1001 * Function called if a connection has been stalled for a while,
1002 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1004 * @param cls Closure (poll ctx).
1005 * @param tc TaskContext.
1008 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1010 struct MeshFlowControl *fc = cls;
1011 struct GNUNET_MESH_Poll msg;
1012 struct MeshConnection *c;
1014 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1015 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1021 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1022 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
1023 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1024 fc == &c->fwd_fc ? "FWD" : "BCK");
1026 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1027 msg.header.size = htons (sizeof (msg));
1028 msg.pid = htonl (fc->last_pid_sent);
1029 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1030 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1031 fc == &c->fwd_fc, GNUNET_YES,
1037 * Timeout function due to lack of keepalive/traffic from the owner.
1038 * Destroys connection if called.
1040 * @param cls Closure (connection to destroy).
1041 * @param tc TaskContext.
1044 connection_fwd_timeout (void *cls,
1045 const struct GNUNET_SCHEDULER_TaskContext *tc)
1047 struct MeshConnection *c = cls;
1049 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1050 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1053 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1055 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1066 * Timeout function due to lack of keepalive/traffic from the destination.
1067 * Destroys connection if called.
1069 * @param cls Closure (connection to destroy).
1070 * @param tc TaskContext
1073 connection_bck_timeout (void *cls,
1074 const struct GNUNET_SCHEDULER_TaskContext *tc)
1076 struct MeshConnection *c = cls;
1078 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1079 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1082 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1085 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1096 * Resets the connection timeout task, some other message has done the
1098 * - For the first peer on the direction this means to send
1099 * a keepalive or a path confirmation message (either create or ACK).
1100 * - For all other peers, this means to destroy the connection,
1101 * due to lack of activity.
1102 * Starts the timeout if no timeout was running (connection just created).
1104 * @param c Connection whose timeout to reset.
1105 * @param fwd Is this forward?
1107 * TODO use heap to improve efficiency of scheduler.
1110 connection_reset_timeout (struct MeshConnection *c, int fwd)
1112 GNUNET_SCHEDULER_TaskIdentifier *ti;
1113 GNUNET_SCHEDULER_Task f;
1115 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1117 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1119 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1120 GNUNET_SCHEDULER_cancel (*ti);
1122 if (GMC_is_origin (c, fwd)) /* Startpoint */
1124 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1125 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1127 else /* Relay, endpoint. */
1129 struct GNUNET_TIME_Relative delay;
1131 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1132 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1133 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1139 * Add the connection to the list of both neighbors.
1141 * @param c Connection.
1143 * @return #GNUNET_OK if everything went fine
1144 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1147 register_neighbors (struct MeshConnection *c)
1149 struct MeshPeer *next_peer;
1150 struct MeshPeer *prev_peer;
1152 next_peer = get_next_hop (c);
1153 prev_peer = get_prev_hop (c);
1155 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1156 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1158 if (GMC_is_origin (c, GNUNET_YES))
1159 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1160 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1162 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1164 GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
1165 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1166 GMP_2s (next_peer), GMP_is_neighbor (next_peer));
1167 return GNUNET_SYSERR;
1170 GMP_add_connection (next_peer, c);
1171 GMP_add_connection (prev_peer, c);
1178 * Remove the connection from the list of both neighbors.
1180 * @param c Connection.
1183 unregister_neighbors (struct MeshConnection *c)
1185 struct MeshPeer *peer;
1187 peer = get_next_hop (c);
1188 if (GNUNET_OK != GMP_remove_connection (peer, c))
1190 GNUNET_break (MESH_CONNECTION_NEW == c->state
1191 || MESH_CONNECTION_DESTROYED == c->state);
1192 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1193 if (NULL != c->t) GMT_debug (c->t);
1196 peer = get_prev_hop (c);
1197 if (GNUNET_OK != GMP_remove_connection (peer, c))
1199 GNUNET_break (MESH_CONNECTION_NEW == c->state
1200 || MESH_CONNECTION_DESTROYED == c->state);
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1202 if (NULL != c->t) GMT_debug (c->t);
1208 * Bind the connection to the peer and the tunnel to that peer.
1210 * If the peer has no tunnel, create one. Update tunnel and connection
1211 * data structres to reflect new status.
1213 * @param c Connection.
1217 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1219 GMP_add_tunnel (peer);
1220 c->t = GMP_get_tunnel (peer);
1221 GMT_add_connection (c->t, c);
1226 * Builds a path from a PeerIdentity array.
1228 * @param peers PeerIdentity array.
1229 * @param size Size of the @c peers array.
1230 * @param own_pos Output parameter: own position in the path.
1232 * @return Fixed and shortened path.
1234 static struct MeshPeerPath *
1235 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1237 unsigned int *own_pos)
1239 struct MeshPeerPath *path;
1240 GNUNET_PEER_Id shortid;
1243 unsigned int offset;
1246 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1247 path = path_new (size);
1250 for (i = 0; i < size; i++)
1252 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1253 i, GNUNET_i2s (&peers[i]));
1254 shortid = GNUNET_PEER_intern (&peers[i]);
1256 /* Check for loops / duplicates */
1257 for (j = 0; j < i - offset; j++)
1259 if (path->peers[j] == shortid)
1261 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1263 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset);
1264 GNUNET_PEER_change_rc (shortid, -1);
1267 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1268 path->peers[i - offset] = shortid;
1269 if (path->peers[i] == myid)
1272 path->length -= offset;
1274 if (path->peers[*own_pos] != myid)
1276 /* create path: self not found in path through self */
1277 GNUNET_break_op (0);
1278 path_destroy (path);
1285 /******************************************************************************/
1286 /******************************** API ***********************************/
1287 /******************************************************************************/
1290 * Core handler for connection creation.
1292 * @param cls Closure (unused).
1293 * @param peer Sender (neighbor).
1294 * @param message Message.
1296 * @return GNUNET_OK to keep the connection open,
1297 * GNUNET_SYSERR to close it (signal serious error)
1300 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1301 const struct GNUNET_MessageHeader *message)
1303 struct GNUNET_MESH_ConnectionCreate *msg;
1304 struct GNUNET_PeerIdentity *id;
1305 struct GNUNET_HashCode *cid;
1306 struct MeshPeerPath *path;
1307 struct MeshPeer *dest_peer;
1308 struct MeshPeer *orig_peer;
1309 struct MeshConnection *c;
1310 unsigned int own_pos;
1313 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1317 size = ntohs (message->size);
1318 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1320 GNUNET_break_op (0);
1324 /* Calculate hops */
1325 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1326 if (size % sizeof (struct GNUNET_PeerIdentity))
1328 GNUNET_break_op (0);
1331 size /= sizeof (struct GNUNET_PeerIdentity);
1334 GNUNET_break_op (0);
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1339 /* Get parameters */
1340 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1342 id = (struct GNUNET_PeerIdentity *) &msg[1];
1343 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s (%s->).\n",
1344 GNUNET_h2s (cid), GNUNET_i2s (id));
1346 /* Create connection */
1347 c = connection_get (cid);
1350 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1354 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1355 GMP_add_path_to_all (path, GNUNET_NO);
1356 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1357 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1360 path_destroy (path);
1363 connection_reset_timeout (c, GNUNET_YES);
1367 path = path_duplicate (c->path);
1369 if (MESH_CONNECTION_NEW == c->state)
1370 connection_change_state (c, MESH_CONNECTION_SENT);
1372 /* Remember peers */
1373 dest_peer = GMP_get (&id[size - 1]);
1374 orig_peer = GMP_get (&id[0]);
1376 /* Is it a connection to us? */
1377 if (c->own_pos == size - 1)
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1380 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1382 add_to_peer (c, orig_peer);
1383 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1384 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1386 send_connection_ack (c, GNUNET_NO);
1387 if (MESH_CONNECTION_SENT == c->state)
1388 connection_change_state (c, MESH_CONNECTION_ACK);
1390 /* Keep tunnel alive in direction dest->owner*/
1391 if (GNUNET_SCHEDULER_NO_TASK == c->bck_maintenance_task)
1393 c->bck_maintenance_task =
1394 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1395 &connection_bck_keepalive, c);
1400 /* It's for somebody else! Retransmit. */
1401 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1402 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1403 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1404 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES,
1407 path_destroy (path);
1413 * Core handler for path confirmations.
1415 * @param cls closure
1416 * @param message message
1417 * @param peer peer identity this notification is about
1419 * @return GNUNET_OK to keep the connection open,
1420 * GNUNET_SYSERR to close it (signal serious error)
1423 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1424 const struct GNUNET_MessageHeader *message)
1426 struct GNUNET_MESH_ConnectionACK *msg;
1427 struct MeshConnection *c;
1428 struct MeshPeerPath *p;
1429 struct MeshPeer *pi;
1430 enum MeshConnectionState oldstate;
1433 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1435 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1437 GNUNET_h2s (&msg->cid));
1438 c = connection_get (&msg->cid);
1441 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1447 if (GNUNET_NO != c->destroy)
1449 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1453 oldstate = c->state;
1454 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1455 pi = GMP_get (peer);
1456 if (get_next_hop (c) == pi)
1458 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1460 if (MESH_CONNECTION_SENT == oldstate)
1461 connection_change_state (c, MESH_CONNECTION_ACK);
1463 else if (get_prev_hop (c) == pi)
1465 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1467 connection_change_state (c, MESH_CONNECTION_READY);
1471 GNUNET_break_op (0);
1475 connection_reset_timeout (c, fwd);
1477 /* Add path to peers? */
1481 GMP_add_path_to_all (p, GNUNET_YES);
1488 /* Message for us as creator? */
1489 if (GMC_is_origin (c, GNUNET_YES))
1491 if (GNUNET_NO != fwd)
1493 GNUNET_break_op (0);
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1498 /* If just created, cancel the short timeout and start a long one */
1499 if (MESH_CONNECTION_SENT == oldstate)
1500 connection_reset_timeout (c, GNUNET_YES);
1502 /* Change connection state */
1503 connection_change_state (c, MESH_CONNECTION_READY);
1504 send_connection_ack (c, GNUNET_YES);
1506 /* Change tunnel state, trigger KX */
1507 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1508 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1513 /* Message for us as destination? */
1514 if (GMC_is_terminal (c, GNUNET_YES))
1516 if (GNUNET_YES != fwd)
1518 GNUNET_break_op (0);
1521 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1523 /* If just created, cancel the short timeout and start a long one */
1524 if (MESH_CONNECTION_ACK == oldstate)
1525 connection_reset_timeout (c, GNUNET_NO);
1527 /* Change tunnel state */
1528 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1529 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1534 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1535 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1541 * Core handler for notifications of broken paths
1543 * @param cls Closure (unused).
1544 * @param id Peer identity of sending neighbor.
1545 * @param message Message.
1547 * @return GNUNET_OK to keep the connection open,
1548 * GNUNET_SYSERR to close it (signal serious error)
1551 GMC_handle_broken (void* cls,
1552 const struct GNUNET_PeerIdentity* id,
1553 const struct GNUNET_MessageHeader* message)
1555 struct GNUNET_MESH_ConnectionBroken *msg;
1556 struct MeshConnection *c;
1559 LOG (GNUNET_ERROR_TYPE_DEBUG,
1560 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1561 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1562 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1563 GNUNET_i2s (&msg->peer1));
1564 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1565 GNUNET_i2s (&msg->peer2));
1566 c = connection_get (&msg->cid);
1569 GNUNET_break_op (0);
1573 fwd = is_fwd (c, id);
1574 if (GMC_is_terminal (c, fwd))
1576 if (0 < c->pending_messages)
1577 c->destroy = GNUNET_YES;
1583 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1584 c->destroy = GNUNET_YES;
1585 connection_cancel_queues (c, !fwd);
1594 * Core handler for tunnel destruction
1596 * @param cls Closure (unused).
1597 * @param peer Peer identity of sending neighbor.
1598 * @param message Message.
1600 * @return GNUNET_OK to keep the connection open,
1601 * GNUNET_SYSERR to close it (signal serious error)
1604 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1605 const struct GNUNET_MessageHeader *message)
1607 struct GNUNET_MESH_ConnectionDestroy *msg;
1608 struct MeshConnection *c;
1611 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1612 LOG (GNUNET_ERROR_TYPE_DEBUG,
1613 "Got a CONNECTION DESTROY message from %s\n",
1615 LOG (GNUNET_ERROR_TYPE_DEBUG,
1616 " for connection %s\n",
1617 GNUNET_h2s (&msg->cid));
1618 c = connection_get (&msg->cid);
1621 /* Probably already got the message from another path,
1622 * destroyed the tunnel and retransmitted to children.
1625 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1627 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1630 fwd = is_fwd (c, peer);
1631 if (GNUNET_SYSERR == fwd)
1633 GNUNET_break_op (0); /* FIXME */
1636 if (GNUNET_NO == GMC_is_terminal (c, fwd))
1637 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1638 else if (0 == c->pending_messages)
1640 LOG (GNUNET_ERROR_TYPE_DEBUG, "! directly destroying connection!\n");
1644 c->destroy = GNUNET_YES;
1645 c->state = MESH_CONNECTION_DESTROYED;
1648 GMT_remove_connection (c->t, c);
1656 * Generic handler for mesh network encrypted traffic.
1658 * @param peer Peer identity this notification is about.
1659 * @param msg Encrypted message.
1661 * @return GNUNET_OK to keep the connection open,
1662 * GNUNET_SYSERR to close it (signal serious error)
1665 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1666 const struct GNUNET_MESH_Encrypted *msg)
1668 struct MeshConnection *c;
1669 struct MeshPeer *neighbor;
1670 struct MeshFlowControl *fc;
1671 GNUNET_PEER_Id peer_id;
1679 size = ntohs (msg->header.size);
1681 sizeof (struct GNUNET_MESH_Encrypted) +
1682 sizeof (struct GNUNET_MessageHeader))
1684 GNUNET_break_op (0);
1687 type = ntohs (msg->header.type);
1688 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1689 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1690 GM_m2s (type), ntohl (msg->pid), GNUNET_i2s (peer));
1692 /* Check connection */
1693 c = connection_get (&msg->cid);
1696 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1697 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
1698 GNUNET_h2s (&msg->cid));
1702 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1704 /* Check if origin is as expected */
1705 neighbor = get_prev_hop (c);
1706 peer_id = GNUNET_PEER_search (peer);
1707 if (peer_id == GMP_get_short_id (neighbor))
1713 neighbor = get_next_hop (c);
1714 if (peer_id == GMP_get_short_id (neighbor))
1720 /* Unexpected peer sending traffic on a connection. */
1721 GNUNET_break_op (0);
1727 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1728 pid = ntohl (msg->pid);
1729 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1731 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1732 LOG (GNUNET_ERROR_TYPE_DEBUG,
1733 "WARNING Received PID %u, (prev %u), ACK %u\n",
1734 pid, fc->last_pid_recv, fc->last_ack_sent);
1737 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1739 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1740 LOG (GNUNET_ERROR_TYPE_DEBUG,
1741 " Pid %u not expected (%u+), dropping!\n",
1742 pid, fc->last_pid_recv + 1);
1745 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1746 connection_change_state (c, MESH_CONNECTION_READY);
1747 connection_reset_timeout (c, fwd);
1748 fc->last_pid_recv = pid;
1750 /* Is this message for us? */
1751 if (GMC_is_terminal (c, fwd))
1753 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1754 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1758 GNUNET_break (GNUNET_NO != c->destroy);
1761 fc->last_pid_recv = pid;
1762 GMT_handle_encrypted (c->t, msg);
1763 GMC_send_ack (c, fwd, GNUNET_NO);
1767 /* Message not for us: forward to next hop */
1768 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1769 ttl = ntohl (msg->ttl);
1770 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1773 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1774 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1775 GMC_send_ack (c, fwd, GNUNET_NO);
1779 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1780 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1786 * Generic handler for mesh network encrypted traffic.
1788 * @param peer Peer identity this notification is about.
1789 * @param msg Encrypted message.
1791 * @return GNUNET_OK to keep the connection open,
1792 * GNUNET_SYSERR to close it (signal serious error)
1795 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1796 const struct GNUNET_MESH_KX *msg)
1798 struct MeshConnection *c;
1799 struct MeshPeer *neighbor;
1800 GNUNET_PEER_Id peer_id;
1806 size = ntohs (msg->header.size);
1808 sizeof (struct GNUNET_MESH_Encrypted) +
1809 sizeof (struct GNUNET_MessageHeader))
1811 GNUNET_break_op (0);
1814 type = ntohs (msg->header.type);
1815 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1816 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1817 GM_m2s (type), GNUNET_i2s (peer));
1819 /* Check connection */
1820 c = connection_get (&msg->cid);
1823 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1824 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING kx on unknown connection %s\n",
1825 GNUNET_h2s (&msg->cid));
1828 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
1830 /* Check if origin is as expected */
1831 neighbor = get_prev_hop (c);
1832 peer_id = GNUNET_PEER_search (peer);
1833 if (peer_id == GMP_get_short_id (neighbor))
1839 neighbor = get_next_hop (c);
1840 if (peer_id == GMP_get_short_id (neighbor))
1846 /* Unexpected peer sending traffic on a connection. */
1847 GNUNET_break_op (0);
1852 /* Count as connection confirmation. */
1853 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1855 connection_change_state (c, MESH_CONNECTION_READY);
1858 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1859 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1862 connection_reset_timeout (c, fwd);
1864 /* Is this message for us? */
1865 if (GMC_is_terminal (c, fwd))
1867 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1868 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1874 GMT_handle_kx (c->t, &msg[1].header);
1878 /* Message not for us: forward to next hop */
1879 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1880 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1881 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1888 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1890 * @param cls Closure (unused).
1891 * @param message Message received.
1892 * @param peer Peer who sent the message.
1894 * @return GNUNET_OK to keep the connection open,
1895 * GNUNET_SYSERR to close it (signal serious error)
1898 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1899 const struct GNUNET_MessageHeader *message)
1901 return handle_mesh_encrypted (peer,
1902 (struct GNUNET_MESH_Encrypted *)message);
1907 * Core handler for key exchange traffic (ephemeral key, ping, pong).
1909 * @param cls Closure (unused).
1910 * @param message Message received.
1911 * @param peer Peer who sent the message.
1913 * @return GNUNET_OK to keep the connection open,
1914 * GNUNET_SYSERR to close it (signal serious error)
1917 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1918 const struct GNUNET_MessageHeader *message)
1920 return handle_mesh_kx (peer,
1921 (struct GNUNET_MESH_KX *) message);
1926 * Core handler for mesh network traffic point-to-point acks.
1928 * @param cls closure
1929 * @param message message
1930 * @param peer peer identity this notification is about
1932 * @return GNUNET_OK to keep the connection open,
1933 * GNUNET_SYSERR to close it (signal serious error)
1936 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1937 const struct GNUNET_MessageHeader *message)
1939 struct GNUNET_MESH_ACK *msg;
1940 struct MeshConnection *c;
1941 struct MeshFlowControl *fc;
1946 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1947 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1949 msg = (struct GNUNET_MESH_ACK *) message;
1951 c = connection_get (&msg->cid);
1955 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1960 /* Is this a forward or backward ACK? */
1961 id = GNUNET_PEER_search (peer);
1962 if (GMP_get_short_id (get_next_hop (c)) == id)
1964 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1968 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1970 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1976 GNUNET_break_op (0);
1980 ack = ntohl (msg->ack);
1981 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1982 ack, fc->last_ack_recv);
1983 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
1984 fc->last_ack_recv = ack;
1986 /* Cancel polling if the ACK is big enough. */
1987 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1988 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1990 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1991 GNUNET_SCHEDULER_cancel (fc->poll_task);
1992 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1993 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1996 connection_unlock_queue (c, fwd);
2003 * Core handler for mesh network traffic point-to-point ack polls.
2005 * @param cls closure
2006 * @param message message
2007 * @param peer peer identity this notification is about
2009 * @return GNUNET_OK to keep the connection open,
2010 * GNUNET_SYSERR to close it (signal serious error)
2013 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2014 const struct GNUNET_MessageHeader *message)
2016 struct GNUNET_MESH_Poll *msg;
2017 struct MeshConnection *c;
2018 struct MeshFlowControl *fc;
2023 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2024 LOG (GNUNET_ERROR_TYPE_DEBUG,
2025 "Got a POLL message from %s!\n",
2028 msg = (struct GNUNET_MESH_Poll *) message;
2030 c = connection_get (&msg->cid);
2034 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2036 LOG (GNUNET_ERROR_TYPE_DEBUG,
2037 "WARNING POLL message on unknown connection %s!\n",
2038 GNUNET_h2s (&msg->cid));
2042 /* Is this a forward or backward ACK?
2043 * Note: a poll should never be needed in a loopback case,
2044 * since there is no possiblility of packet loss there, so
2045 * this way of discerining FWD/BCK should not be a problem.
2047 id = GNUNET_PEER_search (peer);
2048 if (GMP_get_short_id (get_next_hop (c)) == id)
2050 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2053 else if (GMP_get_short_id (get_prev_hop (c)) == id)
2055 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2060 GNUNET_break_op (0);
2064 pid = ntohl (msg->pid);
2065 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2066 fc->last_pid_recv = pid;
2067 fwd = fc == &c->bck_fc;
2068 GMC_send_ack (c, fwd, GNUNET_YES);
2075 * Core handler for mesh keepalives.
2077 * @param cls closure
2078 * @param message message
2079 * @param peer peer identity this notification is about
2080 * @return GNUNET_OK to keep the connection open,
2081 * GNUNET_SYSERR to close it (signal serious error)
2083 * TODO: Check who we got this from, to validate route.
2086 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2087 const struct GNUNET_MessageHeader *message)
2089 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2090 struct MeshConnection *c;
2091 struct MeshPeer *neighbor;
2092 GNUNET_PEER_Id peer_id;
2095 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2096 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
2099 c = connection_get (&msg->cid);
2102 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2107 /* Check if origin is as expected TODO refactor and reuse */
2108 peer_id = GNUNET_PEER_search (peer);
2109 neighbor = get_prev_hop (c);
2110 if (peer_id == GMP_get_short_id (neighbor))
2116 neighbor = get_next_hop (c);
2117 if (peer_id == GMP_get_short_id (neighbor))
2123 GNUNET_break_op (0);
2128 connection_change_state (c, MESH_CONNECTION_READY);
2129 connection_reset_timeout (c, fwd);
2131 if (GMC_is_terminal (c, fwd))
2134 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2135 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2142 * Send an ACK on the appropriate connection/channel, depending on
2143 * the direction and the position of the peer.
2145 * @param c Which connection to send the hop-by-hop ACK.
2146 * @param fwd Is this a fwd ACK? (will go dest->root).
2147 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2150 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2152 unsigned int buffer;
2154 LOG (GNUNET_ERROR_TYPE_DEBUG,
2155 "GMC send %s ACK on %s\n",
2156 GM_f2s (fwd), GMC_2s (c));
2164 if (GNUNET_NO != c->destroy)
2166 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2170 /* Get available buffer space */
2171 if (GMC_is_terminal (c, fwd))
2173 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2174 buffer = GMT_get_channels_buffer (c->t);
2178 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2179 buffer = GMC_get_buffer (c, fwd);
2181 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2182 if (0 == buffer && GNUNET_NO == force)
2185 /* Send available buffer space */
2186 if (GMC_is_origin (c, fwd))
2188 GNUNET_assert (NULL != c->t);
2189 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2190 GMT_unchoke_channels (c->t);
2194 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2195 send_ack (c, buffer, fwd, force);
2201 * Initialize the connections subsystem
2203 * @param c Configuration handle.
2206 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2208 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2210 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2213 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2214 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2215 GNUNET_SCHEDULER_shutdown ();
2220 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2223 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2224 "MESH", "MAX_CONNECTIONS", "MISSING");
2225 GNUNET_SCHEDULER_shutdown ();
2230 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2231 &refresh_connection_time))
2233 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2234 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2235 GNUNET_SCHEDULER_shutdown ();
2238 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2239 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2244 * Destroy each connection on shutdown.
2246 * @param cls Closure (unused).
2247 * @param key Current key code (CID, unused).
2248 * @param value Value in the hash map (connection)
2250 * @return #GNUNET_YES, because we should continue to iterate,
2253 shutdown_iterator (void *cls,
2254 const struct GNUNET_HashCode *key,
2257 struct MeshConnection *c = value;
2265 * Shut down the connections subsystem.
2270 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2271 GNUNET_CONTAINER_multihashmap_destroy (connections);
2276 struct MeshConnection *
2277 GMC_new (const struct GNUNET_HashCode *cid,
2278 struct MeshTunnel3 *t,
2279 struct MeshPeerPath *p,
2280 unsigned int own_pos)
2282 struct MeshConnection *c;
2284 c = GNUNET_new (struct MeshConnection);
2286 GNUNET_assert (GNUNET_OK ==
2287 GNUNET_CONTAINER_multihashmap_put (connections,
2289 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2290 fc_init (&c->fwd_fc);
2291 fc_init (&c->bck_fc);
2296 GNUNET_assert (own_pos <= p->length - 1);
2297 c->own_pos = own_pos;
2300 if (GNUNET_OK != register_neighbors (c))
2304 GMT_remove_path (c->t, p);
2314 c->fwd_maintenance_task =
2315 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2316 &connection_fwd_keepalive, c);
2324 GMC_destroy (struct MeshConnection *c)
2332 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2333 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2336 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2337 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2338 &c->fwd_fc, &c->bck_fc);
2339 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2340 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2342 /* Cancel all traffic */
2343 if (NULL != c->path)
2345 connection_cancel_queues (c, GNUNET_YES);
2346 connection_cancel_queues (c, GNUNET_NO);
2347 unregister_neighbors (c);
2350 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2351 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2353 /* Cancel maintainance task (keepalive/timeout) */
2354 if (NULL != c->fwd_fc.poll_msg)
2356 GMC_cancel (c->fwd_fc.poll_msg);
2357 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2359 if (NULL != c->bck_fc.poll_msg)
2361 GMC_cancel (c->bck_fc.poll_msg);
2362 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2365 /* Delete from tunnel */
2367 GMT_remove_connection (c->t, c);
2369 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
2370 path_destroy (c->path);
2371 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2372 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2373 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2374 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2375 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2377 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2378 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2380 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2382 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2383 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2386 GNUNET_break (GNUNET_YES ==
2387 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2389 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2394 * Get the connection ID.
2396 * @param c Connection to get the ID from.
2398 * @return ID of the connection.
2400 const struct GNUNET_HashCode *
2401 GMC_get_id (const struct MeshConnection *c)
2408 * Get the connection path.
2410 * @param c Connection to get the path from.
2412 * @return path used by the connection.
2414 const struct MeshPeerPath *
2415 GMC_get_path (const struct MeshConnection *c)
2417 if (GNUNET_NO == c->destroy)
2424 * Get the connection state.
2426 * @param c Connection to get the state from.
2428 * @return state of the connection.
2430 enum MeshConnectionState
2431 GMC_get_state (const struct MeshConnection *c)
2437 * Get the connection tunnel.
2439 * @param c Connection to get the tunnel from.
2441 * @return tunnel of the connection.
2443 struct MeshTunnel3 *
2444 GMC_get_tunnel (const struct MeshConnection *c)
2451 * Get free buffer space in a connection.
2453 * @param c Connection.
2454 * @param fwd Is query about FWD traffic?
2456 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2459 GMC_get_buffer (struct MeshConnection *c, int fwd)
2461 struct MeshFlowControl *fc;
2463 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2465 return (fc->queue_max - fc->queue_n);
2469 * Get how many messages have we allowed to send to us from a direction.
2471 * @param c Connection.
2472 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2474 * @return last_ack_sent - last_pid_recv
2477 GMC_get_allowed (struct MeshConnection *c, int fwd)
2479 struct MeshFlowControl *fc;
2481 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2482 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2486 return (fc->last_ack_sent - fc->last_pid_recv);
2490 * Get messages queued in a connection.
2492 * @param c Connection.
2493 * @param fwd Is query about FWD traffic?
2495 * @return Number of messages queued.
2498 GMC_get_qn (struct MeshConnection *c, int fwd)
2500 struct MeshFlowControl *fc;
2502 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2509 * Allow the connection to advertise a buffer of the given size.
2511 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2512 * allowing up to last_pid_recv + buffer.
2514 * @param c Connection.
2515 * @param buffer How many more messages the connection can accept.
2516 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2519 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2521 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2522 GMC_2s (c), buffer, GM_f2s (fwd));
2523 send_ack (c, buffer, fwd, GNUNET_NO);
2528 * Notify other peers on a connection of a broken link. Mark connections
2529 * to destroy after all traffic has been sent.
2531 * @param c Connection on which there has been a disconnection.
2532 * @param peer Peer that disconnected.
2535 GMC_notify_broken (struct MeshConnection *c,
2536 struct MeshPeer *peer)
2540 LOG (GNUNET_ERROR_TYPE_DEBUG,
2541 " notify broken on %s due to %s disconnect\n",
2542 GMC_2s (c), GMP_2s (peer));
2544 fwd = peer == get_prev_hop (c);
2546 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2548 /* Local shutdown, no one to notify about this. */
2552 if (GNUNET_NO == c->destroy)
2553 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2555 /* Connection will have at least one pending message
2556 * (the one we just scheduled), so no point in checking whether to
2557 * destroy immediately. */
2558 c->destroy = GNUNET_YES;
2559 c->state = MESH_CONNECTION_DESTROYED;
2562 * Cancel all queues, if no message is left, connection will be destroyed.
2564 connection_cancel_queues (c, !fwd);
2571 * Is this peer the first one on the connection?
2573 * @param c Connection.
2574 * @param fwd Is this about fwd traffic?
2576 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2579 GMC_is_origin (struct MeshConnection *c, int fwd)
2581 if (!fwd && c->path->length - 1 == c->own_pos )
2583 if (fwd && 0 == c->own_pos)
2590 * Is this peer the last one on the connection?
2592 * @param c Connection.
2593 * @param fwd Is this about fwd traffic?
2594 * Note that the ROOT is the terminal for BCK traffic!
2596 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2599 GMC_is_terminal (struct MeshConnection *c, int fwd)
2601 return GMC_is_origin (c, !fwd);
2606 * See if we are allowed to send by the next hop in the given direction.
2608 * @param c Connection.
2609 * @param fwd Is this about fwd traffic?
2611 * @return #GNUNET_YES in case it's OK to send.
2614 GMC_is_sendable (struct MeshConnection *c, int fwd)
2616 struct MeshFlowControl *fc;
2618 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2619 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2625 * Sends an already built message on a connection, properly registering
2626 * all used resources.
2628 * @param message Message to send. Function makes a copy of it.
2629 * If message is not hop-by-hop, decrements TTL of copy.
2630 * @param c Connection on which this message is transmitted.
2631 * @param fwd Is this a fwd message?
2632 * @param force Force the connection to accept the message (buffer overfill).
2633 * @param cont Continuation called once message is sent. Can be NULL.
2634 * @param cont_cls Closure for @c cont.
2636 * @return Handle to cancel the message before it's sent.
2637 * NULL on error or if @c cont is NULL.
2638 * Invalid on @c cont call.
2640 struct MeshConnectionQueue *
2641 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2642 struct MeshConnection *c, int fwd, int force,
2643 GMC_sent cont, void *cont_cls)
2645 struct MeshFlowControl *fc;
2646 struct MeshConnectionQueue *q;
2652 size = ntohs (message->size);
2653 data = GNUNET_malloc (size);
2654 memcpy (data, message, size);
2655 type = ntohs (message->type);
2656 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2657 GM_m2s (type), size, GMC_2s (c));
2659 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2660 droppable = GNUNET_NO == force;
2663 struct GNUNET_MESH_Encrypted *emsg;
2664 struct GNUNET_MESH_KX *kmsg;
2665 struct GNUNET_MESH_ACK *amsg;
2666 struct GNUNET_MESH_Poll *pmsg;
2667 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2668 struct GNUNET_MESH_ConnectionBroken *bmsg;
2671 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2672 emsg = (struct GNUNET_MESH_Encrypted *) data;
2673 ttl = ntohl (emsg->ttl);
2676 GNUNET_break_op (0);
2681 emsg->ttl = htonl (ttl - 1);
2682 emsg->pid = htonl (fc->next_pid++);
2683 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2684 if (GNUNET_YES == droppable)
2687 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2688 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2689 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2693 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2695 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2697 GMC_start_poll (c, fwd);
2701 case GNUNET_MESSAGE_TYPE_MESH_KX:
2702 kmsg = (struct GNUNET_MESH_KX *) data;
2706 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2707 amsg = (struct GNUNET_MESH_ACK *) data;
2709 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2710 droppable = GNUNET_NO;
2713 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2714 pmsg = (struct GNUNET_MESH_Poll *) data;
2716 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2717 droppable = GNUNET_NO;
2720 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2721 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2726 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2727 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2732 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2733 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2734 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2743 if (fc->queue_n > fc->queue_max && droppable)
2745 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2748 LOG (GNUNET_ERROR_TYPE_DEBUG,
2749 "queue full: %u/%u\n",
2750 fc->queue_n, fc->queue_max);
2751 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2757 return NULL; /* Drop this message */
2760 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2761 c->pending_messages++;
2763 q = GNUNET_new (struct MeshConnectionQueue);
2764 q->forced = !droppable;
2765 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2769 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GMC_2s (c));
2775 q->cont_cls = cont_cls;
2781 * Cancel a previously sent message while it's in the queue.
2783 * ONLY can be called before the continuation given to the send function
2784 * is called. Once the continuation is called, the message is no longer in the
2787 * @param q Handle to the queue.
2790 GMC_cancel (struct MeshConnectionQueue *q)
2792 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2794 /* queue destroy calls message_sent, which calls q->cont and frees q */
2795 GMP_queue_destroy (q->q, GNUNET_YES);
2800 * Sends a CREATE CONNECTION message for a path to a peer.
2801 * Changes the connection and tunnel states if necessary.
2803 * @param connection Connection to create.
2806 GMC_send_create (struct MeshConnection *connection)
2808 enum MeshTunnel3CState state;
2811 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2812 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2814 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2815 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2816 connection, connection->pending_messages);
2817 connection->pending_messages++;
2819 GMP_queue_add (get_next_hop (connection), NULL,
2820 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2821 size, connection, GNUNET_YES, &message_sent, NULL);
2823 state = GMT_get_cstate (connection->t);
2824 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2825 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2826 if (MESH_CONNECTION_NEW == connection->state)
2827 connection_change_state (connection, MESH_CONNECTION_SENT);
2832 * Send a message to all peers in this connection that the connection
2833 * is no longer valid.
2835 * If some peer should not receive the message, it should be zero'ed out
2836 * before calling this function.
2838 * @param c The connection whose peers to notify.
2841 GMC_send_destroy (struct MeshConnection *c)
2843 struct GNUNET_MESH_ConnectionDestroy msg;
2845 if (GNUNET_YES == c->destroy)
2848 msg.header.size = htons (sizeof (msg));
2849 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2851 LOG (GNUNET_ERROR_TYPE_DEBUG,
2852 " sending connection destroy for connection %s\n",
2855 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2856 GMC_send_prebuilt_message (&msg.header, c,
2857 GNUNET_YES, GNUNET_YES, NULL, NULL);
2858 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2859 GMC_send_prebuilt_message (&msg.header, c,
2860 GNUNET_NO, GNUNET_YES, NULL, NULL);
2861 c->destroy = GNUNET_YES;
2862 c->state = MESH_CONNECTION_DESTROYED;
2867 * @brief Start a polling timer for the connection.
2869 * When a neighbor does not accept more traffic on the connection it could be
2870 * caused by a simple congestion or by a lost ACK. Polling enables to check
2871 * for the lastest ACK status for a connection.
2873 * @param c Connection.
2874 * @param fwd Should we poll in the FWD direction?
2877 GMC_start_poll (struct MeshConnection *c, int fwd)
2879 struct MeshFlowControl *fc;
2881 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2882 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
2884 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
2886 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
2887 fc->poll_task, fc->poll_msg);
2890 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
2891 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2898 * @brief Stop polling a connection for ACKs.
2900 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2902 * @param c Connection.
2903 * @param fwd Should we stop the poll in the FWD direction?
2906 GMC_stop_poll (struct MeshConnection *c, int fwd)
2908 struct MeshFlowControl *fc;
2910 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2911 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2913 GNUNET_SCHEDULER_cancel (fc->poll_task);
2914 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2919 * Get a (static) string for a connection.
2921 * @param c Connection.
2924 GMC_2s (struct MeshConnection *c)
2931 static char buf[128];
2933 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2936 return GNUNET_h2s (&c->id);