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 * Time when message was queued for sending.
78 struct GNUNET_TIME_Absolute queue_timestamp;
81 * #GNUNET_YES if message was management traffic (POLL, ACK, ...).
83 int management_traffic;
96 * Type of the message's payload, if it was encrypted data.
98 uint16_t payload_type;
101 *ID of the payload (PID, ACK #, ...).
106 * Connection this message was sent on.
108 struct CadetConnection *c;
111 * Direction in @a c this message was send on (#GNUNET_YES = FWD).
118 * Struct containing all information regarding a given peer
127 struct CadetPeerQueue *q_head;
128 struct CadetPeerQueue *q_tail;
131 * Last time we heard from this peer
133 struct GNUNET_TIME_Absolute last_contact;
136 * Paths to reach the peer, ordered by ascending hop count
138 struct CadetPeerPath *path_head;
141 * Paths to reach the peer, ordered by ascending hop count
143 struct CadetPeerPath *path_tail;
146 * Handle to stop the DHT search for paths to this peer
148 struct GCD_search_handle *search_h;
151 * Handle to stop the DHT search for paths to this peer
153 struct GNUNET_SCHEDULER_Task *search_delayed;
156 * Tunnel to this peer, if any.
158 struct CadetTunnel *tunnel;
161 * Connections that go through this peer; indexed by tid.
163 struct GNUNET_CONTAINER_MultiHashMap *connections;
166 * Handle for core transmissions.
168 struct GNUNET_MQ_Handle *core_mq;
171 * How many messages are in the queue to this peer.
173 unsigned int queue_n;
178 struct GNUNET_HELLO_Message* hello;
181 * Handle to us offering the HELLO to the transport.
183 struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
186 * Handle to our ATS request asking ATS to suggest an address
187 * to TRANSPORT for this peer (to establish a direct link).
189 struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
194 /******************************************************************************/
195 /******************************* GLOBALS ***********************************/
196 /******************************************************************************/
199 * Global handle to the statistics service.
201 extern struct GNUNET_STATISTICS_Handle *stats;
204 * Local peer own ID (full value).
206 extern struct GNUNET_PeerIdentity my_full_id;
209 * Local peer own ID (short)
211 extern GNUNET_PEER_Id myid;
214 * Peers known, indexed by PeerIdentity, values of type `struct CadetPeer`.
216 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
219 * How many peers do we want to remember?
221 static unsigned long long max_peers;
224 * Percentage of messages that will be dropped (for test purposes only).
226 static unsigned long long drop_percent;
229 * Handle to communicate with CORE.
231 static struct GNUNET_CORE_Handle *core_handle;
236 static const struct GNUNET_CONFIGURATION_Handle *cfg;
239 * Handle to communicate with ATS.
241 static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
246 static int in_shutdown;
249 /******************************************************************************/
250 /***************************** CORE HELPERS *********************************/
251 /******************************************************************************/
255 * Iterator to notify all connections of a broken link. Mark connections
256 * to destroy after all traffic has been sent.
258 * @param cls Closure (disconnected peer).
259 * @param key Current key code (peer id).
260 * @param value Value in the hash map (connection).
262 * @return #GNUNET_YES to continue to iterate.
265 notify_broken (void *cls,
266 const struct GNUNET_HashCode *key,
269 struct CadetPeer *peer = cls;
270 struct CadetConnection *c = value;
272 LOG (GNUNET_ERROR_TYPE_DEBUG,
273 "Notifying %s due to %s disconnect\n",
274 GCC_2s (c), GCP_2s (peer));
275 GCC_neighbor_disconnected (c, peer);
281 * Remove the direct path to the peer.
283 * @param peer Peer to remove the direct path from.
285 static struct CadetPeerPath *
286 pop_direct_path (struct CadetPeer *peer)
288 struct CadetPeerPath *iter;
290 for (iter = peer->path_head; NULL != iter; iter = iter->next)
292 if (2 >= iter->length)
294 GNUNET_CONTAINER_DLL_remove (peer->path_head,
304 * Call the continuation after a message has been sent or dropped.
306 * This funcion removes the message from the queue.
308 * @param q Queue handle.
309 * @param sent #GNUNET_YES if was sent to CORE, #GNUNET_NO if dropped.
312 call_peer_cont (struct CadetPeerQueue *q, int sent);
315 /******************************************************************************/
316 /***************************** CORE CALLBACKS *********************************/
317 /******************************************************************************/
321 * Method called whenever a given peer connects.
323 * @param cls Core closure (unused).
324 * @param peer Peer identity this notification is about
325 * @param mq Message Queue to this peer.
327 * @return Internal closure for handlers (CadetPeer struct).
330 core_connect_handler (void *cls,
331 const struct GNUNET_PeerIdentity *peer,
332 struct GNUNET_MQ_Handle *mq)
334 struct CadetPeer *neighbor;
335 struct CadetPeerPath *path;
338 GCC_check_connections ();
339 GNUNET_snprintf (own_id,
342 GNUNET_i2s (&my_full_id));
344 /* Save a path to the neighbor */
345 neighbor = GCP_get (peer, GNUNET_YES);
346 if (myid == neighbor->id)
348 LOG (GNUNET_ERROR_TYPE_INFO,
349 "CONNECTED %s (self)\n",
355 LOG (GNUNET_ERROR_TYPE_INFO,
356 "CONNECTED %s <= %s\n",
360 path->peers[1] = neighbor->id;
361 GNUNET_PEER_change_rc (neighbor->id, 1);
362 GNUNET_assert (NULL == neighbor->core_mq);
363 neighbor->core_mq = mq;
365 path->peers[0] = myid;
366 GNUNET_PEER_change_rc (myid, 1);
367 GCP_add_path (neighbor, path, GNUNET_YES);
369 /* Create the connections hashmap */
370 GNUNET_assert (NULL == neighbor->connections);
371 neighbor->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
372 GNUNET_STATISTICS_update (stats,
377 if ( (NULL != GCP_get_tunnel (neighbor)) &&
378 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
380 GCP_connect (neighbor);
382 GCC_check_connections ();
389 * Method called whenever a peer disconnects.
391 * @param cls Core closure (unused).
392 * @param peer Peer identity this notification is about.
393 * @param internal_cls Internal closure (CadetPeer struct).
396 core_disconnect_handler (void *cls,
397 const struct GNUNET_PeerIdentity *peer,
400 struct CadetPeer *p = internal_cls;
401 struct CadetPeerPath *direct_path;
404 GCC_check_connections ();
405 strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
409 LOG (GNUNET_ERROR_TYPE_INFO,
410 "DISCONNECTED %s (self)\n",
415 LOG (GNUNET_ERROR_TYPE_INFO,
416 "DISCONNECTED %s <= %s\n",
417 own_id, GNUNET_i2s (peer));
420 direct_path = pop_direct_path (p);
421 if (NULL != p->connections)
423 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
426 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
427 p->connections = NULL;
429 GNUNET_STATISTICS_update (stats,
433 path_destroy (direct_path);
434 GCC_check_connections ();
438 /******************************************************************************/
439 /******************************************************************************/
440 /******************************************************************************/
441 /******************************************************************************/
442 /******************************************************************************/
445 * Check if the create_connection message has the appropriate size.
447 * @param cls Closure (unused).
448 * @param msg Message to check.
450 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
453 check_create (void *cls, const struct GNUNET_CADET_ConnectionCreate *msg)
457 size = ntohs (msg->header.size);
458 if (size < sizeof (*msg))
467 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
469 * @param cls Closure (CadetPeer for neighbor that sent the message).
470 * @param msg Message itself.
473 handle_create (void *cls, const struct GNUNET_CADET_ConnectionCreate *msg)
475 struct CadetPeer *peer = cls;
476 GCC_handle_create (peer, msg);
481 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK
483 * @param cls Closure (CadetPeer for neighbor that sent the message).
484 * @param msg Message itself.
487 handle_confirm (void *cls, const struct GNUNET_CADET_ConnectionACK *msg)
489 struct CadetPeer *peer = cls;
490 GCC_handle_confirm (peer, msg);
495 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
497 * @param cls Closure (CadetPeer for neighbor that sent the message).
498 * @param msg Message itself.
501 handle_broken (void *cls, const struct GNUNET_CADET_ConnectionBroken *msg)
503 struct CadetPeer *peer = cls;
504 GCC_handle_broken (peer, msg);
509 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
511 * @param cls Closure (CadetPeer for neighbor that sent the message).
512 * @param msg Message itself.
515 handle_destroy (void *cls, const struct GNUNET_CADET_ConnectionDestroy *msg)
517 struct CadetPeer *peer = cls;
518 GCC_handle_destroy (peer, msg);
523 * Handle for #GNUNET_MESSAGE_TYPE_CADET_ACK
525 * @param cls Closure (CadetPeer for neighbor that sent the message).
526 * @param msg Message itself.
529 handle_ack (void *cls, const struct GNUNET_CADET_ACK *msg)
531 struct CadetPeer *peer = cls;
532 GCC_handle_ack (peer, msg);
537 * Handle for #GNUNET_MESSAGE_TYPE_CADET_POLL
539 * @param cls Closure (CadetPeer for neighbor that sent the message).
540 * @param msg Message itself.
543 handle_poll (void *cls, const struct GNUNET_CADET_Poll *msg)
545 struct CadetPeer *peer = cls;
546 GCC_handle_poll (peer, msg);
551 * Handle for #GNUNET_MESSAGE_TYPE_CADET_KX
553 * @param cls Closure (CadetPeer for neighbor that sent the message).
554 * @param msg Message itself.
557 handle_kx (void *cls, const struct GNUNET_CADET_KX *msg)
559 struct CadetPeer *peer = cls;
560 GCC_handle_kx (peer, msg);
565 * Check if the encrypted message has the appropriate size.
567 * @param cls Closure (unused).
568 * @param msg Message to check.
570 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
573 check_encrypted (void *cls, const struct GNUNET_CADET_Encrypted *msg)
576 uint16_t minimum_size;
578 size = ntohs (msg->header.size);
579 minimum_size = sizeof (struct GNUNET_CADET_Encrypted)
580 + sizeof (struct GNUNET_MessageHeader);
582 if (size < minimum_size)
591 * Handle for #GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED.
593 * @param cls Closure (CadetPeer for neighbor that sent the message).
594 * @param msg Message itself.
597 handle_encrypted (void *cls, const struct GNUNET_CADET_Encrypted *msg)
599 struct CadetPeer *peer = cls;
600 GCC_handle_encrypted (peer, msg);
605 * To be called on core init/fail.
607 * @param cls Closure (config)
608 * @param identity The public identity of this peer.
611 core_init_notify (void *cls,
612 const struct GNUNET_PeerIdentity *identity);
616 connect_to_core (const struct GNUNET_CONFIGURATION_Handle *c)
618 struct GNUNET_MQ_MessageHandler core_handlers[] = {
619 GNUNET_MQ_hd_var_size (create,
620 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
621 struct GNUNET_CADET_ConnectionCreate,
623 GNUNET_MQ_hd_fixed_size (confirm,
624 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
625 struct GNUNET_CADET_ConnectionACK,
627 GNUNET_MQ_hd_fixed_size (broken,
628 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
629 struct GNUNET_CADET_ConnectionBroken,
631 GNUNET_MQ_hd_fixed_size (destroy,
632 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
633 struct GNUNET_CADET_ConnectionDestroy,
635 GNUNET_MQ_hd_fixed_size (ack,
636 GNUNET_MESSAGE_TYPE_CADET_ACK,
637 struct GNUNET_CADET_ACK,
639 GNUNET_MQ_hd_fixed_size (poll,
640 GNUNET_MESSAGE_TYPE_CADET_POLL,
641 struct GNUNET_CADET_Poll,
643 GNUNET_MQ_hd_fixed_size (kx,
644 GNUNET_MESSAGE_TYPE_CADET_KX,
645 struct GNUNET_CADET_KX,
647 GNUNET_MQ_hd_var_size (encrypted,
648 GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED,
649 struct GNUNET_CADET_Encrypted,
651 GNUNET_MQ_handler_end ()
653 core_handle = GNUNET_CORE_connecT (c, NULL,
655 &core_connect_handler,
656 &core_disconnect_handler,
660 /******************************************************************************/
661 /******************************************************************************/
662 /******************************************************************************/
663 /******************************************************************************/
664 /******************************************************************************/
667 * To be called on core init/fail.
669 * @param cls Closure (config)
670 * @param identity The public identity of this peer.
673 core_init_notify (void *cls,
674 const struct GNUNET_PeerIdentity *core_identity)
676 const struct GNUNET_CONFIGURATION_Handle *c = cls;
678 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
679 if (0 != memcmp (core_identity, &my_full_id, sizeof (my_full_id)))
681 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
682 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (core_identity));
683 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
684 GNUNET_CORE_disconnecT (core_handle);
692 /******************************************************************************/
693 /******************************** STATIC ***********************************/
694 /******************************************************************************/
698 * Get priority for a queued message.
700 * @param q Queued message
702 * @return CORE priority to use.
705 * FIXME use when sending
707 enum GNUNET_CORE_Priority
708 get_priority (struct CadetPeerQueue *q)
710 enum GNUNET_CORE_Priority low;
711 enum GNUNET_CORE_Priority high;
716 return GNUNET_CORE_PRIO_BACKGROUND;
719 /* Relayed traffic has lower priority, our own traffic has higher */
720 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->c_fwd))
722 low = GNUNET_CORE_PRIO_BEST_EFFORT;
723 high = GNUNET_CORE_PRIO_URGENT;
727 low = GNUNET_CORE_PRIO_URGENT;
728 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
731 /* Bulky payload has lower priority, control traffic has higher. */
732 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
739 * Cancel all messages queued to CORE MQ towards this peer.
741 * @param peer Peer towards which to cancel all messages.
744 cancel_queued_messages (struct CadetPeer *peer)
746 while (NULL != peer->q_head)
748 struct CadetPeerQueue *q;
751 call_peer_cont (q, GNUNET_NO);
758 * Destroy the peer_info and free any allocated resources linked to it
760 * @param peer The peer_info to destroy.
761 * @return #GNUNET_OK on success
764 peer_destroy (struct CadetPeer *peer)
766 struct GNUNET_PeerIdentity id;
767 struct CadetPeerPath *p;
768 struct CadetPeerPath *nextp;
770 GNUNET_PEER_resolve (peer->id, &id);
771 GNUNET_PEER_change_rc (peer->id, -1);
773 LOG (GNUNET_ERROR_TYPE_INFO,
774 "destroying peer %s\n",
778 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
781 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
783 GCP_stop_search (peer);
788 GNUNET_CONTAINER_DLL_remove (peer->path_head,
794 if (NULL != peer->tunnel)
795 GCT_destroy_empty (peer->tunnel);
796 if (NULL != peer->connections)
798 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
799 GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
800 peer->connections = NULL;
802 if (NULL != peer->hello_offer)
804 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
805 peer->hello_offer = NULL;
807 if (NULL != peer->connectivity_suggestion)
809 GNUNET_ATS_connectivity_suggest_cancel (peer->connectivity_suggestion);
810 peer->connectivity_suggestion = NULL;
812 cancel_queued_messages (peer);
814 GNUNET_free_non_null (peer->hello);
821 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
824 * @param key current key code
825 * @param value value in the hash map
826 * @return #GNUNET_YES if we should continue to iterate,
830 shutdown_peer (void *cls,
831 const struct GNUNET_PeerIdentity *key,
834 struct CadetPeer *p = value;
835 struct CadetTunnel *t = p->tunnel;
837 LOG (GNUNET_ERROR_TYPE_DEBUG, " shutting down %s\n", GCP_2s (p));
847 * Check if peer is searching for a path (either active or delayed search).
849 * @param peer Peer to check
850 * @return #GNUNET_YES if there is a search active.
851 * #GNUNET_NO otherwise.
854 is_searching (const struct CadetPeer *peer)
856 return ( (NULL == peer->search_h) &&
857 (NULL == peer->search_delayed) ) ?
858 GNUNET_NO : GNUNET_YES;
863 * @brief Start a search for a peer.
865 * @param cls Closure (Peer to search for).
868 delayed_search (void *cls)
870 struct CadetPeer *peer = cls;
872 peer->search_delayed = NULL;
873 GCC_check_connections ();
874 GCP_start_search (peer);
875 GCC_check_connections ();
880 * Returns if peer is used (has a tunnel or is neighbor).
882 * @param peer Peer to check.
883 * @return #GNUNET_YES if peer is in use.
886 peer_is_used (struct CadetPeer *peer)
888 struct CadetPeerPath *p;
890 if (NULL != peer->tunnel)
893 for (p = peer->path_head; NULL != p; p = p->next)
903 * Iterator over all the peers to get the oldest timestamp.
905 * @param cls Closure (unsued).
906 * @param key ID of the peer.
907 * @param value Peer_Info of the peer.
910 peer_get_oldest (void *cls,
911 const struct GNUNET_PeerIdentity *key,
914 struct CadetPeer *p = value;
915 struct GNUNET_TIME_Absolute *abs = cls;
917 /* Don't count active peers */
918 if (GNUNET_YES == peer_is_used (p))
921 if (abs->abs_value_us < p->last_contact.abs_value_us)
922 abs->abs_value_us = p->last_contact.abs_value_us;
929 * Iterator over all the peers to remove the oldest entry.
931 * @param cls Closure (unsued).
932 * @param key ID of the peer.
933 * @param value Peer_Info of the peer.
936 peer_timeout (void *cls,
937 const struct GNUNET_PeerIdentity *key,
940 struct CadetPeer *p = value;
941 struct GNUNET_TIME_Absolute *abs = cls;
943 LOG (GNUNET_ERROR_TYPE_WARNING,
944 "peer %s timeout\n", GNUNET_i2s (key));
946 if (p->last_contact.abs_value_us == abs->abs_value_us &&
947 GNUNET_NO == peer_is_used (p))
957 * Delete oldest unused peer.
960 peer_delete_oldest (void)
962 struct GNUNET_TIME_Absolute abs;
964 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
966 GNUNET_CONTAINER_multipeermap_iterate (peers,
969 GNUNET_CONTAINER_multipeermap_iterate (peers,
976 * Choose the best (yet unused) path towards a peer,
977 * considering the tunnel properties.
979 * @param peer The destination peer.
980 * @return Best current known path towards the peer, if any.
982 static struct CadetPeerPath *
983 peer_get_best_path (const struct CadetPeer *peer)
985 struct CadetPeerPath *best_p;
986 struct CadetPeerPath *p;
987 unsigned int best_cost;
990 best_cost = UINT_MAX;
992 for (p = peer->path_head; NULL != p; p = p->next)
994 if (GNUNET_NO == path_is_valid (p))
995 continue; /* Don't use invalid paths. */
996 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
997 continue; /* If path is already in use, skip it. */
999 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
1010 * Function to process paths received for a new peer addition. The recorded
1011 * paths form the initial tunnel, which can be optimized later.
1012 * Called on each result obtained for the DHT search.
1014 * @param cls Closure (peer towards a path has been found).
1015 * @param path Path created from the DHT query. Will be freed afterwards.
1018 search_handler (void *cls, const struct CadetPeerPath *path)
1020 struct CadetPeer *peer = cls;
1021 unsigned int connection_count;
1023 GCC_check_connections ();
1024 GCP_add_path_to_all (path, GNUNET_NO);
1026 /* Count connections */
1027 connection_count = GCT_count_connections (peer->tunnel);
1029 /* If we already have our minimum (or more) connections, it's enough */
1030 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1032 GCC_check_connections ();
1036 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1038 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1041 GCC_check_connections ();
1046 * Test if a message type is connection management traffic
1047 * or regular payload traffic.
1049 * @param type Message type.
1051 * @return #GNUNET_YES if connection management, #GNUNET_NO otherwise.
1054 is_connection_management (uint16_t type)
1056 return type == GNUNET_MESSAGE_TYPE_CADET_ACK ||
1057 type == GNUNET_MESSAGE_TYPE_CADET_POLL;
1062 * Debug function should NEVER return true in production code, useful to
1063 * simulate losses for testcases.
1065 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
1068 should_I_drop (void)
1070 if (0 == drop_percent)
1073 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1080 /******************************************************************************/
1081 /******************************** API ***********************************/
1082 /******************************************************************************/
1085 * Call the continuation after a message has been sent or dropped.
1087 * This funcion removes the message from the queue.
1089 * @param q Queue handle.
1090 * @param sent #GNUNET_YES if was sent to CORE, #GNUNET_NO if dropped.
1093 call_peer_cont (struct CadetPeerQueue *q, int sent)
1095 LOG (GNUNET_ERROR_TYPE_DEBUG, " core mq just sent %s\n", GC_m2s (q->type));
1096 if (NULL != q->cont)
1098 struct GNUNET_TIME_Relative wait_time;
1100 wait_time = GNUNET_TIME_absolute_get_duration (q->queue_timestamp);
1101 LOG (GNUNET_ERROR_TYPE_DEBUG,
1102 " calling callback on %s after %s\n",
1104 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1105 q->cont (q->cont_cls,
1106 q->c, q->c_fwd, sent,
1107 q->type, q->payload_type, q->payload_id,
1108 q->size, wait_time);
1111 GNUNET_CONTAINER_DLL_remove (q->peer->q_head, q->peer->q_tail, q);
1116 * Function called by MQ when a message is sent to CORE.
1118 * @param cls Closure (queue handle).
1123 struct CadetPeerQueue *q = cls;
1125 if (GNUNET_NO == q->management_traffic)
1129 call_peer_cont (q, GNUNET_YES);
1135 * @brief Send a message to another peer (using CORE).
1137 * @param peer Peer towards which to queue the message.
1138 * @param message Message to send.
1139 * @param payload_type Type of the message's payload, for debug messages.
1140 * 0 if the message is a retransmission (unknown payload).
1141 * UINT16_MAX if the message does not have payload.
1142 * @param payload_id ID of the payload (MID, ACK #, etc)
1143 * @param c Connection this message belongs to (can be NULL).
1144 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1145 * @param cont Continuation to be called once CORE has sent the message.
1146 * @param cont_cls Closure for @c cont.
1148 * @return A handle to the message in the queue or NULL (if dropped).
1150 struct CadetPeerQueue *
1151 GCP_send (struct CadetPeer *peer,
1152 const struct GNUNET_MessageHeader *message,
1153 uint16_t payload_type,
1154 uint32_t payload_id,
1155 struct CadetConnection *c,
1160 struct CadetPeerQueue *q;
1164 GCC_check_connections ();
1165 type = ntohs (message->type);
1166 size = ntohs (message->size);
1167 LOG (GNUNET_ERROR_TYPE_DEBUG,
1168 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1169 GC_m2s (type), GC_m2s (payload_type), payload_id,
1170 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1172 if (NULL == peer->connections)
1174 /* We are not connected to this peer, ignore request. */
1176 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1177 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1182 q = GNUNET_new (struct CadetPeerQueue);
1183 q->env = GNUNET_MQ_msg_copy (message);
1186 q->cont_cls = cont_cls;
1187 q->queue_timestamp = GNUNET_TIME_absolute_get ();
1188 q->management_traffic = is_connection_management (type);
1191 q->payload_type = payload_type;
1192 q->payload_id = payload_id;
1195 GNUNET_MQ_notify_sent (q->env, &mq_sent, q);
1196 GNUNET_CONTAINER_DLL_insert (peer->q_head, peer->q_tail, q);
1198 if (GNUNET_YES == q->management_traffic)
1200 GNUNET_MQ_send (peer->core_mq, q->env); // FIXME implement "_urgent", use
1204 if (GNUNET_YES == should_I_drop ())
1206 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n",
1207 GC_m2s (q->type), GC_m2s (q->payload_type),
1208 q->payload_id, GCC_2s (c), GC_f2s (q->c_fwd));
1209 GNUNET_MQ_discard (q->env);
1210 call_peer_cont (q, GNUNET_YES);
1211 GNUNET_CONTAINER_DLL_remove (peer->q_head, peer->q_tail, q);
1215 GNUNET_MQ_send (peer->core_mq, q->env);
1219 GCC_check_connections ();
1225 * Cancel sending a message. Message must have been sent with
1226 * #GCP_send before. May not be called after the notify sent
1227 * callback has been called.
1229 * It DOES call the continuation given to #GCP_send.
1231 * @param q Queue handle to cancel
1234 GCP_send_cancel (struct CadetPeerQueue *q)
1236 call_peer_cont (q, GNUNET_NO);
1237 GNUNET_MQ_send_cancel (q->env);
1243 * Initialize the peer subsystem.
1245 * @param c Configuration.
1248 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1251 LOG (GNUNET_ERROR_TYPE_DEBUG,
1253 in_shutdown = GNUNET_NO;
1254 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1256 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1259 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1260 "CADET", "MAX_PEERS", "USING DEFAULT");
1265 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1272 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1273 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1274 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1275 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1276 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1278 ats_ch = GNUNET_ATS_connectivity_init (c);
1279 connect_to_core (c);
1280 if (NULL == core_handle)
1283 GNUNET_SCHEDULER_shutdown ();
1289 * Shut down the peer subsystem.
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "Shutting down peer subsystem\n");
1296 in_shutdown = GNUNET_YES;
1297 if (NULL != core_handle)
1299 GNUNET_CORE_disconnecT (core_handle);
1302 GNUNET_PEER_change_rc (myid, -1);
1303 /* With MQ API, CORE calls the disconnect handler for every peer
1304 * after calling GNUNET_CORE_disconnecT, shutdown must occur *after* that.
1306 GNUNET_CONTAINER_multipeermap_iterate (peers,
1311 GNUNET_ATS_connectivity_done (ats_ch);
1314 GNUNET_CONTAINER_multipeermap_destroy (peers);
1320 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1321 * one and insert it in the appropriate structures if the peer is not known yet.
1323 * @param peer_id Full identity of the peer.
1324 * @param create #GNUNET_YES if a new peer should be created if unknown.
1325 * #GNUNET_NO otherwise.
1327 * @return Existing or newly created peer structure.
1328 * NULL if unknown and not requested @a create
1331 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1333 struct CadetPeer *peer;
1335 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1338 peer = GNUNET_new (struct CadetPeer);
1339 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1341 peer_delete_oldest ();
1343 GNUNET_assert (GNUNET_OK ==
1344 GNUNET_CONTAINER_multipeermap_put (peers,
1347 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1348 peer->id = GNUNET_PEER_intern (peer_id);
1350 peer->last_contact = GNUNET_TIME_absolute_get ();
1357 * Retrieve the CadetPeer stucture associated with the
1358 * peer. Optionally create one and insert it in the appropriate
1359 * structures if the peer is not known yet.
1361 * @param peer Short identity of the peer.
1362 * @param create #GNUNET_YES if a new peer should be created if unknown.
1363 * #GNUNET_NO otherwise.
1365 * @return Existing or newly created peer structure.
1366 * NULL if unknown and not requested @a create
1369 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1371 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1376 * Function called once #GNUNET_TRANSPORT_offer_hello() is done.
1377 * Marks the operation as finished.
1379 * @param cls Closure (our `struct CadetPeer`).
1382 hello_offer_done (void *cls)
1384 struct CadetPeer *peer = cls;
1386 peer->hello_offer = NULL;
1391 * Try to establish a new connection to this peer (in its tunnel).
1392 * If the peer doesn't have any path to it yet, try to get one.
1393 * If the peer already has some path, send a CREATE CONNECTION towards it.
1395 * @param peer Peer to connect to.
1398 GCP_connect (struct CadetPeer *peer)
1400 struct CadetTunnel *t;
1401 struct CadetPeerPath *path;
1402 struct CadetConnection *c;
1405 GCC_check_connections ();
1406 LOG (GNUNET_ERROR_TYPE_DEBUG,
1407 "peer_connect towards %s\n",
1409 /* If we have a current hello, try to connect using it. */
1410 GCP_try_connect (peer);
1414 rerun_search = GNUNET_NO;
1416 if (NULL != peer->path_head)
1418 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1419 path = peer_get_best_path (peer);
1425 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1428 c = GCT_use_path (t, path);
1431 /* This case can happen when the path includes a first hop that is
1432 * not yet known to be connected.
1434 * This happens quite often during testing when running cadet
1435 * under valgrind: core connect notifications come very late
1436 * and the DHT result has already come and created a valid
1437 * path. In this case, the peer->connections
1438 * hashmaps will be NULL and tunnel_use_path will not be able
1439 * to create a connection from that path.
1441 * Re-running the DHT GET should give core time to callback.
1443 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1444 * updating statistics about this issue.
1446 rerun_search = GNUNET_YES;
1450 GCC_send_create (c);
1456 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1460 if (GNUNET_YES == rerun_search)
1462 struct GNUNET_TIME_Relative delay;
1464 GCP_stop_search (peer);
1465 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1466 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
1469 GCC_check_connections ();
1473 if (GNUNET_NO == is_searching (peer))
1474 GCP_start_search (peer);
1475 GCC_check_connections ();
1480 * Chech whether there is a direct (core level) connection to peer.
1482 * @param peer Peer to check.
1484 * @return #GNUNET_YES if there is a direct connection.
1487 GCP_is_neighbor (const struct CadetPeer *peer)
1489 struct CadetPeerPath *path;
1491 if (NULL == peer->connections)
1494 for (path = peer->path_head; NULL != path; path = path->next)
1496 if (3 > path->length)
1500 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1506 * Create and initialize a new tunnel towards a peer, in case it has none.
1507 * In case the peer already has a tunnel, nothing is done.
1509 * Does not generate any traffic, just creates the local data structures.
1511 * @param peer Peer towards which to create the tunnel.
1514 GCP_add_tunnel (struct CadetPeer *peer)
1516 GCC_check_connections ();
1517 if (NULL != peer->tunnel)
1519 peer->tunnel = GCT_new (peer);
1520 GCC_check_connections ();
1525 * Add a connection to a neighboring peer.
1527 * Store that the peer is the first hop of the connection in one
1528 * direction and that on peer disconnect the connection must be
1529 * notified and destroyed, for it will no longer be valid.
1531 * @param peer Peer to add connection to.
1532 * @param c Connection to add.
1533 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
1536 GCP_add_connection (struct CadetPeer *peer,
1537 struct CadetConnection *c,
1540 LOG (GNUNET_ERROR_TYPE_DEBUG,
1541 "adding connection %s\n",
1543 LOG (GNUNET_ERROR_TYPE_DEBUG,
1546 GNUNET_assert (NULL != peer->connections);
1547 GNUNET_assert (GNUNET_OK ==
1548 GNUNET_CONTAINER_multihashmap_put (peer->connections,
1551 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1552 LOG (GNUNET_ERROR_TYPE_DEBUG,
1553 "Peer %s has now %u connections.\n",
1555 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1560 * Add the path to the peer and update the path used to reach it in case this
1563 * @param peer Destination peer to add the path to.
1564 * @param path New path to add. Last peer must be @c peer.
1565 * Path will be either used of freed if already known.
1566 * @param trusted Do we trust that this path is real?
1568 * @return path if path was taken, pointer to existing duplicate if exists
1571 struct CadetPeerPath *
1572 GCP_add_path (struct CadetPeer *peer,
1573 struct CadetPeerPath *path,
1576 struct CadetPeerPath *aux;
1580 GCC_check_connections ();
1581 LOG (GNUNET_ERROR_TYPE_DEBUG,
1582 "adding path [%u] to peer %s\n",
1583 path->length, GCP_2s (peer));
1585 if (NULL == peer || NULL == path
1586 || path->peers[path->length - 1] != peer->id)
1589 path_destroy (path);
1593 for (l = 1; l < path->length; l++)
1595 if (path->peers[l] == myid)
1597 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1598 for (l2 = 0; l2 < path->length - l; l2++)
1600 path->peers[l2] = path->peers[l + l2];
1604 path->peers = GNUNET_realloc (path->peers,
1605 path->length * sizeof (GNUNET_PEER_Id));
1609 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1611 if (2 >= path->length && GNUNET_NO == trusted)
1613 /* Only allow CORE to tell us about direct paths */
1614 path_destroy (path);
1618 l = path_get_length (path);
1621 path_destroy (path);
1625 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1626 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1628 l2 = path_get_length (aux);
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1632 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1633 peer->path_tail, aux, path);
1638 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1640 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1641 path_destroy (path);
1646 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head,
1649 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1652 if (NULL != peer->tunnel
1653 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
1654 && 2 < path->length) /* Direct paths are handled by core_connect */
1658 GCC_check_connections ();
1664 * Add the path to the origin peer and update the path used to reach it in case
1665 * this is the shortest.
1666 * The path is given in peer_info -> destination, therefore we turn the path
1667 * upside down first.
1669 * @param peer Peer to add the path to, being the origin of the path.
1670 * @param path New path to add after being inversed.
1671 * Path will be either used or freed.
1672 * @param trusted Do we trust that this path is real?
1674 * @return path if path was taken, pointer to existing duplicate if exists
1677 struct CadetPeerPath *
1678 GCP_add_path_to_origin (struct CadetPeer *peer,
1679 struct CadetPeerPath *path,
1685 return GCP_add_path (peer, path, trusted);
1690 * Adds a path to the info of all the peers in the path
1692 * @param p Path to process.
1693 * @param confirmed Whether we know if the path works or not.
1696 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1700 /* TODO: invert and add to origin */
1701 /* TODO: replace all "GCP_add_path" with this, make the other one static */
1702 GCC_check_connections ();
1703 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1704 for (i++; i < p->length; i++)
1706 struct CadetPeer *peer;
1707 struct CadetPeerPath *copy;
1709 peer = GCP_get_short (p->peers[i], GNUNET_YES);
1710 copy = path_duplicate (p);
1711 copy->length = i + 1;
1712 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
1714 GCC_check_connections ();
1719 * Remove any path to the peer that has the exact same peers as the one given.
1721 * @param peer Peer to remove the path from.
1722 * @param path Path to remove. Is always destroyed .
1725 GCP_remove_path (struct CadetPeer *peer,
1726 struct CadetPeerPath *path)
1728 struct CadetPeerPath *iter;
1729 struct CadetPeerPath *next;
1731 GCC_check_connections ();
1732 GNUNET_assert (myid == path->peers[0]);
1733 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1735 LOG (GNUNET_ERROR_TYPE_INFO,
1736 "Removing path %p (%u) from %s\n",
1737 path, path->length, GCP_2s (peer));
1739 for (iter = peer->path_head; NULL != iter; iter = next)
1742 if (0 == path_cmp (path, iter))
1744 GNUNET_CONTAINER_DLL_remove (peer->path_head,
1748 path_destroy (iter);
1751 path_destroy (path);
1752 GCC_check_connections ();
1757 * Check that we are aware of a connection from a neighboring peer.
1759 * @param peer Peer to the connection is with
1760 * @param c Connection that should be in the map with this peer.
1763 GCP_check_connection (const struct CadetPeer *peer,
1764 const struct CadetConnection *c)
1766 GNUNET_assert (NULL != peer);
1767 GNUNET_assert (NULL != peer->connections);
1769 GNUNET_assert (GNUNET_YES ==
1770 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
1777 * Remove a connection from a neighboring peer.
1779 * @param peer Peer to remove connection from.
1780 * @param c Connection to remove.
1783 GCP_remove_connection (struct CadetPeer *peer,
1784 const struct CadetConnection *c)
1786 LOG (GNUNET_ERROR_TYPE_DEBUG,
1787 "Removing connection %s\n",
1789 LOG (GNUNET_ERROR_TYPE_DEBUG,
1792 if ( (NULL == peer) ||
1793 (NULL == peer->connections) )
1795 GNUNET_assert (GNUNET_YES ==
1796 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1799 LOG (GNUNET_ERROR_TYPE_DEBUG,
1800 "Peer %s remains with %u connections.\n",
1802 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1807 * Start the DHT search for new paths towards the peer: we don't have
1808 * enough good connections.
1810 * @param peer Destination peer.
1813 GCP_start_search (struct CadetPeer *peer)
1815 const struct GNUNET_PeerIdentity *id;
1816 struct CadetTunnel *t = peer->tunnel;
1818 GCC_check_connections ();
1819 if (NULL != peer->search_h)
1825 if (NULL != peer->search_delayed)
1826 GCP_stop_search (peer);
1828 id = GNUNET_PEER_resolve2 (peer->id);
1829 peer->search_h = GCD_search (id, &search_handler, peer);
1833 /* Why would we search for a peer with no tunnel towards it? */
1838 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
1839 || 0 == GCT_count_any_connections (t))
1841 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
1843 GCC_check_connections ();
1848 * Stop the DHT search for new paths towards the peer: we already have
1849 * enough good connections.
1851 * @param peer Destination peer.
1854 GCP_stop_search (struct CadetPeer *peer)
1856 GCC_check_connections ();
1857 if (NULL != peer->search_h)
1859 GCD_search_stop (peer->search_h);
1860 peer->search_h = NULL;
1862 if (NULL != peer->search_delayed)
1864 GNUNET_SCHEDULER_cancel (peer->search_delayed);
1865 peer->search_delayed = NULL;
1867 GCC_check_connections ();
1872 * Get the Full ID of a peer.
1874 * @param peer Peer to get from.
1876 * @return Full ID of peer.
1878 const struct GNUNET_PeerIdentity *
1879 GCP_get_id (const struct CadetPeer *peer)
1881 return GNUNET_PEER_resolve2 (peer->id);
1886 * Get the Short ID of a peer.
1888 * @param peer Peer to get from.
1890 * @return Short ID of peer.
1893 GCP_get_short_id (const struct CadetPeer *peer)
1902 * If tunnel is NULL and there was a search active, stop it, as it's useless.
1908 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
1911 if (NULL == t && GNUNET_YES == is_searching (peer))
1913 GCP_stop_search (peer);
1919 * Get the tunnel towards a peer.
1921 * @param peer Peer to get from.
1923 * @return Tunnel towards peer.
1925 struct CadetTunnel *
1926 GCP_get_tunnel (const struct CadetPeer *peer)
1930 return peer->tunnel;
1935 * Set the hello message.
1937 * @param peer Peer whose message to set.
1938 * @param hello Hello message.
1941 GCP_set_hello (struct CadetPeer *peer,
1942 const struct GNUNET_HELLO_Message *hello)
1944 struct GNUNET_HELLO_Message *old;
1947 GCC_check_connections ();
1948 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
1952 old = GCP_get_hello (peer);
1955 size = GNUNET_HELLO_size (hello);
1956 peer->hello = GNUNET_malloc (size);
1957 GNUNET_memcpy (peer->hello, hello, size);
1961 peer->hello = GNUNET_HELLO_merge (old, hello);
1964 GCC_check_connections ();
1969 * Get the hello message.
1971 * @param peer Peer whose message to get.
1973 * @return Hello message.
1975 struct GNUNET_HELLO_Message *
1976 GCP_get_hello (struct CadetPeer *peer)
1978 struct GNUNET_TIME_Absolute expiration;
1979 struct GNUNET_TIME_Relative remaining;
1981 if (NULL == peer->hello)
1984 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
1985 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
1986 if (0 == remaining.rel_value_us)
1988 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
1989 GNUNET_STRINGS_absolute_time_to_string (expiration));
1990 GNUNET_free (peer->hello);
1998 * Try to connect to a peer on TRANSPORT level.
2000 * @param peer Peer to whom to connect.
2003 GCP_try_connect (struct CadetPeer *peer)
2005 struct GNUNET_HELLO_Message *hello;
2006 struct GNUNET_MessageHeader *mh;
2009 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2011 "DISABLE_TRY_CONNECT"))
2013 GCC_check_connections ();
2014 if (GNUNET_YES == GCP_is_neighbor (peer))
2016 hello = GCP_get_hello (peer);
2020 mh = GNUNET_HELLO_get_header (hello);
2021 if (NULL != peer->hello_offer)
2023 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
2024 peer->hello_offer = NULL;
2026 peer->hello_offer = GNUNET_TRANSPORT_offer_hello (cfg,
2030 if (NULL == peer->connectivity_suggestion)
2031 peer->connectivity_suggestion
2032 = GNUNET_ATS_connectivity_suggest (ats_ch,
2035 GCC_check_connections ();
2040 * Notify a peer that a link between two other peers is broken. If any path
2041 * used that link, eliminate it.
2043 * @param peer Peer affected by the change.
2044 * @param peer1 Peer whose link is broken.
2045 * @param peer2 Peer whose link is broken.
2048 GCP_notify_broken_link (struct CadetPeer *peer,
2049 const struct GNUNET_PeerIdentity *peer1,
2050 const struct GNUNET_PeerIdentity *peer2)
2052 struct CadetPeerPath *iter;
2053 struct CadetPeerPath *next;
2058 GCC_check_connections ();
2059 p1 = GNUNET_PEER_search (peer1);
2060 p2 = GNUNET_PEER_search (peer2);
2062 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2063 if (0 == p1 || 0 == p2)
2065 /* We don't even know them */
2069 for (iter = peer->path_head; NULL != iter; iter = next)
2072 for (i = 0; i < iter->length - 1; i++)
2074 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2075 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2080 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2083 path_invalidate (iter);
2087 GCC_check_connections ();
2092 * Count the number of known paths toward the peer.
2094 * @param peer Peer to get path info.
2096 * @return Number of known paths.
2099 GCP_count_paths (const struct CadetPeer *peer)
2101 struct CadetPeerPath *iter;
2104 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2112 * Iterate over the paths to a peer.
2114 * @param peer Peer to get path info.
2115 * @param callback Function to call for every path.
2116 * @param cls Closure for @a callback.
2118 * @return Number of iterated paths.
2121 GCP_iterate_paths (struct CadetPeer *peer,
2122 GCP_path_iterator callback,
2125 struct CadetPeerPath *iter;
2128 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2131 if (GNUNET_YES != callback (cls, peer, iter))
2140 * Iterate all known peers.
2142 * @param iter Iterator.
2143 * @param cls Closure for @c iter.
2146 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2149 GCC_check_connections ();
2150 GNUNET_CONTAINER_multipeermap_iterate (peers,
2153 GCC_check_connections ();
2158 * Get the static string for a peer ID.
2162 * @return Static string for it's ID.
2165 GCP_2s (const struct CadetPeer *peer)
2169 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2173 /* end of gnunet-service-cadet_peer.c */