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 * Method called whenever a given peer connects.
240 * @param peer peer identity this notification is about
243 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
246 struct MeshPeerPath *path;
248 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer connected\n");
249 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&my_full_id));
253 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self)\n");
258 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (peer));
260 path->peers[1] = mp->id;
261 GNUNET_PEER_change_rc (mp->id, 1);
262 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
264 path->peers[0] = myid;
265 GNUNET_PEER_change_rc (myid, 1);
266 GMP_add_path (mp, path, GNUNET_YES);
268 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
274 * Method called whenever a peer disconnects.
277 * @param peer peer identity this notification is about
280 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
284 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer disconnected\n");
285 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
292 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self: %s)\n", GMP_2s (p));
294 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GMP_2s (p));
297 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
298 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
299 p->connections = NULL;
300 if (NULL != p->core_transmit)
302 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
303 p->core_transmit = NULL;
305 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
312 * Functions to handle messages from core
314 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
315 {&GMC_handle_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
317 {&GMC_handle_confirm, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
318 sizeof (struct GNUNET_MESH_ConnectionACK)},
319 {&GMC_handle_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
320 sizeof (struct GNUNET_MESH_ConnectionBroken)},
321 {&GMC_handle_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
322 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
323 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_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},
330 {&GMC_handle_kx, GNUNET_MESSAGE_TYPE_MESH_KX, 0},
336 * To be called on core init/fail.
338 * @param cls Closure (config)
339 * @param identity the public identity of this peer
342 core_init (void *cls,
343 const struct GNUNET_PeerIdentity *identity)
345 const struct GNUNET_CONFIGURATION_Handle *c = cls;
348 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
349 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
351 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
352 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
353 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
354 GNUNET_CORE_disconnect (core_handle);
355 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
356 NULL, /* Closure passed to MESH functions */
357 &core_init, /* Call core_init once connected */
358 &core_connect, /* Handle connects */
359 &core_disconnect, /* remove peers on disconnects */
360 NULL, /* Don't notify about all incoming messages */
361 GNUNET_NO, /* For header only in notification */
362 NULL, /* Don't notify about all outbound messages */
363 GNUNET_NO, /* For header-only out notification */
364 core_handlers); /* Register these handlers */
373 * Core callback to write a pre-constructed data packet to core buffer
375 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
376 * @param size Number of bytes available in buf.
377 * @param buf Where the to write the message.
379 * @return number of bytes written to buf
382 send_core_data_raw (void *cls, size_t size, void *buf)
384 struct GNUNET_MessageHeader *msg = cls;
387 GNUNET_assert (NULL != msg);
388 total_size = ntohs (msg->size);
390 if (total_size > size)
395 memcpy (buf, msg, total_size);
402 * Function to send a create connection message to a peer.
404 * @param c Connection to create.
405 * @param size number of bytes available in buf
406 * @param buf where the callee should write the message
407 * @return number of bytes written to buf
410 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
412 struct GNUNET_MESH_ConnectionCreate *msg;
413 struct GNUNET_PeerIdentity *peer_ptr;
414 const struct MeshPeerPath *p = GMC_get_path (c);
421 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
423 sizeof (struct GNUNET_MESH_ConnectionCreate) +
424 p->length * sizeof (struct GNUNET_PeerIdentity);
426 if (size < size_needed || NULL == buf)
431 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
432 msg->header.size = htons (size_needed);
433 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
434 msg->cid = *GMC_get_id (c);
436 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
437 for (i = 0; i < p->length; i++)
439 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
442 LOG (GNUNET_ERROR_TYPE_DEBUG,
443 "CONNECTION CREATE (%u bytes long) sent!\n",
450 * Creates a path ack message in buf and frees all unused resources.
452 * @param c Connection to send an ACK on.
453 * @param size number of bytes available in buf
454 * @param buf where the callee should write the message
456 * @return number of bytes written to buf
459 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
461 struct GNUNET_MESH_ConnectionACK *msg = buf;
463 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
464 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
469 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
470 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
471 msg->cid = *GMC_get_id (c);
474 /* TODO add signature */
476 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
477 return sizeof (struct GNUNET_MESH_ConnectionACK);
481 /******************************************************************************/
482 /******************************** STATIC ***********************************/
483 /******************************************************************************/
486 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
489 * @param key current key code
490 * @param value value in the hash map
491 * @return #GNUNET_YES if we should continue to iterate,
495 shutdown_tunnel (void *cls,
496 const struct GNUNET_PeerIdentity *key,
499 struct MeshPeer *p = value;
500 struct MeshTunnel3 *t = p->tunnel;
510 * Destroy the peer_info and free any allocated resources linked to it
512 * @param peer The peer_info to destroy.
514 * @return GNUNET_OK on success
517 peer_destroy (struct MeshPeer *peer)
519 struct GNUNET_PeerIdentity id;
520 struct MeshPeerPath *p;
521 struct MeshPeerPath *nextp;
523 GNUNET_PEER_resolve (peer->id, &id);
524 GNUNET_PEER_change_rc (peer->id, -1);
526 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
529 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
532 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
534 if (NULL != peer->search_h)
536 GMD_search_stop (peer->search_h);
542 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
546 GMT_destroy_empty (peer->tunnel);
553 * Returns if peer is used (has a tunnel or is neighbor).
555 * @param peer Peer to check.
557 * @return #GNUNET_YES if peer is in use.
560 peer_is_used (struct MeshPeer *peer)
562 struct MeshPeerPath *p;
564 if (NULL != peer->tunnel)
567 for (p = peer->path_head; NULL != p; p = p->next)
577 * Iterator over all the peers to get the oldest timestamp.
579 * @param cls Closure (unsued).
580 * @param key ID of the peer.
581 * @param value Peer_Info of the peer.
584 peer_get_oldest (void *cls,
585 const struct GNUNET_PeerIdentity *key,
588 struct MeshPeer *p = value;
589 struct GNUNET_TIME_Absolute *abs = cls;
591 /* Don't count active peers */
592 if (GNUNET_YES == peer_is_used (p))
595 if (abs->abs_value_us < p->last_contact.abs_value_us)
596 abs->abs_value_us = p->last_contact.abs_value_us;
603 * Iterator over all the peers to remove the oldest entry.
605 * @param cls Closure (unsued).
606 * @param key ID of the peer.
607 * @param value Peer_Info of the peer.
610 peer_timeout (void *cls,
611 const struct GNUNET_PeerIdentity *key,
614 struct MeshPeer *p = value;
615 struct GNUNET_TIME_Absolute *abs = cls;
617 LOG (GNUNET_ERROR_TYPE_WARNING,
618 "peer %s timeout\n", GNUNET_i2s (key));
620 if (p->last_contact.abs_value_us == abs->abs_value_us &&
621 GNUNET_NO == peer_is_used (p))
631 * Delete oldest unused peer.
634 peer_delete_oldest (void)
636 struct GNUNET_TIME_Absolute abs;
638 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
640 GNUNET_CONTAINER_multipeermap_iterate (peers,
643 GNUNET_CONTAINER_multipeermap_iterate (peers,
650 * Choose the best (yet unused) path towards a peer,
651 * considering the tunnel properties.
653 * @param peer The destination peer.
655 * @return Best current known path towards the peer, if any.
657 static struct MeshPeerPath *
658 peer_get_best_path (const struct MeshPeer *peer)
660 struct MeshPeerPath *best_p;
661 struct MeshPeerPath *p;
662 unsigned int best_cost;
665 best_cost = UINT_MAX;
667 for (p = peer->path_head; NULL != p; p = p->next)
669 if (GNUNET_YES == GMT_is_path_used (peer->tunnel, p))
670 continue; /* If path is already in use, skip it. */
672 if ((cost = GMT_get_path_cost (peer->tunnel, p)) < best_cost)
683 queue_is_sendable (struct MeshPeerQueue *q)
685 /* Is PID-independent? */
688 case GNUNET_MESSAGE_TYPE_MESH_ACK:
689 case GNUNET_MESSAGE_TYPE_MESH_POLL:
693 if (GMC_is_sendable (q->c, q->fwd))
701 * Get first sendable message.
703 * @param peer The destination peer.
705 * @return Best current known path towards the peer, if any.
707 static struct MeshPeerQueue *
708 peer_get_first_message (const struct MeshPeer *peer)
710 struct MeshPeerQueue *q;
712 for (q = peer->queue_head; NULL != q; q = q->next)
714 if (queue_is_sendable (q))
723 * Function to process paths received for a new peer addition. The recorded
724 * paths form the initial tunnel, which can be optimized later.
725 * Called on each result obtained for the DHT search.
731 search_handler (void *cls, const struct MeshPeerPath *path)
733 struct MeshPeer *peer = cls;
734 unsigned int connection_count;
736 GMP_add_path_to_all (path, GNUNET_NO);
738 /* Count connections */
739 connection_count = GMT_count_connections (peer->tunnel);
741 /* If we already have 3 (or more (?!)) connections, it's enough */
742 if (3 <= connection_count)
745 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (peer->tunnel))
747 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
755 * Core callback to write a queued packet to core buffer
757 * @param cls Closure (peer info).
758 * @param size Number of bytes available in buf.
759 * @param buf Where the to write the message.
761 * @return number of bytes written to buf
764 queue_send (void *cls, size_t size, void *buf)
766 struct MeshPeer *peer = cls;
767 struct MeshConnection *c;
768 struct MeshPeerQueue *queue;
769 const struct GNUNET_PeerIdentity *dst_id;
772 peer->core_transmit = NULL;
773 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send towards %s (max %u)\n",
774 GMP_2s (peer), size);
776 if (NULL == buf || 0 == size)
778 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
783 queue = peer_get_first_message (peer);
786 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
791 dst_id = GNUNET_PEER_resolve2 (peer->id);
792 LOG (GNUNET_ERROR_TYPE_DEBUG, "* on connection %s\n", GMC_2s (c));
793 /* Check if buffer size is enough for the message */
794 if (queue->size > size)
796 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
797 peer->core_transmit =
798 GNUNET_CORE_notify_transmit_ready (core_handle,
801 GNUNET_TIME_UNIT_FOREVER_REL,
808 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
813 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
814 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
815 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
816 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
817 case GNUNET_MESSAGE_TYPE_MESH_KX:
818 case GNUNET_MESSAGE_TYPE_MESH_ACK:
819 case GNUNET_MESSAGE_TYPE_MESH_POLL:
820 LOG (GNUNET_ERROR_TYPE_DEBUG,
822 GM_m2s (queue->type));
823 data_size = send_core_data_raw (queue->cls, size, buf);
825 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
826 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
827 if (GMC_is_origin (c, GNUNET_YES))
828 data_size = send_core_connection_create (queue->c, size, buf);
830 data_size = send_core_data_raw (queue->cls, size, buf);
832 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
833 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
834 if (GMC_is_origin (c, GNUNET_NO) ||
835 GMC_is_origin (c, GNUNET_YES))
836 data_size = send_core_connection_ack (queue->c, size, buf);
838 data_size = send_core_data_raw (queue->cls, size, buf);
840 case GNUNET_MESSAGE_TYPE_MESH_DATA:
841 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
842 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
843 /* This should be encapsulted */
849 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n", queue->type);
853 if (0 < drop_percent &&
854 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
856 LOG (GNUNET_ERROR_TYPE_WARNING,
857 "Dropping message of type %s\n",
858 GM_m2s (queue->type));
862 /* Free queue, but cls was freed by send_core_* */
863 GMP_queue_destroy (queue, GNUNET_NO);
865 /* If more data in queue, send next */
866 queue = peer_get_first_message (peer);
869 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
870 if (NULL == peer->core_transmit)
872 peer->core_transmit =
873 GNUNET_CORE_notify_transmit_ready(core_handle,
876 GNUNET_TIME_UNIT_FOREVER_REL,
881 queue->start_waiting = GNUNET_TIME_absolute_get ();
885 LOG (GNUNET_ERROR_TYPE_DEBUG,
886 "* tmt rdy called somewhere else\n");
888 // GMC_start_poll (); FIXME needed?
892 // GMC_stop_poll(); FIXME needed?
895 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
900 /******************************************************************************/
901 /******************************** API ***********************************/
902 /******************************************************************************/
906 * Free a transmission that was already queued with all resources
907 * associated to the request.
909 * @param queue Queue handler to cancel.
910 * @param clear_cls Is it necessary to free associated cls?
913 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
915 struct MeshPeer *peer;
918 GNUNET_assert (NULL != queue->c);
920 if (GNUNET_YES == clear_cls)
922 LOG (GNUNET_ERROR_TYPE_DEBUG, "# queue destroy type %s\n",
923 GM_m2s (queue->type));
926 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
927 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
929 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
930 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
931 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
932 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
933 case GNUNET_MESSAGE_TYPE_MESH_KX:
934 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
935 case GNUNET_MESSAGE_TYPE_MESH_ACK:
936 case GNUNET_MESSAGE_TYPE_MESH_POLL:
937 GNUNET_free_non_null (queue->cls);
942 LOG (GNUNET_ERROR_TYPE_ERROR, "# type %s unknown!\n",
943 GM_m2s (queue->type));
946 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
948 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
949 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
954 if (NULL != queue->callback)
956 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Calling callback\n");
957 queue->callback (queue->callback_cls,
958 queue->c, queue->type,
959 queue->fwd, queue->size,
960 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
968 * @brief Queue and pass message to core when possible.
970 * @param peer Peer towards which to queue the message.
971 * @param cls Closure (@c type dependant). It will be used by queue_send to
972 * build the message to be sent if not already prebuilt.
973 * @param type Type of the message, 0 for a raw message.
974 * @param size Size of the message.
975 * @param c Connection this message belongs to (cannot be NULL).
976 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
977 * @param cont Continuation to be called once CORE has taken the message.
978 * @param cont_cls Closure for @c cont.
980 * @return Handle to cancel the message before it is sent. Once cont is called
981 * message has been sent and therefore the handle is no longer valid.
983 struct MeshPeerQueue *
984 GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
985 struct MeshConnection *c, int fwd,
986 GMP_sent cont, void *cont_cls)
988 struct MeshPeerQueue *queue;
992 LOG (GNUNET_ERROR_TYPE_DEBUG,
993 "queue add %s %s towards %s (size %u) on c %p (%s)\n",
994 GM_f2s (fwd), GM_m2s (type), GMP_2s(peer),
995 size, c, GMC_2s (c));
996 GNUNET_assert (NULL != c);
998 if (NULL == peer->connections)
1000 /* We are not connected to this peer, ignore request. */
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING %s not a neighbor\n", GMP_2s (peer));
1002 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1009 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1010 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1015 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1017 call_core = GMC_is_sendable (c, fwd);
1018 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1025 queue->callback = cont;
1026 queue->callback_cls = cont_cls;
1029 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1034 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1035 call_core = GNUNET_YES;
1038 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1040 LOG (GNUNET_ERROR_TYPE_DEBUG,
1041 "calling core tmt rdy towards %s for %u bytes\n",
1042 GMP_2s (peer), size);
1043 peer->core_transmit =
1044 GNUNET_CORE_notify_transmit_ready (core_handle,
1047 GNUNET_TIME_UNIT_FOREVER_REL,
1048 GNUNET_PEER_resolve2 (peer->id),
1052 queue->start_waiting = GNUNET_TIME_absolute_get ();
1056 LOG (GNUNET_ERROR_TYPE_DEBUG,
1057 "core tmt rdy towards %s already called\n",
1066 * Cancel all queued messages to a peer that belong to a certain connection.
1068 * @param peer Peer towards whom to cancel.
1069 * @param c Connection whose queued messages to cancel. Might be destroyed by
1070 * the sent continuation call.
1073 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1075 struct MeshPeerQueue *q;
1076 struct MeshPeerQueue *next;
1077 struct MeshPeerQueue *prev;
1079 for (q = peer->queue_head; NULL != q; q = next)
1084 LOG (GNUNET_ERROR_TYPE_DEBUG,
1085 "GMP_cancel_queue %s\n",
1087 GMP_queue_destroy (q, GNUNET_YES);
1089 /* Get next from prev, q->next might be already freed:
1090 * queue destroy -> callback -> GMC_destroy -> cancel_queues -> here
1093 next = peer->queue_head;
1102 if (NULL == peer->queue_head)
1104 if (NULL != peer->core_transmit)
1106 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1107 peer->core_transmit = NULL;
1114 * Get the first transmittable message for a connection.
1116 * @param peer Neighboring peer.
1117 * @param c Connection.
1119 * @return First transmittable message.
1121 static struct MeshPeerQueue *
1122 connection_get_first_message (struct MeshPeer *peer, struct MeshConnection *c)
1124 struct MeshPeerQueue *q;
1126 for (q = peer->queue_head; NULL != q; q = q->next)
1130 if (queue_is_sendable (q))
1132 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1135 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1143 GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
1145 struct MeshPeerQueue *q;
1148 if (NULL != peer->core_transmit)
1150 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1151 return; /* Already unlocked */
1154 q = connection_get_first_message (peer, c);
1157 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1158 return; /* Nothing to transmit */
1162 peer->core_transmit =
1163 GNUNET_CORE_notify_transmit_ready (core_handle,
1166 GNUNET_TIME_UNIT_FOREVER_REL,
1167 GNUNET_PEER_resolve2 (peer->id),
1175 * Initialize the peer subsystem.
1177 * @param c Configuration.
1180 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1183 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1185 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1188 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1189 "MESH", "MAX_PEERS", "USING DEFAULT");
1194 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1201 LOG (GNUNET_ERROR_TYPE_WARNING,
1202 "\n***************************************\n"
1203 "Mesh is running with drop mode enabled.\n"
1204 "This is NOT a good idea!\n"
1205 "Remove the DROP_PERCENT option from your configuration.\n"
1206 "***************************************\n");
1209 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1210 NULL, /* Closure passed to MESH functions */
1211 &core_init, /* Call core_init once connected */
1212 &core_connect, /* Handle connects */
1213 &core_disconnect, /* remove peers on disconnects */
1214 NULL, /* Don't notify about all incoming messages */
1215 GNUNET_NO, /* For header only in notification */
1216 NULL, /* Don't notify about all outbound messages */
1217 GNUNET_NO, /* For header-only out notification */
1218 core_handlers); /* Register these handlers */
1219 if (NULL == core_handle)
1222 GNUNET_SCHEDULER_shutdown ();
1228 * Shut down the peer subsystem.
1233 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1235 if (core_handle != NULL)
1237 GNUNET_CORE_disconnect (core_handle);
1240 GNUNET_PEER_change_rc (myid, -1);
1244 * Retrieve the MeshPeer stucture associated with the peer, create one
1245 * and insert it in the appropriate structures if the peer is not known yet.
1247 * @param peer_id Full identity of the peer.
1249 * @return Existing or newly created peer structure.
1252 GMP_get (const struct GNUNET_PeerIdentity *peer_id)
1254 struct MeshPeer *peer;
1256 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1259 peer = GNUNET_new (struct MeshPeer);
1260 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1262 peer_delete_oldest ();
1264 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1266 peer->id = GNUNET_PEER_intern (peer_id);
1268 peer->last_contact = GNUNET_TIME_absolute_get();
1275 * Retrieve the MeshPeer stucture associated with the peer, create one
1276 * and insert it in the appropriate structures if the peer is not known yet.
1278 * @param peer Short identity of the peer.
1280 * @return Existing or newly created peer structure.
1283 GMP_get_short (const GNUNET_PEER_Id peer)
1285 return GMP_get (GNUNET_PEER_resolve2 (peer));
1290 * Try to establish a new connection to this peer (in its tunnel).
1291 * If the peer doesn't have any path to it yet, try to get one.
1292 * If the peer already has some path, send a CREATE CONNECTION towards it.
1294 * @param peer Peer to connect to.
1297 GMP_connect (struct MeshPeer *peer)
1299 struct MeshTunnel3 *t;
1300 struct MeshPeerPath *p;
1301 struct MeshConnection *c;
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GMP_2s (peer));
1307 rerun_search = GNUNET_NO;
1309 if (NULL != peer->path_head)
1311 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1312 p = peer_get_best_path (peer);
1315 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1316 c = GMT_use_path (t, p);
1319 /* This case can happen when the path includes a first hop that is
1320 * not yet known to be connected.
1322 * This happens quite often during testing when running mesh
1323 * under valgrind: core connect notifications come very late and the
1324 * DHT result has already come and created a valid path.
1325 * In this case, the peer->connections hashmap will be NULL and
1326 * tunnel_use_path will not be able to create a connection from that
1329 * Re-running the DHT GET should give core time to callback.
1331 * GMT_use_path -> GMC_new -> register_neighbors takes care of
1332 * updating statistics about this issue.
1334 rerun_search = GNUNET_YES;
1338 GMC_send_create (c);
1344 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1348 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1350 GMD_search_stop (peer->search_h);
1351 peer->search_h = NULL;
1352 LOG (GNUNET_ERROR_TYPE_DEBUG,
1353 " Stopping DHT GET for peer %s\n",
1357 if (NULL == peer->search_h)
1359 const struct GNUNET_PeerIdentity *id;
1361 id = GNUNET_PEER_resolve2 (peer->id);
1362 LOG (GNUNET_ERROR_TYPE_DEBUG,
1363 " Starting DHT GET for peer %s\n", GMP_2s (peer));
1364 peer->search_h = GMD_search (id, &search_handler, peer);
1365 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
1366 GMT_change_cstate (t, MESH_TUNNEL3_SEARCHING);
1378 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel3 *t)
1385 * Chech whether there is a direct (core level) connection to peer.
1387 * @param peer Peer to check.
1389 * @return #GNUNET_YES if there is a direct connection.
1392 GMP_is_neighbor (const struct MeshPeer *peer)
1394 struct MeshPeerPath *path;
1396 if (NULL == peer->connections)
1399 for (path = peer->path_head; NULL != path; path = path->next)
1401 if (3 > path->length)
1405 GNUNET_break (0); /* Is not a neighbor but connections is not NULL */
1411 * Create and initialize a new tunnel towards a peer, in case it has none.
1412 * In case the peer already has a tunnel, nothing is done.
1414 * Does not generate any traffic, just creates the local data structures.
1416 * @param peer Peer towards which to create the tunnel.
1419 GMP_add_tunnel (struct MeshPeer *peer)
1421 if (NULL != peer->tunnel)
1423 peer->tunnel = GMT_new (peer);
1428 * Add a connection to a neighboring peer.
1430 * Store that the peer is the first hop of the connection in one
1431 * direction and that on peer disconnect the connection must be
1432 * notified and destroyed, for it will no longer be valid.
1434 * @param peer Peer to add connection to.
1435 * @param c Connection to add.
1437 * @return GNUNET_OK on success.
1440 GMP_add_connection (struct MeshPeer *peer,
1441 struct MeshConnection *c)
1443 if (NULL == peer->connections)
1446 return GNUNET_SYSERR;
1448 return GNUNET_CONTAINER_multihashmap_put (peer->connections,
1451 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1456 * Add the path to the peer and update the path used to reach it in case this
1459 * @param peer Destination peer to add the path to.
1460 * @param path New path to add. Last peer must be the peer in arg 1.
1461 * Path will be either used of freed if already known.
1462 * @param trusted Do we trust that this path is real?
1464 * @return path if path was taken, pointer to existing duplicate if exists
1467 struct MeshPeerPath *
1468 GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *path,
1471 struct MeshPeerPath *aux;
1475 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1476 path->length, GMP_2s (peer));
1478 if ((NULL == peer) || (NULL == path))
1481 path_destroy (path);
1484 if (path->peers[path->length - 1] != peer->id)
1487 path_destroy (path);
1490 if (2 >= path->length && GNUNET_NO == trusted)
1492 /* Only allow CORE to tell us about direct paths */
1493 path_destroy (path);
1496 for (l = 1; l < path->length; l++)
1498 if (path->peers[l] == myid)
1500 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1501 for (l2 = 0; l2 < path->length - l; l2++)
1503 path->peers[l2] = path->peers[l + l2];
1507 path->peers = GNUNET_realloc (path->peers,
1508 path->length * sizeof (GNUNET_PEER_Id));
1512 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u]\n", path->length);
1514 l = path_get_length (path);
1517 path_destroy (path);
1521 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1522 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1524 l2 = path_get_length (aux);
1527 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1528 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1529 peer->path_tail, aux, path);
1534 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1536 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1537 path_destroy (path);
1542 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1550 * Add the path to the origin peer and update the path used to reach it in case
1551 * this is the shortest.
1552 * The path is given in peer_info -> destination, therefore we turn the path
1553 * upside down first.
1555 * @param peer Peer to add the path to, being the origin of the path.
1556 * @param path New path to add after being inversed.
1557 * Path will be either used or freed.
1558 * @param trusted Do we trust that this path is real?
1560 * @return path if path was taken, pointer to existing duplicate if exists
1563 struct MeshPeerPath *
1564 GMP_add_path_to_origin (struct MeshPeer *peer,
1565 struct MeshPeerPath *path,
1571 return GMP_add_path (peer, path, trusted);
1576 * Adds a path to the info of all the peers in the path
1578 * @param p Path to process.
1579 * @param confirmed Whether we know if the path works or not.
1582 GMP_add_path_to_all (const struct MeshPeerPath *p, int confirmed)
1586 /* TODO: invert and add */
1587 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1588 for (i++; i < p->length; i++)
1590 struct MeshPeer *aux;
1591 struct MeshPeerPath *copy;
1593 aux = GMP_get_short (p->peers[i]);
1594 copy = path_duplicate (p);
1595 copy->length = i + 1;
1596 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1602 * Remove any path to the peer that has the extact same peers as the one given.
1604 * @param peer Peer to remove the path from.
1605 * @param path Path to remove. Is always destroyed .
1608 GMP_remove_path (struct MeshPeer *peer, struct MeshPeerPath *path)
1610 struct MeshPeerPath *iter;
1611 struct MeshPeerPath *next;
1613 GNUNET_assert (myid == path->peers[0]);
1614 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1616 for (iter = peer->path_head; NULL != iter; iter = next)
1619 if (0 == memcmp (path->peers, iter->peers,
1620 sizeof (GNUNET_PEER_Id) * path->length))
1622 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
1623 path_destroy (iter);
1628 path_destroy (path);
1633 * Remove a connection from a neighboring peer.
1635 * @param peer Peer to remove connection from.
1636 * @param c Connection to remove.
1638 * @return GNUNET_OK on success.
1641 GMP_remove_connection (struct MeshPeer *peer,
1642 const struct MeshConnection *c)
1644 if (NULL == peer || NULL == peer->connections)
1646 LOG (GNUNET_ERROR_TYPE_DEBUG,
1647 "Peer %s is not a neighbor!\n",
1649 return GNUNET_SYSERR;
1651 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1657 * Start the DHT search for new paths towards the peer: we don't have
1658 * enough good connections.
1660 * @param peer Destination peer.
1663 GMP_start_search (struct MeshPeer *peer)
1665 if (NULL != peer->search_h)
1671 peer->search_h = GMD_search (GMP_get_id (peer), &search_handler, peer);
1676 * Stop the DHT search for new paths towards the peer: we already have
1677 * enough good connections.
1679 * @param peer Destination peer.
1682 GMP_stop_search (struct MeshPeer *peer)
1684 if (NULL == peer->search_h)
1690 GMD_search_stop (peer->search_h);
1691 peer->search_h = NULL;
1696 * Get the Full ID of a peer.
1698 * @param peer Peer to get from.
1700 * @return Full ID of peer.
1702 const struct GNUNET_PeerIdentity *
1703 GMP_get_id (const struct MeshPeer *peer)
1705 return GNUNET_PEER_resolve2 (peer->id);
1710 * Get the Short ID of a peer.
1712 * @param peer Peer to get from.
1714 * @return Short ID of peer.
1717 GMP_get_short_id (const struct MeshPeer *peer)
1724 * Get the tunnel towards a peer.
1726 * @param peer Peer to get from.
1728 * @return Tunnel towards peer.
1730 struct MeshTunnel3 *
1731 GMP_get_tunnel (const struct MeshPeer *peer)
1733 GNUNET_assert (NULL != peer->tunnel);
1734 return peer->tunnel;
1739 * Get the static string for a peer ID.
1743 * @return Static string for it's ID.
1746 GMP_2s (const struct MeshPeer *peer)
1750 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));