2 This file is part of GNUnet.
3 (C) 2007, 2008, 2009 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 2, 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 bootstrapping via topology servers
24 * @author Christian Grothoff
29 #include "gnunet_core_service.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_util_lib.h"
36 #define DEBUG_TOPOLOGY GNUNET_NO
39 * For how long do we blacklist a peer after a failed
42 #define BLACKLIST_AFTER_ATTEMPT GNUNET_TIME_UNIT_HOURS
45 * For how long do we blacklist a friend after a failed
48 #define BLACKLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
51 * How frequently are we allowed to ask PEERINFO for more
52 * HELLO's to advertise (at most)?
54 #define MIN_HELLO_GATHER_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 27)
57 * How often do we at most advertise the same HELLO to the same peer?
58 * Also used to remove HELLOs of peers that PEERINFO no longer lists
61 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
65 * List of neighbours, friends and blacklisted peers.
71 * This is a linked list.
73 struct PeerList *next;
76 * Is this peer listed here because he is a friend?
81 * Are we connected to this peer right now?
86 * Until what time should we not try to connect again
89 struct GNUNET_TIME_Absolute blacklisted_until;
92 * Last time we transmitted a HELLO to this peer?
94 struct GNUNET_TIME_Absolute last_hello_sent;
99 struct GNUNET_PeerIdentity id;
105 * List of HELLOs we may consider for advertising.
110 * This is a linked list.
112 struct HelloList *next;
115 * Pointer to the HELLO message. Memory allocated as part
116 * of the "struct HelloList" --- do not free!
118 struct GNUNET_HELLO_Message *msg;
121 * Bloom filter used to mark which peers already got
124 struct GNUNET_CONTAINER_BloomFilter *filter;
127 * What peer is this HELLO for?
129 struct GNUNET_PeerIdentity id;
132 * When should we remove this entry from the linked list (either
133 * resetting the filter or possibly eliminating it for good because
134 * we no longer consider the peer to be participating in the
137 struct GNUNET_TIME_Absolute expiration;
142 * Linked list of HELLOs for advertising.
144 static struct HelloList *hellos;
149 static struct GNUNET_SCHEDULER_Handle * sched;
154 static const struct GNUNET_CONFIGURATION_Handle * cfg;
157 * Handle to the core API.
159 static struct GNUNET_CORE_Handle *handle;
162 * Handle to the transport API.
164 static struct GNUNET_TRANSPORT_Handle *transport;
167 * Identity of this peer.
169 static struct GNUNET_PeerIdentity my_identity;
172 * Linked list of all of our friends and all of our current
175 static struct PeerList *friends;
178 * Timestamp from the last time we tried to gather HELLOs.
180 static struct GNUNET_TIME_Absolute last_hello_gather_time;
183 * Flag to disallow non-friend connections (pure F2F mode).
185 static int friends_only;
188 * Minimum number of friends to have in the
189 * connection set before we allow non-friends.
191 static unsigned int minimum_friend_count;
194 * Number of peers (friends and others) that we are currently connected to.
196 static unsigned int connection_count;
199 * Target number of connections.
201 static unsigned int target_connection_count;
204 * Number of friends that we are currently connected to.
206 static unsigned int friend_count;
209 * Should the topology daemon try to establish connections?
211 static int autoconnect;
214 * Are we currently having a request pending with
215 * PEERINFO asking for HELLOs for advertising?
217 static int hello_gathering_active;
222 * Force a disconnect from the specified peer.
225 force_disconnect (const struct GNUNET_PeerIdentity *peer)
227 GNUNET_CORE_peer_configure (handle,
229 GNUNET_TIME_UNIT_FOREVER_REL,
239 * Function called by core when our attempt to connect
240 * succeeded. Does nothing.
243 ready_callback (void *cls,
244 size_t size, void *buf)
251 * Try to connect to the specified peer.
253 * @param pos NULL if not in friend list yet
256 attempt_connect (const struct GNUNET_PeerIdentity *peer,
257 struct PeerList *pos)
264 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
270 pos = GNUNET_malloc (sizeof(struct PeerList));
275 if (GNUNET_YES == pos->is_friend)
276 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT_FRIEND);
278 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT);
279 GNUNET_CORE_notify_transmit_ready (handle,
281 GNUNET_TIME_UNIT_MINUTES,
283 sizeof(struct GNUNET_MessageHeader),
290 * Is this peer one of our friends?
293 is_friend (const struct GNUNET_PeerIdentity * peer)
295 struct PeerList *pos;
300 if ( (GNUNET_YES == pos->is_friend) &&
301 (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity))) )
310 * Check if an additional connection from the given peer is allowed.
313 is_connection_allowed (const struct GNUNET_PeerIdentity * peer)
315 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
316 return GNUNET_SYSERR; /* disallow connections to self */
317 if (is_friend (peer))
319 if (GNUNET_YES == friends_only)
320 return GNUNET_SYSERR;
321 if (friend_count >= minimum_friend_count)
323 return GNUNET_SYSERR;
328 * Method called whenever a peer connects.
331 * @param peer peer identity this notification is about
333 static void connect_notify (void *cls,
335 GNUNET_PeerIdentity * peer)
337 struct PeerList *pos;
343 if ( (GNUNET_YES == pos->is_friend) &&
344 (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity))) )
346 GNUNET_assert (GNUNET_NO == pos->is_connected);
347 pos->is_connected = GNUNET_YES;
348 pos->blacklisted_until.value = 0; /* remove blacklisting */
354 pos = GNUNET_malloc (sizeof(struct PeerList));
356 pos->is_connected = GNUNET_YES;
359 if (GNUNET_OK != is_connection_allowed (peer))
360 force_disconnect (peer);
365 * Disconnect from all non-friends (we're below quota).
370 struct PeerList *pos;
375 if (GNUNET_NO == pos->is_friend)
377 GNUNET_assert (GNUNET_YES == pos->is_connected);
378 force_disconnect (&pos->id);
386 * Method called whenever a peer disconnects.
389 * @param peer peer identity this notification is about
391 static void disconnect_notify (void *cls,
393 GNUNET_PeerIdentity * peer)
395 struct PeerList *pos;
396 struct PeerList *prev;
403 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
405 GNUNET_assert (GNUNET_YES == pos->is_connected);
406 pos->is_connected = GNUNET_NO;
407 if (GNUNET_YES == pos->is_friend)
410 if (friend_count < minimum_friend_count)
412 /* disconnect from all non-friends */
414 attempt_connect (peer, pos);
423 prev->next = pos->next;
436 * Find more peers that we should connect to and ask the
437 * core to establish connections.
440 find_more_peers (void *cls,
441 const struct GNUNET_SCHEDULER_TaskContext *tc);
445 * Determine when we should try again to find more peers and
449 schedule_peer_search ()
451 struct GNUNET_TIME_Relative delay;
453 /* Typically, we try again every 15 minutes; the minimum period is
454 15s; if we are above the connection target, we reduce re-trying
455 by the square of how much we are above; so for example, with 200%
456 of the connection target we would only look for more peers once
457 every hour (after all, we're quite busy processing twice as many
458 connections as we intended to have); similarly, if we are at only
459 25% of our connectivity goal, we will try 16x as hard to connect
460 (so roughly once a minute, plus the 15s minimum delay */
461 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
462 15 + 15 * 60 * connection_count * connection_count / target_connection_count / target_connection_count);
463 GNUNET_SCHEDULER_add_delayed (sched,
465 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
466 GNUNET_SCHEDULER_NO_TASK,
476 * Iterator called on each address.
478 * @param cls flag that we will set if we see any addresses.
481 address_iterator (void *cls,
483 struct GNUNET_TIME_Absolute expiration,
484 const void *addr, size_t addrlen)
488 return GNUNET_SYSERR;
493 * We've gotten a HELLO from another peer.
494 * Consider it for advertising.
497 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
500 struct GNUNET_PeerIdentity pid;
501 struct HelloList *pos;
504 have_address = GNUNET_NO;
505 GNUNET_HELLO_iterate_addresses (hello,
509 if (GNUNET_NO == have_address)
510 return; /* no point in advertising this one... */
511 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
515 if (0 == memcmp (&pos->id,
517 sizeof(struct GNUNET_PeerIdentity)))
518 return; /* duplicate, at least "mostly" */
521 size = GNUNET_HELLO_size (hello);
522 pos = GNUNET_malloc (sizeof(struct HelloList) + size);
523 pos->msg = (struct GNUNET_HELLO_Message*) &pos[1];
524 memcpy (&pos->msg, hello, size);
526 pos->expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
527 /* 2^{-5} chance of not sending a HELLO to a peer is
528 acceptably small (if the filter is 50% full);
529 64 bytes of memory are small compared to the rest
530 of the data structure and would only really become
531 "useless" once a HELLO has been passed on to ~100
532 other peers, which is likely more than enough in
533 any case; hence 64, 5 as bloomfilter parameters. */
534 pos->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
535 /* never send a peer its own HELLO */
536 GNUNET_CONTAINER_bloomfilter_add (pos->filter, &pos->id.hashPubKey);
543 * Peerinfo calls this function to let us know about a
544 * possible peer that we might want to connect to.
547 process_peer (void *cls,
548 const struct GNUNET_PeerIdentity *peer,
549 const struct GNUNET_HELLO_Message *hello,
552 struct PeerList *pos;
556 /* last call, schedule 'find_more_peers' again... */
557 schedule_peer_search ();
562 /* no HELLO known; can not connect, ignore! */
565 if (0 == memcmp (&my_identity,
566 peer, sizeof (struct GNUNET_PeerIdentity)))
567 return; /* that's me! */
569 consider_for_advertising (hello);
573 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
575 if (GNUNET_YES == pos->is_connected)
577 if (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value > 0)
578 return; /* peer still blacklisted */
579 if (GNUNET_YES == pos->is_friend)
581 attempt_connect (peer, pos);
587 if (GNUNET_YES == friends_only)
589 if (friend_count < minimum_friend_count)
591 attempt_connect (peer, NULL);
596 * Try to add more friends to our connection set.
601 struct PeerList *pos;
606 if ( (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value == 0) &&
607 (GNUNET_YES == pos->is_friend) &&
608 (GNUNET_YES != pos->is_connected) )
609 attempt_connect (&pos->id, pos);
616 * Discard peer entries for blacklisted peers
617 * where the blacklisting has expired.
620 discard_old_blacklist_entries ()
622 struct PeerList *pos;
623 struct PeerList *next;
624 struct PeerList *prev;
628 while (NULL != (pos = next))
631 if ( (GNUNET_NO == pos->is_friend) &&
632 (GNUNET_NO == pos->is_connected) &&
633 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
635 /* delete 'pos' from list */
651 * Find more peers that we should connect to and ask the
652 * core to establish connections.
655 find_more_peers (void *cls,
656 const struct GNUNET_SCHEDULER_TaskContext *tc)
658 discard_old_blacklist_entries ();
659 if (target_connection_count <= connection_count)
661 schedule_peer_search ();
664 if ( (GNUNET_YES == friends_only) ||
665 (friend_count < minimum_friend_count) )
668 schedule_peer_search ();
671 GNUNET_PEERINFO_for_all (cfg,
674 0, GNUNET_TIME_UNIT_FOREVER_REL,
675 &process_peer, NULL);
680 * Function called after GNUNET_CORE_connect has succeeded
681 * (or failed for good).
684 * @param server handle to the server, NULL if we failed
685 * @param my_id ID of this peer, NULL if we failed
686 * @param publicKey public key of this peer, NULL if we failed
689 core_init (void *cls,
690 struct GNUNET_CORE_Handle * server,
691 const struct GNUNET_PeerIdentity *
694 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
699 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
700 _("Failed to connect to core service, can not manage topology!\n"));
704 my_identity = *my_id;
706 GNUNET_SCHEDULER_add_delayed (sched,
708 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
709 GNUNET_SCHEDULER_NO_TASK,
710 GNUNET_TIME_UNIT_SECONDS /* give core time to tell us about existing connections */,
717 * gnunet-daemon-topology command line options.
719 static struct GNUNET_GETOPT_CommandLineOption options[] = {
720 GNUNET_GETOPT_OPTION_END
725 * Read the friends file.
728 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
735 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
736 unsigned int entries_found;
740 GNUNET_CONFIGURATION_get_value_filename (cfg,
745 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
746 _("Option `%s' in section `%s' not specified!\n"),
751 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
752 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
753 | GNUNET_DISK_PERM_USER_WRITE);
754 if (0 != STAT (fn, &frstat))
756 if ((friends_only) || (minimum_friend_count > 0))
758 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
759 _("Could not read friends list `%s'\n"), fn);
764 if (frstat.st_size == 0)
766 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
767 _("Friends file `%s' is empty.\n"),
772 data = GNUNET_malloc_large (frstat.st_size);
773 if (frstat.st_size !=
774 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
777 _("Failed to read friends list from `%s'\n"), fn);
784 while ((pos < frstat.st_size) && isspace (data[pos]))
786 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
787 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
789 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
790 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
792 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
793 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
794 (unsigned long long) pos);
796 while ((pos < frstat.st_size) && (!isspace (data[pos])))
800 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
801 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &hc))
803 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
804 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
805 (unsigned long long) pos,
811 fl = GNUNET_malloc (sizeof(struct PeerList));
812 fl->is_friend = GNUNET_YES;
813 fl->id.hashPubKey = hc;
817 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
818 while ((pos < frstat.st_size) && isspace (data[pos]))
823 if ( (minimum_friend_count > entries_found) &&
824 (friends_only == GNUNET_NO) )
826 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
827 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
829 if ( (minimum_friend_count > target_connection_count) &&
830 (friends_only == GNUNET_NO) )
832 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
833 _("More friendly connections required than target total number of connections.\n"));
839 * This function is called whenever an encrypted HELLO message is
843 * @param peer the other peer involved (sender or receiver, NULL
844 * for loopback messages where we are both sender and receiver)
845 * @param message the actual HELLO message
846 * @return GNUNET_OK to keep the connection open,
847 * GNUNET_SYSERR to close it (signal serious error)
850 handle_encrypted_hello (void *cls,
851 const struct GNUNET_PeerIdentity * other,
852 const struct GNUNET_MessageHeader *
855 if (transport != NULL)
856 GNUNET_TRANSPORT_offer_hello (transport,
863 * Peerinfo calls this function to let us know about a
864 * possible peer that we might want to connect to.
867 gather_hello_callback (void *cls,
868 const struct GNUNET_PeerIdentity *peer,
869 const struct GNUNET_HELLO_Message *hello,
874 hello_gathering_active = GNUNET_NO;
878 consider_for_advertising (hello);
883 * Function to fill send buffer with HELLO.
885 * @param receiver the receiver of the message
886 * @param position is the reference to the
887 * first unused position in the buffer where GNUnet is building
889 * @param padding is the number of bytes left in that buffer.
890 * @return the number of bytes written to
891 * that buffer (must be a positive number).
894 hello_advertising (void *cls,
895 const struct GNUNET_PeerIdentity *
901 struct HelloList *pos;
902 struct HelloList *prev;
903 struct HelloList *next;
909 if (0 == memcmp (&pl->id, receiver, sizeof (struct GNUNET_PeerIdentity)))
918 /* find applicable HELLOs */
921 while (NULL != (pos = next))
925 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
926 &receiver->hashPubKey))
928 if (0 == GNUNET_TIME_absolute_get_remaining (pos->expiration).value)
930 /* time to discard... */
935 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
945 size = GNUNET_HELLO_size (pos->msg);
948 memcpy (position, pos->msg, size);
949 GNUNET_CONTAINER_bloomfilter_add (pos->filter,
950 &receiver->hashPubKey);
958 if ( (GNUNET_NO == hello_gathering_active) &&
959 (GNUNET_TIME_absolute_get_duration (last_hello_gather_time).value >
960 MIN_HELLO_GATHER_DELAY.value) )
962 hello_gathering_active = GNUNET_YES;
963 last_hello_gather_time = GNUNET_TIME_absolute_get();
964 GNUNET_PEERINFO_for_all (cfg,
967 0, GNUNET_TIME_UNIT_FOREVER_REL,
968 &gather_hello_callback, NULL);
975 * Last task run during shutdown. Disconnects us from
976 * the transport and core.
979 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
983 GNUNET_TRANSPORT_disconnect (transport);
985 GNUNET_CORE_disconnect (handle);
987 while (NULL != (pl = friends))
996 * Main function that will be run.
999 * @param s the scheduler to use
1000 * @param args remaining command-line arguments
1001 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1002 * @param c configuration
1006 struct GNUNET_SCHEDULER_Handle * s,
1008 const char *cfgfile,
1009 const struct GNUNET_CONFIGURATION_Handle * c)
1011 struct GNUNET_CORE_MessageHandler handlers[] =
1013 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1016 unsigned long long opt;
1020 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1023 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1027 GNUNET_CONFIGURATION_get_value_number (cfg,
1032 minimum_friend_count = (unsigned int) opt;
1034 GNUNET_CONFIGURATION_get_value_number (cfg,
1036 "TARGET-CONNECTION-COUNT",
1039 target_connection_count = (unsigned int) opt;
1041 if ( (friends_only == GNUNET_YES) ||
1042 (minimum_friend_count > 0) )
1043 read_friends_file (cfg);
1045 transport = GNUNET_TRANSPORT_connect (sched,
1051 GNUNET_CORE_connect (sched,
1053 GNUNET_TIME_UNIT_FOREVER_REL,
1063 GNUNET_SCHEDULER_add_delayed (sched,
1065 GNUNET_SCHEDULER_PRIORITY_IDLE,
1066 GNUNET_SCHEDULER_NO_TASK,
1067 GNUNET_TIME_UNIT_FOREVER_REL,
1068 &cleaning_task, NULL);
1073 * The main function for the topology daemon.
1075 * @param argc number of arguments from the command line
1076 * @param argv command line arguments
1077 * @return 0 ok, 1 on error
1080 main (int argc, char *const *argv)
1085 GNUNET_PROGRAM_run (argc,
1088 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1090 &run, NULL)) ? 0 : 1;
1094 /* end of gnunet-daemon-topology.c */