2 This file is part of GNUnet.
3 (C) 2007-2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file topology/gnunet-daemon-topology.c
23 * @brief code for maintaining the cadet topology
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_friends_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
38 * Minimum required delay between calls to GNUNET_TRANSPORT_try_connect.
40 #define MAX_CONNECT_FREQUENCY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
43 * Maximum delay allowed delay between calls to GNUNET_TRANSPORT_try_connect.
45 #define MIN_CONNECT_FREQUENCY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 2500)
48 * For how long do we blacklist a peer after a failed connection
49 * attempt? This is the baseline factor which is then multiplied by
50 * two to the power of the number of failed attempts.
52 #define GREYLIST_AFTER_ATTEMPT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
55 * For how long do we blacklist a friend after a failed connection
56 * attempt? This is the baseline factor which is then multiplied by
57 * two to the power of the number of failed attempts.
59 #define GREYLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
62 * For how long do we blacklist anyone under any cirumstances at least after a failed connection
63 * attempt? This is the absolute minimum, regardless of what the calculation based on
64 * exponential backoff returns.
66 #define GREYLIST_AFTER_ATTEMPT_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
69 * For how long do we blacklist anyone under any cirumstances at most after a failed connection
70 * attempt? This is the absolute maximum, regardless of what the calculation based on
71 * exponential back-off returns.
73 #define GREYLIST_AFTER_ATTEMPT_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
76 * At what frequency do we sent HELLOs to a peer?
78 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
81 * After what time period do we expire the HELLO Bloom filter?
83 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
87 * Record for neighbours, friends and blacklisted peers.
92 * Which peer is this entry about?
94 struct GNUNET_PeerIdentity pid;
97 * Our handle for the request to transmit HELLOs to this peer; NULL
98 * if no such request is pending.
100 struct GNUNET_CORE_TransmitHandle *hello_req;
103 * Pointer to the HELLO message of this peer; can be NULL.
105 struct GNUNET_HELLO_Message *hello;
108 * Bloom filter used to mark which peers already got the HELLO
111 struct GNUNET_CONTAINER_BloomFilter *filter;
114 * Until what time should we not try to connect again
117 struct GNUNET_TIME_Absolute greylisted_until;
120 * Next time we are allowed to transmit a HELLO to this peer?
122 struct GNUNET_TIME_Absolute next_hello_allowed;
125 * When should we reset the bloom filter of this entry?
127 struct GNUNET_TIME_Absolute filter_expiration;
130 * When should try next connection attempt?
132 struct GNUNET_TIME_Absolute next_connect_attempt;
135 * ID of task we use to wait for the time to send the next HELLO
138 struct GNUNET_SCHEDULER_Task * hello_delay_task;
141 * Task for issuing GNUNET_TRANSPORT_try_connect for this peer.
143 struct GNUNET_SCHEDULER_Task * attempt_connect_task;
146 * ID of task we use to clear peers from the greylist.
148 struct GNUNET_SCHEDULER_Task * greylist_clean_task;
151 * How often have we tried so far?
153 unsigned int connect_attempts;
156 * Is this peer listed here because he is a friend?
161 * Are we connected to this peer right now?
169 * Our peerinfo notification context. We use notification
170 * to instantly learn about new peers as they are discovered.
172 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
177 static const struct GNUNET_CONFIGURATION_Handle *cfg;
180 * Handle to the core API.
182 static struct GNUNET_CORE_Handle *handle;
185 * Handle to the transport API.
187 static struct GNUNET_TRANSPORT_Handle *transport;
190 * Identity of this peer.
192 static struct GNUNET_PeerIdentity my_identity;
195 * All of our friends, all of our current neighbours and all peers for
196 * which we have HELLOs. So pretty much everyone. Maps peer identities
197 * to 'struct Peer *' values.
199 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
202 * Handle for reporting statistics.
204 static struct GNUNET_STATISTICS_Handle *stats;
207 * Blacklist (NULL if we have none).
209 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
212 * When can we next ask transport to create a connection?
214 static struct GNUNET_TIME_Absolute next_connect_attempt;
217 * Task scheduled to try to add peers.
219 static struct GNUNET_SCHEDULER_Task * add_task;
222 * Flag to disallow non-friend connections (pure F2F mode).
224 static int friends_only;
227 * Minimum number of friends to have in the
228 * connection set before we allow non-friends.
230 static unsigned int minimum_friend_count;
233 * Number of peers (friends and others) that we are currently connected to.
235 static unsigned int connection_count;
238 * Target number of connections.
240 static unsigned int target_connection_count;
243 * Number of friends that we are currently connected to.
245 static unsigned int friend_count;
249 * Function that decides if a connection is acceptable or not.
250 * If we have a blacklist, only friends are allowed, so the check
254 * @param pid peer to approve or disapprove
255 * @return GNUNET_OK if the connection is allowed
258 blacklist_check (void *cls, const struct GNUNET_PeerIdentity *pid)
262 pos = GNUNET_CONTAINER_multipeermap_get (peers, pid);
263 if ((pos != NULL) && (pos->is_friend == GNUNET_YES))
265 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers blacklisted"), 1,
267 return GNUNET_SYSERR;
272 * Whitelist all peers that we blacklisted; we've passed
273 * the minimum number of friends.
278 if (blacklist != NULL)
280 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
287 * Check if an additional connection from the given peer is allowed.
289 * @param peer connection to check
290 * @return GNUNET_OK if the connection is allowed
293 is_connection_allowed (struct Peer *peer)
296 memcmp (&my_identity, &peer->pid, sizeof (struct GNUNET_PeerIdentity)))
297 return GNUNET_SYSERR; /* disallow connections to self */
300 if (GNUNET_YES == friends_only)
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "Determined that `%s' is not allowed to connect (not a friend)\n",
304 GNUNET_i2s (&peer->pid));
305 return GNUNET_SYSERR;
307 if (friend_count >= minimum_friend_count)
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
311 GNUNET_i2s (&peer->pid));
312 return GNUNET_SYSERR;
317 * Free all resources associated with the given peer.
319 * @param cls closure (not used)
320 * @param pid identity of the peer
321 * @param value peer to free
322 * @return GNUNET_YES (always: continue to iterate)
325 free_peer (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
327 struct Peer *pos = value;
329 GNUNET_break (GNUNET_NO == pos->is_connected);
330 GNUNET_break (GNUNET_OK ==
331 GNUNET_CONTAINER_multipeermap_remove (peers, pid, pos));
332 if (pos->hello_req != NULL)
333 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
334 if (pos->hello_delay_task != NULL)
335 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
336 if (pos->attempt_connect_task != NULL)
337 GNUNET_SCHEDULER_cancel (pos->attempt_connect_task);
338 if (pos->greylist_clean_task != NULL)
339 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
340 GNUNET_free_non_null (pos->hello);
341 if (pos->filter != NULL)
342 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
349 * Discard peer entries for greylisted peers
350 * where the greylisting has expired.
352 * @param cls 'struct Peer' to greylist
353 * @param tc scheduler context
356 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
360 * Try to connect to the specified peer.
362 * @param pos peer to connect to
365 attempt_connect (struct Peer *pos)
367 struct GNUNET_TIME_Relative rem;
369 if ((connection_count >= target_connection_count) &&
370 (friend_count >= minimum_friend_count))
372 if (GNUNET_YES == pos->is_connected)
374 if (GNUNET_OK != is_connection_allowed (pos))
376 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value_us > 0)
378 if (GNUNET_YES == pos->is_friend)
379 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
381 rem = GREYLIST_AFTER_ATTEMPT;
382 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
383 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
384 if (pos->connect_attempts > 30)
385 pos->connect_attempts = 30;
387 pos->connect_attempts ++;
388 rem = GNUNET_TIME_relative_multiply (rem, 1 << pos->connect_attempts);
389 rem = GNUNET_TIME_relative_max (rem, GREYLIST_AFTER_ATTEMPT_MIN);
390 rem = GNUNET_TIME_relative_min (rem, GREYLIST_AFTER_ATTEMPT_MAX);
391 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
393 pos->next_connect_attempt = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
394 GNUNET_TIME_relative_multiply (MAX_CONNECT_FREQUENCY_DELAY, pos->connect_attempts));
395 pos->next_connect_attempt = GNUNET_TIME_absolute_min (pos->next_connect_attempt,
396 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), MIN_CONNECT_FREQUENCY_DELAY));
398 if (pos->greylist_clean_task != NULL)
399 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
400 pos->greylist_clean_task =
401 GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to connect to `%s'\n",
403 GNUNET_i2s (&pos->pid));
404 GNUNET_STATISTICS_update (stats,
406 ("# connect requests issued to transport"), 1,
408 GNUNET_TRANSPORT_try_connect (transport, &pos->pid, NULL, NULL);
413 * Try to connect to the specified peer.
415 * @param cls peer to connect to
416 * @param tc scheduler context
419 do_attempt_connect (void *cls,
420 const struct GNUNET_SCHEDULER_TaskContext *tc)
422 struct Peer *pos = cls;
423 struct GNUNET_TIME_Relative delay;
425 pos->attempt_connect_task = NULL;
426 if (GNUNET_YES == pos->is_connected)
429 /* Try next connection attempt, when:
430 * - topology allows the next transport connection attempt
432 * - the next connection event for this peer is allowed
434 delay = GNUNET_TIME_relative_max (GNUNET_TIME_absolute_get_remaining (next_connect_attempt),
435 GNUNET_TIME_absolute_get_remaining (pos->next_connect_attempt));
437 if (delay.rel_value_us > 0)
439 pos->attempt_connect_task = GNUNET_SCHEDULER_add_delayed (delay,
444 next_connect_attempt = GNUNET_TIME_relative_to_absolute (MAX_CONNECT_FREQUENCY_DELAY);
445 attempt_connect (pos);
450 * Schedule a task to try to connect to the specified peer.
452 * @param pos peer to connect to
455 schedule_attempt_connect (struct Peer *pos)
457 if (NULL != pos->attempt_connect_task)
459 pos->attempt_connect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_connect_attempt),
466 * Discard peer entries for greylisted peers
467 * where the greylisting has expired.
469 * @param cls 'struct Peer' to greylist
470 * @param tc scheduler context
473 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
475 struct Peer *pos = cls;
476 struct GNUNET_TIME_Relative rem;
478 pos->greylist_clean_task = NULL;
479 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
480 if (0 == rem.rel_value_us)
482 schedule_attempt_connect (pos);
486 pos->greylist_clean_task =
487 GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
489 if ((GNUNET_NO == pos->is_friend) && (GNUNET_NO == pos->is_connected) &&
490 (NULL == pos->hello))
492 free_peer (NULL, &pos->pid, pos);
499 * Create a new entry in the peer list.
501 * @param peer identity of the new entry
502 * @param hello hello message, can be NULL
503 * @param is_friend is the new entry for a friend?
504 * @return the new entry
507 make_peer (const struct GNUNET_PeerIdentity *peer,
508 const struct GNUNET_HELLO_Message *hello, int is_friend)
512 ret = GNUNET_new (struct Peer);
514 ret->is_friend = is_friend;
515 ret->next_connect_attempt = GNUNET_TIME_absolute_get();
518 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
519 memcpy (ret->hello, hello, GNUNET_HELLO_size (hello));
521 GNUNET_break (GNUNET_OK ==
522 GNUNET_CONTAINER_multipeermap_put (peers, peer,
524 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
530 * Setup bloom filter for the given peer entry.
532 * @param peer entry to initialize
535 setup_filter (struct Peer *peer)
537 struct GNUNET_HashCode hc;
539 /* 2^{-5} chance of not sending a HELLO to a peer is
540 * acceptably small (if the filter is 50% full);
541 * 64 bytes of memory are small compared to the rest
542 * of the data structure and would only really become
543 * "useless" once a HELLO has been passed on to ~100
544 * other peers, which is likely more than enough in
545 * any case; hence 64, 5 as bloomfilter parameters. */
546 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
547 peer->filter_expiration =
548 GNUNET_TIME_relative_to_absolute
549 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
550 /* never send a peer its own HELLO */
551 GNUNET_CRYPTO_hash (&peer->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
552 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &hc);
557 * Function to fill send buffer with HELLO.
559 * @param cls 'struct Peer' of the target peer
560 * @param size number of bytes available in @a buf
561 * @param buf where the callee should write the message
562 * @return number of bytes written to @a buf
565 hello_advertising_ready (void *cls, size_t size, void *buf);
569 * Closure for 'find_advertisable_hello'.
571 struct FindAdvHelloContext
575 * Peer we want to advertise to.
580 * Where to store the result (peer selected for advertising).
585 * Maximum HELLO size we can use right now.
589 struct GNUNET_TIME_Relative next_adv;
594 * Find a peer that would be reasonable for advertising.
597 * @param pid identity of a peer
598 * @param value 'struct Peer*' for the peer we are considering
599 * @return GNUNET_YES (continue iteration)
602 find_advertisable_hello (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
604 struct FindAdvHelloContext *fah = cls;
605 struct Peer *pos = value;
606 struct GNUNET_TIME_Relative rst_time;
607 struct GNUNET_HashCode hc;
610 if (pos == fah->peer)
612 if (pos->hello == NULL)
614 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
615 if (0 == rst_time.rel_value_us)
617 /* time to discard... */
618 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
621 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
622 hs = GNUNET_HELLO_size (pos->hello);
623 if (hs > fah->max_size)
625 GNUNET_CRYPTO_hash (&fah->peer->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
627 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
635 * Calculate when we would like to send the next HELLO to this
636 * peer and ask for it.
638 * @param cls for which peer to schedule the HELLO
639 * @param tc task context
642 schedule_next_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
644 struct Peer *pl = cls;
645 struct FindAdvHelloContext fah;
647 struct GNUNET_TIME_Relative delay;
649 pl->hello_delay_task = NULL;
650 GNUNET_assert (GNUNET_YES == pl->is_connected);
651 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
652 return; /* we're out of here */
653 if (pl->hello_req != NULL)
654 return; /* did not finish sending the previous one */
655 /* find applicable HELLOs */
658 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
659 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
660 GNUNET_CONTAINER_multipeermap_iterate (peers, &find_advertisable_hello, &fah);
661 pl->hello_delay_task =
662 GNUNET_SCHEDULER_add_delayed (fah.next_adv, &schedule_next_hello, pl);
663 if (fah.result == NULL)
665 next_want = GNUNET_HELLO_size (fah.result->hello);
666 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
667 if (0 == delay.rel_value_us)
671 GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES,
672 GNUNET_CORE_PRIO_BEST_EFFORT,
673 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
675 &hello_advertising_ready, pl);
681 * Cancel existing requests for sending HELLOs to this peer
682 * and recalculate when we should send HELLOs to it based
683 * on our current state (something changed!).
685 * @param cls closure, 'struct Peer' to skip, or NULL
686 * @param pid identity of a peer
687 * @param value 'struct Peer*' for the peer
688 * @return GNUNET_YES (always)
691 reschedule_hellos (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
693 struct Peer *peer = value;
694 struct Peer *skip = cls;
698 if (!peer->is_connected)
700 if (peer->hello_req != NULL)
702 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
703 peer->hello_req = NULL;
705 if (peer->hello_delay_task != NULL)
707 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
708 peer->hello_delay_task = NULL;
710 peer->hello_delay_task =
711 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
717 * Method called whenever a peer connects.
720 * @param peer peer identity this notification is about
723 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Core told us that we are connecting to `%s'\n",
730 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
734 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
735 connection_count, GNUNET_NO);
736 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
739 pos = make_peer (peer, NULL, GNUNET_NO);
740 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
744 GNUNET_assert (GNUNET_NO == pos->is_connected);
745 pos->greylisted_until.abs_value_us = 0; /* remove greylisting */
747 pos->is_connected = GNUNET_YES;
748 pos->connect_attempts = 0; /* re-set back-off factor */
749 pos->next_connect_attempt = GNUNET_TIME_absolute_get(); /* re-set back-off factor */
752 if ((friend_count == minimum_friend_count - 1) &&
753 (GNUNET_YES != friends_only))
756 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
757 friend_count, GNUNET_NO);
759 reschedule_hellos (NULL, peer, pos);
764 * Try to add more peers to our connection set.
766 * @param cls closure, not used
767 * @param pid identity of a peer
768 * @param value 'struct Peer*' for the peer
769 * @return GNUNET_YES (continue to iterate)
772 try_add_peers (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
774 struct Peer *pos = value;
776 schedule_attempt_connect (pos);
782 * Add peers and schedule connection attempt
784 * @param cls unused, NULL
785 * @param tc scheduler context
788 add_peer_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
792 GNUNET_CONTAINER_multipeermap_iterate (peers, &try_add_peers, NULL);
797 * Method called whenever a peer disconnects.
800 * @param peer peer identity this notification is about
803 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
807 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Core told us that we disconnected from `%s'\n",
812 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
818 if (pos->is_connected != GNUNET_YES)
823 pos->is_connected = GNUNET_NO;
825 if (NULL != pos->hello_req)
827 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
828 pos->hello_req = NULL;
830 if (NULL != pos->hello_delay_task)
832 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
833 pos->hello_delay_task = NULL;
835 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
836 connection_count, GNUNET_NO);
840 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
841 friend_count, GNUNET_NO);
843 if (((connection_count < target_connection_count) ||
844 (friend_count < minimum_friend_count)) &&
846 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
847 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
848 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
853 * Iterator called on each address.
855 * @param cls flag that we will set if we see any addresses
856 * @param address the address of the peer
857 * @param expiration when will the given address expire
858 * @return GNUNET_SYSERR always, to terminate iteration
861 address_iterator (void *cls, const struct GNUNET_HELLO_Address *address,
862 struct GNUNET_TIME_Absolute expiration)
867 return GNUNET_SYSERR;
872 * We've gotten a HELLO from another peer. Consider it for
875 * @param hello the HELLO we got
878 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
881 struct GNUNET_PeerIdentity pid;
882 struct GNUNET_TIME_Absolute dt;
883 struct GNUNET_HELLO_Message *nh;
887 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
892 if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
893 return; /* that's me! */
894 have_address = GNUNET_NO;
895 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &address_iterator,
897 if (GNUNET_NO == have_address)
898 return; /* no point in advertising this one... */
899 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
902 peer = make_peer (&pid, hello, GNUNET_NO);
904 else if (peer->hello != NULL)
906 dt = GNUNET_HELLO_equals (peer->hello, hello, GNUNET_TIME_absolute_get ());
907 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
908 return; /* nothing new here */
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911 "Found `%s' from peer `%s' for advertising\n", "HELLO",
913 if (peer->hello != NULL)
915 nh = GNUNET_HELLO_merge (peer->hello, hello);
916 GNUNET_free (peer->hello);
921 size = GNUNET_HELLO_size (hello);
922 peer->hello = GNUNET_malloc (size);
923 memcpy (peer->hello, hello, size);
925 if (peer->filter != NULL)
926 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
928 /* since we have a new HELLO to pick from, re-schedule all
929 * HELLO requests that are not bound by the HELLO send rate! */
930 GNUNET_CONTAINER_multipeermap_iterate (peers, &reschedule_hellos, peer);
935 * PEERINFO calls this function to let us know about a possible peer
936 * that we might want to connect to.
938 * @param cls closure (not used)
939 * @param peer potential peer to connect to
940 * @param hello HELLO for this peer (or NULL)
941 * @param err_msg NULL if successful, otherwise contains error message
944 process_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
945 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 _("Error in communication with PEERINFO service: %s\n"),
954 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
955 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO, &process_peer, NULL);
958 GNUNET_assert (peer != NULL);
959 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
960 return; /* that's me! */
963 /* free existing HELLO, if any */
964 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
967 GNUNET_free_non_null (pos->hello);
969 if (pos->filter != NULL)
971 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
974 if ((GNUNET_NO == pos->is_connected) && (GNUNET_NO == pos->is_friend) &&
976 GNUNET_TIME_absolute_get_remaining (pos->
977 greylisted_until).rel_value_us))
978 free_peer (NULL, &pos->pid, pos);
982 consider_for_advertising (hello);
983 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
985 pos = make_peer (peer, hello, GNUNET_NO);
986 GNUNET_assert (NULL != pos);
987 if (GNUNET_YES == pos->is_connected)
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already connected to peer `%s'\n",
993 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value_us > 0)
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already tried peer `%s' recently\n",
997 return; /* peer still greylisted */
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Considering connecting to peer `%s'\n",
1001 schedule_attempt_connect (pos);
1006 * Function called after #GNUNET_CORE_connect has succeeded
1007 * (or failed for good).
1009 * @param cls closure
1010 * @param my_id ID of this peer, NULL if we failed
1013 core_init (void *cls,
1014 const struct GNUNET_PeerIdentity *my_id)
1018 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1020 ("Failed to connect to core service, can not manage topology!\n"));
1021 GNUNET_SCHEDULER_shutdown ();
1024 my_identity = *my_id;
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1027 GNUNET_i2s (my_id));
1028 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO,
1029 &process_peer, NULL);
1034 * Process friend found in FRIENDS file.
1036 * @param cls pointer to an `unsigned int` to be incremented per friend found
1037 * @param pid identity of the friend
1040 handle_friend (void *cls,
1041 const struct GNUNET_PeerIdentity *pid)
1043 unsigned int *entries_found = cls;
1046 if (0 == memcmp (pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1048 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1049 _("Found myself `%s' in friend list (useless, ignored)\n"),
1054 fl = make_peer (pid, NULL, GNUNET_YES);
1055 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1056 _("Found friend `%s' in configuration\n"),
1057 GNUNET_i2s (&fl->pid));
1062 * Read the friends file.
1065 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1067 unsigned int entries_found;
1071 GNUNET_FRIENDS_parse (cfg,
1075 if ((friends_only) || (minimum_friend_count > 0))
1076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1077 _("Encountered errors parsing friends list!\n"));
1079 GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
1080 entries_found, GNUNET_NO);
1081 if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1083 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1084 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1086 if ((minimum_friend_count > target_connection_count) &&
1087 (friends_only == GNUNET_NO))
1089 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1090 _("More friendly connections required than target total number of connections.\n"));
1096 * This function is called whenever an encrypted HELLO message is
1099 * @param cls closure
1100 * @param other the other peer involved (sender or receiver, NULL
1101 * for loopback messages where we are both sender and receiver)
1102 * @param message the actual HELLO message
1103 * @return GNUNET_OK to keep the connection open,
1104 * GNUNET_SYSERR to close it (signal serious error)
1107 handle_encrypted_hello (void *cls, const struct GNUNET_PeerIdentity *other,
1108 const struct GNUNET_MessageHeader *message)
1111 struct GNUNET_PeerIdentity pid;
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received encrypted `%s' from peer `%s'",
1114 "HELLO", GNUNET_i2s (other));
1116 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1118 GNUNET_break_op (0);
1119 return GNUNET_SYSERR;
1121 GNUNET_STATISTICS_update (stats, gettext_noop ("# HELLO messages received"),
1123 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
1126 if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
1131 if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
1133 if ((GNUNET_YES != peer->is_friend) &&
1134 (friend_count < minimum_friend_count))
1137 if (transport != NULL)
1138 GNUNET_TRANSPORT_offer_hello (transport, message, NULL, NULL);
1144 * Function to fill send buffer with HELLO.
1146 * @param cls 'struct Peer' of the target peer
1147 * @param size number of bytes available in buf
1148 * @param buf where the callee should write the message
1149 * @return number of bytes written to buf
1152 hello_advertising_ready (void *cls, size_t size, void *buf)
1154 struct Peer *pl = cls;
1155 struct FindAdvHelloContext fah;
1157 struct GNUNET_HashCode hc;
1159 pl->hello_req = NULL;
1160 GNUNET_assert (GNUNET_YES == pl->is_connected);
1161 /* find applicable HELLOs */
1164 fah.max_size = size;
1165 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1166 GNUNET_CONTAINER_multipeermap_iterate (peers, &find_advertisable_hello, &fah);
1168 if (fah.result != NULL)
1170 want = GNUNET_HELLO_size (fah.result->hello);
1171 GNUNET_assert (want <= size);
1172 memcpy (buf, fah.result->hello, want);
1173 GNUNET_CRYPTO_hash (&pl->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
1174 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &hc);
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' with %u bytes", "HELLO",
1176 (unsigned int) want);
1177 GNUNET_STATISTICS_update (stats,
1178 gettext_noop ("# HELLO messages gossipped"), 1,
1182 if (pl->hello_delay_task != NULL)
1183 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1184 pl->next_hello_allowed =
1185 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1186 pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1192 * Last task run during shutdown. Disconnects us from
1193 * the transport and core.
1195 * @param cls unused, NULL
1196 * @param tc scheduler context
1199 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1201 if (NULL != peerinfo_notify)
1203 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1204 peerinfo_notify = NULL;
1206 GNUNET_TRANSPORT_disconnect (transport);
1210 GNUNET_CORE_disconnect (handle);
1214 if (NULL != add_task)
1216 GNUNET_SCHEDULER_cancel (add_task);
1219 GNUNET_CONTAINER_multipeermap_iterate (peers, &free_peer, NULL);
1220 GNUNET_CONTAINER_multipeermap_destroy (peers);
1224 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1231 * Main function that will be run.
1233 * @param cls closure
1234 * @param args remaining command-line arguments
1235 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1236 * @param c configuration
1241 const char *cfgfile,
1242 const struct GNUNET_CONFIGURATION_Handle *c)
1244 static struct GNUNET_CORE_MessageHandler handlers[] = {
1245 {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1248 unsigned long long opt;
1251 stats = GNUNET_STATISTICS_create ("topology", cfg);
1253 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY");
1255 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "MINIMUM-FRIENDS",
1258 minimum_friend_count = (unsigned int) opt;
1260 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY",
1261 "TARGET-CONNECTION-COUNT", &opt))
1263 target_connection_count = (unsigned int) opt;
1264 peers = GNUNET_CONTAINER_multipeermap_create (target_connection_count * 2, GNUNET_NO);
1266 if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0))
1267 read_friends_file (cfg);
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 "Topology would like %u connections with at least %u friends\n",
1270 target_connection_count, minimum_friend_count);
1271 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
1272 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
1273 transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
1275 GNUNET_CORE_connect (cfg, NULL,
1282 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
1284 if (NULL == transport)
1286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1287 _("Failed to connect to `%s' service.\n"), "transport");
1288 GNUNET_SCHEDULER_shutdown ();
1293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1294 _("Failed to connect to `%s' service.\n"), "core");
1295 GNUNET_SCHEDULER_shutdown ();
1302 * The main function for the topology daemon.
1304 * @param argc number of arguments from the command line
1305 * @param argv command line arguments
1306 * @return 0 ok, 1 on error
1309 main (int argc, char *const *argv)
1311 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1312 GNUNET_GETOPT_OPTION_END
1316 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1321 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",
1323 ("GNUnet topology control (maintaining P2P cadet and F2F constraints)"),
1324 options, &run, NULL)) ? 0 : 1;
1325 GNUNET_free ((void*) argv);
1334 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1336 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1338 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1339 mallopt (M_TOP_PAD, 1 * 1024);
1344 /* end of gnunet-daemon-topology.c */