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;
210 static struct GNUNET_TRANSPORT_Handle *transport_handle;
213 /******************************************************************************/
214 /***************************** CORE CALLBACKS *********************************/
215 /******************************************************************************/
219 * Iterator to notify all connections of a broken link. Mark connections
220 * to destroy after all traffic has been sent.
222 * @param cls Closure (peer disconnected).
223 * @param key Current key code (peer id).
224 * @param value Value in the hash map (connection).
226 * @return #GNUNET_YES to continue to iterate.
229 notify_broken (void *cls,
230 const struct GNUNET_HashCode *key,
233 struct MeshPeer *peer = cls;
234 struct MeshConnection *c = value;
236 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
237 GMC_2s (c), GMP_2s (peer));
238 GMC_notify_broken (c, peer);
245 * Remove the direct path to the peer.
247 * @param peer Peer to remove the direct path from.
250 static struct MeshPeerPath *
251 pop_direct_path (struct MeshPeer *peer)
253 struct MeshPeerPath *iter;
255 for (iter = peer->path_head; NULL != iter; iter = iter->next)
257 if (2 <= iter->length)
259 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
269 * Method called whenever a given peer connects.
272 * @param peer peer identity this notification is about
275 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
278 struct MeshPeerPath *path;
280 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer connected\n");
281 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&my_full_id));
285 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self)\n");
290 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (peer));
292 path->peers[1] = mp->id;
293 GNUNET_PEER_change_rc (mp->id, 1);
294 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
296 path->peers[0] = myid;
297 GNUNET_PEER_change_rc (myid, 1);
298 GMP_add_path (mp, path, GNUNET_YES);
300 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
306 * Method called whenever a peer disconnects.
309 * @param peer peer identity this notification is about
312 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
315 struct MeshPeerPath *direct_path;
317 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer disconnected\n");
318 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
325 LOG (GNUNET_ERROR_TYPE_DEBUG, " (self: %s)\n", GMP_2s (p));
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GMP_2s (p));
329 direct_path = pop_direct_path (p);
330 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
331 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
332 p->connections = NULL;
333 if (NULL != p->core_transmit)
335 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
336 p->core_transmit = NULL;
338 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
340 path_destroy (direct_path);
346 * Functions to handle messages from core
348 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
349 {&GMC_handle_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
351 {&GMC_handle_confirm, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
352 sizeof (struct GNUNET_MESH_ConnectionACK)},
353 {&GMC_handle_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
354 sizeof (struct GNUNET_MESH_ConnectionBroken)},
355 {&GMC_handle_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
356 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
357 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE,
358 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
359 {&GMC_handle_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
360 sizeof (struct GNUNET_MESH_ACK)},
361 {&GMC_handle_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
362 sizeof (struct GNUNET_MESH_Poll)},
363 {&GMC_handle_encrypted, GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED, 0},
364 {&GMC_handle_kx, GNUNET_MESSAGE_TYPE_MESH_KX, 0},
370 * To be called on core init/fail.
372 * @param cls Closure (config)
373 * @param identity the public identity of this peer
376 core_init (void *cls,
377 const struct GNUNET_PeerIdentity *identity)
379 const struct GNUNET_CONFIGURATION_Handle *c = cls;
382 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
383 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
385 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
386 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
387 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
388 GNUNET_CORE_disconnect (core_handle);
389 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
390 NULL, /* Closure passed to MESH functions */
391 &core_init, /* Call core_init once connected */
392 &core_connect, /* Handle connects */
393 &core_disconnect, /* remove peers on disconnects */
394 NULL, /* Don't notify about all incoming messages */
395 GNUNET_NO, /* For header only in notification */
396 NULL, /* Don't notify about all outbound messages */
397 GNUNET_NO, /* For header-only out notification */
398 core_handlers); /* Register these handlers */
407 * Core callback to write a pre-constructed data packet to core buffer
409 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
410 * @param size Number of bytes available in buf.
411 * @param buf Where the to write the message.
413 * @return number of bytes written to buf
416 send_core_data_raw (void *cls, size_t size, void *buf)
418 struct GNUNET_MessageHeader *msg = cls;
421 GNUNET_assert (NULL != msg);
422 total_size = ntohs (msg->size);
424 if (total_size > size)
429 memcpy (buf, msg, total_size);
436 * Function to send a create connection message to a peer.
438 * @param c Connection to create.
439 * @param size number of bytes available in buf
440 * @param buf where the callee should write the message
441 * @return number of bytes written to buf
444 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
446 struct GNUNET_MESH_ConnectionCreate *msg;
447 struct GNUNET_PeerIdentity *peer_ptr;
448 const struct MeshPeerPath *p = GMC_get_path (c);
455 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
457 sizeof (struct GNUNET_MESH_ConnectionCreate) +
458 p->length * sizeof (struct GNUNET_PeerIdentity);
460 if (size < size_needed || NULL == buf)
465 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
466 msg->header.size = htons (size_needed);
467 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
468 msg->cid = *GMC_get_id (c);
470 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
471 for (i = 0; i < p->length; i++)
473 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
476 LOG (GNUNET_ERROR_TYPE_DEBUG,
477 "CONNECTION CREATE (%u bytes long) sent!\n",
484 * Creates a path ack message in buf and frees all unused resources.
486 * @param c Connection to send an ACK on.
487 * @param size number of bytes available in buf
488 * @param buf where the callee should write the message
490 * @return number of bytes written to buf
493 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
495 struct GNUNET_MESH_ConnectionACK *msg = buf;
497 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
498 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
503 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
504 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
505 msg->cid = *GMC_get_id (c);
508 /* TODO add signature */
510 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
511 return sizeof (struct GNUNET_MESH_ConnectionACK);
515 /******************************************************************************/
516 /******************************** STATIC ***********************************/
517 /******************************************************************************/
521 * Get priority for a queued message.
523 * @param q Queued message
525 * @return CORE priority to use.
527 static enum GNUNET_CORE_Priority
528 get_priority (struct MeshPeerQueue *q)
530 enum GNUNET_CORE_Priority low;
531 enum GNUNET_CORE_Priority high;
536 return GNUNET_CORE_PRIO_BACKGROUND;
539 /* Relayed traffic has lower priority, our own traffic has higher */
540 if (NULL == q->c || GNUNET_NO == GMC_is_origin (q->c, q->fwd))
542 low = GNUNET_CORE_PRIO_BEST_EFFORT;
543 high = GNUNET_CORE_PRIO_URGENT;
547 low = GNUNET_CORE_PRIO_URGENT;
548 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
551 /* Bulky payload has lower priority, control traffic has higher. */
552 if (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED == q->type)
560 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
563 * @param key current key code
564 * @param value value in the hash map
565 * @return #GNUNET_YES if we should continue to iterate,
569 shutdown_tunnel (void *cls,
570 const struct GNUNET_PeerIdentity *key,
573 struct MeshPeer *p = value;
574 struct MeshTunnel3 *t = p->tunnel;
583 * Destroy the peer_info and free any allocated resources linked to it
585 * @param peer The peer_info to destroy.
587 * @return GNUNET_OK on success
590 peer_destroy (struct MeshPeer *peer)
592 struct GNUNET_PeerIdentity id;
593 struct MeshPeerPath *p;
594 struct MeshPeerPath *nextp;
596 GNUNET_PEER_resolve (peer->id, &id);
597 GNUNET_PEER_change_rc (peer->id, -1);
599 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
602 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
605 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
607 if (NULL != peer->search_h)
609 GMD_search_stop (peer->search_h);
615 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
619 GMT_destroy_empty (peer->tunnel);
626 * Returns if peer is used (has a tunnel or is neighbor).
628 * @param peer Peer to check.
630 * @return #GNUNET_YES if peer is in use.
633 peer_is_used (struct MeshPeer *peer)
635 struct MeshPeerPath *p;
637 if (NULL != peer->tunnel)
640 for (p = peer->path_head; NULL != p; p = p->next)
650 * Iterator over all the peers to get the oldest timestamp.
652 * @param cls Closure (unsued).
653 * @param key ID of the peer.
654 * @param value Peer_Info of the peer.
657 peer_get_oldest (void *cls,
658 const struct GNUNET_PeerIdentity *key,
661 struct MeshPeer *p = value;
662 struct GNUNET_TIME_Absolute *abs = cls;
664 /* Don't count active peers */
665 if (GNUNET_YES == peer_is_used (p))
668 if (abs->abs_value_us < p->last_contact.abs_value_us)
669 abs->abs_value_us = p->last_contact.abs_value_us;
676 * Iterator over all the peers to remove the oldest entry.
678 * @param cls Closure (unsued).
679 * @param key ID of the peer.
680 * @param value Peer_Info of the peer.
683 peer_timeout (void *cls,
684 const struct GNUNET_PeerIdentity *key,
687 struct MeshPeer *p = value;
688 struct GNUNET_TIME_Absolute *abs = cls;
690 LOG (GNUNET_ERROR_TYPE_WARNING,
691 "peer %s timeout\n", GNUNET_i2s (key));
693 if (p->last_contact.abs_value_us == abs->abs_value_us &&
694 GNUNET_NO == peer_is_used (p))
704 * Delete oldest unused peer.
707 peer_delete_oldest (void)
709 struct GNUNET_TIME_Absolute abs;
711 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
713 GNUNET_CONTAINER_multipeermap_iterate (peers,
716 GNUNET_CONTAINER_multipeermap_iterate (peers,
723 * Choose the best (yet unused) path towards a peer,
724 * considering the tunnel properties.
726 * @param peer The destination peer.
728 * @return Best current known path towards the peer, if any.
730 static struct MeshPeerPath *
731 peer_get_best_path (const struct MeshPeer *peer)
733 struct MeshPeerPath *best_p;
734 struct MeshPeerPath *p;
735 unsigned int best_cost;
738 best_cost = UINT_MAX;
740 for (p = peer->path_head; NULL != p; p = p->next)
742 if (GNUNET_YES == GMT_is_path_used (peer->tunnel, p))
743 continue; /* If path is already in use, skip it. */
745 if (GNUNET_NO == path_is_valid (p))
746 continue; /* Don't use invalid paths. */
748 if ((cost = GMT_get_path_cost (peer->tunnel, p)) < best_cost)
759 queue_is_sendable (struct MeshPeerQueue *q)
761 /* Is PID-independent? */
764 case GNUNET_MESSAGE_TYPE_MESH_ACK:
765 case GNUNET_MESSAGE_TYPE_MESH_POLL:
766 case GNUNET_MESSAGE_TYPE_MESH_KX:
767 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
768 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
769 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
770 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
771 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
774 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
781 if (GMC_is_sendable (q->c, q->fwd))
789 * Get first sendable message.
791 * @param peer The destination peer.
793 * @return Best current known path towards the peer, if any.
795 static struct MeshPeerQueue *
796 peer_get_first_message (const struct MeshPeer *peer)
798 struct MeshPeerQueue *q;
800 for (q = peer->queue_head; NULL != q; q = q->next)
802 if (queue_is_sendable (q))
811 * Function to process paths received for a new peer addition. The recorded
812 * paths form the initial tunnel, which can be optimized later.
813 * Called on each result obtained for the DHT search.
819 search_handler (void *cls, const struct MeshPeerPath *path)
821 struct MeshPeer *peer = cls;
822 unsigned int connection_count;
824 GMP_add_path_to_all (path, GNUNET_NO);
826 /* Count connections */
827 connection_count = GMT_count_connections (peer->tunnel);
829 /* If we already have 3 (or more (?!)) connections, it's enough */
830 if (3 <= connection_count)
833 if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (peer->tunnel))
835 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
844 * Core callback to write a queued packet to core buffer
846 * @param cls Closure (peer info).
847 * @param size Number of bytes available in buf.
848 * @param buf Where the to write the message.
850 * @return number of bytes written to buf
853 queue_send (void *cls, size_t size, void *buf)
855 struct MeshPeer *peer = cls;
856 struct MeshConnection *c;
857 struct MeshPeerQueue *queue;
858 const struct GNUNET_PeerIdentity *dst_id;
861 peer->core_transmit = NULL;
862 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send towards %s (max %u)\n",
863 GMP_2s (peer), size);
865 if (NULL == buf || 0 == size)
867 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
872 queue = peer_get_first_message (peer);
875 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
880 dst_id = GNUNET_PEER_resolve2 (peer->id);
881 LOG (GNUNET_ERROR_TYPE_DEBUG, "* on connection %s\n", GMC_2s (c));
882 /* Check if buffer size is enough for the message */
883 if (queue->size > size)
885 LOG (GNUNET_ERROR_TYPE_WARNING, "not enough room (%u vs %u), reissue\n",
887 peer->core_transmit =
888 GNUNET_CORE_notify_transmit_ready (core_handle,
889 GNUNET_NO, get_priority (queue),
890 GNUNET_TIME_UNIT_FOREVER_REL,
897 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
902 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
903 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
904 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
905 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
906 case GNUNET_MESSAGE_TYPE_MESH_KX:
907 case GNUNET_MESSAGE_TYPE_MESH_ACK:
908 case GNUNET_MESSAGE_TYPE_MESH_POLL:
909 LOG (GNUNET_ERROR_TYPE_DEBUG, "* raw: %s\n", GM_m2s (queue->type));
910 data_size = send_core_data_raw (queue->cls, size, buf);
912 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
913 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
914 if (GMC_is_origin (c, GNUNET_YES))
915 data_size = send_core_connection_create (queue->c, size, buf);
917 data_size = send_core_data_raw (queue->cls, size, buf);
919 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
920 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
921 if (GMC_is_origin (c, GNUNET_NO) ||
922 GMC_is_origin (c, GNUNET_YES))
923 data_size = send_core_connection_ack (queue->c, size, buf);
925 data_size = send_core_data_raw (queue->cls, size, buf);
927 case GNUNET_MESSAGE_TYPE_MESH_DATA:
928 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
929 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
930 /* This should be encapsulted */
936 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n", queue->type);
940 if (0 < drop_percent &&
941 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
943 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s on connection\n",
944 GM_m2s (queue->type), GMC_2s (c));
949 LOG (GNUNET_ERROR_TYPE_INFO,
950 "ss %s on connection %s (%p) %s (size %u)\n",
951 GM_m2s (queue->type), GMC_2s (c), c, GM_f2s (queue->fwd), data_size);
954 /* Free queue, but cls was freed by send_core_* */
955 GMP_queue_destroy (queue, GNUNET_NO);
957 /* If more data in queue, send next */
958 queue = peer_get_first_message (peer);
961 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
962 if (NULL == peer->core_transmit)
964 peer->core_transmit =
965 GNUNET_CORE_notify_transmit_ready (core_handle,
966 GNUNET_NO, get_priority (queue),
967 GNUNET_TIME_UNIT_FOREVER_REL,
972 queue->start_waiting = GNUNET_TIME_absolute_get ();
976 LOG (GNUNET_ERROR_TYPE_DEBUG,
977 "* tmt rdy called somewhere else\n");
979 // GMC_start_poll (); FIXME needed?
983 // GMC_stop_poll(); FIXME needed?
986 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
991 /******************************************************************************/
992 /******************************** API ***********************************/
993 /******************************************************************************/
997 * Free a transmission that was already queued with all resources
998 * associated to the request.
1000 * @param queue Queue handler to cancel.
1001 * @param clear_cls Is it necessary to free associated cls?
1004 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
1006 struct MeshPeer *peer;
1010 if (GNUNET_YES == clear_cls)
1012 LOG (GNUNET_ERROR_TYPE_DEBUG, "# queue destroy type %s\n",
1013 GM_m2s (queue->type));
1014 switch (queue->type)
1016 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
1017 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1019 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1020 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1021 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1022 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
1023 case GNUNET_MESSAGE_TYPE_MESH_KX:
1024 case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
1025 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1026 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1027 GNUNET_free_non_null (queue->cls);
1032 LOG (GNUNET_ERROR_TYPE_ERROR, "# type %s unknown!\n",
1033 GM_m2s (queue->type));
1036 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1038 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
1039 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
1044 if (NULL != queue->callback)
1046 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Calling callback\n");
1047 queue->callback (queue->callback_cls,
1048 queue->c, queue->type,
1049 queue->fwd, queue->size,
1050 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
1053 GNUNET_free (queue);
1058 * @brief Queue and pass message to core when possible.
1060 * @param peer Peer towards which to queue the message.
1061 * @param cls Closure (@c type dependant). It will be used by queue_send to
1062 * build the message to be sent if not already prebuilt.
1063 * @param type Type of the message, 0 for a raw message.
1064 * @param size Size of the message.
1065 * @param c Connection this message belongs to (can be NULL).
1066 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1067 * @param cont Continuation to be called once CORE has taken the message.
1068 * @param cont_cls Closure for @c cont.
1070 * @return Handle to cancel the message before it is sent. Once cont is called
1071 * message has been sent and therefore the handle is no longer valid.
1073 struct MeshPeerQueue *
1074 GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
1075 struct MeshConnection *c, int fwd,
1076 GMP_sent cont, void *cont_cls)
1078 struct MeshPeerQueue *queue;
1082 LOG (GNUNET_ERROR_TYPE_INFO, "qq %s on connection %s (%p) %s towards %s (size %u)\n",
1083 GM_m2s (type), GMC_2s (c), c, GM_f2s (fwd), GMP_2s(peer), size);
1085 if (NULL == peer->connections)
1087 /* We are not connected to this peer, ignore request. */
1088 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING %s not a neighbor\n", GMP_2s (peer));
1089 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1096 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1097 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1104 call_core = NULL == c ? GNUNET_YES : GMC_is_sendable (c, fwd);
1105 queue = GNUNET_new (struct MeshPeerQueue);
1112 queue->callback = cont;
1113 queue->callback_cls = cont_cls;
1116 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1121 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1122 call_core = GNUNET_YES;
1125 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1127 LOG (GNUNET_ERROR_TYPE_DEBUG,
1128 "calling core tmt rdy towards %s for %u bytes\n",
1129 GMP_2s (peer), size);
1130 peer->core_transmit =
1131 GNUNET_CORE_notify_transmit_ready (core_handle,
1132 GNUNET_NO, get_priority (queue),
1133 GNUNET_TIME_UNIT_FOREVER_REL,
1134 GNUNET_PEER_resolve2 (peer->id),
1138 queue->start_waiting = GNUNET_TIME_absolute_get ();
1142 LOG (GNUNET_ERROR_TYPE_DEBUG,
1143 "core tmt rdy towards %s already called\n",
1152 * Cancel all queued messages to a peer that belong to a certain connection.
1154 * @param peer Peer towards whom to cancel.
1155 * @param c Connection whose queued messages to cancel. Might be destroyed by
1156 * the sent continuation call.
1159 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1161 struct MeshPeerQueue *q;
1162 struct MeshPeerQueue *next;
1163 struct MeshPeerQueue *prev;
1165 for (q = peer->queue_head; NULL != q; q = next)
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP_cancel_queue %s\n", GM_m2s (q->type));
1171 GMP_queue_destroy (q, GNUNET_YES);
1173 /* Get next from prev, q->next might be already freed:
1174 * queue destroy -> callback -> GMC_destroy -> cancel_queues -> here
1177 next = peer->queue_head;
1186 if (NULL == peer->queue_head)
1188 if (NULL != peer->core_transmit)
1190 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1191 peer->core_transmit = NULL;
1198 * Get the first transmittable message for a connection.
1200 * @param peer Neighboring peer.
1201 * @param c Connection.
1203 * @return First transmittable message.
1205 static struct MeshPeerQueue *
1206 connection_get_first_message (struct MeshPeer *peer, struct MeshConnection *c)
1208 struct MeshPeerQueue *q;
1210 for (q = peer->queue_head; NULL != q; q = q->next)
1214 if (queue_is_sendable (q))
1216 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1219 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1227 GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
1229 struct MeshPeerQueue *q;
1232 if (NULL != peer->core_transmit)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1235 return; /* Already unlocked */
1238 q = connection_get_first_message (peer, c);
1241 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1242 return; /* Nothing to transmit */
1246 peer->core_transmit =
1247 GNUNET_CORE_notify_transmit_ready (core_handle,
1248 GNUNET_NO, get_priority (q),
1249 GNUNET_TIME_UNIT_FOREVER_REL,
1250 GNUNET_PEER_resolve2 (peer->id),
1258 * Initialize the peer subsystem.
1260 * @param c Configuration.
1263 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1265 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1266 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1268 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1271 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1272 "MESH", "MAX_PEERS", "USING DEFAULT");
1277 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1284 LOG (GNUNET_ERROR_TYPE_WARNING,
1285 "\n***************************************\n"
1286 "Mesh is running with drop mode enabled.\n"
1287 "This is NOT a good idea!\n"
1288 "Remove the DROP_PERCENT option from your configuration.\n"
1289 "***************************************\n");
1292 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1293 NULL, /* Closure passed to MESH functions */
1294 &core_init, /* Call core_init once connected */
1295 &core_connect, /* Handle connects */
1296 &core_disconnect, /* remove peers on disconnects */
1297 NULL, /* Don't notify about all incoming messages */
1298 GNUNET_NO, /* For header only in notification */
1299 NULL, /* Don't notify about all outbound messages */
1300 GNUNET_NO, /* For header-only out notification */
1301 core_handlers); /* Register these handlers */
1302 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id,
1304 NULL, NULL, NULL); /* Notify callbacks */
1306 if (NULL == core_handle || NULL == transport_handle)
1309 GNUNET_SCHEDULER_shutdown ();
1316 * Shut down the peer subsystem.
1321 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1323 if (core_handle != NULL)
1325 GNUNET_CORE_disconnect (core_handle);
1328 if (transport_handle != NULL)
1330 GNUNET_TRANSPORT_disconnect (transport_handle);
1331 transport_handle = NULL;
1333 GNUNET_PEER_change_rc (myid, -1);
1337 * Retrieve the MeshPeer stucture associated with the peer, create one
1338 * and insert it in the appropriate structures if the peer is not known yet.
1340 * @param peer_id Full identity of the peer.
1342 * @return Existing or newly created peer structure.
1345 GMP_get (const struct GNUNET_PeerIdentity *peer_id)
1347 struct MeshPeer *peer;
1349 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1352 peer = GNUNET_new (struct MeshPeer);
1353 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1355 peer_delete_oldest ();
1357 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1358 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1359 peer->id = GNUNET_PEER_intern (peer_id);
1361 peer->last_contact = GNUNET_TIME_absolute_get();
1368 * Retrieve the MeshPeer stucture associated with the peer, create one
1369 * and insert it in the appropriate structures if the peer is not known yet.
1371 * @param peer Short identity of the peer.
1373 * @return Existing or newly created peer structure.
1376 GMP_get_short (const GNUNET_PEER_Id peer)
1378 return GMP_get (GNUNET_PEER_resolve2 (peer));
1383 * Try to connect to a peer on transport level.
1385 * @param cls Closure (peer).
1386 * @param tc TaskContext.
1389 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1391 struct MeshPeer *peer = cls;
1393 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1396 GNUNET_TRANSPORT_try_connect (transport_handle,
1397 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1402 * Try to establish a new connection to this peer (in its tunnel).
1403 * If the peer doesn't have any path to it yet, try to get one.
1404 * If the peer already has some path, send a CREATE CONNECTION towards it.
1406 * @param peer Peer to connect to.
1409 GMP_connect (struct MeshPeer *peer)
1411 struct MeshTunnel3 *t;
1412 struct MeshPeerPath *p;
1413 struct MeshConnection *c;
1416 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GMP_2s (peer));
1418 /* If we have a current hello, try to connect using it. */
1419 GMP_try_connect (peer);
1423 rerun_search = GNUNET_NO;
1425 if (NULL != peer->path_head)
1427 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1428 p = peer_get_best_path (peer);
1431 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1432 c = GMT_use_path (t, p);
1435 /* This case can happen when the path includes a first hop that is
1436 * not yet known to be connected.
1438 * This happens quite often during testing when running mesh
1439 * under valgrind: core connect notifications come very late and the
1440 * DHT result has already come and created a valid path.
1441 * In this case, the peer->connections hashmap will be NULL and
1442 * tunnel_use_path will not be able to create a connection from that
1445 * Re-running the DHT GET should give core time to callback.
1447 * GMT_use_path -> GMC_new -> register_neighbors takes care of
1448 * updating statistics about this issue.
1450 rerun_search = GNUNET_YES;
1454 GMC_send_create (c);
1460 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1464 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1466 GMD_search_stop (peer->search_h);
1467 peer->search_h = NULL;
1468 LOG (GNUNET_ERROR_TYPE_DEBUG,
1469 " Stopping DHT GET for peer %s\n",
1473 if (NULL == peer->search_h)
1475 const struct GNUNET_PeerIdentity *id;
1477 id = GNUNET_PEER_resolve2 (peer->id);
1478 LOG (GNUNET_ERROR_TYPE_DEBUG,
1479 " Starting DHT GET for peer %s\n", GMP_2s (peer));
1480 peer->search_h = GMD_search (id, &search_handler, peer);
1481 if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
1482 GMT_change_cstate (t, MESH_TUNNEL3_SEARCHING);
1488 * Chech whether there is a direct (core level) connection to peer.
1490 * @param peer Peer to check.
1492 * @return #GNUNET_YES if there is a direct connection.
1495 GMP_is_neighbor (const struct MeshPeer *peer)
1497 struct MeshPeerPath *path;
1499 if (NULL == peer->connections)
1502 for (path = peer->path_head; NULL != path; path = path->next)
1504 if (3 > path->length)
1508 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1514 * Create and initialize a new tunnel towards a peer, in case it has none.
1515 * In case the peer already has a tunnel, nothing is done.
1517 * Does not generate any traffic, just creates the local data structures.
1519 * @param peer Peer towards which to create the tunnel.
1522 GMP_add_tunnel (struct MeshPeer *peer)
1524 if (NULL != peer->tunnel)
1526 peer->tunnel = GMT_new (peer);
1531 * Add a connection to a neighboring peer.
1533 * Store that the peer is the first hop of the connection in one
1534 * direction and that on peer disconnect the connection must be
1535 * notified and destroyed, for it will no longer be valid.
1537 * @param peer Peer to add connection to.
1538 * @param c Connection to add.
1540 * @return GNUNET_OK on success.
1543 GMP_add_connection (struct MeshPeer *peer,
1544 struct MeshConnection *c)
1547 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GMC_2s (c));
1548 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GMP_2s (peer));
1550 if (NULL == peer->connections)
1553 LOG (GNUNET_ERROR_TYPE_DEBUG,
1554 "Peer %s is not a neighbor!\n",
1556 return GNUNET_SYSERR;
1558 LOG (GNUNET_ERROR_TYPE_DEBUG,
1559 "peer %s ok, has %u connections.\n",
1560 GMP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1561 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1564 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1565 LOG (GNUNET_ERROR_TYPE_DEBUG,
1566 " now has %u connections.\n",
1567 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1568 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1575 * Add the path to the peer and update the path used to reach it in case this
1578 * @param peer Destination peer to add the path to.
1579 * @param path New path to add. Last peer must be the peer in arg 1.
1580 * Path will be either used of freed if already known.
1581 * @param trusted Do we trust that this path is real?
1583 * @return path if path was taken, pointer to existing duplicate if exists
1586 struct MeshPeerPath *
1587 GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *path,
1590 struct MeshPeerPath *aux;
1594 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1595 path->length, GMP_2s (peer));
1597 if ((NULL == peer) || (NULL == path))
1600 path_destroy (path);
1603 if (path->peers[path->length - 1] != peer->id)
1606 path_destroy (path);
1609 if (2 >= path->length && GNUNET_NO == trusted)
1611 /* Only allow CORE to tell us about direct paths */
1612 path_destroy (path);
1615 for (l = 1; l < path->length; l++)
1617 if (path->peers[l] == myid)
1619 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1620 for (l2 = 0; l2 < path->length - l; l2++)
1622 path->peers[l2] = path->peers[l + l2];
1626 path->peers = GNUNET_realloc (path->peers,
1627 path->length * sizeof (GNUNET_PEER_Id));
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u]\n", path->length);
1633 l = path_get_length (path);
1636 path_destroy (path);
1640 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1641 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1643 l2 = path_get_length (aux);
1646 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1647 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1648 peer->path_tail, aux, path);
1653 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1655 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1656 path_destroy (path);
1661 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1663 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1669 * Add the path to the origin peer and update the path used to reach it in case
1670 * this is the shortest.
1671 * The path is given in peer_info -> destination, therefore we turn the path
1672 * upside down first.
1674 * @param peer Peer to add the path to, being the origin of the path.
1675 * @param path New path to add after being inversed.
1676 * Path will be either used or freed.
1677 * @param trusted Do we trust that this path is real?
1679 * @return path if path was taken, pointer to existing duplicate if exists
1682 struct MeshPeerPath *
1683 GMP_add_path_to_origin (struct MeshPeer *peer,
1684 struct MeshPeerPath *path,
1690 return GMP_add_path (peer, path, trusted);
1695 * Adds a path to the info of all the peers in the path
1697 * @param p Path to process.
1698 * @param confirmed Whether we know if the path works or not.
1701 GMP_add_path_to_all (const struct MeshPeerPath *p, int confirmed)
1705 /* TODO: invert and add */
1706 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1707 for (i++; i < p->length; i++)
1709 struct MeshPeer *aux;
1710 struct MeshPeerPath *copy;
1712 aux = GMP_get_short (p->peers[i]);
1713 copy = path_duplicate (p);
1714 copy->length = i + 1;
1715 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1721 * Remove any path to the peer that has the extact same peers as the one given.
1723 * @param peer Peer to remove the path from.
1724 * @param path Path to remove. Is always destroyed .
1727 GMP_remove_path (struct MeshPeer *peer, struct MeshPeerPath *path)
1729 struct MeshPeerPath *iter;
1730 struct MeshPeerPath *next;
1732 GNUNET_assert (myid == path->peers[0]);
1733 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1735 for (iter = peer->path_head; NULL != iter; iter = next)
1738 if (0 == memcmp (path->peers, iter->peers,
1739 sizeof (GNUNET_PEER_Id) * path->length))
1741 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
1742 path_destroy (iter);
1747 path_destroy (path);
1752 * Remove a connection from a neighboring peer.
1754 * @param peer Peer to remove connection from.
1755 * @param c Connection to remove.
1757 * @return GNUNET_OK on success.
1760 GMP_remove_connection (struct MeshPeer *peer,
1761 const struct MeshConnection *c)
1763 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GMC_2s (c));
1764 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GMP_2s (peer));
1766 if (NULL == peer || NULL == peer->connections)
1768 LOG (GNUNET_ERROR_TYPE_DEBUG,
1769 "Peer %s is not a neighbor!\n",
1771 return GNUNET_SYSERR;
1773 LOG (GNUNET_ERROR_TYPE_DEBUG,
1774 "peer %s ok, has %u connections.\n",
1775 GMP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1777 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1783 * Start the DHT search for new paths towards the peer: we don't have
1784 * enough good connections.
1786 * @param peer Destination peer.
1789 GMP_start_search (struct MeshPeer *peer)
1791 if (NULL != peer->search_h)
1797 peer->search_h = GMD_search (GMP_get_id (peer), &search_handler, peer);
1802 * Stop the DHT search for new paths towards the peer: we already have
1803 * enough good connections.
1805 * @param peer Destination peer.
1808 GMP_stop_search (struct MeshPeer *peer)
1810 if (NULL == peer->search_h)
1816 GMD_search_stop (peer->search_h);
1817 peer->search_h = NULL;
1822 * Get the Full ID of a peer.
1824 * @param peer Peer to get from.
1826 * @return Full ID of peer.
1828 const struct GNUNET_PeerIdentity *
1829 GMP_get_id (const struct MeshPeer *peer)
1831 return GNUNET_PEER_resolve2 (peer->id);
1836 * Get the Short ID of a peer.
1838 * @param peer Peer to get from.
1840 * @return Short ID of peer.
1843 GMP_get_short_id (const struct MeshPeer *peer)
1856 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel3 *t)
1863 * Get the tunnel towards a peer.
1865 * @param peer Peer to get from.
1867 * @return Tunnel towards peer.
1869 struct MeshTunnel3 *
1870 GMP_get_tunnel (const struct MeshPeer *peer)
1872 return peer->tunnel;
1877 * Set the hello message.
1879 * @param peer Peer whose message to set.
1880 * @param hello Hello message.
1883 GMP_set_hello (struct MeshPeer *peer, const struct GNUNET_HELLO_Message *hello)
1885 struct GNUNET_HELLO_Message *old;
1888 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GMP_2s (peer));
1892 old = GMP_get_hello (peer);
1895 size = GNUNET_HELLO_size (hello);
1896 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
1897 peer->hello = GNUNET_malloc (size);
1898 memcpy (peer->hello, hello, size);
1902 peer->hello = GNUNET_HELLO_merge (old, hello);
1903 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
1904 peer->hello, GNUNET_HELLO_size (hello));
1911 * Get the hello message.
1913 * @param peer Peer whose message to get.
1915 * @return Hello message.
1917 struct GNUNET_HELLO_Message *
1918 GMP_get_hello (struct MeshPeer *peer)
1920 struct GNUNET_TIME_Absolute expiration;
1921 struct GNUNET_TIME_Relative remaining;
1923 if (NULL == peer->hello)
1926 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
1927 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
1928 if (0 == remaining.rel_value_us)
1930 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
1931 GNUNET_STRINGS_absolute_time_to_string (expiration));
1932 GNUNET_free (peer->hello);
1940 * Try to connect to a peer on TRANSPORT level.
1942 * @param peer Peer to whom to connect.
1945 GMP_try_connect (struct MeshPeer *peer)
1947 struct GNUNET_HELLO_Message *hello;
1948 struct GNUNET_MessageHeader *mh;
1950 hello = GMP_get_hello (peer);
1954 mh = GNUNET_HELLO_get_header (hello);
1955 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
1960 * Count the number of known paths toward the peer.
1962 * @param peer Peer to get path info.
1964 * @return Number of known paths.
1967 GMP_count_paths (const struct MeshPeer *peer)
1969 struct MeshPeerPath *iter;
1972 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
1980 * Iterate all known peers.
1982 * @param iter Iterator.
1983 * @param cls Closure for @c iter.
1986 GMP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
1988 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
1993 * Get the static string for a peer ID.
1997 * @return Static string for it's ID.
2000 GMP_2s (const struct MeshPeer *peer)
2004 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));