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.
184 struct MeshPeerPath *path;
187 * Position of the local peer in the path.
189 unsigned int own_pos;
192 * Task to keep the used paths alive at the owner,
193 * time tunnel out on all the other peers.
195 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
198 * Task to keep the used paths alive at the destination,
199 * time tunnel out on all the other peers.
201 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
204 * Pending message count.
206 int pending_messages;
209 * Destroy flag: if true, destroy on last message.
215 * Handle for messages queued but not yet sent.
217 struct MeshConnectionQueue
220 * Peer queue handle, to cancel if necessary.
222 struct MeshPeerQueue *q;
225 * Continuation to call once sent.
230 * Closure for @c cont.
235 /******************************************************************************/
236 /******************************* GLOBALS ***********************************/
237 /******************************************************************************/
240 * Global handle to the statistics service.
242 extern struct GNUNET_STATISTICS_Handle *stats;
245 * Local peer own ID (memory efficient handle).
247 extern GNUNET_PEER_Id myid;
250 * Local peer own ID (full value).
252 extern struct GNUNET_PeerIdentity my_full_id;
255 * Connections known, indexed by cid (MeshConnection).
257 static struct GNUNET_CONTAINER_MultiHashMap *connections;
260 * How many connections are we willing to maintain.
261 * Local connections are always allowed, even if there are more connections than max.
263 static unsigned long long max_connections;
266 * How many messages *in total* are we willing to queue, divide by number of
267 * connections to get connection queue size.
269 static unsigned long long max_msgs_queue;
272 * How often to send path keepalives. Paths timeout after 4 missed.
274 static struct GNUNET_TIME_Relative refresh_connection_time;
277 * How often to send path create / ACKs.
279 static struct GNUNET_TIME_Relative create_connection_time;
282 /******************************************************************************/
283 /******************************** STATIC ***********************************/
284 /******************************************************************************/
286 #if 0 // avoid compiler warning for unused static function
288 fc_debug (struct MeshFlowControl *fc)
290 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
291 fc->last_pid_recv, fc->last_ack_sent);
292 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
293 fc->last_pid_sent, fc->last_ack_recv);
294 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
295 fc->queue_n, fc->queue_max);
299 connection_debug (struct MeshConnection *c)
303 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
306 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
307 peer2s (c->t->peer), GMC_2s (c));
308 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
309 c->state, c->pending_messages);
310 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
311 fc_debug (&c->fwd_fc);
312 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
313 fc_debug (&c->bck_fc);
318 * Get string description for tunnel state.
320 * @param s Tunnel state.
322 * @return String representation.
325 GMC_state2s (enum MeshConnectionState s)
329 case MESH_CONNECTION_NEW:
330 return "MESH_CONNECTION_NEW";
331 case MESH_CONNECTION_SENT:
332 return "MESH_CONNECTION_SENT";
333 case MESH_CONNECTION_ACK:
334 return "MESH_CONNECTION_ACK";
335 case MESH_CONNECTION_READY:
336 return "MESH_CONNECTION_READY";
337 case MESH_CONNECTION_DESTROYED:
338 return "MESH_CONNECTION_DESTROYED";
340 return "MESH_CONNECTION_STATE_ERROR";
346 * Initialize a Flow Control structure to the initial state.
348 * @param fc Flow Control structure to initialize.
351 fc_init (struct MeshFlowControl *fc)
354 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
355 fc->last_pid_recv = (uint32_t) -1;
356 fc->last_ack_sent = (uint32_t) 0;
357 fc->last_ack_recv = (uint32_t) 0;
358 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
359 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
361 fc->queue_max = (max_msgs_queue / max_connections) + 1;
368 * @param cid Connection ID.
370 static struct MeshConnection *
371 connection_get (const struct GNUNET_HashCode *cid)
373 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
378 connection_change_state (struct MeshConnection* c,
379 enum MeshConnectionState state)
381 LOG (GNUNET_ERROR_TYPE_DEBUG,
382 "Connection %s state was %s\n",
383 GMC_2s (c), GMC_state2s (c->state));
384 if (MESH_CONNECTION_DESTROYED == c->state)
386 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
389 LOG (GNUNET_ERROR_TYPE_DEBUG,
390 "Connection %s state is now %s\n",
391 GMC_2s (c), GMC_state2s (state));
397 * Callback called when a queued ACK message is sent.
399 * @param cls Closure (FC).
400 * @param c Connection this message was on.
401 * @param q Queue handler this call invalidates.
402 * @param type Type of message sent.
403 * @param fwd Was this a FWD going message?
404 * @param size Size of the message.
408 struct MeshConnection *c,
409 struct MeshConnectionQueue *q,
410 uint16_t type, int fwd, size_t size)
412 struct MeshFlowControl *fc = cls;
419 * Send an ACK on the connection, informing the predecessor about
420 * the available buffer space. Should not be called in case the peer
421 * is origin (no predecessor) in the @c fwd direction.
423 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
424 * the ACK itself goes "back" (dest->root).
426 * @param c Connection on which to send the ACK.
427 * @param buffer How much space free to advertise?
428 * @param fwd Is this FWD ACK? (Going dest -> root)
429 * @param force Don't optimize out.
432 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
434 struct MeshFlowControl *next_fc;
435 struct MeshFlowControl *prev_fc;
436 struct GNUNET_MESH_ACK msg;
440 /* If origin, there is no connection to send ACKs. Wrong function! */
441 if (GMC_is_origin (c, fwd))
447 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
448 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
450 LOG (GNUNET_ERROR_TYPE_DEBUG,
451 "connection send %s ack on %s\n",
452 fwd ? "FWD" : "BCK", GMC_2s (c));
454 /* Check if we need to transmit the ACK. */
455 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
456 if (3 < delta && buffer < delta && GNUNET_NO == force)
458 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 " last pid recv: %u, last ack sent: %u\n",
461 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
465 /* Ok, ACK might be necessary, what PID to ACK? */
466 ack = prev_fc->last_pid_recv + buffer;
467 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 " last pid %u, last ack %u, qmax %u, q %u\n",
470 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
471 next_fc->queue_max, next_fc->queue_n);
472 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
474 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
478 /* Check if message is already in queue */
479 if (NULL != prev_fc->ack_msg)
481 if (GMC_is_pid_bigger (ack, prev_fc->last_ack_sent))
483 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
484 GMC_cancel (prev_fc->ack_msg);
485 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
489 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
494 prev_fc->last_ack_sent = ack;
496 /* Build ACK message and send on connection */
497 msg.header.size = htons (sizeof (msg));
498 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
499 msg.ack = htonl (ack);
502 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c, !fwd,
508 * Callback called when a queued message is sent.
510 * Calculates the average time and connection packet tracking.
512 * @param cls Closure (ConnectionQueue Handle).
513 * @param c Connection this message was on.
514 * @param type Type of message sent.
515 * @param fwd Was this a FWD going message?
516 * @param size Size of the message.
517 * @param wait Time spent waiting for core (only the time for THIS message)
520 message_sent (void *cls,
521 struct MeshConnection *c, uint16_t type,
522 int fwd, size_t size,
523 struct GNUNET_TIME_Relative wait)
525 struct MeshConnectionPerformance *p;
526 struct MeshFlowControl *fc;
527 struct MeshConnectionQueue *q = cls;
530 fc = fwd ? &c->fwd_fc : &c->bck_fc;
531 LOG (GNUNET_ERROR_TYPE_DEBUG,
534 GNUNET_MESH_DEBUG_M2S (type));
535 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
540 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
541 q->cont (q->cont_cls, c, q, type, fwd, size);
545 c->pending_messages--;
546 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
548 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
552 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
555 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
557 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
559 LOG (GNUNET_ERROR_TYPE_DEBUG,
560 "! accounting pid %u\n",
562 GMC_send_ack (c, fwd, GNUNET_NO);
565 case GNUNET_MESSAGE_TYPE_MESH_POLL:
569 case GNUNET_MESSAGE_TYPE_MESH_ACK:
576 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
579 return; /* Only endpoints are interested in timing. */
582 usecsperbyte = ((double) wait.rel_value_us) / size;
583 if (p->size == AVG_MSGS)
585 /* Array is full. Substract oldest value, add new one and store. */
586 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
587 p->usecsperbyte[p->idx] = usecsperbyte;
588 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
592 /* Array not yet full. Add current value to avg and store. */
593 p->usecsperbyte[p->idx] = usecsperbyte;
595 p->avg += p->usecsperbyte[p->idx];
599 p->idx = (p->idx + 1) % AVG_MSGS;
604 * Get the previous hop in a connection
606 * @param c Connection.
608 * @return Previous peer in the connection.
610 static struct MeshPeer *
611 get_prev_hop (const struct MeshConnection *c)
615 LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos);
616 if (0 == c->own_pos || c->path->length < 2)
617 id = c->path->peers[0];
619 id = c->path->peers[c->own_pos - 1];
621 return GMP_get_short (id);
626 * Get the next hop in a connection
628 * @param c Connection.
630 * @return Next peer in the connection.
632 static struct MeshPeer *
633 get_next_hop (const struct MeshConnection *c)
637 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
638 id = c->path->peers[c->path->length - 1];
640 id = c->path->peers[c->own_pos + 1];
642 return GMP_get_short (id);
647 * Get the hop in a connection.
649 * @param c Connection.
650 * @param fwd Next hop?
652 * @return Next peer in the connection.
654 static struct MeshPeer *
655 get_hop (struct MeshConnection *c, int fwd)
658 return get_next_hop (c);
659 return get_prev_hop (c);
664 * Is traffic coming from this sender 'FWD' traffic?
666 * @param c Connection to check.
667 * @param sender Peer identity of neighbor.
669 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
670 * the traffic is 'FWD'.
671 * #GNUNET_NO for BCK.
672 * #GNUNET_SYSERR for errors.
675 is_fwd (const struct MeshConnection *c,
676 const struct GNUNET_PeerIdentity *sender)
680 id = GNUNET_PEER_search (sender);
681 if (GMP_get_short_id (get_prev_hop (c)) == id)
684 if (GMP_get_short_id (get_next_hop (c)) == id)
688 return GNUNET_SYSERR;
693 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
694 * or a first CONNECTION_ACK directed to us.
696 * @param connection Connection to confirm.
697 * @param fwd Should we send it FWD? (root->dest)
698 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
701 send_connection_ack (struct MeshConnection *connection, int fwd)
703 struct MeshTunnel3 *t;
706 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
707 !fwd ? "FWD" : "BCK");
708 GMP_queue_add (get_hop (connection, fwd), NULL,
709 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
710 sizeof (struct GNUNET_MESH_ConnectionACK),
711 connection, fwd, &message_sent, NULL);
712 connection->pending_messages++;
713 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
714 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
715 if (MESH_CONNECTION_READY != connection->state)
716 connection_change_state (connection, MESH_CONNECTION_SENT);
721 * Send a notification that a connection is broken.
723 * @param c Connection that is broken.
724 * @param id1 Peer that has disconnected.
725 * @param id2 Peer that has disconnected.
726 * @param fwd Direction towards which to send it.
729 send_broken (struct MeshConnection *c,
730 const struct GNUNET_PeerIdentity *id1,
731 const struct GNUNET_PeerIdentity *id2,
734 struct GNUNET_MESH_ConnectionBroken msg;
736 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
737 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
741 GMC_send_prebuilt_message (&msg.header, c, fwd, NULL, NULL);
747 * Send keepalive packets for a connection.
749 * @param c Connection to keep alive..
750 * @param fwd Is this a FWD keepalive? (owner -> dest).
752 * FIXME register in GMC_send_prebuilt_message()
755 connection_keepalive (struct MeshConnection *c, int fwd)
757 struct GNUNET_MESH_ConnectionKeepAlive *msg;
758 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
761 LOG (GNUNET_ERROR_TYPE_DEBUG,
762 "sending %s keepalive for connection %s]\n",
763 fwd ? "FWD" : "BCK", GMC_2s (c));
765 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
766 msg->header.size = htons (size);
767 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
770 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
775 * Send CONNECTION_{CREATE/ACK} packets for a connection.
777 * @param c Connection for which to send the message.
778 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
781 connection_recreate (struct MeshConnection *c, int fwd)
783 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
787 send_connection_ack (c, GNUNET_NO);
792 * Generic connection timer management.
793 * Depending on the role of the peer in the connection will send the
794 * appropriate message (build or keepalive)
796 * @param c Conncetion to maintain.
800 connection_maintain (struct MeshConnection *c, int fwd)
802 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
804 /* TODO DHT GET with RO_BART */
809 case MESH_CONNECTION_NEW:
812 case MESH_CONNECTION_SENT:
813 connection_recreate (c, fwd);
815 case MESH_CONNECTION_READY:
816 connection_keepalive (c, fwd);
825 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
827 struct MeshConnection *c = cls;
828 struct GNUNET_TIME_Relative delay;
830 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
831 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
834 connection_maintain (c, GNUNET_YES);
835 delay = c->state == MESH_CONNECTION_READY ?
836 refresh_connection_time : create_connection_time;
837 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
838 &connection_fwd_keepalive,
844 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
846 struct MeshConnection *c = cls;
847 struct GNUNET_TIME_Relative delay;
849 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
850 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
853 connection_maintain (c, GNUNET_NO);
854 delay = c->state == MESH_CONNECTION_READY ?
855 refresh_connection_time : create_connection_time;
856 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
857 &connection_bck_keepalive,
863 * @brief Re-initiate traffic on this connection if necessary.
865 * Check if there is traffic queued towards this peer
866 * and the core transmit handle is NULL (traffic was stalled).
867 * If so, call core tmt rdy.
869 * @param c Connection on which initiate traffic.
870 * @param fwd Is this about fwd traffic?
873 connection_unlock_queue (struct MeshConnection *c, int fwd)
875 struct MeshPeer *peer;
877 LOG (GNUNET_ERROR_TYPE_DEBUG,
878 "connection_unlock_queue %s on %s\n",
879 fwd ? "FWD" : "BCK", GMC_2s (c));
881 if (GMC_is_terminal (c, fwd))
883 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
887 peer = get_hop (c, fwd);
888 GMP_queue_unlock (peer, c);
893 * Cancel all transmissions that belong to a certain connection.
895 * If the connection is scheduled for destruction and no more messages are left,
896 * the connection will be destroyed by the continuation call.
898 * @param c Connection which to cancel. Might be destroyed during this call.
899 * @param fwd Cancel fwd traffic?
902 connection_cancel_queues (struct MeshConnection *c, int fwd)
904 struct MeshFlowControl *fc;
905 struct MeshPeer *peer;
907 LOG (GNUNET_ERROR_TYPE_DEBUG,
908 " *** Cancel %s queues for connection %s\n",
909 fwd ? "FWD" : "BCK", GMC_2s (c));
916 fc = fwd ? &c->fwd_fc : &c->bck_fc;
917 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
919 GNUNET_SCHEDULER_cancel (fc->poll_task);
920 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
921 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
923 peer = get_hop (c, fwd);
924 GMP_queue_cancel (peer, c);
929 * Function called if a connection has been stalled for a while,
930 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
932 * @param cls Closure (poll ctx).
933 * @param tc TaskContext.
936 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
940 * Callback called when a queued POLL message is sent.
942 * @param cls Closure (FC).
943 * @param c Connection this message was on.
944 * @param q Queue handler this call invalidates.
945 * @param type Type of message sent.
946 * @param fwd Was this a FWD going message?
947 * @param size Size of the message.
950 poll_sent (void *cls,
951 struct MeshConnection *c,
952 struct MeshConnectionQueue *q,
953 uint16_t type, int fwd, size_t size)
955 struct MeshFlowControl *fc = cls;
959 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
962 LOG (GNUNET_ERROR_TYPE_DEBUG,
963 " *** POLL sent for , scheduling new one!\n");
965 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
966 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
967 &connection_poll, fc);
968 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
973 * Function called if a connection has been stalled for a while,
974 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
976 * @param cls Closure (poll ctx).
977 * @param tc TaskContext.
980 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
982 struct MeshFlowControl *fc = cls;
983 struct GNUNET_MESH_Poll msg;
984 struct MeshConnection *c;
986 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
987 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
993 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
994 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
995 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
996 fc == &c->fwd_fc ? "FWD" : "BCK");
998 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
999 msg.header.size = htons (sizeof (msg));
1000 msg.pid = htonl (fc->last_pid_sent);
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1002 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c, fc == &c->fwd_fc,
1008 * Timeout function due to lack of keepalive/traffic from the owner.
1009 * Destroys connection if called.
1011 * @param cls Closure (connection to destroy).
1012 * @param tc TaskContext.
1015 connection_fwd_timeout (void *cls,
1016 const struct GNUNET_SCHEDULER_TaskContext *tc)
1018 struct MeshConnection *c = cls;
1020 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1021 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1023 LOG (GNUNET_ERROR_TYPE_DEBUG,
1024 "Connection %s[%X] FWD timed out. Destroying.\n",
1028 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1036 * Timeout function due to lack of keepalive/traffic from the destination.
1037 * Destroys connection if called.
1039 * @param cls Closure (connection to destroy).
1040 * @param tc TaskContext
1043 connection_bck_timeout (void *cls,
1044 const struct GNUNET_SCHEDULER_TaskContext *tc)
1046 struct MeshConnection *c = cls;
1048 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1049 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1052 LOG (GNUNET_ERROR_TYPE_DEBUG,
1053 "Connection %s[%X] FWD timed out. Destroying.\n",
1054 GMT_2s (c->t), c->id);
1056 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1064 * Resets the connection timeout task, some other message has done the
1066 * - For the first peer on the direction this means to send
1067 * a keepalive or a path confirmation message (either create or ACK).
1068 * - For all other peers, this means to destroy the connection,
1069 * due to lack of activity.
1070 * Starts the tiemout if no timeout was running (connection just created).
1072 * @param c Connection whose timeout to reset.
1073 * @param fwd Is this forward?
1075 * TODO use heap to improve efficiency of scheduler.
1078 connection_reset_timeout (struct MeshConnection *c, int fwd)
1080 GNUNET_SCHEDULER_TaskIdentifier *ti;
1081 GNUNET_SCHEDULER_Task f;
1083 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1085 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1086 GNUNET_SCHEDULER_cancel (*ti);
1088 if (GMC_is_origin (c, fwd)) /* Startpoint */
1090 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1091 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1093 else /* Relay, endpoint. */
1095 struct GNUNET_TIME_Relative delay;
1097 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1098 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1099 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1105 * Add the connection to the list of both neighbors.
1107 * @param c Connection.
1109 * @return #GNUNET_OK if everything went fine
1110 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1113 register_neighbors (struct MeshConnection *c)
1115 struct MeshPeer *next_peer;
1116 struct MeshPeer *prev_peer;
1118 next_peer = get_next_hop (c);
1119 prev_peer = get_prev_hop (c);
1121 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1122 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1123 return GNUNET_SYSERR;
1125 GMP_add_connection (next_peer, c);
1126 GMP_add_connection (prev_peer, c);
1133 * Remove the connection from the list of both neighbors.
1135 * @param c Connection.
1138 unregister_neighbors (struct MeshConnection *c)
1140 struct MeshPeer *peer;
1142 peer = get_next_hop (c);
1143 GMP_remove_connection (peer, c);
1145 peer = get_prev_hop (c);
1146 GMP_remove_connection (peer, c);
1152 * Bind the connection to the peer and the tunnel to that peer.
1154 * If the peer has no tunnel, create one. Update tunnel and connection
1155 * data structres to reflect new status.
1157 * @param c Connection.
1161 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1163 GMP_add_tunnel (peer);
1164 c->t = GMP_get_tunnel (peer);
1165 GMT_add_connection (c->t, c);
1168 /******************************************************************************/
1169 /******************************** API ***********************************/
1170 /******************************************************************************/
1173 * Core handler for connection creation.
1175 * @param cls Closure (unused).
1176 * @param peer Sender (neighbor).
1177 * @param message Message.
1179 * @return GNUNET_OK to keep the connection open,
1180 * GNUNET_SYSERR to close it (signal serious error)
1183 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1184 const struct GNUNET_MessageHeader *message)
1186 struct GNUNET_MESH_ConnectionCreate *msg;
1187 struct GNUNET_PeerIdentity *id;
1188 struct GNUNET_HashCode *cid;
1189 struct MeshPeerPath *path;
1190 struct MeshPeer *dest_peer;
1191 struct MeshPeer *orig_peer;
1192 struct MeshConnection *c;
1193 unsigned int own_pos;
1197 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1198 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1201 size = ntohs (message->size);
1202 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1204 GNUNET_break_op (0);
1208 /* Calculate hops */
1209 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1210 if (size % sizeof (struct GNUNET_PeerIdentity))
1212 GNUNET_break_op (0);
1215 size /= sizeof (struct GNUNET_PeerIdentity);
1218 GNUNET_break_op (0);
1221 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1223 /* Get parameters */
1224 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1226 id = (struct GNUNET_PeerIdentity *) &msg[1];
1227 LOG (GNUNET_ERROR_TYPE_DEBUG,
1228 " connection %s (%s).\n",
1229 GNUNET_h2s (cid), GNUNET_i2s (id));
1231 /* Create connection */
1232 c = connection_get (cid);
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1237 path = path_new (size);
1239 for (i = 0; i < size; i++)
1241 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1242 GNUNET_i2s (&id[i]));
1243 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1244 if (path->peers[i] == myid)
1247 if (own_pos == 0 && path->peers[own_pos] != myid)
1249 /* create path: self not found in path through self */
1250 GNUNET_break_op (0);
1251 path_destroy (path);
1254 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1255 GMP_add_path_to_all (path, GNUNET_NO);
1256 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1257 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1260 path_destroy (path);
1263 connection_reset_timeout (c, GNUNET_YES);
1267 path = path_duplicate (c->path);
1269 if (MESH_CONNECTION_NEW == c->state)
1270 connection_change_state (c, MESH_CONNECTION_SENT);
1272 /* Remember peers */
1273 dest_peer = GMP_get (&id[size - 1]);
1274 orig_peer = GMP_get (&id[0]);
1276 /* Is it a connection to us? */
1277 if (c->own_pos == size - 1)
1279 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1280 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1282 add_to_peer (c, orig_peer);
1283 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1284 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1286 send_connection_ack (c, GNUNET_NO);
1287 if (MESH_CONNECTION_SENT == c->state)
1288 connection_change_state (c, MESH_CONNECTION_ACK);
1290 /* Keep tunnel alive in direction dest->owner*/
1291 c->bck_maintenance_task =
1292 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1293 &connection_bck_keepalive, c);
1297 /* It's for somebody else! Retransmit. */
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1299 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1300 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1301 GMC_send_prebuilt_message (message, c, GNUNET_YES, NULL, NULL);
1303 path_destroy (path);
1309 * Core handler for path confirmations.
1311 * @param cls closure
1312 * @param message message
1313 * @param peer peer identity this notification is about
1315 * @return GNUNET_OK to keep the connection open,
1316 * GNUNET_SYSERR to close it (signal serious error)
1319 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1320 const struct GNUNET_MessageHeader *message)
1322 struct GNUNET_MESH_ConnectionACK *msg;
1323 struct MeshConnection *c;
1324 struct MeshPeerPath *p;
1325 struct MeshPeer *pi;
1326 enum MeshConnectionState oldstate;
1329 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1330 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1331 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1333 GNUNET_h2s (&msg->cid));
1334 c = connection_get (&msg->cid);
1337 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1339 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1343 oldstate = c->state;
1344 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1345 pi = GMP_get (peer);
1346 if (get_next_hop (c) == pi)
1348 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1350 if (MESH_CONNECTION_SENT == oldstate)
1351 connection_change_state (c, MESH_CONNECTION_ACK);
1353 else if (get_prev_hop (c) == pi)
1355 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1357 connection_change_state (c, MESH_CONNECTION_READY);
1361 GNUNET_break_op (0);
1365 connection_reset_timeout (c, fwd);
1367 /* Add path to peers? */
1371 GMP_add_path_to_all (p, GNUNET_YES);
1378 /* Message for us as creator? */
1379 if (GMC_is_origin (c, GNUNET_YES))
1381 if (GNUNET_NO != fwd)
1383 GNUNET_break_op (0);
1386 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1388 /* If just created, cancel the short timeout and start a long one */
1389 if (MESH_CONNECTION_SENT == oldstate)
1390 connection_reset_timeout (c, GNUNET_YES);
1392 /* Change connection and tunnel state */
1393 connection_change_state (c, MESH_CONNECTION_READY);
1394 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1395 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1397 /* Send ACK (~TCP ACK)*/
1398 send_connection_ack (c, GNUNET_YES);
1402 /* Message for us as destination? */
1403 if (GMC_is_terminal (c, GNUNET_YES))
1405 if (GNUNET_YES != fwd)
1407 GNUNET_break_op (0);
1410 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1412 /* If just created, cancel the short timeout and start a long one */
1413 if (MESH_CONNECTION_ACK == oldstate)
1414 connection_reset_timeout (c, GNUNET_NO);
1416 /* Change tunnel state */
1417 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1418 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1423 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1424 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1430 * Core handler for notifications of broken paths
1432 * @param cls Closure (unused).
1433 * @param id Peer identity of sending neighbor.
1434 * @param message Message.
1436 * @return GNUNET_OK to keep the connection open,
1437 * GNUNET_SYSERR to close it (signal serious error)
1440 GMC_handle_broken (void* cls,
1441 const struct GNUNET_PeerIdentity* id,
1442 const struct GNUNET_MessageHeader* message)
1444 struct GNUNET_MESH_ConnectionBroken *msg;
1445 struct MeshConnection *c;
1448 LOG (GNUNET_ERROR_TYPE_DEBUG,
1449 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1450 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1452 GNUNET_i2s (&msg->peer1));
1453 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1454 GNUNET_i2s (&msg->peer2));
1455 c = connection_get (&msg->cid);
1458 GNUNET_break_op (0);
1462 fwd = is_fwd (c, id);
1463 connection_cancel_queues (c, !fwd);
1464 if (GMC_is_terminal (c, fwd))
1466 if (0 < c->pending_messages)
1467 c->destroy = GNUNET_YES;
1473 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1474 c->destroy = GNUNET_YES;
1483 * Core handler for tunnel destruction
1485 * @param cls Closure (unused).
1486 * @param peer Peer identity of sending neighbor.
1487 * @param message Message.
1489 * @return GNUNET_OK to keep the connection open,
1490 * GNUNET_SYSERR to close it (signal serious error)
1493 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1494 const struct GNUNET_MessageHeader *message)
1496 struct GNUNET_MESH_ConnectionDestroy *msg;
1497 struct MeshConnection *c;
1500 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1501 LOG (GNUNET_ERROR_TYPE_DEBUG,
1502 "Got a CONNECTION DESTROY message from %s\n",
1504 LOG (GNUNET_ERROR_TYPE_DEBUG,
1505 " for connection %s\n",
1506 GNUNET_h2s (&msg->cid));
1507 c = connection_get (&msg->cid);
1510 /* Probably already got the message from another path,
1511 * destroyed the tunnel and retransmitted to children.
1514 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1519 fwd = is_fwd (c, peer);
1520 if (GNUNET_SYSERR == fwd)
1522 GNUNET_break_op (0);
1525 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1526 c->destroy = GNUNET_YES;
1527 c->state = MESH_CONNECTION_DESTROYED;
1533 * Generic handler for mesh network encrypted traffic.
1535 * @param peer Peer identity this notification is about.
1536 * @param msg Encrypted message.
1538 * @return GNUNET_OK to keep the connection open,
1539 * GNUNET_SYSERR to close it (signal serious error)
1542 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1543 const struct GNUNET_MESH_Encrypted *msg)
1545 struct MeshConnection *c;
1546 struct MeshPeer *neighbor;
1547 struct MeshFlowControl *fc;
1548 GNUNET_PEER_Id peer_id;
1556 size = ntohs (msg->header.size);
1558 sizeof (struct GNUNET_MESH_Encrypted) +
1559 sizeof (struct GNUNET_MessageHeader))
1561 GNUNET_break_op (0);
1564 type = ntohs (msg->header.type);
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1567 GNUNET_MESH_DEBUG_M2S (type), ntohl (msg->pid), GNUNET_i2s (peer));
1569 /* Check connection */
1570 c = connection_get (&msg->cid);
1573 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1574 LOG (GNUNET_ERROR_TYPE_DEBUG,
1575 "WARNING connection %s unknown\n",
1576 GNUNET_h2s (&msg->cid));
1580 /* Check if origin is as expected */
1581 neighbor = get_prev_hop (c);
1582 peer_id = GNUNET_PEER_search (peer);
1583 if (peer_id == GMP_get_short_id (neighbor))
1589 neighbor = get_next_hop (c);
1590 if (peer_id == GMP_get_short_id (neighbor))
1596 /* Unexpected peer sending traffic on a connection. */
1597 GNUNET_break_op (0);
1603 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1604 pid = ntohl (msg->pid);
1605 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1607 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1608 LOG (GNUNET_ERROR_TYPE_DEBUG,
1609 "WARNING Received PID %u, (prev %u), ACK %u\n",
1610 pid, fc->last_pid_recv, fc->last_ack_sent);
1613 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1615 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1616 LOG (GNUNET_ERROR_TYPE_DEBUG,
1617 " Pid %u not expected (%u+), dropping!\n",
1618 pid, fc->last_pid_recv + 1);
1621 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1622 connection_change_state (c, MESH_CONNECTION_READY);
1623 connection_reset_timeout (c, fwd);
1624 fc->last_pid_recv = pid;
1626 /* Is this message for us? */
1627 if (GMC_is_terminal (c, fwd))
1629 /* TODO signature verification */
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1631 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1638 fc->last_pid_recv = pid;
1639 GMT_handle_encrypted (c->t, msg);
1640 GMC_send_ack (c, fwd, GNUNET_NO);
1644 /* Message not for us: forward to next hop */
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1646 ttl = ntohl (msg->ttl);
1647 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1650 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1651 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1652 GMC_send_ack (c, fwd, GNUNET_NO);
1656 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1657 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
1663 * Generic handler for mesh network encrypted traffic.
1665 * @param peer Peer identity this notification is about.
1666 * @param msg Encrypted message.
1668 * @return GNUNET_OK to keep the connection open,
1669 * GNUNET_SYSERR to close it (signal serious error)
1672 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1673 const struct GNUNET_MESH_KX *msg)
1675 struct MeshConnection *c;
1676 struct MeshPeer *neighbor;
1677 GNUNET_PEER_Id peer_id;
1683 size = ntohs (msg->header.size);
1685 sizeof (struct GNUNET_MESH_Encrypted) +
1686 sizeof (struct GNUNET_MessageHeader))
1688 GNUNET_break_op (0);
1691 type = ntohs (msg->header.type);
1692 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1693 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1694 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1696 /* Check connection */
1697 c = connection_get (&msg->cid);
1700 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1705 /* Check if origin is as expected */
1706 neighbor = get_prev_hop (c);
1707 peer_id = GNUNET_PEER_search (peer);
1708 if (peer_id == GMP_get_short_id (neighbor))
1714 neighbor = get_next_hop (c);
1715 if (peer_id == GMP_get_short_id (neighbor))
1721 /* Unexpected peer sending traffic on a connection. */
1722 GNUNET_break_op (0);
1727 /* Count as connection confirmation. */
1728 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1729 connection_change_state (c, MESH_CONNECTION_READY);
1730 connection_reset_timeout (c, fwd);
1733 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1734 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1737 /* Is this message for us? */
1738 if (GMC_is_terminal (c, fwd))
1740 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1741 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1747 GMT_handle_kx (c->t, &msg[1].header);
1751 /* Message not for us: forward to next hop */
1752 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1753 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1754 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
1761 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1763 * @param cls Closure (unused).
1764 * @param message Message received.
1765 * @param peer Peer who sent the message.
1767 * @return GNUNET_OK to keep the connection open,
1768 * GNUNET_SYSERR to close it (signal serious error)
1771 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1772 const struct GNUNET_MessageHeader *message)
1774 return handle_mesh_encrypted (peer,
1775 (struct GNUNET_MESH_Encrypted *)message);
1780 * Core handler for key exchange traffic (ephemeral key, ping, pong).
1782 * @param cls Closure (unused).
1783 * @param message Message received.
1784 * @param peer Peer who sent the message.
1786 * @return GNUNET_OK to keep the connection open,
1787 * GNUNET_SYSERR to close it (signal serious error)
1790 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1791 const struct GNUNET_MessageHeader *message)
1793 return handle_mesh_kx (peer,
1794 (struct GNUNET_MESH_KX *) message);
1799 * Core handler for mesh network traffic point-to-point acks.
1801 * @param cls closure
1802 * @param message message
1803 * @param peer peer identity this notification is about
1805 * @return GNUNET_OK to keep the connection open,
1806 * GNUNET_SYSERR to close it (signal serious error)
1809 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1810 const struct GNUNET_MessageHeader *message)
1812 struct GNUNET_MESH_ACK *msg;
1813 struct MeshConnection *c;
1814 struct MeshFlowControl *fc;
1819 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1820 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1822 msg = (struct GNUNET_MESH_ACK *) message;
1824 c = connection_get (&msg->cid);
1828 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1833 /* Is this a forward or backward ACK? */
1834 id = GNUNET_PEER_search (peer);
1835 if (GMP_get_short_id (get_next_hop (c)) == id)
1837 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1841 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1843 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1849 GNUNET_break_op (0);
1853 ack = ntohl (msg->ack);
1854 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1855 ack, fc->last_ack_recv);
1856 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1857 fc->last_ack_recv = ack;
1859 /* Cancel polling if the ACK is big enough. */
1860 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1861 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1863 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1864 GNUNET_SCHEDULER_cancel (fc->poll_task);
1865 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1866 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1869 connection_unlock_queue (c, fwd);
1876 * Core handler for mesh network traffic point-to-point ack polls.
1878 * @param cls closure
1879 * @param message message
1880 * @param peer peer identity this notification is about
1882 * @return GNUNET_OK to keep the connection open,
1883 * GNUNET_SYSERR to close it (signal serious error)
1886 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1887 const struct GNUNET_MessageHeader *message)
1889 struct GNUNET_MESH_Poll *msg;
1890 struct MeshConnection *c;
1891 struct MeshFlowControl *fc;
1896 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1897 LOG (GNUNET_ERROR_TYPE_DEBUG,
1898 "Got a POLL packet from %s!\n",
1901 msg = (struct GNUNET_MESH_Poll *) message;
1903 c = connection_get (&msg->cid);
1907 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1909 GNUNET_break_op (0);
1913 /* Is this a forward or backward ACK?
1914 * Note: a poll should never be needed in a loopback case,
1915 * since there is no possiblility of packet loss there, so
1916 * this way of discerining FWD/BCK should not be a problem.
1918 id = GNUNET_PEER_search (peer);
1919 if (GMP_get_short_id (get_next_hop (c)) == id)
1921 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
1924 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1926 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
1931 GNUNET_break_op (0);
1935 pid = ntohl (msg->pid);
1936 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
1937 fc->last_pid_recv = pid;
1938 fwd = fc == &c->bck_fc;
1939 GMC_send_ack (c, fwd, GNUNET_YES);
1946 * Core handler for mesh keepalives.
1948 * @param cls closure
1949 * @param message message
1950 * @param peer peer identity this notification is about
1951 * @return GNUNET_OK to keep the connection open,
1952 * GNUNET_SYSERR to close it (signal serious error)
1954 * TODO: Check who we got this from, to validate route.
1957 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1958 const struct GNUNET_MessageHeader *message)
1960 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1961 struct MeshConnection *c;
1962 struct MeshPeer *neighbor;
1963 GNUNET_PEER_Id peer_id;
1966 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1967 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1970 c = connection_get (&msg->cid);
1973 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1978 /* Check if origin is as expected TODO refactor and reuse */
1979 peer_id = GNUNET_PEER_search (peer);
1980 neighbor = get_prev_hop (c);
1981 if (peer_id == GMP_get_short_id (neighbor))
1987 neighbor = get_next_hop (c);
1988 if (peer_id == GMP_get_short_id (neighbor))
1994 GNUNET_break_op (0);
1999 connection_change_state (c, MESH_CONNECTION_READY);
2000 connection_reset_timeout (c, fwd);
2002 if (GMC_is_terminal (c, fwd))
2005 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2006 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
2013 * Send an ACK on the appropriate connection/channel, depending on
2014 * the direction and the position of the peer.
2016 * @param c Which connection to send the hop-by-hop ACK.
2017 * @param fwd Is this a fwd ACK? (will go dest->root).
2018 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2021 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2023 unsigned int buffer;
2025 LOG (GNUNET_ERROR_TYPE_DEBUG,
2026 "GMC send %s ACK on %s\n",
2027 fwd ? "FWD" : "BCK", GMC_2s (c));
2035 if (GNUNET_NO != c->destroy)
2037 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2041 /* Get available buffer space */
2042 if (GMC_is_terminal (c, fwd))
2044 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2045 buffer = GMT_get_channels_buffer (c->t);
2049 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2050 buffer = GMC_get_buffer (c, fwd);
2052 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2053 if (0 == buffer && GNUNET_NO == force)
2056 /* Send available buffer space */
2057 if (GMC_is_origin (c, fwd))
2059 GNUNET_assert (NULL != c->t);
2060 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2061 GMT_unchoke_channels (c->t);
2065 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2066 send_ack (c, buffer, fwd, force);
2072 * Initialize the connections subsystem
2074 * @param c Configuration handle.
2077 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2079 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2081 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2084 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2085 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2086 GNUNET_SCHEDULER_shutdown ();
2091 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2094 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2095 "MESH", "MAX_CONNECTIONS", "MISSING");
2096 GNUNET_SCHEDULER_shutdown ();
2101 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2102 &refresh_connection_time))
2104 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2105 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2106 GNUNET_SCHEDULER_shutdown ();
2109 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2110 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2115 * Destroy each connection on shutdown.
2117 * @param cls Closure (unused).
2118 * @param key Current key code (CID, unused).
2119 * @param value Value in the hash map (connection)
2121 * @return #GNUNET_YES, because we should continue to iterate,
2124 shutdown_iterator (void *cls,
2125 const struct GNUNET_HashCode *key,
2128 struct MeshConnection *c = value;
2136 * Shut down the connections subsystem.
2141 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2142 GNUNET_CONTAINER_multihashmap_destroy (connections);
2147 struct MeshConnection *
2148 GMC_new (const struct GNUNET_HashCode *cid,
2149 struct MeshTunnel3 *t,
2150 struct MeshPeerPath *p,
2151 unsigned int own_pos)
2153 struct MeshConnection *c;
2155 c = GNUNET_new (struct MeshConnection);
2157 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
2158 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2159 fc_init (&c->fwd_fc);
2160 fc_init (&c->bck_fc);
2165 if (own_pos > p->length - 1)
2171 c->own_pos = own_pos;
2176 c->fwd_maintenance_task =
2177 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2178 &connection_fwd_keepalive, c);
2180 if (GNUNET_OK != register_neighbors (c))
2191 GMC_destroy (struct MeshConnection *c)
2196 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2197 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2200 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2201 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2202 &c->fwd_fc, &c->bck_fc);
2203 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2204 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2206 /* Cancel all traffic */
2207 connection_cancel_queues (c, GNUNET_YES);
2208 connection_cancel_queues (c, GNUNET_NO);
2210 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2211 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2213 /* Cancel maintainance task (keepalive/timeout) */
2214 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2215 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2216 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2217 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2218 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2220 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2221 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2223 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2225 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2226 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2228 if (NULL != c->fwd_fc.poll_msg)
2230 GMC_cancel (c->fwd_fc.poll_msg);
2231 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2233 if (NULL != c->bck_fc.poll_msg)
2235 GMC_cancel (c->bck_fc.poll_msg);
2236 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2239 /* Unregister from neighbors */
2240 unregister_neighbors (c);
2243 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2245 GMT_remove_connection (c->t, c);
2247 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES))
2248 path_destroy (c->path);
2250 GNUNET_break (GNUNET_YES ==
2251 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2257 * Get the connection ID.
2259 * @param c Connection to get the ID from.
2261 * @return ID of the connection.
2263 const struct GNUNET_HashCode *
2264 GMC_get_id (const struct MeshConnection *c)
2271 * Get the connection path.
2273 * @param c Connection to get the path from.
2275 * @return path used by the connection.
2277 const struct MeshPeerPath *
2278 GMC_get_path (const struct MeshConnection *c)
2280 if (GNUNET_NO == c->destroy)
2287 * Get the connection state.
2289 * @param c Connection to get the state from.
2291 * @return state of the connection.
2293 enum MeshConnectionState
2294 GMC_get_state (const struct MeshConnection *c)
2300 * Get the connection tunnel.
2302 * @param c Connection to get the tunnel from.
2304 * @return tunnel of the connection.
2306 struct MeshTunnel3 *
2307 GMC_get_tunnel (const struct MeshConnection *c)
2314 * Get free buffer space in a connection.
2316 * @param c Connection.
2317 * @param fwd Is query about FWD traffic?
2319 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2322 GMC_get_buffer (struct MeshConnection *c, int fwd)
2324 struct MeshFlowControl *fc;
2326 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2328 return (fc->queue_max - fc->queue_n);
2332 * Get how many messages have we allowed to send to us from a direction.
2334 * @param c Connection.
2335 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2337 * @return last_ack_sent - last_pid_recv
2340 GMC_get_allowed (struct MeshConnection *c, int fwd)
2342 struct MeshFlowControl *fc;
2344 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2345 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2349 return (fc->last_ack_sent - fc->last_pid_recv);
2353 * Get messages queued in a connection.
2355 * @param c Connection.
2356 * @param fwd Is query about FWD traffic?
2358 * @return Number of messages queued.
2361 GMC_get_qn (struct MeshConnection *c, int fwd)
2363 struct MeshFlowControl *fc;
2365 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2372 * Allow the connection to advertise a buffer of the given size.
2374 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2375 * allowing up to last_pid_recv + buffer.
2377 * @param c Connection.
2378 * @param buffer How many more messages the connection can accept.
2379 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2382 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2384 send_ack (c, buffer, fwd, GNUNET_NO);
2389 * Notify other peers on a connection of a broken link. Mark connections
2390 * to destroy after all traffic has been sent.
2392 * @param c Connection on which there has been a disconnection.
2393 * @param peer Peer that disconnected.
2396 GMC_notify_broken (struct MeshConnection *c,
2397 struct MeshPeer *peer)
2401 LOG (GNUNET_ERROR_TYPE_DEBUG,
2402 " notify broken on %s due to %s disconnect\n",
2403 GMC_2s (c), GMP_2s (peer));
2405 fwd = peer == get_prev_hop (c);
2407 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2409 /* Local shutdown, no one to notify about this. */
2413 if (GNUNET_NO == c->destroy)
2414 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2416 /* Connection will have at least one pending message
2417 * (the one we just scheduled), so no point in checking whether to
2418 * destroy immediately. */
2419 c->destroy = GNUNET_YES;
2420 c->state = MESH_CONNECTION_DESTROYED;
2423 * Cancel all queues, if no message is left, connection will be destroyed.
2425 connection_cancel_queues (c, !fwd);
2432 * Is this peer the first one on the connection?
2434 * @param c Connection.
2435 * @param fwd Is this about fwd traffic?
2437 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2440 GMC_is_origin (struct MeshConnection *c, int fwd)
2442 if (!fwd && c->path->length - 1 == c->own_pos )
2444 if (fwd && 0 == c->own_pos)
2451 * Is this peer the last one on the connection?
2453 * @param c Connection.
2454 * @param fwd Is this about fwd traffic?
2455 * Note that the ROOT is the terminal for BCK traffic!
2457 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2460 GMC_is_terminal (struct MeshConnection *c, int fwd)
2462 return GMC_is_origin (c, !fwd);
2467 * See if we are allowed to send by the next hop in the given direction.
2469 * @param c Connection.
2470 * @param fwd Is this about fwd traffic?
2472 * @return #GNUNET_YES in case it's OK to send.
2475 GMC_is_sendable (struct MeshConnection *c, int fwd)
2477 struct MeshFlowControl *fc;
2479 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2480 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2486 * Sends an already built message on a connection, properly registering
2487 * all used resources.
2489 * @param message Message to send. Function makes a copy of it.
2490 * If message is not hop-by-hop, decrements TTL of copy.
2491 * @param c Connection on which this message is transmitted.
2492 * @param fwd Is this a fwd message?
2493 * @param cont Continuation called once message is sent. Can be NULL.
2494 * @param cont_cls Closure for @c cont.
2496 * @return Handle to cancel the message before it's sent.
2497 * NULL on error or if @c cont is NULL.
2498 * Invalid on @c cont call.
2500 struct MeshConnectionQueue *
2501 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2502 struct MeshConnection *c, int fwd,
2503 GMC_sent cont, void *cont_cls)
2505 struct MeshFlowControl *fc;
2506 struct MeshConnectionQueue *q;
2512 size = ntohs (message->size);
2513 data = GNUNET_malloc (size);
2514 memcpy (data, message, size);
2515 type = ntohs (message->type);
2516 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2517 GNUNET_MESH_DEBUG_M2S (type), size, GMC_2s (c));
2519 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2520 droppable = GNUNET_YES;
2523 struct GNUNET_MESH_Encrypted *emsg;
2524 struct GNUNET_MESH_KX *kmsg;
2525 struct GNUNET_MESH_ACK *amsg;
2526 struct GNUNET_MESH_Poll *pmsg;
2527 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2528 struct GNUNET_MESH_ConnectionBroken *bmsg;
2531 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2532 emsg = (struct GNUNET_MESH_Encrypted *) data;
2533 ttl = ntohl (emsg->ttl);
2536 GNUNET_break_op (0);
2541 emsg->ttl = htonl (ttl - 1);
2542 emsg->pid = htonl (fc->next_pid++);
2543 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2545 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2546 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2547 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2548 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2550 GMC_start_poll (c, fwd);
2554 case GNUNET_MESSAGE_TYPE_MESH_KX:
2555 kmsg = (struct GNUNET_MESH_KX *) data;
2559 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2560 amsg = (struct GNUNET_MESH_ACK *) data;
2562 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2563 droppable = GNUNET_NO;
2566 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2567 pmsg = (struct GNUNET_MESH_Poll *) data;
2569 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2570 droppable = GNUNET_NO;
2573 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2574 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2579 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2580 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2585 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2586 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2595 if (fc->queue_n > fc->queue_max && droppable)
2597 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2600 LOG (GNUNET_ERROR_TYPE_DEBUG,
2601 "queue full: %u/%u\n",
2602 fc->queue_n, fc->queue_max);
2603 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2609 return NULL; /* Drop this message */
2612 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2613 c->pending_messages++;
2617 (void) GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2618 &message_sent, NULL);
2622 q = GNUNET_new (struct MeshConnectionQueue);
2623 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2633 q->cont_cls = cont_cls;
2639 * Cancel a previously sent message while it's in the queue.
2641 * ONLY can be called before the continuation given to the send function
2642 * is called. Once the continuation is called, the message is no longer in the
2645 * @param q Handle to the queue.
2648 GMC_cancel (struct MeshConnectionQueue *q)
2650 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2652 /* queue destroy calls message_sent, which calls q->cont and frees q */
2653 GMP_queue_destroy (q->q, GNUNET_YES);
2658 * Sends a CREATE CONNECTION message for a path to a peer.
2659 * Changes the connection and tunnel states if necessary.
2661 * @param connection Connection to create.
2664 GMC_send_create (struct MeshConnection *connection)
2666 enum MeshTunnel3CState state;
2669 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2670 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2671 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2672 GMP_queue_add (get_next_hop (connection), NULL,
2673 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2674 size, connection, GNUNET_YES, &message_sent, NULL);
2675 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2676 connection, connection->pending_messages);
2677 connection->pending_messages++;
2678 state = GMT_get_cstate (connection->t);
2679 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2680 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2681 if (MESH_CONNECTION_NEW == connection->state)
2682 connection_change_state (connection, MESH_CONNECTION_SENT);
2687 * Send a message to all peers in this connection that the connection
2688 * is no longer valid.
2690 * If some peer should not receive the message, it should be zero'ed out
2691 * before calling this function.
2693 * @param c The connection whose peers to notify.
2696 GMC_send_destroy (struct MeshConnection *c)
2698 struct GNUNET_MESH_ConnectionDestroy msg;
2700 if (GNUNET_YES == c->destroy)
2703 msg.header.size = htons (sizeof (msg));
2704 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2706 LOG (GNUNET_ERROR_TYPE_DEBUG,
2707 " sending connection destroy for connection %s\n",
2710 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2711 GMC_send_prebuilt_message (&msg.header, c, GNUNET_YES, NULL, NULL);
2712 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2713 GMC_send_prebuilt_message (&msg.header, c, GNUNET_NO, NULL, NULL);
2714 c->destroy = GNUNET_YES;
2715 c->state = MESH_CONNECTION_DESTROYED;
2720 * @brief Start a polling timer for the connection.
2722 * When a neighbor does not accept more traffic on the connection it could be
2723 * caused by a simple congestion or by a lost ACK. Polling enables to check
2724 * for the lastest ACK status for a connection.
2726 * @param c Connection.
2727 * @param fwd Should we poll in the FWD direction?
2730 GMC_start_poll (struct MeshConnection *c, int fwd)
2732 struct MeshFlowControl *fc;
2734 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2735 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
2736 fwd ? "FWD" : "BCK");
2737 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
2739 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
2740 fc->poll_task, fc->poll_msg);
2743 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
2744 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2751 * @brief Stop polling a connection for ACKs.
2753 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2755 * @param c Connection.
2756 * @param fwd Should we stop the poll in the FWD direction?
2759 GMC_stop_poll (struct MeshConnection *c, int fwd)
2761 struct MeshFlowControl *fc;
2763 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2764 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2766 GNUNET_SCHEDULER_cancel (fc->poll_task);
2767 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2772 * Get a (static) string for a connection.
2774 * @param c Connection.
2777 GMC_2s (struct MeshConnection *c)
2784 static char buf[128];
2786 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2789 return GNUNET_h2s (&c->id);