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_InformationRequestContext *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'
261 * @param peer NULL on error (then what?)
262 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
263 * @param bpm_out set to the current bandwidth limit (sending) for this peer
264 * @param latency current latency estimate, "FOREVER" if we have been
266 * @param amount set to the amount that was actually reserved or unreserved
267 * @param preference current traffic preference for the given peer
270 disconnect_done (void *cls,
272 GNUNET_PeerIdentity * peer,
274 unsigned int bpm_out,
275 struct GNUNET_TIME_Relative
277 unsigned long long preference)
279 struct DisconnectList *dl = cls;
281 GNUNET_CONTAINER_DLL_remove (disconnect_head,
289 * Force a disconnect from the specified peer. This is currently done by
290 * changing the bandwidth policy to 0 bytes per second.
291 * FIXME: maybe we want a nicer CORE API for both connect and disconnect...
292 * FIXME: this policy change is never undone; how do we reconnect ever?
295 force_disconnect (const struct GNUNET_PeerIdentity *peer)
297 struct DisconnectList *dl;
299 dl = GNUNET_malloc (sizeof (struct DisconnectList));
301 GNUNET_CONTAINER_DLL_insert (disconnect_head,
304 dl->rh = GNUNET_CORE_peer_get_info (sched, cfg,
306 GNUNET_TIME_UNIT_FOREVER_REL,
316 * Function called by core when our attempt to connect succeeded.
317 * Transmits a 'DUMMY' message to trigger the session key exchange.
318 * FIXME: this is an issue with the current CORE API.
321 ready_callback (void *cls,
322 size_t size, void *buf)
324 struct PeerList *pos = cls;
325 struct GNUNET_MessageHeader hdr;
327 pos->connect_req = NULL;
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "Core told us that our attempt to connect failed.\n");
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Sending dummy message to establish connection.\n");
340 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
341 hdr.type = htons (GNUNET_MESSAGE_TYPE_TOPOLOGY_DUMMY);
342 memcpy (buf, &hdr, sizeof (struct GNUNET_MessageHeader));
343 return sizeof (struct GNUNET_MessageHeader);
348 * Try to connect to the specified peer.
350 * @param pos peer to connect to
353 attempt_connect (struct PeerList *pos)
355 if (GNUNET_YES == pos->is_friend)
356 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT_FRIEND);
358 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT);
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Asking core to connect to `%s'\n",
362 GNUNET_i2s (&pos->id));
364 pos->connect_req = GNUNET_CORE_notify_transmit_ready (handle,
366 GNUNET_TIME_UNIT_MINUTES,
368 sizeof(struct GNUNET_MessageHeader),
375 * Find a peer in our linked list.
376 * FIXME: should probably use a hash map instead.
379 find_peer (const struct GNUNET_PeerIdentity * peer)
381 struct PeerList *pos;
386 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
395 * Check if an additional connection from the given peer is allowed.
398 is_connection_allowed (struct PeerList *peer)
400 if (0 == memcmp (&my_identity, &peer->id, sizeof (struct GNUNET_PeerIdentity)))
401 return GNUNET_SYSERR; /* disallow connections to self */
404 if (GNUNET_YES == friends_only)
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Determined that `%s' is not allowed to connect (not a friend)\n",
409 GNUNET_i2s (&peer->id));
411 return GNUNET_SYSERR;
413 if (friend_count >= minimum_friend_count)
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
418 GNUNET_i2s (&peer->id));
420 return GNUNET_SYSERR;
425 * Create a new entry in the peer list.
427 * @param peer identity of the new entry
428 * @param hello hello message, can be NULL
429 * @param is_friend is the new entry for a friend?
430 * @return the new entry
432 static struct PeerList *
433 make_peer (const struct
434 GNUNET_PeerIdentity * peer,
435 const struct GNUNET_HELLO_Message *hello,
438 struct PeerList *ret;
440 ret = GNUNET_malloc (sizeof (struct PeerList));
442 ret->is_friend = is_friend;
445 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
446 memcpy (ret->hello, hello,
447 GNUNET_HELLO_size (hello));
456 * Free all resources associated with the given peer.
458 * @param peer peer to free
461 free_peer (struct PeerList *peer)
463 struct PeerList *pos;
464 struct PeerList *prev;
473 GNUNET_assert (pos != NULL);
477 prev->next = pos->next;
478 if (pos->hello_req != NULL)
479 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
480 if (pos->connect_req != NULL)
481 GNUNET_CORE_notify_transmit_ready_cancel (pos->connect_req);
482 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
483 GNUNET_SCHEDULER_cancel (sched,
484 pos->hello_delay_task);
490 * Setup bloom filter for the given peer entry.
492 * @param peer entry to initialize
495 setup_filter (struct PeerList *peer)
497 /* 2^{-5} chance of not sending a HELLO to a peer is
498 acceptably small (if the filter is 50% full);
499 64 bytes of memory are small compared to the rest
500 of the data structure and would only really become
501 "useless" once a HELLO has been passed on to ~100
502 other peers, which is likely more than enough in
503 any case; hence 64, 5 as bloomfilter parameters. */
504 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
505 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
506 /* never send a peer its own HELLO */
507 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->id.hashPubKey);
512 * Function to fill send buffer with HELLO.
514 * @param cls 'struct PeerList' of the target peer
515 * @param size number of bytes available in buf
516 * @param buf where the callee should write the message
517 * @return number of bytes written to buf
520 hello_advertising_ready (void *cls,
526 * Calculate when we would like to send the next HELLO to this
527 * peer and ask for it.
529 * @param cls for which peer to schedule the HELLO
530 * @param tc task context
533 schedule_next_hello (void *cls,
534 const struct GNUNET_SCHEDULER_TaskContext *tc)
536 struct PeerList *pl = cls;
537 struct PeerList *pos;
538 struct PeerList *next;
540 struct GNUNET_TIME_Relative next_adv;
541 struct GNUNET_TIME_Relative rst_time;
543 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
544 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
545 return; /* we're out of here */
547 next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
548 /* find applicable HELLOs */
550 while (NULL != (pos = next))
553 if (pos->hello == NULL)
555 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
556 if (0 == rst_time.value)
558 /* time to discard... */
559 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
564 if (rst_time.value < next_adv.value)
565 next_want = GNUNET_HELLO_size (pos->hello);
566 next_adv = GNUNET_TIME_relative_min (rst_time,
570 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
575 next_adv = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
576 if (next_adv.value == 0)
579 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
583 &hello_advertising_ready,
588 = GNUNET_SCHEDULER_add_delayed (sched,
590 &schedule_next_hello,
596 * Cancel existing requests for sending HELLOs to this peer
597 * and recalculate when we should send HELLOs to it based
598 * on our current state (something changed!).
601 reschedule_hellos (struct PeerList *peer)
603 if (peer->hello_req != NULL)
605 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
606 peer->hello_req = NULL;
608 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
610 GNUNET_SCHEDULER_cancel (sched,
611 peer->hello_delay_task);
612 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
614 peer->hello_delay_task
615 = GNUNET_SCHEDULER_add_now (sched,
616 &schedule_next_hello,
622 * Method called whenever a peer connects.
625 * @param peer peer identity this notification is about
628 connect_notify (void *cls,
630 GNUNET_PeerIdentity * peer)
632 struct PeerList *pos;
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Core told us that we are connecting to `%s'\n",
640 pos = find_peer (peer);
643 pos = make_peer (peer, NULL, GNUNET_NO);
644 if (GNUNET_OK != is_connection_allowed (pos))
646 GNUNET_assert (pos->is_friend == GNUNET_NO);
647 pos->is_connected = GNUNET_YES;
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "Connection to `%s' is forbidden, forcing disconnect!\n",
653 force_disconnect (&pos->id);
659 GNUNET_assert (GNUNET_NO == pos->is_connected);
660 pos->blacklisted_until.value = 0; /* remove blacklisting */
662 pos->is_connected = GNUNET_YES;
665 reschedule_hellos (pos);
670 * Disconnect from all non-friends (we're below quota).
675 struct PeerList *pos;
680 if ( (GNUNET_NO == pos->is_friend) &&
681 (GNUNET_YES == pos->is_connected) )
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Connection to `%s' is not from a friend, forcing disconnect!\n",
686 GNUNET_i2s (&pos->id));
688 force_disconnect (&pos->id);
696 * Try to add more peers to our connection set.
701 struct PeerList *pos;
706 if ( (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value == 0) &&
707 ( (GNUNET_YES == pos->is_friend) ||
708 (friend_count >= minimum_friend_count) ) &&
709 (GNUNET_YES != pos->is_connected) )
710 attempt_connect (pos);
717 * Method called whenever a peer disconnects.
720 * @param peer peer identity this notification is about
723 disconnect_notify (void *cls,
725 GNUNET_PeerIdentity * peer)
727 struct PeerList *pos;
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Core told us that we disconnected from `%s'\n",
734 pos = find_peer (peer);
740 if (pos->is_connected != GNUNET_YES)
748 if ( (connection_count < target_connection_count) ||
749 (friend_count < minimum_friend_count) )
751 if (friend_count < minimum_friend_count)
753 /* disconnect from all non-friends */
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "Not enough friendly connections, dropping all non-friend connections\n");
764 * Iterator called on each address.
766 * @param cls flag that we will set if we see any addresses
767 * @param tname name of the transport
768 * @param expiration when will the given address expire
769 * @param addr the address of the peer
770 * @param addrlen number of bytes in addr
771 * @return GNUNET_SYSERR always, to terminate iteration
774 address_iterator (void *cls,
776 struct GNUNET_TIME_Absolute expiration,
777 const void *addr, size_t addrlen)
781 return GNUNET_SYSERR;
786 * We've gotten a HELLO from another peer. Consider it for
790 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
793 struct GNUNET_PeerIdentity pid;
794 struct PeerList *peer;
795 struct PeerList *pos;
798 have_address = GNUNET_NO;
799 GNUNET_HELLO_iterate_addresses (hello,
803 if (GNUNET_NO == have_address)
804 return; /* no point in advertising this one... */
805 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
806 peer = find_peer (&pid);
808 peer = make_peer (&pid, hello, GNUNET_NO);
809 // FIXME: check if 'hello' is any different from peer->hello?
810 GNUNET_free_non_null (peer->hello);
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
813 "Found `%s' from peer `%s' for advertising\n",
817 size = GNUNET_HELLO_size (hello);
818 peer->hello = GNUNET_malloc (size);
819 memcpy (peer->hello, hello, size);
820 if (peer->filter != NULL)
821 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
823 /* since we have a new HELLO to pick from, re-schedule all
824 HELLO requests that are not bound by the HELLO send rate! */
830 if ( (pos->is_connected) &&
831 (GNUNET_TIME_absolute_get_remaining (pos->next_hello_allowed).value <= HELLO_ADVERTISEMENT_MIN_FREQUENCY.value) )
832 reschedule_hellos (pos);
840 * Peerinfo calls this function to let us know about a possible peer
841 * that we might want to connect to.
844 process_peer (void *cls,
845 const struct GNUNET_PeerIdentity *peer,
846 const struct GNUNET_HELLO_Message *hello,
849 struct PeerList *pos;
851 GNUNET_assert (peer != NULL);
852 if (0 == memcmp (&my_identity,
853 peer, sizeof (struct GNUNET_PeerIdentity)))
854 return; /* that's me! */
857 /* free existing HELLO, if any */
858 if (NULL != (pos = find_peer (peer)))
860 GNUNET_free_non_null (pos->hello);
862 if (pos->filter != NULL)
864 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
867 if ( (! pos->is_connected) &&
868 (! pos->is_friend) &&
869 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
874 consider_for_advertising (hello);
875 pos = find_peer (peer);
876 GNUNET_assert (NULL != pos);
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Considering connecting to peer `%s'\n",
882 if (GNUNET_YES == pos->is_connected)
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Already connected to peer `%s'\n",
891 if (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value > 0)
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Already tried peer `%s' recently\n",
898 return; /* peer still blacklisted */
900 if ( (GNUNET_YES == pos->is_friend) ||
901 (GNUNET_YES != friends_only) ||
902 (friend_count >= minimum_friend_count) )
903 attempt_connect (pos);
908 * Discard peer entries for blacklisted peers
909 * where the blacklisting has expired.
912 discard_old_blacklist_entries (void *cls,
913 const struct GNUNET_SCHEDULER_TaskContext *tc)
915 struct PeerList *pos;
916 struct PeerList *next;
918 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
921 while (NULL != (pos = next))
924 if ( (GNUNET_NO == pos->is_friend) &&
925 (GNUNET_NO == pos->is_connected) &&
926 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
929 GNUNET_SCHEDULER_add_delayed (sched,
930 BLACKLIST_AFTER_ATTEMPT,
931 &discard_old_blacklist_entries,
937 * Function called after GNUNET_CORE_connect has succeeded
938 * (or failed for good).
941 * @param server handle to the server, NULL if we failed
942 * @param my_id ID of this peer, NULL if we failed
943 * @param publicKey public key of this peer, NULL if we failed
946 core_init (void *cls,
947 struct GNUNET_CORE_Handle * server,
948 const struct GNUNET_PeerIdentity *
951 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
956 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
957 _("Failed to connect to core service, can not manage topology!\n"));
958 GNUNET_SCHEDULER_shutdown (sched);
962 my_identity = *my_id;
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 GNUNET_SCHEDULER_add_delayed (sched,
969 BLACKLIST_AFTER_ATTEMPT,
970 &discard_old_blacklist_entries,
976 * Read the friends file.
979 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
984 struct GNUNET_PeerIdentity pid;
986 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
987 unsigned int entries_found;
991 GNUNET_CONFIGURATION_get_value_filename (cfg,
996 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
997 _("Option `%s' in section `%s' not specified!\n"),
1002 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1003 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1004 | GNUNET_DISK_PERM_USER_WRITE);
1005 if (0 != STAT (fn, &frstat))
1007 if ((friends_only) || (minimum_friend_count > 0))
1009 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1010 _("Could not read friends list `%s'\n"), fn);
1015 if (frstat.st_size == 0)
1017 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1018 _("Friends file `%s' is empty.\n"),
1023 data = GNUNET_malloc_large (frstat.st_size);
1024 if (frstat.st_size !=
1025 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1027 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1028 _("Failed to read friends list from `%s'\n"), fn);
1035 while ((pos < frstat.st_size) && isspace (data[pos]))
1037 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1038 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1040 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1041 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1043 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1044 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1045 (unsigned long long) pos);
1047 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1051 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1052 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1054 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1055 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1056 (unsigned long long) pos,
1062 fl = make_peer (&pid,
1065 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1066 _("Found friend `%s' in configuration\n"),
1067 GNUNET_i2s (&fl->id));
1069 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1070 while ((pos < frstat.st_size) && isspace (data[pos]))
1075 if ( (minimum_friend_count > entries_found) &&
1076 (friends_only == GNUNET_NO) )
1078 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1079 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1081 if ( (minimum_friend_count > target_connection_count) &&
1082 (friends_only == GNUNET_NO) )
1084 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1085 _("More friendly connections required than target total number of connections.\n"));
1091 * This function is called whenever an encrypted HELLO message is
1094 * @param cls closure
1095 * @param other the other peer involved (sender or receiver, NULL
1096 * for loopback messages where we are both sender and receiver)
1097 * @param message the actual HELLO message
1098 * @return GNUNET_OK to keep the connection open,
1099 * GNUNET_SYSERR to close it (signal serious error)
1102 handle_encrypted_hello (void *cls,
1103 const struct GNUNET_PeerIdentity * other,
1104 const struct GNUNET_MessageHeader *
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1109 "Received encrypted `%s' from peer `%s'",
1111 GNUNET_i2s (other));
1113 if (transport != NULL)
1114 GNUNET_TRANSPORT_offer_hello (transport,
1121 * Function to fill send buffer with HELLO.
1123 * @param cls 'struct PeerList' of the target peer
1124 * @param size number of bytes available in buf
1125 * @param buf where the callee should write the message
1126 * @return number of bytes written to buf
1129 hello_advertising_ready (void *cls,
1133 struct PeerList *pl = cls;
1134 struct PeerList *pos;
1135 struct PeerList *next;
1139 pl->hello_req = NULL;
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "Data solicited for `%s', considering sending `%s'",
1143 GNUNET_i2s (&pl->id),
1146 /* find applicable HELLOs */
1148 while (NULL != (pos = next))
1151 if (pos->hello == NULL)
1153 if (0 == GNUNET_TIME_absolute_get_remaining (pos->filter_expiration).value)
1155 /* time to discard... */
1156 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
1160 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
1161 &pl->id.hashPubKey))
1167 hs = GNUNET_HELLO_size (pos->hello);
1171 memcpy (buf, pos->hello, want);
1172 GNUNET_CONTAINER_bloomfilter_add (pos->filter,
1173 &pl->id.hashPubKey);
1174 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "Sending %u bytes of `%s's",
1178 (unsigned int) want,
1183 pl->hello_delay_task
1184 = GNUNET_SCHEDULER_add_now (sched,
1185 &schedule_next_hello,
1192 * Last task run during shutdown. Disconnects us from
1193 * the transport and core.
1196 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1198 struct DisconnectList *dl;
1200 if (NULL != peerinfo_notify)
1202 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1203 peerinfo_notify = NULL;
1205 GNUNET_TRANSPORT_disconnect (transport);
1207 while (NULL != peers)
1211 GNUNET_CORE_disconnect (handle);
1214 while (NULL != (dl = disconnect_head))
1216 GNUNET_CONTAINER_DLL_remove (disconnect_head,
1219 GNUNET_CORE_peer_get_info_cancel (dl->rh);
1226 * Main function that will be run.
1228 * @param cls closure
1229 * @param s the scheduler to use
1230 * @param args remaining command-line arguments
1231 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1232 * @param c configuration
1236 struct GNUNET_SCHEDULER_Handle * s,
1238 const char *cfgfile,
1239 const struct GNUNET_CONFIGURATION_Handle * c)
1241 struct GNUNET_CORE_MessageHandler handlers[] =
1243 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1246 unsigned long long opt;
1250 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1253 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1257 GNUNET_CONFIGURATION_get_value_number (cfg,
1262 minimum_friend_count = (unsigned int) opt;
1264 GNUNET_CONFIGURATION_get_value_number (cfg,
1266 "TARGET-CONNECTION-COUNT",
1269 target_connection_count = (unsigned int) opt;
1271 if ( (friends_only == GNUNET_YES) ||
1272 (minimum_friend_count > 0) )
1273 read_friends_file (cfg);
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "Topology would like %u connections with at least %u friends (%s)\n",
1277 target_connection_count,
1278 minimum_friend_count,
1279 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1281 transport = GNUNET_TRANSPORT_connect (sched,
1287 handle = GNUNET_CORE_connect (sched,
1289 GNUNET_TIME_UNIT_FOREVER_REL,
1298 GNUNET_SCHEDULER_add_delayed (sched,
1299 GNUNET_TIME_UNIT_FOREVER_REL,
1300 &cleaning_task, NULL);
1301 if (NULL == transport)
1303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1304 _("Failed to connect to `%s' service.\n"),
1306 GNUNET_SCHEDULER_shutdown (sched);
1311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1312 _("Failed to connect to `%s' service.\n"),
1314 GNUNET_SCHEDULER_shutdown (sched);
1317 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1324 * gnunet-daemon-topology command line options.
1326 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1327 GNUNET_GETOPT_OPTION_END
1332 * The main function for the topology daemon.
1334 * @param argc number of arguments from the command line
1335 * @param argv command line arguments
1336 * @return 0 ok, 1 on error
1339 main (int argc, char *const *argv)
1344 GNUNET_PROGRAM_run (argc,
1347 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1349 &run, NULL)) ? 0 : 1;
1353 /* end of gnunet-daemon-topology.c */