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
29 #include "gnunet_constants.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_util_lib.h"
38 #define DEBUG_TOPOLOGY GNUNET_NO
41 * For how long do we blacklist a peer after a failed connection
44 #define GREYLIST_AFTER_ATTEMPT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 * For how long do we blacklist a friend after a failed connection
50 #define GREYLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
53 * For how long do we blacklist anyone under any cirumstances after a failed connection
56 #define GREYLIST_AFTER_ATTEMPT_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
59 * For how long do we blacklist anyone under any cirumstances after a failed connection
62 #define GREYLIST_AFTER_ATTEMPT_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 18)
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 * Record for neighbours, friends and blacklisted peers.
81 * Which peer is this entry about?
83 struct GNUNET_PeerIdentity pid;
86 * Our handle for the request to transmit HELLOs to this peer; NULL
87 * if no such request is pending.
89 struct GNUNET_CORE_TransmitHandle *hello_req;
92 * Our handle for the request to connect to this peer; NULL if no
93 * such request is pending.
95 struct GNUNET_CORE_PeerRequestHandle *connect_req;
98 * Pointer to the HELLO message of this peer; can be NULL.
100 struct GNUNET_HELLO_Message *hello;
103 * Bloom filter used to mark which peers already got the HELLO
106 struct GNUNET_CONTAINER_BloomFilter *filter;
109 * Until what time should we not try to connect again
112 struct GNUNET_TIME_Absolute greylisted_until;
115 * Next time we are allowed to transmit a HELLO to this peer?
117 struct GNUNET_TIME_Absolute next_hello_allowed;
120 * When should we reset the bloom filter of this entry?
122 struct GNUNET_TIME_Absolute filter_expiration;
125 * ID of task we use to wait for the time to send the next HELLO
128 GNUNET_SCHEDULER_TaskIdentifier hello_delay_task;
131 * ID of task we use to clear peers from the greylist.
133 GNUNET_SCHEDULER_TaskIdentifier greylist_clean_task;
136 * How often have we tried so far?
138 unsigned int connect_attempts;
141 * Is this peer listed here because he is a friend?
146 * Are we connected to this peer right now?
154 * Our peerinfo notification context. We use notification
155 * to instantly learn about new peers as they are discovered.
157 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
162 static struct GNUNET_SCHEDULER_Handle *sched;
167 static const struct GNUNET_CONFIGURATION_Handle *cfg;
170 * Handle to the core API.
172 static struct GNUNET_CORE_Handle *handle;
175 * Handle to the transport API.
177 static struct GNUNET_TRANSPORT_Handle *transport;
180 * Identity of this peer.
182 static struct GNUNET_PeerIdentity my_identity;
185 * All of our friends, all of our current neighbours and all peers for
186 * which we have HELLOs. So pretty much everyone. Maps peer identities
187 * to 'struct Peer *' values.
189 static struct GNUNET_CONTAINER_MultiHashMap *peers;
192 * Handle for reporting statistics.
194 static struct GNUNET_STATISTICS_Handle *stats;
197 * Blacklist (NULL if we have none).
199 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
202 * Flag to disallow non-friend connections (pure F2F mode).
204 static int friends_only;
207 * Minimum number of friends to have in the
208 * connection set before we allow non-friends.
210 static unsigned int minimum_friend_count;
213 * Number of peers (friends and others) that we are currently connected to.
215 static unsigned int connection_count;
218 * Target number of connections.
220 static unsigned int target_connection_count;
223 * Number of friends that we are currently connected to.
225 static unsigned int friend_count;
228 * Should the topology daemon try to establish connections?
230 static int autoconnect;
234 * Function that decides if a connection is acceptable or not.
235 * If we have a blacklist, only friends are allowed, so the check
239 * @param pid peer to approve or disapproave
240 * @return GNUNET_OK if the connection is allowed
243 blacklist_check (void *cls,
244 const struct GNUNET_PeerIdentity *pid)
248 pos = GNUNET_CONTAINER_multihashmap_get (peers, &pid->hashPubKey);
249 if ( (pos != NULL) &&
250 (pos->is_friend == GNUNET_YES) )
252 GNUNET_STATISTICS_update (stats,
253 gettext_noop ("# peers blacklisted"),
256 return GNUNET_SYSERR;
261 * Whitelist all peers that we blacklisted; we've passed
262 * the minimum number of friends.
267 if (blacklist != NULL)
269 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
276 * Function called by core when our attempt to connect succeeded.
278 * @param cls the 'struct Peer' for which we issued the connect request
279 * @param tc scheduler context
282 connect_completed_callback (void *cls,
283 const struct GNUNET_SCHEDULER_TaskContext *tc)
285 struct Peer *pos = cls;
287 pos->connect_req = NULL;
292 * Check if an additional connection from the given peer is allowed.
294 * @param peer connection to check
295 * @return GNUNET_OK if the connection is allowed
298 is_connection_allowed (struct Peer *peer)
300 if (0 == memcmp (&my_identity,
302 sizeof (struct GNUNET_PeerIdentity)))
303 return GNUNET_SYSERR; /* disallow connections to self */
306 if (GNUNET_YES == friends_only)
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Determined that `%s' is not allowed to connect (not a friend)\n",
311 GNUNET_i2s (&peer->pid));
313 return GNUNET_SYSERR;
315 if (friend_count >= minimum_friend_count)
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
320 GNUNET_i2s (&peer->pid));
322 return GNUNET_SYSERR;
327 * Free all resources associated with the given peer.
329 * @param cls closure (not used)
330 * @param pid identity of the peer
331 * @param value peer to free
332 * @return GNUNET_YES (always: continue to iterate)
335 free_peer (void *cls,
336 const GNUNET_HashCode *pid,
339 struct Peer *pos = value;
341 GNUNET_break (GNUNET_OK ==
342 GNUNET_CONTAINER_multihashmap_remove (peers,
345 if (pos->hello_req != NULL)
346 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
347 if (pos->connect_req != NULL)
348 GNUNET_CORE_peer_request_connect_cancel (pos->connect_req);
349 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
350 GNUNET_SCHEDULER_cancel (sched,
351 pos->hello_delay_task);
352 GNUNET_free_non_null (pos->hello);
353 if (pos->filter != NULL)
354 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
361 * Discard peer entries for greylisted peers
362 * where the greylisting has expired.
364 * @param cls 'struct Peer' to greylist
365 * @param tc scheduler context
368 remove_from_greylist (void *cls,
369 const struct GNUNET_SCHEDULER_TaskContext *tc);
373 * Try to connect to the specified peer.
375 * @param pos peer to connect to
378 attempt_connect (struct Peer *pos)
380 struct GNUNET_TIME_Relative rem;
382 if ( (connection_count >= target_connection_count) &&
383 (friend_count >= minimum_friend_count) )
385 if (GNUNET_YES == pos->is_connected)
387 if (GNUNET_OK != is_connection_allowed (pos))
389 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value > 0)
391 if (GNUNET_YES == pos->is_friend)
392 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
394 rem = GREYLIST_AFTER_ATTEMPT;
395 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
396 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
397 if (pos->connect_attempts > 30)
398 pos->connect_attempts = 30;
399 rem = GNUNET_TIME_relative_multiply (rem, 1 << (++pos->connect_attempts));
400 rem = GNUNET_TIME_relative_max (rem,
401 GREYLIST_AFTER_ATTEMPT_MIN);
402 rem = GNUNET_TIME_relative_min (rem,
403 GREYLIST_AFTER_ATTEMPT_MAX);
404 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
405 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
406 GNUNET_SCHEDULER_cancel (sched,
407 pos->greylist_clean_task);
408 pos->greylist_clean_task
409 = GNUNET_SCHEDULER_add_delayed (sched,
411 &remove_from_greylist,
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Asking to connect to `%s'\n",
416 GNUNET_i2s (&pos->pid));
418 GNUNET_STATISTICS_update (stats,
419 gettext_noop ("# connect requests issued to core"),
422 pos->connect_req = GNUNET_CORE_peer_request_connect (sched, cfg,
423 GNUNET_TIME_UNIT_MINUTES,
425 &connect_completed_callback,
431 * Discard peer entries for greylisted peers
432 * where the greylisting has expired.
434 * @param cls 'struct Peer' to greylist
435 * @param tc scheduler context
438 remove_from_greylist (void *cls,
439 const struct GNUNET_SCHEDULER_TaskContext *tc)
441 struct Peer *pos = cls;
442 struct GNUNET_TIME_Relative rem;
444 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
445 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
448 attempt_connect (pos);
452 pos->greylist_clean_task
453 = GNUNET_SCHEDULER_add_delayed (sched,
455 &remove_from_greylist,
458 if ( (GNUNET_NO == pos->is_friend) &&
459 (GNUNET_NO == pos->is_connected) )
461 free_peer (NULL, &pos->pid.hashPubKey, pos);
468 * Create a new entry in the peer list.
470 * @param peer identity of the new entry
471 * @param hello hello message, can be NULL
472 * @param is_friend is the new entry for a friend?
473 * @return the new entry
476 make_peer (const struct
477 GNUNET_PeerIdentity * peer,
478 const struct GNUNET_HELLO_Message *hello,
483 ret = GNUNET_malloc (sizeof (struct Peer));
485 ret->is_friend = is_friend;
488 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
489 memcpy (ret->hello, hello,
490 GNUNET_HELLO_size (hello));
492 GNUNET_CONTAINER_multihashmap_put (peers,
495 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
501 * Setup bloom filter for the given peer entry.
503 * @param peer entry to initialize
506 setup_filter (struct Peer *peer)
508 /* 2^{-5} chance of not sending a HELLO to a peer is
509 acceptably small (if the filter is 50% full);
510 64 bytes of memory are small compared to the rest
511 of the data structure and would only really become
512 "useless" once a HELLO has been passed on to ~100
513 other peers, which is likely more than enough in
514 any case; hence 64, 5 as bloomfilter parameters. */
515 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
516 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
517 /* never send a peer its own HELLO */
518 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
523 * Function to fill send buffer with HELLO.
525 * @param cls 'struct Peer' of the target peer
526 * @param size number of bytes available in buf
527 * @param buf where the callee should write the message
528 * @return number of bytes written to buf
531 hello_advertising_ready (void *cls,
539 * Closure for 'find_advertisable_hello'.
541 struct FindAdvHelloContext {
544 * Peer we want to advertise to.
549 * Where to store the result (peer selected for advertising).
554 * Maximum HELLO size we can use right now.
558 struct GNUNET_TIME_Relative next_adv;
563 * Find a peer that would be reasonable for advertising.
566 * @param pid identity of a peer
567 * @param value 'struct Peer*' for the peer we are considering
568 * @return GNUNET_YES (continue iteration)
571 find_advertisable_hello (void *cls,
572 const GNUNET_HashCode *pid,
575 struct FindAdvHelloContext *fah = cls;
576 struct Peer *pos = value;
577 struct GNUNET_TIME_Relative rst_time;
580 if (pos == fah->peer)
582 if (pos->hello == NULL)
584 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
585 if (0 == rst_time.value)
587 /* time to discard... */
588 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
591 fah->next_adv = GNUNET_TIME_relative_min (rst_time,
593 hs = GNUNET_HELLO_size (pos->hello);
594 if (hs > fah->max_size)
597 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
598 &fah->peer->pid.hashPubKey))
605 * Calculate when we would like to send the next HELLO to this
606 * peer and ask for it.
608 * @param cls for which peer to schedule the HELLO
609 * @param tc task context
612 schedule_next_hello (void *cls,
613 const struct GNUNET_SCHEDULER_TaskContext *tc)
615 struct Peer *pl = cls;
616 struct FindAdvHelloContext fah;
618 struct GNUNET_TIME_Relative delay;
620 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
621 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
622 return; /* we're out of here */
623 if (pl->hello_req != NULL)
624 return; /* did not finish sending the previous one */
625 /* find applicable HELLOs */
628 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE;
629 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
630 GNUNET_CONTAINER_multihashmap_iterate (peers,
631 &find_advertisable_hello,
634 = GNUNET_SCHEDULER_add_delayed (sched,
636 &schedule_next_hello,
638 if (fah.result == NULL)
640 next_want = GNUNET_HELLO_size (fah.result->hello);
641 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
642 if (delay.value == 0)
645 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
646 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
649 &hello_advertising_ready,
657 * Cancel existing requests for sending HELLOs to this peer
658 * and recalculate when we should send HELLOs to it based
659 * on our current state (something changed!).
661 * @param cls closure, 'struct Peer' to skip, or NULL
662 * @param pid identity of a peer
663 * @param value 'struct Peer*' for the peer
664 * @return GNUNET_YES (always)
667 reschedule_hellos (void *cls,
668 const GNUNET_HashCode *pid,
671 struct Peer *peer = value;
672 struct Peer *skip = cls;
676 if (! peer->is_connected)
678 if (peer->hello_req != NULL)
680 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
681 peer->hello_req = NULL;
683 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
685 GNUNET_SCHEDULER_cancel (sched,
686 peer->hello_delay_task);
687 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
689 peer->hello_delay_task
690 = GNUNET_SCHEDULER_add_now (sched,
691 &schedule_next_hello,
698 * Method called whenever a peer connects.
701 * @param peer peer identity this notification is about
702 * @param latency reported latency of the connection with 'other'
703 * @param distance reported distance (DV) to 'other'
706 connect_notify (void *cls,
708 GNUNET_PeerIdentity * peer,
709 struct GNUNET_TIME_Relative latency,
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "Core told us that we are connecting to `%s'\n",
720 GNUNET_STATISTICS_set (stats,
721 gettext_noop ("# peers connected"),
724 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
727 pos = make_peer (peer, NULL, GNUNET_NO);
728 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
732 GNUNET_assert (GNUNET_NO == pos->is_connected);
733 pos->greylisted_until.value = 0; /* remove greylisting */
735 pos->is_connected = GNUNET_YES;
736 pos->connect_attempts = 0; /* re-set back-off factor */
739 if ( (friend_count == minimum_friend_count - 1) &&
740 (GNUNET_YES != friends_only) )
743 GNUNET_STATISTICS_set (stats,
744 gettext_noop ("# friends connected"),
748 reschedule_hellos (NULL, &peer->hashPubKey, pos);
753 * Try to add more peers to our connection set.
755 * @param cls closure, not used
756 * @param pid identity of a peer
757 * @param value 'struct Peer*' for the peer
758 * @return GNUNET_YES (continue to iterate)
761 try_add_peers (void *cls,
762 const GNUNET_HashCode *pid,
765 struct Peer *pos = value;
767 attempt_connect (pos);
773 * Method called whenever a peer disconnects.
776 * @param peer peer identity this notification is about
779 disconnect_notify (void *cls,
781 GNUNET_PeerIdentity * peer)
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
787 "Core told us that we disconnected from `%s'\n",
790 pos = GNUNET_CONTAINER_multihashmap_get (peers,
797 if (pos->is_connected != GNUNET_YES)
803 GNUNET_STATISTICS_set (stats,
804 gettext_noop ("# peers connected"),
810 GNUNET_STATISTICS_set (stats,
811 gettext_noop ("# friends connected"),
815 if ( (connection_count < target_connection_count) ||
816 (friend_count < minimum_friend_count) )
817 GNUNET_CONTAINER_multihashmap_iterate (peers,
820 if ( (friend_count < minimum_friend_count) &&
821 (blacklist == NULL) )
822 blacklist = GNUNET_TRANSPORT_blacklist (sched, cfg,
823 &blacklist_check, NULL);
828 * Iterator called on each address.
830 * @param cls flag that we will set if we see any addresses
831 * @param tname name of the transport
832 * @param expiration when will the given address expire
833 * @param addr the address of the peer
834 * @param addrlen number of bytes in addr
835 * @return GNUNET_SYSERR always, to terminate iteration
838 address_iterator (void *cls,
840 struct GNUNET_TIME_Absolute expiration,
841 const void *addr, size_t addrlen)
845 return GNUNET_SYSERR;
850 * We've gotten a HELLO from another peer. Consider it for
853 * @param hello the HELLO we got
856 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
859 struct GNUNET_PeerIdentity pid;
860 struct GNUNET_TIME_Absolute dt;
861 struct GNUNET_HELLO_Message *nh;
865 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
866 if (0 == memcmp (&pid,
868 sizeof (struct GNUNET_PeerIdentity)))
869 return; /* that's me! */
870 have_address = GNUNET_NO;
871 GNUNET_HELLO_iterate_addresses (hello,
875 if (GNUNET_NO == have_address)
876 return; /* no point in advertising this one... */
877 peer = GNUNET_CONTAINER_multihashmap_get (peers,
881 peer = make_peer (&pid, hello, GNUNET_NO);
883 else if (peer->hello != NULL)
885 dt = GNUNET_HELLO_equals (peer->hello,
887 GNUNET_TIME_absolute_get());
888 if (dt.value == GNUNET_TIME_UNIT_FOREVER_ABS.value)
889 return; /* nothing new here */
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
893 "Found `%s' from peer `%s' for advertising\n",
897 if (peer->hello != NULL)
899 nh = GNUNET_HELLO_merge (peer->hello,
901 GNUNET_free (peer->hello);
906 size = GNUNET_HELLO_size (hello);
907 peer->hello = GNUNET_malloc (size);
908 memcpy (peer->hello, hello, size);
910 if (peer->filter != NULL)
911 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
913 /* since we have a new HELLO to pick from, re-schedule all
914 HELLO requests that are not bound by the HELLO send rate! */
915 GNUNET_CONTAINER_multihashmap_iterate (peers,
922 * PEERINFO calls this function to let us know about a possible peer
923 * that we might want to connect to.
925 * @param cls closure (not used)
926 * @param peer potential peer to connect to
927 * @param hello HELLO for this peer (or NULL)
928 * @param trust how much we trust the peer (not used)
931 process_peer (void *cls,
932 const struct GNUNET_PeerIdentity *peer,
933 const struct GNUNET_HELLO_Message *hello,
938 GNUNET_assert (peer != NULL);
939 if (0 == memcmp (&my_identity,
940 peer, sizeof (struct GNUNET_PeerIdentity)))
941 return; /* that's me! */
944 /* free existing HELLO, if any */
945 pos = GNUNET_CONTAINER_multihashmap_get (peers,
949 GNUNET_free_non_null (pos->hello);
951 if (pos->filter != NULL)
953 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
956 if ( (! pos->is_connected) &&
957 (! pos->is_friend) &&
958 (0 == GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value) )
959 free_peer (NULL, &pos->pid.hashPubKey, pos);
963 consider_for_advertising (hello);
964 pos = GNUNET_CONTAINER_multihashmap_get (peers,
967 pos = make_peer (peer, hello, GNUNET_NO);
968 GNUNET_assert (NULL != pos);
969 if (GNUNET_YES == pos->is_connected)
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Already connected to peer `%s'\n",
978 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value > 0)
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
982 "Already tried peer `%s' recently\n",
985 return; /* peer still greylisted */
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989 "Considering connecting to peer `%s'\n",
992 attempt_connect (pos);
997 * Function called after GNUNET_CORE_connect has succeeded
998 * (or failed for good).
1000 * @param cls closure
1001 * @param server handle to the server, NULL if we failed
1002 * @param my_id ID of this peer, NULL if we failed
1003 * @param publicKey public key of this peer, NULL if we failed
1006 core_init (void *cls,
1007 struct GNUNET_CORE_Handle * server,
1008 const struct GNUNET_PeerIdentity *
1011 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1017 _("Failed to connect to core service, can not manage topology!\n"));
1018 GNUNET_SCHEDULER_shutdown (sched);
1022 my_identity = *my_id;
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026 GNUNET_i2s (my_id));
1028 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1035 * Read the friends file.
1038 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1043 struct GNUNET_PeerIdentity pid;
1045 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1046 unsigned int entries_found;
1050 GNUNET_CONFIGURATION_get_value_filename (cfg,
1055 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1056 _("Option `%s' in section `%s' not specified!\n"),
1061 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1062 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1063 | GNUNET_DISK_PERM_USER_WRITE);
1064 if (0 != STAT (fn, &frstat))
1066 if ((friends_only) || (minimum_friend_count > 0))
1068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1069 _("Could not read friends list `%s'\n"), fn);
1074 if (frstat.st_size == 0)
1076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1077 _("Friends file `%s' is empty.\n"),
1082 data = GNUNET_malloc_large (frstat.st_size);
1083 if (frstat.st_size !=
1084 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1086 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1087 _("Failed to read friends list from `%s'\n"), fn);
1094 while ((pos < frstat.st_size) && isspace (data[pos]))
1096 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1097 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1099 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1100 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1102 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1103 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1104 (unsigned long long) pos);
1106 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1110 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1111 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1113 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1114 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1115 (unsigned long long) pos,
1120 if (0 != memcmp (&pid,
1122 sizeof (struct GNUNET_PeerIdentity)))
1125 fl = make_peer (&pid,
1128 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1129 _("Found friend `%s' in configuration\n"),
1130 GNUNET_i2s (&fl->pid));
1134 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1135 _("Found myself `%s' in friend list (useless, ignored)\n"),
1139 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1140 while ((pos < frstat.st_size) && isspace (data[pos]))
1145 GNUNET_STATISTICS_update (stats,
1146 gettext_noop ("# friends in configuration"),
1149 if ( (minimum_friend_count > entries_found) &&
1150 (friends_only == GNUNET_NO) )
1152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1153 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1155 if ( (minimum_friend_count > target_connection_count) &&
1156 (friends_only == GNUNET_NO) )
1158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1159 _("More friendly connections required than target total number of connections.\n"));
1165 * This function is called whenever an encrypted HELLO message is
1168 * @param cls closure
1169 * @param other the other peer involved (sender or receiver, NULL
1170 * for loopback messages where we are both sender and receiver)
1171 * @param message the actual HELLO message
1172 * @param latency reported latency of the connection with 'other'
1173 * @param distance reported distance (DV) to 'other'
1174 * @return GNUNET_OK to keep the connection open,
1175 * GNUNET_SYSERR to close it (signal serious error)
1178 handle_encrypted_hello (void *cls,
1179 const struct GNUNET_PeerIdentity * other,
1180 const struct GNUNET_MessageHeader *
1182 struct GNUNET_TIME_Relative latency,
1186 struct GNUNET_PeerIdentity pid;
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Received encrypted `%s' from peer `%s'",
1192 GNUNET_i2s (other));
1195 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) message,
1198 GNUNET_break_op (0);
1199 return GNUNET_SYSERR;
1201 GNUNET_STATISTICS_update (stats,
1202 gettext_noop ("# HELLO messages received"),
1205 peer = GNUNET_CONTAINER_multihashmap_get (peers,
1209 if ( (GNUNET_YES == friends_only) ||
1210 (friend_count < minimum_friend_count) )
1215 if ( (GNUNET_YES != peer->is_friend) &&
1216 (GNUNET_YES == friends_only) )
1218 if ( (GNUNET_YES != peer->is_friend) &&
1219 (friend_count < minimum_friend_count) )
1222 if (transport != NULL)
1223 GNUNET_TRANSPORT_offer_hello (transport,
1230 * Function to fill send buffer with HELLO.
1232 * @param cls 'struct Peer' of the target peer
1233 * @param size number of bytes available in buf
1234 * @param buf where the callee should write the message
1235 * @return number of bytes written to buf
1238 hello_advertising_ready (void *cls,
1242 struct Peer *pl = cls;
1243 struct FindAdvHelloContext fah;
1246 pl->hello_req = NULL;
1247 /* find applicable HELLOs */
1250 fah.max_size = size;
1251 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1252 GNUNET_CONTAINER_multihashmap_iterate (peers,
1253 &find_advertisable_hello,
1256 if (fah.result != NULL)
1258 want = GNUNET_HELLO_size (fah.result->hello);
1259 GNUNET_assert (want <= size);
1260 memcpy (buf, fah.result->hello, want);
1261 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
1262 &pl->pid.hashPubKey);
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Sending `%s' with %u bytes",
1267 (unsigned int) want);
1269 GNUNET_STATISTICS_update (stats,
1270 gettext_noop ("# HELLO messages gossipped"),
1274 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1275 pl->hello_delay_task
1276 = GNUNET_SCHEDULER_add_now (sched,
1277 &schedule_next_hello,
1284 * Last task run during shutdown. Disconnects us from
1285 * the transport and core.
1287 * @param cls unused, NULL
1288 * @param tc scheduler context
1291 cleaning_task (void *cls,
1292 const struct GNUNET_SCHEDULER_TaskContext *tc)
1294 if (NULL != peerinfo_notify)
1296 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1297 peerinfo_notify = NULL;
1299 GNUNET_TRANSPORT_disconnect (transport);
1301 GNUNET_CONTAINER_multihashmap_iterate (peers,
1304 GNUNET_CONTAINER_multihashmap_destroy (peers);
1307 GNUNET_CORE_disconnect (handle);
1313 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1320 * Main function that will be run.
1322 * @param cls closure
1323 * @param s the scheduler to use
1324 * @param args remaining command-line arguments
1325 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1326 * @param c configuration
1330 struct GNUNET_SCHEDULER_Handle * s,
1332 const char *cfgfile,
1333 const struct GNUNET_CONFIGURATION_Handle * c)
1335 struct GNUNET_CORE_MessageHandler handlers[] =
1337 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1340 unsigned long long opt;
1344 stats = GNUNET_STATISTICS_create (sched, "topology", cfg);
1345 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1348 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1352 GNUNET_CONFIGURATION_get_value_number (cfg,
1357 minimum_friend_count = (unsigned int) opt;
1359 GNUNET_CONFIGURATION_get_value_number (cfg,
1361 "TARGET-CONNECTION-COUNT",
1364 target_connection_count = (unsigned int) opt;
1365 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1367 if ( (friends_only == GNUNET_YES) ||
1368 (minimum_friend_count > 0) )
1369 read_friends_file (cfg);
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372 "Topology would like %u connections with at least %u friends (%s)\n",
1373 target_connection_count,
1374 minimum_friend_count,
1375 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1377 if (friend_count < minimum_friend_count)
1378 blacklist = GNUNET_TRANSPORT_blacklist (sched, cfg,
1379 &blacklist_check, NULL);
1380 transport = GNUNET_TRANSPORT_connect (sched,
1386 handle = GNUNET_CORE_connect (sched,
1388 GNUNET_TIME_UNIT_FOREVER_REL,
1396 GNUNET_SCHEDULER_add_delayed (sched,
1397 GNUNET_TIME_UNIT_FOREVER_REL,
1398 &cleaning_task, NULL);
1399 if (NULL == transport)
1401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1402 _("Failed to connect to `%s' service.\n"),
1404 GNUNET_SCHEDULER_shutdown (sched);
1409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1410 _("Failed to connect to `%s' service.\n"),
1412 GNUNET_SCHEDULER_shutdown (sched);
1419 * gnunet-daemon-topology command line options.
1421 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1422 GNUNET_GETOPT_OPTION_END
1427 * The main function for the topology daemon.
1429 * @param argc number of arguments from the command line
1430 * @param argv command line arguments
1431 * @return 0 ok, 1 on error
1434 main (int argc, char *const *argv)
1439 GNUNET_PROGRAM_run (argc,
1442 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1444 &run, NULL)) ? 0 : 1;
1448 /* end of gnunet-daemon-topology.c */