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 \
55 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
58 * After what time period do we expire the HELLO Bloom filter?
60 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY \
61 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 4)
65 * Record for neighbours, friends and blacklisted peers.
69 * Which peer is this entry about?
71 struct GNUNET_PeerIdentity pid;
74 * Our handle for transmitting to this peer; NULL
75 * if peer is not connected.
77 struct GNUNET_MQ_Handle *mq;
80 * Pointer to the HELLO message of this peer; can be NULL.
82 struct GNUNET_HELLO_Message *hello;
85 * Bloom filter used to mark which peers already got the HELLO
88 struct GNUNET_CONTAINER_BloomFilter *filter;
91 * Next time we are allowed to transmit a HELLO to this peer?
93 struct GNUNET_TIME_Absolute next_hello_allowed;
96 * When should we reset the bloom filter of this entry?
98 struct GNUNET_TIME_Absolute filter_expiration;
101 * ID of task we use to wait for the time to send the next HELLO
104 struct GNUNET_SCHEDULER_Task *hello_delay_task;
107 * Handle for our connectivity suggestion for this peer.
109 struct GNUNET_ATS_ConnectivitySuggestHandle *sh;
112 * How much would we like to connect to this peer?
117 * 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, const struct GNUNET_PeerIdentity *pid)
223 pos = GNUNET_CONTAINER_multipeermap_get(peers, pid);
224 if ((NULL != pos) && (GNUNET_YES == pos->is_friend))
226 GNUNET_STATISTICS_update(stats,
227 gettext_noop("# peers blacklisted"),
230 return GNUNET_SYSERR;
235 * Whitelist all peers that we blacklisted; we've passed
236 * the minimum number of friends.
241 if (NULL != blacklist)
243 GNUNET_TRANSPORT_blacklist_cancel(blacklist);
250 * Free all resources associated with the given peer.
252 * @param cls closure (not used)
253 * @param pid identity of the peer
254 * @param value peer to free
255 * @return #GNUNET_YES (always: continue to iterate)
258 free_peer(void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
260 struct Peer *pos = value;
262 GNUNET_break(NULL == pos->mq);
263 GNUNET_break(GNUNET_OK ==
264 GNUNET_CONTAINER_multipeermap_remove(peers, pid, pos));
265 if (NULL != pos->hello_delay_task)
267 GNUNET_SCHEDULER_cancel(pos->hello_delay_task);
268 pos->hello_delay_task = NULL;
272 GNUNET_ATS_connectivity_suggest_cancel(pos->sh);
275 if (NULL != pos->hello)
277 GNUNET_free_non_null(pos->hello);
280 if (NULL != pos->filter)
282 GNUNET_CONTAINER_bloomfilter_free(pos->filter);
291 * Recalculate how much we want to be connected to the specified peer
292 * and let ATS know about the result.
294 * @param pos peer to consider connecting to
297 attempt_connect(struct Peer *pos)
301 if (0 == GNUNET_memcmp(&my_identity, &pos->pid))
302 return; /* This is myself, nothing to do. */
303 if (connection_count < target_connection_count)
307 if ((friend_count < minimum_friend_count) || (GNUNET_YES == friends_only))
310 strength += 10; /* urgently needed */
312 strength = 0; /* disallowed */
315 strength *= 2; /* friends always count more */
317 strength *= 2; /* existing connections preferred */
318 if (strength == pos->strength)
319 return; /* nothing to do */
322 GNUNET_ATS_connectivity_suggest_cancel(pos->sh);
325 pos->strength = strength;
328 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
329 "Asking to connect to `%s' with strength %u\n",
330 GNUNET_i2s(&pos->pid),
331 (unsigned int)strength);
332 GNUNET_STATISTICS_update(stats,
333 gettext_noop("# connect requests issued to ATS"),
336 pos->sh = GNUNET_ATS_connectivity_suggest(ats, &pos->pid, strength);
342 * Create a new entry in the peer list.
344 * @param peer identity of the new entry
345 * @param hello hello message, can be NULL
346 * @param is_friend is the new entry for a friend?
347 * @return the new entry
350 make_peer(const struct GNUNET_PeerIdentity *peer,
351 const struct GNUNET_HELLO_Message *hello,
356 ret = GNUNET_new(struct Peer);
358 ret->is_friend = is_friend;
361 ret->hello = GNUNET_malloc(GNUNET_HELLO_size(hello));
362 GNUNET_memcpy(ret->hello, hello, GNUNET_HELLO_size(hello));
364 GNUNET_break(GNUNET_OK ==
365 GNUNET_CONTAINER_multipeermap_put(
369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
375 * Setup bloom filter for the given peer entry.
377 * @param peer entry to initialize
380 setup_filter(struct Peer *peer)
382 struct GNUNET_HashCode hc;
384 /* 2^{-5} chance of not sending a HELLO to a peer is
385 * acceptably small (if the filter is 50% full);
386 * 64 bytes of memory are small compared to the rest
387 * of the data structure and would only really become
388 * "useless" once a HELLO has been passed on to ~100
389 * other peers, which is likely more than enough in
390 * any case; hence 64, 5 as bloomfilter parameters. */
391 peer->filter = GNUNET_CONTAINER_bloomfilter_init(NULL, 64, 5);
392 peer->filter_expiration =
393 GNUNET_TIME_relative_to_absolute(HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
394 /* never send a peer its own HELLO */
395 GNUNET_CRYPTO_hash(&peer->pid, sizeof(struct GNUNET_PeerIdentity), &hc);
396 GNUNET_CONTAINER_bloomfilter_add(peer->filter, &hc);
401 * Closure for #find_advertisable_hello().
403 struct FindAdvHelloContext {
405 * Peer we want to advertise to.
410 * Where to store the result (peer selected for advertising).
415 * Maximum HELLO size we can use right now.
419 struct GNUNET_TIME_Relative next_adv;
424 * Find a peer that would be reasonable for advertising.
427 * @param pid identity of a peer
428 * @param value 'struct Peer*' for the peer we are considering
429 * @return #GNUNET_YES (continue iteration)
432 find_advertisable_hello(void *cls,
433 const struct GNUNET_PeerIdentity *pid,
436 struct FindAdvHelloContext *fah = cls;
437 struct Peer *pos = value;
438 struct GNUNET_TIME_Relative rst_time;
439 struct GNUNET_HashCode hc;
442 if (pos == fah->peer)
444 if (pos->hello == NULL)
446 rst_time = GNUNET_TIME_absolute_get_remaining(pos->filter_expiration);
447 if (0 == rst_time.rel_value_us)
449 /* time to discard... */
450 GNUNET_CONTAINER_bloomfilter_free(pos->filter);
453 fah->next_adv = GNUNET_TIME_relative_min(rst_time, fah->next_adv);
454 hs = GNUNET_HELLO_size(pos->hello);
455 if (hs > fah->max_size)
457 GNUNET_CRYPTO_hash(&fah->peer->pid,
458 sizeof(struct GNUNET_PeerIdentity),
460 if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test(pos->filter, &hc))
467 * Calculate when we would like to send the next HELLO to this
468 * peer and ask for it.
470 * @param cls for which peer to schedule the HELLO
473 schedule_next_hello(void *cls)
475 struct Peer *pl = cls;
476 struct FindAdvHelloContext fah;
477 struct GNUNET_MQ_Envelope *env;
479 struct GNUNET_TIME_Relative delay;
480 struct GNUNET_HashCode hc;
482 pl->hello_delay_task = NULL;
483 GNUNET_assert(NULL != pl->mq);
484 /* find applicable HELLOs */
487 fah.max_size = GNUNET_MAX_MESSAGE_SIZE - 1;
488 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
489 GNUNET_CONTAINER_multipeermap_iterate(peers, &find_advertisable_hello, &fah);
490 pl->hello_delay_task =
491 GNUNET_SCHEDULER_add_delayed(fah.next_adv, &schedule_next_hello, pl);
492 if (NULL == fah.result)
494 delay = GNUNET_TIME_absolute_get_remaining(pl->next_hello_allowed);
495 if (0 != delay.rel_value_us)
498 want = GNUNET_HELLO_size(fah.result->hello);
499 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
500 "Sending HELLO with %u bytes",
502 env = GNUNET_MQ_msg_copy(&fah.result->hello->header);
503 GNUNET_MQ_send(pl->mq, env);
505 /* avoid sending this one again soon */
506 GNUNET_CRYPTO_hash(&pl->pid, sizeof(struct GNUNET_PeerIdentity), &hc);
507 GNUNET_CONTAINER_bloomfilter_add(fah.result->filter, &hc);
509 GNUNET_STATISTICS_update(stats,
510 gettext_noop("# HELLO messages gossipped"),
513 /* prepare to send the next one */
514 pl->next_hello_allowed =
515 GNUNET_TIME_relative_to_absolute(HELLO_ADVERTISEMENT_MIN_FREQUENCY);
516 if (NULL != pl->hello_delay_task)
517 GNUNET_SCHEDULER_cancel(pl->hello_delay_task);
518 pl->hello_delay_task = GNUNET_SCHEDULER_add_now(&schedule_next_hello, pl);
523 * Cancel existing requests for sending HELLOs to this peer
524 * and recalculate when we should send HELLOs to it based
525 * on our current state (something changed!).
527 * @param cls closure `struct Peer` to skip, or NULL
528 * @param pid identity of a peer
529 * @param value `struct Peer *` for the peer
530 * @return #GNUNET_YES (always)
533 reschedule_hellos(void *cls,
534 const struct GNUNET_PeerIdentity *pid,
537 struct Peer *peer = value;
538 struct Peer *skip = cls;
542 if (NULL == peer->mq)
544 if (NULL != peer->hello_delay_task)
546 GNUNET_SCHEDULER_cancel(peer->hello_delay_task);
547 peer->hello_delay_task = NULL;
549 peer->hello_delay_task =
550 GNUNET_SCHEDULER_add_now(&schedule_next_hello, peer);
556 * Method called whenever a peer connects.
559 * @param peer peer identity this notification is about
560 * @param mq message queue for communicating with @a peer
561 * @return our `struct Peer` for @a peer
564 connect_notify(void *cls,
565 const struct GNUNET_PeerIdentity *peer,
566 struct GNUNET_MQ_Handle *mq)
570 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
571 "Core told us that we are connecting to `%s'\n",
573 if (0 == GNUNET_memcmp(&my_identity, peer))
575 GNUNET_MQ_set_options(mq, GNUNET_MQ_PRIO_BEST_EFFORT);
577 GNUNET_STATISTICS_set(stats,
578 gettext_noop("# peers connected"),
581 pos = GNUNET_CONTAINER_multipeermap_get(peers, peer);
584 pos = make_peer(peer, NULL, GNUNET_NO);
588 GNUNET_assert(NULL == pos->mq);
594 if ((friend_count == minimum_friend_count) && (GNUNET_YES != friends_only))
596 GNUNET_STATISTICS_set(stats,
597 gettext_noop("# friends connected"),
601 reschedule_hellos(NULL, peer, pos);
607 * Try to add more peers to our connection set.
609 * @param cls closure, not used
610 * @param pid identity of a peer
611 * @param value `struct Peer *` for the peer
612 * @return #GNUNET_YES (continue to iterate)
615 try_add_peers(void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
617 struct Peer *pos = value;
619 attempt_connect(pos);
625 * Add peers and schedule connection attempt
627 * @param cls unused, NULL
630 add_peer_task(void *cls)
634 GNUNET_CONTAINER_multipeermap_iterate(peers, &try_add_peers, NULL);
639 * Method called whenever a peer disconnects.
642 * @param peer peer identity this notification is about
643 * @param internal_cls the `struct Peer` for this peer
646 disconnect_notify(void *cls,
647 const struct GNUNET_PeerIdentity *peer,
650 struct Peer *pos = internal_cls;
653 return; /* myself, we're shutting down */
654 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
655 "Core told us that we disconnected from `%s'\n",
664 if (NULL != pos->hello_delay_task)
666 GNUNET_SCHEDULER_cancel(pos->hello_delay_task);
667 pos->hello_delay_task = NULL;
669 GNUNET_STATISTICS_set(stats,
670 gettext_noop("# peers connected"),
676 GNUNET_STATISTICS_set(stats,
677 gettext_noop("# friends connected"),
681 if (((connection_count < target_connection_count) ||
682 (friend_count < minimum_friend_count)) &&
684 add_task = GNUNET_SCHEDULER_add_now(&add_peer_task, NULL);
685 if ((friend_count < minimum_friend_count) && (NULL == blacklist))
686 blacklist = GNUNET_TRANSPORT_blacklist(cfg, &blacklist_check, NULL);
691 * Iterator called on each address.
693 * @param cls flag that we will set if we see any addresses
694 * @param address the address of the peer
695 * @param expiration when will the given address expire
696 * @return #GNUNET_SYSERR always, to terminate iteration
699 address_iterator(void *cls,
700 const struct GNUNET_HELLO_Address *address,
701 struct GNUNET_TIME_Absolute expiration)
706 return GNUNET_SYSERR;
711 * We've gotten a HELLO from another peer. Consider it for
714 * @param hello the HELLO we got
717 consider_for_advertising(const struct GNUNET_HELLO_Message *hello)
720 struct GNUNET_PeerIdentity pid;
721 struct GNUNET_TIME_Absolute dt;
722 struct GNUNET_HELLO_Message *nh;
726 if (GNUNET_OK != GNUNET_HELLO_get_id(hello, &pid))
731 if (0 == GNUNET_memcmp(&pid, &my_identity))
732 return; /* that's me! */
733 have_address = GNUNET_NO;
734 GNUNET_HELLO_iterate_addresses(hello,
738 if (GNUNET_NO == have_address)
739 return; /* no point in advertising this one... */
740 peer = GNUNET_CONTAINER_multipeermap_get(peers, &pid);
743 peer = make_peer(&pid, hello, GNUNET_NO);
745 else if (NULL != peer->hello)
747 dt = GNUNET_HELLO_equals(peer->hello, hello, GNUNET_TIME_absolute_get());
748 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
749 return; /* nothing new here */
751 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
752 "Found HELLO from peer `%s' for advertising\n",
754 if (NULL != peer->hello)
756 nh = GNUNET_HELLO_merge(peer->hello, hello);
757 GNUNET_free(peer->hello);
762 size = GNUNET_HELLO_size(hello);
763 peer->hello = GNUNET_malloc(size);
764 GNUNET_memcpy(peer->hello, hello, size);
766 if (NULL != peer->filter)
768 GNUNET_CONTAINER_bloomfilter_free(peer->filter);
772 /* since we have a new HELLO to pick from, re-schedule all
773 * HELLO requests that are not bound by the HELLO send rate! */
774 GNUNET_CONTAINER_multipeermap_iterate(peers, &reschedule_hellos, peer);
779 * PEERINFO calls this function to let us know about a possible peer
780 * that we might want to connect to.
782 * @param cls closure (not used)
783 * @param peer potential peer to connect to
784 * @param hello HELLO for this peer (or NULL)
785 * @param err_msg NULL if successful, otherwise contains error message
788 process_peer(void *cls,
789 const struct GNUNET_PeerIdentity *peer,
790 const struct GNUNET_HELLO_Message *hello,
797 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
798 _("Error in communication with PEERINFO service: %s\n"),
800 GNUNET_PEERINFO_notify_cancel(peerinfo_notify);
802 GNUNET_PEERINFO_notify(cfg, GNUNET_NO, &process_peer, NULL);
805 GNUNET_assert(NULL != peer);
806 if (0 == GNUNET_memcmp(&my_identity, peer))
807 return; /* that's me! */
810 /* free existing HELLO, if any */
811 pos = GNUNET_CONTAINER_multipeermap_get(peers, peer);
814 GNUNET_free_non_null(pos->hello);
816 if (NULL != pos->filter)
818 GNUNET_CONTAINER_bloomfilter_free(pos->filter);
821 if ((NULL == pos->mq) && (GNUNET_NO == pos->is_friend))
822 free_peer(NULL, &pos->pid, pos);
826 consider_for_advertising(hello);
827 pos = GNUNET_CONTAINER_multipeermap_get(peers, peer);
829 pos = make_peer(peer, hello, GNUNET_NO);
830 attempt_connect(pos);
835 * Function called after #GNUNET_CORE_connect has succeeded
836 * (or failed for good).
839 * @param my_id ID of this peer, NULL if we failed
842 core_init(void *cls, const struct GNUNET_PeerIdentity *my_id)
847 GNUNET_ERROR_TYPE_ERROR,
848 _("Failed to connect to core service, can not manage topology!\n"));
849 GNUNET_SCHEDULER_shutdown();
852 my_identity = *my_id;
853 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I am peer `%s'\n", GNUNET_i2s(my_id));
855 GNUNET_PEERINFO_notify(cfg, GNUNET_NO, &process_peer, NULL);
860 * Process friend found in FRIENDS file.
862 * @param cls pointer to an `unsigned int` to be incremented per friend found
863 * @param pid identity of the friend
866 handle_friend(void *cls, const struct GNUNET_PeerIdentity *pid)
868 unsigned int *entries_found = cls;
871 if (0 == GNUNET_memcmp(pid, &my_identity))
873 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
874 _("Found myself `%s' in friend list (useless, ignored)\n"),
879 fl = make_peer(pid, NULL, GNUNET_YES);
880 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
881 _("Found friend `%s' in configuration\n"),
882 GNUNET_i2s(&fl->pid));
887 * Read the friends file.
890 read_friends_file(const struct GNUNET_CONFIGURATION_Handle *cfg)
892 unsigned int entries_found;
895 if (GNUNET_OK != GNUNET_FRIENDS_parse(cfg, &handle_friend, &entries_found))
897 if ((GNUNET_YES == friends_only) || (minimum_friend_count > 0))
898 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
899 _("Encountered errors parsing friends list!\n"));
901 GNUNET_STATISTICS_update(stats,
902 gettext_noop("# friends in configuration"),
905 if ((minimum_friend_count > entries_found) && (GNUNET_NO == friends_only))
908 GNUNET_ERROR_TYPE_WARNING,
910 "Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
912 if ((minimum_friend_count > target_connection_count) &&
913 (GNUNET_NO == friends_only))
916 GNUNET_ERROR_TYPE_WARNING,
918 "More friendly connections required than target total number of connections.\n"));
924 * This function is called whenever an encrypted HELLO message is
927 * @param cls closure with the peer identity of the sender
928 * @param message the actual HELLO message
929 * @return #GNUNET_OK if @a message is well-formed
930 * #GNUNET_SYSERR if @a message is invalid
933 check_hello(void *cls, const struct GNUNET_HELLO_Message *message)
935 struct GNUNET_PeerIdentity pid;
937 if (GNUNET_OK != GNUNET_HELLO_get_id(message, &pid))
940 return GNUNET_SYSERR;
947 * This function is called whenever an encrypted HELLO message is
950 * @param cls closure with the peer identity of the sender
951 * @param message the actual HELLO message
954 handle_hello(void *cls, const struct GNUNET_HELLO_Message *message)
956 const struct GNUNET_PeerIdentity *other = cls;
958 struct GNUNET_PeerIdentity pid;
960 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
961 "Received encrypted HELLO from peer `%s'",
963 GNUNET_assert(GNUNET_OK == GNUNET_HELLO_get_id(message, &pid));
964 GNUNET_STATISTICS_update(stats,
965 gettext_noop("# HELLO messages received"),
968 peer = GNUNET_CONTAINER_multipeermap_get(peers, &pid);
971 if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
976 if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
978 if ((GNUNET_YES != peer->is_friend) &&
979 (friend_count < minimum_friend_count))
982 (void)GNUNET_PEERINFO_add_peer(pi, message, NULL, NULL);
987 * Last task run during shutdown. Disconnects us from
988 * the transport and core.
990 * @param cls unused, NULL
993 cleaning_task(void *cls)
995 if (NULL != peerinfo_notify)
997 GNUNET_PEERINFO_notify_cancel(peerinfo_notify);
998 peerinfo_notify = NULL;
1002 GNUNET_CORE_disconnect(handle);
1006 if (NULL != add_task)
1008 GNUNET_SCHEDULER_cancel(add_task);
1013 GNUNET_TRANSPORT_offer_hello_cancel(oh);
1016 GNUNET_CONTAINER_multipeermap_iterate(peers, &free_peer, NULL);
1017 GNUNET_CONTAINER_multipeermap_destroy(peers);
1021 GNUNET_ATS_connectivity_done(ats);
1026 GNUNET_PEERINFO_disconnect(pi);
1031 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
1038 * Main function that will be run.
1040 * @param cls closure
1041 * @param args remaining command-line arguments
1042 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1043 * @param c configuration
1048 const char *cfgfile,
1049 const struct GNUNET_CONFIGURATION_Handle *c)
1051 struct GNUNET_MQ_MessageHandler handlers[] =
1052 { GNUNET_MQ_hd_var_size(hello,
1053 GNUNET_MESSAGE_TYPE_HELLO,
1054 struct GNUNET_HELLO_Message,
1056 GNUNET_MQ_handler_end() };
1057 unsigned long long opt;
1060 stats = GNUNET_STATISTICS_create("topology", cfg);
1062 GNUNET_CONFIGURATION_get_value_yesno(cfg, "TOPOLOGY", "FRIENDS-ONLY");
1063 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg,
1068 minimum_friend_count = (unsigned int)opt;
1070 GNUNET_CONFIGURATION_get_value_number(cfg,
1072 "TARGET-CONNECTION-COUNT",
1075 target_connection_count = (unsigned int)opt;
1076 peers = GNUNET_CONTAINER_multipeermap_create(target_connection_count * 2,
1078 read_friends_file(cfg);
1079 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1080 "Topology would like %u connections with at least %u friends\n",
1081 target_connection_count,
1082 minimum_friend_count);
1083 if ((GNUNET_YES == friends_only) || (minimum_friend_count > 0))
1084 blacklist = GNUNET_TRANSPORT_blacklist(cfg, &blacklist_check, NULL);
1085 ats = GNUNET_ATS_connectivity_init(cfg);
1086 pi = GNUNET_PEERINFO_connect(cfg);
1087 handle = GNUNET_CORE_connect(cfg,
1093 GNUNET_SCHEDULER_add_shutdown(&cleaning_task, NULL);
1096 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1097 _("Failed to connect to `%s' service.\n"),
1099 GNUNET_SCHEDULER_shutdown();
1106 * The main function for the topology daemon.
1108 * @param argc number of arguments from the command line
1109 * @param argv command line arguments
1110 * @return 0 ok, 1 on error
1113 main(int argc, char *const *argv)
1115 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1116 GNUNET_GETOPT_OPTION_END
1120 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
1123 ret = (GNUNET_OK == GNUNET_PROGRAM_run(argc,
1125 "gnunet-daemon-topology",
1126 _("GNUnet topology control"),
1132 GNUNET_free((void *)argv);
1137 #if defined(LINUX) && defined(__GLIBC__)
1141 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1143 void __attribute__ ((constructor)) GNUNET_ARM_memory_init()
1145 mallopt(M_TRIM_THRESHOLD, 4 * 1024);
1146 mallopt(M_TOP_PAD, 1 * 1024);
1151 /* end of gnunet-daemon-topology.c */