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;
1019 struct GNUNET_TIME_Absolute dt;
1020 struct GNUNET_HELLO_Message *nh;
1024 GNUNET_break (GNUNET_OK == GNUNET_HELLO_get_id (hello, &pid));
1025 if (0 == memcmp (&pid,
1027 sizeof (struct GNUNET_PeerIdentity)))
1028 return; /* that's me! */
1029 have_address = GNUNET_NO;
1030 GNUNET_HELLO_iterate_addresses (hello,
1034 if (GNUNET_NO == have_address)
1035 return; /* no point in advertising this one... */
1036 peer = GNUNET_CONTAINER_multihashmap_get (peers,
1040 peer = make_peer (&pid, hello, GNUNET_NO);
1042 else if (peer->hello != NULL)
1044 dt = GNUNET_HELLO_equals (peer->hello,
1046 GNUNET_TIME_absolute_get());
1047 if (dt.value == GNUNET_TIME_UNIT_FOREVER_ABS.value)
1048 return; /* nothing new here */
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "Found `%s' from peer `%s' for advertising\n",
1056 if (peer->hello != NULL)
1058 nh = GNUNET_HELLO_merge (peer->hello,
1060 GNUNET_free (peer->hello);
1065 size = GNUNET_HELLO_size (hello);
1066 peer->hello = GNUNET_malloc (size);
1067 memcpy (peer->hello, hello, size);
1069 if (peer->filter != NULL)
1070 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
1071 setup_filter (peer);
1072 /* since we have a new HELLO to pick from, re-schedule all
1073 HELLO requests that are not bound by the HELLO send rate! */
1074 GNUNET_CONTAINER_multihashmap_iterate (peers,
1081 * PEERINFO calls this function to let us know about a possible peer
1082 * that we might want to connect to.
1084 * @param cls closure (not used)
1085 * @param peer potential peer to connect to
1086 * @param hello HELLO for this peer (or NULL)
1087 * @param trust how much we trust the peer (not used)
1090 process_peer (void *cls,
1091 const struct GNUNET_PeerIdentity *peer,
1092 const struct GNUNET_HELLO_Message *hello,
1097 GNUNET_assert (peer != NULL);
1098 if (0 == memcmp (&my_identity,
1099 peer, sizeof (struct GNUNET_PeerIdentity)))
1100 return; /* that's me! */
1103 /* free existing HELLO, if any */
1104 pos = GNUNET_CONTAINER_multihashmap_get (peers,
1108 GNUNET_free_non_null (pos->hello);
1110 if (pos->filter != NULL)
1112 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
1115 if ( (! pos->is_connected) &&
1116 (! pos->is_friend) &&
1117 (0 == GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value) )
1118 free_peer (NULL, &pos->pid.hashPubKey, pos);
1122 consider_for_advertising (hello);
1123 pos = GNUNET_CONTAINER_multihashmap_get (peers,
1126 pos = make_peer (peer, hello, GNUNET_NO);
1127 GNUNET_assert (NULL != pos);
1128 if (GNUNET_YES == pos->is_connected)
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Already connected to peer `%s'\n",
1137 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).value > 0)
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1141 "Already tried peer `%s' recently\n",
1144 return; /* peer still greylisted */
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1148 "Considering connecting to peer `%s'\n",
1151 attempt_connect (pos);
1156 * Function called after GNUNET_CORE_connect has succeeded
1157 * (or failed for good).
1159 * @param cls closure
1160 * @param server handle to the server, NULL if we failed
1161 * @param my_id ID of this peer, NULL if we failed
1162 * @param publicKey public key of this peer, NULL if we failed
1165 core_init (void *cls,
1166 struct GNUNET_CORE_Handle * server,
1167 const struct GNUNET_PeerIdentity *
1170 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1176 _("Failed to connect to core service, can not manage topology!\n"));
1177 GNUNET_SCHEDULER_shutdown (sched);
1181 my_identity = *my_id;
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 GNUNET_i2s (my_id));
1187 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, sched,
1194 * Read the friends file.
1197 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1202 struct GNUNET_PeerIdentity pid;
1204 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1205 unsigned int entries_found;
1209 GNUNET_CONFIGURATION_get_value_filename (cfg,
1214 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1215 _("Option `%s' in section `%s' not specified!\n"),
1220 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1221 GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1222 | GNUNET_DISK_PERM_USER_WRITE);
1223 if (0 != STAT (fn, &frstat))
1225 if ((friends_only) || (minimum_friend_count > 0))
1227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1228 _("Could not read friends list `%s'\n"), fn);
1233 if (frstat.st_size == 0)
1235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1236 _("Friends file `%s' is empty.\n"),
1241 data = GNUNET_malloc_large (frstat.st_size);
1242 if (frstat.st_size !=
1243 GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1246 _("Failed to read friends list from `%s'\n"), fn);
1253 while ((pos < frstat.st_size) && isspace (data[pos]))
1255 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1256 (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1258 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1259 if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1261 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1262 _("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1263 (unsigned long long) pos);
1265 while ((pos < frstat.st_size) && (!isspace (data[pos])))
1269 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1270 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1273 _("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1274 (unsigned long long) pos,
1279 if (0 != memcmp (&pid,
1281 sizeof (struct GNUNET_PeerIdentity)))
1284 fl = make_peer (&pid,
1287 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1288 _("Found friend `%s' in configuration\n"),
1289 GNUNET_i2s (&fl->pid));
1293 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1294 _("Found myself `%s' in friend list (useless, ignored)\n"),
1298 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1299 while ((pos < frstat.st_size) && isspace (data[pos]))
1304 GNUNET_STATISTICS_update (stats,
1305 gettext_noop ("# friends in configuration"),
1308 if ( (minimum_friend_count > entries_found) &&
1309 (friends_only == GNUNET_NO) )
1311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1312 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1314 if ( (minimum_friend_count > target_connection_count) &&
1315 (friends_only == GNUNET_NO) )
1317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1318 _("More friendly connections required than target total number of connections.\n"));
1324 * This function is called whenever an encrypted HELLO message is
1327 * @param cls closure
1328 * @param other the other peer involved (sender or receiver, NULL
1329 * for loopback messages where we are both sender and receiver)
1330 * @param message the actual HELLO message
1331 * @param latency reported latency of the connection with 'other'
1332 * @param distance reported distance (DV) to 'other'
1333 * @return GNUNET_OK to keep the connection open,
1334 * GNUNET_SYSERR to close it (signal serious error)
1337 handle_encrypted_hello (void *cls,
1338 const struct GNUNET_PeerIdentity * other,
1339 const struct GNUNET_MessageHeader *
1341 struct GNUNET_TIME_Relative latency,
1345 struct GNUNET_PeerIdentity pid;
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 "Received encrypted `%s' from peer `%s'",
1351 GNUNET_i2s (other));
1354 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) message,
1357 GNUNET_break_op (0);
1358 return GNUNET_SYSERR;
1360 GNUNET_STATISTICS_update (stats,
1361 gettext_noop ("# HELLO messages received"),
1364 peer = GNUNET_CONTAINER_multihashmap_get (peers,
1366 if ( (peer != NULL) &&
1367 (peer->is_blocked) )
1368 return GNUNET_OK; /* ignore: currently blocked */
1369 if ( (GNUNET_YES == friends_only) &&
1371 (GNUNET_YES != peer->is_friend) ) )
1372 return GNUNET_OK; /* ignore: not a friend */
1373 if (transport != NULL)
1374 GNUNET_TRANSPORT_offer_hello (transport,
1381 * Function to fill send buffer with HELLO.
1383 * @param cls 'struct Peer' of the target peer
1384 * @param size number of bytes available in buf
1385 * @param buf where the callee should write the message
1386 * @return number of bytes written to buf
1389 hello_advertising_ready (void *cls,
1393 struct Peer *pl = cls;
1394 struct FindAdvHelloContext fah;
1397 pl->hello_req = NULL;
1398 /* find applicable HELLOs */
1401 fah.max_size = size;
1402 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1403 GNUNET_CONTAINER_multihashmap_iterate (peers,
1404 &find_advertisable_hello,
1407 if (fah.result != NULL)
1409 want = GNUNET_HELLO_size (fah.result->hello);
1410 GNUNET_assert (want <= size);
1411 memcpy (buf, fah.result->hello, want);
1412 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
1413 &pl->pid.hashPubKey);
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1416 "Sending `%s' with %u bytes",
1418 (unsigned int) want);
1420 GNUNET_STATISTICS_update (stats,
1421 gettext_noop ("# HELLO messages gossipped"),
1425 pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1426 pl->hello_delay_task
1427 = GNUNET_SCHEDULER_add_now (sched,
1428 &schedule_next_hello,
1435 * Last task run during shutdown. Disconnects us from
1436 * the transport and core.
1438 * @param cls unused, NULL
1439 * @param tc scheduler context
1442 cleaning_task (void *cls,
1443 const struct GNUNET_SCHEDULER_TaskContext *tc)
1445 struct DisconnectList *dl;
1447 if (NULL != peerinfo_notify)
1449 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1450 peerinfo_notify = NULL;
1452 GNUNET_TRANSPORT_disconnect (transport);
1454 GNUNET_CONTAINER_multihashmap_iterate (peers,
1457 GNUNET_CONTAINER_multihashmap_destroy (peers);
1460 GNUNET_CORE_disconnect (handle);
1463 while (NULL != (dl = disconnect_head))
1465 GNUNET_CONTAINER_DLL_remove (disconnect_head,
1468 GNUNET_TRANSPORT_blacklist_cancel (dl->rh);
1473 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1480 * Main function that will be run.
1482 * @param cls closure
1483 * @param s the scheduler to use
1484 * @param args remaining command-line arguments
1485 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1486 * @param c configuration
1490 struct GNUNET_SCHEDULER_Handle * s,
1492 const char *cfgfile,
1493 const struct GNUNET_CONFIGURATION_Handle * c)
1495 struct GNUNET_CORE_MessageHandler handlers[] =
1497 { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1500 unsigned long long opt;
1504 stats = GNUNET_STATISTICS_create (sched, "topology", cfg);
1505 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1508 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1512 GNUNET_CONFIGURATION_get_value_number (cfg,
1517 minimum_friend_count = (unsigned int) opt;
1519 GNUNET_CONFIGURATION_get_value_number (cfg,
1521 "TARGET-CONNECTION-COUNT",
1524 target_connection_count = (unsigned int) opt;
1525 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1527 if ( (friends_only == GNUNET_YES) ||
1528 (minimum_friend_count > 0) )
1529 read_friends_file (cfg);
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "Topology would like %u connections with at least %u friends (%s)\n",
1533 target_connection_count,
1534 minimum_friend_count,
1535 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1537 transport = GNUNET_TRANSPORT_connect (sched,
1543 handle = GNUNET_CORE_connect (sched,
1545 GNUNET_TIME_UNIT_FOREVER_REL,
1554 GNUNET_SCHEDULER_add_delayed (sched,
1555 GNUNET_TIME_UNIT_FOREVER_REL,
1556 &cleaning_task, NULL);
1557 if (NULL == transport)
1559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1560 _("Failed to connect to `%s' service.\n"),
1562 GNUNET_SCHEDULER_shutdown (sched);
1567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1568 _("Failed to connect to `%s' service.\n"),
1570 GNUNET_SCHEDULER_shutdown (sched);
1577 * gnunet-daemon-topology command line options.
1579 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1580 GNUNET_GETOPT_OPTION_END
1585 * The main function for the topology daemon.
1587 * @param argc number of arguments from the command line
1588 * @param argv command line arguments
1589 * @return 0 ok, 1 on error
1592 main (int argc, char *const *argv)
1597 GNUNET_PROGRAM_run (argc,
1600 _("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1602 &run, NULL)) ? 0 : 1;
1606 /* end of gnunet-daemon-topology.c */