2 This file is part of GNUnet.
3 (C) 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file topology/gnunet-daemon-topology.c
23 * @brief code for maintaining the mesh topology
24 * @author Christian Grothoff
29 #include "gnunet_constants.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_util_lib.h"
38 #define DEBUG_TOPOLOGY GNUNET_YES
41 * For how long do we blacklist a peer after a failed connection
44 #define GREYLIST_AFTER_ATTEMPT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 * For how long do we blacklist a friend after a failed connection
50 #define GREYLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
53 * For how long do we blacklist anyone under any cirumstances after a failed connection
56 #define GREYLIST_AFTER_ATTEMPT_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
59 * For how long do we blacklist anyone under any cirumstances after a failed connection
62 #define GREYLIST_AFTER_ATTEMPT_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
65 * How often do we at most advertise any HELLO to a peer?
67 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
70 * How often do we at most advertise the same HELLO to the same peer?
72 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
76 * Record for neighbours, friends and blacklisted peers.
81 * Which peer is this entry about?
83 struct GNUNET_PeerIdentity pid;
86 * Our handle for the request to transmit HELLOs to this peer; NULL
87 * if no such request is pending.
89 struct GNUNET_CORE_TransmitHandle *hello_req;
92 * Pointer to the HELLO message of this peer; can be NULL.
94 struct GNUNET_HELLO_Message *hello;
97 * Bloom filter used to mark which peers already got the HELLO
100 struct GNUNET_CONTAINER_BloomFilter *filter;
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 * How often have we tried so far?
132 unsigned int connect_attempts;
135 * Is this peer listed here because he is a friend?
140 * Are we connected to this peer right now?
148 * Our peerinfo notification context. We use notification
149 * to instantly learn about new peers as they are discovered.
151 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
156 static const struct GNUNET_CONFIGURATION_Handle *cfg;
159 * Handle to the core API.
161 static struct GNUNET_CORE_Handle *handle;
164 * Handle to the transport API.
166 static struct GNUNET_TRANSPORT_Handle *transport;
169 * Identity of this peer.
171 static struct GNUNET_PeerIdentity my_identity;
174 * All of our friends, all of our current neighbours and all peers for
175 * which we have HELLOs. So pretty much everyone. Maps peer identities
176 * to 'struct Peer *' values.
178 static struct GNUNET_CONTAINER_MultiHashMap *peers;
181 * Handle for reporting statistics.
183 static struct GNUNET_STATISTICS_Handle *stats;
186 * Blacklist (NULL if we have none).
188 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
191 * Task scheduled to try to add peers.
193 static GNUNET_SCHEDULER_TaskIdentifier add_task;
196 * Flag to disallow non-friend connections (pure F2F mode).
198 static int friends_only;
201 * Minimum number of friends to have in the
202 * connection set before we allow non-friends.
204 static unsigned int minimum_friend_count;
207 * Number of peers (friends and others) that we are currently connected to.
209 static unsigned int connection_count;
212 * Target number of connections.
214 static unsigned int target_connection_count;
217 * Number of friends that we are currently connected to.
219 static unsigned int friend_count;
222 * Should the topology daemon try to establish connections?
224 static int autoconnect;
228 * Function that decides if a connection is acceptable or not.
229 * If we have a blacklist, only friends are allowed, so the check
233 * @param pid peer to approve or disapproave
234 * @return GNUNET_OK if the connection is allowed
237 blacklist_check (void *cls, const struct GNUNET_PeerIdentity *pid)
241 pos = GNUNET_CONTAINER_multihashmap_get (peers, &pid->hashPubKey);
242 if ((pos != NULL) && (pos->is_friend == GNUNET_YES))
244 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers blacklisted"), 1,
246 return GNUNET_SYSERR;
251 * Whitelist all peers that we blacklisted; we've passed
252 * the minimum number of friends.
257 if (blacklist != NULL)
259 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
266 * Check if an additional connection from the given peer is allowed.
268 * @param peer connection to check
269 * @return GNUNET_OK if the connection is allowed
272 is_connection_allowed (struct Peer *peer)
275 memcmp (&my_identity, &peer->pid, sizeof (struct GNUNET_PeerIdentity)))
276 return GNUNET_SYSERR; /* disallow connections to self */
279 if (GNUNET_YES == friends_only)
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "Determined that `%s' is not allowed to connect (not a friend)\n",
284 GNUNET_i2s (&peer->pid));
286 return GNUNET_SYSERR;
288 if (friend_count >= minimum_friend_count)
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
293 GNUNET_i2s (&peer->pid));
295 return GNUNET_SYSERR;
300 * Free all resources associated with the given peer.
302 * @param cls closure (not used)
303 * @param pid identity of the peer
304 * @param value peer to free
305 * @return GNUNET_YES (always: continue to iterate)
308 free_peer (void *cls, const GNUNET_HashCode * pid, void *value)
310 struct Peer *pos = value;
312 GNUNET_break (GNUNET_OK ==
313 GNUNET_CONTAINER_multihashmap_remove (peers, pid, pos));
314 if (pos->hello_req != NULL)
315 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
316 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
317 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
318 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
319 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
320 GNUNET_free_non_null (pos->hello);
321 if (pos->filter != NULL)
322 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
329 * Discard peer entries for greylisted peers
330 * where the greylisting has expired.
332 * @param cls 'struct Peer' to greylist
333 * @param tc scheduler context
336 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
340 * Try to connect to the specified peer.
342 * @param pos peer to connect to
345 attempt_connect (struct Peer *pos)
347 struct GNUNET_TIME_Relative rem;
349 if ((connection_count >= target_connection_count) &&
350 (friend_count >= minimum_friend_count))
352 if (GNUNET_YES == pos->is_connected)
354 if (GNUNET_OK != is_connection_allowed (pos))
356 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
358 if (GNUNET_YES == pos->is_friend)
359 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
361 rem = GREYLIST_AFTER_ATTEMPT;
362 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
363 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
364 if (pos->connect_attempts > 30)
365 pos->connect_attempts = 30;
366 rem = GNUNET_TIME_relative_multiply (rem, 1 << (++pos->connect_attempts));
367 rem = GNUNET_TIME_relative_max (rem, GREYLIST_AFTER_ATTEMPT_MIN);
368 rem = GNUNET_TIME_relative_min (rem, GREYLIST_AFTER_ATTEMPT_MAX);
369 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
370 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
371 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
372 pos->greylist_clean_task =
373 GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to connect to `%s'\n",
376 GNUNET_i2s (&pos->pid));
378 GNUNET_STATISTICS_update (stats,
380 ("# connect requests issued to transport"), 1,
382 GNUNET_TRANSPORT_try_connect (transport, &pos->pid);
387 * Discard peer entries for greylisted peers
388 * where the greylisting has expired.
390 * @param cls 'struct Peer' to greylist
391 * @param tc scheduler context
394 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
396 struct Peer *pos = cls;
397 struct GNUNET_TIME_Relative rem;
399 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
400 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
401 if (rem.rel_value == 0)
403 attempt_connect (pos);
407 pos->greylist_clean_task =
408 GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
410 if ((GNUNET_NO == pos->is_friend) && (GNUNET_NO == pos->is_connected) &&
411 (NULL == pos->hello))
413 free_peer (NULL, &pos->pid.hashPubKey, pos);
420 * Create a new entry in the peer list.
422 * @param peer identity of the new entry
423 * @param hello hello message, can be NULL
424 * @param is_friend is the new entry for a friend?
425 * @return the new entry
428 make_peer (const struct GNUNET_PeerIdentity *peer,
429 const struct GNUNET_HELLO_Message *hello, int is_friend)
433 ret = GNUNET_malloc (sizeof (struct Peer));
435 ret->is_friend = is_friend;
438 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
439 memcpy (ret->hello, hello, GNUNET_HELLO_size (hello));
441 GNUNET_break (GNUNET_OK ==
442 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey,
444 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
450 * Setup bloom filter for the given peer entry.
452 * @param peer entry to initialize
455 setup_filter (struct Peer *peer)
457 /* 2^{-5} chance of not sending a HELLO to a peer is
458 * acceptably small (if the filter is 50% full);
459 * 64 bytes of memory are small compared to the rest
460 * of the data structure and would only really become
461 * "useless" once a HELLO has been passed on to ~100
462 * other peers, which is likely more than enough in
463 * any case; hence 64, 5 as bloomfilter parameters. */
464 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
465 peer->filter_expiration =
466 GNUNET_TIME_relative_to_absolute
467 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
468 /* never send a peer its own HELLO */
469 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
474 * Function to fill send buffer with HELLO.
476 * @param cls 'struct Peer' of the target peer
477 * @param size number of bytes available in buf
478 * @param buf where the callee should write the message
479 * @return number of bytes written to buf
482 hello_advertising_ready (void *cls, size_t size, void *buf);
486 * Closure for 'find_advertisable_hello'.
488 struct FindAdvHelloContext
492 * Peer we want to advertise to.
497 * Where to store the result (peer selected for advertising).
502 * Maximum HELLO size we can use right now.
506 struct GNUNET_TIME_Relative next_adv;
511 * Find a peer that would be reasonable for advertising.
514 * @param pid identity of a peer
515 * @param value 'struct Peer*' for the peer we are considering
516 * @return GNUNET_YES (continue iteration)
519 find_advertisable_hello (void *cls, const GNUNET_HashCode * pid, void *value)
521 struct FindAdvHelloContext *fah = cls;
522 struct Peer *pos = value;
523 struct GNUNET_TIME_Relative rst_time;
526 if (pos == fah->peer)
528 if (pos->hello == NULL)
530 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
531 if (0 == rst_time.rel_value)
533 /* time to discard... */
534 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
537 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
538 hs = GNUNET_HELLO_size (pos->hello);
539 if (hs > fah->max_size)
542 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
543 &fah->peer->pid.hashPubKey))
550 * Calculate when we would like to send the next HELLO to this
551 * peer and ask for it.
553 * @param cls for which peer to schedule the HELLO
554 * @param tc task context
557 schedule_next_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
559 struct Peer *pl = cls;
560 struct FindAdvHelloContext fah;
562 struct GNUNET_TIME_Relative delay;
564 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
565 GNUNET_assert (GNUNET_YES == pl->is_connected);
566 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
567 return; /* we're out of here */
568 if (pl->hello_req != NULL)
569 return; /* did not finish sending the previous one */
570 /* find applicable HELLOs */
573 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
574 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
575 GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
576 pl->hello_delay_task =
577 GNUNET_SCHEDULER_add_delayed (fah.next_adv, &schedule_next_hello, pl);
578 if (fah.result == NULL)
580 next_want = GNUNET_HELLO_size (fah.result->hello);
581 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
582 if (delay.rel_value == 0)
586 GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES, 0,
587 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
589 &hello_advertising_ready, pl);
595 * Cancel existing requests for sending HELLOs to this peer
596 * and recalculate when we should send HELLOs to it based
597 * on our current state (something changed!).
599 * @param cls closure, 'struct Peer' to skip, or NULL
600 * @param pid identity of a peer
601 * @param value 'struct Peer*' for the peer
602 * @return GNUNET_YES (always)
605 reschedule_hellos (void *cls, const GNUNET_HashCode * pid, void *value)
607 struct Peer *peer = value;
608 struct Peer *skip = cls;
612 if (!peer->is_connected)
614 if (peer->hello_req != NULL)
616 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
617 peer->hello_req = NULL;
619 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
621 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
622 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
624 peer->hello_delay_task =
625 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
631 * Method called whenever a peer connects.
634 * @param peer peer identity this notification is about
635 * @param atsi performance data
636 * @param atsi_count number of records in 'atsi'
639 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
640 const struct GNUNET_ATS_Information *atsi,
641 unsigned int atsi_count)
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 "Core told us that we are connecting to `%s'\n",
650 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
654 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
655 connection_count, GNUNET_NO);
656 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
659 pos = make_peer (peer, NULL, GNUNET_NO);
660 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
664 GNUNET_assert (GNUNET_NO == pos->is_connected);
665 pos->greylisted_until.abs_value = 0; /* remove greylisting */
667 pos->is_connected = GNUNET_YES;
668 pos->connect_attempts = 0; /* re-set back-off factor */
671 if ((friend_count == minimum_friend_count - 1) &&
672 (GNUNET_YES != friends_only))
675 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
676 friend_count, GNUNET_NO);
678 reschedule_hellos (NULL, &peer->hashPubKey, pos);
683 * Try to add more peers to our connection set.
685 * @param cls closure, not used
686 * @param pid identity of a peer
687 * @param value 'struct Peer*' for the peer
688 * @return GNUNET_YES (continue to iterate)
691 try_add_peers (void *cls, const GNUNET_HashCode * pid, void *value)
693 struct Peer *pos = value;
695 attempt_connect (pos);
701 * Last task run during shutdown. Disconnects us from
702 * the transport and core.
704 * @param cls unused, NULL
705 * @param tc scheduler context
708 add_peer_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
710 add_task = GNUNET_SCHEDULER_NO_TASK;
712 GNUNET_CONTAINER_multihashmap_iterate (peers, &try_add_peers, NULL);
716 * Method called whenever a peer disconnects.
719 * @param peer peer identity this notification is about
722 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
726 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Core told us that we disconnected from `%s'\n",
733 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
739 if (pos->is_connected != GNUNET_YES)
744 pos->is_connected = GNUNET_NO;
746 if (NULL != pos->hello_req)
748 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
749 pos->hello_req = NULL;
751 if (GNUNET_SCHEDULER_NO_TASK != pos->hello_delay_task)
753 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
754 pos->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
756 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
757 connection_count, GNUNET_NO);
761 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
762 friend_count, GNUNET_NO);
764 if (((connection_count < target_connection_count) ||
765 (friend_count < minimum_friend_count)) &&
766 (GNUNET_SCHEDULER_NO_TASK == add_task))
767 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
768 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
769 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
774 * Iterator called on each address.
776 * @param cls flag that we will set if we see any addresses
777 * @param address the address of the peer
778 * @param expiration when will the given address expire
779 * @return GNUNET_SYSERR always, to terminate iteration
782 address_iterator (void *cls, const struct GNUNET_HELLO_Address *address,
783 struct GNUNET_TIME_Absolute expiration)
788 return GNUNET_SYSERR;
793 * We've gotten a HELLO from another peer. Consider it for
796 * @param hello the HELLO we got
799 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
802 struct GNUNET_PeerIdentity pid;
803 struct GNUNET_TIME_Absolute dt;
804 struct GNUNET_HELLO_Message *nh;
808 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
813 if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
814 return; /* that's me! */
815 have_address = GNUNET_NO;
816 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &address_iterator,
818 if (GNUNET_NO == have_address)
819 return; /* no point in advertising this one... */
820 peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
823 peer = make_peer (&pid, hello, GNUNET_NO);
825 else if (peer->hello != NULL)
827 dt = GNUNET_HELLO_equals (peer->hello, hello, GNUNET_TIME_absolute_get ());
828 if (dt.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
829 return; /* nothing new here */
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "Found `%s' from peer `%s' for advertising\n", "HELLO",
836 if (peer->hello != NULL)
838 nh = GNUNET_HELLO_merge (peer->hello, hello);
839 GNUNET_free (peer->hello);
844 size = GNUNET_HELLO_size (hello);
845 peer->hello = GNUNET_malloc (size);
846 memcpy (peer->hello, hello, size);
848 if (peer->filter != NULL)
849 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
851 /* since we have a new HELLO to pick from, re-schedule all
852 * HELLO requests that are not bound by the HELLO send rate! */
853 GNUNET_CONTAINER_multihashmap_iterate (peers, &reschedule_hellos, peer);
858 * PEERINFO calls this function to let us know about a possible peer
859 * that we might want to connect to.
861 * @param cls closure (not used)
862 * @param peer potential peer to connect to
863 * @param hello HELLO for this peer (or NULL)
864 * @param err_msg NULL if successful, otherwise contains error message
867 process_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
868 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
875 _("Error in communication with PEERINFO service: %s\n"),
877 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
878 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
881 GNUNET_assert (peer != NULL);
882 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
883 return; /* that's me! */
886 /* free existing HELLO, if any */
887 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
890 GNUNET_free_non_null (pos->hello);
892 if (pos->filter != NULL)
894 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
897 if ((!pos->is_connected) && (!pos->is_friend) &&
899 GNUNET_TIME_absolute_get_remaining (pos->
900 greylisted_until).rel_value))
901 free_peer (NULL, &pos->pid.hashPubKey, pos);
905 consider_for_advertising (hello);
906 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
908 pos = make_peer (peer, hello, GNUNET_NO);
909 GNUNET_assert (NULL != pos);
910 if (GNUNET_YES == pos->is_connected)
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already connected to peer `%s'\n",
918 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already tried peer `%s' recently\n",
924 return; /* peer still greylisted */
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Considering connecting to peer `%s'\n",
930 attempt_connect (pos);
935 * Function called after GNUNET_CORE_connect has succeeded
936 * (or failed for good).
939 * @param server handle to the server, NULL if we failed
940 * @param my_id ID of this peer, NULL if we failed
943 core_init (void *cls, struct GNUNET_CORE_Handle *server,
944 const struct GNUNET_PeerIdentity *my_id)
948 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
950 ("Failed to connect to core service, can not manage topology!\n"));
951 GNUNET_SCHEDULER_shutdown ();
955 my_identity = *my_id;
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am peer `%s'\n", GNUNET_i2s (my_id));
959 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
964 * Read the friends file.
967 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
972 struct GNUNET_PeerIdentity pid;
974 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
975 unsigned int entries_found;
979 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
981 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
982 _("Option `%s' in section `%s' not specified!\n"), "FRIENDS",
986 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
987 GNUNET_DISK_fn_write (fn, NULL, 0,
988 GNUNET_DISK_PERM_USER_READ |
989 GNUNET_DISK_PERM_USER_WRITE);
990 if (0 != STAT (fn, &frstat))
992 if ((friends_only) || (minimum_friend_count > 0))
993 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
994 _("Could not read friends list `%s'\n"), fn);
998 if (frstat.st_size == 0)
1000 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Friends file `%s' is empty.\n"),
1005 data = GNUNET_malloc_large (frstat.st_size);
1008 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1009 _("Failed to read friends list from `%s': out of memory\n"),
1014 if (frstat.st_size != GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1017 _("Failed to read friends list from `%s'\n"), fn);
1024 while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1026 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1028 frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1030 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1033 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1035 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1037 ("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1038 (unsigned long long) pos);
1040 while ((pos < frstat.st_size) && (!isspace ((unsigned char) data[pos])))
1044 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1046 GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1048 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1050 ("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1051 (unsigned long long) pos, &enc);
1055 if (0 != memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1058 fl = make_peer (&pid, NULL, GNUNET_YES);
1059 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1060 _("Found friend `%s' in configuration\n"),
1061 GNUNET_i2s (&fl->pid));
1065 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1066 _("Found myself `%s' in friend list (useless, ignored)\n"),
1070 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1071 while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1076 GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
1077 entries_found, GNUNET_NO);
1078 if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1080 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1082 ("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1084 if ((minimum_friend_count > target_connection_count) &&
1085 (friends_only == GNUNET_NO))
1087 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1089 ("More friendly connections required than target total number of connections.\n"));
1095 * This function is called whenever an encrypted HELLO message is
1098 * @param cls closure
1099 * @param other the other peer involved (sender or receiver, NULL
1100 * for loopback messages where we are both sender and receiver)
1101 * @param message the actual HELLO message
1102 * @param atsi performance data
1103 * @param atsi_count number of records in 'atsi'
1104 * @return GNUNET_OK to keep the connection open,
1105 * GNUNET_SYSERR to close it (signal serious error)
1108 handle_encrypted_hello (void *cls, const struct GNUNET_PeerIdentity *other,
1109 const struct GNUNET_MessageHeader *message,
1110 const struct GNUNET_ATS_Information *atsi,
1111 unsigned int atsi_count)
1114 struct GNUNET_PeerIdentity pid;
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received encrypted `%s' from peer `%s'",
1118 "HELLO", GNUNET_i2s (other));
1121 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1123 GNUNET_break_op (0);
1124 return GNUNET_SYSERR;
1126 GNUNET_STATISTICS_update (stats, gettext_noop ("# HELLO messages received"),
1128 peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
1131 if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
1136 if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
1138 if ((GNUNET_YES != peer->is_friend) &&
1139 (friend_count < minimum_friend_count))
1142 if (transport != NULL)
1143 GNUNET_TRANSPORT_offer_hello (transport, message, NULL, NULL);
1149 * Function to fill send buffer with HELLO.
1151 * @param cls 'struct Peer' of the target peer
1152 * @param size number of bytes available in buf
1153 * @param buf where the callee should write the message
1154 * @return number of bytes written to buf
1157 hello_advertising_ready (void *cls, size_t size, void *buf)
1159 struct Peer *pl = cls;
1160 struct FindAdvHelloContext fah;
1163 pl->hello_req = NULL;
1164 GNUNET_assert (GNUNET_YES == pl->is_connected);
1165 /* find applicable HELLOs */
1168 fah.max_size = size;
1169 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1170 GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
1172 if (fah.result != NULL)
1174 want = GNUNET_HELLO_size (fah.result->hello);
1175 GNUNET_assert (want <= size);
1176 memcpy (buf, fah.result->hello, want);
1177 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &pl->pid.hashPubKey);
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' with %u bytes", "HELLO",
1180 (unsigned int) want);
1182 GNUNET_STATISTICS_update (stats,
1183 gettext_noop ("# HELLO messages gossipped"), 1,
1187 if (pl->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
1188 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1189 pl->next_hello_allowed =
1190 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1191 pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1197 * Last task run during shutdown. Disconnects us from
1198 * the transport and core.
1200 * @param cls unused, NULL
1201 * @param tc scheduler context
1204 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1206 if (NULL != peerinfo_notify)
1208 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1209 peerinfo_notify = NULL;
1211 if (GNUNET_SCHEDULER_NO_TASK != add_task)
1213 GNUNET_SCHEDULER_cancel (add_task);
1214 add_task = GNUNET_SCHEDULER_NO_TASK;
1216 GNUNET_TRANSPORT_disconnect (transport);
1218 GNUNET_CONTAINER_multihashmap_iterate (peers, &free_peer, NULL);
1219 GNUNET_CONTAINER_multihashmap_destroy (peers);
1222 GNUNET_CORE_disconnect (handle);
1228 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1235 * Main function that will be run.
1237 * @param cls closure
1238 * @param args remaining command-line arguments
1239 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1240 * @param c configuration
1243 run (void *cls, char *const *args, const char *cfgfile,
1244 const struct GNUNET_CONFIGURATION_Handle *c)
1246 static struct GNUNET_CORE_MessageHandler handlers[] = {
1247 {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1250 unsigned long long opt;
1253 stats = GNUNET_STATISTICS_create ("topology", cfg);
1255 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "AUTOCONNECT");
1257 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY");
1259 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "MINIMUM-FRIENDS",
1262 minimum_friend_count = (unsigned int) opt;
1264 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY",
1265 "TARGET-CONNECTION-COUNT", &opt))
1267 target_connection_count = (unsigned int) opt;
1268 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1270 if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0))
1271 read_friends_file (cfg);
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274 "Topology would like %u connections with at least %u friends (%s)\n",
1275 target_connection_count, minimum_friend_count,
1276 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1278 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
1279 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
1280 transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
1282 GNUNET_CORE_connect (cfg, 1, NULL, &core_init, &connect_notify,
1283 &disconnect_notify, NULL, GNUNET_NO, NULL, GNUNET_NO,
1285 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
1287 if (NULL == transport)
1289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1290 _("Failed to connect to `%s' service.\n"), "transport");
1291 GNUNET_SCHEDULER_shutdown ();
1296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1297 _("Failed to connect to `%s' service.\n"), "core");
1298 GNUNET_SCHEDULER_shutdown ();
1305 * The main function for the topology daemon.
1307 * @param argc number of arguments from the command line
1308 * @param argv command line arguments
1309 * @return 0 ok, 1 on error
1312 main (int argc, char *const *argv)
1314 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1315 GNUNET_GETOPT_OPTION_END
1321 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",
1323 ("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1324 options, &run, NULL)) ? 0 : 1;
1328 /* end of gnunet-daemon-topology.c */