2 This file is part of GNUnet.
3 Copyright (C) 2007-2015 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 the request to transmit HELLOs to this peer; NULL
74 * if no such request is pending.
76 struct GNUNET_CORE_TransmitHandle *hello_req;
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?
121 * Are we connected to this peer right now?
129 * Our peerinfo notification context. We use notification
130 * to instantly learn about new peers as they are discovered.
132 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
137 static const struct GNUNET_CONFIGURATION_Handle *cfg;
140 * Handle to the CORE service.
142 static struct GNUNET_CORE_Handle *handle;
145 * Handle to the TRANSPORT service.
147 static struct GNUNET_TRANSPORT_Handle *transport;
150 * Handle to the ATS service.
152 static struct GNUNET_ATS_ConnectivityHandle *ats;
155 * Identity of this peer.
157 static struct GNUNET_PeerIdentity my_identity;
160 * All of our friends, all of our current neighbours and all peers for
161 * which we have HELLOs. So pretty much everyone. Maps peer identities
162 * to `struct Peer *` values.
164 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
167 * Handle for reporting statistics.
169 static struct GNUNET_STATISTICS_Handle *stats;
172 * Blacklist (NULL if we have none).
174 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
177 * Task scheduled to asynchronously reconsider adding/removing
178 * peer connectivity suggestions.
180 static struct GNUNET_SCHEDULER_Task *add_task;
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 (GNUNET_NO == pos->is_connected);
268 GNUNET_break (GNUNET_OK ==
269 GNUNET_CONTAINER_multipeermap_remove (peers,
272 if (NULL != pos->hello_req)
274 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
275 pos->hello_req = NULL;
277 if (NULL != pos->hello_delay_task)
279 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
280 pos->hello_delay_task = NULL;
284 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
287 if (NULL != pos->hello)
289 GNUNET_free_non_null (pos->hello);
292 if (NULL != pos->filter)
294 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
303 * Recalculate how much we want to be connected to the specified peer
304 * and let ATS know about the result.
306 * @param pos peer to consider connecting to
309 attempt_connect (struct Peer *pos)
314 memcmp (&my_identity,
316 sizeof (struct GNUNET_PeerIdentity)))
317 return; /* This is myself, nothing to do. */
318 if (connection_count < target_connection_count)
322 if ( (friend_count < minimum_friend_count) ||
323 (GNUNET_YES == friends_only) )
326 strength += 10; /* urgently needed */
328 strength = 0; /* disallowed */
331 strength *= 2; /* friends always count more */
332 if (pos->is_connected)
333 strength *= 2; /* existing connections preferred */
334 if (strength == pos->strength)
335 return; /* nothing to do */
338 GNUNET_ATS_connectivity_suggest_cancel (pos->sh);
341 pos->strength = strength;
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Asking to connect to `%s' with strength %u\n",
346 GNUNET_i2s (&pos->pid),
347 (unsigned int) strength);
348 GNUNET_STATISTICS_update (stats,
349 gettext_noop ("# connect requests issued to ATS"),
352 pos->sh = GNUNET_ATS_connectivity_suggest (ats,
360 * Create a new entry in the peer list.
362 * @param peer identity of the new entry
363 * @param hello hello message, can be NULL
364 * @param is_friend is the new entry for a friend?
365 * @return the new entry
368 make_peer (const struct GNUNET_PeerIdentity *peer,
369 const struct GNUNET_HELLO_Message *hello,
374 ret = GNUNET_new (struct Peer);
376 ret->is_friend = is_friend;
379 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
382 GNUNET_HELLO_size (hello));
384 GNUNET_break (GNUNET_OK ==
385 GNUNET_CONTAINER_multipeermap_put (peers,
388 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
394 * Setup bloom filter for the given peer entry.
396 * @param peer entry to initialize
399 setup_filter (struct Peer *peer)
401 struct GNUNET_HashCode hc;
403 /* 2^{-5} chance of not sending a HELLO to a peer is
404 * acceptably small (if the filter is 50% full);
405 * 64 bytes of memory are small compared to the rest
406 * of the data structure and would only really become
407 * "useless" once a HELLO has been passed on to ~100
408 * other peers, which is likely more than enough in
409 * any case; hence 64, 5 as bloomfilter parameters. */
410 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
411 peer->filter_expiration =
412 GNUNET_TIME_relative_to_absolute
413 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
414 /* never send a peer its own HELLO */
415 GNUNET_CRYPTO_hash (&peer->pid,
416 sizeof (struct GNUNET_PeerIdentity),
418 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &hc);
423 * Function to fill send buffer with HELLO.
425 * @param cls `struct Peer` of the target peer
426 * @param size number of bytes available in @a buf
427 * @param buf where the callee should write the message
428 * @return number of bytes written to @a buf
431 hello_advertising_ready (void *cls,
437 * Closure for #find_advertisable_hello().
439 struct FindAdvHelloContext
443 * Peer we want to advertise to.
448 * Where to store the result (peer selected for advertising).
453 * Maximum HELLO size we can use right now.
457 struct GNUNET_TIME_Relative next_adv;
462 * Find a peer that would be reasonable for advertising.
465 * @param pid identity of a peer
466 * @param value 'struct Peer*' for the peer we are considering
467 * @return #GNUNET_YES (continue iteration)
470 find_advertisable_hello (void *cls,
471 const struct GNUNET_PeerIdentity *pid,
474 struct FindAdvHelloContext *fah = cls;
475 struct Peer *pos = value;
476 struct GNUNET_TIME_Relative rst_time;
477 struct GNUNET_HashCode hc;
480 if (pos == fah->peer)
482 if (pos->hello == NULL)
484 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
485 if (0 == rst_time.rel_value_us)
487 /* time to discard... */
488 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
491 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
492 hs = GNUNET_HELLO_size (pos->hello);
493 if (hs > fah->max_size)
495 GNUNET_CRYPTO_hash (&fah->peer->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
497 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
505 * Calculate when we would like to send the next HELLO to this
506 * peer and ask for it.
508 * @param cls for which peer to schedule the HELLO
511 schedule_next_hello (void *cls)
513 struct Peer *pl = cls;
514 struct FindAdvHelloContext fah;
516 struct GNUNET_TIME_Relative delay;
518 pl->hello_delay_task = NULL;
519 GNUNET_assert (GNUNET_YES == pl->is_connected);
520 if (pl->hello_req != NULL)
521 return; /* did not finish sending the previous one */
522 /* find applicable HELLOs */
525 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
526 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
527 GNUNET_CONTAINER_multipeermap_iterate (peers,
528 &find_advertisable_hello,
530 pl->hello_delay_task =
531 GNUNET_SCHEDULER_add_delayed (fah.next_adv,
532 &schedule_next_hello,
534 if (NULL == fah.result)
536 next_want = GNUNET_HELLO_size (fah.result->hello);
537 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
538 if (0 == delay.rel_value_us)
542 GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES,
543 GNUNET_CORE_PRIO_BEST_EFFORT,
544 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
546 &hello_advertising_ready, pl);
552 * Cancel existing requests for sending HELLOs to this peer
553 * and recalculate when we should send HELLOs to it based
554 * on our current state (something changed!).
556 * @param cls closure `struct Peer` to skip, or NULL
557 * @param pid identity of a peer
558 * @param value `struct Peer *` for the peer
559 * @return #GNUNET_YES (always)
562 reschedule_hellos (void *cls,
563 const struct GNUNET_PeerIdentity *pid,
566 struct Peer *peer = value;
567 struct Peer *skip = cls;
571 if (!peer->is_connected)
573 if (peer->hello_req != NULL)
575 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
576 peer->hello_req = NULL;
578 if (peer->hello_delay_task != NULL)
580 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
581 peer->hello_delay_task = NULL;
583 peer->hello_delay_task =
584 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
590 * Method called whenever a peer connects.
593 * @param peer peer identity this notification is about
596 connect_notify (void *cls,
597 const struct GNUNET_PeerIdentity *peer)
601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
602 "Core told us that we are connecting to `%s'\n",
604 if (0 == memcmp (&my_identity,
606 sizeof (struct GNUNET_PeerIdentity)))
610 GNUNET_STATISTICS_set (stats,
611 gettext_noop ("# peers connected"),
614 pos = GNUNET_CONTAINER_multipeermap_get (peers,
618 pos = make_peer (peer,
624 GNUNET_assert (GNUNET_NO == pos->is_connected);
626 pos->is_connected = GNUNET_YES;
630 if ( (friend_count == minimum_friend_count) &&
631 (GNUNET_YES != friends_only) )
633 GNUNET_STATISTICS_set (stats,
634 gettext_noop ("# friends connected"),
638 reschedule_hellos (NULL,
645 * Try to add more peers to our connection set.
647 * @param cls closure, not used
648 * @param pid identity of a peer
649 * @param value `struct Peer *` for the peer
650 * @return #GNUNET_YES (continue to iterate)
653 try_add_peers (void *cls,
654 const struct GNUNET_PeerIdentity *pid,
657 struct Peer *pos = value;
659 attempt_connect (pos);
665 * Add peers and schedule connection attempt
667 * @param cls unused, NULL
670 add_peer_task (void *cls)
674 GNUNET_CONTAINER_multipeermap_iterate (peers,
681 * Method called whenever a peer disconnects.
684 * @param peer peer identity this notification is about
687 disconnect_notify (void *cls,
688 const struct GNUNET_PeerIdentity *peer)
692 if (0 == memcmp (&my_identity,
694 sizeof (struct GNUNET_PeerIdentity)))
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
697 "Core told us that we disconnected from `%s'\n",
699 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
705 if (GNUNET_YES != pos->is_connected)
710 pos->is_connected = GNUNET_NO;
712 if (NULL != pos->hello_req)
714 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
715 pos->hello_req = NULL;
717 if (NULL != pos->hello_delay_task)
719 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
720 pos->hello_delay_task = NULL;
722 GNUNET_STATISTICS_set (stats,
723 gettext_noop ("# peers connected"),
729 GNUNET_STATISTICS_set (stats,
730 gettext_noop ("# friends connected"),
734 if ( ( (connection_count < target_connection_count) ||
735 (friend_count < minimum_friend_count)) &&
737 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task,
739 if ( (friend_count < minimum_friend_count) &&
741 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
748 * Iterator called on each address.
750 * @param cls flag that we will set if we see any addresses
751 * @param address the address of the peer
752 * @param expiration when will the given address expire
753 * @return #GNUNET_SYSERR always, to terminate iteration
756 address_iterator (void *cls,
757 const struct GNUNET_HELLO_Address *address,
758 struct GNUNET_TIME_Absolute expiration)
763 return GNUNET_SYSERR;
768 * We've gotten a HELLO from another peer. Consider it for
771 * @param hello the HELLO we got
774 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
777 struct GNUNET_PeerIdentity pid;
778 struct GNUNET_TIME_Absolute dt;
779 struct GNUNET_HELLO_Message *nh;
783 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
788 if (0 == memcmp (&pid,
790 sizeof (struct GNUNET_PeerIdentity)))
791 return; /* that's me! */
792 have_address = GNUNET_NO;
793 GNUNET_HELLO_iterate_addresses (hello,
797 if (GNUNET_NO == have_address)
798 return; /* no point in advertising this one... */
799 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
802 peer = make_peer (&pid,
806 else if (NULL != peer->hello)
808 dt = GNUNET_HELLO_equals (peer->hello,
810 GNUNET_TIME_absolute_get ());
811 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
812 return; /* nothing new here */
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815 "Found HELLO from peer `%s' for advertising\n",
817 if (NULL != peer->hello)
819 nh = GNUNET_HELLO_merge (peer->hello,
821 GNUNET_free (peer->hello);
826 size = GNUNET_HELLO_size (hello);
827 peer->hello = GNUNET_malloc (size);
832 if (NULL != peer->filter)
834 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
838 /* since we have a new HELLO to pick from, re-schedule all
839 * HELLO requests that are not bound by the HELLO send rate! */
840 GNUNET_CONTAINER_multipeermap_iterate (peers,
847 * PEERINFO calls this function to let us know about a possible peer
848 * that we might want to connect to.
850 * @param cls closure (not used)
851 * @param peer potential peer to connect to
852 * @param hello HELLO for this peer (or NULL)
853 * @param err_msg NULL if successful, otherwise contains error message
856 process_peer (void *cls,
857 const struct GNUNET_PeerIdentity *peer,
858 const struct GNUNET_HELLO_Message *hello,
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866 _("Error in communication with PEERINFO service: %s\n"),
868 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
869 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
875 GNUNET_assert (NULL != peer);
876 if (0 == memcmp (&my_identity,
878 sizeof (struct GNUNET_PeerIdentity)))
879 return; /* that's me! */
882 /* free existing HELLO, if any */
883 pos = GNUNET_CONTAINER_multipeermap_get (peers,
887 GNUNET_free_non_null (pos->hello);
889 if (NULL != pos->filter)
891 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
894 if ( (GNUNET_NO == pos->is_connected) &&
895 (GNUNET_NO == pos->is_friend) )
902 consider_for_advertising (hello);
903 pos = GNUNET_CONTAINER_multipeermap_get (peers,
906 pos = make_peer (peer,
909 attempt_connect (pos);
914 * Function called after #GNUNET_CORE_connect has succeeded
915 * (or failed for good).
918 * @param my_id ID of this peer, NULL if we failed
921 core_init (void *cls,
922 const struct GNUNET_PeerIdentity *my_id)
926 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
927 _("Failed to connect to core service, can not manage topology!\n"));
928 GNUNET_SCHEDULER_shutdown ();
931 my_identity = *my_id;
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
935 peerinfo_notify = GNUNET_PEERINFO_notify (cfg,
943 * Process friend found in FRIENDS file.
945 * @param cls pointer to an `unsigned int` to be incremented per friend found
946 * @param pid identity of the friend
949 handle_friend (void *cls,
950 const struct GNUNET_PeerIdentity *pid)
952 unsigned int *entries_found = cls;
955 if (0 == memcmp (pid,
957 sizeof (struct GNUNET_PeerIdentity)))
959 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
960 _("Found myself `%s' in friend list (useless, ignored)\n"),
965 fl = make_peer (pid, NULL, GNUNET_YES);
966 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
967 _("Found friend `%s' in configuration\n"),
968 GNUNET_i2s (&fl->pid));
973 * Read the friends file.
976 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
978 unsigned int entries_found;
982 GNUNET_FRIENDS_parse (cfg,
986 if ( (GNUNET_YES == friends_only) ||
987 (minimum_friend_count > 0))
988 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
989 _("Encountered errors parsing friends list!\n"));
991 GNUNET_STATISTICS_update (stats,
992 gettext_noop ("# friends in configuration"),
995 if ( (minimum_friend_count > entries_found) &&
996 (GNUNET_NO == friends_only) )
998 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
999 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1001 if ( (minimum_friend_count > target_connection_count) &&
1002 (GNUNET_NO == friends_only))
1004 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1005 _("More friendly connections required than target total number of connections.\n"));
1011 * This function is called whenever an encrypted HELLO message is
1014 * @param cls closure
1015 * @param other the other peer involved (sender or receiver, NULL
1016 * for loopback messages where we are both sender and receiver)
1017 * @param message the actual HELLO message
1018 * @return #GNUNET_OK to keep the connection open,
1019 * #GNUNET_SYSERR to close it (signal serious error)
1022 handle_encrypted_hello (void *cls,
1023 const struct GNUNET_PeerIdentity *other,
1024 const struct GNUNET_MessageHeader *message)
1027 struct GNUNET_PeerIdentity pid;
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1030 "Received encrypted HELLO from peer `%s'",
1031 GNUNET_i2s (other));
1033 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1035 GNUNET_break_op (0);
1036 return GNUNET_SYSERR;
1038 GNUNET_STATISTICS_update (stats,
1039 gettext_noop ("# HELLO messages received"),
1042 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
1045 if ( (GNUNET_YES == friends_only) ||
1046 (friend_count < minimum_friend_count) )
1051 if ( (GNUNET_YES != peer->is_friend) &&
1052 (GNUNET_YES == friends_only) )
1054 if ((GNUNET_YES != peer->is_friend) &&
1055 (friend_count < minimum_friend_count))
1058 if (NULL != transport)
1059 GNUNET_TRANSPORT_offer_hello (transport,
1068 * Function to fill send buffer with HELLO.
1070 * @param cls `struct Peer` of the target peer
1071 * @param size number of bytes available in buf
1072 * @param buf where the callee should write the message
1073 * @return number of bytes written to buf
1076 hello_advertising_ready (void *cls,
1080 struct Peer *pl = cls;
1081 struct FindAdvHelloContext fah;
1083 struct GNUNET_HashCode hc;
1085 pl->hello_req = NULL;
1086 GNUNET_assert (GNUNET_YES == pl->is_connected);
1087 /* find applicable HELLOs */
1090 fah.max_size = size;
1091 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1092 GNUNET_CONTAINER_multipeermap_iterate (peers,
1093 &find_advertisable_hello,
1096 if (NULL != fah.result)
1098 want = GNUNET_HELLO_size (fah.result->hello);
1099 GNUNET_assert (want <= size);
1103 GNUNET_CRYPTO_hash (&pl->pid,
1104 sizeof (struct GNUNET_PeerIdentity),
1106 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1109 "Sending HELLO with %u bytes",
1110 (unsigned int) want);
1111 GNUNET_STATISTICS_update (stats,
1112 gettext_noop ("# HELLO messages gossipped"), 1,
1116 if (pl->hello_delay_task != NULL)
1117 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1118 pl->next_hello_allowed =
1119 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1120 pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1126 * Last task run during shutdown. Disconnects us from
1127 * the transport and core.
1129 * @param cls unused, NULL
1132 cleaning_task (void *cls)
1134 if (NULL != peerinfo_notify)
1136 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1137 peerinfo_notify = NULL;
1139 if (NULL != transport)
1141 GNUNET_TRANSPORT_disconnect (transport);
1146 GNUNET_CORE_disconnect (handle);
1150 if (NULL != add_task)
1152 GNUNET_SCHEDULER_cancel (add_task);
1155 GNUNET_CONTAINER_multipeermap_iterate (peers,
1158 GNUNET_CONTAINER_multipeermap_destroy (peers);
1162 GNUNET_ATS_connectivity_done (ats);
1167 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1174 * Main function that will be run.
1176 * @param cls closure
1177 * @param args remaining command-line arguments
1178 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1179 * @param c configuration
1184 const char *cfgfile,
1185 const struct GNUNET_CONFIGURATION_Handle *c)
1187 static struct GNUNET_CORE_MessageHandler handlers[] = {
1188 {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1191 unsigned long long opt;
1194 stats = GNUNET_STATISTICS_create ("topology", cfg);
1196 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1200 GNUNET_CONFIGURATION_get_value_number (cfg,
1205 minimum_friend_count = (unsigned int) opt;
1207 GNUNET_CONFIGURATION_get_value_number (cfg,
1209 "TARGET-CONNECTION-COUNT",
1212 target_connection_count = (unsigned int) opt;
1213 peers = GNUNET_CONTAINER_multipeermap_create (target_connection_count * 2,
1215 read_friends_file (cfg);
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1217 "Topology would like %u connections with at least %u friends\n",
1218 target_connection_count,
1219 minimum_friend_count);
1220 if ( (friend_count < minimum_friend_count) &&
1221 (NULL == blacklist))
1222 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1225 ats = GNUNET_ATS_connectivity_init (cfg);
1226 transport = GNUNET_TRANSPORT_connect (cfg,
1233 GNUNET_CORE_connect (cfg, NULL,
1240 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
1242 if (NULL == transport)
1244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1245 _("Failed to connect to `%s' service.\n"),
1247 GNUNET_SCHEDULER_shutdown ();
1252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1253 _("Failed to connect to `%s' service.\n"),
1255 GNUNET_SCHEDULER_shutdown ();
1262 * The main function for the topology daemon.
1264 * @param argc number of arguments from the command line
1265 * @param argv command line arguments
1266 * @return 0 ok, 1 on error
1269 main (int argc, char *const *argv)
1271 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1272 GNUNET_GETOPT_OPTION_END
1276 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1281 GNUNET_PROGRAM_run (argc, argv,
1282 "gnunet-daemon-topology",
1283 _("GNUnet topology control"),
1284 options, &run, NULL)) ? 0 : 1;
1285 GNUNET_free ((void*) argv);
1290 #if defined(LINUX) && defined(__GLIBC__)
1294 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1296 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1298 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1299 mallopt (M_TOP_PAD, 1 * 1024);
1304 /* end of gnunet-daemon-topology.c */