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";
338 return "MESH_CONNECTION_STATE_ERROR";
344 * Initialize a Flow Control structure to the initial state.
346 * @param fc Flow Control structure to initialize.
349 fc_init (struct MeshFlowControl *fc)
352 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
353 fc->last_pid_recv = (uint32_t) -1;
354 fc->last_ack_sent = (uint32_t) 0;
355 fc->last_ack_recv = (uint32_t) 0;
356 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
357 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
359 fc->queue_max = (max_msgs_queue / max_connections) + 1;
366 * @param cid Connection ID.
368 static struct MeshConnection *
369 connection_get (const struct GNUNET_HashCode *cid)
371 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
376 connection_change_state (struct MeshConnection* c,
377 enum MeshConnectionState state)
379 LOG (GNUNET_ERROR_TYPE_DEBUG,
380 "Connection %s state was %s\n",
381 GMC_2s (c), GMC_state2s (c->state));
382 LOG (GNUNET_ERROR_TYPE_DEBUG,
383 "Connection %s state is now %s\n",
384 GMC_2s (c), GMC_state2s (state));
390 * Callback called when a queued ACK message is sent.
392 * @param cls Closure (FC).
393 * @param c Connection this message was on.
394 * @param q Queue handler this call invalidates.
395 * @param type Type of message sent.
396 * @param fwd Was this a FWD going message?
397 * @param size Size of the message.
401 struct MeshConnection *c,
402 struct MeshConnectionQueue *q,
403 uint16_t type, int fwd, size_t size)
405 struct MeshFlowControl *fc = cls;
412 * Send an ACK on the connection, informing the predecessor about
413 * the available buffer space. Should not be called in case the peer
414 * is origin (no predecessor) in the @c fwd direction.
416 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
417 * the ACK itself goes "back" (dest->root).
419 * @param c Connection on which to send the ACK.
420 * @param buffer How much space free to advertise?
421 * @param fwd Is this FWD ACK? (Going dest -> root)
422 * @param force Don't optimize out.
425 send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
427 struct MeshFlowControl *next_fc;
428 struct MeshFlowControl *prev_fc;
429 struct GNUNET_MESH_ACK msg;
433 /* If origin, there is no connection to send ACKs. Wrong function! */
434 if (GMC_is_origin (c, fwd))
440 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
441 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
443 LOG (GNUNET_ERROR_TYPE_DEBUG,
444 "connection send %s ack on %s\n",
445 fwd ? "FWD" : "BCK", GMC_2s (c));
447 /* Check if we need to transmit the ACK. */
448 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
449 if (3 < delta && buffer < delta && GNUNET_NO == force)
451 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
452 LOG (GNUNET_ERROR_TYPE_DEBUG,
453 " last pid recv: %u, last ack sent: %u\n",
454 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
458 /* Ok, ACK might be necessary, what PID to ACK? */
459 ack = prev_fc->last_pid_recv + buffer;
460 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
461 LOG (GNUNET_ERROR_TYPE_DEBUG,
462 " last pid %u, last ack %u, qmax %u, q %u\n",
463 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
464 next_fc->queue_max, next_fc->queue_n);
465 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
467 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
471 /* Check if message is already in queue */
472 if (NULL != prev_fc->ack_msg)
474 if (GMC_is_pid_bigger (ack, prev_fc->last_ack_sent))
476 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
477 GMC_cancel (prev_fc->ack_msg);
478 /* GMC_cancel triggers ack_sent(), which clears fc->ack_msg */
482 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
487 prev_fc->last_ack_sent = ack;
489 /* Build ACK message and send on connection */
490 msg.header.size = htons (sizeof (msg));
491 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
492 msg.ack = htonl (ack);
495 prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c, !fwd,
501 * Callback called when a queued message is sent.
503 * Calculates the average time and connection packet tracking.
505 * @param cls Closure (ConnectionQueue Handle).
506 * @param c Connection this message was on.
507 * @param type Type of message sent.
508 * @param fwd Was this a FWD going message?
509 * @param size Size of the message.
510 * @param wait Time spent waiting for core (only the time for THIS message)
513 message_sent (void *cls,
514 struct MeshConnection *c, uint16_t type,
515 int fwd, size_t size,
516 struct GNUNET_TIME_Relative wait)
518 struct MeshConnectionPerformance *p;
519 struct MeshFlowControl *fc;
520 struct MeshConnectionQueue *q = cls;
523 fc = fwd ? &c->fwd_fc : &c->bck_fc;
524 LOG (GNUNET_ERROR_TYPE_DEBUG,
527 GNUNET_MESH_DEBUG_M2S (type));
528 LOG (GNUNET_ERROR_TYPE_DEBUG, "! C_P- %p %u\n", c, c->pending_messages);
533 LOG (GNUNET_ERROR_TYPE_DEBUG, "! calling cont\n");
534 q->cont (q->cont_cls, c, q, type, fwd, size);
538 c->pending_messages--;
539 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
541 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
545 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
548 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
550 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
552 LOG (GNUNET_ERROR_TYPE_DEBUG,
553 "! accounting pid %u\n",
555 GMC_send_ack (c, fwd, GNUNET_NO);
558 case GNUNET_MESSAGE_TYPE_MESH_POLL:
562 case GNUNET_MESSAGE_TYPE_MESH_ACK:
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
572 return; /* Only endpoints are interested in timing. */
575 usecsperbyte = ((double) wait.rel_value_us) / size;
576 if (p->size == AVG_MSGS)
578 /* Array is full. Substract oldest value, add new one and store. */
579 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
580 p->usecsperbyte[p->idx] = usecsperbyte;
581 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
585 /* Array not yet full. Add current value to avg and store. */
586 p->usecsperbyte[p->idx] = usecsperbyte;
588 p->avg += p->usecsperbyte[p->idx];
592 p->idx = (p->idx + 1) % AVG_MSGS;
597 * Get the previous hop in a connection
599 * @param c Connection.
601 * @return Previous peer in the connection.
603 static struct MeshPeer *
604 get_prev_hop (const struct MeshConnection *c)
608 LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos);
609 if (0 == c->own_pos || c->path->length < 2)
610 id = c->path->peers[0];
612 id = c->path->peers[c->own_pos - 1];
614 return GMP_get_short (id);
619 * Get the next hop in a connection
621 * @param c Connection.
623 * @return Next peer in the connection.
625 static struct MeshPeer *
626 get_next_hop (const struct MeshConnection *c)
630 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
631 id = c->path->peers[c->path->length - 1];
633 id = c->path->peers[c->own_pos + 1];
635 return GMP_get_short (id);
640 * Get the hop in a connection.
642 * @param c Connection.
643 * @param fwd Next hop?
645 * @return Next peer in the connection.
647 static struct MeshPeer *
648 get_hop (struct MeshConnection *c, int fwd)
651 return get_next_hop (c);
652 return get_prev_hop (c);
657 * Is traffic coming from this sender 'FWD' traffic?
659 * @param c Connection to check.
660 * @param sender Peer identity of neighbor.
662 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
663 * the traffic is 'FWD'.
664 * #GNUNET_NO for BCK.
665 * #GNUNET_SYSERR for errors.
668 is_fwd (const struct MeshConnection *c,
669 const struct GNUNET_PeerIdentity *sender)
673 id = GNUNET_PEER_search (sender);
674 if (GMP_get_short_id (get_prev_hop (c)) == id)
677 if (GMP_get_short_id (get_next_hop (c)) == id)
681 return GNUNET_SYSERR;
686 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
687 * or a first CONNECTION_ACK directed to us.
689 * @param connection Connection to confirm.
690 * @param fwd Should we send it FWD? (root->dest)
691 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
694 send_connection_ack (struct MeshConnection *connection, int fwd)
696 struct MeshTunnel3 *t;
699 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
700 !fwd ? "FWD" : "BCK");
701 GMP_queue_add (get_hop (connection, fwd), NULL,
702 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
703 sizeof (struct GNUNET_MESH_ConnectionACK),
704 connection, fwd, &message_sent, NULL);
705 connection->pending_messages++;
706 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
707 GMT_change_state (t, MESH_TUNNEL3_WAITING);
708 if (MESH_CONNECTION_READY != connection->state)
709 connection_change_state (connection, MESH_CONNECTION_SENT);
714 * Send a notification that a connection is broken.
716 * @param c Connection that is broken.
717 * @param id1 Peer that has disconnected.
718 * @param id2 Peer that has disconnected.
719 * @param fwd Direction towards which to send it.
722 send_broken (struct MeshConnection *c,
723 const struct GNUNET_PeerIdentity *id1,
724 const struct GNUNET_PeerIdentity *id2,
727 struct GNUNET_MESH_ConnectionBroken msg;
729 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
730 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
734 GMC_send_prebuilt_message (&msg.header, c, fwd, NULL, NULL);
740 * Send keepalive packets for a connection.
742 * @param c Connection to keep alive..
743 * @param fwd Is this a FWD keepalive? (owner -> dest).
745 * FIXME use only one type, register in GMC_send_prebuilt_message()
748 connection_keepalive (struct MeshConnection *c, int fwd)
750 struct GNUNET_MESH_ConnectionKeepAlive *msg;
751 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
755 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
756 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "sending %s keepalive for connection %s]\n",
760 fwd ? "FWD" : "BCK", GMC_2s (c));
762 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
763 msg->header.size = htons (size);
764 msg->header.type = htons (type);
767 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
772 * Send CONNECTION_{CREATE/ACK} packets for a connection.
774 * @param c Connection for which to send the message.
775 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
778 connection_recreate (struct MeshConnection *c, int fwd)
780 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
784 send_connection_ack (c, GNUNET_NO);
789 * Generic connection timer management.
790 * Depending on the role of the peer in the connection will send the
791 * appropriate message (build or keepalive)
793 * @param c Conncetion to maintain.
797 connection_maintain (struct MeshConnection *c, int fwd)
799 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
801 /* TODO DHT GET with RO_BART */
806 case MESH_CONNECTION_NEW:
809 case MESH_CONNECTION_SENT:
810 connection_recreate (c, fwd);
812 case MESH_CONNECTION_READY:
813 connection_keepalive (c, fwd);
822 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
824 struct MeshConnection *c = cls;
825 struct GNUNET_TIME_Relative delay;
827 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
828 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
831 connection_maintain (c, GNUNET_YES);
832 delay = c->state == MESH_CONNECTION_READY ?
833 refresh_connection_time : create_connection_time;
834 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
835 &connection_fwd_keepalive,
841 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
843 struct MeshConnection *c = cls;
844 struct GNUNET_TIME_Relative delay;
846 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
847 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
850 connection_maintain (c, GNUNET_NO);
851 delay = c->state == MESH_CONNECTION_READY ?
852 refresh_connection_time : create_connection_time;
853 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (delay,
854 &connection_bck_keepalive,
860 * @brief Re-initiate traffic on this connection if necessary.
862 * Check if there is traffic queued towards this peer
863 * and the core transmit handle is NULL (traffic was stalled).
864 * If so, call core tmt rdy.
866 * @param c Connection on which initiate traffic.
867 * @param fwd Is this about fwd traffic?
870 connection_unlock_queue (struct MeshConnection *c, int fwd)
872 struct MeshPeer *peer;
874 LOG (GNUNET_ERROR_TYPE_DEBUG,
875 "connection_unlock_queue %s on %s\n",
876 fwd ? "FWD" : "BCK", GMC_2s (c));
878 if (GMC_is_terminal (c, fwd))
880 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
884 peer = get_hop (c, fwd);
885 GMP_queue_unlock (peer, c);
890 * Cancel all transmissions that belong to a certain connection.
892 * If the connection is scheduled for destruction and no more messages are left,
893 * the connection will be destroyed by the continuation call.
895 * @param c Connection which to cancel. Might be destroyed during this call.
896 * @param fwd Cancel fwd traffic?
899 connection_cancel_queues (struct MeshConnection *c, int fwd)
901 struct MeshFlowControl *fc;
902 struct MeshPeer *peer;
910 fc = fwd ? &c->fwd_fc : &c->bck_fc;
911 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
913 GNUNET_SCHEDULER_cancel (fc->poll_task);
914 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
916 peer = get_hop (c, fwd);
917 GMP_queue_cancel (peer, c);
922 * Function called if a connection has been stalled for a while,
923 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
925 * @param cls Closure (poll ctx).
926 * @param tc TaskContext.
929 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
933 * Callback called when a queued POLL message is sent.
935 * @param cls Closure (FC).
936 * @param c Connection this message was on.
937 * @param q Queue handler this call invalidates.
938 * @param type Type of message sent.
939 * @param fwd Was this a FWD going message?
940 * @param size Size of the message.
943 poll_sent (void *cls,
944 struct MeshConnection *c,
945 struct MeshConnectionQueue *q,
946 uint16_t type, int fwd, size_t size)
948 struct MeshFlowControl *fc = cls;
950 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL sent, scheduling new one!\n");
952 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
953 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
954 &connection_poll, fc);
958 * Function called if a connection has been stalled for a while,
959 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
961 * @param cls Closure (poll ctx).
962 * @param tc TaskContext.
965 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
967 struct MeshFlowControl *fc = cls;
968 struct GNUNET_MESH_Poll msg;
969 struct MeshConnection *c;
971 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
972 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
978 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
979 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%s]\n", GMC_2s (c));
980 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
981 fc == &c->fwd_fc ? "FWD" : "BCK");
983 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
984 msg.header.size = htons (sizeof (msg));
985 msg.pid = htonl (fc->last_pid_sent);
986 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
987 fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c, fc == &c->fwd_fc,
993 * Timeout function due to lack of keepalive/traffic from the owner.
994 * Destroys connection if called.
996 * @param cls Closure (connection to destroy).
997 * @param tc TaskContext.
1000 connection_fwd_timeout (void *cls,
1001 const struct GNUNET_SCHEDULER_TaskContext *tc)
1003 struct MeshConnection *c = cls;
1005 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1006 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1008 LOG (GNUNET_ERROR_TYPE_DEBUG,
1009 "Connection %s[%X] FWD timed out. Destroying.\n",
1013 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1021 * Timeout function due to lack of keepalive/traffic from the destination.
1022 * Destroys connection if called.
1024 * @param cls Closure (connection to destroy).
1025 * @param tc TaskContext
1028 connection_bck_timeout (void *cls,
1029 const struct GNUNET_SCHEDULER_TaskContext *tc)
1031 struct MeshConnection *c = cls;
1033 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1034 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1037 LOG (GNUNET_ERROR_TYPE_DEBUG,
1038 "Connection %s[%X] FWD timed out. Destroying.\n",
1039 GMT_2s (c->t), c->id);
1041 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1049 * Resets the connection timeout task, some other message has done the
1051 * - For the first peer on the direction this means to send
1052 * a keepalive or a path confirmation message (either create or ACK).
1053 * - For all other peers, this means to destroy the connection,
1054 * due to lack of activity.
1055 * Starts the tiemout if no timeout was running (connection just created).
1057 * @param c Connection whose timeout to reset.
1058 * @param fwd Is this forward?
1060 * TODO use heap to improve efficiency of scheduler.
1063 connection_reset_timeout (struct MeshConnection *c, int fwd)
1065 GNUNET_SCHEDULER_TaskIdentifier *ti;
1066 GNUNET_SCHEDULER_Task f;
1068 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1070 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1071 GNUNET_SCHEDULER_cancel (*ti);
1073 if (GMC_is_origin (c, fwd)) /* Startpoint */
1075 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1076 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1078 else /* Relay, endpoint. */
1080 struct GNUNET_TIME_Relative delay;
1082 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1083 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1084 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1090 * Add the connection to the list of both neighbors.
1092 * @param c Connection.
1094 * @return #GNUNET_OK if everything went fine
1095 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1098 register_neighbors (struct MeshConnection *c)
1100 struct MeshPeer *next_peer;
1101 struct MeshPeer *prev_peer;
1103 next_peer = get_next_hop (c);
1104 prev_peer = get_prev_hop (c);
1106 if (GNUNET_NO == GMP_is_neighbor (next_peer)
1107 || GNUNET_NO == GMP_is_neighbor (prev_peer))
1108 return GNUNET_SYSERR;
1110 GMP_add_connection (next_peer, c);
1111 GMP_add_connection (prev_peer, c);
1118 * Remove the connection from the list of both neighbors.
1120 * @param c Connection.
1123 unregister_neighbors (struct MeshConnection *c)
1125 struct MeshPeer *peer;
1127 peer = get_next_hop (c);
1128 GMP_remove_connection (peer, c);
1130 peer = get_prev_hop (c);
1131 GMP_remove_connection (peer, c);
1137 * Bind the connection to the peer and the tunnel to that peer.
1139 * If the peer has no tunnel, create one. Update tunnel and connection
1140 * data structres to reflect new status.
1142 * @param c Connection.
1146 add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
1148 GMP_add_tunnel (peer);
1149 c->t = GMP_get_tunnel (peer);
1150 GMT_add_connection (c->t, c);
1153 /******************************************************************************/
1154 /******************************** API ***********************************/
1155 /******************************************************************************/
1158 * Core handler for connection creation.
1160 * @param cls Closure (unused).
1161 * @param peer Sender (neighbor).
1162 * @param message Message.
1164 * @return GNUNET_OK to keep the connection open,
1165 * GNUNET_SYSERR to close it (signal serious error)
1168 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1169 const struct GNUNET_MessageHeader *message)
1171 struct GNUNET_MESH_ConnectionCreate *msg;
1172 struct GNUNET_PeerIdentity *id;
1173 struct GNUNET_HashCode *cid;
1174 struct MeshPeerPath *path;
1175 struct MeshPeer *dest_peer;
1176 struct MeshPeer *orig_peer;
1177 struct MeshConnection *c;
1178 unsigned int own_pos;
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1183 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1186 size = ntohs (message->size);
1187 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1189 GNUNET_break_op (0);
1193 /* Calculate hops */
1194 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1195 if (size % sizeof (struct GNUNET_PeerIdentity))
1197 GNUNET_break_op (0);
1200 size /= sizeof (struct GNUNET_PeerIdentity);
1203 GNUNET_break_op (0);
1206 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1208 /* Get parameters */
1209 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1211 id = (struct GNUNET_PeerIdentity *) &msg[1];
1212 LOG (GNUNET_ERROR_TYPE_DEBUG,
1213 " connection %s (%s).\n",
1214 GNUNET_h2s (cid), GNUNET_i2s (id));
1216 /* Create connection */
1217 c = connection_get (cid);
1221 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1222 path = path_new (size);
1224 for (i = 0; i < size; i++)
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1227 GNUNET_i2s (&id[i]));
1228 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1229 if (path->peers[i] == myid)
1232 if (own_pos == 0 && path->peers[own_pos] != myid)
1234 /* create path: self not found in path through self */
1235 GNUNET_break_op (0);
1236 path_destroy (path);
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1240 GMP_add_path_to_all (path, GNUNET_NO);
1241 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1242 c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
1245 path_destroy (path);
1248 connection_reset_timeout (c, GNUNET_YES);
1252 path = path_duplicate (c->path);
1254 if (MESH_CONNECTION_NEW == c->state)
1255 connection_change_state (c, MESH_CONNECTION_SENT);
1257 /* Remember peers */
1258 dest_peer = GMP_get (&id[size - 1]);
1259 orig_peer = GMP_get (&id[0]);
1261 /* Is it a connection to us? */
1262 if (c->own_pos == size - 1)
1264 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1265 GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
1267 add_to_peer (c, orig_peer);
1268 if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
1269 GMT_change_state (c->t, MESH_TUNNEL3_WAITING);
1271 send_connection_ack (c, GNUNET_NO);
1272 if (MESH_CONNECTION_SENT == c->state)
1273 connection_change_state (c, MESH_CONNECTION_ACK);
1275 /* Keep tunnel alive in direction dest->owner*/
1276 c->bck_maintenance_task =
1277 GNUNET_SCHEDULER_add_delayed (create_connection_time,
1278 &connection_bck_keepalive, c);
1282 /* It's for somebody else! Retransmit. */
1283 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1284 GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1285 GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1286 GMC_send_prebuilt_message (message, c, GNUNET_YES, NULL, NULL);
1288 path_destroy (path);
1294 * Core handler for path confirmations.
1296 * @param cls closure
1297 * @param message message
1298 * @param peer peer identity this notification is about
1300 * @return GNUNET_OK to keep the connection open,
1301 * GNUNET_SYSERR to close it (signal serious error)
1304 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1305 const struct GNUNET_MessageHeader *message)
1307 struct GNUNET_MESH_ConnectionACK *msg;
1308 struct MeshConnection *c;
1309 struct MeshPeerPath *p;
1310 struct MeshPeer *pi;
1311 enum MeshConnectionState oldstate;
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1315 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1316 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1317 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1318 GNUNET_h2s (&msg->cid));
1319 c = connection_get (&msg->cid);
1322 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1324 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1328 oldstate = c->state;
1329 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1330 pi = GMP_get (peer);
1331 if (get_next_hop (c) == pi)
1333 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1335 if (MESH_CONNECTION_SENT == oldstate)
1336 connection_change_state (c, MESH_CONNECTION_ACK);
1338 else if (get_prev_hop (c) == pi)
1340 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1342 connection_change_state (c, MESH_CONNECTION_READY);
1346 GNUNET_break_op (0);
1350 connection_reset_timeout (c, fwd);
1352 /* Add path to peers? */
1356 GMP_add_path_to_all (p, GNUNET_YES);
1363 /* Message for us as creator? */
1364 if (GMC_is_origin (c, GNUNET_YES))
1366 if (GNUNET_NO != fwd)
1368 GNUNET_break_op (0);
1371 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1373 /* If just created, cancel the short timeout and start a long one */
1374 if (MESH_CONNECTION_SENT == oldstate)
1375 connection_reset_timeout (c, GNUNET_YES);
1377 /* Change connection and tunnel state */
1378 connection_change_state (c, MESH_CONNECTION_READY);
1379 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1380 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1382 /* Send ACK (~TCP ACK)*/
1383 send_connection_ack (c, GNUNET_YES);
1387 /* Message for us as destination? */
1388 if (GMC_is_terminal (c, GNUNET_YES))
1390 if (GNUNET_YES != fwd)
1392 GNUNET_break_op (0);
1395 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1397 /* If just created, cancel the short timeout and start a long one */
1398 if (MESH_CONNECTION_ACK == oldstate)
1399 connection_reset_timeout (c, GNUNET_NO);
1401 /* Change tunnel state */
1402 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1403 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1408 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1409 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1415 * Core handler for notifications of broken paths
1417 * @param cls Closure (unused).
1418 * @param id Peer identity of sending neighbor.
1419 * @param message Message.
1421 * @return GNUNET_OK to keep the connection open,
1422 * GNUNET_SYSERR to close it (signal serious error)
1425 GMC_handle_broken (void* cls,
1426 const struct GNUNET_PeerIdentity* id,
1427 const struct GNUNET_MessageHeader* message)
1429 struct GNUNET_MESH_ConnectionBroken *msg;
1430 struct MeshConnection *c;
1433 LOG (GNUNET_ERROR_TYPE_DEBUG,
1434 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (id));
1435 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1437 GNUNET_i2s (&msg->peer1));
1438 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1439 GNUNET_i2s (&msg->peer2));
1440 c = connection_get (&msg->cid);
1443 GNUNET_break_op (0);
1447 fwd = is_fwd (c, id);
1448 connection_cancel_queues (c, !fwd);
1449 if (GMC_is_terminal (c, fwd))
1451 if (0 < c->pending_messages)
1452 c->destroy = GNUNET_YES;
1458 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1459 c->destroy = GNUNET_YES;
1468 * Core handler for tunnel destruction
1470 * @param cls Closure (unused).
1471 * @param peer Peer identity of sending neighbor.
1472 * @param message Message.
1474 * @return GNUNET_OK to keep the connection open,
1475 * GNUNET_SYSERR to close it (signal serious error)
1478 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1479 const struct GNUNET_MessageHeader *message)
1481 struct GNUNET_MESH_ConnectionDestroy *msg;
1482 struct MeshConnection *c;
1485 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1486 LOG (GNUNET_ERROR_TYPE_DEBUG,
1487 "Got a CONNECTION DESTROY message from %s\n",
1489 LOG (GNUNET_ERROR_TYPE_DEBUG,
1490 " for connection %s\n",
1491 GNUNET_h2s (&msg->cid));
1492 c = connection_get (&msg->cid);
1495 /* Probably already got the message from another path,
1496 * destroyed the tunnel and retransmitted to children.
1499 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1503 fwd = is_fwd (c, peer);
1504 if (GNUNET_SYSERR == fwd)
1506 GNUNET_break_op (0);
1509 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1510 c->destroy = GNUNET_YES;
1516 * Generic handler for mesh network encrypted traffic.
1518 * @param peer Peer identity this notification is about.
1519 * @param msg Encrypted message.
1521 * @return GNUNET_OK to keep the connection open,
1522 * GNUNET_SYSERR to close it (signal serious error)
1525 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1526 const struct GNUNET_MESH_Encrypted *msg)
1528 struct MeshConnection *c;
1529 struct MeshPeer *neighbor;
1530 struct MeshFlowControl *fc;
1531 GNUNET_PEER_Id peer_id;
1539 size = ntohs (msg->header.size);
1541 sizeof (struct GNUNET_MESH_Encrypted) +
1542 sizeof (struct GNUNET_MessageHeader))
1544 GNUNET_break_op (0);
1547 type = ntohs (msg->header.type);
1548 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1549 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
1550 GNUNET_MESH_DEBUG_M2S (type), ntohl (msg->pid), GNUNET_i2s (peer));
1552 /* Check connection */
1553 c = connection_get (&msg->cid);
1556 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1557 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1561 /* Check if origin is as expected */
1562 neighbor = get_prev_hop (c);
1563 peer_id = GNUNET_PEER_search (peer);
1564 if (peer_id == GMP_get_short_id (neighbor))
1570 neighbor = get_next_hop (c);
1571 if (peer_id == GMP_get_short_id (neighbor))
1577 /* Unexpected peer sending traffic on a connection. */
1578 GNUNET_break_op (0);
1584 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1585 pid = ntohl (msg->pid);
1586 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1588 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1589 LOG (GNUNET_ERROR_TYPE_DEBUG,
1590 "WARNING Received PID %u, (prev %u), ACK %u\n",
1591 pid, fc->last_pid_recv, fc->last_ack_sent);
1594 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1596 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1597 LOG (GNUNET_ERROR_TYPE_DEBUG,
1598 " Pid %u not expected (%u+), dropping!\n",
1599 pid, fc->last_pid_recv + 1);
1602 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1603 connection_change_state (c, MESH_CONNECTION_READY);
1604 connection_reset_timeout (c, fwd);
1605 fc->last_pid_recv = pid;
1607 /* Is this message for us? */
1608 if (GMC_is_terminal (c, fwd))
1610 /* TODO signature verification */
1611 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1612 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1619 fc->last_pid_recv = pid;
1620 GMT_handle_encrypted (c->t, msg);
1621 GMC_send_ack (c, fwd, GNUNET_NO);
1625 /* Message not for us: forward to next hop */
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1627 ttl = ntohl (msg->ttl);
1628 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1631 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1632 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1633 GMC_send_ack (c, fwd, GNUNET_NO);
1637 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1638 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
1644 * Generic handler for mesh network encrypted traffic.
1646 * @param peer Peer identity this notification is about.
1647 * @param msg Encrypted message.
1649 * @return GNUNET_OK to keep the connection open,
1650 * GNUNET_SYSERR to close it (signal serious error)
1653 handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
1654 const struct GNUNET_MESH_KX *msg)
1656 struct MeshConnection *c;
1657 struct MeshPeer *neighbor;
1658 GNUNET_PEER_Id peer_id;
1664 size = ntohs (msg->header.size);
1666 sizeof (struct GNUNET_MESH_Encrypted) +
1667 sizeof (struct GNUNET_MessageHeader))
1669 GNUNET_break_op (0);
1672 type = ntohs (msg->header.type);
1673 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1674 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1675 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1677 /* Check connection */
1678 c = connection_get (&msg->cid);
1681 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1682 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1686 /* Check if origin is as expected */
1687 neighbor = get_prev_hop (c);
1688 peer_id = GNUNET_PEER_search (peer);
1689 if (peer_id == GMP_get_short_id (neighbor))
1695 neighbor = get_next_hop (c);
1696 if (peer_id == GMP_get_short_id (neighbor))
1702 /* Unexpected peer sending traffic on a connection. */
1703 GNUNET_break_op (0);
1708 /* Count as connection confirmation. */
1709 if (MESH_CONNECTION_SENT == c->state || MESH_CONNECTION_ACK == c->state)
1710 connection_change_state (c, MESH_CONNECTION_READY);
1711 connection_reset_timeout (c, fwd);
1714 if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
1715 GMT_change_state (c->t, MESH_TUNNEL3_READY);
1718 /* Is this message for us? */
1719 if (GMC_is_terminal (c, fwd))
1721 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1722 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1728 GMT_handle_kx (c->t, &msg[1].header);
1732 /* Message not for us: forward to next hop */
1733 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1734 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1735 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
1742 * Core handler for encrypted mesh network traffic (channel mgmt, data).
1744 * @param cls Closure (unused).
1745 * @param message Message received.
1746 * @param peer Peer who sent the message.
1748 * @return GNUNET_OK to keep the connection open,
1749 * GNUNET_SYSERR to close it (signal serious error)
1752 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
1753 const struct GNUNET_MessageHeader *message)
1755 return handle_mesh_encrypted (peer,
1756 (struct GNUNET_MESH_Encrypted *)message);
1761 * Core handler for key exchange traffic (ephemeral key, ping, pong).
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_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
1772 const struct GNUNET_MessageHeader *message)
1774 return handle_mesh_kx (peer,
1775 (struct GNUNET_MESH_KX *) message);
1780 * Core handler for mesh network traffic point-to-point acks.
1782 * @param cls closure
1783 * @param message message
1784 * @param peer peer identity this notification is about
1786 * @return GNUNET_OK to keep the connection open,
1787 * GNUNET_SYSERR to close it (signal serious error)
1790 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1791 const struct GNUNET_MessageHeader *message)
1793 struct GNUNET_MESH_ACK *msg;
1794 struct MeshConnection *c;
1795 struct MeshFlowControl *fc;
1800 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1801 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1803 msg = (struct GNUNET_MESH_ACK *) message;
1805 c = connection_get (&msg->cid);
1809 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1814 /* Is this a forward or backward ACK? */
1815 id = GNUNET_PEER_search (peer);
1816 if (GMP_get_short_id (get_next_hop (c)) == id)
1818 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
1822 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1824 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
1830 GNUNET_break_op (0);
1834 ack = ntohl (msg->ack);
1835 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
1836 ack, fc->last_ack_recv);
1837 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1838 fc->last_ack_recv = ack;
1840 /* Cancel polling if the ACK is big enough. */
1841 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1842 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1844 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
1845 GNUNET_SCHEDULER_cancel (fc->poll_task);
1846 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1847 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1850 connection_unlock_queue (c, fwd);
1857 * Core handler for mesh network traffic point-to-point ack polls.
1859 * @param cls closure
1860 * @param message message
1861 * @param peer peer identity this notification is about
1863 * @return GNUNET_OK to keep the connection open,
1864 * GNUNET_SYSERR to close it (signal serious error)
1867 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1868 const struct GNUNET_MessageHeader *message)
1870 struct GNUNET_MESH_Poll *msg;
1871 struct MeshConnection *c;
1872 struct MeshFlowControl *fc;
1877 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1878 LOG (GNUNET_ERROR_TYPE_DEBUG,
1879 "Got a POLL packet from %s!\n",
1882 msg = (struct GNUNET_MESH_Poll *) message;
1884 c = connection_get (&msg->cid);
1888 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1890 GNUNET_break_op (0);
1894 /* Is this a forward or backward ACK?
1895 * Note: a poll should never be needed in a loopback case,
1896 * since there is no possiblility of packet loss there, so
1897 * this way of discerining FWD/BCK should not be a problem.
1899 id = GNUNET_PEER_search (peer);
1900 if (GMP_get_short_id (get_next_hop (c)) == id)
1902 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
1905 else if (GMP_get_short_id (get_prev_hop (c)) == id)
1907 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
1912 GNUNET_break_op (0);
1916 pid = ntohl (msg->pid);
1917 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
1918 fc->last_pid_recv = pid;
1919 fwd = fc == &c->bck_fc;
1920 GMC_send_ack (c, fwd, GNUNET_YES);
1927 * Core handler for mesh keepalives.
1929 * @param cls closure
1930 * @param message message
1931 * @param peer peer identity this notification is about
1932 * @return GNUNET_OK to keep the connection open,
1933 * GNUNET_SYSERR to close it (signal serious error)
1935 * TODO: Check who we got this from, to validate route.
1938 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1939 const struct GNUNET_MessageHeader *message)
1941 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1942 struct MeshConnection *c;
1943 struct MeshPeer *neighbor;
1946 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1947 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1950 c = connection_get (&msg->cid);
1953 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1958 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1959 GNUNET_YES : GNUNET_NO;
1961 /* Check if origin is as expected */
1962 neighbor = get_hop (c, fwd);
1963 if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1965 GNUNET_break_op (0);
1969 connection_change_state (c, MESH_CONNECTION_READY);
1970 connection_reset_timeout (c, fwd);
1972 if (GMC_is_terminal (c, fwd))
1975 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1976 GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
1983 * Send an ACK on the appropriate connection/channel, depending on
1984 * the direction and the position of the peer.
1986 * @param c Which connection to send the hop-by-hop ACK.
1987 * @param fwd Is this a fwd ACK? (will go dest->root).
1988 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
1991 GMC_send_ack (struct MeshConnection *c, int fwd, int force)
1993 unsigned int buffer;
1995 LOG (GNUNET_ERROR_TYPE_DEBUG,
1996 "GMC send %s ACK on %s\n",
1997 fwd ? "FWD" : "BCK", GMC_2s (c));
2005 /* Get available buffer space */
2006 if (GMC_is_terminal (c, fwd))
2008 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2009 buffer = GMT_get_channels_buffer (c->t);
2013 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2014 buffer = GMC_get_buffer (c, fwd);
2016 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2017 if (0 == buffer && GNUNET_NO == force)
2020 /* Send available buffer space */
2021 if (GMC_is_origin (c, fwd))
2023 GNUNET_assert (NULL != c->t);
2024 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2025 GMT_unchoke_channels (c->t);
2029 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2030 send_ack (c, buffer, fwd, force);
2036 * Initialize the connections subsystem
2038 * @param c Configuration handle.
2041 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2043 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2045 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2048 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2049 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2050 GNUNET_SCHEDULER_shutdown ();
2055 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2058 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2059 "MESH", "MAX_CONNECTIONS", "MISSING");
2060 GNUNET_SCHEDULER_shutdown ();
2065 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2066 &refresh_connection_time))
2068 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2069 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2070 GNUNET_SCHEDULER_shutdown ();
2073 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2074 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2078 * Shut down the connections subsystem.
2083 GNUNET_CONTAINER_multihashmap_destroy (connections);
2088 struct MeshConnection *
2089 GMC_new (const struct GNUNET_HashCode *cid,
2090 struct MeshTunnel3 *t,
2091 struct MeshPeerPath *p,
2092 unsigned int own_pos)
2094 struct MeshConnection *c;
2096 c = GNUNET_new (struct MeshConnection);
2098 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
2099 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2100 fc_init (&c->fwd_fc);
2101 fc_init (&c->bck_fc);
2106 if (own_pos > p->length - 1)
2112 c->own_pos = own_pos;
2117 c->fwd_maintenance_task =
2118 GNUNET_SCHEDULER_add_delayed (create_connection_time,
2119 &connection_fwd_keepalive, c);
2121 if (GNUNET_OK != register_neighbors (c))
2132 GMC_destroy (struct MeshConnection *c)
2137 if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
2138 return; /* -> message_sent -> GMC_destroy. Don't loop. */
2141 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GMC_2s (c));
2143 /* Cancel all traffic */
2144 connection_cancel_queues (c, GNUNET_YES);
2145 connection_cancel_queues (c, GNUNET_NO);
2147 /* Cancel maintainance task (keepalive/timeout) */
2148 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2149 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2150 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2151 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2152 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2153 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2154 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2155 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2156 if (NULL != c->fwd_fc.poll_msg)
2157 GMC_cancel (c->fwd_fc.poll_msg);
2158 if (NULL != c->bck_fc.poll_msg)
2159 GMC_cancel (c->bck_fc.poll_msg);
2161 /* Unregister from neighbors */
2162 unregister_neighbors (c);
2165 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2167 GMT_remove_connection (c->t, c);
2169 if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES))
2170 path_destroy (c->path);
2172 GNUNET_break (GNUNET_YES ==
2173 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
2179 * Get the connection ID.
2181 * @param c Connection to get the ID from.
2183 * @return ID of the connection.
2185 const struct GNUNET_HashCode *
2186 GMC_get_id (const struct MeshConnection *c)
2193 * Get the connection path.
2195 * @param c Connection to get the path from.
2197 * @return path used by the connection.
2199 const struct MeshPeerPath *
2200 GMC_get_path (const struct MeshConnection *c)
2207 * Get the connection state.
2209 * @param c Connection to get the state from.
2211 * @return state of the connection.
2213 enum MeshConnectionState
2214 GMC_get_state (const struct MeshConnection *c)
2220 * Get the connection tunnel.
2222 * @param c Connection to get the tunnel from.
2224 * @return tunnel of the connection.
2226 struct MeshTunnel3 *
2227 GMC_get_tunnel (const struct MeshConnection *c)
2234 * Get free buffer space in a connection.
2236 * @param c Connection.
2237 * @param fwd Is query about FWD traffic?
2239 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2242 GMC_get_buffer (struct MeshConnection *c, int fwd)
2244 struct MeshFlowControl *fc;
2246 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2248 return (fc->queue_max - fc->queue_n);
2252 * Get how many messages have we allowed to send to us from a direction.
2254 * @param c Connection.
2255 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2257 * @return last_ack_sent - last_pid_recv
2260 GMC_get_allowed (struct MeshConnection *c, int fwd)
2262 struct MeshFlowControl *fc;
2264 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2265 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2269 return (fc->last_ack_sent - fc->last_pid_recv);
2273 * Get messages queued in a connection.
2275 * @param c Connection.
2276 * @param fwd Is query about FWD traffic?
2278 * @return Number of messages queued.
2281 GMC_get_qn (struct MeshConnection *c, int fwd)
2283 struct MeshFlowControl *fc;
2285 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2292 * Allow the connection to advertise a buffer of the given size.
2294 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2295 * allowing up to last_pid_recv + buffer.
2297 * @param c Connection.
2298 * @param buffer How many more messages the connection can accept.
2299 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2302 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
2304 send_ack (c, buffer, fwd, GNUNET_NO);
2309 * Notify other peers on a connection of a broken link. Mark connections
2310 * to destroy after all traffic has been sent.
2312 * @param c Connection on which there has been a disconnection.
2313 * @param peer Peer that disconnected.
2316 GMC_notify_broken (struct MeshConnection *c,
2317 struct MeshPeer *peer)
2321 LOG (GNUNET_ERROR_TYPE_DEBUG,
2322 " notify broken on %s due to %s disconnect\n",
2323 GMC_2s (c), GMP_2s (peer));
2325 fwd = peer == get_prev_hop (c);
2327 if (GNUNET_YES == GMC_is_terminal (c, fwd))
2329 /* Local shutdown, no one to notify about this. */
2333 if (GNUNET_NO == c->destroy)
2334 send_broken (c, &my_full_id, GMP_get_id (peer), fwd);
2336 /* Connection will have at least one pending message
2337 * (the one we just scheduled), so no point in checking whether to
2338 * destroy immediately. */
2339 c->destroy = GNUNET_YES;
2342 * Cancel all queues, if no message is left, connection will be destroyed.
2344 connection_cancel_queues (c, !fwd);
2351 * Is this peer the first one on the connection?
2353 * @param c Connection.
2354 * @param fwd Is this about fwd traffic?
2356 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2359 GMC_is_origin (struct MeshConnection *c, int fwd)
2361 if (!fwd && c->path->length - 1 == c->own_pos )
2363 if (fwd && 0 == c->own_pos)
2370 * Is this peer the last one on the connection?
2372 * @param c Connection.
2373 * @param fwd Is this about fwd traffic?
2374 * Note that the ROOT is the terminal for BCK traffic!
2376 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2379 GMC_is_terminal (struct MeshConnection *c, int fwd)
2381 return GMC_is_origin (c, !fwd);
2386 * See if we are allowed to send by the next hop in the given direction.
2388 * @param c Connection.
2389 * @param fwd Is this about fwd traffic?
2391 * @return #GNUNET_YES in case it's OK to send.
2394 GMC_is_sendable (struct MeshConnection *c, int fwd)
2396 struct MeshFlowControl *fc;
2398 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2399 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2405 * Sends an already built message on a connection, properly registering
2406 * all used resources.
2408 * @param message Message to send. Function makes a copy of it.
2409 * If message is not hop-by-hop, decrements TTL of copy.
2410 * @param c Connection on which this message is transmitted.
2411 * @param fwd Is this a fwd message?
2412 * @param cont Continuation called once message is sent. Can be NULL.
2413 * @param cont_cls Closure for @c cont.
2415 * @return Handle to cancel the message before it's sent.
2416 * NULL on error or if @c cont is NULL.
2417 * Invalid on @c cont call.
2419 struct MeshConnectionQueue *
2420 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2421 struct MeshConnection *c, int fwd,
2422 GMC_sent cont, void *cont_cls)
2424 struct MeshFlowControl *fc;
2425 struct MeshConnectionQueue *q;
2431 size = ntohs (message->size);
2432 data = GNUNET_malloc (size);
2433 memcpy (data, message, size);
2434 type = ntohs (message->type);
2435 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
2436 GNUNET_MESH_DEBUG_M2S (type), size, GMC_2s (c));
2438 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2439 droppable = GNUNET_YES;
2442 struct GNUNET_MESH_Encrypted *emsg;
2443 struct GNUNET_MESH_KX *kmsg;
2444 struct GNUNET_MESH_ACK *amsg;
2445 struct GNUNET_MESH_Poll *pmsg;
2446 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2447 struct GNUNET_MESH_ConnectionBroken *bmsg;
2450 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
2451 emsg = (struct GNUNET_MESH_Encrypted *) data;
2452 ttl = ntohl (emsg->ttl);
2455 GNUNET_break_op (0);
2460 emsg->ttl = htonl (ttl - 1);
2461 emsg->pid = htonl (fc->next_pid++);
2462 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2464 LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
2465 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2466 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2467 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2469 GMC_start_poll (c, fwd);
2473 case GNUNET_MESSAGE_TYPE_MESH_KX:
2474 kmsg = (struct GNUNET_MESH_KX *) data;
2478 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2479 amsg = (struct GNUNET_MESH_ACK *) data;
2481 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2482 droppable = GNUNET_NO;
2485 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2486 pmsg = (struct GNUNET_MESH_Poll *) data;
2488 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2489 droppable = GNUNET_NO;
2492 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
2493 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2498 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2499 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2504 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2505 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2512 if (fc->queue_n > fc->queue_max && droppable)
2514 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2517 LOG (GNUNET_ERROR_TYPE_DEBUG,
2518 "queue full: %u/%u\n",
2519 fc->queue_n, fc->queue_max);
2520 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == type)
2526 return NULL; /* Drop this message */
2529 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
2530 c->pending_messages++;
2534 (void) GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2535 &message_sent, NULL);
2539 q = GNUNET_new (struct MeshConnectionQueue);
2540 q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
2550 q->cont_cls = cont_cls;
2556 * Cancel a previously sent message while it's in the queue.
2558 * ONLY can be called before the continuation given to the send function
2559 * is called. Once the continuation is called, the message is no longer in the
2562 * @param q Handle to the queue.
2565 GMC_cancel (struct MeshConnectionQueue *q)
2567 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
2569 /* queue destroy calls message_sent, which calls q->cont and frees q */
2570 GMP_queue_destroy (q->q, GNUNET_YES);
2575 * Sends a CREATE CONNECTION message for a path to a peer.
2576 * Changes the connection and tunnel states if necessary.
2578 * @param connection Connection to create.
2581 GMC_send_create (struct MeshConnection *connection)
2583 enum MeshTunnel3State state;
2586 size = sizeof (struct GNUNET_MESH_ConnectionCreate);
2587 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
2588 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2589 GMP_queue_add (get_next_hop (connection), NULL,
2590 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2591 size, connection, GNUNET_YES, &message_sent, NULL);
2592 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
2593 connection, connection->pending_messages);
2594 connection->pending_messages++;
2595 state = GMT_get_state (connection->t);
2596 if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
2597 GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
2598 if (MESH_CONNECTION_NEW == connection->state)
2599 connection_change_state (connection, MESH_CONNECTION_SENT);
2604 * Send a message to all peers in this connection that the connection
2605 * is no longer valid.
2607 * If some peer should not receive the message, it should be zero'ed out
2608 * before calling this function.
2610 * @param c The connection whose peers to notify.
2613 GMC_send_destroy (struct MeshConnection *c)
2615 struct GNUNET_MESH_ConnectionDestroy msg;
2617 if (GNUNET_YES == c->destroy)
2620 msg.header.size = htons (sizeof (msg));
2621 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY);;
2623 LOG (GNUNET_ERROR_TYPE_DEBUG,
2624 " sending connection destroy for connection %s\n",
2627 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2628 GMC_send_prebuilt_message (&msg.header, c, GNUNET_YES, NULL, NULL);
2629 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2630 GMC_send_prebuilt_message (&msg.header, c, GNUNET_NO, NULL, NULL);
2631 c->destroy = GNUNET_YES;
2636 * @brief Start a polling timer for the connection.
2638 * When a neighbor does not accept more traffic on the connection it could be
2639 * caused by a simple congestion or by a lost ACK. Polling enables to check
2640 * for the lastest ACK status for a connection.
2642 * @param c Connection.
2643 * @param fwd Should we poll in the FWD direction?
2646 GMC_start_poll (struct MeshConnection *c, int fwd)
2648 struct MeshFlowControl *fc;
2650 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2651 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task && NULL != fc->poll_msg)
2655 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2662 * @brief Stop polling a connection for ACKs.
2664 * Once we have enough ACKs for future traffic, polls are no longer necessary.
2666 * @param c Connection.
2667 * @param fwd Should we stop the poll in the FWD direction?
2670 GMC_stop_poll (struct MeshConnection *c, int fwd)
2672 struct MeshFlowControl *fc;
2674 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2675 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2677 GNUNET_SCHEDULER_cancel (fc->poll_task);
2678 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2683 * Get a (static) string for a connection.
2685 * @param c Connection.
2688 GMC_2s (struct MeshConnection *c)
2692 static char buf[128];
2694 sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
2697 return GNUNET_h2s (&c->id);