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 MeshTunnel3 *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 * Local peer own ID (full value).
183 extern struct GNUNET_PeerIdentity my_full_id;
186 * Local peer own ID (short)
188 extern GNUNET_PEER_Id myid;
191 * Peers known, indexed by PeerIdentity (MeshPeer).
193 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
196 * How many peers do we want to remember?
198 static unsigned long long max_peers;
201 * Percentage of messages that will be dropped (for test purposes only).
203 static unsigned long long drop_percent;
206 * Handle to communicate with core.
208 static struct GNUNET_CORE_Handle *core_handle;
211 /******************************************************************************/
212 /***************************** CORE CALLBACKS *********************************/
213 /******************************************************************************/
217 * Iterator to notify all connections of a broken link. Mark connections
218 * to destroy after all traffic has been sent.
220 * @param cls Closure (peer disconnected).
221 * @param key Current key code (peer id).
222 * @param value Value in the hash map (connection).
224 * @return GNUNET_YES if we should continue to iterate,
228 notify_broken (void *cls,
229 const struct GNUNET_HashCode *key,
232 struct MeshPeer *peer = cls;
233 struct MeshConnection *c = value;
235 GMC_notify_broken (c, peer);
242 * Method called whenever a given peer connects.
245 * @param peer peer identity this notification is about
248 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
251 struct MeshPeerPath *path;
253 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer connected\n");
254 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&my_full_id));
258 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self)\n");
263 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (peer));
265 path->peers[1] = mp->id;
266 GNUNET_PEER_change_rc (mp->id, 1);
267 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
269 path->peers[0] = myid;
270 GNUNET_PEER_change_rc (myid, 1);
271 GMP_add_path (mp, path, GNUNET_YES);
273 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
279 * Method called whenever a peer disconnects.
282 * @param peer peer identity this notification is about
285 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
289 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer disconnected\n");
290 pi = GNUNET_CONTAINER_multipeermap_get (peers, peer);
297 GNUNET_CONTAINER_multihashmap_iterate (pi->connections, ¬ify_broken, pi);
298 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
299 pi->connections = NULL;
300 if (NULL != pi->core_transmit)
302 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit);
303 pi->core_transmit = NULL;
307 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self)\n");
309 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
316 * Functions to handle messages from core
318 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
319 {&GMC_handle_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
321 {&GMC_handle_confirm, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
322 sizeof (struct GNUNET_MESH_ConnectionACK)},
323 {&GMC_handle_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
324 sizeof (struct GNUNET_MESH_ConnectionBroken)},
325 {&GMC_handle_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
326 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
327 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
328 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
329 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
330 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
331 {&GMC_handle_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
332 sizeof (struct GNUNET_MESH_ACK)},
333 {&GMC_handle_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
334 sizeof (struct GNUNET_MESH_Poll)},
335 {&GMC_handle_encrypted, GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED, 0},
341 * To be called on core init/fail.
343 * @param cls Closure (config)
344 * @param identity the public identity of this peer
347 core_init (void *cls,
348 const struct GNUNET_PeerIdentity *identity)
350 const struct GNUNET_CONFIGURATION_Handle *c = cls;
353 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
354 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
356 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
357 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
358 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
359 GNUNET_CORE_disconnect (core_handle);
360 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
361 NULL, /* Closure passed to MESH functions */
362 &core_init, /* Call core_init once connected */
363 &core_connect, /* Handle connects */
364 &core_disconnect, /* remove peers on disconnects */
365 NULL, /* Don't notify about all incoming messages */
366 GNUNET_NO, /* For header only in notification */
367 NULL, /* Don't notify about all outbound messages */
368 GNUNET_NO, /* For header-only out notification */
369 core_handlers); /* Register these handlers */
378 * Core callback to write a pre-constructed data packet to core buffer
380 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
381 * @param size Number of bytes available in buf.
382 * @param buf Where the to write the message.
384 * @return number of bytes written to buf
387 send_core_data_raw (void *cls, size_t size, void *buf)
389 struct GNUNET_MessageHeader *msg = cls;
392 GNUNET_assert (NULL != msg);
393 total_size = ntohs (msg->size);
395 if (total_size > size)
400 memcpy (buf, msg, total_size);
407 * Function to send a create connection message to a peer.
409 * @param c Connection to create.
410 * @param size number of bytes available in buf
411 * @param buf where the callee should write the message
412 * @return number of bytes written to buf
415 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
417 struct GNUNET_MESH_ConnectionCreate *msg;
418 struct GNUNET_PeerIdentity *peer_ptr;
419 const struct MeshPeerPath *p = GMC_get_path (c);
423 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
425 sizeof (struct GNUNET_MESH_ConnectionCreate) +
426 p->length * sizeof (struct GNUNET_PeerIdentity);
428 if (size < size_needed || NULL == buf)
433 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
434 msg->header.size = htons (size_needed);
435 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
436 msg->cid = *GMC_get_id (c);
438 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
439 for (i = 0; i < p->length; i++)
441 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
444 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 "CONNECTION CREATE (%u bytes long) sent!\n",
452 * Creates a path ack message in buf and frees all unused resources.
454 * @param c Connection to send an ACK on.
455 * @param size number of bytes available in buf
456 * @param buf where the callee should write the message
458 * @return number of bytes written to buf
461 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
463 struct GNUNET_MESH_ConnectionACK *msg = buf;
465 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
466 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
471 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
472 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
473 msg->cid = *GMC_get_id (c);
476 /* TODO add signature */
478 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
479 return sizeof (struct GNUNET_MESH_ConnectionACK);
483 /******************************************************************************/
484 /******************************** STATIC ***********************************/
485 /******************************************************************************/
488 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
491 * @param key current key code
492 * @param value value in the hash map
493 * @return #GNUNET_YES if we should continue to iterate,
497 shutdown_tunnel (void *cls,
498 const struct GNUNET_PeerIdentity *key,
501 struct MeshPeer *p = value;
502 struct MeshTunnel3 *t = p->tunnel;
512 * Destroy the peer_info and free any allocated resources linked to it
514 * @param peer The peer_info to destroy.
516 * @return GNUNET_OK on success
519 peer_destroy (struct MeshPeer *peer)
521 struct GNUNET_PeerIdentity id;
522 struct MeshPeerPath *p;
523 struct MeshPeerPath *nextp;
525 GNUNET_PEER_resolve (peer->id, &id);
526 GNUNET_PEER_change_rc (peer->id, -1);
529 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
532 LOG (GNUNET_ERROR_TYPE_WARNING,
533 "removing peer %s, not in peermap\n", GNUNET_i2s (&id));
535 if (NULL != peer->search_h)
537 GMD_search_stop (peer->search_h);
543 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
547 GMT_destroy_empty (peer->tunnel);
554 * Returns if peer is used (has a tunnel, is neighbor).
556 * @param peer Peer to check.
558 * @return GNUNET_YES if peer is in use.
561 peer_is_used (struct MeshPeer *peer)
563 struct MeshPeerPath *p;
565 if (NULL != peer->tunnel)
568 for (p = peer->path_head; NULL != p; p = p->next)
578 * Iterator over all the peers to get the oldest timestamp.
580 * @param cls Closure (unsued).
581 * @param key ID of the peer.
582 * @param value Peer_Info of the peer.
585 peer_get_oldest (void *cls,
586 const struct GNUNET_PeerIdentity *key,
589 struct MeshPeer *p = value;
590 struct GNUNET_TIME_Absolute *abs = cls;
592 /* Don't count active peers */
593 if (GNUNET_YES == peer_is_used (p))
596 if (abs->abs_value_us < p->last_contact.abs_value_us)
597 abs->abs_value_us = p->last_contact.abs_value_us;
604 * Iterator over all the peers to remove the oldest entry.
606 * @param cls Closure (unsued).
607 * @param key ID of the peer.
608 * @param value Peer_Info of the peer.
611 peer_timeout (void *cls,
612 const struct GNUNET_PeerIdentity *key,
615 struct MeshPeer *p = value;
616 struct GNUNET_TIME_Absolute *abs = cls;
618 if (p->last_contact.abs_value_us == abs->abs_value_us &&
619 GNUNET_NO == peer_is_used (p))
629 * Delete oldest unused peer.
632 peer_delete_oldest (void)
634 struct GNUNET_TIME_Absolute abs;
636 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
638 GNUNET_CONTAINER_multipeermap_iterate (peers,
641 GNUNET_CONTAINER_multipeermap_iterate (peers,
648 * Choose the best path towards a peer considering the tunnel properties.
650 * @param peer The destination peer.
652 * @return Best current known path towards the peer, if any.
654 static struct MeshPeerPath *
655 peer_get_best_path (const struct MeshPeer *peer)
657 struct MeshPeerPath *best_p;
658 struct MeshPeerPath *p;
659 unsigned int best_cost;
662 best_cost = UINT_MAX;
664 for (p = peer->path_head; NULL != p; p = p->next)
666 if (GNUNET_YES == GMT_is_path_used (peer->tunnel, p))
667 continue; /* If path is already in use, skip it. */
669 if ((cost = GMT_get_path_cost (peer->tunnel, p)) < best_cost)
680 queue_is_sendable (struct MeshPeerQueue *q)
682 /* Is PID-independent? */
685 case GNUNET_MESSAGE_TYPE_MESH_ACK:
686 case GNUNET_MESSAGE_TYPE_MESH_POLL:
690 if (GMC_is_sendable (q->c, q->fwd))
698 * Get first sendable message.
700 * @param peer The destination peer.
702 * @return Best current known path towards the peer, if any.
704 static struct MeshPeerQueue *
705 peer_get_first_message (const struct MeshPeer *peer)
707 struct MeshPeerQueue *q;
709 for (q = peer->queue_head; NULL != q; q = q->next)
711 if (queue_is_sendable (q))
720 * Function to process paths received for a new peer addition. The recorded
721 * paths form the initial tunnel, which can be optimized later.
722 * Called on each result obtained for the DHT search.
728 search_handler (void *cls, const struct MeshPeerPath *path)
730 struct MeshPeer *peer = cls;
731 unsigned int connection_count;
733 GMP_add_path_to_all (path, GNUNET_NO);
735 /* Count connections */
736 connection_count = GMT_count_connections (peer->tunnel);
738 /* If we already have 3 (or more (?!)) connections, it's enough */
739 if (3 <= connection_count)
742 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (peer->tunnel))
744 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
752 * Free a transmission that was already queued with all resources
753 * associated to the request.
755 * @param queue Queue handler to cancel.
756 * @param clear_cls Is it necessary to free associated cls?
759 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
761 struct MeshPeer *peer;
764 GNUNET_assert (NULL != queue->c);
766 if (GNUNET_YES == clear_cls)
768 LOG (GNUNET_ERROR_TYPE_DEBUG, "# queue destroy type %s\n",
769 GNUNET_MESH_DEBUG_M2S (queue->type));
772 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
773 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
774 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
776 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
777 case GNUNET_MESSAGE_TYPE_MESH_ACK:
778 case GNUNET_MESSAGE_TYPE_MESH_POLL:
779 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
780 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
781 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
782 LOG (GNUNET_ERROR_TYPE_DEBUG, "# prebuilt message\n");;
783 GNUNET_free_non_null (queue->cls);
788 LOG (GNUNET_ERROR_TYPE_ERROR, "# type %s unknown!\n",
789 GNUNET_MESH_DEBUG_M2S (queue->type));
792 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
794 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
795 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
800 if (NULL != queue->callback)
802 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Calling callback\n");
803 queue->callback (queue->callback_cls,
804 queue->c, queue->type,
805 queue->fwd, queue->size,
806 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
813 * Core callback to write a queued packet to core buffer
815 * @param cls Closure (peer info).
816 * @param size Number of bytes available in buf.
817 * @param buf Where the to write the message.
819 * @return number of bytes written to buf
822 queue_send (void *cls, size_t size, void *buf)
824 struct MeshPeer *peer = cls;
825 struct MeshConnection *c;
826 struct MeshPeerQueue *queue;
827 const struct GNUNET_PeerIdentity *dst_id;
830 peer->core_transmit = NULL;
831 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
833 if (NULL == buf || 0 == size)
835 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
840 queue = peer_get_first_message (peer);
843 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
848 dst_id = GNUNET_PEER_resolve2 (peer->id);
849 LOG (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
850 /* Check if buffer size is enough for the message */
851 if (queue->size > size)
853 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
854 peer->core_transmit =
855 GNUNET_CORE_notify_transmit_ready (core_handle,
858 GNUNET_TIME_UNIT_FOREVER_REL,
865 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
870 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
871 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
872 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
873 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
874 case GNUNET_MESSAGE_TYPE_MESH_ACK:
875 case GNUNET_MESSAGE_TYPE_MESH_POLL:
876 LOG (GNUNET_ERROR_TYPE_DEBUG,
878 GNUNET_MESH_DEBUG_M2S (queue->type));
879 data_size = send_core_data_raw (queue->cls, size, buf);
881 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
882 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
883 if (GMC_is_origin (c, GNUNET_YES))
884 data_size = send_core_connection_create (queue->c, size, buf);
886 data_size = send_core_data_raw (queue->cls, size, buf);
888 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
889 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
890 if (GMC_is_origin (c, GNUNET_NO) ||
891 GMC_is_origin (c, GNUNET_YES))
892 data_size = send_core_connection_ack (queue->c, size, buf);
894 data_size = send_core_data_raw (queue->cls, size, buf);
896 case GNUNET_MESSAGE_TYPE_MESH_DATA:
897 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
898 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
899 /* This should be encapsulted */
905 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
910 if (0 < drop_percent &&
911 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
913 LOG (GNUNET_ERROR_TYPE_WARNING,
914 "Dropping message of type %s\n",
915 GNUNET_MESH_DEBUG_M2S (queue->type));
919 /* Free queue, but cls was freed by send_core_* */
920 queue_destroy (queue, GNUNET_NO);
922 /* If more data in queue, send next */
923 queue = peer_get_first_message (peer);
926 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
927 if (NULL == peer->core_transmit)
929 peer->core_transmit =
930 GNUNET_CORE_notify_transmit_ready(core_handle,
933 GNUNET_TIME_UNIT_FOREVER_REL,
938 queue->start_waiting = GNUNET_TIME_absolute_get ();
942 LOG (GNUNET_ERROR_TYPE_DEBUG,
943 "* tmt rdy called somewhere else\n");
945 // GMC_start_poll (); FIXME needed?
949 // GMC_stop_poll(); FIXME needed?
952 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
957 /******************************************************************************/
958 /******************************** API ***********************************/
959 /******************************************************************************/
962 * @brief Queue and pass message to core when possible.
964 * @param peer Peer towards which to queue the message.
965 * @param cls Closure (@c type dependant). It will be used by queue_send to
966 * build the message to be sent if not already prebuilt.
967 * @param type Type of the message, 0 for a raw message.
968 * @param size Size of the message.
969 * @param c Connection this message belongs to (cannot be NULL).
970 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
971 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
972 * @param cont Continuation to be called once CORE has taken the message.
973 * @param cont_cls Closure for @c cont.
976 GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
977 struct MeshConnection *c, struct MeshChannel *ch, int fwd,
978 GMP_sent cont, void *cont_cls)
980 struct MeshPeerQueue *queue;
984 LOG (GNUNET_ERROR_TYPE_DEBUG,
985 "queue add %s %s (%u) on c %p, ch %p\n",
986 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
987 GNUNET_assert (NULL != c);
989 if (NULL == peer->connections)
991 /* We are not connected to this peer, ignore request. */
998 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
999 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1004 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1006 call_core = GMC_is_sendable (c, fwd);
1007 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1015 queue->callback = cont;
1016 queue->callback_cls = cont_cls;
1017 if (100 <= priority)
1019 struct MeshPeerQueue *copy;
1020 struct MeshPeerQueue *next;
1022 for (copy = peer->queue_head; NULL != copy; copy = next)
1025 if (copy->type == type && copy->c == c && copy->fwd == fwd)
1027 /* Example: also a FWD ACK for connection XYZ */
1028 queue_destroy (copy, GNUNET_YES);
1031 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1035 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1039 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1041 LOG (GNUNET_ERROR_TYPE_DEBUG,
1042 "calling core tmt rdy towards %s for %u bytes\n",
1043 GMP_2s (peer), size);
1044 peer->core_transmit =
1045 GNUNET_CORE_notify_transmit_ready (core_handle,
1048 GNUNET_TIME_UNIT_FOREVER_REL,
1049 GNUNET_PEER_resolve2 (peer->id),
1053 queue->start_waiting = GNUNET_TIME_absolute_get ();
1057 LOG (GNUNET_ERROR_TYPE_DEBUG,
1058 "core tmt rdy towards %s already called\n",
1066 * Cancel all queued messages to a peer that belong to a certain connection.
1068 * @param peer Peer towards whom to cancel.
1069 * @param c Connection whose queued messages to cancel.
1072 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1074 struct MeshPeerQueue *q;
1075 struct MeshPeerQueue *next;
1077 for (q = peer->queue_head; NULL != q; q = next)
1082 LOG (GNUNET_ERROR_TYPE_DEBUG,
1083 "connection_cancel_queue %s\n",
1084 GNUNET_MESH_DEBUG_M2S (q->type));
1085 queue_destroy (q, GNUNET_YES);
1088 if (NULL == peer->queue_head)
1090 if (NULL != peer->core_transmit)
1092 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1093 peer->core_transmit = NULL;
1100 * Get the first transmittable message for a connection.
1102 * @param peer Neighboring peer.
1103 * @param c Connection.
1105 * @return First transmittable message.
1107 static struct MeshPeerQueue *
1108 connection_get_first_message (struct MeshPeer *peer, struct MeshConnection *c)
1110 struct MeshPeerQueue *q;
1112 for (q = peer->queue_head; NULL != q; q = q->next)
1116 if (queue_is_sendable (q))
1124 GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
1126 struct MeshPeerQueue *q;
1129 if (NULL != peer->core_transmit)
1131 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1132 return; /* Already unlocked */
1135 q = connection_get_first_message (peer, c);
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1139 return; /* Nothing to transmit */
1143 peer->core_transmit =
1144 GNUNET_CORE_notify_transmit_ready (core_handle,
1147 GNUNET_TIME_UNIT_FOREVER_REL,
1148 GNUNET_PEER_resolve2 (peer->id),
1156 * Initialize the peer subsystem.
1158 * @param c Configuration.
1159 * @param id Peer identity
1162 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1164 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1165 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1167 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1170 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1171 "MESH", "MAX_PEERS", "USING DEFAULT");
1176 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1183 LOG (GNUNET_ERROR_TYPE_WARNING,
1184 "\n***************************************\n"
1185 "Mesh is running with drop mode enabled.\n"
1186 "This is NOT a good idea!\n"
1187 "Remove the DROP_PERCENT option from your configuration.\n"
1188 "***************************************\n");
1191 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1192 NULL, /* Closure passed to MESH functions */
1193 &core_init, /* Call core_init once connected */
1194 &core_connect, /* Handle connects */
1195 &core_disconnect, /* remove peers on disconnects */
1196 NULL, /* Don't notify about all incoming messages */
1197 GNUNET_NO, /* For header only in notification */
1198 NULL, /* Don't notify about all outbound messages */
1199 GNUNET_NO, /* For header-only out notification */
1200 core_handlers); /* Register these handlers */
1201 if (NULL == core_handle)
1204 GNUNET_SCHEDULER_shutdown ();
1210 * Shut down the peer subsystem.
1215 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1217 if (core_handle != NULL)
1219 GNUNET_CORE_disconnect (core_handle);
1222 GNUNET_PEER_change_rc (myid, -1);
1226 * Retrieve the MeshPeer stucture associated with the peer, create one
1227 * and insert it in the appropriate structures if the peer is not known yet.
1229 * @param peer_id Full identity of the peer.
1231 * @return Existing or newly created peer structure.
1234 GMP_get (const struct GNUNET_PeerIdentity *peer_id)
1236 struct MeshPeer *peer;
1238 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1241 peer = GNUNET_new (struct MeshPeer);
1242 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1244 peer_delete_oldest ();
1246 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1247 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1248 peer->id = GNUNET_PEER_intern (peer_id);
1250 peer->last_contact = GNUNET_TIME_absolute_get();
1257 * Retrieve the MeshPeer stucture associated with the peer, create one
1258 * and insert it in the appropriate structures if the peer is not known yet.
1260 * @param peer Short identity of the peer.
1262 * @return Existing or newly created peer structure.
1265 GMP_get_short (const GNUNET_PEER_Id peer)
1267 return GMP_get (GNUNET_PEER_resolve2 (peer));
1272 * Try to establish a new connection to this peer (in its tunnel).
1273 * If the peer doesn't have any path to it yet, try to get one.
1274 * If the peer already has some path, send a CREATE CONNECTION towards it.
1276 * @param peer Peer to connect to.
1279 GMP_connect (struct MeshPeer *peer)
1281 struct MeshTunnel3 *t;
1282 struct MeshPeerPath *p;
1283 struct MeshConnection *c;
1286 LOG (GNUNET_ERROR_TYPE_DEBUG,
1287 "peer_connect towards %s\n",
1291 rerun_search = GNUNET_NO;
1293 if (NULL != peer->path_head)
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
1296 p = peer_get_best_path (peer);
1299 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1300 c = GMT_use_path (t, p);
1303 /* This case can happen when the path includes a first hop that is
1304 * not yet known to be connected.
1306 * This happens quite often during testing when running mesh
1307 * under valgrind: core connect notifications come very late and the
1308 * DHT result has already come and created a valid path.
1309 * In this case, the peer->connections hashmap will be NULL and
1310 * tunnel_use_path will not be able to create a connection from that
1313 * Re-running the DHT GET should give core time to callback.
1316 rerun_search = GNUNET_YES;
1320 GMC_send_create (c);
1326 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1328 GMD_search_stop (peer->search_h);
1329 peer->search_h = NULL;
1330 LOG (GNUNET_ERROR_TYPE_DEBUG,
1331 " Stopping DHT GET for peer %s\n",
1335 if (NULL == peer->search_h)
1337 const struct GNUNET_PeerIdentity *id;
1339 id = GNUNET_PEER_resolve2 (peer->id);
1340 LOG (GNUNET_ERROR_TYPE_DEBUG,
1341 " Starting DHT GET for peer %s\n", GMP_2s (peer));
1342 peer->search_h = GMD_search (id, &search_handler, peer);
1343 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
1344 GMT_change_state (t, MESH_TUNNEL3_SEARCHING);
1356 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel3 *t)
1363 * Chech whether there is a direct (core level) connection to peer.
1365 * @param peer Peer to check.
1367 * @return GNUNET_YES if there is a direct connection.
1370 GMP_is_neighbor (const struct MeshPeer *peer)
1372 struct MeshPeerPath *path;
1374 if (NULL == peer->connections)
1377 for (path = peer->path_head; NULL != path; path = path->next)
1379 if (3 > path->length)
1383 GNUNET_break (0); /* Is not a neighbor but connections is not NULL */
1389 * Create and initialize a new tunnel towards a peer, in case it has none.
1390 * In case the peer already has a tunnel, nothing is done.
1392 * Does not generate any traffic, just creates the local data structures.
1394 * @param peer Peer towards which to create the tunnel.
1397 GMP_add_tunnel (struct MeshPeer *peer)
1399 if (NULL != peer->tunnel)
1401 peer->tunnel = GMT_new (peer);
1406 * Add a connection to a neighboring peer.
1408 * Store that the peer is the first hop of the connection in one
1409 * direction and that on peer disconnect the connection must be
1410 * notified and destroyed, for it will no longer be valid.
1412 * @param peer Peer to add connection to.
1413 * @param c Connection to add.
1415 * @return GNUNET_OK on success.
1418 GMP_add_connection (struct MeshPeer *peer,
1419 struct MeshConnection *c)
1421 if (NULL == peer->connections)
1424 return GNUNET_SYSERR;
1426 return GNUNET_CONTAINER_multihashmap_put (peer->connections,
1429 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1434 * Add the path to the peer and update the path used to reach it in case this
1437 * @param peer_info Destination peer to add the path to.
1438 * @param path New path to add. Last peer must be the peer in arg 1.
1439 * Path will be either used of freed if already known.
1440 * @param trusted Do we trust that this path is real?
1443 GMP_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
1446 struct MeshPeerPath *aux;
1450 if ((NULL == peer_info) || (NULL == path))
1453 path_destroy (path);
1456 if (path->peers[path->length - 1] != peer_info->id)
1459 path_destroy (path);
1462 if (2 >= path->length && GNUNET_NO == trusted)
1464 /* Only allow CORE to tell us about direct paths */
1465 path_destroy (path);
1468 for (l = 1; l < path->length; l++)
1470 if (path->peers[l] == myid)
1472 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1473 for (l2 = 0; l2 < path->length - l; l2++)
1475 path->peers[l2] = path->peers[l + l2];
1480 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1484 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1485 path->length, GMP_2s (peer_info));
1487 l = path_get_length (path);
1490 path_destroy (path);
1494 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1495 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1497 l2 = path_get_length (aux);
1500 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1501 peer_info->path_tail, aux, path);
1506 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1508 path_destroy (path);
1513 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1520 * Add the path to the origin peer and update the path used to reach it in case
1521 * this is the shortest.
1522 * The path is given in peer_info -> destination, therefore we turn the path
1523 * upside down first.
1525 * @param peer Peer to add the path to, being the origin of the path.
1526 * @param path New path to add after being inversed.
1527 * Path will be either used or freed.
1528 * @param trusted Do we trust that this path is real?
1531 GMP_add_path_to_origin (struct MeshPeer *peer,
1532 struct MeshPeerPath *path,
1538 GMP_add_path (peer, path, trusted);
1543 * Adds a path to the info of all the peers in the path
1545 * @param p Path to process.
1546 * @param confirmed Whether we know if the path works or not.
1549 GMP_add_path_to_all (const struct MeshPeerPath *p, int confirmed)
1553 /* TODO: invert and add */
1554 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1555 for (i++; i < p->length; i++)
1557 struct MeshPeer *aux;
1558 struct MeshPeerPath *copy;
1560 aux = GMP_get_short (p->peers[i]);
1561 copy = path_duplicate (p);
1562 copy->length = i + 1;
1563 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1569 * Remove a connection from a neighboring peer.
1571 * @param peer Peer to remove connection from.
1572 * @param c Connection to remove.
1574 * @return GNUNET_OK on success.
1577 GMP_remove_connection (struct MeshPeer *peer,
1578 const struct MeshConnection *c)
1580 if (NULL == peer || NULL == peer->connections)
1583 LOG (GNUNET_ERROR_TYPE_DEBUG,
1584 "Peer %s is not a neighbor!\n",
1586 return GNUNET_SYSERR;
1588 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1594 * Start the DHT search for new paths towards the peer: we don't have
1595 * enough good connections.
1597 * @param peer Destination peer.
1600 GMP_start_search (struct MeshPeer *peer)
1602 if (NULL != peer->search_h)
1608 peer->search_h = GMD_search (GMP_get_id (peer), &search_handler, peer);
1613 * Stop the DHT search for new paths towards the peer: we already have
1614 * enough good connections.
1616 * @param peer Destination peer.
1619 GMP_stop_search (struct MeshPeer *peer)
1621 if (NULL == peer->search_h)
1627 GMD_search_stop (peer->search_h);
1628 peer->search_h = NULL;
1633 * Get the Full ID of a peer.
1635 * @param peer Peer to get from.
1637 * @return Full ID of peer.
1639 const struct GNUNET_PeerIdentity *
1640 GMP_get_id (const struct MeshPeer *peer)
1642 return GNUNET_PEER_resolve2 (peer->id);
1647 * Get the Short ID of a peer.
1649 * @param peer Peer to get from.
1651 * @return Short ID of peer.
1654 GMP_get_short_id (const struct MeshPeer *peer)
1661 * Get the tunnel towards a peer.
1663 * @param peer Peer to get from.
1665 * @return Tunnel towards peer.
1667 struct MeshTunnel3 *
1668 GMP_get_tunnel (const struct MeshPeer *peer)
1670 return peer->tunnel;
1675 * Get the static string for a peer ID.
1679 * @return Static string for it's ID.
1682 GMP_2s (const struct MeshPeer *peer)
1686 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));