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
27 * - move code to use hash table instead of linked list
28 * - instead of periodically discarding blacklisted entries,
29 * simply add task that is triggered at the right time (earlier free,
31 * - check if new HELLO learned is different from old HELLO
32 * before resetting entire state!
37 #include "gnunet_core_service.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_peerinfo_service.h"
40 #include "gnunet_transport_service.h"
41 #include "gnunet_util_lib.h"
44 #define DEBUG_TOPOLOGY GNUNET_NO
47 * For how long do we blacklist a peer after a failed connection
50 #define BLACKLIST_AFTER_ATTEMPT GNUNET_TIME_UNIT_HOURS
53 * For how long do we blacklist a friend after a failed connection
56 #define BLACKLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
59 * How often do we at most advertise any HELLO to a peer?
61 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
64 * How often do we at most advertise the same HELLO to the same peer?
66 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
70 * List of neighbours, friends and blacklisted peers.
76 * This is a linked list.
78 struct PeerList *next;
81 * Our handle for the request to transmit HELLOs to this peer; NULL
82 * if no such request is pending.
84 struct GNUNET_CORE_TransmitHandle *hello_req;
87 * Our handle for the request to connect to this peer; NULL if no
88 * such request is pending.
90 struct GNUNET_CORE_PeerRequestHandle *connect_req;
93 * Pointer to the HELLO message of this peer; can be NULL.
95 struct GNUNET_HELLO_Message *hello;
98 * Bloom filter used to mark which peers already got the HELLO
101 struct GNUNET_CONTAINER_BloomFilter *filter;
104 * Is this peer listed here because he is a friend?
109 * Are we connected to this peer right now?
114 * Until what time should we not try to connect again
117 struct GNUNET_TIME_Absolute blacklisted_until;
120 * Next time we are allowed to transmit a HELLO to this peer?
122 struct GNUNET_TIME_Absolute next_hello_allowed;
125 * When should we reset the bloom filter of this entry?
127 struct GNUNET_TIME_Absolute filter_expiration;
130 * ID of task we use to wait for the time to send the next HELLO
133 GNUNET_SCHEDULER_TaskIdentifier hello_delay_task;
138 struct GNUNET_PeerIdentity id;
144 * Entry in linked list of active 'disconnect' requests that we have issued.
146 struct DisconnectList
149 * This is a doubly-linked list.
151 struct DisconnectList *next;
154 * This is a doubly-linked list.
156 struct DisconnectList *prev;
159 * Our request handle.
161 struct GNUNET_TRANSPORT_BlacklistRequest *rh;
164 * Peer we tried to disconnect.
166 struct GNUNET_PeerIdentity peer;
172 * Our peerinfo notification context. We use notification
173 * to instantly learn about new peers as they are discovered.
175 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
180 static struct GNUNET_SCHEDULER_Handle *sched;
185 static const struct GNUNET_CONFIGURATION_Handle *cfg;
188 * Handle to the core API.
190 static struct GNUNET_CORE_Handle *handle;
193 * Handle to the transport API.
195 static struct GNUNET_TRANSPORT_Handle *transport;
198 * Identity of this peer.
200 static struct GNUNET_PeerIdentity my_identity;
203 * Linked list of all of our friends, all of our current neighbours
204 * and all peers for which we have HELLOs. So pretty much everyone.
206 static struct PeerList *peers;
209 * Flag to disallow non-friend connections (pure F2F mode).
211 static int friends_only;
214 * Minimum number of friends to have in the
215 * connection set before we allow non-friends.
217 static unsigned int minimum_friend_count;
220 * Number of peers (friends and others) that we are currently connected to.
222 static unsigned int connection_count;
225 * Target number of connections.
227 static unsigned int target_connection_count;
230 * Number of friends that we are currently connected to.
232 static unsigned int friend_count;
235 * Should the topology daemon try to establish connections?
237 static int autoconnect;
240 * Head of doubly-linked list of active 'disconnect' requests that we have issued.
242 static struct DisconnectList *disconnect_head;
245 * Head of doubly-linked list of active 'disconnect' requests that we have issued.
247 static struct DisconnectList *disconnect_tail;
251 * Function called once our request to 'disconnect' a peer
254 * @param cls our 'struct DisconnectList'
258 disconnect_done (void *cls,
259 const struct GNUNET_SCHEDULER_TaskContext *tc)
261 struct DisconnectList *dl = cls;
263 GNUNET_CONTAINER_DLL_remove (disconnect_head,
271 * Force a disconnect from the specified peer.
272 * FIXME: this policy change is never undone; how do we reconnect ever?
275 force_disconnect (const struct GNUNET_PeerIdentity *peer)
277 struct DisconnectList *dl;
279 dl = GNUNET_malloc (sizeof (struct DisconnectList));
281 GNUNET_CONTAINER_DLL_insert (disconnect_head,
284 dl->rh = GNUNET_TRANSPORT_blacklist (sched, cfg,
286 GNUNET_TIME_UNIT_FOREVER_REL,
287 GNUNET_TIME_UNIT_FOREVER_REL,
294 * Function called by core when our attempt to connect succeeded.
297 connect_completed_callback (void *cls,
298 const struct GNUNET_SCHEDULER_TaskContext *tc)
300 struct PeerList *pos = cls;
302 pos->connect_req = NULL;
307 * Try to connect to the specified peer.
309 * @param pos peer to connect to
312 attempt_connect (struct PeerList *pos)
314 if (GNUNET_YES == pos->is_friend)
315 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT_FRIEND);
317 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT);
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "Asking core to connect to `%s'\n",
321 GNUNET_i2s (&pos->id));
323 pos->connect_req = GNUNET_CORE_peer_request_connect (sched, cfg,
324 GNUNET_TIME_UNIT_MINUTES,
326 &connect_completed_callback,
332 * Find a peer in our linked list.
333 * FIXME: should probably use a hash map instead.
336 find_peer (const struct GNUNET_PeerIdentity * peer)
338 struct PeerList *pos;
343 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
352 * Check if an additional connection from the given peer is allowed.
355 is_connection_allowed (struct PeerList *peer)
357 if (0 == memcmp (&my_identity, &peer->id, sizeof (struct GNUNET_PeerIdentity)))
358 return GNUNET_SYSERR; /* disallow connections to self */
361 if (GNUNET_YES == friends_only)
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Determined that `%s' is not allowed to connect (not a friend)\n",
366 GNUNET_i2s (&peer->id));
368 return GNUNET_SYSERR;
370 if (friend_count >= minimum_friend_count)
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
375 GNUNET_i2s (&peer->id));
377 return GNUNET_SYSERR;
382 * Create a new entry in the peer list.
384 * @param peer identity of the new entry
385 * @param hello hello message, can be NULL
386 * @param is_friend is the new entry for a friend?
387 * @return the new entry
389 static struct PeerList *
390 make_peer (const struct
391 GNUNET_PeerIdentity * peer,
392 const struct GNUNET_HELLO_Message *hello,
395 struct PeerList *ret;
397 ret = GNUNET_malloc (sizeof (struct PeerList));
399 ret->is_friend = is_friend;
402 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
403 memcpy (ret->hello, hello,
404 GNUNET_HELLO_size (hello));
413 * Free all resources associated with the given peer.
415 * @param peer peer to free
418 free_peer (struct PeerList *peer)
420 struct PeerList *pos;
421 struct PeerList *prev;
430 GNUNET_assert (pos != NULL);
434 prev->next = pos->next;
435 if (pos->hello_req != NULL)
436 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
437 if (pos->connect_req != NULL)
438 GNUNET_CORE_peer_request_connect_cancel (pos->connect_req);
439 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
440 GNUNET_SCHEDULER_cancel (sched,
441 pos->hello_delay_task);
447 * Setup bloom filter for the given peer entry.
449 * @param peer entry to initialize
452 setup_filter (struct PeerList *peer)
454 /* 2^{-5} chance of not sending a HELLO to a peer is
455 acceptably small (if the filter is 50% full);
456 64 bytes of memory are small compared to the rest
457 of the data structure and would only really become
458 "useless" once a HELLO has been passed on to ~100
459 other peers, which is likely more than enough in
460 any case; hence 64, 5 as bloomfilter parameters. */
461 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
462 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
463 /* never send a peer its own HELLO */
464 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->id.hashPubKey);
469 * Function to fill send buffer with HELLO.
471 * @param cls 'struct PeerList' of the target peer
472 * @param size number of bytes available in buf
473 * @param buf where the callee should write the message
474 * @return number of bytes written to buf
477 hello_advertising_ready (void *cls,
483 * Calculate when we would like to send the next HELLO to this
484 * peer and ask for it.
486 * @param cls for which peer to schedule the HELLO
487 * @param tc task context
490 schedule_next_hello (void *cls,
491 const struct GNUNET_SCHEDULER_TaskContext *tc)
493 struct PeerList *pl = cls;
494 struct PeerList *pos;
495 struct PeerList *next;
497 struct GNUNET_TIME_Relative next_adv;
498 struct GNUNET_TIME_Relative rst_time;
500 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
501 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
502 return; /* we're out of here */
504 next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
505 /* find applicable HELLOs */
507 while (NULL != (pos = next))
510 if (pos->hello == NULL)
512 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
513 if (0 == rst_time.value)
515 /* time to discard... */
516 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
521 if (rst_time.value < next_adv.value)
522 next_want = GNUNET_HELLO_size (pos->hello);
523 next_adv = GNUNET_TIME_relative_min (rst_time,
527 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
532 next_adv = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
533 if (next_adv.value == 0)
536 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
540 &hello_advertising_ready,
545 = GNUNET_SCHEDULER_add_delayed (sched,
547 &schedule_next_hello,
553 * Cancel existing requests for sending HELLOs to this peer
554 * and recalculate when we should send HELLOs to it based
555 * on our current state (something changed!).
558 reschedule_hellos (struct PeerList *peer)
560 if (peer->hello_req != NULL)
562 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
563 peer->hello_req = NULL;
565 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
567 GNUNET_SCHEDULER_cancel (sched,
568 peer->hello_delay_task);
569 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
571 peer->hello_delay_task
572 = GNUNET_SCHEDULER_add_now (sched,
573 &schedule_next_hello,
579 * Method called whenever a peer connects.
582 * @param peer peer identity this notification is about
583 * @param latency reported latency of the connection with 'other'
584 * @param distance reported distance (DV) to 'other'
587 connect_notify (void *cls,
589 GNUNET_PeerIdentity * peer,
590 struct GNUNET_TIME_Relative latency,
593 struct PeerList *pos;
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 "Core told us that we are connecting to `%s'\n",
601 pos = find_peer (peer);
604 pos = make_peer (peer, NULL, GNUNET_NO);
605 if (GNUNET_OK != is_connection_allowed (pos))
607 GNUNET_assert (pos->is_friend == GNUNET_NO);
608 pos->is_connected = GNUNET_YES;
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611 "Connection to `%s' is forbidden, forcing disconnect!\n",
614 force_disconnect (&pos->id);
620 GNUNET_assert (GNUNET_NO == pos->is_connected);
621 pos->blacklisted_until.value = 0; /* remove blacklisting */
623 pos->is_connected = GNUNET_YES;
626 reschedule_hellos (pos);
631 * Disconnect from all non-friends (we're below quota).
636 struct PeerList *pos;
641 if ( (GNUNET_NO == pos->is_friend) &&
642 (GNUNET_YES == pos->is_connected) )
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Connection to `%s' is not from a friend, forcing disconnect!\n",
647 GNUNET_i2s (&pos->id));
649 force_disconnect (&pos->id);
657 * Try to add more peers to our connection set.
662 struct PeerList *pos;
667 if ( (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value == 0) &&
668 ( (GNUNET_YES == pos->is_friend) ||
669 (friend_count >= minimum_friend_count) ) &&
670 (GNUNET_YES != pos->is_connected) )
671 attempt_connect (pos);
678 * Method called whenever a peer disconnects.
681 * @param peer peer identity this notification is about
684 disconnect_notify (void *cls,
686 GNUNET_PeerIdentity * peer)
688 struct PeerList *pos;
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692 "Core told us that we disconnected from `%s'\n",
695 pos = find_peer (peer);
701 if (pos->is_connected != GNUNET_YES)
709 if ( (connection_count < target_connection_count) ||
710 (friend_count < minimum_friend_count) )
712 if (friend_count < minimum_friend_count)
714 /* disconnect from all non-friends */
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
717 "Not enough friendly connections, dropping all non-friend connections\n");
725 * Iterator called on each address.
727 * @param cls flag that we will set if we see any addresses
728 * @param tname name of the transport
729 * @param expiration when will the given address expire
730 * @param addr the address of the peer
731 * @param addrlen number of bytes in addr
732 * @return GNUNET_SYSERR always, to terminate iteration
735 address_iterator (void *cls,
737 struct GNUNET_TIME_Absolute expiration,
738 const void *addr, size_t addrlen)
742 return GNUNET_SYSERR;
747 * We've gotten a HELLO from another peer. Consider it for
751 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
754 struct GNUNET_PeerIdentity pid;
755 struct PeerList *peer;
756 struct PeerList *pos;
759 have_address = GNUNET_NO;
760 GNUNET_HELLO_iterate_addresses (hello,
764 if (GNUNET_NO == have_address)
765 return; /* no point in advertising this one... */
766 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
767 peer = find_peer (&pid);
769 peer = make_peer (&pid, hello, GNUNET_NO);
770 // FIXME: check if 'hello' is any different from peer->hello?
771 GNUNET_free_non_null (peer->hello);
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 "Found `%s' from peer `%s' for advertising\n",
778 size = GNUNET_HELLO_size (hello);
779 peer->hello = GNUNET_malloc (size);
780 memcpy (peer->hello, hello, size);
781 if (peer->filter != NULL)
782 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
784 /* since we have a new HELLO to pick from, re-schedule all
785 HELLO requests that are not bound by the HELLO send rate! */
791 if ( (pos->is_connected) &&
792 (GNUNET_TIME_absolute_get_remaining (pos->next_hello_allowed).value <= HELLO_ADVERTISEMENT_MIN_FREQUENCY.value) )
793 reschedule_hellos (pos);
801 * Peerinfo calls this function to let us know about a possible peer
802 * that we might want to connect to.
805 process_peer (void *cls,
806 const struct GNUNET_PeerIdentity *peer,
807 const struct GNUNET_HELLO_Message *hello,
810 struct PeerList *pos;
812 GNUNET_assert (peer != NULL);
813 if (0 == memcmp (&my_identity,
814 peer, sizeof (struct GNUNET_PeerIdentity)))
815 return; /* that's me! */
818 /* free existing HELLO, if any */
819 if (NULL != (pos = find_peer (peer)))
821 GNUNET_free_non_null (pos->hello);
823 if (pos->filter != NULL)
825 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
828 if ( (! pos->is_connected) &&
829 (! pos->is_friend) &&
830 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
835 consider_for_advertising (hello);
836 pos = find_peer (peer);
837 GNUNET_assert (NULL != pos);
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
840 "Considering connecting to peer `%s'\n",
843 if (GNUNET_YES == pos->is_connected)
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
847 "Already connected to peer `%s'\n",
852 if (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value > 0)
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "Already tried peer `%s' recently\n",
859 return; /* peer still blacklisted */
861 if ( (GNUNET_YES == pos->is_friend) ||
862 (GNUNET_YES != friends_only) ||
863 (friend_count >= minimum_friend_count) )
864 attempt_connect (pos);
869 * Discard peer entries for blacklisted peers
870 * where the blacklisting has expired.
873 discard_old_blacklist_entries (void *cls,
874 const struct GNUNET_SCHEDULER_TaskContext *tc)
876 struct PeerList *pos;
877 struct PeerList *next;
879 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
882 while (NULL != (pos = next))
885 if ( (GNUNET_NO == pos->is_friend) &&
886 (GNUNET_NO == pos->is_connected) &&
887 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
890 GNUNET_SCHEDULER_add_delayed (sched,
891 BLACKLIST_AFTER_ATTEMPT,
892 &discard_old_blacklist_entries,
898 * Function called after GNUNET_CORE_connect has succeeded
899 * (or failed for good).
902 * @param server handle to the server, NULL if we failed
903 * @param my_id ID of this peer, NULL if we failed
904 * @param publicKey public key of this peer, NULL if we failed
907 core_init (void *cls,
908 struct GNUNET_CORE_Handle * server,
909 const struct GNUNET_PeerIdentity *
912 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
917 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
918 _("Failed to connect to core service, can not manage topology!\n"));
919 GNUNET_SCHEDULER_shutdown (sched);
923 my_identity = *my_id;
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 GNUNET_SCHEDULER_add_delayed (sched,
930 BLACKLIST_AFTER_ATTEMPT,
931 &discard_old_blacklist_entries,
937 * Read the friends file.
940 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
945 struct GNUNET_PeerIdentity pid;
947 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
948 unsigned int entries_found;
952 GNUNET_CONFIGURATION_get_value_filename (cfg,
957 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
958 _("Option `%s' in section `%s' not specified!\n"),
963 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
964 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
965 | GNUNET_DISK_PERM_USER_WRITE);
966 if (0 != STAT (fn, &frstat))
968 if ((friends_only) || (minimum_friend_count > 0))
970 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
971 _("Could not read friends list `%s'\n"), fn);
976 if (frstat.st_size == 0)
978 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
979 _("Friends file `%s' is empty.\n"),
984 data = GNUNET_malloc_large (frstat.st_size);
985 if (frstat.st_size !=
986 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
989 _("Failed to read friends list from `%s'\n"), fn);
996 while ((pos < frstat.st_size) && isspace (data[pos]))
998 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
999 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1001 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1002 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1004 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1005 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1006 (unsigned long long) pos);
1008 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1012 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1013 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1015 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1016 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1017 (unsigned long long) pos,
1023 fl = make_peer (&pid,
1026 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1027 _("Found friend `%s' in configuration\n"),
1028 GNUNET_i2s (&fl->id));
1030 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1031 while ((pos < frstat.st_size) && isspace (data[pos]))
1036 if ( (minimum_friend_count > entries_found) &&
1037 (friends_only == GNUNET_NO) )
1039 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1040 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1042 if ( (minimum_friend_count > target_connection_count) &&
1043 (friends_only == GNUNET_NO) )
1045 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1046 _("More friendly connections required than target total number of connections.\n"));
1052 * This function is called whenever an encrypted HELLO message is
1055 * @param cls closure
1056 * @param other the other peer involved (sender or receiver, NULL
1057 * for loopback messages where we are both sender and receiver)
1058 * @param message the actual HELLO message
1059 * @param latency reported latency of the connection with 'other'
1060 * @param distance reported distance (DV) to 'other'
1061 * @return GNUNET_OK to keep the connection open,
1062 * GNUNET_SYSERR to close it (signal serious error)
1065 handle_encrypted_hello (void *cls,
1066 const struct GNUNET_PeerIdentity * other,
1067 const struct GNUNET_MessageHeader *
1069 struct GNUNET_TIME_Relative latency,
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 "Received encrypted `%s' from peer `%s'",
1076 GNUNET_i2s (other));
1078 if (transport != NULL)
1079 GNUNET_TRANSPORT_offer_hello (transport,
1086 * Function to fill send buffer with HELLO.
1088 * @param cls 'struct PeerList' of the target peer
1089 * @param size number of bytes available in buf
1090 * @param buf where the callee should write the message
1091 * @return number of bytes written to buf
1094 hello_advertising_ready (void *cls,
1098 struct PeerList *pl = cls;
1099 struct PeerList *pos;
1100 struct PeerList *next;
1104 pl->hello_req = NULL;
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1107 "Data solicited for `%s', considering sending `%s'",
1108 GNUNET_i2s (&pl->id),
1111 /* find applicable HELLOs */
1113 while (NULL != (pos = next))
1116 if (pos->hello == NULL)
1118 if (0 == GNUNET_TIME_absolute_get_remaining (pos->filter_expiration).value)
1120 /* time to discard... */
1121 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
1125 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
1126 &pl->id.hashPubKey))
1132 hs = GNUNET_HELLO_size (pos->hello);
1136 memcpy (buf, pos->hello, want);
1137 GNUNET_CONTAINER_bloomfilter_add (pos->filter,
1138 &pl->id.hashPubKey);
1139 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "Sending %u bytes of `%s's",
1143 (unsigned int) want,
1148 pl->hello_delay_task
1149 = GNUNET_SCHEDULER_add_now (sched,
1150 &schedule_next_hello,
1157 * Last task run during shutdown. Disconnects us from
1158 * the transport and core.
1161 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1163 struct DisconnectList *dl;
1165 if (NULL != peerinfo_notify)
1167 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1168 peerinfo_notify = NULL;
1170 GNUNET_TRANSPORT_disconnect (transport);
1172 while (NULL != peers)
1176 GNUNET_CORE_disconnect (handle);
1179 while (NULL != (dl = disconnect_head))
1181 GNUNET_CONTAINER_DLL_remove (disconnect_head,
1184 GNUNET_TRANSPORT_blacklist_cancel (dl->rh);
1191 * Main function that will be run.
1193 * @param cls closure
1194 * @param s the scheduler to use
1195 * @param args remaining command-line arguments
1196 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1197 * @param c configuration
1201 struct GNUNET_SCHEDULER_Handle * s,
1203 const char *cfgfile,
1204 const struct GNUNET_CONFIGURATION_Handle * c)
1206 struct GNUNET_CORE_MessageHandler handlers[] =
1208 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1211 unsigned long long opt;
1215 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1218 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1222 GNUNET_CONFIGURATION_get_value_number (cfg,
1227 minimum_friend_count = (unsigned int) opt;
1229 GNUNET_CONFIGURATION_get_value_number (cfg,
1231 "TARGET-CONNECTION-COUNT",
1234 target_connection_count = (unsigned int) opt;
1236 if ( (friends_only == GNUNET_YES) ||
1237 (minimum_friend_count > 0) )
1238 read_friends_file (cfg);
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1241 "Topology would like %u connections with at least %u friends (%s)\n",
1242 target_connection_count,
1243 minimum_friend_count,
1244 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1246 transport = GNUNET_TRANSPORT_connect (sched,
1252 handle = GNUNET_CORE_connect (sched,
1254 GNUNET_TIME_UNIT_FOREVER_REL,
1263 GNUNET_SCHEDULER_add_delayed (sched,
1264 GNUNET_TIME_UNIT_FOREVER_REL,
1265 &cleaning_task, NULL);
1266 if (NULL == transport)
1268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1269 _("Failed to connect to `%s' service.\n"),
1271 GNUNET_SCHEDULER_shutdown (sched);
1276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1277 _("Failed to connect to `%s' service.\n"),
1279 GNUNET_SCHEDULER_shutdown (sched);
1282 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1289 * gnunet-daemon-topology command line options.
1291 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1292 GNUNET_GETOPT_OPTION_END
1297 * The main function for the topology daemon.
1299 * @param argc number of arguments from the command line
1300 * @param argv command line arguments
1301 * @return 0 ok, 1 on error
1304 main (int argc, char *const *argv)
1309 GNUNET_PROGRAM_run (argc,
1312 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1314 &run, NULL)) ? 0 : 1;
1318 /* end of gnunet-daemon-topology.c */