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);
524 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
527 LOG (GNUNET_ERROR_TYPE_WARNING,
528 "removing peer %s, not in peermap\n", GNUNET_i2s (&id));
530 if (NULL != peer->search_h)
532 GMD_search_stop (peer->search_h);
538 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
542 GMT_destroy_empty (peer->tunnel);
549 * Returns if peer is used (has a tunnel or is neighbor).
551 * @param peer Peer to check.
553 * @return #GNUNET_YES if peer is in use.
556 peer_is_used (struct MeshPeer *peer)
558 struct MeshPeerPath *p;
560 if (NULL != peer->tunnel)
563 for (p = peer->path_head; NULL != p; p = p->next)
573 * Iterator over all the peers to get the oldest timestamp.
575 * @param cls Closure (unsued).
576 * @param key ID of the peer.
577 * @param value Peer_Info of the peer.
580 peer_get_oldest (void *cls,
581 const struct GNUNET_PeerIdentity *key,
584 struct MeshPeer *p = value;
585 struct GNUNET_TIME_Absolute *abs = cls;
587 /* Don't count active peers */
588 if (GNUNET_YES == peer_is_used (p))
591 if (abs->abs_value_us < p->last_contact.abs_value_us)
592 abs->abs_value_us = p->last_contact.abs_value_us;
599 * Iterator over all the peers to remove the oldest entry.
601 * @param cls Closure (unsued).
602 * @param key ID of the peer.
603 * @param value Peer_Info of the peer.
606 peer_timeout (void *cls,
607 const struct GNUNET_PeerIdentity *key,
610 struct MeshPeer *p = value;
611 struct GNUNET_TIME_Absolute *abs = cls;
613 if (p->last_contact.abs_value_us == abs->abs_value_us &&
614 GNUNET_NO == peer_is_used (p))
624 * Delete oldest unused peer.
627 peer_delete_oldest (void)
629 struct GNUNET_TIME_Absolute abs;
631 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
633 GNUNET_CONTAINER_multipeermap_iterate (peers,
636 GNUNET_CONTAINER_multipeermap_iterate (peers,
643 * Choose the best path towards a peer considering the tunnel properties.
645 * @param peer The destination peer.
647 * @return Best current known path towards the peer, if any.
649 static struct MeshPeerPath *
650 peer_get_best_path (const struct MeshPeer *peer)
652 struct MeshPeerPath *best_p;
653 struct MeshPeerPath *p;
654 unsigned int best_cost;
657 best_cost = UINT_MAX;
659 for (p = peer->path_head; NULL != p; p = p->next)
661 if (GNUNET_YES == GMT_is_path_used (peer->tunnel, p))
662 continue; /* If path is already in use, skip it. */
664 if ((cost = GMT_get_path_cost (peer->tunnel, p)) < best_cost)
675 queue_is_sendable (struct MeshPeerQueue *q)
677 /* Is PID-independent? */
680 case GNUNET_MESSAGE_TYPE_MESH_ACK:
681 case GNUNET_MESSAGE_TYPE_MESH_POLL:
685 if (GMC_is_sendable (q->c, q->fwd))
693 * Get first sendable message.
695 * @param peer The destination peer.
697 * @return Best current known path towards the peer, if any.
699 static struct MeshPeerQueue *
700 peer_get_first_message (const struct MeshPeer *peer)
702 struct MeshPeerQueue *q;
704 for (q = peer->queue_head; NULL != q; q = q->next)
706 if (queue_is_sendable (q))
715 * Function to process paths received for a new peer addition. The recorded
716 * paths form the initial tunnel, which can be optimized later.
717 * Called on each result obtained for the DHT search.
723 search_handler (void *cls, const struct MeshPeerPath *path)
725 struct MeshPeer *peer = cls;
726 unsigned int connection_count;
728 GMP_add_path_to_all (path, GNUNET_NO);
730 /* Count connections */
731 connection_count = GMT_count_connections (peer->tunnel);
733 /* If we already have 3 (or more (?!)) connections, it's enough */
734 if (3 <= connection_count)
737 if (MESH_TUNNEL3_SEARCHING == GMT_get_state (peer->tunnel))
739 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
747 * Core callback to write a queued packet to core buffer
749 * @param cls Closure (peer info).
750 * @param size Number of bytes available in buf.
751 * @param buf Where the to write the message.
753 * @return number of bytes written to buf
756 queue_send (void *cls, size_t size, void *buf)
758 struct MeshPeer *peer = cls;
759 struct MeshConnection *c;
760 struct MeshPeerQueue *queue;
761 const struct GNUNET_PeerIdentity *dst_id;
764 peer->core_transmit = NULL;
765 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
767 if (NULL == buf || 0 == size)
769 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
774 queue = peer_get_first_message (peer);
777 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
782 dst_id = GNUNET_PEER_resolve2 (peer->id);
783 LOG (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
784 /* Check if buffer size is enough for the message */
785 if (queue->size > size)
787 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
788 peer->core_transmit =
789 GNUNET_CORE_notify_transmit_ready (core_handle,
792 GNUNET_TIME_UNIT_FOREVER_REL,
799 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
804 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
805 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
806 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
807 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
808 case GNUNET_MESSAGE_TYPE_MESH_KX:
809 case GNUNET_MESSAGE_TYPE_MESH_ACK:
810 case GNUNET_MESSAGE_TYPE_MESH_POLL:
811 LOG (GNUNET_ERROR_TYPE_DEBUG,
813 GNUNET_MESH_DEBUG_M2S (queue->type));
814 data_size = send_core_data_raw (queue->cls, size, buf);
816 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
817 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
818 if (GMC_is_origin (c, GNUNET_YES))
819 data_size = send_core_connection_create (queue->c, size, buf);
821 data_size = send_core_data_raw (queue->cls, size, buf);
823 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
824 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
825 if (GMC_is_origin (c, GNUNET_NO) ||
826 GMC_is_origin (c, GNUNET_YES))
827 data_size = send_core_connection_ack (queue->c, size, buf);
829 data_size = send_core_data_raw (queue->cls, size, buf);
831 case GNUNET_MESSAGE_TYPE_MESH_DATA:
832 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
833 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
834 /* This should be encapsulted */
840 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
845 if (0 < drop_percent &&
846 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
848 LOG (GNUNET_ERROR_TYPE_WARNING,
849 "Dropping message of type %s\n",
850 GNUNET_MESH_DEBUG_M2S (queue->type));
854 /* Free queue, but cls was freed by send_core_* */
855 GMP_queue_destroy (queue, GNUNET_NO);
857 /* If more data in queue, send next */
858 queue = peer_get_first_message (peer);
861 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
862 if (NULL == peer->core_transmit)
864 peer->core_transmit =
865 GNUNET_CORE_notify_transmit_ready(core_handle,
868 GNUNET_TIME_UNIT_FOREVER_REL,
873 queue->start_waiting = GNUNET_TIME_absolute_get ();
877 LOG (GNUNET_ERROR_TYPE_DEBUG,
878 "* tmt rdy called somewhere else\n");
880 // GMC_start_poll (); FIXME needed?
884 // GMC_stop_poll(); FIXME needed?
887 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
892 /******************************************************************************/
893 /******************************** API ***********************************/
894 /******************************************************************************/
898 * Free a transmission that was already queued with all resources
899 * associated to the request.
901 * @param queue Queue handler to cancel.
902 * @param clear_cls Is it necessary to free associated cls?
905 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
907 struct MeshPeer *peer;
910 GNUNET_assert (NULL != queue->c);
912 if (GNUNET_YES == clear_cls)
914 LOG (GNUNET_ERROR_TYPE_DEBUG, "# queue destroy type %s\n",
915 GNUNET_MESH_DEBUG_M2S (queue->type));
918 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
919 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
920 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
922 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
923 case GNUNET_MESSAGE_TYPE_MESH_ACK:
924 case GNUNET_MESSAGE_TYPE_MESH_POLL:
925 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
926 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
927 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
928 case GNUNET_MESSAGE_TYPE_MESH_KX:
929 LOG (GNUNET_ERROR_TYPE_DEBUG, "# prebuilt message\n");;
930 GNUNET_free_non_null (queue->cls);
935 LOG (GNUNET_ERROR_TYPE_ERROR, "# type %s unknown!\n",
936 GNUNET_MESH_DEBUG_M2S (queue->type));
939 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
941 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
942 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
947 if (NULL != queue->callback)
949 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Calling callback\n");
950 queue->callback (queue->callback_cls,
951 queue->c, queue->type,
952 queue->fwd, queue->size,
953 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
961 * @brief Queue and pass message to core when possible.
963 * @param peer Peer towards which to queue the message.
964 * @param cls Closure (@c type dependant). It will be used by queue_send to
965 * build the message to be sent if not already prebuilt.
966 * @param type Type of the message, 0 for a raw message.
967 * @param size Size of the message.
968 * @param c Connection this message belongs to (cannot be NULL).
969 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
970 * @param cont Continuation to be called once CORE has taken the message.
971 * @param cont_cls Closure for @c cont.
973 * @return Handle to cancel the message before it is sent. Once cont is called
974 * message has been sent and therefore the handle is no longer valid.
976 struct MeshPeerQueue *
977 GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
978 struct MeshConnection *c, int fwd,
979 GMP_sent cont, void *cont_cls)
981 struct MeshPeerQueue *queue;
985 LOG (GNUNET_ERROR_TYPE_DEBUG,
986 "queue add %s %s towards %s (size %u) on c %p (%s)\n",
987 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), GMP_2s(peer),
988 size, c, GMC_2s (c));
989 GNUNET_assert (NULL != c);
991 if (NULL == peer->connections)
993 /* We are not connected to this peer, ignore request. */
1000 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1001 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1006 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1008 call_core = GMC_is_sendable (c, fwd);
1009 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1016 queue->callback = cont;
1017 queue->callback_cls = cont_cls;
1020 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1025 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1026 call_core = GNUNET_YES;
1029 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1031 LOG (GNUNET_ERROR_TYPE_DEBUG,
1032 "calling core tmt rdy towards %s for %u bytes\n",
1033 GMP_2s (peer), size);
1034 peer->core_transmit =
1035 GNUNET_CORE_notify_transmit_ready (core_handle,
1038 GNUNET_TIME_UNIT_FOREVER_REL,
1039 GNUNET_PEER_resolve2 (peer->id),
1043 queue->start_waiting = GNUNET_TIME_absolute_get ();
1047 LOG (GNUNET_ERROR_TYPE_DEBUG,
1048 "core tmt rdy towards %s already called\n",
1057 * Cancel all queued messages to a peer that belong to a certain connection.
1059 * @param peer Peer towards whom to cancel.
1060 * @param c Connection whose queued messages to cancel. Might be destroyed by
1061 * the sent continuation call.
1064 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1066 struct MeshPeerQueue *q;
1067 struct MeshPeerQueue *next;
1068 struct MeshPeerQueue *prev;
1070 for (q = peer->queue_head; NULL != q; q = next)
1075 LOG (GNUNET_ERROR_TYPE_DEBUG,
1076 "GMP_cancel_queue %s\n",
1077 GNUNET_MESH_DEBUG_M2S (q->type));
1078 GMP_queue_destroy (q, GNUNET_YES);
1080 /* Get next from prev, q->next might be already freed:
1081 * queue destroy -> callback -> GMC_destroy -> cancel_queues -> here
1084 next = peer->queue_head;
1093 if (NULL == peer->queue_head)
1095 if (NULL != peer->core_transmit)
1097 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1098 peer->core_transmit = NULL;
1105 * Get the first transmittable message for a connection.
1107 * @param peer Neighboring peer.
1108 * @param c Connection.
1110 * @return First transmittable message.
1112 static struct MeshPeerQueue *
1113 connection_get_first_message (struct MeshPeer *peer, struct MeshConnection *c)
1115 struct MeshPeerQueue *q;
1117 for (q = peer->queue_head; NULL != q; q = q->next)
1121 if (queue_is_sendable (q))
1123 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1126 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1134 GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
1136 struct MeshPeerQueue *q;
1139 if (NULL != peer->core_transmit)
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1142 return; /* Already unlocked */
1145 q = connection_get_first_message (peer, c);
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1149 return; /* Nothing to transmit */
1153 peer->core_transmit =
1154 GNUNET_CORE_notify_transmit_ready (core_handle,
1157 GNUNET_TIME_UNIT_FOREVER_REL,
1158 GNUNET_PEER_resolve2 (peer->id),
1166 * Initialize the peer subsystem.
1168 * @param c Configuration.
1171 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1173 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1174 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1176 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1179 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1180 "MESH", "MAX_PEERS", "USING DEFAULT");
1185 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1192 LOG (GNUNET_ERROR_TYPE_WARNING,
1193 "\n***************************************\n"
1194 "Mesh is running with drop mode enabled.\n"
1195 "This is NOT a good idea!\n"
1196 "Remove the DROP_PERCENT option from your configuration.\n"
1197 "***************************************\n");
1200 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1201 NULL, /* Closure passed to MESH functions */
1202 &core_init, /* Call core_init once connected */
1203 &core_connect, /* Handle connects */
1204 &core_disconnect, /* remove peers on disconnects */
1205 NULL, /* Don't notify about all incoming messages */
1206 GNUNET_NO, /* For header only in notification */
1207 NULL, /* Don't notify about all outbound messages */
1208 GNUNET_NO, /* For header-only out notification */
1209 core_handlers); /* Register these handlers */
1210 if (NULL == core_handle)
1213 GNUNET_SCHEDULER_shutdown ();
1219 * Shut down the peer subsystem.
1224 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1226 if (core_handle != NULL)
1228 GNUNET_CORE_disconnect (core_handle);
1231 GNUNET_PEER_change_rc (myid, -1);
1235 * Retrieve the MeshPeer stucture associated with the peer, create one
1236 * and insert it in the appropriate structures if the peer is not known yet.
1238 * @param peer_id Full identity of the peer.
1240 * @return Existing or newly created peer structure.
1243 GMP_get (const struct GNUNET_PeerIdentity *peer_id)
1245 struct MeshPeer *peer;
1247 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1250 peer = GNUNET_new (struct MeshPeer);
1251 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1253 peer_delete_oldest ();
1255 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1257 peer->id = GNUNET_PEER_intern (peer_id);
1259 peer->last_contact = GNUNET_TIME_absolute_get();
1266 * Retrieve the MeshPeer stucture associated with the peer, create one
1267 * and insert it in the appropriate structures if the peer is not known yet.
1269 * @param peer Short identity of the peer.
1271 * @return Existing or newly created peer structure.
1274 GMP_get_short (const GNUNET_PEER_Id peer)
1276 return GMP_get (GNUNET_PEER_resolve2 (peer));
1281 * Try to establish a new connection to this peer (in its tunnel).
1282 * If the peer doesn't have any path to it yet, try to get one.
1283 * If the peer already has some path, send a CREATE CONNECTION towards it.
1285 * @param peer Peer to connect to.
1288 GMP_connect (struct MeshPeer *peer)
1290 struct MeshTunnel3 *t;
1291 struct MeshPeerPath *p;
1292 struct MeshConnection *c;
1295 LOG (GNUNET_ERROR_TYPE_DEBUG,
1296 "peer_connect towards %s\n",
1300 rerun_search = GNUNET_NO;
1302 if (NULL != peer->path_head)
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
1305 p = peer_get_best_path (peer);
1308 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1309 c = GMT_use_path (t, p);
1312 /* This case can happen when the path includes a first hop that is
1313 * not yet known to be connected.
1315 * This happens quite often during testing when running mesh
1316 * under valgrind: core connect notifications come very late and the
1317 * DHT result has already come and created a valid path.
1318 * In this case, the peer->connections hashmap will be NULL and
1319 * tunnel_use_path will not be able to create a connection from that
1322 * Re-running the DHT GET should give core time to callback.
1325 rerun_search = GNUNET_YES;
1329 GMC_send_create (c);
1335 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1337 GMD_search_stop (peer->search_h);
1338 peer->search_h = NULL;
1339 LOG (GNUNET_ERROR_TYPE_DEBUG,
1340 " Stopping DHT GET for peer %s\n",
1344 if (NULL == peer->search_h)
1346 const struct GNUNET_PeerIdentity *id;
1348 id = GNUNET_PEER_resolve2 (peer->id);
1349 LOG (GNUNET_ERROR_TYPE_DEBUG,
1350 " Starting DHT GET for peer %s\n", GMP_2s (peer));
1351 peer->search_h = GMD_search (id, &search_handler, peer);
1352 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
1353 GMT_change_state (t, MESH_TUNNEL3_SEARCHING);
1365 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel3 *t)
1372 * Chech whether there is a direct (core level) connection to peer.
1374 * @param peer Peer to check.
1376 * @return #GNUNET_YES if there is a direct connection.
1379 GMP_is_neighbor (const struct MeshPeer *peer)
1381 struct MeshPeerPath *path;
1383 if (NULL == peer->connections)
1386 for (path = peer->path_head; NULL != path; path = path->next)
1388 if (3 > path->length)
1392 GNUNET_break (0); /* Is not a neighbor but connections is not NULL */
1398 * Create and initialize a new tunnel towards a peer, in case it has none.
1399 * In case the peer already has a tunnel, nothing is done.
1401 * Does not generate any traffic, just creates the local data structures.
1403 * @param peer Peer towards which to create the tunnel.
1406 GMP_add_tunnel (struct MeshPeer *peer)
1408 if (NULL != peer->tunnel)
1410 peer->tunnel = GMT_new (peer);
1415 * Add a connection to a neighboring peer.
1417 * Store that the peer is the first hop of the connection in one
1418 * direction and that on peer disconnect the connection must be
1419 * notified and destroyed, for it will no longer be valid.
1421 * @param peer Peer to add connection to.
1422 * @param c Connection to add.
1424 * @return GNUNET_OK on success.
1427 GMP_add_connection (struct MeshPeer *peer,
1428 struct MeshConnection *c)
1430 if (NULL == peer->connections)
1433 return GNUNET_SYSERR;
1435 return GNUNET_CONTAINER_multihashmap_put (peer->connections,
1438 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1443 * Add the path to the peer and update the path used to reach it in case this
1446 * @param peer Destination peer to add the path to.
1447 * @param path New path to add. Last peer must be the peer in arg 1.
1448 * Path will be either used of freed if already known.
1449 * @param trusted Do we trust that this path is real?
1451 * @return path if path was taken, pointer to existing duplicate if exists
1454 struct MeshPeerPath *
1455 GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *path,
1458 struct MeshPeerPath *aux;
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1463 path->length, GMP_2s (peer));
1465 if ((NULL == peer) || (NULL == path))
1468 path_destroy (path);
1471 if (path->peers[path->length - 1] != peer->id)
1474 path_destroy (path);
1477 if (2 >= path->length && GNUNET_NO == trusted)
1479 /* Only allow CORE to tell us about direct paths */
1480 path_destroy (path);
1483 for (l = 1; l < path->length; l++)
1485 if (path->peers[l] == myid)
1487 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1488 for (l2 = 0; l2 < path->length - l; l2++)
1490 path->peers[l2] = path->peers[l + l2];
1494 path->peers = GNUNET_realloc (path->peers,
1495 path->length * sizeof (GNUNET_PEER_Id));
1499 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u]\n", path->length);
1501 l = path_get_length (path);
1504 path_destroy (path);
1508 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1509 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1511 l2 = path_get_length (aux);
1514 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1515 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1516 peer->path_tail, aux, path);
1521 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1523 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1524 path_destroy (path);
1529 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1531 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1537 * Add the path to the origin peer and update the path used to reach it in case
1538 * this is the shortest.
1539 * The path is given in peer_info -> destination, therefore we turn the path
1540 * upside down first.
1542 * @param peer Peer to add the path to, being the origin of the path.
1543 * @param path New path to add after being inversed.
1544 * Path will be either used or freed.
1545 * @param trusted Do we trust that this path is real?
1547 * @return path if path was taken, pointer to existing duplicate if exists
1550 struct MeshPeerPath *
1551 GMP_add_path_to_origin (struct MeshPeer *peer,
1552 struct MeshPeerPath *path,
1558 return GMP_add_path (peer, path, trusted);
1563 * Adds a path to the info of all the peers in the path
1565 * @param p Path to process.
1566 * @param confirmed Whether we know if the path works or not.
1569 GMP_add_path_to_all (const struct MeshPeerPath *p, int confirmed)
1573 /* TODO: invert and add */
1574 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1575 for (i++; i < p->length; i++)
1577 struct MeshPeer *aux;
1578 struct MeshPeerPath *copy;
1580 aux = GMP_get_short (p->peers[i]);
1581 copy = path_duplicate (p);
1582 copy->length = i + 1;
1583 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1589 * Remove a connection from a neighboring peer.
1591 * @param peer Peer to remove connection from.
1592 * @param c Connection to remove.
1594 * @return GNUNET_OK on success.
1597 GMP_remove_connection (struct MeshPeer *peer,
1598 const struct MeshConnection *c)
1600 if (NULL == peer || NULL == peer->connections)
1603 LOG (GNUNET_ERROR_TYPE_WARNING,
1604 "Peer %s is not a neighbor!\n",
1606 return GNUNET_SYSERR;
1608 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1614 * Start the DHT search for new paths towards the peer: we don't have
1615 * enough good connections.
1617 * @param peer Destination peer.
1620 GMP_start_search (struct MeshPeer *peer)
1622 if (NULL != peer->search_h)
1628 peer->search_h = GMD_search (GMP_get_id (peer), &search_handler, peer);
1633 * Stop the DHT search for new paths towards the peer: we already have
1634 * enough good connections.
1636 * @param peer Destination peer.
1639 GMP_stop_search (struct MeshPeer *peer)
1641 if (NULL == peer->search_h)
1647 GMD_search_stop (peer->search_h);
1648 peer->search_h = NULL;
1653 * Get the Full ID of a peer.
1655 * @param peer Peer to get from.
1657 * @return Full ID of peer.
1659 const struct GNUNET_PeerIdentity *
1660 GMP_get_id (const struct MeshPeer *peer)
1662 return GNUNET_PEER_resolve2 (peer->id);
1667 * Get the Short ID of a peer.
1669 * @param peer Peer to get from.
1671 * @return Short ID of peer.
1674 GMP_get_short_id (const struct MeshPeer *peer)
1681 * Get the tunnel towards a peer.
1683 * @param peer Peer to get from.
1685 * @return Tunnel towards peer.
1687 struct MeshTunnel3 *
1688 GMP_get_tunnel (const struct MeshPeer *peer)
1690 return peer->tunnel;
1695 * Get the static string for a peer ID.
1699 * @return Static string for it's ID.
1702 GMP_2s (const struct MeshPeer *peer)
1706 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));