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 * - make sure that PEERINFO *also* notifies us when a HELLO expires
28 * (otherwise the 'soft state' of this module does not work nicely)
29 * - CORE API's connect/disconnect API is not nice (we think we are
30 * connected when we are not; disconnect using bandwidth-limiting
31 * does not really work and is not nice)
33 * - move code to use hash table instead of linked list
34 * - instead of periodically discarding blacklisted entries,
35 * simply add task that is triggered at the right time (earlier free,
37 * - check if new HELLO learned is different from old HELLO
38 * before resetting entire state!
43 #include "gnunet_core_service.h"
44 #include "gnunet_protocols.h"
45 #include "gnunet_peerinfo_service.h"
46 #include "gnunet_transport_service.h"
47 #include "gnunet_util_lib.h"
50 #define DEBUG_TOPOLOGY GNUNET_NO
53 * For how long do we blacklist a peer after a failed connection
56 #define BLACKLIST_AFTER_ATTEMPT GNUNET_TIME_UNIT_HOURS
59 * For how long do we blacklist a friend after a failed connection
62 #define BLACKLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
65 * How often do we at most advertise any HELLO to a peer?
67 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
70 * How often do we at most advertise the same HELLO to the same peer?
72 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
76 * List of neighbours, friends and blacklisted peers.
82 * This is a linked list.
84 struct PeerList *next;
87 * Our handle for the request to transmit HELLOs to this peer; NULL
88 * if no such request is pending.
90 struct GNUNET_CORE_TransmitHandle *hello_req;
93 * Our handle for the request to connect to this peer; NULL if no
94 * such request is pending.
96 struct GNUNET_CORE_TransmitHandle *connect_req;
99 * Pointer to the HELLO message of this peer; can be NULL.
101 struct GNUNET_HELLO_Message *hello;
104 * Bloom filter used to mark which peers already got the HELLO
107 struct GNUNET_CONTAINER_BloomFilter *filter;
110 * Is this peer listed here because he is a friend?
115 * Are we connected to this peer right now?
120 * Until what time should we not try to connect again
123 struct GNUNET_TIME_Absolute blacklisted_until;
126 * Next time we are allowed to transmit a HELLO to this peer?
128 struct GNUNET_TIME_Absolute next_hello_allowed;
131 * When should we reset the bloom filter of this entry?
133 struct GNUNET_TIME_Absolute filter_expiration;
136 * ID of task we use to wait for the time to send the next HELLO
139 GNUNET_SCHEDULER_TaskIdentifier hello_delay_task;
144 struct GNUNET_PeerIdentity id;
150 * Entry in linked list of active 'disconnect' requests that we have issued.
152 struct DisconnectList
155 * This is a doubly-linked list.
157 struct DisconnectList *next;
160 * This is a doubly-linked list.
162 struct DisconnectList *prev;
165 * Our request handle.
167 struct GNUNET_CORE_PeerRequestHandle *rh;
170 * Peer we tried to disconnect.
172 struct GNUNET_PeerIdentity peer;
178 * Our peerinfo notification context. We use notification
179 * to instantly learn about new peers as they are discovered.
181 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
186 static struct GNUNET_SCHEDULER_Handle *sched;
191 static const struct GNUNET_CONFIGURATION_Handle *cfg;
194 * Handle to the core API.
196 static struct GNUNET_CORE_Handle *handle;
199 * Handle to the transport API.
201 static struct GNUNET_TRANSPORT_Handle *transport;
204 * Identity of this peer.
206 static struct GNUNET_PeerIdentity my_identity;
209 * Linked list of all of our friends, all of our current neighbours
210 * and all peers for which we have HELLOs. So pretty much everyone.
212 static struct PeerList *peers;
215 * Flag to disallow non-friend connections (pure F2F mode).
217 static int friends_only;
220 * Minimum number of friends to have in the
221 * connection set before we allow non-friends.
223 static unsigned int minimum_friend_count;
226 * Number of peers (friends and others) that we are currently connected to.
228 static unsigned int connection_count;
231 * Target number of connections.
233 static unsigned int target_connection_count;
236 * Number of friends that we are currently connected to.
238 static unsigned int friend_count;
241 * Should the topology daemon try to establish connections?
243 static int autoconnect;
246 * Head of doubly-linked list of active 'disconnect' requests that we have issued.
248 static struct DisconnectList *disconnect_head;
251 * Head of doubly-linked list of active 'disconnect' requests that we have issued.
253 static struct DisconnectList *disconnect_tail;
257 * Function called once our request to 'disconnect' a peer
260 * @param cls our 'struct DisconnectList'
264 disconnect_done (void *cls,
265 const struct GNUNET_SCHEDULER_TaskContext *tc)
267 struct DisconnectList *dl = cls;
269 GNUNET_CONTAINER_DLL_remove (disconnect_head,
277 * Force a disconnect from the specified peer. This is currently done by
278 * changing the bandwidth policy to 0 bytes per second.
279 * FIXME: maybe we want a nicer CORE API for both connect and disconnect...
280 * FIXME: this policy change is never undone; how do we reconnect ever?
283 force_disconnect (const struct GNUNET_PeerIdentity *peer)
285 struct DisconnectList *dl;
287 dl = GNUNET_malloc (sizeof (struct DisconnectList));
289 GNUNET_CONTAINER_DLL_insert (disconnect_head,
292 dl->rh = GNUNET_CORE_peer_request_disconnect (sched, cfg,
293 GNUNET_TIME_UNIT_FOREVER_REL,
301 * Function called by core when our attempt to connect succeeded.
302 * Transmits a 'DUMMY' message to trigger the session key exchange.
303 * FIXME: this is an issue with the current CORE API.
306 ready_callback (void *cls,
307 size_t size, void *buf)
309 struct PeerList *pos = cls;
310 struct GNUNET_MessageHeader hdr;
312 pos->connect_req = NULL;
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Core told us that our attempt to connect failed.\n");
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Sending dummy message to establish connection.\n");
325 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
326 hdr.type = htons (GNUNET_MESSAGE_TYPE_TOPOLOGY_DUMMY);
327 memcpy (buf, &hdr, sizeof (struct GNUNET_MessageHeader));
328 return sizeof (struct GNUNET_MessageHeader);
333 * Try to connect to the specified peer.
335 * @param pos peer to connect to
338 attempt_connect (struct PeerList *pos)
340 if (GNUNET_YES == pos->is_friend)
341 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT_FRIEND);
343 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT);
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Asking core to connect to `%s'\n",
347 GNUNET_i2s (&pos->id));
349 pos->connect_req = GNUNET_CORE_notify_transmit_ready (handle,
351 GNUNET_TIME_UNIT_MINUTES,
353 sizeof(struct GNUNET_MessageHeader),
360 * Find a peer in our linked list.
361 * FIXME: should probably use a hash map instead.
364 find_peer (const struct GNUNET_PeerIdentity * peer)
366 struct PeerList *pos;
371 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
380 * Check if an additional connection from the given peer is allowed.
383 is_connection_allowed (struct PeerList *peer)
385 if (0 == memcmp (&my_identity, &peer->id, sizeof (struct GNUNET_PeerIdentity)))
386 return GNUNET_SYSERR; /* disallow connections to self */
389 if (GNUNET_YES == friends_only)
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "Determined that `%s' is not allowed to connect (not a friend)\n",
394 GNUNET_i2s (&peer->id));
396 return GNUNET_SYSERR;
398 if (friend_count >= minimum_friend_count)
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
403 GNUNET_i2s (&peer->id));
405 return GNUNET_SYSERR;
410 * Create a new entry in the peer list.
412 * @param peer identity of the new entry
413 * @param hello hello message, can be NULL
414 * @param is_friend is the new entry for a friend?
415 * @return the new entry
417 static struct PeerList *
418 make_peer (const struct
419 GNUNET_PeerIdentity * peer,
420 const struct GNUNET_HELLO_Message *hello,
423 struct PeerList *ret;
425 ret = GNUNET_malloc (sizeof (struct PeerList));
427 ret->is_friend = is_friend;
430 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
431 memcpy (ret->hello, hello,
432 GNUNET_HELLO_size (hello));
441 * Free all resources associated with the given peer.
443 * @param peer peer to free
446 free_peer (struct PeerList *peer)
448 struct PeerList *pos;
449 struct PeerList *prev;
458 GNUNET_assert (pos != NULL);
462 prev->next = pos->next;
463 if (pos->hello_req != NULL)
464 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
465 if (pos->connect_req != NULL)
466 GNUNET_CORE_notify_transmit_ready_cancel (pos->connect_req);
467 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
468 GNUNET_SCHEDULER_cancel (sched,
469 pos->hello_delay_task);
475 * Setup bloom filter for the given peer entry.
477 * @param peer entry to initialize
480 setup_filter (struct PeerList *peer)
482 /* 2^{-5} chance of not sending a HELLO to a peer is
483 acceptably small (if the filter is 50% full);
484 64 bytes of memory are small compared to the rest
485 of the data structure and would only really become
486 "useless" once a HELLO has been passed on to ~100
487 other peers, which is likely more than enough in
488 any case; hence 64, 5 as bloomfilter parameters. */
489 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
490 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
491 /* never send a peer its own HELLO */
492 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->id.hashPubKey);
497 * Function to fill send buffer with HELLO.
499 * @param cls 'struct PeerList' of the target peer
500 * @param size number of bytes available in buf
501 * @param buf where the callee should write the message
502 * @return number of bytes written to buf
505 hello_advertising_ready (void *cls,
511 * Calculate when we would like to send the next HELLO to this
512 * peer and ask for it.
514 * @param cls for which peer to schedule the HELLO
515 * @param tc task context
518 schedule_next_hello (void *cls,
519 const struct GNUNET_SCHEDULER_TaskContext *tc)
521 struct PeerList *pl = cls;
522 struct PeerList *pos;
523 struct PeerList *next;
525 struct GNUNET_TIME_Relative next_adv;
526 struct GNUNET_TIME_Relative rst_time;
528 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
529 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
530 return; /* we're out of here */
532 next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
533 /* find applicable HELLOs */
535 while (NULL != (pos = next))
538 if (pos->hello == NULL)
540 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
541 if (0 == rst_time.value)
543 /* time to discard... */
544 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
549 if (rst_time.value < next_adv.value)
550 next_want = GNUNET_HELLO_size (pos->hello);
551 next_adv = GNUNET_TIME_relative_min (rst_time,
555 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
560 next_adv = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
561 if (next_adv.value == 0)
564 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
568 &hello_advertising_ready,
573 = GNUNET_SCHEDULER_add_delayed (sched,
575 &schedule_next_hello,
581 * Cancel existing requests for sending HELLOs to this peer
582 * and recalculate when we should send HELLOs to it based
583 * on our current state (something changed!).
586 reschedule_hellos (struct PeerList *peer)
588 if (peer->hello_req != NULL)
590 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
591 peer->hello_req = NULL;
593 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
595 GNUNET_SCHEDULER_cancel (sched,
596 peer->hello_delay_task);
597 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
599 peer->hello_delay_task
600 = GNUNET_SCHEDULER_add_now (sched,
601 &schedule_next_hello,
607 * Method called whenever a peer connects.
610 * @param peer peer identity this notification is about
611 * @param latency reported latency of the connection with 'other'
612 * @param distance reported distance (DV) to 'other'
615 connect_notify (void *cls,
617 GNUNET_PeerIdentity * peer,
618 struct GNUNET_TIME_Relative latency,
621 struct PeerList *pos;
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Core told us that we are connecting to `%s'\n",
629 pos = find_peer (peer);
632 pos = make_peer (peer, NULL, GNUNET_NO);
633 if (GNUNET_OK != is_connection_allowed (pos))
635 GNUNET_assert (pos->is_friend == GNUNET_NO);
636 pos->is_connected = GNUNET_YES;
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "Connection to `%s' is forbidden, forcing disconnect!\n",
642 force_disconnect (&pos->id);
648 GNUNET_assert (GNUNET_NO == pos->is_connected);
649 pos->blacklisted_until.value = 0; /* remove blacklisting */
651 pos->is_connected = GNUNET_YES;
654 reschedule_hellos (pos);
659 * Disconnect from all non-friends (we're below quota).
664 struct PeerList *pos;
669 if ( (GNUNET_NO == pos->is_friend) &&
670 (GNUNET_YES == pos->is_connected) )
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 "Connection to `%s' is not from a friend, forcing disconnect!\n",
675 GNUNET_i2s (&pos->id));
677 force_disconnect (&pos->id);
685 * Try to add more peers to our connection set.
690 struct PeerList *pos;
695 if ( (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value == 0) &&
696 ( (GNUNET_YES == pos->is_friend) ||
697 (friend_count >= minimum_friend_count) ) &&
698 (GNUNET_YES != pos->is_connected) )
699 attempt_connect (pos);
706 * Method called whenever a peer disconnects.
709 * @param peer peer identity this notification is about
712 disconnect_notify (void *cls,
714 GNUNET_PeerIdentity * peer)
716 struct PeerList *pos;
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Core told us that we disconnected from `%s'\n",
723 pos = find_peer (peer);
729 if (pos->is_connected != GNUNET_YES)
737 if ( (connection_count < target_connection_count) ||
738 (friend_count < minimum_friend_count) )
740 if (friend_count < minimum_friend_count)
742 /* disconnect from all non-friends */
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Not enough friendly connections, dropping all non-friend connections\n");
753 * Iterator called on each address.
755 * @param cls flag that we will set if we see any addresses
756 * @param tname name of the transport
757 * @param expiration when will the given address expire
758 * @param addr the address of the peer
759 * @param addrlen number of bytes in addr
760 * @return GNUNET_SYSERR always, to terminate iteration
763 address_iterator (void *cls,
765 struct GNUNET_TIME_Absolute expiration,
766 const void *addr, size_t addrlen)
770 return GNUNET_SYSERR;
775 * We've gotten a HELLO from another peer. Consider it for
779 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
782 struct GNUNET_PeerIdentity pid;
783 struct PeerList *peer;
784 struct PeerList *pos;
787 have_address = GNUNET_NO;
788 GNUNET_HELLO_iterate_addresses (hello,
792 if (GNUNET_NO == have_address)
793 return; /* no point in advertising this one... */
794 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
795 peer = find_peer (&pid);
797 peer = make_peer (&pid, hello, GNUNET_NO);
798 // FIXME: check if 'hello' is any different from peer->hello?
799 GNUNET_free_non_null (peer->hello);
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Found `%s' from peer `%s' for advertising\n",
806 size = GNUNET_HELLO_size (hello);
807 peer->hello = GNUNET_malloc (size);
808 memcpy (peer->hello, hello, size);
809 if (peer->filter != NULL)
810 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
812 /* since we have a new HELLO to pick from, re-schedule all
813 HELLO requests that are not bound by the HELLO send rate! */
819 if ( (pos->is_connected) &&
820 (GNUNET_TIME_absolute_get_remaining (pos->next_hello_allowed).value <= HELLO_ADVERTISEMENT_MIN_FREQUENCY.value) )
821 reschedule_hellos (pos);
829 * Peerinfo calls this function to let us know about a possible peer
830 * that we might want to connect to.
833 process_peer (void *cls,
834 const struct GNUNET_PeerIdentity *peer,
835 const struct GNUNET_HELLO_Message *hello,
838 struct PeerList *pos;
840 GNUNET_assert (peer != NULL);
841 if (0 == memcmp (&my_identity,
842 peer, sizeof (struct GNUNET_PeerIdentity)))
843 return; /* that's me! */
846 /* free existing HELLO, if any */
847 if (NULL != (pos = find_peer (peer)))
849 GNUNET_free_non_null (pos->hello);
851 if (pos->filter != NULL)
853 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
856 if ( (! pos->is_connected) &&
857 (! pos->is_friend) &&
858 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
863 consider_for_advertising (hello);
864 pos = find_peer (peer);
865 GNUNET_assert (NULL != pos);
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 "Considering connecting to peer `%s'\n",
871 if (GNUNET_YES == pos->is_connected)
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
875 "Already connected to peer `%s'\n",
880 if (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value > 0)
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Already tried peer `%s' recently\n",
887 return; /* peer still blacklisted */
889 if ( (GNUNET_YES == pos->is_friend) ||
890 (GNUNET_YES != friends_only) ||
891 (friend_count >= minimum_friend_count) )
892 attempt_connect (pos);
897 * Discard peer entries for blacklisted peers
898 * where the blacklisting has expired.
901 discard_old_blacklist_entries (void *cls,
902 const struct GNUNET_SCHEDULER_TaskContext *tc)
904 struct PeerList *pos;
905 struct PeerList *next;
907 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
910 while (NULL != (pos = next))
913 if ( (GNUNET_NO == pos->is_friend) &&
914 (GNUNET_NO == pos->is_connected) &&
915 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
918 GNUNET_SCHEDULER_add_delayed (sched,
919 BLACKLIST_AFTER_ATTEMPT,
920 &discard_old_blacklist_entries,
926 * Function called after GNUNET_CORE_connect has succeeded
927 * (or failed for good).
930 * @param server handle to the server, NULL if we failed
931 * @param my_id ID of this peer, NULL if we failed
932 * @param publicKey public key of this peer, NULL if we failed
935 core_init (void *cls,
936 struct GNUNET_CORE_Handle * server,
937 const struct GNUNET_PeerIdentity *
940 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
945 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
946 _("Failed to connect to core service, can not manage topology!\n"));
947 GNUNET_SCHEDULER_shutdown (sched);
951 my_identity = *my_id;
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 GNUNET_SCHEDULER_add_delayed (sched,
958 BLACKLIST_AFTER_ATTEMPT,
959 &discard_old_blacklist_entries,
965 * Read the friends file.
968 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
973 struct GNUNET_PeerIdentity pid;
975 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
976 unsigned int entries_found;
980 GNUNET_CONFIGURATION_get_value_filename (cfg,
985 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
986 _("Option `%s' in section `%s' not specified!\n"),
991 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
992 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
993 | GNUNET_DISK_PERM_USER_WRITE);
994 if (0 != STAT (fn, &frstat))
996 if ((friends_only) || (minimum_friend_count > 0))
998 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
999 _("Could not read friends list `%s'\n"), fn);
1004 if (frstat.st_size == 0)
1006 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1007 _("Friends file `%s' is empty.\n"),
1012 data = GNUNET_malloc_large (frstat.st_size);
1013 if (frstat.st_size !=
1014 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1017 _("Failed to read friends list from `%s'\n"), fn);
1024 while ((pos < frstat.st_size) && isspace (data[pos]))
1026 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1027 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1029 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1030 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1032 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1033 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1034 (unsigned long long) pos);
1036 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1040 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1041 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1043 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1044 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1045 (unsigned long long) pos,
1051 fl = make_peer (&pid,
1054 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1055 _("Found friend `%s' in configuration\n"),
1056 GNUNET_i2s (&fl->id));
1058 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1059 while ((pos < frstat.st_size) && isspace (data[pos]))
1064 if ( (minimum_friend_count > entries_found) &&
1065 (friends_only == GNUNET_NO) )
1067 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1068 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1070 if ( (minimum_friend_count > target_connection_count) &&
1071 (friends_only == GNUNET_NO) )
1073 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1074 _("More friendly connections required than target total number of connections.\n"));
1080 * This function is called whenever an encrypted HELLO message is
1083 * @param cls closure
1084 * @param other the other peer involved (sender or receiver, NULL
1085 * for loopback messages where we are both sender and receiver)
1086 * @param message the actual HELLO message
1087 * @param latency reported latency of the connection with 'other'
1088 * @param distance reported distance (DV) to 'other'
1089 * @return GNUNET_OK to keep the connection open,
1090 * GNUNET_SYSERR to close it (signal serious error)
1093 handle_encrypted_hello (void *cls,
1094 const struct GNUNET_PeerIdentity * other,
1095 const struct GNUNET_MessageHeader *
1097 struct GNUNET_TIME_Relative latency,
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "Received encrypted `%s' from peer `%s'",
1104 GNUNET_i2s (other));
1106 if (transport != NULL)
1107 GNUNET_TRANSPORT_offer_hello (transport,
1114 * Function to fill send buffer with HELLO.
1116 * @param cls 'struct PeerList' of the target peer
1117 * @param size number of bytes available in buf
1118 * @param buf where the callee should write the message
1119 * @return number of bytes written to buf
1122 hello_advertising_ready (void *cls,
1126 struct PeerList *pl = cls;
1127 struct PeerList *pos;
1128 struct PeerList *next;
1132 pl->hello_req = NULL;
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1135 "Data solicited for `%s', considering sending `%s'",
1136 GNUNET_i2s (&pl->id),
1139 /* find applicable HELLOs */
1141 while (NULL != (pos = next))
1144 if (pos->hello == NULL)
1146 if (0 == GNUNET_TIME_absolute_get_remaining (pos->filter_expiration).value)
1148 /* time to discard... */
1149 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
1153 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
1154 &pl->id.hashPubKey))
1160 hs = GNUNET_HELLO_size (pos->hello);
1164 memcpy (buf, pos->hello, want);
1165 GNUNET_CONTAINER_bloomfilter_add (pos->filter,
1166 &pl->id.hashPubKey);
1167 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Sending %u bytes of `%s's",
1171 (unsigned int) want,
1176 pl->hello_delay_task
1177 = GNUNET_SCHEDULER_add_now (sched,
1178 &schedule_next_hello,
1185 * Last task run during shutdown. Disconnects us from
1186 * the transport and core.
1189 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1191 struct DisconnectList *dl;
1193 if (NULL != peerinfo_notify)
1195 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1196 peerinfo_notify = NULL;
1198 GNUNET_TRANSPORT_disconnect (transport);
1200 while (NULL != peers)
1204 GNUNET_CORE_disconnect (handle);
1207 while (NULL != (dl = disconnect_head))
1209 GNUNET_CONTAINER_DLL_remove (disconnect_head,
1212 GNUNET_CORE_peer_request_cancel (dl->rh);
1219 * Main function that will be run.
1221 * @param cls closure
1222 * @param s the scheduler to use
1223 * @param args remaining command-line arguments
1224 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1225 * @param c configuration
1229 struct GNUNET_SCHEDULER_Handle * s,
1231 const char *cfgfile,
1232 const struct GNUNET_CONFIGURATION_Handle * c)
1234 struct GNUNET_CORE_MessageHandler handlers[] =
1236 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1239 unsigned long long opt;
1243 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1246 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1250 GNUNET_CONFIGURATION_get_value_number (cfg,
1255 minimum_friend_count = (unsigned int) opt;
1257 GNUNET_CONFIGURATION_get_value_number (cfg,
1259 "TARGET-CONNECTION-COUNT",
1262 target_connection_count = (unsigned int) opt;
1264 if ( (friends_only == GNUNET_YES) ||
1265 (minimum_friend_count > 0) )
1266 read_friends_file (cfg);
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 "Topology would like %u connections with at least %u friends (%s)\n",
1270 target_connection_count,
1271 minimum_friend_count,
1272 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1274 transport = GNUNET_TRANSPORT_connect (sched,
1280 handle = GNUNET_CORE_connect (sched,
1282 GNUNET_TIME_UNIT_FOREVER_REL,
1291 GNUNET_SCHEDULER_add_delayed (sched,
1292 GNUNET_TIME_UNIT_FOREVER_REL,
1293 &cleaning_task, NULL);
1294 if (NULL == transport)
1296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1297 _("Failed to connect to `%s' service.\n"),
1299 GNUNET_SCHEDULER_shutdown (sched);
1304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1305 _("Failed to connect to `%s' service.\n"),
1307 GNUNET_SCHEDULER_shutdown (sched);
1310 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1317 * gnunet-daemon-topology command line options.
1319 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1320 GNUNET_GETOPT_OPTION_END
1325 * The main function for the topology daemon.
1327 * @param argc number of arguments from the command line
1328 * @param argv command line arguments
1329 * @return 0 ok, 1 on error
1332 main (int argc, char *const *argv)
1337 GNUNET_PROGRAM_run (argc,
1340 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1342 &run, NULL)) ? 0 : 1;
1346 /* end of gnunet-daemon-topology.c */