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/>.
20 * @file topology/gnunet-daemon-topology.c
21 * @brief code for maintaining the overlay topology
22 * @author Christian Grothoff
24 * This daemon combines three functions:
25 * - suggesting to ATS which peers we might want to connect to
26 * - enforcing the F2F restrictions (by blacklisting)
29 * All three require similar information (who are our friends
30 * impacts connectivity suggestions; connectivity suggestions
31 * should consider blacklisting; connectivity suggestions
32 * should consider available/known HELLOs; gossip requires
33 * connectivity data; connectivity suggestions require
34 * connectivity data), which is why they are combined in this
38 #include "gnunet_util_lib.h"
39 #include "gnunet_friends_lib.h"
40 #include "gnunet_constants.h"
41 #include "gnunet_core_service.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_peerinfo_service.h"
44 #include "gnunet_statistics_service.h"
45 #include "gnunet_transport_service.h"
46 #include "gnunet_ats_service.h"
50 * At what frequency do we sent HELLOs to a peer?
52 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
55 * After what time period do we expire the HELLO Bloom filter?
57 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
61 * Record for neighbours, friends and blacklisted peers.
66 * Which peer is this entry about?
68 struct GNUNET_PeerIdentity pid;
71 * Our handle for transmitting to this peer; NULL
72 * if peer is not connected.
74 struct GNUNET_MQ_Handle *mq;
77 * Pointer to the HELLO message of this peer; can be NULL.
79 struct GNUNET_HELLO_Message *hello;
82 * Bloom filter used to mark which peers already got the HELLO
85 struct GNUNET_CONTAINER_BloomFilter *filter;
88 * Next time we are allowed to transmit a HELLO to this peer?
90 struct GNUNET_TIME_Absolute next_hello_allowed;
93 * When should we reset the bloom filter of this entry?
95 struct GNUNET_TIME_Absolute filter_expiration;
98 * ID of task we use to wait for the time to send the next HELLO
101 struct GNUNET_SCHEDULER_Task *hello_delay_task;
104 * Handle for our connectivity suggestion for this peer.
106 struct GNUNET_ATS_ConnectivitySuggestHandle *sh;
109 * How much would we like to connect to this peer?
114 * Is this peer listed here because it is a friend?
122 * Our peerinfo notification context. We use notification
123 * to instantly learn about new peers as they are discovered.
125 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
130 static const struct GNUNET_CONFIGURATION_Handle *cfg;
133 * Handle to the CORE service.
135 static struct GNUNET_CORE_Handle *handle;
138 * Handle to the PEERINFO service.
140 static struct GNUNET_PEERINFO_Handle *pi;
143 * Handle to the ATS service.
145 static struct GNUNET_ATS_ConnectivityHandle *ats;
148 * Identity of this peer.
150 static struct GNUNET_PeerIdentity my_identity;
153 * All of our friends, all of our current neighbours and all peers for
154 * which we have HELLOs. So pretty much everyone. Maps peer identities
155 * to `struct Peer *` values.
157 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
160 * Handle for reporting statistics.
162 static struct GNUNET_STATISTICS_Handle *stats;
165 * Blacklist (NULL if we have none).
167 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
170 * Task scheduled to asynchronously reconsider adding/removing
171 * peer connectivity suggestions.
173 static struct GNUNET_SCHEDULER_Task *add_task;
176 * Active HELLO offering to transport service.
178 static struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
181 * Flag to disallow non-friend connections (pure F2F mode).
183 static int friends_only;
186 * Minimum number of friends to have in the
187 * connection set before we allow non-friends.
189 static unsigned int minimum_friend_count;
192 * Number of peers (friends and others) that we are currently connected to.
194 static unsigned int connection_count;
197 * Target number of connections.
199 static unsigned int target_connection_count;
202 * Number of friends that we are currently connected to.
204 static unsigned int friend_count;
208 * Function that decides if a connection is acceptable or not.
209 * If we have a blacklist, only friends are allowed, so the check
213 * @param pid peer to approve or disapprove
214 * @return #GNUNET_OK if the connection is allowed
217 blacklist_check (void *cls,
218 const struct GNUNET_PeerIdentity *pid)
222 pos = GNUNET_CONTAINER_multipeermap_get (peers,
224 if ( (NULL != pos) &&
225 (GNUNET_YES == pos->is_friend))
227 GNUNET_STATISTICS_update (stats,
228 gettext_noop ("# peers blacklisted"),
231 return GNUNET_SYSERR;
236 * Whitelist all peers that we blacklisted; we've passed
237 * the minimum number of friends.
242 if (NULL != blacklist)
244 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
251 * Free all resources associated with the given peer.
253 * @param cls closure (not used)
254 * @param pid identity of the peer
255 * @param value peer to free
256 * @return #GNUNET_YES (always: continue to iterate)
259 free_peer (void *cls,
260 const struct GNUNET_PeerIdentity * pid,
263 struct Peer *pos = value;
265 GNUNET_break (NULL == pos->mq);
266 GNUNET_break (GNUNET_OK ==
267 GNUNET_CONTAINER_multipeermap_remove (peers,
270 if (NULL != pos->hello_delay_task)
272 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
273 pos->hello_delay_task = NULL;
277 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
280 if (NULL != pos->hello)
282 GNUNET_free_non_null (pos->hello);
285 if (NULL != pos->filter)
287 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
296 * Recalculate how much we want to be connected to the specified peer
297 * and let ATS know about the result.
299 * @param pos peer to consider connecting to
302 attempt_connect (struct Peer *pos)
307 memcmp (&my_identity,
309 sizeof (struct GNUNET_PeerIdentity)))
310 return; /* This is myself, nothing to do. */
311 if (connection_count < target_connection_count)
315 if ( (friend_count < minimum_friend_count) ||
316 (GNUNET_YES == friends_only) )
319 strength += 10; /* urgently needed */
321 strength = 0; /* disallowed */
324 strength *= 2; /* friends always count more */
326 strength *= 2; /* existing connections preferred */
327 if (strength == pos->strength)
328 return; /* nothing to do */
331 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
334 pos->strength = strength;
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Asking to connect to `%s' with strength %u\n",
339 GNUNET_i2s (&pos->pid),
340 (unsigned int) strength);
341 GNUNET_STATISTICS_update (stats,
342 gettext_noop ("# connect requests issued to ATS"),
345 pos->sh = GNUNET_ATS_connectivity_suggest (ats,
353 * Create a new entry in the peer list.
355 * @param peer identity of the new entry
356 * @param hello hello message, can be NULL
357 * @param is_friend is the new entry for a friend?
358 * @return the new entry
361 make_peer (const struct GNUNET_PeerIdentity *peer,
362 const struct GNUNET_HELLO_Message *hello,
367 ret = GNUNET_new (struct Peer);
369 ret->is_friend = is_friend;
372 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
373 GNUNET_memcpy (ret->hello,
375 GNUNET_HELLO_size (hello));
377 GNUNET_break (GNUNET_OK ==
378 GNUNET_CONTAINER_multipeermap_put (peers,
381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
387 * Setup bloom filter for the given peer entry.
389 * @param peer entry to initialize
392 setup_filter (struct Peer *peer)
394 struct GNUNET_HashCode hc;
396 /* 2^{-5} chance of not sending a HELLO to a peer is
397 * acceptably small (if the filter is 50% full);
398 * 64 bytes of memory are small compared to the rest
399 * of the data structure and would only really become
400 * "useless" once a HELLO has been passed on to ~100
401 * other peers, which is likely more than enough in
402 * any case; hence 64, 5 as bloomfilter parameters. */
403 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
404 peer->filter_expiration =
405 GNUNET_TIME_relative_to_absolute
406 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
407 /* never send a peer its own HELLO */
408 GNUNET_CRYPTO_hash (&peer->pid,
409 sizeof (struct GNUNET_PeerIdentity),
411 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &hc);
416 * Closure for #find_advertisable_hello().
418 struct FindAdvHelloContext
422 * Peer we want to advertise to.
427 * Where to store the result (peer selected for advertising).
432 * Maximum HELLO size we can use right now.
436 struct GNUNET_TIME_Relative next_adv;
441 * Find a peer that would be reasonable for advertising.
444 * @param pid identity of a peer
445 * @param value 'struct Peer*' for the peer we are considering
446 * @return #GNUNET_YES (continue iteration)
449 find_advertisable_hello (void *cls,
450 const struct GNUNET_PeerIdentity *pid,
453 struct FindAdvHelloContext *fah = cls;
454 struct Peer *pos = value;
455 struct GNUNET_TIME_Relative rst_time;
456 struct GNUNET_HashCode hc;
459 if (pos == fah->peer)
461 if (pos->hello == NULL)
463 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
464 if (0 == rst_time.rel_value_us)
466 /* time to discard... */
467 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
470 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
471 hs = GNUNET_HELLO_size (pos->hello);
472 if (hs > fah->max_size)
474 GNUNET_CRYPTO_hash (&fah->peer->pid,
475 sizeof (struct GNUNET_PeerIdentity), &hc);
477 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
485 * Calculate when we would like to send the next HELLO to this
486 * peer and ask for it.
488 * @param cls for which peer to schedule the HELLO
491 schedule_next_hello (void *cls)
493 struct Peer *pl = cls;
494 struct FindAdvHelloContext fah;
495 struct GNUNET_MQ_Envelope *env;
497 struct GNUNET_TIME_Relative delay;
498 struct GNUNET_HashCode hc;
500 pl->hello_delay_task = NULL;
501 GNUNET_assert (NULL != pl->mq);
502 /* find applicable HELLOs */
505 fah.max_size = GNUNET_MAX_MESSAGE_SIZE - 1;
506 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
507 GNUNET_CONTAINER_multipeermap_iterate (peers,
508 &find_advertisable_hello,
510 pl->hello_delay_task =
511 GNUNET_SCHEDULER_add_delayed (fah.next_adv,
512 &schedule_next_hello,
514 if (NULL == fah.result)
516 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
517 if (0 != delay.rel_value_us)
520 want = GNUNET_HELLO_size (fah.result->hello);
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "Sending HELLO with %u bytes",
523 (unsigned int) want);
524 env = GNUNET_MQ_msg_copy (&fah.result->hello->header);
525 GNUNET_MQ_send (pl->mq,
528 /* avoid sending this one again soon */
529 GNUNET_CRYPTO_hash (&pl->pid,
530 sizeof (struct GNUNET_PeerIdentity),
532 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
535 GNUNET_STATISTICS_update (stats,
536 gettext_noop ("# HELLO messages gossipped"),
539 /* prepare to send the next one */
540 pl->next_hello_allowed
541 = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
542 if (NULL != pl->hello_delay_task)
543 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
545 = GNUNET_SCHEDULER_add_now (&schedule_next_hello,
551 * Cancel existing requests for sending HELLOs to this peer
552 * and recalculate when we should send HELLOs to it based
553 * on our current state (something changed!).
555 * @param cls closure `struct Peer` to skip, or NULL
556 * @param pid identity of a peer
557 * @param value `struct Peer *` for the peer
558 * @return #GNUNET_YES (always)
561 reschedule_hellos (void *cls,
562 const struct GNUNET_PeerIdentity *pid,
565 struct Peer *peer = value;
566 struct Peer *skip = cls;
570 if (NULL == peer->mq)
572 if (NULL != peer->hello_delay_task)
574 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
575 peer->hello_delay_task = NULL;
577 peer->hello_delay_task =
578 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
584 * Method called whenever a peer connects.
587 * @param peer peer identity this notification is about
588 * @param mq message queue for communicating with @a peer
589 * @return our `struct Peer` for @a peer
592 connect_notify (void *cls,
593 const struct GNUNET_PeerIdentity *peer,
594 struct GNUNET_MQ_Handle *mq)
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "Core told us that we are connecting to `%s'\n",
603 if (0 == memcmp (&my_identity,
605 sizeof (struct GNUNET_PeerIdentity)))
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 == memcmp (&pid,
784 sizeof (struct GNUNET_PeerIdentity)))
785 return; /* that's me! */
786 have_address = GNUNET_NO;
787 GNUNET_HELLO_iterate_addresses (hello,
791 if (GNUNET_NO == have_address)
792 return; /* no point in advertising this one... */
793 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
796 peer = make_peer (&pid,
800 else if (NULL != peer->hello)
802 dt = GNUNET_HELLO_equals (peer->hello,
804 GNUNET_TIME_absolute_get ());
805 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
806 return; /* nothing new here */
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Found HELLO from peer `%s' for advertising\n",
811 if (NULL != peer->hello)
813 nh = GNUNET_HELLO_merge (peer->hello,
815 GNUNET_free (peer->hello);
820 size = GNUNET_HELLO_size (hello);
821 peer->hello = GNUNET_malloc (size);
822 GNUNET_memcpy (peer->hello,
826 if (NULL != peer->filter)
828 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
832 /* since we have a new HELLO to pick from, re-schedule all
833 * HELLO requests that are not bound by the HELLO send rate! */
834 GNUNET_CONTAINER_multipeermap_iterate (peers,
841 * PEERINFO calls this function to let us know about a possible peer
842 * that we might want to connect to.
844 * @param cls closure (not used)
845 * @param peer potential peer to connect to
846 * @param hello HELLO for this peer (or NULL)
847 * @param err_msg NULL if successful, otherwise contains error message
850 process_peer (void *cls,
851 const struct GNUNET_PeerIdentity *peer,
852 const struct GNUNET_HELLO_Message *hello,
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 _("Error in communication with PEERINFO service: %s\n"),
862 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
863 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
869 GNUNET_assert (NULL != peer);
870 if (0 == memcmp (&my_identity,
872 sizeof (struct GNUNET_PeerIdentity)))
873 return; /* that's me! */
876 /* free existing HELLO, if any */
877 pos = GNUNET_CONTAINER_multipeermap_get (peers,
881 GNUNET_free_non_null (pos->hello);
883 if (NULL != pos->filter)
885 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
888 if ( (NULL == pos->mq) &&
889 (GNUNET_NO == pos->is_friend) )
896 consider_for_advertising (hello);
897 pos = GNUNET_CONTAINER_multipeermap_get (peers,
900 pos = make_peer (peer,
903 attempt_connect (pos);
908 * Function called after #GNUNET_CORE_connect has succeeded
909 * (or failed for good).
912 * @param my_id ID of this peer, NULL if we failed
915 core_init (void *cls,
916 const struct GNUNET_PeerIdentity *my_id)
920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
921 _("Failed to connect to core service, can not manage topology!\n"));
922 GNUNET_SCHEDULER_shutdown ();
925 my_identity = *my_id;
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
937 * Process friend found in FRIENDS file.
939 * @param cls pointer to an `unsigned int` to be incremented per friend found
940 * @param pid identity of the friend
943 handle_friend (void *cls,
944 const struct GNUNET_PeerIdentity *pid)
946 unsigned int *entries_found = cls;
949 if (0 == memcmp (pid,
951 sizeof (struct GNUNET_PeerIdentity)))
953 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
954 _("Found myself `%s' in friend list (useless, ignored)\n"),
959 fl = make_peer (pid, NULL, GNUNET_YES);
960 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
961 _("Found friend `%s' in configuration\n"),
962 GNUNET_i2s (&fl->pid));
967 * Read the friends file.
970 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
972 unsigned int entries_found;
976 GNUNET_FRIENDS_parse (cfg,
980 if ( (GNUNET_YES == friends_only) ||
981 (minimum_friend_count > 0))
982 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
983 _("Encountered errors parsing friends list!\n"));
985 GNUNET_STATISTICS_update (stats,
986 gettext_noop ("# friends in configuration"),
989 if ( (minimum_friend_count > entries_found) &&
990 (GNUNET_NO == friends_only) )
992 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
993 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
995 if ( (minimum_friend_count > target_connection_count) &&
996 (GNUNET_NO == friends_only))
998 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
999 _("More friendly connections required than target total number of connections.\n"));
1005 * This function is called whenever an encrypted HELLO message is
1008 * @param cls closure with the peer identity of the sender
1009 * @param message the actual HELLO message
1010 * @return #GNUNET_OK if @a message is well-formed
1011 * #GNUNET_SYSERR if @a message is invalid
1014 check_hello (void *cls,
1015 const struct GNUNET_HELLO_Message *message)
1017 struct GNUNET_PeerIdentity pid;
1020 GNUNET_HELLO_get_id (message,
1023 GNUNET_break_op (0);
1024 return GNUNET_SYSERR;
1031 * This function is called whenever an encrypted HELLO message is
1034 * @param cls closure with the peer identity of the sender
1035 * @param message the actual HELLO message
1038 handle_hello (void *cls,
1039 const struct GNUNET_HELLO_Message *message)
1041 const struct GNUNET_PeerIdentity *other = cls;
1043 struct GNUNET_PeerIdentity pid;
1045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1046 "Received encrypted HELLO from peer `%s'",
1047 GNUNET_i2s (other));
1048 GNUNET_assert (GNUNET_OK ==
1049 GNUNET_HELLO_get_id (message,
1051 GNUNET_STATISTICS_update (stats,
1052 gettext_noop ("# HELLO messages received"),
1055 peer = GNUNET_CONTAINER_multipeermap_get (peers,
1059 if ( (GNUNET_YES == friends_only) ||
1060 (friend_count < minimum_friend_count) )
1065 if ( (GNUNET_YES != peer->is_friend) &&
1066 (GNUNET_YES == friends_only) )
1068 if ((GNUNET_YES != peer->is_friend) &&
1069 (friend_count < minimum_friend_count))
1072 (void) GNUNET_PEERINFO_add_peer (pi,
1080 * Last task run during shutdown. Disconnects us from
1081 * the transport and core.
1083 * @param cls unused, NULL
1086 cleaning_task (void *cls)
1088 if (NULL != peerinfo_notify)
1090 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1091 peerinfo_notify = NULL;
1095 GNUNET_CORE_disconnect (handle);
1099 if (NULL != add_task)
1101 GNUNET_SCHEDULER_cancel (add_task);
1106 GNUNET_TRANSPORT_offer_hello_cancel (oh);
1109 GNUNET_CONTAINER_multipeermap_iterate (peers,
1112 GNUNET_CONTAINER_multipeermap_destroy (peers);
1116 GNUNET_ATS_connectivity_done (ats);
1121 GNUNET_PEERINFO_disconnect (pi);
1126 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1133 * Main function that will be run.
1135 * @param cls closure
1136 * @param args remaining command-line arguments
1137 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1138 * @param c configuration
1143 const char *cfgfile,
1144 const struct GNUNET_CONFIGURATION_Handle *c)
1146 struct GNUNET_MQ_MessageHandler handlers[] = {
1147 GNUNET_MQ_hd_var_size (hello,
1148 GNUNET_MESSAGE_TYPE_HELLO,
1149 struct GNUNET_HELLO_Message,
1151 GNUNET_MQ_handler_end ()
1153 unsigned long long opt;
1156 stats = GNUNET_STATISTICS_create ("topology", cfg);
1158 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1162 GNUNET_CONFIGURATION_get_value_number (cfg,
1167 minimum_friend_count = (unsigned int) opt;
1169 GNUNET_CONFIGURATION_get_value_number (cfg,
1171 "TARGET-CONNECTION-COUNT",
1174 target_connection_count = (unsigned int) opt;
1175 peers = GNUNET_CONTAINER_multipeermap_create (target_connection_count * 2,
1177 read_friends_file (cfg);
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 "Topology would like %u connections with at least %u friends\n",
1180 target_connection_count,
1181 minimum_friend_count);
1182 if ( (GNUNET_YES == friends_only) ||
1183 (minimum_friend_count > 0))
1184 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1187 ats = GNUNET_ATS_connectivity_init (cfg);
1188 pi = GNUNET_PEERINFO_connect (cfg);
1189 handle = GNUNET_CORE_connect (cfg,
1195 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
1199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1200 _("Failed to connect to `%s' service.\n"),
1202 GNUNET_SCHEDULER_shutdown ();
1209 * The main function for the topology daemon.
1211 * @param argc number of arguments from the command line
1212 * @param argv command line arguments
1213 * @return 0 ok, 1 on error
1216 main (int argc, char *const *argv)
1218 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1219 GNUNET_GETOPT_OPTION_END
1223 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1228 GNUNET_PROGRAM_run (argc, argv,
1229 "gnunet-daemon-topology",
1230 _("GNUnet topology control"),
1231 options, &run, NULL)) ? 0 : 1;
1232 GNUNET_free ((void*) argv);
1237 #if defined(LINUX) && defined(__GLIBC__)
1241 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1243 void __attribute__ ((constructor))
1244 GNUNET_ARM_memory_init ()
1246 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1247 mallopt (M_TOP_PAD, 1 * 1024);
1252 /* end of gnunet-daemon-topology.c */