2 This file is part of GNUnet.
3 (C) 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.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_core_service.h"
27 #include "mesh_protocol_enc.h"
29 #include "gnunet-service-mesh_peer.h"
30 #include "gnunet-service-mesh_dht.h"
31 #include "gnunet-service-mesh_connection.h"
32 #include "gnunet-service-mesh_local.h"
33 #include "mesh_path.h"
35 /******************************************************************************/
36 /******************************** STRUCTS **********************************/
37 /******************************************************************************/
40 * Struct containing all information regarding a given peer
50 * Last time we heard from this peer
52 struct GNUNET_TIME_Absolute last_contact;
55 * Paths to reach the peer, ordered by ascending hop count
57 struct MeshPeerPath *path_head;
60 * Paths to reach the peer, ordered by ascending hop count
62 struct MeshPeerPath *path_tail;
65 * Handle to stop the DHT search for paths to this peer
67 struct GMD_search_handle *search_h;
70 * Tunnel to this peer, if any.
72 struct MeshTunnel2 *tunnel;
75 * Connections that go through this peer, indexed by tid;
77 struct GNUNET_CONTAINER_MultiHashMap *connections;
80 * Handle for queued transmissions
82 struct GNUNET_CORE_TransmitHandle *core_transmit;
85 * Transmission queue to core DLL head
87 struct MeshPeerQueue *queue_head;
90 * Transmission queue to core DLL tail
92 struct MeshPeerQueue *queue_tail;
95 * How many messages are in the queue to this peer.
101 /******************************************************************************/
102 /******************************* GLOBALS ***********************************/
103 /******************************************************************************/
106 * Peers known, indexed by PeerIdentity (MeshPeer).
108 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
111 * How many peers do we want to remember?
113 static unsigned long long max_peers;
116 * Percentage of messages that will be dropped (for test purposes only).
118 static unsigned long long drop_percent;
121 * Handle to communicate with core.
123 static struct GNUNET_CORE_Handle *core_handle;
126 * Local peer own ID (full value).
128 const static struct GNUNET_PeerIdentity *my_full_id;
130 /******************************************************************************/
131 /***************************** CORE CALLBACKS *********************************/
132 /******************************************************************************/
136 * Iterator to notify all connections of a broken link. Mark connections
137 * to destroy after all traffic has been sent.
139 * @param cls Closure (peer disconnected).
140 * @param key Current key code (peer id).
141 * @param value Value in the hash map (connection).
143 * @return GNUNET_YES if we should continue to iterate,
147 notify_broken (void *cls,
148 const struct GNUNET_HashCode *key,
151 struct MeshPeer *peer = cls;
152 struct MeshConnection *c = value;
154 GMC_notify_broken (c, peer, my_full_id);
161 * Method called whenever a given peer connects.
164 * @param peer peer identity this notification is about
167 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
170 struct MeshPeerPath *path;
172 LOG ("Peer connected\n");
173 LOG (" %s\n", GNUNET_i2s (&my_full_id));
174 pi = peer_get (peer);
182 LOG (" %s\n", GNUNET_i2s (peer));
184 path->peers[1] = pi->id;
185 GNUNET_PEER_change_rc (pi->id, 1);
186 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
188 path->peers[0] = myid;
189 GNUNET_PEER_change_rc (myid, 1);
190 peer_add_path (pi, path, GNUNET_YES);
192 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
198 * Method called whenever a peer disconnects.
201 * @param peer peer identity this notification is about
204 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
208 LOG ("Peer disconnected\n");
209 pi = GNUNET_CONTAINER_multipeermap_get (peers, peer);
216 GNUNET_CONTAINER_multihashmap_iterate (pi->connections, ¬ify_broken, pi);
217 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
218 pi->connections = NULL;
219 if (NULL != pi->core_transmit)
221 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit);
222 pi->core_transmit = NULL;
228 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
235 * Functions to handle messages from core
237 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
238 {&GMC_handle_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
240 {&GMC_handle_confirm, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
241 sizeof (struct GNUNET_MESH_ConnectionACK)},
242 {&GMC_handle_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
243 sizeof (struct GNUNET_MESH_ConnectionBroken)},
244 {&GMC_handle_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
245 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
246 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
247 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
248 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
249 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
250 {&GMC_handle_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
251 sizeof (struct GNUNET_MESH_ACK)},
252 {&GMC_handle_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
253 sizeof (struct GNUNET_MESH_Poll)},
254 {&GMC_handle_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
255 {&GMC_handle_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
261 * To be called on core init/fail.
263 * @param cls Closure (config)
264 * @param identity the public identity of this peer
267 core_init (void *cls,
268 const struct GNUNET_PeerIdentity *identity)
270 const struct GNUNET_CONFIGURATION_Handle *c = cls;
273 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
274 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
276 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
277 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
278 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
279 GNUNET_CORE_disconnect (core_handle);
280 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
281 NULL, /* Closure passed to MESH functions */
282 &core_init, /* Call core_init once connected */
283 &core_connect, /* Handle connects */
284 &core_disconnect, /* remove peers on disconnects */
285 NULL, /* Don't notify about all incoming messages */
286 GNUNET_NO, /* For header only in notification */
287 NULL, /* Don't notify about all outbound messages */
288 GNUNET_NO, /* For header-only out notification */
289 core_handlers); /* Register these handlers */
298 * Core callback to write a pre-constructed data packet to core buffer
300 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
301 * @param size Number of bytes available in buf.
302 * @param buf Where the to write the message.
304 * @return number of bytes written to buf
307 send_core_data_raw (void *cls, size_t size, void *buf)
309 struct GNUNET_MessageHeader *msg = cls;
312 GNUNET_assert (NULL != msg);
313 total_size = ntohs (msg->size);
315 if (total_size > size)
320 memcpy (buf, msg, total_size);
327 * Function to send a create connection message to a peer.
329 * @param c Connection to create.
330 * @param size number of bytes available in buf
331 * @param buf where the callee should write the message
332 * @return number of bytes written to buf
335 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
337 struct GNUNET_MESH_ConnectionCreate *msg;
338 struct GNUNET_PeerIdentity *peer_ptr;
339 struct MeshPeerPath *p = c->path;
343 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
345 sizeof (struct GNUNET_MESH_ConnectionCreate) +
346 p->length * sizeof (struct GNUNET_PeerIdentity);
348 if (size < size_needed || NULL == buf)
353 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
354 msg->header.size = htons (size_needed);
355 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
358 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
359 for (i = 0; i < p->length; i++)
361 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
364 LOG (GNUNET_ERROR_TYPE_DEBUG,
365 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
371 * Creates a path ack message in buf and frees all unused resources.
373 * @param c Connection to send an ACK on.
374 * @param size number of bytes available in buf
375 * @param buf where the callee should write the message
377 * @return number of bytes written to buf
380 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
382 struct GNUNET_MESH_ConnectionACK *msg = buf;
383 struct MeshTunnel2 *t = c->t;
385 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
386 GNUNET_assert (NULL != t);
387 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
392 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
393 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
397 /* TODO add signature */
399 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
400 return sizeof (struct GNUNET_MESH_ConnectionACK);
404 /******************************************************************************/
405 /******************************** STATIC ***********************************/
406 /******************************************************************************/
409 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
412 * @param key current key code
413 * @param value value in the hash map
414 * @return #GNUNET_YES if we should continue to iterate,
418 shutdown_tunnel (void *cls,
419 const struct GNUNET_PeerIdentity *key,
422 struct MeshPeer *p = value;
423 struct MeshTunnel2 *t = p->tunnel;
433 * Destroy the peer_info and free any allocated resources linked to it
435 * @param peer The peer_info to destroy.
437 * @return GNUNET_OK on success
440 peer_destroy (struct MeshPeer *peer)
442 struct GNUNET_PeerIdentity id;
443 struct MeshPeerPath *p;
444 struct MeshPeerPath *nextp;
446 GNUNET_PEER_resolve (peer->id, &id);
447 GNUNET_PEER_change_rc (peer->id, -1);
450 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
453 LOG (GNUNET_ERROR_TYPE_WARNING,
454 "removing peer %s, not in peermap\n", GNUNET_i2s (&id));
456 if (NULL != peer->search_h)
458 GMD_search_stop (peer->search_h);
464 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
468 tunnel_destroy_empty (peer->tunnel);
475 * Returns if peer is used (has a tunnel, is neighbor).
477 * @peer Peer to check.
479 * @return GNUNET_YES if peer is in use.
482 peer_is_used (struct MeshPeer *peer)
484 struct MeshPeerPath *p;
486 if (NULL != peer->tunnel)
489 for (p = peer->path_head; NULL != p; p = p->next)
499 * Iterator over all the peers to get the oldest timestamp.
501 * @param cls Closure (unsued).
502 * @param key ID of the peer.
503 * @param value Peer_Info of the peer.
506 peer_get_oldest (void *cls,
507 const struct GNUNET_PeerIdentity *key,
510 struct MeshPeer *p = value;
511 struct GNUNET_TIME_Absolute *abs = cls;
513 /* Don't count active peers */
514 if (GNUNET_YES == peer_is_used (p))
517 if (abs->abs_value_us < p->last_contact.abs_value_us)
518 abs->abs_value_us = p->last_contact.abs_value_us;
525 * Iterator over all the peers to remove the oldest entry.
527 * @param cls Closure (unsued).
528 * @param key ID of the peer.
529 * @param value Peer_Info of the peer.
532 peer_timeout (void *cls,
533 const struct GNUNET_PeerIdentity *key,
536 struct MeshPeer *p = value;
537 struct GNUNET_TIME_Absolute *abs = cls;
539 if (p->last_contact.abs_value_us == abs->abs_value_us &&
540 GNUNET_NO == peer_is_used (p))
550 * Delete oldest unused peer.
553 peer_delete_oldest (void)
555 struct GNUNET_TIME_Absolute abs;
557 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
559 GNUNET_CONTAINER_multipeermap_iterate (peers,
562 GNUNET_CONTAINER_multipeermap_iterate (peers,
569 * Retrieve the MeshPeer stucture associated with the peer, create one
570 * and insert it in the appropriate structures if the peer is not known yet.
572 * @param peer Full identity of the peer.
574 * @return Existing or newly created peer info.
576 static struct MeshPeer *
577 peer_get (const struct GNUNET_PeerIdentity *peer_id)
579 struct MeshPeer *peer;
581 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
584 peer = GNUNET_new (struct MeshPeer);
585 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
587 peer_delete_oldest ();
589 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
590 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
591 peer->id = GNUNET_PEER_intern (peer_id);
593 peer->last_contact = GNUNET_TIME_absolute_get();
600 * Retrieve the MeshPeer stucture associated with the peer, create one
601 * and insert it in the appropriate structures if the peer is not known yet.
603 * @param peer Short identity of the peer.
605 * @return Existing or newly created peer info.
607 static struct MeshPeer *
608 peer_get_short (const GNUNET_PEER_Id peer)
610 return peer_get (GNUNET_PEER_resolve2 (peer));
615 * Get a cost of a path for a peer considering existing tunnel connections.
617 * @param peer Peer towards which the path is considered.
618 * @param path Candidate path.
620 * @return Cost of the path (path length + number of overlapping nodes)
623 peer_get_path_cost (const struct MeshPeer *peer,
624 const struct MeshPeerPath *path)
626 struct MeshConnection *c;
627 unsigned int overlap;
635 GNUNET_assert (NULL != peer->tunnel);
637 for (i = 0; i < path->length; i++)
639 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
641 for (j = 0; j < c->path->length; j++)
643 if (path->peers[i] == c->path->peers[j])
651 return (path->length + overlap) * (path->score * -1);
656 * Choose the best path towards a peer considering the tunnel properties.
658 * @param peer The destination peer.
660 * @return Best current known path towards the peer, if any.
662 static struct MeshPeerPath *
663 peer_get_best_path (const struct MeshPeer *peer)
665 struct MeshPeerPath *best_p;
666 struct MeshPeerPath *p;
667 struct MeshConnection *c;
668 unsigned int best_cost;
671 best_cost = UINT_MAX;
673 for (p = peer->path_head; NULL != p; p = p->next)
675 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
679 continue; /* If path is in use in a connection, skip it. */
681 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
692 * Add the path to the peer and update the path used to reach it in case this
695 * @param peer_info Destination peer to add the path to.
696 * @param path New path to add. Last peer must be the peer in arg 1.
697 * Path will be either used of freed if already known.
698 * @param trusted Do we trust that this path is real?
701 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
704 struct MeshPeerPath *aux;
708 if ((NULL == peer_info) || (NULL == path))
714 if (path->peers[path->length - 1] != peer_info->id)
720 if (2 >= path->length && GNUNET_NO == trusted)
722 /* Only allow CORE to tell us about direct paths */
726 for (l = 1; l < path->length; l++)
728 if (path->peers[l] == myid)
730 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
731 for (l2 = 0; l2 < path->length - l; l2++)
733 path->peers[l2] = path->peers[l + l2];
738 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
742 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
743 path->length, peer2s (peer_info));
745 l = path_get_length (path);
752 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
753 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
755 l2 = path_get_length (aux);
758 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
759 peer_info->path_tail, aux, path);
764 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
771 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
778 * Add the path to the origin peer and update the path used to reach it in case
779 * this is the shortest.
780 * The path is given in peer_info -> destination, therefore we turn the path
783 * @param peer_info Peer to add the path to, being the origin of the path.
784 * @param path New path to add after being inversed.
785 * Path will be either used or freed.
786 * @param trusted Do we trust that this path is real?
789 peer_add_path_to_origin (struct MeshPeer *peer_info,
790 struct MeshPeerPath *path, int trusted)
795 peer_add_path (peer_info, path, trusted);
800 * Adds a path to the peer_infos of all the peers in the path
802 * @param p Path to process.
803 * @param confirmed Whether we know if the path works or not.
806 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
810 /* TODO: invert and add */
811 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
812 for (i++; i < p->length; i++)
814 struct MeshPeer *aux;
815 struct MeshPeerPath *copy;
817 aux = peer_get_short (p->peers[i]);
818 copy = path_duplicate (p);
819 copy->length = i + 1;
820 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
826 * Function to process paths received for a new peer addition. The recorded
827 * paths form the initial tunnel, which can be optimized later.
828 * Called on each result obtained for the DHT search.
834 search_handler (void *cls, struct MeshPeerPath *path)
836 struct MeshPeer *peer = cls;
837 unsigned int connection_count;
839 path_add_to_peers (path, GNUNET_NO);
841 /* Count connections */
842 connection_count = GMC_count (peer->tunnel->connection_head);
844 /* If we already have 3 (or more (?!)) connections, it's enough */
845 if (3 <= connection_count)
848 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
850 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
858 * Core callback to write a queued packet to core buffer
860 * @param cls Closure (peer info).
861 * @param size Number of bytes available in buf.
862 * @param buf Where the to write the message.
864 * @return number of bytes written to buf
867 queue_send (void *cls, size_t size, void *buf)
869 struct MeshPeer *peer = cls;
870 struct MeshFlowControl *fc;
871 struct MeshConnection *c;
872 struct GNUNET_MessageHeader *msg;
873 struct MeshPeerQueue *queue;
874 struct MeshTunnel2 *t;
875 struct MeshChannel *ch;
876 const struct GNUNET_PeerIdentity *dst_id;
882 peer->core_transmit = NULL;
883 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
885 if (NULL == buf || 0 == size)
887 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
892 queue = peer_get_first_message (peer);
895 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
900 fc = fwd ? &c->fwd_fc : &c->bck_fc;
902 dst_id = GNUNET_PEER_resolve2 (peer->id);
903 LOG (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
904 /* Check if buffer size is enough for the message */
905 if (queue->size > size)
907 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
908 peer->core_transmit =
909 GNUNET_CORE_notify_transmit_ready (core_handle,
912 GNUNET_TIME_UNIT_FOREVER_REL,
919 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
921 t = (NULL != c) ? c->t : NULL;
927 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
928 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
929 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
930 case GNUNET_MESSAGE_TYPE_MESH_FWD:
931 case GNUNET_MESSAGE_TYPE_MESH_BCK:
932 case GNUNET_MESSAGE_TYPE_MESH_ACK:
933 case GNUNET_MESSAGE_TYPE_MESH_POLL:
934 LOG (GNUNET_ERROR_TYPE_DEBUG,
936 GNUNET_MESH_DEBUG_M2S (queue->type));
937 data_size = send_core_data_raw (queue->cls, size, buf);
938 msg = (struct GNUNET_MessageHeader *) buf;
939 type = ntohs (msg->type);
941 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
942 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
943 if (GMC_is_origin (c, GNUNET_YES))
944 data_size = send_core_connection_create (queue->c, size, buf);
946 data_size = send_core_data_raw (queue->cls, size, buf);
948 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
949 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
950 if (GMC_is_origin (c, GNUNET_NO) ||
951 GMC_is_origin (c, GNUNET_YES))
952 data_size = send_core_connection_ack (queue->c, size, buf);
954 data_size = send_core_data_raw (queue->cls, size, buf);
956 case GNUNET_MESSAGE_TYPE_MESH_DATA:
957 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
958 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
959 /* This should be encapsulted */
965 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
970 if (0 < drop_percent &&
971 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
973 LOG (GNUNET_ERROR_TYPE_WARNING,
974 "Dropping message of type %s\n",
975 GNUNET_MESH_DEBUG_M2S (queue->type));
979 /* Free queue, but cls was freed by send_core_* */
981 queue_destroy (queue, GNUNET_NO);
983 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
986 case GNUNET_MESSAGE_TYPE_MESH_FWD:
987 case GNUNET_MESSAGE_TYPE_MESH_BCK:
988 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
989 LOG (GNUNET_ERROR_TYPE_DEBUG, "* accounting pid %u\n", pid);
990 fc->last_pid_sent = pid;
991 send_ack (c, ch, fwd);
997 /* If more data in queue, send next */
998 queue = peer_get_first_message (peer);
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
1002 if (NULL == peer->core_transmit) {
1003 peer->core_transmit =
1004 GNUNET_CORE_notify_transmit_ready(core_handle,
1007 GNUNET_TIME_UNIT_FOREVER_REL,
1015 LOG (GNUNET_ERROR_TYPE_DEBUG,
1016 "* tmt rdy called somewhere else\n");
1018 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
1022 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
1027 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1029 GNUNET_SCHEDULER_cancel (fc->poll_task);
1030 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1035 c->pending_messages--;
1036 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
1038 LOG (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
1045 t->pending_messages--;
1046 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
1048 // LOG (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
1059 * Get first sendable message.
1061 * @param peer The destination peer.
1063 * @return Best current known path towards the peer, if any.
1065 static struct MeshPeerQueue *
1066 peer_get_first_message (const struct MeshPeer *peer)
1068 struct MeshPeerQueue *q;
1070 for (q = peer->queue_head; NULL != q; q = q->next)
1072 if (queue_is_sendable (q))
1081 queue_is_sendable (struct MeshPeerQueue *q)
1083 struct MeshFlowControl *fc;
1085 /* Is PID-independent? */
1088 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1089 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1093 /* Is PID allowed? */
1094 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
1095 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1102 /******************************************************************************/
1103 /******************************** API ***********************************/
1104 /******************************************************************************/
1108 * Free a transmission that was already queued with all resources
1109 * associated to the request.
1111 * @param queue Queue handler to cancel.
1112 * @param clear_cls Is it necessary to free associated cls?
1115 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
1117 struct MeshPeer *peer;
1118 struct MeshFlowControl *fc;
1123 GNUNET_assert (NULL != queue->c);
1124 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
1126 if (GNUNET_YES == clear_cls)
1128 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
1129 GNUNET_MESH_DEBUG_M2S (queue->type));
1130 switch (queue->type)
1132 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
1133 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
1134 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1135 GNUNET_break (GNUNET_YES == queue->c->destroy);
1137 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1138 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1139 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1140 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1141 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1142 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1143 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
1145 GNUNET_free_non_null (queue->cls);
1150 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1151 GNUNET_MESH_DEBUG_M2S (queue->type));
1155 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1157 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
1158 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
1160 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u\n", fc, fc->queue_n);
1164 if (NULL != queue->c)
1166 queue->c->pending_messages--;
1167 if (NULL != queue->c->t)
1169 queue->c->t->pending_messages--;
1173 GNUNET_free (queue);
1178 * @brief Queue and pass message to core when possible.
1180 * @param cls Closure (@c type dependant). It will be used by queue_send to
1181 * build the message to be sent if not already prebuilt.
1182 * @param type Type of the message, 0 for a raw message.
1183 * @param size Size of the message.
1184 * @param c Connection this message belongs to (cannot be NULL).
1185 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1186 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1189 GMP_queue_add (void *cls, uint16_t type, size_t size,
1190 struct MeshConnection *c,
1191 struct MeshChannel *ch,
1194 struct MeshPeerQueue *queue;
1195 struct MeshFlowControl *fc;
1196 struct MeshPeer *peer;
1200 LOG (GNUNET_ERROR_TYPE_DEBUG,
1201 "queue add %s %s (%u) on c %p, ch %p\n",
1202 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
1203 GNUNET_assert (NULL != c);
1205 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1206 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
1214 if (NULL == peer->connections)
1216 /* We are not connected to this peer, ignore request. */
1217 GNUNET_break_op (0);
1223 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1224 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1230 LOG (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
1231 if (fc->queue_n >= fc->queue_max && 0 == priority)
1233 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
1236 LOG (GNUNET_ERROR_TYPE_DEBUG,
1237 "queue full: %u/%u\n",
1238 fc->queue_n, fc->queue_max);
1239 return; /* Drop this message */
1242 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
1244 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
1246 call_core = GNUNET_NO;
1247 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task &&
1248 GNUNET_MESSAGE_TYPE_MESH_POLL != type)
1250 LOG (GNUNET_ERROR_TYPE_DEBUG,
1251 "no buffer space (%u > %u): starting poll\n",
1252 fc->last_pid_sent + 1, fc->last_ack_recv);
1253 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1259 call_core = GNUNET_YES;
1260 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1268 if (100 <= priority)
1270 struct MeshPeerQueue *copy;
1271 struct MeshPeerQueue *next;
1273 for (copy = peer->queue_head; NULL != copy; copy = next)
1276 if (copy->type == type && copy->c == c && copy->fwd == fwd)
1278 /* Example: also a FWD ACK for connection XYZ */
1279 queue_destroy (copy, GNUNET_YES);
1282 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1286 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1287 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
1292 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "calling core tmt rdy towards %s for %u bytes\n",
1296 peer2s (peer), size);
1297 peer->core_transmit =
1298 GNUNET_CORE_notify_transmit_ready (core_handle,
1301 GNUNET_TIME_UNIT_FOREVER_REL,
1302 GNUNET_PEER_resolve2 (peer->id),
1309 LOG (GNUNET_ERROR_TYPE_DEBUG,
1310 "core tmt rdy towards %s already called\n",
1314 c->pending_messages++;
1316 c->t->pending_messages++;
1322 * Initialize the peer subsystem.
1324 * @param c Configuration.
1325 * @param id Peer identity
1328 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c,
1329 const struct GNUNET_PeerIdentity *id)
1332 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1334 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1337 LOG_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1338 "MESH", "MAX_PEERS", "USING DEFAULT");
1343 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1350 LOG (GNUNET_ERROR_TYPE_WARNING,
1351 "\n***************************************\n"
1352 "Mesh is running with drop mode enabled.\n"
1353 "This is NOT a good idea!\n"
1354 "Remove the DROP_PERCENT option from your configuration.\n"
1355 "***************************************\n");
1358 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1359 NULL, /* Closure passed to MESH functions */
1360 &core_init, /* Call core_init once connected */
1361 &core_connect, /* Handle connects */
1362 &core_disconnect, /* remove peers on disconnects */
1363 NULL, /* Don't notify about all incoming messages */
1364 GNUNET_NO, /* For header only in notification */
1365 NULL, /* Don't notify about all outbound messages */
1366 GNUNET_NO, /* For header-only out notification */
1367 core_handlers); /* Register these handlers */
1368 if (NULL == core_handle)
1371 GNUNET_SCHEDULER_shutdown ();
1377 * Shut down the peer subsystem.
1382 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1387 * Try to establish a new connection to this peer in the given tunnel.
1388 * If the peer doesn't have any path to it yet, try to get one.
1389 * If the peer already has some path, send a CREATE CONNECTION towards it.
1391 * @param peer PeerInfo of the peer.
1394 GMP_connect (struct MeshPeer *peer)
1396 struct MeshTunnel2 *t;
1397 struct MeshPeerPath *p;
1398 struct MeshConnection *c;
1401 LOG (GNUNET_ERROR_TYPE_DEBUG,
1402 "peer_connect towards %s\n",
1406 rerun_search = GNUNET_NO;
1408 if (NULL != peer->path_head)
1410 LOG (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
1411 p = peer_get_best_path (peer);
1414 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1415 c = tunnel_use_path (t, p);
1418 /* This case can happen when the path includes a first hop that is
1419 * not yet known to be connected.
1421 * This happens quite often during testing when running mesh
1422 * under valgrind: core connect notifications come very late and the
1423 * DHT result has already come and created a valid path.
1424 * In this case, the peer->connections hashmap will be NULL and
1425 * tunnel_use_path will not be able to create a connection from that
1428 * Re-running the DHT GET should give core time to callback.
1431 rerun_search = GNUNET_YES;
1435 send_connection_create (c);
1441 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1443 GMD_search_stop (peer->search_h);
1444 peer->search_h = NULL;
1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1446 " Stopping DHT GET for peer %s\n", peer2s (peer));
1449 if (NULL == peer->search_h)
1451 const struct GNUNET_PeerIdentity *id;
1453 id = GNUNET_PEER_resolve2 (peer->id);
1454 LOG (GNUNET_ERROR_TYPE_DEBUG,
1455 " Starting DHT GET for peer %s\n", peer2s (peer));
1456 peer->search_h = GMD_search (id, &search_handler, peer);
1457 if (MESH_TUNNEL_NEW == t->state)
1458 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
1463 * Get the static string for a peer ID.
1467 * @return Static string for it's ID.
1470 GMP_2s (const struct MeshPeer *peer)
1474 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));