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 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 memcmp (&my_identity,
311 sizeof (struct GNUNET_PeerIdentity)))
312 return; /* This is myself, nothing to do. */
313 if (connection_count < target_connection_count)
317 if ( (friend_count < minimum_friend_count) ||
318 (GNUNET_YES == friends_only) )
321 strength += 10; /* urgently needed */
323 strength = 0; /* disallowed */
326 strength *= 2; /* friends always count more */
328 strength *= 2; /* existing connections preferred */
329 if (strength == pos->strength)
330 return; /* nothing to do */
333 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
336 pos->strength = strength;
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Asking to connect to `%s' with strength %u\n",
341 GNUNET_i2s (&pos->pid),
342 (unsigned int) strength);
343 GNUNET_STATISTICS_update (stats,
344 gettext_noop ("# connect requests issued to ATS"),
347 pos->sh = GNUNET_ATS_connectivity_suggest (ats,
355 * Create a new entry in the peer list.
357 * @param peer identity of the new entry
358 * @param hello hello message, can be NULL
359 * @param is_friend is the new entry for a friend?
360 * @return the new entry
363 make_peer (const struct GNUNET_PeerIdentity *peer,
364 const struct GNUNET_HELLO_Message *hello,
369 ret = GNUNET_new (struct Peer);
371 ret->is_friend = is_friend;
374 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
375 GNUNET_memcpy (ret->hello,
377 GNUNET_HELLO_size (hello));
379 GNUNET_break (GNUNET_OK ==
380 GNUNET_CONTAINER_multipeermap_put (peers,
383 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
389 * Setup bloom filter for the given peer entry.
391 * @param peer entry to initialize
394 setup_filter (struct Peer *peer)
396 struct GNUNET_HashCode hc;
398 /* 2^{-5} chance of not sending a HELLO to a peer is
399 * acceptably small (if the filter is 50% full);
400 * 64 bytes of memory are small compared to the rest
401 * of the data structure and would only really become
402 * "useless" once a HELLO has been passed on to ~100
403 * other peers, which is likely more than enough in
404 * any case; hence 64, 5 as bloomfilter parameters. */
405 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
406 peer->filter_expiration =
407 GNUNET_TIME_relative_to_absolute
408 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
409 /* never send a peer its own HELLO */
410 GNUNET_CRYPTO_hash (&peer->pid,
411 sizeof (struct GNUNET_PeerIdentity),
413 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &hc);
418 * Closure for #find_advertisable_hello().
420 struct FindAdvHelloContext
424 * Peer we want to advertise to.
429 * Where to store the result (peer selected for advertising).
434 * Maximum HELLO size we can use right now.
438 struct GNUNET_TIME_Relative next_adv;
443 * Find a peer that would be reasonable for advertising.
446 * @param pid identity of a peer
447 * @param value 'struct Peer*' for the peer we are considering
448 * @return #GNUNET_YES (continue iteration)
451 find_advertisable_hello (void *cls,
452 const struct GNUNET_PeerIdentity *pid,
455 struct FindAdvHelloContext *fah = cls;
456 struct Peer *pos = value;
457 struct GNUNET_TIME_Relative rst_time;
458 struct GNUNET_HashCode hc;
461 if (pos == fah->peer)
463 if (pos->hello == NULL)
465 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
466 if (0 == rst_time.rel_value_us)
468 /* time to discard... */
469 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
472 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
473 hs = GNUNET_HELLO_size (pos->hello);
474 if (hs > fah->max_size)
476 GNUNET_CRYPTO_hash (&fah->peer->pid,
477 sizeof (struct GNUNET_PeerIdentity), &hc);
479 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
487 * Calculate when we would like to send the next HELLO to this
488 * peer and ask for it.
490 * @param cls for which peer to schedule the HELLO
493 schedule_next_hello (void *cls)
495 struct Peer *pl = cls;
496 struct FindAdvHelloContext fah;
497 struct GNUNET_MQ_Envelope *env;
499 struct GNUNET_TIME_Relative delay;
500 struct GNUNET_HashCode hc;
502 pl->hello_delay_task = NULL;
503 GNUNET_assert (NULL != pl->mq);
504 /* find applicable HELLOs */
507 fah.max_size = GNUNET_MAX_MESSAGE_SIZE - 1;
508 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
509 GNUNET_CONTAINER_multipeermap_iterate (peers,
510 &find_advertisable_hello,
512 pl->hello_delay_task =
513 GNUNET_SCHEDULER_add_delayed (fah.next_adv,
514 &schedule_next_hello,
516 if (NULL == fah.result)
518 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
519 if (0 != delay.rel_value_us)
522 want = GNUNET_HELLO_size (fah.result->hello);
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "Sending HELLO with %u bytes",
525 (unsigned int) want);
526 env = GNUNET_MQ_msg_copy (&fah.result->hello->header);
527 GNUNET_MQ_send (pl->mq,
530 /* avoid sending this one again soon */
531 GNUNET_CRYPTO_hash (&pl->pid,
532 sizeof (struct GNUNET_PeerIdentity),
534 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
537 GNUNET_STATISTICS_update (stats,
538 gettext_noop ("# HELLO messages gossipped"),
541 /* prepare to send the next one */
542 pl->next_hello_allowed
543 = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
544 if (NULL != pl->hello_delay_task)
545 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
547 = GNUNET_SCHEDULER_add_now (&schedule_next_hello,
553 * Cancel existing requests for sending HELLOs to this peer
554 * and recalculate when we should send HELLOs to it based
555 * on our current state (something changed!).
557 * @param cls closure `struct Peer` to skip, or NULL
558 * @param pid identity of a peer
559 * @param value `struct Peer *` for the peer
560 * @return #GNUNET_YES (always)
563 reschedule_hellos (void *cls,
564 const struct GNUNET_PeerIdentity *pid,
567 struct Peer *peer = value;
568 struct Peer *skip = cls;
572 if (NULL == peer->mq)
574 if (NULL != peer->hello_delay_task)
576 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
577 peer->hello_delay_task = NULL;
579 peer->hello_delay_task =
580 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
586 * Method called whenever a peer connects.
589 * @param peer peer identity this notification is about
590 * @param mq message queue for communicating with @a peer
591 * @return our `struct Peer` for @a peer
594 connect_notify (void *cls,
595 const struct GNUNET_PeerIdentity *peer,
596 struct GNUNET_MQ_Handle *mq)
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603 "Core told us that we are connecting to `%s'\n",
605 if (0 == memcmp (&my_identity,
607 sizeof (struct GNUNET_PeerIdentity)))
609 extra = GNUNET_CORE_get_mq_options (GNUNET_YES,
610 GNUNET_CORE_PRIO_BEST_EFFORT,
612 GNUNET_MQ_set_options (mq,
616 GNUNET_STATISTICS_set (stats,
617 gettext_noop ("# peers connected"),
620 pos = GNUNET_CONTAINER_multipeermap_get (peers,
624 pos = make_peer (peer,
630 GNUNET_assert (NULL == pos->mq);
636 if ( (friend_count == minimum_friend_count) &&
637 (GNUNET_YES != friends_only) )
639 GNUNET_STATISTICS_set (stats,
640 gettext_noop ("# friends connected"),
644 reschedule_hellos (NULL,
652 * Try to add more peers to our connection set.
654 * @param cls closure, not used
655 * @param pid identity of a peer
656 * @param value `struct Peer *` for the peer
657 * @return #GNUNET_YES (continue to iterate)
660 try_add_peers (void *cls,
661 const struct GNUNET_PeerIdentity *pid,
664 struct Peer *pos = value;
666 attempt_connect (pos);
672 * Add peers and schedule connection attempt
674 * @param cls unused, NULL
677 add_peer_task (void *cls)
681 GNUNET_CONTAINER_multipeermap_iterate (peers,
688 * Method called whenever a peer disconnects.
691 * @param peer peer identity this notification is about
692 * @param internal_cls the `struct Peer` for this peer
695 disconnect_notify (void *cls,
696 const struct GNUNET_PeerIdentity *peer,
699 struct Peer *pos = internal_cls;
702 return; /* myself, we're shutting down */
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704 "Core told us that we disconnected from `%s'\n",
713 if (NULL != pos->hello_delay_task)
715 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
716 pos->hello_delay_task = NULL;
718 GNUNET_STATISTICS_set (stats,
719 gettext_noop ("# peers connected"),
725 GNUNET_STATISTICS_set (stats,
726 gettext_noop ("# friends connected"),
730 if ( ( (connection_count < target_connection_count) ||
731 (friend_count < minimum_friend_count)) &&
733 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task,
735 if ( (friend_count < minimum_friend_count) &&
737 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
744 * Iterator called on each address.
746 * @param cls flag that we will set if we see any addresses
747 * @param address the address of the peer
748 * @param expiration when will the given address expire
749 * @return #GNUNET_SYSERR always, to terminate iteration
752 address_iterator (void *cls,
753 const struct GNUNET_HELLO_Address *address,
754 struct GNUNET_TIME_Absolute expiration)
759 return GNUNET_SYSERR;
764 * We've gotten a HELLO from another peer. Consider it for
767 * @param hello the HELLO we got
770 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
773 struct GNUNET_PeerIdentity pid;
774 struct GNUNET_TIME_Absolute dt;
775 struct GNUNET_HELLO_Message *nh;
779 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
784 if (0 == memcmp (&pid,
786 sizeof (struct GNUNET_PeerIdentity)))
787 return; /* that's me! */
788 have_address = GNUNET_NO;
789 GNUNET_HELLO_iterate_addresses (hello,
793 if (GNUNET_NO == have_address)
794 return; /* no point in advertising this one... */
795 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
798 peer = make_peer (&pid,
802 else if (NULL != peer->hello)
804 dt = GNUNET_HELLO_equals (peer->hello,
806 GNUNET_TIME_absolute_get ());
807 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
808 return; /* nothing new here */
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "Found HELLO from peer `%s' for advertising\n",
813 if (NULL != peer->hello)
815 nh = GNUNET_HELLO_merge (peer->hello,
817 GNUNET_free (peer->hello);
822 size = GNUNET_HELLO_size (hello);
823 peer->hello = GNUNET_malloc (size);
824 GNUNET_memcpy (peer->hello,
828 if (NULL != peer->filter)
830 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
834 /* since we have a new HELLO to pick from, re-schedule all
835 * HELLO requests that are not bound by the HELLO send rate! */
836 GNUNET_CONTAINER_multipeermap_iterate (peers,
843 * PEERINFO calls this function to let us know about a possible peer
844 * that we might want to connect to.
846 * @param cls closure (not used)
847 * @param peer potential peer to connect to
848 * @param hello HELLO for this peer (or NULL)
849 * @param err_msg NULL if successful, otherwise contains error message
852 process_peer (void *cls,
853 const struct GNUNET_PeerIdentity *peer,
854 const struct GNUNET_HELLO_Message *hello,
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 _("Error in communication with PEERINFO service: %s\n"),
864 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
865 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
871 GNUNET_assert (NULL != peer);
872 if (0 == memcmp (&my_identity,
874 sizeof (struct GNUNET_PeerIdentity)))
875 return; /* that's me! */
878 /* free existing HELLO, if any */
879 pos = GNUNET_CONTAINER_multipeermap_get (peers,
883 GNUNET_free_non_null (pos->hello);
885 if (NULL != pos->filter)
887 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
890 if ( (NULL == pos->mq) &&
891 (GNUNET_NO == pos->is_friend) )
898 consider_for_advertising (hello);
899 pos = GNUNET_CONTAINER_multipeermap_get (peers,
902 pos = make_peer (peer,
905 attempt_connect (pos);
910 * Function called after #GNUNET_CORE_connect has succeeded
911 * (or failed for good).
914 * @param my_id ID of this peer, NULL if we failed
917 core_init (void *cls,
918 const struct GNUNET_PeerIdentity *my_id)
922 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
923 _("Failed to connect to core service, can not manage topology!\n"));
924 GNUNET_SCHEDULER_shutdown ();
927 my_identity = *my_id;
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
939 * Process friend found in FRIENDS file.
941 * @param cls pointer to an `unsigned int` to be incremented per friend found
942 * @param pid identity of the friend
945 handle_friend (void *cls,
946 const struct GNUNET_PeerIdentity *pid)
948 unsigned int *entries_found = cls;
951 if (0 == memcmp (pid,
953 sizeof (struct GNUNET_PeerIdentity)))
955 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
956 _("Found myself `%s' in friend list (useless, ignored)\n"),
961 fl = make_peer (pid, NULL, GNUNET_YES);
962 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
963 _("Found friend `%s' in configuration\n"),
964 GNUNET_i2s (&fl->pid));
969 * Read the friends file.
972 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
974 unsigned int entries_found;
978 GNUNET_FRIENDS_parse (cfg,
982 if ( (GNUNET_YES == friends_only) ||
983 (minimum_friend_count > 0))
984 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
985 _("Encountered errors parsing friends list!\n"));
987 GNUNET_STATISTICS_update (stats,
988 gettext_noop ("# friends in configuration"),
991 if ( (minimum_friend_count > entries_found) &&
992 (GNUNET_NO == friends_only) )
994 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
995 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
997 if ( (minimum_friend_count > target_connection_count) &&
998 (GNUNET_NO == friends_only))
1000 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1001 _("More friendly connections required than target total number of connections.\n"));
1007 * This function is called whenever an encrypted HELLO message is
1010 * @param cls closure with the peer identity of the sender
1011 * @param message the actual HELLO message
1012 * @return #GNUNET_OK if @a message is well-formed
1013 * #GNUNET_SYSERR if @a message is invalid
1016 check_hello (void *cls,
1017 const struct GNUNET_HELLO_Message *message)
1019 struct GNUNET_PeerIdentity pid;
1022 GNUNET_HELLO_get_id (message,
1025 GNUNET_break_op (0);
1026 return GNUNET_SYSERR;
1033 * This function is called whenever an encrypted HELLO message is
1036 * @param cls closure with the peer identity of the sender
1037 * @param message the actual HELLO message
1040 handle_hello (void *cls,
1041 const struct GNUNET_HELLO_Message *message)
1043 const struct GNUNET_PeerIdentity *other = cls;
1045 struct GNUNET_PeerIdentity pid;
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Received encrypted HELLO from peer `%s'",
1049 GNUNET_i2s (other));
1050 GNUNET_assert (GNUNET_OK ==
1051 GNUNET_HELLO_get_id (message,
1053 GNUNET_STATISTICS_update (stats,
1054 gettext_noop ("# HELLO messages received"),
1057 peer = GNUNET_CONTAINER_multipeermap_get (peers,
1061 if ( (GNUNET_YES == friends_only) ||
1062 (friend_count < minimum_friend_count) )
1067 if ( (GNUNET_YES != peer->is_friend) &&
1068 (GNUNET_YES == friends_only) )
1070 if ((GNUNET_YES != peer->is_friend) &&
1071 (friend_count < minimum_friend_count))
1074 (void) GNUNET_PEERINFO_add_peer (pi,
1082 * Last task run during shutdown. Disconnects us from
1083 * the transport and core.
1085 * @param cls unused, NULL
1088 cleaning_task (void *cls)
1090 if (NULL != peerinfo_notify)
1092 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1093 peerinfo_notify = NULL;
1097 GNUNET_CORE_disconnect (handle);
1101 if (NULL != add_task)
1103 GNUNET_SCHEDULER_cancel (add_task);
1108 GNUNET_TRANSPORT_offer_hello_cancel (oh);
1111 GNUNET_CONTAINER_multipeermap_iterate (peers,
1114 GNUNET_CONTAINER_multipeermap_destroy (peers);
1118 GNUNET_ATS_connectivity_done (ats);
1123 GNUNET_PEERINFO_disconnect (pi);
1128 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1135 * Main function that will be run.
1137 * @param cls closure
1138 * @param args remaining command-line arguments
1139 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1140 * @param c configuration
1145 const char *cfgfile,
1146 const struct GNUNET_CONFIGURATION_Handle *c)
1148 struct GNUNET_MQ_MessageHandler handlers[] = {
1149 GNUNET_MQ_hd_var_size (hello,
1150 GNUNET_MESSAGE_TYPE_HELLO,
1151 struct GNUNET_HELLO_Message,
1153 GNUNET_MQ_handler_end ()
1155 unsigned long long opt;
1158 stats = GNUNET_STATISTICS_create ("topology", cfg);
1160 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1164 GNUNET_CONFIGURATION_get_value_number (cfg,
1169 minimum_friend_count = (unsigned int) opt;
1171 GNUNET_CONFIGURATION_get_value_number (cfg,
1173 "TARGET-CONNECTION-COUNT",
1176 target_connection_count = (unsigned int) opt;
1177 peers = GNUNET_CONTAINER_multipeermap_create (target_connection_count * 2,
1179 read_friends_file (cfg);
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "Topology would like %u connections with at least %u friends\n",
1182 target_connection_count,
1183 minimum_friend_count);
1184 if ( (GNUNET_YES == friends_only) ||
1185 (minimum_friend_count > 0))
1186 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1189 ats = GNUNET_ATS_connectivity_init (cfg);
1190 pi = GNUNET_PEERINFO_connect (cfg);
1191 handle = GNUNET_CORE_connect (cfg,
1197 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
1201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1202 _("Failed to connect to `%s' service.\n"),
1204 GNUNET_SCHEDULER_shutdown ();
1211 * The main function for the topology daemon.
1213 * @param argc number of arguments from the command line
1214 * @param argv command line arguments
1215 * @return 0 ok, 1 on error
1218 main (int argc, char *const *argv)
1220 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1221 GNUNET_GETOPT_OPTION_END
1225 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1230 GNUNET_PROGRAM_run (argc, argv,
1231 "gnunet-daemon-topology",
1232 _("GNUnet topology control"),
1233 options, &run, NULL)) ? 0 : 1;
1234 GNUNET_free ((void*) argv);
1239 #if defined(LINUX) && defined(__GLIBC__)
1243 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1245 void __attribute__ ((constructor))
1246 GNUNET_ARM_memory_init ()
1248 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1249 mallopt (M_TOP_PAD, 1 * 1024);
1254 /* end of gnunet-daemon-topology.c */