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_tunnel.h"
34 #include "mesh_path.h"
36 #define LOG(level, ...) GNUNET_log_from (level,"mesh-p2p",__VA_ARGS__)
38 /******************************************************************************/
39 /******************************** STRUCTS **********************************/
40 /******************************************************************************/
43 * Struct containing info about a queued transmission to this peer
50 struct MeshPeerQueue *next;
55 struct MeshPeerQueue *prev;
58 * Peer this transmission is directed to.
60 struct MeshPeer *peer;
63 * Connection this message belongs to.
65 struct MeshConnection *c;
73 * Pointer to info stucture used as cls.
88 * Set when this message starts waiting for CORE.
90 struct GNUNET_TIME_Absolute start_waiting;
93 * Function to call on sending.
98 * Closure for callback.
104 * Struct containing all information regarding a given peer
114 * Last time we heard from this peer
116 struct GNUNET_TIME_Absolute last_contact;
119 * Paths to reach the peer, ordered by ascending hop count
121 struct MeshPeerPath *path_head;
124 * Paths to reach the peer, ordered by ascending hop count
126 struct MeshPeerPath *path_tail;
129 * Handle to stop the DHT search for paths to this peer
131 struct GMD_search_handle *search_h;
134 * Tunnel to this peer, if any.
136 struct MeshTunnel3 *tunnel;
139 * Connections that go through this peer, indexed by tid;
141 struct GNUNET_CONTAINER_MultiHashMap *connections;
144 * Handle for queued transmissions
146 struct GNUNET_CORE_TransmitHandle *core_transmit;
149 * Transmission queue to core DLL head
151 struct MeshPeerQueue *queue_head;
154 * Transmission queue to core DLL tail
156 struct MeshPeerQueue *queue_tail;
159 * How many messages are in the queue to this peer.
161 unsigned int queue_n;
165 /******************************************************************************/
166 /******************************* GLOBALS ***********************************/
167 /******************************************************************************/
170 * Global handle to the statistics service.
172 extern struct GNUNET_STATISTICS_Handle *stats;
175 * Local peer own ID (full value).
177 extern struct GNUNET_PeerIdentity my_full_id;
180 * Local peer own ID (short)
182 extern GNUNET_PEER_Id myid;
185 * Peers known, indexed by PeerIdentity (MeshPeer).
187 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
190 * How many peers do we want to remember?
192 static unsigned long long max_peers;
195 * Percentage of messages that will be dropped (for test purposes only).
197 static unsigned long long drop_percent;
200 * Handle to communicate with core.
202 static struct GNUNET_CORE_Handle *core_handle;
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);
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 (GNUNET_ERROR_TYPE_DEBUG, "Peer connected\n");
248 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&my_full_id));
252 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self)\n");
257 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (peer));
259 path->peers[1] = mp->id;
260 GNUNET_PEER_change_rc (mp->id, 1);
261 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
263 path->peers[0] = myid;
264 GNUNET_PEER_change_rc (myid, 1);
265 GMP_add_path (mp, path, GNUNET_YES);
267 mp->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 (GNUNET_ERROR_TYPE_DEBUG, "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;
301 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self)\n");
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_encrypted, GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED, 0},
335 * To be called on core init/fail.
337 * @param cls Closure (config)
338 * @param identity the public identity of this peer
341 core_init (void *cls,
342 const struct GNUNET_PeerIdentity *identity)
344 const struct GNUNET_CONFIGURATION_Handle *c = cls;
347 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
348 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
350 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
351 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
352 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
353 GNUNET_CORE_disconnect (core_handle);
354 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
355 NULL, /* Closure passed to MESH functions */
356 &core_init, /* Call core_init once connected */
357 &core_connect, /* Handle connects */
358 &core_disconnect, /* remove peers on disconnects */
359 NULL, /* Don't notify about all incoming messages */
360 GNUNET_NO, /* For header only in notification */
361 NULL, /* Don't notify about all outbound messages */
362 GNUNET_NO, /* For header-only out notification */
363 core_handlers); /* Register these handlers */
372 * Core callback to write a pre-constructed data packet to core buffer
374 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
375 * @param size Number of bytes available in buf.
376 * @param buf Where the to write the message.
378 * @return number of bytes written to buf
381 send_core_data_raw (void *cls, size_t size, void *buf)
383 struct GNUNET_MessageHeader *msg = cls;
386 GNUNET_assert (NULL != msg);
387 total_size = ntohs (msg->size);
389 if (total_size > size)
394 memcpy (buf, msg, total_size);
401 * Function to send a create connection message to a peer.
403 * @param c Connection to create.
404 * @param size number of bytes available in buf
405 * @param buf where the callee should write the message
406 * @return number of bytes written to buf
409 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
411 struct GNUNET_MESH_ConnectionCreate *msg;
412 struct GNUNET_PeerIdentity *peer_ptr;
413 const struct MeshPeerPath *p = GMC_get_path (c);
417 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
419 sizeof (struct GNUNET_MESH_ConnectionCreate) +
420 p->length * sizeof (struct GNUNET_PeerIdentity);
422 if (size < size_needed || NULL == buf)
427 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
428 msg->header.size = htons (size_needed);
429 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
430 msg->cid = *GMC_get_id (c);
432 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
433 for (i = 0; i < p->length; i++)
435 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
438 LOG (GNUNET_ERROR_TYPE_DEBUG,
439 "CONNECTION CREATE (%u bytes long) sent!\n",
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;
459 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
460 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
465 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
466 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
467 msg->cid = *GMC_get_id (c);
470 /* TODO add signature */
472 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
473 return sizeof (struct GNUNET_MESH_ConnectionACK);
477 /******************************************************************************/
478 /******************************** STATIC ***********************************/
479 /******************************************************************************/
482 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
485 * @param key current key code
486 * @param value value in the hash map
487 * @return #GNUNET_YES if we should continue to iterate,
491 shutdown_tunnel (void *cls,
492 const struct GNUNET_PeerIdentity *key,
495 struct MeshPeer *p = value;
496 struct MeshTunnel3 *t = p->tunnel;
506 * Destroy the peer_info and free any allocated resources linked to it
508 * @param peer The peer_info to destroy.
510 * @return GNUNET_OK on success
513 peer_destroy (struct MeshPeer *peer)
515 struct GNUNET_PeerIdentity id;
516 struct MeshPeerPath *p;
517 struct MeshPeerPath *nextp;
519 GNUNET_PEER_resolve (peer->id, &id);
520 GNUNET_PEER_change_rc (peer->id, -1);
523 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
526 LOG (GNUNET_ERROR_TYPE_WARNING,
527 "removing peer %s, not in peermap\n", GNUNET_i2s (&id));
529 if (NULL != peer->search_h)
531 GMD_search_stop (peer->search_h);
537 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
541 GMT_destroy_empty (peer->tunnel);
548 * Returns if peer is used (has a tunnel, is neighbor).
550 * @param peer Peer to check.
552 * @return GNUNET_YES if peer is in use.
555 peer_is_used (struct MeshPeer *peer)
557 struct MeshPeerPath *p;
559 if (NULL != peer->tunnel)
562 for (p = peer->path_head; NULL != p; p = p->next)
572 * Iterator over all the peers to get the oldest timestamp.
574 * @param cls Closure (unsued).
575 * @param key ID of the peer.
576 * @param value Peer_Info of the peer.
579 peer_get_oldest (void *cls,
580 const struct GNUNET_PeerIdentity *key,
583 struct MeshPeer *p = value;
584 struct GNUNET_TIME_Absolute *abs = cls;
586 /* Don't count active peers */
587 if (GNUNET_YES == peer_is_used (p))
590 if (abs->abs_value_us < p->last_contact.abs_value_us)
591 abs->abs_value_us = p->last_contact.abs_value_us;
598 * Iterator over all the peers to remove the oldest entry.
600 * @param cls Closure (unsued).
601 * @param key ID of the peer.
602 * @param value Peer_Info of the peer.
605 peer_timeout (void *cls,
606 const struct GNUNET_PeerIdentity *key,
609 struct MeshPeer *p = value;
610 struct GNUNET_TIME_Absolute *abs = cls;
612 if (p->last_contact.abs_value_us == abs->abs_value_us &&
613 GNUNET_NO == peer_is_used (p))
623 * Delete oldest unused peer.
626 peer_delete_oldest (void)
628 struct GNUNET_TIME_Absolute abs;
630 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
632 GNUNET_CONTAINER_multipeermap_iterate (peers,
635 GNUNET_CONTAINER_multipeermap_iterate (peers,
642 * Choose the best path towards a peer considering the tunnel properties.
644 * @param peer The destination peer.
646 * @return Best current known path towards the peer, if any.
648 static struct MeshPeerPath *
649 peer_get_best_path (const struct MeshPeer *peer)
651 struct MeshPeerPath *best_p;
652 struct MeshPeerPath *p;
653 unsigned int best_cost;
656 best_cost = UINT_MAX;
658 for (p = peer->path_head; NULL != p; p = p->next)
660 if (GNUNET_YES == GMT_is_path_used (peer->tunnel, p))
661 continue; /* If path is already in use, skip it. */
663 if ((cost = GMT_get_path_cost (peer->tunnel, p)) < best_cost)
674 queue_is_sendable (struct MeshPeerQueue *q)
676 /* Is PID-independent? */
679 case GNUNET_MESSAGE_TYPE_MESH_ACK:
680 case GNUNET_MESSAGE_TYPE_MESH_POLL:
684 if (GMC_is_sendable (q->c, q->fwd))
692 * Get first sendable message.
694 * @param peer The destination peer.
696 * @return Best current known path towards the peer, if any.
698 static struct MeshPeerQueue *
699 peer_get_first_message (const struct MeshPeer *peer)
701 struct MeshPeerQueue *q;
703 for (q = peer->queue_head; NULL != q; q = q->next)
705 if (queue_is_sendable (q))
714 * Function to process paths received for a new peer addition. The recorded
715 * paths form the initial tunnel, which can be optimized later.
716 * Called on each result obtained for the DHT search.
722 search_handler (void *cls, const struct MeshPeerPath *path)
724 struct MeshPeer *peer = cls;
725 unsigned int connection_count;
727 GMP_add_path_to_all (path, GNUNET_NO);
729 /* Count connections */
730 connection_count = GMT_count_connections (peer->tunnel);
732 /* If we already have 3 (or more (?!)) connections, it's enough */
733 if (3 <= connection_count)
736 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (peer->tunnel))
738 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
746 * Free a transmission that was already queued with all resources
747 * associated to the request.
749 * @param queue Queue handler to cancel.
750 * @param clear_cls Is it necessary to free associated cls?
753 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
755 struct MeshPeer *peer;
758 GNUNET_assert (NULL != queue->c);
760 if (GNUNET_YES == clear_cls)
762 LOG (GNUNET_ERROR_TYPE_DEBUG, "# queue destroy type %s\n",
763 GNUNET_MESH_DEBUG_M2S (queue->type));
766 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
767 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
768 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
770 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
771 case GNUNET_MESSAGE_TYPE_MESH_ACK:
772 case GNUNET_MESSAGE_TYPE_MESH_POLL:
773 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
774 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
775 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
776 LOG (GNUNET_ERROR_TYPE_DEBUG, "# prebuilt message\n");;
777 GNUNET_free_non_null (queue->cls);
782 LOG (GNUNET_ERROR_TYPE_ERROR, "# type %s unknown!\n",
783 GNUNET_MESH_DEBUG_M2S (queue->type));
786 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
788 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
789 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
794 if (NULL != queue->callback)
796 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Calling callback\n");
797 queue->callback (queue->callback_cls,
798 queue->c, queue->type,
799 queue->fwd, queue->size,
800 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
807 * Core callback to write a queued packet to core buffer
809 * @param cls Closure (peer info).
810 * @param size Number of bytes available in buf.
811 * @param buf Where the to write the message.
813 * @return number of bytes written to buf
816 queue_send (void *cls, size_t size, void *buf)
818 struct MeshPeer *peer = cls;
819 struct MeshConnection *c;
820 struct MeshPeerQueue *queue;
821 const struct GNUNET_PeerIdentity *dst_id;
824 peer->core_transmit = NULL;
825 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
827 if (NULL == buf || 0 == size)
829 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
834 queue = peer_get_first_message (peer);
837 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
842 dst_id = GNUNET_PEER_resolve2 (peer->id);
843 LOG (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
844 /* Check if buffer size is enough for the message */
845 if (queue->size > size)
847 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
848 peer->core_transmit =
849 GNUNET_CORE_notify_transmit_ready (core_handle,
852 GNUNET_TIME_UNIT_FOREVER_REL,
859 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
864 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
865 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
866 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
867 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
868 case GNUNET_MESSAGE_TYPE_MESH_ACK:
869 case GNUNET_MESSAGE_TYPE_MESH_POLL:
870 LOG (GNUNET_ERROR_TYPE_DEBUG,
872 GNUNET_MESH_DEBUG_M2S (queue->type));
873 data_size = send_core_data_raw (queue->cls, size, buf);
875 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
876 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
877 if (GMC_is_origin (c, GNUNET_YES))
878 data_size = send_core_connection_create (queue->c, size, buf);
880 data_size = send_core_data_raw (queue->cls, size, buf);
882 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
883 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
884 if (GMC_is_origin (c, GNUNET_NO) ||
885 GMC_is_origin (c, GNUNET_YES))
886 data_size = send_core_connection_ack (queue->c, size, buf);
888 data_size = send_core_data_raw (queue->cls, size, buf);
890 case GNUNET_MESSAGE_TYPE_MESH_DATA:
891 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
892 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
893 /* This should be encapsulted */
899 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
904 if (0 < drop_percent &&
905 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
907 LOG (GNUNET_ERROR_TYPE_WARNING,
908 "Dropping message of type %s\n",
909 GNUNET_MESH_DEBUG_M2S (queue->type));
913 /* Free queue, but cls was freed by send_core_* */
914 queue_destroy (queue, GNUNET_NO);
916 /* If more data in queue, send next */
917 queue = peer_get_first_message (peer);
920 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
921 if (NULL == peer->core_transmit)
923 peer->core_transmit =
924 GNUNET_CORE_notify_transmit_ready(core_handle,
927 GNUNET_TIME_UNIT_FOREVER_REL,
932 queue->start_waiting = GNUNET_TIME_absolute_get ();
936 LOG (GNUNET_ERROR_TYPE_DEBUG,
937 "* tmt rdy called somewhere else\n");
939 // GMC_start_poll (); FIXME needed?
943 // GMC_stop_poll(); FIXME needed?
946 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
951 /******************************************************************************/
952 /******************************** API ***********************************/
953 /******************************************************************************/
956 * @brief Queue and pass message to core when possible.
958 * @param peer Peer towards which to queue the message.
959 * @param cls Closure (@c type dependant). It will be used by queue_send to
960 * build the message to be sent if not already prebuilt.
961 * @param type Type of the message, 0 for a raw message.
962 * @param size Size of the message.
963 * @param c Connection this message belongs to (cannot be NULL).
964 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
965 * @param cont Continuation to be called once CORE has taken the message.
966 * @param cont_cls Closure for @c cont.
969 GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
970 struct MeshConnection *c, int fwd,
971 GMP_sent cont, void *cont_cls)
973 struct MeshPeerQueue *queue;
977 LOG (GNUNET_ERROR_TYPE_DEBUG,
978 "queue add %s %s towards %s (size %u) on c %p (%s)\n",
979 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), GMP_2s(peer),
980 size, c, GMC_2s (c));
981 GNUNET_assert (NULL != c);
983 if (NULL == peer->connections)
985 /* We are not connected to this peer, ignore request. */
992 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
993 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
998 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1000 call_core = GMC_is_sendable (c, fwd);
1001 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1008 queue->callback = cont;
1009 queue->callback_cls = cont_cls;
1010 if (100 <= priority)
1012 struct MeshPeerQueue *copy;
1013 struct MeshPeerQueue *next;
1015 for (copy = peer->queue_head; NULL != copy; copy = next)
1018 if (copy->type == type && copy->c == c && copy->fwd == fwd)
1020 /* Example: also a FWD ACK for connection XYZ */
1021 queue_destroy (copy, GNUNET_YES);
1024 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1028 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1032 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1034 LOG (GNUNET_ERROR_TYPE_DEBUG,
1035 "calling core tmt rdy towards %s for %u bytes\n",
1036 GMP_2s (peer), size);
1037 peer->core_transmit =
1038 GNUNET_CORE_notify_transmit_ready (core_handle,
1041 GNUNET_TIME_UNIT_FOREVER_REL,
1042 GNUNET_PEER_resolve2 (peer->id),
1046 queue->start_waiting = GNUNET_TIME_absolute_get ();
1050 LOG (GNUNET_ERROR_TYPE_DEBUG,
1051 "core tmt rdy towards %s already called\n",
1059 * Cancel all queued messages to a peer that belong to a certain connection.
1061 * @param peer Peer towards whom to cancel.
1062 * @param c Connection whose queued messages to cancel. Might be destroyed by
1063 * the sent continuation call.
1066 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1068 struct MeshPeerQueue *q;
1069 struct MeshPeerQueue *next;
1070 struct MeshPeerQueue *prev;
1072 for (q = peer->queue_head; NULL != q; q = next)
1077 LOG (GNUNET_ERROR_TYPE_DEBUG,
1078 "GMP_cancel_queue %s\n",
1079 GNUNET_MESH_DEBUG_M2S (q->type));
1080 queue_destroy (q, GNUNET_YES);
1082 /* Get next from prev, q->next might be already freed:
1083 * queue destroy -> callback -> GMC_destroy -> cancel_queues -> here
1086 next = peer->queue_head;
1095 if (NULL == peer->queue_head)
1097 if (NULL != peer->core_transmit)
1099 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1100 peer->core_transmit = NULL;
1107 * Get the first transmittable message for a connection.
1109 * @param peer Neighboring peer.
1110 * @param c Connection.
1112 * @return First transmittable message.
1114 static struct MeshPeerQueue *
1115 connection_get_first_message (struct MeshPeer *peer, struct MeshConnection *c)
1117 struct MeshPeerQueue *q;
1119 for (q = peer->queue_head; NULL != q; q = q->next)
1123 if (queue_is_sendable (q))
1131 GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
1133 struct MeshPeerQueue *q;
1136 if (NULL != peer->core_transmit)
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1139 return; /* Already unlocked */
1142 q = connection_get_first_message (peer, c);
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1146 return; /* Nothing to transmit */
1150 peer->core_transmit =
1151 GNUNET_CORE_notify_transmit_ready (core_handle,
1154 GNUNET_TIME_UNIT_FOREVER_REL,
1155 GNUNET_PEER_resolve2 (peer->id),
1163 * Initialize the peer subsystem.
1165 * @param c Configuration.
1168 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1171 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1173 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1176 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1177 "MESH", "MAX_PEERS", "USING DEFAULT");
1182 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1189 LOG (GNUNET_ERROR_TYPE_WARNING,
1190 "\n***************************************\n"
1191 "Mesh is running with drop mode enabled.\n"
1192 "This is NOT a good idea!\n"
1193 "Remove the DROP_PERCENT option from your configuration.\n"
1194 "***************************************\n");
1197 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1198 NULL, /* Closure passed to MESH functions */
1199 &core_init, /* Call core_init once connected */
1200 &core_connect, /* Handle connects */
1201 &core_disconnect, /* remove peers on disconnects */
1202 NULL, /* Don't notify about all incoming messages */
1203 GNUNET_NO, /* For header only in notification */
1204 NULL, /* Don't notify about all outbound messages */
1205 GNUNET_NO, /* For header-only out notification */
1206 core_handlers); /* Register these handlers */
1207 if (NULL == core_handle)
1210 GNUNET_SCHEDULER_shutdown ();
1216 * Shut down the peer subsystem.
1221 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1223 if (core_handle != NULL)
1225 GNUNET_CORE_disconnect (core_handle);
1228 GNUNET_PEER_change_rc (myid, -1);
1232 * Retrieve the MeshPeer stucture associated with the peer, create one
1233 * and insert it in the appropriate structures if the peer is not known yet.
1235 * @param peer_id Full identity of the peer.
1237 * @return Existing or newly created peer structure.
1240 GMP_get (const struct GNUNET_PeerIdentity *peer_id)
1242 struct MeshPeer *peer;
1244 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1247 peer = GNUNET_new (struct MeshPeer);
1248 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1250 peer_delete_oldest ();
1252 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1253 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1254 peer->id = GNUNET_PEER_intern (peer_id);
1256 peer->last_contact = GNUNET_TIME_absolute_get();
1263 * Retrieve the MeshPeer stucture associated with the peer, create one
1264 * and insert it in the appropriate structures if the peer is not known yet.
1266 * @param peer Short identity of the peer.
1268 * @return Existing or newly created peer structure.
1271 GMP_get_short (const GNUNET_PEER_Id peer)
1273 return GMP_get (GNUNET_PEER_resolve2 (peer));
1278 * Try to establish a new connection to this peer (in its tunnel).
1279 * If the peer doesn't have any path to it yet, try to get one.
1280 * If the peer already has some path, send a CREATE CONNECTION towards it.
1282 * @param peer Peer to connect to.
1285 GMP_connect (struct MeshPeer *peer)
1287 struct MeshTunnel3 *t;
1288 struct MeshPeerPath *p;
1289 struct MeshConnection *c;
1292 LOG (GNUNET_ERROR_TYPE_DEBUG,
1293 "peer_connect towards %s\n",
1297 rerun_search = GNUNET_NO;
1299 if (NULL != peer->path_head)
1301 LOG (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
1302 p = peer_get_best_path (peer);
1305 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1306 c = GMT_use_path (t, p);
1309 /* This case can happen when the path includes a first hop that is
1310 * not yet known to be connected.
1312 * This happens quite often during testing when running mesh
1313 * under valgrind: core connect notifications come very late and the
1314 * DHT result has already come and created a valid path.
1315 * In this case, the peer->connections hashmap will be NULL and
1316 * tunnel_use_path will not be able to create a connection from that
1319 * Re-running the DHT GET should give core time to callback.
1322 rerun_search = GNUNET_YES;
1326 GMC_send_create (c);
1332 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1334 GMD_search_stop (peer->search_h);
1335 peer->search_h = NULL;
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 " Stopping DHT GET for peer %s\n",
1341 if (NULL == peer->search_h)
1343 const struct GNUNET_PeerIdentity *id;
1345 id = GNUNET_PEER_resolve2 (peer->id);
1346 LOG (GNUNET_ERROR_TYPE_DEBUG,
1347 " Starting DHT GET for peer %s\n", GMP_2s (peer));
1348 peer->search_h = GMD_search (id, &search_handler, peer);
1349 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
1350 GMT_change_state (t, MESH_TUNNEL3_SEARCHING);
1362 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel3 *t)
1369 * Chech whether there is a direct (core level) connection to peer.
1371 * @param peer Peer to check.
1373 * @return GNUNET_YES if there is a direct connection.
1376 GMP_is_neighbor (const struct MeshPeer *peer)
1378 struct MeshPeerPath *path;
1380 if (NULL == peer->connections)
1383 for (path = peer->path_head; NULL != path; path = path->next)
1385 if (3 > path->length)
1389 GNUNET_break (0); /* Is not a neighbor but connections is not NULL */
1395 * Create and initialize a new tunnel towards a peer, in case it has none.
1396 * In case the peer already has a tunnel, nothing is done.
1398 * Does not generate any traffic, just creates the local data structures.
1400 * @param peer Peer towards which to create the tunnel.
1403 GMP_add_tunnel (struct MeshPeer *peer)
1405 if (NULL != peer->tunnel)
1407 peer->tunnel = GMT_new (peer);
1412 * Add a connection to a neighboring peer.
1414 * Store that the peer is the first hop of the connection in one
1415 * direction and that on peer disconnect the connection must be
1416 * notified and destroyed, for it will no longer be valid.
1418 * @param peer Peer to add connection to.
1419 * @param c Connection to add.
1421 * @return GNUNET_OK on success.
1424 GMP_add_connection (struct MeshPeer *peer,
1425 struct MeshConnection *c)
1427 if (NULL == peer->connections)
1430 return GNUNET_SYSERR;
1432 return GNUNET_CONTAINER_multihashmap_put (peer->connections,
1435 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1440 * Add the path to the peer and update the path used to reach it in case this
1443 * @param peer Destination peer to add the path to.
1444 * @param path New path to add. Last peer must be the peer in arg 1.
1445 * Path will be either used of freed if already known.
1446 * @param trusted Do we trust that this path is real?
1448 * @return path if path was taken, pointer to existing duplicate if exists
1451 struct MeshPeerPath *
1452 GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *path,
1455 struct MeshPeerPath *aux;
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1460 path->length, GMP_2s (peer));
1462 if ((NULL == peer) || (NULL == path))
1465 path_destroy (path);
1468 if (path->peers[path->length - 1] != peer->id)
1471 path_destroy (path);
1474 if (2 >= path->length && GNUNET_NO == trusted)
1476 /* Only allow CORE to tell us about direct paths */
1477 path_destroy (path);
1480 for (l = 1; l < path->length; l++)
1482 if (path->peers[l] == myid)
1484 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1485 for (l2 = 0; l2 < path->length - l; l2++)
1487 path->peers[l2] = path->peers[l + l2];
1491 path->peers = GNUNET_realloc (path->peers,
1492 path->length * sizeof (GNUNET_PEER_Id));
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u]\n", path->length);
1498 l = path_get_length (path);
1501 path_destroy (path);
1505 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1506 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1508 l2 = path_get_length (aux);
1511 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1512 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1513 peer->path_tail, aux, path);
1518 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1520 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1521 path_destroy (path);
1526 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1534 * Add the path to the origin peer and update the path used to reach it in case
1535 * this is the shortest.
1536 * The path is given in peer_info -> destination, therefore we turn the path
1537 * upside down first.
1539 * @param peer Peer to add the path to, being the origin of the path.
1540 * @param path New path to add after being inversed.
1541 * Path will be either used or freed.
1542 * @param trusted Do we trust that this path is real?
1544 * @return path if path was taken, pointer to existing duplicate if exists
1547 struct MeshPeerPath *
1548 GMP_add_path_to_origin (struct MeshPeer *peer,
1549 struct MeshPeerPath *path,
1555 return GMP_add_path (peer, path, trusted);
1560 * Adds a path to the info of all the peers in the path
1562 * @param p Path to process.
1563 * @param confirmed Whether we know if the path works or not.
1566 GMP_add_path_to_all (const struct MeshPeerPath *p, int confirmed)
1570 /* TODO: invert and add */
1571 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1572 for (i++; i < p->length; i++)
1574 struct MeshPeer *aux;
1575 struct MeshPeerPath *copy;
1577 aux = GMP_get_short (p->peers[i]);
1578 copy = path_duplicate (p);
1579 copy->length = i + 1;
1580 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1586 * Remove a connection from a neighboring peer.
1588 * @param peer Peer to remove connection from.
1589 * @param c Connection to remove.
1591 * @return GNUNET_OK on success.
1594 GMP_remove_connection (struct MeshPeer *peer,
1595 const struct MeshConnection *c)
1597 if (NULL == peer || NULL == peer->connections)
1600 LOG (GNUNET_ERROR_TYPE_DEBUG,
1601 "Peer %s is not a neighbor!\n",
1603 return GNUNET_SYSERR;
1605 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1611 * Start the DHT search for new paths towards the peer: we don't have
1612 * enough good connections.
1614 * @param peer Destination peer.
1617 GMP_start_search (struct MeshPeer *peer)
1619 if (NULL != peer->search_h)
1625 peer->search_h = GMD_search (GMP_get_id (peer), &search_handler, peer);
1630 * Stop the DHT search for new paths towards the peer: we already have
1631 * enough good connections.
1633 * @param peer Destination peer.
1636 GMP_stop_search (struct MeshPeer *peer)
1638 if (NULL == peer->search_h)
1644 GMD_search_stop (peer->search_h);
1645 peer->search_h = NULL;
1650 * Get the Full ID of a peer.
1652 * @param peer Peer to get from.
1654 * @return Full ID of peer.
1656 const struct GNUNET_PeerIdentity *
1657 GMP_get_id (const struct MeshPeer *peer)
1659 return GNUNET_PEER_resolve2 (peer->id);
1664 * Get the Short ID of a peer.
1666 * @param peer Peer to get from.
1668 * @return Short ID of peer.
1671 GMP_get_short_id (const struct MeshPeer *peer)
1678 * Get the tunnel towards a peer.
1680 * @param peer Peer to get from.
1682 * @return Tunnel towards peer.
1684 struct MeshTunnel3 *
1685 GMP_get_tunnel (const struct MeshPeer *peer)
1687 return peer->tunnel;
1692 * Get the static string for a peer ID.
1696 * @return Static string for it's ID.
1699 GMP_2s (const struct MeshPeer *peer)
1703 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));