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 3, 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_YES
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, 1)
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 const struct GNUNET_CONFIGURATION_Handle *cfg;
165 * Handle to the core API.
167 static struct GNUNET_CORE_Handle *handle;
170 * Handle to the transport API.
172 static struct GNUNET_TRANSPORT_Handle *transport;
175 * Identity of this peer.
177 static struct GNUNET_PeerIdentity my_identity;
180 * All of our friends, all of our current neighbours and all peers for
181 * which we have HELLOs. So pretty much everyone. Maps peer identities
182 * to 'struct Peer *' values.
184 static struct GNUNET_CONTAINER_MultiHashMap *peers;
187 * Handle for reporting statistics.
189 static struct GNUNET_STATISTICS_Handle *stats;
192 * Blacklist (NULL if we have none).
194 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
197 * Task scheduled to try to add peers.
199 static GNUNET_SCHEDULER_TaskIdentifier add_task;
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 request to connect was transmitted.
278 * @param cls the 'struct Peer' for which we issued the connect request
279 * @param success was the request transmitted
282 connect_completed_callback (void *cls,
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 (pos->hello_delay_task);
351 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
352 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
353 GNUNET_free_non_null (pos->hello);
354 if (pos->filter != NULL)
355 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
362 * Discard peer entries for greylisted peers
363 * where the greylisting has expired.
365 * @param cls 'struct Peer' to greylist
366 * @param tc scheduler context
369 remove_from_greylist (void *cls,
370 const struct GNUNET_SCHEDULER_TaskContext *tc);
374 * Try to connect to the specified peer.
376 * @param pos peer to connect to
379 attempt_connect (struct Peer *pos)
381 struct GNUNET_TIME_Relative rem;
383 if ( (connection_count >= target_connection_count) &&
384 (friend_count >= minimum_friend_count) )
386 if (GNUNET_YES == pos->is_connected)
388 if (GNUNET_OK != is_connection_allowed (pos))
390 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
392 if (GNUNET_YES == pos->is_friend)
393 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
395 rem = GREYLIST_AFTER_ATTEMPT;
396 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
397 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
398 if (pos->connect_attempts > 30)
399 pos->connect_attempts = 30;
400 rem = GNUNET_TIME_relative_multiply (rem, 1 << (++pos->connect_attempts));
401 rem = GNUNET_TIME_relative_max (rem,
402 GREYLIST_AFTER_ATTEMPT_MIN);
403 rem = GNUNET_TIME_relative_min (rem,
404 GREYLIST_AFTER_ATTEMPT_MAX);
405 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
406 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
407 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
408 pos->greylist_clean_task
409 = GNUNET_SCHEDULER_add_delayed (rem,
410 &remove_from_greylist,
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414 "Asking to connect to `%s'\n",
415 GNUNET_i2s (&pos->pid));
417 GNUNET_STATISTICS_update (stats,
418 gettext_noop ("# connect requests issued to core"),
421 pos->connect_req = GNUNET_CORE_peer_request_connect (handle,
423 &connect_completed_callback,
429 * Discard peer entries for greylisted peers
430 * where the greylisting has expired.
432 * @param cls 'struct Peer' to greylist
433 * @param tc scheduler context
436 remove_from_greylist (void *cls,
437 const struct GNUNET_SCHEDULER_TaskContext *tc)
439 struct Peer *pos = cls;
440 struct GNUNET_TIME_Relative rem;
442 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
443 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
444 if (rem.rel_value == 0)
446 attempt_connect (pos);
450 pos->greylist_clean_task
451 = GNUNET_SCHEDULER_add_delayed (rem,
452 &remove_from_greylist,
455 if ( (GNUNET_NO == pos->is_friend) &&
456 (GNUNET_NO == pos->is_connected) &&
457 (NULL == pos->hello) )
459 free_peer (NULL, &pos->pid.hashPubKey, pos);
466 * Create a new entry in the peer list.
468 * @param peer identity of the new entry
469 * @param hello hello message, can be NULL
470 * @param is_friend is the new entry for a friend?
471 * @return the new entry
474 make_peer (const struct
475 GNUNET_PeerIdentity * peer,
476 const struct GNUNET_HELLO_Message *hello,
481 ret = GNUNET_malloc (sizeof (struct Peer));
483 ret->is_friend = is_friend;
486 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
487 memcpy (ret->hello, hello,
488 GNUNET_HELLO_size (hello));
490 GNUNET_break (GNUNET_OK ==
491 GNUNET_CONTAINER_multihashmap_put (peers,
494 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
500 * Setup bloom filter for the given peer entry.
502 * @param peer entry to initialize
505 setup_filter (struct Peer *peer)
507 /* 2^{-5} chance of not sending a HELLO to a peer is
508 acceptably small (if the filter is 50% full);
509 64 bytes of memory are small compared to the rest
510 of the data structure and would only really become
511 "useless" once a HELLO has been passed on to ~100
512 other peers, which is likely more than enough in
513 any case; hence 64, 5 as bloomfilter parameters. */
514 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
515 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
516 /* never send a peer its own HELLO */
517 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
522 * Function to fill send buffer with HELLO.
524 * @param cls 'struct Peer' of the target peer
525 * @param size number of bytes available in buf
526 * @param buf where the callee should write the message
527 * @return number of bytes written to buf
530 hello_advertising_ready (void *cls,
536 * Closure for 'find_advertisable_hello'.
538 struct FindAdvHelloContext {
541 * Peer we want to advertise to.
546 * Where to store the result (peer selected for advertising).
551 * Maximum HELLO size we can use right now.
555 struct GNUNET_TIME_Relative next_adv;
560 * Find a peer that would be reasonable for advertising.
563 * @param pid identity of a peer
564 * @param value 'struct Peer*' for the peer we are considering
565 * @return GNUNET_YES (continue iteration)
568 find_advertisable_hello (void *cls,
569 const GNUNET_HashCode *pid,
572 struct FindAdvHelloContext *fah = cls;
573 struct Peer *pos = value;
574 struct GNUNET_TIME_Relative rst_time;
577 if (pos == fah->peer)
579 if (pos->hello == NULL)
581 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
582 if (0 == rst_time.rel_value)
584 /* time to discard... */
585 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
588 fah->next_adv = GNUNET_TIME_relative_min (rst_time,
590 hs = GNUNET_HELLO_size (pos->hello);
591 if (hs > fah->max_size)
594 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
595 &fah->peer->pid.hashPubKey))
602 * Calculate when we would like to send the next HELLO to this
603 * peer and ask for it.
605 * @param cls for which peer to schedule the HELLO
606 * @param tc task context
609 schedule_next_hello (void *cls,
610 const struct GNUNET_SCHEDULER_TaskContext *tc)
612 struct Peer *pl = cls;
613 struct FindAdvHelloContext fah;
615 struct GNUNET_TIME_Relative delay;
617 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
618 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
619 return; /* we're out of here */
620 if (pl->hello_req != NULL)
621 return; /* did not finish sending the previous one */
622 /* find applicable HELLOs */
625 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
626 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
627 GNUNET_CONTAINER_multihashmap_iterate (peers,
628 &find_advertisable_hello,
631 = GNUNET_SCHEDULER_add_delayed (fah.next_adv,
632 &schedule_next_hello,
634 if (fah.result == NULL)
636 next_want = GNUNET_HELLO_size (fah.result->hello);
637 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
638 if (delay.rel_value == 0)
641 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle,
644 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
647 &hello_advertising_ready,
654 * Cancel existing requests for sending HELLOs to this peer
655 * and recalculate when we should send HELLOs to it based
656 * on our current state (something changed!).
658 * @param cls closure, 'struct Peer' to skip, or NULL
659 * @param pid identity of a peer
660 * @param value 'struct Peer*' for the peer
661 * @return GNUNET_YES (always)
664 reschedule_hellos (void *cls,
665 const GNUNET_HashCode *pid,
668 struct Peer *peer = value;
669 struct Peer *skip = cls;
673 if (! peer->is_connected)
675 if (peer->hello_req != NULL)
677 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
678 peer->hello_req = NULL;
680 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
682 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
683 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
685 peer->hello_delay_task
686 = GNUNET_SCHEDULER_add_now (&schedule_next_hello,
693 * Method called whenever a peer connects.
696 * @param peer peer identity this notification is about
697 * @param atsi performance data
700 connect_notify (void *cls,
702 GNUNET_PeerIdentity * peer,
703 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "Core told us that we are connecting to `%s'\n",
712 if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
716 GNUNET_STATISTICS_set (stats,
717 gettext_noop ("# peers connected"),
720 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
723 pos = make_peer (peer, NULL, GNUNET_NO);
724 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
728 GNUNET_assert (GNUNET_NO == pos->is_connected);
729 pos->greylisted_until.abs_value = 0; /* remove greylisting */
731 pos->is_connected = GNUNET_YES;
732 pos->connect_attempts = 0; /* re-set back-off factor */
735 if ( (friend_count == minimum_friend_count - 1) &&
736 (GNUNET_YES != friends_only) )
739 GNUNET_STATISTICS_set (stats,
740 gettext_noop ("# friends connected"),
744 reschedule_hellos (NULL, &peer->hashPubKey, pos);
749 * Try to add more peers to our connection set.
751 * @param cls closure, not used
752 * @param pid identity of a peer
753 * @param value 'struct Peer*' for the peer
754 * @return GNUNET_YES (continue to iterate)
757 try_add_peers (void *cls,
758 const GNUNET_HashCode *pid,
761 struct Peer *pos = value;
763 attempt_connect (pos);
769 * Last task run during shutdown. Disconnects us from
770 * the transport and core.
772 * @param cls unused, NULL
773 * @param tc scheduler context
776 add_peer_task (void *cls,
777 const struct GNUNET_SCHEDULER_TaskContext *tc)
779 add_task = GNUNET_SCHEDULER_NO_TASK;
781 GNUNET_CONTAINER_multihashmap_iterate (peers,
787 * Method called whenever a peer disconnects.
790 * @param peer peer identity this notification is about
793 disconnect_notify (void *cls,
795 GNUNET_PeerIdentity * peer)
799 if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "Core told us that we disconnected from `%s'\n",
806 pos = GNUNET_CONTAINER_multihashmap_get (peers,
813 if (pos->is_connected != GNUNET_YES)
818 pos->is_connected = GNUNET_NO;
820 GNUNET_STATISTICS_set (stats,
821 gettext_noop ("# peers connected"),
827 GNUNET_STATISTICS_set (stats,
828 gettext_noop ("# friends connected"),
832 if ( ( (connection_count < target_connection_count) ||
833 (friend_count < minimum_friend_count) ) &&
834 (GNUNET_SCHEDULER_NO_TASK == add_task) )
835 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
836 if ( (friend_count < minimum_friend_count) &&
837 (blacklist == NULL) )
838 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
839 &blacklist_check, NULL);
844 * Iterator called on each address.
846 * @param cls flag that we will set if we see any addresses
847 * @param tname name of the transport
848 * @param expiration when will the given address expire
849 * @param addr the address of the peer
850 * @param addrlen number of bytes in addr
851 * @return GNUNET_SYSERR always, to terminate iteration
854 address_iterator (void *cls,
856 struct GNUNET_TIME_Absolute expiration,
862 return GNUNET_SYSERR;
867 * We've gotten a HELLO from another peer. Consider it for
870 * @param hello the HELLO we got
873 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
876 struct GNUNET_PeerIdentity pid;
877 struct GNUNET_TIME_Absolute dt;
878 struct GNUNET_HELLO_Message *nh;
882 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
887 if (0 == memcmp (&pid,
889 sizeof (struct GNUNET_PeerIdentity)))
890 return; /* that's me! */
891 have_address = GNUNET_NO;
892 GNUNET_HELLO_iterate_addresses (hello,
896 if (GNUNET_NO == have_address)
897 return; /* no point in advertising this one... */
898 peer = GNUNET_CONTAINER_multihashmap_get (peers,
902 peer = make_peer (&pid, hello, GNUNET_NO);
904 else if (peer->hello != NULL)
906 dt = GNUNET_HELLO_equals (peer->hello,
908 GNUNET_TIME_absolute_get());
909 if (dt.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
910 return; /* nothing new here */
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 "Found `%s' from peer `%s' for advertising\n",
918 if (peer->hello != NULL)
920 nh = GNUNET_HELLO_merge (peer->hello,
922 GNUNET_free (peer->hello);
927 size = GNUNET_HELLO_size (hello);
928 peer->hello = GNUNET_malloc (size);
929 memcpy (peer->hello, hello, size);
931 if (peer->filter != NULL)
932 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
934 /* since we have a new HELLO to pick from, re-schedule all
935 HELLO requests that are not bound by the HELLO send rate! */
936 GNUNET_CONTAINER_multihashmap_iterate (peers,
943 * PEERINFO calls this function to let us know about a possible peer
944 * that we might want to connect to.
946 * @param cls closure (not used)
947 * @param peer potential peer to connect to
948 * @param hello HELLO for this peer (or NULL)
949 * @param err_msg NULL if successful, otherwise contains error message
952 process_peer (void *cls,
953 const struct GNUNET_PeerIdentity *peer,
954 const struct GNUNET_HELLO_Message *hello,
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 _("Error in communication with PEERINFO service: %s\n"),
964 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
965 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer,
969 GNUNET_assert (peer != NULL);
970 if (0 == memcmp (&my_identity,
971 peer, sizeof (struct GNUNET_PeerIdentity)))
972 return; /* that's me! */
975 /* free existing HELLO, if any */
976 pos = GNUNET_CONTAINER_multihashmap_get (peers,
980 GNUNET_free_non_null (pos->hello);
982 if (pos->filter != NULL)
984 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
987 if ( (! pos->is_connected) &&
988 (! pos->is_friend) &&
989 (0 == GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value) )
990 free_peer (NULL, &pos->pid.hashPubKey, pos);
994 consider_for_advertising (hello);
995 pos = GNUNET_CONTAINER_multihashmap_get (peers,
998 pos = make_peer (peer, hello, GNUNET_NO);
999 GNUNET_assert (NULL != pos);
1000 if (GNUNET_YES == pos->is_connected)
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 "Already connected to peer `%s'\n",
1009 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 "Already tried peer `%s' recently\n",
1016 return; /* peer still greylisted */
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1020 "Considering connecting to peer `%s'\n",
1023 attempt_connect (pos);
1028 * Function called after GNUNET_CORE_connect has succeeded
1029 * (or failed for good).
1031 * @param cls closure
1032 * @param server handle to the server, NULL if we failed
1033 * @param my_id ID of this peer, NULL if we failed
1034 * @param publicKey public key of this peer, NULL if we failed
1037 core_init (void *cls,
1038 struct GNUNET_CORE_Handle * server,
1039 const struct GNUNET_PeerIdentity *
1042 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1048 _("Failed to connect to core service, can not manage topology!\n"));
1049 GNUNET_SCHEDULER_shutdown ();
1053 my_identity = *my_id;
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 GNUNET_i2s (my_id));
1059 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer,
1065 * Read the friends file.
1068 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1073 struct GNUNET_PeerIdentity pid;
1075 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1076 unsigned int entries_found;
1080 GNUNET_CONFIGURATION_get_value_filename (cfg,
1085 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1086 _("Option `%s' in section `%s' not specified!\n"),
1091 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1092 GNUNET_DISK_fn_write (fn, NULL, 0,
1093 GNUNET_DISK_PERM_USER_READ
1094 | GNUNET_DISK_PERM_USER_WRITE);
1095 if (0 != STAT (fn, &frstat))
1097 if ((friends_only) || (minimum_friend_count > 0))
1098 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1099 _("Could not read friends list `%s'\n"), fn);
1103 if (frstat.st_size == 0)
1105 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1106 _("Friends file `%s' is empty.\n"),
1111 data = GNUNET_malloc_large (frstat.st_size);
1114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1115 _("Failed to read friends list from `%s': out of memory\n"), fn);
1119 if (frstat.st_size !=
1120 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1123 _("Failed to read friends list from `%s'\n"), fn);
1130 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1132 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1133 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1135 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1136 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1138 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1139 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1140 (unsigned long long) pos);
1142 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1146 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1147 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1149 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1150 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1151 (unsigned long long) pos,
1156 if (0 != memcmp (&pid,
1158 sizeof (struct GNUNET_PeerIdentity)))
1161 fl = make_peer (&pid,
1164 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1165 _("Found friend `%s' in configuration\n"),
1166 GNUNET_i2s (&fl->pid));
1170 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1171 _("Found myself `%s' in friend list (useless, ignored)\n"),
1175 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1176 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1181 GNUNET_STATISTICS_update (stats,
1182 gettext_noop ("# friends in configuration"),
1185 if ( (minimum_friend_count > entries_found) &&
1186 (friends_only == GNUNET_NO) )
1188 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1189 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1191 if ( (minimum_friend_count > target_connection_count) &&
1192 (friends_only == GNUNET_NO) )
1194 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1195 _("More friendly connections required than target total number of connections.\n"));
1201 * This function is called whenever an encrypted HELLO message is
1204 * @param cls closure
1205 * @param other the other peer involved (sender or receiver, NULL
1206 * for loopback messages where we are both sender and receiver)
1207 * @param message the actual HELLO message
1208 * @param atsi performance data
1209 * @return GNUNET_OK to keep the connection open,
1210 * GNUNET_SYSERR to close it (signal serious error)
1213 handle_encrypted_hello (void *cls,
1214 const struct GNUNET_PeerIdentity * other,
1215 const struct GNUNET_MessageHeader *
1217 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1220 struct GNUNET_PeerIdentity pid;
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1224 "Received encrypted `%s' from peer `%s'",
1226 GNUNET_i2s (other));
1229 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) message,
1232 GNUNET_break_op (0);
1233 return GNUNET_SYSERR;
1235 GNUNET_STATISTICS_update (stats,
1236 gettext_noop ("# HELLO messages received"),
1239 peer = GNUNET_CONTAINER_multihashmap_get (peers,
1243 if ( (GNUNET_YES == friends_only) ||
1244 (friend_count < minimum_friend_count) )
1249 if ( (GNUNET_YES != peer->is_friend) &&
1250 (GNUNET_YES == friends_only) )
1252 if ( (GNUNET_YES != peer->is_friend) &&
1253 (friend_count < minimum_friend_count) )
1256 if (transport != NULL)
1257 GNUNET_TRANSPORT_offer_hello (transport,
1258 message, NULL, NULL);
1264 * Function to fill send buffer with HELLO.
1266 * @param cls 'struct Peer' of the target peer
1267 * @param size number of bytes available in buf
1268 * @param buf where the callee should write the message
1269 * @return number of bytes written to buf
1272 hello_advertising_ready (void *cls,
1276 struct Peer *pl = cls;
1277 struct FindAdvHelloContext fah;
1280 pl->hello_req = NULL;
1281 /* find applicable HELLOs */
1284 fah.max_size = size;
1285 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1286 GNUNET_CONTAINER_multihashmap_iterate (peers,
1287 &find_advertisable_hello,
1290 if (fah.result != NULL)
1292 want = GNUNET_HELLO_size (fah.result->hello);
1293 GNUNET_assert (want <= size);
1294 memcpy (buf, fah.result->hello, want);
1295 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
1296 &pl->pid.hashPubKey);
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1299 "Sending `%s' with %u bytes",
1301 (unsigned int) want);
1303 GNUNET_STATISTICS_update (stats,
1304 gettext_noop ("# HELLO messages gossipped"),
1308 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1309 pl->hello_delay_task
1310 = GNUNET_SCHEDULER_add_now (&schedule_next_hello,
1317 * Last task run during shutdown. Disconnects us from
1318 * the transport and core.
1320 * @param cls unused, NULL
1321 * @param tc scheduler context
1324 cleaning_task (void *cls,
1325 const struct GNUNET_SCHEDULER_TaskContext *tc)
1327 if (NULL != peerinfo_notify)
1329 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1330 peerinfo_notify = NULL;
1332 if (GNUNET_SCHEDULER_NO_TASK != add_task)
1334 GNUNET_SCHEDULER_cancel (add_task);
1335 add_task = GNUNET_SCHEDULER_NO_TASK;
1337 GNUNET_TRANSPORT_disconnect (transport);
1339 GNUNET_CONTAINER_multihashmap_iterate (peers,
1342 GNUNET_CONTAINER_multihashmap_destroy (peers);
1345 GNUNET_CORE_disconnect (handle);
1351 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1358 * Main function that will be run.
1360 * @param cls closure
1361 * @param args remaining command-line arguments
1362 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1363 * @param c configuration
1368 const char *cfgfile,
1369 const struct GNUNET_CONFIGURATION_Handle * c)
1371 struct GNUNET_CORE_MessageHandler handlers[] =
1373 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1376 unsigned long long opt;
1379 stats = GNUNET_STATISTICS_create ("topology", cfg);
1380 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1383 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1387 GNUNET_CONFIGURATION_get_value_number (cfg,
1392 minimum_friend_count = (unsigned int) opt;
1394 GNUNET_CONFIGURATION_get_value_number (cfg,
1396 "TARGET-CONNECTION-COUNT",
1399 target_connection_count = (unsigned int) opt;
1400 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1402 if ( (friends_only == GNUNET_YES) ||
1403 (minimum_friend_count > 0) )
1404 read_friends_file (cfg);
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407 "Topology would like %u connections with at least %u friends (%s)\n",
1408 target_connection_count,
1409 minimum_friend_count,
1410 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1412 if (friend_count < minimum_friend_count)
1413 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1414 &blacklist_check, NULL);
1415 transport = GNUNET_TRANSPORT_connect (cfg,
1421 handle = GNUNET_CORE_connect (cfg,
1431 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1432 &cleaning_task, NULL);
1433 if (NULL == transport)
1435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1436 _("Failed to connect to `%s' service.\n"),
1438 GNUNET_SCHEDULER_shutdown ();
1443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1444 _("Failed to connect to `%s' service.\n"),
1446 GNUNET_SCHEDULER_shutdown ();
1453 * The main function for the topology daemon.
1455 * @param argc number of arguments from the command line
1456 * @param argv command line arguments
1457 * @return 0 ok, 1 on error
1460 main (int argc, char *const *argv)
1462 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1463 GNUNET_GETOPT_OPTION_END
1468 GNUNET_PROGRAM_run (argc,
1471 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1473 &run, NULL)) ? 0 : 1;
1477 /* end of gnunet-daemon-topology.c */