2 This file is part of GNUnet.
3 Copyright (C) 2007-2015 Christian Grothoff (and other contributing authors)
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.
22 * @file topology/gnunet-daemon-topology.c
23 * @brief code for maintaining the overlay topology
24 * @author Christian Grothoff
26 * This daemon combines three functions:
27 * - suggesting to ATS which peers we might want to connect to
28 * - enforcing the F2F restrictions (by blacklisting)
31 * All three require similar information (who are our friends
32 * impacts connectivity suggestions; connectivity suggestions
33 * should consider blacklisting; connectivity suggestions
34 * should consider available/known HELLOs; gossip requires
35 * connectivity data; connectivity suggestions require
36 * connectivity data), which is why they are combined in this
40 #include "gnunet_util_lib.h"
41 #include "gnunet_friends_lib.h"
42 #include "gnunet_constants.h"
43 #include "gnunet_core_service.h"
44 #include "gnunet_protocols.h"
45 #include "gnunet_peerinfo_service.h"
46 #include "gnunet_statistics_service.h"
47 #include "gnunet_transport_service.h"
48 #include "gnunet_ats_service.h"
52 * At what frequency do we sent HELLOs to a peer?
54 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
57 * After what time period do we expire the HELLO Bloom filter?
59 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
63 * Record for neighbours, friends and blacklisted peers.
68 * Which peer is this entry about?
70 struct GNUNET_PeerIdentity pid;
73 * Our handle for the request to transmit HELLOs to this peer; NULL
74 * if no such request is pending.
76 struct GNUNET_CORE_TransmitHandle *hello_req;
79 * Pointer to the HELLO message of this peer; can be NULL.
81 struct GNUNET_HELLO_Message *hello;
84 * Bloom filter used to mark which peers already got the HELLO
87 struct GNUNET_CONTAINER_BloomFilter *filter;
90 * Next time we are allowed to transmit a HELLO to this peer?
92 struct GNUNET_TIME_Absolute next_hello_allowed;
95 * When should we reset the bloom filter of this entry?
97 struct GNUNET_TIME_Absolute filter_expiration;
100 * ID of task we use to wait for the time to send the next HELLO
103 struct GNUNET_SCHEDULER_Task *hello_delay_task;
106 * Handle for our connectivity suggestion for this peer.
108 struct GNUNET_ATS_ConnectivitySuggestHandle *sh;
111 * How much would we like to connect to this peer?
116 * Is this peer listed here because he is a friend?
121 * Are we connected to this peer right now?
129 * Our peerinfo notification context. We use notification
130 * to instantly learn about new peers as they are discovered.
132 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
137 static const struct GNUNET_CONFIGURATION_Handle *cfg;
140 * Handle to the CORE service.
142 static struct GNUNET_CORE_Handle *handle;
145 * Handle to the TRANSPORT service.
147 static struct GNUNET_TRANSPORT_Handle *transport;
150 * Handle to the ATS service.
152 static struct GNUNET_ATS_ConnectivityHandle *ats;
155 * Identity of this peer.
157 static struct GNUNET_PeerIdentity my_identity;
160 * All of our friends, all of our current neighbours and all peers for
161 * which we have HELLOs. So pretty much everyone. Maps peer identities
162 * to `struct Peer *` values.
164 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
167 * Handle for reporting statistics.
169 static struct GNUNET_STATISTICS_Handle *stats;
172 * Blacklist (NULL if we have none).
174 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
177 * Task scheduled to asynchronously reconsider adding/removing
178 * peer connectivity suggestions.
180 static struct GNUNET_SCHEDULER_Task *add_task;
183 * Flag to disallow non-friend connections (pure F2F mode).
185 static int friends_only;
188 * Minimum number of friends to have in the
189 * connection set before we allow non-friends.
191 static unsigned int minimum_friend_count;
194 * Number of peers (friends and others) that we are currently connected to.
196 static unsigned int connection_count;
199 * Target number of connections.
201 static unsigned int target_connection_count;
204 * Number of friends that we are currently connected to.
206 static unsigned int friend_count;
210 * Function that decides if a connection is acceptable or not.
211 * If we have a blacklist, only friends are allowed, so the check
215 * @param pid peer to approve or disapprove
216 * @return #GNUNET_OK if the connection is allowed
219 blacklist_check (void *cls,
220 const struct GNUNET_PeerIdentity *pid)
224 pos = GNUNET_CONTAINER_multipeermap_get (peers,
226 if ( (NULL != pos) &&
227 (GNUNET_YES == pos->is_friend))
229 GNUNET_STATISTICS_update (stats,
230 gettext_noop ("# peers blacklisted"),
233 return GNUNET_SYSERR;
238 * Whitelist all peers that we blacklisted; we've passed
239 * the minimum number of friends.
244 if (NULL != blacklist)
246 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
253 * Free all resources associated with the given peer.
255 * @param cls closure (not used)
256 * @param pid identity of the peer
257 * @param value peer to free
258 * @return #GNUNET_YES (always: continue to iterate)
261 free_peer (void *cls,
262 const struct GNUNET_PeerIdentity * pid,
265 struct Peer *pos = value;
267 GNUNET_break (GNUNET_NO == pos->is_connected);
268 GNUNET_break (GNUNET_OK ==
269 GNUNET_CONTAINER_multipeermap_remove (peers,
272 if (NULL != pos->hello_req)
274 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
275 pos->hello_req = NULL;
277 if (NULL != pos->hello_delay_task)
279 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
280 pos->hello_delay_task = NULL;
284 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
287 if (NULL != pos->hello)
289 GNUNET_free_non_null (pos->hello);
292 if (NULL != pos->filter)
294 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
303 * Recalculate how much we want to be connected to the specified peer
304 * and let ATS know about the result.
306 * @param pos peer to consider connecting to
309 attempt_connect (struct Peer *pos)
314 memcmp (&my_identity,
316 sizeof (struct GNUNET_PeerIdentity)))
317 return; /* This is myself, nothing to do. */
318 if (connection_count < target_connection_count)
322 if ( (friend_count < minimum_friend_count) ||
323 (GNUNET_YES == friends_only) )
326 strength += 10; /* urgently needed */
328 strength = 0; /* disallowed */
331 strength *= 2; /* friends always count more */
332 if (pos->is_connected)
333 strength *= 2; /* existing connections preferred */
334 if (strength == pos->strength)
335 return; /* nothing to do */
338 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
341 pos->strength = strength;
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Asking to connect to `%s' with strength %u\n",
346 GNUNET_i2s (&pos->pid),
347 (unsigned int) strength);
348 GNUNET_STATISTICS_update (stats,
349 gettext_noop ("# connect requests issued to ATS"),
352 pos->sh = GNUNET_ATS_connectivity_suggest (ats,
360 * Create a new entry in the peer list.
362 * @param peer identity of the new entry
363 * @param hello hello message, can be NULL
364 * @param is_friend is the new entry for a friend?
365 * @return the new entry
368 make_peer (const struct GNUNET_PeerIdentity *peer,
369 const struct GNUNET_HELLO_Message *hello,
374 ret = GNUNET_new (struct Peer);
376 ret->is_friend = is_friend;
379 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
382 GNUNET_HELLO_size (hello));
384 GNUNET_break (GNUNET_OK ==
385 GNUNET_CONTAINER_multipeermap_put (peers,
388 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
394 * Setup bloom filter for the given peer entry.
396 * @param peer entry to initialize
399 setup_filter (struct Peer *peer)
401 struct GNUNET_HashCode hc;
403 /* 2^{-5} chance of not sending a HELLO to a peer is
404 * acceptably small (if the filter is 50% full);
405 * 64 bytes of memory are small compared to the rest
406 * of the data structure and would only really become
407 * "useless" once a HELLO has been passed on to ~100
408 * other peers, which is likely more than enough in
409 * any case; hence 64, 5 as bloomfilter parameters. */
410 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
411 peer->filter_expiration =
412 GNUNET_TIME_relative_to_absolute
413 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
414 /* never send a peer its own HELLO */
415 GNUNET_CRYPTO_hash (&peer->pid,
416 sizeof (struct GNUNET_PeerIdentity),
418 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &hc);
423 * Function to fill send buffer with HELLO.
425 * @param cls `struct Peer` of the target peer
426 * @param size number of bytes available in @a buf
427 * @param buf where the callee should write the message
428 * @return number of bytes written to @a buf
431 hello_advertising_ready (void *cls,
437 * Closure for #find_advertisable_hello().
439 struct FindAdvHelloContext
443 * Peer we want to advertise to.
448 * Where to store the result (peer selected for advertising).
453 * Maximum HELLO size we can use right now.
457 struct GNUNET_TIME_Relative next_adv;
462 * Find a peer that would be reasonable for advertising.
465 * @param pid identity of a peer
466 * @param value 'struct Peer*' for the peer we are considering
467 * @return #GNUNET_YES (continue iteration)
470 find_advertisable_hello (void *cls,
471 const struct GNUNET_PeerIdentity *pid,
474 struct FindAdvHelloContext *fah = cls;
475 struct Peer *pos = value;
476 struct GNUNET_TIME_Relative rst_time;
477 struct GNUNET_HashCode hc;
480 if (pos == fah->peer)
482 if (pos->hello == NULL)
484 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
485 if (0 == rst_time.rel_value_us)
487 /* time to discard... */
488 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
491 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
492 hs = GNUNET_HELLO_size (pos->hello);
493 if (hs > fah->max_size)
495 GNUNET_CRYPTO_hash (&fah->peer->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
497 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
505 * Calculate when we would like to send the next HELLO to this
506 * peer and ask for it.
508 * @param cls for which peer to schedule the HELLO
509 * @param tc task context
512 schedule_next_hello (void *cls,
513 const struct GNUNET_SCHEDULER_TaskContext *tc)
515 struct Peer *pl = cls;
516 struct FindAdvHelloContext fah;
518 struct GNUNET_TIME_Relative delay;
520 pl->hello_delay_task = NULL;
521 GNUNET_assert (GNUNET_YES == pl->is_connected);
522 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
523 return; /* we're out of here */
524 if (pl->hello_req != NULL)
525 return; /* did not finish sending the previous one */
526 /* find applicable HELLOs */
529 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
530 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
531 GNUNET_CONTAINER_multipeermap_iterate (peers,
532 &find_advertisable_hello,
534 pl->hello_delay_task =
535 GNUNET_SCHEDULER_add_delayed (fah.next_adv,
536 &schedule_next_hello,
538 if (fah.result == NULL)
540 next_want = GNUNET_HELLO_size (fah.result->hello);
541 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
542 if (0 == delay.rel_value_us)
546 GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES,
547 GNUNET_CORE_PRIO_BEST_EFFORT,
548 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
550 &hello_advertising_ready, pl);
556 * Cancel existing requests for sending HELLOs to this peer
557 * and recalculate when we should send HELLOs to it based
558 * on our current state (something changed!).
560 * @param cls closure `struct Peer` to skip, or NULL
561 * @param pid identity of a peer
562 * @param value `struct Peer *` for the peer
563 * @return #GNUNET_YES (always)
566 reschedule_hellos (void *cls,
567 const struct GNUNET_PeerIdentity *pid,
570 struct Peer *peer = value;
571 struct Peer *skip = cls;
575 if (!peer->is_connected)
577 if (peer->hello_req != NULL)
579 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
580 peer->hello_req = NULL;
582 if (peer->hello_delay_task != NULL)
584 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
585 peer->hello_delay_task = NULL;
587 peer->hello_delay_task =
588 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
594 * Method called whenever a peer connects.
597 * @param peer peer identity this notification is about
600 connect_notify (void *cls,
601 const struct GNUNET_PeerIdentity *peer)
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "Core told us that we are connecting to `%s'\n",
608 if (0 == memcmp (&my_identity,
610 sizeof (struct GNUNET_PeerIdentity)))
614 GNUNET_STATISTICS_set (stats,
615 gettext_noop ("# peers connected"),
618 pos = GNUNET_CONTAINER_multipeermap_get (peers,
622 pos = make_peer (peer,
628 GNUNET_assert (GNUNET_NO == pos->is_connected);
630 pos->is_connected = GNUNET_YES;
634 if ( (friend_count == minimum_friend_count) &&
635 (GNUNET_YES != friends_only) )
637 GNUNET_STATISTICS_set (stats,
638 gettext_noop ("# friends connected"),
642 reschedule_hellos (NULL,
649 * Try to add more peers to our connection set.
651 * @param cls closure, not used
652 * @param pid identity of a peer
653 * @param value `struct Peer *` for the peer
654 * @return #GNUNET_YES (continue to iterate)
657 try_add_peers (void *cls,
658 const struct GNUNET_PeerIdentity *pid,
661 struct Peer *pos = value;
663 attempt_connect (pos);
669 * Add peers and schedule connection attempt
671 * @param cls unused, NULL
672 * @param tc scheduler context
675 add_peer_task (void *cls,
676 const struct GNUNET_SCHEDULER_TaskContext *tc)
680 GNUNET_CONTAINER_multipeermap_iterate (peers,
687 * Method called whenever a peer disconnects.
690 * @param peer peer identity this notification is about
693 disconnect_notify (void *cls,
694 const struct GNUNET_PeerIdentity *peer)
698 if (0 == memcmp (&my_identity,
700 sizeof (struct GNUNET_PeerIdentity)))
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
703 "Core told us that we disconnected from `%s'\n",
705 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
711 if (GNUNET_YES != pos->is_connected)
716 pos->is_connected = GNUNET_NO;
718 if (NULL != pos->hello_req)
720 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
721 pos->hello_req = NULL;
723 if (NULL != pos->hello_delay_task)
725 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
726 pos->hello_delay_task = NULL;
728 GNUNET_STATISTICS_set (stats,
729 gettext_noop ("# peers connected"),
735 GNUNET_STATISTICS_set (stats,
736 gettext_noop ("# friends connected"),
740 if ( ( (connection_count < target_connection_count) ||
741 (friend_count < minimum_friend_count)) &&
743 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task,
745 if ( (friend_count < minimum_friend_count) &&
747 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
754 * Iterator called on each address.
756 * @param cls flag that we will set if we see any addresses
757 * @param address the address of the peer
758 * @param expiration when will the given address expire
759 * @return #GNUNET_SYSERR always, to terminate iteration
762 address_iterator (void *cls,
763 const struct GNUNET_HELLO_Address *address,
764 struct GNUNET_TIME_Absolute expiration)
769 return GNUNET_SYSERR;
774 * We've gotten a HELLO from another peer. Consider it for
777 * @param hello the HELLO we got
780 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
783 struct GNUNET_PeerIdentity pid;
784 struct GNUNET_TIME_Absolute dt;
785 struct GNUNET_HELLO_Message *nh;
789 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
794 if (0 == memcmp (&pid,
796 sizeof (struct GNUNET_PeerIdentity)))
797 return; /* that's me! */
798 have_address = GNUNET_NO;
799 GNUNET_HELLO_iterate_addresses (hello,
803 if (GNUNET_NO == have_address)
804 return; /* no point in advertising this one... */
805 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
808 peer = make_peer (&pid,
812 else if (NULL != peer->hello)
814 dt = GNUNET_HELLO_equals (peer->hello,
816 GNUNET_TIME_absolute_get ());
817 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
818 return; /* nothing new here */
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Found HELLO from peer `%s' for advertising\n",
823 if (NULL != peer->hello)
825 nh = GNUNET_HELLO_merge (peer->hello,
827 GNUNET_free (peer->hello);
832 size = GNUNET_HELLO_size (hello);
833 peer->hello = GNUNET_malloc (size);
838 if (NULL != peer->filter)
840 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
844 /* since we have a new HELLO to pick from, re-schedule all
845 * HELLO requests that are not bound by the HELLO send rate! */
846 GNUNET_CONTAINER_multipeermap_iterate (peers,
853 * PEERINFO calls this function to let us know about a possible peer
854 * that we might want to connect to.
856 * @param cls closure (not used)
857 * @param peer potential peer to connect to
858 * @param hello HELLO for this peer (or NULL)
859 * @param err_msg NULL if successful, otherwise contains error message
862 process_peer (void *cls,
863 const struct GNUNET_PeerIdentity *peer,
864 const struct GNUNET_HELLO_Message *hello,
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 _("Error in communication with PEERINFO service: %s\n"),
874 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
875 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
881 GNUNET_assert (NULL != peer);
882 if (0 == memcmp (&my_identity,
884 sizeof (struct GNUNET_PeerIdentity)))
885 return; /* that's me! */
888 /* free existing HELLO, if any */
889 pos = GNUNET_CONTAINER_multipeermap_get (peers,
893 GNUNET_free_non_null (pos->hello);
895 if (NULL != pos->filter)
897 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
900 if ( (GNUNET_NO == pos->is_connected) &&
901 (GNUNET_NO == pos->is_friend) )
908 consider_for_advertising (hello);
909 pos = GNUNET_CONTAINER_multipeermap_get (peers,
912 pos = make_peer (peer,
915 attempt_connect (pos);
920 * Function called after #GNUNET_CORE_connect has succeeded
921 * (or failed for good).
924 * @param my_id ID of this peer, NULL if we failed
927 core_init (void *cls,
928 const struct GNUNET_PeerIdentity *my_id)
932 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
933 _("Failed to connect to core service, can not manage topology!\n"));
934 GNUNET_SCHEDULER_shutdown ();
937 my_identity = *my_id;
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
949 * Process friend found in FRIENDS file.
951 * @param cls pointer to an `unsigned int` to be incremented per friend found
952 * @param pid identity of the friend
955 handle_friend (void *cls,
956 const struct GNUNET_PeerIdentity *pid)
958 unsigned int *entries_found = cls;
961 if (0 == memcmp (pid,
963 sizeof (struct GNUNET_PeerIdentity)))
965 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
966 _("Found myself `%s' in friend list (useless, ignored)\n"),
971 fl = make_peer (pid, NULL, GNUNET_YES);
972 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
973 _("Found friend `%s' in configuration\n"),
974 GNUNET_i2s (&fl->pid));
979 * Read the friends file.
982 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
984 unsigned int entries_found;
988 GNUNET_FRIENDS_parse (cfg,
992 if ( (GNUNET_YES == friends_only) ||
993 (minimum_friend_count > 0))
994 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
995 _("Encountered errors parsing friends list!\n"));
997 GNUNET_STATISTICS_update (stats,
998 gettext_noop ("# friends in configuration"),
1001 if ( (minimum_friend_count > entries_found) &&
1002 (GNUNET_NO == friends_only) )
1004 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1005 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1007 if ( (minimum_friend_count > target_connection_count) &&
1008 (GNUNET_NO == friends_only))
1010 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1011 _("More friendly connections required than target total number of connections.\n"));
1017 * This function is called whenever an encrypted HELLO message is
1020 * @param cls closure
1021 * @param other the other peer involved (sender or receiver, NULL
1022 * for loopback messages where we are both sender and receiver)
1023 * @param message the actual HELLO message
1024 * @return #GNUNET_OK to keep the connection open,
1025 * #GNUNET_SYSERR to close it (signal serious error)
1028 handle_encrypted_hello (void *cls,
1029 const struct GNUNET_PeerIdentity *other,
1030 const struct GNUNET_MessageHeader *message)
1033 struct GNUNET_PeerIdentity pid;
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Received encrypted HELLO from peer `%s'",
1037 GNUNET_i2s (other));
1039 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1041 GNUNET_break_op (0);
1042 return GNUNET_SYSERR;
1044 GNUNET_STATISTICS_update (stats,
1045 gettext_noop ("# HELLO messages received"),
1048 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
1051 if ( (GNUNET_YES == friends_only) ||
1052 (friend_count < minimum_friend_count) )
1057 if ( (GNUNET_YES != peer->is_friend) &&
1058 (GNUNET_YES == friends_only) )
1060 if ((GNUNET_YES != peer->is_friend) &&
1061 (friend_count < minimum_friend_count))
1064 if (NULL != transport)
1065 GNUNET_TRANSPORT_offer_hello (transport,
1074 * Function to fill send buffer with HELLO.
1076 * @param cls `struct Peer` of the target peer
1077 * @param size number of bytes available in buf
1078 * @param buf where the callee should write the message
1079 * @return number of bytes written to buf
1082 hello_advertising_ready (void *cls,
1086 struct Peer *pl = cls;
1087 struct FindAdvHelloContext fah;
1089 struct GNUNET_HashCode hc;
1091 pl->hello_req = NULL;
1092 GNUNET_assert (GNUNET_YES == pl->is_connected);
1093 /* find applicable HELLOs */
1096 fah.max_size = size;
1097 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1098 GNUNET_CONTAINER_multipeermap_iterate (peers,
1099 &find_advertisable_hello,
1102 if (NULL != fah.result)
1104 want = GNUNET_HELLO_size (fah.result->hello);
1105 GNUNET_assert (want <= size);
1109 GNUNET_CRYPTO_hash (&pl->pid,
1110 sizeof (struct GNUNET_PeerIdentity),
1112 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1115 "Sending HELLO with %u bytes",
1116 (unsigned int) want);
1117 GNUNET_STATISTICS_update (stats,
1118 gettext_noop ("# HELLO messages gossipped"), 1,
1122 if (pl->hello_delay_task != NULL)
1123 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1124 pl->next_hello_allowed =
1125 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1126 pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1132 * Last task run during shutdown. Disconnects us from
1133 * the transport and core.
1135 * @param cls unused, NULL
1136 * @param tc scheduler context
1139 cleaning_task (void *cls,
1140 const struct GNUNET_SCHEDULER_TaskContext *tc)
1142 if (NULL != peerinfo_notify)
1144 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1145 peerinfo_notify = NULL;
1147 if (NULL != transport)
1149 GNUNET_TRANSPORT_disconnect (transport);
1154 GNUNET_CORE_disconnect (handle);
1158 if (NULL != add_task)
1160 GNUNET_SCHEDULER_cancel (add_task);
1163 GNUNET_CONTAINER_multipeermap_iterate (peers,
1166 GNUNET_CONTAINER_multipeermap_destroy (peers);
1170 GNUNET_ATS_connectivity_done (ats);
1175 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1182 * Main function that will be run.
1184 * @param cls closure
1185 * @param args remaining command-line arguments
1186 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1187 * @param c configuration
1192 const char *cfgfile,
1193 const struct GNUNET_CONFIGURATION_Handle *c)
1195 static struct GNUNET_CORE_MessageHandler handlers[] = {
1196 {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1199 unsigned long long opt;
1202 stats = GNUNET_STATISTICS_create ("topology", cfg);
1204 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1208 GNUNET_CONFIGURATION_get_value_number (cfg,
1213 minimum_friend_count = (unsigned int) opt;
1215 GNUNET_CONFIGURATION_get_value_number (cfg,
1217 "TARGET-CONNECTION-COUNT",
1220 target_connection_count = (unsigned int) opt;
1221 peers = GNUNET_CONTAINER_multipeermap_create (target_connection_count * 2,
1223 read_friends_file (cfg);
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "Topology would like %u connections with at least %u friends\n",
1226 target_connection_count,
1227 minimum_friend_count);
1228 if ( (friend_count < minimum_friend_count) &&
1229 (NULL == blacklist))
1230 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1233 ats = GNUNET_ATS_connectivity_init (cfg);
1234 transport = GNUNET_TRANSPORT_connect (cfg,
1241 GNUNET_CORE_connect (cfg, NULL,
1248 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1251 if (NULL == transport)
1253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1254 _("Failed to connect to `%s' service.\n"),
1256 GNUNET_SCHEDULER_shutdown ();
1261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1262 _("Failed to connect to `%s' service.\n"),
1264 GNUNET_SCHEDULER_shutdown ();
1271 * The main function for the topology daemon.
1273 * @param argc number of arguments from the command line
1274 * @param argv command line arguments
1275 * @return 0 ok, 1 on error
1278 main (int argc, char *const *argv)
1280 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1281 GNUNET_GETOPT_OPTION_END
1285 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1290 GNUNET_PROGRAM_run (argc, argv,
1291 "gnunet-daemon-topology",
1292 _("GNUnet topology control"),
1293 options, &run, NULL)) ? 0 : 1;
1294 GNUNET_free ((void*) argv);
1299 #if defined(LINUX) && defined(__GLIBC__)
1303 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1305 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1307 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1308 mallopt (M_TOP_PAD, 1 * 1024);
1313 /* end of gnunet-daemon-topology.c */