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_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 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
353 GNUNET_SCHEDULER_cancel (sched,
354 pos->greylist_clean_task);
355 GNUNET_free_non_null (pos->hello);
356 if (pos->filter != NULL)
357 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
364 * Discard peer entries for greylisted peers
365 * where the greylisting has expired.
367 * @param cls 'struct Peer' to greylist
368 * @param tc scheduler context
371 remove_from_greylist (void *cls,
372 const struct GNUNET_SCHEDULER_TaskContext *tc);
376 * Try to connect to the specified peer.
378 * @param pos peer to connect to
381 attempt_connect (struct Peer *pos)
383 struct GNUNET_TIME_Relative rem;
385 if ( (connection_count >= target_connection_count) &&
386 (friend_count >= minimum_friend_count) )
388 if (GNUNET_YES == pos->is_connected)
390 if (GNUNET_OK != is_connection_allowed (pos))
392 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
394 if (GNUNET_YES == pos->is_friend)
395 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
397 rem = GREYLIST_AFTER_ATTEMPT;
398 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
399 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
400 if (pos->connect_attempts > 30)
401 pos->connect_attempts = 30;
402 rem = GNUNET_TIME_relative_multiply (rem, 1 << (++pos->connect_attempts));
403 rem = GNUNET_TIME_relative_max (rem,
404 GREYLIST_AFTER_ATTEMPT_MIN);
405 rem = GNUNET_TIME_relative_min (rem,
406 GREYLIST_AFTER_ATTEMPT_MAX);
407 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
408 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
409 GNUNET_SCHEDULER_cancel (sched,
410 pos->greylist_clean_task);
411 pos->greylist_clean_task
412 = GNUNET_SCHEDULER_add_delayed (sched,
414 &remove_from_greylist,
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Asking to connect to `%s'\n",
419 GNUNET_i2s (&pos->pid));
421 GNUNET_STATISTICS_update (stats,
422 gettext_noop ("# connect requests issued to core"),
425 pos->connect_req = GNUNET_CORE_peer_request_connect (sched, cfg,
426 GNUNET_TIME_UNIT_MINUTES,
428 &connect_completed_callback,
434 * Discard peer entries for greylisted peers
435 * where the greylisting has expired.
437 * @param cls 'struct Peer' to greylist
438 * @param tc scheduler context
441 remove_from_greylist (void *cls,
442 const struct GNUNET_SCHEDULER_TaskContext *tc)
444 struct Peer *pos = cls;
445 struct GNUNET_TIME_Relative rem;
447 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
448 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
449 if (rem.rel_value == 0)
451 attempt_connect (pos);
455 pos->greylist_clean_task
456 = GNUNET_SCHEDULER_add_delayed (sched,
458 &remove_from_greylist,
461 if ( (GNUNET_NO == pos->is_friend) &&
462 (GNUNET_NO == pos->is_connected) )
464 free_peer (NULL, &pos->pid.hashPubKey, pos);
471 * Create a new entry in the peer list.
473 * @param peer identity of the new entry
474 * @param hello hello message, can be NULL
475 * @param is_friend is the new entry for a friend?
476 * @return the new entry
479 make_peer (const struct
480 GNUNET_PeerIdentity * peer,
481 const struct GNUNET_HELLO_Message *hello,
486 ret = GNUNET_malloc (sizeof (struct Peer));
488 ret->is_friend = is_friend;
491 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
492 memcpy (ret->hello, hello,
493 GNUNET_HELLO_size (hello));
495 GNUNET_break (GNUNET_OK ==
496 GNUNET_CONTAINER_multihashmap_put (peers,
499 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
505 * Setup bloom filter for the given peer entry.
507 * @param peer entry to initialize
510 setup_filter (struct Peer *peer)
512 /* 2^{-5} chance of not sending a HELLO to a peer is
513 acceptably small (if the filter is 50% full);
514 64 bytes of memory are small compared to the rest
515 of the data structure and would only really become
516 "useless" once a HELLO has been passed on to ~100
517 other peers, which is likely more than enough in
518 any case; hence 64, 5 as bloomfilter parameters. */
519 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
520 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
521 /* never send a peer its own HELLO */
522 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
527 * Function to fill send buffer with HELLO.
529 * @param cls 'struct Peer' of the target peer
530 * @param size number of bytes available in buf
531 * @param buf where the callee should write the message
532 * @return number of bytes written to buf
535 hello_advertising_ready (void *cls,
543 * Closure for 'find_advertisable_hello'.
545 struct FindAdvHelloContext {
548 * Peer we want to advertise to.
553 * Where to store the result (peer selected for advertising).
558 * Maximum HELLO size we can use right now.
562 struct GNUNET_TIME_Relative next_adv;
567 * Find a peer that would be reasonable for advertising.
570 * @param pid identity of a peer
571 * @param value 'struct Peer*' for the peer we are considering
572 * @return GNUNET_YES (continue iteration)
575 find_advertisable_hello (void *cls,
576 const GNUNET_HashCode *pid,
579 struct FindAdvHelloContext *fah = cls;
580 struct Peer *pos = value;
581 struct GNUNET_TIME_Relative rst_time;
584 if (pos == fah->peer)
586 if (pos->hello == NULL)
588 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
589 if (0 == rst_time.rel_value)
591 /* time to discard... */
592 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
595 fah->next_adv = GNUNET_TIME_relative_min (rst_time,
597 hs = GNUNET_HELLO_size (pos->hello);
598 if (hs > fah->max_size)
601 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
602 &fah->peer->pid.hashPubKey))
609 * Calculate when we would like to send the next HELLO to this
610 * peer and ask for it.
612 * @param cls for which peer to schedule the HELLO
613 * @param tc task context
616 schedule_next_hello (void *cls,
617 const struct GNUNET_SCHEDULER_TaskContext *tc)
619 struct Peer *pl = cls;
620 struct FindAdvHelloContext fah;
622 struct GNUNET_TIME_Relative delay;
624 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
625 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
626 return; /* we're out of here */
627 if (pl->hello_req != NULL)
628 return; /* did not finish sending the previous one */
629 /* find applicable HELLOs */
632 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
633 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
634 GNUNET_CONTAINER_multihashmap_iterate (peers,
635 &find_advertisable_hello,
638 = GNUNET_SCHEDULER_add_delayed (sched,
640 &schedule_next_hello,
642 if (fah.result == NULL)
644 next_want = GNUNET_HELLO_size (fah.result->hello);
645 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
646 if (delay.rel_value == 0)
649 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
650 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
653 &hello_advertising_ready,
660 * Cancel existing requests for sending HELLOs to this peer
661 * and recalculate when we should send HELLOs to it based
662 * on our current state (something changed!).
664 * @param cls closure, 'struct Peer' to skip, or NULL
665 * @param pid identity of a peer
666 * @param value 'struct Peer*' for the peer
667 * @return GNUNET_YES (always)
670 reschedule_hellos (void *cls,
671 const GNUNET_HashCode *pid,
674 struct Peer *peer = value;
675 struct Peer *skip = cls;
679 if (! peer->is_connected)
681 if (peer->hello_req != NULL)
683 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
684 peer->hello_req = NULL;
686 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
688 GNUNET_SCHEDULER_cancel (sched,
689 peer->hello_delay_task);
690 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
692 peer->hello_delay_task
693 = GNUNET_SCHEDULER_add_now (sched,
694 &schedule_next_hello,
701 * Method called whenever a peer connects.
704 * @param peer peer identity this notification is about
705 * @param latency reported latency of the connection with 'other'
706 * @param distance reported distance (DV) to 'other'
709 connect_notify (void *cls,
711 GNUNET_PeerIdentity * peer,
712 struct GNUNET_TIME_Relative latency,
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Core told us that we are connecting to `%s'\n",
723 GNUNET_STATISTICS_set (stats,
724 gettext_noop ("# peers connected"),
727 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
730 pos = make_peer (peer, NULL, GNUNET_NO);
731 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
735 GNUNET_assert (GNUNET_NO == pos->is_connected);
736 pos->greylisted_until.abs_value = 0; /* remove greylisting */
738 pos->is_connected = GNUNET_YES;
739 pos->connect_attempts = 0; /* re-set back-off factor */
742 if ( (friend_count == minimum_friend_count - 1) &&
743 (GNUNET_YES != friends_only) )
746 GNUNET_STATISTICS_set (stats,
747 gettext_noop ("# friends connected"),
751 reschedule_hellos (NULL, &peer->hashPubKey, pos);
756 * Try to add more peers to our connection set.
758 * @param cls closure, not used
759 * @param pid identity of a peer
760 * @param value 'struct Peer*' for the peer
761 * @return GNUNET_YES (continue to iterate)
764 try_add_peers (void *cls,
765 const GNUNET_HashCode *pid,
768 struct Peer *pos = value;
770 attempt_connect (pos);
776 * Method called whenever a peer disconnects.
779 * @param peer peer identity this notification is about
782 disconnect_notify (void *cls,
784 GNUNET_PeerIdentity * peer)
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Core told us that we disconnected from `%s'\n",
793 pos = GNUNET_CONTAINER_multihashmap_get (peers,
800 if (pos->is_connected != GNUNET_YES)
806 GNUNET_STATISTICS_set (stats,
807 gettext_noop ("# peers connected"),
813 GNUNET_STATISTICS_set (stats,
814 gettext_noop ("# friends connected"),
818 if ( (connection_count < target_connection_count) ||
819 (friend_count < minimum_friend_count) )
820 GNUNET_CONTAINER_multihashmap_iterate (peers,
823 if ( (friend_count < minimum_friend_count) &&
824 (blacklist == NULL) )
825 blacklist = GNUNET_TRANSPORT_blacklist (sched, cfg,
826 &blacklist_check, NULL);
831 * Iterator called on each address.
833 * @param cls flag that we will set if we see any addresses
834 * @param tname name of the transport
835 * @param expiration when will the given address expire
836 * @param addr the address of the peer
837 * @param addrlen number of bytes in addr
838 * @return GNUNET_SYSERR always, to terminate iteration
841 address_iterator (void *cls,
843 struct GNUNET_TIME_Absolute expiration,
849 return GNUNET_SYSERR;
854 * We've gotten a HELLO from another peer. Consider it for
857 * @param hello the HELLO we got
860 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
863 struct GNUNET_PeerIdentity pid;
864 struct GNUNET_TIME_Absolute dt;
865 struct GNUNET_HELLO_Message *nh;
869 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
874 if (0 == memcmp (&pid,
876 sizeof (struct GNUNET_PeerIdentity)))
877 return; /* that's me! */
878 have_address = GNUNET_NO;
879 GNUNET_HELLO_iterate_addresses (hello,
883 if (GNUNET_NO == have_address)
884 return; /* no point in advertising this one... */
885 peer = GNUNET_CONTAINER_multihashmap_get (peers,
889 peer = make_peer (&pid, hello, GNUNET_NO);
891 else if (peer->hello != NULL)
893 dt = GNUNET_HELLO_equals (peer->hello,
895 GNUNET_TIME_absolute_get());
896 if (dt.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
897 return; /* nothing new here */
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Found `%s' from peer `%s' for advertising\n",
905 if (peer->hello != NULL)
907 nh = GNUNET_HELLO_merge (peer->hello,
909 GNUNET_free (peer->hello);
914 size = GNUNET_HELLO_size (hello);
915 peer->hello = GNUNET_malloc (size);
916 memcpy (peer->hello, hello, size);
918 if (peer->filter != NULL)
919 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
921 /* since we have a new HELLO to pick from, re-schedule all
922 HELLO requests that are not bound by the HELLO send rate! */
923 GNUNET_CONTAINER_multihashmap_iterate (peers,
930 * PEERINFO calls this function to let us know about a possible peer
931 * that we might want to connect to.
933 * @param cls closure (not used)
934 * @param peer potential peer to connect to
935 * @param hello HELLO for this peer (or NULL)
938 process_peer (void *cls,
939 const struct GNUNET_PeerIdentity *peer,
940 const struct GNUNET_HELLO_Message *hello)
944 GNUNET_assert (peer != NULL);
945 if (0 == memcmp (&my_identity,
946 peer, sizeof (struct GNUNET_PeerIdentity)))
947 return; /* that's me! */
950 /* free existing HELLO, if any */
951 pos = GNUNET_CONTAINER_multihashmap_get (peers,
955 GNUNET_free_non_null (pos->hello);
957 if (pos->filter != NULL)
959 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
962 if ( (! pos->is_connected) &&
963 (! pos->is_friend) &&
964 (0 == GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value) )
965 free_peer (NULL, &pos->pid.hashPubKey, pos);
969 consider_for_advertising (hello);
970 pos = GNUNET_CONTAINER_multihashmap_get (peers,
973 pos = make_peer (peer, hello, GNUNET_NO);
974 GNUNET_assert (NULL != pos);
975 if (GNUNET_YES == pos->is_connected)
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Already connected to peer `%s'\n",
984 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988 "Already tried peer `%s' recently\n",
991 return; /* peer still greylisted */
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "Considering connecting to peer `%s'\n",
998 attempt_connect (pos);
1003 * Function called after GNUNET_CORE_connect has succeeded
1004 * (or failed for good).
1006 * @param cls closure
1007 * @param server handle to the server, NULL if we failed
1008 * @param my_id ID of this peer, NULL if we failed
1009 * @param publicKey public key of this peer, NULL if we failed
1012 core_init (void *cls,
1013 struct GNUNET_CORE_Handle * server,
1014 const struct GNUNET_PeerIdentity *
1017 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1022 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1023 _("Failed to connect to core service, can not manage topology!\n"));
1024 GNUNET_SCHEDULER_shutdown (sched);
1028 my_identity = *my_id;
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 GNUNET_i2s (my_id));
1034 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1041 * Read the friends file.
1044 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1049 struct GNUNET_PeerIdentity pid;
1051 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1052 unsigned int entries_found;
1056 GNUNET_CONFIGURATION_get_value_filename (cfg,
1061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1062 _("Option `%s' in section `%s' not specified!\n"),
1067 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1068 GNUNET_DISK_fn_write (fn, NULL, 0,
1069 GNUNET_DISK_PERM_USER_READ
1070 | GNUNET_DISK_PERM_USER_WRITE);
1071 if (0 != STAT (fn, &frstat))
1073 if ((friends_only) || (minimum_friend_count > 0))
1074 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1075 _("Could not read friends list `%s'\n"), fn);
1079 if (frstat.st_size == 0)
1081 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1082 _("Friends file `%s' is empty.\n"),
1087 data = GNUNET_malloc_large (frstat.st_size);
1090 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1091 _("Failed to read friends list from `%s': out of memory\n"), fn);
1095 if (frstat.st_size !=
1096 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1098 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1099 _("Failed to read friends list from `%s'\n"), fn);
1106 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1108 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1109 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1111 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1112 if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1115 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1116 (unsigned long long) pos);
1118 while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1122 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1123 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1125 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1126 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1127 (unsigned long long) pos,
1132 if (0 != memcmp (&pid,
1134 sizeof (struct GNUNET_PeerIdentity)))
1137 fl = make_peer (&pid,
1140 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1141 _("Found friend `%s' in configuration\n"),
1142 GNUNET_i2s (&fl->pid));
1146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1147 _("Found myself `%s' in friend list (useless, ignored)\n"),
1151 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1152 while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1157 GNUNET_STATISTICS_update (stats,
1158 gettext_noop ("# friends in configuration"),
1161 if ( (minimum_friend_count > entries_found) &&
1162 (friends_only == GNUNET_NO) )
1164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1165 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1167 if ( (minimum_friend_count > target_connection_count) &&
1168 (friends_only == GNUNET_NO) )
1170 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1171 _("More friendly connections required than target total number of connections.\n"));
1177 * This function is called whenever an encrypted HELLO message is
1180 * @param cls closure
1181 * @param other the other peer involved (sender or receiver, NULL
1182 * for loopback messages where we are both sender and receiver)
1183 * @param message the actual HELLO message
1184 * @param latency reported latency of the connection with 'other'
1185 * @param distance reported distance (DV) to 'other'
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 struct GNUNET_TIME_Relative latency,
1198 struct GNUNET_PeerIdentity pid;
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202 "Received encrypted `%s' from peer `%s'",
1204 GNUNET_i2s (other));
1207 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) message,
1210 GNUNET_break_op (0);
1211 return GNUNET_SYSERR;
1213 GNUNET_STATISTICS_update (stats,
1214 gettext_noop ("# HELLO messages received"),
1217 peer = GNUNET_CONTAINER_multihashmap_get (peers,
1221 if ( (GNUNET_YES == friends_only) ||
1222 (friend_count < minimum_friend_count) )
1227 if ( (GNUNET_YES != peer->is_friend) &&
1228 (GNUNET_YES == friends_only) )
1230 if ( (GNUNET_YES != peer->is_friend) &&
1231 (friend_count < minimum_friend_count) )
1234 if (transport != NULL)
1235 GNUNET_TRANSPORT_offer_hello (transport,
1242 * Function to fill send buffer with HELLO.
1244 * @param cls 'struct Peer' of the target peer
1245 * @param size number of bytes available in buf
1246 * @param buf where the callee should write the message
1247 * @return number of bytes written to buf
1250 hello_advertising_ready (void *cls,
1254 struct Peer *pl = cls;
1255 struct FindAdvHelloContext fah;
1258 pl->hello_req = NULL;
1259 /* find applicable HELLOs */
1262 fah.max_size = size;
1263 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1264 GNUNET_CONTAINER_multihashmap_iterate (peers,
1265 &find_advertisable_hello,
1268 if (fah.result != NULL)
1270 want = GNUNET_HELLO_size (fah.result->hello);
1271 GNUNET_assert (want <= size);
1272 memcpy (buf, fah.result->hello, want);
1273 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
1274 &pl->pid.hashPubKey);
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1277 "Sending `%s' with %u bytes",
1279 (unsigned int) want);
1281 GNUNET_STATISTICS_update (stats,
1282 gettext_noop ("# HELLO messages gossipped"),
1286 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1287 pl->hello_delay_task
1288 = GNUNET_SCHEDULER_add_now (sched,
1289 &schedule_next_hello,
1296 * Last task run during shutdown. Disconnects us from
1297 * the transport and core.
1299 * @param cls unused, NULL
1300 * @param tc scheduler context
1303 cleaning_task (void *cls,
1304 const struct GNUNET_SCHEDULER_TaskContext *tc)
1306 if (NULL != peerinfo_notify)
1308 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1309 peerinfo_notify = NULL;
1311 GNUNET_TRANSPORT_disconnect (transport);
1313 GNUNET_CONTAINER_multihashmap_iterate (peers,
1316 GNUNET_CONTAINER_multihashmap_destroy (peers);
1319 GNUNET_CORE_disconnect (handle);
1325 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1332 * Main function that will be run.
1334 * @param cls closure
1335 * @param s the scheduler to use
1336 * @param args remaining command-line arguments
1337 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1338 * @param c configuration
1342 struct GNUNET_SCHEDULER_Handle * s,
1344 const char *cfgfile,
1345 const struct GNUNET_CONFIGURATION_Handle * c)
1347 struct GNUNET_CORE_MessageHandler handlers[] =
1349 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1352 unsigned long long opt;
1356 stats = GNUNET_STATISTICS_create (sched, "topology", cfg);
1357 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1360 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1364 GNUNET_CONFIGURATION_get_value_number (cfg,
1369 minimum_friend_count = (unsigned int) opt;
1371 GNUNET_CONFIGURATION_get_value_number (cfg,
1373 "TARGET-CONNECTION-COUNT",
1376 target_connection_count = (unsigned int) opt;
1377 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1379 if ( (friends_only == GNUNET_YES) ||
1380 (minimum_friend_count > 0) )
1381 read_friends_file (cfg);
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1384 "Topology would like %u connections with at least %u friends (%s)\n",
1385 target_connection_count,
1386 minimum_friend_count,
1387 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1389 if (friend_count < minimum_friend_count)
1390 blacklist = GNUNET_TRANSPORT_blacklist (sched, cfg,
1391 &blacklist_check, NULL);
1392 transport = GNUNET_TRANSPORT_connect (sched,
1399 handle = GNUNET_CORE_connect (sched,
1401 GNUNET_TIME_UNIT_FOREVER_REL,
1410 GNUNET_SCHEDULER_add_delayed (sched,
1411 GNUNET_TIME_UNIT_FOREVER_REL,
1412 &cleaning_task, NULL);
1413 if (NULL == transport)
1415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1416 _("Failed to connect to `%s' service.\n"),
1418 GNUNET_SCHEDULER_shutdown (sched);
1423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1424 _("Failed to connect to `%s' service.\n"),
1426 GNUNET_SCHEDULER_shutdown (sched);
1433 * The main function for the topology daemon.
1435 * @param argc number of arguments from the command line
1436 * @param argv command line arguments
1437 * @return 0 ok, 1 on error
1440 main (int argc, char *const *argv)
1442 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1443 GNUNET_GETOPT_OPTION_END
1448 GNUNET_PROGRAM_run (argc,
1451 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1453 &run, NULL)) ? 0 : 1;
1457 /* end of gnunet-daemon-topology.c */