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 GM_f2s (fwd), 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 (GM_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,
509 * Callback called when a queued message is sent.
511 * Calculates the average time and connection packet tracking.
513 * @param cls Closure (ConnectionQueue Handle).
514 * @param c Connection this message was on.
515 * @param type Type of message sent.
516 * @param fwd Was this a FWD going message?
517 * @param size Size of the message.
518 * @param wait Time spent waiting for core (only the time for THIS message)
521 message_sent (void *cls,
522 struct MeshConnection *c, uint16_t type,
523 int fwd, size_t size,
524 struct GNUNET_TIME_Relative wait)
526 struct MeshConnectionPerformance *p;
527 struct MeshFlowControl *fc;
528 struct MeshConnectionQueue *q = cls;
531 fc = fwd ? &c->fwd_fc : &c->bck_fc;
532 LOG (GNUNET_ERROR_TYPE_DEBUG,
536 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
541 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
542 q->cont (q->cont_cls, c, q, type, fwd, size);
546 c->pending_messages--;
547 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
549 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
553 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
556 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
558 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
560 LOG (GNUNET_ERROR_TYPE_DEBUG,
561 "! accounting pid %u\n",
563 GMC_send_ack (c, fwd, GNUNET_NO);
566 case GNUNET_MESSAGE_TYPE_MESH_POLL:
570 case GNUNET_MESSAGE_TYPE_MESH_ACK:
577 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
580 return; /* Only endpoints are interested in timing. */
583 usecsperbyte = ((double) wait.rel_value_us) / size;
584 if (p->size == AVG_MSGS)
586 /* Array is full. Substract oldest value, add new one and store. */
587 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
588 p->usecsperbyte[p->idx] = usecsperbyte;
589 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
593 /* Array not yet full. Add current value to avg and store. */
594 p->usecsperbyte[p->idx] = usecsperbyte;
596 p->avg += p->usecsperbyte[p->idx];
600 p->idx = (p->idx + 1) % AVG_MSGS;
605 * Get the previous hop in a connection
607 * @param c Connection.
609 * @return Previous peer in the connection.
611 static struct MeshPeer *
612 get_prev_hop (const struct MeshConnection *c)
616 LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos);
617 if (0 == c->own_pos || c->path->length < 2)
618 id = c->path->peers[0];
620 id = c->path->peers[c->own_pos - 1];
622 return GMP_get_short (id);
627 * Get the next hop in a connection
629 * @param c Connection.
631 * @return Next peer in the connection.
633 static struct MeshPeer *
634 get_next_hop (const struct MeshConnection *c)
638 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
639 id = c->path->peers[c->path->length - 1];
641 id = c->path->peers[c->own_pos + 1];
643 return GMP_get_short (id);
648 * Get the hop in a connection.
650 * @param c Connection.
651 * @param fwd Next hop?
653 * @return Next peer in the connection.
655 static struct MeshPeer *
656 get_hop (struct MeshConnection *c, int fwd)
659 return get_next_hop (c);
660 return get_prev_hop (c);
665 * Is traffic coming from this sender 'FWD' traffic?
667 * @param c Connection to check.
668 * @param sender Peer identity of neighbor.
670 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
671 * the traffic is 'FWD'.
672 * #GNUNET_NO for BCK.
673 * #GNUNET_SYSERR for errors.
676 is_fwd (const struct MeshConnection *c,
677 const struct GNUNET_PeerIdentity *sender)
681 id = GNUNET_PEER_search (sender);
682 if (GMP_get_short_id (get_prev_hop (c)) == id)
685 if (GMP_get_short_id (get_next_hop (c)) == id)
689 return GNUNET_SYSERR;
694 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
695 * or a first CONNECTION_ACK directed to us.
697 * @param connection Connection to confirm.
698 * @param fwd Should we send it FWD? (root->dest)
699 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
702 send_connection_ack (struct MeshConnection *connection, int fwd)
704 struct MeshTunnel3 *t;
707 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
709 GMP_queue_add (get_hop (connection, fwd), NULL,
710 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
711 sizeof (struct GNUNET_MESH_ConnectionACK),
712 connection, fwd, &message_sent, NULL);
713 connection->pending_messages++;
714 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
715 GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
716 if (MESH_CONNECTION_READY != connection->state)
717 connection_change_state (connection, MESH_CONNECTION_SENT);
722 * Send a notification that a connection is broken.
724 * @param c Connection that is broken.
725 * @param id1 Peer that has disconnected.
726 * @param id2 Peer that has disconnected.
727 * @param fwd Direction towards which to send it.
730 send_broken (struct MeshConnection *c,
731 const struct GNUNET_PeerIdentity *id1,
732 const struct GNUNET_PeerIdentity *id2,
735 struct GNUNET_MESH_ConnectionBroken msg;
737 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
738 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
742 GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, 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).
753 connection_keepalive (struct MeshConnection *c, int fwd)
755 struct GNUNET_MESH_ConnectionKeepAlive *msg;
756 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
759 LOG (GNUNET_ERROR_TYPE_DEBUG,
760 "sending %s keepalive for connection %s]\n",
761 GM_f2s (fwd), GMC_2s (c));
763 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
764 msg->header.size = htons (size);
765 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
767 msg->reserved = htonl (0);
769 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
774 * Send CONNECTION_{CREATE/ACK} packets for a connection.
776 * @param c Connection for which to send the message.
777 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
780 connection_recreate (struct MeshConnection *c, int fwd)
782 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
786 send_connection_ack (c, GNUNET_NO);
791 * Generic connection timer management.
792 * Depending on the role of the peer in the connection will send the
793 * appropriate message (build or keepalive)
795 * @param c Conncetion to maintain.
799 connection_maintain (struct MeshConnection *c, int fwd)
801 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
803 /* TODO DHT GET with RO_BART */
808 case MESH_CONNECTION_NEW:
811 case MESH_CONNECTION_SENT:
812 connection_recreate (c, fwd);
814 case MESH_CONNECTION_READY:
815 connection_keepalive (c, fwd);
824 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
826 struct MeshConnection *c = cls;
827 struct GNUNET_TIME_Relative delay;
829 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
830 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
833 connection_maintain (c, GNUNET_YES);
834 delay = c->state == MESH_CONNECTION_READY ?
835 refresh_connection_time : create_connection_time;
836 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
837 &connection_fwd_keepalive,
843 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
845 struct MeshConnection *c = cls;
846 struct GNUNET_TIME_Relative delay;
848 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
849 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
852 connection_maintain (c, GNUNET_NO);
853 delay = c->state == MESH_CONNECTION_READY ?
854 refresh_connection_time : create_connection_time;
855 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
856 &connection_bck_keepalive,
862 * @brief Re-initiate traffic on this connection if necessary.
864 * Check if there is traffic queued towards this peer
865 * and the core transmit handle is NULL (traffic was stalled).
866 * If so, call core tmt rdy.
868 * @param c Connection on which initiate traffic.
869 * @param fwd Is this about fwd traffic?
872 connection_unlock_queue (struct MeshConnection *c, int fwd)
874 struct MeshPeer *peer;
876 LOG (GNUNET_ERROR_TYPE_DEBUG,
877 "connection_unlock_queue %s on %s\n",
878 GM_f2s (fwd), GMC_2s (c));
880 if (GMC_is_terminal (c, fwd))
882 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
886 peer = get_hop (c, fwd);
887 GMP_queue_unlock (peer, c);
892 * Cancel all transmissions that belong to a certain connection.
894 * If the connection is scheduled for destruction and no more messages are left,
895 * the connection will be destroyed by the continuation call.
897 * @param c Connection which to cancel. Might be destroyed during this call.
898 * @param fwd Cancel fwd traffic?
901 connection_cancel_queues (struct MeshConnection *c, int fwd)
903 struct MeshFlowControl *fc;
904 struct MeshPeer *peer;
906 LOG (GNUNET_ERROR_TYPE_DEBUG,
907 " *** Cancel %s queues for connection %s\n",
908 GM_f2s (fwd), GMC_2s (c));
915 fc = fwd ? &c->fwd_fc : &c->bck_fc;
916 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
918 GNUNET_SCHEDULER_cancel (fc->poll_task);
919 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
920 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
922 peer = get_hop (c, fwd);
923 GMP_queue_cancel (peer, c);
928 * Function called if a connection has been stalled for a while,
929 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
931 * @param cls Closure (poll ctx).
932 * @param tc TaskContext.
935 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
939 * Callback called when a queued POLL message is sent.
941 * @param cls Closure (FC).
942 * @param c Connection this message was on.
943 * @param q Queue handler this call invalidates.
944 * @param type Type of message sent.
945 * @param fwd Was this a FWD going message?
946 * @param size Size of the message.
949 poll_sent (void *cls,
950 struct MeshConnection *c,
951 struct MeshConnectionQueue *q,
952 uint16_t type, int fwd, size_t size)
954 struct MeshFlowControl *fc = cls;
958 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
961 LOG (GNUNET_ERROR_TYPE_DEBUG,
962 " *** POLL sent for , scheduling new one!\n");
964 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
965 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
966 &connection_poll, fc);
967 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
972 * Function called if a connection has been stalled for a while,
973 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
975 * @param cls Closure (poll ctx).
976 * @param tc TaskContext.
979 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
981 struct MeshFlowControl *fc = cls;
982 struct GNUNET_MESH_Poll msg;
983 struct MeshConnection *c;
985 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
986 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
992 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
993 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
994 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
995 fc == &c->fwd_fc ? "FWD" : "BCK");
997 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
998 msg.header.size = htons (sizeof (msg));
999 msg.pid = htonl (fc->last_pid_sent);
1000 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1001 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
1002 fc == &c->fwd_fc, GNUNET_YES,
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 timeout 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 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
1087 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1088 GNUNET_SCHEDULER_cancel (*ti);
1090 if (GMC_is_origin (c, fwd)) /* Startpoint */
1092 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1093 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1095 else /* Relay, endpoint. */
1097 struct GNUNET_TIME_Relative delay;
1099 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1100 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1101 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1107 * Add the connection to the list of both neighbors.
1109 * @param c Connection.
1111 * @return #GNUNET_OK if everything went fine
1112 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1115 register_neighbors (struct MeshConnection *c)
1117 struct MeshPeer *next_peer;
1118 struct MeshPeer *prev_peer;
1120 next_peer = get_next_hop (c);
1121 prev_peer = get_prev_hop (c);
1123 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1124 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1125 return GNUNET_SYSERR;
1127 GMP_add_connection (next_peer, c);
1128 GMP_add_connection (prev_peer, c);
1135 * Remove the connection from the list of both neighbors.
1137 * @param c Connection.
1140 unregister_neighbors (struct MeshConnection *c)
1142 struct MeshPeer *peer;
1144 peer = get_next_hop (c);
1145 GMP_remove_connection (peer, c);
1147 peer = get_prev_hop (c);
1148 GMP_remove_connection (peer, c);
1154 * Bind the connection to the peer and the tunnel to that peer.
1156 * If the peer has no tunnel, create one. Update tunnel and connection
1157 * data structres to reflect new status.
1159 * @param c Connection.
1163 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1165 GMP_add_tunnel (peer);
1166 c->t = GMP_get_tunnel (peer);
1167 GMT_add_connection (c->t, c);
1170 /******************************************************************************/
1171 /******************************** API ***********************************/
1172 /******************************************************************************/
1175 * Core handler for connection creation.
1177 * @param cls Closure (unused).
1178 * @param peer Sender (neighbor).
1179 * @param message Message.
1181 * @return GNUNET_OK to keep the connection open,
1182 * GNUNET_SYSERR to close it (signal serious error)
1185 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1186 const struct GNUNET_MessageHeader *message)
1188 struct GNUNET_MESH_ConnectionCreate *msg;
1189 struct GNUNET_PeerIdentity *id;
1190 struct GNUNET_HashCode *cid;
1191 struct MeshPeerPath *path;
1192 struct MeshPeer *dest_peer;
1193 struct MeshPeer *orig_peer;
1194 struct MeshConnection *c;
1195 unsigned int own_pos;
1199 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1200 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1203 size = ntohs (message->size);
1204 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1206 GNUNET_break_op (0);
1210 /* Calculate hops */
1211 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1212 if (size % sizeof (struct GNUNET_PeerIdentity))
1214 GNUNET_break_op (0);
1217 size /= sizeof (struct GNUNET_PeerIdentity);
1220 GNUNET_break_op (0);
1223 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1225 /* Get parameters */
1226 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1228 id = (struct GNUNET_PeerIdentity *) &msg[1];
1229 LOG (GNUNET_ERROR_TYPE_DEBUG,
1230 " connection %s (%s).\n",
1231 GNUNET_h2s (cid), GNUNET_i2s (id));
1233 /* Create connection */
1234 c = connection_get (cid);
1238 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1239 path = path_new (size);
1241 for (i = 0; i < size; i++)
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1244 GNUNET_i2s (&id[i]));
1245 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1246 if (path->peers[i] == myid)
1249 if (own_pos == 0 && path->peers[own_pos] != myid)
1251 /* create path: self not found in path through self */
1252 GNUNET_break_op (0);
1253 path_destroy (path);
1256 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1257 GMP_add_path_to_all (path, GNUNET_NO);
1258 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1259 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1262 path_destroy (path);
1265 connection_reset_timeout (c, GNUNET_YES);
1269 path = path_duplicate (c->path);
1271 if (MESH_CONNECTION_NEW == c->state)
1272 connection_change_state (c, MESH_CONNECTION_SENT);
1274 /* Remember peers */
1275 dest_peer = GMP_get (&id[size - 1]);
1276 orig_peer = GMP_get (&id[0]);
1278 /* Is it a connection to us? */
1279 if (c->own_pos == size - 1)
1281 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1282 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1284 add_to_peer (c, orig_peer);
1285 if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
1286 GMT_change_cstate (c->t, MESH_TUNNEL3_WAITING);
1288 send_connection_ack (c, GNUNET_NO);
1289 if (MESH_CONNECTION_SENT == c->state)
1290 connection_change_state (c, MESH_CONNECTION_ACK);
1292 /* Keep tunnel alive in direction dest->owner*/
1293 c->bck_maintenance_task =
1294 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1295 &connection_bck_keepalive, c);
1299 /* It's for somebody else! Retransmit. */
1300 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1301 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1302 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1303 GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES, NULL, NULL);
1305 path_destroy (path);
1311 * Core handler for path confirmations.
1313 * @param cls closure
1314 * @param message message
1315 * @param peer peer identity this notification is about
1317 * @return GNUNET_OK to keep the connection open,
1318 * GNUNET_SYSERR to close it (signal serious error)
1321 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1322 const struct GNUNET_MessageHeader *message)
1324 struct GNUNET_MESH_ConnectionACK *msg;
1325 struct MeshConnection *c;
1326 struct MeshPeerPath *p;
1327 struct MeshPeer *pi;
1328 enum MeshConnectionState oldstate;
1331 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1333 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1334 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1335 GNUNET_h2s (&msg->cid));
1336 c = connection_get (&msg->cid);
1339 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1341 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1345 oldstate = c->state;
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1347 pi = GMP_get (peer);
1348 if (get_next_hop (c) == pi)
1350 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1352 if (MESH_CONNECTION_SENT == oldstate)
1353 connection_change_state (c, MESH_CONNECTION_ACK);
1355 else if (get_prev_hop (c) == pi)
1357 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1359 connection_change_state (c, MESH_CONNECTION_READY);
1363 GNUNET_break_op (0);
1367 connection_reset_timeout (c, fwd);
1369 /* Add path to peers? */
1373 GMP_add_path_to_all (p, GNUNET_YES);
1380 /* Message for us as creator? */
1381 if (GMC_is_origin (c, GNUNET_YES))
1383 if (GNUNET_NO != fwd)
1385 GNUNET_break_op (0);
1388 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1390 /* If just created, cancel the short timeout and start a long one */
1391 if (MESH_CONNECTION_SENT == oldstate)
1392 connection_reset_timeout (c, GNUNET_YES);
1394 /* Change connection state */
1395 connection_change_state (c, MESH_CONNECTION_READY);
1396 send_connection_ack (c, GNUNET_YES);
1398 /* Change tunnel state, trigger KX */
1399 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1400 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1405 /* Message for us as destination? */
1406 if (GMC_is_terminal (c, GNUNET_YES))
1408 if (GNUNET_YES != fwd)
1410 GNUNET_break_op (0);
1413 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1415 /* If just created, cancel the short timeout and start a long one */
1416 if (MESH_CONNECTION_ACK == oldstate)
1417 connection_reset_timeout (c, GNUNET_NO);
1419 /* Change tunnel state */
1420 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1421 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1426 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1427 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1433 * Core handler for notifications of broken paths
1435 * @param cls Closure (unused).
1436 * @param id Peer identity of sending neighbor.
1437 * @param message Message.
1439 * @return GNUNET_OK to keep the connection open,
1440 * GNUNET_SYSERR to close it (signal serious error)
1443 GMC_handle_broken (void* cls,
1444 const struct GNUNET_PeerIdentity* id,
1445 const struct GNUNET_MessageHeader* message)
1447 struct GNUNET_MESH_ConnectionBroken *msg;
1448 struct MeshConnection *c;
1451 LOG (GNUNET_ERROR_TYPE_DEBUG,
1452 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1453 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1454 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1455 GNUNET_i2s (&msg->peer1));
1456 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1457 GNUNET_i2s (&msg->peer2));
1458 c = connection_get (&msg->cid);
1461 GNUNET_break_op (0);
1465 fwd = is_fwd (c, id);
1466 connection_cancel_queues (c, !fwd);
1467 if (GMC_is_terminal (c, fwd))
1469 if (0 < c->pending_messages)
1470 c->destroy = GNUNET_YES;
1476 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1477 c->destroy = GNUNET_YES;
1486 * Core handler for tunnel destruction
1488 * @param cls Closure (unused).
1489 * @param peer Peer identity of sending neighbor.
1490 * @param message Message.
1492 * @return GNUNET_OK to keep the connection open,
1493 * GNUNET_SYSERR to close it (signal serious error)
1496 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1497 const struct GNUNET_MessageHeader *message)
1499 struct GNUNET_MESH_ConnectionDestroy *msg;
1500 struct MeshConnection *c;
1503 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1504 LOG (GNUNET_ERROR_TYPE_DEBUG,
1505 "Got a CONNECTION DESTROY message from %s\n",
1507 LOG (GNUNET_ERROR_TYPE_DEBUG,
1508 " for connection %s\n",
1509 GNUNET_h2s (&msg->cid));
1510 c = connection_get (&msg->cid);
1513 /* Probably already got the message from another path,
1514 * destroyed the tunnel and retransmitted to children.
1517 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1519 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1522 fwd = is_fwd (c, peer);
1523 if (GNUNET_SYSERR == fwd)
1525 GNUNET_break_op (0);
1528 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
1529 c->destroy = GNUNET_YES;
1530 c->state = MESH_CONNECTION_DESTROYED;
1536 * Generic handler for mesh network encrypted traffic.
1538 * @param peer Peer identity this notification is about.
1539 * @param msg Encrypted message.
1541 * @return GNUNET_OK to keep the connection open,
1542 * GNUNET_SYSERR to close it (signal serious error)
1545 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1546 const struct GNUNET_MESH_Encrypted *msg)
1548 struct MeshConnection *c;
1549 struct MeshPeer *neighbor;
1550 struct MeshFlowControl *fc;
1551 GNUNET_PEER_Id peer_id;
1559 size = ntohs (msg->header.size);
1561 sizeof (struct GNUNET_MESH_Encrypted) +
1562 sizeof (struct GNUNET_MessageHeader))
1564 GNUNET_break_op (0);
1567 type = ntohs (msg->header.type);
1568 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1569 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1570 GM_m2s (type), ntohl (msg->pid), GNUNET_i2s (peer));
1572 /* Check connection */
1573 c = connection_get (&msg->cid);
1576 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1577 LOG (GNUNET_ERROR_TYPE_DEBUG,
1578 "WARNING connection %s unknown\n",
1579 GNUNET_h2s (&msg->cid));
1583 /* Check if origin is as expected */
1584 neighbor = get_prev_hop (c);
1585 peer_id = GNUNET_PEER_search (peer);
1586 if (peer_id == GMP_get_short_id (neighbor))
1592 neighbor = get_next_hop (c);
1593 if (peer_id == GMP_get_short_id (neighbor))
1599 /* Unexpected peer sending traffic on a connection. */
1600 GNUNET_break_op (0);
1606 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1607 pid = ntohl (msg->pid);
1608 if (GM_is_pid_bigger (pid, fc->last_ack_sent))
1610 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1611 LOG (GNUNET_ERROR_TYPE_DEBUG,
1612 "WARNING Received PID %u, (prev %u), ACK %u\n",
1613 pid, fc->last_pid_recv, fc->last_ack_sent);
1616 if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
1618 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1619 LOG (GNUNET_ERROR_TYPE_DEBUG,
1620 " Pid %u not expected (%u+), dropping!\n",
1621 pid, fc->last_pid_recv + 1);
1624 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1625 connection_change_state (c, MESH_CONNECTION_READY);
1626 connection_reset_timeout (c, fwd);
1627 fc->last_pid_recv = pid;
1629 /* Is this message for us? */
1630 if (GMC_is_terminal (c, fwd))
1632 /* TODO signature verification */
1633 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1634 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1641 fc->last_pid_recv = pid;
1642 GMT_handle_encrypted (c->t, msg);
1643 GMC_send_ack (c, fwd, GNUNET_NO);
1647 /* Message not for us: forward to next hop */
1648 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1649 ttl = ntohl (msg->ttl);
1650 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1653 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1654 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1655 GMC_send_ack (c, fwd, GNUNET_NO);
1659 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1660 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1666 * Generic handler for mesh network encrypted traffic.
1668 * @param peer Peer identity this notification is about.
1669 * @param msg Encrypted message.
1671 * @return GNUNET_OK to keep the connection open,
1672 * GNUNET_SYSERR to close it (signal serious error)
1675 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1676 const struct GNUNET_MESH_KX *msg)
1678 struct MeshConnection *c;
1679 struct MeshPeer *neighbor;
1680 GNUNET_PEER_Id peer_id;
1686 size = ntohs (msg->header.size);
1688 sizeof (struct GNUNET_MESH_Encrypted) +
1689 sizeof (struct GNUNET_MessageHeader))
1691 GNUNET_break_op (0);
1694 type = ntohs (msg->header.type);
1695 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1696 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1697 GM_m2s (type), GNUNET_i2s (peer));
1699 /* Check connection */
1700 c = connection_get (&msg->cid);
1703 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1704 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1708 /* Check if origin is as expected */
1709 neighbor = get_prev_hop (c);
1710 peer_id = GNUNET_PEER_search (peer);
1711 if (peer_id == GMP_get_short_id (neighbor))
1717 neighbor = get_next_hop (c);
1718 if (peer_id == GMP_get_short_id (neighbor))
1724 /* Unexpected peer sending traffic on a connection. */
1725 GNUNET_break_op (0);
1730 /* Count as connection confirmation. */
1731 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1732 connection_change_state (c, MESH_CONNECTION_READY);
1733 connection_reset_timeout (c, fwd);
1736 if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
1737 GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
1740 /* Is this message for us? */
1741 if (GMC_is_terminal (c, fwd))
1743 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1744 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1750 GMT_handle_kx (c->t, &msg[1].header);
1754 /* Message not for us: forward to next hop */
1755 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1756 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1757 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
1764 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1766 * @param cls Closure (unused).
1767 * @param message Message received.
1768 * @param peer Peer who sent the message.
1770 * @return GNUNET_OK to keep the connection open,
1771 * GNUNET_SYSERR to close it (signal serious error)
1774 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1775 const struct GNUNET_MessageHeader *message)
1777 return handle_mesh_encrypted (peer,
1778 (struct GNUNET_MESH_Encrypted *)message);
1783 * Core handler for key exchange traffic (ephemeral key, ping, pong).
1785 * @param cls Closure (unused).
1786 * @param message Message received.
1787 * @param peer Peer who sent the message.
1789 * @return GNUNET_OK to keep the connection open,
1790 * GNUNET_SYSERR to close it (signal serious error)
1793 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1794 const struct GNUNET_MessageHeader *message)
1796 return handle_mesh_kx (peer,
1797 (struct GNUNET_MESH_KX *) message);
1802 * Core handler for mesh network traffic point-to-point acks.
1804 * @param cls closure
1805 * @param message message
1806 * @param peer peer identity this notification is about
1808 * @return GNUNET_OK to keep the connection open,
1809 * GNUNET_SYSERR to close it (signal serious error)
1812 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1813 const struct GNUNET_MessageHeader *message)
1815 struct GNUNET_MESH_ACK *msg;
1816 struct MeshConnection *c;
1817 struct MeshFlowControl *fc;
1822 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1823 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1825 msg = (struct GNUNET_MESH_ACK *) message;
1827 c = connection_get (&msg->cid);
1831 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1836 /* Is this a forward or backward ACK? */
1837 id = GNUNET_PEER_search (peer);
1838 if (GMP_get_short_id (get_next_hop (c)) == id)
1840 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1844 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1846 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1852 GNUNET_break_op (0);
1856 ack = ntohl (msg->ack);
1857 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1858 ack, fc->last_ack_recv);
1859 if (GM_is_pid_bigger (ack, fc->last_ack_recv))
1860 fc->last_ack_recv = ack;
1862 /* Cancel polling if the ACK is big enough. */
1863 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1864 GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1866 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1867 GNUNET_SCHEDULER_cancel (fc->poll_task);
1868 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1869 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1872 connection_unlock_queue (c, fwd);
1879 * Core handler for mesh network traffic point-to-point ack polls.
1881 * @param cls closure
1882 * @param message message
1883 * @param peer peer identity this notification is about
1885 * @return GNUNET_OK to keep the connection open,
1886 * GNUNET_SYSERR to close it (signal serious error)
1889 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1890 const struct GNUNET_MessageHeader *message)
1892 struct GNUNET_MESH_Poll *msg;
1893 struct MeshConnection *c;
1894 struct MeshFlowControl *fc;
1899 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1900 LOG (GNUNET_ERROR_TYPE_DEBUG,
1901 "Got a POLL packet from %s!\n",
1904 msg = (struct GNUNET_MESH_Poll *) message;
1906 c = connection_get (&msg->cid);
1910 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1912 GNUNET_break_op (0);
1916 /* Is this a forward or backward ACK?
1917 * Note: a poll should never be needed in a loopback case,
1918 * since there is no possiblility of packet loss there, so
1919 * this way of discerining FWD/BCK should not be a problem.
1921 id = GNUNET_PEER_search (peer);
1922 if (GMP_get_short_id (get_next_hop (c)) == id)
1924 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
1927 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1929 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
1934 GNUNET_break_op (0);
1938 pid = ntohl (msg->pid);
1939 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
1940 fc->last_pid_recv = pid;
1941 fwd = fc == &c->bck_fc;
1942 GMC_send_ack (c, fwd, GNUNET_YES);
1949 * Core handler for mesh keepalives.
1951 * @param cls closure
1952 * @param message message
1953 * @param peer peer identity this notification is about
1954 * @return GNUNET_OK to keep the connection open,
1955 * GNUNET_SYSERR to close it (signal serious error)
1957 * TODO: Check who we got this from, to validate route.
1960 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1961 const struct GNUNET_MessageHeader *message)
1963 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1964 struct MeshConnection *c;
1965 struct MeshPeer *neighbor;
1966 GNUNET_PEER_Id peer_id;
1969 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1970 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1973 c = connection_get (&msg->cid);
1976 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1981 /* Check if origin is as expected TODO refactor and reuse */
1982 peer_id = GNUNET_PEER_search (peer);
1983 neighbor = get_prev_hop (c);
1984 if (peer_id == GMP_get_short_id (neighbor))
1990 neighbor = get_next_hop (c);
1991 if (peer_id == GMP_get_short_id (neighbor))
1997 GNUNET_break_op (0);
2002 connection_change_state (c, MESH_CONNECTION_READY);
2003 connection_reset_timeout (c, fwd);
2005 if (GMC_is_terminal (c, fwd))
2008 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2009 GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
2016 * Send an ACK on the appropriate connection/channel, depending on
2017 * the direction and the position of the peer.
2019 * @param c Which connection to send the hop-by-hop ACK.
2020 * @param fwd Is this a fwd ACK? (will go dest->root).
2021 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2024 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
2026 unsigned int buffer;
2028 LOG (GNUNET_ERROR_TYPE_DEBUG,
2029 "GMC send %s ACK on %s\n",
2030 GM_f2s (fwd), GMC_2s (c));
2038 if (GNUNET_NO != c->destroy)
2040 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2044 /* Get available buffer space */
2045 if (GMC_is_terminal (c, fwd))
2047 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2048 buffer = GMT_get_channels_buffer (c->t);
2052 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2053 buffer = GMC_get_buffer (c, fwd);
2055 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2056 if (0 == buffer && GNUNET_NO == force)
2059 /* Send available buffer space */
2060 if (GMC_is_origin (c, fwd))
2062 GNUNET_assert (NULL != c->t);
2063 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2064 GMT_unchoke_channels (c->t);
2068 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2069 send_ack (c, buffer, fwd, force);
2075 * Initialize the connections subsystem
2077 * @param c Configuration handle.
2080 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2082 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2084 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2087 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2088 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2089 GNUNET_SCHEDULER_shutdown ();
2094 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2097 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2098 "MESH", "MAX_CONNECTIONS", "MISSING");
2099 GNUNET_SCHEDULER_shutdown ();
2104 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2105 &refresh_connection_time))
2107 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2108 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2109 GNUNET_SCHEDULER_shutdown ();
2112 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2113 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2118 * Destroy each connection on shutdown.
2120 * @param cls Closure (unused).
2121 * @param key Current key code (CID, unused).
2122 * @param value Value in the hash map (connection)
2124 * @return #GNUNET_YES, because we should continue to iterate,
2127 shutdown_iterator (void *cls,
2128 const struct GNUNET_HashCode *key,
2131 struct MeshConnection *c = value;
2139 * Shut down the connections subsystem.
2144 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2145 GNUNET_CONTAINER_multihashmap_destroy (connections);
2150 struct MeshConnection *
2151 GMC_new (const struct GNUNET_HashCode *cid,
2152 struct MeshTunnel3 *t,
2153 struct MeshPeerPath *p,
2154 unsigned int own_pos)
2156 struct MeshConnection *c;
2158 c = GNUNET_new (struct MeshConnection);
2160 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
2161 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2162 fc_init (&c->fwd_fc);
2163 fc_init (&c->bck_fc);
2168 if (own_pos > p->length - 1)
2174 c->own_pos = own_pos;
2179 c->fwd_maintenance_task =
2180 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2181 &connection_fwd_keepalive, c);
2183 if (GNUNET_OK != register_neighbors (c))
2194 GMC_destroy (struct MeshConnection *c)
2199 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2200 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2203 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2204 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2205 &c->fwd_fc, &c->bck_fc);
2206 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2207 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2209 /* Cancel all traffic */
2210 connection_cancel_queues (c, GNUNET_YES);
2211 connection_cancel_queues (c, GNUNET_NO);
2213 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2214 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2216 /* Cancel maintainance task (keepalive/timeout) */
2217 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2218 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2219 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2220 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2221 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2223 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2224 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2226 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2228 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2229 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2231 if (NULL != c->fwd_fc.poll_msg)
2233 GMC_cancel (c->fwd_fc.poll_msg);
2234 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2236 if (NULL != c->bck_fc.poll_msg)
2238 GMC_cancel (c->bck_fc.poll_msg);
2239 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2242 /* Unregister from neighbors */
2243 unregister_neighbors (c);
2246 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2248 GMT_remove_connection (c->t, c);
2250 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES))
2251 path_destroy (c->path);
2253 GNUNET_break (GNUNET_YES ==
2254 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2260 * Get the connection ID.
2262 * @param c Connection to get the ID from.
2264 * @return ID of the connection.
2266 const struct GNUNET_HashCode *
2267 GMC_get_id (const struct MeshConnection *c)
2274 * Get the connection path.
2276 * @param c Connection to get the path from.
2278 * @return path used by the connection.
2280 const struct MeshPeerPath *
2281 GMC_get_path (const struct MeshConnection *c)
2283 if (GNUNET_NO == c->destroy)
2290 * Get the connection state.
2292 * @param c Connection to get the state from.
2294 * @return state of the connection.
2296 enum MeshConnectionState
2297 GMC_get_state (const struct MeshConnection *c)
2303 * Get the connection tunnel.
2305 * @param c Connection to get the tunnel from.
2307 * @return tunnel of the connection.
2309 struct MeshTunnel3 *
2310 GMC_get_tunnel (const struct MeshConnection *c)
2317 * Get free buffer space in a connection.
2319 * @param c Connection.
2320 * @param fwd Is query about FWD traffic?
2322 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2325 GMC_get_buffer (struct MeshConnection *c, int fwd)
2327 struct MeshFlowControl *fc;
2329 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2331 return (fc->queue_max - fc->queue_n);
2335 * Get how many messages have we allowed to send to us from a direction.
2337 * @param c Connection.
2338 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2340 * @return last_ack_sent - last_pid_recv
2343 GMC_get_allowed (struct MeshConnection *c, int fwd)
2345 struct MeshFlowControl *fc;
2347 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2348 if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2352 return (fc->last_ack_sent - fc->last_pid_recv);
2356 * Get messages queued in a connection.
2358 * @param c Connection.
2359 * @param fwd Is query about FWD traffic?
2361 * @return Number of messages queued.
2364 GMC_get_qn (struct MeshConnection *c, int fwd)
2366 struct MeshFlowControl *fc;
2368 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2375 * Allow the connection to advertise a buffer of the given size.
2377 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2378 * allowing up to last_pid_recv + buffer.
2380 * @param c Connection.
2381 * @param buffer How many more messages the connection can accept.
2382 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2385 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2387 send_ack (c, buffer, fwd, GNUNET_NO);
2392 * Notify other peers on a connection of a broken link. Mark connections
2393 * to destroy after all traffic has been sent.
2395 * @param c Connection on which there has been a disconnection.
2396 * @param peer Peer that disconnected.
2399 GMC_notify_broken (struct MeshConnection *c,
2400 struct MeshPeer *peer)
2404 LOG (GNUNET_ERROR_TYPE_DEBUG,
2405 " notify broken on %s due to %s disconnect\n",
2406 GMC_2s (c), GMP_2s (peer));
2408 fwd = peer == get_prev_hop (c);
2410 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2412 /* Local shutdown, no one to notify about this. */
2416 if (GNUNET_NO == c->destroy)
2417 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2419 /* Connection will have at least one pending message
2420 * (the one we just scheduled), so no point in checking whether to
2421 * destroy immediately. */
2422 c->destroy = GNUNET_YES;
2423 c->state = MESH_CONNECTION_DESTROYED;
2426 * Cancel all queues, if no message is left, connection will be destroyed.
2428 connection_cancel_queues (c, !fwd);
2435 * Is this peer the first one on the connection?
2437 * @param c Connection.
2438 * @param fwd Is this about fwd traffic?
2440 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2443 GMC_is_origin (struct MeshConnection *c, int fwd)
2445 if (!fwd && c->path->length - 1 == c->own_pos )
2447 if (fwd && 0 == c->own_pos)
2454 * Is this peer the last one on the connection?
2456 * @param c Connection.
2457 * @param fwd Is this about fwd traffic?
2458 * Note that the ROOT is the terminal for BCK traffic!
2460 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2463 GMC_is_terminal (struct MeshConnection *c, int fwd)
2465 return GMC_is_origin (c, !fwd);
2470 * See if we are allowed to send by the next hop in the given direction.
2472 * @param c Connection.
2473 * @param fwd Is this about fwd traffic?
2475 * @return #GNUNET_YES in case it's OK to send.
2478 GMC_is_sendable (struct MeshConnection *c, int fwd)
2480 struct MeshFlowControl *fc;
2482 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2483 if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2489 * Sends an already built message on a connection, properly registering
2490 * all used resources.
2492 * @param message Message to send. Function makes a copy of it.
2493 * If message is not hop-by-hop, decrements TTL of copy.
2494 * @param c Connection on which this message is transmitted.
2495 * @param fwd Is this a fwd message?
2496 * @param force Force the connection to accept the message (buffer overfill).
2497 * @param cont Continuation called once message is sent. Can be NULL.
2498 * @param cont_cls Closure for @c cont.
2500 * @return Handle to cancel the message before it's sent.
2501 * NULL on error or if @c cont is NULL.
2502 * Invalid on @c cont call.
2504 struct MeshConnectionQueue *
2505 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2506 struct MeshConnection *c, int fwd, int force,
2507 GMC_sent cont, void *cont_cls)
2509 struct MeshFlowControl *fc;
2510 struct MeshConnectionQueue *q;
2516 size = ntohs (message->size);
2517 data = GNUNET_malloc (size);
2518 memcpy (data, message, size);
2519 type = ntohs (message->type);
2520 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2521 GM_m2s (type), size, GMC_2s (c));
2523 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2524 droppable = GNUNET_NO == force;
2527 struct GNUNET_MESH_Encrypted *emsg;
2528 struct GNUNET_MESH_KX *kmsg;
2529 struct GNUNET_MESH_ACK *amsg;
2530 struct GNUNET_MESH_Poll *pmsg;
2531 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2532 struct GNUNET_MESH_ConnectionBroken *bmsg;
2535 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2536 emsg = (struct GNUNET_MESH_Encrypted *) data;
2537 ttl = ntohl (emsg->ttl);
2540 GNUNET_break_op (0);
2545 emsg->ttl = htonl (ttl - 1);
2546 emsg->pid = htonl (fc->next_pid++);
2547 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2549 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2550 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2551 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2552 if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2554 GMC_start_poll (c, fwd);
2558 case GNUNET_MESSAGE_TYPE_MESH_KX:
2559 kmsg = (struct GNUNET_MESH_KX *) data;
2563 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2564 amsg = (struct GNUNET_MESH_ACK *) data;
2566 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2567 droppable = GNUNET_NO;
2570 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2571 pmsg = (struct GNUNET_MESH_Poll *) data;
2573 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2574 droppable = GNUNET_NO;
2577 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2578 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2583 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2584 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2589 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2590 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2591 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
2600 if (fc->queue_n > fc->queue_max && droppable)
2602 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2605 LOG (GNUNET_ERROR_TYPE_DEBUG,
2606 "queue full: %u/%u\n",
2607 fc->queue_n, fc->queue_max);
2608 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2614 return NULL; /* Drop this message */
2617 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2618 c->pending_messages++;
2622 (void) GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2623 &message_sent, NULL);
2627 q = GNUNET_new (struct MeshConnectionQueue);
2628 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2638 q->cont_cls = cont_cls;
2644 * Cancel a previously sent message while it's in the queue.
2646 * ONLY can be called before the continuation given to the send function
2647 * is called. Once the continuation is called, the message is no longer in the
2650 * @param q Handle to the queue.
2653 GMC_cancel (struct MeshConnectionQueue *q)
2655 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2657 /* queue destroy calls message_sent, which calls q->cont and frees q */
2658 GMP_queue_destroy (q->q, GNUNET_YES);
2663 * Sends a CREATE CONNECTION message for a path to a peer.
2664 * Changes the connection and tunnel states if necessary.
2666 * @param connection Connection to create.
2669 GMC_send_create (struct MeshConnection *connection)
2671 enum MeshTunnel3CState state;
2674 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2675 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2676 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2677 GMP_queue_add (get_next_hop (connection), NULL,
2678 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2679 size, connection, GNUNET_YES, &message_sent, NULL);
2680 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2681 connection, connection->pending_messages);
2682 connection->pending_messages++;
2683 state = GMT_get_cstate (connection->t);
2684 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2685 GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
2686 if (MESH_CONNECTION_NEW == connection->state)
2687 connection_change_state (connection, MESH_CONNECTION_SENT);
2692 * Send a message to all peers in this connection that the connection
2693 * is no longer valid.
2695 * If some peer should not receive the message, it should be zero'ed out
2696 * before calling this function.
2698 * @param c The connection whose peers to notify.
2701 GMC_send_destroy (struct MeshConnection *c)
2703 struct GNUNET_MESH_ConnectionDestroy msg;
2705 if (GNUNET_YES == c->destroy)
2708 msg.header.size = htons (sizeof (msg));
2709 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2711 LOG (GNUNET_ERROR_TYPE_DEBUG,
2712 " sending connection destroy for connection %s\n",
2715 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2716 GMC_send_prebuilt_message (&msg.header, c,
2717 GNUNET_YES, GNUNET_YES, NULL, NULL);
2718 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2719 GMC_send_prebuilt_message (&msg.header, c,
2720 GNUNET_NO, GNUNET_YES, NULL, NULL);
2721 c->destroy = GNUNET_YES;
2722 c->state = MESH_CONNECTION_DESTROYED;
2727 * @brief Start a polling timer for the connection.
2729 * When a neighbor does not accept more traffic on the connection it could be
2730 * caused by a simple congestion or by a lost ACK. Polling enables to check
2731 * for the lastest ACK status for a connection.
2733 * @param c Connection.
2734 * @param fwd Should we poll in the FWD direction?
2737 GMC_start_poll (struct MeshConnection *c, int fwd)
2739 struct MeshFlowControl *fc;
2741 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2742 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
2744 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
2746 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
2747 fc->poll_task, fc->poll_msg);
2750 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
2751 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2758 * @brief Stop polling a connection for ACKs.
2760 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2762 * @param c Connection.
2763 * @param fwd Should we stop the poll in the FWD direction?
2766 GMC_stop_poll (struct MeshConnection *c, int fwd)
2768 struct MeshFlowControl *fc;
2770 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2771 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2773 GNUNET_SCHEDULER_cancel (fc->poll_task);
2774 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2779 * Get a (static) string for a connection.
2781 * @param c Connection.
2784 GMC_2s (struct MeshConnection *c)
2791 static char buf[128];
2793 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2796 return GNUNET_h2s (&c->id);