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
27 * - blacklisting & respect for blacklist
28 * - calculate target_connection_count!
29 * - calculate peer_search retry delay
34 #include "gnunet_core_service.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_peerinfo_service.h"
37 #include "gnunet_util_lib.h"
40 #define DEBUG_TOPOLOGY GNUNET_NO
44 * List of neighbours and friends.
50 * This is a linked list.
52 struct FriendList *next;
55 * Is this peer listed here because he is a friend?
60 * Are we connected to this peer right now?
65 * Until what time should we not try to connect again
68 struct GNUNET_TIME_Absolute blacklisted_until;
73 struct GNUNET_PeerIdentity id;
81 static struct GNUNET_SCHEDULER_Handle * sched;
86 static struct GNUNET_CONFIGURATION_Handle * cfg;
89 * Handle to the core API.
91 static struct GNUNET_CORE_Handle *handle;
94 * Identity of this peer.
96 static struct GNUNET_PeerIdentity my_identity;
99 * Linked list of all of our friends and all of our current
102 static struct FriendList *friends;
105 * Flag to disallow non-friend connections (pure F2F mode).
107 static int friends_only;
110 * Minimum number of friends to have in the
111 * connection set before we allow non-friends.
113 static unsigned int minimum_friend_count;
116 * Number of peers (friends and others) that we are currently connected to.
118 static unsigned int connection_count;
121 * Target number of connections.
123 static unsigned int target_connection_count;
126 * Number of friends that we are currently connected to.
128 static unsigned int friend_count;
131 * Should the topology daemon try to establish connections?
133 static int autoconnect;
138 * Force a disconnect from the specified peer.
141 force_disconnect (const struct GNUNET_PeerIdentity *peer)
143 GNUNET_CORE_peer_configure (handle,
145 GNUNET_TIME_UNIT_FOREVER_REL,
155 * Function called by core when our attempt to connect
156 * succeeded. Does nothing.
159 ready_callback (void *cls,
160 size_t size, void *buf)
167 * Try to connect to the specified peer.
169 * @param pos NULL if not in friend list yet
172 attempt_connect (const struct GNUNET_PeerIdentity *peer,
173 struct FriendList *pos)
175 /* FIXME: do blacklist! */
176 GNUNET_CORE_notify_transmit_ready (handle,
178 GNUNET_TIME_UNIT_MINUTES,
180 sizeof(struct GNUNET_MessageHeader),
187 * Is this peer one of our friends?
190 is_friend (const struct GNUNET_PeerIdentity * peer)
192 struct FriendList *pos;
197 if ( (GNUNET_YES == pos->is_friend) &&
198 (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity))) )
207 * Check if an additional connection from the given peer is allowed.
210 is_connection_allowed (const struct GNUNET_PeerIdentity * peer)
212 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
213 return GNUNET_SYSERR; /* disallow connections to self */
214 if (is_friend (peer))
216 if (GNUNET_YES == friends_only)
217 return GNUNET_SYSERR;
218 if (friend_count >= minimum_friend_count)
220 return GNUNET_SYSERR;
225 * Method called whenever a peer connects.
228 * @param peer peer identity this notification is about
230 static void connect_notify (void *cls,
232 GNUNET_PeerIdentity * peer)
234 struct FriendList *pos;
240 if ( (GNUNET_YES == pos->is_friend) &&
241 (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity))) )
243 GNUNET_assert (GNUNET_NO == pos->is_connected);
244 pos->is_connected = GNUNET_YES;
250 pos = GNUNET_malloc (sizeof(struct FriendList));
252 pos->is_connected = GNUNET_YES;
255 if (GNUNET_OK != is_connection_allowed (peer))
256 force_disconnect (peer);
261 * Disconnect from all non-friends (we're below quota).
266 struct FriendList *pos;
271 if (GNUNET_NO == pos->is_friend)
273 GNUNET_assert (GNUNET_YES == pos->is_connected);
274 force_disconnect (&pos->id);
282 * Method called whenever a peer disconnects.
285 * @param peer peer identity this notification is about
287 static void disconnect_notify (void *cls,
289 GNUNET_PeerIdentity * peer)
291 struct FriendList *pos;
292 struct FriendList *prev;
299 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
301 GNUNET_assert (GNUNET_YES == pos->is_connected);
302 pos->is_connected = GNUNET_NO;
303 if (GNUNET_YES == pos->is_friend)
306 if (friend_count < minimum_friend_count)
308 /* disconnect from all non-friends */
310 attempt_connect (peer, pos);
319 prev->next = pos->next;
331 * Find more peers that we should connect to and ask the
332 * core to establish connections.
335 find_more_peers (void *cls,
336 const struct GNUNET_SCHEDULER_TaskContext *tc);
340 * Determine when we should try again to find more peers and
344 schedule_peer_search ()
346 struct GNUNET_TIME_Relative delay;
348 /* FIXME: calculate reasonable delay here */
349 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES,
351 GNUNET_SCHEDULER_add_delayed (sched,
353 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
354 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
362 * Peerinfo calls this function to let us know about a
363 * possible peer that we might want to connect to.
366 process_peer (void *cls,
367 const struct GNUNET_PeerIdentity * peer,
368 const struct GNUNET_HELLO_Message * hello,
371 struct FriendList *pos;
375 /* last call, schedule 'find_more_peers' again... */
376 schedule_peer_search ();
381 /* no HELLO known; can not connect, ignore! */
384 if (0 == memcmp (&my_identity,
385 peer, sizeof (struct GNUNET_PeerIdentity)))
386 return; /* that's me! */
391 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
393 if (GNUNET_YES == pos->is_connected)
395 /* FIXME: check blacklisted... */
396 if (GNUNET_YES == pos->is_friend)
398 attempt_connect (peer, pos);
404 if (GNUNET_YES == friends_only)
406 if (friend_count < minimum_friend_count)
408 attempt_connect (peer, NULL);
413 * Try to add more friends to our connection set.
418 struct FriendList *pos;
423 /* FIXME: check friends for blacklisting... */
424 if ( (GNUNET_YES == pos->is_friend) &&
425 (GNUNET_YES != pos->is_connected) )
426 attempt_connect (&pos->id, pos);
433 * Find more peers that we should connect to and ask the
434 * core to establish connections.
437 find_more_peers (void *cls,
438 const struct GNUNET_SCHEDULER_TaskContext *tc)
440 if (target_connection_count <= connection_count)
442 schedule_peer_search ();
445 if ( (GNUNET_YES == friends_only) ||
446 (friend_count < minimum_friend_count) )
449 schedule_peer_search ();
452 GNUNET_PEERINFO_for_all (cfg,
455 0, GNUNET_TIME_UNIT_FOREVER_REL,
456 &process_peer, NULL);
461 * Function called after GNUNET_CORE_connect has succeeded
462 * (or failed for good).
465 * @param server handle to the server, NULL if we failed
466 * @param my_id ID of this peer, NULL if we failed
467 * @param publicKey public key of this peer, NULL if we failed
470 core_init (void *cls,
471 struct GNUNET_CORE_Handle * server,
472 const struct GNUNET_PeerIdentity *
475 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
481 _("Failed to connect to core service, can not manage topology!\n"));
485 my_identity = *my_id;
487 GNUNET_SCHEDULER_add_delayed (sched,
489 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
490 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
491 GNUNET_TIME_UNIT_SECONDS /* give core time to tell us about existing connections */,
498 * gnunet-daemon-topology command line options.
500 static struct GNUNET_GETOPT_CommandLineOption options[] = {
501 GNUNET_GETOPT_OPTION_END
506 * Read the friends file.
509 read_friends_file (struct GNUNET_CONFIGURATION_Handle *cfg)
516 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
517 unsigned int entries_found;
518 struct FriendList *fl;
521 GNUNET_CONFIGURATION_get_value_filename (cfg,
525 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
526 GNUNET_DISK_file_write (fn, NULL, 0, "600");
527 if (0 != STAT (fn, &frstat))
529 if ((friends_only) || (minimum_friend_count > 0))
531 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
532 _("Could not read friends list `%s'\n"), fn);
537 if (frstat.st_size == 0)
539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
540 _("Friends file `%s' is empty.\n"),
545 data = GNUNET_malloc_large (frstat.st_size);
546 if (frstat.st_size !=
547 GNUNET_DISK_file_read (fn, frstat.st_size, data))
549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
550 _("Failed to read friends list from `%s'\n"), fn);
557 while ((pos < frstat.st_size) && isspace (data[pos]))
559 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
560 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
562 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
563 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
566 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
567 (unsigned long long) pos);
569 while ((pos < frstat.st_size) && (!isspace (data[pos])))
573 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
574 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &hc))
576 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
577 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
578 (unsigned long long) pos,
584 fl = GNUNET_malloc (sizeof(struct FriendList));
585 fl->is_friend = GNUNET_YES;
586 fl->id.hashPubKey = hc;
590 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
591 while ((pos < frstat.st_size) && isspace (data[pos]))
596 if ( (minimum_friend_count > entries_found) &&
597 (friends_only == GNUNET_NO) )
599 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
600 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
602 if ( (minimum_friend_count > target_connection_count) &&
603 (friends_only == GNUNET_NO) )
605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
606 _("More friendly connections required than target total number of connections.\n"));
612 * Main function that will be run.
615 * @param s the scheduler to use
616 * @param args remaining command-line arguments
617 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
618 * @param c configuration
622 struct GNUNET_SCHEDULER_Handle * s,
625 struct GNUNET_CONFIGURATION_Handle * c)
627 struct GNUNET_CORE_MessageHandler handlers[] =
631 unsigned long long opt;
635 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
638 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
642 GNUNET_CONFIGURATION_get_value_number (cfg,
646 minimum_friend_count = (unsigned int) opt;
648 if ( (friends_only == GNUNET_YES) ||
649 (minimum_friend_count > 0) )
650 read_friends_file (cfg);
651 GNUNET_CORE_connect (sched,
653 GNUNET_TIME_UNIT_FOREVER_REL,
666 * The main function for the topology daemon.
668 * @param argc number of arguments from the command line
669 * @param argv command line arguments
670 * @return 0 ok, 1 on error
673 main (int argc, char *const *argv)
678 GNUNET_PROGRAM_run (argc,
681 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
683 &run, NULL)) ? 0 : 1;
687 /* end of gnunet-daemon-topology.c */