2 This file is part of GNUnet.
3 (C) 2007, 2008, 2009, 2010 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
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"
35 #include "gnunet_util_lib.h"
38 #define DEBUG_TOPOLOGY GNUNET_YES
41 * For how long do we blacklist a peer after a failed connection
44 #define GREYLIST_AFTER_ATTEMPT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 * For how long do we blacklist a friend after a failed connection
50 #define GREYLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
53 * For how long do we blacklist anyone under any cirumstances after a failed connection
56 #define GREYLIST_AFTER_ATTEMPT_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
59 * For how long do we blacklist anyone under any cirumstances after a failed connection
62 #define GREYLIST_AFTER_ATTEMPT_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
65 * How often do we at most advertise any HELLO to a peer?
67 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
70 * How often do we at most advertise the same HELLO to the same peer?
72 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
76 * Record for neighbours, friends and blacklisted peers.
81 * Which peer is this entry about?
83 struct GNUNET_PeerIdentity pid;
86 * Our handle for the request to transmit HELLOs to this peer; NULL
87 * if no such request is pending.
89 struct GNUNET_CORE_TransmitHandle *hello_req;
92 * Our handle for the request to connect to this peer; NULL if no
93 * such request is pending.
95 struct GNUNET_CORE_PeerRequestHandle *connect_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 * ID of task we use to clear peers from the greylist.
133 GNUNET_SCHEDULER_TaskIdentifier greylist_clean_task;
136 * How often have we tried so far?
138 unsigned int connect_attempts;
141 * Is this peer listed here because he is a friend?
146 * Are we connected to this peer right now?
154 * Our peerinfo notification context. We use notification
155 * to instantly learn about new peers as they are discovered.
157 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
162 static const struct GNUNET_CONFIGURATION_Handle *cfg;
165 * Handle to the core API.
167 static struct GNUNET_CORE_Handle *handle;
170 * Handle to the transport API.
172 static struct GNUNET_TRANSPORT_Handle *transport;
175 * Identity of this peer.
177 static struct GNUNET_PeerIdentity my_identity;
180 * All of our friends, all of our current neighbours and all peers for
181 * which we have HELLOs. So pretty much everyone. Maps peer identities
182 * to 'struct Peer *' values.
184 static struct GNUNET_CONTAINER_MultiHashMap *peers;
187 * Handle for reporting statistics.
189 static struct GNUNET_STATISTICS_Handle *stats;
192 * Blacklist (NULL if we have none).
194 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
197 * Task scheduled to try to add peers.
199 static GNUNET_SCHEDULER_TaskIdentifier add_task;
202 * Flag to disallow non-friend connections (pure F2F mode).
204 static int friends_only;
207 * Minimum number of friends to have in the
208 * connection set before we allow non-friends.
210 static unsigned int minimum_friend_count;
213 * Number of peers (friends and others) that we are currently connected to.
215 static unsigned int connection_count;
218 * Target number of connections.
220 static unsigned int target_connection_count;
223 * Number of friends that we are currently connected to.
225 static unsigned int friend_count;
228 * Should the topology daemon try to establish connections?
230 static int autoconnect;
234 * Function that decides if a connection is acceptable or not.
235 * If we have a blacklist, only friends are allowed, so the check
239 * @param pid peer to approve or disapproave
240 * @return GNUNET_OK if the connection is allowed
243 blacklist_check (void *cls, const struct GNUNET_PeerIdentity *pid)
247 pos = GNUNET_CONTAINER_multihashmap_get (peers, &pid->hashPubKey);
248 if ((pos != NULL) && (pos->is_friend == GNUNET_YES))
250 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers blacklisted"), 1,
252 return GNUNET_SYSERR;
257 * Whitelist all peers that we blacklisted; we've passed
258 * the minimum number of friends.
263 if (blacklist != NULL)
265 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
272 * Function called by core when our request to connect was transmitted.
274 * @param cls the 'struct Peer' for which we issued the connect request
275 * @param success was the request transmitted
278 connect_completed_callback (void *cls, int success)
280 struct Peer *pos = cls;
282 pos->connect_req = NULL;
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)
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Determined that `%s' is not allowed to connect (not a friend)\n",
305 GNUNET_i2s (&peer->pid));
307 return GNUNET_SYSERR;
309 if (friend_count >= minimum_friend_count)
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
314 GNUNET_i2s (&peer->pid));
316 return GNUNET_SYSERR;
321 * Free all resources associated with the given peer.
323 * @param cls closure (not used)
324 * @param pid identity of the peer
325 * @param value peer to free
326 * @return GNUNET_YES (always: continue to iterate)
329 free_peer (void *cls, const GNUNET_HashCode * pid, void *value)
331 struct Peer *pos = value;
333 GNUNET_break (GNUNET_OK ==
334 GNUNET_CONTAINER_multihashmap_remove (peers, pid, pos));
335 if (pos->hello_req != NULL)
336 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
337 if (pos->connect_req != NULL)
338 GNUNET_CORE_peer_request_connect_cancel (pos->connect_req);
339 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
340 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
341 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
342 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
343 GNUNET_free_non_null (pos->hello);
344 if (pos->filter != NULL)
345 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
352 * Discard peer entries for greylisted peers
353 * where the greylisting has expired.
355 * @param cls 'struct Peer' to greylist
356 * @param tc scheduler context
359 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
363 * Try to connect to the specified peer.
365 * @param pos peer to connect to
368 attempt_connect (struct Peer *pos)
370 struct GNUNET_TIME_Relative rem;
372 if ((connection_count >= target_connection_count) &&
373 (friend_count >= minimum_friend_count))
375 if (GNUNET_YES == pos->is_connected)
377 if (GNUNET_OK != is_connection_allowed (pos))
379 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
381 if (GNUNET_YES == pos->is_friend)
382 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
384 rem = GREYLIST_AFTER_ATTEMPT;
385 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
386 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
387 if (pos->connect_attempts > 30)
388 pos->connect_attempts = 30;
389 rem = GNUNET_TIME_relative_multiply (rem, 1 << (++pos->connect_attempts));
390 rem = GNUNET_TIME_relative_max (rem, GREYLIST_AFTER_ATTEMPT_MIN);
391 rem = GNUNET_TIME_relative_min (rem, GREYLIST_AFTER_ATTEMPT_MAX);
392 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
393 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
394 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
395 pos->greylist_clean_task =
396 GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to connect to `%s'\n",
399 GNUNET_i2s (&pos->pid));
401 GNUNET_STATISTICS_update (stats,
402 gettext_noop ("# connect requests issued to core"),
405 GNUNET_CORE_peer_request_connect (handle, &pos->pid,
406 &connect_completed_callback, pos);
411 * Discard peer entries for greylisted peers
412 * where the greylisting has expired.
414 * @param cls 'struct Peer' to greylist
415 * @param tc scheduler context
418 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
420 struct Peer *pos = cls;
421 struct GNUNET_TIME_Relative rem;
423 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
424 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
425 if (rem.rel_value == 0)
427 attempt_connect (pos);
431 pos->greylist_clean_task =
432 GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
434 if ((GNUNET_NO == pos->is_friend) && (GNUNET_NO == pos->is_connected) &&
435 (NULL == pos->hello))
437 free_peer (NULL, &pos->pid.hashPubKey, pos);
444 * Create a new entry in the peer list.
446 * @param peer identity of the new entry
447 * @param hello hello message, can be NULL
448 * @param is_friend is the new entry for a friend?
449 * @return the new entry
452 make_peer (const struct GNUNET_PeerIdentity *peer,
453 const struct GNUNET_HELLO_Message *hello, int is_friend)
457 ret = GNUNET_malloc (sizeof (struct Peer));
459 ret->is_friend = is_friend;
462 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
463 memcpy (ret->hello, hello, GNUNET_HELLO_size (hello));
465 GNUNET_break (GNUNET_OK ==
466 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey,
468 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
474 * Setup bloom filter for the given peer entry.
476 * @param peer entry to initialize
479 setup_filter (struct Peer *peer)
481 /* 2^{-5} chance of not sending a HELLO to a peer is
482 * acceptably small (if the filter is 50% full);
483 * 64 bytes of memory are small compared to the rest
484 * of the data structure and would only really become
485 * "useless" once a HELLO has been passed on to ~100
486 * other peers, which is likely more than enough in
487 * any case; hence 64, 5 as bloomfilter parameters. */
488 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
489 peer->filter_expiration =
490 GNUNET_TIME_relative_to_absolute
491 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
492 /* never send a peer its own HELLO */
493 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
498 * Function to fill send buffer with HELLO.
500 * @param cls 'struct Peer' of the target peer
501 * @param size number of bytes available in buf
502 * @param buf where the callee should write the message
503 * @return number of bytes written to buf
506 hello_advertising_ready (void *cls, size_t size, void *buf);
510 * Closure for 'find_advertisable_hello'.
512 struct FindAdvHelloContext
516 * Peer we want to advertise to.
521 * Where to store the result (peer selected for advertising).
526 * Maximum HELLO size we can use right now.
530 struct GNUNET_TIME_Relative next_adv;
535 * Find a peer that would be reasonable for advertising.
538 * @param pid identity of a peer
539 * @param value 'struct Peer*' for the peer we are considering
540 * @return GNUNET_YES (continue iteration)
543 find_advertisable_hello (void *cls, const GNUNET_HashCode * pid, void *value)
545 struct FindAdvHelloContext *fah = cls;
546 struct Peer *pos = value;
547 struct GNUNET_TIME_Relative rst_time;
550 if (pos == fah->peer)
552 if (pos->hello == NULL)
554 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
555 if (0 == rst_time.rel_value)
557 /* time to discard... */
558 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
561 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
562 hs = GNUNET_HELLO_size (pos->hello);
563 if (hs > fah->max_size)
566 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
567 &fah->peer->pid.hashPubKey))
574 * Calculate when we would like to send the next HELLO to this
575 * peer and ask for it.
577 * @param cls for which peer to schedule the HELLO
578 * @param tc task context
581 schedule_next_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
583 struct Peer *pl = cls;
584 struct FindAdvHelloContext fah;
586 struct GNUNET_TIME_Relative delay;
588 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
589 GNUNET_assert (GNUNET_YES == pl->is_connected);
590 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
591 return; /* we're out of here */
592 if (pl->hello_req != NULL)
593 return; /* did not finish sending the previous one */
594 /* find applicable HELLOs */
597 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
598 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
599 GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
600 pl->hello_delay_task =
601 GNUNET_SCHEDULER_add_delayed (fah.next_adv, &schedule_next_hello, pl);
602 if (fah.result == NULL)
604 next_want = GNUNET_HELLO_size (fah.result->hello);
605 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
606 if (delay.rel_value == 0)
610 GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES, 0,
611 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
613 &hello_advertising_ready, pl);
619 * Cancel existing requests for sending HELLOs to this peer
620 * and recalculate when we should send HELLOs to it based
621 * on our current state (something changed!).
623 * @param cls closure, 'struct Peer' to skip, or NULL
624 * @param pid identity of a peer
625 * @param value 'struct Peer*' for the peer
626 * @return GNUNET_YES (always)
629 reschedule_hellos (void *cls, const GNUNET_HashCode * pid, void *value)
631 struct Peer *peer = value;
632 struct Peer *skip = cls;
636 if (!peer->is_connected)
638 if (peer->hello_req != NULL)
640 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
641 peer->hello_req = NULL;
643 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
645 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
646 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
648 peer->hello_delay_task =
649 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
655 * Method called whenever a peer connects.
658 * @param peer peer identity this notification is about
659 * @param atsi performance data
662 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
663 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "Core told us that we are connecting to `%s'\n",
672 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
676 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
677 connection_count, GNUNET_NO);
678 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
681 pos = make_peer (peer, NULL, GNUNET_NO);
682 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
686 GNUNET_assert (GNUNET_NO == pos->is_connected);
687 pos->greylisted_until.abs_value = 0; /* remove greylisting */
689 pos->is_connected = GNUNET_YES;
690 pos->connect_attempts = 0; /* re-set back-off factor */
693 if ((friend_count == minimum_friend_count - 1) &&
694 (GNUNET_YES != friends_only))
697 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
698 friend_count, GNUNET_NO);
700 reschedule_hellos (NULL, &peer->hashPubKey, pos);
705 * Try to add more peers to our connection set.
707 * @param cls closure, not used
708 * @param pid identity of a peer
709 * @param value 'struct Peer*' for the peer
710 * @return GNUNET_YES (continue to iterate)
713 try_add_peers (void *cls, const GNUNET_HashCode * pid, void *value)
715 struct Peer *pos = value;
717 attempt_connect (pos);
723 * Last task run during shutdown. Disconnects us from
724 * the transport and core.
726 * @param cls unused, NULL
727 * @param tc scheduler context
730 add_peer_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
732 add_task = GNUNET_SCHEDULER_NO_TASK;
734 GNUNET_CONTAINER_multihashmap_iterate (peers, &try_add_peers, NULL);
738 * Method called whenever a peer disconnects.
741 * @param peer peer identity this notification is about
744 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
748 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Core told us that we disconnected from `%s'\n",
755 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
761 if (pos->is_connected != GNUNET_YES)
766 pos->is_connected = GNUNET_NO;
768 if (NULL != pos->hello_req)
770 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
771 pos->hello_req = NULL;
773 if (GNUNET_SCHEDULER_NO_TASK != pos->hello_delay_task)
775 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
776 pos->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
778 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
779 connection_count, GNUNET_NO);
783 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
784 friend_count, GNUNET_NO);
786 if (((connection_count < target_connection_count) ||
787 (friend_count < minimum_friend_count)) &&
788 (GNUNET_SCHEDULER_NO_TASK == add_task))
789 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
790 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
791 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
796 * Iterator called on each address.
798 * @param cls flag that we will set if we see any addresses
799 * @param tname name of the transport
800 * @param expiration when will the given address expire
801 * @param addr the address of the peer
802 * @param addrlen number of bytes in addr
803 * @return GNUNET_SYSERR always, to terminate iteration
806 address_iterator (void *cls, const char *tname,
807 struct GNUNET_TIME_Absolute expiration, const void *addr,
813 return GNUNET_SYSERR;
818 * We've gotten a HELLO from another peer. Consider it for
821 * @param hello the HELLO we got
824 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
827 struct GNUNET_PeerIdentity pid;
828 struct GNUNET_TIME_Absolute dt;
829 struct GNUNET_HELLO_Message *nh;
833 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
838 if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
839 return; /* that's me! */
840 have_address = GNUNET_NO;
841 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &address_iterator,
843 if (GNUNET_NO == have_address)
844 return; /* no point in advertising this one... */
845 peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
848 peer = make_peer (&pid, hello, GNUNET_NO);
850 else if (peer->hello != NULL)
852 dt = GNUNET_HELLO_equals (peer->hello, hello, GNUNET_TIME_absolute_get ());
853 if (dt.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
854 return; /* nothing new here */
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
858 "Found `%s' from peer `%s' for advertising\n", "HELLO",
861 if (peer->hello != NULL)
863 nh = GNUNET_HELLO_merge (peer->hello, hello);
864 GNUNET_free (peer->hello);
869 size = GNUNET_HELLO_size (hello);
870 peer->hello = GNUNET_malloc (size);
871 memcpy (peer->hello, hello, size);
873 if (peer->filter != NULL)
874 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
876 /* since we have a new HELLO to pick from, re-schedule all
877 * HELLO requests that are not bound by the HELLO send rate! */
878 GNUNET_CONTAINER_multihashmap_iterate (peers, &reschedule_hellos, peer);
883 * PEERINFO calls this function to let us know about a possible peer
884 * that we might want to connect to.
886 * @param cls closure (not used)
887 * @param peer potential peer to connect to
888 * @param hello HELLO for this peer (or NULL)
889 * @param err_msg NULL if successful, otherwise contains error message
892 process_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
893 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 _("Error in communication with PEERINFO service: %s\n"),
902 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
903 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
906 GNUNET_assert (peer != NULL);
907 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
908 return; /* that's me! */
911 /* free existing HELLO, if any */
912 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
915 GNUNET_free_non_null (pos->hello);
917 if (pos->filter != NULL)
919 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
922 if ((!pos->is_connected) && (!pos->is_friend) &&
924 GNUNET_TIME_absolute_get_remaining (pos->
925 greylisted_until).rel_value))
926 free_peer (NULL, &pos->pid.hashPubKey, pos);
930 consider_for_advertising (hello);
931 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
933 pos = make_peer (peer, hello, GNUNET_NO);
934 GNUNET_assert (NULL != pos);
935 if (GNUNET_YES == pos->is_connected)
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already connected to peer `%s'\n",
943 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already tried peer `%s' recently\n",
949 return; /* peer still greylisted */
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Considering connecting to peer `%s'\n",
955 attempt_connect (pos);
960 * Function called after GNUNET_CORE_connect has succeeded
961 * (or failed for good).
964 * @param server handle to the server, NULL if we failed
965 * @param my_id ID of this peer, NULL if we failed
966 * @param publicKey public key of this peer, NULL if we failed
969 core_init (void *cls, struct GNUNET_CORE_Handle *server,
970 const struct GNUNET_PeerIdentity *my_id,
971 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
975 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
977 ("Failed to connect to core service, can not manage topology!\n"));
978 GNUNET_SCHEDULER_shutdown ();
982 my_identity = *my_id;
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am peer `%s'\n", GNUNET_i2s (my_id));
986 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
991 * Read the friends file.
994 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
999 struct GNUNET_PeerIdentity pid;
1001 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1002 unsigned int entries_found;
1006 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
1008 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1009 _("Option `%s' in section `%s' not specified!\n"), "FRIENDS",
1013 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1014 GNUNET_DISK_fn_write (fn, NULL, 0,
1015 GNUNET_DISK_PERM_USER_READ |
1016 GNUNET_DISK_PERM_USER_WRITE);
1017 if (0 != STAT (fn, &frstat))
1019 if ((friends_only) || (minimum_friend_count > 0))
1020 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1021 _("Could not read friends list `%s'\n"), fn);
1025 if (frstat.st_size == 0)
1027 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Friends file `%s' is empty.\n"),
1032 data = GNUNET_malloc_large (frstat.st_size);
1035 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1036 _("Failed to read friends list from `%s': out of memory\n"),
1041 if (frstat.st_size != GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1043 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1044 _("Failed to read friends list from `%s'\n"), fn);
1051 while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1053 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1055 frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1057 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1060 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1062 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1064 ("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1065 (unsigned long long) pos);
1067 while ((pos < frstat.st_size) && (!isspace ((unsigned char) data[pos])))
1071 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1073 GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1075 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1077 ("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1078 (unsigned long long) pos, &enc);
1082 if (0 != memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1085 fl = make_peer (&pid, NULL, GNUNET_YES);
1086 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1087 _("Found friend `%s' in configuration\n"),
1088 GNUNET_i2s (&fl->pid));
1092 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1093 _("Found myself `%s' in friend list (useless, ignored)\n"),
1097 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1098 while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1103 GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
1104 entries_found, GNUNET_NO);
1105 if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1107 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1109 ("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1111 if ((minimum_friend_count > target_connection_count) &&
1112 (friends_only == GNUNET_NO))
1114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1116 ("More friendly connections required than target total number of connections.\n"));
1122 * This function is called whenever an encrypted HELLO message is
1125 * @param cls closure
1126 * @param other the other peer involved (sender or receiver, NULL
1127 * for loopback messages where we are both sender and receiver)
1128 * @param message the actual HELLO message
1129 * @param atsi performance data
1130 * @return GNUNET_OK to keep the connection open,
1131 * GNUNET_SYSERR to close it (signal serious error)
1134 handle_encrypted_hello (void *cls, const struct GNUNET_PeerIdentity *other,
1135 const struct GNUNET_MessageHeader *message,
1136 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1139 struct GNUNET_PeerIdentity pid;
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received encrypted `%s' from peer `%s'",
1143 "HELLO", GNUNET_i2s (other));
1146 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1148 GNUNET_break_op (0);
1149 return GNUNET_SYSERR;
1151 GNUNET_STATISTICS_update (stats, gettext_noop ("# HELLO messages received"),
1153 peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
1156 if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
1161 if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
1163 if ((GNUNET_YES != peer->is_friend) &&
1164 (friend_count < minimum_friend_count))
1167 if (transport != NULL)
1168 GNUNET_TRANSPORT_offer_hello (transport, message, NULL, NULL);
1174 * Function to fill send buffer with HELLO.
1176 * @param cls 'struct Peer' of the target peer
1177 * @param size number of bytes available in buf
1178 * @param buf where the callee should write the message
1179 * @return number of bytes written to buf
1182 hello_advertising_ready (void *cls, size_t size, void *buf)
1184 struct Peer *pl = cls;
1185 struct FindAdvHelloContext fah;
1188 pl->hello_req = NULL;
1189 GNUNET_assert (GNUNET_YES == pl->is_connected);
1190 /* find applicable HELLOs */
1193 fah.max_size = size;
1194 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1195 GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
1197 if (fah.result != NULL)
1199 want = GNUNET_HELLO_size (fah.result->hello);
1200 GNUNET_assert (want <= size);
1201 memcpy (buf, fah.result->hello, want);
1202 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &pl->pid.hashPubKey);
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' with %u bytes", "HELLO",
1205 (unsigned int) want);
1207 GNUNET_STATISTICS_update (stats,
1208 gettext_noop ("# HELLO messages gossipped"), 1,
1212 if (pl->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
1213 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1214 pl->next_hello_allowed =
1215 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1216 pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1222 * Last task run during shutdown. Disconnects us from
1223 * the transport and core.
1225 * @param cls unused, NULL
1226 * @param tc scheduler context
1229 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1231 if (NULL != peerinfo_notify)
1233 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1234 peerinfo_notify = NULL;
1236 if (GNUNET_SCHEDULER_NO_TASK != add_task)
1238 GNUNET_SCHEDULER_cancel (add_task);
1239 add_task = GNUNET_SCHEDULER_NO_TASK;
1241 GNUNET_TRANSPORT_disconnect (transport);
1243 GNUNET_CONTAINER_multihashmap_iterate (peers, &free_peer, NULL);
1244 GNUNET_CONTAINER_multihashmap_destroy (peers);
1247 GNUNET_CORE_disconnect (handle);
1253 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1260 * Main function that will be run.
1262 * @param cls closure
1263 * @param args remaining command-line arguments
1264 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1265 * @param c configuration
1268 run (void *cls, char *const *args, const char *cfgfile,
1269 const struct GNUNET_CONFIGURATION_Handle *c)
1271 static struct GNUNET_CORE_MessageHandler handlers[] = {
1272 {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1275 unsigned long long opt;
1278 stats = GNUNET_STATISTICS_create ("topology", cfg);
1280 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "AUTOCONNECT");
1282 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY");
1284 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "MINIMUM-FRIENDS",
1287 minimum_friend_count = (unsigned int) opt;
1289 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY",
1290 "TARGET-CONNECTION-COUNT", &opt))
1292 target_connection_count = (unsigned int) opt;
1293 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1295 if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0))
1296 read_friends_file (cfg);
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1299 "Topology would like %u connections with at least %u friends (%s)\n",
1300 target_connection_count, minimum_friend_count,
1301 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1303 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
1304 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
1305 transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
1307 GNUNET_CORE_connect (cfg, 1, NULL, &core_init, &connect_notify,
1308 &disconnect_notify, NULL, NULL, GNUNET_NO, NULL,
1309 GNUNET_NO, handlers);
1310 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
1312 if (NULL == transport)
1314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1315 _("Failed to connect to `%s' service.\n"), "transport");
1316 GNUNET_SCHEDULER_shutdown ();
1321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1322 _("Failed to connect to `%s' service.\n"), "core");
1323 GNUNET_SCHEDULER_shutdown ();
1330 * The main function for the topology daemon.
1332 * @param argc number of arguments from the command line
1333 * @param argv command line arguments
1334 * @return 0 ok, 1 on error
1337 main (int argc, char *const *argv)
1339 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1340 GNUNET_GETOPT_OPTION_END
1346 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",
1348 ("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1349 options, &run, NULL)) ? 0 : 1;
1353 /* end of gnunet-daemon-topology.c */