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;
465 struct PeerList *next;
469 while (peer != (pos = next))
474 GNUNET_assert (pos != NULL);
479 if (pos->hello_req != NULL)
480 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
481 if (pos->connect_req != NULL)
482 GNUNET_CORE_notify_transmit_ready_cancel (pos->connect_req);
483 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
484 GNUNET_SCHEDULER_cancel (sched,
485 pos->hello_delay_task);
491 * Setup bloom filter for the given peer entry.
493 * @param peer entry to initialize
496 setup_filter (struct PeerList *peer)
498 /* 2^{-5} chance of not sending a HELLO to a peer is
499 acceptably small (if the filter is 50% full);
500 64 bytes of memory are small compared to the rest
501 of the data structure and would only really become
502 "useless" once a HELLO has been passed on to ~100
503 other peers, which is likely more than enough in
504 any case; hence 64, 5 as bloomfilter parameters. */
505 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
506 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
507 /* never send a peer its own HELLO */
508 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->id.hashPubKey);
513 * Function to fill send buffer with HELLO.
515 * @param cls 'struct PeerList' of the target peer
516 * @param size number of bytes available in buf
517 * @param buf where the callee should write the message
518 * @return number of bytes written to buf
521 hello_advertising_ready (void *cls,
527 * Calculate when we would like to send the next HELLO to this
528 * peer and ask for it.
530 * @param cls for which peer to schedule the HELLO
531 * @param tc task context
534 schedule_next_hello (void *cls,
535 const struct GNUNET_SCHEDULER_TaskContext *tc)
537 struct PeerList *pl = cls;
538 struct PeerList *pos;
539 struct PeerList *next;
541 struct GNUNET_TIME_Relative next_adv;
542 struct GNUNET_TIME_Relative rst_time;
544 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
545 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
546 return; /* we're out of here */
548 next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
549 /* find applicable HELLOs */
551 while (NULL != (pos = next))
554 if (pos->hello == NULL)
556 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
557 if (0 == rst_time.value)
559 /* time to discard... */
560 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
565 if (rst_time.value < next_adv.value)
566 next_want = GNUNET_HELLO_size (pos->hello);
567 next_adv = GNUNET_TIME_relative_min (rst_time,
571 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
576 next_adv = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
577 if (next_adv.value == 0)
580 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
584 &hello_advertising_ready,
589 = GNUNET_SCHEDULER_add_delayed (sched,
591 &schedule_next_hello,
597 * Cancel existing requests for sending HELLOs to this peer
598 * and recalculate when we should send HELLOs to it based
599 * on our current state (something changed!).
602 reschedule_hellos (struct PeerList *peer)
604 if (peer->hello_req != NULL)
606 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
607 peer->hello_req = NULL;
609 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
611 GNUNET_SCHEDULER_cancel (sched,
612 peer->hello_delay_task);
613 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
615 peer->hello_delay_task
616 = GNUNET_SCHEDULER_add_now (sched,
617 &schedule_next_hello,
623 * Method called whenever a peer connects.
626 * @param peer peer identity this notification is about
629 connect_notify (void *cls,
631 GNUNET_PeerIdentity * peer)
633 struct PeerList *pos;
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
637 "Core told us that we are connecting to `%s'\n",
641 pos = find_peer (peer);
644 pos = make_peer (peer, NULL, GNUNET_NO);
645 if (GNUNET_OK != is_connection_allowed (pos))
647 GNUNET_assert (pos->is_friend == GNUNET_NO);
648 pos->is_connected = GNUNET_YES;
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 "Connection to `%s' is forbidden, forcing disconnect!\n",
654 force_disconnect (&pos->id);
660 GNUNET_assert (GNUNET_NO == pos->is_connected);
661 pos->blacklisted_until.value = 0; /* remove blacklisting */
663 pos->is_connected = GNUNET_YES;
666 reschedule_hellos (pos);
671 * Disconnect from all non-friends (we're below quota).
676 struct PeerList *pos;
681 if ( (GNUNET_NO == pos->is_friend) &&
682 (GNUNET_YES == pos->is_connected) )
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "Connection to `%s' is not from a friend, forcing disconnect!\n",
687 GNUNET_i2s (&pos->id));
689 force_disconnect (&pos->id);
697 * Try to add more peers to our connection set.
702 struct PeerList *pos;
707 if ( (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value == 0) &&
708 ( (GNUNET_YES == pos->is_friend) ||
709 (friend_count >= minimum_friend_count) ) &&
710 (GNUNET_YES != pos->is_connected) )
711 attempt_connect (pos);
718 * Method called whenever a peer disconnects.
721 * @param peer peer identity this notification is about
724 disconnect_notify (void *cls,
726 GNUNET_PeerIdentity * peer)
728 struct PeerList *pos;
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "Core told us that we disconnected from `%s'\n",
735 pos = find_peer (peer);
741 if (pos->is_connected != GNUNET_YES)
749 if ( (connection_count < target_connection_count) ||
750 (friend_count < minimum_friend_count) )
752 if (friend_count < minimum_friend_count)
754 /* disconnect from all non-friends */
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "Not enough friendly connections, dropping all non-friend connections\n");
765 * Iterator called on each address.
767 * @param cls flag that we will set if we see any addresses
768 * @param tname name of the transport
769 * @param expiration when will the given address expire
770 * @param addr the address of the peer
771 * @param addrlen number of bytes in addr
772 * @return GNUNET_SYSERR always, to terminate iteration
775 address_iterator (void *cls,
777 struct GNUNET_TIME_Absolute expiration,
778 const void *addr, size_t addrlen)
782 return GNUNET_SYSERR;
787 * We've gotten a HELLO from another peer. Consider it for
791 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
794 struct GNUNET_PeerIdentity pid;
795 struct PeerList *peer;
796 struct PeerList *pos;
799 have_address = GNUNET_NO;
800 GNUNET_HELLO_iterate_addresses (hello,
804 if (GNUNET_NO == have_address)
805 return; /* no point in advertising this one... */
806 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
807 peer = find_peer (&pid);
809 peer = make_peer (&pid, hello, GNUNET_NO);
810 // FIXME: check if 'hello' is any different from peer->hello?
811 GNUNET_free_non_null (peer->hello);
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "Found `%s' from peer `%s' for advertising\n",
818 size = GNUNET_HELLO_size (hello);
819 peer->hello = GNUNET_malloc (size);
820 memcpy (peer->hello, hello, size);
821 if (peer->filter != NULL)
822 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
824 /* since we have a new HELLO to pick from, re-schedule all
825 HELLO requests that are not bound by the HELLO send rate! */
831 if ( (pos->is_connected) &&
832 (GNUNET_TIME_absolute_get_remaining (pos->next_hello_allowed).value <= HELLO_ADVERTISEMENT_MIN_FREQUENCY.value) )
833 reschedule_hellos (pos);
841 * Peerinfo calls this function to let us know about a possible peer
842 * that we might want to connect to.
845 process_peer (void *cls,
846 const struct GNUNET_PeerIdentity *peer,
847 const struct GNUNET_HELLO_Message *hello,
850 struct PeerList *pos;
852 GNUNET_assert (peer != NULL);
853 if (0 == memcmp (&my_identity,
854 peer, sizeof (struct GNUNET_PeerIdentity)))
855 return; /* that's me! */
858 /* free existing HELLO, if any */
859 pos = find_peer (peer);
860 if (NULL != (pos = find_peer (peer)))
862 GNUNET_free_non_null (pos->hello);
864 if (pos->filter != NULL)
866 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
869 if ( (! pos->is_connected) &&
870 (! pos->is_friend) &&
871 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
876 consider_for_advertising (hello);
877 pos = find_peer (peer);
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "Considering connecting to peer `%s'\n",
883 if (GNUNET_YES == pos->is_connected)
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
887 "Already connected to peer `%s'\n",
892 if (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value > 0)
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Already tried peer `%s' recently\n",
899 return; /* peer still blacklisted */
901 if ( (GNUNET_YES == pos->is_friend) ||
902 (GNUNET_YES != friends_only) ||
903 (friend_count >= minimum_friend_count) )
904 attempt_connect (pos);
909 * Discard peer entries for blacklisted peers
910 * where the blacklisting has expired.
913 discard_old_blacklist_entries (void *cls,
914 const struct GNUNET_SCHEDULER_TaskContext *tc)
916 struct PeerList *pos;
917 struct PeerList *next;
919 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
922 while (NULL != (pos = next))
925 if ( (GNUNET_NO == pos->is_friend) &&
926 (GNUNET_NO == pos->is_connected) &&
927 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
930 GNUNET_SCHEDULER_add_delayed (sched,
931 BLACKLIST_AFTER_ATTEMPT,
932 &discard_old_blacklist_entries,
938 * Function called after GNUNET_CORE_connect has succeeded
939 * (or failed for good).
942 * @param server handle to the server, NULL if we failed
943 * @param my_id ID of this peer, NULL if we failed
944 * @param publicKey public key of this peer, NULL if we failed
947 core_init (void *cls,
948 struct GNUNET_CORE_Handle * server,
949 const struct GNUNET_PeerIdentity *
952 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
957 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
958 _("Failed to connect to core service, can not manage topology!\n"));
959 GNUNET_SCHEDULER_shutdown (sched);
963 my_identity = *my_id;
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 GNUNET_SCHEDULER_add_delayed (sched,
970 BLACKLIST_AFTER_ATTEMPT,
971 &discard_old_blacklist_entries,
977 * Read the friends file.
980 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
985 struct GNUNET_PeerIdentity pid;
987 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
988 unsigned int entries_found;
992 GNUNET_CONFIGURATION_get_value_filename (cfg,
997 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
998 _("Option `%s' in section `%s' not specified!\n"),
1003 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1004 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1005 | GNUNET_DISK_PERM_USER_WRITE);
1006 if (0 != STAT (fn, &frstat))
1008 if ((friends_only) || (minimum_friend_count > 0))
1010 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1011 _("Could not read friends list `%s'\n"), fn);
1016 if (frstat.st_size == 0)
1018 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1019 _("Friends file `%s' is empty.\n"),
1024 data = GNUNET_malloc_large (frstat.st_size);
1025 if (frstat.st_size !=
1026 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1028 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1029 _("Failed to read friends list from `%s'\n"), fn);
1036 while ((pos < frstat.st_size) && isspace (data[pos]))
1038 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1039 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1041 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1042 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1044 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1045 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1046 (unsigned long long) pos);
1048 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1052 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1053 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1055 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1056 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1057 (unsigned long long) pos,
1063 fl = make_peer (&pid,
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1068 "Found friend `%s' in configuration\n",
1069 GNUNET_i2s (&fl->id));
1072 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1073 while ((pos < frstat.st_size) && isspace (data[pos]))
1078 if ( (minimum_friend_count > entries_found) &&
1079 (friends_only == GNUNET_NO) )
1081 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1082 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1084 if ( (minimum_friend_count > target_connection_count) &&
1085 (friends_only == GNUNET_NO) )
1087 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1088 _("More friendly connections required than target total number of connections.\n"));
1094 * This function is called whenever an encrypted HELLO message is
1097 * @param cls closure
1098 * @param other the other peer involved (sender or receiver, NULL
1099 * for loopback messages where we are both sender and receiver)
1100 * @param message the actual HELLO message
1101 * @return GNUNET_OK to keep the connection open,
1102 * GNUNET_SYSERR to close it (signal serious error)
1105 handle_encrypted_hello (void *cls,
1106 const struct GNUNET_PeerIdentity * other,
1107 const struct GNUNET_MessageHeader *
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "Received encrypted `%s' from peer `%s'",
1114 GNUNET_i2s (other));
1116 if (transport != NULL)
1117 GNUNET_TRANSPORT_offer_hello (transport,
1124 * Function to fill send buffer with HELLO.
1126 * @param cls 'struct PeerList' of the target peer
1127 * @param size number of bytes available in buf
1128 * @param buf where the callee should write the message
1129 * @return number of bytes written to buf
1132 hello_advertising_ready (void *cls,
1136 struct PeerList *pl = cls;
1137 struct PeerList *pos;
1138 struct PeerList *next;
1142 pl->hello_req = NULL;
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "Data solicited for `%s', considering sending `%s'",
1146 GNUNET_i2s (&pl->id),
1149 /* find applicable HELLOs */
1151 while (NULL != (pos = next))
1154 if (pos->hello == NULL)
1156 if (0 == GNUNET_TIME_absolute_get_remaining (pos->filter_expiration).value)
1158 /* time to discard... */
1159 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
1163 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
1164 &pl->id.hashPubKey))
1170 hs = GNUNET_HELLO_size (pos->hello);
1174 memcpy (buf, pos->hello, want);
1175 GNUNET_CONTAINER_bloomfilter_add (pos->filter,
1176 &pl->id.hashPubKey);
1177 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "Sending %u bytes of `%s's",
1181 (unsigned int) want,
1186 pl->hello_delay_task
1187 = GNUNET_SCHEDULER_add_now (sched,
1188 &schedule_next_hello,
1195 * Last task run during shutdown. Disconnects us from
1196 * the transport and core.
1199 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1201 struct DisconnectList *dl;
1203 if (NULL != peerinfo_notify)
1205 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1206 peerinfo_notify = NULL;
1208 GNUNET_TRANSPORT_disconnect (transport);
1210 while (NULL != peers)
1214 GNUNET_CORE_disconnect (handle);
1217 while (NULL != (dl = disconnect_head))
1219 GNUNET_CONTAINER_DLL_remove (disconnect_head,
1222 GNUNET_CORE_peer_get_info_cancel (dl->rh);
1229 * Main function that will be run.
1231 * @param cls closure
1232 * @param s the scheduler to use
1233 * @param args remaining command-line arguments
1234 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1235 * @param c configuration
1239 struct GNUNET_SCHEDULER_Handle * s,
1241 const char *cfgfile,
1242 const struct GNUNET_CONFIGURATION_Handle * c)
1244 struct GNUNET_CORE_MessageHandler handlers[] =
1246 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1249 unsigned long long opt;
1253 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1256 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1260 GNUNET_CONFIGURATION_get_value_number (cfg,
1265 minimum_friend_count = (unsigned int) opt;
1267 GNUNET_CONFIGURATION_get_value_number (cfg,
1269 "TARGET-CONNECTION-COUNT",
1272 target_connection_count = (unsigned int) opt;
1274 if ( (friends_only == GNUNET_YES) ||
1275 (minimum_friend_count > 0) )
1276 read_friends_file (cfg);
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "Topology would like %u connections with at least %u friends (%s)\n",
1280 target_connection_count,
1281 minimum_friend_count,
1282 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1284 transport = GNUNET_TRANSPORT_connect (sched,
1290 handle = GNUNET_CORE_connect (sched,
1292 GNUNET_TIME_UNIT_FOREVER_REL,
1300 GNUNET_SCHEDULER_add_delayed (sched,
1301 GNUNET_TIME_UNIT_FOREVER_REL,
1302 &cleaning_task, NULL);
1303 if (NULL == transport)
1305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1306 _("Failed to connect to `%s' service.\n"),
1308 GNUNET_SCHEDULER_shutdown (sched);
1313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1314 _("Failed to connect to `%s' service.\n"),
1316 GNUNET_SCHEDULER_shutdown (sched);
1319 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1326 * gnunet-daemon-topology command line options.
1328 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1329 GNUNET_GETOPT_OPTION_END
1334 * The main function for the topology daemon.
1336 * @param argc number of arguments from the command line
1337 * @param argv command line arguments
1338 * @return 0 ok, 1 on error
1341 main (int argc, char *const *argv)
1346 GNUNET_PROGRAM_run (argc,
1349 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1351 &run, NULL)) ? 0 : 1;
1355 /* end of gnunet-daemon-topology.c */