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 * Our handle for the request to connect to this peer; NULL if no
93 * such request is pending.
95 struct GNUNET_CORE_PeerRequestHandle *connect_req;
98 * Pointer to the HELLO message of this peer; can be NULL.
100 struct GNUNET_HELLO_Message *hello;
103 * Bloom filter used to mark which peers already got the HELLO
106 struct GNUNET_CONTAINER_BloomFilter *filter;
109 * Until what time should we not try to connect again
112 struct GNUNET_TIME_Absolute greylisted_until;
115 * Next time we are allowed to transmit a HELLO to this peer?
117 struct GNUNET_TIME_Absolute next_hello_allowed;
120 * When should we reset the bloom filter of this entry?
122 struct GNUNET_TIME_Absolute filter_expiration;
125 * ID of task we use to wait for the time to send the next HELLO
128 GNUNET_SCHEDULER_TaskIdentifier hello_delay_task;
131 * ID of task we use to clear peers from the greylist.
133 GNUNET_SCHEDULER_TaskIdentifier greylist_clean_task;
136 * How often have we tried so far?
138 unsigned int connect_attempts;
141 * Is this peer listed here because he is a friend?
146 * Are we connected to this peer right now?
154 * Our peerinfo notification context. We use notification
155 * to instantly learn about new peers as they are discovered.
157 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
162 static const struct GNUNET_CONFIGURATION_Handle *cfg;
165 * Handle to the core API.
167 static struct GNUNET_CORE_Handle *handle;
170 * Handle to the transport API.
172 static struct GNUNET_TRANSPORT_Handle *transport;
175 * Identity of this peer.
177 static struct GNUNET_PeerIdentity my_identity;
180 * All of our friends, all of our current neighbours and all peers for
181 * which we have HELLOs. So pretty much everyone. Maps peer identities
182 * to 'struct Peer *' values.
184 static struct GNUNET_CONTAINER_MultiHashMap *peers;
187 * Handle for reporting statistics.
189 static struct GNUNET_STATISTICS_Handle *stats;
192 * Blacklist (NULL if we have none).
194 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
197 * Task scheduled to try to add peers.
199 static GNUNET_SCHEDULER_TaskIdentifier add_task;
202 * Flag to disallow non-friend connections (pure F2F mode).
204 static int friends_only;
207 * Minimum number of friends to have in the
208 * connection set before we allow non-friends.
210 static unsigned int minimum_friend_count;
213 * Number of peers (friends and others) that we are currently connected to.
215 static unsigned int connection_count;
218 * Target number of connections.
220 static unsigned int target_connection_count;
223 * Number of friends that we are currently connected to.
225 static unsigned int friend_count;
228 * Should the topology daemon try to establish connections?
230 static int autoconnect;
234 * Function that decides if a connection is acceptable or not.
235 * If we have a blacklist, only friends are allowed, so the check
239 * @param pid peer to approve or disapproave
240 * @return GNUNET_OK if the connection is allowed
243 blacklist_check (void *cls, const struct GNUNET_PeerIdentity *pid)
247 pos = GNUNET_CONTAINER_multihashmap_get (peers, &pid->hashPubKey);
248 if ((pos != NULL) && (pos->is_friend == GNUNET_YES))
250 GNUNET_STATISTICS_update (stats,
251 gettext_noop ("# peers blacklisted"), 1, GNUNET_NO);
252 return GNUNET_SYSERR;
257 * Whitelist all peers that we blacklisted; we've passed
258 * the minimum number of friends.
263 if (blacklist != NULL)
265 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
272 * Function called by core when our request to connect was transmitted.
274 * @param cls the 'struct Peer' for which we issued the connect request
275 * @param success was the request transmitted
278 connect_completed_callback (void *cls, int success)
280 struct Peer *pos = cls;
282 pos->connect_req = NULL;
287 * Check if an additional connection from the given peer is allowed.
289 * @param peer connection to check
290 * @return GNUNET_OK if the connection is allowed
293 is_connection_allowed (struct Peer *peer)
295 if (0 == memcmp (&my_identity,
296 &peer->pid, sizeof (struct GNUNET_PeerIdentity)))
297 return GNUNET_SYSERR; /* disallow connections to self */
300 if (GNUNET_YES == friends_only)
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Determined that `%s' is not allowed to connect (not a friend)\n",
305 GNUNET_i2s (&peer->pid));
307 return GNUNET_SYSERR;
309 if (friend_count >= minimum_friend_count)
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
314 GNUNET_i2s (&peer->pid));
316 return GNUNET_SYSERR;
321 * Free all resources associated with the given peer.
323 * @param cls closure (not used)
324 * @param pid identity of the peer
325 * @param value peer to free
326 * @return GNUNET_YES (always: continue to iterate)
329 free_peer (void *cls, const GNUNET_HashCode * pid, void *value)
331 struct Peer *pos = value;
333 GNUNET_break (GNUNET_OK ==
334 GNUNET_CONTAINER_multihashmap_remove (peers, pid, pos));
335 if (pos->hello_req != NULL)
336 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
337 if (pos->connect_req != NULL)
338 GNUNET_CORE_peer_request_connect_cancel (pos->connect_req);
339 if (pos->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
340 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
341 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
342 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
343 GNUNET_free_non_null (pos->hello);
344 if (pos->filter != NULL)
345 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
352 * Discard peer entries for greylisted peers
353 * where the greylisting has expired.
355 * @param cls 'struct Peer' to greylist
356 * @param tc scheduler context
359 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
363 * Try to connect to the specified peer.
365 * @param pos peer to connect to
368 attempt_connect (struct Peer *pos)
370 struct GNUNET_TIME_Relative rem;
372 if ((connection_count >= target_connection_count) &&
373 (friend_count >= minimum_friend_count))
375 if (GNUNET_YES == pos->is_connected)
377 if (GNUNET_OK != is_connection_allowed (pos))
379 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
381 if (GNUNET_YES == pos->is_friend)
382 rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
384 rem = GREYLIST_AFTER_ATTEMPT;
385 rem = GNUNET_TIME_relative_multiply (rem, connection_count);
386 rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
387 if (pos->connect_attempts > 30)
388 pos->connect_attempts = 30;
389 rem = GNUNET_TIME_relative_multiply (rem, 1 << (++pos->connect_attempts));
390 rem = GNUNET_TIME_relative_max (rem, GREYLIST_AFTER_ATTEMPT_MIN);
391 rem = GNUNET_TIME_relative_min (rem, GREYLIST_AFTER_ATTEMPT_MAX);
392 pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
393 if (pos->greylist_clean_task != GNUNET_SCHEDULER_NO_TASK)
394 GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
395 pos->greylist_clean_task
396 = GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Asking to connect to `%s'\n", GNUNET_i2s (&pos->pid));
401 GNUNET_STATISTICS_update (stats,
402 gettext_noop ("# connect requests issued to core"),
404 pos->connect_req = GNUNET_CORE_peer_request_connect (handle,
406 &connect_completed_callback,
412 * Discard peer entries for greylisted peers
413 * where the greylisting has expired.
415 * @param cls 'struct Peer' to greylist
416 * @param tc scheduler context
419 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
421 struct Peer *pos = cls;
422 struct GNUNET_TIME_Relative rem;
424 pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
425 rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
426 if (rem.rel_value == 0)
428 attempt_connect (pos);
432 pos->greylist_clean_task
433 = GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
435 if ((GNUNET_NO == pos->is_friend) &&
436 (GNUNET_NO == pos->is_connected) && (NULL == pos->hello))
438 free_peer (NULL, &pos->pid.hashPubKey, pos);
445 * Create a new entry in the peer list.
447 * @param peer identity of the new entry
448 * @param hello hello message, can be NULL
449 * @param is_friend is the new entry for a friend?
450 * @return the new entry
453 make_peer (const struct
454 GNUNET_PeerIdentity *peer,
455 const struct GNUNET_HELLO_Message *hello, int is_friend)
459 ret = GNUNET_malloc (sizeof (struct Peer));
461 ret->is_friend = is_friend;
464 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
465 memcpy (ret->hello, hello, GNUNET_HELLO_size (hello));
467 GNUNET_break (GNUNET_OK ==
468 GNUNET_CONTAINER_multihashmap_put (peers,
471 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
477 * Setup bloom filter for the given peer entry.
479 * @param peer entry to initialize
482 setup_filter (struct Peer *peer)
484 /* 2^{-5} chance of not sending a HELLO to a peer is
485 * acceptably small (if the filter is 50% full);
486 * 64 bytes of memory are small compared to the rest
487 * of the data structure and would only really become
488 * "useless" once a HELLO has been passed on to ~100
489 * other peers, which is likely more than enough in
490 * any case; hence 64, 5 as bloomfilter parameters. */
491 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
492 peer->filter_expiration =
493 GNUNET_TIME_relative_to_absolute
494 (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
495 /* never send a peer its own HELLO */
496 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
501 * Function to fill send buffer with HELLO.
503 * @param cls 'struct Peer' of the target peer
504 * @param size number of bytes available in buf
505 * @param buf where the callee should write the message
506 * @return number of bytes written to buf
508 static size_t hello_advertising_ready (void *cls, size_t size, void *buf);
512 * Closure for 'find_advertisable_hello'.
514 struct FindAdvHelloContext
518 * Peer we want to advertise to.
523 * Where to store the result (peer selected for advertising).
528 * Maximum HELLO size we can use right now.
532 struct GNUNET_TIME_Relative next_adv;
537 * Find a peer that would be reasonable for advertising.
540 * @param pid identity of a peer
541 * @param value 'struct Peer*' for the peer we are considering
542 * @return GNUNET_YES (continue iteration)
545 find_advertisable_hello (void *cls, const GNUNET_HashCode * pid, void *value)
547 struct FindAdvHelloContext *fah = cls;
548 struct Peer *pos = value;
549 struct GNUNET_TIME_Relative rst_time;
552 if (pos == fah->peer)
554 if (pos->hello == NULL)
556 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
557 if (0 == rst_time.rel_value)
559 /* time to discard... */
560 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
563 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
564 hs = GNUNET_HELLO_size (pos->hello);
565 if (hs > fah->max_size)
568 GNUNET_CONTAINER_bloomfilter_test (pos->filter,
569 &fah->peer->pid.hashPubKey))
576 * Calculate when we would like to send the next HELLO to this
577 * peer and ask for it.
579 * @param cls for which peer to schedule the HELLO
580 * @param tc task context
583 schedule_next_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
585 struct Peer *pl = cls;
586 struct FindAdvHelloContext fah;
588 struct GNUNET_TIME_Relative delay;
590 pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
591 GNUNET_assert (GNUNET_YES == pl->is_connected);
592 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
593 return; /* we're out of here */
594 if (pl->hello_req != NULL)
595 return; /* did not finish sending the previous one */
596 /* find applicable HELLOs */
599 fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
600 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
601 GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
603 = GNUNET_SCHEDULER_add_delayed (fah.next_adv, &schedule_next_hello, pl);
604 if (fah.result == NULL)
606 next_want = GNUNET_HELLO_size (fah.result->hello);
607 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
608 if (delay.rel_value == 0)
611 pl->hello_req = GNUNET_CORE_notify_transmit_ready (handle,
614 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
617 &hello_advertising_ready,
624 * Cancel existing requests for sending HELLOs to this peer
625 * and recalculate when we should send HELLOs to it based
626 * on our current state (something changed!).
628 * @param cls closure, 'struct Peer' to skip, or NULL
629 * @param pid identity of a peer
630 * @param value 'struct Peer*' for the peer
631 * @return GNUNET_YES (always)
634 reschedule_hellos (void *cls, const GNUNET_HashCode * pid, void *value)
636 struct Peer *peer = value;
637 struct Peer *skip = cls;
641 if (!peer->is_connected)
643 if (peer->hello_req != NULL)
645 GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
646 peer->hello_req = NULL;
648 if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
650 GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
651 peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
653 peer->hello_delay_task
654 = GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
660 * Method called whenever a peer connects.
663 * @param peer peer identity this notification is about
664 * @param atsi performance data
667 connect_notify (void *cls,
669 GNUNET_PeerIdentity *peer,
670 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Core told us that we are connecting to `%s'\n",
679 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
683 GNUNET_STATISTICS_set (stats,
684 gettext_noop ("# peers connected"),
685 connection_count, GNUNET_NO);
686 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
689 pos = make_peer (peer, NULL, GNUNET_NO);
690 GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
694 GNUNET_assert (GNUNET_NO == pos->is_connected);
695 pos->greylisted_until.abs_value = 0; /* remove greylisting */
697 pos->is_connected = GNUNET_YES;
698 pos->connect_attempts = 0; /* re-set back-off factor */
701 if ((friend_count == minimum_friend_count - 1) &&
702 (GNUNET_YES != friends_only))
705 GNUNET_STATISTICS_set (stats,
706 gettext_noop ("# friends connected"),
707 friend_count, GNUNET_NO);
709 reschedule_hellos (NULL, &peer->hashPubKey, pos);
714 * Try to add more peers to our connection set.
716 * @param cls closure, not used
717 * @param pid identity of a peer
718 * @param value 'struct Peer*' for the peer
719 * @return GNUNET_YES (continue to iterate)
722 try_add_peers (void *cls, const GNUNET_HashCode * pid, void *value)
724 struct Peer *pos = value;
726 attempt_connect (pos);
732 * Last task run during shutdown. Disconnects us from
733 * the transport and core.
735 * @param cls unused, NULL
736 * @param tc scheduler context
739 add_peer_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
741 add_task = GNUNET_SCHEDULER_NO_TASK;
743 GNUNET_CONTAINER_multihashmap_iterate (peers, &try_add_peers, NULL);
747 * Method called whenever a peer disconnects.
750 * @param peer peer identity this notification is about
753 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
757 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761 "Core told us that we disconnected from `%s'\n",
764 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
770 if (pos->is_connected != GNUNET_YES)
775 pos->is_connected = GNUNET_NO;
777 if (NULL != pos->hello_req)
779 GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
780 pos->hello_req = NULL;
782 if (GNUNET_SCHEDULER_NO_TASK != pos->hello_delay_task)
784 GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
785 pos->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
787 GNUNET_STATISTICS_set (stats,
788 gettext_noop ("# peers connected"),
789 connection_count, GNUNET_NO);
793 GNUNET_STATISTICS_set (stats,
794 gettext_noop ("# friends connected"),
795 friend_count, GNUNET_NO);
797 if (((connection_count < target_connection_count) ||
798 (friend_count < minimum_friend_count)) &&
799 (GNUNET_SCHEDULER_NO_TASK == add_task))
800 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
801 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
802 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
807 * Iterator called on each address.
809 * @param cls flag that we will set if we see any addresses
810 * @param tname name of the transport
811 * @param expiration when will the given address expire
812 * @param addr the address of the peer
813 * @param addrlen number of bytes in addr
814 * @return GNUNET_SYSERR always, to terminate iteration
817 address_iterator (void *cls,
819 struct GNUNET_TIME_Absolute expiration,
820 const void *addr, uint16_t addrlen)
825 return GNUNET_SYSERR;
830 * We've gotten a HELLO from another peer. Consider it for
833 * @param hello the HELLO we got
836 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
839 struct GNUNET_PeerIdentity pid;
840 struct GNUNET_TIME_Absolute dt;
841 struct GNUNET_HELLO_Message *nh;
845 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
850 if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
851 return; /* that's me! */
852 have_address = GNUNET_NO;
853 GNUNET_HELLO_iterate_addresses (hello,
854 GNUNET_NO, &address_iterator, &have_address);
855 if (GNUNET_NO == have_address)
856 return; /* no point in advertising this one... */
857 peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
860 peer = make_peer (&pid, hello, GNUNET_NO);
862 else if (peer->hello != NULL)
864 dt = GNUNET_HELLO_equals (peer->hello, hello, GNUNET_TIME_absolute_get ());
865 if (dt.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
866 return; /* nothing new here */
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Found `%s' from peer `%s' for advertising\n",
871 "HELLO", GNUNET_i2s (&pid));
873 if (peer->hello != NULL)
875 nh = GNUNET_HELLO_merge (peer->hello, hello);
876 GNUNET_free (peer->hello);
881 size = GNUNET_HELLO_size (hello);
882 peer->hello = GNUNET_malloc (size);
883 memcpy (peer->hello, hello, size);
885 if (peer->filter != NULL)
886 GNUNET_CONTAINER_bloomfilter_free (peer->filter);
888 /* since we have a new HELLO to pick from, re-schedule all
889 * HELLO requests that are not bound by the HELLO send rate! */
890 GNUNET_CONTAINER_multihashmap_iterate (peers, &reschedule_hellos, peer);
895 * PEERINFO calls this function to let us know about a possible peer
896 * that we might want to connect to.
898 * @param cls closure (not used)
899 * @param peer potential peer to connect to
900 * @param hello HELLO for this peer (or NULL)
901 * @param err_msg NULL if successful, otherwise contains error message
904 process_peer (void *cls,
905 const struct GNUNET_PeerIdentity *peer,
906 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 _("Error in communication with PEERINFO service: %s\n"),
915 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
916 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
919 GNUNET_assert (peer != NULL);
920 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
921 return; /* that's me! */
924 /* free existing HELLO, if any */
925 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
928 GNUNET_free_non_null (pos->hello);
930 if (pos->filter != NULL)
932 GNUNET_CONTAINER_bloomfilter_free (pos->filter);
935 if ((!pos->is_connected) &&
938 GNUNET_TIME_absolute_get_remaining (pos->
939 greylisted_until).rel_value))
940 free_peer (NULL, &pos->pid.hashPubKey, pos);
944 consider_for_advertising (hello);
945 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
947 pos = make_peer (peer, hello, GNUNET_NO);
948 GNUNET_assert (NULL != pos);
949 if (GNUNET_YES == pos->is_connected)
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "Already connected to peer `%s'\n", GNUNET_i2s (peer));
957 if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "Already tried peer `%s' recently\n", GNUNET_i2s (peer));
963 return; /* peer still greylisted */
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "Considering connecting to peer `%s'\n", GNUNET_i2s (peer));
969 attempt_connect (pos);
974 * Function called after GNUNET_CORE_connect has succeeded
975 * (or failed for good).
978 * @param server handle to the server, NULL if we failed
979 * @param my_id ID of this peer, NULL if we failed
980 * @param publicKey public key of this peer, NULL if we failed
983 core_init (void *cls,
984 struct GNUNET_CORE_Handle *server,
985 const struct GNUNET_PeerIdentity *my_id,
986 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
990 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
992 ("Failed to connect to core service, can not manage topology!\n"));
993 GNUNET_SCHEDULER_shutdown ();
997 my_identity = *my_id;
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am peer `%s'\n", GNUNET_i2s (my_id));
1001 peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
1006 * Read the friends file.
1009 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1014 struct GNUNET_PeerIdentity pid;
1016 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1017 unsigned int entries_found;
1021 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
1023 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1024 _("Option `%s' in section `%s' not specified!\n"),
1025 "FRIENDS", "TOPOLOGY");
1028 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1029 GNUNET_DISK_fn_write (fn, NULL, 0,
1030 GNUNET_DISK_PERM_USER_READ
1031 | GNUNET_DISK_PERM_USER_WRITE);
1032 if (0 != STAT (fn, &frstat))
1034 if ((friends_only) || (minimum_friend_count > 0))
1035 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1036 _("Could not read friends list `%s'\n"), fn);
1040 if (frstat.st_size == 0)
1042 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1043 _("Friends file `%s' is empty.\n"), fn);
1047 data = GNUNET_malloc_large (frstat.st_size);
1050 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1051 _("Failed to read friends list from `%s': out of memory\n"),
1056 if (frstat.st_size != GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1058 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1059 _("Failed to read friends list from `%s'\n"), fn);
1066 while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1068 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1070 frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1072 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1075 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1077 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1079 ("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1080 (unsigned long long) pos);
1082 while ((pos < frstat.st_size) && (!isspace ((unsigned char) data[pos])))
1086 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1088 GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1090 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1092 ("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1093 (unsigned long long) pos, &enc);
1097 if (0 != memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1100 fl = make_peer (&pid, NULL, GNUNET_YES);
1101 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1102 _("Found friend `%s' in configuration\n"),
1103 GNUNET_i2s (&fl->pid));
1107 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1108 _("Found myself `%s' in friend list (useless, ignored)\n"),
1112 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1113 while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1118 GNUNET_STATISTICS_update (stats,
1119 gettext_noop ("# friends in configuration"),
1120 entries_found, GNUNET_NO);
1121 if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1123 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1125 ("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1127 if ((minimum_friend_count > target_connection_count) &&
1128 (friends_only == GNUNET_NO))
1130 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1132 ("More friendly connections required than target total number of connections.\n"));
1138 * This function is called whenever an encrypted HELLO message is
1141 * @param cls closure
1142 * @param other the other peer involved (sender or receiver, NULL
1143 * for loopback messages where we are both sender and receiver)
1144 * @param message the actual HELLO message
1145 * @param atsi performance data
1146 * @return GNUNET_OK to keep the connection open,
1147 * GNUNET_SYSERR to close it (signal serious error)
1150 handle_encrypted_hello (void *cls,
1151 const struct GNUNET_PeerIdentity *other,
1152 const struct GNUNET_MessageHeader *message,
1153 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1156 struct GNUNET_PeerIdentity pid;
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "Received encrypted `%s' from peer `%s'",
1161 "HELLO", GNUNET_i2s (other));
1164 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1166 GNUNET_break_op (0);
1167 return GNUNET_SYSERR;
1169 GNUNET_STATISTICS_update (stats,
1170 gettext_noop ("# HELLO messages received"),
1172 peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
1175 if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
1180 if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
1182 if ((GNUNET_YES != peer->is_friend) &&
1183 (friend_count < minimum_friend_count))
1186 if (transport != NULL)
1187 GNUNET_TRANSPORT_offer_hello (transport, message, NULL, NULL);
1193 * Function to fill send buffer with HELLO.
1195 * @param cls 'struct Peer' of the target peer
1196 * @param size number of bytes available in buf
1197 * @param buf where the callee should write the message
1198 * @return number of bytes written to buf
1201 hello_advertising_ready (void *cls, size_t size, void *buf)
1203 struct Peer *pl = cls;
1204 struct FindAdvHelloContext fah;
1207 pl->hello_req = NULL;
1208 GNUNET_assert (GNUNET_YES == pl->is_connected);
1209 /* find applicable HELLOs */
1212 fah.max_size = size;
1213 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1214 GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
1216 if (fah.result != NULL)
1218 want = GNUNET_HELLO_size (fah.result->hello);
1219 GNUNET_assert (want <= size);
1220 memcpy (buf, fah.result->hello, want);
1221 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &pl->pid.hashPubKey);
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1224 "Sending `%s' with %u bytes", "HELLO", (unsigned int) want);
1226 GNUNET_STATISTICS_update (stats,
1227 gettext_noop ("# HELLO messages gossipped"),
1231 if (pl->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
1232 GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1233 pl->next_hello_allowed =
1234 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1235 pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1241 * Last task run during shutdown. Disconnects us from
1242 * the transport and core.
1244 * @param cls unused, NULL
1245 * @param tc scheduler context
1248 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1250 if (NULL != peerinfo_notify)
1252 GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1253 peerinfo_notify = NULL;
1255 if (GNUNET_SCHEDULER_NO_TASK != add_task)
1257 GNUNET_SCHEDULER_cancel (add_task);
1258 add_task = GNUNET_SCHEDULER_NO_TASK;
1260 GNUNET_TRANSPORT_disconnect (transport);
1262 GNUNET_CONTAINER_multihashmap_iterate (peers, &free_peer, NULL);
1263 GNUNET_CONTAINER_multihashmap_destroy (peers);
1266 GNUNET_CORE_disconnect (handle);
1272 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1279 * Main function that will be run.
1281 * @param cls closure
1282 * @param args remaining command-line arguments
1283 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1284 * @param c configuration
1289 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1291 static struct GNUNET_CORE_MessageHandler handlers[] = {
1292 {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1295 unsigned long long opt;
1298 stats = GNUNET_STATISTICS_create ("topology", cfg);
1299 autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1302 friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1306 GNUNET_CONFIGURATION_get_value_number (cfg,
1308 "MINIMUM-FRIENDS", &opt))
1310 minimum_friend_count = (unsigned int) opt;
1312 GNUNET_CONFIGURATION_get_value_number (cfg,
1314 "TARGET-CONNECTION-COUNT", &opt))
1316 target_connection_count = (unsigned int) opt;
1317 peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1319 if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0))
1320 read_friends_file (cfg);
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "Topology would like %u connections with at least %u friends (%s)\n",
1324 target_connection_count,
1325 minimum_friend_count,
1326 autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1328 if ((friend_count < minimum_friend_count) && (blacklist == NULL))
1329 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
1330 transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
1331 handle = GNUNET_CORE_connect (cfg,
1338 NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
1339 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1340 &cleaning_task, NULL);
1341 if (NULL == transport)
1343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1344 _("Failed to connect to `%s' service.\n"), "transport");
1345 GNUNET_SCHEDULER_shutdown ();
1350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1351 _("Failed to connect to `%s' service.\n"), "core");
1352 GNUNET_SCHEDULER_shutdown ();
1359 * The main function for the topology daemon.
1361 * @param argc number of arguments from the command line
1362 * @param argv command line arguments
1363 * @return 0 ok, 1 on error
1366 main (int argc, char *const *argv)
1368 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1369 GNUNET_GETOPT_OPTION_END
1374 GNUNET_PROGRAM_run (argc,
1376 "gnunet-daemon-topology",
1378 ("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1379 options, &run, NULL)) ? 0 : 1;
1383 /* end of gnunet-daemon-topology.c */