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"
56 * Should messages be delayed randomly? This option should be set to
57 * #GNUNET_NO only for experiments, not in production.
59 #define USE_RANDOM_DELAYS GNUNET_YES
62 * Generate extensive debug-level log messages?
64 #define DEBUG_NSE GNUNET_NO
67 * Over how many values do we calculate the weighted average?
69 #define HISTORY_SIZE 64
72 * Message priority to use.
74 #define NSE_PRIORITY GNUNET_CORE_PRIO_CRITICAL_CONTROL
77 #define log2(a) (log(a)/log(2))
81 * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
83 static unsigned long long nse_work_required;
86 * Interval for sending network size estimation flood requests.
88 static struct GNUNET_TIME_Relative gnunet_nse_interval;
91 * Interval between proof find runs.
93 static struct GNUNET_TIME_Relative proof_find_delay;
95 #if ENABLE_NSE_HISTOGRAM
98 * Handle to test if testbed logger service is running or not
100 struct GNUNET_CLIENT_TestHandle *logger_test;
103 * Handle for writing when we received messages to disk.
105 static struct GNUNET_TESTBED_LOGGER_Handle *lh;
108 * Handle for writing message received timestamp information to disk.
110 static struct GNUNET_BIO_WriteHandle *histogram;
116 * Per-peer information.
122 * Core handle for sending messages to this peer.
124 struct GNUNET_MQ_Handle *mq;
127 * What is the identity of the peer?
129 const struct GNUNET_PeerIdentity *id;
132 * Task scheduled to send message to this peer.
134 struct GNUNET_SCHEDULER_Task *transmit_task;
137 * Did we receive or send a message about the previous round
138 * to this peer yet? #GNUNET_YES if the previous round has
139 * been taken care of.
143 #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;
165 GNUNET_NETWORK_STRUCT_BEGIN
168 * Network size estimate reply; sent when "this"
169 * peer's timer has run out before receiving a
170 * valid reply from another peer.
172 struct GNUNET_NSE_FloodMessage
175 * Type: #GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD
177 struct GNUNET_MessageHeader header;
180 * Number of hops this message has taken so far.
182 uint32_t hop_count GNUNET_PACKED;
187 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
190 * The current timestamp value (which all
191 * peers should agree on).
193 struct GNUNET_TIME_AbsoluteNBO timestamp;
196 * Number of matching bits between the hash
197 * of timestamp and the initiator's public
200 uint32_t matching_bits GNUNET_PACKED;
203 * Public key of the originator.
205 struct GNUNET_PeerIdentity origin;
208 * Proof of work, causing leading zeros when hashed with pkey.
210 uint64_t proof_of_work GNUNET_PACKED;
213 * Signature (over range specified in purpose).
215 struct GNUNET_CRYPTO_EddsaSignature signature;
217 GNUNET_NETWORK_STRUCT_END
220 * Handle to our current configuration.
222 static const struct GNUNET_CONFIGURATION_Handle *cfg;
225 * Handle to the statistics service.
227 static struct GNUNET_STATISTICS_Handle *stats;
230 * Handle to the core service.
232 static struct GNUNET_CORE_Handle *core_api;
235 * Map of all connected peers.
237 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
240 * The current network size estimate. Number of bits matching on
243 static double current_size_estimate;
246 * The standard deviation of the last #HISTORY_SIZE network
249 static double current_std_dev = NAN;
252 * Current hop counter estimate (estimate for network diameter).
254 static uint32_t hop_count_max;
257 * Message for the next round, if we got any.
259 static struct GNUNET_NSE_FloodMessage next_message;
262 * Array of recent size estimate messages.
264 static struct GNUNET_NSE_FloodMessage size_estimate_messages[HISTORY_SIZE];
267 * Index of most recent estimate.
269 static unsigned int estimate_index;
272 * Number of valid entries in the history.
274 static unsigned int estimate_count;
277 * Task scheduled to update our flood message for the next round.
279 static struct GNUNET_SCHEDULER_Task *flood_task;
282 * Task scheduled to compute our proof.
284 static struct GNUNET_SCHEDULER_Task *proof_task;
287 * Notification context, simplifies client broadcasts.
289 static struct GNUNET_NotificationContext *nc;
292 * The next major time.
294 static struct GNUNET_TIME_Absolute next_timestamp;
297 * The current major time.
299 static struct GNUNET_TIME_Absolute current_timestamp;
302 * The private key of this peer.
304 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
307 * The peer identity of this peer.
309 static struct GNUNET_PeerIdentity my_identity;
312 * Proof of work for this peer.
314 static uint64_t my_proof;
318 * Initialize a message to clients with the current network
321 * @param em message to fill in
324 setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
334 /* Weighted incremental algorithm for stddev according to West (1979) */
346 for (unsigned int i = 0; i < estimate_count; i++)
348 unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
350 val = htonl (size_estimate_messages[j].matching_bits);
351 weight = estimate_count + 1 - i;
353 temp = weight + sumweight;
355 r = q * weight / temp;
357 sum += sumweight * q * r;
360 if (estimate_count > 0)
361 variance = (sum / sumweight) * estimate_count / (estimate_count - 1.0);
363 /* trivial version for debugging */
366 /* non-weighted trivial version */
372 for (unsigned int i = 0; i < estimate_count; i++)
374 unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
376 val = htonl (size_estimate_messages[j].matching_bits);
380 if (0 != estimate_count)
382 mean = sum / estimate_count;
383 variance = (vsq - mean * sum) / (estimate_count - 1.0); // terrible for numerical stability...
387 std_dev = sqrt (variance);
389 std_dev = variance; /* must be infinity due to estimate_count == 0 */
390 current_std_dev = std_dev;
391 current_size_estimate = mean;
393 em->header.size = htons (sizeof (struct GNUNET_NSE_ClientMessage));
394 em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
395 em->reserved = htonl (0);
396 em->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
398 double se = mean - 0.332747;
399 unsigned int j = GNUNET_CONTAINER_multipeermap_size (peers);
401 j = 1; /* Avoid log2(0); can only happen if CORE didn't report
402 connection to self yet */
404 em->size_estimate = GNUNET_hton_double (GNUNET_MAX (se,
406 em->std_deviation = GNUNET_hton_double (std_dev);
407 GNUNET_STATISTICS_set (stats,
408 "# nodes in the network (estimate)",
409 (uint64_t) pow (2, GNUNET_MAX (se,
417 * Handler for START message from client, triggers an
418 * immediate current network estimate notification.
419 * Also, we remember the client for updates upon future
420 * estimate measurements.
422 * @param cls client who sent the message
423 * @param message the message received
426 handle_start (void *cls,
427 const struct GNUNET_MessageHeader *message)
429 struct GNUNET_SERVICE_Client *client = cls;
430 struct GNUNET_MQ_Handle *mq;
431 struct GNUNET_NSE_ClientMessage em;
432 struct GNUNET_MQ_Envelope *env;
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Received START message from client\n");
436 mq = GNUNET_SERVICE_client_get_mq (client);
437 GNUNET_notification_context_add (nc,
439 setup_estimate_message (&em);
440 env = GNUNET_MQ_msg_copy (&em.header);
443 GNUNET_SERVICE_client_continue (client);
448 * How long should we delay a message to go the given number of
451 * @param matching_bits number of matching bits to consider
454 get_matching_bits_delay (uint32_t matching_bits)
456 /* Calculated as: S + f/2 - (f / pi) * (atan(x - p')) */
457 // S is next_timestamp (ignored in return value)
458 // f is frequency (gnunet_nse_interval)
459 // x is matching_bits
460 // p' is current_size_estimate
461 return ((double) gnunet_nse_interval.rel_value_us / (double) 2.0) -
462 ((gnunet_nse_interval.rel_value_us / M_PI) *
463 atan (matching_bits - current_size_estimate));
468 * What delay randomization should we apply for a given number of matching bits?
470 * @param matching_bits number of matching bits
471 * @return random delay to apply
473 static struct GNUNET_TIME_Relative
474 get_delay_randomization (uint32_t matching_bits)
476 #if USE_RANDOM_DELAYS
477 struct GNUNET_TIME_Relative ret;
481 d = get_matching_bits_delay (matching_bits);
482 i = (uint32_t) (d / (double) (hop_count_max + 1));
483 ret.rel_value_us = i;
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Randomizing flood using latencies up to %s\n",
486 GNUNET_STRINGS_relative_time_to_string (ret,
488 ret.rel_value_us = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, i + 1);
491 return GNUNET_TIME_UNIT_ZERO;
497 * Calculate the 'proof-of-work' hash (an expensive hash).
499 * @param buf data to hash
500 * @param buf_len number of bytes in @a buf
501 * @param result where to write the resulting hash
504 pow_hash (const void *buf,
506 struct GNUNET_HashCode *result)
509 gcry_kdf_derive (buf, buf_len,
512 "gnunet-proof-of-work",
513 strlen ("gnunet-proof-of-work"),
514 2 /* iterations; keep cost of individual op small */,
515 sizeof (struct GNUNET_HashCode),
521 * Get the number of matching bits that the given timestamp has to the given peer ID.
523 * @param timestamp time to generate key
524 * @param id peer identity to compare with
525 * @return number of matching bits
528 get_matching_bits (struct GNUNET_TIME_Absolute timestamp,
529 const struct GNUNET_PeerIdentity *id)
531 struct GNUNET_HashCode timestamp_hash;
532 struct GNUNET_HashCode pid_hash;
534 GNUNET_CRYPTO_hash (×tamp.abs_value_us,
535 sizeof (timestamp.abs_value_us),
537 GNUNET_CRYPTO_hash (id,
538 sizeof (struct GNUNET_PeerIdentity),
540 return GNUNET_CRYPTO_hash_matching_bits (×tamp_hash,
546 * Get the transmission delay that should be applied for a
549 * @param round_offset -1 for the previous round (random delay between 0 and 50ms)
550 * 0 for the current round (based on our proximity to time key)
551 * @return delay that should be applied
553 static struct GNUNET_TIME_Relative
554 get_transmit_delay (int round_offset)
556 struct GNUNET_TIME_Relative ret;
557 struct GNUNET_TIME_Absolute tgt;
559 uint32_t matching_bits;
561 switch (round_offset)
564 /* previous round is randomized between 0 and 50 ms */
565 #if USE_RANDOM_DELAYS
566 ret.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
569 ret = GNUNET_TIME_UNIT_ZERO;
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572 "Transmitting previous round behind schedule in %s\n",
573 GNUNET_STRINGS_relative_time_to_string (ret,
577 /* current round is based on best-known matching_bits */
579 ntohl (size_estimate_messages[estimate_index].matching_bits);
580 dist_delay = get_matching_bits_delay (matching_bits);
581 dist_delay += get_delay_randomization (matching_bits).rel_value_us;
582 ret.rel_value_us = (uint64_t) dist_delay;
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "For round %s, delay for %u matching bits is %s\n",
585 GNUNET_STRINGS_absolute_time_to_string (current_timestamp),
586 (unsigned int) matching_bits,
587 GNUNET_STRINGS_relative_time_to_string (ret,
589 /* now consider round start time and add delay to it */
590 tgt = GNUNET_TIME_absolute_add (current_timestamp,
592 return GNUNET_TIME_absolute_get_remaining (tgt);
595 return GNUNET_TIME_UNIT_FOREVER_REL;
600 * Task that triggers a NSE P2P transmission.
602 * @param cls the `struct NSEPeerEntry *`
605 transmit_task_cb (void *cls)
607 struct NSEPeerEntry *peer_entry = cls;
609 struct GNUNET_MQ_Envelope *env;
611 peer_entry->transmit_task = NULL;
612 idx = estimate_index;
613 if (GNUNET_NO == peer_entry->previous_round)
615 idx = (idx + HISTORY_SIZE - 1) % HISTORY_SIZE;
616 peer_entry->previous_round = GNUNET_YES;
617 peer_entry->transmit_task
618 = GNUNET_SCHEDULER_add_delayed (get_transmit_delay (0),
622 if ((0 == ntohl (size_estimate_messages[idx].hop_count)) &&
623 (NULL != proof_task))
625 GNUNET_STATISTICS_update (stats,
626 "# flood messages not generated (no proof yet)",
631 if (0 == ntohs (size_estimate_messages[idx].header.size))
633 GNUNET_STATISTICS_update (stats,
634 "# flood messages not generated (lack of history)",
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "In round %s, sending to `%s' estimate with %u bits\n",
641 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (size_estimate_messages[idx].timestamp)),
642 GNUNET_i2s (peer_entry->id),
643 (unsigned int) ntohl (size_estimate_messages[idx].matching_bits));
644 if (0 == ntohl (size_estimate_messages[idx].hop_count))
645 GNUNET_STATISTICS_update (stats,
646 "# flood messages started",
649 GNUNET_STATISTICS_update (stats,
650 "# flood messages transmitted",
653 #if ENABLE_NSE_HISTOGRAM
654 peer_entry->transmitted_messages++;
655 peer_entry->last_transmitted_size
656 = ntohl(size_estimate_messages[idx].matching_bits);
658 env = GNUNET_MQ_msg_copy (&size_estimate_messages[idx].header);
659 GNUNET_MQ_send (peer_entry->mq,
665 * We've sent on our flood message or one that we received which was
666 * validated and closer than ours. Update the global list of recent
667 * messages and the average. Also re-broadcast the message to any
671 update_network_size_estimate ()
673 struct GNUNET_NSE_ClientMessage em;
675 setup_estimate_message (&em);
676 GNUNET_notification_context_broadcast (nc,
683 * Setup a flood message in our history array at the given
684 * slot offset for the given timestamp.
686 * @param slot index to use
687 * @param ts timestamp to use
690 setup_flood_message (unsigned int slot,
691 struct GNUNET_TIME_Absolute ts)
693 struct GNUNET_NSE_FloodMessage *fm;
694 uint32_t matching_bits;
696 matching_bits = get_matching_bits (ts,
698 fm = &size_estimate_messages[slot];
699 fm->header.size = htons (sizeof (struct GNUNET_NSE_FloodMessage));
700 fm->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD);
701 fm->hop_count = htonl (0);
702 fm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_NSE_SEND);
704 htonl (sizeof (struct GNUNET_NSE_FloodMessage) -
705 sizeof (struct GNUNET_MessageHeader) - sizeof (uint32_t) -
706 sizeof (struct GNUNET_CRYPTO_EddsaSignature));
707 fm->matching_bits = htonl (matching_bits);
708 fm->timestamp = GNUNET_TIME_absolute_hton (ts);
709 fm->origin = my_identity;
710 fm->proof_of_work = my_proof;
711 if (nse_work_required > 0)
712 GNUNET_assert (GNUNET_OK ==
713 GNUNET_CRYPTO_eddsa_sign (my_private_key,
717 memset (&fm->signature,
719 sizeof (fm->signature));
724 * Schedule transmission for the given peer for the current round based
725 * on what we know about the desired delay.
728 * @param key hash of peer identity
729 * @param value the `struct NSEPeerEntry`
730 * @return #GNUNET_OK (continue to iterate)
733 schedule_current_round (void *cls,
734 const struct GNUNET_PeerIdentity * key,
737 struct NSEPeerEntry *peer_entry = value;
738 struct GNUNET_TIME_Relative delay;
740 if (NULL != peer_entry->transmit_task)
742 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
743 peer_entry->previous_round = GNUNET_NO;
745 #if ENABLE_NSE_HISTOGRAM
746 if (peer_entry->received_messages > 1)
747 GNUNET_STATISTICS_update(stats,
749 peer_entry->received_messages - 1,
751 peer_entry->transmitted_messages = 0;
752 peer_entry->last_transmitted_size = 0;
753 peer_entry->received_messages = 0;
756 get_transmit_delay ((GNUNET_NO == peer_entry->previous_round) ? -1 : 0);
757 peer_entry->transmit_task =
758 GNUNET_SCHEDULER_add_delayed (delay,
766 * Update our flood message to be sent (and our timestamps).
771 update_flood_message (void *cls)
773 struct GNUNET_TIME_Relative offset;
777 offset = GNUNET_TIME_absolute_get_remaining (next_timestamp);
778 if (0 != offset.rel_value_us)
780 /* somehow run early, delay more */
782 GNUNET_SCHEDULER_add_delayed (offset,
783 &update_flood_message,
787 estimate_index = (estimate_index + 1) % HISTORY_SIZE;
788 if (estimate_count < HISTORY_SIZE)
790 current_timestamp = next_timestamp;
792 GNUNET_TIME_absolute_add (current_timestamp, gnunet_nse_interval);
793 if ( (current_timestamp.abs_value_us ==
794 GNUNET_TIME_absolute_ntoh (next_message.timestamp).abs_value_us) &&
795 (get_matching_bits (current_timestamp, &my_identity) <
796 ntohl(next_message.matching_bits)) )
798 /* we received a message for this round way early, use it! */
799 size_estimate_messages[estimate_index] = next_message;
800 size_estimate_messages[estimate_index].hop_count =
801 htonl (1 + ntohl (next_message.hop_count));
804 setup_flood_message (estimate_index,
806 next_message.matching_bits = htonl (0); /* reset for 'next' round */
808 for (i = 0; i < HISTORY_SIZE; i++)
809 hop_count_max = GNUNET_MAX (ntohl (size_estimate_messages[i].hop_count),
811 GNUNET_CONTAINER_multipeermap_iterate (peers,
812 &schedule_current_round,
815 = GNUNET_SCHEDULER_add_at (next_timestamp,
816 &update_flood_message,
822 * Count the leading zeroes in hash.
824 * @param hash to count leading zeros in
825 * @return the number of leading zero bits.
828 count_leading_zeroes (const struct GNUNET_HashCode *hash)
830 unsigned int hash_count;
833 while (0 == GNUNET_CRYPTO_hash_get_bit (hash,
841 * Check whether the given public key and integer are a valid proof of
844 * @param pkey the public key
845 * @param val the integer
846 * @return #GNUNET_YES if valid, #GNUNET_NO if not
849 check_proof_of_work (const struct GNUNET_CRYPTO_EddsaPublicKey *pkey,
852 char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
853 sizeof (val)] GNUNET_ALIGN;
854 struct GNUNET_HashCode result;
859 GNUNET_memcpy (&buf[sizeof (val)],
861 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
865 return (count_leading_zeroes (&result) >=
866 nse_work_required) ? GNUNET_YES : GNUNET_NO;
871 * Write our current proof to disk.
879 GNUNET_CONFIGURATION_get_value_filename (cfg,
884 if (sizeof (my_proof) !=
885 GNUNET_DISK_fn_write (proof,
888 GNUNET_DISK_PERM_USER_READ |
889 GNUNET_DISK_PERM_USER_WRITE))
890 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
898 * Find our proof of work.
900 * @param cls closure (unused)
903 find_proof (void *cls)
905 #define ROUND_SIZE 10
907 char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
908 sizeof (uint64_t)] GNUNET_ALIGN;
909 struct GNUNET_HashCode result;
913 GNUNET_memcpy (&buf[sizeof (uint64_t)], &my_identity,
914 sizeof (struct GNUNET_PeerIdentity));
917 while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
919 GNUNET_memcpy (buf, &counter, sizeof (uint64_t));
920 pow_hash (buf, sizeof (buf), &result);
921 if (nse_work_required <= count_leading_zeroes (&result))
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Proof of work found: %llu!\n",
925 (unsigned long long) GNUNET_ntohll (counter));
927 setup_flood_message (estimate_index, current_timestamp);
933 if (my_proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
936 "Testing proofs currently at %llu\n",
937 (unsigned long long) counter);
938 /* remember progress every 100 rounds */
947 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay,
948 GNUNET_SCHEDULER_PRIORITY_IDLE,
954 * An incoming flood message has been received which claims
955 * to have more bits matching than any we know in this time
956 * period. Verify the signature and/or proof of work.
958 * @param incoming_flood the message to verify
959 * @return #GNUNET_YES if the message is verified
960 * #GNUNET_NO if the key/signature don't verify
963 verify_message_crypto (const struct GNUNET_NSE_FloodMessage *incoming_flood)
966 check_proof_of_work (&incoming_flood->origin.public_key,
967 incoming_flood->proof_of_work))
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
970 "Proof of work invalid: %llu!\n",
972 GNUNET_ntohll (incoming_flood->proof_of_work));
976 if ((nse_work_required > 0) &&
978 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_NSE_SEND,
979 &incoming_flood->purpose,
980 &incoming_flood->signature,
981 &incoming_flood->origin.public_key)))
991 * Update transmissions for the given peer for the current round based
992 * on updated proximity information.
994 * @param cls peer entry to exclude from updates
995 * @param key hash of peer identity
996 * @param value the `struct NSEPeerEntry *` of a peer to transmit to
997 * @return #GNUNET_OK (continue to iterate)
1000 update_flood_times (void *cls,
1001 const struct GNUNET_PeerIdentity *key,
1004 struct NSEPeerEntry *exclude = cls;
1005 struct NSEPeerEntry *peer_entry = value;
1006 struct GNUNET_TIME_Relative delay;
1008 if (peer_entry == exclude)
1009 return GNUNET_OK; /* trigger of the update */
1010 if (GNUNET_NO == peer_entry->previous_round)
1012 /* still stuck in previous round, no point to update, check that
1013 * we are active here though... */
1014 if (NULL == peer_entry->transmit_task)
1020 if (NULL != peer_entry->transmit_task)
1022 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1023 peer_entry->transmit_task = NULL;
1025 delay = get_transmit_delay (0);
1026 peer_entry->transmit_task =
1027 GNUNET_SCHEDULER_add_delayed (delay,
1028 &transmit_task_cb, peer_entry);
1034 * Core handler for size estimate flooding messages.
1036 * @param cls peer this message is from
1037 * @param incoming_flood received message
1040 handle_p2p_estimate (void *cls,
1041 const struct GNUNET_NSE_FloodMessage *incoming_flood)
1043 struct NSEPeerEntry *peer_entry = cls;
1044 struct GNUNET_TIME_Absolute ts;
1045 uint32_t matching_bits;
1048 #if ENABLE_NSE_HISTOGRAM
1052 t = GNUNET_TIME_absolute_get().abs_value_us;
1054 GNUNET_TESTBED_LOGGER_write (lh, &t, sizeof (uint64_t));
1055 if (NULL != histogram)
1056 GNUNET_BIO_write_int64 (histogram, t);
1059 GNUNET_STATISTICS_update (stats,
1060 "# flood messages received",
1063 matching_bits = ntohl (incoming_flood->matching_bits);
1068 struct GNUNET_PeerIdentity os;
1070 GNUNET_snprintf (origin,
1073 GNUNET_i2s (&incoming_flood->origin));
1074 GNUNET_snprintf (pred,
1077 GNUNET_i2s (peer_entry->id));
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Flood at %s from `%s' via `%s' at `%s' with bits %u\n",
1080 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp)),
1083 GNUNET_i2s (&my_identity),
1084 (unsigned int) matching_bits);
1088 #if ENABLE_NSE_HISTOGRAM
1089 peer_entry->received_messages++;
1090 if (peer_entry->transmitted_messages > 0 &&
1091 peer_entry->last_transmitted_size >= matching_bits)
1092 GNUNET_STATISTICS_update(stats,
1098 ts = GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp);
1099 if (ts.abs_value_us == current_timestamp.abs_value_us)
1100 idx = estimate_index;
1101 else if (ts.abs_value_us ==
1102 current_timestamp.abs_value_us - gnunet_nse_interval.rel_value_us)
1103 idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE;
1104 else if (ts.abs_value_us == next_timestamp.abs_value_us)
1106 if (matching_bits <= ntohl (next_message.matching_bits))
1107 return; /* ignore, simply too early/late */
1109 verify_message_crypto (incoming_flood))
1111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1112 "Peer %s is likely ill-configured!\n",
1113 GNUNET_i2s (peer_entry->id));
1114 GNUNET_break_op (0);
1117 next_message = *incoming_flood;
1122 GNUNET_STATISTICS_update (stats,
1123 "# flood messages discarded (clock skew too large)",
1127 if (0 == (GNUNET_memcmp (peer_entry->id,
1130 /* send to self, update our own estimate IF this also comes from us! */
1132 GNUNET_memcmp (&incoming_flood->origin,
1134 update_network_size_estimate ();
1137 if (matching_bits ==
1138 ntohl (size_estimate_messages[idx].matching_bits))
1140 /* Cancel transmission in the other direction, as this peer clearly has
1141 up-to-date information already. Even if we didn't talk to this peer in
1142 the previous round, we should no longer send it stale information as it
1143 told us about the current round! */
1144 peer_entry->previous_round = GNUNET_YES;
1145 if (idx != estimate_index)
1147 /* do not transmit information for the previous round to this peer
1148 anymore (but allow current round) */
1151 /* got up-to-date information for current round, cancel transmission to
1152 * this peer altogether */
1153 if (NULL != peer_entry->transmit_task)
1155 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1156 peer_entry->transmit_task = NULL;
1160 if (matching_bits < ntohl (size_estimate_messages[idx].matching_bits))
1162 if ( (idx < estimate_index) &&
1163 (peer_entry->previous_round == GNUNET_YES))
1165 peer_entry->previous_round = GNUNET_NO;
1167 /* push back our result now, that peer is spreading bad information... */
1168 if (NULL != peer_entry->transmit_task)
1169 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1170 peer_entry->transmit_task
1171 = GNUNET_SCHEDULER_add_now (&transmit_task_cb,
1173 /* Not closer than our most recent message, no need to do work here */
1174 GNUNET_STATISTICS_update (stats,
1175 "# flood messages ignored (had closer already)",
1181 verify_message_crypto (incoming_flood))
1183 GNUNET_break_op (0);
1186 GNUNET_assert (matching_bits >
1187 ntohl (size_estimate_messages[idx].matching_bits));
1188 /* Cancel transmission in the other direction, as this peer clearly has
1189 * up-to-date information already.
1191 peer_entry->previous_round = GNUNET_YES;
1192 if (idx == estimate_index)
1194 /* cancel any activity for current round */
1195 if (NULL != peer_entry->transmit_task)
1197 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1198 peer_entry->transmit_task = NULL;
1201 size_estimate_messages[idx] = *incoming_flood;
1202 size_estimate_messages[idx].hop_count =
1203 htonl (ntohl (incoming_flood->hop_count) + 1);
1205 GNUNET_MAX (ntohl (incoming_flood->hop_count) + 1,
1207 GNUNET_STATISTICS_set (stats,
1208 "# estimated network diameter",
1209 hop_count_max, GNUNET_NO);
1211 /* have a new, better size estimate, inform clients */
1212 update_network_size_estimate ();
1215 GNUNET_CONTAINER_multipeermap_iterate (peers,
1216 &update_flood_times,
1222 * Method called whenever a peer connects. Sets up the PeerEntry and
1223 * schedules the initial size info transmission to this peer.
1225 * @param cls closure
1226 * @param peer peer identity this notification is about
1229 handle_core_connect (void *cls,
1230 const struct GNUNET_PeerIdentity *peer,
1231 struct GNUNET_MQ_Handle *mq)
1233 struct NSEPeerEntry *peer_entry;
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "Peer `%s' connected to us\n",
1240 /* set our default transmission options */
1241 extra = GNUNET_CORE_get_mq_options (GNUNET_NO,
1244 GNUNET_MQ_set_options (mq,
1247 /* create our peer entry for this peer */
1248 peer_entry = GNUNET_new (struct NSEPeerEntry);
1249 peer_entry->id = peer;
1250 peer_entry->mq = mq;
1251 GNUNET_assert (GNUNET_OK ==
1252 GNUNET_CONTAINER_multipeermap_put (peers,
1255 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1256 peer_entry->transmit_task =
1257 GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1),
1260 GNUNET_STATISTICS_update (stats,
1261 "# peers connected",
1269 * Method called whenever a peer disconnects. Deletes the PeerEntry and cancels
1270 * any pending transmission requests to that peer.
1272 * @param cls closure
1273 * @param peer peer identity this notification is about
1274 * @parma internal_cls the `struct NSEPeerEntry` for the @a peer
1277 handle_core_disconnect (void *cls,
1278 const struct GNUNET_PeerIdentity *peer,
1281 struct NSEPeerEntry *pos = internal_cls;
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284 "Peer `%s' disconnected from us\n",
1286 GNUNET_assert (GNUNET_YES ==
1287 GNUNET_CONTAINER_multipeermap_remove (peers,
1290 if (NULL != pos->transmit_task)
1292 GNUNET_SCHEDULER_cancel (pos->transmit_task);
1293 pos->transmit_task = NULL;
1296 GNUNET_STATISTICS_update (stats,
1297 "# peers connected",
1303 #if ENABLE_NSE_HISTOGRAM
1305 * Functions of this type are called to notify a successful transmission of the
1306 * message to the logger service
1309 * @param size the amount of data sent (ignored)
1312 flush_comp_cb (void *cls,
1315 GNUNET_TESTBED_LOGGER_disconnect (lh);
1322 * Task run during shutdown.
1327 shutdown_task (void *cls)
1329 if (NULL != flood_task)
1331 GNUNET_SCHEDULER_cancel (flood_task);
1334 if (NULL != proof_task)
1336 GNUNET_SCHEDULER_cancel (proof_task);
1338 write_proof (); /* remember progress */
1342 GNUNET_notification_context_destroy (nc);
1345 if (NULL != core_api)
1347 GNUNET_CORE_disconnect (core_api);
1352 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1357 GNUNET_CONTAINER_multipeermap_destroy (peers);
1360 if (NULL != my_private_key)
1362 GNUNET_free (my_private_key);
1363 my_private_key = NULL;
1365 #if ENABLE_NSE_HISTOGRAM
1366 if (NULL != logger_test)
1368 GNUNET_CLIENT_service_test_cancel (logger_test);
1373 GNUNET_TESTBED_LOGGER_flush (lh,
1377 if (NULL != histogram)
1379 GNUNET_BIO_write_close (histogram);
1387 * Called on core init/fail.
1389 * @param cls service closure
1390 * @param identity the public identity of this peer
1393 core_init (void *cls,
1394 const struct GNUNET_PeerIdentity *identity)
1396 struct GNUNET_TIME_Absolute now;
1397 struct GNUNET_TIME_Absolute prev_time;
1399 if (NULL == identity)
1401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1402 "Connection to core FAILED!\n");
1403 GNUNET_SCHEDULER_shutdown ();
1407 GNUNET_memcmp (&my_identity,
1409 now = GNUNET_TIME_absolute_get ();
1410 current_timestamp.abs_value_us =
1411 (now.abs_value_us / gnunet_nse_interval.rel_value_us) *
1412 gnunet_nse_interval.rel_value_us;
1414 GNUNET_TIME_absolute_add (current_timestamp,
1415 gnunet_nse_interval);
1416 estimate_index = HISTORY_SIZE - 1;
1419 check_proof_of_work (&my_identity.public_key,
1422 int idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE;
1423 prev_time.abs_value_us =
1424 current_timestamp.abs_value_us - gnunet_nse_interval.rel_value_us;
1425 setup_flood_message (idx,
1427 setup_flood_message (estimate_index,
1432 = GNUNET_SCHEDULER_add_at (next_timestamp,
1433 &update_flood_message,
1438 #if ENABLE_NSE_HISTOGRAM
1440 * Function called with the status of the testbed logger service
1443 * @param status #GNUNET_YES if the service is running,
1444 * #GNUNET_NO if the service is not running
1445 * #GNUNET_SYSERR if the configuration is invalid
1448 status_cb (void *cls,
1452 if (GNUNET_YES != status)
1454 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1455 "Testbed logger not running\n");
1458 if (NULL == (lh = GNUNET_TESTBED_LOGGER_connect (cfg)))
1460 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1461 "Cannot connect to the testbed logger. Exiting.\n");
1462 GNUNET_SCHEDULER_shutdown ();
1469 * Handle network size estimate clients.
1471 * @param cls closure
1472 * @param c configuration to use
1473 * @param service the initialized service
1477 const struct GNUNET_CONFIGURATION_Handle *c,
1478 struct GNUNET_SERVICE_Handle *service)
1480 struct GNUNET_MQ_MessageHandler core_handlers[] = {
1481 GNUNET_MQ_hd_fixed_size (p2p_estimate,
1482 GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD,
1483 struct GNUNET_NSE_FloodMessage,
1485 GNUNET_MQ_handler_end ()
1488 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
1492 GNUNET_CONFIGURATION_get_value_time (cfg,
1495 &gnunet_nse_interval))
1497 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1500 GNUNET_SCHEDULER_shutdown ();
1504 GNUNET_CONFIGURATION_get_value_time (cfg,
1509 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1512 GNUNET_SCHEDULER_shutdown ();
1516 GNUNET_CONFIGURATION_get_value_number (cfg,
1519 &nse_work_required))
1521 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1524 GNUNET_SCHEDULER_shutdown ();
1527 if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8)
1529 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1532 _("Value is too large.\n"));
1533 GNUNET_SCHEDULER_shutdown ();
1537 #if ENABLE_NSE_HISTOGRAM
1539 char *histogram_dir;
1543 GNUNET_CONFIGURATION_get_value_filename (cfg,
1548 GNUNET_assert (0 < GNUNET_asprintf (&histogram_fn,
1551 GNUNET_free (histogram_dir);
1552 histogram = GNUNET_BIO_write_open (histogram_fn);
1553 if (NULL == histogram)
1554 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1555 "Unable to open histogram file `%s'\n",
1557 GNUNET_free (histogram_fn);
1560 GNUNET_CLIENT_service_test ("testbed-logger",
1562 GNUNET_TIME_UNIT_SECONDS,
1569 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1571 pk = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
1572 GNUNET_assert (NULL != pk);
1573 my_private_key = pk;
1574 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1575 &my_identity.public_key);
1577 GNUNET_CONFIGURATION_get_value_filename (cfg,
1582 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1585 GNUNET_free (my_private_key);
1586 my_private_key = NULL;
1587 GNUNET_SCHEDULER_shutdown ();
1590 if ((GNUNET_YES != GNUNET_DISK_file_test (proof)) ||
1591 (sizeof (my_proof) !=
1592 GNUNET_DISK_fn_read (proof,
1594 sizeof (my_proof))))
1596 GNUNET_free (proof);
1598 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1602 peers = GNUNET_CONTAINER_multipeermap_create (128,
1604 nc = GNUNET_notification_context_create (1);
1605 /* Connect to core service and register core handlers */
1606 core_api = GNUNET_CORE_connect (cfg, /* Main configuration */
1607 NULL, /* Closure passed to functions */
1608 &core_init, /* Call core_init once connected */
1609 &handle_core_connect, /* Handle connects */
1610 &handle_core_disconnect, /* Handle disconnects */
1611 core_handlers); /* Register these handlers */
1612 if (NULL == core_api)
1614 GNUNET_SCHEDULER_shutdown ();
1617 stats = GNUNET_STATISTICS_create ("nse",
1623 * Callback called when a client connects to the service.
1625 * @param cls closure for the service
1626 * @param c the new client that connected to the service
1627 * @param mq the message queue used to send messages to the client
1631 client_connect_cb (void *cls,
1632 struct GNUNET_SERVICE_Client *c,
1633 struct GNUNET_MQ_Handle *mq)
1640 * Callback called when a client disconnected from the service
1642 * @param cls closure for the service
1643 * @param c the client that disconnected
1644 * @param internal_cls should be equal to @a c
1647 client_disconnect_cb (void *cls,
1648 struct GNUNET_SERVICE_Client *c,
1651 GNUNET_assert (c == internal_cls);
1656 * Define "main" method using service macro.
1660 GNUNET_SERVICE_OPTION_NONE,
1663 &client_disconnect_cb,
1665 GNUNET_MQ_hd_fixed_size (start,
1666 GNUNET_MESSAGE_TYPE_NSE_START,
1667 struct GNUNET_MessageHeader,
1669 GNUNET_MQ_handler_end ());
1672 #if defined(LINUX) && defined(__GLIBC__)
1676 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1678 void __attribute__ ((constructor))
1679 GNUNET_ARM_memory_init ()
1681 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1682 mallopt (M_TOP_PAD, 1 * 1024);
1689 /* end of gnunet-service-nse.c */