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 GMC_notify_broken (c, peer);
235 * Method called whenever a given peer connects.
238 * @param peer peer identity this notification is about
241 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
244 struct MeshPeerPath *path;
246 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer connected\n");
247 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&my_full_id));
251 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self)\n");
256 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (peer));
258 path->peers[1] = mp->id;
259 GNUNET_PEER_change_rc (mp->id, 1);
260 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
262 path->peers[0] = myid;
263 GNUNET_PEER_change_rc (myid, 1);
264 GMP_add_path (mp, path, GNUNET_YES);
266 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
272 * Method called whenever a peer disconnects.
275 * @param peer peer identity this notification is about
278 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
282 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer disconnected\n");
283 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
290 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self)\n");
292 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GMP_2s (p));
295 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
296 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
297 p->connections = NULL;
298 if (NULL != p->core_transmit)
300 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
301 p->core_transmit = NULL;
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},
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);
418 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
420 sizeof (struct GNUNET_MESH_ConnectionCreate) +
421 p->length * sizeof (struct GNUNET_PeerIdentity);
423 if (size < size_needed || NULL == buf)
428 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
429 msg->header.size = htons (size_needed);
430 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
431 msg->cid = *GMC_get_id (c);
433 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
434 for (i = 0; i < p->length; i++)
436 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
439 LOG (GNUNET_ERROR_TYPE_DEBUG,
440 "CONNECTION CREATE (%u bytes long) sent!\n",
447 * Creates a path ack message in buf and frees all unused resources.
449 * @param c Connection to send an ACK on.
450 * @param size number of bytes available in buf
451 * @param buf where the callee should write the message
453 * @return number of bytes written to buf
456 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
458 struct GNUNET_MESH_ConnectionACK *msg = buf;
460 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
461 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
466 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
467 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
468 msg->cid = *GMC_get_id (c);
471 /* TODO add signature */
473 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
474 return sizeof (struct GNUNET_MESH_ConnectionACK);
478 /******************************************************************************/
479 /******************************** STATIC ***********************************/
480 /******************************************************************************/
483 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
486 * @param key current key code
487 * @param value value in the hash map
488 * @return #GNUNET_YES if we should continue to iterate,
492 shutdown_tunnel (void *cls,
493 const struct GNUNET_PeerIdentity *key,
496 struct MeshPeer *p = value;
497 struct MeshTunnel3 *t = p->tunnel;
507 * Destroy the peer_info and free any allocated resources linked to it
509 * @param peer The peer_info to destroy.
511 * @return GNUNET_OK on success
514 peer_destroy (struct MeshPeer *peer)
516 struct GNUNET_PeerIdentity id;
517 struct MeshPeerPath *p;
518 struct MeshPeerPath *nextp;
520 GNUNET_PEER_resolve (peer->id, &id);
521 GNUNET_PEER_change_rc (peer->id, -1);
523 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
526 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
529 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
531 if (NULL != peer->search_h)
533 GMD_search_stop (peer->search_h);
539 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
543 GMT_destroy_empty (peer->tunnel);
550 * Returns if peer is used (has a tunnel or is neighbor).
552 * @param peer Peer to check.
554 * @return #GNUNET_YES if peer is in use.
557 peer_is_used (struct MeshPeer *peer)
559 struct MeshPeerPath *p;
561 if (NULL != peer->tunnel)
564 for (p = peer->path_head; NULL != p; p = p->next)
574 * Iterator over all the peers to get the oldest timestamp.
576 * @param cls Closure (unsued).
577 * @param key ID of the peer.
578 * @param value Peer_Info of the peer.
581 peer_get_oldest (void *cls,
582 const struct GNUNET_PeerIdentity *key,
585 struct MeshPeer *p = value;
586 struct GNUNET_TIME_Absolute *abs = cls;
588 /* Don't count active peers */
589 if (GNUNET_YES == peer_is_used (p))
592 if (abs->abs_value_us < p->last_contact.abs_value_us)
593 abs->abs_value_us = p->last_contact.abs_value_us;
600 * Iterator over all the peers to remove the oldest entry.
602 * @param cls Closure (unsued).
603 * @param key ID of the peer.
604 * @param value Peer_Info of the peer.
607 peer_timeout (void *cls,
608 const struct GNUNET_PeerIdentity *key,
611 struct MeshPeer *p = value;
612 struct GNUNET_TIME_Absolute *abs = cls;
614 LOG (GNUNET_ERROR_TYPE_WARNING,
615 "peer %s timeout\n", GNUNET_i2s (key));
617 if (p->last_contact.abs_value_us == abs->abs_value_us &&
618 GNUNET_NO == peer_is_used (p))
628 * Delete oldest unused peer.
631 peer_delete_oldest (void)
633 struct GNUNET_TIME_Absolute abs;
635 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
637 GNUNET_CONTAINER_multipeermap_iterate (peers,
640 GNUNET_CONTAINER_multipeermap_iterate (peers,
647 * Choose the best path towards a peer considering the tunnel properties.
649 * @param peer The destination peer.
651 * @return Best current known path towards the peer, if any.
653 static struct MeshPeerPath *
654 peer_get_best_path (const struct MeshPeer *peer)
656 struct MeshPeerPath *best_p;
657 struct MeshPeerPath *p;
658 unsigned int best_cost;
661 best_cost = UINT_MAX;
663 for (p = peer->path_head; NULL != p; p = p->next)
665 if (GNUNET_YES == GMT_is_path_used (peer->tunnel, p))
666 continue; /* If path is already in use, skip it. */
668 if ((cost = GMT_get_path_cost (peer->tunnel, p)) < best_cost)
679 queue_is_sendable (struct MeshPeerQueue *q)
681 /* Is PID-independent? */
684 case GNUNET_MESSAGE_TYPE_MESH_ACK:
685 case GNUNET_MESSAGE_TYPE_MESH_POLL:
689 if (GMC_is_sendable (q->c, q->fwd))
697 * Get first sendable message.
699 * @param peer The destination peer.
701 * @return Best current known path towards the peer, if any.
703 static struct MeshPeerQueue *
704 peer_get_first_message (const struct MeshPeer *peer)
706 struct MeshPeerQueue *q;
708 for (q = peer->queue_head; NULL != q; q = q->next)
710 if (queue_is_sendable (q))
719 * Function to process paths received for a new peer addition. The recorded
720 * paths form the initial tunnel, which can be optimized later.
721 * Called on each result obtained for the DHT search.
727 search_handler (void *cls, const struct MeshPeerPath *path)
729 struct MeshPeer *peer = cls;
730 unsigned int connection_count;
732 GMP_add_path_to_all (path, GNUNET_NO);
734 /* Count connections */
735 connection_count = GMT_count_connections (peer->tunnel);
737 /* If we already have 3 (or more (?!)) connections, it's enough */
738 if (3 <= connection_count)
741 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (peer->tunnel))
743 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
751 * Core callback to write a queued packet to core buffer
753 * @param cls Closure (peer info).
754 * @param size Number of bytes available in buf.
755 * @param buf Where the to write the message.
757 * @return number of bytes written to buf
759 * FIXME add GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE
762 queue_send (void *cls, size_t size, void *buf)
764 struct MeshPeer *peer = cls;
765 struct MeshConnection *c;
766 struct MeshPeerQueue *queue;
767 const struct GNUNET_PeerIdentity *dst_id;
770 peer->core_transmit = NULL;
771 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
773 if (NULL == buf || 0 == size)
775 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
780 queue = peer_get_first_message (peer);
783 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
788 dst_id = GNUNET_PEER_resolve2 (peer->id);
789 LOG (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
790 LOG (GNUNET_ERROR_TYPE_DEBUG, "* on connection %s\n", GMC_2s (c));
791 /* Check if buffer size is enough for the message */
792 if (queue->size > size)
794 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
795 peer->core_transmit =
796 GNUNET_CORE_notify_transmit_ready (core_handle,
799 GNUNET_TIME_UNIT_FOREVER_REL,
806 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
811 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
812 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
813 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
814 case GNUNET_MESSAGE_TYPE_MESH_KX:
815 case GNUNET_MESSAGE_TYPE_MESH_ACK:
816 case GNUNET_MESSAGE_TYPE_MESH_POLL:
817 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 GNUNET_MESH_DEBUG_M2S (queue->type));
820 data_size = send_core_data_raw (queue->cls, size, buf);
822 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
823 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
824 if (GMC_is_origin (c, GNUNET_YES))
825 data_size = send_core_connection_create (queue->c, size, buf);
827 data_size = send_core_data_raw (queue->cls, size, buf);
829 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
830 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
831 if (GMC_is_origin (c, GNUNET_NO) ||
832 GMC_is_origin (c, GNUNET_YES))
833 data_size = send_core_connection_ack (queue->c, size, buf);
835 data_size = send_core_data_raw (queue->cls, size, buf);
837 case GNUNET_MESSAGE_TYPE_MESH_DATA:
838 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
839 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
840 /* This should be encapsulted */
846 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n", queue->type);
850 if (0 < drop_percent &&
851 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
853 LOG (GNUNET_ERROR_TYPE_WARNING,
854 "Dropping message of type %s\n",
855 GNUNET_MESH_DEBUG_M2S (queue->type));
859 /* Free queue, but cls was freed by send_core_* */
860 GMP_queue_destroy (queue, GNUNET_NO);
862 /* If more data in queue, send next */
863 queue = peer_get_first_message (peer);
866 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
867 if (NULL == peer->core_transmit)
869 peer->core_transmit =
870 GNUNET_CORE_notify_transmit_ready(core_handle,
873 GNUNET_TIME_UNIT_FOREVER_REL,
878 queue->start_waiting = GNUNET_TIME_absolute_get ();
882 LOG (GNUNET_ERROR_TYPE_DEBUG,
883 "* tmt rdy called somewhere else\n");
885 // GMC_start_poll (); FIXME needed?
889 // GMC_stop_poll(); FIXME needed?
892 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
897 /******************************************************************************/
898 /******************************** API ***********************************/
899 /******************************************************************************/
903 * Free a transmission that was already queued with all resources
904 * associated to the request.
906 * @param queue Queue handler to cancel.
907 * @param clear_cls Is it necessary to free associated cls?
910 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
912 struct MeshPeer *peer;
915 GNUNET_assert (NULL != queue->c);
917 if (GNUNET_YES == clear_cls)
919 LOG (GNUNET_ERROR_TYPE_DEBUG, "# queue destroy type %s\n",
920 GNUNET_MESH_DEBUG_M2S (queue->type));
923 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
924 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
926 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
927 case GNUNET_MESSAGE_TYPE_MESH_ACK:
928 case GNUNET_MESSAGE_TYPE_MESH_POLL:
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_KX:
933 LOG (GNUNET_ERROR_TYPE_DEBUG, "# prebuilt message\n");;
934 GNUNET_free_non_null (queue->cls);
939 LOG (GNUNET_ERROR_TYPE_ERROR, "# type %s unknown!\n",
940 GNUNET_MESH_DEBUG_M2S (queue->type));
943 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
945 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
946 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
951 if (NULL != queue->callback)
953 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Calling callback\n");
954 queue->callback (queue->callback_cls,
955 queue->c, queue->type,
956 queue->fwd, queue->size,
957 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
965 * @brief Queue and pass message to core when possible.
967 * @param peer Peer towards which to queue the message.
968 * @param cls Closure (@c type dependant). It will be used by queue_send to
969 * build the message to be sent if not already prebuilt.
970 * @param type Type of the message, 0 for a raw message.
971 * @param size Size of the message.
972 * @param c Connection this message belongs to (cannot be NULL).
973 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
974 * @param cont Continuation to be called once CORE has taken the message.
975 * @param cont_cls Closure for @c cont.
977 * @return Handle to cancel the message before it is sent. Once cont is called
978 * message has been sent and therefore the handle is no longer valid.
980 struct MeshPeerQueue *
981 GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
982 struct MeshConnection *c, int fwd,
983 GMP_sent cont, void *cont_cls)
985 struct MeshPeerQueue *queue;
989 LOG (GNUNET_ERROR_TYPE_DEBUG,
990 "queue add %s %s towards %s (size %u) on c %p (%s)\n",
991 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), GMP_2s(peer),
992 size, c, GMC_2s (c));
993 GNUNET_assert (NULL != c);
995 if (NULL == peer->connections)
997 /* We are not connected to this peer, ignore request. */
1004 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1005 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1010 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1012 call_core = GMC_is_sendable (c, fwd);
1013 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1020 queue->callback = cont;
1021 queue->callback_cls = cont_cls;
1024 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1029 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1030 call_core = GNUNET_YES;
1033 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1035 LOG (GNUNET_ERROR_TYPE_DEBUG,
1036 "calling core tmt rdy towards %s for %u bytes\n",
1037 GMP_2s (peer), size);
1038 peer->core_transmit =
1039 GNUNET_CORE_notify_transmit_ready (core_handle,
1042 GNUNET_TIME_UNIT_FOREVER_REL,
1043 GNUNET_PEER_resolve2 (peer->id),
1047 queue->start_waiting = GNUNET_TIME_absolute_get ();
1051 LOG (GNUNET_ERROR_TYPE_DEBUG,
1052 "core tmt rdy towards %s already called\n",
1061 * Cancel all queued messages to a peer that belong to a certain connection.
1063 * @param peer Peer towards whom to cancel.
1064 * @param c Connection whose queued messages to cancel. Might be destroyed by
1065 * the sent continuation call.
1068 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1070 struct MeshPeerQueue *q;
1071 struct MeshPeerQueue *next;
1072 struct MeshPeerQueue *prev;
1074 for (q = peer->queue_head; NULL != q; q = next)
1079 LOG (GNUNET_ERROR_TYPE_DEBUG,
1080 "GMP_cancel_queue %s\n",
1081 GNUNET_MESH_DEBUG_M2S (q->type));
1082 GMP_queue_destroy (q, GNUNET_YES);
1084 /* Get next from prev, q->next might be already freed:
1085 * queue destroy -> callback -> GMC_destroy -> cancel_queues -> here
1088 next = peer->queue_head;
1097 if (NULL == peer->queue_head)
1099 if (NULL != peer->core_transmit)
1101 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1102 peer->core_transmit = NULL;
1109 * Get the first transmittable message for a connection.
1111 * @param peer Neighboring peer.
1112 * @param c Connection.
1114 * @return First transmittable message.
1116 static struct MeshPeerQueue *
1117 connection_get_first_message (struct MeshPeer *peer, struct MeshConnection *c)
1119 struct MeshPeerQueue *q;
1121 for (q = peer->queue_head; NULL != q; q = q->next)
1125 if (queue_is_sendable (q))
1127 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1130 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1138 GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
1140 struct MeshPeerQueue *q;
1143 if (NULL != peer->core_transmit)
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1146 return; /* Already unlocked */
1149 q = connection_get_first_message (peer, c);
1152 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1153 return; /* Nothing to transmit */
1157 peer->core_transmit =
1158 GNUNET_CORE_notify_transmit_ready (core_handle,
1161 GNUNET_TIME_UNIT_FOREVER_REL,
1162 GNUNET_PEER_resolve2 (peer->id),
1170 * Initialize the peer subsystem.
1172 * @param c Configuration.
1175 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1177 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1178 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1180 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1183 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1184 "MESH", "MAX_PEERS", "USING DEFAULT");
1189 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1196 LOG (GNUNET_ERROR_TYPE_WARNING,
1197 "\n***************************************\n"
1198 "Mesh is running with drop mode enabled.\n"
1199 "This is NOT a good idea!\n"
1200 "Remove the DROP_PERCENT option from your configuration.\n"
1201 "***************************************\n");
1204 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1205 NULL, /* Closure passed to MESH functions */
1206 &core_init, /* Call core_init once connected */
1207 &core_connect, /* Handle connects */
1208 &core_disconnect, /* remove peers on disconnects */
1209 NULL, /* Don't notify about all incoming messages */
1210 GNUNET_NO, /* For header only in notification */
1211 NULL, /* Don't notify about all outbound messages */
1212 GNUNET_NO, /* For header-only out notification */
1213 core_handlers); /* Register these handlers */
1214 if (NULL == core_handle)
1217 GNUNET_SCHEDULER_shutdown ();
1223 * Shut down the peer subsystem.
1228 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1230 if (core_handle != NULL)
1232 GNUNET_CORE_disconnect (core_handle);
1235 GNUNET_PEER_change_rc (myid, -1);
1239 * Retrieve the MeshPeer stucture associated with the peer, create one
1240 * and insert it in the appropriate structures if the peer is not known yet.
1242 * @param peer_id Full identity of the peer.
1244 * @return Existing or newly created peer structure.
1247 GMP_get (const struct GNUNET_PeerIdentity *peer_id)
1249 struct MeshPeer *peer;
1251 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1254 peer = GNUNET_new (struct MeshPeer);
1255 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1257 peer_delete_oldest ();
1259 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1260 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1261 peer->id = GNUNET_PEER_intern (peer_id);
1263 peer->last_contact = GNUNET_TIME_absolute_get();
1270 * Retrieve the MeshPeer stucture associated with the peer, create one
1271 * and insert it in the appropriate structures if the peer is not known yet.
1273 * @param peer Short identity of the peer.
1275 * @return Existing or newly created peer structure.
1278 GMP_get_short (const GNUNET_PEER_Id peer)
1280 return GMP_get (GNUNET_PEER_resolve2 (peer));
1285 * Try to establish a new connection to this peer (in its tunnel).
1286 * If the peer doesn't have any path to it yet, try to get one.
1287 * If the peer already has some path, send a CREATE CONNECTION towards it.
1289 * @param peer Peer to connect to.
1292 GMP_connect (struct MeshPeer *peer)
1294 struct MeshTunnel3 *t;
1295 struct MeshPeerPath *p;
1296 struct MeshConnection *c;
1299 LOG (GNUNET_ERROR_TYPE_DEBUG,
1300 "peer_connect towards %s\n",
1304 rerun_search = GNUNET_NO;
1306 if (NULL != peer->path_head)
1308 LOG (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
1309 p = peer_get_best_path (peer);
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1313 c = GMT_use_path (t, p);
1316 /* This case can happen when the path includes a first hop that is
1317 * not yet known to be connected.
1319 * This happens quite often during testing when running mesh
1320 * under valgrind: core connect notifications come very late and the
1321 * DHT result has already come and created a valid path.
1322 * In this case, the peer->connections hashmap will be NULL and
1323 * tunnel_use_path will not be able to create a connection from that
1326 * Re-running the DHT GET should give core time to callback.
1329 rerun_search = GNUNET_YES;
1333 GMC_send_create (c);
1339 LOG (GNUNET_ERROR_TYPE_DEBUG, "but is NULL!!\n");
1344 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1346 GMD_search_stop (peer->search_h);
1347 peer->search_h = NULL;
1348 LOG (GNUNET_ERROR_TYPE_DEBUG,
1349 " Stopping DHT GET for peer %s\n",
1353 if (NULL == peer->search_h)
1355 const struct GNUNET_PeerIdentity *id;
1357 id = GNUNET_PEER_resolve2 (peer->id);
1358 LOG (GNUNET_ERROR_TYPE_DEBUG,
1359 " Starting DHT GET for peer %s\n", GMP_2s (peer));
1360 peer->search_h = GMD_search (id, &search_handler, peer);
1361 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
1362 GMT_change_cstate (t, MESH_TUNNEL3_SEARCHING);
1374 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel3 *t)
1381 * Chech whether there is a direct (core level) connection to peer.
1383 * @param peer Peer to check.
1385 * @return #GNUNET_YES if there is a direct connection.
1388 GMP_is_neighbor (const struct MeshPeer *peer)
1390 struct MeshPeerPath *path;
1392 if (NULL == peer->connections)
1395 for (path = peer->path_head; NULL != path; path = path->next)
1397 if (3 > path->length)
1401 GNUNET_break (0); /* Is not a neighbor but connections is not NULL */
1407 * Create and initialize a new tunnel towards a peer, in case it has none.
1408 * In case the peer already has a tunnel, nothing is done.
1410 * Does not generate any traffic, just creates the local data structures.
1412 * @param peer Peer towards which to create the tunnel.
1415 GMP_add_tunnel (struct MeshPeer *peer)
1417 if (NULL != peer->tunnel)
1419 peer->tunnel = GMT_new (peer);
1424 * Add a connection to a neighboring peer.
1426 * Store that the peer is the first hop of the connection in one
1427 * direction and that on peer disconnect the connection must be
1428 * notified and destroyed, for it will no longer be valid.
1430 * @param peer Peer to add connection to.
1431 * @param c Connection to add.
1433 * @return GNUNET_OK on success.
1436 GMP_add_connection (struct MeshPeer *peer,
1437 struct MeshConnection *c)
1439 if (NULL == peer->connections)
1442 return GNUNET_SYSERR;
1444 return GNUNET_CONTAINER_multihashmap_put (peer->connections,
1447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1452 * Add the path to the peer and update the path used to reach it in case this
1455 * @param peer Destination peer to add the path to.
1456 * @param path New path to add. Last peer must be the peer in arg 1.
1457 * Path will be either used of freed if already known.
1458 * @param trusted Do we trust that this path is real?
1460 * @return path if path was taken, pointer to existing duplicate if exists
1463 struct MeshPeerPath *
1464 GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *path,
1467 struct MeshPeerPath *aux;
1471 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1472 path->length, GMP_2s (peer));
1474 if ((NULL == peer) || (NULL == path))
1477 path_destroy (path);
1480 if (path->peers[path->length - 1] != peer->id)
1483 path_destroy (path);
1486 if (2 >= path->length && GNUNET_NO == trusted)
1488 /* Only allow CORE to tell us about direct paths */
1489 path_destroy (path);
1492 for (l = 1; l < path->length; l++)
1494 if (path->peers[l] == myid)
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1497 for (l2 = 0; l2 < path->length - l; l2++)
1499 path->peers[l2] = path->peers[l + l2];
1503 path->peers = GNUNET_realloc (path->peers,
1504 path->length * sizeof (GNUNET_PEER_Id));
1508 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u]\n", path->length);
1510 l = path_get_length (path);
1513 path_destroy (path);
1517 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1518 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1520 l2 = path_get_length (aux);
1523 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1524 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1525 peer->path_tail, aux, path);
1530 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1532 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1533 path_destroy (path);
1538 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1540 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1546 * Add the path to the origin peer and update the path used to reach it in case
1547 * this is the shortest.
1548 * The path is given in peer_info -> destination, therefore we turn the path
1549 * upside down first.
1551 * @param peer Peer to add the path to, being the origin of the path.
1552 * @param path New path to add after being inversed.
1553 * Path will be either used or freed.
1554 * @param trusted Do we trust that this path is real?
1556 * @return path if path was taken, pointer to existing duplicate if exists
1559 struct MeshPeerPath *
1560 GMP_add_path_to_origin (struct MeshPeer *peer,
1561 struct MeshPeerPath *path,
1567 return GMP_add_path (peer, path, trusted);
1572 * Adds a path to the info of all the peers in the path
1574 * @param p Path to process.
1575 * @param confirmed Whether we know if the path works or not.
1578 GMP_add_path_to_all (const struct MeshPeerPath *p, int confirmed)
1582 /* TODO: invert and add */
1583 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1584 for (i++; i < p->length; i++)
1586 struct MeshPeer *aux;
1587 struct MeshPeerPath *copy;
1589 aux = GMP_get_short (p->peers[i]);
1590 copy = path_duplicate (p);
1591 copy->length = i + 1;
1592 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1598 * Remove a connection from a neighboring peer.
1600 * @param peer Peer to remove connection from.
1601 * @param c Connection to remove.
1603 * @return GNUNET_OK on success.
1606 GMP_remove_connection (struct MeshPeer *peer,
1607 const struct MeshConnection *c)
1609 if (NULL == peer || NULL == peer->connections)
1612 LOG (GNUNET_ERROR_TYPE_WARNING,
1613 "Peer %s is not a neighbor!\n",
1615 return GNUNET_SYSERR;
1617 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1623 * Start the DHT search for new paths towards the peer: we don't have
1624 * enough good connections.
1626 * @param peer Destination peer.
1629 GMP_start_search (struct MeshPeer *peer)
1631 if (NULL != peer->search_h)
1637 peer->search_h = GMD_search (GMP_get_id (peer), &search_handler, peer);
1642 * Stop the DHT search for new paths towards the peer: we already have
1643 * enough good connections.
1645 * @param peer Destination peer.
1648 GMP_stop_search (struct MeshPeer *peer)
1650 if (NULL == peer->search_h)
1656 GMD_search_stop (peer->search_h);
1657 peer->search_h = NULL;
1662 * Get the Full ID of a peer.
1664 * @param peer Peer to get from.
1666 * @return Full ID of peer.
1668 const struct GNUNET_PeerIdentity *
1669 GMP_get_id (const struct MeshPeer *peer)
1671 return GNUNET_PEER_resolve2 (peer->id);
1676 * Get the Short ID of a peer.
1678 * @param peer Peer to get from.
1680 * @return Short ID of peer.
1683 GMP_get_short_id (const struct MeshPeer *peer)
1690 * Get the tunnel towards a peer.
1692 * @param peer Peer to get from.
1694 * @return Tunnel towards peer.
1696 struct MeshTunnel3 *
1697 GMP_get_tunnel (const struct MeshPeer *peer)
1699 return peer->tunnel;
1704 * Get the static string for a peer ID.
1708 * @return Static string for it's ID.
1711 GMP_2s (const struct MeshPeer *peer)
1715 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));