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, "DD %s (%s %u) on conn %s %s\n",
1228 GC_m2s (q->type), GC_m2s (q->payload_type),
1229 q->payload_id, GCC_2s (c), GC_f2s (q->c_fwd));
1230 q->drop_task = GNUNET_SCHEDULER_add_now (&drop_cb,
1234 GNUNET_MQ_send (peer->core_mq, q->env);
1238 GCC_check_connections ();
1244 * Cancel sending a message. Message must have been sent with
1245 * #GCP_send before. May not be called after the notify sent
1246 * callback has been called.
1248 * It DOES call the continuation given to #GCP_send.
1250 * @param q Queue handle to cancel
1253 GCP_send_cancel (struct CadetPeerQueue *q)
1255 if (NULL != q->drop_task)
1257 GNUNET_SCHEDULER_cancel (q->drop_task);
1258 q->drop_task = NULL;
1259 GNUNET_MQ_discard (q->env);
1263 GNUNET_MQ_send_cancel (q->env);
1265 call_peer_cont (q, GNUNET_NO);
1271 * Initialize the peer subsystem.
1273 * @param c Configuration.
1276 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1279 LOG (GNUNET_ERROR_TYPE_DEBUG,
1281 in_shutdown = GNUNET_NO;
1282 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1284 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1287 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1288 "CADET", "MAX_PEERS", "USING DEFAULT");
1293 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1300 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1301 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1302 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1303 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1304 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1306 ats_ch = GNUNET_ATS_connectivity_init (c);
1307 connect_to_core (c);
1308 if (NULL == core_handle)
1311 GNUNET_SCHEDULER_shutdown ();
1317 * Shut down the peer subsystem.
1322 LOG (GNUNET_ERROR_TYPE_DEBUG,
1323 "Shutting down peer subsystem\n");
1324 in_shutdown = GNUNET_YES;
1325 if (NULL != core_handle)
1327 GNUNET_CORE_disconnecT (core_handle);
1330 GNUNET_PEER_change_rc (myid, -1);
1331 /* With MQ API, CORE calls the disconnect handler for every peer
1332 * after calling GNUNET_CORE_disconnecT, shutdown must occur *after* that.
1334 GNUNET_CONTAINER_multipeermap_iterate (peers,
1339 GNUNET_ATS_connectivity_done (ats_ch);
1342 GNUNET_CONTAINER_multipeermap_destroy (peers);
1348 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1349 * one and insert it in the appropriate structures if the peer is not known yet.
1351 * @param peer_id Full identity of the peer.
1352 * @param create #GNUNET_YES if a new peer should be created if unknown.
1353 * #GNUNET_NO otherwise.
1355 * @return Existing or newly created peer structure.
1356 * NULL if unknown and not requested @a create
1359 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1361 struct CadetPeer *peer;
1363 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1366 peer = GNUNET_new (struct CadetPeer);
1367 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1369 peer_delete_oldest ();
1371 GNUNET_assert (GNUNET_OK ==
1372 GNUNET_CONTAINER_multipeermap_put (peers,
1375 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1376 peer->id = GNUNET_PEER_intern (peer_id);
1378 peer->last_contact = GNUNET_TIME_absolute_get ();
1385 * Retrieve the CadetPeer stucture associated with the
1386 * peer. Optionally create one and insert it in the appropriate
1387 * structures if the peer is not known yet.
1389 * @param peer Short identity of the peer.
1390 * @param create #GNUNET_YES if a new peer should be created if unknown.
1391 * #GNUNET_NO otherwise.
1393 * @return Existing or newly created peer structure.
1394 * NULL if unknown and not requested @a create
1397 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1399 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1404 * Function called once #GNUNET_TRANSPORT_offer_hello() is done.
1405 * Marks the operation as finished.
1407 * @param cls Closure (our `struct CadetPeer`).
1410 hello_offer_done (void *cls)
1412 struct CadetPeer *peer = cls;
1414 peer->hello_offer = NULL;
1419 * Try to establish a new connection to this peer (in its tunnel).
1420 * If the peer doesn't have any path to it yet, try to get one.
1421 * If the peer already has some path, send a CREATE CONNECTION towards it.
1423 * @param peer Peer to connect to.
1426 GCP_connect (struct CadetPeer *peer)
1428 struct CadetTunnel *t;
1429 struct CadetPeerPath *path;
1430 struct CadetConnection *c;
1433 GCC_check_connections ();
1434 LOG (GNUNET_ERROR_TYPE_DEBUG,
1435 "peer_connect towards %s\n",
1437 /* If we have a current hello, try to connect using it. */
1438 GCP_try_connect (peer);
1442 rerun_search = GNUNET_NO;
1444 if (NULL != peer->path_head)
1446 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1447 path = peer_get_best_path (peer);
1453 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1456 c = GCT_use_path (t, path);
1459 /* This case can happen when the path includes a first hop that is
1460 * not yet known to be connected.
1462 * This happens quite often during testing when running cadet
1463 * under valgrind: core connect notifications come very late
1464 * and the DHT result has already come and created a valid
1465 * path. In this case, the peer->connections
1466 * hashmaps will be NULL and tunnel_use_path will not be able
1467 * to create a connection from that path.
1469 * Re-running the DHT GET should give core time to callback.
1471 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1472 * updating statistics about this issue.
1474 rerun_search = GNUNET_YES;
1478 GCC_send_create (c);
1484 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1488 if (GNUNET_YES == rerun_search)
1490 struct GNUNET_TIME_Relative delay;
1492 GCP_stop_search (peer);
1493 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1494 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
1497 GCC_check_connections ();
1501 if (GNUNET_NO == is_searching (peer))
1502 GCP_start_search (peer);
1503 GCC_check_connections ();
1508 * Chech whether there is a direct (core level) connection to peer.
1510 * @param peer Peer to check.
1512 * @return #GNUNET_YES if there is a direct connection.
1515 GCP_is_neighbor (const struct CadetPeer *peer)
1517 struct CadetPeerPath *path;
1519 if (NULL == peer->connections)
1522 for (path = peer->path_head; NULL != path; path = path->next)
1524 if (3 > path->length)
1528 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1534 * Create and initialize a new tunnel towards a peer, in case it has none.
1535 * In case the peer already has a tunnel, nothing is done.
1537 * Does not generate any traffic, just creates the local data structures.
1539 * @param peer Peer towards which to create the tunnel.
1542 GCP_add_tunnel (struct CadetPeer *peer)
1544 GCC_check_connections ();
1545 if (NULL != peer->tunnel)
1547 peer->tunnel = GCT_new (peer);
1548 GCC_check_connections ();
1553 * Add a connection to a neighboring peer.
1555 * Store that the peer is the first hop of the connection in one
1556 * direction and that on peer disconnect the connection must be
1557 * notified and destroyed, for it will no longer be valid.
1559 * @param peer Peer to add connection to.
1560 * @param c Connection to add.
1561 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
1564 GCP_add_connection (struct CadetPeer *peer,
1565 struct CadetConnection *c,
1568 LOG (GNUNET_ERROR_TYPE_DEBUG,
1569 "adding connection %s\n",
1571 LOG (GNUNET_ERROR_TYPE_DEBUG,
1574 GNUNET_assert (NULL != peer->connections);
1575 GNUNET_assert (GNUNET_OK ==
1576 GNUNET_CONTAINER_multihashmap_put (peer->connections,
1579 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1580 LOG (GNUNET_ERROR_TYPE_DEBUG,
1581 "Peer %s has now %u connections.\n",
1583 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1588 * Add the path to the peer and update the path used to reach it in case this
1591 * @param peer Destination peer to add the path to.
1592 * @param path New path to add. Last peer must be @c peer.
1593 * Path will be either used of freed if already known.
1594 * @param trusted Do we trust that this path is real?
1596 * @return path if path was taken, pointer to existing duplicate if exists
1599 struct CadetPeerPath *
1600 GCP_add_path (struct CadetPeer *peer,
1601 struct CadetPeerPath *path,
1604 struct CadetPeerPath *aux;
1608 GCC_check_connections ();
1609 LOG (GNUNET_ERROR_TYPE_DEBUG,
1610 "adding path [%u] to peer %s\n",
1611 path->length, GCP_2s (peer));
1613 if (NULL == peer || NULL == path
1614 || path->peers[path->length - 1] != peer->id)
1617 path_destroy (path);
1621 for (l = 1; l < path->length; l++)
1623 if (path->peers[l] == myid)
1625 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1626 for (l2 = 0; l2 < path->length - l; l2++)
1628 path->peers[l2] = path->peers[l + l2];
1632 path->peers = GNUNET_realloc (path->peers,
1633 path->length * sizeof (GNUNET_PEER_Id));
1637 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1639 if (2 >= path->length && GNUNET_NO == trusted)
1641 /* Only allow CORE to tell us about direct paths */
1642 path_destroy (path);
1646 l = path_get_length (path);
1649 path_destroy (path);
1653 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1654 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1656 l2 = path_get_length (aux);
1659 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1660 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1661 peer->path_tail, aux, path);
1666 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1669 path_destroy (path);
1674 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head,
1677 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1680 if (NULL != peer->tunnel
1681 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
1682 && 2 < path->length) /* Direct paths are handled by core_connect */
1686 GCC_check_connections ();
1692 * Add the path to the origin peer and update the path used to reach it in case
1693 * this is the shortest.
1694 * The path is given in peer_info -> destination, therefore we turn the path
1695 * upside down first.
1697 * @param peer Peer to add the path to, being the origin of the path.
1698 * @param path New path to add after being inversed.
1699 * Path will be either used or freed.
1700 * @param trusted Do we trust that this path is real?
1702 * @return path if path was taken, pointer to existing duplicate if exists
1705 struct CadetPeerPath *
1706 GCP_add_path_to_origin (struct CadetPeer *peer,
1707 struct CadetPeerPath *path,
1713 return GCP_add_path (peer, path, trusted);
1718 * Adds a path to the info of all the peers in the path
1720 * @param p Path to process.
1721 * @param confirmed Whether we know if the path works or not.
1724 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1728 /* TODO: invert and add to origin */
1729 /* TODO: replace all "GCP_add_path" with this, make the other one static */
1730 GCC_check_connections ();
1731 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1732 for (i++; i < p->length; i++)
1734 struct CadetPeer *peer;
1735 struct CadetPeerPath *copy;
1737 peer = GCP_get_short (p->peers[i], GNUNET_YES);
1738 copy = path_duplicate (p);
1739 copy->length = i + 1;
1740 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
1742 GCC_check_connections ();
1747 * Remove any path to the peer that has the exact same peers as the one given.
1749 * @param peer Peer to remove the path from.
1750 * @param path Path to remove. Is always destroyed .
1753 GCP_remove_path (struct CadetPeer *peer,
1754 struct CadetPeerPath *path)
1756 struct CadetPeerPath *iter;
1757 struct CadetPeerPath *next;
1759 GCC_check_connections ();
1760 GNUNET_assert (myid == path->peers[0]);
1761 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1763 LOG (GNUNET_ERROR_TYPE_INFO,
1764 "Removing path %p (%u) from %s\n",
1765 path, path->length, GCP_2s (peer));
1767 for (iter = peer->path_head; NULL != iter; iter = next)
1770 if (0 == path_cmp (path, iter))
1772 GNUNET_CONTAINER_DLL_remove (peer->path_head,
1776 path_destroy (iter);
1779 path_destroy (path);
1780 GCC_check_connections ();
1785 * Check that we are aware of a connection from a neighboring peer.
1787 * @param peer Peer to the connection is with
1788 * @param c Connection that should be in the map with this peer.
1791 GCP_check_connection (const struct CadetPeer *peer,
1792 const struct CadetConnection *c)
1794 GNUNET_assert (NULL != peer);
1795 GNUNET_assert (NULL != peer->connections);
1797 GNUNET_assert (GNUNET_YES ==
1798 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
1805 * Remove a connection from a neighboring peer.
1807 * @param peer Peer to remove connection from.
1808 * @param c Connection to remove.
1811 GCP_remove_connection (struct CadetPeer *peer,
1812 const struct CadetConnection *c)
1814 LOG (GNUNET_ERROR_TYPE_DEBUG,
1815 "Removing connection %s\n",
1817 LOG (GNUNET_ERROR_TYPE_DEBUG,
1820 if ( (NULL == peer) ||
1821 (NULL == peer->connections) )
1823 GNUNET_assert (GNUNET_YES ==
1824 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1827 LOG (GNUNET_ERROR_TYPE_DEBUG,
1828 "Peer %s remains with %u connections.\n",
1830 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1835 * Start the DHT search for new paths towards the peer: we don't have
1836 * enough good connections.
1838 * @param peer Destination peer.
1841 GCP_start_search (struct CadetPeer *peer)
1843 const struct GNUNET_PeerIdentity *id;
1844 struct CadetTunnel *t = peer->tunnel;
1846 GCC_check_connections ();
1847 if (NULL != peer->search_h)
1853 if (NULL != peer->search_delayed)
1854 GCP_stop_search (peer);
1856 id = GNUNET_PEER_resolve2 (peer->id);
1857 peer->search_h = GCD_search (id, &search_handler, peer);
1861 /* Why would we search for a peer with no tunnel towards it? */
1866 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
1867 || 0 == GCT_count_any_connections (t))
1869 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
1871 GCC_check_connections ();
1876 * Stop the DHT search for new paths towards the peer: we already have
1877 * enough good connections.
1879 * @param peer Destination peer.
1882 GCP_stop_search (struct CadetPeer *peer)
1884 GCC_check_connections ();
1885 if (NULL != peer->search_h)
1887 GCD_search_stop (peer->search_h);
1888 peer->search_h = NULL;
1890 if (NULL != peer->search_delayed)
1892 GNUNET_SCHEDULER_cancel (peer->search_delayed);
1893 peer->search_delayed = NULL;
1895 GCC_check_connections ();
1900 * Get the Full ID of a peer.
1902 * @param peer Peer to get from.
1904 * @return Full ID of peer.
1906 const struct GNUNET_PeerIdentity *
1907 GCP_get_id (const struct CadetPeer *peer)
1909 return GNUNET_PEER_resolve2 (peer->id);
1914 * Get the Short ID of a peer.
1916 * @param peer Peer to get from.
1918 * @return Short ID of peer.
1921 GCP_get_short_id (const struct CadetPeer *peer)
1930 * If tunnel is NULL and there was a search active, stop it, as it's useless.
1936 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
1939 if (NULL == t && GNUNET_YES == is_searching (peer))
1941 GCP_stop_search (peer);
1947 * Get the tunnel towards a peer.
1949 * @param peer Peer to get from.
1951 * @return Tunnel towards peer.
1953 struct CadetTunnel *
1954 GCP_get_tunnel (const struct CadetPeer *peer)
1958 return peer->tunnel;
1963 * Set the hello message.
1965 * @param peer Peer whose message to set.
1966 * @param hello Hello message.
1969 GCP_set_hello (struct CadetPeer *peer,
1970 const struct GNUNET_HELLO_Message *hello)
1972 struct GNUNET_HELLO_Message *old;
1975 GCC_check_connections ();
1976 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
1980 old = GCP_get_hello (peer);
1983 size = GNUNET_HELLO_size (hello);
1984 peer->hello = GNUNET_malloc (size);
1985 GNUNET_memcpy (peer->hello, hello, size);
1989 peer->hello = GNUNET_HELLO_merge (old, hello);
1992 GCC_check_connections ();
1997 * Get the hello message.
1999 * @param peer Peer whose message to get.
2001 * @return Hello message.
2003 struct GNUNET_HELLO_Message *
2004 GCP_get_hello (struct CadetPeer *peer)
2006 struct GNUNET_TIME_Absolute expiration;
2007 struct GNUNET_TIME_Relative remaining;
2009 if (NULL == peer->hello)
2012 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2013 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2014 if (0 == remaining.rel_value_us)
2016 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2017 GNUNET_STRINGS_absolute_time_to_string (expiration));
2018 GNUNET_free (peer->hello);
2026 * Try to connect to a peer on TRANSPORT level.
2028 * @param peer Peer to whom to connect.
2031 GCP_try_connect (struct CadetPeer *peer)
2033 struct GNUNET_HELLO_Message *hello;
2034 struct GNUNET_MessageHeader *mh;
2037 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2039 "DISABLE_TRY_CONNECT"))
2041 GCC_check_connections ();
2042 if (GNUNET_YES == GCP_is_neighbor (peer))
2044 hello = GCP_get_hello (peer);
2048 mh = GNUNET_HELLO_get_header (hello);
2049 if (NULL != peer->hello_offer)
2051 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
2052 peer->hello_offer = NULL;
2054 peer->hello_offer = GNUNET_TRANSPORT_offer_hello (cfg,
2058 if (NULL == peer->connectivity_suggestion)
2059 peer->connectivity_suggestion
2060 = GNUNET_ATS_connectivity_suggest (ats_ch,
2063 GCC_check_connections ();
2068 * Notify a peer that a link between two other peers is broken. If any path
2069 * used that link, eliminate it.
2071 * @param peer Peer affected by the change.
2072 * @param peer1 Peer whose link is broken.
2073 * @param peer2 Peer whose link is broken.
2076 GCP_notify_broken_link (struct CadetPeer *peer,
2077 const struct GNUNET_PeerIdentity *peer1,
2078 const struct GNUNET_PeerIdentity *peer2)
2080 struct CadetPeerPath *iter;
2081 struct CadetPeerPath *next;
2086 GCC_check_connections ();
2087 p1 = GNUNET_PEER_search (peer1);
2088 p2 = GNUNET_PEER_search (peer2);
2090 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2091 if (0 == p1 || 0 == p2)
2093 /* We don't even know them */
2097 for (iter = peer->path_head; NULL != iter; iter = next)
2100 for (i = 0; i < iter->length - 1; i++)
2102 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2103 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2108 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2111 path_invalidate (iter);
2115 GCC_check_connections ();
2120 * Count the number of known paths toward the peer.
2122 * @param peer Peer to get path info.
2124 * @return Number of known paths.
2127 GCP_count_paths (const struct CadetPeer *peer)
2129 struct CadetPeerPath *iter;
2132 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2140 * Iterate over the paths to a peer.
2142 * @param peer Peer to get path info.
2143 * @param callback Function to call for every path.
2144 * @param cls Closure for @a callback.
2146 * @return Number of iterated paths.
2149 GCP_iterate_paths (struct CadetPeer *peer,
2150 GCP_path_iterator callback,
2153 struct CadetPeerPath *iter;
2156 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2159 if (GNUNET_YES != callback (cls, peer, iter))
2168 * Iterate all known peers.
2170 * @param iter Iterator.
2171 * @param cls Closure for @c iter.
2174 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2177 GCC_check_connections ();
2178 GNUNET_CONTAINER_multipeermap_iterate (peers,
2181 GCC_check_connections ();
2186 * Get the static string for a peer ID.
2190 * @return Static string for it's ID.
2193 GCP_2s (const struct CadetPeer *peer)
2197 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2201 /* end of gnunet-service-cadet_peer.c */