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_transport_service.h"
26 #include "gnunet_core_service.h"
27 #include "gnunet_statistics_service.h"
29 #include "mesh_protocol.h"
31 #include "gnunet-service-mesh_peer.h"
32 #include "gnunet-service-mesh_dht.h"
33 #include "gnunet-service-mesh_connection.h"
34 #include "gnunet-service-mesh_tunnel.h"
35 #include "mesh_path.h"
37 #define LOG(level, ...) GNUNET_log_from (level,"mesh-p2p",__VA_ARGS__)
39 /******************************************************************************/
40 /******************************** STRUCTS **********************************/
41 /******************************************************************************/
44 * Struct containing info about a queued transmission to this peer
51 struct MeshPeerQueue *next;
56 struct MeshPeerQueue *prev;
59 * Peer this transmission is directed to.
61 struct MeshPeer *peer;
64 * Connection this message belongs to.
66 struct MeshConnection *c;
74 * Pointer to info stucture used as cls.
89 * Set when this message starts waiting for CORE.
91 struct GNUNET_TIME_Absolute start_waiting;
94 * Function to call on sending.
99 * Closure for callback.
105 * Struct containing all information regarding a given peer
115 * Last time we heard from this peer
117 struct GNUNET_TIME_Absolute last_contact;
120 * Paths to reach the peer, ordered by ascending hop count
122 struct MeshPeerPath *path_head;
125 * Paths to reach the peer, ordered by ascending hop count
127 struct MeshPeerPath *path_tail;
130 * Handle to stop the DHT search for paths to this peer
132 struct GMD_search_handle *search_h;
135 * Tunnel to this peer, if any.
137 struct MeshTunnel3 *tunnel;
140 * Connections that go through this peer, indexed by tid;
142 struct GNUNET_CONTAINER_MultiHashMap *connections;
145 * Handle for queued transmissions
147 struct GNUNET_CORE_TransmitHandle *core_transmit;
150 * Transmission queue to core DLL head
152 struct MeshPeerQueue *queue_head;
155 * Transmission queue to core DLL tail
157 struct MeshPeerQueue *queue_tail;
160 * How many messages are in the queue to this peer.
162 unsigned int queue_n;
167 struct GNUNET_HELLO_Message* hello;
171 /******************************************************************************/
172 /******************************* GLOBALS ***********************************/
173 /******************************************************************************/
176 * Global handle to the statistics service.
178 extern struct GNUNET_STATISTICS_Handle *stats;
181 * Local peer own ID (full value).
183 extern struct GNUNET_PeerIdentity my_full_id;
186 * Local peer own ID (short)
188 extern GNUNET_PEER_Id myid;
191 * Peers known, indexed by PeerIdentity (MeshPeer).
193 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
196 * How many peers do we want to remember?
198 static unsigned long long max_peers;
201 * Percentage of messages that will be dropped (for test purposes only).
203 static unsigned long long drop_percent;
206 * Handle to communicate with core.
208 static struct GNUNET_CORE_Handle *core_handle;
211 * Handle to try to start new connections.
213 static struct GNUNET_TRANSPORT_Handle *transport_handle;
215 /******************************************************************************/
216 /***************************** CORE CALLBACKS *********************************/
217 /******************************************************************************/
221 * Iterator to notify all connections of a broken link. Mark connections
222 * to destroy after all traffic has been sent.
224 * @param cls Closure (peer disconnected).
225 * @param key Current key code (peer id).
226 * @param value Value in the hash map (connection).
228 * @return #GNUNET_YES to continue to iterate.
231 notify_broken (void *cls,
232 const struct GNUNET_HashCode *key,
235 struct MeshPeer *peer = cls;
236 struct MeshConnection *c = value;
238 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
239 GMC_2s (c), GMP_2s (peer));
240 GMC_notify_broken (c, peer);
247 * Remove the direct path to the peer.
249 * @param peer Peer to remove the direct path from.
252 static struct MeshPeerPath *
253 pop_direct_path (struct MeshPeer *peer)
255 struct MeshPeerPath *iter;
257 for (iter = peer->path_head; NULL != iter; iter = iter->next)
259 if (2 <= iter->length)
261 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
271 * Method called whenever a given peer connects.
274 * @param peer peer identity this notification is about
277 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
280 struct MeshPeerPath *path;
283 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
287 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s (self)\n", own_id);
292 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTED %s <= %s\n",
293 own_id, GNUNET_i2s (peer));
295 path->peers[1] = mp->id;
296 GNUNET_PEER_change_rc (mp->id, 1);
297 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
299 path->peers[0] = myid;
300 GNUNET_PEER_change_rc (myid, 1);
301 GMP_add_path (mp, path, GNUNET_YES);
303 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
309 * Method called whenever a peer disconnects.
312 * @param peer peer identity this notification is about
315 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
318 struct MeshPeerPath *direct_path;
321 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
322 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
329 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s (self)\n", own_id);
331 LOG (GNUNET_ERROR_TYPE_DEBUG, "DISCONNECTED %s <= %s\n",
332 own_id, GNUNET_i2s (peer));
333 direct_path = pop_direct_path (p);
334 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
335 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
336 p->connections = NULL;
337 if (NULL != p->core_transmit)
339 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
340 p->core_transmit = NULL;
342 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
344 path_destroy (direct_path);
350 * Functions to handle messages from core
352 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
353 {&GMC_handle_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE, 0},
354 {&GMC_handle_confirm, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
355 sizeof (struct GNUNET_MESH_ConnectionACK)},
356 {&GMC_handle_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
357 sizeof (struct GNUNET_MESH_ConnectionBroken)},
358 {&GMC_handle_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
359 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
360 {&GMC_handle_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
361 sizeof (struct GNUNET_MESH_ACK)},
362 {&GMC_handle_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
363 sizeof (struct GNUNET_MESH_Poll)},
364 {&GMC_handle_encrypted, GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED, 0},
365 {&GMC_handle_kx, GNUNET_MESSAGE_TYPE_MESH_KX, 0},
371 * To be called on core init/fail.
373 * @param cls Closure (config)
374 * @param identity the public identity of this peer
377 core_init (void *cls,
378 const struct GNUNET_PeerIdentity *identity)
380 const struct GNUNET_CONFIGURATION_Handle *c = cls;
383 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
384 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
386 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
387 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
388 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
389 GNUNET_CORE_disconnect (core_handle);
390 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
391 NULL, /* Closure passed to MESH functions */
392 &core_init, /* Call core_init once connected */
393 &core_connect, /* Handle connects */
394 &core_disconnect, /* remove peers on disconnects */
395 NULL, /* Don't notify about all incoming messages */
396 GNUNET_NO, /* For header only in notification */
397 NULL, /* Don't notify about all outbound messages */
398 GNUNET_NO, /* For header-only out notification */
399 core_handlers); /* Register these handlers */
408 * Core callback to write a pre-constructed data packet to core buffer
410 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
411 * @param size Number of bytes available in buf.
412 * @param buf Where the to write the message.
414 * @return number of bytes written to buf
417 send_core_data_raw (void *cls, size_t size, void *buf)
419 struct GNUNET_MessageHeader *msg = cls;
422 GNUNET_assert (NULL != msg);
423 total_size = ntohs (msg->size);
425 if (total_size > size)
430 memcpy (buf, msg, total_size);
437 * Function to send a create connection message to a peer.
439 * @param c Connection to create.
440 * @param size number of bytes available in buf
441 * @param buf where the callee should write the message
442 * @return number of bytes written to buf
445 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
447 struct GNUNET_MESH_ConnectionCreate *msg;
448 struct GNUNET_PeerIdentity *peer_ptr;
449 const struct MeshPeerPath *p = GMC_get_path (c);
456 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
458 sizeof (struct GNUNET_MESH_ConnectionCreate) +
459 p->length * sizeof (struct GNUNET_PeerIdentity);
461 if (size < size_needed || NULL == buf)
466 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
467 msg->header.size = htons (size_needed);
468 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
469 msg->cid = *GMC_get_id (c);
471 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
472 for (i = 0; i < p->length; i++)
474 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
477 LOG (GNUNET_ERROR_TYPE_DEBUG,
478 "CONNECTION CREATE (%u bytes long) sent!\n",
485 * Creates a path ack message in buf and frees all unused resources.
487 * @param c Connection to send an ACK on.
488 * @param size number of bytes available in buf
489 * @param buf where the callee should write the message
491 * @return number of bytes written to buf
494 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
496 struct GNUNET_MESH_ConnectionACK *msg = buf;
498 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
499 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
504 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
505 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
506 msg->cid = *GMC_get_id (c);
508 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
509 return sizeof (struct GNUNET_MESH_ConnectionACK);
513 /******************************************************************************/
514 /******************************** STATIC ***********************************/
515 /******************************************************************************/
519 * Get priority for a queued message.
521 * @param q Queued message
523 * @return CORE priority to use.
525 static enum GNUNET_CORE_Priority
526 get_priority (struct MeshPeerQueue *q)
528 enum GNUNET_CORE_Priority low;
529 enum GNUNET_CORE_Priority high;
534 return GNUNET_CORE_PRIO_BACKGROUND;
537 /* Relayed traffic has lower priority, our own traffic has higher */
538 if (NULL == q->c || GNUNET_NO == GMC_is_origin (q->c, q->fwd))
540 low = GNUNET_CORE_PRIO_BEST_EFFORT;
541 high = GNUNET_CORE_PRIO_URGENT;
545 low = GNUNET_CORE_PRIO_URGENT;
546 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
549 /* Bulky payload has lower priority, control traffic has higher. */
550 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == q->type)
558 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
561 * @param key current key code
562 * @param value value in the hash map
563 * @return #GNUNET_YES if we should continue to iterate,
567 shutdown_tunnel (void *cls,
568 const struct GNUNET_PeerIdentity *key,
571 struct MeshPeer *p = value;
572 struct MeshTunnel3 *t = p->tunnel;
581 * Destroy the peer_info and free any allocated resources linked to it
583 * @param peer The peer_info to destroy.
585 * @return GNUNET_OK on success
588 peer_destroy (struct MeshPeer *peer)
590 struct GNUNET_PeerIdentity id;
591 struct MeshPeerPath *p;
592 struct MeshPeerPath *nextp;
594 GNUNET_PEER_resolve (peer->id, &id);
595 GNUNET_PEER_change_rc (peer->id, -1);
597 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
600 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
603 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
605 if (NULL != peer->search_h)
607 GMD_search_stop (peer->search_h);
613 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
617 GMT_destroy_empty (peer->tunnel);
624 * Returns if peer is used (has a tunnel or is neighbor).
626 * @param peer Peer to check.
628 * @return #GNUNET_YES if peer is in use.
631 peer_is_used (struct MeshPeer *peer)
633 struct MeshPeerPath *p;
635 if (NULL != peer->tunnel)
638 for (p = peer->path_head; NULL != p; p = p->next)
648 * Iterator over all the peers to get the oldest timestamp.
650 * @param cls Closure (unsued).
651 * @param key ID of the peer.
652 * @param value Peer_Info of the peer.
655 peer_get_oldest (void *cls,
656 const struct GNUNET_PeerIdentity *key,
659 struct MeshPeer *p = value;
660 struct GNUNET_TIME_Absolute *abs = cls;
662 /* Don't count active peers */
663 if (GNUNET_YES == peer_is_used (p))
666 if (abs->abs_value_us < p->last_contact.abs_value_us)
667 abs->abs_value_us = p->last_contact.abs_value_us;
674 * Iterator over all the peers to remove the oldest entry.
676 * @param cls Closure (unsued).
677 * @param key ID of the peer.
678 * @param value Peer_Info of the peer.
681 peer_timeout (void *cls,
682 const struct GNUNET_PeerIdentity *key,
685 struct MeshPeer *p = value;
686 struct GNUNET_TIME_Absolute *abs = cls;
688 LOG (GNUNET_ERROR_TYPE_WARNING,
689 "peer %s timeout\n", GNUNET_i2s (key));
691 if (p->last_contact.abs_value_us == abs->abs_value_us &&
692 GNUNET_NO == peer_is_used (p))
702 * Delete oldest unused peer.
705 peer_delete_oldest (void)
707 struct GNUNET_TIME_Absolute abs;
709 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
711 GNUNET_CONTAINER_multipeermap_iterate (peers,
714 GNUNET_CONTAINER_multipeermap_iterate (peers,
721 * Choose the best (yet unused) path towards a peer,
722 * considering the tunnel properties.
724 * @param peer The destination peer.
726 * @return Best current known path towards the peer, if any.
728 static struct MeshPeerPath *
729 peer_get_best_path (const struct MeshPeer *peer)
731 struct MeshPeerPath *best_p;
732 struct MeshPeerPath *p;
733 unsigned int best_cost;
736 best_cost = UINT_MAX;
738 for (p = peer->path_head; NULL != p; p = p->next)
740 if (GNUNET_YES == GMT_is_path_used (peer->tunnel, p))
741 continue; /* If path is already in use, skip it. */
743 if (GNUNET_NO == path_is_valid (p))
744 continue; /* Don't use invalid paths. */
746 if ((cost = GMT_get_path_cost (peer->tunnel, p)) < best_cost)
757 queue_is_sendable (struct MeshPeerQueue *q)
759 /* Is PID-independent? */
762 case GNUNET_MESSAGE_TYPE_MESH_ACK:
763 case GNUNET_MESSAGE_TYPE_MESH_POLL:
764 case GNUNET_MESSAGE_TYPE_MESH_KX:
765 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
766 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
767 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
768 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
769 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
772 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
779 if (GMC_is_sendable (q->c, q->fwd))
787 * Get first sendable message.
789 * @param peer The destination peer.
791 * @return Best current known path towards the peer, if any.
793 static struct MeshPeerQueue *
794 peer_get_first_message (const struct MeshPeer *peer)
796 struct MeshPeerQueue *q;
798 for (q = peer->queue_head; NULL != q; q = q->next)
800 if (queue_is_sendable (q))
809 * Function to process paths received for a new peer addition. The recorded
810 * paths form the initial tunnel, which can be optimized later.
811 * Called on each result obtained for the DHT search.
817 search_handler (void *cls, const struct MeshPeerPath *path)
819 struct MeshPeer *peer = cls;
820 unsigned int connection_count;
822 GMP_add_path_to_all (path, GNUNET_NO);
824 /* Count connections */
825 connection_count = GMT_count_connections (peer->tunnel);
827 /* If we already have 3 (or more (?!)) connections, it's enough */
828 if (3 <= connection_count)
831 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (peer->tunnel))
833 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
842 * Core callback to write a queued packet to core buffer
844 * @param cls Closure (peer info).
845 * @param size Number of bytes available in buf.
846 * @param buf Where the to write the message.
848 * @return number of bytes written to buf
851 queue_send (void *cls, size_t size, void *buf)
853 struct MeshPeer *peer = cls;
854 struct MeshConnection *c;
855 struct MeshPeerQueue *queue;
856 const struct GNUNET_PeerIdentity *dst_id;
859 peer->core_transmit = NULL;
860 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send towards %s (max %u)\n",
861 GMP_2s (peer), size);
863 if (NULL == buf || 0 == size)
865 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
870 queue = peer_get_first_message (peer);
873 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
878 dst_id = GNUNET_PEER_resolve2 (peer->id);
879 LOG (GNUNET_ERROR_TYPE_DEBUG, "* on connection %s\n", GMC_2s (c));
880 /* Check if buffer size is enough for the message */
881 if (queue->size > size)
883 LOG (GNUNET_ERROR_TYPE_WARNING, "not enough room (%u vs %u), reissue\n",
885 peer->core_transmit =
886 GNUNET_CORE_notify_transmit_ready (core_handle,
887 GNUNET_NO, get_priority (queue),
888 GNUNET_TIME_UNIT_FOREVER_REL,
895 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
900 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
901 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
902 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
903 case GNUNET_MESSAGE_TYPE_MESH_KX:
904 case GNUNET_MESSAGE_TYPE_MESH_ACK:
905 case GNUNET_MESSAGE_TYPE_MESH_POLL:
906 LOG (GNUNET_ERROR_TYPE_DEBUG, "* raw: %s\n", GM_m2s (queue->type));
907 data_size = send_core_data_raw (queue->cls, size, buf);
909 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
910 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
911 if (GMC_is_origin (c, GNUNET_YES))
912 data_size = send_core_connection_create (queue->c, size, buf);
914 data_size = send_core_data_raw (queue->cls, size, buf);
916 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
917 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
918 if (GMC_is_origin (c, GNUNET_NO) ||
919 GMC_is_origin (c, GNUNET_YES))
920 data_size = send_core_connection_ack (queue->c, size, buf);
922 data_size = send_core_data_raw (queue->cls, size, buf);
924 case GNUNET_MESSAGE_TYPE_MESH_DATA:
925 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
926 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
927 /* This should be encapsulted */
933 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n", queue->type);
937 if (0 < drop_percent &&
938 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
940 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s on connection\n",
941 GM_m2s (queue->type), GMC_2s (c));
946 LOG (GNUNET_ERROR_TYPE_INFO,
947 "ss %s on connection %s (%p) %s (size %u)\n",
948 GM_m2s (queue->type), GMC_2s (c), c, GM_f2s (queue->fwd), data_size);
951 /* Free queue, but cls was freed by send_core_* */
952 GMP_queue_destroy (queue, GNUNET_NO);
954 /* If more data in queue, send next */
955 queue = peer_get_first_message (peer);
958 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
959 if (NULL == peer->core_transmit)
961 peer->core_transmit =
962 GNUNET_CORE_notify_transmit_ready (core_handle,
963 GNUNET_NO, get_priority (queue),
964 GNUNET_TIME_UNIT_FOREVER_REL,
969 queue->start_waiting = GNUNET_TIME_absolute_get ();
973 LOG (GNUNET_ERROR_TYPE_DEBUG,
974 "* tmt rdy called somewhere else\n");
976 // GMC_start_poll (); FIXME needed?
980 // GMC_stop_poll(); FIXME needed?
983 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
988 /******************************************************************************/
989 /******************************** API ***********************************/
990 /******************************************************************************/
994 * Free a transmission that was already queued with all resources
995 * associated to the request.
997 * @param queue Queue handler to cancel.
998 * @param clear_cls Is it necessary to free associated cls?
1001 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
1003 struct MeshPeer *peer;
1007 if (GNUNET_YES == clear_cls)
1009 LOG (GNUNET_ERROR_TYPE_DEBUG, "# queue destroy type %s\n",
1010 GM_m2s (queue->type));
1011 switch (queue->type)
1013 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
1014 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1016 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1017 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1018 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1019 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
1020 case GNUNET_MESSAGE_TYPE_MESH_KX:
1021 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
1022 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1023 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1024 GNUNET_free_non_null (queue->cls);
1029 LOG (GNUNET_ERROR_TYPE_ERROR, "# type %s unknown!\n",
1030 GM_m2s (queue->type));
1033 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1035 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
1036 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
1041 if (NULL != queue->callback)
1043 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Calling callback\n");
1044 queue->callback (queue->callback_cls,
1045 queue->c, queue->type,
1046 queue->fwd, queue->size,
1047 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
1050 GNUNET_free (queue);
1055 * @brief Queue and pass message to core when possible.
1057 * @param peer Peer towards which to queue the message.
1058 * @param cls Closure (@c type dependant). It will be used by queue_send to
1059 * build the message to be sent if not already prebuilt.
1060 * @param type Type of the message, 0 for a raw message.
1061 * @param size Size of the message.
1062 * @param c Connection this message belongs to (can be NULL).
1063 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1064 * @param cont Continuation to be called once CORE has taken the message.
1065 * @param cont_cls Closure for @c cont.
1067 * @return Handle to cancel the message before it is sent. Once cont is called
1068 * message has been sent and therefore the handle is no longer valid.
1070 struct MeshPeerQueue *
1071 GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
1072 struct MeshConnection *c, int fwd,
1073 GMP_sent cont, void *cont_cls)
1075 struct MeshPeerQueue *queue;
1079 LOG (GNUNET_ERROR_TYPE_INFO, "qq %s on connection %s (%p) %s towards %s (size %u)\n",
1080 GM_m2s (type), GMC_2s (c), c, GM_f2s (fwd), GMP_2s(peer), size);
1082 if (NULL == peer->connections)
1084 /* We are not connected to this peer, ignore request. */
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING %s not a neighbor\n", GMP_2s (peer));
1086 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1093 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1094 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1099 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1101 call_core = NULL == c ? GNUNET_YES : GMC_is_sendable (c, fwd);
1102 queue = GNUNET_new (struct MeshPeerQueue);
1109 queue->callback = cont;
1110 queue->callback_cls = cont_cls;
1113 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1118 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1119 call_core = GNUNET_YES;
1122 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1124 LOG (GNUNET_ERROR_TYPE_DEBUG,
1125 "calling core tmt rdy towards %s for %u bytes\n",
1126 GMP_2s (peer), size);
1127 peer->core_transmit =
1128 GNUNET_CORE_notify_transmit_ready (core_handle,
1129 GNUNET_NO, get_priority (queue),
1130 GNUNET_TIME_UNIT_FOREVER_REL,
1131 GNUNET_PEER_resolve2 (peer->id),
1135 queue->start_waiting = GNUNET_TIME_absolute_get ();
1139 LOG (GNUNET_ERROR_TYPE_DEBUG,
1140 "core tmt rdy towards %s already called\n",
1149 * Cancel all queued messages to a peer that belong to a certain connection.
1151 * @param peer Peer towards whom to cancel.
1152 * @param c Connection whose queued messages to cancel. Might be destroyed by
1153 * the sent continuation call.
1156 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1158 struct MeshPeerQueue *q;
1159 struct MeshPeerQueue *next;
1160 struct MeshPeerQueue *prev;
1162 for (q = peer->queue_head; NULL != q; q = next)
1167 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP_cancel_queue %s\n", GM_m2s (q->type));
1168 GMP_queue_destroy (q, GNUNET_YES);
1170 /* Get next from prev, q->next might be already freed:
1171 * queue destroy -> callback -> GMC_destroy -> cancel_queues -> here
1174 next = peer->queue_head;
1183 if (NULL == peer->queue_head)
1185 if (NULL != peer->core_transmit)
1187 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1188 peer->core_transmit = NULL;
1195 * Get the first transmittable message for a connection.
1197 * @param peer Neighboring peer.
1198 * @param c Connection.
1200 * @return First transmittable message.
1202 static struct MeshPeerQueue *
1203 connection_get_first_message (struct MeshPeer *peer, struct MeshConnection *c)
1205 struct MeshPeerQueue *q;
1207 for (q = peer->queue_head; NULL != q; q = q->next)
1211 if (queue_is_sendable (q))
1213 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1216 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1224 * Get the first message for a connection and unqueue it.
1226 * @param peer Neighboring peer.
1227 * @param c Connection.
1229 * @return First message for this connection.
1231 struct GNUNET_MessageHeader *
1232 GMP_connection_pop (struct MeshPeer *peer, struct MeshConnection *c)
1234 struct MeshPeerQueue *q;
1235 struct GNUNET_MessageHeader *msg;
1237 for (q = peer->queue_head; NULL != q; q = q->next)
1243 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1244 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1245 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
1246 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1247 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1248 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1249 GMP_queue_destroy (q, GNUNET_YES);
1252 case GNUNET_MESSAGE_TYPE_MESH_KX:
1253 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
1254 msg = (struct GNUNET_MessageHeader *) q->cls;
1255 GMP_queue_destroy (q, GNUNET_NO);
1268 GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
1270 struct MeshPeerQueue *q;
1273 if (NULL != peer->core_transmit)
1275 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1276 return; /* Already unlocked */
1279 q = connection_get_first_message (peer, c);
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1283 return; /* Nothing to transmit */
1287 peer->core_transmit =
1288 GNUNET_CORE_notify_transmit_ready (core_handle,
1289 GNUNET_NO, get_priority (q),
1290 GNUNET_TIME_UNIT_FOREVER_REL,
1291 GNUNET_PEER_resolve2 (peer->id),
1299 * Initialize the peer subsystem.
1301 * @param c Configuration.
1304 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1307 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1309 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1312 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1313 "MESH", "MAX_PEERS", "USING DEFAULT");
1318 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1325 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1326 LOG (GNUNET_ERROR_TYPE_WARNING, "Mesh is running with DROP enabled.\n");
1327 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1328 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1329 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1332 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1333 NULL, /* Closure passed to MESH functions */
1334 &core_init, /* Call core_init once connected */
1335 &core_connect, /* Handle connects */
1336 &core_disconnect, /* remove peers on disconnects */
1337 NULL, /* Don't notify about all incoming messages */
1338 GNUNET_NO, /* For header only in notification */
1339 NULL, /* Don't notify about all outbound messages */
1340 GNUNET_NO, /* For header-only out notification */
1341 core_handlers); /* Register these handlers */
1343 GNUNET_CONFIGURATION_get_value_yesno (c, "MESH", "DISABLE_TRY_CONNECT"))
1345 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1346 /* Notify callbacks */
1351 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1352 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1353 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1354 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1359 if (NULL == core_handle)
1362 GNUNET_SCHEDULER_shutdown ();
1369 * Shut down the peer subsystem.
1374 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1376 if (core_handle != NULL)
1378 GNUNET_CORE_disconnect (core_handle);
1381 if (transport_handle != NULL)
1383 GNUNET_TRANSPORT_disconnect (transport_handle);
1384 transport_handle = NULL;
1386 GNUNET_PEER_change_rc (myid, -1);
1390 * Retrieve the MeshPeer stucture associated with the peer, create one
1391 * and insert it in the appropriate structures if the peer is not known yet.
1393 * @param peer_id Full identity of the peer.
1395 * @return Existing or newly created peer structure.
1398 GMP_get (const struct GNUNET_PeerIdentity *peer_id)
1400 struct MeshPeer *peer;
1402 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1405 peer = GNUNET_new (struct MeshPeer);
1406 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1408 peer_delete_oldest ();
1410 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1411 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1412 peer->id = GNUNET_PEER_intern (peer_id);
1414 peer->last_contact = GNUNET_TIME_absolute_get();
1421 * Retrieve the MeshPeer stucture associated with the peer, create one
1422 * and insert it in the appropriate structures if the peer is not known yet.
1424 * @param peer Short identity of the peer.
1426 * @return Existing or newly created peer structure.
1429 GMP_get_short (const GNUNET_PEER_Id peer)
1431 return GMP_get (GNUNET_PEER_resolve2 (peer));
1436 * Try to connect to a peer on transport level.
1438 * @param cls Closure (peer).
1439 * @param tc TaskContext.
1442 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1444 struct MeshPeer *peer = cls;
1446 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1449 GNUNET_TRANSPORT_try_connect (transport_handle,
1450 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1455 * Try to establish a new connection to this peer (in its tunnel).
1456 * If the peer doesn't have any path to it yet, try to get one.
1457 * If the peer already has some path, send a CREATE CONNECTION towards it.
1459 * @param peer Peer to connect to.
1462 GMP_connect (struct MeshPeer *peer)
1464 struct MeshTunnel3 *t;
1465 struct MeshPeerPath *p;
1466 struct MeshConnection *c;
1469 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GMP_2s (peer));
1471 /* If we have a current hello, try to connect using it. */
1472 GMP_try_connect (peer);
1476 rerun_search = GNUNET_NO;
1478 if (NULL != peer->path_head)
1480 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1481 p = peer_get_best_path (peer);
1484 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1485 c = GMT_use_path (t, p);
1488 /* This case can happen when the path includes a first hop that is
1489 * not yet known to be connected.
1491 * This happens quite often during testing when running mesh
1492 * under valgrind: core connect notifications come very late and the
1493 * DHT result has already come and created a valid path.
1494 * In this case, the peer->connections hashmap will be NULL and
1495 * tunnel_use_path will not be able to create a connection from that
1498 * Re-running the DHT GET should give core time to callback.
1500 * GMT_use_path -> GMC_new -> register_neighbors takes care of
1501 * updating statistics about this issue.
1503 rerun_search = GNUNET_YES;
1507 GMC_send_create (c);
1513 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1517 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1519 GMD_search_stop (peer->search_h);
1520 peer->search_h = NULL;
1521 LOG (GNUNET_ERROR_TYPE_DEBUG,
1522 " Stopping DHT GET for peer %s\n",
1526 if (NULL == peer->search_h)
1528 const struct GNUNET_PeerIdentity *id;
1530 id = GNUNET_PEER_resolve2 (peer->id);
1531 LOG (GNUNET_ERROR_TYPE_DEBUG,
1532 " Starting DHT GET for peer %s\n", GMP_2s (peer));
1533 peer->search_h = GMD_search (id, &search_handler, peer);
1534 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
1535 GMT_change_cstate (t, MESH_TUNNEL3_SEARCHING);
1541 * Chech whether there is a direct (core level) connection to peer.
1543 * @param peer Peer to check.
1545 * @return #GNUNET_YES if there is a direct connection.
1548 GMP_is_neighbor (const struct MeshPeer *peer)
1550 struct MeshPeerPath *path;
1552 if (NULL == peer->connections)
1555 for (path = peer->path_head; NULL != path; path = path->next)
1557 if (3 > path->length)
1561 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1567 * Create and initialize a new tunnel towards a peer, in case it has none.
1568 * In case the peer already has a tunnel, nothing is done.
1570 * Does not generate any traffic, just creates the local data structures.
1572 * @param peer Peer towards which to create the tunnel.
1575 GMP_add_tunnel (struct MeshPeer *peer)
1577 if (NULL != peer->tunnel)
1579 peer->tunnel = GMT_new (peer);
1584 * Add a connection to a neighboring peer.
1586 * Store that the peer is the first hop of the connection in one
1587 * direction and that on peer disconnect the connection must be
1588 * notified and destroyed, for it will no longer be valid.
1590 * @param peer Peer to add connection to.
1591 * @param c Connection to add.
1593 * @return GNUNET_OK on success.
1596 GMP_add_connection (struct MeshPeer *peer,
1597 struct MeshConnection *c)
1600 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GMC_2s (c));
1601 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GMP_2s (peer));
1603 if (NULL == peer->connections)
1606 LOG (GNUNET_ERROR_TYPE_DEBUG,
1607 "Peer %s is not a neighbor!\n",
1609 return GNUNET_SYSERR;
1611 LOG (GNUNET_ERROR_TYPE_DEBUG,
1612 "peer %s ok, has %u connections.\n",
1613 GMP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1614 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1618 LOG (GNUNET_ERROR_TYPE_DEBUG,
1619 " now has %u connections.\n",
1620 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1621 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1628 * Add the path to the peer and update the path used to reach it in case this
1631 * @param peer Destination peer to add the path to.
1632 * @param path New path to add. Last peer must be the peer in arg 1.
1633 * Path will be either used of freed if already known.
1634 * @param trusted Do we trust that this path is real?
1636 * @return path if path was taken, pointer to existing duplicate if exists
1639 struct MeshPeerPath *
1640 GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *path,
1643 struct MeshPeerPath *aux;
1647 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1648 path->length, GMP_2s (peer));
1650 if ((NULL == peer) || (NULL == path))
1653 path_destroy (path);
1656 if (path->peers[path->length - 1] != peer->id)
1659 path_destroy (path);
1662 if (2 >= path->length && GNUNET_NO == trusted)
1664 /* Only allow CORE to tell us about direct paths */
1665 path_destroy (path);
1668 for (l = 1; l < path->length; l++)
1670 if (path->peers[l] == myid)
1672 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1673 for (l2 = 0; l2 < path->length - l; l2++)
1675 path->peers[l2] = path->peers[l + l2];
1679 path->peers = GNUNET_realloc (path->peers,
1680 path->length * sizeof (GNUNET_PEER_Id));
1684 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1686 l = path_get_length (path);
1689 path_destroy (path);
1693 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1694 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1696 l2 = path_get_length (aux);
1699 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1700 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1701 peer->path_tail, aux, path);
1702 if (NULL != peer->tunnel && 3 < GMT_count_connections (peer->tunnel))
1710 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1712 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1713 path_destroy (path);
1718 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1720 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1721 if (NULL != peer->tunnel && 3 < GMT_count_connections (peer->tunnel))
1730 * Add the path to the origin peer and update the path used to reach it in case
1731 * this is the shortest.
1732 * The path is given in peer_info -> destination, therefore we turn the path
1733 * upside down first.
1735 * @param peer Peer to add the path to, being the origin of the path.
1736 * @param path New path to add after being inversed.
1737 * Path will be either used or freed.
1738 * @param trusted Do we trust that this path is real?
1740 * @return path if path was taken, pointer to existing duplicate if exists
1743 struct MeshPeerPath *
1744 GMP_add_path_to_origin (struct MeshPeer *peer,
1745 struct MeshPeerPath *path,
1751 return GMP_add_path (peer, path, trusted);
1756 * Adds a path to the info of all the peers in the path
1758 * @param p Path to process.
1759 * @param confirmed Whether we know if the path works or not.
1762 GMP_add_path_to_all (const struct MeshPeerPath *p, int confirmed)
1766 /* TODO: invert and add */
1767 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1768 for (i++; i < p->length; i++)
1770 struct MeshPeer *aux;
1771 struct MeshPeerPath *copy;
1773 aux = GMP_get_short (p->peers[i]);
1774 copy = path_duplicate (p);
1775 copy->length = i + 1;
1776 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1782 * Remove any path to the peer that has the extact same peers as the one given.
1784 * @param peer Peer to remove the path from.
1785 * @param path Path to remove. Is always destroyed .
1788 GMP_remove_path (struct MeshPeer *peer, struct MeshPeerPath *path)
1790 struct MeshPeerPath *iter;
1791 struct MeshPeerPath *next;
1793 GNUNET_assert (myid == path->peers[0]);
1794 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1796 for (iter = peer->path_head; NULL != iter; iter = next)
1799 if (0 == memcmp (path->peers, iter->peers,
1800 sizeof (GNUNET_PEER_Id) * path->length))
1802 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
1803 path_destroy (iter);
1808 path_destroy (path);
1813 * Remove a connection from a neighboring peer.
1815 * @param peer Peer to remove connection from.
1816 * @param c Connection to remove.
1818 * @return GNUNET_OK on success.
1821 GMP_remove_connection (struct MeshPeer *peer,
1822 const struct MeshConnection *c)
1824 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GMC_2s (c));
1825 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GMP_2s (peer));
1827 if (NULL == peer || NULL == peer->connections)
1829 LOG (GNUNET_ERROR_TYPE_DEBUG,
1830 "Peer %s is not a neighbor!\n",
1832 return GNUNET_SYSERR;
1834 LOG (GNUNET_ERROR_TYPE_DEBUG,
1835 "peer %s ok, has %u connections.\n",
1836 GMP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1838 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1844 * Start the DHT search for new paths towards the peer: we don't have
1845 * enough good connections.
1847 * @param peer Destination peer.
1850 GMP_start_search (struct MeshPeer *peer)
1852 if (NULL != peer->search_h)
1858 peer->search_h = GMD_search (GMP_get_id (peer), &search_handler, peer);
1863 * Stop the DHT search for new paths towards the peer: we already have
1864 * enough good connections.
1866 * @param peer Destination peer.
1869 GMP_stop_search (struct MeshPeer *peer)
1871 if (NULL == peer->search_h)
1877 GMD_search_stop (peer->search_h);
1878 peer->search_h = NULL;
1883 * Get the Full ID of a peer.
1885 * @param peer Peer to get from.
1887 * @return Full ID of peer.
1889 const struct GNUNET_PeerIdentity *
1890 GMP_get_id (const struct MeshPeer *peer)
1892 return GNUNET_PEER_resolve2 (peer->id);
1897 * Get the Short ID of a peer.
1899 * @param peer Peer to get from.
1901 * @return Short ID of peer.
1904 GMP_get_short_id (const struct MeshPeer *peer)
1917 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel3 *t)
1924 * Get the tunnel towards a peer.
1926 * @param peer Peer to get from.
1928 * @return Tunnel towards peer.
1930 struct MeshTunnel3 *
1931 GMP_get_tunnel (const struct MeshPeer *peer)
1933 return peer->tunnel;
1938 * Set the hello message.
1940 * @param peer Peer whose message to set.
1941 * @param hello Hello message.
1944 GMP_set_hello (struct MeshPeer *peer, const struct GNUNET_HELLO_Message *hello)
1946 struct GNUNET_HELLO_Message *old;
1949 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GMP_2s (peer));
1953 old = GMP_get_hello (peer);
1956 size = GNUNET_HELLO_size (hello);
1957 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
1958 peer->hello = GNUNET_malloc (size);
1959 memcpy (peer->hello, hello, size);
1963 peer->hello = GNUNET_HELLO_merge (old, hello);
1964 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
1965 peer->hello, GNUNET_HELLO_size (hello));
1972 * Get the hello message.
1974 * @param peer Peer whose message to get.
1976 * @return Hello message.
1978 struct GNUNET_HELLO_Message *
1979 GMP_get_hello (struct MeshPeer *peer)
1981 struct GNUNET_TIME_Absolute expiration;
1982 struct GNUNET_TIME_Relative remaining;
1984 if (NULL == peer->hello)
1987 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
1988 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
1989 if (0 == remaining.rel_value_us)
1991 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
1992 GNUNET_STRINGS_absolute_time_to_string (expiration));
1993 GNUNET_free (peer->hello);
2001 * Try to connect to a peer on TRANSPORT level.
2003 * @param peer Peer to whom to connect.
2006 GMP_try_connect (struct MeshPeer *peer)
2008 struct GNUNET_HELLO_Message *hello;
2009 struct GNUNET_MessageHeader *mh;
2011 if (NULL == transport_handle)
2014 hello = GMP_get_hello (peer);
2018 mh = GNUNET_HELLO_get_header (hello);
2019 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2024 * Count the number of known paths toward the peer.
2026 * @param peer Peer to get path info.
2028 * @return Number of known paths.
2031 GMP_count_paths (const struct MeshPeer *peer)
2033 struct MeshPeerPath *iter;
2036 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2044 * Iterate all known peers.
2046 * @param iter Iterator.
2047 * @param cls Closure for @c iter.
2050 GMP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2052 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2057 * Get the static string for a peer ID.
2061 * @return Static string for it's ID.
2064 GMP_2s (const struct MeshPeer *peer)
2068 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));