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"
26 #include "gnunet_statistics_service.h"
28 #include "mesh_protocol_enc.h"
30 #include "gnunet-service-mesh_peer.h"
31 #include "gnunet-service-mesh_dht.h"
32 #include "gnunet-service-mesh_connection.h"
33 #include "gnunet-service-mesh_local.h"
34 #include "gnunet-service-mesh_tunnel.h"
35 #include "mesh_path.h"
37 #define LOG(level, ...) GNUNET_log_from (level,"mesh-p2p",__VA_ARGS__)
39 /******************************************************************************/
40 /******************************** STRUCTS **********************************/
41 /******************************************************************************/
44 * Struct containing info about a queued transmission to this peer
51 struct MeshPeerQueue *next;
56 struct MeshPeerQueue *prev;
59 * Peer this transmission is directed to.
61 struct MeshPeer *peer;
64 * Connection this message belongs to.
66 struct MeshConnection *c;
74 * Channel this message belongs to, if known.
76 struct MeshChannel *ch;
79 * Pointer to info stucture used as cls.
94 * Set when this message starts waiting for CORE.
96 struct GNUNET_TIME_Absolute start_waiting;
99 * Function to call on sending.
104 * Closure for callback.
110 * Struct containing all information regarding a given peer
120 * Last time we heard from this peer
122 struct GNUNET_TIME_Absolute last_contact;
125 * Paths to reach the peer, ordered by ascending hop count
127 struct MeshPeerPath *path_head;
130 * Paths to reach the peer, ordered by ascending hop count
132 struct MeshPeerPath *path_tail;
135 * Handle to stop the DHT search for paths to this peer
137 struct GMD_search_handle *search_h;
140 * Tunnel to this peer, if any.
142 struct MeshTunnel2 *tunnel;
145 * Connections that go through this peer, indexed by tid;
147 struct GNUNET_CONTAINER_MultiHashMap *connections;
150 * Handle for queued transmissions
152 struct GNUNET_CORE_TransmitHandle *core_transmit;
155 * Transmission queue to core DLL head
157 struct MeshPeerQueue *queue_head;
160 * Transmission queue to core DLL tail
162 struct MeshPeerQueue *queue_tail;
165 * How many messages are in the queue to this peer.
167 unsigned int queue_n;
171 /******************************************************************************/
172 /******************************* GLOBALS ***********************************/
173 /******************************************************************************/
176 * Global handle to the statistics service.
178 extern struct GNUNET_STATISTICS_Handle *stats;
181 * Peers known, indexed by PeerIdentity (MeshPeer).
183 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
186 * How many peers do we want to remember?
188 static unsigned long long max_peers;
191 * Percentage of messages that will be dropped (for test purposes only).
193 static unsigned long long drop_percent;
196 * Handle to communicate with core.
198 static struct GNUNET_CORE_Handle *core_handle;
201 * Local peer own ID (full value).
203 const static struct GNUNET_PeerIdentity *my_full_id;
205 /******************************************************************************/
206 /***************************** CORE CALLBACKS *********************************/
207 /******************************************************************************/
211 * Iterator to notify all connections of a broken link. Mark connections
212 * to destroy after all traffic has been sent.
214 * @param cls Closure (peer disconnected).
215 * @param key Current key code (peer id).
216 * @param value Value in the hash map (connection).
218 * @return GNUNET_YES if we should continue to iterate,
222 notify_broken (void *cls,
223 const struct GNUNET_HashCode *key,
226 struct MeshPeer *peer = cls;
227 struct MeshConnection *c = value;
229 GMC_notify_broken (c, peer, my_full_id);
236 * Method called whenever a given peer connects.
239 * @param peer peer identity this notification is about
242 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
245 struct MeshPeerPath *path;
247 LOG ("Peer connected\n");
248 LOG (" %s\n", GNUNET_i2s (&my_full_id));
249 pi = peer_get (peer);
257 LOG (" %s\n", GNUNET_i2s (peer));
259 path->peers[1] = pi->id;
260 GNUNET_PEER_change_rc (pi->id, 1);
261 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
263 path->peers[0] = myid;
264 GNUNET_PEER_change_rc (myid, 1);
265 peer_add_path (pi, path, GNUNET_YES);
267 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
273 * Method called whenever a peer disconnects.
276 * @param peer peer identity this notification is about
279 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
283 LOG ("Peer disconnected\n");
284 pi = GNUNET_CONTAINER_multipeermap_get (peers, peer);
291 GNUNET_CONTAINER_multihashmap_iterate (pi->connections, ¬ify_broken, pi);
292 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
293 pi->connections = NULL;
294 if (NULL != pi->core_transmit)
296 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit);
297 pi->core_transmit = NULL;
303 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
310 * Functions to handle messages from core
312 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
313 {&GMC_handle_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
315 {&GMC_handle_confirm, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
316 sizeof (struct GNUNET_MESH_ConnectionACK)},
317 {&GMC_handle_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
318 sizeof (struct GNUNET_MESH_ConnectionBroken)},
319 {&GMC_handle_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
320 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
321 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
322 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
323 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
324 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
325 {&GMC_handle_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
326 sizeof (struct GNUNET_MESH_ACK)},
327 {&GMC_handle_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
328 sizeof (struct GNUNET_MESH_Poll)},
329 {&GMC_handle_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
330 {&GMC_handle_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
336 * To be called on core init/fail.
338 * @param cls Closure (config)
339 * @param identity the public identity of this peer
342 core_init (void *cls,
343 const struct GNUNET_PeerIdentity *identity)
345 const struct GNUNET_CONFIGURATION_Handle *c = cls;
348 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
349 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
351 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
352 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
353 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
354 GNUNET_CORE_disconnect (core_handle);
355 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
356 NULL, /* Closure passed to MESH functions */
357 &core_init, /* Call core_init once connected */
358 &core_connect, /* Handle connects */
359 &core_disconnect, /* remove peers on disconnects */
360 NULL, /* Don't notify about all incoming messages */
361 GNUNET_NO, /* For header only in notification */
362 NULL, /* Don't notify about all outbound messages */
363 GNUNET_NO, /* For header-only out notification */
364 core_handlers); /* Register these handlers */
373 * Core callback to write a pre-constructed data packet to core buffer
375 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
376 * @param size Number of bytes available in buf.
377 * @param buf Where the to write the message.
379 * @return number of bytes written to buf
382 send_core_data_raw (void *cls, size_t size, void *buf)
384 struct GNUNET_MessageHeader *msg = cls;
387 GNUNET_assert (NULL != msg);
388 total_size = ntohs (msg->size);
390 if (total_size > size)
395 memcpy (buf, msg, total_size);
402 * Function to send a create connection message to a peer.
404 * @param c Connection to create.
405 * @param size number of bytes available in buf
406 * @param buf where the callee should write the message
407 * @return number of bytes written to buf
410 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
412 struct GNUNET_MESH_ConnectionCreate *msg;
413 struct GNUNET_PeerIdentity *peer_ptr;
414 struct MeshPeerPath *p = c->path;
418 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
420 sizeof (struct GNUNET_MESH_ConnectionCreate) +
421 p->length * sizeof (struct GNUNET_PeerIdentity);
423 if (size < size_needed || NULL == buf)
428 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
429 msg->header.size = htons (size_needed);
430 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
433 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
434 for (i = 0; i < p->length; i++)
436 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
439 LOG (GNUNET_ERROR_TYPE_DEBUG,
440 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
446 * Creates a path ack message in buf and frees all unused resources.
448 * @param c Connection to send an ACK on.
449 * @param size number of bytes available in buf
450 * @param buf where the callee should write the message
452 * @return number of bytes written to buf
455 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
457 struct GNUNET_MESH_ConnectionACK *msg = buf;
458 struct MeshTunnel2 *t = c->t;
460 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
461 GNUNET_assert (NULL != t);
462 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
467 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
468 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
472 /* TODO add signature */
474 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
475 return sizeof (struct GNUNET_MESH_ConnectionACK);
479 /******************************************************************************/
480 /******************************** STATIC ***********************************/
481 /******************************************************************************/
484 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
487 * @param key current key code
488 * @param value value in the hash map
489 * @return #GNUNET_YES if we should continue to iterate,
493 shutdown_tunnel (void *cls,
494 const struct GNUNET_PeerIdentity *key,
497 struct MeshPeer *p = value;
498 struct MeshTunnel2 *t = p->tunnel;
508 * Destroy the peer_info and free any allocated resources linked to it
510 * @param peer The peer_info to destroy.
512 * @return GNUNET_OK on success
515 peer_destroy (struct MeshPeer *peer)
517 struct GNUNET_PeerIdentity id;
518 struct MeshPeerPath *p;
519 struct MeshPeerPath *nextp;
521 GNUNET_PEER_resolve (peer->id, &id);
522 GNUNET_PEER_change_rc (peer->id, -1);
525 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
528 LOG (GNUNET_ERROR_TYPE_WARNING,
529 "removing peer %s, not in peermap\n", GNUNET_i2s (&id));
531 if (NULL != peer->search_h)
533 GMD_search_stop (peer->search_h);
539 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
543 tunnel_destroy_empty (peer->tunnel);
550 * Returns if peer is used (has a tunnel, is neighbor).
552 * @peer Peer to check.
554 * @return GNUNET_YES if peer is in use.
557 peer_is_used (struct MeshPeer *peer)
559 struct MeshPeerPath *p;
561 if (NULL != peer->tunnel)
564 for (p = peer->path_head; NULL != p; p = p->next)
574 * Iterator over all the peers to get the oldest timestamp.
576 * @param cls Closure (unsued).
577 * @param key ID of the peer.
578 * @param value Peer_Info of the peer.
581 peer_get_oldest (void *cls,
582 const struct GNUNET_PeerIdentity *key,
585 struct MeshPeer *p = value;
586 struct GNUNET_TIME_Absolute *abs = cls;
588 /* Don't count active peers */
589 if (GNUNET_YES == peer_is_used (p))
592 if (abs->abs_value_us < p->last_contact.abs_value_us)
593 abs->abs_value_us = p->last_contact.abs_value_us;
600 * Iterator over all the peers to remove the oldest entry.
602 * @param cls Closure (unsued).
603 * @param key ID of the peer.
604 * @param value Peer_Info of the peer.
607 peer_timeout (void *cls,
608 const struct GNUNET_PeerIdentity *key,
611 struct MeshPeer *p = value;
612 struct GNUNET_TIME_Absolute *abs = cls;
614 if (p->last_contact.abs_value_us == abs->abs_value_us &&
615 GNUNET_NO == peer_is_used (p))
625 * Delete oldest unused peer.
628 peer_delete_oldest (void)
630 struct GNUNET_TIME_Absolute abs;
632 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
634 GNUNET_CONTAINER_multipeermap_iterate (peers,
637 GNUNET_CONTAINER_multipeermap_iterate (peers,
644 * Retrieve the MeshPeer stucture associated with the peer, create one
645 * and insert it in the appropriate structures if the peer is not known yet.
647 * @param peer Full identity of the peer.
649 * @return Existing or newly created peer info.
651 static struct MeshPeer *
652 peer_get (const struct GNUNET_PeerIdentity *peer_id)
654 struct MeshPeer *peer;
656 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
659 peer = GNUNET_new (struct MeshPeer);
660 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
662 peer_delete_oldest ();
664 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
665 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
666 peer->id = GNUNET_PEER_intern (peer_id);
668 peer->last_contact = GNUNET_TIME_absolute_get();
675 * Retrieve the MeshPeer stucture associated with the peer, create one
676 * and insert it in the appropriate structures if the peer is not known yet.
678 * @param peer Short identity of the peer.
680 * @return Existing or newly created peer info.
682 static struct MeshPeer *
683 peer_get_short (const GNUNET_PEER_Id peer)
685 return peer_get (GNUNET_PEER_resolve2 (peer));
690 * Get a cost of a path for a peer considering existing tunnel connections.
692 * @param peer Peer towards which the path is considered.
693 * @param path Candidate path.
695 * @return Cost of the path (path length + number of overlapping nodes)
698 peer_get_path_cost (const struct MeshPeer *peer,
699 const struct MeshPeerPath *path)
701 struct MeshConnection *c;
702 unsigned int overlap;
710 GNUNET_assert (NULL != peer->tunnel);
712 for (i = 0; i < path->length; i++)
714 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
716 for (j = 0; j < c->path->length; j++)
718 if (path->peers[i] == c->path->peers[j])
726 return (path->length + overlap) * (path->score * -1);
731 * Choose the best path towards a peer considering the tunnel properties.
733 * @param peer The destination peer.
735 * @return Best current known path towards the peer, if any.
737 static struct MeshPeerPath *
738 peer_get_best_path (const struct MeshPeer *peer)
740 struct MeshPeerPath *best_p;
741 struct MeshPeerPath *p;
742 struct MeshConnection *c;
743 unsigned int best_cost;
746 best_cost = UINT_MAX;
748 for (p = peer->path_head; NULL != p; p = p->next)
750 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
754 continue; /* If path is in use in a connection, skip it. */
756 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
767 * Add the path to the peer and update the path used to reach it in case this
770 * @param peer_info Destination peer to add the path to.
771 * @param path New path to add. Last peer must be the peer in arg 1.
772 * Path will be either used of freed if already known.
773 * @param trusted Do we trust that this path is real?
776 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
779 struct MeshPeerPath *aux;
783 if ((NULL == peer_info) || (NULL == path))
789 if (path->peers[path->length - 1] != peer_info->id)
795 if (2 >= path->length && GNUNET_NO == trusted)
797 /* Only allow CORE to tell us about direct paths */
801 for (l = 1; l < path->length; l++)
803 if (path->peers[l] == myid)
805 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
806 for (l2 = 0; l2 < path->length - l; l2++)
808 path->peers[l2] = path->peers[l + l2];
813 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
817 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
818 path->length, peer2s (peer_info));
820 l = path_get_length (path);
827 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
828 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
830 l2 = path_get_length (aux);
833 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
834 peer_info->path_tail, aux, path);
839 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
846 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
853 * Add the path to the origin peer and update the path used to reach it in case
854 * this is the shortest.
855 * The path is given in peer_info -> destination, therefore we turn the path
858 * @param peer_info Peer to add the path to, being the origin of the path.
859 * @param path New path to add after being inversed.
860 * Path will be either used or freed.
861 * @param trusted Do we trust that this path is real?
864 peer_add_path_to_origin (struct MeshPeer *peer_info,
865 struct MeshPeerPath *path, int trusted)
870 peer_add_path (peer_info, path, trusted);
875 * Adds a path to the peer_infos of all the peers in the path
877 * @param p Path to process.
878 * @param confirmed Whether we know if the path works or not.
881 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
885 /* TODO: invert and add */
886 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
887 for (i++; i < p->length; i++)
889 struct MeshPeer *aux;
890 struct MeshPeerPath *copy;
892 aux = peer_get_short (p->peers[i]);
893 copy = path_duplicate (p);
894 copy->length = i + 1;
895 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
901 * Function to process paths received for a new peer addition. The recorded
902 * paths form the initial tunnel, which can be optimized later.
903 * Called on each result obtained for the DHT search.
909 search_handler (void *cls, struct MeshPeerPath *path)
911 struct MeshPeer *peer = cls;
912 unsigned int connection_count;
914 path_add_to_peers (path, GNUNET_NO);
916 /* Count connections */
917 connection_count = GMC_count (peer->tunnel->connection_head);
919 /* If we already have 3 (or more (?!)) connections, it's enough */
920 if (3 <= connection_count)
923 if (peer->tunnel->state == MESH_TUNNEL3_SEARCHING)
925 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
933 * Core callback to write a queued packet to core buffer
935 * @param cls Closure (peer info).
936 * @param size Number of bytes available in buf.
937 * @param buf Where the to write the message.
939 * @return number of bytes written to buf
942 queue_send (void *cls, size_t size, void *buf)
944 struct MeshPeer *peer = cls;
945 struct MeshFlowControl *fc;
946 struct MeshConnection *c;
947 struct GNUNET_MessageHeader *msg;
948 struct MeshPeerQueue *queue;
949 struct MeshTunnel2 *t;
950 struct MeshChannel *ch;
951 const struct GNUNET_PeerIdentity *dst_id;
957 peer->core_transmit = NULL;
958 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
960 if (NULL == buf || 0 == size)
962 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
967 queue = peer_get_first_message (peer);
970 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
975 fc = fwd ? &c->fwd_fc : &c->bck_fc;
977 dst_id = GNUNET_PEER_resolve2 (peer->id);
978 LOG (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
979 /* Check if buffer size is enough for the message */
980 if (queue->size > size)
982 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
983 peer->core_transmit =
984 GNUNET_CORE_notify_transmit_ready (core_handle,
987 GNUNET_TIME_UNIT_FOREVER_REL,
994 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
996 t = (NULL != c) ? c->t : NULL;
1000 switch (queue->type)
1002 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL3_DESTROY:
1003 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
1004 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1005 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1006 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1007 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1008 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1009 LOG (GNUNET_ERROR_TYPE_DEBUG,
1011 GNUNET_MESH_DEBUG_M2S (queue->type));
1012 data_size = send_core_data_raw (queue->cls, size, buf);
1013 msg = (struct GNUNET_MessageHeader *) buf;
1014 type = ntohs (msg->type);
1016 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1017 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
1018 if (GMC_is_origin (c, GNUNET_YES))
1019 data_size = send_core_connection_create (queue->c, size, buf);
1021 data_size = send_core_data_raw (queue->cls, size, buf);
1023 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1024 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
1025 if (GMC_is_origin (c, GNUNET_NO) ||
1026 GMC_is_origin (c, GNUNET_YES))
1027 data_size = send_core_connection_ack (queue->c, size, buf);
1029 data_size = send_core_data_raw (queue->cls, size, buf);
1031 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1032 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1033 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1034 /* This should be encapsulted */
1040 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
1045 if (0 < drop_percent &&
1046 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1048 LOG (GNUNET_ERROR_TYPE_WARNING,
1049 "Dropping message of type %s\n",
1050 GNUNET_MESH_DEBUG_M2S (queue->type));
1054 if (NULL != queue->callback)
1056 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Calling callback\n");
1057 queue->callback (queue->callback_cls,
1059 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
1062 /* Free queue, but cls was freed by send_core_* */
1064 GMP_queue_destroy (queue, GNUNET_NO);
1066 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
1069 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1070 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1071 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, "* accounting pid %u\n", pid);
1073 fc->last_pid_sent = pid;
1074 send_ack (c, ch, fwd);
1080 /* If more data in queue, send next */
1081 queue = peer_get_first_message (peer);
1084 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
1085 if (NULL == peer->core_transmit) {
1086 peer->core_transmit =
1087 GNUNET_CORE_notify_transmit_ready(core_handle,
1090 GNUNET_TIME_UNIT_FOREVER_REL,
1095 queue->start_waiting = GNUNET_TIME_absolute_get ();
1099 LOG (GNUNET_ERROR_TYPE_DEBUG,
1100 "* tmt rdy called somewhere else\n");
1102 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
1106 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
1111 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1113 GNUNET_SCHEDULER_cancel (fc->poll_task);
1114 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1119 c->pending_messages--;
1120 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
1122 LOG (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
1129 t->pending_messages--;
1130 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
1132 // LOG (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
1136 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
1143 * Get first sendable message.
1145 * @param peer The destination peer.
1147 * @return Best current known path towards the peer, if any.
1149 static struct MeshPeerQueue *
1150 peer_get_first_message (const struct MeshPeer *peer)
1152 struct MeshPeerQueue *q;
1154 for (q = peer->queue_head; NULL != q; q = q->next)
1156 if (queue_is_sendable (q))
1165 queue_is_sendable (struct MeshPeerQueue *q)
1167 struct MeshFlowControl *fc;
1169 /* Is PID-independent? */
1172 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1173 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1177 /* Is PID allowed? */
1178 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
1179 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1186 /******************************************************************************/
1187 /******************************** API ***********************************/
1188 /******************************************************************************/
1192 * Free a transmission that was already queued with all resources
1193 * associated to the request.
1195 * @param queue Queue handler to cancel.
1196 * @param clear_cls Is it necessary to free associated cls?
1199 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
1201 struct MeshPeer *peer;
1202 struct MeshFlowControl *fc;
1207 GNUNET_assert (NULL != queue->c);
1208 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
1210 if (GNUNET_YES == clear_cls)
1212 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
1213 GNUNET_MESH_DEBUG_M2S (queue->type));
1214 switch (queue->type)
1216 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
1217 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL3_DESTROY:
1218 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1219 GNUNET_break (GNUNET_YES == queue->c->destroy);
1221 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1222 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1223 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1224 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1225 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1226 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1227 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1228 LOG (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
1229 GNUNET_free_non_null (queue->cls);
1234 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1235 GNUNET_MESH_DEBUG_M2S (queue->type));
1239 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1241 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
1242 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
1244 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u\n", fc, fc->queue_n);
1248 if (NULL != queue->c)
1250 queue->c->pending_messages--;
1251 if (NULL != queue->c->t)
1253 queue->c->t->pending_messages--;
1257 GNUNET_free (queue);
1262 * @brief Queue and pass message to core when possible.
1264 * @param cls Closure (@c type dependant). It will be used by queue_send to
1265 * build the message to be sent if not already prebuilt.
1266 * @param type Type of the message, 0 for a raw message.
1267 * @param size Size of the message.
1268 * @param c Connection this message belongs to (cannot be NULL).
1269 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1270 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1271 * @param callback Function to be called once CORE has taken the message.
1272 * @param callback_cls Closure for @c callback.
1275 GMP_queue_add (void *cls, uint16_t type, size_t size,
1276 struct MeshConnection *c,
1277 struct MeshChannel *ch,
1279 GMP_sent callback, void *callback_cls)
1281 struct MeshPeerQueue *queue;
1282 struct MeshFlowControl *fc;
1283 struct MeshPeer *peer;
1287 LOG (GNUNET_ERROR_TYPE_DEBUG,
1288 "queue add %s %s (%u) on c %p, ch %p\n",
1289 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
1290 GNUNET_assert (NULL != c);
1292 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1293 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
1301 if (NULL == peer->connections)
1303 /* We are not connected to this peer, ignore request. */
1304 GNUNET_break_op (0);
1310 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1311 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1316 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1317 LOG (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
1318 if (fc->queue_n >= fc->queue_max && 0 == priority)
1320 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
1323 LOG (GNUNET_ERROR_TYPE_DEBUG,
1324 "queue full: %u/%u\n",
1325 fc->queue_n, fc->queue_max);
1326 return; /* Drop this message */
1329 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
1330 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
1331 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
1333 call_core = GNUNET_NO;
1334 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task &&
1335 GNUNET_MESSAGE_TYPE_MESH_POLL != type)
1337 LOG (GNUNET_ERROR_TYPE_DEBUG,
1338 "no buffer space (%u > %u): starting poll\n",
1339 fc->last_pid_sent + 1, fc->last_ack_recv);
1340 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1346 call_core = GNUNET_YES;
1347 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1355 queue->callback = callback;
1356 queue->callback_cls = callback_cls;
1357 if (100 <= priority)
1359 struct MeshPeerQueue *copy;
1360 struct MeshPeerQueue *next;
1362 for (copy = peer->queue_head; NULL != copy; copy = next)
1365 if (copy->type == type && copy->c == c && copy->fwd == fwd)
1367 /* Example: also a FWD ACK for connection XYZ */
1368 GMP_queue_destroy (copy, GNUNET_YES);
1371 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1375 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1376 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
1381 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1383 LOG (GNUNET_ERROR_TYPE_DEBUG,
1384 "calling core tmt rdy towards %s for %u bytes\n",
1385 peer2s (peer), size);
1386 peer->core_transmit =
1387 GNUNET_CORE_notify_transmit_ready (core_handle,
1390 GNUNET_TIME_UNIT_FOREVER_REL,
1391 GNUNET_PEER_resolve2 (peer->id),
1395 queue->start_waiting = GNUNET_TIME_absolute_get ();
1399 LOG (GNUNET_ERROR_TYPE_DEBUG,
1400 "core tmt rdy towards %s already called\n",
1404 c->pending_messages++;
1406 c->t->pending_messages++;
1411 * Cancel all queued messages to a peer that belong to a certain connection.
1413 * @param peer Peer towards whom to cancel.
1414 * @param c Connection whose queued messages to cancel.
1417 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1419 struct MeshPeerQueue *q;
1420 struct MeshPeerQueue *next;
1422 for (q = peer->queue_head; NULL != q; q = next)
1427 LOG (GNUNET_ERROR_TYPE_DEBUG,
1428 "connection_cancel_queue %s\n",
1429 GNUNET_MESH_DEBUG_M2S (q->type));
1430 GMP_queue_destroy (q, GNUNET_YES);
1433 if (NULL == peer->queue_head)
1435 if (NULL != peer->core_transmit)
1437 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1438 peer->core_transmit = NULL;
1445 * Initialize the peer subsystem.
1447 * @param c Configuration.
1448 * @param id Peer identity
1451 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c,
1452 const struct GNUNET_PeerIdentity *id)
1455 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1457 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1460 LOG_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1461 "MESH", "MAX_PEERS", "USING DEFAULT");
1466 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1473 LOG (GNUNET_ERROR_TYPE_WARNING,
1474 "\n***************************************\n"
1475 "Mesh is running with drop mode enabled.\n"
1476 "This is NOT a good idea!\n"
1477 "Remove the DROP_PERCENT option from your configuration.\n"
1478 "***************************************\n");
1481 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1482 NULL, /* Closure passed to MESH functions */
1483 &core_init, /* Call core_init once connected */
1484 &core_connect, /* Handle connects */
1485 &core_disconnect, /* remove peers on disconnects */
1486 NULL, /* Don't notify about all incoming messages */
1487 GNUNET_NO, /* For header only in notification */
1488 NULL, /* Don't notify about all outbound messages */
1489 GNUNET_NO, /* For header-only out notification */
1490 core_handlers); /* Register these handlers */
1491 if (NULL == core_handle)
1494 GNUNET_SCHEDULER_shutdown ();
1500 * Shut down the peer subsystem.
1505 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1507 if (core_handle != NULL)
1509 GNUNET_CORE_disconnect (core_handle);
1516 * Try to establish a new connection to this peer in the given tunnel.
1517 * If the peer doesn't have any path to it yet, try to get one.
1518 * If the peer already has some path, send a CREATE CONNECTION towards it.
1520 * @param peer PeerInfo of the peer.
1523 GMP_connect (struct MeshPeer *peer)
1525 struct MeshTunnel2 *t;
1526 struct MeshPeerPath *p;
1527 struct MeshConnection *c;
1530 LOG (GNUNET_ERROR_TYPE_DEBUG,
1531 "peer_connect towards %s\n",
1535 rerun_search = GNUNET_NO;
1537 if (NULL != peer->path_head)
1539 LOG (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
1540 p = peer_get_best_path (peer);
1543 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1544 c = tunnel_use_path (t, p);
1547 /* This case can happen when the path includes a first hop that is
1548 * not yet known to be connected.
1550 * This happens quite often during testing when running mesh
1551 * under valgrind: core connect notifications come very late and the
1552 * DHT result has already come and created a valid path.
1553 * In this case, the peer->connections hashmap will be NULL and
1554 * tunnel_use_path will not be able to create a connection from that
1557 * Re-running the DHT GET should give core time to callback.
1560 rerun_search = GNUNET_YES;
1564 send_connection_create (c);
1570 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1572 GMD_search_stop (peer->search_h);
1573 peer->search_h = NULL;
1574 LOG (GNUNET_ERROR_TYPE_DEBUG,
1575 " Stopping DHT GET for peer %s\n",
1579 if (NULL == peer->search_h)
1581 const struct GNUNET_PeerIdentity *id;
1583 id = GNUNET_PEER_resolve2 (peer->id);
1584 LOG (GNUNET_ERROR_TYPE_DEBUG,
1585 " Starting DHT GET for peer %s\n", peer2s (peer));
1586 peer->search_h = GMD_search (id, &search_handler, peer);
1587 if (MESH_TUNNEL3_NEW == t->state)
1588 GMT_change_state (t, MESH_TUNNEL3_SEARCHING);
1600 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel2 *t)
1607 * Chech whether there is a direct (core level) connection to peer.
1609 * @param peer Peer to check.
1611 * @return GNUNET_YES if there is a direct connection.
1614 GMP_is_neighbor (const struct MeshPeer *peer)
1616 struct MeshPeerPath *path;
1618 if (NULL == peer->connections)
1621 for (path = peer->path_head; NULL != path; path = path->next)
1623 if (3 > path->length)
1627 GNUNET_break (0); /* Is not a neighbor but connections is not NULL */
1633 * Add a connection to a neighboring peer.
1635 * Store that the peer is the first hop of the connection in one
1636 * direction and that on peer disconnect the connection must be
1637 * notified and destroyed, for it will no longer be valid.
1639 * @param peer Peer to add connection to.
1640 * @param c Connection to add.
1642 * @return GNUNET_OK on success.
1645 GMP_add_connection (struct MeshPeer *peer,
1646 const struct MeshConnection *c)
1648 if (NULL == peer->connections)
1651 return GNUNET_SYSERR;
1653 return GNUNET_CONTAINER_multihashmap_put (peer->connections,
1656 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1661 * Remove a connection from a neighboring peer.
1663 * @param peer Peer to remove connection from.
1664 * @param c Connection to remove.
1666 * @return GNUNET_OK on success.
1669 GMP_remove_connection (struct MeshPeer *peer,
1670 const struct MeshConnection *c)
1672 if (NULL == peer || NULL == peer->connections)
1675 return GNUNET_SYSERR;
1677 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1683 * Get the Full ID of a peer.
1685 * @param peer Peer to get from.
1687 * @return Full ID of peer.
1689 struct GNUNET_PeerIdentity *
1690 GMP_get_id (const struct MeshPeer *peer)
1692 return GNUNET_PEER_resolve2 (peer->id);
1697 * Get the Short ID of a peer.
1699 * @param peer Peer to get from.
1701 * @return Short ID of peer.
1704 GMP_get_short_id (const struct MeshPeer *peer)
1711 * Get the static string for a peer ID.
1715 * @return Static string for it's ID.
1718 GMP_2s (const struct MeshPeer *peer)
1722 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));