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 * Flag to disallow non-friend connections (pure F2F mode).
199 static int friends_only;
202 * Minimum number of friends to have in the
203 * connection set before we allow non-friends.
205 static unsigned int minimum_friend_count;
208 * Number of peers (friends and others) that we are currently connected to.
210 static unsigned int connection_count;
213 * Target number of connections.
215 static unsigned int target_connection_count;
218 * Number of friends that we are currently connected to.
220 static unsigned int friend_count;
223 * Should the topology daemon try to establish connections?
225 static int autoconnect;
229 * Function that decides if a connection is acceptable or not.
230 * If we have a blacklist, only friends are allowed, so the check
234 * @param pid peer to approve or disapproave
235 * @return GNUNET_OK if the connection is allowed
238 blacklist_check (void *cls,
239 const struct GNUNET_PeerIdentity *pid)
243 pos = GNUNET_CONTAINER_multihashmap_get (peers, &pid->hashPubKey);
244 if ( (pos != NULL) &&
245 (pos->is_friend == GNUNET_YES) )
247 GNUNET_STATISTICS_update (stats,
248 gettext_noop ("# peers blacklisted"),
251 return GNUNET_SYSERR;
256 * Whitelist all peers that we blacklisted; we've passed
257 * the minimum number of friends.
262 if (blacklist != NULL)
264 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
271 * Function called by core when our attempt to connect succeeded.
273 * @param cls the 'struct Peer' for which we issued the connect request
274 * @param success was the request transmitted
277 connect_completed_callback (void *cls,
280 struct Peer *pos = cls;
282 pos->connect_req = NULL;
287 * Check if an additional connection from the given peer is allowed.
289 * @param peer connection to check
290 * @return GNUNET_OK if the connection is allowed
293 is_connection_allowed (struct Peer *peer)
295 if (0 == memcmp (&my_identity,
297 sizeof (struct GNUNET_PeerIdentity)))
298 return GNUNET_SYSERR; /* disallow connections to self */
301 if (GNUNET_YES == friends_only)
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 "Determined that `%s' is not allowed to connect (not a friend)\n",
306 GNUNET_i2s (&peer->pid));
308 return GNUNET_SYSERR;
310 if (friend_count >= minimum_friend_count)
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
315 GNUNET_i2s (&peer->pid));
317 return GNUNET_SYSERR;
322 * Free all resources associated with the given peer.
324 * @param cls closure (not used)
325 * @param pid identity of the peer
326 * @param value peer to free
327 * @return GNUNET_YES (always: continue to iterate)
330 free_peer (void *cls,
331 const GNUNET_HashCode *pid,
334 struct Peer *pos = value;
336 GNUNET_break (GNUNET_OK ==
337 GNUNET_CONTAINER_multihashmap_remove (peers,
340 if (pos->hello_req != NULL)
341 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
342 if (pos->connect_req != NULL)
343 GNUNET_CORE_peer_request_connect_cancel (pos->connect_req);
344 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
345 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
346 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
347 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
348 GNUNET_free_non_null (pos->hello);
349 if (pos->filter != NULL)
350 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
357 * Discard peer entries for greylisted peers
358 * where the greylisting has expired.
360 * @param cls 'struct Peer' to greylist
361 * @param tc scheduler context
364 remove_from_greylist (void *cls,
365 const struct GNUNET_SCHEDULER_TaskContext *tc);
369 * Try to connect to the specified peer.
371 * @param pos peer to connect to
374 attempt_connect (struct Peer *pos)
376 struct GNUNET_TIME_Relative rem;
378 if ( (connection_count >= target_connection_count) &&
379 (friend_count >= minimum_friend_count) )
381 if (GNUNET_YES == pos->is_connected)
383 if (GNUNET_OK != is_connection_allowed (pos))
385 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
387 if (GNUNET_YES == pos->is_friend)
388 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
390 rem = GREYLIST_AFTER_ATTEMPT;
391 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
392 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
393 if (pos->connect_attempts > 30)
394 pos->connect_attempts = 30;
395 rem = GNUNET_TIME_relative_multiply (rem, 1 << (++pos->connect_attempts));
396 rem = GNUNET_TIME_relative_max (rem,
397 GREYLIST_AFTER_ATTEMPT_MIN);
398 rem = GNUNET_TIME_relative_min (rem,
399 GREYLIST_AFTER_ATTEMPT_MAX);
400 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
401 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
402 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
403 pos->greylist_clean_task
404 = GNUNET_SCHEDULER_add_delayed (rem,
405 &remove_from_greylist,
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Asking to connect to `%s'\n",
410 GNUNET_i2s (&pos->pid));
412 GNUNET_STATISTICS_update (stats,
413 gettext_noop ("# connect requests issued to core"),
416 pos->connect_req = GNUNET_CORE_peer_request_connect (handle,
417 GNUNET_TIME_UNIT_MINUTES,
419 &connect_completed_callback,
425 * Discard peer entries for greylisted peers
426 * where the greylisting has expired.
428 * @param cls 'struct Peer' to greylist
429 * @param tc scheduler context
432 remove_from_greylist (void *cls,
433 const struct GNUNET_SCHEDULER_TaskContext *tc)
435 struct Peer *pos = cls;
436 struct GNUNET_TIME_Relative rem;
438 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
439 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
440 if (rem.rel_value == 0)
442 attempt_connect (pos);
446 pos->greylist_clean_task
447 = GNUNET_SCHEDULER_add_delayed (rem,
448 &remove_from_greylist,
451 if ( (GNUNET_NO == pos->is_friend) &&
452 (GNUNET_NO == pos->is_connected) )
454 free_peer (NULL, &pos->pid.hashPubKey, pos);
461 * Create a new entry in the peer list.
463 * @param peer identity of the new entry
464 * @param hello hello message, can be NULL
465 * @param is_friend is the new entry for a friend?
466 * @return the new entry
469 make_peer (const struct
470 GNUNET_PeerIdentity * peer,
471 const struct GNUNET_HELLO_Message *hello,
476 ret = GNUNET_malloc (sizeof (struct Peer));
478 ret->is_friend = is_friend;
481 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
482 memcpy (ret->hello, hello,
483 GNUNET_HELLO_size (hello));
485 GNUNET_break (GNUNET_OK ==
486 GNUNET_CONTAINER_multihashmap_put (peers,
489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
495 * Setup bloom filter for the given peer entry.
497 * @param peer entry to initialize
500 setup_filter (struct Peer *peer)
502 /* 2^{-5} chance of not sending a HELLO to a peer is
503 acceptably small (if the filter is 50% full);
504 64 bytes of memory are small compared to the rest
505 of the data structure and would only really become
506 "useless" once a HELLO has been passed on to ~100
507 other peers, which is likely more than enough in
508 any case; hence 64, 5 as bloomfilter parameters. */
509 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
510 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
511 /* never send a peer its own HELLO */
512 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
517 * Function to fill send buffer with HELLO.
519 * @param cls 'struct Peer' of the target peer
520 * @param size number of bytes available in buf
521 * @param buf where the callee should write the message
522 * @return number of bytes written to buf
525 hello_advertising_ready (void *cls,
533 * Closure for 'find_advertisable_hello'.
535 struct FindAdvHelloContext {
538 * Peer we want to advertise to.
543 * Where to store the result (peer selected for advertising).
548 * Maximum HELLO size we can use right now.
552 struct GNUNET_TIME_Relative next_adv;
557 * Find a peer that would be reasonable for advertising.
560 * @param pid identity of a peer
561 * @param value 'struct Peer*' for the peer we are considering
562 * @return GNUNET_YES (continue iteration)
565 find_advertisable_hello (void *cls,
566 const GNUNET_HashCode *pid,
569 struct FindAdvHelloContext *fah = cls;
570 struct Peer *pos = value;
571 struct GNUNET_TIME_Relative rst_time;
574 if (pos == fah->peer)
576 if (pos->hello == NULL)
578 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
579 if (0 == rst_time.rel_value)
581 /* time to discard... */
582 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
585 fah->next_adv = GNUNET_TIME_relative_min (rst_time,
587 hs = GNUNET_HELLO_size (pos->hello);
588 if (hs > fah->max_size)
591 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
592 &fah->peer->pid.hashPubKey))
599 * Calculate when we would like to send the next HELLO to this
600 * peer and ask for it.
602 * @param cls for which peer to schedule the HELLO
603 * @param tc task context
606 schedule_next_hello (void *cls,
607 const struct GNUNET_SCHEDULER_TaskContext *tc)
609 struct Peer *pl = cls;
610 struct FindAdvHelloContext fah;
612 struct GNUNET_TIME_Relative delay;
614 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
615 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
616 return; /* we're out of here */
617 if (pl->hello_req != NULL)
618 return; /* did not finish sending the previous one */
619 /* find applicable HELLOs */
622 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
623 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
624 GNUNET_CONTAINER_multihashmap_iterate (peers,
625 &find_advertisable_hello,
628 = GNUNET_SCHEDULER_add_delayed (fah.next_adv,
629 &schedule_next_hello,
631 if (fah.result == NULL)
633 next_want = GNUNET_HELLO_size (fah.result->hello);
634 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
635 if (delay.rel_value == 0)
638 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle,
641 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
644 &hello_advertising_ready,
651 * Cancel existing requests for sending HELLOs to this peer
652 * and recalculate when we should send HELLOs to it based
653 * on our current state (something changed!).
655 * @param cls closure, 'struct Peer' to skip, or NULL
656 * @param pid identity of a peer
657 * @param value 'struct Peer*' for the peer
658 * @return GNUNET_YES (always)
661 reschedule_hellos (void *cls,
662 const GNUNET_HashCode *pid,
665 struct Peer *peer = value;
666 struct Peer *skip = cls;
670 if (! peer->is_connected)
672 if (peer->hello_req != NULL)
674 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
675 peer->hello_req = NULL;
677 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
679 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
680 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
682 peer->hello_delay_task
683 = GNUNET_SCHEDULER_add_now (&schedule_next_hello,
690 * Method called whenever a peer connects.
693 * @param peer peer identity this notification is about
694 * @param atsi performance data
697 connect_notify (void *cls,
699 GNUNET_PeerIdentity * peer,
700 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706 "Core told us that we are connecting to `%s'\n",
709 if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
713 GNUNET_STATISTICS_set (stats,
714 gettext_noop ("# peers connected"),
717 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
720 pos = make_peer (peer, NULL, GNUNET_NO);
721 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
725 GNUNET_assert (GNUNET_NO == pos->is_connected);
726 pos->greylisted_until.abs_value = 0; /* remove greylisting */
728 pos->is_connected = GNUNET_YES;
729 pos->connect_attempts = 0; /* re-set back-off factor */
732 if ( (friend_count == minimum_friend_count - 1) &&
733 (GNUNET_YES != friends_only) )
736 GNUNET_STATISTICS_set (stats,
737 gettext_noop ("# friends connected"),
741 reschedule_hellos (NULL, &peer->hashPubKey, pos);
746 * Try to add more peers to our connection set.
748 * @param cls closure, not used
749 * @param pid identity of a peer
750 * @param value 'struct Peer*' for the peer
751 * @return GNUNET_YES (continue to iterate)
754 try_add_peers (void *cls,
755 const GNUNET_HashCode *pid,
758 struct Peer *pos = value;
760 attempt_connect (pos);
766 * Method called whenever a peer disconnects.
769 * @param peer peer identity this notification is about
772 disconnect_notify (void *cls,
774 GNUNET_PeerIdentity * peer)
778 if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Core told us that we disconnected from `%s'\n",
785 pos = GNUNET_CONTAINER_multihashmap_get (peers,
792 if (pos->is_connected != GNUNET_YES)
797 pos->is_connected = GNUNET_NO;
799 GNUNET_STATISTICS_set (stats,
800 gettext_noop ("# peers connected"),
806 GNUNET_STATISTICS_set (stats,
807 gettext_noop ("# friends connected"),
811 if ( (connection_count < target_connection_count) ||
812 (friend_count < minimum_friend_count) )
813 GNUNET_CONTAINER_multihashmap_iterate (peers,
816 if ( (friend_count < minimum_friend_count) &&
817 (blacklist == NULL) )
818 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
819 &blacklist_check, NULL);
824 * Iterator called on each address.
826 * @param cls flag that we will set if we see any addresses
827 * @param tname name of the transport
828 * @param expiration when will the given address expire
829 * @param addr the address of the peer
830 * @param addrlen number of bytes in addr
831 * @return GNUNET_SYSERR always, to terminate iteration
834 address_iterator (void *cls,
836 struct GNUNET_TIME_Absolute expiration,
842 return GNUNET_SYSERR;
847 * We've gotten a HELLO from another peer. Consider it for
850 * @param hello the HELLO we got
853 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
856 struct GNUNET_PeerIdentity pid;
857 struct GNUNET_TIME_Absolute dt;
858 struct GNUNET_HELLO_Message *nh;
862 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
867 if (0 == memcmp (&pid,
869 sizeof (struct GNUNET_PeerIdentity)))
870 return; /* that's me! */
871 have_address = GNUNET_NO;
872 GNUNET_HELLO_iterate_addresses (hello,
876 if (GNUNET_NO == have_address)
877 return; /* no point in advertising this one... */
878 peer = GNUNET_CONTAINER_multihashmap_get (peers,
882 peer = make_peer (&pid, hello, GNUNET_NO);
884 else if (peer->hello != NULL)
886 dt = GNUNET_HELLO_equals (peer->hello,
888 GNUNET_TIME_absolute_get());
889 if (dt.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
890 return; /* nothing new here */
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "Found `%s' from peer `%s' for advertising\n",
898 if (peer->hello != NULL)
900 nh = GNUNET_HELLO_merge (peer->hello,
902 GNUNET_free (peer->hello);
907 size = GNUNET_HELLO_size (hello);
908 peer->hello = GNUNET_malloc (size);
909 memcpy (peer->hello, hello, size);
911 if (peer->filter != NULL)
912 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
914 /* since we have a new HELLO to pick from, re-schedule all
915 HELLO requests that are not bound by the HELLO send rate! */
916 GNUNET_CONTAINER_multihashmap_iterate (peers,
923 * PEERINFO calls this function to let us know about a possible peer
924 * that we might want to connect to.
926 * @param cls closure (not used)
927 * @param peer potential peer to connect to
928 * @param hello HELLO for this peer (or NULL)
929 * @param err_msg NULL if successful, otherwise contains error message
932 process_peer (void *cls,
933 const struct GNUNET_PeerIdentity *peer,
934 const struct GNUNET_HELLO_Message *hello,
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 _("Error in communication with PEERINFO service\n"));
946 GNUNET_assert (peer != NULL);
947 if (0 == memcmp (&my_identity,
948 peer, sizeof (struct GNUNET_PeerIdentity)))
949 return; /* that's me! */
952 /* free existing HELLO, if any */
953 pos = GNUNET_CONTAINER_multihashmap_get (peers,
957 GNUNET_free_non_null (pos->hello);
959 if (pos->filter != NULL)
961 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
964 if ( (! pos->is_connected) &&
965 (! pos->is_friend) &&
966 (0 == GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value) )
967 free_peer (NULL, &pos->pid.hashPubKey, pos);
971 consider_for_advertising (hello);
972 pos = GNUNET_CONTAINER_multihashmap_get (peers,
975 pos = make_peer (peer, hello, GNUNET_NO);
976 GNUNET_assert (NULL != pos);
977 if (GNUNET_YES == pos->is_connected)
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Already connected to peer `%s'\n",
986 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990 "Already tried peer `%s' recently\n",
993 return; /* peer still greylisted */
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "Considering connecting to peer `%s'\n",
1000 attempt_connect (pos);
1005 * Function called after GNUNET_CORE_connect has succeeded
1006 * (or failed for good).
1008 * @param cls closure
1009 * @param server handle to the server, NULL if we failed
1010 * @param my_id ID of this peer, NULL if we failed
1011 * @param publicKey public key of this peer, NULL if we failed
1014 core_init (void *cls,
1015 struct GNUNET_CORE_Handle * server,
1016 const struct GNUNET_PeerIdentity *
1019 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1024 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1025 _("Failed to connect to core service, can not manage topology!\n"));
1026 GNUNET_SCHEDULER_shutdown ();
1030 my_identity = *my_id;
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 GNUNET_i2s (my_id));
1036 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer,
1042 * Read the friends file.
1045 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1050 struct GNUNET_PeerIdentity pid;
1052 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1053 unsigned int entries_found;
1057 GNUNET_CONFIGURATION_get_value_filename (cfg,
1062 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1063 _("Option `%s' in section `%s' not specified!\n"),
1068 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1069 GNUNET_DISK_fn_write (fn, NULL, 0,
1070 GNUNET_DISK_PERM_USER_READ
1071 | GNUNET_DISK_PERM_USER_WRITE);
1072 if (0 != STAT (fn, &frstat))
1074 if ((friends_only) || (minimum_friend_count > 0))
1075 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1076 _("Could not read friends list `%s'\n"), fn);
1080 if (frstat.st_size == 0)
1082 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1083 _("Friends file `%s' is empty.\n"),
1088 data = GNUNET_malloc_large (frstat.st_size);
1091 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1092 _("Failed to read friends list from `%s': out of memory\n"), fn);
1096 if (frstat.st_size !=
1097 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1099 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1100 _("Failed to read friends list from `%s'\n"), fn);
1107 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1109 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1110 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1112 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1113 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1115 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1116 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1117 (unsigned long long) pos);
1119 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1123 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1124 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1126 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1127 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1128 (unsigned long long) pos,
1133 if (0 != memcmp (&pid,
1135 sizeof (struct GNUNET_PeerIdentity)))
1138 fl = make_peer (&pid,
1141 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1142 _("Found friend `%s' in configuration\n"),
1143 GNUNET_i2s (&fl->pid));
1147 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1148 _("Found myself `%s' in friend list (useless, ignored)\n"),
1152 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1153 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1158 GNUNET_STATISTICS_update (stats,
1159 gettext_noop ("# friends in configuration"),
1162 if ( (minimum_friend_count > entries_found) &&
1163 (friends_only == GNUNET_NO) )
1165 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1166 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1168 if ( (minimum_friend_count > target_connection_count) &&
1169 (friends_only == GNUNET_NO) )
1171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1172 _("More friendly connections required than target total number of connections.\n"));
1178 * This function is called whenever an encrypted HELLO message is
1181 * @param cls closure
1182 * @param other the other peer involved (sender or receiver, NULL
1183 * for loopback messages where we are both sender and receiver)
1184 * @param message the actual HELLO message
1185 * @param atsi performance data
1186 * @return GNUNET_OK to keep the connection open,
1187 * GNUNET_SYSERR to close it (signal serious error)
1190 handle_encrypted_hello (void *cls,
1191 const struct GNUNET_PeerIdentity * other,
1192 const struct GNUNET_MessageHeader *
1194 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1197 struct GNUNET_PeerIdentity pid;
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 "Received encrypted `%s' from peer `%s'",
1203 GNUNET_i2s (other));
1206 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) message,
1209 GNUNET_break_op (0);
1210 return GNUNET_SYSERR;
1212 GNUNET_STATISTICS_update (stats,
1213 gettext_noop ("# HELLO messages received"),
1216 peer = GNUNET_CONTAINER_multihashmap_get (peers,
1220 if ( (GNUNET_YES == friends_only) ||
1221 (friend_count < minimum_friend_count) )
1226 if ( (GNUNET_YES != peer->is_friend) &&
1227 (GNUNET_YES == friends_only) )
1229 if ( (GNUNET_YES != peer->is_friend) &&
1230 (friend_count < minimum_friend_count) )
1233 if (transport != NULL)
1234 GNUNET_TRANSPORT_offer_hello (transport,
1235 message, NULL, NULL);
1241 * Function to fill send buffer with HELLO.
1243 * @param cls 'struct Peer' of the target peer
1244 * @param size number of bytes available in buf
1245 * @param buf where the callee should write the message
1246 * @return number of bytes written to buf
1249 hello_advertising_ready (void *cls,
1253 struct Peer *pl = cls;
1254 struct FindAdvHelloContext fah;
1257 pl->hello_req = NULL;
1258 /* find applicable HELLOs */
1261 fah.max_size = size;
1262 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1263 GNUNET_CONTAINER_multihashmap_iterate (peers,
1264 &find_advertisable_hello,
1267 if (fah.result != NULL)
1269 want = GNUNET_HELLO_size (fah.result->hello);
1270 GNUNET_assert (want <= size);
1271 memcpy (buf, fah.result->hello, want);
1272 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
1273 &pl->pid.hashPubKey);
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "Sending `%s' with %u bytes",
1278 (unsigned int) want);
1280 GNUNET_STATISTICS_update (stats,
1281 gettext_noop ("# HELLO messages gossipped"),
1285 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1286 pl->hello_delay_task
1287 = GNUNET_SCHEDULER_add_now (&schedule_next_hello,
1294 * Last task run during shutdown. Disconnects us from
1295 * the transport and core.
1297 * @param cls unused, NULL
1298 * @param tc scheduler context
1301 cleaning_task (void *cls,
1302 const struct GNUNET_SCHEDULER_TaskContext *tc)
1304 if (NULL != peerinfo_notify)
1306 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1307 peerinfo_notify = NULL;
1309 GNUNET_TRANSPORT_disconnect (transport);
1311 GNUNET_CONTAINER_multihashmap_iterate (peers,
1314 GNUNET_CONTAINER_multihashmap_destroy (peers);
1317 GNUNET_CORE_disconnect (handle);
1323 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1330 * Main function that will be run.
1332 * @param cls closure
1333 * @param args remaining command-line arguments
1334 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1335 * @param c configuration
1340 const char *cfgfile,
1341 const struct GNUNET_CONFIGURATION_Handle * c)
1343 struct GNUNET_CORE_MessageHandler handlers[] =
1345 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1348 unsigned long long opt;
1351 stats = GNUNET_STATISTICS_create ("topology", cfg);
1352 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1355 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1359 GNUNET_CONFIGURATION_get_value_number (cfg,
1364 minimum_friend_count = (unsigned int) opt;
1366 GNUNET_CONFIGURATION_get_value_number (cfg,
1368 "TARGET-CONNECTION-COUNT",
1371 target_connection_count = (unsigned int) opt;
1372 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1374 if ( (friends_only == GNUNET_YES) ||
1375 (minimum_friend_count > 0) )
1376 read_friends_file (cfg);
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1379 "Topology would like %u connections with at least %u friends (%s)\n",
1380 target_connection_count,
1381 minimum_friend_count,
1382 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1384 if (friend_count < minimum_friend_count)
1385 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1386 &blacklist_check, NULL);
1387 transport = GNUNET_TRANSPORT_connect (cfg,
1393 handle = GNUNET_CORE_connect (cfg,
1403 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1404 &cleaning_task, NULL);
1405 if (NULL == transport)
1407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1408 _("Failed to connect to `%s' service.\n"),
1410 GNUNET_SCHEDULER_shutdown ();
1415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1416 _("Failed to connect to `%s' service.\n"),
1418 GNUNET_SCHEDULER_shutdown ();
1425 * The main function for the topology daemon.
1427 * @param argc number of arguments from the command line
1428 * @param argv command line arguments
1429 * @return 0 ok, 1 on error
1432 main (int argc, char *const *argv)
1434 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1435 GNUNET_GETOPT_OPTION_END
1440 GNUNET_PROGRAM_run (argc,
1443 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1445 &run, NULL)) ? 0 : 1;
1449 /* end of gnunet-daemon-topology.c */