2 This file is part of GNUnet.
3 Copyright (C) 2007-2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 transmitting to this peer; NULL
74 * if peer is not connected.
76 struct GNUNET_MQ_Handle *mq;
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 it is a friend?
124 * Our peerinfo notification context. We use notification
125 * to instantly learn about new peers as they are discovered.
127 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
132 static const struct GNUNET_CONFIGURATION_Handle *cfg;
135 * Handle to the CORE service.
137 static struct GNUNET_CORE_Handle *handle;
140 * Handle to the PEERINFO service.
142 static struct GNUNET_PEERINFO_Handle *pi;
145 * Handle to the ATS service.
147 static struct GNUNET_ATS_ConnectivityHandle *ats;
150 * Identity of this peer.
152 static struct GNUNET_PeerIdentity my_identity;
155 * All of our friends, all of our current neighbours and all peers for
156 * which we have HELLOs. So pretty much everyone. Maps peer identities
157 * to `struct Peer *` values.
159 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
162 * Handle for reporting statistics.
164 static struct GNUNET_STATISTICS_Handle *stats;
167 * Blacklist (NULL if we have none).
169 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
172 * Task scheduled to asynchronously reconsider adding/removing
173 * peer connectivity suggestions.
175 static struct GNUNET_SCHEDULER_Task *add_task;
178 * Active HELLO offering to transport service.
180 static struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
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 (NULL == pos->mq);
268 GNUNET_break (GNUNET_OK ==
269 GNUNET_CONTAINER_multipeermap_remove (peers,
272 if (NULL != pos->hello_delay_task)
274 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
275 pos->hello_delay_task = NULL;
279 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
282 if (NULL != pos->hello)
284 GNUNET_free_non_null (pos->hello);
287 if (NULL != pos->filter)
289 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
298 * Recalculate how much we want to be connected to the specified peer
299 * and let ATS know about the result.
301 * @param pos peer to consider connecting to
304 attempt_connect (struct Peer *pos)
309 GNUNET_memcmp (&my_identity,
311 return; /* This is myself, nothing to do. */
312 if (connection_count < target_connection_count)
316 if ( (friend_count < minimum_friend_count) ||
317 (GNUNET_YES == friends_only) )
320 strength += 10; /* urgently needed */
322 strength = 0; /* disallowed */
325 strength *= 2; /* friends always count more */
327 strength *= 2; /* existing connections preferred */
328 if (strength == pos->strength)
329 return; /* nothing to do */
332 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
335 pos->strength = strength;
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "Asking to connect to `%s' with strength %u\n",
340 GNUNET_i2s (&pos->pid),
341 (unsigned int) strength);
342 GNUNET_STATISTICS_update (stats,
343 gettext_noop ("# connect requests issued to ATS"),
346 pos->sh = GNUNET_ATS_connectivity_suggest (ats,
354 * Create a new entry in the peer list.
356 * @param peer identity of the new entry
357 * @param hello hello message, can be NULL
358 * @param is_friend is the new entry for a friend?
359 * @return the new entry
362 make_peer (const struct GNUNET_PeerIdentity *peer,
363 const struct GNUNET_HELLO_Message *hello,
368 ret = GNUNET_new (struct Peer);
370 ret->is_friend = is_friend;
373 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
374 GNUNET_memcpy (ret->hello,
376 GNUNET_HELLO_size (hello));
378 GNUNET_break (GNUNET_OK ==
379 GNUNET_CONTAINER_multipeermap_put (peers,
382 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
388 * Setup bloom filter for the given peer entry.
390 * @param peer entry to initialize
393 setup_filter (struct Peer *peer)
395 struct GNUNET_HashCode hc;
397 /* 2^{-5} chance of not sending a HELLO to a peer is
398 * acceptably small (if the filter is 50% full);
399 * 64 bytes of memory are small compared to the rest
400 * of the data structure and would only really become
401 * "useless" once a HELLO has been passed on to ~100
402 * other peers, which is likely more than enough in
403 * any case; hence 64, 5 as bloomfilter parameters. */
404 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
405 peer->filter_expiration =
406 GNUNET_TIME_relative_to_absolute
407 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
408 /* never send a peer its own HELLO */
409 GNUNET_CRYPTO_hash (&peer->pid,
410 sizeof (struct GNUNET_PeerIdentity),
412 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &hc);
417 * Closure for #find_advertisable_hello().
419 struct FindAdvHelloContext
423 * Peer we want to advertise to.
428 * Where to store the result (peer selected for advertising).
433 * Maximum HELLO size we can use right now.
437 struct GNUNET_TIME_Relative next_adv;
442 * Find a peer that would be reasonable for advertising.
445 * @param pid identity of a peer
446 * @param value 'struct Peer*' for the peer we are considering
447 * @return #GNUNET_YES (continue iteration)
450 find_advertisable_hello (void *cls,
451 const struct GNUNET_PeerIdentity *pid,
454 struct FindAdvHelloContext *fah = cls;
455 struct Peer *pos = value;
456 struct GNUNET_TIME_Relative rst_time;
457 struct GNUNET_HashCode hc;
460 if (pos == fah->peer)
462 if (pos->hello == NULL)
464 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
465 if (0 == rst_time.rel_value_us)
467 /* time to discard... */
468 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
471 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
472 hs = GNUNET_HELLO_size (pos->hello);
473 if (hs > fah->max_size)
475 GNUNET_CRYPTO_hash (&fah->peer->pid,
476 sizeof (struct GNUNET_PeerIdentity), &hc);
478 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
486 * Calculate when we would like to send the next HELLO to this
487 * peer and ask for it.
489 * @param cls for which peer to schedule the HELLO
492 schedule_next_hello (void *cls)
494 struct Peer *pl = cls;
495 struct FindAdvHelloContext fah;
496 struct GNUNET_MQ_Envelope *env;
498 struct GNUNET_TIME_Relative delay;
499 struct GNUNET_HashCode hc;
501 pl->hello_delay_task = NULL;
502 GNUNET_assert (NULL != pl->mq);
503 /* find applicable HELLOs */
506 fah.max_size = GNUNET_MAX_MESSAGE_SIZE - 1;
507 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
508 GNUNET_CONTAINER_multipeermap_iterate (peers,
509 &find_advertisable_hello,
511 pl->hello_delay_task =
512 GNUNET_SCHEDULER_add_delayed (fah.next_adv,
513 &schedule_next_hello,
515 if (NULL == fah.result)
517 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
518 if (0 != delay.rel_value_us)
521 want = GNUNET_HELLO_size (fah.result->hello);
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Sending HELLO with %u bytes",
524 (unsigned int) want);
525 env = GNUNET_MQ_msg_copy (&fah.result->hello->header);
526 GNUNET_MQ_send (pl->mq,
529 /* avoid sending this one again soon */
530 GNUNET_CRYPTO_hash (&pl->pid,
531 sizeof (struct GNUNET_PeerIdentity),
533 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
536 GNUNET_STATISTICS_update (stats,
537 gettext_noop ("# HELLO messages gossipped"),
540 /* prepare to send the next one */
541 pl->next_hello_allowed
542 = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
543 if (NULL != pl->hello_delay_task)
544 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
546 = GNUNET_SCHEDULER_add_now (&schedule_next_hello,
552 * Cancel existing requests for sending HELLOs to this peer
553 * and recalculate when we should send HELLOs to it based
554 * on our current state (something changed!).
556 * @param cls closure `struct Peer` to skip, or NULL
557 * @param pid identity of a peer
558 * @param value `struct Peer *` for the peer
559 * @return #GNUNET_YES (always)
562 reschedule_hellos (void *cls,
563 const struct GNUNET_PeerIdentity *pid,
566 struct Peer *peer = value;
567 struct Peer *skip = cls;
571 if (NULL == peer->mq)
573 if (NULL != peer->hello_delay_task)
575 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
576 peer->hello_delay_task = NULL;
578 peer->hello_delay_task =
579 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
585 * Method called whenever a peer connects.
588 * @param peer peer identity this notification is about
589 * @param mq message queue for communicating with @a peer
590 * @return our `struct Peer` for @a peer
593 connect_notify (void *cls,
594 const struct GNUNET_PeerIdentity *peer,
595 struct GNUNET_MQ_Handle *mq)
601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
602 "Core told us that we are connecting to `%s'\n",
604 if (0 == GNUNET_memcmp (&my_identity,
607 extra = GNUNET_CORE_get_mq_options (GNUNET_YES,
608 GNUNET_CORE_PRIO_BEST_EFFORT,
610 GNUNET_MQ_set_options (mq,
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 (NULL == pos->mq);
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,
650 * Try to add more peers to our connection set.
652 * @param cls closure, not used
653 * @param pid identity of a peer
654 * @param value `struct Peer *` for the peer
655 * @return #GNUNET_YES (continue to iterate)
658 try_add_peers (void *cls,
659 const struct GNUNET_PeerIdentity *pid,
662 struct Peer *pos = value;
664 attempt_connect (pos);
670 * Add peers and schedule connection attempt
672 * @param cls unused, NULL
675 add_peer_task (void *cls)
679 GNUNET_CONTAINER_multipeermap_iterate (peers,
686 * Method called whenever a peer disconnects.
689 * @param peer peer identity this notification is about
690 * @param internal_cls the `struct Peer` for this peer
693 disconnect_notify (void *cls,
694 const struct GNUNET_PeerIdentity *peer,
697 struct Peer *pos = internal_cls;
700 return; /* myself, we're shutting down */
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Core told us that we disconnected from `%s'\n",
711 if (NULL != pos->hello_delay_task)
713 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
714 pos->hello_delay_task = NULL;
716 GNUNET_STATISTICS_set (stats,
717 gettext_noop ("# peers connected"),
723 GNUNET_STATISTICS_set (stats,
724 gettext_noop ("# friends connected"),
728 if ( ( (connection_count < target_connection_count) ||
729 (friend_count < minimum_friend_count)) &&
731 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task,
733 if ( (friend_count < minimum_friend_count) &&
735 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
742 * Iterator called on each address.
744 * @param cls flag that we will set if we see any addresses
745 * @param address the address of the peer
746 * @param expiration when will the given address expire
747 * @return #GNUNET_SYSERR always, to terminate iteration
750 address_iterator (void *cls,
751 const struct GNUNET_HELLO_Address *address,
752 struct GNUNET_TIME_Absolute expiration)
757 return GNUNET_SYSERR;
762 * We've gotten a HELLO from another peer. Consider it for
765 * @param hello the HELLO we got
768 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
771 struct GNUNET_PeerIdentity pid;
772 struct GNUNET_TIME_Absolute dt;
773 struct GNUNET_HELLO_Message *nh;
777 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
782 if (0 == GNUNET_memcmp (&pid,
784 return; /* that's me! */
785 have_address = GNUNET_NO;
786 GNUNET_HELLO_iterate_addresses (hello,
790 if (GNUNET_NO == have_address)
791 return; /* no point in advertising this one... */
792 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
795 peer = make_peer (&pid,
799 else if (NULL != peer->hello)
801 dt = GNUNET_HELLO_equals (peer->hello,
803 GNUNET_TIME_absolute_get ());
804 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
805 return; /* nothing new here */
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 "Found HELLO from peer `%s' for advertising\n",
810 if (NULL != peer->hello)
812 nh = GNUNET_HELLO_merge (peer->hello,
814 GNUNET_free (peer->hello);
819 size = GNUNET_HELLO_size (hello);
820 peer->hello = GNUNET_malloc (size);
821 GNUNET_memcpy (peer->hello,
825 if (NULL != peer->filter)
827 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
831 /* since we have a new HELLO to pick from, re-schedule all
832 * HELLO requests that are not bound by the HELLO send rate! */
833 GNUNET_CONTAINER_multipeermap_iterate (peers,
840 * PEERINFO calls this function to let us know about a possible peer
841 * that we might want to connect to.
843 * @param cls closure (not used)
844 * @param peer potential peer to connect to
845 * @param hello HELLO for this peer (or NULL)
846 * @param err_msg NULL if successful, otherwise contains error message
849 process_peer (void *cls,
850 const struct GNUNET_PeerIdentity *peer,
851 const struct GNUNET_HELLO_Message *hello,
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 _("Error in communication with PEERINFO service: %s\n"),
861 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
862 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
868 GNUNET_assert (NULL != peer);
869 if (0 == GNUNET_memcmp (&my_identity,
871 return; /* that's me! */
874 /* free existing HELLO, if any */
875 pos = GNUNET_CONTAINER_multipeermap_get (peers,
879 GNUNET_free_non_null (pos->hello);
881 if (NULL != pos->filter)
883 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
886 if ( (NULL == pos->mq) &&
887 (GNUNET_NO == pos->is_friend) )
894 consider_for_advertising (hello);
895 pos = GNUNET_CONTAINER_multipeermap_get (peers,
898 pos = make_peer (peer,
901 attempt_connect (pos);
906 * Function called after #GNUNET_CORE_connect has succeeded
907 * (or failed for good).
910 * @param my_id ID of this peer, NULL if we failed
913 core_init (void *cls,
914 const struct GNUNET_PeerIdentity *my_id)
918 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
919 _("Failed to connect to core service, can not manage topology!\n"));
920 GNUNET_SCHEDULER_shutdown ();
923 my_identity = *my_id;
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
935 * Process friend found in FRIENDS file.
937 * @param cls pointer to an `unsigned int` to be incremented per friend found
938 * @param pid identity of the friend
941 handle_friend (void *cls,
942 const struct GNUNET_PeerIdentity *pid)
944 unsigned int *entries_found = cls;
947 if (0 == GNUNET_memcmp (pid,
950 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
951 _("Found myself `%s' in friend list (useless, ignored)\n"),
956 fl = make_peer (pid, NULL, GNUNET_YES);
957 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
958 _("Found friend `%s' in configuration\n"),
959 GNUNET_i2s (&fl->pid));
964 * Read the friends file.
967 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
969 unsigned int entries_found;
973 GNUNET_FRIENDS_parse (cfg,
977 if ( (GNUNET_YES == friends_only) ||
978 (minimum_friend_count > 0))
979 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
980 _("Encountered errors parsing friends list!\n"));
982 GNUNET_STATISTICS_update (stats,
983 gettext_noop ("# friends in configuration"),
986 if ( (minimum_friend_count > entries_found) &&
987 (GNUNET_NO == friends_only) )
989 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
990 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
992 if ( (minimum_friend_count > target_connection_count) &&
993 (GNUNET_NO == friends_only))
995 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
996 _("More friendly connections required than target total number of connections.\n"));
1002 * This function is called whenever an encrypted HELLO message is
1005 * @param cls closure with the peer identity of the sender
1006 * @param message the actual HELLO message
1007 * @return #GNUNET_OK if @a message is well-formed
1008 * #GNUNET_SYSERR if @a message is invalid
1011 check_hello (void *cls,
1012 const struct GNUNET_HELLO_Message *message)
1014 struct GNUNET_PeerIdentity pid;
1017 GNUNET_HELLO_get_id (message,
1020 GNUNET_break_op (0);
1021 return GNUNET_SYSERR;
1028 * This function is called whenever an encrypted HELLO message is
1031 * @param cls closure with the peer identity of the sender
1032 * @param message the actual HELLO message
1035 handle_hello (void *cls,
1036 const struct GNUNET_HELLO_Message *message)
1038 const struct GNUNET_PeerIdentity *other = cls;
1040 struct GNUNET_PeerIdentity pid;
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "Received encrypted HELLO from peer `%s'",
1044 GNUNET_i2s (other));
1045 GNUNET_assert (GNUNET_OK ==
1046 GNUNET_HELLO_get_id (message,
1048 GNUNET_STATISTICS_update (stats,
1049 gettext_noop ("# HELLO messages received"),
1052 peer = GNUNET_CONTAINER_multipeermap_get (peers,
1056 if ( (GNUNET_YES == friends_only) ||
1057 (friend_count < minimum_friend_count) )
1062 if ( (GNUNET_YES != peer->is_friend) &&
1063 (GNUNET_YES == friends_only) )
1065 if ((GNUNET_YES != peer->is_friend) &&
1066 (friend_count < minimum_friend_count))
1069 (void) GNUNET_PEERINFO_add_peer (pi,
1077 * Last task run during shutdown. Disconnects us from
1078 * the transport and core.
1080 * @param cls unused, NULL
1083 cleaning_task (void *cls)
1085 if (NULL != peerinfo_notify)
1087 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1088 peerinfo_notify = NULL;
1092 GNUNET_CORE_disconnect (handle);
1096 if (NULL != add_task)
1098 GNUNET_SCHEDULER_cancel (add_task);
1103 GNUNET_TRANSPORT_offer_hello_cancel (oh);
1106 GNUNET_CONTAINER_multipeermap_iterate (peers,
1109 GNUNET_CONTAINER_multipeermap_destroy (peers);
1113 GNUNET_ATS_connectivity_done (ats);
1118 GNUNET_PEERINFO_disconnect (pi);
1123 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1130 * Main function that will be run.
1132 * @param cls closure
1133 * @param args remaining command-line arguments
1134 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1135 * @param c configuration
1140 const char *cfgfile,
1141 const struct GNUNET_CONFIGURATION_Handle *c)
1143 struct GNUNET_MQ_MessageHandler handlers[] = {
1144 GNUNET_MQ_hd_var_size (hello,
1145 GNUNET_MESSAGE_TYPE_HELLO,
1146 struct GNUNET_HELLO_Message,
1148 GNUNET_MQ_handler_end ()
1150 unsigned long long opt;
1153 stats = GNUNET_STATISTICS_create ("topology", cfg);
1155 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1159 GNUNET_CONFIGURATION_get_value_number (cfg,
1164 minimum_friend_count = (unsigned int) opt;
1166 GNUNET_CONFIGURATION_get_value_number (cfg,
1168 "TARGET-CONNECTION-COUNT",
1171 target_connection_count = (unsigned int) opt;
1172 peers = GNUNET_CONTAINER_multipeermap_create (target_connection_count * 2,
1174 read_friends_file (cfg);
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Topology would like %u connections with at least %u friends\n",
1177 target_connection_count,
1178 minimum_friend_count);
1179 if ( (GNUNET_YES == friends_only) ||
1180 (minimum_friend_count > 0))
1181 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1184 ats = GNUNET_ATS_connectivity_init (cfg);
1185 pi = GNUNET_PEERINFO_connect (cfg);
1186 handle = GNUNET_CORE_connect (cfg,
1192 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
1196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1197 _("Failed to connect to `%s' service.\n"),
1199 GNUNET_SCHEDULER_shutdown ();
1206 * The main function for the topology daemon.
1208 * @param argc number of arguments from the command line
1209 * @param argv command line arguments
1210 * @return 0 ok, 1 on error
1216 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1217 GNUNET_GETOPT_OPTION_END
1221 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1226 GNUNET_PROGRAM_run (argc, argv,
1227 "gnunet-daemon-topology",
1228 _("GNUnet topology control"),
1229 options, &run, NULL)) ? 0 : 1;
1230 GNUNET_free ((void*) argv);
1235 #if defined(LINUX) && defined(__GLIBC__)
1239 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1241 void __attribute__ ((constructor))
1242 GNUNET_ARM_memory_init ()
1244 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1245 mallopt (M_TOP_PAD, 1 * 1024);
1250 /* end of gnunet-daemon-topology.c */