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_UNIT_HOURS
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, 15)
53 * How often do we at most advertise any HELLO to a peer?
55 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
58 * How often do we at most advertise the same HELLO to the same peer?
60 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
64 * Record for neighbours, friends and blacklisted peers.
69 * Which peer is this entry about?
71 struct GNUNET_PeerIdentity pid;
74 * Our handle for the request to transmit HELLOs to this peer; NULL
75 * if no such request is pending.
77 struct GNUNET_CORE_TransmitHandle *hello_req;
80 * Our handle for the request to connect to this peer; NULL if no
81 * such request is pending.
83 struct GNUNET_CORE_PeerRequestHandle *connect_req;
86 * Pointer to the HELLO message of this peer; can be NULL.
88 struct GNUNET_HELLO_Message *hello;
91 * Bloom filter used to mark which peers already got the HELLO
94 struct GNUNET_CONTAINER_BloomFilter *filter;
97 * Our request handle for *whitelisting* this peer (NULL if
98 * no whitelisting request is pending).
100 struct GNUNET_TRANSPORT_BlacklistRequest *wh;
103 * Until what time should we not try to connect again
106 struct GNUNET_TIME_Absolute greylisted_until;
109 * Next time we are allowed to transmit a HELLO to this peer?
111 struct GNUNET_TIME_Absolute next_hello_allowed;
114 * When should we reset the bloom filter of this entry?
116 struct GNUNET_TIME_Absolute filter_expiration;
119 * ID of task we use to wait for the time to send the next HELLO
122 GNUNET_SCHEDULER_TaskIdentifier hello_delay_task;
125 * ID of task we use to clear peers from the greylist.
127 GNUNET_SCHEDULER_TaskIdentifier greylist_clean_task;
130 * Is this peer listed here because he is a friend?
135 * Are we connected to this peer right now?
140 * Are we currently blocking this peer (via blacklist)?
148 * Entry in linked list of active 'disconnect' requests that we have issued.
150 struct DisconnectList
153 * This is a doubly-linked list.
155 struct DisconnectList *next;
158 * This is a doubly-linked list.
160 struct DisconnectList *prev;
163 * Our request handle.
165 struct GNUNET_TRANSPORT_BlacklistRequest *rh;
168 * Peer we tried to disconnect.
170 struct GNUNET_PeerIdentity peer;
176 * Our peerinfo notification context. We use notification
177 * to instantly learn about new peers as they are discovered.
179 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
184 static struct GNUNET_SCHEDULER_Handle *sched;
189 static const struct GNUNET_CONFIGURATION_Handle *cfg;
192 * Handle to the core API.
194 static struct GNUNET_CORE_Handle *handle;
197 * Handle to the transport API.
199 static struct GNUNET_TRANSPORT_Handle *transport;
202 * Identity of this peer.
204 static struct GNUNET_PeerIdentity my_identity;
207 * All of our friends, all of our current neighbours and all peers for
208 * which we have HELLOs. So pretty much everyone. Maps peer identities
209 * to 'struct Peer *' values.
211 static struct GNUNET_CONTAINER_MultiHashMap *peers;
214 * Handle for reporting statistics.
216 static struct GNUNET_STATISTICS_Handle *stats;
219 * Flag to disallow non-friend connections (pure F2F mode).
221 static int friends_only;
224 * Minimum number of friends to have in the
225 * connection set before we allow non-friends.
227 static unsigned int minimum_friend_count;
230 * Number of peers (friends and others) that we are currently connected to.
232 static unsigned int connection_count;
235 * Target number of connections.
237 static unsigned int target_connection_count;
240 * Number of friends that we are currently connected to.
242 static unsigned int friend_count;
245 * Should the topology daemon try to establish connections?
247 static int autoconnect;
250 * Head of doubly-linked list of active 'disconnect' requests that we have issued.
252 static struct DisconnectList *disconnect_head;
255 * Head of doubly-linked list of active 'disconnect' requests that we have issued.
257 static struct DisconnectList *disconnect_tail;
261 * Function called once our request to 'disconnect' a peer
264 * @param cls our 'struct DisconnectList'
268 disconnect_done (void *cls,
269 const struct GNUNET_SCHEDULER_TaskContext *tc)
271 struct DisconnectList *dl = cls;
273 GNUNET_STATISTICS_update (stats,
274 gettext_noop ("# peers blacklisted"),
277 GNUNET_CONTAINER_DLL_remove (disconnect_head,
285 * Force a disconnect from the specified peer.
287 * @param pl peer to disconnect
290 force_disconnect (struct Peer *pl)
292 const struct GNUNET_PeerIdentity *peer = &pl->pid;
293 struct DisconnectList *dl;
297 GNUNET_TRANSPORT_blacklist_cancel (pl->wh);
300 pl->is_blocked = GNUNET_YES;
301 dl = GNUNET_malloc (sizeof (struct DisconnectList));
303 GNUNET_CONTAINER_DLL_insert (disconnect_head,
306 dl->rh = GNUNET_TRANSPORT_blacklist (sched, cfg,
308 GNUNET_TIME_UNIT_FOREVER_REL,
309 GNUNET_TIME_UNIT_FOREVER_REL,
317 * Function called once our request to 'whitelist' a peer
320 * @param cls our 'struct Peer'
324 whitelist_done (void *cls,
325 const struct GNUNET_SCHEDULER_TaskContext *tc)
327 struct Peer *pl = cls;
330 GNUNET_STATISTICS_update (stats,
331 gettext_noop ("# peers blacklisted"),
338 * Whitelist the given peer (if it was blacklisted before).
340 * @param cls closure (not used)
341 * @param pid identity of the peer
342 * @param value peer to free
343 * @return GNUNET_YES (always: continue to iterate)
346 whitelist_peer (void *cls,
347 const GNUNET_HashCode *pid,
350 struct Peer *pl = value;
352 if (! pl->is_blocked)
354 pl->wh = GNUNET_TRANSPORT_blacklist (sched, cfg,
356 GNUNET_TIME_UNIT_ZERO,
357 GNUNET_TIME_UNIT_FOREVER_REL,
360 pl->is_blocked = GNUNET_NO;
366 * Whitelist all peers that we blacklisted; we've passed
367 * the minimum number of friends.
372 struct DisconnectList *dl;
374 /* first, cancel all blacklisting requests */
375 while (NULL != (dl = disconnect_head))
377 GNUNET_CONTAINER_DLL_remove (disconnect_head,
380 GNUNET_TRANSPORT_blacklist_cancel (dl->rh);
383 /* then, specifically whitelist all peers that we
384 know to have blacklisted */
385 GNUNET_CONTAINER_multihashmap_iterate (peers,
392 * Function called by core when our attempt to connect succeeded.
394 * @param cls the 'struct Peer' for which we issued the connect request
395 * @param tc scheduler context
398 connect_completed_callback (void *cls,
399 const struct GNUNET_SCHEDULER_TaskContext *tc)
401 struct Peer *pos = cls;
403 pos->connect_req = NULL;
408 * Check if an additional connection from the given peer is allowed.
410 * @param peer connection to check
411 * @return GNUNET_OK if the connection is allowed
414 is_connection_allowed (struct Peer *peer)
416 if (0 == memcmp (&my_identity,
418 sizeof (struct GNUNET_PeerIdentity)))
419 return GNUNET_SYSERR; /* disallow connections to self */
422 if (GNUNET_YES == friends_only)
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Determined that `%s' is not allowed to connect (not a friend)\n",
427 GNUNET_i2s (&peer->pid));
429 return GNUNET_SYSERR;
431 if (friend_count >= minimum_friend_count)
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
436 GNUNET_i2s (&peer->pid));
438 return GNUNET_SYSERR;
443 * Free all resources associated with the given peer.
445 * @param cls closure (not used)
446 * @param pid identity of the peer
447 * @param value peer to free
448 * @return GNUNET_YES (always: continue to iterate)
451 free_peer (void *cls,
452 const GNUNET_HashCode *pid,
455 struct Peer *pos = value;
457 GNUNET_break (GNUNET_OK ==
458 GNUNET_CONTAINER_multihashmap_remove (peers,
461 if (pos->hello_req != NULL)
462 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
464 GNUNET_TRANSPORT_blacklist_cancel (pos->wh);
465 if (pos->connect_req != NULL)
466 GNUNET_CORE_peer_request_connect_cancel (pos->connect_req);
467 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
468 GNUNET_SCHEDULER_cancel (sched,
469 pos->hello_delay_task);
470 GNUNET_free_non_null (pos->hello);
471 if (pos->filter != NULL)
472 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
479 * Discard peer entries for greylisted peers
480 * where the greylisting has expired.
482 * @param cls 'struct Peer' to greylist
483 * @param tc scheduler context
486 remove_from_greylist (void *cls,
487 const struct GNUNET_SCHEDULER_TaskContext *tc);
491 * Try to connect to the specified peer.
493 * @param pos peer to connect to
496 attempt_connect (struct Peer *pos)
498 struct GNUNET_TIME_Relative rem;
500 if ( (connection_count >= target_connection_count) &&
501 (friend_count >= minimum_friend_count) )
503 if (GNUNET_YES == pos->is_connected)
505 if (GNUNET_OK != is_connection_allowed (pos))
507 if (GNUNET_YES == pos->is_blocked)
509 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value > 0)
511 if (GNUNET_YES == pos->is_friend)
512 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
514 rem = GREYLIST_AFTER_ATTEMPT;
515 /* FIXME: do exponential back-off? */
516 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
517 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
518 GNUNET_SCHEDULER_cancel (sched,
519 pos->greylist_clean_task);
520 pos->greylist_clean_task
521 = GNUNET_SCHEDULER_add_delayed (sched,
523 &remove_from_greylist,
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "Asking to connect to `%s'\n",
528 GNUNET_i2s (&pos->pid));
530 GNUNET_STATISTICS_update (stats,
531 gettext_noop ("# connect requests issued to core"),
534 pos->connect_req = GNUNET_CORE_peer_request_connect (sched, cfg,
535 GNUNET_TIME_UNIT_MINUTES,
537 &connect_completed_callback,
543 * Discard peer entries for greylisted peers
544 * where the greylisting has expired.
546 * @param cls 'struct Peer' to greylist
547 * @param tc scheduler context
550 remove_from_greylist (void *cls,
551 const struct GNUNET_SCHEDULER_TaskContext *tc)
553 struct Peer *pos = cls;
554 struct GNUNET_TIME_Relative rem;
556 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
557 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
560 attempt_connect (pos);
564 pos->greylist_clean_task
565 = GNUNET_SCHEDULER_add_delayed (sched,
567 &remove_from_greylist,
570 if ( (GNUNET_NO == pos->is_friend) &&
571 (GNUNET_NO == pos->is_blocked) &&
572 (GNUNET_NO == pos->is_connected) )
574 free_peer (NULL, &pos->pid.hashPubKey, pos);
581 * Create a new entry in the peer list.
583 * @param peer identity of the new entry
584 * @param hello hello message, can be NULL
585 * @param is_friend is the new entry for a friend?
586 * @return the new entry
589 make_peer (const struct
590 GNUNET_PeerIdentity * peer,
591 const struct GNUNET_HELLO_Message *hello,
596 ret = GNUNET_malloc (sizeof (struct Peer));
598 ret->is_friend = is_friend;
601 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
602 memcpy (ret->hello, hello,
603 GNUNET_HELLO_size (hello));
605 GNUNET_CONTAINER_multihashmap_put (peers,
608 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
614 * Setup bloom filter for the given peer entry.
616 * @param peer entry to initialize
619 setup_filter (struct Peer *peer)
621 /* 2^{-5} chance of not sending a HELLO to a peer is
622 acceptably small (if the filter is 50% full);
623 64 bytes of memory are small compared to the rest
624 of the data structure and would only really become
625 "useless" once a HELLO has been passed on to ~100
626 other peers, which is likely more than enough in
627 any case; hence 64, 5 as bloomfilter parameters. */
628 peer->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
629 peer->filter_expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
630 /* never send a peer its own HELLO */
631 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
636 * Function to fill send buffer with HELLO.
638 * @param cls 'struct Peer' of the target peer
639 * @param size number of bytes available in buf
640 * @param buf where the callee should write the message
641 * @return number of bytes written to buf
644 hello_advertising_ready (void *cls,
652 * Closure for 'find_advertisable_hello'.
654 struct FindAdvHelloContext {
657 * Peer we want to advertise to.
662 * Where to store the result (peer selected for advertising).
667 * Maximum HELLO size we can use right now.
671 struct GNUNET_TIME_Relative next_adv;
676 * Find a peer that would be reasonable for advertising.
679 * @param pid identity of a peer
680 * @param value 'struct Peer*' for the peer we are considering
681 * @return GNUNET_YES (continue iteration)
684 find_advertisable_hello (void *cls,
685 const GNUNET_HashCode *pid,
688 struct FindAdvHelloContext *fah = cls;
689 struct Peer *pos = value;
690 struct GNUNET_TIME_Relative rst_time;
693 if (pos == fah->peer)
695 if (pos->hello == NULL)
697 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
698 if (0 == rst_time.value)
700 /* time to discard... */
701 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
704 fah->next_adv = GNUNET_TIME_relative_min (rst_time,
706 hs = GNUNET_HELLO_size (pos->hello);
707 if (hs > fah->max_size)
710 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
711 &fah->peer->pid.hashPubKey))
718 * Calculate when we would like to send the next HELLO to this
719 * peer and ask for it.
721 * @param cls for which peer to schedule the HELLO
722 * @param tc task context
725 schedule_next_hello (void *cls,
726 const struct GNUNET_SCHEDULER_TaskContext *tc)
728 struct Peer *pl = cls;
729 struct FindAdvHelloContext fah;
731 struct GNUNET_TIME_Relative delay;
733 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
734 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
735 return; /* we're out of here */
736 if (pl->hello_req != NULL)
737 return; /* did not finish sending the previous one */
738 /* find applicable HELLOs */
741 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE;
742 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
743 GNUNET_CONTAINER_multihashmap_iterate (peers,
744 &find_advertisable_hello,
747 = GNUNET_SCHEDULER_add_delayed (sched,
749 &schedule_next_hello,
751 if (fah.result == NULL)
753 next_want = GNUNET_HELLO_size (fah.result->hello);
754 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
755 if (delay.value == 0)
758 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle, 0,
759 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
762 &hello_advertising_ready,
770 * Cancel existing requests for sending HELLOs to this peer
771 * and recalculate when we should send HELLOs to it based
772 * on our current state (something changed!).
774 * @param cls closure, 'struct Peer' to skip, or NULL
775 * @param pid identity of a peer
776 * @param value 'struct Peer*' for the peer
777 * @return GNUNET_YES (always)
780 reschedule_hellos (void *cls,
781 const GNUNET_HashCode *pid,
784 struct Peer *peer = value;
785 struct Peer *skip = cls;
789 if (! peer->is_connected)
791 if (peer->hello_req != NULL)
793 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
794 peer->hello_req = NULL;
796 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
798 GNUNET_SCHEDULER_cancel (sched,
799 peer->hello_delay_task);
800 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
802 peer->hello_delay_task
803 = GNUNET_SCHEDULER_add_now (sched,
804 &schedule_next_hello,
811 * Method called whenever a peer connects.
814 * @param peer peer identity this notification is about
815 * @param latency reported latency of the connection with 'other'
816 * @param distance reported distance (DV) to 'other'
819 connect_notify (void *cls,
821 GNUNET_PeerIdentity * peer,
822 struct GNUNET_TIME_Relative latency,
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Core told us that we are connecting to `%s'\n",
833 GNUNET_STATISTICS_set (stats,
834 gettext_noop ("# peers connected"),
837 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
840 pos = make_peer (peer, NULL, GNUNET_NO);
841 if (GNUNET_OK != is_connection_allowed (pos))
843 GNUNET_assert (pos->is_friend == GNUNET_NO);
844 pos->is_connected = GNUNET_YES;
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
847 "Connection to `%s' is forbidden, forcing disconnect!\n",
850 force_disconnect (pos);
856 GNUNET_assert (GNUNET_NO == pos->is_connected);
857 pos->greylisted_until.value = 0; /* remove greylisting */
859 pos->is_connected = GNUNET_YES;
862 if ( (friend_count == minimum_friend_count - 1) &&
863 (GNUNET_YES != friends_only) )
866 GNUNET_STATISTICS_set (stats,
867 gettext_noop ("# friends connected"),
871 reschedule_hellos (NULL, &peer->hashPubKey, pos);
876 * Disconnect from all non-friends (we're below quota).
878 * @param cls closure, not used
879 * @param pid identity of a peer
880 * @param value 'struct Peer*' for the peer
881 * @return GNUNET_YES (continue to iterate)
884 drop_non_friends (void *cls,
885 const GNUNET_HashCode *pid,
888 struct Peer *pos = value;
890 if ( (GNUNET_NO == pos->is_friend) &&
891 (GNUNET_YES == pos->is_connected) )
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Connection to `%s' is not from a friend, forcing disconnect!\n",
896 GNUNET_i2s (&pos->pid));
898 force_disconnect (pos);
905 * Try to add more peers to our connection set.
907 * @param cls closure, not used
908 * @param pid identity of a peer
909 * @param value 'struct Peer*' for the peer
910 * @return GNUNET_YES (continue to iterate)
913 try_add_peers (void *cls,
914 const GNUNET_HashCode *pid,
917 struct Peer *pos = value;
919 attempt_connect (pos);
925 * Method called whenever a peer disconnects.
928 * @param peer peer identity this notification is about
931 disconnect_notify (void *cls,
933 GNUNET_PeerIdentity * peer)
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939 "Core told us that we disconnected from `%s'\n",
942 pos = GNUNET_CONTAINER_multihashmap_get (peers,
949 if (pos->is_connected != GNUNET_YES)
955 GNUNET_STATISTICS_set (stats,
956 gettext_noop ("# peers connected"),
962 GNUNET_STATISTICS_set (stats,
963 gettext_noop ("# friends connected"),
967 if ( (connection_count < target_connection_count) ||
968 (friend_count < minimum_friend_count) )
969 GNUNET_CONTAINER_multihashmap_iterate (peers,
972 if (friend_count < minimum_friend_count)
974 /* disconnect from all non-friends */
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977 "Not enough friendly connections, dropping all non-friend connections\n");
979 GNUNET_CONTAINER_multihashmap_iterate (peers,
987 * Iterator called on each address.
989 * @param cls flag that we will set if we see any addresses
990 * @param tname name of the transport
991 * @param expiration when will the given address expire
992 * @param addr the address of the peer
993 * @param addrlen number of bytes in addr
994 * @return GNUNET_SYSERR always, to terminate iteration
997 address_iterator (void *cls,
999 struct GNUNET_TIME_Absolute expiration,
1000 const void *addr, size_t addrlen)
1004 return GNUNET_SYSERR;
1009 * We've gotten a HELLO from another peer. Consider it for
1012 * @param hello the HELLO we got
1015 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
1018 struct GNUNET_PeerIdentity pid;
1022 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
1023 if (0 == memcmp (&pid,
1025 sizeof (struct GNUNET_PeerIdentity)))
1026 return; /* that's me! */
1027 have_address = GNUNET_NO;
1028 GNUNET_HELLO_iterate_addresses (hello,
1032 if (GNUNET_NO == have_address)
1033 return; /* no point in advertising this one... */
1034 peer = GNUNET_CONTAINER_multihashmap_get (peers,
1037 peer = make_peer (&pid, hello, GNUNET_NO);
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040 "Found `%s' from peer `%s' for advertising\n",
1044 size = GNUNET_HELLO_size (hello);
1045 GNUNET_free_non_null (peer->hello);
1046 peer->hello = GNUNET_malloc (size);
1047 memcpy (peer->hello, hello, size);
1048 if (peer->filter != NULL)
1049 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
1050 setup_filter (peer);
1051 /* since we have a new HELLO to pick from, re-schedule all
1052 HELLO requests that are not bound by the HELLO send rate! */
1053 GNUNET_CONTAINER_multihashmap_iterate (peers,
1060 * PEERINFO calls this function to let us know about a possible peer
1061 * that we might want to connect to.
1063 * @param cls closure (not used)
1064 * @param peer potential peer to connect to
1065 * @param hello HELLO for this peer (or NULL)
1066 * @param trust how much we trust the peer (not used)
1069 process_peer (void *cls,
1070 const struct GNUNET_PeerIdentity *peer,
1071 const struct GNUNET_HELLO_Message *hello,
1076 GNUNET_assert (peer != NULL);
1077 if (0 == memcmp (&my_identity,
1078 peer, sizeof (struct GNUNET_PeerIdentity)))
1079 return; /* that's me! */
1082 /* free existing HELLO, if any */
1083 pos = GNUNET_CONTAINER_multihashmap_get (peers,
1087 GNUNET_free_non_null (pos->hello);
1089 if (pos->filter != NULL)
1091 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
1094 if ( (! pos->is_connected) &&
1095 (! pos->is_friend) &&
1096 (0 == GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value) )
1097 free_peer (NULL, &pos->pid.hashPubKey, pos);
1101 consider_for_advertising (hello);
1102 pos = GNUNET_CONTAINER_multihashmap_get (peers,
1105 pos = make_peer (peer, hello, GNUNET_NO);
1106 GNUNET_assert (NULL != pos);
1107 if (GNUNET_YES == pos->is_connected)
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "Already connected to peer `%s'\n",
1116 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value > 0)
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 "Already tried peer `%s' recently\n",
1123 return; /* peer still greylisted */
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1127 "Considering connecting to peer `%s'\n",
1130 attempt_connect (pos);
1135 * Function called after GNUNET_CORE_connect has succeeded
1136 * (or failed for good).
1138 * @param cls closure
1139 * @param server handle to the server, NULL if we failed
1140 * @param my_id ID of this peer, NULL if we failed
1141 * @param publicKey public key of this peer, NULL if we failed
1144 core_init (void *cls,
1145 struct GNUNET_CORE_Handle * server,
1146 const struct GNUNET_PeerIdentity *
1149 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1155 _("Failed to connect to core service, can not manage topology!\n"));
1156 GNUNET_SCHEDULER_shutdown (sched);
1160 my_identity = *my_id;
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 GNUNET_i2s (my_id));
1166 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1173 * Read the friends file.
1176 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1181 struct GNUNET_PeerIdentity pid;
1183 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1184 unsigned int entries_found;
1188 GNUNET_CONFIGURATION_get_value_filename (cfg,
1193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1194 _("Option `%s' in section `%s' not specified!\n"),
1199 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1200 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1201 | GNUNET_DISK_PERM_USER_WRITE);
1202 if (0 != STAT (fn, &frstat))
1204 if ((friends_only) || (minimum_friend_count > 0))
1206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1207 _("Could not read friends list `%s'\n"), fn);
1212 if (frstat.st_size == 0)
1214 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1215 _("Friends file `%s' is empty.\n"),
1220 data = GNUNET_malloc_large (frstat.st_size);
1221 if (frstat.st_size !=
1222 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1225 _("Failed to read friends list from `%s'\n"), fn);
1232 while ((pos < frstat.st_size) && isspace (data[pos]))
1234 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1235 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1237 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1238 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1240 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1241 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1242 (unsigned long long) pos);
1244 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1248 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1249 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1252 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1253 (unsigned long long) pos,
1258 if (0 != memcmp (&pid,
1260 sizeof (struct GNUNET_PeerIdentity)))
1263 fl = make_peer (&pid,
1266 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1267 _("Found friend `%s' in configuration\n"),
1268 GNUNET_i2s (&fl->pid));
1272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1273 _("Found myself `%s' in friend list (useless, ignored)\n"),
1277 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1278 while ((pos < frstat.st_size) && isspace (data[pos]))
1283 GNUNET_STATISTICS_update (stats,
1284 gettext_noop ("# friends in configuration"),
1287 if ( (minimum_friend_count > entries_found) &&
1288 (friends_only == GNUNET_NO) )
1290 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1291 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1293 if ( (minimum_friend_count > target_connection_count) &&
1294 (friends_only == GNUNET_NO) )
1296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1297 _("More friendly connections required than target total number of connections.\n"));
1303 * This function is called whenever an encrypted HELLO message is
1306 * @param cls closure
1307 * @param other the other peer involved (sender or receiver, NULL
1308 * for loopback messages where we are both sender and receiver)
1309 * @param message the actual HELLO message
1310 * @param latency reported latency of the connection with 'other'
1311 * @param distance reported distance (DV) to 'other'
1312 * @return GNUNET_OK to keep the connection open,
1313 * GNUNET_SYSERR to close it (signal serious error)
1316 handle_encrypted_hello (void *cls,
1317 const struct GNUNET_PeerIdentity * other,
1318 const struct GNUNET_MessageHeader *
1320 struct GNUNET_TIME_Relative latency,
1324 struct GNUNET_PeerIdentity pid;
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1328 "Received encrypted `%s' from peer `%s'",
1330 GNUNET_i2s (other));
1333 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) message,
1336 GNUNET_break_op (0);
1337 return GNUNET_SYSERR;
1339 GNUNET_STATISTICS_update (stats,
1340 gettext_noop ("# HELLO messages received"),
1343 peer = GNUNET_CONTAINER_multihashmap_get (peers,
1345 if ( (peer != NULL) &&
1346 (peer->is_blocked) )
1347 return GNUNET_OK; /* ignore: currently blocked */
1348 if ( (GNUNET_YES == friends_only) &&
1350 (GNUNET_YES != peer->is_friend) ) )
1351 return GNUNET_OK; /* ignore: not a friend */
1352 if (transport != NULL)
1353 GNUNET_TRANSPORT_offer_hello (transport,
1360 * Function to fill send buffer with HELLO.
1362 * @param cls 'struct Peer' of the target peer
1363 * @param size number of bytes available in buf
1364 * @param buf where the callee should write the message
1365 * @return number of bytes written to buf
1368 hello_advertising_ready (void *cls,
1372 struct Peer *pl = cls;
1373 struct FindAdvHelloContext fah;
1376 pl->hello_req = NULL;
1377 /* find applicable HELLOs */
1380 fah.max_size = size;
1381 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1382 GNUNET_CONTAINER_multihashmap_iterate (peers,
1383 &find_advertisable_hello,
1386 if (fah.result != NULL)
1388 want = GNUNET_HELLO_size (fah.result->hello);
1389 GNUNET_assert (want <= size);
1390 memcpy (buf, fah.result->hello, want);
1391 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
1392 &pl->pid.hashPubKey);
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Sending `%s' with %u bytes",
1397 (unsigned int) want);
1399 GNUNET_STATISTICS_update (stats,
1400 gettext_noop ("# HELLO messages gossipped"),
1404 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1405 pl->hello_delay_task
1406 = GNUNET_SCHEDULER_add_now (sched,
1407 &schedule_next_hello,
1414 * Last task run during shutdown. Disconnects us from
1415 * the transport and core.
1417 * @param cls unused, NULL
1418 * @param tc scheduler context
1421 cleaning_task (void *cls,
1422 const struct GNUNET_SCHEDULER_TaskContext *tc)
1424 struct DisconnectList *dl;
1426 if (NULL != peerinfo_notify)
1428 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1429 peerinfo_notify = NULL;
1431 GNUNET_TRANSPORT_disconnect (transport);
1433 GNUNET_CONTAINER_multihashmap_iterate (peers,
1436 GNUNET_CONTAINER_multihashmap_destroy (peers);
1439 GNUNET_CORE_disconnect (handle);
1442 while (NULL != (dl = disconnect_head))
1444 GNUNET_CONTAINER_DLL_remove (disconnect_head,
1447 GNUNET_TRANSPORT_blacklist_cancel (dl->rh);
1452 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1459 * Main function that will be run.
1461 * @param cls closure
1462 * @param s the scheduler to use
1463 * @param args remaining command-line arguments
1464 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1465 * @param c configuration
1469 struct GNUNET_SCHEDULER_Handle * s,
1471 const char *cfgfile,
1472 const struct GNUNET_CONFIGURATION_Handle * c)
1474 struct GNUNET_CORE_MessageHandler handlers[] =
1476 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1479 unsigned long long opt;
1483 stats = GNUNET_STATISTICS_create (sched, "topology", cfg);
1484 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1487 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1491 GNUNET_CONFIGURATION_get_value_number (cfg,
1496 minimum_friend_count = (unsigned int) opt;
1498 GNUNET_CONFIGURATION_get_value_number (cfg,
1500 "TARGET-CONNECTION-COUNT",
1503 target_connection_count = (unsigned int) opt;
1504 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1506 if ( (friends_only == GNUNET_YES) ||
1507 (minimum_friend_count > 0) )
1508 read_friends_file (cfg);
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "Topology would like %u connections with at least %u friends (%s)\n",
1512 target_connection_count,
1513 minimum_friend_count,
1514 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1516 transport = GNUNET_TRANSPORT_connect (sched,
1522 handle = GNUNET_CORE_connect (sched,
1524 GNUNET_TIME_UNIT_FOREVER_REL,
1533 GNUNET_SCHEDULER_add_delayed (sched,
1534 GNUNET_TIME_UNIT_FOREVER_REL,
1535 &cleaning_task, NULL);
1536 if (NULL == transport)
1538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1539 _("Failed to connect to `%s' service.\n"),
1541 GNUNET_SCHEDULER_shutdown (sched);
1546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1547 _("Failed to connect to `%s' service.\n"),
1549 GNUNET_SCHEDULER_shutdown (sched);
1556 * gnunet-daemon-topology command line options.
1558 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1559 GNUNET_GETOPT_OPTION_END
1564 * The main function for the topology daemon.
1566 * @param argc number of arguments from the command line
1567 * @param argv command line arguments
1568 * @return 0 ok, 1 on error
1571 main (int argc, char *const *argv)
1576 GNUNET_PROGRAM_run (argc,
1579 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1581 &run, NULL)) ? 0 : 1;
1585 /* end of gnunet-daemon-topology.c */