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, &my_full_id);
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] = pi->id;
266 GNUNET_PEER_change_rc (pi->id, 1);
267 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
269 path->peers[0] = myid;
270 GNUNET_PEER_change_rc (myid, 1);
271 peer_add_path (pi, path, GNUNET_YES);
273 pi->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_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
336 {&GMC_handle_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
342 * To be called on core init/fail.
344 * @param cls Closure (config)
345 * @param identity the public identity of this peer
348 core_init (void *cls,
349 const struct GNUNET_PeerIdentity *identity)
351 const struct GNUNET_CONFIGURATION_Handle *c = cls;
354 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
355 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
357 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
358 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
359 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
360 GNUNET_CORE_disconnect (core_handle);
361 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
362 NULL, /* Closure passed to MESH functions */
363 &core_init, /* Call core_init once connected */
364 &core_connect, /* Handle connects */
365 &core_disconnect, /* remove peers on disconnects */
366 NULL, /* Don't notify about all incoming messages */
367 GNUNET_NO, /* For header only in notification */
368 NULL, /* Don't notify about all outbound messages */
369 GNUNET_NO, /* For header-only out notification */
370 core_handlers); /* Register these handlers */
379 * Core callback to write a pre-constructed data packet to core buffer
381 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
382 * @param size Number of bytes available in buf.
383 * @param buf Where the to write the message.
385 * @return number of bytes written to buf
388 send_core_data_raw (void *cls, size_t size, void *buf)
390 struct GNUNET_MessageHeader *msg = cls;
393 GNUNET_assert (NULL != msg);
394 total_size = ntohs (msg->size);
396 if (total_size > size)
401 memcpy (buf, msg, total_size);
408 * Function to send a create connection message to a peer.
410 * @param c Connection to create.
411 * @param size number of bytes available in buf
412 * @param buf where the callee should write the message
413 * @return number of bytes written to buf
416 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
418 struct GNUNET_MESH_ConnectionCreate *msg;
419 struct GNUNET_PeerIdentity *peer_ptr;
420 const struct MeshPeerPath *p = GMC_get_path (c);
424 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
426 sizeof (struct GNUNET_MESH_ConnectionCreate) +
427 p->length * sizeof (struct GNUNET_PeerIdentity);
429 if (size < size_needed || NULL == buf)
434 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
435 msg->header.size = htons (size_needed);
436 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
437 msg->cid = *GMC_get_id (c);
439 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
440 for (i = 0; i < p->length; i++)
442 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
445 LOG (GNUNET_ERROR_TYPE_DEBUG,
446 "CONNECTION CREATE (%u bytes long) sent!\n",
453 * Creates a path ack message in buf and frees all unused resources.
455 * @param c Connection to send an ACK on.
456 * @param size number of bytes available in buf
457 * @param buf where the callee should write the message
459 * @return number of bytes written to buf
462 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
464 struct GNUNET_MESH_ConnectionACK *msg = buf;
466 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
467 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
472 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
473 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
474 msg->cid = *GMC_get_id (c);
477 /* TODO add signature */
479 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
480 return sizeof (struct GNUNET_MESH_ConnectionACK);
484 /******************************************************************************/
485 /******************************** STATIC ***********************************/
486 /******************************************************************************/
489 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
492 * @param key current key code
493 * @param value value in the hash map
494 * @return #GNUNET_YES if we should continue to iterate,
498 shutdown_tunnel (void *cls,
499 const struct GNUNET_PeerIdentity *key,
502 struct MeshPeer *p = value;
503 struct MeshTunnel3 *t = p->tunnel;
513 * Destroy the peer_info and free any allocated resources linked to it
515 * @param peer The peer_info to destroy.
517 * @return GNUNET_OK on success
520 peer_destroy (struct MeshPeer *peer)
522 struct GNUNET_PeerIdentity id;
523 struct MeshPeerPath *p;
524 struct MeshPeerPath *nextp;
526 GNUNET_PEER_resolve (peer->id, &id);
527 GNUNET_PEER_change_rc (peer->id, -1);
530 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
533 LOG (GNUNET_ERROR_TYPE_WARNING,
534 "removing peer %s, not in peermap\n", GNUNET_i2s (&id));
536 if (NULL != peer->search_h)
538 GMD_search_stop (peer->search_h);
544 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
548 GMT_destroy_empty (peer->tunnel);
555 * Returns if peer is used (has a tunnel, is neighbor).
557 * @peer Peer to check.
559 * @return GNUNET_YES if peer is in use.
562 peer_is_used (struct MeshPeer *peer)
564 struct MeshPeerPath *p;
566 if (NULL != peer->tunnel)
569 for (p = peer->path_head; NULL != p; p = p->next)
579 * Iterator over all the peers to get the oldest timestamp.
581 * @param cls Closure (unsued).
582 * @param key ID of the peer.
583 * @param value Peer_Info of the peer.
586 peer_get_oldest (void *cls,
587 const struct GNUNET_PeerIdentity *key,
590 struct MeshPeer *p = value;
591 struct GNUNET_TIME_Absolute *abs = cls;
593 /* Don't count active peers */
594 if (GNUNET_YES == peer_is_used (p))
597 if (abs->abs_value_us < p->last_contact.abs_value_us)
598 abs->abs_value_us = p->last_contact.abs_value_us;
605 * Iterator over all the peers to remove the oldest entry.
607 * @param cls Closure (unsued).
608 * @param key ID of the peer.
609 * @param value Peer_Info of the peer.
612 peer_timeout (void *cls,
613 const struct GNUNET_PeerIdentity *key,
616 struct MeshPeer *p = value;
617 struct GNUNET_TIME_Absolute *abs = cls;
619 if (p->last_contact.abs_value_us == abs->abs_value_us &&
620 GNUNET_NO == peer_is_used (p))
630 * Delete oldest unused peer.
633 peer_delete_oldest (void)
635 struct GNUNET_TIME_Absolute abs;
637 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
639 GNUNET_CONTAINER_multipeermap_iterate (peers,
642 GNUNET_CONTAINER_multipeermap_iterate (peers,
649 * Choose the best path towards a peer considering the tunnel properties.
651 * @param peer The destination peer.
653 * @return Best current known path towards the peer, if any.
655 static struct MeshPeerPath *
656 peer_get_best_path (const struct MeshPeer *peer)
658 struct MeshPeerPath *best_p;
659 struct MeshPeerPath *p;
660 unsigned int best_cost;
663 best_cost = UINT_MAX;
665 for (p = peer->path_head; NULL != p; p = p->next)
667 if (GNUNET_YES == GMT_is_path_used (peer->tunnel, p))
668 continue; /* If path is already in use, skip it. */
670 if ((cost = GMT_get_path_cost (peer->tunnel, p)) < best_cost)
681 queue_is_sendable (struct MeshPeerQueue *q)
683 /* Is PID-independent? */
686 case GNUNET_MESSAGE_TYPE_MESH_ACK:
687 case GNUNET_MESSAGE_TYPE_MESH_POLL:
691 if (GMC_is_sendable (q->c, q->fwd))
699 * Get first sendable message.
701 * @param peer The destination peer.
703 * @return Best current known path towards the peer, if any.
705 static struct MeshPeerQueue *
706 peer_get_first_message (const struct MeshPeer *peer)
708 struct MeshPeerQueue *q;
710 for (q = peer->queue_head; NULL != q; q = q->next)
712 if (queue_is_sendable (q))
721 * Function to process paths received for a new peer addition. The recorded
722 * paths form the initial tunnel, which can be optimized later.
723 * Called on each result obtained for the DHT search.
729 search_handler (void *cls, const struct MeshPeerPath *path)
731 struct MeshPeer *peer = cls;
732 unsigned int connection_count;
734 GMP_add_path_to_all (path, GNUNET_NO);
736 /* Count connections */
737 connection_count = GMT_count_connections (peer->tunnel);
739 /* If we already have 3 (or more (?!)) connections, it's enough */
740 if (3 <= connection_count)
743 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (peer->tunnel))
745 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
753 * Free a transmission that was already queued with all resources
754 * associated to the request.
756 * @param queue Queue handler to cancel.
757 * @param clear_cls Is it necessary to free associated cls?
760 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
762 struct MeshPeer *peer;
765 GNUNET_assert (NULL != queue->c);
767 if (GNUNET_YES == clear_cls)
769 LOG (GNUNET_ERROR_TYPE_DEBUG, "# queue destroy type %s\n",
770 GNUNET_MESH_DEBUG_M2S (queue->type));
773 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
774 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
775 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
777 case GNUNET_MESSAGE_TYPE_MESH_FWD:
778 case GNUNET_MESSAGE_TYPE_MESH_BCK:
779 case GNUNET_MESSAGE_TYPE_MESH_ACK:
780 case GNUNET_MESSAGE_TYPE_MESH_POLL:
781 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
782 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
783 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
784 LOG (GNUNET_ERROR_TYPE_DEBUG, "# prebuilt message\n");;
785 GNUNET_free_non_null (queue->cls);
790 LOG (GNUNET_ERROR_TYPE_ERROR, "# type %s unknown!\n",
791 GNUNET_MESH_DEBUG_M2S (queue->type));
794 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
796 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
797 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
802 if (NULL != queue->callback)
804 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Calling callback\n");
805 queue->callback (queue->callback_cls,
806 queue->c, queue->type,
807 queue->fwd, queue->size,
808 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
815 * Core callback to write a queued packet to core buffer
817 * @param cls Closure (peer info).
818 * @param size Number of bytes available in buf.
819 * @param buf Where the to write the message.
821 * @return number of bytes written to buf
824 queue_send (void *cls, size_t size, void *buf)
826 struct MeshPeer *peer = cls;
827 struct MeshConnection *c;
828 struct MeshPeerQueue *queue;
829 const struct GNUNET_PeerIdentity *dst_id;
832 peer->core_transmit = NULL;
833 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
835 if (NULL == buf || 0 == size)
837 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
842 queue = peer_get_first_message (peer);
845 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
850 dst_id = GNUNET_PEER_resolve2 (peer->id);
851 LOG (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
852 /* Check if buffer size is enough for the message */
853 if (queue->size > size)
855 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
856 peer->core_transmit =
857 GNUNET_CORE_notify_transmit_ready (core_handle,
860 GNUNET_TIME_UNIT_FOREVER_REL,
867 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
872 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
873 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
874 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
875 case GNUNET_MESSAGE_TYPE_MESH_FWD:
876 case GNUNET_MESSAGE_TYPE_MESH_BCK:
877 case GNUNET_MESSAGE_TYPE_MESH_ACK:
878 case GNUNET_MESSAGE_TYPE_MESH_POLL:
879 LOG (GNUNET_ERROR_TYPE_DEBUG,
881 GNUNET_MESH_DEBUG_M2S (queue->type));
882 data_size = send_core_data_raw (queue->cls, size, buf);
884 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
885 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
886 if (GMC_is_origin (c, GNUNET_YES))
887 data_size = send_core_connection_create (queue->c, size, buf);
889 data_size = send_core_data_raw (queue->cls, size, buf);
891 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
892 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
893 if (GMC_is_origin (c, GNUNET_NO) ||
894 GMC_is_origin (c, GNUNET_YES))
895 data_size = send_core_connection_ack (queue->c, size, buf);
897 data_size = send_core_data_raw (queue->cls, size, buf);
899 case GNUNET_MESSAGE_TYPE_MESH_DATA:
900 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
901 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
902 /* This should be encapsulted */
908 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
913 if (0 < drop_percent &&
914 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
916 LOG (GNUNET_ERROR_TYPE_WARNING,
917 "Dropping message of type %s\n",
918 GNUNET_MESH_DEBUG_M2S (queue->type));
922 /* Free queue, but cls was freed by send_core_* */
923 queue_destroy (queue, GNUNET_NO);
925 /* If more data in queue, send next */
926 queue = peer_get_first_message (peer);
929 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
930 if (NULL == peer->core_transmit)
932 peer->core_transmit =
933 GNUNET_CORE_notify_transmit_ready(core_handle,
936 GNUNET_TIME_UNIT_FOREVER_REL,
941 queue->start_waiting = GNUNET_TIME_absolute_get ();
945 LOG (GNUNET_ERROR_TYPE_DEBUG,
946 "* tmt rdy called somewhere else\n");
948 // GMC_start_poll (); FIXME needed?
952 // GMC_stop_poll(); FIXME needed?
955 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
960 /******************************************************************************/
961 /******************************** API ***********************************/
962 /******************************************************************************/
965 * @brief Queue and pass message to core when possible.
967 * @param peer Peer towards which to queue the message.
968 * @param cls Closure (@c type dependant). It will be used by queue_send to
969 * build the message to be sent if not already prebuilt.
970 * @param type Type of the message, 0 for a raw message.
971 * @param size Size of the message.
972 * @param c Connection this message belongs to (cannot be NULL).
973 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
974 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
975 * @param cont Continuation to be called once CORE has taken the message.
976 * @param cont_cls Closure for @c cont.
979 GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
980 struct MeshConnection *c, struct MeshChannel *ch, int fwd,
981 GMP_sent cont, void *cont_cls)
983 struct MeshPeerQueue *queue;
987 LOG (GNUNET_ERROR_TYPE_DEBUG,
988 "queue add %s %s (%u) on c %p, ch %p\n",
989 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
990 GNUNET_assert (NULL != c);
992 if (NULL == peer->connections)
994 /* We are not connected to this peer, ignore request. */
1001 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1002 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1007 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1009 call_core = GMC_is_sendable (c, fwd);
1010 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1018 queue->callback = cont;
1019 queue->callback_cls = cont_cls;
1020 if (100 <= priority)
1022 struct MeshPeerQueue *copy;
1023 struct MeshPeerQueue *next;
1025 for (copy = peer->queue_head; NULL != copy; copy = next)
1028 if (copy->type == type && copy->c == c && copy->fwd == fwd)
1030 /* Example: also a FWD ACK for connection XYZ */
1031 queue_destroy (copy, GNUNET_YES);
1034 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1038 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1042 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1044 LOG (GNUNET_ERROR_TYPE_DEBUG,
1045 "calling core tmt rdy towards %s for %u bytes\n",
1046 GMP_2s (peer), size);
1047 peer->core_transmit =
1048 GNUNET_CORE_notify_transmit_ready (core_handle,
1051 GNUNET_TIME_UNIT_FOREVER_REL,
1052 GNUNET_PEER_resolve2 (peer->id),
1056 queue->start_waiting = GNUNET_TIME_absolute_get ();
1060 LOG (GNUNET_ERROR_TYPE_DEBUG,
1061 "core tmt rdy towards %s already called\n",
1069 * Cancel all queued messages to a peer that belong to a certain connection.
1071 * @param peer Peer towards whom to cancel.
1072 * @param c Connection whose queued messages to cancel.
1075 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1077 struct MeshPeerQueue *q;
1078 struct MeshPeerQueue *next;
1080 for (q = peer->queue_head; NULL != q; q = next)
1085 LOG (GNUNET_ERROR_TYPE_DEBUG,
1086 "connection_cancel_queue %s\n",
1087 GNUNET_MESH_DEBUG_M2S (q->type));
1088 queue_destroy (q, GNUNET_YES);
1091 if (NULL == peer->queue_head)
1093 if (NULL != peer->core_transmit)
1095 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1096 peer->core_transmit = NULL;
1103 * Get the first transmittable message for a connection.
1105 * @param peer Neighboring peer.
1106 * @param c Connection.
1108 * @return First transmittable message.
1110 static struct MeshPeerQueue *
1111 connection_get_first_message (struct MeshPeer *peer, struct MeshConnection *c)
1113 struct MeshPeerQueue *q;
1115 for (q = peer->queue_head; NULL != q; q = q->next)
1119 if (queue_is_sendable (q))
1127 GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
1129 struct MeshPeerQueue *q;
1132 if (NULL != peer->core_transmit)
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1135 return; /* Already unlocked */
1138 q = connection_get_first_message (peer, c);
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1142 return; /* Nothing to transmit */
1146 peer->core_transmit =
1147 GNUNET_CORE_notify_transmit_ready (core_handle,
1150 GNUNET_TIME_UNIT_FOREVER_REL,
1151 GNUNET_PEER_resolve2 (peer->id),
1159 * Initialize the peer subsystem.
1161 * @param c Configuration.
1162 * @param id Peer identity
1165 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1167 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1169 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1172 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1173 "MESH", "MAX_PEERS", "USING DEFAULT");
1178 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1185 LOG (GNUNET_ERROR_TYPE_WARNING,
1186 "\n***************************************\n"
1187 "Mesh is running with drop mode enabled.\n"
1188 "This is NOT a good idea!\n"
1189 "Remove the DROP_PERCENT option from your configuration.\n"
1190 "***************************************\n");
1193 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1194 NULL, /* Closure passed to MESH functions */
1195 &core_init, /* Call core_init once connected */
1196 &core_connect, /* Handle connects */
1197 &core_disconnect, /* remove peers on disconnects */
1198 NULL, /* Don't notify about all incoming messages */
1199 GNUNET_NO, /* For header only in notification */
1200 NULL, /* Don't notify about all outbound messages */
1201 GNUNET_NO, /* For header-only out notification */
1202 core_handlers); /* Register these handlers */
1203 if (NULL == core_handle)
1206 GNUNET_SCHEDULER_shutdown ();
1212 * Shut down the peer subsystem.
1217 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1219 if (core_handle != NULL)
1221 GNUNET_CORE_disconnect (core_handle);
1224 GNUNET_PEER_change_rc (myid, -1);
1228 * Retrieve the MeshPeer stucture associated with the peer, create one
1229 * and insert it in the appropriate structures if the peer is not known yet.
1231 * @param peer Full identity of the peer.
1233 * @return Existing or newly created peer structure.
1236 GMP_get (const struct GNUNET_PeerIdentity *peer_id)
1238 struct MeshPeer *peer;
1240 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1243 peer = GNUNET_new (struct MeshPeer);
1244 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1246 peer_delete_oldest ();
1248 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1249 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1250 peer->id = GNUNET_PEER_intern (peer_id);
1252 peer->last_contact = GNUNET_TIME_absolute_get();
1259 * Retrieve the MeshPeer stucture associated with the peer, create one
1260 * and insert it in the appropriate structures if the peer is not known yet.
1262 * @param peer Short identity of the peer.
1264 * @return Existing or newly created peer structure.
1267 GMP_get_short (const GNUNET_PEER_Id peer)
1269 return GMP_get (GNUNET_PEER_resolve2 (peer));
1274 * Try to establish a new connection to this peer (in its tunnel).
1275 * If the peer doesn't have any path to it yet, try to get one.
1276 * If the peer already has some path, send a CREATE CONNECTION towards it.
1278 * @param peer Peer to connect to.
1281 GMP_connect (struct MeshPeer *peer)
1283 struct MeshTunnel3 *t;
1284 struct MeshPeerPath *p;
1285 struct MeshConnection *c;
1288 LOG (GNUNET_ERROR_TYPE_DEBUG,
1289 "peer_connect towards %s\n",
1293 rerun_search = GNUNET_NO;
1295 if (NULL != peer->path_head)
1297 LOG (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
1298 p = peer_get_best_path (peer);
1301 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1302 c = GMT_use_path (t, p);
1305 /* This case can happen when the path includes a first hop that is
1306 * not yet known to be connected.
1308 * This happens quite often during testing when running mesh
1309 * under valgrind: core connect notifications come very late and the
1310 * DHT result has already come and created a valid path.
1311 * In this case, the peer->connections hashmap will be NULL and
1312 * tunnel_use_path will not be able to create a connection from that
1315 * Re-running the DHT GET should give core time to callback.
1318 rerun_search = GNUNET_YES;
1322 GMC_send_create (c);
1328 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1330 GMD_search_stop (peer->search_h);
1331 peer->search_h = NULL;
1332 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 " Stopping DHT GET for peer %s\n",
1337 if (NULL == peer->search_h)
1339 const struct GNUNET_PeerIdentity *id;
1341 id = GNUNET_PEER_resolve2 (peer->id);
1342 LOG (GNUNET_ERROR_TYPE_DEBUG,
1343 " Starting DHT GET for peer %s\n", GMP_2s (peer));
1344 peer->search_h = GMD_search (id, &search_handler, peer);
1345 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
1346 GMT_change_state (t, MESH_TUNNEL3_SEARCHING);
1358 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel3 *t)
1365 * Chech whether there is a direct (core level) connection to peer.
1367 * @param peer Peer to check.
1369 * @return GNUNET_YES if there is a direct connection.
1372 GMP_is_neighbor (const struct MeshPeer *peer)
1374 struct MeshPeerPath *path;
1376 if (NULL == peer->connections)
1379 for (path = peer->path_head; NULL != path; path = path->next)
1381 if (3 > path->length)
1385 GNUNET_break (0); /* Is not a neighbor but connections is not NULL */
1391 * Create and initialize a new tunnel towards a peer, in case it has none.
1393 * Does not generate any traffic, just creates the local data structures.
1395 * @param peer Peer towards which to create the tunnel.
1398 GMP_add_tunnel (struct MeshPeer *peer)
1400 if (NULL != peer->tunnel)
1402 peer->tunnel = GMT_new (peer);
1407 * Add a connection to a neighboring peer.
1409 * Store that the peer is the first hop of the connection in one
1410 * direction and that on peer disconnect the connection must be
1411 * notified and destroyed, for it will no longer be valid.
1413 * @param peer Peer to add connection to.
1414 * @param c Connection to add.
1416 * @return GNUNET_OK on success.
1419 GMP_add_connection (struct MeshPeer *peer,
1420 struct MeshConnection *c)
1422 if (NULL == peer->connections)
1425 return GNUNET_SYSERR;
1427 return GNUNET_CONTAINER_multihashmap_put (peer->connections,
1430 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1435 * Add the path to the peer and update the path used to reach it in case this
1438 * @param peer_info Destination peer to add the path to.
1439 * @param path New path to add. Last peer must be the peer in arg 1.
1440 * Path will be either used of freed if already known.
1441 * @param trusted Do we trust that this path is real?
1444 GMP_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
1447 struct MeshPeerPath *aux;
1451 if ((NULL == peer_info) || (NULL == path))
1454 path_destroy (path);
1457 if (path->peers[path->length - 1] != peer_info->id)
1460 path_destroy (path);
1463 if (2 >= path->length && GNUNET_NO == trusted)
1465 /* Only allow CORE to tell us about direct paths */
1466 path_destroy (path);
1469 for (l = 1; l < path->length; l++)
1471 if (path->peers[l] == myid)
1473 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1474 for (l2 = 0; l2 < path->length - l; l2++)
1476 path->peers[l2] = path->peers[l + l2];
1481 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1486 path->length, GMP_2s (peer_info));
1488 l = path_get_length (path);
1491 path_destroy (path);
1495 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1496 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1498 l2 = path_get_length (aux);
1501 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1502 peer_info->path_tail, aux, path);
1507 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1509 path_destroy (path);
1514 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1521 * Add the path to the origin peer and update the path used to reach it in case
1522 * this is the shortest.
1523 * The path is given in peer_info -> destination, therefore we turn the path
1524 * upside down first.
1526 * @param peer_info Peer to add the path to, being the origin of the path.
1527 * @param path New path to add after being inversed.
1528 * Path will be either used or freed.
1529 * @param trusted Do we trust that this path is real?
1532 GMP_add_path_to_origin (struct MeshPeer *peer,
1533 struct MeshPeerPath *path,
1539 GMP_add_path (peer, path, trusted);
1544 * Adds a path to the info of all the peers in the path
1546 * @param p Path to process.
1547 * @param confirmed Whether we know if the path works or not.
1550 GMP_add_path_to_all (const struct MeshPeerPath *p, int confirmed)
1554 /* TODO: invert and add */
1555 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1556 for (i++; i < p->length; i++)
1558 struct MeshPeer *aux;
1559 struct MeshPeerPath *copy;
1561 aux = GMP_get_short (p->peers[i]);
1562 copy = path_duplicate (p);
1563 copy->length = i + 1;
1564 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1570 * Remove a connection from a neighboring peer.
1572 * @param peer Peer to remove connection from.
1573 * @param c Connection to remove.
1575 * @return GNUNET_OK on success.
1578 GMP_remove_connection (struct MeshPeer *peer,
1579 const struct MeshConnection *c)
1581 if (NULL == peer || NULL == peer->connections)
1584 return GNUNET_SYSERR;
1586 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1592 * Get the Full ID of a peer.
1594 * @param peer Peer to get from.
1596 * @return Full ID of peer.
1598 const struct GNUNET_PeerIdentity *
1599 GMP_get_id (const struct MeshPeer *peer)
1601 return GNUNET_PEER_resolve2 (peer->id);
1606 * Get the Short ID of a peer.
1608 * @param peer Peer to get from.
1610 * @return Short ID of peer.
1613 GMP_get_short_id (const struct MeshPeer *peer)
1620 * Get the tunnel towards a peer.
1622 * @param peer Peer to get from.
1624 * @return Tunnel towards peer.
1626 struct MeshTunnel3 *
1627 GMP_get_tunnel (const struct MeshPeer *peer)
1629 return peer->tunnel;
1634 * Get the static string for a peer ID.
1638 * @return Static string for it's ID.
1641 GMP_2s (const struct MeshPeer *peer)
1645 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));