2 This file is part of GNUnet.
3 Copyright (C) 2013, 2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/gnunet-service-cadet_peer.c
22 * @brief GNUnet CADET service connection handling
23 * @author Bartlomiej Polot
26 #include "gnunet_util_lib.h"
27 #include "gnunet_signatures.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "cadet_protocol.h"
33 #include "gnunet-service-cadet_peer.h"
34 #include "gnunet-service-cadet_dht.h"
35 #include "gnunet-service-cadet_connection.h"
36 #include "gnunet-service-cadet_tunnel.h"
37 #include "cadet_path.h"
39 #define LOG(level, ...) GNUNET_log_from (level,"cadet-p2p",__VA_ARGS__)
40 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-p2p",__VA_ARGS__)
43 /******************************************************************************/
44 /******************************** STRUCTS **********************************/
45 /******************************************************************************/
48 * Information about a queued message on the peer level.
50 struct CadetPeerQueue {
52 struct CadetPeerQueue *next;
53 struct CadetPeerQueue *prev;
56 * Envelope to cancel message before MQ sends it.
58 struct GNUNET_MQ_Envelope *env;
61 * Peer (neighbor) this message is being sent to.
63 struct CadetPeer *peer;
66 * Continuation to call to notify higher layers about message sent.
71 * Closure for @a cont.
76 * Task to asynchronously run the drop continuation.
78 struct GNUNET_SCHEDULER_Task *drop_task;
81 * Time when message was queued for sending.
83 struct GNUNET_TIME_Absolute queue_timestamp;
86 * #GNUNET_YES if message was management traffic (POLL, ACK, ...).
88 int management_traffic;
101 * Type of the message's payload, if it was encrypted data.
103 uint16_t payload_type;
106 *ID of the payload (PID, ACK #, ...).
111 * Connection this message was sent on.
113 struct CadetConnection *c;
116 * Direction in @a c this message was send on (#GNUNET_YES = FWD).
123 * Struct containing all information regarding a given peer
132 struct CadetPeerQueue *q_head;
133 struct CadetPeerQueue *q_tail;
136 * Last time we heard from this peer
138 struct GNUNET_TIME_Absolute last_contact;
141 * Paths to reach the peer, ordered by ascending hop count
143 struct CadetPeerPath *path_head;
146 * Paths to reach the peer, ordered by ascending hop count
148 struct CadetPeerPath *path_tail;
151 * Handle to stop the DHT search for paths to this peer
153 struct GCD_search_handle *search_h;
156 * Handle to stop the DHT search for paths to this peer
158 struct GNUNET_SCHEDULER_Task *search_delayed;
161 * Tunnel to this peer, if any.
163 struct CadetTunnel *tunnel;
166 * Connections that go through this peer; indexed by tid.
168 struct GNUNET_CONTAINER_MultiShortmap *connections;
171 * Handle for core transmissions.
173 struct GNUNET_MQ_Handle *core_mq;
176 * How many messages are in the queue to this peer.
178 unsigned int queue_n;
183 struct GNUNET_HELLO_Message* hello;
186 * Handle to us offering the HELLO to the transport.
188 struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
191 * Handle to our ATS request asking ATS to suggest an address
192 * to TRANSPORT for this peer (to establish a direct link).
194 struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
199 /******************************************************************************/
200 /******************************* GLOBALS ***********************************/
201 /******************************************************************************/
204 * Global handle to the statistics service.
206 extern struct GNUNET_STATISTICS_Handle *stats;
209 * Local peer own ID (full value).
211 extern struct GNUNET_PeerIdentity my_full_id;
214 * Local peer own ID (short)
216 extern GNUNET_PEER_Id myid;
219 * Peers known, indexed by PeerIdentity, values of type `struct CadetPeer`.
221 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
224 * How many peers do we want to remember?
226 static unsigned long long max_peers;
229 * Percentage of messages that will be dropped (for test purposes only).
231 static unsigned long long drop_percent;
234 * Handle to communicate with CORE.
236 static struct GNUNET_CORE_Handle *core_handle;
241 static const struct GNUNET_CONFIGURATION_Handle *cfg;
244 * Handle to communicate with ATS.
246 static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
251 static int in_shutdown;
254 /******************************************************************************/
255 /***************************** CORE HELPERS *********************************/
256 /******************************************************************************/
260 * Iterator to notify all connections of a broken link. Mark connections
261 * to destroy after all traffic has been sent.
263 * @param cls Closure (disconnected peer).
264 * @param key Current key code (peer id).
265 * @param value Value in the hash map (connection).
267 * @return #GNUNET_YES to continue to iterate.
270 notify_broken (void *cls,
271 const struct GNUNET_ShortHashCode *key,
274 struct CadetPeer *peer = cls;
275 struct CadetConnection *c = value;
277 LOG (GNUNET_ERROR_TYPE_DEBUG,
278 "Notifying %s due to %s disconnect\n",
279 GCC_2s (c), GCP_2s (peer));
280 GCC_neighbor_disconnected (c, peer);
286 * Remove the direct path to the peer.
288 * @param peer Peer to remove the direct path from.
290 static struct CadetPeerPath *
291 pop_direct_path (struct CadetPeer *peer)
293 struct CadetPeerPath *iter;
295 for (iter = peer->path_head; NULL != iter; iter = iter->next)
297 if (2 >= iter->length)
299 GNUNET_CONTAINER_DLL_remove (peer->path_head,
309 * Call the continuation after a message has been sent or dropped.
311 * This funcion removes the message from the queue.
313 * @param q Queue handle.
314 * @param sent #GNUNET_YES if was sent to CORE, #GNUNET_NO if dropped.
317 call_peer_cont (struct CadetPeerQueue *q, int sent);
320 /******************************************************************************/
321 /***************************** CORE CALLBACKS *********************************/
322 /******************************************************************************/
326 * Method called whenever a given peer connects.
328 * @param cls Core closure (unused).
329 * @param peer Peer identity this notification is about
330 * @param mq Message Queue to this peer.
332 * @return Internal closure for handlers (CadetPeer struct).
335 core_connect_handler (void *cls,
336 const struct GNUNET_PeerIdentity *peer,
337 struct GNUNET_MQ_Handle *mq)
339 struct CadetPeer *neighbor;
340 struct CadetPeerPath *path;
343 GCC_check_connections ();
344 GNUNET_snprintf (own_id,
347 GNUNET_i2s (&my_full_id));
349 /* Save a path to the neighbor */
350 neighbor = GCP_get (peer, GNUNET_YES);
351 if (myid == neighbor->id)
353 LOG (GNUNET_ERROR_TYPE_INFO,
354 "CONNECTED %s (self)\n",
360 LOG (GNUNET_ERROR_TYPE_INFO,
361 "CONNECTED %s <= %s\n",
365 path->peers[1] = neighbor->id;
366 GNUNET_PEER_change_rc (neighbor->id, 1);
367 GNUNET_assert (NULL == neighbor->core_mq);
368 neighbor->core_mq = mq;
370 path->peers[0] = myid;
371 GNUNET_PEER_change_rc (myid, 1);
372 GCP_add_path (neighbor, path, GNUNET_YES);
374 /* Create the connections hashmap */
375 GNUNET_assert (NULL == neighbor->connections);
376 neighbor->connections = GNUNET_CONTAINER_multishortmap_create (16,
378 GNUNET_STATISTICS_update (stats,
383 if ( (NULL != GCP_get_tunnel (neighbor)) &&
384 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
386 GCP_connect (neighbor);
388 GCC_check_connections ();
395 * Method called whenever a peer disconnects.
397 * @param cls Core closure (unused).
398 * @param peer Peer identity this notification is about.
399 * @param internal_cls Internal closure (CadetPeer struct).
402 core_disconnect_handler (void *cls,
403 const struct GNUNET_PeerIdentity *peer,
406 struct CadetPeer *p = internal_cls;
407 struct CadetPeerPath *direct_path;
410 GCC_check_connections ();
411 strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
415 LOG (GNUNET_ERROR_TYPE_INFO,
416 "DISCONNECTED %s (self)\n",
421 LOG (GNUNET_ERROR_TYPE_INFO,
422 "DISCONNECTED %s <= %s\n",
423 own_id, GNUNET_i2s (peer));
426 direct_path = pop_direct_path (p);
427 if (NULL != p->connections)
429 GNUNET_CONTAINER_multishortmap_iterate (p->connections,
432 GNUNET_CONTAINER_multishortmap_destroy (p->connections);
433 p->connections = NULL;
435 GNUNET_STATISTICS_update (stats,
439 path_destroy (direct_path);
440 GCC_check_connections ();
444 /******************************************************************************/
445 /******************************************************************************/
446 /******************************************************************************/
447 /******************************************************************************/
448 /******************************************************************************/
451 * Check if the create_connection message has the appropriate size.
453 * @param cls Closure (unused).
454 * @param msg Message to check.
456 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
459 check_create (void *cls, const struct GNUNET_CADET_ConnectionCreateMessage *msg)
463 size = ntohs (msg->header.size);
464 if (size < sizeof (*msg))
473 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
475 * @param cls Closure (CadetPeer for neighbor that sent the message).
476 * @param msg Message itself.
479 handle_create (void *cls, const struct GNUNET_CADET_ConnectionCreateMessage *msg)
481 struct CadetPeer *peer = cls;
482 GCC_handle_create (peer, msg);
487 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
489 * @param cls Closure (CadetPeer for neighbor that sent the message).
490 * @param msg Message itself.
493 handle_confirm (void *cls, const struct GNUNET_CADET_ConnectionCreateMessageAckMessage *msg)
495 struct CadetPeer *peer = cls;
496 GCC_handle_confirm (peer, msg);
501 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
503 * @param cls Closure (CadetPeer for neighbor that sent the message).
504 * @param msg Message itself.
507 handle_broken (void *cls, const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
509 struct CadetPeer *peer = cls;
510 GCC_handle_broken (peer, msg);
515 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
517 * @param cls Closure (CadetPeer for neighbor that sent the message).
518 * @param msg Message itself.
521 handle_destroy (void *cls, const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
523 struct CadetPeer *peer = cls;
524 GCC_handle_destroy (peer, msg);
529 * Handle for #GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED_HOP_BY_HOP_ACK
531 * @param cls Closure (CadetPeer for neighbor that sent the message).
532 * @param msg Message itself.
535 handle_ack (void *cls, const struct GNUNET_CADET_ConnectionEncryptedAckMessage *msg)
537 struct CadetPeer *peer = cls;
538 GCC_handle_ack (peer, msg);
543 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_POLL
545 * @param cls Closure (CadetPeer for neighbor that sent the message).
546 * @param msg Message itself.
549 handle_poll (void *cls, const struct GNUNET_CADET_ConnectionHopByHopPollMessage *msg)
551 struct CadetPeer *peer = cls;
552 GCC_handle_poll (peer, msg);
557 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
559 * @param cls Closure (CadetPeer for neighbor that sent the message).
560 * @param msg Message itself.
563 handle_kx (void *cls, const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
565 struct CadetPeer *peer = cls;
566 GCC_handle_kx (peer, msg);
571 * Check if the encrypted message has the appropriate size.
573 * @param cls Closure (unused).
574 * @param msg Message to check.
576 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
579 check_encrypted (void *cls, const struct GNUNET_CADET_ConnectionEncryptedMessage *msg)
582 uint16_t minimum_size;
584 size = ntohs (msg->header.size);
585 minimum_size = sizeof (struct GNUNET_CADET_ConnectionEncryptedMessage)
586 + sizeof (struct GNUNET_MessageHeader);
588 if (size < minimum_size)
597 * Handle for #GNUNET_MESSAGE_TYPE_CONNECTION_ENCRYPTED.
599 * @param cls Closure (CadetPeer for neighbor that sent the message).
600 * @param msg Message itself.
603 handle_encrypted (void *cls, const struct GNUNET_CADET_ConnectionEncryptedMessage *msg)
605 struct CadetPeer *peer = cls;
606 GCC_handle_encrypted (peer, msg);
611 * To be called on core init/fail.
613 * @param cls Closure (config)
614 * @param identity The public identity of this peer.
617 core_init_notify (void *cls,
618 const struct GNUNET_PeerIdentity *identity);
622 connect_to_core (const struct GNUNET_CONFIGURATION_Handle *c)
624 struct GNUNET_MQ_MessageHandler core_handlers[] = {
625 GNUNET_MQ_hd_var_size (create,
626 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
627 struct GNUNET_CADET_ConnectionCreateMessage,
629 GNUNET_MQ_hd_fixed_size (confirm,
630 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
631 struct GNUNET_CADET_ConnectionCreateMessageAckMessage,
633 GNUNET_MQ_hd_fixed_size (broken,
634 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
635 struct GNUNET_CADET_ConnectionBrokenMessage,
637 GNUNET_MQ_hd_fixed_size (destroy,
638 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
639 struct GNUNET_CADET_ConnectionDestroyMessage,
641 GNUNET_MQ_hd_fixed_size (ack,
642 GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED_HOP_BY_HOP_ACK,
643 struct GNUNET_CADET_ConnectionEncryptedAckMessage,
645 GNUNET_MQ_hd_fixed_size (poll,
646 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_POLL,
647 struct GNUNET_CADET_ConnectionHopByHopPollMessage,
649 GNUNET_MQ_hd_fixed_size (kx,
650 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
651 struct GNUNET_CADET_TunnelKeyExchangeMessage,
653 GNUNET_MQ_hd_var_size (encrypted,
654 GNUNET_MESSAGE_TYPE_CONNECTION_ENCRYPTED,
655 struct GNUNET_CADET_ConnectionEncryptedMessage,
657 GNUNET_MQ_handler_end ()
659 core_handle = GNUNET_CORE_connect (c, NULL,
661 &core_connect_handler,
662 &core_disconnect_handler,
666 /******************************************************************************/
667 /******************************************************************************/
668 /******************************************************************************/
669 /******************************************************************************/
670 /******************************************************************************/
673 * To be called on core init/fail.
675 * @param cls Closure (config)
676 * @param identity The public identity of this peer.
679 core_init_notify (void *cls,
680 const struct GNUNET_PeerIdentity *core_identity)
682 const struct GNUNET_CONFIGURATION_Handle *c = cls;
684 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
685 if (0 != memcmp (core_identity, &my_full_id, sizeof (my_full_id)))
687 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
688 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (core_identity));
689 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
690 GNUNET_CORE_disconnect (core_handle);
698 /******************************************************************************/
699 /******************************** STATIC ***********************************/
700 /******************************************************************************/
704 * Get priority for a queued message.
706 * @param q Queued message
708 * @return CORE priority to use.
711 * FIXME use when sending
713 enum GNUNET_CORE_Priority
714 get_priority (struct CadetPeerQueue *q)
716 enum GNUNET_CORE_Priority low;
717 enum GNUNET_CORE_Priority high;
722 return GNUNET_CORE_PRIO_BACKGROUND;
725 /* Relayed traffic has lower priority, our own traffic has higher */
726 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->c_fwd))
728 low = GNUNET_CORE_PRIO_BEST_EFFORT;
729 high = GNUNET_CORE_PRIO_URGENT;
733 low = GNUNET_CORE_PRIO_URGENT;
734 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
737 /* Bulky payload has lower priority, control traffic has higher. */
738 if (GNUNET_MESSAGE_TYPE_CONNECTION_ENCRYPTED == q->type)
745 * Cancel all messages queued to CORE MQ towards this peer.
747 * @param peer Peer towards which to cancel all messages.
750 cancel_queued_messages (struct CadetPeer *peer)
752 while (NULL != peer->q_head)
754 struct CadetPeerQueue *q;
757 call_peer_cont (q, GNUNET_NO);
764 * Destroy the peer_info and free any allocated resources linked to it
766 * @param peer The peer_info to destroy.
767 * @return #GNUNET_OK on success
770 peer_destroy (struct CadetPeer *peer)
772 struct GNUNET_PeerIdentity id;
773 struct CadetPeerPath *p;
774 struct CadetPeerPath *nextp;
776 GNUNET_PEER_resolve (peer->id, &id);
777 GNUNET_PEER_change_rc (peer->id, -1);
779 LOG (GNUNET_ERROR_TYPE_INFO,
780 "destroying peer %s\n",
784 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
787 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
789 GCP_stop_search (peer);
794 GNUNET_CONTAINER_DLL_remove (peer->path_head,
800 if (NULL != peer->tunnel)
801 GCT_destroy_empty (peer->tunnel);
802 if (NULL != peer->connections)
804 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (peer->connections));
805 GNUNET_CONTAINER_multishortmap_destroy (peer->connections);
806 peer->connections = NULL;
808 if (NULL != peer->hello_offer)
810 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
811 peer->hello_offer = NULL;
813 if (NULL != peer->connectivity_suggestion)
815 GNUNET_ATS_connectivity_suggest_cancel (peer->connectivity_suggestion);
816 peer->connectivity_suggestion = NULL;
818 cancel_queued_messages (peer);
820 GNUNET_free_non_null (peer->hello);
827 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
830 * @param key current key code
831 * @param value value in the hash map
832 * @return #GNUNET_YES if we should continue to iterate,
836 shutdown_peer (void *cls,
837 const struct GNUNET_PeerIdentity *key,
840 struct CadetPeer *p = value;
841 struct CadetTunnel *t = p->tunnel;
843 LOG (GNUNET_ERROR_TYPE_DEBUG, " shutting down %s\n", GCP_2s (p));
853 * Check if peer is searching for a path (either active or delayed search).
855 * @param peer Peer to check
856 * @return #GNUNET_YES if there is a search active.
857 * #GNUNET_NO otherwise.
860 is_searching (const struct CadetPeer *peer)
862 return ( (NULL == peer->search_h) &&
863 (NULL == peer->search_delayed) ) ?
864 GNUNET_NO : GNUNET_YES;
869 * @brief Start a search for a peer.
871 * @param cls Closure (Peer to search for).
874 delayed_search (void *cls)
876 struct CadetPeer *peer = cls;
878 peer->search_delayed = NULL;
879 GCC_check_connections ();
880 GCP_start_search (peer);
881 GCC_check_connections ();
886 * Returns if peer is used (has a tunnel or is neighbor).
888 * @param peer Peer to check.
889 * @return #GNUNET_YES if peer is in use.
892 peer_is_used (struct CadetPeer *peer)
894 struct CadetPeerPath *p;
896 if (NULL != peer->tunnel)
899 for (p = peer->path_head; NULL != p; p = p->next)
909 * Iterator over all the peers to get the oldest timestamp.
911 * @param cls Closure (unsued).
912 * @param key ID of the peer.
913 * @param value Peer_Info of the peer.
916 peer_get_oldest (void *cls,
917 const struct GNUNET_PeerIdentity *key,
920 struct CadetPeer *p = value;
921 struct GNUNET_TIME_Absolute *abs = cls;
923 /* Don't count active peers */
924 if (GNUNET_YES == peer_is_used (p))
927 if (abs->abs_value_us < p->last_contact.abs_value_us)
928 abs->abs_value_us = p->last_contact.abs_value_us;
935 * Iterator over all the peers to remove the oldest entry.
937 * @param cls Closure (unsued).
938 * @param key ID of the peer.
939 * @param value Peer_Info of the peer.
942 peer_timeout (void *cls,
943 const struct GNUNET_PeerIdentity *key,
946 struct CadetPeer *p = value;
947 struct GNUNET_TIME_Absolute *abs = cls;
949 LOG (GNUNET_ERROR_TYPE_WARNING,
950 "peer %s timeout\n", GNUNET_i2s (key));
952 if (p->last_contact.abs_value_us == abs->abs_value_us &&
953 GNUNET_NO == peer_is_used (p))
963 * Delete oldest unused peer.
966 peer_delete_oldest (void)
968 struct GNUNET_TIME_Absolute abs;
970 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
972 GNUNET_CONTAINER_multipeermap_iterate (peers,
975 GNUNET_CONTAINER_multipeermap_iterate (peers,
982 * Choose the best (yet unused) path towards a peer,
983 * considering the tunnel properties.
985 * @param peer The destination peer.
986 * @return Best current known path towards the peer, if any.
988 static struct CadetPeerPath *
989 peer_get_best_path (const struct CadetPeer *peer)
991 struct CadetPeerPath *best_p;
992 struct CadetPeerPath *p;
993 unsigned int best_cost;
996 best_cost = UINT_MAX;
998 for (p = peer->path_head; NULL != p; p = p->next)
1000 if (GNUNET_NO == path_is_valid (p))
1001 continue; /* Don't use invalid paths. */
1002 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
1003 continue; /* If path is already in use, skip it. */
1005 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
1016 * Function to process paths received for a new peer addition. The recorded
1017 * paths form the initial tunnel, which can be optimized later.
1018 * Called on each result obtained for the DHT search.
1020 * @param cls Closure (peer towards a path has been found).
1021 * @param path Path created from the DHT query. Will be freed afterwards.
1024 search_handler (void *cls, const struct CadetPeerPath *path)
1026 struct CadetPeer *peer = cls;
1027 unsigned int connection_count;
1029 GCC_check_connections ();
1030 GCP_add_path_to_all (path, GNUNET_NO);
1032 /* Count connections */
1033 connection_count = GCT_count_connections (peer->tunnel);
1035 /* If we already have our minimum (or more) connections, it's enough */
1036 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1038 GCC_check_connections ();
1042 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1047 GCC_check_connections ();
1052 * Test if a message type is connection management traffic
1053 * or regular payload traffic.
1055 * @param type Message type.
1057 * @return #GNUNET_YES if connection management, #GNUNET_NO otherwise.
1060 is_connection_management (uint16_t type)
1062 return type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED_HOP_BY_HOP_ACK ||
1063 type == GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_POLL;
1068 * Debug function should NEVER return true in production code, useful to
1069 * simulate losses for testcases.
1071 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
1074 should_I_drop (void)
1076 if (0 == drop_percent)
1079 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1086 /******************************************************************************/
1087 /******************************** API ***********************************/
1088 /******************************************************************************/
1091 * Call the continuation after a message has been sent or dropped.
1093 * This funcion removes the message from the queue.
1095 * @param q Queue handle.
1096 * @param sent #GNUNET_YES if was sent to CORE, #GNUNET_NO if dropped.
1099 call_peer_cont (struct CadetPeerQueue *q, int sent)
1101 LOG (GNUNET_ERROR_TYPE_DEBUG, " core mq just sent %s\n", GC_m2s (q->type));
1102 if (NULL != q->cont)
1104 struct GNUNET_TIME_Relative wait_time;
1106 wait_time = GNUNET_TIME_absolute_get_duration (q->queue_timestamp);
1107 LOG (GNUNET_ERROR_TYPE_DEBUG,
1108 " calling callback on %s after %s\n",
1110 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1111 q->cont (q->cont_cls,
1112 q->c, q->c_fwd, sent,
1113 q->type, q->payload_type, q->payload_id,
1114 q->size, wait_time);
1117 GNUNET_CONTAINER_DLL_remove (q->peer->q_head, q->peer->q_tail, q);
1122 * Function called by MQ when a message is sent to CORE.
1124 * @param cls Closure (queue handle).
1129 struct CadetPeerQueue *q = cls;
1131 if (GNUNET_NO == q->management_traffic)
1135 call_peer_cont (q, GNUNET_YES);
1141 * Finish the drop operation.
1143 * @param cls queue entry to finish drop for
1148 struct CadetPeerQueue *q = cls;
1150 GNUNET_MQ_discard (q->env);
1151 call_peer_cont (q, GNUNET_YES);
1157 * @brief Send a message to another peer (using CORE).
1159 * @param peer Peer towards which to queue the message.
1160 * @param message Message to send.
1161 * @param payload_type Type of the message's payload, for debug messages.
1162 * 0 if the message is a retransmission (unknown payload).
1163 * UINT16_MAX if the message does not have payload.
1164 * @param payload_id ID of the payload (MID, ACK #, etc)
1165 * @param c Connection this message belongs to (can be NULL).
1166 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1167 * @param cont Continuation to be called once CORE has sent the message.
1168 * @param cont_cls Closure for @c cont.
1170 * @return A handle to the message in the queue or NULL (if dropped).
1172 struct CadetPeerQueue *
1173 GCP_send (struct CadetPeer *peer,
1174 const struct GNUNET_MessageHeader *message,
1175 uint16_t payload_type,
1176 uint32_t payload_id,
1177 struct CadetConnection *c,
1182 struct CadetPeerQueue *q;
1186 GCC_check_connections ();
1187 type = ntohs (message->type);
1188 size = ntohs (message->size);
1189 LOG (GNUNET_ERROR_TYPE_DEBUG,
1190 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1191 GC_m2s (type), GC_m2s (payload_type), payload_id,
1192 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1194 if (NULL == peer->connections)
1196 /* We are not connected to this peer, ignore request. */
1198 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1199 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1204 q = GNUNET_new (struct CadetPeerQueue);
1205 q->env = GNUNET_MQ_msg_copy (message);
1208 q->cont_cls = cont_cls;
1209 q->queue_timestamp = GNUNET_TIME_absolute_get ();
1210 q->management_traffic = is_connection_management (type);
1213 q->payload_type = payload_type;
1214 q->payload_id = payload_id;
1217 GNUNET_MQ_notify_sent (q->env, &mq_sent, q);
1218 GNUNET_CONTAINER_DLL_insert (peer->q_head, peer->q_tail, q);
1220 if (GNUNET_YES == q->management_traffic)
1222 GNUNET_MQ_send (peer->core_mq, q->env); // FIXME implement "_urgent", use
1226 if (GNUNET_YES == should_I_drop ())
1228 LOG (GNUNET_ERROR_TYPE_WARNING,
1229 "DD %s (%s %u) on conn %s %s (random drop for testing)\n",
1230 GC_m2s (q->type), GC_m2s (q->payload_type),
1231 q->payload_id, GCC_2s (c), GC_f2s (q->c_fwd));
1232 q->drop_task = GNUNET_SCHEDULER_add_now (&drop_cb,
1236 GNUNET_MQ_send (peer->core_mq, q->env);
1240 GCC_check_connections ();
1246 * Cancel sending a message. Message must have been sent with
1247 * #GCP_send before. May not be called after the notify sent
1248 * callback has been called.
1250 * It DOES call the continuation given to #GCP_send.
1252 * @param q Queue handle to cancel
1255 GCP_send_cancel (struct CadetPeerQueue *q)
1257 if (NULL != q->drop_task)
1259 GNUNET_SCHEDULER_cancel (q->drop_task);
1260 q->drop_task = NULL;
1261 GNUNET_MQ_discard (q->env);
1265 GNUNET_MQ_send_cancel (q->env);
1267 call_peer_cont (q, GNUNET_NO);
1273 * Initialize the peer subsystem.
1275 * @param c Configuration.
1278 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1281 LOG (GNUNET_ERROR_TYPE_DEBUG,
1283 in_shutdown = GNUNET_NO;
1284 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1286 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1289 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1290 "CADET", "MAX_PEERS", "USING DEFAULT");
1295 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1302 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1303 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1304 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1305 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1306 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1308 ats_ch = GNUNET_ATS_connectivity_init (c);
1309 connect_to_core (c);
1310 if (NULL == core_handle)
1313 GNUNET_SCHEDULER_shutdown ();
1319 * Shut down the peer subsystem.
1324 LOG (GNUNET_ERROR_TYPE_DEBUG,
1325 "Shutting down peer subsystem\n");
1326 in_shutdown = GNUNET_YES;
1327 if (NULL != core_handle)
1329 GNUNET_CORE_disconnect (core_handle);
1332 GNUNET_PEER_change_rc (myid, -1);
1333 /* With MQ API, CORE calls the disconnect handler for every peer
1334 * after calling GNUNET_CORE_disconnect, shutdown must occur *after* that.
1336 GNUNET_CONTAINER_multipeermap_iterate (peers,
1341 GNUNET_ATS_connectivity_done (ats_ch);
1344 GNUNET_CONTAINER_multipeermap_destroy (peers);
1350 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1351 * one and insert it in the appropriate structures if the peer is not known yet.
1353 * @param peer_id Full identity of the peer.
1354 * @param create #GNUNET_YES if a new peer should be created if unknown.
1355 * #GNUNET_NO otherwise.
1357 * @return Existing or newly created peer structure.
1358 * NULL if unknown and not requested @a create
1361 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1363 struct CadetPeer *peer;
1365 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1368 peer = GNUNET_new (struct CadetPeer);
1369 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1371 peer_delete_oldest ();
1373 GNUNET_assert (GNUNET_OK ==
1374 GNUNET_CONTAINER_multipeermap_put (peers,
1377 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1378 peer->id = GNUNET_PEER_intern (peer_id);
1380 peer->last_contact = GNUNET_TIME_absolute_get ();
1387 * Retrieve the CadetPeer stucture associated with the
1388 * peer. Optionally create one and insert it in the appropriate
1389 * structures if the peer is not known yet.
1391 * @param peer Short identity of the peer.
1392 * @param create #GNUNET_YES if a new peer should be created if unknown.
1393 * #GNUNET_NO otherwise.
1395 * @return Existing or newly created peer structure.
1396 * NULL if unknown and not requested @a create
1399 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1401 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1406 * Function called once #GNUNET_TRANSPORT_offer_hello() is done.
1407 * Marks the operation as finished.
1409 * @param cls Closure (our `struct CadetPeer`).
1412 hello_offer_done (void *cls)
1414 struct CadetPeer *peer = cls;
1416 peer->hello_offer = NULL;
1421 * Try to establish a new connection to this peer (in its tunnel).
1422 * If the peer doesn't have any path to it yet, try to get one.
1423 * If the peer already has some path, send a CREATE CONNECTION towards it.
1425 * @param peer Peer to connect to.
1428 GCP_connect (struct CadetPeer *peer)
1430 struct CadetTunnel *t;
1431 struct CadetPeerPath *path;
1432 struct CadetConnection *c;
1435 GCC_check_connections ();
1436 LOG (GNUNET_ERROR_TYPE_DEBUG,
1437 "peer_connect towards %s\n",
1439 /* If we have a current hello, try to connect using it. */
1440 GCP_try_connect (peer);
1444 rerun_search = GNUNET_NO;
1446 if (NULL != peer->path_head)
1448 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1449 path = peer_get_best_path (peer);
1455 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1458 c = GCT_use_path (t, path);
1461 /* This case can happen when the path includes a first hop that is
1462 * not yet known to be connected.
1464 * This happens quite often during testing when running cadet
1465 * under valgrind: core connect notifications come very late
1466 * and the DHT result has already come and created a valid
1467 * path. In this case, the peer->connections
1468 * hashmaps will be NULL and tunnel_use_path will not be able
1469 * to create a connection from that path.
1471 * Re-running the DHT GET should give core time to callback.
1473 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1474 * updating statistics about this issue.
1476 rerun_search = GNUNET_YES;
1480 GCC_send_create (c);
1486 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1490 if (GNUNET_YES == rerun_search)
1492 struct GNUNET_TIME_Relative delay;
1494 GCP_stop_search (peer);
1495 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1496 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
1499 GCC_check_connections ();
1503 if (GNUNET_NO == is_searching (peer))
1504 GCP_start_search (peer);
1505 GCC_check_connections ();
1510 * Chech whether there is a direct (core level) connection to peer.
1512 * @param peer Peer to check.
1514 * @return #GNUNET_YES if there is a direct connection.
1517 GCP_is_neighbor (const struct CadetPeer *peer)
1519 struct CadetPeerPath *path;
1521 if (NULL == peer->connections)
1524 for (path = peer->path_head; NULL != path; path = path->next)
1526 if (3 > path->length)
1530 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1536 * Create and initialize a new tunnel towards a peer, in case it has none.
1537 * In case the peer already has a tunnel, nothing is done.
1539 * Does not generate any traffic, just creates the local data structures.
1541 * @param peer Peer towards which to create the tunnel.
1544 GCP_add_tunnel (struct CadetPeer *peer)
1546 GCC_check_connections ();
1547 if (NULL != peer->tunnel)
1549 peer->tunnel = GCT_new (peer);
1550 GCC_check_connections ();
1555 * Add a connection to a neighboring peer.
1557 * Store that the peer is the first hop of the connection in one
1558 * direction and that on peer disconnect the connection must be
1559 * notified and destroyed, for it will no longer be valid.
1561 * @param peer Peer to add connection to.
1562 * @param c Connection to add.
1563 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
1566 GCP_add_connection (struct CadetPeer *peer,
1567 struct CadetConnection *c,
1570 LOG (GNUNET_ERROR_TYPE_DEBUG,
1571 "adding connection %s\n",
1573 LOG (GNUNET_ERROR_TYPE_DEBUG,
1576 GNUNET_assert (NULL != peer->connections);
1577 GNUNET_assert (GNUNET_OK ==
1578 GNUNET_CONTAINER_multishortmap_put (peer->connections,
1579 &GCC_get_id (c)->connection_of_tunnel,
1581 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1582 LOG (GNUNET_ERROR_TYPE_DEBUG,
1583 "Peer %s has now %u connections.\n",
1585 GNUNET_CONTAINER_multishortmap_size (peer->connections));
1590 * Add the path to the peer and update the path used to reach it in case this
1593 * @param peer Destination peer to add the path to.
1594 * @param path New path to add. Last peer must be @c peer.
1595 * Path will be either used of freed if already known.
1596 * @param trusted Do we trust that this path is real?
1598 * @return path if path was taken, pointer to existing duplicate if exists
1601 struct CadetPeerPath *
1602 GCP_add_path (struct CadetPeer *peer,
1603 struct CadetPeerPath *path,
1606 struct CadetPeerPath *aux;
1610 GCC_check_connections ();
1611 LOG (GNUNET_ERROR_TYPE_DEBUG,
1612 "adding path [%u] to peer %s\n",
1613 path->length, GCP_2s (peer));
1615 if (NULL == peer || NULL == path
1616 || path->peers[path->length - 1] != peer->id)
1619 path_destroy (path);
1623 for (l = 1; l < path->length; l++)
1625 if (path->peers[l] == myid)
1627 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1628 for (l2 = 0; l2 < path->length - l; l2++)
1630 path->peers[l2] = path->peers[l + l2];
1634 path->peers = GNUNET_realloc (path->peers,
1635 path->length * sizeof (GNUNET_PEER_Id));
1639 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1641 if (2 >= path->length && GNUNET_NO == trusted)
1643 /* Only allow CORE to tell us about direct paths */
1644 path_destroy (path);
1648 l = path_get_length (path);
1651 path_destroy (path);
1655 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1656 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1658 l2 = path_get_length (aux);
1661 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1662 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1663 peer->path_tail, aux, path);
1668 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1671 path_destroy (path);
1676 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head,
1679 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1682 if (NULL != peer->tunnel
1683 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
1684 && 2 < path->length) /* Direct paths are handled by core_connect */
1688 GCC_check_connections ();
1694 * Add the path to the origin peer and update the path used to reach it in case
1695 * this is the shortest.
1696 * The path is given in peer_info -> destination, therefore we turn the path
1697 * upside down first.
1699 * @param peer Peer to add the path to, being the origin of the path.
1700 * @param path New path to add after being inversed.
1701 * Path will be either used or freed.
1702 * @param trusted Do we trust that this path is real?
1704 * @return path if path was taken, pointer to existing duplicate if exists
1707 struct CadetPeerPath *
1708 GCP_add_path_to_origin (struct CadetPeer *peer,
1709 struct CadetPeerPath *path,
1715 return GCP_add_path (peer, path, trusted);
1720 * Adds a path to the info of all the peers in the path
1722 * @param p Path to process.
1723 * @param confirmed Whether we know if the path works or not.
1726 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1730 /* TODO: invert and add to origin */
1731 /* TODO: replace all "GCP_add_path" with this, make the other one static */
1732 GCC_check_connections ();
1733 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1734 for (i++; i < p->length; i++)
1736 struct CadetPeer *peer;
1737 struct CadetPeerPath *copy;
1739 peer = GCP_get_short (p->peers[i], GNUNET_YES);
1740 copy = path_duplicate (p);
1741 copy->length = i + 1;
1742 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
1744 GCC_check_connections ();
1749 * Remove any path to the peer that has the exact same peers as the one given.
1751 * @param peer Peer to remove the path from.
1752 * @param path Path to remove. Is always destroyed .
1755 GCP_remove_path (struct CadetPeer *peer,
1756 struct CadetPeerPath *path)
1758 struct CadetPeerPath *iter;
1759 struct CadetPeerPath *next;
1761 GCC_check_connections ();
1762 GNUNET_assert (myid == path->peers[0]);
1763 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1765 LOG (GNUNET_ERROR_TYPE_INFO,
1766 "Removing path %p (%u) from %s\n",
1767 path, path->length, GCP_2s (peer));
1769 for (iter = peer->path_head; NULL != iter; iter = next)
1772 if (0 == path_cmp (path, iter))
1774 GNUNET_CONTAINER_DLL_remove (peer->path_head,
1778 path_destroy (iter);
1781 path_destroy (path);
1782 GCC_check_connections ();
1787 * Check that we are aware of a connection from a neighboring peer.
1789 * @param peer Peer to the connection is with
1790 * @param c Connection that should be in the map with this peer.
1793 GCP_check_connection (const struct CadetPeer *peer,
1794 const struct CadetConnection *c)
1796 GNUNET_assert (NULL != peer);
1797 GNUNET_assert (NULL != peer->connections);
1799 GNUNET_assert (GNUNET_YES ==
1800 GNUNET_CONTAINER_multishortmap_contains_value (peer->connections,
1801 &GCC_get_id (c)->connection_of_tunnel,
1807 * Remove a connection from a neighboring peer.
1809 * @param peer Peer to remove connection from.
1810 * @param c Connection to remove.
1813 GCP_remove_connection (struct CadetPeer *peer,
1814 const struct CadetConnection *c)
1816 LOG (GNUNET_ERROR_TYPE_DEBUG,
1817 "Removing connection %s\n",
1819 LOG (GNUNET_ERROR_TYPE_DEBUG,
1822 if ( (NULL == peer) ||
1823 (NULL == peer->connections) )
1825 GNUNET_assert (GNUNET_YES ==
1826 GNUNET_CONTAINER_multishortmap_remove (peer->connections,
1827 &GCC_get_id (c)->connection_of_tunnel,
1829 LOG (GNUNET_ERROR_TYPE_DEBUG,
1830 "Peer %s remains with %u connections.\n",
1832 GNUNET_CONTAINER_multishortmap_size (peer->connections));
1837 * Start the DHT search for new paths towards the peer: we don't have
1838 * enough good connections.
1840 * @param peer Destination peer.
1843 GCP_start_search (struct CadetPeer *peer)
1845 const struct GNUNET_PeerIdentity *id;
1846 struct CadetTunnel *t = peer->tunnel;
1848 GCC_check_connections ();
1849 if (NULL != peer->search_h)
1855 if (NULL != peer->search_delayed)
1856 GCP_stop_search (peer);
1858 id = GNUNET_PEER_resolve2 (peer->id);
1859 peer->search_h = GCD_search (id, &search_handler, peer);
1863 /* Why would we search for a peer with no tunnel towards it? */
1868 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
1869 || 0 == GCT_count_any_connections (t))
1871 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
1873 GCC_check_connections ();
1878 * Stop the DHT search for new paths towards the peer: we already have
1879 * enough good connections.
1881 * @param peer Destination peer.
1884 GCP_stop_search (struct CadetPeer *peer)
1886 GCC_check_connections ();
1887 if (NULL != peer->search_h)
1889 GCD_search_stop (peer->search_h);
1890 peer->search_h = NULL;
1892 if (NULL != peer->search_delayed)
1894 GNUNET_SCHEDULER_cancel (peer->search_delayed);
1895 peer->search_delayed = NULL;
1897 GCC_check_connections ();
1902 * Get the Full ID of a peer.
1904 * @param peer Peer to get from.
1906 * @return Full ID of peer.
1908 const struct GNUNET_PeerIdentity *
1909 GCP_get_id (const struct CadetPeer *peer)
1911 return GNUNET_PEER_resolve2 (peer->id);
1916 * Get the Short ID of a peer.
1918 * @param peer Peer to get from.
1920 * @return Short ID of peer.
1923 GCP_get_short_id (const struct CadetPeer *peer)
1932 * If tunnel is NULL and there was a search active, stop it, as it's useless.
1938 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
1941 if (NULL == t && GNUNET_YES == is_searching (peer))
1943 GCP_stop_search (peer);
1949 * Get the tunnel towards a peer.
1951 * @param peer Peer to get from.
1953 * @return Tunnel towards peer.
1955 struct CadetTunnel *
1956 GCP_get_tunnel (const struct CadetPeer *peer)
1960 return peer->tunnel;
1965 * Set the hello message.
1967 * @param peer Peer whose message to set.
1968 * @param hello Hello message.
1971 GCP_set_hello (struct CadetPeer *peer,
1972 const struct GNUNET_HELLO_Message *hello)
1974 struct GNUNET_HELLO_Message *old;
1977 GCC_check_connections ();
1978 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
1982 old = GCP_get_hello (peer);
1985 size = GNUNET_HELLO_size (hello);
1986 peer->hello = GNUNET_malloc (size);
1987 GNUNET_memcpy (peer->hello, hello, size);
1991 peer->hello = GNUNET_HELLO_merge (old, hello);
1994 GCC_check_connections ();
1999 * Get the hello message.
2001 * @param peer Peer whose message to get.
2003 * @return Hello message.
2005 struct GNUNET_HELLO_Message *
2006 GCP_get_hello (struct CadetPeer *peer)
2008 struct GNUNET_TIME_Absolute expiration;
2009 struct GNUNET_TIME_Relative remaining;
2011 if (NULL == peer->hello)
2014 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2015 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2016 if (0 == remaining.rel_value_us)
2018 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2019 GNUNET_STRINGS_absolute_time_to_string (expiration));
2020 GNUNET_free (peer->hello);
2028 * Try to connect to a peer on TRANSPORT level.
2030 * @param peer Peer to whom to connect.
2033 GCP_try_connect (struct CadetPeer *peer)
2035 struct GNUNET_HELLO_Message *hello;
2036 struct GNUNET_MessageHeader *mh;
2039 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2041 "DISABLE_TRY_CONNECT"))
2043 GCC_check_connections ();
2044 if (GNUNET_YES == GCP_is_neighbor (peer))
2046 hello = GCP_get_hello (peer);
2050 mh = GNUNET_HELLO_get_header (hello);
2051 if (NULL != peer->hello_offer)
2053 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
2054 peer->hello_offer = NULL;
2056 peer->hello_offer = GNUNET_TRANSPORT_offer_hello (cfg,
2060 if (NULL == peer->connectivity_suggestion)
2061 peer->connectivity_suggestion
2062 = GNUNET_ATS_connectivity_suggest (ats_ch,
2065 GCC_check_connections ();
2070 * Notify a peer that a link between two other peers is broken. If any path
2071 * used that link, eliminate it.
2073 * @param peer Peer affected by the change.
2074 * @param peer1 Peer whose link is broken.
2075 * @param peer2 Peer whose link is broken.
2078 GCP_notify_broken_link (struct CadetPeer *peer,
2079 const struct GNUNET_PeerIdentity *peer1,
2080 const struct GNUNET_PeerIdentity *peer2)
2082 struct CadetPeerPath *iter;
2083 struct CadetPeerPath *next;
2088 GCC_check_connections ();
2089 p1 = GNUNET_PEER_search (peer1);
2090 p2 = GNUNET_PEER_search (peer2);
2092 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2093 if (0 == p1 || 0 == p2)
2095 /* We don't even know them */
2099 for (iter = peer->path_head; NULL != iter; iter = next)
2102 for (i = 0; i < iter->length - 1; i++)
2104 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2105 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2110 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2113 path_invalidate (iter);
2117 GCC_check_connections ();
2122 * Count the number of known paths toward the peer.
2124 * @param peer Peer to get path info.
2126 * @return Number of known paths.
2129 GCP_count_paths (const struct CadetPeer *peer)
2131 struct CadetPeerPath *iter;
2134 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2142 * Iterate over the paths to a peer.
2144 * @param peer Peer to get path info.
2145 * @param callback Function to call for every path.
2146 * @param cls Closure for @a callback.
2148 * @return Number of iterated paths.
2151 GCP_iterate_paths (struct CadetPeer *peer,
2152 GCP_path_iterator callback,
2155 struct CadetPeerPath *iter;
2158 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2161 if (GNUNET_YES != callback (cls, peer, iter))
2170 * Iterate all known peers.
2172 * @param iter Iterator.
2173 * @param cls Closure for @c iter.
2176 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2179 GCC_check_connections ();
2180 GNUNET_CONTAINER_multipeermap_iterate (peers,
2183 GCC_check_connections ();
2188 * Get the static string for a peer ID.
2192 * @return Static string for it's ID.
2195 GCP_2s (const struct CadetPeer *peer)
2199 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2203 /* end of gnunet-service-cadet_peer.c */