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
689 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "Core told us that we are connecting to `%s'\n",
696 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
700 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
701 connection_count, GNUNET_NO);
702 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
705 pos = make_peer (peer, NULL, GNUNET_NO);
706 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
710 GNUNET_assert (GNUNET_NO == pos->is_connected);
711 pos->greylisted_until.abs_value = 0; /* remove greylisting */
713 pos->is_connected = GNUNET_YES;
714 pos->connect_attempts = 0; /* re-set back-off factor */
717 if ((friend_count == minimum_friend_count - 1) &&
718 (GNUNET_YES != friends_only))
721 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
722 friend_count, GNUNET_NO);
724 reschedule_hellos (NULL, &peer->hashPubKey, pos);
729 * Try to add more peers to our connection set.
731 * @param cls closure, not used
732 * @param pid identity of a peer
733 * @param value 'struct Peer*' for the peer
734 * @return GNUNET_YES (continue to iterate)
737 try_add_peers (void *cls, const struct GNUNET_HashCode * pid, void *value)
739 struct Peer *pos = value;
741 schedule_attempt_connect (pos);
747 * Last task run during shutdown. Disconnects us from
748 * the transport and core.
750 * @param cls unused, NULL
751 * @param tc scheduler context
754 add_peer_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
756 add_task = GNUNET_SCHEDULER_NO_TASK;
758 GNUNET_CONTAINER_multihashmap_iterate (peers, &try_add_peers, NULL);
763 * Method called whenever a peer disconnects.
766 * @param peer peer identity this notification is about
769 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
773 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Core told us that we disconnected from `%s'\n",
778 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
784 if (pos->is_connected != GNUNET_YES)
789 pos->is_connected = GNUNET_NO;
791 if (NULL != pos->hello_req)
793 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
794 pos->hello_req = NULL;
796 if (GNUNET_SCHEDULER_NO_TASK != pos->hello_delay_task)
798 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
799 pos->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
801 GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
802 connection_count, GNUNET_NO);
806 GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
807 friend_count, GNUNET_NO);
809 if (((connection_count < target_connection_count) ||
810 (friend_count < minimum_friend_count)) &&
811 (GNUNET_SCHEDULER_NO_TASK == add_task))
812 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
813 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
814 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
819 * Iterator called on each address.
821 * @param cls flag that we will set if we see any addresses
822 * @param address the address of the peer
823 * @param expiration when will the given address expire
824 * @return GNUNET_SYSERR always, to terminate iteration
827 address_iterator (void *cls, const struct GNUNET_HELLO_Address *address,
828 struct GNUNET_TIME_Absolute expiration)
833 return GNUNET_SYSERR;
838 * We've gotten a HELLO from another peer. Consider it for
841 * @param hello the HELLO we got
844 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
847 struct GNUNET_PeerIdentity pid;
848 struct GNUNET_TIME_Absolute dt;
849 struct GNUNET_HELLO_Message *nh;
853 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
858 if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
859 return; /* that's me! */
860 have_address = GNUNET_NO;
861 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &address_iterator,
863 if (GNUNET_NO == have_address)
864 return; /* no point in advertising this one... */
865 peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
868 peer = make_peer (&pid, hello, GNUNET_NO);
870 else if (peer->hello != NULL)
872 dt = GNUNET_HELLO_equals (peer->hello, hello, GNUNET_TIME_absolute_get ());
873 if (dt.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
874 return; /* nothing new here */
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
877 "Found `%s' from peer `%s' for advertising\n", "HELLO",
879 if (peer->hello != NULL)
881 nh = GNUNET_HELLO_merge (peer->hello, hello);
882 GNUNET_free (peer->hello);
887 size = GNUNET_HELLO_size (hello);
888 peer->hello = GNUNET_malloc (size);
889 memcpy (peer->hello, hello, size);
891 if (peer->filter != NULL)
892 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
894 /* since we have a new HELLO to pick from, re-schedule all
895 * HELLO requests that are not bound by the HELLO send rate! */
896 GNUNET_CONTAINER_multihashmap_iterate (peers, &reschedule_hellos, peer);
901 * PEERINFO calls this function to let us know about a possible peer
902 * that we might want to connect to.
904 * @param cls closure (not used)
905 * @param peer potential peer to connect to
906 * @param hello HELLO for this peer (or NULL)
907 * @param err_msg NULL if successful, otherwise contains error message
910 process_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
911 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 _("Error in communication with PEERINFO service: %s\n"),
920 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
921 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO, &process_peer, NULL);
924 GNUNET_assert (peer != NULL);
925 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
926 return; /* that's me! */
929 /* free existing HELLO, if any */
930 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
933 GNUNET_free_non_null (pos->hello);
935 if (pos->filter != NULL)
937 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
940 if ((GNUNET_NO == pos->is_connected) && (GNUNET_NO == pos->is_friend) &&
942 GNUNET_TIME_absolute_get_remaining (pos->
943 greylisted_until).rel_value))
944 free_peer (NULL, &pos->pid.hashPubKey, pos);
948 consider_for_advertising (hello);
949 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
951 pos = make_peer (peer, hello, GNUNET_NO);
952 GNUNET_assert (NULL != pos);
953 if (GNUNET_YES == pos->is_connected)
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already connected to peer `%s'\n",
959 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already tried peer `%s' recently\n",
963 return; /* peer still greylisted */
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Considering connecting to peer `%s'\n",
967 schedule_attempt_connect (pos);
972 * Function called after GNUNET_CORE_connect has succeeded
973 * (or failed for good).
976 * @param server handle to the server, NULL if we failed
977 * @param my_id ID of this peer, NULL if we failed
980 core_init (void *cls, struct GNUNET_CORE_Handle *server,
981 const struct GNUNET_PeerIdentity *my_id)
985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
987 ("Failed to connect to core service, can not manage topology!\n"));
988 GNUNET_SCHEDULER_shutdown ();
992 my_identity = *my_id;
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am peer `%s'\n", GNUNET_i2s (my_id));
994 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO, &process_peer, NULL);
999 * Read the friends file.
1002 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1007 struct GNUNET_PeerIdentity pid;
1009 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1010 unsigned int entries_found;
1014 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
1016 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1017 "topology", "FRIENDS");
1020 if ( (GNUNET_OK != GNUNET_DISK_file_test (fn)) &&
1021 (GNUNET_OK != GNUNET_DISK_fn_write (fn, NULL, 0,
1022 GNUNET_DISK_PERM_USER_READ |
1023 GNUNET_DISK_PERM_USER_WRITE)) )
1024 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
1025 if (GNUNET_OK != GNUNET_DISK_file_size (fn,
1026 &fsize, GNUNET_NO, GNUNET_YES))
1028 if ((friends_only) || (minimum_friend_count > 0))
1029 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1030 _("Could not read friends list `%s'\n"), fn);
1036 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Friends file `%s' is empty.\n"),
1041 data = GNUNET_malloc_large (fsize);
1044 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1045 _("Failed to read friends list from `%s': out of memory\n"),
1050 if (fsize != GNUNET_DISK_fn_read (fn, data, fsize))
1052 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1053 _("Failed to read friends list from `%s'\n"), fn);
1060 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
1062 while ((fsize >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1064 fsize - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1066 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1069 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1071 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1073 ("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1074 (unsigned long long) pos);
1076 while ((pos < fsize) && (!isspace ((unsigned char) data[pos])))
1080 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1082 GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1084 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1086 ("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1087 (unsigned long long) pos, &enc);
1091 if (0 != memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1094 fl = make_peer (&pid, NULL, GNUNET_YES);
1095 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1096 _("Found friend `%s' in configuration\n"),
1097 GNUNET_i2s (&fl->pid));
1101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1102 _("Found myself `%s' in friend list (useless, ignored)\n"),
1106 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1107 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
1112 GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
1113 entries_found, GNUNET_NO);
1114 if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1116 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1118 ("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1120 if ((minimum_friend_count > target_connection_count) &&
1121 (friends_only == GNUNET_NO))
1123 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1125 ("More friendly connections required than target total number of connections.\n"));
1131 * This function is called whenever an encrypted HELLO message is
1134 * @param cls closure
1135 * @param other the other peer involved (sender or receiver, NULL
1136 * for loopback messages where we are both sender and receiver)
1137 * @param message the actual HELLO message
1138 * @return GNUNET_OK to keep the connection open,
1139 * GNUNET_SYSERR to close it (signal serious error)
1142 handle_encrypted_hello (void *cls, const struct GNUNET_PeerIdentity *other,
1143 const struct GNUNET_MessageHeader *message)
1146 struct GNUNET_PeerIdentity pid;
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received encrypted `%s' from peer `%s'",
1149 "HELLO", GNUNET_i2s (other));
1151 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1153 GNUNET_break_op (0);
1154 return GNUNET_SYSERR;
1156 GNUNET_STATISTICS_update (stats, gettext_noop ("# HELLO messages received"),
1158 peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
1161 if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
1166 if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
1168 if ((GNUNET_YES != peer->is_friend) &&
1169 (friend_count < minimum_friend_count))
1172 if (transport != NULL)
1173 GNUNET_TRANSPORT_offer_hello (transport, message, NULL, NULL);
1179 * Function to fill send buffer with HELLO.
1181 * @param cls 'struct Peer' of the target peer
1182 * @param size number of bytes available in buf
1183 * @param buf where the callee should write the message
1184 * @return number of bytes written to buf
1187 hello_advertising_ready (void *cls, size_t size, void *buf)
1189 struct Peer *pl = cls;
1190 struct FindAdvHelloContext fah;
1193 pl->hello_req = NULL;
1194 GNUNET_assert (GNUNET_YES == pl->is_connected);
1195 /* find applicable HELLOs */
1198 fah.max_size = size;
1199 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1200 GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
1202 if (fah.result != NULL)
1204 want = GNUNET_HELLO_size (fah.result->hello);
1205 GNUNET_assert (want <= size);
1206 memcpy (buf, fah.result->hello, want);
1207 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &pl->pid.hashPubKey);
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' with %u bytes", "HELLO",
1209 (unsigned int) want);
1210 GNUNET_STATISTICS_update (stats,
1211 gettext_noop ("# HELLO messages gossipped"), 1,
1215 if (pl->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
1216 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1217 pl->next_hello_allowed =
1218 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1219 pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1225 * Last task run during shutdown. Disconnects us from
1226 * the transport and core.
1228 * @param cls unused, NULL
1229 * @param tc scheduler context
1232 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1234 if (NULL != peerinfo_notify)
1236 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1237 peerinfo_notify = NULL;
1239 GNUNET_TRANSPORT_disconnect (transport);
1243 GNUNET_CORE_disconnect (handle);
1247 if (GNUNET_SCHEDULER_NO_TASK != add_task)
1249 GNUNET_SCHEDULER_cancel (add_task);
1250 add_task = GNUNET_SCHEDULER_NO_TASK;
1252 GNUNET_CONTAINER_multihashmap_iterate (peers, &free_peer, NULL);
1253 GNUNET_CONTAINER_multihashmap_destroy (peers);
1257 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1264 * Main function that will be run.
1266 * @param cls closure
1267 * @param args remaining command-line arguments
1268 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1269 * @param c configuration
1272 run (void *cls, char *const *args, const char *cfgfile,
1273 const struct GNUNET_CONFIGURATION_Handle *c)
1275 static struct GNUNET_CORE_MessageHandler handlers[] = {
1276 {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1279 unsigned long long opt;
1282 stats = GNUNET_STATISTICS_create ("topology", cfg);
1284 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY");
1286 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "MINIMUM-FRIENDS",
1289 minimum_friend_count = (unsigned int) opt;
1291 GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY",
1292 "TARGET-CONNECTION-COUNT", &opt))
1294 target_connection_count = (unsigned int) opt;
1295 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2, GNUNET_NO);
1297 if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0))
1298 read_friends_file (cfg);
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 "Topology would like %u connections with at least %u friends\n",
1301 target_connection_count, minimum_friend_count);
1302 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
1303 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
1304 transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
1306 GNUNET_CORE_connect (cfg, NULL, &core_init, &connect_notify,
1307 &disconnect_notify, NULL, GNUNET_NO, NULL, GNUNET_NO,
1309 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
1311 if (NULL == transport)
1313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1314 _("Failed to connect to `%s' service.\n"), "transport");
1315 GNUNET_SCHEDULER_shutdown ();
1320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1321 _("Failed to connect to `%s' service.\n"), "core");
1322 GNUNET_SCHEDULER_shutdown ();
1329 * The main function for the topology daemon.
1331 * @param argc number of arguments from the command line
1332 * @param argv command line arguments
1333 * @return 0 ok, 1 on error
1336 main (int argc, char *const *argv)
1338 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1339 GNUNET_GETOPT_OPTION_END
1343 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1348 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",
1350 ("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1351 options, &run, NULL)) ? 0 : 1;
1352 GNUNET_free ((void*) argv);
1361 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1363 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1365 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1366 mallopt (M_TOP_PAD, 1 * 1024);
1371 /* end of gnunet-daemon-topology.c */