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 peer who we should try to connect to
351 * @param pos entry in our friend list; NULL if not in friend list yet
354 attempt_connect (struct PeerList *pos)
356 if (GNUNET_YES == pos->is_friend)
357 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT_FRIEND);
359 pos->blacklisted_until = GNUNET_TIME_relative_to_absolute (BLACKLIST_AFTER_ATTEMPT);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Asking core to connect to `%s'\n",
363 GNUNET_i2s (&pos->id));
365 pos->connect_req = GNUNET_CORE_notify_transmit_ready (handle,
367 GNUNET_TIME_UNIT_MINUTES,
369 sizeof(struct GNUNET_MessageHeader),
376 * Find a peer in our linked list.
377 * FIXME: should probably use a hash map instead.
380 find_peer (const struct GNUNET_PeerIdentity * peer)
382 struct PeerList *pos;
387 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
396 * Check if an additional connection from the given peer is allowed.
399 is_connection_allowed (struct PeerList *peer)
401 if (0 == memcmp (&my_identity, &peer->id, sizeof (struct GNUNET_PeerIdentity)))
402 return GNUNET_SYSERR; /* disallow connections to self */
405 if (GNUNET_YES == friends_only)
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Determined that `%s' is not allowed to connect (not a friend)\n",
410 GNUNET_i2s (&peer->id));
412 return GNUNET_SYSERR;
414 if (friend_count >= minimum_friend_count)
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
419 GNUNET_i2s (&peer->id));
421 return GNUNET_SYSERR;
426 * Create a new entry in the peer list.
428 * @param peer identity of the new entry
429 * @param hello hello message, can be NULL
430 * @param is_friend is the new entry for a friend?
431 * @return the new entry
433 static struct PeerList *
434 make_peer (const struct
435 GNUNET_PeerIdentity * peer,
436 const struct GNUNET_HELLO_Message *hello,
439 struct PeerList *ret;
441 ret = GNUNET_malloc (sizeof (struct PeerList));
443 ret->is_friend = is_friend;
446 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
447 memcpy (ret->hello, hello,
448 GNUNET_HELLO_size (hello));
457 * Free all resources associated with the given peer.
459 * @param peer peer to free
462 free_peer (struct PeerList *peer)
464 struct PeerList *pos;
465 struct PeerList *prev;
466 struct PeerList *next;
470 while (peer != (pos = next))
475 GNUNET_assert (pos != NULL);
480 if (pos->hello_req != NULL)
481 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
482 if (pos->connect_req != NULL)
483 GNUNET_CORE_notify_transmit_ready_cancel (pos->connect_req);
484 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
485 GNUNET_SCHEDULER_cancel (sched,
486 pos->hello_delay_task);
492 * Setup bloom filter for the given peer entry.
494 * @param peer entry to initialize
497 setup_filter (struct PeerList *peer)
499 /* 2^{-5} chance of not sending a HELLO to a peer is
500 acceptably small (if the filter is 50% full);
501 64 bytes of memory are small compared to the rest
502 of the data structure and would only really become
503 "useless" once a HELLO has been passed on to ~100
504 other peers, which is likely more than enough in
505 any case; hence 64, 5 as bloomfilter parameters. */
506 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
507 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
508 /* never send a peer its own HELLO */
509 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->id.hashPubKey);
514 * Function to fill send buffer with HELLO.
516 * @param cls 'struct PeerList' of the target peer
517 * @param size number of bytes available in buf
518 * @param buf where the callee should write the message
519 * @return number of bytes written to buf
522 hello_advertising_ready (void *cls,
528 * Calculate when we would like to send the next HELLO to this
529 * peer and ask for it.
531 * @param cls for which peer to schedule the HELLO
532 * @param tc task context
535 schedule_next_hello (void *cls,
536 const struct GNUNET_SCHEDULER_TaskContext *tc)
538 struct PeerList *pl = cls;
539 struct PeerList *pos;
540 struct PeerList *next;
542 struct GNUNET_TIME_Relative next_adv;
543 struct GNUNET_TIME_Relative rst_time;
545 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
546 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
547 return; /* we're out of here */
549 next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
550 /* find applicable HELLOs */
552 while (NULL != (pos = next))
555 if (pos->hello == NULL)
557 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
558 if (0 == rst_time.value)
560 /* time to discard... */
561 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
566 if (rst_time.value < next_adv.value)
567 next_want = GNUNET_HELLO_size (pos->hello);
568 next_adv = GNUNET_TIME_relative_min (rst_time,
572 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
577 next_adv = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
578 if (next_adv.value == 0)
581 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
585 &hello_advertising_ready,
590 = GNUNET_SCHEDULER_add_delayed (sched,
592 &schedule_next_hello,
598 * Cancel existing requests for sending HELLOs to this peer
599 * and recalculate when we should send HELLOs to it based
600 * on our current state (something changed!).
603 reschedule_hellos (struct PeerList *peer)
605 if (peer->hello_req != NULL)
607 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
608 peer->hello_req = NULL;
610 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
612 GNUNET_SCHEDULER_cancel (sched,
613 peer->hello_delay_task);
614 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
616 peer->hello_delay_task
617 = GNUNET_SCHEDULER_add_now (sched,
618 &schedule_next_hello,
624 * Method called whenever a peer connects.
627 * @param peer peer identity this notification is about
630 connect_notify (void *cls,
632 GNUNET_PeerIdentity * peer)
634 struct PeerList *pos;
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Core told us that we are connecting to `%s'\n",
642 pos = find_peer (peer);
645 pos = make_peer (peer, NULL, GNUNET_NO);
646 if (GNUNET_OK != is_connection_allowed (pos))
648 GNUNET_assert (pos->is_friend == GNUNET_NO);
649 pos->is_connected = GNUNET_YES;
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
652 "Connection to `%s' is forbidden, forcing disconnect!\n",
655 force_disconnect (&pos->id);
661 GNUNET_assert (GNUNET_NO == pos->is_connected);
662 pos->blacklisted_until.value = 0; /* remove blacklisting */
664 pos->is_connected = GNUNET_YES;
667 reschedule_hellos (pos);
672 * Disconnect from all non-friends (we're below quota).
677 struct PeerList *pos;
682 if ( (GNUNET_NO == pos->is_friend) &&
683 (GNUNET_YES == pos->is_connected) )
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Connection to `%s' is not from a friend, forcing disconnect!\n",
688 GNUNET_i2s (&pos->id));
690 force_disconnect (&pos->id);
698 * Try to add more peers to our connection set.
703 struct PeerList *pos;
708 if ( (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value == 0) &&
709 ( (GNUNET_YES == pos->is_friend) ||
710 (friend_count >= minimum_friend_count) ) &&
711 (GNUNET_YES != pos->is_connected) )
712 attempt_connect (pos);
719 * Method called whenever a peer disconnects.
722 * @param peer peer identity this notification is about
725 disconnect_notify (void *cls,
727 GNUNET_PeerIdentity * peer)
729 struct PeerList *pos;
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Core told us that we disconnected from `%s'\n",
736 pos = find_peer (peer);
742 if (pos->is_connected != GNUNET_YES)
750 if ( (connection_count < target_connection_count) ||
751 (friend_count < minimum_friend_count) )
753 if (friend_count < minimum_friend_count)
755 /* disconnect from all non-friends */
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
758 "Not enough friendly connections, dropping all non-friend connections\n");
766 * Iterator called on each address.
768 * @param cls flag that we will set if we see any addresses
769 * @param tname name of the transport
770 * @param expiration when will the given address expire
771 * @param addr the address of the peer
772 * @param addrlen number of bytes in addr
773 * @return GNUNET_SYSERR always, to terminate iteration
776 address_iterator (void *cls,
778 struct GNUNET_TIME_Absolute expiration,
779 const void *addr, size_t addrlen)
783 return GNUNET_SYSERR;
788 * We've gotten a HELLO from another peer. Consider it for
792 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
795 struct GNUNET_PeerIdentity pid;
796 struct PeerList *peer;
797 struct PeerList *pos;
800 have_address = GNUNET_NO;
801 GNUNET_HELLO_iterate_addresses (hello,
805 if (GNUNET_NO == have_address)
806 return; /* no point in advertising this one... */
807 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
808 peer = find_peer (&pid);
810 peer = make_peer (&pid, hello, GNUNET_NO);
811 // FIXME: check if 'hello' is any different from peer->hello?
812 GNUNET_free_non_null (peer->hello);
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815 "Found `%s' from peer `%s' for advertising\n",
819 size = GNUNET_HELLO_size (hello);
820 peer->hello = GNUNET_malloc (size);
821 memcpy (peer->hello, hello, size);
822 if (peer->filter != NULL)
823 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
825 /* since we have a new HELLO to pick from, re-schedule all
826 HELLO requests that are not bound by the HELLO send rate! */
832 if ( (pos->is_connected) &&
833 (GNUNET_TIME_absolute_get_remaining (pos->next_hello_allowed).value <= HELLO_ADVERTISEMENT_MIN_FREQUENCY.value) )
834 reschedule_hellos (pos);
842 * Peerinfo calls this function to let us know about a possible peer
843 * that we might want to connect to.
846 process_peer (void *cls,
847 const struct GNUNET_PeerIdentity *peer,
848 const struct GNUNET_HELLO_Message *hello,
851 struct PeerList *pos;
853 GNUNET_assert (peer != NULL);
854 if (0 == memcmp (&my_identity,
855 peer, sizeof (struct GNUNET_PeerIdentity)))
856 return; /* that's me! */
859 /* free existing HELLO, if any */
860 pos = find_peer (peer);
861 if (NULL != (pos = find_peer (peer)))
863 GNUNET_free_non_null (pos->hello);
865 if (pos->filter != NULL)
867 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
870 if ( (! pos->is_connected) &&
871 (! pos->is_friend) &&
872 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
877 consider_for_advertising (hello);
878 pos = find_peer (peer);
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Considering connecting to peer `%s'\n",
884 if (GNUNET_YES == pos->is_connected)
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "Already connected to peer `%s'\n",
893 if (GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value > 0)
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
897 "Already tried peer `%s' recently\n",
900 return; /* peer still blacklisted */
902 if ( (GNUNET_YES == pos->is_friend) ||
903 (GNUNET_YES != friends_only) ||
904 (friend_count >= minimum_friend_count) )
905 attempt_connect (pos);
910 * Discard peer entries for blacklisted peers
911 * where the blacklisting has expired.
914 discard_old_blacklist_entries (void *cls,
915 const struct GNUNET_SCHEDULER_TaskContext *tc)
917 struct PeerList *pos;
918 struct PeerList *next;
920 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
923 while (NULL != (pos = next))
926 if ( (GNUNET_NO == pos->is_friend) &&
927 (GNUNET_NO == pos->is_connected) &&
928 (0 == GNUNET_TIME_absolute_get_remaining (pos->blacklisted_until).value) )
931 GNUNET_SCHEDULER_add_delayed (sched,
932 BLACKLIST_AFTER_ATTEMPT,
933 &discard_old_blacklist_entries,
939 * Function called after GNUNET_CORE_connect has succeeded
940 * (or failed for good).
943 * @param server handle to the server, NULL if we failed
944 * @param my_id ID of this peer, NULL if we failed
945 * @param publicKey public key of this peer, NULL if we failed
948 core_init (void *cls,
949 struct GNUNET_CORE_Handle * server,
950 const struct GNUNET_PeerIdentity *
953 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
958 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
959 _("Failed to connect to core service, can not manage topology!\n"));
960 GNUNET_SCHEDULER_shutdown (sched);
964 my_identity = *my_id;
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
970 GNUNET_SCHEDULER_add_delayed (sched,
971 BLACKLIST_AFTER_ATTEMPT,
972 &discard_old_blacklist_entries,
978 * Read the friends file.
981 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
986 struct GNUNET_PeerIdentity pid;
988 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
989 unsigned int entries_found;
993 GNUNET_CONFIGURATION_get_value_filename (cfg,
998 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
999 _("Option `%s' in section `%s' not specified!\n"),
1004 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1005 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1006 | GNUNET_DISK_PERM_USER_WRITE);
1007 if (0 != STAT (fn, &frstat))
1009 if ((friends_only) || (minimum_friend_count > 0))
1011 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1012 _("Could not read friends list `%s'\n"), fn);
1017 if (frstat.st_size == 0)
1019 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1020 _("Friends file `%s' is empty.\n"),
1025 data = GNUNET_malloc_large (frstat.st_size);
1026 if (frstat.st_size !=
1027 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1029 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1030 _("Failed to read friends list from `%s'\n"), fn);
1037 while ((pos < frstat.st_size) && isspace (data[pos]))
1039 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1040 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1042 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1043 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1045 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1046 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1047 (unsigned long long) pos);
1049 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1053 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1054 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1056 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1057 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1058 (unsigned long long) pos,
1064 fl = make_peer (&pid,
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Found friend `%s' in configuration\n",
1070 GNUNET_i2s (&fl->id));
1073 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1074 while ((pos < frstat.st_size) && isspace (data[pos]))
1079 if ( (minimum_friend_count > entries_found) &&
1080 (friends_only == GNUNET_NO) )
1082 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1083 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1085 if ( (minimum_friend_count > target_connection_count) &&
1086 (friends_only == GNUNET_NO) )
1088 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1089 _("More friendly connections required than target total number of connections.\n"));
1095 * This function is called whenever an encrypted HELLO message is
1098 * @param cls closure
1099 * @param other the other peer involved (sender or receiver, NULL
1100 * for loopback messages where we are both sender and receiver)
1101 * @param message the actual HELLO message
1102 * @return GNUNET_OK to keep the connection open,
1103 * GNUNET_SYSERR to close it (signal serious error)
1106 handle_encrypted_hello (void *cls,
1107 const struct GNUNET_PeerIdentity * other,
1108 const struct GNUNET_MessageHeader *
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "Received encrypted `%s' from peer `%s'",
1115 GNUNET_i2s (other));
1117 if (transport != NULL)
1118 GNUNET_TRANSPORT_offer_hello (transport,
1125 * Function to fill send buffer with HELLO.
1127 * @param cls 'struct PeerList' of the target peer
1128 * @param size number of bytes available in buf
1129 * @param buf where the callee should write the message
1130 * @return number of bytes written to buf
1133 hello_advertising_ready (void *cls,
1137 struct PeerList *pl = cls;
1138 struct PeerList *pos;
1139 struct PeerList *next;
1143 pl->hello_req = NULL;
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 "Data solicited for `%s', considering sending `%s'",
1147 GNUNET_i2s (&pl->id),
1150 /* find applicable HELLOs */
1152 while (NULL != (pos = next))
1155 if (pos->hello == NULL)
1157 if (0 == GNUNET_TIME_absolute_get_remaining (pos->filter_expiration).value)
1159 /* time to discard... */
1160 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
1164 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
1165 &pl->id.hashPubKey))
1171 hs = GNUNET_HELLO_size (pos->hello);
1175 memcpy (buf, pos->hello, want);
1176 GNUNET_CONTAINER_bloomfilter_add (pos->filter,
1177 &pl->id.hashPubKey);
1178 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "Sending %u bytes of `%s's",
1182 (unsigned int) want,
1187 pl->hello_delay_task
1188 = GNUNET_SCHEDULER_add_now (sched,
1189 &schedule_next_hello,
1196 * Last task run during shutdown. Disconnects us from
1197 * the transport and core.
1200 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1202 struct DisconnectList *dl;
1204 if (NULL != peerinfo_notify)
1206 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1207 peerinfo_notify = NULL;
1209 GNUNET_TRANSPORT_disconnect (transport);
1211 while (NULL != peers)
1215 GNUNET_CORE_disconnect (handle);
1218 while (NULL != (dl = disconnect_head))
1220 GNUNET_CONTAINER_DLL_remove (disconnect_head,
1223 GNUNET_CORE_peer_get_info_cancel (dl->rh);
1230 * Main function that will be run.
1232 * @param cls closure
1233 * @param s the scheduler to use
1234 * @param args remaining command-line arguments
1235 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1236 * @param c configuration
1240 struct GNUNET_SCHEDULER_Handle * s,
1242 const char *cfgfile,
1243 const struct GNUNET_CONFIGURATION_Handle * c)
1245 struct GNUNET_CORE_MessageHandler handlers[] =
1247 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1250 unsigned long long opt;
1254 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1257 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1261 GNUNET_CONFIGURATION_get_value_number (cfg,
1266 minimum_friend_count = (unsigned int) opt;
1268 GNUNET_CONFIGURATION_get_value_number (cfg,
1270 "TARGET-CONNECTION-COUNT",
1273 target_connection_count = (unsigned int) opt;
1275 if ( (friends_only == GNUNET_YES) ||
1276 (minimum_friend_count > 0) )
1277 read_friends_file (cfg);
1279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1280 "Topology would like %u connections with at least %u friends (%s)\n",
1281 target_connection_count,
1282 minimum_friend_count,
1283 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1285 transport = GNUNET_TRANSPORT_connect (sched,
1291 handle = GNUNET_CORE_connect (sched,
1293 GNUNET_TIME_UNIT_FOREVER_REL,
1301 GNUNET_SCHEDULER_add_delayed (sched,
1302 GNUNET_TIME_UNIT_FOREVER_REL,
1303 &cleaning_task, NULL);
1304 if (NULL == transport)
1306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1307 _("Failed to connect to `%s' service.\n"),
1309 GNUNET_SCHEDULER_shutdown (sched);
1314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1315 _("Failed to connect to `%s' service.\n"),
1317 GNUNET_SCHEDULER_shutdown (sched);
1320 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1327 * gnunet-daemon-topology command line options.
1329 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1330 GNUNET_GETOPT_OPTION_END
1335 * The main function for the topology daemon.
1337 * @param argc number of arguments from the command line
1338 * @param argv command line arguments
1339 * @return 0 ok, 1 on error
1342 main (int argc, char *const *argv)
1347 GNUNET_PROGRAM_run (argc,
1350 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1352 &run, NULL)) ? 0 : 1;
1356 /* end of gnunet-daemon-topology.c */