2 This file is part of GNUnet.
3 (C) 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file topology/gnunet-daemon-topology.c
23 * @brief code for maintaining the mesh topology
24 * @author Christian Grothoff
29 #include "gnunet_constants.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_util_lib.h"
38 #define DEBUG_TOPOLOGY GNUNET_NO
41 * For how long do we blacklist a peer after a failed connection
44 #define GREYLIST_AFTER_ATTEMPT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 * For how long do we blacklist a friend after a failed connection
50 #define GREYLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
53 * For how long do we blacklist anyone under any cirumstances after a failed connection
56 #define GREYLIST_AFTER_ATTEMPT_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
59 * For how long do we blacklist anyone under any cirumstances after a failed connection
62 #define GREYLIST_AFTER_ATTEMPT_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 18)
65 * How often do we at most advertise any HELLO to a peer?
67 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
70 * How often do we at most advertise the same HELLO to the same peer?
72 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
76 * Record for neighbours, friends and blacklisted peers.
81 * Which peer is this entry about?
83 struct GNUNET_PeerIdentity pid;
86 * Our handle for the request to transmit HELLOs to this peer; NULL
87 * if no such request is pending.
89 struct GNUNET_CORE_TransmitHandle *hello_req;
92 * Our handle for the request to connect to this peer; NULL if no
93 * such request is pending.
95 struct GNUNET_CORE_PeerRequestHandle *connect_req;
98 * Pointer to the HELLO message of this peer; can be NULL.
100 struct GNUNET_HELLO_Message *hello;
103 * Bloom filter used to mark which peers already got the HELLO
106 struct GNUNET_CONTAINER_BloomFilter *filter;
109 * Our request handle for *whitelisting* this peer (NULL if
110 * no whitelisting request is pending).
112 struct GNUNET_TRANSPORT_BlacklistRequest *wh;
115 * Until what time should we not try to connect again
118 struct GNUNET_TIME_Absolute greylisted_until;
121 * Next time we are allowed to transmit a HELLO to this peer?
123 struct GNUNET_TIME_Absolute next_hello_allowed;
126 * When should we reset the bloom filter of this entry?
128 struct GNUNET_TIME_Absolute filter_expiration;
131 * ID of task we use to wait for the time to send the next HELLO
134 GNUNET_SCHEDULER_TaskIdentifier hello_delay_task;
137 * ID of task we use to clear peers from the greylist.
139 GNUNET_SCHEDULER_TaskIdentifier greylist_clean_task;
142 * How often have we tried so far?
144 unsigned int connect_attempts;
147 * Is this peer listed here because he is a friend?
152 * Are we connected to this peer right now?
157 * Are we currently blocking this peer (via blacklist)?
165 * Entry in linked list of active 'disconnect' requests that we have issued.
167 struct DisconnectList
170 * This is a doubly-linked list.
172 struct DisconnectList *next;
175 * This is a doubly-linked list.
177 struct DisconnectList *prev;
180 * Our request handle.
182 struct GNUNET_TRANSPORT_BlacklistRequest *rh;
185 * Peer we tried to disconnect.
187 struct GNUNET_PeerIdentity peer;
193 * Our peerinfo notification context. We use notification
194 * to instantly learn about new peers as they are discovered.
196 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
201 static struct GNUNET_SCHEDULER_Handle *sched;
206 static const struct GNUNET_CONFIGURATION_Handle *cfg;
209 * Handle to the core API.
211 static struct GNUNET_CORE_Handle *handle;
214 * Handle to the transport API.
216 static struct GNUNET_TRANSPORT_Handle *transport;
219 * Identity of this peer.
221 static struct GNUNET_PeerIdentity my_identity;
224 * All of our friends, all of our current neighbours and all peers for
225 * which we have HELLOs. So pretty much everyone. Maps peer identities
226 * to 'struct Peer *' values.
228 static struct GNUNET_CONTAINER_MultiHashMap *peers;
231 * Handle for reporting statistics.
233 static struct GNUNET_STATISTICS_Handle *stats;
236 * Flag to disallow non-friend connections (pure F2F mode).
238 static int friends_only;
241 * Minimum number of friends to have in the
242 * connection set before we allow non-friends.
244 static unsigned int minimum_friend_count;
247 * Number of peers (friends and others) that we are currently connected to.
249 static unsigned int connection_count;
252 * Target number of connections.
254 static unsigned int target_connection_count;
257 * Number of friends that we are currently connected to.
259 static unsigned int friend_count;
262 * Should the topology daemon try to establish connections?
264 static int autoconnect;
267 * Head of doubly-linked list of active 'disconnect' requests that we have issued.
269 static struct DisconnectList *disconnect_head;
272 * Head of doubly-linked list of active 'disconnect' requests that we have issued.
274 static struct DisconnectList *disconnect_tail;
278 * Function called once our request to 'disconnect' a peer
281 * @param cls our 'struct DisconnectList'
285 disconnect_done (void *cls,
286 const struct GNUNET_SCHEDULER_TaskContext *tc)
288 struct DisconnectList *dl = cls;
290 GNUNET_STATISTICS_update (stats,
291 gettext_noop ("# peers blacklisted"),
294 GNUNET_CONTAINER_DLL_remove (disconnect_head,
302 * Force a disconnect from the specified peer.
304 * @param pl peer to disconnect
307 force_disconnect (struct Peer *pl)
309 const struct GNUNET_PeerIdentity *peer = &pl->pid;
310 struct DisconnectList *dl;
314 GNUNET_TRANSPORT_blacklist_cancel (pl->wh);
317 pl->is_blocked = GNUNET_YES;
318 dl = GNUNET_malloc (sizeof (struct DisconnectList));
320 GNUNET_CONTAINER_DLL_insert (disconnect_head,
323 dl->rh = GNUNET_TRANSPORT_blacklist (sched, cfg,
325 GNUNET_TIME_UNIT_FOREVER_REL,
326 GNUNET_TIME_UNIT_FOREVER_REL,
334 * Function called once our request to 'whitelist' a peer
337 * @param cls our 'struct Peer'
341 whitelist_done (void *cls,
342 const struct GNUNET_SCHEDULER_TaskContext *tc)
344 struct Peer *pl = cls;
347 GNUNET_STATISTICS_update (stats,
348 gettext_noop ("# peers blacklisted"),
355 * Whitelist the given peer (if it was blacklisted before).
357 * @param cls closure (not used)
358 * @param pid identity of the peer
359 * @param value peer to free
360 * @return GNUNET_YES (always: continue to iterate)
363 whitelist_peer (void *cls,
364 const GNUNET_HashCode *pid,
367 struct Peer *pl = value;
369 if (! pl->is_blocked)
371 pl->wh = GNUNET_TRANSPORT_blacklist (sched, cfg,
373 GNUNET_TIME_UNIT_ZERO,
374 GNUNET_TIME_UNIT_FOREVER_REL,
377 pl->is_blocked = GNUNET_NO;
383 * Whitelist all peers that we blacklisted; we've passed
384 * the minimum number of friends.
389 struct DisconnectList *dl;
391 /* first, cancel all blacklisting requests */
392 while (NULL != (dl = disconnect_head))
394 GNUNET_CONTAINER_DLL_remove (disconnect_head,
397 GNUNET_TRANSPORT_blacklist_cancel (dl->rh);
400 /* then, specifically whitelist all peers that we
401 know to have blacklisted */
402 GNUNET_CONTAINER_multihashmap_iterate (peers,
409 * Function called by core when our attempt to connect succeeded.
411 * @param cls the 'struct Peer' for which we issued the connect request
412 * @param tc scheduler context
415 connect_completed_callback (void *cls,
416 const struct GNUNET_SCHEDULER_TaskContext *tc)
418 struct Peer *pos = cls;
420 pos->connect_req = NULL;
425 * Check if an additional connection from the given peer is allowed.
427 * @param peer connection to check
428 * @return GNUNET_OK if the connection is allowed
431 is_connection_allowed (struct Peer *peer)
433 if (0 == memcmp (&my_identity,
435 sizeof (struct GNUNET_PeerIdentity)))
436 return GNUNET_SYSERR; /* disallow connections to self */
439 if (GNUNET_YES == friends_only)
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Determined that `%s' is not allowed to connect (not a friend)\n",
444 GNUNET_i2s (&peer->pid));
446 return GNUNET_SYSERR;
448 if (friend_count >= minimum_friend_count)
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
453 GNUNET_i2s (&peer->pid));
455 return GNUNET_SYSERR;
460 * Free all resources associated with the given peer.
462 * @param cls closure (not used)
463 * @param pid identity of the peer
464 * @param value peer to free
465 * @return GNUNET_YES (always: continue to iterate)
468 free_peer (void *cls,
469 const GNUNET_HashCode *pid,
472 struct Peer *pos = value;
474 GNUNET_break (GNUNET_OK ==
475 GNUNET_CONTAINER_multihashmap_remove (peers,
478 if (pos->hello_req != NULL)
479 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
481 GNUNET_TRANSPORT_blacklist_cancel (pos->wh);
482 if (pos->connect_req != NULL)
483 GNUNET_CORE_peer_request_connect_cancel (pos->connect_req);
484 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
485 GNUNET_SCHEDULER_cancel (sched,
486 pos->hello_delay_task);
487 GNUNET_free_non_null (pos->hello);
488 if (pos->filter != NULL)
489 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
496 * Discard peer entries for greylisted peers
497 * where the greylisting has expired.
499 * @param cls 'struct Peer' to greylist
500 * @param tc scheduler context
503 remove_from_greylist (void *cls,
504 const struct GNUNET_SCHEDULER_TaskContext *tc);
508 * Try to connect to the specified peer.
510 * @param pos peer to connect to
513 attempt_connect (struct Peer *pos)
515 struct GNUNET_TIME_Relative rem;
517 if ( (connection_count >= target_connection_count) &&
518 (friend_count >= minimum_friend_count) )
520 if (GNUNET_YES == pos->is_connected)
522 if (GNUNET_OK != is_connection_allowed (pos))
524 if (GNUNET_YES == pos->is_blocked)
526 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value > 0)
528 if (GNUNET_YES == pos->is_friend)
529 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
531 rem = GREYLIST_AFTER_ATTEMPT;
532 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
533 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
534 if (pos->connect_attempts > 30)
535 pos->connect_attempts = 30;
536 rem = GNUNET_TIME_relative_multiply (rem, 1 << (++pos->connect_attempts));
537 rem = GNUNET_TIME_relative_max (rem,
538 GREYLIST_AFTER_ATTEMPT_MIN);
539 rem = GNUNET_TIME_relative_min (rem,
540 GREYLIST_AFTER_ATTEMPT_MAX);
541 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
542 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
543 GNUNET_SCHEDULER_cancel (sched,
544 pos->greylist_clean_task);
545 pos->greylist_clean_task
546 = GNUNET_SCHEDULER_add_delayed (sched,
548 &remove_from_greylist,
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "Asking to connect to `%s'\n",
553 GNUNET_i2s (&pos->pid));
555 GNUNET_STATISTICS_update (stats,
556 gettext_noop ("# connect requests issued to core"),
559 pos->connect_req = GNUNET_CORE_peer_request_connect (sched, cfg,
560 GNUNET_TIME_UNIT_MINUTES,
562 &connect_completed_callback,
568 * Discard peer entries for greylisted peers
569 * where the greylisting has expired.
571 * @param cls 'struct Peer' to greylist
572 * @param tc scheduler context
575 remove_from_greylist (void *cls,
576 const struct GNUNET_SCHEDULER_TaskContext *tc)
578 struct Peer *pos = cls;
579 struct GNUNET_TIME_Relative rem;
581 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
582 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
585 attempt_connect (pos);
589 pos->greylist_clean_task
590 = GNUNET_SCHEDULER_add_delayed (sched,
592 &remove_from_greylist,
595 if ( (GNUNET_NO == pos->is_friend) &&
596 (GNUNET_NO == pos->is_blocked) &&
597 (GNUNET_NO == pos->is_connected) )
599 free_peer (NULL, &pos->pid.hashPubKey, pos);
606 * Create a new entry in the peer list.
608 * @param peer identity of the new entry
609 * @param hello hello message, can be NULL
610 * @param is_friend is the new entry for a friend?
611 * @return the new entry
614 make_peer (const struct
615 GNUNET_PeerIdentity * peer,
616 const struct GNUNET_HELLO_Message *hello,
621 ret = GNUNET_malloc (sizeof (struct Peer));
623 ret->is_friend = is_friend;
626 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
627 memcpy (ret->hello, hello,
628 GNUNET_HELLO_size (hello));
630 GNUNET_CONTAINER_multihashmap_put (peers,
633 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
639 * Setup bloom filter for the given peer entry.
641 * @param peer entry to initialize
644 setup_filter (struct Peer *peer)
646 /* 2^{-5} chance of not sending a HELLO to a peer is
647 acceptably small (if the filter is 50% full);
648 64 bytes of memory are small compared to the rest
649 of the data structure and would only really become
650 "useless" once a HELLO has been passed on to ~100
651 other peers, which is likely more than enough in
652 any case; hence 64, 5 as bloomfilter parameters. */
653 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
654 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
655 /* never send a peer its own HELLO */
656 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
661 * Function to fill send buffer with HELLO.
663 * @param cls 'struct Peer' of the target peer
664 * @param size number of bytes available in buf
665 * @param buf where the callee should write the message
666 * @return number of bytes written to buf
669 hello_advertising_ready (void *cls,
677 * Closure for 'find_advertisable_hello'.
679 struct FindAdvHelloContext {
682 * Peer we want to advertise to.
687 * Where to store the result (peer selected for advertising).
692 * Maximum HELLO size we can use right now.
696 struct GNUNET_TIME_Relative next_adv;
701 * Find a peer that would be reasonable for advertising.
704 * @param pid identity of a peer
705 * @param value 'struct Peer*' for the peer we are considering
706 * @return GNUNET_YES (continue iteration)
709 find_advertisable_hello (void *cls,
710 const GNUNET_HashCode *pid,
713 struct FindAdvHelloContext *fah = cls;
714 struct Peer *pos = value;
715 struct GNUNET_TIME_Relative rst_time;
718 if (pos == fah->peer)
720 if (pos->hello == NULL)
722 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
723 if (0 == rst_time.value)
725 /* time to discard... */
726 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
729 fah->next_adv = GNUNET_TIME_relative_min (rst_time,
731 hs = GNUNET_HELLO_size (pos->hello);
732 if (hs > fah->max_size)
735 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
736 &fah->peer->pid.hashPubKey))
743 * Calculate when we would like to send the next HELLO to this
744 * peer and ask for it.
746 * @param cls for which peer to schedule the HELLO
747 * @param tc task context
750 schedule_next_hello (void *cls,
751 const struct GNUNET_SCHEDULER_TaskContext *tc)
753 struct Peer *pl = cls;
754 struct FindAdvHelloContext fah;
756 struct GNUNET_TIME_Relative delay;
758 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
759 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
760 return; /* we're out of here */
761 if (pl->hello_req != NULL)
762 return; /* did not finish sending the previous one */
763 /* find applicable HELLOs */
766 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE;
767 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
768 GNUNET_CONTAINER_multihashmap_iterate (peers,
769 &find_advertisable_hello,
772 = GNUNET_SCHEDULER_add_delayed (sched,
774 &schedule_next_hello,
776 if (fah.result == NULL)
778 next_want = GNUNET_HELLO_size (fah.result->hello);
779 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
780 if (delay.value == 0)
783 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
784 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
787 &hello_advertising_ready,
795 * Cancel existing requests for sending HELLOs to this peer
796 * and recalculate when we should send HELLOs to it based
797 * on our current state (something changed!).
799 * @param cls closure, 'struct Peer' to skip, or NULL
800 * @param pid identity of a peer
801 * @param value 'struct Peer*' for the peer
802 * @return GNUNET_YES (always)
805 reschedule_hellos (void *cls,
806 const GNUNET_HashCode *pid,
809 struct Peer *peer = value;
810 struct Peer *skip = cls;
814 if (! peer->is_connected)
816 if (peer->hello_req != NULL)
818 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
819 peer->hello_req = NULL;
821 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
823 GNUNET_SCHEDULER_cancel (sched,
824 peer->hello_delay_task);
825 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
827 peer->hello_delay_task
828 = GNUNET_SCHEDULER_add_now (sched,
829 &schedule_next_hello,
836 * Method called whenever a peer connects.
839 * @param peer peer identity this notification is about
840 * @param latency reported latency of the connection with 'other'
841 * @param distance reported distance (DV) to 'other'
844 connect_notify (void *cls,
846 GNUNET_PeerIdentity * peer,
847 struct GNUNET_TIME_Relative latency,
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "Core told us that we are connecting to `%s'\n",
858 GNUNET_STATISTICS_set (stats,
859 gettext_noop ("# peers connected"),
862 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
865 pos = make_peer (peer, NULL, GNUNET_NO);
866 if (GNUNET_OK != is_connection_allowed (pos))
868 GNUNET_assert (pos->is_friend == GNUNET_NO);
869 pos->is_connected = GNUNET_YES;
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "Connection to `%s' is forbidden, forcing disconnect!\n",
875 force_disconnect (pos);
881 GNUNET_assert (GNUNET_NO == pos->is_connected);
882 pos->greylisted_until.value = 0; /* remove greylisting */
884 pos->is_connected = GNUNET_YES;
885 pos->connect_attempts = 0; /* re-set back-off factor */
888 if ( (friend_count == minimum_friend_count - 1) &&
889 (GNUNET_YES != friends_only) )
892 GNUNET_STATISTICS_set (stats,
893 gettext_noop ("# friends connected"),
897 reschedule_hellos (NULL, &peer->hashPubKey, pos);
902 * Disconnect from all non-friends (we're below quota).
904 * @param cls closure, not used
905 * @param pid identity of a peer
906 * @param value 'struct Peer*' for the peer
907 * @return GNUNET_YES (continue to iterate)
910 drop_non_friends (void *cls,
911 const GNUNET_HashCode *pid,
914 struct Peer *pos = value;
916 if ( (GNUNET_NO == pos->is_friend) &&
917 (GNUNET_YES == pos->is_connected) )
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Connection to `%s' is not from a friend, forcing disconnect!\n",
922 GNUNET_i2s (&pos->pid));
924 force_disconnect (pos);
931 * Try to add more peers to our connection set.
933 * @param cls closure, not used
934 * @param pid identity of a peer
935 * @param value 'struct Peer*' for the peer
936 * @return GNUNET_YES (continue to iterate)
939 try_add_peers (void *cls,
940 const GNUNET_HashCode *pid,
943 struct Peer *pos = value;
945 attempt_connect (pos);
951 * Method called whenever a peer disconnects.
954 * @param peer peer identity this notification is about
957 disconnect_notify (void *cls,
959 GNUNET_PeerIdentity * peer)
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "Core told us that we disconnected from `%s'\n",
968 pos = GNUNET_CONTAINER_multihashmap_get (peers,
975 if (pos->is_connected != GNUNET_YES)
981 GNUNET_STATISTICS_set (stats,
982 gettext_noop ("# peers connected"),
988 GNUNET_STATISTICS_set (stats,
989 gettext_noop ("# friends connected"),
993 if ( (connection_count < target_connection_count) ||
994 (friend_count < minimum_friend_count) )
995 GNUNET_CONTAINER_multihashmap_iterate (peers,
998 if (friend_count < minimum_friend_count)
1000 /* disconnect from all non-friends */
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 "Not enough friendly connections, dropping all non-friend connections\n");
1005 GNUNET_CONTAINER_multihashmap_iterate (peers,
1013 * Iterator called on each address.
1015 * @param cls flag that we will set if we see any addresses
1016 * @param tname name of the transport
1017 * @param expiration when will the given address expire
1018 * @param addr the address of the peer
1019 * @param addrlen number of bytes in addr
1020 * @return GNUNET_SYSERR always, to terminate iteration
1023 address_iterator (void *cls,
1025 struct GNUNET_TIME_Absolute expiration,
1026 const void *addr, size_t addrlen)
1030 return GNUNET_SYSERR;
1035 * We've gotten a HELLO from another peer. Consider it for
1038 * @param hello the HELLO we got
1041 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
1044 struct GNUNET_PeerIdentity pid;
1045 struct GNUNET_TIME_Absolute dt;
1046 struct GNUNET_HELLO_Message *nh;
1050 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
1051 if (0 == memcmp (&pid,
1053 sizeof (struct GNUNET_PeerIdentity)))
1054 return; /* that's me! */
1055 have_address = GNUNET_NO;
1056 GNUNET_HELLO_iterate_addresses (hello,
1060 if (GNUNET_NO == have_address)
1061 return; /* no point in advertising this one... */
1062 peer = GNUNET_CONTAINER_multihashmap_get (peers,
1066 peer = make_peer (&pid, hello, GNUNET_NO);
1068 else if (peer->hello != NULL)
1070 dt = GNUNET_HELLO_equals (peer->hello,
1072 GNUNET_TIME_absolute_get());
1073 if (dt.value == GNUNET_TIME_UNIT_FOREVER_ABS.value)
1074 return; /* nothing new here */
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "Found `%s' from peer `%s' for advertising\n",
1082 if (peer->hello != NULL)
1084 nh = GNUNET_HELLO_merge (peer->hello,
1086 GNUNET_free (peer->hello);
1091 size = GNUNET_HELLO_size (hello);
1092 peer->hello = GNUNET_malloc (size);
1093 memcpy (peer->hello, hello, size);
1095 if (peer->filter != NULL)
1096 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
1097 setup_filter (peer);
1098 /* since we have a new HELLO to pick from, re-schedule all
1099 HELLO requests that are not bound by the HELLO send rate! */
1100 GNUNET_CONTAINER_multihashmap_iterate (peers,
1107 * PEERINFO calls this function to let us know about a possible peer
1108 * that we might want to connect to.
1110 * @param cls closure (not used)
1111 * @param peer potential peer to connect to
1112 * @param hello HELLO for this peer (or NULL)
1113 * @param trust how much we trust the peer (not used)
1116 process_peer (void *cls,
1117 const struct GNUNET_PeerIdentity *peer,
1118 const struct GNUNET_HELLO_Message *hello,
1123 GNUNET_assert (peer != NULL);
1124 if (0 == memcmp (&my_identity,
1125 peer, sizeof (struct GNUNET_PeerIdentity)))
1126 return; /* that's me! */
1129 /* free existing HELLO, if any */
1130 pos = GNUNET_CONTAINER_multihashmap_get (peers,
1134 GNUNET_free_non_null (pos->hello);
1136 if (pos->filter != NULL)
1138 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
1141 if ( (! pos->is_connected) &&
1142 (! pos->is_friend) &&
1143 (0 == GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value) )
1144 free_peer (NULL, &pos->pid.hashPubKey, pos);
1148 consider_for_advertising (hello);
1149 pos = GNUNET_CONTAINER_multihashmap_get (peers,
1152 pos = make_peer (peer, hello, GNUNET_NO);
1153 GNUNET_assert (NULL != pos);
1154 if (GNUNET_YES == pos->is_connected)
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "Already connected to peer `%s'\n",
1163 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value > 0)
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 "Already tried peer `%s' recently\n",
1170 return; /* peer still greylisted */
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "Considering connecting to peer `%s'\n",
1177 attempt_connect (pos);
1182 * Function called after GNUNET_CORE_connect has succeeded
1183 * (or failed for good).
1185 * @param cls closure
1186 * @param server handle to the server, NULL if we failed
1187 * @param my_id ID of this peer, NULL if we failed
1188 * @param publicKey public key of this peer, NULL if we failed
1191 core_init (void *cls,
1192 struct GNUNET_CORE_Handle * server,
1193 const struct GNUNET_PeerIdentity *
1196 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1202 _("Failed to connect to core service, can not manage topology!\n"));
1203 GNUNET_SCHEDULER_shutdown (sched);
1207 my_identity = *my_id;
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 GNUNET_i2s (my_id));
1213 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1220 * Read the friends file.
1223 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1228 struct GNUNET_PeerIdentity pid;
1230 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1231 unsigned int entries_found;
1235 GNUNET_CONFIGURATION_get_value_filename (cfg,
1240 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1241 _("Option `%s' in section `%s' not specified!\n"),
1246 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1247 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1248 | GNUNET_DISK_PERM_USER_WRITE);
1249 if (0 != STAT (fn, &frstat))
1251 if ((friends_only) || (minimum_friend_count > 0))
1253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1254 _("Could not read friends list `%s'\n"), fn);
1259 if (frstat.st_size == 0)
1261 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1262 _("Friends file `%s' is empty.\n"),
1267 data = GNUNET_malloc_large (frstat.st_size);
1268 if (frstat.st_size !=
1269 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1272 _("Failed to read friends list from `%s'\n"), fn);
1279 while ((pos < frstat.st_size) && isspace (data[pos]))
1281 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1282 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1284 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1285 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1287 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1288 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1289 (unsigned long long) pos);
1291 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1295 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1296 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1299 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1300 (unsigned long long) pos,
1305 if (0 != memcmp (&pid,
1307 sizeof (struct GNUNET_PeerIdentity)))
1310 fl = make_peer (&pid,
1313 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1314 _("Found friend `%s' in configuration\n"),
1315 GNUNET_i2s (&fl->pid));
1319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1320 _("Found myself `%s' in friend list (useless, ignored)\n"),
1324 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1325 while ((pos < frstat.st_size) && isspace (data[pos]))
1330 GNUNET_STATISTICS_update (stats,
1331 gettext_noop ("# friends in configuration"),
1334 if ( (minimum_friend_count > entries_found) &&
1335 (friends_only == GNUNET_NO) )
1337 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1338 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1340 if ( (minimum_friend_count > target_connection_count) &&
1341 (friends_only == GNUNET_NO) )
1343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1344 _("More friendly connections required than target total number of connections.\n"));
1350 * This function is called whenever an encrypted HELLO message is
1353 * @param cls closure
1354 * @param other the other peer involved (sender or receiver, NULL
1355 * for loopback messages where we are both sender and receiver)
1356 * @param message the actual HELLO message
1357 * @param latency reported latency of the connection with 'other'
1358 * @param distance reported distance (DV) to 'other'
1359 * @return GNUNET_OK to keep the connection open,
1360 * GNUNET_SYSERR to close it (signal serious error)
1363 handle_encrypted_hello (void *cls,
1364 const struct GNUNET_PeerIdentity * other,
1365 const struct GNUNET_MessageHeader *
1367 struct GNUNET_TIME_Relative latency,
1371 struct GNUNET_PeerIdentity pid;
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 "Received encrypted `%s' from peer `%s'",
1377 GNUNET_i2s (other));
1380 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) message,
1383 GNUNET_break_op (0);
1384 return GNUNET_SYSERR;
1386 GNUNET_STATISTICS_update (stats,
1387 gettext_noop ("# HELLO messages received"),
1390 peer = GNUNET_CONTAINER_multihashmap_get (peers,
1392 if ( (peer != NULL) &&
1393 (peer->is_blocked) )
1394 return GNUNET_OK; /* ignore: currently blocked */
1395 if ( (GNUNET_YES == friends_only) &&
1397 (GNUNET_YES != peer->is_friend) ) )
1398 return GNUNET_OK; /* ignore: not a friend */
1399 if (transport != NULL)
1400 GNUNET_TRANSPORT_offer_hello (transport,
1407 * Function to fill send buffer with HELLO.
1409 * @param cls 'struct Peer' of the target peer
1410 * @param size number of bytes available in buf
1411 * @param buf where the callee should write the message
1412 * @return number of bytes written to buf
1415 hello_advertising_ready (void *cls,
1419 struct Peer *pl = cls;
1420 struct FindAdvHelloContext fah;
1423 pl->hello_req = NULL;
1424 /* find applicable HELLOs */
1427 fah.max_size = size;
1428 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1429 GNUNET_CONTAINER_multihashmap_iterate (peers,
1430 &find_advertisable_hello,
1433 if (fah.result != NULL)
1435 want = GNUNET_HELLO_size (fah.result->hello);
1436 GNUNET_assert (want <= size);
1437 memcpy (buf, fah.result->hello, want);
1438 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
1439 &pl->pid.hashPubKey);
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Sending `%s' with %u bytes",
1444 (unsigned int) want);
1446 GNUNET_STATISTICS_update (stats,
1447 gettext_noop ("# HELLO messages gossipped"),
1451 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1452 pl->hello_delay_task
1453 = GNUNET_SCHEDULER_add_now (sched,
1454 &schedule_next_hello,
1461 * Last task run during shutdown. Disconnects us from
1462 * the transport and core.
1464 * @param cls unused, NULL
1465 * @param tc scheduler context
1468 cleaning_task (void *cls,
1469 const struct GNUNET_SCHEDULER_TaskContext *tc)
1471 struct DisconnectList *dl;
1473 if (NULL != peerinfo_notify)
1475 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1476 peerinfo_notify = NULL;
1478 GNUNET_TRANSPORT_disconnect (transport);
1480 GNUNET_CONTAINER_multihashmap_iterate (peers,
1483 GNUNET_CONTAINER_multihashmap_destroy (peers);
1486 GNUNET_CORE_disconnect (handle);
1489 while (NULL != (dl = disconnect_head))
1491 GNUNET_CONTAINER_DLL_remove (disconnect_head,
1494 GNUNET_TRANSPORT_blacklist_cancel (dl->rh);
1499 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1506 * Main function that will be run.
1508 * @param cls closure
1509 * @param s the scheduler to use
1510 * @param args remaining command-line arguments
1511 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1512 * @param c configuration
1516 struct GNUNET_SCHEDULER_Handle * s,
1518 const char *cfgfile,
1519 const struct GNUNET_CONFIGURATION_Handle * c)
1521 struct GNUNET_CORE_MessageHandler handlers[] =
1523 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1526 unsigned long long opt;
1530 stats = GNUNET_STATISTICS_create (sched, "topology", cfg);
1531 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1534 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1538 GNUNET_CONFIGURATION_get_value_number (cfg,
1543 minimum_friend_count = (unsigned int) opt;
1545 GNUNET_CONFIGURATION_get_value_number (cfg,
1547 "TARGET-CONNECTION-COUNT",
1550 target_connection_count = (unsigned int) opt;
1551 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1553 if ( (friends_only == GNUNET_YES) ||
1554 (minimum_friend_count > 0) )
1555 read_friends_file (cfg);
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1558 "Topology would like %u connections with at least %u friends (%s)\n",
1559 target_connection_count,
1560 minimum_friend_count,
1561 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1563 transport = GNUNET_TRANSPORT_connect (sched,
1569 handle = GNUNET_CORE_connect (sched,
1571 GNUNET_TIME_UNIT_FOREVER_REL,
1580 GNUNET_SCHEDULER_add_delayed (sched,
1581 GNUNET_TIME_UNIT_FOREVER_REL,
1582 &cleaning_task, NULL);
1583 if (NULL == transport)
1585 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1586 _("Failed to connect to `%s' service.\n"),
1588 GNUNET_SCHEDULER_shutdown (sched);
1593 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1594 _("Failed to connect to `%s' service.\n"),
1596 GNUNET_SCHEDULER_shutdown (sched);
1603 * gnunet-daemon-topology command line options.
1605 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1606 GNUNET_GETOPT_OPTION_END
1611 * The main function for the topology daemon.
1613 * @param argc number of arguments from the command line
1614 * @param argv command line arguments
1615 * @return 0 ok, 1 on error
1618 main (int argc, char *const *argv)
1623 GNUNET_PROGRAM_run (argc,
1626 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1628 &run, NULL)) ? 0 : 1;
1632 /* end of gnunet-daemon-topology.c */