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 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 maintaining the mesh topology
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 connection
42 #define BLACKLIST_AFTER_ATTEMPT GNUNET_TIME_UNIT_HOURS
45 * For how long do we blacklist a friend after a failed connection
48 #define BLACKLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
51 * How often do we at most advertise any HELLO to a peer?
53 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
56 * How often do we at most advertise the same HELLO to the same peer?
58 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
62 * List of neighbours, friends and blacklisted peers.
68 * This is a linked list.
70 struct PeerList *next;
73 * Our handle for the request to transmit HELLOs to this peer; NULL
74 * if no such request is pending.
76 struct GNUNET_CORE_TransmitHandle *hello_req;
79 * Our handle for the request to connect to this peer; NULL if no
80 * such request is pending.
82 struct GNUNET_CORE_PeerRequestHandle *connect_req;
85 * Pointer to the HELLO message of this peer; can be NULL.
87 struct GNUNET_HELLO_Message *hello;
90 * Bloom filter used to mark which peers already got the HELLO
93 struct GNUNET_CONTAINER_BloomFilter *filter;
96 * Our request handle for *whitelisting* this peer (NULL if
97 * no whitelisting request is pending).
99 struct GNUNET_TRANSPORT_BlacklistRequest *wh;
102 * Is this peer listed here because he is a friend?
107 * Are we connected to this peer right now?
112 * Are we currently blocking this peer (via blacklist)?
117 * Until what time should we not try to connect again
120 struct GNUNET_TIME_Absolute blacklisted_until;
123 * Next time we are allowed to transmit a HELLO to this peer?
125 struct GNUNET_TIME_Absolute next_hello_allowed;
128 * When should we reset the bloom filter of this entry?
130 struct GNUNET_TIME_Absolute filter_expiration;
133 * ID of task we use to wait for the time to send the next HELLO
136 GNUNET_SCHEDULER_TaskIdentifier hello_delay_task;
141 struct GNUNET_PeerIdentity id;
147 * Entry in linked list of active 'disconnect' requests that we have issued.
149 struct DisconnectList
152 * This is a doubly-linked list.
154 struct DisconnectList *next;
157 * This is a doubly-linked list.
159 struct DisconnectList *prev;
162 * Our request handle.
164 struct GNUNET_TRANSPORT_BlacklistRequest *rh;
167 * Peer we tried to disconnect.
169 struct GNUNET_PeerIdentity peer;
175 * Our peerinfo notification context. We use notification
176 * to instantly learn about new peers as they are discovered.
178 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
183 static struct GNUNET_SCHEDULER_Handle *sched;
188 static const struct GNUNET_CONFIGURATION_Handle *cfg;
191 * Handle to the core API.
193 static struct GNUNET_CORE_Handle *handle;
196 * Handle to the transport API.
198 static struct GNUNET_TRANSPORT_Handle *transport;
201 * Identity of this peer.
203 static struct GNUNET_PeerIdentity my_identity;
206 * Linked list of all of our friends, all of our current neighbours
207 * and all peers for which we have HELLOs. So pretty much everyone.
209 static struct PeerList *peers;
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;
238 * Should the topology daemon try to establish connections?
240 static int autoconnect;
243 * Head of doubly-linked list of active 'disconnect' requests that we have issued.
245 static struct DisconnectList *disconnect_head;
248 * Head of doubly-linked list of active 'disconnect' requests that we have issued.
250 static struct DisconnectList *disconnect_tail;
254 * Function called once our request to 'disconnect' a peer
257 * @param cls our 'struct DisconnectList'
261 disconnect_done (void *cls,
262 const struct GNUNET_SCHEDULER_TaskContext *tc)
264 struct DisconnectList *dl = cls;
266 GNUNET_CONTAINER_DLL_remove (disconnect_head,
274 * Force a disconnect from the specified peer.
277 force_disconnect (struct PeerList *pl)
279 const struct GNUNET_PeerIdentity *peer = &pl->id;
280 struct DisconnectList *dl;
284 GNUNET_TRANSPORT_blacklist_cancel (pl->wh);
287 pl->is_blocked = GNUNET_YES;
288 dl = GNUNET_malloc (sizeof (struct DisconnectList));
290 GNUNET_CONTAINER_DLL_insert (disconnect_head,
293 dl->rh = GNUNET_TRANSPORT_blacklist (sched, cfg,
295 GNUNET_TIME_UNIT_FOREVER_REL,
296 GNUNET_TIME_UNIT_FOREVER_REL,
304 * Function called once our request to 'whitelist' a peer
307 * @param cls our 'struct PeerList'
311 whitelist_done (void *cls,
312 const struct GNUNET_SCHEDULER_TaskContext *tc)
314 struct PeerList *pl = cls;
321 * Whitelist all peers that we blacklisted; we've passed
322 * the minimum number of friends.
328 struct DisconnectList *dl;
330 /* first, cancel all blacklisting requests */
331 while (NULL != (dl = disconnect_head))
333 GNUNET_CONTAINER_DLL_remove (disconnect_head,
336 GNUNET_TRANSPORT_blacklist_cancel (dl->rh);
339 /* then, specifically whitelist all peers that we
340 know to have blacklisted */
346 pl->wh = GNUNET_TRANSPORT_blacklist (sched, cfg,
348 GNUNET_TIME_UNIT_ZERO,
349 GNUNET_TIME_UNIT_FOREVER_REL,
352 pl->is_blocked = GNUNET_NO;
360 * Function called by core when our attempt to connect succeeded.
363 connect_completed_callback (void *cls,
364 const struct GNUNET_SCHEDULER_TaskContext *tc)
366 struct PeerList *pos = cls;
368 pos->connect_req = NULL;
373 * Try to connect to the specified peer.
375 * @param pos peer to connect to
378 attempt_connect (struct PeerList *pos)
380 if (GNUNET_YES == pos->is_friend)
381 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT_FRIEND);
383 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT);
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "Asking core to connect to `%s'\n",
387 GNUNET_i2s (&pos->id));
389 pos->connect_req = GNUNET_CORE_peer_request_connect (sched, cfg,
390 GNUNET_TIME_UNIT_MINUTES,
392 &connect_completed_callback,
398 * Find a peer in our linked list.
399 * FIXME: should probably use a hash map instead.
402 find_peer (const struct GNUNET_PeerIdentity * peer)
404 struct PeerList *pos;
409 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
418 * Check if an additional connection from the given peer is allowed.
421 is_connection_allowed (struct PeerList *peer)
423 if (0 == memcmp (&my_identity, &peer->id, sizeof (struct GNUNET_PeerIdentity)))
424 return GNUNET_SYSERR; /* disallow connections to self */
427 if (GNUNET_YES == friends_only)
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Determined that `%s' is not allowed to connect (not a friend)\n",
432 GNUNET_i2s (&peer->id));
434 return GNUNET_SYSERR;
436 if (friend_count >= minimum_friend_count)
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
441 GNUNET_i2s (&peer->id));
443 return GNUNET_SYSERR;
448 * Create a new entry in the peer list.
450 * @param peer identity of the new entry
451 * @param hello hello message, can be NULL
452 * @param is_friend is the new entry for a friend?
453 * @return the new entry
455 static struct PeerList *
456 make_peer (const struct
457 GNUNET_PeerIdentity * peer,
458 const struct GNUNET_HELLO_Message *hello,
461 struct PeerList *ret;
463 ret = GNUNET_malloc (sizeof (struct PeerList));
465 ret->is_friend = is_friend;
468 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
469 memcpy (ret->hello, hello,
470 GNUNET_HELLO_size (hello));
479 * Free all resources associated with the given peer.
481 * @param peer peer to free
484 free_peer (struct PeerList *peer)
486 struct PeerList *pos;
487 struct PeerList *prev;
496 GNUNET_assert (pos != NULL);
500 prev->next = pos->next;
501 if (pos->hello_req != NULL)
502 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
504 GNUNET_TRANSPORT_blacklist_cancel (pos->wh);
505 if (pos->connect_req != NULL)
506 GNUNET_CORE_peer_request_connect_cancel (pos->connect_req);
507 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
508 GNUNET_SCHEDULER_cancel (sched,
509 pos->hello_delay_task);
510 GNUNET_free_non_null (pos->hello);
511 if (pos->filter != NULL)
512 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
518 * Setup bloom filter for the given peer entry.
520 * @param peer entry to initialize
523 setup_filter (struct PeerList *peer)
525 /* 2^{-5} chance of not sending a HELLO to a peer is
526 acceptably small (if the filter is 50% full);
527 64 bytes of memory are small compared to the rest
528 of the data structure and would only really become
529 "useless" once a HELLO has been passed on to ~100
530 other peers, which is likely more than enough in
531 any case; hence 64, 5 as bloomfilter parameters. */
532 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
533 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
534 /* never send a peer its own HELLO */
535 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->id.hashPubKey);
540 * Function to fill send buffer with HELLO.
542 * @param cls 'struct PeerList' of the target peer
543 * @param size number of bytes available in buf
544 * @param buf where the callee should write the message
545 * @return number of bytes written to buf
548 hello_advertising_ready (void *cls,
554 * Calculate when we would like to send the next HELLO to this
555 * peer and ask for it.
557 * @param cls for which peer to schedule the HELLO
558 * @param tc task context
561 schedule_next_hello (void *cls,
562 const struct GNUNET_SCHEDULER_TaskContext *tc)
564 struct PeerList *pl = cls;
565 struct PeerList *pos;
566 struct PeerList *next;
568 struct GNUNET_TIME_Relative next_adv;
569 struct GNUNET_TIME_Relative rst_time;
571 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
572 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
573 return; /* we're out of here */
575 next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
576 /* find applicable HELLOs */
578 while (NULL != (pos = next))
581 if (pos->hello == NULL)
583 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
584 if (0 == rst_time.value)
586 /* time to discard... */
587 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
592 if (rst_time.value < next_adv.value)
593 next_want = GNUNET_HELLO_size (pos->hello);
594 next_adv = GNUNET_TIME_relative_min (rst_time,
598 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
603 next_adv = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
604 if (next_adv.value == 0)
607 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
611 &hello_advertising_ready,
616 = GNUNET_SCHEDULER_add_delayed (sched,
618 &schedule_next_hello,
624 * Cancel existing requests for sending HELLOs to this peer
625 * and recalculate when we should send HELLOs to it based
626 * on our current state (something changed!).
629 reschedule_hellos (struct PeerList *peer)
631 if (peer->hello_req != NULL)
633 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
634 peer->hello_req = NULL;
636 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
638 GNUNET_SCHEDULER_cancel (sched,
639 peer->hello_delay_task);
640 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
642 peer->hello_delay_task
643 = GNUNET_SCHEDULER_add_now (sched,
644 &schedule_next_hello,
650 * Method called whenever a peer connects.
653 * @param peer peer identity this notification is about
654 * @param latency reported latency of the connection with 'other'
655 * @param distance reported distance (DV) to 'other'
658 connect_notify (void *cls,
660 GNUNET_PeerIdentity * peer,
661 struct GNUNET_TIME_Relative latency,
664 struct PeerList *pos;
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Core told us that we are connecting to `%s'\n",
672 pos = find_peer (peer);
675 pos = make_peer (peer, NULL, GNUNET_NO);
676 if (GNUNET_OK != is_connection_allowed (pos))
678 GNUNET_assert (pos->is_friend == GNUNET_NO);
679 pos->is_connected = GNUNET_YES;
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 "Connection to `%s' is forbidden, forcing disconnect!\n",
685 force_disconnect (pos);
691 GNUNET_assert (GNUNET_NO == pos->is_connected);
692 pos->blacklisted_until.value = 0; /* remove blacklisting */
694 pos->is_connected = GNUNET_YES;
697 if ( (friend_count == minimum_friend_count - 1) &&
698 (GNUNET_YES != friends_only) )
702 reschedule_hellos (pos);
707 * Disconnect from all non-friends (we're below quota).
712 struct PeerList *pos;
717 if ( (GNUNET_NO == pos->is_friend) &&
718 (GNUNET_YES == pos->is_connected) )
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Connection to `%s' is not from a friend, forcing disconnect!\n",
723 GNUNET_i2s (&pos->id));
725 force_disconnect (pos);
733 * Try to add more peers to our connection set.
738 struct PeerList *pos;
743 if ( (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value == 0) &&
744 ( (GNUNET_YES == pos->is_friend) ||
745 (friend_count >= minimum_friend_count) ) &&
746 (GNUNET_YES != pos->is_connected) )
747 attempt_connect (pos);
754 * Method called whenever a peer disconnects.
757 * @param peer peer identity this notification is about
760 disconnect_notify (void *cls,
762 GNUNET_PeerIdentity * peer)
764 struct PeerList *pos;
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Core told us that we disconnected from `%s'\n",
771 pos = find_peer (peer);
777 if (pos->is_connected != GNUNET_YES)
785 if ( (connection_count < target_connection_count) ||
786 (friend_count < minimum_friend_count) )
788 if (friend_count < minimum_friend_count)
790 /* disconnect from all non-friends */
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793 "Not enough friendly connections, dropping all non-friend connections\n");
801 * Iterator called on each address.
803 * @param cls flag that we will set if we see any addresses
804 * @param tname name of the transport
805 * @param expiration when will the given address expire
806 * @param addr the address of the peer
807 * @param addrlen number of bytes in addr
808 * @return GNUNET_SYSERR always, to terminate iteration
811 address_iterator (void *cls,
813 struct GNUNET_TIME_Absolute expiration,
814 const void *addr, size_t addrlen)
818 return GNUNET_SYSERR;
823 * We've gotten a HELLO from another peer. Consider it for
827 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
830 struct GNUNET_PeerIdentity pid;
831 struct PeerList *peer;
832 struct PeerList *pos;
835 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
836 if (0 == memcmp (&pid,
838 sizeof (struct GNUNET_PeerIdentity)))
839 return; /* that's me! */
840 have_address = GNUNET_NO;
841 GNUNET_HELLO_iterate_addresses (hello,
845 if (GNUNET_NO == have_address)
846 return; /* no point in advertising this one... */
847 peer = find_peer (&pid);
849 peer = make_peer (&pid, hello, GNUNET_NO);
850 // FIXME: check if 'hello' is any different from peer->hello?
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "Found `%s' from peer `%s' for advertising\n",
857 size = GNUNET_HELLO_size (hello);
858 GNUNET_free_non_null (peer->hello);
859 peer->hello = GNUNET_malloc (size);
860 memcpy (peer->hello, hello, size);
861 if (peer->filter != NULL)
862 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
864 /* since we have a new HELLO to pick from, re-schedule all
865 HELLO requests that are not bound by the HELLO send rate! */
871 if ( (pos->is_connected) &&
872 (GNUNET_TIME_absolute_get_remaining (pos->next_hello_allowed).value <= HELLO_ADVERTISEMENT_MIN_FREQUENCY.value) )
873 reschedule_hellos (pos);
881 * Peerinfo calls this function to let us know about a possible peer
882 * that we might want to connect to.
885 process_peer (void *cls,
886 const struct GNUNET_PeerIdentity *peer,
887 const struct GNUNET_HELLO_Message *hello,
890 struct PeerList *pos;
892 GNUNET_assert (peer != NULL);
893 if (0 == memcmp (&my_identity,
894 peer, sizeof (struct GNUNET_PeerIdentity)))
895 return; /* that's me! */
898 /* free existing HELLO, if any */
899 if (NULL != (pos = find_peer (peer)))
901 GNUNET_free_non_null (pos->hello);
903 if (pos->filter != NULL)
905 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
908 if ( (! pos->is_connected) &&
909 (! pos->is_friend) &&
910 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
915 consider_for_advertising (hello);
916 pos = find_peer (peer);
918 pos = make_peer (peer, hello, GNUNET_NO);
919 GNUNET_assert (NULL != pos);
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "Considering connecting to peer `%s'\n",
925 if (GNUNET_YES == pos->is_connected)
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "Already connected to peer `%s'\n",
934 if (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value > 0)
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "Already tried peer `%s' recently\n",
941 return; /* peer still blacklisted */
943 if ( (GNUNET_YES == pos->is_friend) ||
944 (GNUNET_YES != friends_only) ||
945 (friend_count >= minimum_friend_count) )
946 attempt_connect (pos);
951 * Discard peer entries for blacklisted peers
952 * where the blacklisting has expired.
955 discard_old_blacklist_entries (void *cls,
956 const struct GNUNET_SCHEDULER_TaskContext *tc)
958 struct PeerList *pos;
959 struct PeerList *next;
961 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
964 while (NULL != (pos = next))
967 if ( (GNUNET_NO == pos->is_friend) &&
968 (GNUNET_NO == pos->is_connected) &&
969 (GNUNET_NO == pos->is_blocked) &&
970 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
973 GNUNET_SCHEDULER_add_delayed (sched,
974 BLACKLIST_AFTER_ATTEMPT,
975 &discard_old_blacklist_entries,
981 * Function called after GNUNET_CORE_connect has succeeded
982 * (or failed for good).
985 * @param server handle to the server, NULL if we failed
986 * @param my_id ID of this peer, NULL if we failed
987 * @param publicKey public key of this peer, NULL if we failed
990 core_init (void *cls,
991 struct GNUNET_CORE_Handle * server,
992 const struct GNUNET_PeerIdentity *
995 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1000 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1001 _("Failed to connect to core service, can not manage topology!\n"));
1002 GNUNET_SCHEDULER_shutdown (sched);
1006 my_identity = *my_id;
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 GNUNET_i2s (my_id));
1012 GNUNET_SCHEDULER_add_delayed (sched,
1013 BLACKLIST_AFTER_ATTEMPT,
1014 &discard_old_blacklist_entries,
1020 * Read the friends file.
1023 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1028 struct GNUNET_PeerIdentity pid;
1030 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1031 unsigned int entries_found;
1032 struct PeerList *fl;
1035 GNUNET_CONFIGURATION_get_value_filename (cfg,
1040 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1041 _("Option `%s' in section `%s' not specified!\n"),
1046 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1047 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1048 | GNUNET_DISK_PERM_USER_WRITE);
1049 if (0 != STAT (fn, &frstat))
1051 if ((friends_only) || (minimum_friend_count > 0))
1053 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1054 _("Could not read friends list `%s'\n"), fn);
1059 if (frstat.st_size == 0)
1061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1062 _("Friends file `%s' is empty.\n"),
1067 data = GNUNET_malloc_large (frstat.st_size);
1068 if (frstat.st_size !=
1069 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1072 _("Failed to read friends list from `%s'\n"), fn);
1079 while ((pos < frstat.st_size) && isspace (data[pos]))
1081 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1082 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1084 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1085 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1087 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1088 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1089 (unsigned long long) pos);
1091 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1095 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1096 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1098 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1099 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1100 (unsigned long long) pos,
1105 if (0 != memcmp (&pid,
1107 sizeof (struct GNUNET_PeerIdentity)))
1110 fl = make_peer (&pid,
1113 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1114 _("Found friend `%s' in configuration\n"),
1115 GNUNET_i2s (&fl->id));
1119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1120 _("Found myself `%s' in friend list (useless, ignored)\n"),
1124 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1125 while ((pos < frstat.st_size) && isspace (data[pos]))
1130 if ( (minimum_friend_count > entries_found) &&
1131 (friends_only == GNUNET_NO) )
1133 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1134 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1136 if ( (minimum_friend_count > target_connection_count) &&
1137 (friends_only == GNUNET_NO) )
1139 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1140 _("More friendly connections required than target total number of connections.\n"));
1146 * This function is called whenever an encrypted HELLO message is
1149 * @param cls closure
1150 * @param other the other peer involved (sender or receiver, NULL
1151 * for loopback messages where we are both sender and receiver)
1152 * @param message the actual HELLO message
1153 * @param latency reported latency of the connection with 'other'
1154 * @param distance reported distance (DV) to 'other'
1155 * @return GNUNET_OK to keep the connection open,
1156 * GNUNET_SYSERR to close it (signal serious error)
1159 handle_encrypted_hello (void *cls,
1160 const struct GNUNET_PeerIdentity * other,
1161 const struct GNUNET_MessageHeader *
1163 struct GNUNET_TIME_Relative latency,
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168 "Received encrypted `%s' from peer `%s'",
1170 GNUNET_i2s (other));
1172 if (transport != NULL)
1173 GNUNET_TRANSPORT_offer_hello (transport,
1180 * Function to fill send buffer with HELLO.
1182 * @param cls 'struct PeerList' of the target peer
1183 * @param size number of bytes available in buf
1184 * @param buf where the callee should write the message
1185 * @return number of bytes written to buf
1188 hello_advertising_ready (void *cls,
1192 struct PeerList *pl = cls;
1193 struct PeerList *pos;
1194 struct PeerList *next;
1198 pl->hello_req = NULL;
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 "Data solicited for `%s', considering sending `%s'",
1202 GNUNET_i2s (&pl->id),
1205 /* find applicable HELLOs */
1207 while (NULL != (pos = next))
1210 if (pos->hello == NULL)
1212 if (0 == GNUNET_TIME_absolute_get_remaining (pos->filter_expiration).value)
1214 /* time to discard... */
1215 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
1219 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
1220 &pl->id.hashPubKey))
1226 hs = GNUNET_HELLO_size (pos->hello);
1230 memcpy (buf, pos->hello, want);
1231 GNUNET_CONTAINER_bloomfilter_add (pos->filter,
1232 &pl->id.hashPubKey);
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Sending %u bytes of `%s's",
1236 (unsigned int) want,
1241 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1242 pl->hello_delay_task
1243 = GNUNET_SCHEDULER_add_now (sched,
1244 &schedule_next_hello,
1251 * Last task run during shutdown. Disconnects us from
1252 * the transport and core.
1255 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1257 struct DisconnectList *dl;
1259 if (NULL != peerinfo_notify)
1261 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1262 peerinfo_notify = NULL;
1264 GNUNET_TRANSPORT_disconnect (transport);
1266 while (NULL != peers)
1270 GNUNET_CORE_disconnect (handle);
1273 while (NULL != (dl = disconnect_head))
1275 GNUNET_CONTAINER_DLL_remove (disconnect_head,
1278 GNUNET_TRANSPORT_blacklist_cancel (dl->rh);
1285 * Main function that will be run.
1287 * @param cls closure
1288 * @param s the scheduler to use
1289 * @param args remaining command-line arguments
1290 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1291 * @param c configuration
1295 struct GNUNET_SCHEDULER_Handle * s,
1297 const char *cfgfile,
1298 const struct GNUNET_CONFIGURATION_Handle * c)
1300 struct GNUNET_CORE_MessageHandler handlers[] =
1302 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1305 unsigned long long opt;
1309 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1312 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1316 GNUNET_CONFIGURATION_get_value_number (cfg,
1321 minimum_friend_count = (unsigned int) opt;
1323 GNUNET_CONFIGURATION_get_value_number (cfg,
1325 "TARGET-CONNECTION-COUNT",
1328 target_connection_count = (unsigned int) opt;
1330 if ( (friends_only == GNUNET_YES) ||
1331 (minimum_friend_count > 0) )
1332 read_friends_file (cfg);
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "Topology would like %u connections with at least %u friends (%s)\n",
1336 target_connection_count,
1337 minimum_friend_count,
1338 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1340 transport = GNUNET_TRANSPORT_connect (sched,
1346 handle = GNUNET_CORE_connect (sched,
1348 GNUNET_TIME_UNIT_FOREVER_REL,
1357 GNUNET_SCHEDULER_add_delayed (sched,
1358 GNUNET_TIME_UNIT_FOREVER_REL,
1359 &cleaning_task, NULL);
1360 if (NULL == transport)
1362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1363 _("Failed to connect to `%s' service.\n"),
1365 GNUNET_SCHEDULER_shutdown (sched);
1370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1371 _("Failed to connect to `%s' service.\n"),
1373 GNUNET_SCHEDULER_shutdown (sched);
1376 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1383 * gnunet-daemon-topology command line options.
1385 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1386 GNUNET_GETOPT_OPTION_END
1391 * The main function for the topology daemon.
1393 * @param argc number of arguments from the command line
1394 * @param argv command line arguments
1395 * @return 0 ok, 1 on error
1398 main (int argc, char *const *argv)
1403 GNUNET_PROGRAM_run (argc,
1406 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1408 &run, NULL)) ? 0 : 1;
1412 /* end of gnunet-daemon-topology.c */