2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2012, 2013, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file nse/gnunet-service-nse.c
23 * @brief network size estimation service
24 * @author Nathan Evans
25 * @author Christian Grothoff
27 * The purpose of this service is to estimate the size of the network.
28 * Given a specified interval, each peer hashes the most recent
29 * timestamp which is evenly divisible by that interval. This hash is
30 * compared in distance to the peer identity to choose an offset. The
31 * closer the peer identity to the hashed timestamp, the earlier the
32 * peer sends out a "nearest peer" message. The closest peer's
33 * message should thus be received before any others, which stops
34 * those peer from sending their messages at a later duration. So
35 * every peer should receive the same nearest peer message, and from
36 * this can calculate the expected number of peers in the network.
40 #include "gnunet_util_lib.h"
41 #include "gnunet_constants.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_signatures.h"
44 #include "gnunet_statistics_service.h"
45 #include "gnunet_core_service.h"
46 #include "gnunet_nse_service.h"
47 #if ENABLE_NSE_HISTOGRAM
48 #include "gnunet_testbed_logger_service.h"
55 * Should messages be delayed randomly? This option should be set to
56 * #GNUNET_NO only for experiments, not in production.
58 #define USE_RANDOM_DELAYS GNUNET_YES
61 * Generate extensive debug-level log messages?
63 #define DEBUG_NSE GNUNET_NO
66 * Over how many values do we calculate the weighted average?
68 #define HISTORY_SIZE 64
71 * Message priority to use. No real rush, reliability not
72 * required. Corking OK.
74 #define NSE_PRIORITY \
75 (GNUNET_MQ_PRIO_BACKGROUND | GNUNET_MQ_PREF_UNRELIABLE \
76 | GNUNET_MQ_PREF_CORK_ALLOWED)
79 #define log2(a) (log (a) / log (2))
83 * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
85 static unsigned long long nse_work_required;
88 * Interval for sending network size estimation flood requests.
90 static struct GNUNET_TIME_Relative gnunet_nse_interval;
93 * Interval between proof find runs.
95 static struct GNUNET_TIME_Relative proof_find_delay;
97 #if ENABLE_NSE_HISTOGRAM
100 * Handle to test if testbed logger service is running or not
102 struct GNUNET_CLIENT_TestHandle *logger_test;
105 * Handle for writing when we received messages to disk.
107 static struct GNUNET_TESTBED_LOGGER_Handle *lh;
110 * Handle for writing message received timestamp information to disk.
112 static struct GNUNET_BIO_WriteHandle *histogram;
118 * Per-peer information.
123 * Core handle for sending messages to this peer.
125 struct GNUNET_MQ_Handle *mq;
128 * What is the identity of the peer?
130 const struct GNUNET_PeerIdentity *id;
133 * Task scheduled to send message to this peer.
135 struct GNUNET_SCHEDULER_Task *transmit_task;
138 * Did we receive or send a message about the previous round
139 * to this peer yet? #GNUNET_YES if the previous round has
140 * been taken care of.
144 #if ENABLE_NSE_HISTOGRAM
146 * Amount of messages received from this peer on this round.
148 unsigned int received_messages;
151 * Amount of messages transmitted to this peer on this round.
153 unsigned int transmitted_messages;
156 * Which size did we tell the peer the network is?
158 unsigned int last_transmitted_size;
163 GNUNET_NETWORK_STRUCT_BEGIN
166 * Network size estimate reply; sent when "this"
167 * peer's timer has run out before receiving a
168 * valid reply from another peer.
170 struct GNUNET_NSE_FloodMessage
173 * Type: #GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD
175 struct GNUNET_MessageHeader header;
178 * Number of hops this message has taken so far.
180 uint32_t hop_count GNUNET_PACKED;
185 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
188 * The current timestamp value (which all
189 * peers should agree on).
191 struct GNUNET_TIME_AbsoluteNBO timestamp;
194 * Number of matching bits between the hash
195 * of timestamp and the initiator's public
198 uint32_t matching_bits GNUNET_PACKED;
201 * Public key of the originator.
203 struct GNUNET_PeerIdentity origin;
206 * Proof of work, causing leading zeros when hashed with pkey.
208 uint64_t proof_of_work GNUNET_PACKED;
211 * Signature (over range specified in purpose).
213 struct GNUNET_CRYPTO_EddsaSignature signature;
215 GNUNET_NETWORK_STRUCT_END
218 * Handle to our current configuration.
220 static const struct GNUNET_CONFIGURATION_Handle *cfg;
223 * Handle to the statistics service.
225 static struct GNUNET_STATISTICS_Handle *stats;
228 * Handle to the core service.
230 static struct GNUNET_CORE_Handle *core_api;
233 * Map of all connected peers.
235 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
238 * The current network size estimate. Number of bits matching on
241 static double current_size_estimate;
244 * The standard deviation of the last #HISTORY_SIZE network
247 static double current_std_dev = NAN;
250 * Current hop counter estimate (estimate for network diameter).
252 static uint32_t hop_count_max;
255 * Message for the next round, if we got any.
257 static struct GNUNET_NSE_FloodMessage next_message;
260 * Array of recent size estimate messages.
262 static struct GNUNET_NSE_FloodMessage size_estimate_messages[HISTORY_SIZE];
265 * Index of most recent estimate.
267 static unsigned int estimate_index;
270 * Number of valid entries in the history.
272 static unsigned int estimate_count;
275 * Task scheduled to update our flood message for the next round.
277 static struct GNUNET_SCHEDULER_Task *flood_task;
280 * Task scheduled to compute our proof.
282 static struct GNUNET_SCHEDULER_Task *proof_task;
285 * Notification context, simplifies client broadcasts.
287 static struct GNUNET_NotificationContext *nc;
290 * The next major time.
292 static struct GNUNET_TIME_Absolute next_timestamp;
295 * The current major time.
297 static struct GNUNET_TIME_Absolute current_timestamp;
300 * The private key of this peer.
302 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
305 * The peer identity of this peer.
307 static struct GNUNET_PeerIdentity my_identity;
310 * Proof of work for this peer.
312 static uint64_t my_proof;
316 * Initialize a message to clients with the current network
319 * @param em message to fill in
322 setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
332 /* Weighted incremental algorithm for stddev according to West (1979) */
344 for (unsigned int i = 0; i < estimate_count; i++)
346 unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
348 val = htonl (size_estimate_messages[j].matching_bits);
349 weight = estimate_count + 1 - i;
351 temp = weight + sumweight;
353 r = q * weight / temp;
355 sum += sumweight * q * r;
358 if (estimate_count > 0)
359 variance = (sum / sumweight) * estimate_count / (estimate_count - 1.0);
361 /* trivial version for debugging */
364 /* non-weighted trivial version */
370 for (unsigned int i = 0; i < estimate_count; i++)
372 unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
374 val = htonl (size_estimate_messages[j].matching_bits);
378 if (0 != estimate_count)
380 mean = sum / estimate_count;
381 variance = (vsq - mean * sum)
382 / (estimate_count - 1.0); // terrible for numerical stability...
386 std_dev = sqrt (variance);
388 std_dev = variance; /* must be infinity due to estimate_count == 0 */
389 current_std_dev = std_dev;
390 current_size_estimate = mean;
392 em->header.size = htons (sizeof(struct GNUNET_NSE_ClientMessage));
393 em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
394 em->reserved = htonl (0);
395 em->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
397 double se = mean - 0.332747;
398 unsigned int j = GNUNET_CONTAINER_multipeermap_size (peers);
400 j = 1; /* Avoid log2(0); can only happen if CORE didn't report
401 connection to self yet */
403 em->size_estimate = GNUNET_hton_double (GNUNET_MAX (se, nsize));
404 em->std_deviation = GNUNET_hton_double (std_dev);
405 GNUNET_STATISTICS_set (stats,
406 "# nodes in the network (estimate)",
407 (uint64_t) pow (2, GNUNET_MAX (se, nsize)),
414 * Handler for START message from client, triggers an
415 * immediate current network estimate notification.
416 * Also, we remember the client for updates upon future
417 * estimate measurements.
419 * @param cls client who sent the message
420 * @param message the message received
423 handle_start (void *cls, const struct GNUNET_MessageHeader *message)
425 struct GNUNET_SERVICE_Client *client = cls;
426 struct GNUNET_MQ_Handle *mq;
427 struct GNUNET_NSE_ClientMessage em;
428 struct GNUNET_MQ_Envelope *env;
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received START message from client\n");
432 mq = GNUNET_SERVICE_client_get_mq (client);
433 GNUNET_notification_context_add (nc, mq);
434 setup_estimate_message (&em);
435 env = GNUNET_MQ_msg_copy (&em.header);
436 GNUNET_MQ_send (mq, env);
437 GNUNET_SERVICE_client_continue (client);
442 * How long should we delay a message to go the given number of
445 * @param matching_bits number of matching bits to consider
448 get_matching_bits_delay (uint32_t matching_bits)
450 /* Calculated as: S + f/2 - (f / pi) * (atan(x - p')) */
451 // S is next_timestamp (ignored in return value)
452 // f is frequency (gnunet_nse_interval)
453 // x is matching_bits
454 // p' is current_size_estimate
455 return ((double) gnunet_nse_interval.rel_value_us / (double) 2.0)
456 - ((gnunet_nse_interval.rel_value_us / M_PI)
457 * atan (matching_bits - current_size_estimate));
462 * What delay randomization should we apply for a given number of matching bits?
464 * @param matching_bits number of matching bits
465 * @return random delay to apply
467 static struct GNUNET_TIME_Relative
468 get_delay_randomization (uint32_t matching_bits)
470 #if USE_RANDOM_DELAYS
471 struct GNUNET_TIME_Relative ret;
475 d = get_matching_bits_delay (matching_bits);
476 i = (uint32_t) (d / (double) (hop_count_max + 1));
477 ret.rel_value_us = i;
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Randomizing flood using latencies up to %s\n",
480 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
482 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, i + 1);
485 return GNUNET_TIME_UNIT_ZERO;
491 * Get the number of matching bits that the given timestamp has to the given peer ID.
493 * @param timestamp time to generate key
494 * @param id peer identity to compare with
495 * @return number of matching bits
498 get_matching_bits (struct GNUNET_TIME_Absolute timestamp,
499 const struct GNUNET_PeerIdentity *id)
501 struct GNUNET_HashCode timestamp_hash;
502 struct GNUNET_HashCode pid_hash;
504 GNUNET_CRYPTO_hash (×tamp.abs_value_us,
505 sizeof(timestamp.abs_value_us),
507 GNUNET_CRYPTO_hash (id, sizeof(struct GNUNET_PeerIdentity), &pid_hash);
508 return GNUNET_CRYPTO_hash_matching_bits (×tamp_hash, &pid_hash);
513 * Get the transmission delay that should be applied for a
516 * @param round_offset -1 for the previous round (random delay between 0 and 50ms)
517 * 0 for the current round (based on our proximity to time key)
518 * @return delay that should be applied
520 static struct GNUNET_TIME_Relative
521 get_transmit_delay (int round_offset)
523 struct GNUNET_TIME_Relative ret;
524 struct GNUNET_TIME_Absolute tgt;
526 uint32_t matching_bits;
528 switch (round_offset)
531 /* previous round is randomized between 0 and 50 ms */
532 #if USE_RANDOM_DELAYS
534 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 50);
536 ret = GNUNET_TIME_UNIT_ZERO;
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Transmitting previous round behind schedule in %s\n",
540 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
544 /* current round is based on best-known matching_bits */
546 ntohl (size_estimate_messages[estimate_index].matching_bits);
547 dist_delay = get_matching_bits_delay (matching_bits);
548 dist_delay += get_delay_randomization (matching_bits).rel_value_us;
549 ret.rel_value_us = (uint64_t) dist_delay;
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
551 "For round %s, delay for %u matching bits is %s\n",
552 GNUNET_STRINGS_absolute_time_to_string (current_timestamp),
553 (unsigned int) matching_bits,
554 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
555 /* now consider round start time and add delay to it */
556 tgt = GNUNET_TIME_absolute_add (current_timestamp, ret);
557 return GNUNET_TIME_absolute_get_remaining (tgt);
560 return GNUNET_TIME_UNIT_FOREVER_REL;
565 * Task that triggers a NSE P2P transmission.
567 * @param cls the `struct NSEPeerEntry *`
570 transmit_task_cb (void *cls)
572 struct NSEPeerEntry *peer_entry = cls;
574 struct GNUNET_MQ_Envelope *env;
576 peer_entry->transmit_task = NULL;
577 idx = estimate_index;
578 if (GNUNET_NO == peer_entry->previous_round)
580 idx = (idx + HISTORY_SIZE - 1) % HISTORY_SIZE;
581 peer_entry->previous_round = GNUNET_YES;
582 peer_entry->transmit_task =
583 GNUNET_SCHEDULER_add_delayed (get_transmit_delay (0),
587 if ((0 == ntohl (size_estimate_messages[idx].hop_count)) &&
588 (NULL != proof_task))
590 GNUNET_STATISTICS_update (stats,
591 "# flood messages not generated (no proof yet)",
596 if (0 == ntohs (size_estimate_messages[idx].header.size))
598 GNUNET_STATISTICS_update (stats,
599 "# flood messages not generated (lack of history)",
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "In round %s, sending to `%s' estimate with %u bits\n",
606 GNUNET_STRINGS_absolute_time_to_string (
607 GNUNET_TIME_absolute_ntoh (
608 size_estimate_messages[idx].timestamp)),
609 GNUNET_i2s (peer_entry->id),
610 (unsigned int) ntohl (size_estimate_messages[idx].matching_bits));
611 if (0 == ntohl (size_estimate_messages[idx].hop_count))
612 GNUNET_STATISTICS_update (stats, "# flood messages started", 1, GNUNET_NO);
613 GNUNET_STATISTICS_update (stats,
614 "# flood messages transmitted",
617 #if ENABLE_NSE_HISTOGRAM
618 peer_entry->transmitted_messages++;
619 peer_entry->last_transmitted_size =
620 ntohl (size_estimate_messages[idx].matching_bits);
622 env = GNUNET_MQ_msg_copy (&size_estimate_messages[idx].header);
623 GNUNET_MQ_send (peer_entry->mq, env);
628 * We've sent on our flood message or one that we received which was
629 * validated and closer than ours. Update the global list of recent
630 * messages and the average. Also re-broadcast the message to any
634 update_network_size_estimate ()
636 struct GNUNET_NSE_ClientMessage em;
638 setup_estimate_message (&em);
639 GNUNET_notification_context_broadcast (nc, &em.header, GNUNET_YES);
644 * Setup a flood message in our history array at the given
645 * slot offset for the given timestamp.
647 * @param slot index to use
648 * @param ts timestamp to use
651 setup_flood_message (unsigned int slot, struct GNUNET_TIME_Absolute ts)
653 struct GNUNET_NSE_FloodMessage *fm;
654 uint32_t matching_bits;
656 matching_bits = get_matching_bits (ts, &my_identity);
657 fm = &size_estimate_messages[slot];
658 fm->header.size = htons (sizeof(struct GNUNET_NSE_FloodMessage));
659 fm->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD);
660 fm->hop_count = htonl (0);
661 fm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_NSE_SEND);
663 htonl (sizeof(struct GNUNET_NSE_FloodMessage)
664 - sizeof(struct GNUNET_MessageHeader) - sizeof(uint32_t)
665 - sizeof(struct GNUNET_CRYPTO_EddsaSignature));
666 fm->matching_bits = htonl (matching_bits);
667 fm->timestamp = GNUNET_TIME_absolute_hton (ts);
668 fm->origin = my_identity;
669 fm->proof_of_work = my_proof;
670 if (nse_work_required > 0)
671 GNUNET_assert (GNUNET_OK ==
672 GNUNET_CRYPTO_eddsa_sign_ (my_private_key,
676 memset (&fm->signature, 0, sizeof(fm->signature));
681 * Schedule transmission for the given peer for the current round based
682 * on what we know about the desired delay.
685 * @param key hash of peer identity
686 * @param value the `struct NSEPeerEntry`
687 * @return #GNUNET_OK (continue to iterate)
690 schedule_current_round (void *cls,
691 const struct GNUNET_PeerIdentity *key,
694 struct NSEPeerEntry *peer_entry = value;
695 struct GNUNET_TIME_Relative delay;
699 if (NULL != peer_entry->transmit_task)
701 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
702 peer_entry->previous_round = GNUNET_NO;
704 #if ENABLE_NSE_HISTOGRAM
705 if (peer_entry->received_messages > 1)
706 GNUNET_STATISTICS_update (stats,
708 peer_entry->received_messages - 1,
710 peer_entry->transmitted_messages = 0;
711 peer_entry->last_transmitted_size = 0;
712 peer_entry->received_messages = 0;
715 get_transmit_delay ((GNUNET_NO == peer_entry->previous_round) ? -1 : 0);
716 peer_entry->transmit_task =
717 GNUNET_SCHEDULER_add_delayed (delay, &transmit_task_cb, peer_entry);
723 * Update our flood message to be sent (and our timestamps).
728 update_flood_message (void *cls)
730 struct GNUNET_TIME_Relative offset;
734 offset = GNUNET_TIME_absolute_get_remaining (next_timestamp);
735 if (0 != offset.rel_value_us)
737 /* somehow run early, delay more */
739 GNUNET_SCHEDULER_add_delayed (offset, &update_flood_message, NULL);
742 estimate_index = (estimate_index + 1) % HISTORY_SIZE;
743 if (estimate_count < HISTORY_SIZE)
745 current_timestamp = next_timestamp;
747 GNUNET_TIME_absolute_add (current_timestamp, gnunet_nse_interval);
748 if ((current_timestamp.abs_value_us ==
749 GNUNET_TIME_absolute_ntoh (next_message.timestamp).abs_value_us) &&
750 (get_matching_bits (current_timestamp, &my_identity) <
751 ntohl (next_message.matching_bits)))
753 /* we received a message for this round way early, use it! */
754 size_estimate_messages[estimate_index] = next_message;
755 size_estimate_messages[estimate_index].hop_count =
756 htonl (1 + ntohl (next_message.hop_count));
759 setup_flood_message (estimate_index, current_timestamp);
760 next_message.matching_bits = htonl (0); /* reset for 'next' round */
762 for (unsigned int i = 0; i < HISTORY_SIZE; i++)
764 GNUNET_MAX (ntohl (size_estimate_messages[i].hop_count), hop_count_max);
765 GNUNET_CONTAINER_multipeermap_iterate (peers, &schedule_current_round, NULL);
767 GNUNET_SCHEDULER_add_at (next_timestamp, &update_flood_message, NULL);
772 * Count the leading zeroes in hash.
774 * @param hash to count leading zeros in
775 * @return the number of leading zero bits.
778 count_leading_zeroes (const struct GNUNET_HashCode *hash)
780 unsigned int hash_count;
783 while (0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count))
790 * Check whether the given public key and integer are a valid proof of
793 * @param pkey the public key
794 * @param val the integer
795 * @return #GNUNET_YES if valid, #GNUNET_NO if not
798 check_proof_of_work (const struct GNUNET_CRYPTO_EddsaPublicKey *pkey,
801 char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
802 + sizeof(val)] GNUNET_ALIGN;
803 struct GNUNET_HashCode result;
805 GNUNET_memcpy (buf, &val, sizeof(val));
806 GNUNET_memcpy (&buf[sizeof(val)],
808 sizeof(struct GNUNET_CRYPTO_EddsaPublicKey));
809 GNUNET_CRYPTO_pow_hash ("gnunet-nse-proof",
813 return (count_leading_zeroes (&result) >= nse_work_required) ? GNUNET_YES
819 * Write our current proof to disk.
827 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "PROOFFILE", &proof))
829 if (sizeof(my_proof) != GNUNET_DISK_fn_write (proof,
832 GNUNET_DISK_PERM_USER_READ
833 | GNUNET_DISK_PERM_USER_WRITE))
834 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", proof);
840 * Find our proof of work.
842 * @param cls closure (unused)
845 find_proof (void *cls)
847 #define ROUND_SIZE 10
849 char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
850 + sizeof(uint64_t)] GNUNET_ALIGN;
851 struct GNUNET_HashCode result;
856 GNUNET_memcpy (&buf[sizeof(uint64_t)],
858 sizeof(struct GNUNET_PeerIdentity));
861 while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
863 GNUNET_memcpy (buf, &counter, sizeof(uint64_t));
864 GNUNET_CRYPTO_pow_hash ("gnunet-nse-proof",
868 if (nse_work_required <= count_leading_zeroes (&result))
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "Proof of work found: %llu!\n",
873 (unsigned long long) GNUNET_ntohll (counter));
875 setup_flood_message (estimate_index, current_timestamp);
881 if (my_proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Testing proofs currently at %llu\n",
885 (unsigned long long) counter);
886 /* remember progress every 100 rounds */
895 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay,
896 GNUNET_SCHEDULER_PRIORITY_IDLE,
903 * An incoming flood message has been received which claims
904 * to have more bits matching than any we know in this time
905 * period. Verify the signature and/or proof of work.
907 * @param incoming_flood the message to verify
908 * @return #GNUNET_YES if the message is verified
909 * #GNUNET_NO if the key/signature don't verify
912 verify_message_crypto (const struct GNUNET_NSE_FloodMessage *incoming_flood)
914 if (GNUNET_YES != check_proof_of_work (&incoming_flood->origin.public_key,
915 incoming_flood->proof_of_work))
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "Proof of work invalid: %llu!\n",
919 (unsigned long long) GNUNET_ntohll (
920 incoming_flood->proof_of_work));
924 if ((nse_work_required > 0) &&
926 GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_NSE_SEND,
927 &incoming_flood->purpose,
928 &incoming_flood->signature,
929 &incoming_flood->origin.public_key)))
939 * Update transmissions for the given peer for the current round based
940 * on updated proximity information.
942 * @param cls peer entry to exclude from updates
943 * @param key hash of peer identity
944 * @param value the `struct NSEPeerEntry *` of a peer to transmit to
945 * @return #GNUNET_OK (continue to iterate)
948 update_flood_times (void *cls,
949 const struct GNUNET_PeerIdentity *key,
952 struct NSEPeerEntry *exclude = cls;
953 struct NSEPeerEntry *peer_entry = value;
954 struct GNUNET_TIME_Relative delay;
957 if (peer_entry == exclude)
958 return GNUNET_OK; /* trigger of the update */
959 if (GNUNET_NO == peer_entry->previous_round)
961 /* still stuck in previous round, no point to update, check that
962 * we are active here though... */
963 if (NULL == peer_entry->transmit_task)
969 if (NULL != peer_entry->transmit_task)
971 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
972 peer_entry->transmit_task = NULL;
974 delay = get_transmit_delay (0);
975 peer_entry->transmit_task =
976 GNUNET_SCHEDULER_add_delayed (delay, &transmit_task_cb, peer_entry);
982 * Core handler for size estimate flooding messages.
984 * @param cls peer this message is from
985 * @param incoming_flood received message
988 handle_p2p_estimate (void *cls,
989 const struct GNUNET_NSE_FloodMessage *incoming_flood)
991 struct NSEPeerEntry *peer_entry = cls;
992 struct GNUNET_TIME_Absolute ts;
993 uint32_t matching_bits;
996 #if ENABLE_NSE_HISTOGRAM
1000 t = GNUNET_TIME_absolute_get ().abs_value_us;
1002 GNUNET_TESTBED_LOGGER_write (lh, &t, sizeof(uint64_t));
1003 if (NULL != histogram)
1004 GNUNET_BIO_write_int64 (histogram, "histogram-time", t);
1007 GNUNET_STATISTICS_update (stats, "# flood messages received", 1, GNUNET_NO);
1008 matching_bits = ntohl (incoming_flood->matching_bits);
1013 struct GNUNET_PeerIdentity os;
1015 GNUNET_snprintf (origin,
1018 GNUNET_i2s (&incoming_flood->origin));
1019 GNUNET_snprintf (pred, sizeof(pred), "%s", GNUNET_i2s (peer_entry->id));
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 "Flood at %s from `%s' via `%s' at `%s' with bits %u\n",
1022 GNUNET_STRINGS_absolute_time_to_string (
1023 GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp)),
1026 GNUNET_i2s (&my_identity),
1027 (unsigned int) matching_bits);
1031 #if ENABLE_NSE_HISTOGRAM
1032 peer_entry->received_messages++;
1033 if ((peer_entry->transmitted_messages > 0) &&
1034 (peer_entry->last_transmitted_size >= matching_bits) )
1035 GNUNET_STATISTICS_update (stats, "# cross messages", 1, GNUNET_NO);
1038 ts = GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp);
1039 if (ts.abs_value_us == current_timestamp.abs_value_us)
1040 idx = estimate_index;
1041 else if (ts.abs_value_us ==
1042 current_timestamp.abs_value_us - gnunet_nse_interval.rel_value_us)
1043 idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE;
1044 else if (ts.abs_value_us == next_timestamp.abs_value_us)
1046 if (matching_bits <= ntohl (next_message.matching_bits))
1047 return; /* ignore, simply too early/late */
1048 if (GNUNET_YES != verify_message_crypto (incoming_flood))
1050 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1051 "Peer %s is likely ill-configured!\n",
1052 GNUNET_i2s (peer_entry->id));
1053 GNUNET_break_op (0);
1056 next_message = *incoming_flood;
1061 GNUNET_STATISTICS_update (stats,
1062 "# flood messages discarded (clock skew too large)",
1067 if (0 == (GNUNET_memcmp (peer_entry->id, &my_identity)))
1069 /* send to self, update our own estimate IF this also comes from us! */
1070 if (0 == GNUNET_memcmp (&incoming_flood->origin, &my_identity))
1071 update_network_size_estimate ();
1074 if (matching_bits == ntohl (size_estimate_messages[idx].matching_bits))
1076 /* Cancel transmission in the other direction, as this peer clearly has
1077 up-to-date information already. Even if we didn't talk to this peer in
1078 the previous round, we should no longer send it stale information as it
1079 told us about the current round! */
1080 peer_entry->previous_round = GNUNET_YES;
1081 if (idx != estimate_index)
1083 /* do not transmit information for the previous round to this peer
1084 anymore (but allow current round) */
1087 /* got up-to-date information for current round, cancel transmission to
1088 * this peer altogether */
1089 if (NULL != peer_entry->transmit_task)
1091 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1092 peer_entry->transmit_task = NULL;
1096 if (matching_bits < ntohl (size_estimate_messages[idx].matching_bits))
1098 if ((idx < estimate_index) && (peer_entry->previous_round == GNUNET_YES))
1100 peer_entry->previous_round = GNUNET_NO;
1102 /* push back our result now, that peer is spreading bad information... */
1103 if (NULL != peer_entry->transmit_task)
1104 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1105 peer_entry->transmit_task =
1106 GNUNET_SCHEDULER_add_now (&transmit_task_cb, peer_entry);
1107 /* Not closer than our most recent message, no need to do work here */
1108 GNUNET_STATISTICS_update (stats,
1109 "# flood messages ignored (had closer already)",
1114 if (GNUNET_YES != verify_message_crypto (incoming_flood))
1116 GNUNET_break_op (0);
1119 GNUNET_assert (matching_bits >
1120 ntohl (size_estimate_messages[idx].matching_bits));
1121 /* Cancel transmission in the other direction, as this peer clearly has
1122 * up-to-date information already.
1124 peer_entry->previous_round = GNUNET_YES;
1125 if (idx == estimate_index)
1127 /* cancel any activity for current round */
1128 if (NULL != peer_entry->transmit_task)
1130 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1131 peer_entry->transmit_task = NULL;
1134 size_estimate_messages[idx] = *incoming_flood;
1135 size_estimate_messages[idx].hop_count =
1136 htonl (ntohl (incoming_flood->hop_count) + 1);
1138 GNUNET_MAX (ntohl (incoming_flood->hop_count) + 1, hop_count_max);
1139 GNUNET_STATISTICS_set (stats,
1140 "# estimated network diameter",
1144 /* have a new, better size estimate, inform clients */
1145 update_network_size_estimate ();
1148 GNUNET_CONTAINER_multipeermap_iterate (peers,
1149 &update_flood_times,
1155 * Method called whenever a peer connects. Sets up the PeerEntry and
1156 * schedules the initial size info transmission to this peer.
1158 * @param cls closure
1159 * @param peer peer identity this notification is about
1162 handle_core_connect (void *cls,
1163 const struct GNUNET_PeerIdentity *peer,
1164 struct GNUNET_MQ_Handle *mq)
1166 struct NSEPeerEntry *peer_entry;
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Peer `%s' connected to us\n",
1172 /* set our default transmission options */
1173 GNUNET_MQ_set_options (mq, NSE_PRIORITY);
1174 /* create our peer entry for this peer */
1175 peer_entry = GNUNET_new (struct NSEPeerEntry);
1176 peer_entry->id = peer;
1177 peer_entry->mq = mq;
1178 GNUNET_assert (GNUNET_OK ==
1179 GNUNET_CONTAINER_multipeermap_put (
1183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1184 peer_entry->transmit_task =
1185 GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1),
1188 GNUNET_STATISTICS_update (stats, "# peers connected", 1, GNUNET_NO);
1194 * Method called whenever a peer disconnects. Deletes the PeerEntry and cancels
1195 * any pending transmission requests to that peer.
1197 * @param cls closure
1198 * @param peer peer identity this notification is about
1199 * @parma internal_cls the `struct NSEPeerEntry` for the @a peer
1202 handle_core_disconnect (void *cls,
1203 const struct GNUNET_PeerIdentity *peer,
1206 struct NSEPeerEntry *pos = internal_cls;
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210 "Peer `%s' disconnected from us\n",
1212 GNUNET_assert (GNUNET_YES ==
1213 GNUNET_CONTAINER_multipeermap_remove (peers, peer, pos));
1214 if (NULL != pos->transmit_task)
1216 GNUNET_SCHEDULER_cancel (pos->transmit_task);
1217 pos->transmit_task = NULL;
1220 GNUNET_STATISTICS_update (stats, "# peers connected", -1, GNUNET_NO);
1224 #if ENABLE_NSE_HISTOGRAM
1226 * Functions of this type are called to notify a successful transmission of the
1227 * message to the logger service
1230 * @param size the amount of data sent (ignored)
1233 flush_comp_cb (void *cls, size_t size)
1237 GNUNET_TESTBED_LOGGER_disconnect (lh);
1246 * Task run during shutdown.
1251 shutdown_task (void *cls)
1254 if (NULL != flood_task)
1256 GNUNET_SCHEDULER_cancel (flood_task);
1259 if (NULL != proof_task)
1261 GNUNET_SCHEDULER_cancel (proof_task);
1263 write_proof (); /* remember progress */
1267 GNUNET_notification_context_destroy (nc);
1270 if (NULL != core_api)
1272 GNUNET_CORE_disconnect (core_api);
1277 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1282 GNUNET_CONTAINER_multipeermap_destroy (peers);
1285 if (NULL != my_private_key)
1287 GNUNET_free (my_private_key);
1288 my_private_key = NULL;
1290 #if ENABLE_NSE_HISTOGRAM
1291 if (NULL != logger_test)
1293 GNUNET_CLIENT_service_test_cancel (logger_test);
1298 GNUNET_TESTBED_LOGGER_flush (lh, &flush_comp_cb, NULL);
1300 if (NULL != histogram)
1302 GNUNET_BIO_write_close (histogram, NULL);
1310 * Called on core init/fail.
1312 * @param cls service closure
1313 * @param identity the public identity of this peer
1316 core_init (void *cls, const struct GNUNET_PeerIdentity *identity)
1318 struct GNUNET_TIME_Absolute now;
1319 struct GNUNET_TIME_Absolute prev_time;
1322 if (NULL == identity)
1324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connection to core FAILED!\n");
1325 GNUNET_SCHEDULER_shutdown ();
1328 GNUNET_assert (0 == GNUNET_memcmp (&my_identity, identity));
1329 now = GNUNET_TIME_absolute_get ();
1330 current_timestamp.abs_value_us =
1331 (now.abs_value_us / gnunet_nse_interval.rel_value_us)
1332 * gnunet_nse_interval.rel_value_us;
1334 GNUNET_TIME_absolute_add (current_timestamp, gnunet_nse_interval);
1335 estimate_index = HISTORY_SIZE - 1;
1337 if (GNUNET_YES == check_proof_of_work (&my_identity.public_key, my_proof))
1339 int idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE;
1340 prev_time.abs_value_us =
1341 current_timestamp.abs_value_us - gnunet_nse_interval.rel_value_us;
1342 setup_flood_message (idx, prev_time);
1343 setup_flood_message (estimate_index, current_timestamp);
1347 GNUNET_SCHEDULER_add_at (next_timestamp, &update_flood_message, NULL);
1351 #if ENABLE_NSE_HISTOGRAM
1353 * Function called with the status of the testbed logger service
1356 * @param status #GNUNET_YES if the service is running,
1357 * #GNUNET_NO if the service is not running
1358 * #GNUNET_SYSERR if the configuration is invalid
1361 status_cb (void *cls, int status)
1365 if (GNUNET_YES != status)
1367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Testbed logger not running\n");
1370 if (NULL == (lh = GNUNET_TESTBED_LOGGER_connect (cfg)))
1372 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1373 "Cannot connect to the testbed logger. Exiting.\n");
1374 GNUNET_SCHEDULER_shutdown ();
1383 * Handle network size estimate clients.
1385 * @param cls closure
1386 * @param c configuration to use
1387 * @param service the initialized service
1391 const struct GNUNET_CONFIGURATION_Handle *c,
1392 struct GNUNET_SERVICE_Handle *service)
1394 struct GNUNET_MQ_MessageHandler core_handlers[] =
1395 { GNUNET_MQ_hd_fixed_size (p2p_estimate,
1396 GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD,
1397 struct GNUNET_NSE_FloodMessage,
1399 GNUNET_MQ_handler_end () };
1401 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
1406 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
1409 &gnunet_nse_interval))
1411 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "INTERVAL");
1412 GNUNET_SCHEDULER_shutdown ();
1415 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg,
1420 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKDELAY");
1421 GNUNET_SCHEDULER_shutdown ();
1424 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
1427 &nse_work_required))
1429 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS");
1430 GNUNET_SCHEDULER_shutdown ();
1433 if (nse_work_required >= sizeof(struct GNUNET_HashCode) * 8)
1435 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1438 _ ("Value is too large.\n"));
1439 GNUNET_SCHEDULER_shutdown ();
1443 #if ENABLE_NSE_HISTOGRAM
1445 char *histogram_dir;
1448 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg,
1454 0 < GNUNET_asprintf (&histogram_fn, "%s/timestamps", histogram_dir));
1455 GNUNET_free (histogram_dir);
1456 histogram = GNUNET_BIO_write_open_file (histogram_fn);
1457 if (NULL == histogram)
1458 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1459 "Unable to open histogram file `%s'\n",
1461 GNUNET_free (histogram_fn);
1463 logger_test = GNUNET_CLIENT_service_test ("testbed-logger",
1465 GNUNET_TIME_UNIT_SECONDS,
1471 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
1472 pk = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
1473 GNUNET_assert (NULL != pk);
1474 my_private_key = pk;
1475 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
1477 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "PROOFFILE", &proof))
1479 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "PROOFFILE");
1480 GNUNET_free (my_private_key);
1481 my_private_key = NULL;
1482 GNUNET_SCHEDULER_shutdown ();
1485 if ((GNUNET_YES != GNUNET_DISK_file_test (proof)) ||
1486 (sizeof(my_proof) !=
1487 GNUNET_DISK_fn_read (proof, &my_proof, sizeof(my_proof))))
1489 GNUNET_free (proof);
1491 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1495 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1496 nc = GNUNET_notification_context_create (1);
1497 /* Connect to core service and register core handlers */
1499 GNUNET_CORE_connect (cfg, /* Main configuration */
1500 NULL, /* Closure passed to functions */
1501 &core_init, /* Call core_init once connected */
1502 &handle_core_connect, /* Handle connects */
1503 &handle_core_disconnect, /* Handle disconnects */
1504 core_handlers); /* Register these handlers */
1505 if (NULL == core_api)
1507 GNUNET_SCHEDULER_shutdown ();
1510 stats = GNUNET_STATISTICS_create ("nse", cfg);
1515 * Callback called when a client connects to the service.
1517 * @param cls closure for the service
1518 * @param c the new client that connected to the service
1519 * @param mq the message queue used to send messages to the client
1523 client_connect_cb (void *cls,
1524 struct GNUNET_SERVICE_Client *c,
1525 struct GNUNET_MQ_Handle *mq)
1534 * Callback called when a client disconnected from the service
1536 * @param cls closure for the service
1537 * @param c the client that disconnected
1538 * @param internal_cls should be equal to @a c
1541 client_disconnect_cb (void *cls,
1542 struct GNUNET_SERVICE_Client *c,
1546 GNUNET_assert (c == internal_cls);
1551 * Define "main" method using service macro.
1553 GNUNET_SERVICE_MAIN ("nse",
1554 GNUNET_SERVICE_OPTION_NONE,
1557 &client_disconnect_cb,
1559 GNUNET_MQ_hd_fixed_size (start,
1560 GNUNET_MESSAGE_TYPE_NSE_START,
1561 struct GNUNET_MessageHeader,
1563 GNUNET_MQ_handler_end ());
1566 #if defined(__linux__) && defined(__GLIBC__)
1570 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1572 void __attribute__ ((constructor))
1573 GNUNET_ARM_memory_init ()
1575 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1576 mallopt (M_TOP_PAD, 1 * 1024);
1584 /* end of gnunet-service-nse.c */