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_MultiHashMap *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_HashCode *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_multihashmap_create (16, GNUNET_NO);
377 GNUNET_STATISTICS_update (stats,
382 if ( (NULL != GCP_get_tunnel (neighbor)) &&
383 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
385 GCP_connect (neighbor);
387 GCC_check_connections ();
394 * Method called whenever a peer disconnects.
396 * @param cls Core closure (unused).
397 * @param peer Peer identity this notification is about.
398 * @param internal_cls Internal closure (CadetPeer struct).
401 core_disconnect_handler (void *cls,
402 const struct GNUNET_PeerIdentity *peer,
405 struct CadetPeer *p = internal_cls;
406 struct CadetPeerPath *direct_path;
409 GCC_check_connections ();
410 strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
414 LOG (GNUNET_ERROR_TYPE_INFO,
415 "DISCONNECTED %s (self)\n",
420 LOG (GNUNET_ERROR_TYPE_INFO,
421 "DISCONNECTED %s <= %s\n",
422 own_id, GNUNET_i2s (peer));
425 direct_path = pop_direct_path (p);
426 if (NULL != p->connections)
428 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
431 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
432 p->connections = NULL;
434 GNUNET_STATISTICS_update (stats,
438 path_destroy (direct_path);
439 GCC_check_connections ();
443 /******************************************************************************/
444 /******************************************************************************/
445 /******************************************************************************/
446 /******************************************************************************/
447 /******************************************************************************/
450 * Check if the create_connection message has the appropriate size.
452 * @param cls Closure (unused).
453 * @param msg Message to check.
455 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
458 check_create (void *cls, const struct GNUNET_CADET_ConnectionCreate *msg)
462 size = ntohs (msg->header.size);
463 if (size < sizeof (*msg))
472 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
474 * @param cls Closure (CadetPeer for neighbor that sent the message).
475 * @param msg Message itself.
478 handle_create (void *cls, const struct GNUNET_CADET_ConnectionCreate *msg)
480 struct CadetPeer *peer = cls;
481 GCC_handle_create (peer, msg);
486 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK
488 * @param cls Closure (CadetPeer for neighbor that sent the message).
489 * @param msg Message itself.
492 handle_confirm (void *cls, const struct GNUNET_CADET_ConnectionACK *msg)
494 struct CadetPeer *peer = cls;
495 GCC_handle_confirm (peer, msg);
500 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
502 * @param cls Closure (CadetPeer for neighbor that sent the message).
503 * @param msg Message itself.
506 handle_broken (void *cls, const struct GNUNET_CADET_ConnectionBroken *msg)
508 struct CadetPeer *peer = cls;
509 GCC_handle_broken (peer, msg);
514 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
516 * @param cls Closure (CadetPeer for neighbor that sent the message).
517 * @param msg Message itself.
520 handle_destroy (void *cls, const struct GNUNET_CADET_ConnectionDestroy *msg)
522 struct CadetPeer *peer = cls;
523 GCC_handle_destroy (peer, msg);
528 * Handle for #GNUNET_MESSAGE_TYPE_CADET_ACK
530 * @param cls Closure (CadetPeer for neighbor that sent the message).
531 * @param msg Message itself.
534 handle_ack (void *cls, const struct GNUNET_CADET_ACK *msg)
536 struct CadetPeer *peer = cls;
537 GCC_handle_ack (peer, msg);
542 * Handle for #GNUNET_MESSAGE_TYPE_CADET_POLL
544 * @param cls Closure (CadetPeer for neighbor that sent the message).
545 * @param msg Message itself.
548 handle_poll (void *cls, const struct GNUNET_CADET_Poll *msg)
550 struct CadetPeer *peer = cls;
551 GCC_handle_poll (peer, msg);
556 * Handle for #GNUNET_MESSAGE_TYPE_CADET_KX
558 * @param cls Closure (CadetPeer for neighbor that sent the message).
559 * @param msg Message itself.
562 handle_kx (void *cls, const struct GNUNET_CADET_KX *msg)
564 struct CadetPeer *peer = cls;
565 GCC_handle_kx (peer, msg);
570 * Check if the encrypted message has the appropriate size.
572 * @param cls Closure (unused).
573 * @param msg Message to check.
575 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
578 check_encrypted (void *cls, const struct GNUNET_CADET_Encrypted *msg)
581 uint16_t minimum_size;
583 size = ntohs (msg->header.size);
584 minimum_size = sizeof (struct GNUNET_CADET_Encrypted)
585 + sizeof (struct GNUNET_MessageHeader);
587 if (size < minimum_size)
596 * Handle for #GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED.
598 * @param cls Closure (CadetPeer for neighbor that sent the message).
599 * @param msg Message itself.
602 handle_encrypted (void *cls, const struct GNUNET_CADET_Encrypted *msg)
604 struct CadetPeer *peer = cls;
605 GCC_handle_encrypted (peer, msg);
610 * To be called on core init/fail.
612 * @param cls Closure (config)
613 * @param identity The public identity of this peer.
616 core_init_notify (void *cls,
617 const struct GNUNET_PeerIdentity *identity);
621 connect_to_core (const struct GNUNET_CONFIGURATION_Handle *c)
623 struct GNUNET_MQ_MessageHandler core_handlers[] = {
624 GNUNET_MQ_hd_var_size (create,
625 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
626 struct GNUNET_CADET_ConnectionCreate,
628 GNUNET_MQ_hd_fixed_size (confirm,
629 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
630 struct GNUNET_CADET_ConnectionACK,
632 GNUNET_MQ_hd_fixed_size (broken,
633 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
634 struct GNUNET_CADET_ConnectionBroken,
636 GNUNET_MQ_hd_fixed_size (destroy,
637 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
638 struct GNUNET_CADET_ConnectionDestroy,
640 GNUNET_MQ_hd_fixed_size (ack,
641 GNUNET_MESSAGE_TYPE_CADET_ACK,
642 struct GNUNET_CADET_ACK,
644 GNUNET_MQ_hd_fixed_size (poll,
645 GNUNET_MESSAGE_TYPE_CADET_POLL,
646 struct GNUNET_CADET_Poll,
648 GNUNET_MQ_hd_fixed_size (kx,
649 GNUNET_MESSAGE_TYPE_CADET_KX,
650 struct GNUNET_CADET_KX,
652 GNUNET_MQ_hd_var_size (encrypted,
653 GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED,
654 struct GNUNET_CADET_Encrypted,
656 GNUNET_MQ_handler_end ()
658 core_handle = GNUNET_CORE_connect (c, NULL,
660 &core_connect_handler,
661 &core_disconnect_handler,
665 /******************************************************************************/
666 /******************************************************************************/
667 /******************************************************************************/
668 /******************************************************************************/
669 /******************************************************************************/
672 * To be called on core init/fail.
674 * @param cls Closure (config)
675 * @param identity The public identity of this peer.
678 core_init_notify (void *cls,
679 const struct GNUNET_PeerIdentity *core_identity)
681 const struct GNUNET_CONFIGURATION_Handle *c = cls;
683 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
684 if (0 != memcmp (core_identity, &my_full_id, sizeof (my_full_id)))
686 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
687 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (core_identity));
688 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
689 GNUNET_CORE_disconnect (core_handle);
697 /******************************************************************************/
698 /******************************** STATIC ***********************************/
699 /******************************************************************************/
703 * Get priority for a queued message.
705 * @param q Queued message
707 * @return CORE priority to use.
710 * FIXME use when sending
712 enum GNUNET_CORE_Priority
713 get_priority (struct CadetPeerQueue *q)
715 enum GNUNET_CORE_Priority low;
716 enum GNUNET_CORE_Priority high;
721 return GNUNET_CORE_PRIO_BACKGROUND;
724 /* Relayed traffic has lower priority, our own traffic has higher */
725 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->c_fwd))
727 low = GNUNET_CORE_PRIO_BEST_EFFORT;
728 high = GNUNET_CORE_PRIO_URGENT;
732 low = GNUNET_CORE_PRIO_URGENT;
733 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
736 /* Bulky payload has lower priority, control traffic has higher. */
737 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
744 * Cancel all messages queued to CORE MQ towards this peer.
746 * @param peer Peer towards which to cancel all messages.
749 cancel_queued_messages (struct CadetPeer *peer)
751 while (NULL != peer->q_head)
753 struct CadetPeerQueue *q;
756 call_peer_cont (q, GNUNET_NO);
763 * Destroy the peer_info and free any allocated resources linked to it
765 * @param peer The peer_info to destroy.
766 * @return #GNUNET_OK on success
769 peer_destroy (struct CadetPeer *peer)
771 struct GNUNET_PeerIdentity id;
772 struct CadetPeerPath *p;
773 struct CadetPeerPath *nextp;
775 GNUNET_PEER_resolve (peer->id, &id);
776 GNUNET_PEER_change_rc (peer->id, -1);
778 LOG (GNUNET_ERROR_TYPE_INFO,
779 "destroying peer %s\n",
783 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
786 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
788 GCP_stop_search (peer);
793 GNUNET_CONTAINER_DLL_remove (peer->path_head,
799 if (NULL != peer->tunnel)
800 GCT_destroy_empty (peer->tunnel);
801 if (NULL != peer->connections)
803 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
804 GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
805 peer->connections = NULL;
807 if (NULL != peer->hello_offer)
809 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
810 peer->hello_offer = NULL;
812 if (NULL != peer->connectivity_suggestion)
814 GNUNET_ATS_connectivity_suggest_cancel (peer->connectivity_suggestion);
815 peer->connectivity_suggestion = NULL;
817 cancel_queued_messages (peer);
819 GNUNET_free_non_null (peer->hello);
826 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
829 * @param key current key code
830 * @param value value in the hash map
831 * @return #GNUNET_YES if we should continue to iterate,
835 shutdown_peer (void *cls,
836 const struct GNUNET_PeerIdentity *key,
839 struct CadetPeer *p = value;
840 struct CadetTunnel *t = p->tunnel;
842 LOG (GNUNET_ERROR_TYPE_DEBUG, " shutting down %s\n", GCP_2s (p));
852 * Check if peer is searching for a path (either active or delayed search).
854 * @param peer Peer to check
855 * @return #GNUNET_YES if there is a search active.
856 * #GNUNET_NO otherwise.
859 is_searching (const struct CadetPeer *peer)
861 return ( (NULL == peer->search_h) &&
862 (NULL == peer->search_delayed) ) ?
863 GNUNET_NO : GNUNET_YES;
868 * @brief Start a search for a peer.
870 * @param cls Closure (Peer to search for).
873 delayed_search (void *cls)
875 struct CadetPeer *peer = cls;
877 peer->search_delayed = NULL;
878 GCC_check_connections ();
879 GCP_start_search (peer);
880 GCC_check_connections ();
885 * Returns if peer is used (has a tunnel or is neighbor).
887 * @param peer Peer to check.
888 * @return #GNUNET_YES if peer is in use.
891 peer_is_used (struct CadetPeer *peer)
893 struct CadetPeerPath *p;
895 if (NULL != peer->tunnel)
898 for (p = peer->path_head; NULL != p; p = p->next)
908 * Iterator over all the peers to get the oldest timestamp.
910 * @param cls Closure (unsued).
911 * @param key ID of the peer.
912 * @param value Peer_Info of the peer.
915 peer_get_oldest (void *cls,
916 const struct GNUNET_PeerIdentity *key,
919 struct CadetPeer *p = value;
920 struct GNUNET_TIME_Absolute *abs = cls;
922 /* Don't count active peers */
923 if (GNUNET_YES == peer_is_used (p))
926 if (abs->abs_value_us < p->last_contact.abs_value_us)
927 abs->abs_value_us = p->last_contact.abs_value_us;
934 * Iterator over all the peers to remove the oldest entry.
936 * @param cls Closure (unsued).
937 * @param key ID of the peer.
938 * @param value Peer_Info of the peer.
941 peer_timeout (void *cls,
942 const struct GNUNET_PeerIdentity *key,
945 struct CadetPeer *p = value;
946 struct GNUNET_TIME_Absolute *abs = cls;
948 LOG (GNUNET_ERROR_TYPE_WARNING,
949 "peer %s timeout\n", GNUNET_i2s (key));
951 if (p->last_contact.abs_value_us == abs->abs_value_us &&
952 GNUNET_NO == peer_is_used (p))
962 * Delete oldest unused peer.
965 peer_delete_oldest (void)
967 struct GNUNET_TIME_Absolute abs;
969 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
971 GNUNET_CONTAINER_multipeermap_iterate (peers,
974 GNUNET_CONTAINER_multipeermap_iterate (peers,
981 * Choose the best (yet unused) path towards a peer,
982 * considering the tunnel properties.
984 * @param peer The destination peer.
985 * @return Best current known path towards the peer, if any.
987 static struct CadetPeerPath *
988 peer_get_best_path (const struct CadetPeer *peer)
990 struct CadetPeerPath *best_p;
991 struct CadetPeerPath *p;
992 unsigned int best_cost;
995 best_cost = UINT_MAX;
997 for (p = peer->path_head; NULL != p; p = p->next)
999 if (GNUNET_NO == path_is_valid (p))
1000 continue; /* Don't use invalid paths. */
1001 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
1002 continue; /* If path is already in use, skip it. */
1004 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
1015 * Function to process paths received for a new peer addition. The recorded
1016 * paths form the initial tunnel, which can be optimized later.
1017 * Called on each result obtained for the DHT search.
1019 * @param cls Closure (peer towards a path has been found).
1020 * @param path Path created from the DHT query. Will be freed afterwards.
1023 search_handler (void *cls, const struct CadetPeerPath *path)
1025 struct CadetPeer *peer = cls;
1026 unsigned int connection_count;
1028 GCC_check_connections ();
1029 GCP_add_path_to_all (path, GNUNET_NO);
1031 /* Count connections */
1032 connection_count = GCT_count_connections (peer->tunnel);
1034 /* If we already have our minimum (or more) connections, it's enough */
1035 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1037 GCC_check_connections ();
1041 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1043 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1046 GCC_check_connections ();
1051 * Test if a message type is connection management traffic
1052 * or regular payload traffic.
1054 * @param type Message type.
1056 * @return #GNUNET_YES if connection management, #GNUNET_NO otherwise.
1059 is_connection_management (uint16_t type)
1061 return type == GNUNET_MESSAGE_TYPE_CADET_ACK ||
1062 type == GNUNET_MESSAGE_TYPE_CADET_POLL;
1067 * Debug function should NEVER return true in production code, useful to
1068 * simulate losses for testcases.
1070 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
1073 should_I_drop (void)
1075 if (0 == drop_percent)
1078 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1085 /******************************************************************************/
1086 /******************************** API ***********************************/
1087 /******************************************************************************/
1090 * Call the continuation after a message has been sent or dropped.
1092 * This funcion removes the message from the queue.
1094 * @param q Queue handle.
1095 * @param sent #GNUNET_YES if was sent to CORE, #GNUNET_NO if dropped.
1098 call_peer_cont (struct CadetPeerQueue *q, int sent)
1100 LOG (GNUNET_ERROR_TYPE_DEBUG, " core mq just sent %s\n", GC_m2s (q->type));
1101 if (NULL != q->cont)
1103 struct GNUNET_TIME_Relative wait_time;
1105 wait_time = GNUNET_TIME_absolute_get_duration (q->queue_timestamp);
1106 LOG (GNUNET_ERROR_TYPE_DEBUG,
1107 " calling callback on %s after %s\n",
1109 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1110 q->cont (q->cont_cls,
1111 q->c, q->c_fwd, sent,
1112 q->type, q->payload_type, q->payload_id,
1113 q->size, wait_time);
1116 GNUNET_CONTAINER_DLL_remove (q->peer->q_head, q->peer->q_tail, q);
1121 * Function called by MQ when a message is sent to CORE.
1123 * @param cls Closure (queue handle).
1128 struct CadetPeerQueue *q = cls;
1130 if (GNUNET_NO == q->management_traffic)
1134 call_peer_cont (q, GNUNET_YES);
1140 * Finish the drop operation.
1142 * @param cls queue entry to finish drop for
1147 struct CadetPeerQueue *q = cls;
1149 GNUNET_MQ_discard (q->env);
1150 call_peer_cont (q, GNUNET_YES);
1156 * @brief Send a message to another peer (using CORE).
1158 * @param peer Peer towards which to queue the message.
1159 * @param message Message to send.
1160 * @param payload_type Type of the message's payload, for debug messages.
1161 * 0 if the message is a retransmission (unknown payload).
1162 * UINT16_MAX if the message does not have payload.
1163 * @param payload_id ID of the payload (MID, ACK #, etc)
1164 * @param c Connection this message belongs to (can be NULL).
1165 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1166 * @param cont Continuation to be called once CORE has sent the message.
1167 * @param cont_cls Closure for @c cont.
1169 * @return A handle to the message in the queue or NULL (if dropped).
1171 struct CadetPeerQueue *
1172 GCP_send (struct CadetPeer *peer,
1173 const struct GNUNET_MessageHeader *message,
1174 uint16_t payload_type,
1175 uint32_t payload_id,
1176 struct CadetConnection *c,
1181 struct CadetPeerQueue *q;
1185 GCC_check_connections ();
1186 type = ntohs (message->type);
1187 size = ntohs (message->size);
1188 LOG (GNUNET_ERROR_TYPE_DEBUG,
1189 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1190 GC_m2s (type), GC_m2s (payload_type), payload_id,
1191 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1193 if (NULL == peer->connections)
1195 /* We are not connected to this peer, ignore request. */
1197 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1198 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1203 q = GNUNET_new (struct CadetPeerQueue);
1204 q->env = GNUNET_MQ_msg_copy (message);
1207 q->cont_cls = cont_cls;
1208 q->queue_timestamp = GNUNET_TIME_absolute_get ();
1209 q->management_traffic = is_connection_management (type);
1212 q->payload_type = payload_type;
1213 q->payload_id = payload_id;
1216 GNUNET_MQ_notify_sent (q->env, &mq_sent, q);
1217 GNUNET_CONTAINER_DLL_insert (peer->q_head, peer->q_tail, q);
1219 if (GNUNET_YES == q->management_traffic)
1221 GNUNET_MQ_send (peer->core_mq, q->env); // FIXME implement "_urgent", use
1225 if (GNUNET_YES == should_I_drop ())
1227 LOG (GNUNET_ERROR_TYPE_WARNING,
1228 "DD %s (%s %u) on conn %s %s (random drop for testing)\n",
1229 GC_m2s (q->type), GC_m2s (q->payload_type),
1230 q->payload_id, GCC_2s (c), GC_f2s (q->c_fwd));
1231 q->drop_task = GNUNET_SCHEDULER_add_now (&drop_cb,
1235 GNUNET_MQ_send (peer->core_mq, q->env);
1239 GCC_check_connections ();
1245 * Cancel sending a message. Message must have been sent with
1246 * #GCP_send before. May not be called after the notify sent
1247 * callback has been called.
1249 * It DOES call the continuation given to #GCP_send.
1251 * @param q Queue handle to cancel
1254 GCP_send_cancel (struct CadetPeerQueue *q)
1256 if (NULL != q->drop_task)
1258 GNUNET_SCHEDULER_cancel (q->drop_task);
1259 q->drop_task = NULL;
1260 GNUNET_MQ_discard (q->env);
1264 GNUNET_MQ_send_cancel (q->env);
1266 call_peer_cont (q, GNUNET_NO);
1272 * Initialize the peer subsystem.
1274 * @param c Configuration.
1277 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1280 LOG (GNUNET_ERROR_TYPE_DEBUG,
1282 in_shutdown = GNUNET_NO;
1283 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1285 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1288 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1289 "CADET", "MAX_PEERS", "USING DEFAULT");
1294 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1301 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1302 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1303 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1304 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1305 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1307 ats_ch = GNUNET_ATS_connectivity_init (c);
1308 connect_to_core (c);
1309 if (NULL == core_handle)
1312 GNUNET_SCHEDULER_shutdown ();
1318 * Shut down the peer subsystem.
1323 LOG (GNUNET_ERROR_TYPE_DEBUG,
1324 "Shutting down peer subsystem\n");
1325 in_shutdown = GNUNET_YES;
1326 if (NULL != core_handle)
1328 GNUNET_CORE_disconnect (core_handle);
1331 GNUNET_PEER_change_rc (myid, -1);
1332 /* With MQ API, CORE calls the disconnect handler for every peer
1333 * after calling GNUNET_CORE_disconnect, shutdown must occur *after* that.
1335 GNUNET_CONTAINER_multipeermap_iterate (peers,
1340 GNUNET_ATS_connectivity_done (ats_ch);
1343 GNUNET_CONTAINER_multipeermap_destroy (peers);
1349 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1350 * one and insert it in the appropriate structures if the peer is not known yet.
1352 * @param peer_id Full identity of the peer.
1353 * @param create #GNUNET_YES if a new peer should be created if unknown.
1354 * #GNUNET_NO otherwise.
1356 * @return Existing or newly created peer structure.
1357 * NULL if unknown and not requested @a create
1360 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1362 struct CadetPeer *peer;
1364 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1367 peer = GNUNET_new (struct CadetPeer);
1368 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1370 peer_delete_oldest ();
1372 GNUNET_assert (GNUNET_OK ==
1373 GNUNET_CONTAINER_multipeermap_put (peers,
1376 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1377 peer->id = GNUNET_PEER_intern (peer_id);
1379 peer->last_contact = GNUNET_TIME_absolute_get ();
1386 * Retrieve the CadetPeer stucture associated with the
1387 * peer. Optionally create one and insert it in the appropriate
1388 * structures if the peer is not known yet.
1390 * @param peer Short identity of the peer.
1391 * @param create #GNUNET_YES if a new peer should be created if unknown.
1392 * #GNUNET_NO otherwise.
1394 * @return Existing or newly created peer structure.
1395 * NULL if unknown and not requested @a create
1398 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1400 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1405 * Function called once #GNUNET_TRANSPORT_offer_hello() is done.
1406 * Marks the operation as finished.
1408 * @param cls Closure (our `struct CadetPeer`).
1411 hello_offer_done (void *cls)
1413 struct CadetPeer *peer = cls;
1415 peer->hello_offer = NULL;
1420 * Try to establish a new connection to this peer (in its tunnel).
1421 * If the peer doesn't have any path to it yet, try to get one.
1422 * If the peer already has some path, send a CREATE CONNECTION towards it.
1424 * @param peer Peer to connect to.
1427 GCP_connect (struct CadetPeer *peer)
1429 struct CadetTunnel *t;
1430 struct CadetPeerPath *path;
1431 struct CadetConnection *c;
1434 GCC_check_connections ();
1435 LOG (GNUNET_ERROR_TYPE_DEBUG,
1436 "peer_connect towards %s\n",
1438 /* If we have a current hello, try to connect using it. */
1439 GCP_try_connect (peer);
1443 rerun_search = GNUNET_NO;
1445 if (NULL != peer->path_head)
1447 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1448 path = peer_get_best_path (peer);
1454 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1457 c = GCT_use_path (t, path);
1460 /* This case can happen when the path includes a first hop that is
1461 * not yet known to be connected.
1463 * This happens quite often during testing when running cadet
1464 * under valgrind: core connect notifications come very late
1465 * and the DHT result has already come and created a valid
1466 * path. In this case, the peer->connections
1467 * hashmaps will be NULL and tunnel_use_path will not be able
1468 * to create a connection from that path.
1470 * Re-running the DHT GET should give core time to callback.
1472 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1473 * updating statistics about this issue.
1475 rerun_search = GNUNET_YES;
1479 GCC_send_create (c);
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1489 if (GNUNET_YES == rerun_search)
1491 struct GNUNET_TIME_Relative delay;
1493 GCP_stop_search (peer);
1494 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1495 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
1498 GCC_check_connections ();
1502 if (GNUNET_NO == is_searching (peer))
1503 GCP_start_search (peer);
1504 GCC_check_connections ();
1509 * Chech whether there is a direct (core level) connection to peer.
1511 * @param peer Peer to check.
1513 * @return #GNUNET_YES if there is a direct connection.
1516 GCP_is_neighbor (const struct CadetPeer *peer)
1518 struct CadetPeerPath *path;
1520 if (NULL == peer->connections)
1523 for (path = peer->path_head; NULL != path; path = path->next)
1525 if (3 > path->length)
1529 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1535 * Create and initialize a new tunnel towards a peer, in case it has none.
1536 * In case the peer already has a tunnel, nothing is done.
1538 * Does not generate any traffic, just creates the local data structures.
1540 * @param peer Peer towards which to create the tunnel.
1543 GCP_add_tunnel (struct CadetPeer *peer)
1545 GCC_check_connections ();
1546 if (NULL != peer->tunnel)
1548 peer->tunnel = GCT_new (peer);
1549 GCC_check_connections ();
1554 * Add a connection to a neighboring peer.
1556 * Store that the peer is the first hop of the connection in one
1557 * direction and that on peer disconnect the connection must be
1558 * notified and destroyed, for it will no longer be valid.
1560 * @param peer Peer to add connection to.
1561 * @param c Connection to add.
1562 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
1565 GCP_add_connection (struct CadetPeer *peer,
1566 struct CadetConnection *c,
1569 LOG (GNUNET_ERROR_TYPE_DEBUG,
1570 "adding connection %s\n",
1572 LOG (GNUNET_ERROR_TYPE_DEBUG,
1575 GNUNET_assert (NULL != peer->connections);
1576 GNUNET_assert (GNUNET_OK ==
1577 GNUNET_CONTAINER_multihashmap_put (peer->connections,
1580 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1581 LOG (GNUNET_ERROR_TYPE_DEBUG,
1582 "Peer %s has now %u connections.\n",
1584 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1589 * Add the path to the peer and update the path used to reach it in case this
1592 * @param peer Destination peer to add the path to.
1593 * @param path New path to add. Last peer must be @c peer.
1594 * Path will be either used of freed if already known.
1595 * @param trusted Do we trust that this path is real?
1597 * @return path if path was taken, pointer to existing duplicate if exists
1600 struct CadetPeerPath *
1601 GCP_add_path (struct CadetPeer *peer,
1602 struct CadetPeerPath *path,
1605 struct CadetPeerPath *aux;
1609 GCC_check_connections ();
1610 LOG (GNUNET_ERROR_TYPE_DEBUG,
1611 "adding path [%u] to peer %s\n",
1612 path->length, GCP_2s (peer));
1614 if (NULL == peer || NULL == path
1615 || path->peers[path->length - 1] != peer->id)
1618 path_destroy (path);
1622 for (l = 1; l < path->length; l++)
1624 if (path->peers[l] == myid)
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1627 for (l2 = 0; l2 < path->length - l; l2++)
1629 path->peers[l2] = path->peers[l + l2];
1633 path->peers = GNUNET_realloc (path->peers,
1634 path->length * sizeof (GNUNET_PEER_Id));
1638 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1640 if (2 >= path->length && GNUNET_NO == trusted)
1642 /* Only allow CORE to tell us about direct paths */
1643 path_destroy (path);
1647 l = path_get_length (path);
1650 path_destroy (path);
1654 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1655 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1657 l2 = path_get_length (aux);
1660 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1661 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1662 peer->path_tail, aux, path);
1667 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1669 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1670 path_destroy (path);
1675 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head,
1678 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1681 if (NULL != peer->tunnel
1682 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
1683 && 2 < path->length) /* Direct paths are handled by core_connect */
1687 GCC_check_connections ();
1693 * Add the path to the origin peer and update the path used to reach it in case
1694 * this is the shortest.
1695 * The path is given in peer_info -> destination, therefore we turn the path
1696 * upside down first.
1698 * @param peer Peer to add the path to, being the origin of the path.
1699 * @param path New path to add after being inversed.
1700 * Path will be either used or freed.
1701 * @param trusted Do we trust that this path is real?
1703 * @return path if path was taken, pointer to existing duplicate if exists
1706 struct CadetPeerPath *
1707 GCP_add_path_to_origin (struct CadetPeer *peer,
1708 struct CadetPeerPath *path,
1714 return GCP_add_path (peer, path, trusted);
1719 * Adds a path to the info of all the peers in the path
1721 * @param p Path to process.
1722 * @param confirmed Whether we know if the path works or not.
1725 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1729 /* TODO: invert and add to origin */
1730 /* TODO: replace all "GCP_add_path" with this, make the other one static */
1731 GCC_check_connections ();
1732 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1733 for (i++; i < p->length; i++)
1735 struct CadetPeer *peer;
1736 struct CadetPeerPath *copy;
1738 peer = GCP_get_short (p->peers[i], GNUNET_YES);
1739 copy = path_duplicate (p);
1740 copy->length = i + 1;
1741 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
1743 GCC_check_connections ();
1748 * Remove any path to the peer that has the exact same peers as the one given.
1750 * @param peer Peer to remove the path from.
1751 * @param path Path to remove. Is always destroyed .
1754 GCP_remove_path (struct CadetPeer *peer,
1755 struct CadetPeerPath *path)
1757 struct CadetPeerPath *iter;
1758 struct CadetPeerPath *next;
1760 GCC_check_connections ();
1761 GNUNET_assert (myid == path->peers[0]);
1762 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1764 LOG (GNUNET_ERROR_TYPE_INFO,
1765 "Removing path %p (%u) from %s\n",
1766 path, path->length, GCP_2s (peer));
1768 for (iter = peer->path_head; NULL != iter; iter = next)
1771 if (0 == path_cmp (path, iter))
1773 GNUNET_CONTAINER_DLL_remove (peer->path_head,
1777 path_destroy (iter);
1780 path_destroy (path);
1781 GCC_check_connections ();
1786 * Check that we are aware of a connection from a neighboring peer.
1788 * @param peer Peer to the connection is with
1789 * @param c Connection that should be in the map with this peer.
1792 GCP_check_connection (const struct CadetPeer *peer,
1793 const struct CadetConnection *c)
1795 GNUNET_assert (NULL != peer);
1796 GNUNET_assert (NULL != peer->connections);
1798 GNUNET_assert (GNUNET_YES ==
1799 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
1806 * Remove a connection from a neighboring peer.
1808 * @param peer Peer to remove connection from.
1809 * @param c Connection to remove.
1812 GCP_remove_connection (struct CadetPeer *peer,
1813 const struct CadetConnection *c)
1815 LOG (GNUNET_ERROR_TYPE_DEBUG,
1816 "Removing connection %s\n",
1818 LOG (GNUNET_ERROR_TYPE_DEBUG,
1821 if ( (NULL == peer) ||
1822 (NULL == peer->connections) )
1824 GNUNET_assert (GNUNET_YES ==
1825 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1828 LOG (GNUNET_ERROR_TYPE_DEBUG,
1829 "Peer %s remains with %u connections.\n",
1831 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1836 * Start the DHT search for new paths towards the peer: we don't have
1837 * enough good connections.
1839 * @param peer Destination peer.
1842 GCP_start_search (struct CadetPeer *peer)
1844 const struct GNUNET_PeerIdentity *id;
1845 struct CadetTunnel *t = peer->tunnel;
1847 GCC_check_connections ();
1848 if (NULL != peer->search_h)
1854 if (NULL != peer->search_delayed)
1855 GCP_stop_search (peer);
1857 id = GNUNET_PEER_resolve2 (peer->id);
1858 peer->search_h = GCD_search (id, &search_handler, peer);
1862 /* Why would we search for a peer with no tunnel towards it? */
1867 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
1868 || 0 == GCT_count_any_connections (t))
1870 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
1872 GCC_check_connections ();
1877 * Stop the DHT search for new paths towards the peer: we already have
1878 * enough good connections.
1880 * @param peer Destination peer.
1883 GCP_stop_search (struct CadetPeer *peer)
1885 GCC_check_connections ();
1886 if (NULL != peer->search_h)
1888 GCD_search_stop (peer->search_h);
1889 peer->search_h = NULL;
1891 if (NULL != peer->search_delayed)
1893 GNUNET_SCHEDULER_cancel (peer->search_delayed);
1894 peer->search_delayed = NULL;
1896 GCC_check_connections ();
1901 * Get the Full ID of a peer.
1903 * @param peer Peer to get from.
1905 * @return Full ID of peer.
1907 const struct GNUNET_PeerIdentity *
1908 GCP_get_id (const struct CadetPeer *peer)
1910 return GNUNET_PEER_resolve2 (peer->id);
1915 * Get the Short ID of a peer.
1917 * @param peer Peer to get from.
1919 * @return Short ID of peer.
1922 GCP_get_short_id (const struct CadetPeer *peer)
1931 * If tunnel is NULL and there was a search active, stop it, as it's useless.
1937 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
1940 if (NULL == t && GNUNET_YES == is_searching (peer))
1942 GCP_stop_search (peer);
1948 * Get the tunnel towards a peer.
1950 * @param peer Peer to get from.
1952 * @return Tunnel towards peer.
1954 struct CadetTunnel *
1955 GCP_get_tunnel (const struct CadetPeer *peer)
1959 return peer->tunnel;
1964 * Set the hello message.
1966 * @param peer Peer whose message to set.
1967 * @param hello Hello message.
1970 GCP_set_hello (struct CadetPeer *peer,
1971 const struct GNUNET_HELLO_Message *hello)
1973 struct GNUNET_HELLO_Message *old;
1976 GCC_check_connections ();
1977 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
1981 old = GCP_get_hello (peer);
1984 size = GNUNET_HELLO_size (hello);
1985 peer->hello = GNUNET_malloc (size);
1986 GNUNET_memcpy (peer->hello, hello, size);
1990 peer->hello = GNUNET_HELLO_merge (old, hello);
1993 GCC_check_connections ();
1998 * Get the hello message.
2000 * @param peer Peer whose message to get.
2002 * @return Hello message.
2004 struct GNUNET_HELLO_Message *
2005 GCP_get_hello (struct CadetPeer *peer)
2007 struct GNUNET_TIME_Absolute expiration;
2008 struct GNUNET_TIME_Relative remaining;
2010 if (NULL == peer->hello)
2013 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2014 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2015 if (0 == remaining.rel_value_us)
2017 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2018 GNUNET_STRINGS_absolute_time_to_string (expiration));
2019 GNUNET_free (peer->hello);
2027 * Try to connect to a peer on TRANSPORT level.
2029 * @param peer Peer to whom to connect.
2032 GCP_try_connect (struct CadetPeer *peer)
2034 struct GNUNET_HELLO_Message *hello;
2035 struct GNUNET_MessageHeader *mh;
2038 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2040 "DISABLE_TRY_CONNECT"))
2042 GCC_check_connections ();
2043 if (GNUNET_YES == GCP_is_neighbor (peer))
2045 hello = GCP_get_hello (peer);
2049 mh = GNUNET_HELLO_get_header (hello);
2050 if (NULL != peer->hello_offer)
2052 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
2053 peer->hello_offer = NULL;
2055 peer->hello_offer = GNUNET_TRANSPORT_offer_hello (cfg,
2059 if (NULL == peer->connectivity_suggestion)
2060 peer->connectivity_suggestion
2061 = GNUNET_ATS_connectivity_suggest (ats_ch,
2064 GCC_check_connections ();
2069 * Notify a peer that a link between two other peers is broken. If any path
2070 * used that link, eliminate it.
2072 * @param peer Peer affected by the change.
2073 * @param peer1 Peer whose link is broken.
2074 * @param peer2 Peer whose link is broken.
2077 GCP_notify_broken_link (struct CadetPeer *peer,
2078 const struct GNUNET_PeerIdentity *peer1,
2079 const struct GNUNET_PeerIdentity *peer2)
2081 struct CadetPeerPath *iter;
2082 struct CadetPeerPath *next;
2087 GCC_check_connections ();
2088 p1 = GNUNET_PEER_search (peer1);
2089 p2 = GNUNET_PEER_search (peer2);
2091 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2092 if (0 == p1 || 0 == p2)
2094 /* We don't even know them */
2098 for (iter = peer->path_head; NULL != iter; iter = next)
2101 for (i = 0; i < iter->length - 1; i++)
2103 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2104 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2109 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2112 path_invalidate (iter);
2116 GCC_check_connections ();
2121 * Count the number of known paths toward the peer.
2123 * @param peer Peer to get path info.
2125 * @return Number of known paths.
2128 GCP_count_paths (const struct CadetPeer *peer)
2130 struct CadetPeerPath *iter;
2133 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2141 * Iterate over the paths to a peer.
2143 * @param peer Peer to get path info.
2144 * @param callback Function to call for every path.
2145 * @param cls Closure for @a callback.
2147 * @return Number of iterated paths.
2150 GCP_iterate_paths (struct CadetPeer *peer,
2151 GCP_path_iterator callback,
2154 struct CadetPeerPath *iter;
2157 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2160 if (GNUNET_YES != callback (cls, peer, iter))
2169 * Iterate all known peers.
2171 * @param iter Iterator.
2172 * @param cls Closure for @c iter.
2175 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2178 GCC_check_connections ();
2179 GNUNET_CONTAINER_multipeermap_iterate (peers,
2182 GCC_check_connections ();
2187 * Get the static string for a peer ID.
2191 * @return Static string for it's ID.
2194 GCP_2s (const struct CadetPeer *peer)
2198 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2202 /* end of gnunet-service-cadet_peer.c */