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 /******************************************************************************/
49 * Struct containing all information regarding a given peer
59 * Last time we heard from this peer
61 struct GNUNET_TIME_Absolute last_contact;
64 * Paths to reach the peer, ordered by ascending hop count
66 struct CadetPeerPath *path_head;
69 * Paths to reach the peer, ordered by ascending hop count
71 struct CadetPeerPath *path_tail;
74 * Handle to stop the DHT search for paths to this peer
76 struct GCD_search_handle *search_h;
79 * Handle to stop the DHT search for paths to this peer
81 struct GNUNET_SCHEDULER_Task *search_delayed;
84 * Tunnel to this peer, if any.
86 struct CadetTunnel *tunnel;
89 * Connections that go through this peer; indexed by tid.
91 struct GNUNET_CONTAINER_MultiHashMap *connections;
94 * Handle for core transmissions.
96 struct GNUNET_MQ_Handle *core_mq;
99 * How many messages are in the queue to this peer.
101 unsigned int queue_n;
106 struct GNUNET_HELLO_Message* hello;
109 * Handle to us offering the HELLO to the transport.
111 struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
114 * Handle to our ATS request asking ATS to suggest an address
115 * to TRANSPORT for this peer (to establish a direct link).
117 struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
123 * Information about a queued message on the peer level.
125 struct CadetPeerQueue {
128 * Envelope to cancel message before MQ sends it.
130 struct GNUNET_MQ_Envelope *env;
133 * Peer (neighbor) this message is being sent to.
135 struct CadetPeer *peer;
138 * Continuation to call to notify higher layers about message sent.
143 * Closure for @a cont.
148 * Time when message was queued for sending.
150 struct GNUNET_TIME_Absolute queue_timestamp;
153 * #GNUNET_YES if message was management traffic (POLL, ACK, ...).
155 int management_traffic;
168 * Type of the message's payload, if it was encrypted data.
170 uint16_t payload_type;
173 *ID of the payload (PID, ACK #, ...).
178 * Connection this message was sent on.
180 struct CadetConnection *c;
183 * Direction in @a c this message was send on (#GNUNET_YES = FWD).
189 /******************************************************************************/
190 /******************************* GLOBALS ***********************************/
191 /******************************************************************************/
194 * Global handle to the statistics service.
196 extern struct GNUNET_STATISTICS_Handle *stats;
199 * Local peer own ID (full value).
201 extern struct GNUNET_PeerIdentity my_full_id;
204 * Local peer own ID (short)
206 extern GNUNET_PEER_Id myid;
209 * Peers known, indexed by PeerIdentity, values of type `struct CadetPeer`.
211 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
214 * How many peers do we want to remember?
216 static unsigned long long max_peers;
219 * Percentage of messages that will be dropped (for test purposes only).
221 static unsigned long long drop_percent;
224 * Handle to communicate with CORE.
226 static struct GNUNET_CORE_Handle *core_handle;
231 static const struct GNUNET_CONFIGURATION_Handle *cfg;
234 * Handle to communicate with ATS.
236 static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
241 static int in_shutdown;
244 /******************************************************************************/
245 /***************************** CORE HELPERS *********************************/
246 /******************************************************************************/
250 * Iterator to notify all connections of a broken link. Mark connections
251 * to destroy after all traffic has been sent.
253 * @param cls Closure (disconnected peer).
254 * @param key Current key code (peer id).
255 * @param value Value in the hash map (connection).
257 * @return #GNUNET_YES to continue to iterate.
260 notify_broken (void *cls,
261 const struct GNUNET_HashCode *key,
264 struct CadetPeer *peer = cls;
265 struct CadetConnection *c = value;
267 LOG (GNUNET_ERROR_TYPE_DEBUG,
268 "Notifying %s due to %s disconnect\n",
269 GCC_2s (c), GCP_2s (peer));
270 GCC_neighbor_disconnected (c, peer);
276 * Remove the direct path to the peer.
278 * @param peer Peer to remove the direct path from.
280 static struct CadetPeerPath *
281 pop_direct_path (struct CadetPeer *peer)
283 struct CadetPeerPath *iter;
285 for (iter = peer->path_head; NULL != iter; iter = iter->next)
287 if (2 >= iter->length)
289 GNUNET_CONTAINER_DLL_remove (peer->path_head,
299 /******************************************************************************/
300 /***************************** CORE CALLBACKS *********************************/
301 /******************************************************************************/
305 * Method called whenever a given peer connects.
307 * @param cls Core closure (unused).
308 * @param peer Peer identity this notification is about
309 * @param mq Message Queue to this peer.
311 * @return Internal closure for handlers (CadetPeer struct).
314 core_connect_handler (void *cls,
315 const struct GNUNET_PeerIdentity *peer,
316 struct GNUNET_MQ_Handle *mq)
318 struct CadetPeer *neighbor;
319 struct CadetPeerPath *path;
322 GCC_check_connections ();
323 GNUNET_snprintf (own_id,
326 GNUNET_i2s (&my_full_id));
328 /* Save a path to the neighbor */
329 neighbor = GCP_get (peer, GNUNET_YES);
330 if (myid == neighbor->id)
332 LOG (GNUNET_ERROR_TYPE_INFO,
333 "CONNECTED %s (self)\n",
339 LOG (GNUNET_ERROR_TYPE_INFO,
340 "CONNECTED %s <= %s\n",
344 path->peers[1] = neighbor->id;
345 GNUNET_PEER_change_rc (neighbor->id, 1);
346 GNUNET_assert (NULL == neighbor->core_mq);
347 neighbor->core_mq = mq;
349 path->peers[0] = myid;
350 GNUNET_PEER_change_rc (myid, 1);
351 GCP_add_path (neighbor, path, GNUNET_YES);
353 /* Create the connections hashmap */
354 GNUNET_assert (NULL == neighbor->connections);
355 neighbor->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
356 GNUNET_STATISTICS_update (stats,
361 if ( (NULL != GCP_get_tunnel (neighbor)) &&
362 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
364 GCP_connect (neighbor);
366 GCC_check_connections ();
373 * Method called whenever a peer disconnects.
375 * @param cls Core closure (unused).
376 * @param peer Peer identity this notification is about.
377 * @param internal_cls Internal closure (CadetPeer struct).
380 core_disconnect_handler (void *cls,
381 const struct GNUNET_PeerIdentity *peer,
384 struct CadetPeer *p = internal_cls;
385 struct CadetPeerPath *direct_path;
388 GCC_check_connections ();
389 strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
393 LOG (GNUNET_ERROR_TYPE_INFO,
394 "DISCONNECTED %s (self)\n",
399 LOG (GNUNET_ERROR_TYPE_INFO,
400 "DISCONNECTED %s <= %s\n",
401 own_id, GNUNET_i2s (peer));
404 direct_path = pop_direct_path (p);
405 if (NULL != p->connections)
407 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
410 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
411 p->connections = NULL;
413 GNUNET_STATISTICS_update (stats,
417 path_destroy (direct_path);
418 GCC_check_connections ();
422 /******************************************************************************/
423 /******************************************************************************/
424 /******************************************************************************/
425 /******************************************************************************/
426 /******************************************************************************/
429 * Check if the create_connection message has the appropriate size.
431 * @param cls Closure (unused).
432 * @param msg Message to check.
434 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
437 check_create (void *cls, const struct GNUNET_CADET_ConnectionCreate *msg)
441 size = ntohs (msg->header.size);
442 if (size < sizeof (*msg))
451 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
453 * @param cls Closure (CadetPeer for neighbor that sent the message).
454 * @param msg Message itself.
457 handle_create (void *cls, const struct GNUNET_CADET_ConnectionCreate *msg)
459 struct CadetPeer *peer = cls;
460 GCC_handle_create (peer, msg);
465 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK
467 * @param cls Closure (CadetPeer for neighbor that sent the message).
468 * @param msg Message itself.
471 handle_confirm (void *cls, const struct GNUNET_CADET_ConnectionACK *msg)
473 struct CadetPeer *peer = cls;
474 GCC_handle_confirm (peer, msg);
479 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
481 * @param cls Closure (CadetPeer for neighbor that sent the message).
482 * @param msg Message itself.
485 handle_broken (void *cls, const struct GNUNET_CADET_ConnectionBroken *msg)
487 struct CadetPeer *peer = cls;
488 GCC_handle_broken (peer, msg);
493 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
495 * @param cls Closure (CadetPeer for neighbor that sent the message).
496 * @param msg Message itself.
499 handle_destroy (void *cls, const struct GNUNET_CADET_ConnectionDestroy *msg)
501 struct CadetPeer *peer = cls;
502 GCC_handle_destroy (peer, msg);
507 * Handle for #GNUNET_MESSAGE_TYPE_CADET_ACK
509 * @param cls Closure (CadetPeer for neighbor that sent the message).
510 * @param msg Message itself.
513 handle_ack (void *cls, const struct GNUNET_CADET_ACK *msg)
515 struct CadetPeer *peer = cls;
516 GCC_handle_ack (peer, msg);
521 * Handle for #GNUNET_MESSAGE_TYPE_CADET_POLL
523 * @param cls Closure (CadetPeer for neighbor that sent the message).
524 * @param msg Message itself.
527 handle_poll (void *cls, const struct GNUNET_CADET_Poll *msg)
529 struct CadetPeer *peer = cls;
530 GCC_handle_poll (peer, msg);
535 * Check if the Key eXchange message has the appropriate size.
537 * @param cls Closure (unused).
538 * @param msg Message to check.
540 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
543 check_kx (void *cls, const struct GNUNET_CADET_KX *msg)
546 uint16_t expected_size;
548 size = ntohs (msg->header.size);
549 expected_size = sizeof (struct GNUNET_CADET_KX)
550 + sizeof (struct GNUNET_MessageHeader);
552 if (size < expected_size)
561 * Handle for #GNUNET_MESSAGE_TYPE_CADET_KX
563 * @param cls Closure (CadetPeer for neighbor that sent the message).
564 * @param msg Message itself.
567 handle_kx (void *cls, const struct GNUNET_CADET_KX *msg)
569 struct CadetPeer *peer = cls;
570 GCC_handle_kx (peer, msg);
575 * Check if the encrypted message has the appropriate size.
577 * @param cls Closure (unused).
578 * @param msg Message to check.
580 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
583 check_encrypted (void *cls, const struct GNUNET_CADET_AX *msg)
586 uint16_t minimum_size;
588 size = ntohs (msg->header.size);
589 minimum_size = sizeof (struct GNUNET_CADET_AX)
590 + sizeof (struct GNUNET_MessageHeader);
592 if (size < minimum_size)
601 * Handle for #GNUNET_MESSAGE_TYPE_CADET_AX (AXolotl encrypted traffic).
603 * @param cls Closure (CadetPeer for neighbor that sent the message).
604 * @param msg Message itself.
607 handle_encrypted (void *cls, const struct GNUNET_CADET_AX *msg)
609 struct CadetPeer *peer = cls;
610 GCC_handle_encrypted (peer, msg);
615 * To be called on core init/fail.
617 * @param cls Closure (config)
618 * @param identity The public identity of this peer.
621 core_init_notify (void *cls,
622 const struct GNUNET_PeerIdentity *identity);
626 connect_to_core (const struct GNUNET_CONFIGURATION_Handle *c)
628 struct GNUNET_MQ_MessageHandler core_handlers[] = {
629 GNUNET_MQ_hd_var_size (create,
630 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
631 struct GNUNET_CADET_ConnectionCreate,
633 GNUNET_MQ_hd_fixed_size (confirm,
634 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
635 struct GNUNET_CADET_ConnectionACK,
637 GNUNET_MQ_hd_fixed_size (broken,
638 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
639 struct GNUNET_CADET_ConnectionBroken,
641 GNUNET_MQ_hd_fixed_size (destroy,
642 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
643 struct GNUNET_CADET_ConnectionDestroy,
645 GNUNET_MQ_hd_fixed_size (ack,
646 GNUNET_MESSAGE_TYPE_CADET_ACK,
647 struct GNUNET_CADET_ACK,
649 GNUNET_MQ_hd_fixed_size (poll,
650 GNUNET_MESSAGE_TYPE_CADET_POLL,
651 struct GNUNET_CADET_Poll,
653 GNUNET_MQ_hd_var_size (kx,
654 GNUNET_MESSAGE_TYPE_CADET_KX,
655 struct GNUNET_CADET_KX,
657 GNUNET_MQ_hd_var_size (encrypted,
658 GNUNET_MESSAGE_TYPE_CADET_AX,
659 struct GNUNET_CADET_AX,
661 GNUNET_MQ_handler_end ()
663 core_handle = GNUNET_CORE_connecT (c, NULL,
665 &core_connect_handler,
666 &core_disconnect_handler,
670 /******************************************************************************/
671 /******************************************************************************/
672 /******************************************************************************/
673 /******************************************************************************/
674 /******************************************************************************/
677 * To be called on core init/fail.
679 * @param cls Closure (config)
680 * @param identity The public identity of this peer.
683 core_init_notify (void *cls,
684 const struct GNUNET_PeerIdentity *core_identity)
686 const struct GNUNET_CONFIGURATION_Handle *c = cls;
688 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
689 if (0 != memcmp (core_identity, &my_full_id, sizeof (my_full_id)))
691 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
692 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (core_identity));
693 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
694 GNUNET_CORE_disconnecT (core_handle);
702 /******************************************************************************/
703 /******************************** STATIC ***********************************/
704 /******************************************************************************/
708 * Get priority for a queued message.
710 * @param q Queued message
712 * @return CORE priority to use.
715 * FIXME use when sending
717 enum GNUNET_CORE_Priority
718 get_priority (struct CadetPeerQueue *q)
720 enum GNUNET_CORE_Priority low;
721 enum GNUNET_CORE_Priority high;
726 return GNUNET_CORE_PRIO_BACKGROUND;
729 /* Relayed traffic has lower priority, our own traffic has higher */
730 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->c_fwd))
732 low = GNUNET_CORE_PRIO_BEST_EFFORT;
733 high = GNUNET_CORE_PRIO_URGENT;
737 low = GNUNET_CORE_PRIO_URGENT;
738 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
741 /* Bulky payload has lower priority, control traffic has higher. */
742 if (GNUNET_MESSAGE_TYPE_CADET_AX == q->type)
749 * Destroy the peer_info and free any allocated resources linked to it
751 * @param peer The peer_info to destroy.
752 * @return #GNUNET_OK on success
755 peer_destroy (struct CadetPeer *peer)
757 struct GNUNET_PeerIdentity id;
758 struct CadetPeerPath *p;
759 struct CadetPeerPath *nextp;
761 GNUNET_PEER_resolve (peer->id, &id);
762 GNUNET_PEER_change_rc (peer->id, -1);
764 LOG (GNUNET_ERROR_TYPE_INFO,
765 "destroying peer %s\n",
769 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
772 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
774 GCP_stop_search (peer);
779 GNUNET_CONTAINER_DLL_remove (peer->path_head,
785 if (NULL != peer->tunnel)
786 GCT_destroy_empty (peer->tunnel);
787 if (NULL != peer->connections)
789 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
790 GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
791 peer->connections = NULL;
793 if (NULL != peer->hello_offer)
795 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
796 peer->hello_offer = NULL;
798 if (NULL != peer->connectivity_suggestion)
800 GNUNET_ATS_connectivity_suggest_cancel (peer->connectivity_suggestion);
801 peer->connectivity_suggestion = NULL;
804 GNUNET_free_non_null (peer->hello);
811 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
814 * @param key current key code
815 * @param value value in the hash map
816 * @return #GNUNET_YES if we should continue to iterate,
820 shutdown_peer (void *cls,
821 const struct GNUNET_PeerIdentity *key,
824 struct CadetPeer *p = value;
825 struct CadetTunnel *t = p->tunnel;
827 LOG (GNUNET_ERROR_TYPE_DEBUG, " shutting down %s\n", GCP_2s (p));
837 * Check if peer is searching for a path (either active or delayed search).
839 * @param peer Peer to check
840 * @return #GNUNET_YES if there is a search active.
841 * #GNUNET_NO otherwise.
844 is_searching (const struct CadetPeer *peer)
846 return ( (NULL == peer->search_h) &&
847 (NULL == peer->search_delayed) ) ?
848 GNUNET_NO : GNUNET_YES;
853 * @brief Start a search for a peer.
855 * @param cls Closure (Peer to search for).
858 delayed_search (void *cls)
860 struct CadetPeer *peer = cls;
862 peer->search_delayed = NULL;
863 GCC_check_connections ();
864 GCP_start_search (peer);
865 GCC_check_connections ();
870 * Returns if peer is used (has a tunnel or is neighbor).
872 * @param peer Peer to check.
873 * @return #GNUNET_YES if peer is in use.
876 peer_is_used (struct CadetPeer *peer)
878 struct CadetPeerPath *p;
880 if (NULL != peer->tunnel)
883 for (p = peer->path_head; NULL != p; p = p->next)
893 * Iterator over all the peers to get the oldest timestamp.
895 * @param cls Closure (unsued).
896 * @param key ID of the peer.
897 * @param value Peer_Info of the peer.
900 peer_get_oldest (void *cls,
901 const struct GNUNET_PeerIdentity *key,
904 struct CadetPeer *p = value;
905 struct GNUNET_TIME_Absolute *abs = cls;
907 /* Don't count active peers */
908 if (GNUNET_YES == peer_is_used (p))
911 if (abs->abs_value_us < p->last_contact.abs_value_us)
912 abs->abs_value_us = p->last_contact.abs_value_us;
919 * Iterator over all the peers to remove the oldest entry.
921 * @param cls Closure (unsued).
922 * @param key ID of the peer.
923 * @param value Peer_Info of the peer.
926 peer_timeout (void *cls,
927 const struct GNUNET_PeerIdentity *key,
930 struct CadetPeer *p = value;
931 struct GNUNET_TIME_Absolute *abs = cls;
933 LOG (GNUNET_ERROR_TYPE_WARNING,
934 "peer %s timeout\n", GNUNET_i2s (key));
936 if (p->last_contact.abs_value_us == abs->abs_value_us &&
937 GNUNET_NO == peer_is_used (p))
947 * Delete oldest unused peer.
950 peer_delete_oldest (void)
952 struct GNUNET_TIME_Absolute abs;
954 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
956 GNUNET_CONTAINER_multipeermap_iterate (peers,
959 GNUNET_CONTAINER_multipeermap_iterate (peers,
966 * Choose the best (yet unused) path towards a peer,
967 * considering the tunnel properties.
969 * @param peer The destination peer.
970 * @return Best current known path towards the peer, if any.
972 static struct CadetPeerPath *
973 peer_get_best_path (const struct CadetPeer *peer)
975 struct CadetPeerPath *best_p;
976 struct CadetPeerPath *p;
977 unsigned int best_cost;
980 best_cost = UINT_MAX;
982 for (p = peer->path_head; NULL != p; p = p->next)
984 if (GNUNET_NO == path_is_valid (p))
985 continue; /* Don't use invalid paths. */
986 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
987 continue; /* If path is already in use, skip it. */
989 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
1000 * Function to process paths received for a new peer addition. The recorded
1001 * paths form the initial tunnel, which can be optimized later.
1002 * Called on each result obtained for the DHT search.
1004 * @param cls Closure (peer towards a path has been found).
1005 * @param path Path created from the DHT query. Will be freed afterwards.
1008 search_handler (void *cls, const struct CadetPeerPath *path)
1010 struct CadetPeer *peer = cls;
1011 unsigned int connection_count;
1013 GCC_check_connections ();
1014 GCP_add_path_to_all (path, GNUNET_NO);
1016 /* Count connections */
1017 connection_count = GCT_count_connections (peer->tunnel);
1019 /* If we already have our minimum (or more) connections, it's enough */
1020 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1022 GCC_check_connections ();
1026 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1028 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1031 GCC_check_connections ();
1036 * Test if a message type is connection management traffic
1037 * or regular payload traffic.
1039 * @param type Message type.
1041 * @return #GNUNET_YES if connection management, #GNUNET_NO otherwise.
1044 is_connection_management (uint16_t type)
1046 return type == GNUNET_MESSAGE_TYPE_CADET_ACK ||
1047 type == GNUNET_MESSAGE_TYPE_CADET_POLL;
1052 * Debug function should NEVER return true in production code, useful to
1053 * simulate losses for testcases.
1055 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
1058 should_I_drop (void)
1060 if (0 == drop_percent)
1063 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1070 /******************************************************************************/
1071 /******************************** API ***********************************/
1072 /******************************************************************************/
1075 * Call the continuation after a message has been sent or dropped.
1077 * @param q Queue handle.
1078 * @param sent #GNUNET_YES if was sent to CORE, #GNUNET_NO if dropped.
1081 call_peer_cont (const struct CadetPeerQueue *q, int sent)
1083 LOG (GNUNET_ERROR_TYPE_DEBUG, " core mq just sent %s\n", GC_m2s (q->type));
1084 if (NULL != q->cont)
1086 struct GNUNET_TIME_Relative wait_time;
1088 wait_time = GNUNET_TIME_absolute_get_duration (q->queue_timestamp);
1089 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1090 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1091 q->cont (q->cont_cls,
1092 q->c, q->c_fwd, sent,
1093 q->type, q->payload_type, q->payload_id,
1094 q->size, wait_time);
1100 * Function called by MQ when a message is sent to CORE.
1102 * @param cls Closure (queue handle).
1107 struct CadetPeerQueue *q = cls;
1109 if (GNUNET_NO == q->management_traffic)
1113 call_peer_cont (q, GNUNET_YES);
1119 * @brief Send a message to another peer (using CORE).
1121 * @param peer Peer towards which to queue the message.
1122 * @param message Message to send.
1123 * @param payload_type Type of the message's payload, for debug messages.
1124 * 0 if the message is a retransmission (unknown payload).
1125 * UINT16_MAX if the message does not have payload.
1126 * @param payload_id ID of the payload (MID, ACK #, etc)
1127 * @param c Connection this message belongs to (can be NULL).
1128 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1129 * @param cont Continuation to be called once CORE has sent the message.
1130 * @param cont_cls Closure for @c cont.
1132 * @return A handle to the message in the queue or NULL (if dropped).
1134 struct CadetPeerQueue *
1135 GCP_send (struct CadetPeer *peer,
1136 const struct GNUNET_MessageHeader *message,
1137 uint16_t payload_type,
1138 uint32_t payload_id,
1139 struct CadetConnection *c,
1144 struct CadetPeerQueue *q;
1148 GCC_check_connections ();
1149 type = ntohs (message->type);
1150 size = ntohs (message->size);
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1153 GC_m2s (type), GC_m2s (payload_type), payload_id,
1154 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1156 if (NULL == peer->connections)
1158 /* We are not connected to this peer, ignore request. */
1160 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1161 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1166 q = GNUNET_new (struct CadetPeerQueue);
1167 q->env = GNUNET_MQ_msg_copy (message);
1170 q->cont_cls = cont_cls;
1171 q->queue_timestamp = GNUNET_TIME_absolute_get ();
1172 q->management_traffic = is_connection_management (type);
1175 q->payload_type = payload_type;
1176 q->payload_id = payload_id;
1179 GNUNET_MQ_notify_sent (q->env, mq_sent, q);
1181 if (GNUNET_YES == q->management_traffic)
1183 GNUNET_MQ_send (peer->core_mq, q->env); // FIXME implement "_urgent", use
1187 if (GNUNET_YES == should_I_drop())
1189 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n",
1190 GC_m2s (q->type), GC_m2s (q->payload_type),
1191 q->payload_id, GCC_2s (c), GC_f2s (q->c_fwd));
1192 GNUNET_MQ_discard (q->env);
1193 call_peer_cont (q, GNUNET_NO);
1197 GNUNET_MQ_send (peer->core_mq, q->env);
1201 GCC_check_connections ();
1207 * Cancel sending a message. Message must have been sent with
1208 * #GCP_send before. May not be called after the notify sent
1209 * callback has been called.
1211 * It DOES call the continuation given to #GCP_send.
1213 * @param q Queue handle to cancel
1216 GCP_send_cancel (struct CadetPeerQueue *q)
1218 call_peer_cont (q, GNUNET_NO);
1219 GNUNET_MQ_send_cancel (q->env);
1225 * Initialize the peer subsystem.
1227 * @param c Configuration.
1230 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1233 LOG (GNUNET_ERROR_TYPE_DEBUG,
1235 in_shutdown = GNUNET_NO;
1236 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1238 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1241 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1242 "CADET", "MAX_PEERS", "USING DEFAULT");
1247 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1254 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1255 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1256 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1257 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1258 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1260 ats_ch = GNUNET_ATS_connectivity_init (c);
1261 connect_to_core (c);
1262 if (NULL == core_handle)
1265 GNUNET_SCHEDULER_shutdown ();
1271 * Shut down the peer subsystem.
1276 LOG (GNUNET_ERROR_TYPE_DEBUG,
1277 "Shutting down peer subsystem\n");
1278 in_shutdown = GNUNET_YES;
1279 if (NULL != core_handle)
1281 GNUNET_CORE_disconnecT (core_handle);
1286 GNUNET_ATS_connectivity_done (ats_ch);
1289 GNUNET_PEER_change_rc (myid, -1);
1290 /* With MQ API, CORE calls the disconnect handler for every peer
1291 * after calling GNUNET_CORE_disconnecT, shutdown must occur *after* that.
1293 GNUNET_CONTAINER_multipeermap_iterate (peers,
1296 GNUNET_CONTAINER_multipeermap_destroy (peers);
1302 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1303 * one and insert it in the appropriate structures if the peer is not known yet.
1305 * @param peer_id Full identity of the peer.
1306 * @param create #GNUNET_YES if a new peer should be created if unknown.
1307 * #GNUNET_NO otherwise.
1309 * @return Existing or newly created peer structure.
1310 * NULL if unknown and not requested @a create
1313 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1315 struct CadetPeer *peer;
1317 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1320 peer = GNUNET_new (struct CadetPeer);
1321 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1323 peer_delete_oldest ();
1325 GNUNET_assert (GNUNET_OK ==
1326 GNUNET_CONTAINER_multipeermap_put (peers,
1329 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1330 peer->id = GNUNET_PEER_intern (peer_id);
1332 peer->last_contact = GNUNET_TIME_absolute_get ();
1339 * Retrieve the CadetPeer stucture associated with the
1340 * peer. Optionally create one and insert it in the appropriate
1341 * structures if the peer is not known yet.
1343 * @param peer Short identity of the peer.
1344 * @param create #GNUNET_YES if a new peer should be created if unknown.
1345 * #GNUNET_NO otherwise.
1347 * @return Existing or newly created peer structure.
1348 * NULL if unknown and not requested @a create
1351 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1353 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1358 * Function called once #GNUNET_TRANSPORT_offer_hello() is done.
1359 * Marks the operation as finished.
1361 * @param cls Closure (our `struct CadetPeer`).
1364 hello_offer_done (void *cls)
1366 struct CadetPeer *peer = cls;
1368 peer->hello_offer = NULL;
1373 * Try to establish a new connection to this peer (in its tunnel).
1374 * If the peer doesn't have any path to it yet, try to get one.
1375 * If the peer already has some path, send a CREATE CONNECTION towards it.
1377 * @param peer Peer to connect to.
1380 GCP_connect (struct CadetPeer *peer)
1382 struct CadetTunnel *t;
1383 struct CadetPeerPath *path;
1384 struct CadetConnection *c;
1387 GCC_check_connections ();
1388 LOG (GNUNET_ERROR_TYPE_DEBUG,
1389 "peer_connect towards %s\n",
1391 /* If we have a current hello, try to connect using it. */
1392 GCP_try_connect (peer);
1396 rerun_search = GNUNET_NO;
1398 if (NULL != peer->path_head)
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1401 path = peer_get_best_path (peer);
1407 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1410 c = GCT_use_path (t, path);
1413 /* This case can happen when the path includes a first hop that is
1414 * not yet known to be connected.
1416 * This happens quite often during testing when running cadet
1417 * under valgrind: core connect notifications come very late
1418 * and the DHT result has already come and created a valid
1419 * path. In this case, the peer->connections
1420 * hashmaps will be NULL and tunnel_use_path will not be able
1421 * to create a connection from that path.
1423 * Re-running the DHT GET should give core time to callback.
1425 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1426 * updating statistics about this issue.
1428 rerun_search = GNUNET_YES;
1432 GCC_send_create (c);
1438 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1442 if (GNUNET_YES == rerun_search)
1444 struct GNUNET_TIME_Relative delay;
1446 GCP_stop_search (peer);
1447 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1448 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
1451 GCC_check_connections ();
1455 if (GNUNET_NO == is_searching (peer))
1456 GCP_start_search (peer);
1457 GCC_check_connections ();
1462 * Chech whether there is a direct (core level) connection to peer.
1464 * @param peer Peer to check.
1466 * @return #GNUNET_YES if there is a direct connection.
1469 GCP_is_neighbor (const struct CadetPeer *peer)
1471 struct CadetPeerPath *path;
1473 if (NULL == peer->connections)
1476 for (path = peer->path_head; NULL != path; path = path->next)
1478 if (3 > path->length)
1482 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1488 * Create and initialize a new tunnel towards a peer, in case it has none.
1489 * In case the peer already has a tunnel, nothing is done.
1491 * Does not generate any traffic, just creates the local data structures.
1493 * @param peer Peer towards which to create the tunnel.
1496 GCP_add_tunnel (struct CadetPeer *peer)
1498 GCC_check_connections ();
1499 if (NULL != peer->tunnel)
1501 peer->tunnel = GCT_new (peer);
1502 GCC_check_connections ();
1507 * Add a connection to a neighboring peer.
1509 * Store that the peer is the first hop of the connection in one
1510 * direction and that on peer disconnect the connection must be
1511 * notified and destroyed, for it will no longer be valid.
1513 * @param peer Peer to add connection to.
1514 * @param c Connection to add.
1515 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
1518 GCP_add_connection (struct CadetPeer *peer,
1519 struct CadetConnection *c,
1522 LOG (GNUNET_ERROR_TYPE_DEBUG,
1523 "adding connection %s\n",
1525 LOG (GNUNET_ERROR_TYPE_DEBUG,
1528 GNUNET_assert (NULL != peer->connections);
1529 GNUNET_assert (GNUNET_OK ==
1530 GNUNET_CONTAINER_multihashmap_put (peer->connections,
1533 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1534 LOG (GNUNET_ERROR_TYPE_DEBUG,
1535 "Peer %s has now %u connections.\n",
1537 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1542 * Add the path to the peer and update the path used to reach it in case this
1545 * @param peer Destination peer to add the path to.
1546 * @param path New path to add. Last peer must be @c peer.
1547 * Path will be either used of freed if already known.
1548 * @param trusted Do we trust that this path is real?
1550 * @return path if path was taken, pointer to existing duplicate if exists
1553 struct CadetPeerPath *
1554 GCP_add_path (struct CadetPeer *peer,
1555 struct CadetPeerPath *path,
1558 struct CadetPeerPath *aux;
1562 GCC_check_connections ();
1563 LOG (GNUNET_ERROR_TYPE_DEBUG,
1564 "adding path [%u] to peer %s\n",
1565 path->length, GCP_2s (peer));
1567 if (NULL == peer || NULL == path
1568 || path->peers[path->length - 1] != peer->id)
1571 path_destroy (path);
1575 for (l = 1; l < path->length; l++)
1577 if (path->peers[l] == myid)
1579 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1580 for (l2 = 0; l2 < path->length - l; l2++)
1582 path->peers[l2] = path->peers[l + l2];
1586 path->peers = GNUNET_realloc (path->peers,
1587 path->length * sizeof (GNUNET_PEER_Id));
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1593 if (2 >= path->length && GNUNET_NO == trusted)
1595 /* Only allow CORE to tell us about direct paths */
1596 path_destroy (path);
1600 l = path_get_length (path);
1603 path_destroy (path);
1607 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1608 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1610 l2 = path_get_length (aux);
1613 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1614 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1615 peer->path_tail, aux, path);
1620 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1623 path_destroy (path);
1628 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head,
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1634 if (NULL != peer->tunnel
1635 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
1636 && 2 < path->length) /* Direct paths are handled by core_connect */
1640 GCC_check_connections ();
1646 * Add the path to the origin peer and update the path used to reach it in case
1647 * this is the shortest.
1648 * The path is given in peer_info -> destination, therefore we turn the path
1649 * upside down first.
1651 * @param peer Peer to add the path to, being the origin of the path.
1652 * @param path New path to add after being inversed.
1653 * Path will be either used or freed.
1654 * @param trusted Do we trust that this path is real?
1656 * @return path if path was taken, pointer to existing duplicate if exists
1659 struct CadetPeerPath *
1660 GCP_add_path_to_origin (struct CadetPeer *peer,
1661 struct CadetPeerPath *path,
1667 return GCP_add_path (peer, path, trusted);
1672 * Adds a path to the info of all the peers in the path
1674 * @param p Path to process.
1675 * @param confirmed Whether we know if the path works or not.
1678 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1682 /* TODO: invert and add to origin */
1683 /* TODO: replace all "GCP_add_path" with this, make the other one static */
1684 GCC_check_connections ();
1685 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1686 for (i++; i < p->length; i++)
1688 struct CadetPeer *peer;
1689 struct CadetPeerPath *copy;
1691 peer = GCP_get_short (p->peers[i], GNUNET_YES);
1692 copy = path_duplicate (p);
1693 copy->length = i + 1;
1694 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
1696 GCC_check_connections ();
1701 * Remove any path to the peer that has the exact same peers as the one given.
1703 * @param peer Peer to remove the path from.
1704 * @param path Path to remove. Is always destroyed .
1707 GCP_remove_path (struct CadetPeer *peer,
1708 struct CadetPeerPath *path)
1710 struct CadetPeerPath *iter;
1711 struct CadetPeerPath *next;
1713 GCC_check_connections ();
1714 GNUNET_assert (myid == path->peers[0]);
1715 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1717 LOG (GNUNET_ERROR_TYPE_INFO,
1718 "Removing path %p (%u) from %s\n",
1719 path, path->length, GCP_2s (peer));
1721 for (iter = peer->path_head; NULL != iter; iter = next)
1724 if (0 == path_cmp (path, iter))
1726 GNUNET_CONTAINER_DLL_remove (peer->path_head,
1730 path_destroy (iter);
1733 path_destroy (path);
1734 GCC_check_connections ();
1739 * Check that we are aware of a connection from a neighboring peer.
1741 * @param peer Peer to the connection is with
1742 * @param c Connection that should be in the map with this peer.
1745 GCP_check_connection (const struct CadetPeer *peer,
1746 const struct CadetConnection *c)
1748 GNUNET_assert (NULL != peer);
1749 GNUNET_assert (NULL != peer->connections);
1751 GNUNET_assert (GNUNET_YES ==
1752 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
1759 * Remove a connection from a neighboring peer.
1761 * @param peer Peer to remove connection from.
1762 * @param c Connection to remove.
1765 GCP_remove_connection (struct CadetPeer *peer,
1766 const struct CadetConnection *c)
1768 LOG (GNUNET_ERROR_TYPE_DEBUG,
1769 "Removing connection %s\n",
1771 LOG (GNUNET_ERROR_TYPE_DEBUG,
1774 if ( (NULL == peer) ||
1775 (NULL == peer->connections) )
1777 GNUNET_assert (GNUNET_YES ==
1778 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1781 LOG (GNUNET_ERROR_TYPE_DEBUG,
1782 "Peer %s remains with %u connections.\n",
1784 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1789 * Start the DHT search for new paths towards the peer: we don't have
1790 * enough good connections.
1792 * @param peer Destination peer.
1795 GCP_start_search (struct CadetPeer *peer)
1797 const struct GNUNET_PeerIdentity *id;
1798 struct CadetTunnel *t = peer->tunnel;
1800 GCC_check_connections ();
1801 if (NULL != peer->search_h)
1807 if (NULL != peer->search_delayed)
1808 GCP_stop_search (peer);
1810 id = GNUNET_PEER_resolve2 (peer->id);
1811 peer->search_h = GCD_search (id, &search_handler, peer);
1815 /* Why would we search for a peer with no tunnel towards it? */
1820 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
1821 || 0 == GCT_count_any_connections (t))
1823 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
1825 GCC_check_connections ();
1830 * Stop the DHT search for new paths towards the peer: we already have
1831 * enough good connections.
1833 * @param peer Destination peer.
1836 GCP_stop_search (struct CadetPeer *peer)
1838 GCC_check_connections ();
1839 if (NULL != peer->search_h)
1841 GCD_search_stop (peer->search_h);
1842 peer->search_h = NULL;
1844 if (NULL != peer->search_delayed)
1846 GNUNET_SCHEDULER_cancel (peer->search_delayed);
1847 peer->search_delayed = NULL;
1849 GCC_check_connections ();
1854 * Get the Full ID of a peer.
1856 * @param peer Peer to get from.
1858 * @return Full ID of peer.
1860 const struct GNUNET_PeerIdentity *
1861 GCP_get_id (const struct CadetPeer *peer)
1863 return GNUNET_PEER_resolve2 (peer->id);
1868 * Get the Short ID of a peer.
1870 * @param peer Peer to get from.
1872 * @return Short ID of peer.
1875 GCP_get_short_id (const struct CadetPeer *peer)
1884 * If tunnel is NULL and there was a search active, stop it, as it's useless.
1890 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
1893 if (NULL == t && GNUNET_YES == is_searching (peer))
1895 GCP_stop_search (peer);
1901 * Get the tunnel towards a peer.
1903 * @param peer Peer to get from.
1905 * @return Tunnel towards peer.
1907 struct CadetTunnel *
1908 GCP_get_tunnel (const struct CadetPeer *peer)
1912 return peer->tunnel;
1917 * Set the hello message.
1919 * @param peer Peer whose message to set.
1920 * @param hello Hello message.
1923 GCP_set_hello (struct CadetPeer *peer,
1924 const struct GNUNET_HELLO_Message *hello)
1926 struct GNUNET_HELLO_Message *old;
1929 GCC_check_connections ();
1930 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
1934 old = GCP_get_hello (peer);
1937 size = GNUNET_HELLO_size (hello);
1938 peer->hello = GNUNET_malloc (size);
1939 GNUNET_memcpy (peer->hello, hello, size);
1943 peer->hello = GNUNET_HELLO_merge (old, hello);
1946 GCC_check_connections ();
1951 * Get the hello message.
1953 * @param peer Peer whose message to get.
1955 * @return Hello message.
1957 struct GNUNET_HELLO_Message *
1958 GCP_get_hello (struct CadetPeer *peer)
1960 struct GNUNET_TIME_Absolute expiration;
1961 struct GNUNET_TIME_Relative remaining;
1963 if (NULL == peer->hello)
1966 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
1967 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
1968 if (0 == remaining.rel_value_us)
1970 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
1971 GNUNET_STRINGS_absolute_time_to_string (expiration));
1972 GNUNET_free (peer->hello);
1980 * Try to connect to a peer on TRANSPORT level.
1982 * @param peer Peer to whom to connect.
1985 GCP_try_connect (struct CadetPeer *peer)
1987 struct GNUNET_HELLO_Message *hello;
1988 struct GNUNET_MessageHeader *mh;
1991 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1993 "DISABLE_TRY_CONNECT"))
1995 GCC_check_connections ();
1996 if (GNUNET_YES == GCP_is_neighbor (peer))
1998 hello = GCP_get_hello (peer);
2002 mh = GNUNET_HELLO_get_header (hello);
2003 if (NULL != peer->hello_offer)
2005 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
2006 peer->hello_offer = NULL;
2008 peer->hello_offer = GNUNET_TRANSPORT_offer_hello (cfg,
2012 if (NULL == peer->connectivity_suggestion)
2013 peer->connectivity_suggestion
2014 = GNUNET_ATS_connectivity_suggest (ats_ch,
2015 GNUNET_PEER_resolve2 (peer->id),
2017 GCC_check_connections ();
2022 * Notify a peer that a link between two other peers is broken. If any path
2023 * used that link, eliminate it.
2025 * @param peer Peer affected by the change.
2026 * @param peer1 Peer whose link is broken.
2027 * @param peer2 Peer whose link is broken.
2030 GCP_notify_broken_link (struct CadetPeer *peer,
2031 const struct GNUNET_PeerIdentity *peer1,
2032 const struct GNUNET_PeerIdentity *peer2)
2034 struct CadetPeerPath *iter;
2035 struct CadetPeerPath *next;
2040 GCC_check_connections ();
2041 p1 = GNUNET_PEER_search (peer1);
2042 p2 = GNUNET_PEER_search (peer2);
2044 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2045 if (0 == p1 || 0 == p2)
2047 /* We don't even know them */
2051 for (iter = peer->path_head; NULL != iter; iter = next)
2054 for (i = 0; i < iter->length - 1; i++)
2056 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2057 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2062 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2065 path_invalidate (iter);
2069 GCC_check_connections ();
2074 * Count the number of known paths toward the peer.
2076 * @param peer Peer to get path info.
2078 * @return Number of known paths.
2081 GCP_count_paths (const struct CadetPeer *peer)
2083 struct CadetPeerPath *iter;
2086 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2094 * Iterate over the paths to a peer.
2096 * @param peer Peer to get path info.
2097 * @param callback Function to call for every path.
2098 * @param cls Closure for @a callback.
2100 * @return Number of iterated paths.
2103 GCP_iterate_paths (struct CadetPeer *peer,
2104 GCP_path_iterator callback,
2107 struct CadetPeerPath *iter;
2110 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2113 if (GNUNET_YES != callback (cls, peer, iter))
2122 * Iterate all known peers.
2124 * @param iter Iterator.
2125 * @param cls Closure for @c iter.
2128 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2131 GCC_check_connections ();
2132 GNUNET_CONTAINER_multipeermap_iterate (peers,
2135 GCC_check_connections ();
2140 * Get the static string for a peer ID.
2144 * @return Static string for it's ID.
2147 GCP_2s (const struct CadetPeer *peer)
2151 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2155 /* end of gnunet-service-cadet_peer.c */