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_TransmitHandle *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_CORE_PeerRequestHandle *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. This is currently done by
272 * changing the bandwidth policy to 0 bytes per second.
273 * FIXME: maybe we want a nicer CORE API for both connect and disconnect...
274 * FIXME: this policy change is never undone; how do we reconnect ever?
277 force_disconnect (const struct GNUNET_PeerIdentity *peer)
279 struct DisconnectList *dl;
281 dl = GNUNET_malloc (sizeof (struct DisconnectList));
283 GNUNET_CONTAINER_DLL_insert (disconnect_head,
286 dl->rh = GNUNET_CORE_peer_request_disconnect (sched, cfg,
287 GNUNET_TIME_UNIT_FOREVER_REL,
295 * Function called by core when our attempt to connect succeeded.
296 * Transmits a 'DUMMY' message to trigger the session key exchange.
297 * FIXME: this is an issue with the current CORE API.
300 ready_callback (void *cls,
301 size_t size, void *buf)
303 struct PeerList *pos = cls;
304 struct GNUNET_MessageHeader hdr;
306 pos->connect_req = NULL;
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Core told us that our attempt to connect failed.\n");
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Sending dummy message to establish connection.\n");
319 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
320 hdr.type = htons (GNUNET_MESSAGE_TYPE_TOPOLOGY_DUMMY);
321 memcpy (buf, &hdr, sizeof (struct GNUNET_MessageHeader));
322 return sizeof (struct GNUNET_MessageHeader);
327 * Try to connect to the specified peer.
329 * @param pos peer to connect to
332 attempt_connect (struct PeerList *pos)
334 if (GNUNET_YES == pos->is_friend)
335 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT_FRIEND);
337 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT);
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Asking core to connect to `%s'\n",
341 GNUNET_i2s (&pos->id));
343 pos->connect_req = GNUNET_CORE_notify_transmit_ready (handle,
345 GNUNET_TIME_UNIT_MINUTES,
347 sizeof(struct GNUNET_MessageHeader),
354 * Find a peer in our linked list.
355 * FIXME: should probably use a hash map instead.
358 find_peer (const struct GNUNET_PeerIdentity * peer)
360 struct PeerList *pos;
365 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
374 * Check if an additional connection from the given peer is allowed.
377 is_connection_allowed (struct PeerList *peer)
379 if (0 == memcmp (&my_identity, &peer->id, sizeof (struct GNUNET_PeerIdentity)))
380 return GNUNET_SYSERR; /* disallow connections to self */
383 if (GNUNET_YES == friends_only)
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Determined that `%s' is not allowed to connect (not a friend)\n",
388 GNUNET_i2s (&peer->id));
390 return GNUNET_SYSERR;
392 if (friend_count >= minimum_friend_count)
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
397 GNUNET_i2s (&peer->id));
399 return GNUNET_SYSERR;
404 * Create a new entry in the peer list.
406 * @param peer identity of the new entry
407 * @param hello hello message, can be NULL
408 * @param is_friend is the new entry for a friend?
409 * @return the new entry
411 static struct PeerList *
412 make_peer (const struct
413 GNUNET_PeerIdentity * peer,
414 const struct GNUNET_HELLO_Message *hello,
417 struct PeerList *ret;
419 ret = GNUNET_malloc (sizeof (struct PeerList));
421 ret->is_friend = is_friend;
424 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
425 memcpy (ret->hello, hello,
426 GNUNET_HELLO_size (hello));
435 * Free all resources associated with the given peer.
437 * @param peer peer to free
440 free_peer (struct PeerList *peer)
442 struct PeerList *pos;
443 struct PeerList *prev;
452 GNUNET_assert (pos != NULL);
456 prev->next = pos->next;
457 if (pos->hello_req != NULL)
458 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
459 if (pos->connect_req != NULL)
460 GNUNET_CORE_notify_transmit_ready_cancel (pos->connect_req);
461 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
462 GNUNET_SCHEDULER_cancel (sched,
463 pos->hello_delay_task);
469 * Setup bloom filter for the given peer entry.
471 * @param peer entry to initialize
474 setup_filter (struct PeerList *peer)
476 /* 2^{-5} chance of not sending a HELLO to a peer is
477 acceptably small (if the filter is 50% full);
478 64 bytes of memory are small compared to the rest
479 of the data structure and would only really become
480 "useless" once a HELLO has been passed on to ~100
481 other peers, which is likely more than enough in
482 any case; hence 64, 5 as bloomfilter parameters. */
483 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
484 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
485 /* never send a peer its own HELLO */
486 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->id.hashPubKey);
491 * Function to fill send buffer with HELLO.
493 * @param cls 'struct PeerList' of the target peer
494 * @param size number of bytes available in buf
495 * @param buf where the callee should write the message
496 * @return number of bytes written to buf
499 hello_advertising_ready (void *cls,
505 * Calculate when we would like to send the next HELLO to this
506 * peer and ask for it.
508 * @param cls for which peer to schedule the HELLO
509 * @param tc task context
512 schedule_next_hello (void *cls,
513 const struct GNUNET_SCHEDULER_TaskContext *tc)
515 struct PeerList *pl = cls;
516 struct PeerList *pos;
517 struct PeerList *next;
519 struct GNUNET_TIME_Relative next_adv;
520 struct GNUNET_TIME_Relative rst_time;
522 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
523 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
524 return; /* we're out of here */
526 next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
527 /* find applicable HELLOs */
529 while (NULL != (pos = next))
532 if (pos->hello == NULL)
534 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
535 if (0 == rst_time.value)
537 /* time to discard... */
538 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
543 if (rst_time.value < next_adv.value)
544 next_want = GNUNET_HELLO_size (pos->hello);
545 next_adv = GNUNET_TIME_relative_min (rst_time,
549 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
554 next_adv = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
555 if (next_adv.value == 0)
558 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
562 &hello_advertising_ready,
567 = GNUNET_SCHEDULER_add_delayed (sched,
569 &schedule_next_hello,
575 * Cancel existing requests for sending HELLOs to this peer
576 * and recalculate when we should send HELLOs to it based
577 * on our current state (something changed!).
580 reschedule_hellos (struct PeerList *peer)
582 if (peer->hello_req != NULL)
584 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
585 peer->hello_req = NULL;
587 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
589 GNUNET_SCHEDULER_cancel (sched,
590 peer->hello_delay_task);
591 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
593 peer->hello_delay_task
594 = GNUNET_SCHEDULER_add_now (sched,
595 &schedule_next_hello,
601 * Method called whenever a peer connects.
604 * @param peer peer identity this notification is about
605 * @param latency reported latency of the connection with 'other'
606 * @param distance reported distance (DV) to 'other'
609 connect_notify (void *cls,
611 GNUNET_PeerIdentity * peer,
612 struct GNUNET_TIME_Relative latency,
615 struct PeerList *pos;
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "Core told us that we are connecting to `%s'\n",
623 pos = find_peer (peer);
626 pos = make_peer (peer, NULL, GNUNET_NO);
627 if (GNUNET_OK != is_connection_allowed (pos))
629 GNUNET_assert (pos->is_friend == GNUNET_NO);
630 pos->is_connected = GNUNET_YES;
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633 "Connection to `%s' is forbidden, forcing disconnect!\n",
636 force_disconnect (&pos->id);
642 GNUNET_assert (GNUNET_NO == pos->is_connected);
643 pos->blacklisted_until.value = 0; /* remove blacklisting */
645 pos->is_connected = GNUNET_YES;
648 reschedule_hellos (pos);
653 * Disconnect from all non-friends (we're below quota).
658 struct PeerList *pos;
663 if ( (GNUNET_NO == pos->is_friend) &&
664 (GNUNET_YES == pos->is_connected) )
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Connection to `%s' is not from a friend, forcing disconnect!\n",
669 GNUNET_i2s (&pos->id));
671 force_disconnect (&pos->id);
679 * Try to add more peers to our connection set.
684 struct PeerList *pos;
689 if ( (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value == 0) &&
690 ( (GNUNET_YES == pos->is_friend) ||
691 (friend_count >= minimum_friend_count) ) &&
692 (GNUNET_YES != pos->is_connected) )
693 attempt_connect (pos);
700 * Method called whenever a peer disconnects.
703 * @param peer peer identity this notification is about
706 disconnect_notify (void *cls,
708 GNUNET_PeerIdentity * peer)
710 struct PeerList *pos;
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Core told us that we disconnected from `%s'\n",
717 pos = find_peer (peer);
723 if (pos->is_connected != GNUNET_YES)
731 if ( (connection_count < target_connection_count) ||
732 (friend_count < minimum_friend_count) )
734 if (friend_count < minimum_friend_count)
736 /* disconnect from all non-friends */
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
739 "Not enough friendly connections, dropping all non-friend connections\n");
747 * Iterator called on each address.
749 * @param cls flag that we will set if we see any addresses
750 * @param tname name of the transport
751 * @param expiration when will the given address expire
752 * @param addr the address of the peer
753 * @param addrlen number of bytes in addr
754 * @return GNUNET_SYSERR always, to terminate iteration
757 address_iterator (void *cls,
759 struct GNUNET_TIME_Absolute expiration,
760 const void *addr, size_t addrlen)
764 return GNUNET_SYSERR;
769 * We've gotten a HELLO from another peer. Consider it for
773 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
776 struct GNUNET_PeerIdentity pid;
777 struct PeerList *peer;
778 struct PeerList *pos;
781 have_address = GNUNET_NO;
782 GNUNET_HELLO_iterate_addresses (hello,
786 if (GNUNET_NO == have_address)
787 return; /* no point in advertising this one... */
788 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
789 peer = find_peer (&pid);
791 peer = make_peer (&pid, hello, GNUNET_NO);
792 // FIXME: check if 'hello' is any different from peer->hello?
793 GNUNET_free_non_null (peer->hello);
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "Found `%s' from peer `%s' for advertising\n",
800 size = GNUNET_HELLO_size (hello);
801 peer->hello = GNUNET_malloc (size);
802 memcpy (peer->hello, hello, size);
803 if (peer->filter != NULL)
804 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
806 /* since we have a new HELLO to pick from, re-schedule all
807 HELLO requests that are not bound by the HELLO send rate! */
813 if ( (pos->is_connected) &&
814 (GNUNET_TIME_absolute_get_remaining (pos->next_hello_allowed).value <= HELLO_ADVERTISEMENT_MIN_FREQUENCY.value) )
815 reschedule_hellos (pos);
823 * Peerinfo calls this function to let us know about a possible peer
824 * that we might want to connect to.
827 process_peer (void *cls,
828 const struct GNUNET_PeerIdentity *peer,
829 const struct GNUNET_HELLO_Message *hello,
832 struct PeerList *pos;
834 GNUNET_assert (peer != NULL);
835 if (0 == memcmp (&my_identity,
836 peer, sizeof (struct GNUNET_PeerIdentity)))
837 return; /* that's me! */
840 /* free existing HELLO, if any */
841 if (NULL != (pos = find_peer (peer)))
843 GNUNET_free_non_null (pos->hello);
845 if (pos->filter != NULL)
847 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
850 if ( (! pos->is_connected) &&
851 (! pos->is_friend) &&
852 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
857 consider_for_advertising (hello);
858 pos = find_peer (peer);
859 GNUNET_assert (NULL != pos);
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Considering connecting to peer `%s'\n",
865 if (GNUNET_YES == pos->is_connected)
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
869 "Already connected to peer `%s'\n",
874 if (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value > 0)
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "Already tried peer `%s' recently\n",
881 return; /* peer still blacklisted */
883 if ( (GNUNET_YES == pos->is_friend) ||
884 (GNUNET_YES != friends_only) ||
885 (friend_count >= minimum_friend_count) )
886 attempt_connect (pos);
891 * Discard peer entries for blacklisted peers
892 * where the blacklisting has expired.
895 discard_old_blacklist_entries (void *cls,
896 const struct GNUNET_SCHEDULER_TaskContext *tc)
898 struct PeerList *pos;
899 struct PeerList *next;
901 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
904 while (NULL != (pos = next))
907 if ( (GNUNET_NO == pos->is_friend) &&
908 (GNUNET_NO == pos->is_connected) &&
909 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
912 GNUNET_SCHEDULER_add_delayed (sched,
913 BLACKLIST_AFTER_ATTEMPT,
914 &discard_old_blacklist_entries,
920 * Function called after GNUNET_CORE_connect has succeeded
921 * (or failed for good).
924 * @param server handle to the server, NULL if we failed
925 * @param my_id ID of this peer, NULL if we failed
926 * @param publicKey public key of this peer, NULL if we failed
929 core_init (void *cls,
930 struct GNUNET_CORE_Handle * server,
931 const struct GNUNET_PeerIdentity *
934 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
939 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
940 _("Failed to connect to core service, can not manage topology!\n"));
941 GNUNET_SCHEDULER_shutdown (sched);
945 my_identity = *my_id;
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951 GNUNET_SCHEDULER_add_delayed (sched,
952 BLACKLIST_AFTER_ATTEMPT,
953 &discard_old_blacklist_entries,
959 * Read the friends file.
962 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
967 struct GNUNET_PeerIdentity pid;
969 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
970 unsigned int entries_found;
974 GNUNET_CONFIGURATION_get_value_filename (cfg,
979 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
980 _("Option `%s' in section `%s' not specified!\n"),
985 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
986 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
987 | GNUNET_DISK_PERM_USER_WRITE);
988 if (0 != STAT (fn, &frstat))
990 if ((friends_only) || (minimum_friend_count > 0))
992 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
993 _("Could not read friends list `%s'\n"), fn);
998 if (frstat.st_size == 0)
1000 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1001 _("Friends file `%s' is empty.\n"),
1006 data = GNUNET_malloc_large (frstat.st_size);
1007 if (frstat.st_size !=
1008 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1010 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1011 _("Failed to read friends list from `%s'\n"), fn);
1018 while ((pos < frstat.st_size) && isspace (data[pos]))
1020 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1021 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1023 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1024 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1026 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1027 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1028 (unsigned long long) pos);
1030 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1034 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1035 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1037 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1038 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1039 (unsigned long long) pos,
1045 fl = make_peer (&pid,
1048 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1049 _("Found friend `%s' in configuration\n"),
1050 GNUNET_i2s (&fl->id));
1052 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1053 while ((pos < frstat.st_size) && isspace (data[pos]))
1058 if ( (minimum_friend_count > entries_found) &&
1059 (friends_only == GNUNET_NO) )
1061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1062 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1064 if ( (minimum_friend_count > target_connection_count) &&
1065 (friends_only == GNUNET_NO) )
1067 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1068 _("More friendly connections required than target total number of connections.\n"));
1074 * This function is called whenever an encrypted HELLO message is
1077 * @param cls closure
1078 * @param other the other peer involved (sender or receiver, NULL
1079 * for loopback messages where we are both sender and receiver)
1080 * @param message the actual HELLO message
1081 * @param latency reported latency of the connection with 'other'
1082 * @param distance reported distance (DV) to 'other'
1083 * @return GNUNET_OK to keep the connection open,
1084 * GNUNET_SYSERR to close it (signal serious error)
1087 handle_encrypted_hello (void *cls,
1088 const struct GNUNET_PeerIdentity * other,
1089 const struct GNUNET_MessageHeader *
1091 struct GNUNET_TIME_Relative latency,
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096 "Received encrypted `%s' from peer `%s'",
1098 GNUNET_i2s (other));
1100 if (transport != NULL)
1101 GNUNET_TRANSPORT_offer_hello (transport,
1108 * Function to fill send buffer with HELLO.
1110 * @param cls 'struct PeerList' of the target peer
1111 * @param size number of bytes available in buf
1112 * @param buf where the callee should write the message
1113 * @return number of bytes written to buf
1116 hello_advertising_ready (void *cls,
1120 struct PeerList *pl = cls;
1121 struct PeerList *pos;
1122 struct PeerList *next;
1126 pl->hello_req = NULL;
1128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129 "Data solicited for `%s', considering sending `%s'",
1130 GNUNET_i2s (&pl->id),
1133 /* find applicable HELLOs */
1135 while (NULL != (pos = next))
1138 if (pos->hello == NULL)
1140 if (0 == GNUNET_TIME_absolute_get_remaining (pos->filter_expiration).value)
1142 /* time to discard... */
1143 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
1147 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
1148 &pl->id.hashPubKey))
1154 hs = GNUNET_HELLO_size (pos->hello);
1158 memcpy (buf, pos->hello, want);
1159 GNUNET_CONTAINER_bloomfilter_add (pos->filter,
1160 &pl->id.hashPubKey);
1161 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "Sending %u bytes of `%s's",
1165 (unsigned int) want,
1170 pl->hello_delay_task
1171 = GNUNET_SCHEDULER_add_now (sched,
1172 &schedule_next_hello,
1179 * Last task run during shutdown. Disconnects us from
1180 * the transport and core.
1183 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1185 struct DisconnectList *dl;
1187 if (NULL != peerinfo_notify)
1189 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1190 peerinfo_notify = NULL;
1192 GNUNET_TRANSPORT_disconnect (transport);
1194 while (NULL != peers)
1198 GNUNET_CORE_disconnect (handle);
1201 while (NULL != (dl = disconnect_head))
1203 GNUNET_CONTAINER_DLL_remove (disconnect_head,
1206 GNUNET_CORE_peer_request_cancel (dl->rh);
1213 * Main function that will be run.
1215 * @param cls closure
1216 * @param s the scheduler to use
1217 * @param args remaining command-line arguments
1218 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1219 * @param c configuration
1223 struct GNUNET_SCHEDULER_Handle * s,
1225 const char *cfgfile,
1226 const struct GNUNET_CONFIGURATION_Handle * c)
1228 struct GNUNET_CORE_MessageHandler handlers[] =
1230 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1233 unsigned long long opt;
1237 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1240 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1244 GNUNET_CONFIGURATION_get_value_number (cfg,
1249 minimum_friend_count = (unsigned int) opt;
1251 GNUNET_CONFIGURATION_get_value_number (cfg,
1253 "TARGET-CONNECTION-COUNT",
1256 target_connection_count = (unsigned int) opt;
1258 if ( (friends_only == GNUNET_YES) ||
1259 (minimum_friend_count > 0) )
1260 read_friends_file (cfg);
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1263 "Topology would like %u connections with at least %u friends (%s)\n",
1264 target_connection_count,
1265 minimum_friend_count,
1266 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1268 transport = GNUNET_TRANSPORT_connect (sched,
1274 handle = GNUNET_CORE_connect (sched,
1276 GNUNET_TIME_UNIT_FOREVER_REL,
1285 GNUNET_SCHEDULER_add_delayed (sched,
1286 GNUNET_TIME_UNIT_FOREVER_REL,
1287 &cleaning_task, NULL);
1288 if (NULL == transport)
1290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1291 _("Failed to connect to `%s' service.\n"),
1293 GNUNET_SCHEDULER_shutdown (sched);
1298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1299 _("Failed to connect to `%s' service.\n"),
1301 GNUNET_SCHEDULER_shutdown (sched);
1304 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1311 * gnunet-daemon-topology command line options.
1313 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1314 GNUNET_GETOPT_OPTION_END
1319 * The main function for the topology daemon.
1321 * @param argc number of arguments from the command line
1322 * @param argv command line arguments
1323 * @return 0 ok, 1 on error
1326 main (int argc, char *const *argv)
1331 GNUNET_PROGRAM_run (argc,
1334 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1336 &run, NULL)) ? 0 : 1;
1340 /* end of gnunet-daemon-topology.c */