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_core_service.h"
32 #include "gnunet-service-mesh_connection.h"
33 #include "gnunet-service-mesh_peer.h"
34 #include "mesh_protocol_enc.h"
35 #include "mesh_path.h"
38 #define MESH_DEBUG_CONNECTION GNUNET_NO
39 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
40 GNUNET_TIME_UNIT_MINUTES,\
42 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
43 #define MESH_RETRANSMIT_MARGIN 4
46 #if MESH_DEBUG_CONNECTION
47 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
49 #define DEBUG_CONN(...)
54 * All the states a connection can be in.
56 enum MeshConnectionState
59 * Uninitialized status, should never appear in operation.
64 * Connection create message sent, waiting for ACK.
69 * Connection ACK sent, waiting for ACK.
74 * Connection confirmed, ready to carry traffic.
76 MESH_CONNECTION_READY,
82 * Struct containing info about a queued transmission to this peer
89 struct MeshPeerQueue *next;
94 struct MeshPeerQueue *prev;
97 * Peer this transmission is directed to.
99 struct MeshPeer *peer;
102 * Connection this message belongs to.
104 struct MeshConnection *c;
112 * Channel this message belongs to, if known.
114 struct MeshChannel *ch;
117 * Pointer to info stucture used as cls.
127 * Size of the message
134 * Struct to encapsulate all the Flow Control information to a peer to which
135 * we are directly connected (on a core level).
137 struct MeshFlowControl
140 * Connection this controls.
142 struct MeshConnection *c;
145 * How many messages are in the queue on this connection.
147 unsigned int queue_n;
150 * How many messages do we accept in the queue.
152 unsigned int queue_max;
160 * ID of the last packet sent towards the peer.
162 uint32_t last_pid_sent;
165 * ID of the last packet received from the peer.
167 uint32_t last_pid_recv;
170 * Last ACK sent to the peer (peer can't send more than this PID).
172 uint32_t last_ack_sent;
175 * Last ACK sent towards the origin (for traffic towards leaf node).
177 uint32_t last_ack_recv;
180 * Task to poll the peer in case of a lost ACK causes stall.
182 GNUNET_SCHEDULER_TaskIdentifier poll_task;
185 * How frequently to poll for ACKs.
187 struct GNUNET_TIME_Relative poll_time;
192 * Struct containing all information regarding a connection to a peer.
194 struct MeshConnection
199 struct MeshConnection *next;
200 struct MeshConnection *prev;
203 * Tunnel this connection is part of.
205 struct MeshTunnel2 *t;
208 * Flow control information for traffic fwd.
210 struct MeshFlowControl fwd_fc;
213 * Flow control information for traffic bck.
215 struct MeshFlowControl bck_fc;
218 * ID of the connection.
220 struct GNUNET_HashCode id;
223 * State of the connection.
225 enum MeshConnectionState state;
228 * Path being used for the tunnel.
230 struct MeshPeerPath *path;
233 * Position of the local peer in the path.
235 unsigned int own_pos;
238 * Task to keep the used paths alive at the owner,
239 * time tunnel out on all the other peers.
241 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
244 * Task to keep the used paths alive at the destination,
245 * time tunnel out on all the other peers.
247 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
250 * Pending message count.
252 int pending_messages;
255 * Destroy flag: if true, destroy on last message.
262 * Connections known, indexed by cid (MeshConnection).
264 static struct GNUNET_CONTAINER_MultiHashMap *connections;
267 * How many connections are we willing to maintain.
268 * Local connections are always allowed, even if there are more connections than max.
270 static unsigned long long max_connections;
273 * How many messages *in total* are we willing to queue, divide by number of
274 * connections to get connection queue size.
276 static unsigned long long max_msgs_queue;
279 * How often to send path keepalives. Paths timeout after 4 missed.
281 static struct GNUNET_TIME_Relative refresh_connection_time;
284 * Handle to communicate with core.
286 static struct GNUNET_CORE_Handle *core_handle;
290 * Initialize a Flow Control structure to the initial state.
292 * @param fc Flow Control structure to initialize.
295 fc_init (struct MeshFlowControl *fc)
298 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
299 fc->last_pid_recv = (uint32_t) -1;
300 fc->last_ack_sent = (uint32_t) 0;
301 fc->last_ack_recv = (uint32_t) 0;
302 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
303 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
305 fc->queue_max = (max_msgs_queue / max_connections) + 1;
312 * @param cid Connection ID.
314 static struct MeshConnection *
315 connection_get (const struct GNUNET_HashCode *cid)
317 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
322 * Get first sendable message.
324 * @param peer The destination peer.
326 * @return Best current known path towards the peer, if any.
328 static struct MeshPeerQueue *
329 peer_get_first_message (const struct MeshPeer *peer)
331 struct MeshPeerQueue *q;
333 for (q = peer->queue_head; NULL != q; q = q->next)
335 if (queue_is_sendable (q))
344 queue_is_sendable (struct MeshPeerQueue *q)
346 struct MeshFlowControl *fc;
348 /* Is PID-independent? */
351 case GNUNET_MESSAGE_TYPE_MESH_ACK:
352 case GNUNET_MESSAGE_TYPE_MESH_POLL:
356 /* Is PID allowed? */
357 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
358 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
367 * Free a transmission that was already queued with all resources
368 * associated to the request.
370 * @param queue Queue handler to cancel.
371 * @param clear_cls Is it necessary to free associated cls?
374 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
376 struct MeshPeer *peer;
377 struct MeshFlowControl *fc;
382 GNUNET_assert (NULL != queue->c);
383 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
385 if (GNUNET_YES == clear_cls)
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
388 GNUNET_MESH_DEBUG_M2S (queue->type));
391 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
392 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
393 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
394 GNUNET_break (GNUNET_YES == queue->c->destroy);
396 case GNUNET_MESSAGE_TYPE_MESH_FWD:
397 case GNUNET_MESSAGE_TYPE_MESH_BCK:
398 case GNUNET_MESSAGE_TYPE_MESH_ACK:
399 case GNUNET_MESSAGE_TYPE_MESH_POLL:
400 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
401 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
402 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
404 GNUNET_free_non_null (queue->cls);
409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
410 GNUNET_MESH_DEBUG_M2S (queue->type));
414 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
416 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
417 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u\n", fc, fc->queue_n);
423 if (NULL != queue->c)
425 queue->c->pending_messages--;
426 if (NULL != queue->c->t)
428 queue->c->t->pending_messages--;
438 queue_send (void *cls, size_t size, void *buf)
440 struct MeshPeer *peer = cls;
441 struct MeshFlowControl *fc;
442 struct MeshConnection *c;
443 struct GNUNET_MessageHeader *msg;
444 struct MeshPeerQueue *queue;
445 struct MeshTunnel2 *t;
446 struct MeshChannel *ch;
447 const struct GNUNET_PeerIdentity *dst_id;
453 peer->core_transmit = NULL;
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
456 if (NULL == buf || 0 == size)
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
463 queue = peer_get_first_message (peer);
466 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
471 fc = fwd ? &c->fwd_fc : &c->bck_fc;
473 dst_id = GNUNET_PEER_resolve2 (peer->id);
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
475 /* Check if buffer size is enough for the message */
476 if (queue->size > size)
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
479 peer->core_transmit =
480 GNUNET_CORE_notify_transmit_ready (core_handle,
483 GNUNET_TIME_UNIT_FOREVER_REL,
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
492 t = (NULL != c) ? c->t : NULL;
498 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
499 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
500 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
501 case GNUNET_MESSAGE_TYPE_MESH_FWD:
502 case GNUNET_MESSAGE_TYPE_MESH_BCK:
503 case GNUNET_MESSAGE_TYPE_MESH_ACK:
504 case GNUNET_MESSAGE_TYPE_MESH_POLL:
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 GNUNET_MESH_DEBUG_M2S (queue->type));
508 data_size = send_core_data_raw (queue->cls, size, buf);
509 msg = (struct GNUNET_MessageHeader *) buf;
510 type = ntohs (msg->type);
512 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
514 if (GMC_is_origin (c, GNUNET_YES))
515 data_size = send_core_connection_create (queue->c, size, buf);
517 data_size = send_core_data_raw (queue->cls, size, buf);
519 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
521 if (GMC_is_origin (c, GNUNET_NO) ||
522 GMC_is_origin (c, GNUNET_YES))
523 data_size = send_core_connection_ack (queue->c, size, buf);
525 data_size = send_core_data_raw (queue->cls, size, buf);
527 case GNUNET_MESSAGE_TYPE_MESH_DATA:
528 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
529 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
530 /* This should be encapsulted */
536 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
541 if (0 < drop_percent &&
542 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
544 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
545 "Dropping message of type %s\n",
546 GNUNET_MESH_DEBUG_M2S (queue->type));
550 /* Free queue, but cls was freed by send_core_* */
552 queue_destroy (queue, GNUNET_NO);
554 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
557 case GNUNET_MESSAGE_TYPE_MESH_FWD:
558 case GNUNET_MESSAGE_TYPE_MESH_BCK:
559 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* accounting pid %u\n", pid);
561 fc->last_pid_sent = pid;
562 send_ack (c, ch, fwd);
568 /* If more data in queue, send next */
569 queue = peer_get_first_message (peer);
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
573 if (NULL == peer->core_transmit) {
574 peer->core_transmit =
575 GNUNET_CORE_notify_transmit_ready(core_handle,
578 GNUNET_TIME_UNIT_FOREVER_REL,
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "* tmt rdy called somewhere else\n");
589 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
593 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
598 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
600 GNUNET_SCHEDULER_cancel (fc->poll_task);
601 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
606 c->pending_messages--;
607 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
616 t->pending_messages--;
617 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
619 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
630 queue_add (void *cls, uint16_t type, size_t size,
631 struct MeshConnection *c,
632 struct MeshChannel *ch,
635 struct MeshPeerQueue *queue;
636 struct MeshFlowControl *fc;
637 struct MeshPeer *peer;
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "queue add %s %s (%u) on c %p, ch %p\n",
643 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
644 GNUNET_assert (NULL != c);
646 fc = fwd ? &c->fwd_fc : &c->bck_fc;
647 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
655 if (NULL == peer->connections)
657 /* We are not connected to this peer, ignore request. */
664 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
665 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
672 if (fc->queue_n >= fc->queue_max && 0 == priority)
674 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 "queue full: %u/%u\n",
679 fc->queue_n, fc->queue_max);
680 return; /* Drop this message */
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
685 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
687 call_core = GNUNET_NO;
688 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task &&
689 GNUNET_MESSAGE_TYPE_MESH_POLL != type)
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692 "no buffer space (%u > %u): starting poll\n",
693 fc->last_pid_sent + 1, fc->last_ack_recv);
694 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
700 call_core = GNUNET_YES;
701 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
711 struct MeshPeerQueue *copy;
712 struct MeshPeerQueue *next;
714 for (copy = peer->queue_head; NULL != copy; copy = next)
717 if (copy->type == type && copy->c == c && copy->fwd == fwd)
719 /* Example: also a FWD ACK for connection XYZ */
720 queue_destroy (copy, GNUNET_YES);
723 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
727 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
733 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "calling core tmt rdy towards %s for %u bytes\n",
737 peer2s (peer), size);
738 peer->core_transmit =
739 GNUNET_CORE_notify_transmit_ready (core_handle,
742 GNUNET_TIME_UNIT_FOREVER_REL,
743 GNUNET_PEER_resolve2 (peer->id),
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751 "core tmt rdy towards %s already called\n",
755 c->pending_messages++;
757 c->t->pending_messages++;
764 * Sends an already built message on a connection, properly registering
765 * all used resources.
767 * @param message Message to send. Function makes a copy of it.
768 * If message is not hop-by-hop, decrements TTL of copy.
769 * @param c Connection on which this message is transmitted.
770 * @param ch Channel on which this message is transmitted, or NULL.
771 * @param fwd Is this a fwd message?
774 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
775 struct MeshConnection *c,
776 struct MeshChannel *ch,
783 size = ntohs (message->size);
784 data = GNUNET_malloc (size);
785 memcpy (data, message, size);
786 type = ntohs (message->type);
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
788 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
792 struct GNUNET_MESH_Encrypted *emsg;
793 struct GNUNET_MESH_ACK *amsg;
794 struct GNUNET_MESH_Poll *pmsg;
795 struct GNUNET_MESH_ConnectionDestroy *dmsg;
796 struct GNUNET_MESH_ConnectionBroken *bmsg;
799 case GNUNET_MESSAGE_TYPE_MESH_FWD:
800 case GNUNET_MESSAGE_TYPE_MESH_BCK:
801 emsg = (struct GNUNET_MESH_Encrypted *) data;
802 ttl = ntohl (emsg->ttl);
809 emsg->ttl = htonl (ttl - 1);
810 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
814 case GNUNET_MESSAGE_TYPE_MESH_ACK:
815 amsg = (struct GNUNET_MESH_ACK *) data;
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
820 case GNUNET_MESSAGE_TYPE_MESH_POLL:
821 pmsg = (struct GNUNET_MESH_Poll *) data;
823 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
827 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
828 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
833 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
834 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
839 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
840 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
858 struct MeshConnection *
859 GMC_new (const struct GNUNET_HashCode *cid)
861 struct MeshConnection *c;
863 c = GNUNET_new (struct MeshConnection);
865 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
866 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
867 fc_init (&c->fwd_fc);
868 fc_init (&c->bck_fc);
877 GMC_destroy (struct MeshConnection *c)
879 struct MeshPeer *peer;
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
888 /* Cancel all traffic */
889 connection_cancel_queues (c, GNUNET_YES);
890 connection_cancel_queues (c, GNUNET_NO);
892 /* Cancel maintainance task (keepalive/timeout) */
893 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
894 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
895 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
896 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
898 /* Deregister from neighbors */
899 peer = connection_get_next_hop (c);
900 if (NULL != peer && NULL != peer->connections)
901 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
902 peer = connection_get_prev_hop (c);
903 if (NULL != peer && NULL != peer->connections)
904 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
907 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
908 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
915 * Send an ACK informing the predecessor about the available buffer space.
917 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
918 * the ACK itself goes "back" (dest->root).
920 * @param c Connection on which to send the ACK.
921 * @param buffer How much space free to advertise?
922 * @param fwd Is this FWD ACK? (Going dest->owner)
925 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
927 struct MeshFlowControl *next_fc;
928 struct MeshFlowControl *prev_fc;
929 struct GNUNET_MESH_ACK msg;
933 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
934 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937 "connection send %s ack on %s\n",
938 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
940 /* Check if we need to transmit the ACK */
941 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 " last pid recv: %u, last ack sent: %u\n",
946 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
950 /* Ok, ACK might be necessary, what PID to ACK? */
951 delta = next_fc->queue_max - next_fc->queue_n;
952 ack = prev_fc->last_pid_recv + delta;
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 " last pid %u, last ack %u, qmax %u, q %u\n",
956 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
957 next_fc->queue_max, next_fc->queue_n);
958 if (ack == prev_fc->last_ack_sent)
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
964 prev_fc->last_ack_sent = ack;
966 /* Build ACK message and send on connection */
967 msg.header.size = htons (sizeof (msg));
968 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
969 msg.ack = htonl (ack);
972 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
977 connection_change_state (struct MeshConnection* c,
978 enum MeshConnectionState state)
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Connection %s state was %s\n",
982 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
984 "Connection %s state is now %s\n",
985 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
992 * Send keepalive packets for a connection.
994 * @param c Connection to keep alive..
995 * @param fwd Is this a FWD keepalive? (owner -> dest).
998 connection_keepalive (struct MeshConnection *c, int fwd)
1000 struct GNUNET_MESH_ConnectionKeepAlive *msg;
1001 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
1005 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
1006 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1009 "sending %s keepalive for connection %s[%d]\n",
1010 fwd ? "FWD" : "BCK",
1011 peer2s (c->t->peer),
1014 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
1015 msg->header.size = htons (size);
1016 msg->header.type = htons (type);
1019 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1024 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1026 * @param c Connection for which to send the message.
1027 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
1030 connection_recreate (struct MeshConnection *c, int fwd)
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
1034 send_connection_create (c);
1036 send_connection_ack (c, GNUNET_NO);
1041 * Generic connection timer management.
1042 * Depending on the role of the peer in the connection will send the
1043 * appropriate message (build or keepalive)
1045 * @param c Conncetion to maintain.
1046 * @param fwd Is FWD?
1049 connection_maintain (struct MeshConnection *c, int fwd)
1051 if (MESH_TUNNEL_SEARCHING == c->t->state)
1053 /* TODO DHT GET with RO_BART */
1058 case MESH_CONNECTION_NEW:
1060 case MESH_CONNECTION_SENT:
1061 connection_recreate (c, fwd);
1063 case MESH_CONNECTION_READY:
1064 connection_keepalive (c, fwd);
1073 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1075 struct MeshConnection *c = cls;
1077 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1078 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1081 connection_maintain (c, GNUNET_YES);
1082 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1083 &connection_fwd_keepalive,
1089 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1091 struct MeshConnection *c = cls;
1093 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1094 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1097 connection_maintain (c, GNUNET_NO);
1098 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1099 &connection_bck_keepalive,
1105 * Send a message to all peers in this connection that the connection
1106 * is no longer valid.
1108 * If some peer should not receive the message, it should be zero'ed out
1109 * before calling this function.
1111 * @param c The connection whose peers to notify.
1114 connection_send_destroy (struct MeshConnection *c)
1116 struct GNUNET_MESH_ConnectionDestroy msg;
1118 msg.header.size = htons (sizeof (msg));
1119 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 " sending connection destroy for connection %s[%X]\n",
1123 peer2s (c->t->peer),
1126 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
1127 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
1128 if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
1129 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
1130 c->destroy = GNUNET_YES;
1135 * Get free buffer space in a connection.
1137 * @param c Connection.
1138 * @param fwd Is query about FWD traffic?
1140 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1143 connection_get_buffer (struct MeshConnection *c, int fwd)
1145 struct MeshFlowControl *fc;
1147 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1149 return (fc->queue_max - fc->queue_n);
1154 * Get the first transmittable message for a connection.
1156 * @param c Connection.
1157 * @param fwd Is this FWD?
1159 * @return First transmittable message.
1161 static struct MeshPeerQueue *
1162 connection_get_first_message (struct MeshConnection *c, int fwd)
1164 struct MeshPeerQueue *q;
1167 p = connection_get_hop (c, fwd);
1169 for (q = p->queue_head; NULL != q; q = q->next)
1173 if (queue_is_sendable (q))
1182 * @brief Re-initiate traffic on this connection if necessary.
1184 * Check if there is traffic queued towards this peer
1185 * and the core transmit handle is NULL (traffic was stalled).
1186 * If so, call core tmt rdy.
1188 * @param c Connection on which initiate traffic.
1189 * @param fwd Is this about fwd traffic?
1192 connection_unlock_queue (struct MeshConnection *c, int fwd)
1194 struct MeshPeer *peer;
1195 struct MeshPeerQueue *q;
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "connection_unlock_queue %s on %s\n",
1200 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
1202 if (GMC_is_terminal (c, fwd))
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1208 peer = connection_get_hop (c, fwd);
1210 if (NULL != peer->core_transmit)
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1213 return; /* Already unlocked */
1216 q = connection_get_first_message (c, fwd);
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1220 return; /* Nothing to transmit */
1224 peer->core_transmit =
1225 GNUNET_CORE_notify_transmit_ready (core_handle,
1228 GNUNET_TIME_UNIT_FOREVER_REL,
1229 GNUNET_PEER_resolve2 (peer->id),
1237 * Cancel all transmissions that belong to a certain connection.
1239 * @param c Connection which to cancel.
1240 * @param fwd Cancel fwd traffic?
1243 connection_cancel_queues (struct MeshConnection *c, int fwd)
1245 struct MeshPeerQueue *q;
1246 struct MeshPeerQueue *next;
1247 struct MeshFlowControl *fc;
1248 struct MeshPeer *peer;
1255 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1256 peer = connection_get_hop (c, fwd);
1258 for (q = peer->queue_head; NULL != q; q = next)
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "connection_cancel_queue %s\n",
1265 GNUNET_MESH_DEBUG_M2S (q->type));
1266 queue_destroy (q, GNUNET_YES);
1269 if (NULL == peer->queue_head)
1271 if (NULL != peer->core_transmit)
1273 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1274 peer->core_transmit = NULL;
1276 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1278 GNUNET_SCHEDULER_cancel (fc->poll_task);
1279 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1288 * Function called if a connection has been stalled for a while,
1289 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1291 * @param cls Closure (poll ctx).
1292 * @param tc TaskContext.
1295 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1297 struct MeshFlowControl *fc = cls;
1298 struct GNUNET_MESH_Poll msg;
1299 struct MeshConnection *c;
1301 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1302 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
1310 GNUNET_h2s (&c->id));
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
1312 fc == &c->fwd_fc ? "FWD" : "BCK");
1314 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1315 msg.header.size = htons (sizeof (msg));
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
1317 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
1318 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1319 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1320 &connection_poll, fc);
1327 * Get the previous hop in a connection
1329 * @param c Connection.
1331 * @return Previous peer in the connection.
1333 static struct MeshPeer *
1334 connection_get_prev_hop (struct MeshConnection *c)
1338 if (0 == c->own_pos || c->path->length < 2)
1339 id = c->path->peers[0];
1341 id = c->path->peers[c->own_pos - 1];
1343 return peer_get_short (id);
1348 * Get the next hop in a connection
1350 * @param c Connection.
1352 * @return Next peer in the connection.
1354 static struct MeshPeer *
1355 connection_get_next_hop (struct MeshConnection *c)
1359 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1360 id = c->path->peers[c->path->length - 1];
1362 id = c->path->peers[c->own_pos + 1];
1364 return peer_get_short (id);
1369 * Get the hop in a connection.
1371 * @param c Connection.
1372 * @param fwd Next hop?
1374 * @return Next peer in the connection.
1376 static struct MeshPeer *
1377 connection_get_hop (struct MeshConnection *c, int fwd)
1380 return connection_get_next_hop (c);
1381 return connection_get_prev_hop (c);
1388 * Timeout function due to lack of keepalive/traffic from the owner.
1389 * Destroys connection if called.
1391 * @param cls Closure (connection to destroy).
1392 * @param tc TaskContext.
1395 connection_fwd_timeout (void *cls,
1396 const struct GNUNET_SCHEDULER_TaskContext *tc)
1398 struct MeshConnection *c = cls;
1400 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1401 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1404 "Connection %s[%X] FWD timed out. Destroying.\n",
1405 peer2s (c->t->peer),
1408 if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1416 * Timeout function due to lack of keepalive/traffic from the destination.
1417 * Destroys connection if called.
1419 * @param cls Closure (connection to destroy).
1420 * @param tc TaskContext
1423 connection_bck_timeout (void *cls,
1424 const struct GNUNET_SCHEDULER_TaskContext *tc)
1426 struct MeshConnection *c = cls;
1428 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1429 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1433 "Connection %s[%X] FWD timed out. Destroying.\n",
1434 peer2s (c->t->peer),
1437 if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1445 * Resets the connection timeout task, some other message has done the
1447 * - For the first peer on the direction this means to send
1448 * a keepalive or a path confirmation message (either create or ACK).
1449 * - For all other peers, this means to destroy the connection,
1450 * due to lack of activity.
1451 * Starts the tiemout if no timeout was running (connection just created).
1453 * @param c Connection whose timeout to reset.
1454 * @param fwd Is this forward?
1456 * TODO use heap to improve efficiency of scheduler.
1459 connection_reset_timeout (struct MeshConnection *c, int fwd)
1461 GNUNET_SCHEDULER_TaskIdentifier *ti;
1462 GNUNET_SCHEDULER_Task f;
1464 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1466 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1467 GNUNET_SCHEDULER_cancel (*ti);
1469 if (GMC_is_origin (c, fwd)) /* Endpoint */
1471 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
1472 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
1476 struct GNUNET_TIME_Relative delay;
1478 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1479 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1480 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1487 * Core handler for connection creation.
1489 * @param cls Closure (unused).
1490 * @param peer Sender (neighbor).
1491 * @param message Message.
1493 * @return GNUNET_OK to keep the connection open,
1494 * GNUNET_SYSERR to close it (signal serious error)
1497 handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1498 const struct GNUNET_MessageHeader *message)
1500 struct GNUNET_MESH_ConnectionCreate *msg;
1501 struct GNUNET_PeerIdentity *id;
1502 struct GNUNET_HashCode *cid;
1503 struct MeshPeerPath *path;
1504 struct MeshPeer *dest_peer;
1505 struct MeshPeer *orig_peer;
1506 struct MeshConnection *c;
1507 unsigned int own_pos;
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1515 size = ntohs (message->size);
1516 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1518 GNUNET_break_op (0);
1522 /* Calculate hops */
1523 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1524 if (size % sizeof (struct GNUNET_PeerIdentity))
1526 GNUNET_break_op (0);
1529 size /= sizeof (struct GNUNET_PeerIdentity);
1532 GNUNET_break_op (0);
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1537 /* Get parameters */
1538 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1540 id = (struct GNUNET_PeerIdentity *) &msg[1];
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 " connection %s (%s).\n",
1543 GNUNET_h2s (cid), GNUNET_i2s (id));
1545 /* Create connection */
1546 c = connection_get (cid);
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1550 c = connection_new (cid);
1553 connection_reset_timeout (c, GNUNET_YES);
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1557 path = path_new (size);
1559 for (i = 0; i < size; i++)
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
1562 GNUNET_i2s (&id[i]));
1563 path->peers[i] = GNUNET_PEER_intern (&id[i]);
1564 if (path->peers[i] == myid)
1567 if (own_pos == 0 && path->peers[own_pos] != myid)
1569 /* create path: self not found in path through self */
1570 GNUNET_break_op (0);
1571 path_destroy (path);
1572 connection_destroy (c);
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1576 path_add_to_peers (path, GNUNET_NO);
1577 c->path = path_duplicate (path);
1578 c->own_pos = own_pos;
1584 if (MESH_CONNECTION_NEW == c->state)
1585 connection_change_state (c, MESH_CONNECTION_SENT);
1587 /* Remember peers */
1588 dest_peer = peer_get (&id[size - 1]);
1589 orig_peer = peer_get (&id[0]);
1591 /* Is it a connection to us? */
1592 if (c->own_pos == size - 1)
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1595 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
1597 if (NULL == orig_peer->tunnel)
1599 orig_peer->tunnel = tunnel_new ();
1600 orig_peer->tunnel->peer = orig_peer;
1602 tunnel_add_connection (orig_peer->tunnel, c);
1603 if (MESH_TUNNEL_NEW == c->t->state)
1604 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
1606 send_connection_ack (c, GNUNET_NO);
1607 if (MESH_CONNECTION_SENT == c->state)
1608 connection_change_state (c, MESH_CONNECTION_ACK);
1610 /* Keep tunnel alive in direction dest->owner*/
1611 connection_reset_timeout (c, GNUNET_NO);
1615 /* It's for somebody else! Retransmit. */
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1617 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1618 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
1619 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
1626 * Core handler for path confirmations.
1628 * @param cls closure
1629 * @param message message
1630 * @param peer peer identity this notification is about
1632 * @return GNUNET_OK to keep the connection open,
1633 * GNUNET_SYSERR to close it (signal serious error)
1636 handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1637 const struct GNUNET_MessageHeader *message)
1639 struct GNUNET_MESH_ConnectionACK *msg;
1640 struct MeshConnection *c;
1641 struct MeshPeerPath *p;
1642 struct MeshPeer *pi;
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1647 msg = (struct GNUNET_MESH_ConnectionACK *) message;
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
1649 GNUNET_h2s (&msg->cid));
1650 c = connection_get (&msg->cid);
1653 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
1662 pi = peer_get (peer);
1663 if (connection_get_next_hop (c) == pi)
1665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1667 if (MESH_CONNECTION_SENT == c->state)
1668 connection_change_state (c, MESH_CONNECTION_ACK);
1670 else if (connection_get_prev_hop (c) == pi)
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1674 connection_change_state (c, MESH_CONNECTION_READY);
1678 GNUNET_break_op (0);
1681 connection_reset_timeout (c, fwd);
1683 /* Add path to peers? */
1687 path_add_to_peers (p, GNUNET_YES);
1694 /* Message for us as creator? */
1695 if (connection_is_origin (c, GNUNET_YES))
1697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1698 connection_change_state (c, MESH_CONNECTION_READY);
1699 if (MESH_TUNNEL_READY != c->t->state)
1700 tunnel_change_state (c->t, MESH_TUNNEL_READY);
1701 send_connection_ack (c, GNUNET_YES);
1702 tunnel_send_queued_data (c->t, GNUNET_YES);
1703 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
1705 GNUNET_DHT_get_stop (c->t->peer->dhtget);
1706 c->t->peer->dhtget = NULL;
1711 /* Message for us as destination? */
1712 if (GMC_is_terminal (c, GNUNET_YES))
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1715 if (MESH_TUNNEL_READY != c->t->state)
1716 tunnel_change_state (c->t, MESH_TUNNEL_READY);
1717 connection_change_state (c, MESH_CONNECTION_READY);
1718 tunnel_send_queued_data (c->t, GNUNET_NO);
1722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1723 send_prebuilt_message_connection (message, c, NULL, fwd);
1729 * Core handler for notifications of broken paths
1731 * @param cls Closure (unused).
1732 * @param peer Peer identity of sending neighbor.
1733 * @param message Message.
1735 * @return GNUNET_OK to keep the connection open,
1736 * GNUNET_SYSERR to close it (signal serious error)
1739 handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
1740 const struct GNUNET_MessageHeader *message)
1742 struct GNUNET_MESH_ConnectionBroken *msg;
1743 struct MeshConnection *c;
1745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1746 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
1747 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1749 GNUNET_i2s (&msg->peer1));
1750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1751 GNUNET_i2s (&msg->peer2));
1752 c = connection_get (&msg->cid);
1755 GNUNET_break_op (0);
1758 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
1759 GNUNET_PEER_search (&msg->peer2));
1766 * Core handler for tunnel destruction
1768 * @param cls Closure (unused).
1769 * @param peer Peer identity of sending neighbor.
1770 * @param message Message.
1772 * @return GNUNET_OK to keep the connection open,
1773 * GNUNET_SYSERR to close it (signal serious error)
1776 handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1777 const struct GNUNET_MessageHeader *message)
1779 struct GNUNET_MESH_ConnectionDestroy *msg;
1780 struct MeshConnection *c;
1784 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 "Got a CONNECTION DESTROY message from %s\n",
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 " for connection %s\n",
1790 GNUNET_h2s (&msg->cid));
1791 c = connection_get (&msg->cid);
1794 /* Probably already got the message from another path,
1795 * destroyed the tunnel and retransmitted to children.
1798 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1802 id = GNUNET_PEER_search (peer);
1803 if (id == connection_get_prev_hop (c)->id)
1805 else if (id == connection_get_next_hop (c)->id)
1809 GNUNET_break_op (0);
1812 send_prebuilt_message_connection (message, c, NULL, fwd);
1813 c->destroy = GNUNET_YES;
1819 * Generic handler for mesh network encrypted traffic.
1821 * @param peer Peer identity this notification is about.
1822 * @param message Encrypted message.
1823 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1825 * @return GNUNET_OK to keep the connection open,
1826 * GNUNET_SYSERR to close it (signal serious error)
1829 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1830 const struct GNUNET_MESH_Encrypted *msg,
1833 struct MeshConnection *c;
1834 struct MeshTunnel2 *t;
1835 struct MeshPeer *neighbor;
1836 struct MeshFlowControl *fc;
1843 size = ntohs (msg->header.size);
1845 sizeof (struct GNUNET_MESH_Encrypted) +
1846 sizeof (struct GNUNET_MessageHeader))
1848 GNUNET_break_op (0);
1851 type = ntohs (msg->header.type);
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1854 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1856 /* Check connection */
1857 c = connection_get (&msg->cid);
1860 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1865 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1867 /* Check if origin is as expected */
1868 neighbor = connection_get_hop (c, !fwd);
1869 if (peer_get (peer)->id != neighbor->id)
1871 GNUNET_break_op (0);
1876 pid = ntohl (msg->pid);
1877 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1879 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1881 "WARNING Received PID %u, (prev %u), ACK %u\n",
1882 pid, fc->last_pid_recv, fc->last_ack_sent);
1885 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1887 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889 " Pid %u not expected (%u+), dropping!\n",
1890 pid, fc->last_pid_recv + 1);
1893 if (MESH_CONNECTION_SENT == c->state)
1894 connection_change_state (c, MESH_CONNECTION_READY);
1895 connection_reset_timeout (c, fwd);
1896 fc->last_pid_recv = pid;
1898 /* Is this message for us? */
1899 if (GMC_is_terminal (c, fwd))
1901 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
1903 struct GNUNET_MessageHeader *msgh;
1906 /* TODO signature verification */
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
1908 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1910 fc->last_pid_recv = pid;
1911 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
1915 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1916 handle_decrypted (t, msgh, fwd);
1917 off += ntohs (msgh->size);
1919 send_ack (c, NULL, fwd);
1923 /* Message not for us: forward to next hop */
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1925 ttl = ntohl (msg->ttl);
1926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
1929 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1930 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1931 send_ack (c, NULL, fwd);
1934 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1936 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1943 * Core handler for mesh network traffic going orig->dest.
1945 * @param cls Closure (unused).
1946 * @param message Message received.
1947 * @param peer Peer who sent the message.
1949 * @return GNUNET_OK to keep the connection open,
1950 * GNUNET_SYSERR to close it (signal serious error)
1953 handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1954 const struct GNUNET_MessageHeader *message)
1956 return handle_mesh_encrypted (peer,
1957 (struct GNUNET_MESH_Encrypted *)message,
1962 * Core handler for mesh network traffic going dest->orig.
1964 * @param cls Closure (unused).
1965 * @param message Message received.
1966 * @param peer Peer who sent the message.
1968 * @return GNUNET_OK to keep the connection open,
1969 * GNUNET_SYSERR to close it (signal serious error)
1972 handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1973 const struct GNUNET_MessageHeader *message)
1975 return handle_mesh_encrypted (peer,
1976 (struct GNUNET_MESH_Encrypted *)message,
1982 * Core handler for mesh network traffic point-to-point acks.
1984 * @param cls closure
1985 * @param message message
1986 * @param peer peer identity this notification is about
1988 * @return GNUNET_OK to keep the connection open,
1989 * GNUNET_SYSERR to close it (signal serious error)
1992 handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1993 const struct GNUNET_MessageHeader *message)
1995 struct GNUNET_MESH_ACK *msg;
1996 struct MeshConnection *c;
1997 struct MeshFlowControl *fc;
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
2005 msg = (struct GNUNET_MESH_ACK *) message;
2007 c = connection_get (&msg->cid);
2011 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2016 /* Is this a forward or backward ACK? */
2017 id = GNUNET_PEER_search (peer);
2018 if (connection_get_next_hop (c)->id == id)
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2024 else if (connection_get_prev_hop (c)->id == id)
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2032 GNUNET_break_op (0);
2036 ack = ntohl (msg->ack);
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2038 ack, fc->last_ack_recv);
2039 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
2040 fc->last_ack_recv = ack;
2042 /* Cancel polling if the ACK is big enough. */
2043 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2044 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2047 GNUNET_SCHEDULER_cancel (fc->poll_task);
2048 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2049 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2052 connection_unlock_queue (c, fwd);
2059 * Core handler for mesh network traffic point-to-point ack polls.
2061 * @param cls closure
2062 * @param message message
2063 * @param peer peer identity this notification is about
2065 * @return GNUNET_OK to keep the connection open,
2066 * GNUNET_SYSERR to close it (signal serious error)
2069 handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2070 const struct GNUNET_MessageHeader *message)
2072 struct GNUNET_MESH_Poll *msg;
2073 struct MeshConnection *c;
2074 struct MeshFlowControl *fc;
2079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
2083 msg = (struct GNUNET_MESH_Poll *) message;
2085 c = connection_get (&msg->cid);
2089 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2091 GNUNET_break_op (0);
2095 /* Is this a forward or backward ACK?
2096 * Note: a poll should never be needed in a loopback case,
2097 * since there is no possiblility of packet loss there, so
2098 * this way of discerining FWD/BCK should not be a problem.
2100 id = GNUNET_PEER_search (peer);
2101 if (connection_get_next_hop (c)->id == id)
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2106 else if (connection_get_prev_hop (c)->id == id)
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2113 GNUNET_break_op (0);
2117 pid = ntohl (msg->pid);
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
2119 pid, fc->last_pid_recv);
2120 fc->last_pid_recv = pid;
2121 fwd = fc == &c->fwd_fc;
2122 send_ack (c, NULL, fwd);
2129 * Core handler for mesh keepalives.
2131 * @param cls closure
2132 * @param message message
2133 * @param peer peer identity this notification is about
2134 * @return GNUNET_OK to keep the connection open,
2135 * GNUNET_SYSERR to close it (signal serious error)
2137 * TODO: Check who we got this from, to validate route.
2140 handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
2141 const struct GNUNET_MessageHeader *message)
2143 struct GNUNET_MESH_ConnectionKeepAlive *msg;
2144 struct MeshConnection *c;
2145 struct MeshPeer *neighbor;
2148 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
2152 c = connection_get (&msg->cid);
2155 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
2160 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
2161 GNUNET_YES : GNUNET_NO;
2163 /* Check if origin is as expected */
2164 neighbor = connection_get_hop (c, fwd);
2165 if (peer_get (peer)->id != neighbor->id)
2167 GNUNET_break_op (0);
2171 connection_change_state (c, MESH_CONNECTION_READY);
2172 connection_reset_timeout (c, fwd);
2174 if (GMC_is_terminal (c, fwd))
2177 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
2178 send_prebuilt_message_connection (message, c, NULL, fwd);
2185 * Functions to handle messages from core
2187 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2188 {&handle_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2190 {&handle_confirm, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
2191 sizeof (struct GNUNET_MESH_ConnectionACK)},
2192 {&handle_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
2193 sizeof (struct GNUNET_MESH_ConnectionBroken)},
2194 {&handle_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
2195 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
2196 {&handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
2197 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
2198 {&handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
2199 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
2200 {&handle_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
2201 sizeof (struct GNUNET_MESH_ACK)},
2202 {&handle_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
2203 sizeof (struct GNUNET_MESH_Poll)},
2204 {&handle_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
2205 {&handle_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
2212 * Iterator to notify all connections of a broken link. Mark connections
2213 * to destroy after all traffic has been sent.
2215 * @param cls Closure (peer disconnected).
2216 * @param key Current key code (tid).
2217 * @param value Value in the hash map (connection).
2219 * @return GNUNET_YES if we should continue to iterate,
2223 GMC_notify_broken (void *cls,
2224 const struct GNUNET_HashCode *key,
2227 struct MeshPeer *peer = cls;
2228 struct MeshConnection *c = value;
2229 struct GNUNET_MESH_ConnectionBroken msg;
2232 fwd = peer == connection_get_prev_hop (c);
2234 connection_cancel_queues (c, !fwd);
2235 if (GMC_is_terminal (c, fwd))
2237 /* Local shutdown, no one to notify about this. */
2242 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
2243 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
2245 msg.peer1 = my_full_id;
2246 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
2247 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
2248 c->destroy = GNUNET_YES;
2255 * Initialize the connections subsystem
2257 * @param c Configuration handle.
2260 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2263 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2266 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2267 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2268 GNUNET_SCHEDULER_shutdown ();
2273 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2276 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2277 "MESH", "MAX_CONNECTIONS", "MISSING");
2278 GNUNET_SCHEDULER_shutdown ();
2283 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2284 &refresh_connection_time))
2286 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2287 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2288 GNUNET_SCHEDULER_shutdown ();
2291 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2293 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
2294 NULL, /* Closure passed to MESH functions */
2295 &core_init, /* Call core_init once connected */
2296 &core_connect, /* Handle connects */
2297 &core_disconnect, /* remove peers on disconnects */
2298 NULL, /* Don't notify about all incoming messages */
2299 GNUNET_NO, /* For header only in notification */
2300 NULL, /* Don't notify about all outbound messages */
2301 GNUNET_NO, /* For header-only out notification */
2302 core_handlers); /* Register these handlers */
2303 if (NULL == core_handle)
2306 GNUNET_SCHEDULER_shutdown ();
2312 * Shut down the connections subsystem.
2317 if (core_handle != NULL)
2319 GNUNET_CORE_disconnect (core_handle);
2326 * Is this peer the first one on the connection?
2328 * @param c Connection.
2329 * @param fwd Is this about fwd traffic?
2331 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2334 GMC_is_origin (struct MeshConnection *c, int fwd)
2336 if (!fwd && c->path->length - 1 == c->own_pos )
2338 if (fwd && 0 == c->own_pos)
2345 * Is this peer the last one on the connection?
2347 * @param c Connection.
2348 * @param fwd Is this about fwd traffic?
2349 * Note that the ROOT is the terminal for BCK traffic!
2351 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2354 GMC_is_terminal (struct MeshConnection *c, int fwd)
2356 return GMC_is_origin (c, !fwd);