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
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 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 he 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 ATS service.
142 static struct GNUNET_ATS_ConnectivityHandle *ats;
145 * Identity of this peer.
147 static struct GNUNET_PeerIdentity my_identity;
150 * All of our friends, all of our current neighbours and all peers for
151 * which we have HELLOs. So pretty much everyone. Maps peer identities
152 * to `struct Peer *` values.
154 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
157 * Handle for reporting statistics.
159 static struct GNUNET_STATISTICS_Handle *stats;
162 * Blacklist (NULL if we have none).
164 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
167 * Task scheduled to asynchronously reconsider adding/removing
168 * peer connectivity suggestions.
170 static struct GNUNET_SCHEDULER_Task *add_task;
173 * Active HELLO offering to transport service.
175 static struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
178 * Flag to disallow non-friend connections (pure F2F mode).
180 static int friends_only;
183 * Minimum number of friends to have in the
184 * connection set before we allow non-friends.
186 static unsigned int minimum_friend_count;
189 * Number of peers (friends and others) that we are currently connected to.
191 static unsigned int connection_count;
194 * Target number of connections.
196 static unsigned int target_connection_count;
199 * Number of friends that we are currently connected to.
201 static unsigned int friend_count;
205 * Function that decides if a connection is acceptable or not.
206 * If we have a blacklist, only friends are allowed, so the check
210 * @param pid peer to approve or disapprove
211 * @return #GNUNET_OK if the connection is allowed
214 blacklist_check (void *cls,
215 const struct GNUNET_PeerIdentity *pid)
219 pos = GNUNET_CONTAINER_multipeermap_get (peers,
221 if ( (NULL != pos) &&
222 (GNUNET_YES == pos->is_friend))
224 GNUNET_STATISTICS_update (stats,
225 gettext_noop ("# peers blacklisted"),
228 return GNUNET_SYSERR;
233 * Whitelist all peers that we blacklisted; we've passed
234 * the minimum number of friends.
239 if (NULL != blacklist)
241 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
248 * Free all resources associated with the given peer.
250 * @param cls closure (not used)
251 * @param pid identity of the peer
252 * @param value peer to free
253 * @return #GNUNET_YES (always: continue to iterate)
256 free_peer (void *cls,
257 const struct GNUNET_PeerIdentity * pid,
260 struct Peer *pos = value;
262 GNUNET_break (NULL == pos->mq);
263 GNUNET_break (GNUNET_OK ==
264 GNUNET_CONTAINER_multipeermap_remove (peers,
267 if (NULL != pos->hello_delay_task)
269 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
270 pos->hello_delay_task = NULL;
274 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
277 if (NULL != pos->hello)
279 GNUNET_free_non_null (pos->hello);
282 if (NULL != pos->filter)
284 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
293 * Recalculate how much we want to be connected to the specified peer
294 * and let ATS know about the result.
296 * @param pos peer to consider connecting to
299 attempt_connect (struct Peer *pos)
304 memcmp (&my_identity,
306 sizeof (struct GNUNET_PeerIdentity)))
307 return; /* This is myself, nothing to do. */
308 if (connection_count < target_connection_count)
312 if ( (friend_count < minimum_friend_count) ||
313 (GNUNET_YES == friends_only) )
316 strength += 10; /* urgently needed */
318 strength = 0; /* disallowed */
321 strength *= 2; /* friends always count more */
323 strength *= 2; /* existing connections preferred */
324 if (strength == pos->strength)
325 return; /* nothing to do */
328 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
331 pos->strength = strength;
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Asking to connect to `%s' with strength %u\n",
336 GNUNET_i2s (&pos->pid),
337 (unsigned int) strength);
338 GNUNET_STATISTICS_update (stats,
339 gettext_noop ("# connect requests issued to ATS"),
342 pos->sh = GNUNET_ATS_connectivity_suggest (ats,
350 * Create a new entry in the peer list.
352 * @param peer identity of the new entry
353 * @param hello hello message, can be NULL
354 * @param is_friend is the new entry for a friend?
355 * @return the new entry
358 make_peer (const struct GNUNET_PeerIdentity *peer,
359 const struct GNUNET_HELLO_Message *hello,
364 ret = GNUNET_new (struct Peer);
366 ret->is_friend = is_friend;
369 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
370 GNUNET_memcpy (ret->hello,
372 GNUNET_HELLO_size (hello));
374 GNUNET_break (GNUNET_OK ==
375 GNUNET_CONTAINER_multipeermap_put (peers,
378 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
384 * Setup bloom filter for the given peer entry.
386 * @param peer entry to initialize
389 setup_filter (struct Peer *peer)
391 struct GNUNET_HashCode hc;
393 /* 2^{-5} chance of not sending a HELLO to a peer is
394 * acceptably small (if the filter is 50% full);
395 * 64 bytes of memory are small compared to the rest
396 * of the data structure and would only really become
397 * "useless" once a HELLO has been passed on to ~100
398 * other peers, which is likely more than enough in
399 * any case; hence 64, 5 as bloomfilter parameters. */
400 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
401 peer->filter_expiration =
402 GNUNET_TIME_relative_to_absolute
403 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
404 /* never send a peer its own HELLO */
405 GNUNET_CRYPTO_hash (&peer->pid,
406 sizeof (struct GNUNET_PeerIdentity),
408 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &hc);
413 * Closure for #find_advertisable_hello().
415 struct FindAdvHelloContext
419 * Peer we want to advertise to.
424 * Where to store the result (peer selected for advertising).
429 * Maximum HELLO size we can use right now.
433 struct GNUNET_TIME_Relative next_adv;
438 * Find a peer that would be reasonable for advertising.
441 * @param pid identity of a peer
442 * @param value 'struct Peer*' for the peer we are considering
443 * @return #GNUNET_YES (continue iteration)
446 find_advertisable_hello (void *cls,
447 const struct GNUNET_PeerIdentity *pid,
450 struct FindAdvHelloContext *fah = cls;
451 struct Peer *pos = value;
452 struct GNUNET_TIME_Relative rst_time;
453 struct GNUNET_HashCode hc;
456 if (pos == fah->peer)
458 if (pos->hello == NULL)
460 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
461 if (0 == rst_time.rel_value_us)
463 /* time to discard... */
464 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
467 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
468 hs = GNUNET_HELLO_size (pos->hello);
469 if (hs > fah->max_size)
471 GNUNET_CRYPTO_hash (&fah->peer->pid,
472 sizeof (struct GNUNET_PeerIdentity), &hc);
474 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
482 * Calculate when we would like to send the next HELLO to this
483 * peer and ask for it.
485 * @param cls for which peer to schedule the HELLO
488 schedule_next_hello (void *cls)
490 struct Peer *pl = cls;
491 struct FindAdvHelloContext fah;
492 struct GNUNET_MQ_Envelope *env;
494 struct GNUNET_TIME_Relative delay;
495 struct GNUNET_HashCode hc;
497 pl->hello_delay_task = NULL;
498 GNUNET_assert (NULL != pl->mq);
499 /* find applicable HELLOs */
502 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
503 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
504 GNUNET_CONTAINER_multipeermap_iterate (peers,
505 &find_advertisable_hello,
507 pl->hello_delay_task =
508 GNUNET_SCHEDULER_add_delayed (fah.next_adv,
509 &schedule_next_hello,
511 if (NULL == fah.result)
513 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
514 if (0 != delay.rel_value_us)
517 want = GNUNET_HELLO_size (fah.result->hello);
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Sending HELLO with %u bytes",
520 (unsigned int) want);
521 env = GNUNET_MQ_msg_copy (&fah.result->hello->header);
522 GNUNET_MQ_send (pl->mq,
525 /* avoid sending this one again soon */
526 GNUNET_CRYPTO_hash (&pl->pid,
527 sizeof (struct GNUNET_PeerIdentity),
529 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
532 GNUNET_STATISTICS_update (stats,
533 gettext_noop ("# HELLO messages gossipped"),
536 /* prepare to send the next one */
537 pl->next_hello_allowed
538 = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
539 if (NULL != pl->hello_delay_task)
540 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
542 = GNUNET_SCHEDULER_add_now (&schedule_next_hello,
548 * Cancel existing requests for sending HELLOs to this peer
549 * and recalculate when we should send HELLOs to it based
550 * on our current state (something changed!).
552 * @param cls closure `struct Peer` to skip, or NULL
553 * @param pid identity of a peer
554 * @param value `struct Peer *` for the peer
555 * @return #GNUNET_YES (always)
558 reschedule_hellos (void *cls,
559 const struct GNUNET_PeerIdentity *pid,
562 struct Peer *peer = value;
563 struct Peer *skip = cls;
567 if (NULL == peer->mq)
569 if (NULL != peer->hello_delay_task)
571 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
572 peer->hello_delay_task = NULL;
574 peer->hello_delay_task =
575 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
581 * Method called whenever a peer connects.
584 * @param peer peer identity this notification is about
585 * @param mq message queue for communicating with @a peer
586 * @return our `struct Peer` for @a peer
589 connect_notify (void *cls,
590 const struct GNUNET_PeerIdentity *peer,
591 struct GNUNET_MQ_Handle *mq)
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Core told us that we are connecting to `%s'\n",
600 if (0 == memcmp (&my_identity,
602 sizeof (struct GNUNET_PeerIdentity)))
604 extra = GNUNET_CORE_get_mq_options (GNUNET_YES,
605 GNUNET_CORE_PRIO_BEST_EFFORT,
607 GNUNET_MQ_set_options (mq,
611 GNUNET_STATISTICS_set (stats,
612 gettext_noop ("# peers connected"),
615 pos = GNUNET_CONTAINER_multipeermap_get (peers,
619 pos = make_peer (peer,
625 GNUNET_assert (NULL == pos->mq);
631 if ( (friend_count == minimum_friend_count) &&
632 (GNUNET_YES != friends_only) )
634 GNUNET_STATISTICS_set (stats,
635 gettext_noop ("# friends connected"),
639 reschedule_hellos (NULL,
647 * Try to add more peers to our connection set.
649 * @param cls closure, not used
650 * @param pid identity of a peer
651 * @param value `struct Peer *` for the peer
652 * @return #GNUNET_YES (continue to iterate)
655 try_add_peers (void *cls,
656 const struct GNUNET_PeerIdentity *pid,
659 struct Peer *pos = value;
661 attempt_connect (pos);
667 * Add peers and schedule connection attempt
669 * @param cls unused, NULL
672 add_peer_task (void *cls)
676 GNUNET_CONTAINER_multipeermap_iterate (peers,
683 * Method called whenever a peer disconnects.
686 * @param peer peer identity this notification is about
687 * @param internal_cls the `struct Peer` for this peer
690 disconnect_notify (void *cls,
691 const struct GNUNET_PeerIdentity *peer,
694 struct Peer *pos = internal_cls;
697 return; /* myself, we're shutting down */
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699 "Core told us that we disconnected from `%s'\n",
708 if (NULL != pos->hello_delay_task)
710 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
711 pos->hello_delay_task = NULL;
713 GNUNET_STATISTICS_set (stats,
714 gettext_noop ("# peers connected"),
720 GNUNET_STATISTICS_set (stats,
721 gettext_noop ("# friends connected"),
725 if ( ( (connection_count < target_connection_count) ||
726 (friend_count < minimum_friend_count)) &&
728 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task,
730 if ( (friend_count < minimum_friend_count) &&
732 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
739 * Iterator called on each address.
741 * @param cls flag that we will set if we see any addresses
742 * @param address the address of the peer
743 * @param expiration when will the given address expire
744 * @return #GNUNET_SYSERR always, to terminate iteration
747 address_iterator (void *cls,
748 const struct GNUNET_HELLO_Address *address,
749 struct GNUNET_TIME_Absolute expiration)
754 return GNUNET_SYSERR;
759 * We've gotten a HELLO from another peer. Consider it for
762 * @param hello the HELLO we got
765 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
768 struct GNUNET_PeerIdentity pid;
769 struct GNUNET_TIME_Absolute dt;
770 struct GNUNET_HELLO_Message *nh;
774 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
779 if (0 == memcmp (&pid,
781 sizeof (struct GNUNET_PeerIdentity)))
782 return; /* that's me! */
783 have_address = GNUNET_NO;
784 GNUNET_HELLO_iterate_addresses (hello,
788 if (GNUNET_NO == have_address)
789 return; /* no point in advertising this one... */
790 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
793 peer = make_peer (&pid,
797 else if (NULL != peer->hello)
799 dt = GNUNET_HELLO_equals (peer->hello,
801 GNUNET_TIME_absolute_get ());
802 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
803 return; /* nothing new here */
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "Found HELLO from peer `%s' for advertising\n",
808 if (NULL != peer->hello)
810 nh = GNUNET_HELLO_merge (peer->hello,
812 GNUNET_free (peer->hello);
817 size = GNUNET_HELLO_size (hello);
818 peer->hello = GNUNET_malloc (size);
819 GNUNET_memcpy (peer->hello,
823 if (NULL != peer->filter)
825 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
829 /* since we have a new HELLO to pick from, re-schedule all
830 * HELLO requests that are not bound by the HELLO send rate! */
831 GNUNET_CONTAINER_multipeermap_iterate (peers,
838 * PEERINFO calls this function to let us know about a possible peer
839 * that we might want to connect to.
841 * @param cls closure (not used)
842 * @param peer potential peer to connect to
843 * @param hello HELLO for this peer (or NULL)
844 * @param err_msg NULL if successful, otherwise contains error message
847 process_peer (void *cls,
848 const struct GNUNET_PeerIdentity *peer,
849 const struct GNUNET_HELLO_Message *hello,
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 _("Error in communication with PEERINFO service: %s\n"),
859 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
860 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
866 GNUNET_assert (NULL != peer);
867 if (0 == memcmp (&my_identity,
869 sizeof (struct GNUNET_PeerIdentity)))
870 return; /* that's me! */
873 /* free existing HELLO, if any */
874 pos = GNUNET_CONTAINER_multipeermap_get (peers,
878 GNUNET_free_non_null (pos->hello);
880 if (NULL != pos->filter)
882 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
885 if ( (NULL == pos->mq) &&
886 (GNUNET_NO == pos->is_friend) )
893 consider_for_advertising (hello);
894 pos = GNUNET_CONTAINER_multipeermap_get (peers,
897 pos = make_peer (peer,
900 attempt_connect (pos);
905 * Function called after #GNUNET_CORE_connect has succeeded
906 * (or failed for good).
909 * @param my_id ID of this peer, NULL if we failed
912 core_init (void *cls,
913 const struct GNUNET_PeerIdentity *my_id)
917 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
918 _("Failed to connect to core service, can not manage topology!\n"));
919 GNUNET_SCHEDULER_shutdown ();
922 my_identity = *my_id;
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
934 * Process friend found in FRIENDS file.
936 * @param cls pointer to an `unsigned int` to be incremented per friend found
937 * @param pid identity of the friend
940 handle_friend (void *cls,
941 const struct GNUNET_PeerIdentity *pid)
943 unsigned int *entries_found = cls;
946 if (0 == memcmp (pid,
948 sizeof (struct GNUNET_PeerIdentity)))
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 * Hello offer complete. Clean up.
1005 done_offer_hello (void *cls)
1012 * This function is called whenever an encrypted HELLO message is
1015 * @param cls closure with the peer identity of the sender
1016 * @param message the actual HELLO message
1017 * @return #GNUNET_OK if @a message is well-formed
1018 * #GNUNET_SYSERR if @a message is invalid
1021 check_hello (void *cls,
1022 const struct GNUNET_HELLO_Message *message)
1024 struct GNUNET_PeerIdentity pid;
1027 GNUNET_HELLO_get_id (message,
1030 GNUNET_break_op (0);
1031 return GNUNET_SYSERR;
1038 * This function is called whenever an encrypted HELLO message is
1041 * @param cls closure with the peer identity of the sender
1042 * @param message the actual HELLO message
1045 handle_hello (void *cls,
1046 const struct GNUNET_HELLO_Message *message)
1048 const struct GNUNET_PeerIdentity *other = cls;
1050 struct GNUNET_PeerIdentity pid;
1052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1053 "Received encrypted HELLO from peer `%s'",
1054 GNUNET_i2s (other));
1055 GNUNET_assert (GNUNET_OK ==
1056 GNUNET_HELLO_get_id (message,
1058 GNUNET_STATISTICS_update (stats,
1059 gettext_noop ("# HELLO messages received"),
1062 peer = GNUNET_CONTAINER_multipeermap_get (peers,
1066 if ( (GNUNET_YES == friends_only) ||
1067 (friend_count < minimum_friend_count) )
1072 if ( (GNUNET_YES != peer->is_friend) &&
1073 (GNUNET_YES == friends_only) )
1075 if ((GNUNET_YES != peer->is_friend) &&
1076 (friend_count < minimum_friend_count))
1080 GNUNET_TRANSPORT_offer_hello_cancel (oh);
1081 oh = GNUNET_TRANSPORT_offer_hello (cfg,
1089 * Last task run during shutdown. Disconnects us from
1090 * the transport and core.
1092 * @param cls unused, NULL
1095 cleaning_task (void *cls)
1097 if (NULL != peerinfo_notify)
1099 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1100 peerinfo_notify = NULL;
1104 GNUNET_CORE_disconnect (handle);
1108 if (NULL != add_task)
1110 GNUNET_SCHEDULER_cancel (add_task);
1115 GNUNET_TRANSPORT_offer_hello_cancel (oh);
1118 GNUNET_CONTAINER_multipeermap_iterate (peers,
1121 GNUNET_CONTAINER_multipeermap_destroy (peers);
1125 GNUNET_ATS_connectivity_done (ats);
1130 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1137 * Main function that will be run.
1139 * @param cls closure
1140 * @param args remaining command-line arguments
1141 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1142 * @param c configuration
1147 const char *cfgfile,
1148 const struct GNUNET_CONFIGURATION_Handle *c)
1150 struct GNUNET_MQ_MessageHandler handlers[] = {
1151 GNUNET_MQ_hd_var_size (hello,
1152 GNUNET_MESSAGE_TYPE_HELLO,
1153 struct GNUNET_HELLO_Message,
1155 GNUNET_MQ_handler_end ()
1157 unsigned long long opt;
1160 stats = GNUNET_STATISTICS_create ("topology", cfg);
1162 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1166 GNUNET_CONFIGURATION_get_value_number (cfg,
1171 minimum_friend_count = (unsigned int) opt;
1173 GNUNET_CONFIGURATION_get_value_number (cfg,
1175 "TARGET-CONNECTION-COUNT",
1178 target_connection_count = (unsigned int) opt;
1179 peers = GNUNET_CONTAINER_multipeermap_create (target_connection_count * 2,
1181 read_friends_file (cfg);
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "Topology would like %u connections with at least %u friends\n",
1184 target_connection_count,
1185 minimum_friend_count);
1186 if ( (friend_count < minimum_friend_count) &&
1187 (NULL == blacklist))
1188 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1191 ats = GNUNET_ATS_connectivity_init (cfg);
1192 handle = GNUNET_CORE_connect (cfg,
1198 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
1202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1203 _("Failed to connect to `%s' service.\n"),
1205 GNUNET_SCHEDULER_shutdown ();
1212 * The main function for the topology daemon.
1214 * @param argc number of arguments from the command line
1215 * @param argv command line arguments
1216 * @return 0 ok, 1 on error
1219 main (int argc, char *const *argv)
1221 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1222 GNUNET_GETOPT_OPTION_END
1226 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1231 GNUNET_PROGRAM_run (argc, argv,
1232 "gnunet-daemon-topology",
1233 _("GNUnet topology control"),
1234 options, &run, NULL)) ? 0 : 1;
1235 GNUNET_free ((void*) argv);
1240 #if defined(LINUX) && defined(__GLIBC__)
1244 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1246 void __attribute__ ((constructor))
1247 GNUNET_ARM_memory_init ()
1249 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1250 mallopt (M_TOP_PAD, 1 * 1024);
1255 /* end of gnunet-daemon-topology.c */