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 mesh 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 * For how long do we blacklist a peer after a failed connection
44 * attempt? This is the baseline factor which is then multiplied by
45 * two to the power of the number of failed attempts.
47 #define GREYLIST_AFTER_ATTEMPT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
50 * For how long do we blacklist a friend after a failed connection
51 * attempt? This is the baseline factor which is then multiplied by
52 * two to the power of the number of failed attempts.
54 #define GREYLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
57 * For how long do we blacklist anyone under any cirumstances at least after a failed connection
58 * attempt? This is the absolute minimum, regardless of what the calculation based on
59 * exponential backoff returns.
61 #define GREYLIST_AFTER_ATTEMPT_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
64 * For how long do we blacklist anyone under any cirumstances at most after a failed connection
65 * attempt? This is the absolute maximum, regardless of what the calculation based on
66 * exponential back-off returns.
68 #define GREYLIST_AFTER_ATTEMPT_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
71 * At what frequency do we sent HELLOs to a peer?
73 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
76 * After what time period do we expire the HELLO Bloom filter?
78 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
82 * Record for neighbours, friends and blacklisted peers.
87 * Which peer is this entry about?
89 struct GNUNET_PeerIdentity pid;
92 * Our handle for the request to transmit HELLOs to this peer; NULL
93 * if no such request is pending.
95 struct GNUNET_CORE_TransmitHandle *hello_req;
98 * Pointer to the HELLO message of this peer; can be NULL.
100 struct GNUNET_HELLO_Message *hello;
103 * Bloom filter used to mark which peers already got the HELLO
106 struct GNUNET_CONTAINER_BloomFilter *filter;
109 * Until what time should we not try to connect again
112 struct GNUNET_TIME_Absolute greylisted_until;
115 * Next time we are allowed to transmit a HELLO to this peer?
117 struct GNUNET_TIME_Absolute next_hello_allowed;
120 * When should we reset the bloom filter of this entry?
122 struct GNUNET_TIME_Absolute filter_expiration;
125 * ID of task we use to wait for the time to send the next HELLO
128 GNUNET_SCHEDULER_TaskIdentifier hello_delay_task;
131 * Task for issuing GNUNET_TRANSPORT_try_connect for this peer.
133 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task;
136 * ID of task we use to clear peers from the greylist.
138 GNUNET_SCHEDULER_TaskIdentifier greylist_clean_task;
141 * How often have we tried so far?
143 unsigned int connect_attempts;
146 * Is this peer listed here because he is a friend?
151 * Are we connected to this peer right now?
159 * Our peerinfo notification context. We use notification
160 * to instantly learn about new peers as they are discovered.
162 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
167 static const struct GNUNET_CONFIGURATION_Handle *cfg;
170 * Handle to the core API.
172 static struct GNUNET_CORE_Handle *handle;
175 * Handle to the transport API.
177 static struct GNUNET_TRANSPORT_Handle *transport;
180 * Identity of this peer.
182 static struct GNUNET_PeerIdentity my_identity;
185 * All of our friends, all of our current neighbours and all peers for
186 * which we have HELLOs. So pretty much everyone. Maps peer identities
187 * to 'struct Peer *' values.
189 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
192 * Handle for reporting statistics.
194 static struct GNUNET_STATISTICS_Handle *stats;
197 * Blacklist (NULL if we have none).
199 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
202 * When can we next ask transport to create a connection?
204 static struct GNUNET_TIME_Absolute next_connect_attempt;
207 * Task scheduled to try to add peers.
209 static GNUNET_SCHEDULER_TaskIdentifier add_task;
212 * Flag to disallow non-friend connections (pure F2F mode).
214 static int friends_only;
217 * Minimum number of friends to have in the
218 * connection set before we allow non-friends.
220 static unsigned int minimum_friend_count;
223 * Number of peers (friends and others) that we are currently connected to.
225 static unsigned int connection_count;
228 * Target number of connections.
230 static unsigned int target_connection_count;
233 * Number of friends that we are currently connected to.
235 static unsigned int friend_count;
239 * Function that decides if a connection is acceptable or not.
240 * If we have a blacklist, only friends are allowed, so the check
244 * @param pid peer to approve or disapproave
245 * @return GNUNET_OK if the connection is allowed
248 blacklist_check (void *cls, const struct GNUNET_PeerIdentity *pid)
252 pos = GNUNET_CONTAINER_multipeermap_get (peers, pid);
253 if ((pos != NULL) && (pos->is_friend == GNUNET_YES))
255 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers blacklisted"), 1,
257 return GNUNET_SYSERR;
262 * Whitelist all peers that we blacklisted; we've passed
263 * the minimum number of friends.
268 if (blacklist != NULL)
270 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
277 * Check if an additional connection from the given peer is allowed.
279 * @param peer connection to check
280 * @return GNUNET_OK if the connection is allowed
283 is_connection_allowed (struct Peer *peer)
286 memcmp (&my_identity, &peer->pid, sizeof (struct GNUNET_PeerIdentity)))
287 return GNUNET_SYSERR; /* disallow connections to self */
290 if (GNUNET_YES == friends_only)
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Determined that `%s' is not allowed to connect (not a friend)\n",
294 GNUNET_i2s (&peer->pid));
295 return GNUNET_SYSERR;
297 if (friend_count >= minimum_friend_count)
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
301 GNUNET_i2s (&peer->pid));
302 return GNUNET_SYSERR;
307 * Free all resources associated with the given peer.
309 * @param cls closure (not used)
310 * @param pid identity of the peer
311 * @param value peer to free
312 * @return GNUNET_YES (always: continue to iterate)
315 free_peer (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
317 struct Peer *pos = value;
319 GNUNET_break (GNUNET_NO == pos->is_connected);
320 GNUNET_break (GNUNET_OK ==
321 GNUNET_CONTAINER_multipeermap_remove (peers, pid, pos));
322 if (pos->hello_req != NULL)
323 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
324 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
325 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
326 if (pos->attempt_connect_task != GNUNET_SCHEDULER_NO_TASK)
327 GNUNET_SCHEDULER_cancel (pos->attempt_connect_task);
328 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
329 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
330 GNUNET_free_non_null (pos->hello);
331 if (pos->filter != NULL)
332 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
339 * Discard peer entries for greylisted peers
340 * where the greylisting has expired.
342 * @param cls 'struct Peer' to greylist
343 * @param tc scheduler context
346 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
350 * Try to connect to the specified peer.
352 * @param pos peer to connect to
355 attempt_connect (struct Peer *pos)
357 struct GNUNET_TIME_Relative rem;
359 if ((connection_count >= target_connection_count) &&
360 (friend_count >= minimum_friend_count))
362 if (GNUNET_YES == pos->is_connected)
364 if (GNUNET_OK != is_connection_allowed (pos))
366 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value_us > 0)
368 if (GNUNET_YES == pos->is_friend)
369 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
371 rem = GREYLIST_AFTER_ATTEMPT;
372 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
373 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
374 if (pos->connect_attempts > 30)
375 pos->connect_attempts = 30;
376 rem = GNUNET_TIME_relative_multiply (rem, 1 << (++pos->connect_attempts));
377 rem = GNUNET_TIME_relative_max (rem, GREYLIST_AFTER_ATTEMPT_MIN);
378 rem = GNUNET_TIME_relative_min (rem, GREYLIST_AFTER_ATTEMPT_MAX);
379 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
380 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
381 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
382 pos->greylist_clean_task =
383 GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to connect to `%s'\n",
385 GNUNET_i2s (&pos->pid));
386 GNUNET_STATISTICS_update (stats,
388 ("# connect requests issued to transport"), 1,
390 GNUNET_TRANSPORT_try_connect (transport, &pos->pid, NULL, NULL); /*FIXME TRY_CONNECT change */
395 * Try to connect to the specified peer.
397 * @param cls peer to connect to
398 * @param tc scheduler context
401 do_attempt_connect (void *cls,
402 const struct GNUNET_SCHEDULER_TaskContext *tc)
404 struct Peer *pos = cls;
405 struct GNUNET_TIME_Relative delay;
407 pos->attempt_connect_task = GNUNET_SCHEDULER_NO_TASK;
408 if (GNUNET_YES == pos->is_connected)
410 delay = GNUNET_TIME_absolute_get_remaining (next_connect_attempt);
411 if (delay.rel_value_us > 0)
413 pos->attempt_connect_task = GNUNET_SCHEDULER_add_delayed (delay,
418 next_connect_attempt = GNUNET_TIME_relative_to_absolute (MAX_CONNECT_FREQUENCY_DELAY);
419 attempt_connect (pos);
424 * Schedule a task to try to connect to the specified peer.
426 * @param pos peer to connect to
429 schedule_attempt_connect (struct Peer *pos)
431 if (GNUNET_SCHEDULER_NO_TASK != pos->attempt_connect_task)
433 pos->attempt_connect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_connect_attempt),
440 * Discard peer entries for greylisted peers
441 * where the greylisting has expired.
443 * @param cls 'struct Peer' to greylist
444 * @param tc scheduler context
447 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
449 struct Peer *pos = cls;
450 struct GNUNET_TIME_Relative rem;
452 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
453 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
454 if (0 == rem.rel_value_us)
456 schedule_attempt_connect (pos);
460 pos->greylist_clean_task =
461 GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
463 if ((GNUNET_NO == pos->is_friend) && (GNUNET_NO == pos->is_connected) &&
464 (NULL == pos->hello))
466 free_peer (NULL, &pos->pid, pos);
473 * Create a new entry in the peer list.
475 * @param peer identity of the new entry
476 * @param hello hello message, can be NULL
477 * @param is_friend is the new entry for a friend?
478 * @return the new entry
481 make_peer (const struct GNUNET_PeerIdentity *peer,
482 const struct GNUNET_HELLO_Message *hello, int is_friend)
486 ret = GNUNET_new (struct Peer);
488 ret->is_friend = is_friend;
491 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
492 memcpy (ret->hello, hello, GNUNET_HELLO_size (hello));
494 GNUNET_break (GNUNET_OK ==
495 GNUNET_CONTAINER_multipeermap_put (peers, peer,
497 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
503 * Setup bloom filter for the given peer entry.
505 * @param peer entry to initialize
508 setup_filter (struct Peer *peer)
510 struct GNUNET_HashCode hc;
512 /* 2^{-5} chance of not sending a HELLO to a peer is
513 * acceptably small (if the filter is 50% full);
514 * 64 bytes of memory are small compared to the rest
515 * of the data structure and would only really become
516 * "useless" once a HELLO has been passed on to ~100
517 * other peers, which is likely more than enough in
518 * any case; hence 64, 5 as bloomfilter parameters. */
519 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
520 peer->filter_expiration =
521 GNUNET_TIME_relative_to_absolute
522 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
523 /* never send a peer its own HELLO */
524 GNUNET_CRYPTO_hash (&peer->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
525 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &hc);
530 * Function to fill send buffer with HELLO.
532 * @param cls 'struct Peer' of the target peer
533 * @param size number of bytes available in @a buf
534 * @param buf where the callee should write the message
535 * @return number of bytes written to @a buf
538 hello_advertising_ready (void *cls, size_t size, void *buf);
542 * Closure for 'find_advertisable_hello'.
544 struct FindAdvHelloContext
548 * Peer we want to advertise to.
553 * Where to store the result (peer selected for advertising).
558 * Maximum HELLO size we can use right now.
562 struct GNUNET_TIME_Relative next_adv;
567 * Find a peer that would be reasonable for advertising.
570 * @param pid identity of a peer
571 * @param value 'struct Peer*' for the peer we are considering
572 * @return GNUNET_YES (continue iteration)
575 find_advertisable_hello (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
577 struct FindAdvHelloContext *fah = cls;
578 struct Peer *pos = value;
579 struct GNUNET_TIME_Relative rst_time;
580 struct GNUNET_HashCode hc;
583 if (pos == fah->peer)
585 if (pos->hello == NULL)
587 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
588 if (0 == rst_time.rel_value_us)
590 /* time to discard... */
591 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
594 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
595 hs = GNUNET_HELLO_size (pos->hello);
596 if (hs > fah->max_size)
598 GNUNET_CRYPTO_hash (&fah->peer->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
600 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
608 * Calculate when we would like to send the next HELLO to this
609 * peer and ask for it.
611 * @param cls for which peer to schedule the HELLO
612 * @param tc task context
615 schedule_next_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
617 struct Peer *pl = cls;
618 struct FindAdvHelloContext fah;
620 struct GNUNET_TIME_Relative delay;
622 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
623 GNUNET_assert (GNUNET_YES == pl->is_connected);
624 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
625 return; /* we're out of here */
626 if (pl->hello_req != NULL)
627 return; /* did not finish sending the previous one */
628 /* find applicable HELLOs */
631 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
632 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
633 GNUNET_CONTAINER_multipeermap_iterate (peers, &find_advertisable_hello, &fah);
634 pl->hello_delay_task =
635 GNUNET_SCHEDULER_add_delayed (fah.next_adv, &schedule_next_hello, pl);
636 if (fah.result == NULL)
638 next_want = GNUNET_HELLO_size (fah.result->hello);
639 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
640 if (0 == delay.rel_value_us)
644 GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES,
645 GNUNET_CORE_PRIO_BEST_EFFORT,
646 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
648 &hello_advertising_ready, pl);
654 * Cancel existing requests for sending HELLOs to this peer
655 * and recalculate when we should send HELLOs to it based
656 * on our current state (something changed!).
658 * @param cls closure, 'struct Peer' to skip, or NULL
659 * @param pid identity of a peer
660 * @param value 'struct Peer*' for the peer
661 * @return GNUNET_YES (always)
664 reschedule_hellos (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
666 struct Peer *peer = value;
667 struct Peer *skip = cls;
671 if (!peer->is_connected)
673 if (peer->hello_req != NULL)
675 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
676 peer->hello_req = NULL;
678 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
680 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
681 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
683 peer->hello_delay_task =
684 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
690 * Method called whenever a peer connects.
693 * @param peer peer identity this notification is about
696 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
701 "Core told us that we are connecting to `%s'\n",
703 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
707 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
708 connection_count, GNUNET_NO);
709 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
712 pos = make_peer (peer, NULL, GNUNET_NO);
713 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
717 GNUNET_assert (GNUNET_NO == pos->is_connected);
718 pos->greylisted_until.abs_value_us = 0; /* remove greylisting */
720 pos->is_connected = GNUNET_YES;
721 pos->connect_attempts = 0; /* re-set back-off factor */
724 if ((friend_count == minimum_friend_count - 1) &&
725 (GNUNET_YES != friends_only))
728 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
729 friend_count, GNUNET_NO);
731 reschedule_hellos (NULL, peer, pos);
736 * Try to add more peers to our connection set.
738 * @param cls closure, not used
739 * @param pid identity of a peer
740 * @param value 'struct Peer*' for the peer
741 * @return GNUNET_YES (continue to iterate)
744 try_add_peers (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
746 struct Peer *pos = value;
748 schedule_attempt_connect (pos);
754 * Last task run during shutdown. Disconnects us from
755 * the transport and core.
757 * @param cls unused, NULL
758 * @param tc scheduler context
761 add_peer_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
763 add_task = GNUNET_SCHEDULER_NO_TASK;
765 GNUNET_CONTAINER_multipeermap_iterate (peers, &try_add_peers, NULL);
770 * Method called whenever a peer disconnects.
773 * @param peer peer identity this notification is about
776 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
780 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Core told us that we disconnected from `%s'\n",
785 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
791 if (pos->is_connected != GNUNET_YES)
796 pos->is_connected = GNUNET_NO;
798 if (NULL != pos->hello_req)
800 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
801 pos->hello_req = NULL;
803 if (GNUNET_SCHEDULER_NO_TASK != pos->hello_delay_task)
805 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
806 pos->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
808 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
809 connection_count, GNUNET_NO);
813 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
814 friend_count, GNUNET_NO);
816 if (((connection_count < target_connection_count) ||
817 (friend_count < minimum_friend_count)) &&
818 (GNUNET_SCHEDULER_NO_TASK == add_task))
819 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
820 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
821 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
826 * Iterator called on each address.
828 * @param cls flag that we will set if we see any addresses
829 * @param address the address of the peer
830 * @param expiration when will the given address expire
831 * @return GNUNET_SYSERR always, to terminate iteration
834 address_iterator (void *cls, const struct GNUNET_HELLO_Address *address,
835 struct GNUNET_TIME_Absolute expiration)
840 return GNUNET_SYSERR;
845 * We've gotten a HELLO from another peer. Consider it for
848 * @param hello the HELLO we got
851 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
854 struct GNUNET_PeerIdentity pid;
855 struct GNUNET_TIME_Absolute dt;
856 struct GNUNET_HELLO_Message *nh;
860 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
865 if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
866 return; /* that's me! */
867 have_address = GNUNET_NO;
868 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &address_iterator,
870 if (GNUNET_NO == have_address)
871 return; /* no point in advertising this one... */
872 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
875 peer = make_peer (&pid, hello, GNUNET_NO);
877 else if (peer->hello != NULL)
879 dt = GNUNET_HELLO_equals (peer->hello, hello, GNUNET_TIME_absolute_get ());
880 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
881 return; /* nothing new here */
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Found `%s' from peer `%s' for advertising\n", "HELLO",
886 if (peer->hello != NULL)
888 nh = GNUNET_HELLO_merge (peer->hello, hello);
889 GNUNET_free (peer->hello);
894 size = GNUNET_HELLO_size (hello);
895 peer->hello = GNUNET_malloc (size);
896 memcpy (peer->hello, hello, size);
898 if (peer->filter != NULL)
899 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
901 /* since we have a new HELLO to pick from, re-schedule all
902 * HELLO requests that are not bound by the HELLO send rate! */
903 GNUNET_CONTAINER_multipeermap_iterate (peers, &reschedule_hellos, peer);
908 * PEERINFO calls this function to let us know about a possible peer
909 * that we might want to connect to.
911 * @param cls closure (not used)
912 * @param peer potential peer to connect to
913 * @param hello HELLO for this peer (or NULL)
914 * @param err_msg NULL if successful, otherwise contains error message
917 process_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
918 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
925 _("Error in communication with PEERINFO service: %s\n"),
927 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
928 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO, &process_peer, NULL);
931 GNUNET_assert (peer != NULL);
932 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
933 return; /* that's me! */
936 /* free existing HELLO, if any */
937 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
940 GNUNET_free_non_null (pos->hello);
942 if (pos->filter != NULL)
944 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
947 if ((GNUNET_NO == pos->is_connected) && (GNUNET_NO == pos->is_friend) &&
949 GNUNET_TIME_absolute_get_remaining (pos->
950 greylisted_until).rel_value_us))
951 free_peer (NULL, &pos->pid, pos);
955 consider_for_advertising (hello);
956 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
958 pos = make_peer (peer, hello, GNUNET_NO);
959 GNUNET_assert (NULL != pos);
960 if (GNUNET_YES == pos->is_connected)
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already connected to peer `%s'\n",
966 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value_us > 0)
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already tried peer `%s' recently\n",
970 return; /* peer still greylisted */
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Considering connecting to peer `%s'\n",
974 schedule_attempt_connect (pos);
979 * Function called after #GNUNET_CORE_connect has succeeded
980 * (or failed for good).
983 * @param my_id ID of this peer, NULL if we failed
986 core_init (void *cls,
987 const struct GNUNET_PeerIdentity *my_id)
991 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
993 ("Failed to connect to core service, can not manage topology!\n"));
994 GNUNET_SCHEDULER_shutdown ();
997 my_identity = *my_id;
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 GNUNET_i2s (my_id));
1001 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO,
1002 &process_peer, NULL);
1007 * Process friend found in FRIENDS file.
1009 * @param cls pointer to an `unsigned int` to be incremented per friend found
1010 * @param pid identity of the friend
1013 handle_friend (void *cls,
1014 const struct GNUNET_PeerIdentity *pid)
1016 unsigned int *entries_found = cls;
1019 if (0 == memcmp (pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1021 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1022 _("Found myself `%s' in friend list (useless, ignored)\n"),
1027 fl = make_peer (pid, NULL, GNUNET_YES);
1028 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1029 _("Found friend `%s' in configuration\n"),
1030 GNUNET_i2s (&fl->pid));
1035 * Read the friends file.
1038 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1040 unsigned int entries_found;
1044 GNUNET_FRIENDS_parse (cfg,
1048 if ((friends_only) || (minimum_friend_count > 0))
1049 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1050 _("Encountered errors parsing friends list!\n"));
1052 GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
1053 entries_found, GNUNET_NO);
1054 if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1056 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1057 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1059 if ((minimum_friend_count > target_connection_count) &&
1060 (friends_only == GNUNET_NO))
1062 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1063 _("More friendly connections required than target total number of connections.\n"));
1069 * This function is called whenever an encrypted HELLO message is
1072 * @param cls closure
1073 * @param other the other peer involved (sender or receiver, NULL
1074 * for loopback messages where we are both sender and receiver)
1075 * @param message the actual HELLO message
1076 * @return GNUNET_OK to keep the connection open,
1077 * GNUNET_SYSERR to close it (signal serious error)
1080 handle_encrypted_hello (void *cls, const struct GNUNET_PeerIdentity *other,
1081 const struct GNUNET_MessageHeader *message)
1084 struct GNUNET_PeerIdentity pid;
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received encrypted `%s' from peer `%s'",
1087 "HELLO", GNUNET_i2s (other));
1089 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1091 GNUNET_break_op (0);
1092 return GNUNET_SYSERR;
1094 GNUNET_STATISTICS_update (stats, gettext_noop ("# HELLO messages received"),
1096 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
1099 if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
1104 if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
1106 if ((GNUNET_YES != peer->is_friend) &&
1107 (friend_count < minimum_friend_count))
1110 if (transport != NULL)
1111 GNUNET_TRANSPORT_offer_hello (transport, message, NULL, NULL);
1117 * Function to fill send buffer with HELLO.
1119 * @param cls 'struct Peer' of the target peer
1120 * @param size number of bytes available in buf
1121 * @param buf where the callee should write the message
1122 * @return number of bytes written to buf
1125 hello_advertising_ready (void *cls, size_t size, void *buf)
1127 struct Peer *pl = cls;
1128 struct FindAdvHelloContext fah;
1130 struct GNUNET_HashCode hc;
1132 pl->hello_req = NULL;
1133 GNUNET_assert (GNUNET_YES == pl->is_connected);
1134 /* find applicable HELLOs */
1137 fah.max_size = size;
1138 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1139 GNUNET_CONTAINER_multipeermap_iterate (peers, &find_advertisable_hello, &fah);
1141 if (fah.result != NULL)
1143 want = GNUNET_HELLO_size (fah.result->hello);
1144 GNUNET_assert (want <= size);
1145 memcpy (buf, fah.result->hello, want);
1146 GNUNET_CRYPTO_hash (&pl->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
1147 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &hc);
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' with %u bytes", "HELLO",
1149 (unsigned int) want);
1150 GNUNET_STATISTICS_update (stats,
1151 gettext_noop ("# HELLO messages gossipped"), 1,
1155 if (pl->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
1156 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1157 pl->next_hello_allowed =
1158 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1159 pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1165 * Last task run during shutdown. Disconnects us from
1166 * the transport and core.
1168 * @param cls unused, NULL
1169 * @param tc scheduler context
1172 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1174 if (NULL != peerinfo_notify)
1176 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1177 peerinfo_notify = NULL;
1179 GNUNET_TRANSPORT_disconnect (transport);
1183 GNUNET_CORE_disconnect (handle);
1187 if (GNUNET_SCHEDULER_NO_TASK != add_task)
1189 GNUNET_SCHEDULER_cancel (add_task);
1190 add_task = GNUNET_SCHEDULER_NO_TASK;
1192 GNUNET_CONTAINER_multipeermap_iterate (peers, &free_peer, NULL);
1193 GNUNET_CONTAINER_multipeermap_destroy (peers);
1197 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1204 * Main function that will be run.
1206 * @param cls closure
1207 * @param args remaining command-line arguments
1208 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1209 * @param c configuration
1212 run (void *cls, char *const *args, const char *cfgfile,
1213 const struct GNUNET_CONFIGURATION_Handle *c)
1215 static struct GNUNET_CORE_MessageHandler handlers[] = {
1216 {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1219 unsigned long long opt;
1222 stats = GNUNET_STATISTICS_create ("topology", cfg);
1224 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY");
1226 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "MINIMUM-FRIENDS",
1229 minimum_friend_count = (unsigned int) opt;
1231 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY",
1232 "TARGET-CONNECTION-COUNT", &opt))
1234 target_connection_count = (unsigned int) opt;
1235 peers = GNUNET_CONTAINER_multipeermap_create (target_connection_count * 2, GNUNET_NO);
1237 if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0))
1238 read_friends_file (cfg);
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1240 "Topology would like %u connections with at least %u friends\n",
1241 target_connection_count, minimum_friend_count);
1242 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
1243 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
1244 transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
1246 GNUNET_CORE_connect (cfg, NULL, &core_init, &connect_notify,
1247 &disconnect_notify, NULL, GNUNET_NO, NULL, GNUNET_NO,
1249 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
1251 if (NULL == transport)
1253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1254 _("Failed to connect to `%s' service.\n"), "transport");
1255 GNUNET_SCHEDULER_shutdown ();
1260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1261 _("Failed to connect to `%s' service.\n"), "core");
1262 GNUNET_SCHEDULER_shutdown ();
1269 * The main function for the topology daemon.
1271 * @param argc number of arguments from the command line
1272 * @param argv command line arguments
1273 * @return 0 ok, 1 on error
1276 main (int argc, char *const *argv)
1278 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1279 GNUNET_GETOPT_OPTION_END
1283 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1288 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",
1290 ("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1291 options, &run, NULL)) ? 0 : 1;
1292 GNUNET_free ((void*) argv);
1301 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1303 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1305 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1306 mallopt (M_TOP_PAD, 1 * 1024);
1311 /* end of gnunet-daemon-topology.c */