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.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 to continue to iterate.
221 notify_broken (void *cls,
222 const struct GNUNET_HashCode *key,
225 struct MeshPeer *peer = cls;
226 struct MeshConnection *c = value;
228 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
229 GMC_2s (c), GMP_2s (peer));
230 GMC_notify_broken (c, peer);
237 * Remove the direct path to the peer.
239 * @param peer Peer to remove the direct path from.
242 static struct MeshPeerPath *
243 pop_direct_path (struct MeshPeer *peer)
245 struct MeshPeerPath *iter;
247 for (iter = peer->path_head; NULL != iter; iter = iter->next)
249 if (2 <= iter->length)
251 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
261 * Method called whenever a given peer connects.
264 * @param peer peer identity this notification is about
267 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
270 struct MeshPeerPath *path;
272 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer connected\n");
273 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&my_full_id));
277 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self)\n");
282 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (peer));
284 path->peers[1] = mp->id;
285 GNUNET_PEER_change_rc (mp->id, 1);
286 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
288 path->peers[0] = myid;
289 GNUNET_PEER_change_rc (myid, 1);
290 GMP_add_path (mp, path, GNUNET_YES);
292 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
298 * Method called whenever a peer disconnects.
301 * @param peer peer identity this notification is about
304 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
307 struct MeshPeerPath *direct_path;
309 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer disconnected\n");
310 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
317 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self: %s)\n", GMP_2s (p));
319 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GMP_2s (p));
321 direct_path = pop_direct_path (p);
322 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
323 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
324 p->connections = NULL;
325 if (NULL != p->core_transmit)
327 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
328 p->core_transmit = NULL;
330 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
332 path_destroy (direct_path);
338 * Functions to handle messages from core
340 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
341 {&GMC_handle_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
343 {&GMC_handle_confirm, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
344 sizeof (struct GNUNET_MESH_ConnectionACK)},
345 {&GMC_handle_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
346 sizeof (struct GNUNET_MESH_ConnectionBroken)},
347 {&GMC_handle_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
348 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
349 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE,
350 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
351 {&GMC_handle_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
352 sizeof (struct GNUNET_MESH_ACK)},
353 {&GMC_handle_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
354 sizeof (struct GNUNET_MESH_Poll)},
355 {&GMC_handle_encrypted, GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED, 0},
356 {&GMC_handle_kx, GNUNET_MESSAGE_TYPE_MESH_KX, 0},
362 * To be called on core init/fail.
364 * @param cls Closure (config)
365 * @param identity the public identity of this peer
368 core_init (void *cls,
369 const struct GNUNET_PeerIdentity *identity)
371 const struct GNUNET_CONFIGURATION_Handle *c = cls;
374 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
375 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
377 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
378 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
379 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
380 GNUNET_CORE_disconnect (core_handle);
381 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
382 NULL, /* Closure passed to MESH functions */
383 &core_init, /* Call core_init once connected */
384 &core_connect, /* Handle connects */
385 &core_disconnect, /* remove peers on disconnects */
386 NULL, /* Don't notify about all incoming messages */
387 GNUNET_NO, /* For header only in notification */
388 NULL, /* Don't notify about all outbound messages */
389 GNUNET_NO, /* For header-only out notification */
390 core_handlers); /* Register these handlers */
399 * Core callback to write a pre-constructed data packet to core buffer
401 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
402 * @param size Number of bytes available in buf.
403 * @param buf Where the to write the message.
405 * @return number of bytes written to buf
408 send_core_data_raw (void *cls, size_t size, void *buf)
410 struct GNUNET_MessageHeader *msg = cls;
413 GNUNET_assert (NULL != msg);
414 total_size = ntohs (msg->size);
416 if (total_size > size)
421 memcpy (buf, msg, total_size);
428 * Function to send a create connection message to a peer.
430 * @param c Connection to create.
431 * @param size number of bytes available in buf
432 * @param buf where the callee should write the message
433 * @return number of bytes written to buf
436 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
438 struct GNUNET_MESH_ConnectionCreate *msg;
439 struct GNUNET_PeerIdentity *peer_ptr;
440 const struct MeshPeerPath *p = GMC_get_path (c);
447 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
449 sizeof (struct GNUNET_MESH_ConnectionCreate) +
450 p->length * sizeof (struct GNUNET_PeerIdentity);
452 if (size < size_needed || NULL == buf)
457 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
458 msg->header.size = htons (size_needed);
459 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
460 msg->cid = *GMC_get_id (c);
462 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
463 for (i = 0; i < p->length; i++)
465 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 "CONNECTION CREATE (%u bytes long) sent!\n",
476 * Creates a path ack message in buf and frees all unused resources.
478 * @param c Connection to send an ACK on.
479 * @param size number of bytes available in buf
480 * @param buf where the callee should write the message
482 * @return number of bytes written to buf
485 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
487 struct GNUNET_MESH_ConnectionACK *msg = buf;
489 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
490 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
495 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
496 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
497 msg->cid = *GMC_get_id (c);
500 /* TODO add signature */
502 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
503 return sizeof (struct GNUNET_MESH_ConnectionACK);
507 /******************************************************************************/
508 /******************************** STATIC ***********************************/
509 /******************************************************************************/
512 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
515 * @param key current key code
516 * @param value value in the hash map
517 * @return #GNUNET_YES if we should continue to iterate,
521 shutdown_tunnel (void *cls,
522 const struct GNUNET_PeerIdentity *key,
525 struct MeshPeer *p = value;
526 struct MeshTunnel3 *t = p->tunnel;
536 * Destroy the peer_info and free any allocated resources linked to it
538 * @param peer The peer_info to destroy.
540 * @return GNUNET_OK on success
543 peer_destroy (struct MeshPeer *peer)
545 struct GNUNET_PeerIdentity id;
546 struct MeshPeerPath *p;
547 struct MeshPeerPath *nextp;
549 GNUNET_PEER_resolve (peer->id, &id);
550 GNUNET_PEER_change_rc (peer->id, -1);
552 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
555 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
558 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
560 if (NULL != peer->search_h)
562 GMD_search_stop (peer->search_h);
568 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
572 GMT_destroy_empty (peer->tunnel);
579 * Returns if peer is used (has a tunnel or is neighbor).
581 * @param peer Peer to check.
583 * @return #GNUNET_YES if peer is in use.
586 peer_is_used (struct MeshPeer *peer)
588 struct MeshPeerPath *p;
590 if (NULL != peer->tunnel)
593 for (p = peer->path_head; NULL != p; p = p->next)
603 * Iterator over all the peers to get the oldest timestamp.
605 * @param cls Closure (unsued).
606 * @param key ID of the peer.
607 * @param value Peer_Info of the peer.
610 peer_get_oldest (void *cls,
611 const struct GNUNET_PeerIdentity *key,
614 struct MeshPeer *p = value;
615 struct GNUNET_TIME_Absolute *abs = cls;
617 /* Don't count active peers */
618 if (GNUNET_YES == peer_is_used (p))
621 if (abs->abs_value_us < p->last_contact.abs_value_us)
622 abs->abs_value_us = p->last_contact.abs_value_us;
629 * Iterator over all the peers to remove the oldest entry.
631 * @param cls Closure (unsued).
632 * @param key ID of the peer.
633 * @param value Peer_Info of the peer.
636 peer_timeout (void *cls,
637 const struct GNUNET_PeerIdentity *key,
640 struct MeshPeer *p = value;
641 struct GNUNET_TIME_Absolute *abs = cls;
643 LOG (GNUNET_ERROR_TYPE_WARNING,
644 "peer %s timeout\n", GNUNET_i2s (key));
646 if (p->last_contact.abs_value_us == abs->abs_value_us &&
647 GNUNET_NO == peer_is_used (p))
657 * Delete oldest unused peer.
660 peer_delete_oldest (void)
662 struct GNUNET_TIME_Absolute abs;
664 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
666 GNUNET_CONTAINER_multipeermap_iterate (peers,
669 GNUNET_CONTAINER_multipeermap_iterate (peers,
676 * Choose the best (yet unused) path towards a peer,
677 * considering the tunnel properties.
679 * @param peer The destination peer.
681 * @return Best current known path towards the peer, if any.
683 static struct MeshPeerPath *
684 peer_get_best_path (const struct MeshPeer *peer)
686 struct MeshPeerPath *best_p;
687 struct MeshPeerPath *p;
688 unsigned int best_cost;
691 best_cost = UINT_MAX;
693 for (p = peer->path_head; NULL != p; p = p->next)
695 if (GNUNET_YES == GMT_is_path_used (peer->tunnel, p))
696 continue; /* If path is already in use, skip it. */
698 if (GNUNET_NO == path_is_valid (p))
699 continue; /* Don't use invalid paths. */
701 if ((cost = GMT_get_path_cost (peer->tunnel, p)) < best_cost)
712 queue_is_sendable (struct MeshPeerQueue *q)
714 /* Is PID-independent? */
717 case GNUNET_MESSAGE_TYPE_MESH_ACK:
718 case GNUNET_MESSAGE_TYPE_MESH_POLL:
719 case GNUNET_MESSAGE_TYPE_MESH_KX:
720 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
721 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
722 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
723 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
724 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
727 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
734 if (GMC_is_sendable (q->c, q->fwd))
742 * Get first sendable message.
744 * @param peer The destination peer.
746 * @return Best current known path towards the peer, if any.
748 static struct MeshPeerQueue *
749 peer_get_first_message (const struct MeshPeer *peer)
751 struct MeshPeerQueue *q;
753 for (q = peer->queue_head; NULL != q; q = q->next)
755 if (queue_is_sendable (q))
764 * Function to process paths received for a new peer addition. The recorded
765 * paths form the initial tunnel, which can be optimized later.
766 * Called on each result obtained for the DHT search.
772 search_handler (void *cls, const struct MeshPeerPath *path)
774 struct MeshPeer *peer = cls;
775 unsigned int connection_count;
777 GMP_add_path_to_all (path, GNUNET_NO);
779 /* Count connections */
780 connection_count = GMT_count_connections (peer->tunnel);
782 /* If we already have 3 (or more (?!)) connections, it's enough */
783 if (3 <= connection_count)
786 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (peer->tunnel))
788 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
796 * Core callback to write a queued packet to core buffer
798 * @param cls Closure (peer info).
799 * @param size Number of bytes available in buf.
800 * @param buf Where the to write the message.
802 * @return number of bytes written to buf
805 queue_send (void *cls, size_t size, void *buf)
807 struct MeshPeer *peer = cls;
808 struct MeshConnection *c;
809 struct MeshPeerQueue *queue;
810 const struct GNUNET_PeerIdentity *dst_id;
813 peer->core_transmit = NULL;
814 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send towards %s (max %u)\n",
815 GMP_2s (peer), size);
817 if (NULL == buf || 0 == size)
819 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
824 queue = peer_get_first_message (peer);
827 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
832 dst_id = GNUNET_PEER_resolve2 (peer->id);
833 LOG (GNUNET_ERROR_TYPE_DEBUG, "* on connection %s\n", GMC_2s (c));
834 /* Check if buffer size is enough for the message */
835 if (queue->size > size)
837 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
838 peer->core_transmit =
839 GNUNET_CORE_notify_transmit_ready (core_handle,
842 GNUNET_TIME_UNIT_FOREVER_REL,
849 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
854 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
855 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
856 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
857 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
858 case GNUNET_MESSAGE_TYPE_MESH_KX:
859 case GNUNET_MESSAGE_TYPE_MESH_ACK:
860 case GNUNET_MESSAGE_TYPE_MESH_POLL:
861 LOG (GNUNET_ERROR_TYPE_DEBUG,
863 GM_m2s (queue->type));
864 data_size = send_core_data_raw (queue->cls, size, buf);
866 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
867 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
868 if (GMC_is_origin (c, GNUNET_YES))
869 data_size = send_core_connection_create (queue->c, size, buf);
871 data_size = send_core_data_raw (queue->cls, size, buf);
873 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
874 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
875 if (GMC_is_origin (c, GNUNET_NO) ||
876 GMC_is_origin (c, GNUNET_YES))
877 data_size = send_core_connection_ack (queue->c, size, buf);
879 data_size = send_core_data_raw (queue->cls, size, buf);
881 case GNUNET_MESSAGE_TYPE_MESH_DATA:
882 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
883 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
884 /* This should be encapsulted */
890 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n", queue->type);
894 if (0 < drop_percent &&
895 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
897 LOG (GNUNET_ERROR_TYPE_WARNING,
898 "Dropping message of type %s\n",
899 GM_m2s (queue->type));
903 /* Free queue, but cls was freed by send_core_* */
904 GMP_queue_destroy (queue, GNUNET_NO);
906 /* If more data in queue, send next */
907 queue = peer_get_first_message (peer);
910 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
911 if (NULL == peer->core_transmit)
913 peer->core_transmit =
914 GNUNET_CORE_notify_transmit_ready(core_handle,
917 GNUNET_TIME_UNIT_FOREVER_REL,
922 queue->start_waiting = GNUNET_TIME_absolute_get ();
926 LOG (GNUNET_ERROR_TYPE_DEBUG,
927 "* tmt rdy called somewhere else\n");
929 // GMC_start_poll (); FIXME needed?
933 // GMC_stop_poll(); FIXME needed?
936 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
941 /******************************************************************************/
942 /******************************** API ***********************************/
943 /******************************************************************************/
947 * Free a transmission that was already queued with all resources
948 * associated to the request.
950 * @param queue Queue handler to cancel.
951 * @param clear_cls Is it necessary to free associated cls?
954 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
956 struct MeshPeer *peer;
959 GNUNET_assert (NULL != queue->c);
961 if (GNUNET_YES == clear_cls)
963 LOG (GNUNET_ERROR_TYPE_DEBUG, "# queue destroy type %s\n",
964 GM_m2s (queue->type));
967 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
968 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
970 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
971 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
972 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
973 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
974 case GNUNET_MESSAGE_TYPE_MESH_KX:
975 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
976 case GNUNET_MESSAGE_TYPE_MESH_ACK:
977 case GNUNET_MESSAGE_TYPE_MESH_POLL:
978 GNUNET_free_non_null (queue->cls);
983 LOG (GNUNET_ERROR_TYPE_ERROR, "# type %s unknown!\n",
984 GM_m2s (queue->type));
987 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
989 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
990 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
995 if (NULL != queue->callback)
997 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Calling callback\n");
998 queue->callback (queue->callback_cls,
999 queue->c, queue->type,
1000 queue->fwd, queue->size,
1001 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
1004 GNUNET_free (queue);
1009 * @brief Queue and pass message to core when possible.
1011 * @param peer Peer towards which to queue the message.
1012 * @param cls Closure (@c type dependant). It will be used by queue_send to
1013 * build the message to be sent if not already prebuilt.
1014 * @param type Type of the message, 0 for a raw message.
1015 * @param size Size of the message.
1016 * @param c Connection this message belongs to (cannot be NULL).
1017 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1018 * @param cont Continuation to be called once CORE has taken the message.
1019 * @param cont_cls Closure for @c cont.
1021 * @return Handle to cancel the message before it is sent. Once cont is called
1022 * message has been sent and therefore the handle is no longer valid.
1024 struct MeshPeerQueue *
1025 GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
1026 struct MeshConnection *c, int fwd,
1027 GMP_sent cont, void *cont_cls)
1029 struct MeshPeerQueue *queue;
1033 LOG (GNUNET_ERROR_TYPE_DEBUG,
1034 "queue add %s %s towards %s (size %u) on c %p (%s)\n",
1035 GM_f2s (fwd), GM_m2s (type), GMP_2s(peer),
1036 size, c, GMC_2s (c));
1037 GNUNET_assert (NULL != c);
1039 if (NULL == peer->connections)
1041 /* We are not connected to this peer, ignore request. */
1042 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING %s not a neighbor\n", GMP_2s (peer));
1043 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1050 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1051 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1056 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1058 call_core = GMC_is_sendable (c, fwd);
1059 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1066 queue->callback = cont;
1067 queue->callback_cls = cont_cls;
1070 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1075 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1076 call_core = GNUNET_YES;
1079 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1081 LOG (GNUNET_ERROR_TYPE_DEBUG,
1082 "calling core tmt rdy towards %s for %u bytes\n",
1083 GMP_2s (peer), size);
1084 peer->core_transmit =
1085 GNUNET_CORE_notify_transmit_ready (core_handle,
1088 GNUNET_TIME_UNIT_FOREVER_REL,
1089 GNUNET_PEER_resolve2 (peer->id),
1093 queue->start_waiting = GNUNET_TIME_absolute_get ();
1097 LOG (GNUNET_ERROR_TYPE_DEBUG,
1098 "core tmt rdy towards %s already called\n",
1107 * Cancel all queued messages to a peer that belong to a certain connection.
1109 * @param peer Peer towards whom to cancel.
1110 * @param c Connection whose queued messages to cancel. Might be destroyed by
1111 * the sent continuation call.
1114 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1116 struct MeshPeerQueue *q;
1117 struct MeshPeerQueue *next;
1118 struct MeshPeerQueue *prev;
1120 for (q = peer->queue_head; NULL != q; q = next)
1125 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP_cancel_queue %s\n", GM_m2s (q->type));
1126 GMP_queue_destroy (q, GNUNET_YES);
1128 /* Get next from prev, q->next might be already freed:
1129 * queue destroy -> callback -> GMC_destroy -> cancel_queues -> here
1132 next = peer->queue_head;
1141 if (NULL == peer->queue_head)
1143 if (NULL != peer->core_transmit)
1145 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1146 peer->core_transmit = NULL;
1153 * Get the first transmittable message for a connection.
1155 * @param peer Neighboring peer.
1156 * @param c Connection.
1158 * @return First transmittable message.
1160 static struct MeshPeerQueue *
1161 connection_get_first_message (struct MeshPeer *peer, struct MeshConnection *c)
1163 struct MeshPeerQueue *q;
1165 for (q = peer->queue_head; NULL != q; q = q->next)
1169 if (queue_is_sendable (q))
1171 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1174 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1182 GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
1184 struct MeshPeerQueue *q;
1187 if (NULL != peer->core_transmit)
1189 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1190 return; /* Already unlocked */
1193 q = connection_get_first_message (peer, c);
1196 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1197 return; /* Nothing to transmit */
1201 peer->core_transmit =
1202 GNUNET_CORE_notify_transmit_ready (core_handle,
1205 GNUNET_TIME_UNIT_FOREVER_REL,
1206 GNUNET_PEER_resolve2 (peer->id),
1214 * Initialize the peer subsystem.
1216 * @param c Configuration.
1219 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1221 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1222 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1224 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1227 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1228 "MESH", "MAX_PEERS", "USING DEFAULT");
1233 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1240 LOG (GNUNET_ERROR_TYPE_WARNING,
1241 "\n***************************************\n"
1242 "Mesh is running with drop mode enabled.\n"
1243 "This is NOT a good idea!\n"
1244 "Remove the DROP_PERCENT option from your configuration.\n"
1245 "***************************************\n");
1248 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1249 NULL, /* Closure passed to MESH functions */
1250 &core_init, /* Call core_init once connected */
1251 &core_connect, /* Handle connects */
1252 &core_disconnect, /* remove peers on disconnects */
1253 NULL, /* Don't notify about all incoming messages */
1254 GNUNET_NO, /* For header only in notification */
1255 NULL, /* Don't notify about all outbound messages */
1256 GNUNET_NO, /* For header-only out notification */
1257 core_handlers); /* Register these handlers */
1258 if (NULL == core_handle)
1261 GNUNET_SCHEDULER_shutdown ();
1267 * Shut down the peer subsystem.
1272 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1274 if (core_handle != NULL)
1276 GNUNET_CORE_disconnect (core_handle);
1279 GNUNET_PEER_change_rc (myid, -1);
1283 * Retrieve the MeshPeer stucture associated with the peer, create one
1284 * and insert it in the appropriate structures if the peer is not known yet.
1286 * @param peer_id Full identity of the peer.
1288 * @return Existing or newly created peer structure.
1291 GMP_get (const struct GNUNET_PeerIdentity *peer_id)
1293 struct MeshPeer *peer;
1295 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1298 peer = GNUNET_new (struct MeshPeer);
1299 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1301 peer_delete_oldest ();
1303 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1304 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1305 peer->id = GNUNET_PEER_intern (peer_id);
1307 peer->last_contact = GNUNET_TIME_absolute_get();
1314 * Retrieve the MeshPeer stucture associated with the peer, create one
1315 * and insert it in the appropriate structures if the peer is not known yet.
1317 * @param peer Short identity of the peer.
1319 * @return Existing or newly created peer structure.
1322 GMP_get_short (const GNUNET_PEER_Id peer)
1324 return GMP_get (GNUNET_PEER_resolve2 (peer));
1329 * Try to establish a new connection to this peer (in its tunnel).
1330 * If the peer doesn't have any path to it yet, try to get one.
1331 * If the peer already has some path, send a CREATE CONNECTION towards it.
1333 * @param peer Peer to connect to.
1336 GMP_connect (struct MeshPeer *peer)
1338 struct MeshTunnel3 *t;
1339 struct MeshPeerPath *p;
1340 struct MeshConnection *c;
1343 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GMP_2s (peer));
1346 rerun_search = GNUNET_NO;
1348 if (NULL != peer->path_head)
1350 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1351 p = peer_get_best_path (peer);
1354 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1355 c = GMT_use_path (t, p);
1358 /* This case can happen when the path includes a first hop that is
1359 * not yet known to be connected.
1361 * This happens quite often during testing when running mesh
1362 * under valgrind: core connect notifications come very late and the
1363 * DHT result has already come and created a valid path.
1364 * In this case, the peer->connections hashmap will be NULL and
1365 * tunnel_use_path will not be able to create a connection from that
1368 * Re-running the DHT GET should give core time to callback.
1370 * GMT_use_path -> GMC_new -> register_neighbors takes care of
1371 * updating statistics about this issue.
1373 rerun_search = GNUNET_YES;
1377 GMC_send_create (c);
1383 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1387 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1389 GMD_search_stop (peer->search_h);
1390 peer->search_h = NULL;
1391 LOG (GNUNET_ERROR_TYPE_DEBUG,
1392 " Stopping DHT GET for peer %s\n",
1396 if (NULL == peer->search_h)
1398 const struct GNUNET_PeerIdentity *id;
1400 id = GNUNET_PEER_resolve2 (peer->id);
1401 LOG (GNUNET_ERROR_TYPE_DEBUG,
1402 " Starting DHT GET for peer %s\n", GMP_2s (peer));
1403 peer->search_h = GMD_search (id, &search_handler, peer);
1404 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
1405 GMT_change_cstate (t, MESH_TUNNEL3_SEARCHING);
1417 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel3 *t)
1424 * Chech whether there is a direct (core level) connection to peer.
1426 * @param peer Peer to check.
1428 * @return #GNUNET_YES if there is a direct connection.
1431 GMP_is_neighbor (const struct MeshPeer *peer)
1433 struct MeshPeerPath *path;
1435 if (NULL == peer->connections)
1438 for (path = peer->path_head; NULL != path; path = path->next)
1440 if (3 > path->length)
1444 GNUNET_break (0); /* Is not a neighbor but connections is not NULL */
1450 * Create and initialize a new tunnel towards a peer, in case it has none.
1451 * In case the peer already has a tunnel, nothing is done.
1453 * Does not generate any traffic, just creates the local data structures.
1455 * @param peer Peer towards which to create the tunnel.
1458 GMP_add_tunnel (struct MeshPeer *peer)
1460 if (NULL != peer->tunnel)
1462 peer->tunnel = GMT_new (peer);
1467 * Add a connection to a neighboring peer.
1469 * Store that the peer is the first hop of the connection in one
1470 * direction and that on peer disconnect the connection must be
1471 * notified and destroyed, for it will no longer be valid.
1473 * @param peer Peer to add connection to.
1474 * @param c Connection to add.
1476 * @return GNUNET_OK on success.
1479 GMP_add_connection (struct MeshPeer *peer,
1480 struct MeshConnection *c)
1483 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GMC_2s (c));
1484 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GMP_2s (peer));
1486 if (NULL == peer->connections)
1489 LOG (GNUNET_ERROR_TYPE_DEBUG,
1490 "Peer %s is not a neighbor!\n",
1492 return GNUNET_SYSERR;
1494 LOG (GNUNET_ERROR_TYPE_DEBUG,
1495 "peer %s ok, has %u connections.\n",
1496 GMP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1497 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1500 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1501 LOG (GNUNET_ERROR_TYPE_DEBUG,
1502 " now has %u connections.\n",
1503 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1504 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1511 * Add the path to the peer and update the path used to reach it in case this
1514 * @param peer Destination peer to add the path to.
1515 * @param path New path to add. Last peer must be the peer in arg 1.
1516 * Path will be either used of freed if already known.
1517 * @param trusted Do we trust that this path is real?
1519 * @return path if path was taken, pointer to existing duplicate if exists
1522 struct MeshPeerPath *
1523 GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *path,
1526 struct MeshPeerPath *aux;
1530 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1531 path->length, GMP_2s (peer));
1533 if ((NULL == peer) || (NULL == path))
1536 path_destroy (path);
1539 if (path->peers[path->length - 1] != peer->id)
1542 path_destroy (path);
1545 if (2 >= path->length && GNUNET_NO == trusted)
1547 /* Only allow CORE to tell us about direct paths */
1548 path_destroy (path);
1551 for (l = 1; l < path->length; l++)
1553 if (path->peers[l] == myid)
1555 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1556 for (l2 = 0; l2 < path->length - l; l2++)
1558 path->peers[l2] = path->peers[l + l2];
1562 path->peers = GNUNET_realloc (path->peers,
1563 path->length * sizeof (GNUNET_PEER_Id));
1567 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u]\n", path->length);
1569 l = path_get_length (path);
1572 path_destroy (path);
1576 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1577 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1579 l2 = path_get_length (aux);
1582 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1583 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1584 peer->path_tail, aux, path);
1589 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1592 path_destroy (path);
1597 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1605 * Add the path to the origin peer and update the path used to reach it in case
1606 * this is the shortest.
1607 * The path is given in peer_info -> destination, therefore we turn the path
1608 * upside down first.
1610 * @param peer Peer to add the path to, being the origin of the path.
1611 * @param path New path to add after being inversed.
1612 * Path will be either used or freed.
1613 * @param trusted Do we trust that this path is real?
1615 * @return path if path was taken, pointer to existing duplicate if exists
1618 struct MeshPeerPath *
1619 GMP_add_path_to_origin (struct MeshPeer *peer,
1620 struct MeshPeerPath *path,
1626 return GMP_add_path (peer, path, trusted);
1631 * Adds a path to the info of all the peers in the path
1633 * @param p Path to process.
1634 * @param confirmed Whether we know if the path works or not.
1637 GMP_add_path_to_all (const struct MeshPeerPath *p, int confirmed)
1641 /* TODO: invert and add */
1642 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1643 for (i++; i < p->length; i++)
1645 struct MeshPeer *aux;
1646 struct MeshPeerPath *copy;
1648 aux = GMP_get_short (p->peers[i]);
1649 copy = path_duplicate (p);
1650 copy->length = i + 1;
1651 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1657 * Remove any path to the peer that has the extact same peers as the one given.
1659 * @param peer Peer to remove the path from.
1660 * @param path Path to remove. Is always destroyed .
1663 GMP_remove_path (struct MeshPeer *peer, struct MeshPeerPath *path)
1665 struct MeshPeerPath *iter;
1666 struct MeshPeerPath *next;
1668 GNUNET_assert (myid == path->peers[0]);
1669 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1671 for (iter = peer->path_head; NULL != iter; iter = next)
1674 if (0 == memcmp (path->peers, iter->peers,
1675 sizeof (GNUNET_PEER_Id) * path->length))
1677 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
1678 path_destroy (iter);
1683 path_destroy (path);
1688 * Remove a connection from a neighboring peer.
1690 * @param peer Peer to remove connection from.
1691 * @param c Connection to remove.
1693 * @return GNUNET_OK on success.
1696 GMP_remove_connection (struct MeshPeer *peer,
1697 const struct MeshConnection *c)
1699 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GMC_2s (c));
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GMP_2s (peer));
1702 if (NULL == peer || NULL == peer->connections)
1704 LOG (GNUNET_ERROR_TYPE_DEBUG,
1705 "Peer %s is not a neighbor!\n",
1707 return GNUNET_SYSERR;
1709 LOG (GNUNET_ERROR_TYPE_DEBUG,
1710 "peer %s ok, has %u connections.\n",
1711 GMP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1713 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1719 * Start the DHT search for new paths towards the peer: we don't have
1720 * enough good connections.
1722 * @param peer Destination peer.
1725 GMP_start_search (struct MeshPeer *peer)
1727 if (NULL != peer->search_h)
1733 peer->search_h = GMD_search (GMP_get_id (peer), &search_handler, peer);
1738 * Stop the DHT search for new paths towards the peer: we already have
1739 * enough good connections.
1741 * @param peer Destination peer.
1744 GMP_stop_search (struct MeshPeer *peer)
1746 if (NULL == peer->search_h)
1752 GMD_search_stop (peer->search_h);
1753 peer->search_h = NULL;
1758 * Get the Full ID of a peer.
1760 * @param peer Peer to get from.
1762 * @return Full ID of peer.
1764 const struct GNUNET_PeerIdentity *
1765 GMP_get_id (const struct MeshPeer *peer)
1767 return GNUNET_PEER_resolve2 (peer->id);
1772 * Get the Short ID of a peer.
1774 * @param peer Peer to get from.
1776 * @return Short ID of peer.
1779 GMP_get_short_id (const struct MeshPeer *peer)
1786 * Get the tunnel towards a peer.
1788 * @param peer Peer to get from.
1790 * @return Tunnel towards peer.
1792 struct MeshTunnel3 *
1793 GMP_get_tunnel (const struct MeshPeer *peer)
1795 GNUNET_assert (NULL != peer->tunnel);
1796 return peer->tunnel;
1801 * Get the static string for a peer ID.
1805 * @return Static string for it's ID.
1808 GMP_2s (const struct MeshPeer *peer)
1812 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));