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
27 #include "gnunet_constants.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_peerinfo_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_util_lib.h"
37 * Minimum required delay between calls to GNUNET_TRANSPORT_try_connect.
39 #define MAX_CONNECT_FREQUENCY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
42 * For how long do we blacklist a peer after a failed connection
43 * attempt? This is the baseline factor which is then multiplied by
44 * two to the power of the number of failed attempts.
46 #define GREYLIST_AFTER_ATTEMPT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
49 * For how long do we blacklist a friend after a failed connection
50 * attempt? This is the baseline factor which is then multiplied by
51 * two to the power of the number of failed attempts.
53 #define GREYLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
56 * For how long do we blacklist anyone under any cirumstances at least after a failed connection
57 * attempt? This is the absolute minimum, regardless of what the calculation based on
58 * exponential backoff returns.
60 #define GREYLIST_AFTER_ATTEMPT_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
63 * For how long do we blacklist anyone under any cirumstances at most after a failed connection
64 * attempt? This is the absolute maximum, regardless of what the calculation based on
65 * exponential back-off returns.
67 #define GREYLIST_AFTER_ATTEMPT_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
70 * At what frequency do we sent HELLOs to a peer?
72 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
75 * After what time period do we expire the HELLO Bloom filter?
77 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
81 * Record for neighbours, friends and blacklisted peers.
86 * Which peer is this entry about?
88 struct GNUNET_PeerIdentity pid;
91 * Our handle for the request to transmit HELLOs to this peer; NULL
92 * if no such request is pending.
94 struct GNUNET_CORE_TransmitHandle *hello_req;
97 * Pointer to the HELLO message of this peer; can be NULL.
99 struct GNUNET_HELLO_Message *hello;
102 * Bloom filter used to mark which peers already got the HELLO
105 struct GNUNET_CONTAINER_BloomFilter *filter;
108 * Until what time should we not try to connect again
111 struct GNUNET_TIME_Absolute greylisted_until;
114 * Next time we are allowed to transmit a HELLO to this peer?
116 struct GNUNET_TIME_Absolute next_hello_allowed;
119 * When should we reset the bloom filter of this entry?
121 struct GNUNET_TIME_Absolute filter_expiration;
124 * ID of task we use to wait for the time to send the next HELLO
127 GNUNET_SCHEDULER_TaskIdentifier hello_delay_task;
130 * Task for issuing GNUNET_TRANSPORT_try_connect for this peer.
132 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task;
135 * ID of task we use to clear peers from the greylist.
137 GNUNET_SCHEDULER_TaskIdentifier greylist_clean_task;
140 * How often have we tried so far?
142 unsigned int connect_attempts;
145 * Is this peer listed here because he is a friend?
150 * Are we connected to this peer right now?
158 * Our peerinfo notification context. We use notification
159 * to instantly learn about new peers as they are discovered.
161 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
166 static const struct GNUNET_CONFIGURATION_Handle *cfg;
169 * Handle to the core API.
171 static struct GNUNET_CORE_Handle *handle;
174 * Handle to the transport API.
176 static struct GNUNET_TRANSPORT_Handle *transport;
179 * Identity of this peer.
181 static struct GNUNET_PeerIdentity my_identity;
184 * All of our friends, all of our current neighbours and all peers for
185 * which we have HELLOs. So pretty much everyone. Maps peer identities
186 * to 'struct Peer *' values.
188 static struct GNUNET_CONTAINER_MultiHashMap *peers;
191 * Handle for reporting statistics.
193 static struct GNUNET_STATISTICS_Handle *stats;
196 * Blacklist (NULL if we have none).
198 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
201 * When can we next ask transport to create a connection?
203 static struct GNUNET_TIME_Absolute next_connect_attempt;
206 * Task scheduled to try to add peers.
208 static GNUNET_SCHEDULER_TaskIdentifier add_task;
211 * Flag to disallow non-friend connections (pure F2F mode).
213 static int friends_only;
216 * Minimum number of friends to have in the
217 * connection set before we allow non-friends.
219 static unsigned int minimum_friend_count;
222 * Number of peers (friends and others) that we are currently connected to.
224 static unsigned int connection_count;
227 * Target number of connections.
229 static unsigned int target_connection_count;
232 * Number of friends that we are currently connected to.
234 static unsigned int friend_count;
238 * Function that decides if a connection is acceptable or not.
239 * If we have a blacklist, only friends are allowed, so the check
243 * @param pid peer to approve or disapproave
244 * @return GNUNET_OK if the connection is allowed
247 blacklist_check (void *cls, const struct GNUNET_PeerIdentity *pid)
251 pos = GNUNET_CONTAINER_multihashmap_get (peers, &pid->hashPubKey);
252 if ((pos != NULL) && (pos->is_friend == GNUNET_YES))
254 GNUNET_STATISTICS_update (stats, gettext_noop ("# peers blacklisted"), 1,
256 return GNUNET_SYSERR;
261 * Whitelist all peers that we blacklisted; we've passed
262 * the minimum number of friends.
267 if (blacklist != NULL)
269 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
276 * Check if an additional connection from the given peer is allowed.
278 * @param peer connection to check
279 * @return GNUNET_OK if the connection is allowed
282 is_connection_allowed (struct Peer *peer)
285 memcmp (&my_identity, &peer->pid, sizeof (struct GNUNET_PeerIdentity)))
286 return GNUNET_SYSERR; /* disallow connections to self */
289 if (GNUNET_YES == friends_only)
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Determined that `%s' is not allowed to connect (not a friend)\n",
293 GNUNET_i2s (&peer->pid));
294 return GNUNET_SYSERR;
296 if (friend_count >= minimum_friend_count)
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
300 GNUNET_i2s (&peer->pid));
301 return GNUNET_SYSERR;
306 * Free all resources associated with the given peer.
308 * @param cls closure (not used)
309 * @param pid identity of the peer
310 * @param value peer to free
311 * @return GNUNET_YES (always: continue to iterate)
314 free_peer (void *cls, const struct GNUNET_HashCode * pid, void *value)
316 struct Peer *pos = value;
318 GNUNET_break (GNUNET_NO == pos->is_connected);
319 GNUNET_break (GNUNET_OK ==
320 GNUNET_CONTAINER_multihashmap_remove (peers, pid, pos));
321 if (pos->hello_req != NULL)
322 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
323 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
324 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
325 if (pos->attempt_connect_task != GNUNET_SCHEDULER_NO_TASK)
326 GNUNET_SCHEDULER_cancel (pos->attempt_connect_task);
327 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
328 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
329 GNUNET_free_non_null (pos->hello);
330 if (pos->filter != NULL)
331 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
338 * Discard peer entries for greylisted peers
339 * where the greylisting has expired.
341 * @param cls 'struct Peer' to greylist
342 * @param tc scheduler context
345 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
349 * Try to connect to the specified peer.
351 * @param pos peer to connect to
354 attempt_connect (struct Peer *pos)
356 struct GNUNET_TIME_Relative rem;
358 if ((connection_count >= target_connection_count) &&
359 (friend_count >= minimum_friend_count))
361 if (GNUNET_YES == pos->is_connected)
363 if (GNUNET_OK != is_connection_allowed (pos))
365 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
367 if (GNUNET_YES == pos->is_friend)
368 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
370 rem = GREYLIST_AFTER_ATTEMPT;
371 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
372 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
373 if (pos->connect_attempts > 30)
374 pos->connect_attempts = 30;
375 rem = GNUNET_TIME_relative_multiply (rem, 1 << (++pos->connect_attempts));
376 rem = GNUNET_TIME_relative_max (rem, GREYLIST_AFTER_ATTEMPT_MIN);
377 rem = GNUNET_TIME_relative_min (rem, GREYLIST_AFTER_ATTEMPT_MAX);
378 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
379 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
380 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
381 pos->greylist_clean_task =
382 GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to connect to `%s'\n",
384 GNUNET_i2s (&pos->pid));
385 GNUNET_STATISTICS_update (stats,
387 ("# connect requests issued to transport"), 1,
389 GNUNET_TRANSPORT_try_connect (transport, &pos->pid, NULL, NULL); /*FIXME TRY_CONNECT change */
394 * Try to connect to the specified peer.
396 * @param cls peer to connect to
397 * @param tc scheduler context
400 do_attempt_connect (void *cls,
401 const struct GNUNET_SCHEDULER_TaskContext *tc)
403 struct Peer *pos = cls;
404 struct GNUNET_TIME_Relative delay;
406 pos->attempt_connect_task = GNUNET_SCHEDULER_NO_TASK;
407 if (GNUNET_YES == pos->is_connected)
409 delay = GNUNET_TIME_absolute_get_remaining (next_connect_attempt);
410 if (delay.rel_value > 0)
412 pos->attempt_connect_task = GNUNET_SCHEDULER_add_delayed (delay,
417 next_connect_attempt = GNUNET_TIME_relative_to_absolute (MAX_CONNECT_FREQUENCY_DELAY);
418 attempt_connect (pos);
423 * Schedule a task to try to connect to the specified peer.
425 * @param pos peer to connect to
428 schedule_attempt_connect (struct Peer *pos)
430 if (GNUNET_SCHEDULER_NO_TASK != pos->attempt_connect_task)
432 pos->attempt_connect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_connect_attempt),
439 * Discard peer entries for greylisted peers
440 * where the greylisting has expired.
442 * @param cls 'struct Peer' to greylist
443 * @param tc scheduler context
446 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
448 struct Peer *pos = cls;
449 struct GNUNET_TIME_Relative rem;
451 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
452 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
453 if (rem.rel_value == 0)
455 schedule_attempt_connect (pos);
459 pos->greylist_clean_task =
460 GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
462 if ((GNUNET_NO == pos->is_friend) && (GNUNET_NO == pos->is_connected) &&
463 (NULL == pos->hello))
465 free_peer (NULL, &pos->pid.hashPubKey, pos);
472 * Create a new entry in the peer list.
474 * @param peer identity of the new entry
475 * @param hello hello message, can be NULL
476 * @param is_friend is the new entry for a friend?
477 * @return the new entry
480 make_peer (const struct GNUNET_PeerIdentity *peer,
481 const struct GNUNET_HELLO_Message *hello, int is_friend)
485 ret = GNUNET_malloc (sizeof (struct Peer));
487 ret->is_friend = is_friend;
490 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
491 memcpy (ret->hello, hello, GNUNET_HELLO_size (hello));
493 GNUNET_break (GNUNET_OK ==
494 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey,
496 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
502 * Setup bloom filter for the given peer entry.
504 * @param peer entry to initialize
507 setup_filter (struct Peer *peer)
509 /* 2^{-5} chance of not sending a HELLO to a peer is
510 * acceptably small (if the filter is 50% full);
511 * 64 bytes of memory are small compared to the rest
512 * of the data structure and would only really become
513 * "useless" once a HELLO has been passed on to ~100
514 * other peers, which is likely more than enough in
515 * any case; hence 64, 5 as bloomfilter parameters. */
516 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
517 peer->filter_expiration =
518 GNUNET_TIME_relative_to_absolute
519 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
520 /* never send a peer its own HELLO */
521 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
526 * Function to fill send buffer with HELLO.
528 * @param cls 'struct Peer' of the target peer
529 * @param size number of bytes available in buf
530 * @param buf where the callee should write the message
531 * @return number of bytes written to buf
534 hello_advertising_ready (void *cls, size_t size, void *buf);
538 * Closure for 'find_advertisable_hello'.
540 struct FindAdvHelloContext
544 * Peer we want to advertise to.
549 * Where to store the result (peer selected for advertising).
554 * Maximum HELLO size we can use right now.
558 struct GNUNET_TIME_Relative next_adv;
563 * Find a peer that would be reasonable for advertising.
566 * @param pid identity of a peer
567 * @param value 'struct Peer*' for the peer we are considering
568 * @return GNUNET_YES (continue iteration)
571 find_advertisable_hello (void *cls, const struct GNUNET_HashCode * pid, void *value)
573 struct FindAdvHelloContext *fah = cls;
574 struct Peer *pos = value;
575 struct GNUNET_TIME_Relative rst_time;
578 if (pos == fah->peer)
580 if (pos->hello == NULL)
582 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
583 if (0 == rst_time.rel_value)
585 /* time to discard... */
586 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
589 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
590 hs = GNUNET_HELLO_size (pos->hello);
591 if (hs > fah->max_size)
594 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
595 &fah->peer->pid.hashPubKey))
602 * Calculate when we would like to send the next HELLO to this
603 * peer and ask for it.
605 * @param cls for which peer to schedule the HELLO
606 * @param tc task context
609 schedule_next_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
611 struct Peer *pl = cls;
612 struct FindAdvHelloContext fah;
614 struct GNUNET_TIME_Relative delay;
616 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
617 GNUNET_assert (GNUNET_YES == pl->is_connected);
618 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
619 return; /* we're out of here */
620 if (pl->hello_req != NULL)
621 return; /* did not finish sending the previous one */
622 /* find applicable HELLOs */
625 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
626 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
627 GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
628 pl->hello_delay_task =
629 GNUNET_SCHEDULER_add_delayed (fah.next_adv, &schedule_next_hello, pl);
630 if (fah.result == NULL)
632 next_want = GNUNET_HELLO_size (fah.result->hello);
633 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
634 if (delay.rel_value == 0)
638 GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES, 0,
639 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
641 &hello_advertising_ready, pl);
647 * Cancel existing requests for sending HELLOs to this peer
648 * and recalculate when we should send HELLOs to it based
649 * on our current state (something changed!).
651 * @param cls closure, 'struct Peer' to skip, or NULL
652 * @param pid identity of a peer
653 * @param value 'struct Peer*' for the peer
654 * @return GNUNET_YES (always)
657 reschedule_hellos (void *cls, const struct GNUNET_HashCode * pid, void *value)
659 struct Peer *peer = value;
660 struct Peer *skip = cls;
664 if (!peer->is_connected)
666 if (peer->hello_req != NULL)
668 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
669 peer->hello_req = NULL;
671 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
673 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
674 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
676 peer->hello_delay_task =
677 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
683 * Method called whenever a peer connects.
686 * @param peer peer identity this notification is about
687 * @param atsi performance data
688 * @param atsi_count number of records in 'atsi'
691 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
692 const struct GNUNET_ATS_Information *atsi,
693 unsigned int atsi_count)
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "Core told us that we are connecting to `%s'\n",
700 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
704 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
705 connection_count, GNUNET_NO);
706 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
709 pos = make_peer (peer, NULL, GNUNET_NO);
710 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
714 GNUNET_assert (GNUNET_NO == pos->is_connected);
715 pos->greylisted_until.abs_value = 0; /* remove greylisting */
717 pos->is_connected = GNUNET_YES;
718 pos->connect_attempts = 0; /* re-set back-off factor */
721 if ((friend_count == minimum_friend_count - 1) &&
722 (GNUNET_YES != friends_only))
725 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
726 friend_count, GNUNET_NO);
728 reschedule_hellos (NULL, &peer->hashPubKey, pos);
733 * Try to add more peers to our connection set.
735 * @param cls closure, not used
736 * @param pid identity of a peer
737 * @param value 'struct Peer*' for the peer
738 * @return GNUNET_YES (continue to iterate)
741 try_add_peers (void *cls, const struct GNUNET_HashCode * pid, void *value)
743 struct Peer *pos = value;
745 schedule_attempt_connect (pos);
751 * Last task run during shutdown. Disconnects us from
752 * the transport and core.
754 * @param cls unused, NULL
755 * @param tc scheduler context
758 add_peer_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
760 add_task = GNUNET_SCHEDULER_NO_TASK;
762 GNUNET_CONTAINER_multihashmap_iterate (peers, &try_add_peers, NULL);
767 * Method called whenever a peer disconnects.
770 * @param peer peer identity this notification is about
773 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
777 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Core told us that we disconnected from `%s'\n",
782 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
788 if (pos->is_connected != GNUNET_YES)
793 pos->is_connected = GNUNET_NO;
795 if (NULL != pos->hello_req)
797 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
798 pos->hello_req = NULL;
800 if (GNUNET_SCHEDULER_NO_TASK != pos->hello_delay_task)
802 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
803 pos->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
805 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
806 connection_count, GNUNET_NO);
810 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
811 friend_count, GNUNET_NO);
813 if (((connection_count < target_connection_count) ||
814 (friend_count < minimum_friend_count)) &&
815 (GNUNET_SCHEDULER_NO_TASK == add_task))
816 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
817 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
818 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
823 * Iterator called on each address.
825 * @param cls flag that we will set if we see any addresses
826 * @param address the address of the peer
827 * @param expiration when will the given address expire
828 * @return GNUNET_SYSERR always, to terminate iteration
831 address_iterator (void *cls, const struct GNUNET_HELLO_Address *address,
832 struct GNUNET_TIME_Absolute expiration)
837 return GNUNET_SYSERR;
842 * We've gotten a HELLO from another peer. Consider it for
845 * @param hello the HELLO we got
848 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
851 struct GNUNET_PeerIdentity pid;
852 struct GNUNET_TIME_Absolute dt;
853 struct GNUNET_HELLO_Message *nh;
857 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
862 if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
863 return; /* that's me! */
864 have_address = GNUNET_NO;
865 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &address_iterator,
867 if (GNUNET_NO == have_address)
868 return; /* no point in advertising this one... */
869 peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
872 peer = make_peer (&pid, hello, GNUNET_NO);
874 else if (peer->hello != NULL)
876 dt = GNUNET_HELLO_equals (peer->hello, hello, GNUNET_TIME_absolute_get ());
877 if (dt.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
878 return; /* nothing new here */
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Found `%s' from peer `%s' for advertising\n", "HELLO",
883 if (peer->hello != NULL)
885 nh = GNUNET_HELLO_merge (peer->hello, hello);
886 GNUNET_free (peer->hello);
891 size = GNUNET_HELLO_size (hello);
892 peer->hello = GNUNET_malloc (size);
893 memcpy (peer->hello, hello, size);
895 if (peer->filter != NULL)
896 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
898 /* since we have a new HELLO to pick from, re-schedule all
899 * HELLO requests that are not bound by the HELLO send rate! */
900 GNUNET_CONTAINER_multihashmap_iterate (peers, &reschedule_hellos, peer);
905 * PEERINFO calls this function to let us know about a possible peer
906 * that we might want to connect to.
908 * @param cls closure (not used)
909 * @param peer potential peer to connect to
910 * @param hello HELLO for this peer (or NULL)
911 * @param err_msg NULL if successful, otherwise contains error message
914 process_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
915 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 _("Error in communication with PEERINFO service: %s\n"),
924 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
925 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
928 GNUNET_assert (peer != NULL);
929 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
930 return; /* that's me! */
933 /* free existing HELLO, if any */
934 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
937 GNUNET_free_non_null (pos->hello);
939 if (pos->filter != NULL)
941 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
944 if ((GNUNET_NO == pos->is_connected) && (GNUNET_NO == pos->is_friend) &&
946 GNUNET_TIME_absolute_get_remaining (pos->
947 greylisted_until).rel_value))
948 free_peer (NULL, &pos->pid.hashPubKey, pos);
952 consider_for_advertising (hello);
953 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
955 pos = make_peer (peer, hello, GNUNET_NO);
956 GNUNET_assert (NULL != pos);
957 if (GNUNET_YES == pos->is_connected)
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already connected to peer `%s'\n",
963 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already tried peer `%s' recently\n",
967 return; /* peer still greylisted */
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Considering connecting to peer `%s'\n",
971 schedule_attempt_connect (pos);
976 * Function called after GNUNET_CORE_connect has succeeded
977 * (or failed for good).
980 * @param server handle to the server, NULL if we failed
981 * @param my_id ID of this peer, NULL if we failed
984 core_init (void *cls, struct GNUNET_CORE_Handle *server,
985 const struct GNUNET_PeerIdentity *my_id)
989 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
991 ("Failed to connect to core service, can not manage topology!\n"));
992 GNUNET_SCHEDULER_shutdown ();
996 my_identity = *my_id;
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am peer `%s'\n", GNUNET_i2s (my_id));
998 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
1003 * Read the friends file.
1006 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1011 struct GNUNET_PeerIdentity pid;
1013 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1014 unsigned int entries_found;
1018 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
1020 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1021 "topology", "FRIENDS");
1024 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1025 GNUNET_DISK_fn_write (fn, NULL, 0,
1026 GNUNET_DISK_PERM_USER_READ |
1027 GNUNET_DISK_PERM_USER_WRITE);
1028 if (GNUNET_OK != GNUNET_DISK_file_size (fn,
1029 &fsize, GNUNET_NO, GNUNET_YES))
1031 if ((friends_only) || (minimum_friend_count > 0))
1032 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1033 _("Could not read friends list `%s'\n"), fn);
1039 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Friends file `%s' is empty.\n"),
1044 data = GNUNET_malloc_large (fsize);
1047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1048 _("Failed to read friends list from `%s': out of memory\n"),
1053 if (fsize != GNUNET_DISK_fn_read (fn, data, fsize))
1055 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1056 _("Failed to read friends list from `%s'\n"), fn);
1063 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
1065 while ((fsize >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1067 fsize - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1069 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1072 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1074 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1076 ("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1077 (unsigned long long) pos);
1079 while ((pos < fsize) && (!isspace ((unsigned char) data[pos])))
1083 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1085 GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1087 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1089 ("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1090 (unsigned long long) pos, &enc);
1094 if (0 != memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1097 fl = make_peer (&pid, NULL, GNUNET_YES);
1098 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1099 _("Found friend `%s' in configuration\n"),
1100 GNUNET_i2s (&fl->pid));
1104 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1105 _("Found myself `%s' in friend list (useless, ignored)\n"),
1109 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1110 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
1115 GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
1116 entries_found, GNUNET_NO);
1117 if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1121 ("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1123 if ((minimum_friend_count > target_connection_count) &&
1124 (friends_only == GNUNET_NO))
1126 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1128 ("More friendly connections required than target total number of connections.\n"));
1134 * This function is called whenever an encrypted HELLO message is
1137 * @param cls closure
1138 * @param other the other peer involved (sender or receiver, NULL
1139 * for loopback messages where we are both sender and receiver)
1140 * @param message the actual HELLO message
1141 * @param atsi performance data
1142 * @param atsi_count number of records in 'atsi'
1143 * @return GNUNET_OK to keep the connection open,
1144 * GNUNET_SYSERR to close it (signal serious error)
1147 handle_encrypted_hello (void *cls, const struct GNUNET_PeerIdentity *other,
1148 const struct GNUNET_MessageHeader *message,
1149 const struct GNUNET_ATS_Information *atsi,
1150 unsigned int atsi_count)
1153 struct GNUNET_PeerIdentity pid;
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received encrypted `%s' from peer `%s'",
1156 "HELLO", GNUNET_i2s (other));
1158 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1160 GNUNET_break_op (0);
1161 return GNUNET_SYSERR;
1163 GNUNET_STATISTICS_update (stats, gettext_noop ("# HELLO messages received"),
1165 peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
1168 if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
1173 if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
1175 if ((GNUNET_YES != peer->is_friend) &&
1176 (friend_count < minimum_friend_count))
1179 if (transport != NULL)
1180 GNUNET_TRANSPORT_offer_hello (transport, message, NULL, NULL);
1186 * Function to fill send buffer with HELLO.
1188 * @param cls 'struct Peer' of the target peer
1189 * @param size number of bytes available in buf
1190 * @param buf where the callee should write the message
1191 * @return number of bytes written to buf
1194 hello_advertising_ready (void *cls, size_t size, void *buf)
1196 struct Peer *pl = cls;
1197 struct FindAdvHelloContext fah;
1200 pl->hello_req = NULL;
1201 GNUNET_assert (GNUNET_YES == pl->is_connected);
1202 /* find applicable HELLOs */
1205 fah.max_size = size;
1206 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1207 GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
1209 if (fah.result != NULL)
1211 want = GNUNET_HELLO_size (fah.result->hello);
1212 GNUNET_assert (want <= size);
1213 memcpy (buf, fah.result->hello, want);
1214 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &pl->pid.hashPubKey);
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' with %u bytes", "HELLO",
1216 (unsigned int) want);
1217 GNUNET_STATISTICS_update (stats,
1218 gettext_noop ("# HELLO messages gossipped"), 1,
1222 if (pl->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
1223 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1224 pl->next_hello_allowed =
1225 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1226 pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1232 * Last task run during shutdown. Disconnects us from
1233 * the transport and core.
1235 * @param cls unused, NULL
1236 * @param tc scheduler context
1239 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1241 if (NULL != peerinfo_notify)
1243 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1244 peerinfo_notify = NULL;
1246 GNUNET_TRANSPORT_disconnect (transport);
1250 GNUNET_CORE_disconnect (handle);
1254 if (GNUNET_SCHEDULER_NO_TASK != add_task)
1256 GNUNET_SCHEDULER_cancel (add_task);
1257 add_task = GNUNET_SCHEDULER_NO_TASK;
1259 GNUNET_CONTAINER_multihashmap_iterate (peers, &free_peer, NULL);
1260 GNUNET_CONTAINER_multihashmap_destroy (peers);
1264 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1271 * Main function that will be run.
1273 * @param cls closure
1274 * @param args remaining command-line arguments
1275 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1276 * @param c configuration
1279 run (void *cls, char *const *args, const char *cfgfile,
1280 const struct GNUNET_CONFIGURATION_Handle *c)
1282 static struct GNUNET_CORE_MessageHandler handlers[] = {
1283 {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1286 unsigned long long opt;
1289 stats = GNUNET_STATISTICS_create ("topology", cfg);
1291 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY");
1293 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "MINIMUM-FRIENDS",
1296 minimum_friend_count = (unsigned int) opt;
1298 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY",
1299 "TARGET-CONNECTION-COUNT", &opt))
1301 target_connection_count = (unsigned int) opt;
1302 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2, GNUNET_NO);
1304 if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0))
1305 read_friends_file (cfg);
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Topology would like %u connections with at least %u friends\n",
1308 target_connection_count, minimum_friend_count);
1309 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
1310 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
1311 transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
1313 GNUNET_CORE_connect (cfg, NULL, &core_init, &connect_notify,
1314 &disconnect_notify, NULL, GNUNET_NO, NULL, GNUNET_NO,
1316 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
1318 if (NULL == transport)
1320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1321 _("Failed to connect to `%s' service.\n"), "transport");
1322 GNUNET_SCHEDULER_shutdown ();
1327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1328 _("Failed to connect to `%s' service.\n"), "core");
1329 GNUNET_SCHEDULER_shutdown ();
1336 * The main function for the topology daemon.
1338 * @param argc number of arguments from the command line
1339 * @param argv command line arguments
1340 * @return 0 ok, 1 on error
1343 main (int argc, char *const *argv)
1345 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1346 GNUNET_GETOPT_OPTION_END
1350 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1355 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",
1357 ("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1358 options, &run, NULL)) ? 0 : 1;
1359 GNUNET_free ((void*) argv);
1368 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1370 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1372 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1373 mallopt (M_TOP_PAD, 1 * 1024);
1378 /* end of gnunet-daemon-topology.c */