GNUnet 0.27.0
 
Loading...
Searching...
No Matches
gnunet-service-nse.c File Reference

network size estimation service More...

#include "gnunet_common.h"
#include "platform.h"
#include <math.h>
#include "gnunet_util_lib.h"
#include "gnunet_hello_uri_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_signatures.h"
#include "gnunet_statistics_service.h"
#include "gnunet_core_service.h"
#include "gnunet_pils_service.h"
#include "nse.h"
#include <gcrypt.h>
Include dependency graph for gnunet-service-nse.c:

Go to the source code of this file.

Data Structures

struct  NSEPeerEntry
 Per-peer information. More...
 
struct  GNUNET_NSE_FloodMessage
 Network size estimate reply; sent when "this" peer's timer has run out before receiving a valid reply from another peer. More...
 

Macros

#define USE_RANDOM_DELAYS   GNUNET_YES
 Should messages be delayed randomly? This option should be set to GNUNET_NO only for experiments, not in production.
 
#define DEBUG_NSE   GNUNET_NO
 Generate extensive debug-level log messages?
 
#define HISTORY_SIZE   64
 Over how many values do we calculate the weighted average?
 
#define NSE_PRIORITY
 Message priority to use.
 
#define WEST   1
 
#define ROUND_SIZE   10
 

Functions

static void setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
 Initialize a message to clients with the current network size estimate.
 
static void handle_start (void *cls, const struct GNUNET_MessageHeader *message)
 Handler for START message from client, triggers an immediate current network estimate notification.
 
static double get_matching_bits_delay (uint32_t matching_bits)
 How long should we delay a message to go the given number of matching bits?
 
static struct GNUNET_TIME_Relative get_delay_randomization (uint32_t matching_bits)
 What delay randomization should we apply for a given number of matching bits?
 
static uint32_t get_matching_bits (struct GNUNET_TIME_Absolute timestamp, const struct GNUNET_PeerIdentity *id)
 Get the number of matching bits that the given timestamp has to the given peer ID.
 
static struct GNUNET_TIME_Relative get_transmit_delay (int round_offset)
 Get the transmission delay that should be applied for a particular round.
 
static void sign_message_before_send_cb (void *cls, const struct GNUNET_PeerIdentity *pid, const struct GNUNET_CRYPTO_EddsaSignature *sig)
 
static void transmit_task_cb (void *cls)
 Task that triggers a NSE P2P transmission.
 
static void update_network_size_estimate ()
 We've sent on our flood message or one that we received which was validated and closer than ours.
 
static void setup_flood_message (unsigned int slot, struct GNUNET_TIME_Absolute ts)
 Setup a flood message in our history array at the given slot offset for the given timestamp.
 
static int schedule_current_round (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
 Schedule transmission for the given peer for the current round based on what we know about the desired delay.
 
static void update_flood_message (void *cls)
 Update our flood message to be sent (and our timestamps).
 
static enum GNUNET_GenericReturnValue check_proof_of_work (const struct GNUNET_CRYPTO_EddsaPublicKey *pkey, uint64_t val)
 Check whether the given public key and integer are a valid proof of work.
 
static void write_proof (void)
 Write our current proof to disk.
 
static void find_proof (void *cls)
 Find our proof of work.
 
static int verify_message_crypto (const struct GNUNET_NSE_FloodMessage *incoming_flood)
 An incoming flood message has been received which claims to have more bits matching than any we know in this time period.
 
static int update_flood_times (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
 Update transmissions for the given peer for the current round based on updated proximity information.
 
static void handle_p2p_estimate (void *cls, const struct GNUNET_NSE_FloodMessage *incoming_flood)
 Core handler for size estimate flooding messages.
 
static void * handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq, enum GNUNET_CORE_PeerClass class)
 Method called whenever a peer connects.
 
static void handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer, void *internal_cls)
 Method called whenever a peer disconnects.
 
static void shutdown_task (void *cls)
 Task run during shutdown.
 
static void pils_id_change_cb (void *cls, const struct GNUNET_HELLO_Parser *parser, const struct GNUNET_HashCode *addr_hash)
 
static void run (void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service)
 Handle network size estimate clients.
 
static void * client_connect_cb (void *cls, struct GNUNET_SERVICE_Client *c, struct GNUNET_MQ_Handle *mq)
 Callback called when a client connects to the service.
 
static void client_disconnect_cb (void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
 Callback called when a client disconnected from the service.
 
 GNUNET_SERVICE_MAIN (GNUNET_OS_project_data_gnunet(), "nse", GNUNET_SERVICE_OPTION_NONE, &run, &client_connect_cb, &client_disconnect_cb, NULL, GNUNET_MQ_hd_fixed_size(start, GNUNET_MESSAGE_TYPE_NSE_START, struct GNUNET_MessageHeader, NULL), GNUNET_MQ_handler_end())
 Define "main" method using service macro.
 

Variables

static unsigned long long nse_work_required
 Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
 
static struct GNUNET_TIME_Relative gnunet_nse_interval
 Interval for sending network size estimation flood requests.
 
static struct GNUNET_TIME_Relative proof_find_delay
 Interval between proof find runs.
 
static struct GNUNET_CRYPTO_PowSalt salt = { "gnunet-nse-proof" }
 Salt for PoW calculations.
 
static GNUNET_NETWORK_STRUCT_END const struct GNUNET_CONFIGURATION_Handlecfg
 Handle to our current configuration.
 
static struct GNUNET_STATISTICS_Handlestats
 Handle to the statistics service.
 
static struct GNUNET_PILS_Handlepils
 Handle to the PILS service.
 
static struct GNUNET_CORE_Handlecore_api
 Handle to the core service.
 
static struct GNUNET_CONTAINER_MultiPeerMappeers
 Map of all connected peers.
 
static double current_size_estimate
 The current network size estimate.
 
static double current_std_dev = NAN
 The standard deviation of the last HISTORY_SIZE network size estimates.
 
static uint32_t hop_count_max
 Current hop counter estimate (estimate for network diameter).
 
static struct GNUNET_NSE_FloodMessage next_message
 Message for the next round, if we got any.
 
static struct GNUNET_NSE_FloodMessage size_estimate_messages [64]
 Array of recent size estimate messages.
 
static unsigned int estimate_index
 Index of most recent estimate.
 
static unsigned int estimate_count
 Number of valid entries in the history.
 
static struct GNUNET_SCHEDULER_Taskflood_task
 Task scheduled to update our flood message for the next round.
 
static struct GNUNET_SCHEDULER_Taskproof_task
 Task scheduled to compute our proof.
 
static struct GNUNET_NotificationContextnc
 Notification context, simplifies client broadcasts.
 
static struct GNUNET_TIME_Absolute next_timestamp
 The next major time.
 
static struct GNUNET_TIME_Absolute current_timestamp
 The current major time.
 
static uint64_t my_proof
 Proof of work for this peer.
 

Detailed Description

network size estimation service

Author
Nathan Evans
Christian Grothoff

The purpose of this service is to estimate the size of the network. Given a specified interval, each peer hashes the most recent timestamp which is evenly divisible by that interval. This hash is compared in distance to the peer identity to choose an offset. The closer the peer identity to the hashed timestamp, the earlier the peer sends out a "nearest peer" message. The closest peer's message should thus be received before any others, which stops those peer from sending their messages at a later duration. So every peer should receive the same nearest peer message, and from this can calculate the expected number of peers in the network.

Definition in file gnunet-service-nse.c.

Macro Definition Documentation

◆ USE_RANDOM_DELAYS

#define USE_RANDOM_DELAYS   GNUNET_YES

Should messages be delayed randomly? This option should be set to GNUNET_NO only for experiments, not in production.

Definition at line 59 of file gnunet-service-nse.c.

◆ DEBUG_NSE

#define DEBUG_NSE   GNUNET_NO

Generate extensive debug-level log messages?

Definition at line 64 of file gnunet-service-nse.c.

◆ HISTORY_SIZE

#define HISTORY_SIZE   64

Over how many values do we calculate the weighted average?

Definition at line 69 of file gnunet-service-nse.c.

◆ NSE_PRIORITY

#define NSE_PRIORITY
Value:
@ GNUNET_MQ_PRIO_BACKGROUND
Lowest priority, i.e.
@ GNUNET_MQ_PREF_CORK_ALLOWED
Flag to indicate that CORKing is acceptable.
@ GNUNET_MQ_PREF_UNRELIABLE
Flag to indicate that unreliable delivery is acceptable.

Message priority to use.

No real rush, reliability not required. Corking OK.

Definition at line 75 of file gnunet-service-nse.c.

119 { "gnunet-nse-proof" };
120
121
125struct NSEPeerEntry
126{
130 struct GNUNET_MQ_Handle *mq;
131
135 const struct GNUNET_PeerIdentity *id;
136
141
145 uint32_t next_index_to_send;
146
151
157 int previous_round;
158
159#if ENABLE_NSE_HISTOGRAM
163 unsigned int received_messages;
164
168 unsigned int transmitted_messages;
169
173 unsigned int last_transmitted_size;
174#endif
175};
176
177
179
186{
191
195 uint32_t hop_count GNUNET_PACKED;
196
201
207
214
219
224
229};
231
235static const struct GNUNET_CONFIGURATION_Handle *cfg;
236
240static struct GNUNET_STATISTICS_Handle *stats;
241
245static struct GNUNET_PILS_Handle *pils;
246
250static struct GNUNET_CORE_Handle *core_api;
251
256
261static double current_size_estimate;
262
267static double current_std_dev = NAN;
268
272static uint32_t hop_count_max;
273
278
283
287static unsigned int estimate_index;
288
292static unsigned int estimate_count;
293
297static struct GNUNET_SCHEDULER_Task *flood_task;
298
302static struct GNUNET_SCHEDULER_Task *proof_task;
303
307static struct GNUNET_NotificationContext *nc;
308
313
318
322static uint64_t my_proof;
323
324
331static void
333{
334 double mean;
335 double sum;
336 double std_dev;
337 double variance;
338 double val;
339 double nsize;
340
341#define WEST 1
342 /* Weighted incremental algorithm for stddev according to West (1979) */
343#if WEST
344 double sumweight;
345 double weight;
346 double q;
347 double r;
348 double temp;
349
350 mean = 0.0;
351 sum = 0.0;
352 sumweight = 0.0;
353 variance = 0.0;
354 for (unsigned int i = 0; i < estimate_count; i++)
355 {
356 unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
357
358 val = htonl (size_estimate_messages[j].matching_bits);
359 weight = estimate_count + 1 - i;
360
361 temp = weight + sumweight;
362 q = val - mean;
363 r = q * weight / temp;
364 mean += r;
365 sum += sumweight * q * r;
366 sumweight = temp;
367 }
368 if (estimate_count > 0)
369 variance = (sum / sumweight) * estimate_count / (estimate_count - 1.0);
370#else
371 /* trivial version for debugging */
372 double vsq;
373
374 /* non-weighted trivial version */
375 sum = 0.0;
376 vsq = 0.0;
377 variance = 0.0;
378 mean = 0.0;
379
380 for (unsigned int i = 0; i < estimate_count; i++)
381 {
382 unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
383
384 val = htonl (size_estimate_messages[j].matching_bits);
385 sum += val;
386 vsq += val * val;
387 }
388 if (0 != estimate_count)
389 {
390 mean = sum / estimate_count;
391 variance = (vsq - mean * sum)
392 / (estimate_count - 1.0); // terrible for numerical stability...
393 }
394#endif
395 if (variance >= 0)
396 std_dev = sqrt (variance);
397 else
398 std_dev = variance; /* return NaN (due to estimate_count == 0 causing 0.0/0.0) */
399 current_std_dev = std_dev;
401
402 em->header.size = htons (sizeof(struct GNUNET_NSE_ClientMessage));
403 em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
404 em->reserved = htonl (0);
406 {
407 double se = mean - 0.332747;
408 unsigned int j = GNUNET_CONTAINER_multipeermap_size (peers);
409 if (0 == j)
410 j = 1; /* Avoid log2(0); can only happen if CORE didn't report
411 connection to self yet */
412 nsize = log2 (j);
413 em->size_estimate = GNUNET_hton_double (GNUNET_MAX (se, nsize));
414 em->std_deviation = GNUNET_hton_double (std_dev);
416 "# nodes in the network (estimate)",
417 (uint64_t) pow (2, GNUNET_MAX (se, nsize)),
418 GNUNET_NO);
419 }
420}
421
422
432static void
433handle_start (void *cls, const struct GNUNET_MessageHeader *message)
434{
435 struct GNUNET_SERVICE_Client *client = cls;
436 struct GNUNET_MQ_Handle *mq;
437 struct GNUNET_NSE_ClientMessage em;
438 struct GNUNET_MQ_Envelope *env;
439
440 (void) message;
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received START message from client\n");
445 env = GNUNET_MQ_msg_copy (&em.header);
448}
449
450
457static double
459{
460 /* Calculated as: S + f/2 - (f / pi) * (atan(x - p')) */
461 // S is next_timestamp (ignored in return value)
462 // f is frequency (gnunet_nse_interval)
463 // x is matching_bits
464 // p' is current_size_estimate
465 return ((double) gnunet_nse_interval.rel_value_us / (double) 2.0)
468}
469
470
477static struct GNUNET_TIME_Relative
479{
480#if USE_RANDOM_DELAYS
482 uint32_t i;
483 double d;
484
486 i = (uint32_t) (d / (double) (hop_count_max + 1));
487 ret.rel_value_us = i;
489 "Randomizing flood using latencies up to %s\n",
491 ret.rel_value_us =
493 return ret;
494#else
496#endif
497}
498
499
507static uint32_t
509 const struct GNUNET_PeerIdentity *id)
510{
511 struct GNUNET_HashCode timestamp_hash;
512 struct GNUNET_HashCode pid_hash;
513 struct GNUNET_HashCode xor;
514
515 GNUNET_CRYPTO_hash (&timestamp.abs_value_us,
516 sizeof(timestamp.abs_value_us),
517 &timestamp_hash);
519 sizeof(struct GNUNET_PeerIdentity),
520 &pid_hash);
521 GNUNET_CRYPTO_hash_xor (&pid_hash,
522 &timestamp_hash,
523 &xor);
525}
526
527
536static struct GNUNET_TIME_Relative
537get_transmit_delay (int round_offset)
538{
540 struct GNUNET_TIME_Absolute tgt;
541 double dist_delay;
542 uint32_t matching_bits;
543
544 switch (round_offset)
545 {
546 case -1:
547 /* previous round is randomized between 0 and 50 ms */
548#if USE_RANDOM_DELAYS
549 ret.rel_value_us =
551#else
553#endif
555 "Transmitting previous round behind schedule in %s\n",
557 return ret;
558
559 case 0:
560 /* current round is based on best-known matching_bits */
565 ret.rel_value_us = (uint64_t) dist_delay;
567 "For round %s, delay for %u matching bits is %s\n",
569 (unsigned int) matching_bits,
571 /* now consider round start time and add delay to it */
574 }
575 GNUNET_break (0);
577}
578
579
580static void
582 const struct GNUNET_PeerIdentity *pid,
583 const struct GNUNET_CRYPTO_EddsaSignature *sig)
584{
585 struct NSEPeerEntry *pe = cls;
586 struct GNUNET_MQ_Envelope *env;
587 struct GNUNET_NSE_FloodMessage *fm;
588
589 pe->pils_op = NULL;
591 fm->signature = *sig;
593 GNUNET_MQ_send (pe->mq, env);
594}
595
596
602static void
603transmit_task_cb (void *cls)
604{
605 struct NSEPeerEntry *peer_entry = cls;
606 unsigned int idx;
607
608 peer_entry->transmit_task = NULL;
609 idx = estimate_index;
610 if (GNUNET_NO == peer_entry->previous_round)
611 {
612 idx = (idx + HISTORY_SIZE - 1) % HISTORY_SIZE;
613 peer_entry->previous_round = GNUNET_YES;
614 peer_entry->transmit_task =
617 peer_entry);
618 }
619 if ((0 == ntohl (size_estimate_messages[idx].hop_count)) &&
620 (NULL != proof_task))
621 {
623 "# flood messages not generated (no proof yet)",
624 1,
625 GNUNET_NO);
626 return;
627 }
628 if (0 == ntohs (size_estimate_messages[idx].header.size))
629 {
631 "# flood messages not generated (lack of history)",
632 1,
633 GNUNET_NO);
634 return;
635 }
637 "In round %s, sending to `%s' estimate with %u bits\n",
641 GNUNET_i2s (peer_entry->id),
642 (unsigned int) ntohl (size_estimate_messages[idx].matching_bits));
643 if (0 == ntohl (size_estimate_messages[idx].hop_count))
644 GNUNET_STATISTICS_update (stats, "# flood messages started", 1, GNUNET_NO);
646 "# flood messages transmitted",
647 1,
648 GNUNET_NO);
649#if ENABLE_NSE_HISTOGRAM
650 peer_entry->transmitted_messages++;
651 peer_entry->last_transmitted_size =
653#endif
654 peer_entry->next_index_to_send = idx;
656 pils,
657 &size_estimate_messages[idx].purpose,
659 peer_entry);
660}
661
662
669static void
671{
672 struct GNUNET_NSE_ClientMessage em;
673
676}
677
678
686static void
687setup_flood_message (unsigned int slot, struct GNUNET_TIME_Absolute ts)
688{
689 const struct GNUNET_PeerIdentity *my_identity;
690 struct GNUNET_NSE_FloodMessage *fm;
691 uint32_t matching_bits;
692
695
697 fm = &size_estimate_messages[slot];
698 fm->header.size = htons (sizeof(struct GNUNET_NSE_FloodMessage));
700 fm->hop_count = htonl (0);
702 fm->purpose.size =
703 htonl (sizeof(struct GNUNET_NSE_FloodMessage)
704 - sizeof(struct GNUNET_MessageHeader) - sizeof(uint32_t)
705 - sizeof(struct GNUNET_CRYPTO_EddsaSignature));
706 fm->matching_bits = htonl (matching_bits);
708 fm->origin = *my_identity;
710 memset (&fm->signature, 0, sizeof(fm->signature));
711}
712
713
723static int
724schedule_current_round (void *cls,
725 const struct GNUNET_PeerIdentity *key,
726 void *value)
727{
728 struct NSEPeerEntry *peer_entry = value;
729 struct GNUNET_TIME_Relative delay;
730
731 (void) cls;
732 (void) key;
733 if (NULL != peer_entry->transmit_task)
734 {
736 peer_entry->previous_round = GNUNET_NO;
737 }
738#if ENABLE_NSE_HISTOGRAM
739 if (peer_entry->received_messages > 1)
741 "# extra messages",
742 peer_entry->received_messages - 1,
743 GNUNET_NO);
744 peer_entry->transmitted_messages = 0;
745 peer_entry->last_transmitted_size = 0;
746 peer_entry->received_messages = 0;
747#endif
748 delay =
749 get_transmit_delay ((GNUNET_NO == peer_entry->previous_round) ? -1 : 0);
750 peer_entry->transmit_task =
752 return GNUNET_OK;
753}
754
755
761static void
762update_flood_message (void *cls)
763{
764 const struct GNUNET_PeerIdentity *my_identity;
765 struct GNUNET_TIME_Relative offset;
766
767 (void) cls;
768 flood_task = NULL;
770 if (! my_identity)
771 return;
772
774 if (0 != offset.rel_value_us)
775 {
776 /* somehow run early, delay more */
777 flood_task =
779 return;
780 }
791 {
792 /* we received a message for this round way early, use it! */
795 htonl (1 + ntohl (next_message.hop_count));
796 }
797 else
799 next_message.matching_bits = htonl (0); /* reset for 'next' round */
800 hop_count_max = 0;
801 for (unsigned int i = 0; i < HISTORY_SIZE; i++)
803 GNUNET_MAX (ntohl (size_estimate_messages[i].hop_count), hop_count_max);
805 flood_task =
807}
808
809
820 uint64_t val)
821{
822 char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
823 + sizeof(val)] GNUNET_ALIGN;
824 struct GNUNET_HashCode result;
825
826 GNUNET_memcpy (buf, &val, sizeof(val));
827 GNUNET_memcpy (&buf[sizeof(val)],
828 pkey,
829 sizeof(struct GNUNET_CRYPTO_EddsaPublicKey));
831 buf,
832 sizeof(buf),
833 &result);
836 ? GNUNET_YES
837 : GNUNET_NO;
838}
839
840
844static void
845write_proof (void)
846{
847 char *proof;
848
849 if (GNUNET_OK !=
851 "NSE",
852 "PROOFFILE",
853 &proof))
854 return;
856 if (GNUNET_OK !=
858 &my_proof,
859 sizeof(my_proof),
863 "write",
864 proof);
866}
867
868
874static void
875find_proof (void *cls)
876{
877#define ROUND_SIZE 10
878 uint64_t counter;
879 char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
880 + sizeof(uint64_t)] GNUNET_ALIGN;
881 struct GNUNET_HashCode result;
882 const struct GNUNET_PeerIdentity *my_identity;
883 unsigned int i;
884
885 (void) cls;
886 proof_task = NULL;
889 GNUNET_memcpy (&buf[sizeof(uint64_t)],
891 sizeof(struct GNUNET_PeerIdentity));
892 i = 0;
893 counter = my_proof;
894 while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
895 {
896 GNUNET_memcpy (buf, &counter, sizeof(uint64_t));
898 buf,
899 sizeof(buf),
900 &result);
901 if (nse_work_required <=
903 {
904 my_proof = counter;
906 "Proof of work found: %llu!\n",
907 (unsigned long long) GNUNET_ntohll (counter));
908 write_proof ();
910 return;
911 }
912 counter++;
913 i++;
914 }
915 if (my_proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
916 {
918 "Testing proofs currently at %llu\n",
919 (unsigned long long) counter);
920 /* remember progress every 100 rounds */
921 my_proof = counter;
922 write_proof ();
923 }
924 else
925 {
926 my_proof = counter;
927 }
928 proof_task =
931 &find_proof,
932 NULL);
933}
934
935
945static int
946verify_message_crypto (const struct GNUNET_NSE_FloodMessage *incoming_flood)
947{
948 if (GNUNET_YES != check_proof_of_work (&incoming_flood->origin.public_key,
949 incoming_flood->proof_of_work))
950 {
952 "Proof of work invalid: %llu!\n",
953 (unsigned long long) GNUNET_ntohll (
954 incoming_flood->proof_of_work));
955 GNUNET_break_op (0);
956 return GNUNET_NO;
957 }
958 if ((nse_work_required > 0) &&
959 (GNUNET_OK !=
961 &incoming_flood->purpose,
962 &incoming_flood->signature,
963 &incoming_flood->origin.public_key)))
964 {
965 GNUNET_break_op (0);
966 return GNUNET_NO;
967 }
968 return GNUNET_YES;
969}
970
971
981static int
982update_flood_times (void *cls,
983 const struct GNUNET_PeerIdentity *key,
984 void *value)
985{
986 struct NSEPeerEntry *exclude = cls;
987 struct NSEPeerEntry *peer_entry = value;
988 struct GNUNET_TIME_Relative delay;
989
990 (void) key;
991 if (peer_entry == exclude)
992 return GNUNET_OK; /* trigger of the update */
993 if (GNUNET_NO == peer_entry->previous_round)
994 {
995 /* still stuck in previous round, no point to update, check that
996 * we are active here though... */
997 if (NULL == peer_entry->transmit_task)
998 {
999 GNUNET_break (0);
1000 }
1001 return GNUNET_OK;
1002 }
1003 if (NULL != peer_entry->transmit_task)
1004 {
1006 peer_entry->transmit_task = NULL;
1007 }
1008 delay = get_transmit_delay (0);
1009 peer_entry->transmit_task =
1010 GNUNET_SCHEDULER_add_delayed (delay, &transmit_task_cb, peer_entry);
1011 return GNUNET_OK;
1012}
1013
1014
1021static void
1022handle_p2p_estimate (void *cls,
1023 const struct GNUNET_NSE_FloodMessage *incoming_flood)
1024{
1025 const struct GNUNET_PeerIdentity *my_identity;
1026 struct NSEPeerEntry *peer_entry = cls;
1027 struct GNUNET_TIME_Absolute ts;
1028 uint32_t matching_bits;
1029 unsigned int idx;
1030
1032 if (! my_identity)
1033 return;
1034
1035#if ENABLE_NSE_HISTOGRAM
1036 {
1037 uint64_t t;
1038
1040 if (NULL != lh)
1041 GNUNET_TESTBED_LOGGER_write (lh, &t, sizeof(uint64_t));
1042 if (NULL != histogram)
1043 GNUNET_BIO_write_int64 (histogram, "histogram-time", t);
1044 }
1045#endif
1046 GNUNET_STATISTICS_update (stats, "# flood messages received", 1, GNUNET_NO);
1047 matching_bits = ntohl (incoming_flood->matching_bits);
1048#if DEBUG_NSE
1049 {
1050 char origin[5];
1051 char pred[5];
1052 struct GNUNET_PeerIdentity os;
1053
1055 sizeof(origin),
1056 "%s",
1057 GNUNET_i2s (&incoming_flood->origin));
1058 GNUNET_snprintf (pred, sizeof(pred), "%s", GNUNET_i2s (peer_entry->id));
1060 "Flood at %s from `%s' via `%s' at `%s' with bits %u\n",
1062 GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp)),
1063 origin,
1064 pred,
1066 (unsigned int) matching_bits);
1067 }
1068#endif
1069
1070#if ENABLE_NSE_HISTOGRAM
1071 peer_entry->received_messages++;
1072 if ((peer_entry->transmitted_messages > 0) &&
1073 (peer_entry->last_transmitted_size >= matching_bits) )
1074 GNUNET_STATISTICS_update (stats, "# cross messages", 1, GNUNET_NO);
1075#endif
1076
1077 ts = GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp);
1079 idx = estimate_index;
1080 else if (ts.abs_value_us ==
1084 {
1086 return; /* ignore, simply too early/late */
1087 if (GNUNET_YES != verify_message_crypto (incoming_flood))
1088 {
1090 "Peer %s is likely ill-configured!\n",
1091 GNUNET_i2s (peer_entry->id));
1092 GNUNET_break_op (0);
1093 return;
1094 }
1095 next_message = *incoming_flood;
1096 return;
1097 }
1098 else
1099 {
1101 "# flood messages discarded (clock skew too large)",
1102 1,
1103 GNUNET_NO);
1104 return;
1105 }
1106 if (0 == (GNUNET_memcmp (peer_entry->id, my_identity)))
1107 {
1108 /* send to self, update our own estimate IF this also comes from us! */
1109 if (0 == GNUNET_memcmp (&incoming_flood->origin, my_identity))
1111 return;
1112 }
1114 {
1115 /* Cancel transmission in the other direction, as this peer clearly has
1116 up-to-date information already. Even if we didn't talk to this peer in
1117 the previous round, we should no longer send it stale information as it
1118 told us about the current round! */
1119 peer_entry->previous_round = GNUNET_YES;
1120 if (idx != estimate_index)
1121 {
1122 /* do not transmit information for the previous round to this peer
1123 anymore (but allow current round) */
1124 return;
1125 }
1126 /* got up-to-date information for current round, cancel transmission to
1127 * this peer altogether */
1128 if (NULL != peer_entry->transmit_task)
1129 {
1131 peer_entry->transmit_task = NULL;
1132 }
1133 return;
1134 }
1136 {
1137 if ((idx < estimate_index) && (peer_entry->previous_round == GNUNET_YES))
1138 {
1139 peer_entry->previous_round = GNUNET_NO;
1140 }
1141 /* push back our result now, that peer is spreading bad information... */
1142 if (NULL != peer_entry->transmit_task)
1144 peer_entry->transmit_task =
1146 /* Not closer than our most recent message, no need to do work here */
1148 "# flood messages ignored (had closer already)",
1149 1,
1150 GNUNET_NO);
1151 return;
1152 }
1153 if (GNUNET_YES != verify_message_crypto (incoming_flood))
1154 {
1155 GNUNET_break_op (0);
1156 return;
1157 }
1160 /* Cancel transmission in the other direction, as this peer clearly has
1161 * up-to-date information already.
1162 */
1163 peer_entry->previous_round = GNUNET_YES;
1164 if (idx == estimate_index)
1165 {
1166 /* cancel any activity for current round */
1167 if (NULL != peer_entry->transmit_task)
1168 {
1170 peer_entry->transmit_task = NULL;
1171 }
1172 }
1173 size_estimate_messages[idx] = *incoming_flood;
1175 htonl (ntohl (incoming_flood->hop_count) + 1);
1177 GNUNET_MAX (ntohl (incoming_flood->hop_count) + 1, hop_count_max);
1179 "# estimated network diameter",
1181 GNUNET_NO);
1182
1183 /* have a new, better size estimate, inform clients */
1185
1186 /* flood to rest */
1189 peer_entry);
1190}
1191
1192
1201static void *
1202handle_core_connect (void *cls,
1203 const struct GNUNET_PeerIdentity *peer,
1204 struct GNUNET_MQ_Handle *mq,
1205 enum GNUNET_CORE_PeerClass class)
1206{
1207 struct NSEPeerEntry *peer_entry;
1208
1209 (void) cls;
1211 "Peer `%s' connected to us\n",
1212 GNUNET_i2s (peer));
1213 /* set our default transmission options */
1215 /* create our peer entry for this peer */
1216 peer_entry = GNUNET_new (struct NSEPeerEntry);
1217 peer_entry->id = peer;
1218 peer_entry->mq = mq;
1221 peers,
1222 peer_entry->id,
1223 peer_entry,
1225 peer_entry->transmit_task =
1228 peer_entry);
1229 GNUNET_STATISTICS_update (stats, "# peers connected", 1, GNUNET_NO);
1230 return peer_entry;
1231}
1232
1233
1242static void
1243handle_core_disconnect (void *cls,
1244 const struct GNUNET_PeerIdentity *peer,
1245 void *internal_cls)
1246{
1247 struct NSEPeerEntry *pos = internal_cls;
1248
1249 (void) cls;
1251 "Peer `%s' disconnected from us\n",
1252 GNUNET_i2s (peer));
1255 if (NULL != pos->transmit_task)
1256 {
1258 pos->transmit_task = NULL;
1259 }
1260 GNUNET_free (pos);
1261 GNUNET_STATISTICS_update (stats, "# peers connected", -1, GNUNET_NO);
1262}
1263
1264
1265#if ENABLE_NSE_HISTOGRAM
1273static void
1274flush_comp_cb (void *cls, size_t size)
1275{
1276 (void) cls;
1277 (void) size;
1278 GNUNET_TESTBED_LOGGER_disconnect (lh);
1279 lh = NULL;
1280}
1281
1282
1283#endif
1284
1285
1291static void
1292shutdown_task (void *cls)
1293{
1294 (void) cls;
1295 if (NULL != flood_task)
1296 {
1298 flood_task = NULL;
1299 }
1300 if (NULL != proof_task)
1301 {
1303 proof_task = NULL;
1304 write_proof (); /* remember progress */
1305 }
1306 if (NULL != nc)
1307 {
1309 nc = NULL;
1310 }
1311 if (NULL != core_api)
1312 {
1314 core_api = NULL;
1315 }
1316 if (NULL != stats)
1317 {
1319 stats = NULL;
1320 }
1321 if (NULL != peers)
1322 {
1324 peers = NULL;
1325 }
1326 if (NULL != pils)
1327 {
1329 pils = NULL;
1330 }
1331#if ENABLE_NSE_HISTOGRAM
1332 if (NULL != logger_test)
1333 {
1334 GNUNET_CLIENT_service_test_cancel (logger_test);
1335 logger_test = NULL;
1336 }
1337 if (NULL != lh)
1338 {
1339 GNUNET_TESTBED_LOGGER_flush (lh, &flush_comp_cb, NULL);
1340 }
1341 if (NULL != histogram)
1342 {
1343 GNUNET_BIO_write_close (histogram, NULL);
1344 histogram = NULL;
1345 }
1346#endif
1347}
1348
1349
1350static void
1351pils_id_change_cb (void *cls,
1352 const struct GNUNET_HELLO_Parser *parser,
1353 const struct GNUNET_HashCode *addr_hash)
1354{
1355 const struct GNUNET_PeerIdentity *my_identity;
1356 struct GNUNET_TIME_Absolute now;
1357 struct GNUNET_TIME_Absolute prev_time;
1358
1360
1361 if (NULL != proof_task)
1362 {
1364 proof_task = NULL;
1365 }
1366
1367 if (NULL == my_identity)
1368 {
1370 "Connection to core FAILED!\n");
1372 return;
1373 }
1374 proof_task =
1376 &find_proof,
1377 NULL);
1378 now = GNUNET_TIME_absolute_get ();
1380 (now.abs_value_us / gnunet_nse_interval.rel_value_us)
1385 estimate_count = 0;
1387 {
1388 int idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE;
1389 prev_time.abs_value_us =
1391 setup_flood_message (idx, prev_time);
1394 }
1395 if (NULL != flood_task)
1397 flood_task =
1399}
1400
1401
1402#if ENABLE_NSE_HISTOGRAM
1411static void
1412status_cb (void *cls, int status)
1413{
1414 (void) cls;
1415 logger_test = NULL;
1416 if (GNUNET_YES != status)
1417 {
1418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Testbed logger not running\n");
1419 return;
1420 }
1421 if (NULL == (lh = GNUNET_TESTBED_LOGGER_connect (cfg)))
1422 {
1424 "Cannot connect to the testbed logger. Exiting.\n");
1426 }
1427}
1428
1429
1430#endif
1431
1432
1440static void
1441run (void *cls,
1442 const struct GNUNET_CONFIGURATION_Handle *c,
1444{
1445 struct GNUNET_MQ_MessageHandler core_handlers[] =
1446 { GNUNET_MQ_hd_fixed_size (p2p_estimate,
1449 NULL),
1451 char *proof;
1452 const struct GNUNET_CORE_ServiceInfo service_info = {
1454 .version = { 1, 0 },
1455 .version_max = { 1, 0 },
1456 .version_min = { 1, 0 },
1457 };
1458
1459 (void) cls;
1460 (void) service;
1461 cfg = c;
1463 "NSE",
1464 "INTERVAL",
1466 {
1469 return;
1470 }
1472 "NSE",
1473 "WORKDELAY",
1475 {
1478 return;
1479 }
1481 "NSE",
1482 "WORKBITS",
1484 {
1487 return;
1488 }
1489 if (nse_work_required >= sizeof(struct GNUNET_HashCode) * 8)
1490 {
1492 "NSE",
1493 "WORKBITS",
1494 _ ("Value is too large.\n"));
1496 return;
1497 }
1498
1499#if ENABLE_NSE_HISTOGRAM
1500 {
1501 char *histogram_dir;
1502 char *histogram_fn;
1503
1505 "NSE",
1506 "HISTOGRAM_DIR",
1507 &histogram_dir))
1508 {
1510 0 < GNUNET_asprintf (&histogram_fn, "%s/timestamps", histogram_dir));
1511 GNUNET_free (histogram_dir);
1512 histogram = GNUNET_BIO_write_open_file (histogram_fn);
1513 if (NULL == histogram)
1515 "Unable to open histogram file `%s'\n",
1516 histogram_fn);
1517 GNUNET_free (histogram_fn);
1518 }
1519 logger_test = GNUNET_CLIENT_service_test ("testbed-logger",
1520 cfg,
1522 &status_cb,
1523 NULL);
1524 }
1525#endif
1526
1528 if (GNUNET_OK !=
1529 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "PROOFFILE", &proof))
1530 {
1533 return;
1534 }
1536 (sizeof(my_proof) !=
1538 my_proof = 0;
1540
1543 /* Connect to core service and register core handlers */
1544 core_api =
1545 GNUNET_CORE_connect (cfg, /* Main configuration */
1546 NULL, /* Closure passed to functions */
1547 NULL, /* Call core_init once connected */
1548 &handle_core_connect, /* Handle connects */
1549 &handle_core_disconnect, /* Handle disconnects */
1550 core_handlers, /* Register these handlers */
1551 &service_info);
1552 if (NULL == core_api)
1553 {
1555 return;
1556 }
1558 if (NULL == pils)
1559 {
1560 GNUNET_break (0);
1562 return;
1563 }
1565}
1566
1567
1576static void *
1577client_connect_cb (void *cls,
1578 struct GNUNET_SERVICE_Client *c,
1579 struct GNUNET_MQ_Handle *mq)
1580{
1581 (void) cls;
1582 (void) mq;
1583 return c;
1584}
1585
1586
1594static void
1595client_disconnect_cb (void *cls,
1596 struct GNUNET_SERVICE_Client *c,
1597 void *internal_cls)
1598{
1599 (void) cls;
1600 GNUNET_assert (c == internal_cls);
1601}
1602
1603
1608 "nse",
1610 &run,
1613 NULL,
1616 struct GNUNET_MessageHeader,
1617 NULL),
1619
1620
1621#if defined(__linux__) && defined(__GLIBC__)
1622#include <malloc.h>
1623
1624void __attribute__ ((constructor))
1625GNUNET_NSE_memory_init (void);
1626
1630void __attribute__ ((constructor))
1631GNUNET_NSE_memory_init (void)
1632{
1633 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1634 mallopt (M_TOP_PAD, 1 * 1024);
1635 malloc_trim (0);
1636}
1637
1638
1639#endif
1640
1641
1642/* end of gnunet-service-nse.c */
struct GNUNET_MQ_Envelope * env
Definition 005.c:1
static mp_limb_t d[(((256)+GMP_NUMB_BITS - 1)/GMP_NUMB_BITS)]
static int start
Set if we are to start default services (including ARM).
Definition gnunet-arm.c:38
static int ret
Final status code.
Definition gnunet-arm.c:93
static uint64_t timestamp(void)
Get current timestamp.
struct GNUNET_HashCode key
The key used in the DHT.
struct GNUNET_SCHEDULER_Task * shutdown_task
static char * pkey
Public key of the zone to look in, in ASCII.
static struct GNUNET_SCHEDULER_Task * t
Main task.
static char origin[GNUNET_DNSPARSER_MAX_NAME_LENGTH]
Current origin.
static char * value
Value of the record to add/remove.
static int status
The program status; 0 for success.
Definition gnunet-nse.c:39
static int result
Global testing status.
static struct GNUNET_REVOCATION_Query * q
Handle for revocation query.
static unsigned long long matching_bits
Number of matching bits required for revocation.
static uint64_t proof
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
static struct GNUNET_PeerIdentity my_identity
Identity of this peer.
static struct GNUNET_CONTAINER_MultiPeerMap * peers
Map of all connected peers.
static double current_std_dev
The standard deviation of the last HISTORY_SIZE network size estimates.
static struct GNUNET_NSE_FloodMessage size_estimate_messages[64]
Array of recent size estimate messages.
static void update_flood_message(void *cls)
Update our flood message to be sent (and our timestamps).
static void transmit_task_cb(void *cls)
Task that triggers a NSE P2P transmission.
static int update_flood_times(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Update transmissions for the given peer for the current round based on updated proximity information.
static double get_matching_bits_delay(uint32_t matching_bits)
How long should we delay a message to go the given number of matching bits?
static unsigned int estimate_index
Index of most recent estimate.
static struct GNUNET_TIME_Relative gnunet_nse_interval
Interval for sending network size estimation flood requests.
#define NSE_PRIORITY
Message priority to use.
#define HISTORY_SIZE
Over how many values do we calculate the weighted average?
static enum GNUNET_GenericReturnValue check_proof_of_work(const struct GNUNET_CRYPTO_EddsaPublicKey *pkey, uint64_t val)
Check whether the given public key and integer are a valid proof of work.
static struct GNUNET_STATISTICS_Handle * stats
Handle to the statistics service.
static void setup_estimate_message(struct GNUNET_NSE_ClientMessage *em)
Initialize a message to clients with the current network size estimate.
static struct GNUNET_SCHEDULER_Task * proof_task
Task scheduled to compute our proof.
static struct GNUNET_TIME_Absolute current_timestamp
The current major time.
static void sign_message_before_send_cb(void *cls, const struct GNUNET_PeerIdentity *pid, const struct GNUNET_CRYPTO_EddsaSignature *sig)
static struct GNUNET_NotificationContext * nc
Notification context, simplifies client broadcasts.
static void find_proof(void *cls)
Find our proof of work.
static unsigned int estimate_count
Number of valid entries in the history.
static void update_network_size_estimate()
We've sent on our flood message or one that we received which was validated and closer than ours.
static struct GNUNET_TIME_Relative proof_find_delay
Interval between proof find runs.
static int verify_message_crypto(const struct GNUNET_NSE_FloodMessage *incoming_flood)
An incoming flood message has been received which claims to have more bits matching than any we know ...
static uint64_t my_proof
Proof of work for this peer.
static struct GNUNET_CORE_Handle * core_api
Handle to the core service.
static unsigned long long nse_work_required
Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
static void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service)
Handle network size estimate clients.
static GNUNET_NETWORK_STRUCT_END const struct GNUNET_CONFIGURATION_Handle * cfg
Handle to our current configuration.
static void setup_flood_message(unsigned int slot, struct GNUNET_TIME_Absolute ts)
Setup a flood message in our history array at the given slot offset for the given timestamp.
static int schedule_current_round(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Schedule transmission for the given peer for the current round based on what we know about the desire...
static uint32_t hop_count_max
Current hop counter estimate (estimate for network diameter).
static struct GNUNET_TIME_Relative get_transmit_delay(int round_offset)
Get the transmission delay that should be applied for a particular round.
#define ROUND_SIZE
static void handle_start(void *cls, const struct GNUNET_MessageHeader *message)
Handler for START message from client, triggers an immediate current network estimate notification.
static struct GNUNET_NSE_FloodMessage next_message
Message for the next round, if we got any.
static struct GNUNET_SCHEDULER_Task * flood_task
Task scheduled to update our flood message for the next round.
static struct GNUNET_TIME_Absolute next_timestamp
The next major time.
static struct GNUNET_CRYPTO_PowSalt salt
Salt for PoW calculations.
static struct GNUNET_TIME_Relative get_delay_randomization(uint32_t matching_bits)
What delay randomization should we apply for a given number of matching bits?
static double current_size_estimate
The current network size estimate.
static void * client_connect_cb(void *cls, struct GNUNET_SERVICE_Client *c, struct GNUNET_MQ_Handle *mq)
Callback called when a client connects to the service.
static void client_disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
Callback called when a client disconnected from the service.
static void write_proof(void)
Write our current proof to disk.
static void handle_p2p_estimate(void *cls, const struct GNUNET_NSE_FloodMessage *incoming_flood)
Core handler for size estimate flooding messages.
static void handle_core_disconnect(void *cls, const struct GNUNET_PeerIdentity *peer, void *internal_cls)
Method called whenever a peer disconnects.
static void * handle_core_connect(void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq, enum GNUNET_CORE_PeerClass class)
Method called whenever a peer connects.
static struct GNUNET_PILS_Handle * pils
Handle to the PILS service.
static uint32_t get_matching_bits(struct GNUNET_TIME_Absolute timestamp, const struct GNUNET_PeerIdentity *id)
Get the number of matching bits that the given timestamp has to the given peer ID.
static void pils_id_change_cb(void *cls, const struct GNUNET_HELLO_Parser *parser, const struct GNUNET_HashCode *addr_hash)
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
struct GNUNET_PILS_Handle * GNUNET_PILS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_PILS_PidChangeCallback pid_change_cb, void *cls)
Connect to the PILS service.
Definition pils_api.c:465
void GNUNET_PILS_disconnect(struct GNUNET_PILS_Handle *handle)
Disconnect from the PILS service.
Definition pils_api.c:488
struct GNUNET_PILS_Operation * GNUNET_PILS_sign_by_peer_identity(struct GNUNET_PILS_Handle *handle, const struct GNUNET_CRYPTO_SignaturePurpose *purpose, GNUNET_PILS_SignResultCallback cb, void *cb_cls)
Sign data with the peer id.
Definition pils_api.c:528
const struct GNUNET_PeerIdentity * GNUNET_PILS_get_identity(const struct GNUNET_PILS_Handle *handle)
Return the current peer identity of a given handle.
Definition pils_api.c:727
struct GNUNET_PQ_ResultSpec __attribute__
#define GNUNET_SIGNATURE_PURPOSE_NSE_SEND
Signature of a network size estimate message.
enum GNUNET_GenericReturnValue GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h, char **emsg)
Close an IO handle.
Definition bio.c:556
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_file(const char *fn)
Open a file for writing.
Definition bio.c:508
enum GNUNET_GenericReturnValue GNUNET_BIO_write_int64(struct GNUNET_BIO_WriteHandle *h, const char *what, int64_t i)
Write an (u)int64_t.
Definition bio.c:865
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
struct GNUNET_CORE_Handle * GNUNET_CORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls, GNUNET_CORE_StartupCallback init, GNUNET_CORE_ConnectEventHandler connects, GNUNET_CORE_DisconnectEventHandler disconnects, const struct GNUNET_MQ_MessageHandler *handlers, const struct GNUNET_CORE_ServiceInfo *service_info)
Connect to the core service.
Definition core_api.c:698
GNUNET_CORE_PeerClass
The peer class gives a hint about the capabilities of a peer.
void GNUNET_CORE_disconnect(struct GNUNET_CORE_Handle *handle)
Disconnect from the core service.
Definition core_api.c:744
@ GNUNET_CORE_SERVICE_NSE
Identifier for nse (network size estimation) service.
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Generate a random unsigned 64-bit value.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_SignaturePurpose *validate, const struct GNUNET_CRYPTO_EddsaSignature *sig, const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Verify EdDSA signature.
Definition crypto_ecc.c:708
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition disk.c:557
enum GNUNET_GenericReturnValue GNUNET_DISK_fn_write(const char *fn, const void *buf, size_t buf_size, enum GNUNET_DISK_AccessPermissions mode)
Write a buffer to a file atomically.
Definition disk.c:792
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -rf).
Definition disk.c:1140
ssize_t GNUNET_DISK_fn_read(const char *fn, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition disk.c:720
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition crypto_hash.c:41
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
unsigned int GNUNET_CRYPTO_hash_count_leading_zeros(const struct GNUNET_HashCode *h)
Count the number of leading 0 bits in h.
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_log(kind,...)
#define GNUNET_MAX(a, b)
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
void GNUNET_CRYPTO_pow_hash(const struct GNUNET_CRYPTO_PowSalt *salt, const void *buf, size_t buf_len, struct GNUNET_HashCode *result)
Calculate the 'proof-of-work' hash (an expensive hash).
Definition crypto_pow.c:42
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;.
double GNUNET_hton_double(double d)
Convert double to network byte order.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to 'struct's.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#define GNUNET_PACKED
gcc-ism to get packed structs.
@ GNUNET_SCHEDULER_PRIORITY_IDLE
Run when otherwise idle.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
void GNUNET_log_config_invalid(enum GNUNET_ErrorType kind, const char *section, const char *option, const char *required)
Log error message about invalid configuration option value.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_notification_context_destroy(struct GNUNET_NotificationContext *nc)
Destroy the context, force disconnect for all subscribers.
Definition nc.c:138
struct GNUNET_MQ_Envelope * GNUNET_MQ_msg_copy(const struct GNUNET_MessageHeader *hdr)
Create a new envelope by copying an existing message.
Definition mq.c:550
void GNUNET_MQ_set_options(struct GNUNET_MQ_Handle *mq, enum GNUNET_MQ_PriorityPreferences pp)
Set application-specific options for this queue.
Definition mq.c:888
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition mq.c:305
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
struct GNUNET_NotificationContext * GNUNET_notification_context_create(unsigned int queue_length)
Create a new notification context.
Definition nc.c:122
void GNUNET_notification_context_broadcast(struct GNUNET_NotificationContext *nc, const struct GNUNET_MessageHeader *msg, int can_drop)
Send a message to all subscribers of this context.
Definition nc.c:190
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
void GNUNET_notification_context_add(struct GNUNET_NotificationContext *nc, struct GNUNET_MQ_Handle *mq)
Add a subscriber to the notification context.
Definition nc.c:161
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
#define GNUNET_MESSAGE_TYPE_NSE_START
client->service message indicating start
#define GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD
P2P message sent from nearest peer.
#define GNUNET_MESSAGE_TYPE_NSE_ESTIMATE
service->client message indicating
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition scheduler.c:572
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_at(struct GNUNET_TIME_Absolute at, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run at the specified time.
Definition scheduler.c:1260
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition scheduler.c:1345
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:986
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition scheduler.c:1310
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition scheduler.c:1283
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed_with_priority(struct GNUNET_TIME_Relative delay, enum GNUNET_SCHEDULER_Priority priority, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition scheduler.c:1213
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_with_priority(enum GNUNET_SCHEDULER_Priority prio, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified priority.
Definition scheduler.c:1237
#define GNUNET_SERVICE_MAIN(pd, service_name, service_options, init_cb, connect_cb, disconnect_cb, cls,...)
Creates the "main" function for a GNUnet service.
struct GNUNET_MQ_Handle * GNUNET_SERVICE_client_get_mq(struct GNUNET_SERVICE_Client *c)
Obtain the message queue of c.
Definition service.c:2545
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition service.c:2434
@ GNUNET_SERVICE_OPTION_NONE
Use defaults.
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
void GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle, const char *name, uint64_t value, int make_persistent)
Set statistic value for the peer.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_TIME_UNIT_SECONDS
One second.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition time.c:406
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition strings.c:604
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition time.c:111
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition time.c:737
#define GNUNET_TIME_UNIT_ZERO
Relative time zero.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition time.c:452
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition time.c:636
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition strings.c:665
static unsigned int size
Size of the "table".
Definition peer.c:68
#define _(String)
GNU gettext support macro.
Definition platform.h:179
static struct GNUNET_MQ_Handle * mq
Our connection to the resolver service, created on-demand, but then persists until error or shutdown.
Internal representation of the hash map.
Context for the core service connection.
Definition core_api.c:78
Gnunet service info - identifying compatibility with a range of version of a service communicating ov...
enum GNUNET_CORE_Service service
Identifier of the service on top of CORE.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
an ECC signature using EdDSA.
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!...
Context for parsing HELLOs.
Definition hello-uri.c:233
A 512-bit hashcode.
Handle to a message queue.
Definition mq.c:87
Message handler for a specific message type.
Header for all communications.
Network size estimate sent from the service to clients.
Definition nse.h:43
Network size estimate reply; sent when "this" peer's timer has run out before receiving a valid reply...
uint32_t hop_count
Number of hops this message has taken so far.
uint32_t matching_bits
Number of matching bits between the hash of timestamp and the initiator's public key.
struct GNUNET_TIME_AbsoluteNBO timestamp
The current timestamp value (which all peers should agree on).
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD.
struct GNUNET_CRYPTO_SignaturePurpose purpose
Purpose.
struct GNUNET_PeerIdentity origin
Public key of the originator.
uint64_t proof_of_work
Proof of work, causing leading zeros when hashed with pkey.
struct GNUNET_CRYPTO_EddsaSignature signature
Signature (over range specified in purpose).
The notification context is the key datastructure for a convenience API used for transmission of noti...
Definition nc.c:77
A handle for the PILS service.
Definition pils_api.c:82
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key
Entry in list of pending tasks.
Definition scheduler.c:141
Handle to a client that is connected to a service.
Definition service.c:249
Handle to a service.
Definition service.c:116
Handle for the service.
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
Per-peer information.
struct GNUNET_PILS_Operation * pils_op
PILS Operation.
struct GNUNET_MQ_Handle * mq
Core handle for sending messages to this peer.
struct GNUNET_SCHEDULER_Task * transmit_task
Task scheduled to send message to this peer.
const struct GNUNET_PeerIdentity * id
What is the identity of the peer?
int previous_round
Did we receive or send a message about the previous round to this peer yet? GNUNET_YES if the previou...
uint32_t next_index_to_send
Message to be sent.

◆ WEST

#define WEST   1

◆ ROUND_SIZE

#define ROUND_SIZE   10

Function Documentation

◆ setup_estimate_message()

static void setup_estimate_message ( struct GNUNET_NSE_ClientMessage em)
static

Initialize a message to clients with the current network size estimate.

Parameters
emmessage to fill in

Definition at line 333 of file gnunet-service-nse.c.

334{
335 double mean;
336 double sum;
337 double std_dev;
338 double variance;
339 double val;
340 double nsize;
341
342#define WEST 1
343 /* Weighted incremental algorithm for stddev according to West (1979) */
344#if WEST
345 double sumweight;
346 double weight;
347 double q;
348 double r;
349 double temp;
350
351 mean = 0.0;
352 sum = 0.0;
353 sumweight = 0.0;
354 variance = 0.0;
355 for (unsigned int i = 0; i < estimate_count; i++)
356 {
357 unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
358
359 val = htonl (size_estimate_messages[j].matching_bits);
360 weight = estimate_count + 1 - i;
361
362 temp = weight + sumweight;
363 q = val - mean;
364 r = q * weight / temp;
365 mean += r;
366 sum += sumweight * q * r;
367 sumweight = temp;
368 }
369 if (estimate_count > 0)
370 variance = (sum / sumweight) * estimate_count / (estimate_count - 1.0);
371#else
372 /* trivial version for debugging */
373 double vsq;
374
375 /* non-weighted trivial version */
376 sum = 0.0;
377 vsq = 0.0;
378 variance = 0.0;
379 mean = 0.0;
380
381 for (unsigned int i = 0; i < estimate_count; i++)
382 {
383 unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
384
385 val = htonl (size_estimate_messages[j].matching_bits);
386 sum += val;
387 vsq += val * val;
388 }
389 if (0 != estimate_count)
390 {
391 mean = sum / estimate_count;
392 variance = (vsq - mean * sum)
393 / (estimate_count - 1.0); // terrible for numerical stability...
394 }
395#endif
396 if (variance >= 0)
397 std_dev = sqrt (variance);
398 else
399 std_dev = variance; /* return NaN (due to estimate_count == 0 causing 0.0/0.0) */
400 current_std_dev = std_dev;
402
403 em->header.size = htons (sizeof(struct GNUNET_NSE_ClientMessage));
405 em->reserved = htonl (0);
407 {
408 double se = mean - 0.332747;
409 unsigned int j = GNUNET_CONTAINER_multipeermap_size (peers);
410 if (0 == j)
411 j = 1; /* Avoid log2(0); can only happen if CORE didn't report
412 connection to self yet */
413 nsize = log2 (j);
414 em->size_estimate = GNUNET_hton_double (GNUNET_MAX (se, nsize));
415 em->std_deviation = GNUNET_hton_double (std_dev);
417 "# nodes in the network (estimate)",
418 (uint64_t) pow (2, GNUNET_MAX (se, nsize)),
419 GNUNET_NO);
420 }
421}
uint32_t reserved
For alignment.
Definition nse.h:52
double size_estimate
The current estimated network size.
Definition nse.h:62
struct GNUNET_TIME_AbsoluteNBO timestamp
Timestamp at which the server received the message.
Definition nse.h:57
double std_deviation
The standard deviation (rounded down to the nearest integer) of size estimations.
Definition nse.h:69
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_NSE_ESTIMATE.
Definition nse.h:47

References current_size_estimate, current_std_dev, estimate_count, estimate_index, GNUNET_CONTAINER_multipeermap_size(), GNUNET_hton_double(), GNUNET_MAX, GNUNET_MESSAGE_TYPE_NSE_ESTIMATE, GNUNET_NO, GNUNET_STATISTICS_set(), GNUNET_TIME_absolute_get(), GNUNET_TIME_absolute_hton(), GNUNET_NSE_ClientMessage::header, HISTORY_SIZE, matching_bits, peers, q, GNUNET_NSE_ClientMessage::reserved, GNUNET_MessageHeader::size, GNUNET_NSE_ClientMessage::size_estimate, size_estimate_messages, stats, GNUNET_NSE_ClientMessage::std_deviation, GNUNET_NSE_ClientMessage::timestamp, and GNUNET_MessageHeader::type.

Referenced by handle_start(), and update_network_size_estimate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_start()

static void handle_start ( void *  cls,
const struct GNUNET_MessageHeader message 
)
static

Handler for START message from client, triggers an immediate current network estimate notification.

Also, we remember the client for updates upon future estimate measurements.

Parameters
clsclient who sent the message
messagethe message received

Definition at line 434 of file gnunet-service-nse.c.

435{
436 struct GNUNET_SERVICE_Client *client = cls;
437 struct GNUNET_MQ_Handle *mq;
438 struct GNUNET_NSE_ClientMessage em;
439 struct GNUNET_MQ_Envelope *env;
440
441 (void) message;
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received START message from client\n");
446 env = GNUNET_MQ_msg_copy (&em.header);
449}

References env, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_MQ_msg_copy(), GNUNET_MQ_send(), GNUNET_notification_context_add(), GNUNET_SERVICE_client_continue(), GNUNET_SERVICE_client_get_mq(), GNUNET_NSE_ClientMessage::header, mq, nc, and setup_estimate_message().

Here is the call graph for this function:

◆ get_matching_bits_delay()

static double get_matching_bits_delay ( uint32_t  matching_bits)
static

How long should we delay a message to go the given number of matching bits?

Parameters
matching_bitsnumber of matching bits to consider

Definition at line 459 of file gnunet-service-nse.c.

460{
461 /* Calculated as: S + f/2 - (f / pi) * (atan(x - p')) */
462 // S is next_timestamp (ignored in return value)
463 // f is frequency (gnunet_nse_interval)
464 // x is matching_bits
465 // p' is current_size_estimate
466 return ((double) gnunet_nse_interval.rel_value_us / (double) 2.0)
469}

References current_size_estimate, gnunet_nse_interval, matching_bits, and GNUNET_TIME_Relative::rel_value_us.

Referenced by get_delay_randomization(), and get_transmit_delay().

Here is the caller graph for this function:

◆ get_delay_randomization()

static struct GNUNET_TIME_Relative get_delay_randomization ( uint32_t  matching_bits)
static

What delay randomization should we apply for a given number of matching bits?

Parameters
matching_bitsnumber of matching bits
Returns
random delay to apply

Definition at line 479 of file gnunet-service-nse.c.

480{
481#if USE_RANDOM_DELAYS
483 uint32_t i;
484 double d;
485
487 i = (uint32_t) (d / (double) (hop_count_max + 1));
488 ret.rel_value_us = i;
490 "Randomizing flood using latencies up to %s\n",
492 ret.rel_value_us =
494 return ret;
495#else
497#endif
498}

References d, get_matching_bits_delay(), GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u32(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_UNIT_ZERO, GNUNET_YES, hop_count_max, matching_bits, and ret.

Referenced by get_transmit_delay().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_matching_bits()

static uint32_t get_matching_bits ( struct GNUNET_TIME_Absolute  timestamp,
const struct GNUNET_PeerIdentity id 
)
static

Get the number of matching bits that the given timestamp has to the given peer ID.

Parameters
timestamptime to generate key
idpeer identity to compare with
Returns
number of matching bits

Definition at line 509 of file gnunet-service-nse.c.

511{
512 struct GNUNET_HashCode timestamp_hash;
513 struct GNUNET_HashCode pid_hash;
514 struct GNUNET_HashCode xor;
515
516 GNUNET_CRYPTO_hash (&timestamp.abs_value_us,
517 sizeof(timestamp.abs_value_us),
518 &timestamp_hash);
520 sizeof(struct GNUNET_PeerIdentity),
521 &pid_hash);
522 GNUNET_CRYPTO_hash_xor (&pid_hash,
523 &timestamp_hash,
524 &xor);
526}

References GNUNET_CRYPTO_hash(), GNUNET_CRYPTO_hash_count_leading_zeros(), GNUNET_CRYPTO_hash_xor(), and timestamp().

Referenced by setup_flood_message(), and update_flood_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_transmit_delay()

static struct GNUNET_TIME_Relative get_transmit_delay ( int  round_offset)
static

Get the transmission delay that should be applied for a particular round.

Parameters
round_offset-1 for the previous round (random delay between 0 and 50ms) 0 for the current round (based on our proximity to time key)
Returns
delay that should be applied

Definition at line 538 of file gnunet-service-nse.c.

539{
541 struct GNUNET_TIME_Absolute tgt;
542 double dist_delay;
543 uint32_t matching_bits;
544
545 switch (round_offset)
546 {
547 case -1:
548 /* previous round is randomized between 0 and 50 ms */
549#if USE_RANDOM_DELAYS
550 ret.rel_value_us =
552#else
554#endif
556 "Transmitting previous round behind schedule in %s\n",
558 return ret;
559
560 case 0:
561 /* current round is based on best-known matching_bits */
566 ret.rel_value_us = (uint64_t) dist_delay;
568 "For round %s, delay for %u matching bits is %s\n",
570 (unsigned int) matching_bits,
572 /* now consider round start time and add delay to it */
575 }
576 GNUNET_break (0);
578}

References current_timestamp, estimate_index, get_delay_randomization(), get_matching_bits_delay(), GNUNET_break, GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_STRINGS_absolute_time_to_string(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_absolute_add(), GNUNET_TIME_absolute_get_remaining(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_UNIT_ZERO, GNUNET_YES, matching_bits, GNUNET_TIME_Relative::rel_value_us, ret, and size_estimate_messages.

Referenced by handle_core_connect(), schedule_current_round(), transmit_task_cb(), and update_flood_times().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sign_message_before_send_cb()

static void sign_message_before_send_cb ( void *  cls,
const struct GNUNET_PeerIdentity pid,
const struct GNUNET_CRYPTO_EddsaSignature sig 
)
static

Definition at line 582 of file gnunet-service-nse.c.

585{
586 struct NSEPeerEntry *pe = cls;
587 struct GNUNET_MQ_Envelope *env;
588 struct GNUNET_NSE_FloodMessage *fm;
589
590 pe->pils_op = NULL;
592 fm->signature = *sig;
594 GNUNET_MQ_send (pe->mq, env);
595}

References env, GNUNET_MQ_msg_copy(), GNUNET_MQ_send(), GNUNET_NSE_FloodMessage::header, NSEPeerEntry::mq, NSEPeerEntry::next_index_to_send, NSEPeerEntry::pils_op, GNUNET_NSE_FloodMessage::signature, and size_estimate_messages.

Referenced by transmit_task_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ transmit_task_cb()

static void transmit_task_cb ( void *  cls)
static

Task that triggers a NSE P2P transmission.

Parameters
clsthe struct NSEPeerEntry *

Definition at line 604 of file gnunet-service-nse.c.

605{
606 struct NSEPeerEntry *peer_entry = cls;
607 unsigned int idx;
608
609 peer_entry->transmit_task = NULL;
610 idx = estimate_index;
611 if (GNUNET_NO == peer_entry->previous_round)
612 {
613 idx = (idx + HISTORY_SIZE - 1) % HISTORY_SIZE;
614 peer_entry->previous_round = GNUNET_YES;
615 peer_entry->transmit_task =
618 peer_entry);
619 }
620 if ((0 == ntohl (size_estimate_messages[idx].hop_count)) &&
621 (NULL != proof_task))
622 {
624 "# flood messages not generated (no proof yet)",
625 1,
626 GNUNET_NO);
627 return;
628 }
629 if (0 == ntohs (size_estimate_messages[idx].header.size))
630 {
632 "# flood messages not generated (lack of history)",
633 1,
634 GNUNET_NO);
635 return;
636 }
638 "In round %s, sending to `%s' estimate with %u bits\n",
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, "# flood messages started", 1, GNUNET_NO);
647 "# flood messages transmitted",
648 1,
649 GNUNET_NO);
650#if ENABLE_NSE_HISTOGRAM
651 peer_entry->transmitted_messages++;
652 peer_entry->last_transmitted_size =
654#endif
655 peer_entry->next_index_to_send = idx;
657 pils,
658 &size_estimate_messages[idx].purpose,
660 peer_entry);
661}

References estimate_index, get_transmit_delay(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s(), GNUNET_log, GNUNET_NO, GNUNET_PILS_sign_by_peer_identity(), GNUNET_SCHEDULER_add_delayed(), GNUNET_STATISTICS_update(), GNUNET_STRINGS_absolute_time_to_string(), GNUNET_TIME_absolute_ntoh(), GNUNET_YES, HISTORY_SIZE, NSEPeerEntry::id, matching_bits, NSEPeerEntry::next_index_to_send, pils, NSEPeerEntry::pils_op, NSEPeerEntry::previous_round, proof_task, sign_message_before_send_cb(), size_estimate_messages, stats, timestamp(), NSEPeerEntry::transmit_task, and transmit_task_cb().

Referenced by handle_core_connect(), handle_p2p_estimate(), schedule_current_round(), transmit_task_cb(), and update_flood_times().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_network_size_estimate()

static void update_network_size_estimate ( )
static

We've sent on our flood message or one that we received which was validated and closer than ours.

Update the global list of recent messages and the average. Also re-broadcast the message to any clients.

Definition at line 671 of file gnunet-service-nse.c.

672{
673 struct GNUNET_NSE_ClientMessage em;
674
677}

References GNUNET_notification_context_broadcast(), GNUNET_YES, GNUNET_NSE_ClientMessage::header, nc, and setup_estimate_message().

Referenced by handle_p2p_estimate(), and load_underlay().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setup_flood_message()

static void setup_flood_message ( unsigned int  slot,
struct GNUNET_TIME_Absolute  ts 
)
static

Setup a flood message in our history array at the given slot offset for the given timestamp.

Parameters
slotindex to use
tstimestamp to use

Definition at line 688 of file gnunet-service-nse.c.

689{
690 const struct GNUNET_PeerIdentity *my_identity;
691 struct GNUNET_NSE_FloodMessage *fm;
692 uint32_t matching_bits;
693
696
698 fm = &size_estimate_messages[slot];
699 fm->header.size = htons (sizeof(struct GNUNET_NSE_FloodMessage));
701 fm->hop_count = htonl (0);
703 fm->purpose.size =
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);
709 fm->origin = *my_identity;
711 memset (&fm->signature, 0, sizeof(fm->signature));
712}

References get_matching_bits(), GNUNET_assert, GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD, GNUNET_PILS_get_identity(), GNUNET_SIGNATURE_PURPOSE_NSE_SEND, GNUNET_TIME_absolute_hton(), GNUNET_NSE_FloodMessage::header, GNUNET_NSE_FloodMessage::hop_count, matching_bits, GNUNET_NSE_FloodMessage::matching_bits, my_identity, my_proof, GNUNET_NSE_FloodMessage::origin, pils, GNUNET_NSE_FloodMessage::proof_of_work, GNUNET_CRYPTO_SignaturePurpose::purpose, GNUNET_NSE_FloodMessage::purpose, GNUNET_NSE_FloodMessage::signature, GNUNET_MessageHeader::size, GNUNET_CRYPTO_SignaturePurpose::size, size_estimate_messages, GNUNET_NSE_FloodMessage::timestamp, and GNUNET_MessageHeader::type.

Referenced by find_proof(), pils_id_change_cb(), and update_flood_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ schedule_current_round()

static int schedule_current_round ( void *  cls,
const struct GNUNET_PeerIdentity key,
void *  value 
)
static

Schedule transmission for the given peer for the current round based on what we know about the desired delay.

Parameters
clsunused
keyhash of peer identity
valuethe struct NSEPeerEntry
Returns
GNUNET_OK (continue to iterate)

Definition at line 725 of file gnunet-service-nse.c.

728{
729 struct NSEPeerEntry *peer_entry = value;
730 struct GNUNET_TIME_Relative delay;
731
732 (void) cls;
733 (void) key;
734 if (NULL != peer_entry->transmit_task)
735 {
737 peer_entry->previous_round = GNUNET_NO;
738 }
739#if ENABLE_NSE_HISTOGRAM
740 if (peer_entry->received_messages > 1)
742 "# extra messages",
743 peer_entry->received_messages - 1,
744 GNUNET_NO);
745 peer_entry->transmitted_messages = 0;
746 peer_entry->last_transmitted_size = 0;
747 peer_entry->received_messages = 0;
748#endif
749 delay =
750 get_transmit_delay ((GNUNET_NO == peer_entry->previous_round) ? -1 : 0);
751 peer_entry->transmit_task =
753 return GNUNET_OK;
754}

References get_transmit_delay(), GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_cancel(), GNUNET_STATISTICS_update(), key, NSEPeerEntry::previous_round, stats, NSEPeerEntry::transmit_task, transmit_task_cb(), and value.

Referenced by update_flood_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_flood_message()

static void update_flood_message ( void *  cls)
static

Update our flood message to be sent (and our timestamps).

Parameters
clsunused

Definition at line 763 of file gnunet-service-nse.c.

764{
765 const struct GNUNET_PeerIdentity *my_identity;
766 struct GNUNET_TIME_Relative offset;
767
768 (void) cls;
769 flood_task = NULL;
771 if (! my_identity)
772 return;
773
775 if (0 != offset.rel_value_us)
776 {
777 /* somehow run early, delay more */
778 flood_task =
780 return;
781 }
792 {
793 /* we received a message for this round way early, use it! */
796 htonl (1 + ntohl (next_message.hop_count));
797 }
798 else
800 next_message.matching_bits = htonl (0); /* reset for 'next' round */
801 hop_count_max = 0;
802 for (unsigned int i = 0; i < HISTORY_SIZE; i++)
804 GNUNET_MAX (ntohl (size_estimate_messages[i].hop_count), hop_count_max);
806 flood_task =
808}

References GNUNET_TIME_Absolute::abs_value_us, current_timestamp, estimate_count, estimate_index, flood_task, get_matching_bits(), GNUNET_CONTAINER_multipeermap_iterate(), GNUNET_MAX, gnunet_nse_interval, GNUNET_PILS_get_identity(), GNUNET_SCHEDULER_add_at(), GNUNET_SCHEDULER_add_delayed(), GNUNET_TIME_absolute_add(), GNUNET_TIME_absolute_get_remaining(), GNUNET_TIME_absolute_ntoh(), HISTORY_SIZE, GNUNET_NSE_FloodMessage::hop_count, hop_count_max, GNUNET_NSE_FloodMessage::matching_bits, my_identity, next_message, next_timestamp, peers, pils, GNUNET_TIME_Relative::rel_value_us, schedule_current_round(), setup_flood_message(), size_estimate_messages, GNUNET_NSE_FloodMessage::timestamp, and update_flood_message().

Referenced by pils_id_change_cb(), and update_flood_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_proof_of_work()

static enum GNUNET_GenericReturnValue check_proof_of_work ( const struct GNUNET_CRYPTO_EddsaPublicKey pkey,
uint64_t  val 
)
static

Check whether the given public key and integer are a valid proof of work.

Parameters
pkeythe public key
valthe integer
Returns
GNUNET_YES if valid, GNUNET_NO if not

Definition at line 820 of file gnunet-service-nse.c.

822{
823 char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
824 + sizeof(val)] GNUNET_ALIGN;
825 struct GNUNET_HashCode result;
826
827 GNUNET_memcpy (buf, &val, sizeof(val));
828 GNUNET_memcpy (&buf[sizeof(val)],
829 pkey,
830 sizeof(struct GNUNET_CRYPTO_EddsaPublicKey));
832 buf,
833 sizeof(buf),
834 &result);
837 ? GNUNET_YES
838 : GNUNET_NO;
839}

References GNUNET_ALIGN, GNUNET_CRYPTO_hash_count_leading_zeros(), GNUNET_CRYPTO_pow_hash(), GNUNET_memcpy, GNUNET_NO, GNUNET_YES, nse_work_required, pkey, result, and salt.

Referenced by pils_id_change_cb(), and verify_message_crypto().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_proof()

static void write_proof ( void  )
static

Write our current proof to disk.

Definition at line 846 of file gnunet-service-nse.c.

847{
848 char *proof;
849
850 if (GNUNET_OK !=
852 "NSE",
853 "PROOFFILE",
854 &proof))
855 return;
857 if (GNUNET_OK !=
859 &my_proof,
860 sizeof(my_proof),
864 "write",
865 proof);
867}

References cfg, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_DISK_directory_remove(), GNUNET_DISK_fn_write(), GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_strerror_file, GNUNET_OK, my_proof, and proof.

Referenced by find_proof(), and shutdown_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_proof()

static void find_proof ( void *  cls)
static

Find our proof of work.

Parameters
clsclosure (unused)

Definition at line 876 of file gnunet-service-nse.c.

877{
878#define ROUND_SIZE 10
879 uint64_t counter;
880 char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
881 + sizeof(uint64_t)] GNUNET_ALIGN;
882 struct GNUNET_HashCode result;
883 const struct GNUNET_PeerIdentity *my_identity;
884 unsigned int i;
885
886 (void) cls;
887 proof_task = NULL;
890 GNUNET_memcpy (&buf[sizeof(uint64_t)],
892 sizeof(struct GNUNET_PeerIdentity));
893 i = 0;
894 counter = my_proof;
895 while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
896 {
897 GNUNET_memcpy (buf, &counter, sizeof(uint64_t));
899 buf,
900 sizeof(buf),
901 &result);
902 if (nse_work_required <=
904 {
905 my_proof = counter;
907 "Proof of work found: %llu!\n",
908 (unsigned long long) GNUNET_ntohll (counter));
909 write_proof ();
911 return;
912 }
913 counter++;
914 i++;
915 }
916 if (my_proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
917 {
919 "Testing proofs currently at %llu\n",
920 (unsigned long long) counter);
921 /* remember progress every 100 rounds */
922 my_proof = counter;
923 write_proof ();
924 }
925 else
926 {
927 my_proof = counter;
928 }
929 proof_task =
932 &find_proof,
933 NULL);
934}

References current_timestamp, estimate_index, find_proof(), GNUNET_ALIGN, GNUNET_assert, GNUNET_CRYPTO_hash_count_leading_zeros(), GNUNET_CRYPTO_pow_hash(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_memcpy, GNUNET_ntohll(), GNUNET_PILS_get_identity(), GNUNET_SCHEDULER_add_delayed_with_priority(), GNUNET_SCHEDULER_PRIORITY_IDLE, my_identity, my_proof, nse_work_required, pils, proof_find_delay, proof_task, result, ROUND_SIZE, salt, setup_flood_message(), and write_proof().

Referenced by find_proof(), and pils_id_change_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ verify_message_crypto()

static int verify_message_crypto ( const struct GNUNET_NSE_FloodMessage incoming_flood)
static

An incoming flood message has been received which claims to have more bits matching than any we know in this time period.

Verify the signature and/or proof of work.

Parameters
incoming_floodthe message to verify
Returns
GNUNET_YES if the message is verified GNUNET_NO if the key/signature don't verify

Definition at line 947 of file gnunet-service-nse.c.

948{
949 if (GNUNET_YES != check_proof_of_work (&incoming_flood->origin.public_key,
950 incoming_flood->proof_of_work))
951 {
953 "Proof of work invalid: %llu!\n",
954 (unsigned long long) GNUNET_ntohll (
955 incoming_flood->proof_of_work));
956 GNUNET_break_op (0);
957 return GNUNET_NO;
958 }
959 if ((nse_work_required > 0) &&
960 (GNUNET_OK !=
962 &incoming_flood->purpose,
963 &incoming_flood->signature,
964 &incoming_flood->origin.public_key)))
965 {
966 GNUNET_break_op (0);
967 return GNUNET_NO;
968 }
969 return GNUNET_YES;
970}

References check_proof_of_work(), GNUNET_break_op, GNUNET_CRYPTO_eddsa_verify_(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_NO, GNUNET_ntohll(), GNUNET_OK, GNUNET_SIGNATURE_PURPOSE_NSE_SEND, GNUNET_YES, nse_work_required, GNUNET_NSE_FloodMessage::origin, GNUNET_NSE_FloodMessage::proof_of_work, GNUNET_PeerIdentity::public_key, GNUNET_NSE_FloodMessage::purpose, and GNUNET_NSE_FloodMessage::signature.

Referenced by handle_p2p_estimate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_flood_times()

static int update_flood_times ( void *  cls,
const struct GNUNET_PeerIdentity key,
void *  value 
)
static

Update transmissions for the given peer for the current round based on updated proximity information.

Parameters
clspeer entry to exclude from updates
keyhash of peer identity
valuethe struct NSEPeerEntry * of a peer to transmit to
Returns
GNUNET_OK (continue to iterate)

Definition at line 983 of file gnunet-service-nse.c.

986{
987 struct NSEPeerEntry *exclude = cls;
988 struct NSEPeerEntry *peer_entry = value;
989 struct GNUNET_TIME_Relative delay;
990
991 (void) key;
992 if (peer_entry == exclude)
993 return GNUNET_OK; /* trigger of the update */
994 if (GNUNET_NO == peer_entry->previous_round)
995 {
996 /* still stuck in previous round, no point to update, check that
997 * we are active here though... */
998 if (NULL == peer_entry->transmit_task)
999 {
1000 GNUNET_break (0);
1001 }
1002 return GNUNET_OK;
1003 }
1004 if (NULL != peer_entry->transmit_task)
1005 {
1007 peer_entry->transmit_task = NULL;
1008 }
1009 delay = get_transmit_delay (0);
1010 peer_entry->transmit_task =
1011 GNUNET_SCHEDULER_add_delayed (delay, &transmit_task_cb, peer_entry);
1012 return GNUNET_OK;
1013}

References get_transmit_delay(), GNUNET_break, GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_cancel(), key, NSEPeerEntry::previous_round, NSEPeerEntry::transmit_task, transmit_task_cb(), and value.

Referenced by handle_p2p_estimate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_p2p_estimate()

static void handle_p2p_estimate ( void *  cls,
const struct GNUNET_NSE_FloodMessage incoming_flood 
)
static

Core handler for size estimate flooding messages.

Parameters
clspeer this message is from
incoming_floodreceived message

Definition at line 1023 of file gnunet-service-nse.c.

1025{
1026 const struct GNUNET_PeerIdentity *my_identity;
1027 struct NSEPeerEntry *peer_entry = cls;
1028 struct GNUNET_TIME_Absolute ts;
1029 uint32_t matching_bits;
1030 unsigned int idx;
1031
1033 if (! my_identity)
1034 return;
1035
1036#if ENABLE_NSE_HISTOGRAM
1037 {
1038 uint64_t t;
1039
1041 if (NULL != lh)
1042 GNUNET_TESTBED_LOGGER_write (lh, &t, sizeof(uint64_t));
1043 if (NULL != histogram)
1044 GNUNET_BIO_write_int64 (histogram, "histogram-time", t);
1045 }
1046#endif
1047 GNUNET_STATISTICS_update (stats, "# flood messages received", 1, GNUNET_NO);
1048 matching_bits = ntohl (incoming_flood->matching_bits);
1049#if DEBUG_NSE
1050 {
1051 char origin[5];
1052 char pred[5];
1053 struct GNUNET_PeerIdentity os;
1054
1056 sizeof(origin),
1057 "%s",
1058 GNUNET_i2s (&incoming_flood->origin));
1059 GNUNET_snprintf (pred, sizeof(pred), "%s", GNUNET_i2s (peer_entry->id));
1061 "Flood at %s from `%s' via `%s' at `%s' with bits %u\n",
1063 GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp)),
1064 origin,
1065 pred,
1067 (unsigned int) matching_bits);
1068 }
1069#endif
1070
1071#if ENABLE_NSE_HISTOGRAM
1072 peer_entry->received_messages++;
1073 if ((peer_entry->transmitted_messages > 0) &&
1074 (peer_entry->last_transmitted_size >= matching_bits) )
1075 GNUNET_STATISTICS_update (stats, "# cross messages", 1, GNUNET_NO);
1076#endif
1077
1078 ts = GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp);
1079 if (ts.abs_value_us == current_timestamp.abs_value_us)
1080 idx = estimate_index;
1081 else if (ts.abs_value_us ==
1084 else if (ts.abs_value_us == next_timestamp.abs_value_us)
1085 {
1087 return; /* ignore, simply too early/late */
1088 if (GNUNET_YES != verify_message_crypto (incoming_flood))
1089 {
1091 "Peer %s is likely ill-configured!\n",
1092 GNUNET_i2s (peer_entry->id));
1093 GNUNET_break_op (0);
1094 return;
1095 }
1096 next_message = *incoming_flood;
1097 return;
1098 }
1099 else
1100 {
1102 "# flood messages discarded (clock skew too large)",
1103 1,
1104 GNUNET_NO);
1105 return;
1106 }
1107 if (0 == (GNUNET_memcmp (peer_entry->id, my_identity)))
1108 {
1109 /* send to self, update our own estimate IF this also comes from us! */
1110 if (0 == GNUNET_memcmp (&incoming_flood->origin, my_identity))
1112 return;
1113 }
1115 {
1116 /* Cancel transmission in the other direction, as this peer clearly has
1117 up-to-date information already. Even if we didn't talk to this peer in
1118 the previous round, we should no longer send it stale information as it
1119 told us about the current round! */
1120 peer_entry->previous_round = GNUNET_YES;
1121 if (idx != estimate_index)
1122 {
1123 /* do not transmit information for the previous round to this peer
1124 anymore (but allow current round) */
1125 return;
1126 }
1127 /* got up-to-date information for current round, cancel transmission to
1128 * this peer altogether */
1129 if (NULL != peer_entry->transmit_task)
1130 {
1132 peer_entry->transmit_task = NULL;
1133 }
1134 return;
1135 }
1137 {
1138 if ((idx < estimate_index) && (peer_entry->previous_round == GNUNET_YES))
1139 {
1140 peer_entry->previous_round = GNUNET_NO;
1141 }
1142 /* push back our result now, that peer is spreading bad information... */
1143 if (NULL != peer_entry->transmit_task)
1145 peer_entry->transmit_task =
1147 /* Not closer than our most recent message, no need to do work here */
1149 "# flood messages ignored (had closer already)",
1150 1,
1151 GNUNET_NO);
1152 return;
1153 }
1154 if (GNUNET_YES != verify_message_crypto (incoming_flood))
1155 {
1156 GNUNET_break_op (0);
1157 return;
1158 }
1161 /* Cancel transmission in the other direction, as this peer clearly has
1162 * up-to-date information already.
1163 */
1164 peer_entry->previous_round = GNUNET_YES;
1165 if (idx == estimate_index)
1166 {
1167 /* cancel any activity for current round */
1168 if (NULL != peer_entry->transmit_task)
1169 {
1171 peer_entry->transmit_task = NULL;
1172 }
1173 }
1174 size_estimate_messages[idx] = *incoming_flood;
1176 htonl (ntohl (incoming_flood->hop_count) + 1);
1178 GNUNET_MAX (ntohl (incoming_flood->hop_count) + 1, hop_count_max);
1180 "# estimated network diameter",
1182 GNUNET_NO);
1183
1184 /* have a new, better size estimate, inform clients */
1186
1187 /* flood to rest */
1190 peer_entry);
1191}

References GNUNET_TIME_Absolute::abs_value_us, current_timestamp, estimate_index, GNUNET_assert, GNUNET_BIO_write_int64(), GNUNET_break_op, GNUNET_CONTAINER_multipeermap_iterate(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_i2s(), GNUNET_log, GNUNET_MAX, GNUNET_memcmp, GNUNET_NO, gnunet_nse_interval, GNUNET_PILS_get_identity(), GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_cancel(), GNUNET_snprintf(), GNUNET_STATISTICS_set(), GNUNET_STATISTICS_update(), GNUNET_STRINGS_absolute_time_to_string(), GNUNET_TIME_absolute_get(), GNUNET_TIME_absolute_ntoh(), GNUNET_YES, HISTORY_SIZE, GNUNET_NSE_FloodMessage::hop_count, hop_count_max, NSEPeerEntry::id, matching_bits, GNUNET_NSE_FloodMessage::matching_bits, my_identity, next_message, next_timestamp, origin, GNUNET_NSE_FloodMessage::origin, peers, pils, NSEPeerEntry::previous_round, GNUNET_TIME_Relative::rel_value_us, size_estimate_messages, stats, t, GNUNET_NSE_FloodMessage::timestamp, NSEPeerEntry::transmit_task, transmit_task_cb(), update_flood_times(), update_network_size_estimate(), and verify_message_crypto().

Here is the call graph for this function:

◆ handle_core_connect()

static void * handle_core_connect ( void *  cls,
const struct GNUNET_PeerIdentity peer,
struct GNUNET_MQ_Handle mq,
enum GNUNET_CORE_PeerClass  class 
)
static

Method called whenever a peer connects.

Sets up the PeerEntry and schedules the initial size info transmission to this peer.

Parameters
clsclosure
peerpeer identity this notification is about
classclass of the connecting peer

Definition at line 1203 of file gnunet-service-nse.c.

1207{
1208 struct NSEPeerEntry *peer_entry;
1209
1210 (void) cls;
1212 "Peer `%s' connected to us\n",
1213 GNUNET_i2s (peer));
1214 /* set our default transmission options */
1216 /* create our peer entry for this peer */
1217 peer_entry = GNUNET_new (struct NSEPeerEntry);
1218 peer_entry->id = peer;
1219 peer_entry->mq = mq;
1222 peers,
1223 peer_entry->id,
1224 peer_entry,
1226 peer_entry->transmit_task =
1229 peer_entry);
1230 GNUNET_STATISTICS_update (stats, "# peers connected", 1, GNUNET_NO);
1231 return peer_entry;
1232}

References get_transmit_delay(), GNUNET_assert, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CONTAINER_multipeermap_put(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s(), GNUNET_log, GNUNET_MQ_set_options(), GNUNET_new, GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), GNUNET_STATISTICS_update(), NSEPeerEntry::id, mq, NSEPeerEntry::mq, NSE_PRIORITY, peers, stats, NSEPeerEntry::transmit_task, and transmit_task_cb().

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_core_disconnect()

static void handle_core_disconnect ( void *  cls,
const struct GNUNET_PeerIdentity peer,
void *  internal_cls 
)
static

Method called whenever a peer disconnects.

Deletes the PeerEntry and cancels any pending transmission requests to that peer.

Parameters
clsclosure
peerpeer identity this notification is about
internal_clsthe struct NSEPeerEntry for the peer

Definition at line 1244 of file gnunet-service-nse.c.

1247{
1248 struct NSEPeerEntry *pos = internal_cls;
1249
1250 (void) cls;
1252 "Peer `%s' disconnected from us\n",
1253 GNUNET_i2s (peer));
1256 if (NULL != pos->transmit_task)
1257 {
1259 pos->transmit_task = NULL;
1260 }
1261 GNUNET_free (pos);
1262 GNUNET_STATISTICS_update (stats, "# peers connected", -1, GNUNET_NO);
1263}

References GNUNET_assert, GNUNET_CONTAINER_multipeermap_remove(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_i2s(), GNUNET_log, GNUNET_NO, GNUNET_SCHEDULER_cancel(), GNUNET_STATISTICS_update(), GNUNET_YES, peers, stats, and NSEPeerEntry::transmit_task.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdown_task()

static void shutdown_task ( void *  cls)
static

Task run during shutdown.

Parameters
clsunused

Definition at line 1293 of file gnunet-service-nse.c.

1294{
1295 (void) cls;
1296 if (NULL != flood_task)
1297 {
1299 flood_task = NULL;
1300 }
1301 if (NULL != proof_task)
1302 {
1304 proof_task = NULL;
1305 write_proof (); /* remember progress */
1306 }
1307 if (NULL != nc)
1308 {
1310 nc = NULL;
1311 }
1312 if (NULL != core_api)
1313 {
1315 core_api = NULL;
1316 }
1317 if (NULL != stats)
1318 {
1320 stats = NULL;
1321 }
1322 if (NULL != peers)
1323 {
1325 peers = NULL;
1326 }
1327 if (NULL != pils)
1328 {
1330 pils = NULL;
1331 }
1332#if ENABLE_NSE_HISTOGRAM
1333 if (NULL != logger_test)
1334 {
1335 GNUNET_CLIENT_service_test_cancel (logger_test);
1336 logger_test = NULL;
1337 }
1338 if (NULL != lh)
1339 {
1340 GNUNET_TESTBED_LOGGER_flush (lh, &flush_comp_cb, NULL);
1341 }
1342 if (NULL != histogram)
1343 {
1344 GNUNET_BIO_write_close (histogram, NULL);
1345 histogram = NULL;
1346 }
1347#endif
1348}

References core_api, flood_task, GNUNET_BIO_write_close(), GNUNET_CONTAINER_multipeermap_destroy(), GNUNET_CORE_disconnect(), GNUNET_NO, GNUNET_notification_context_destroy(), GNUNET_PILS_disconnect(), GNUNET_SCHEDULER_cancel(), GNUNET_STATISTICS_destroy(), nc, peers, pils, proof_task, stats, and write_proof().

Here is the call graph for this function:

◆ pils_id_change_cb()

static void pils_id_change_cb ( void *  cls,
const struct GNUNET_HELLO_Parser parser,
const struct GNUNET_HashCode addr_hash 
)
static

Definition at line 1352 of file gnunet-service-nse.c.

1355{
1356 const struct GNUNET_PeerIdentity *my_identity;
1357 struct GNUNET_TIME_Absolute now;
1358 struct GNUNET_TIME_Absolute prev_time;
1359
1361
1362 if (NULL != proof_task)
1363 {
1365 proof_task = NULL;
1366 }
1367
1368 if (NULL == my_identity)
1369 {
1371 "Connection to core FAILED!\n");
1373 return;
1374 }
1375 proof_task =
1377 &find_proof,
1378 NULL);
1379 now = GNUNET_TIME_absolute_get ();
1381 (now.abs_value_us / gnunet_nse_interval.rel_value_us)
1386 estimate_count = 0;
1388 {
1389 int idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE;
1390 prev_time.abs_value_us =
1392 setup_flood_message (idx, prev_time);
1395 }
1396 if (NULL != flood_task)
1398 flood_task =
1400}

References GNUNET_TIME_Absolute::abs_value_us, check_proof_of_work(), current_timestamp, estimate_count, estimate_index, find_proof(), flood_task, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, gnunet_nse_interval, GNUNET_PILS_get_identity(), GNUNET_SCHEDULER_add_at(), GNUNET_SCHEDULER_add_with_priority(), GNUNET_SCHEDULER_cancel(), GNUNET_SCHEDULER_PRIORITY_IDLE, GNUNET_SCHEDULER_shutdown(), GNUNET_TIME_absolute_add(), GNUNET_TIME_absolute_get(), GNUNET_YES, HISTORY_SIZE, my_identity, my_proof, next_timestamp, pils, proof_task, GNUNET_PeerIdentity::public_key, GNUNET_TIME_Relative::rel_value_us, setup_flood_message(), and update_flood_message().

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
const struct GNUNET_CONFIGURATION_Handle c,
struct GNUNET_SERVICE_Handle service 
)
static

Handle network size estimate clients.

Parameters
clsclosure
cconfiguration to use
servicethe initialized service

Definition at line 1442 of file gnunet-service-nse.c.

1445{
1446 struct GNUNET_MQ_MessageHandler core_handlers[] =
1447 { GNUNET_MQ_hd_fixed_size (p2p_estimate,
1450 NULL),
1452 char *proof;
1453 const struct GNUNET_CORE_ServiceInfo service_info = {
1455 .version = { 1, 0 },
1456 .version_max = { 1, 0 },
1457 .version_min = { 1, 0 },
1458 };
1459
1460 (void) cls;
1461 (void) service;
1462 cfg = c;
1464 "NSE",
1465 "INTERVAL",
1467 {
1470 return;
1471 }
1473 "NSE",
1474 "WORKDELAY",
1476 {
1479 return;
1480 }
1482 "NSE",
1483 "WORKBITS",
1485 {
1488 return;
1489 }
1490 if (nse_work_required >= sizeof(struct GNUNET_HashCode) * 8)
1491 {
1493 "NSE",
1494 "WORKBITS",
1495 _ ("Value is too large.\n"));
1497 return;
1498 }
1499
1500#if ENABLE_NSE_HISTOGRAM
1501 {
1502 char *histogram_dir;
1503 char *histogram_fn;
1504
1506 "NSE",
1507 "HISTOGRAM_DIR",
1508 &histogram_dir))
1509 {
1511 0 < GNUNET_asprintf (&histogram_fn, "%s/timestamps", histogram_dir));
1512 GNUNET_free (histogram_dir);
1513 histogram = GNUNET_BIO_write_open_file (histogram_fn);
1514 if (NULL == histogram)
1516 "Unable to open histogram file `%s'\n",
1517 histogram_fn);
1518 GNUNET_free (histogram_fn);
1519 }
1520 logger_test = GNUNET_CLIENT_service_test ("testbed-logger",
1521 cfg,
1523 &status_cb,
1524 NULL);
1525 }
1526#endif
1527
1529 if (GNUNET_OK !=
1530 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "PROOFFILE", &proof))
1531 {
1534 return;
1535 }
1537 (sizeof(my_proof) !=
1539 my_proof = 0;
1541
1544 /* Connect to core service and register core handlers */
1545 core_api =
1546 GNUNET_CORE_connect (cfg, /* Main configuration */
1547 NULL, /* Closure passed to functions */
1548 NULL, /* Call core_init once connected */
1549 &handle_core_connect, /* Handle connects */
1550 &handle_core_disconnect, /* Handle disconnects */
1551 core_handlers, /* Register these handlers */
1552 &service_info);
1553 if (NULL == core_api)
1554 {
1556 return;
1557 }
1559 if (NULL == pils)
1560 {
1561 GNUNET_break (0);
1563 return;
1564 }
1566}

References _, cfg, core_api, GNUNET_asprintf(), GNUNET_assert, GNUNET_BIO_write_open_file(), GNUNET_break, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_time(), GNUNET_CONTAINER_multipeermap_create(), GNUNET_CORE_connect(), GNUNET_CORE_SERVICE_NSE, GNUNET_DISK_file_test(), GNUNET_DISK_fn_read(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_config_invalid(), GNUNET_log_config_missing(), GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD, GNUNET_MQ_handler_end, GNUNET_MQ_hd_fixed_size, GNUNET_notification_context_create(), gnunet_nse_interval, GNUNET_OK, GNUNET_PILS_connect(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), GNUNET_STATISTICS_create(), GNUNET_TIME_UNIT_SECONDS, GNUNET_YES, handle_core_connect(), handle_core_disconnect(), my_proof, nc, nse_work_required, peers, pils, pils_id_change_cb(), proof, proof_find_delay, GNUNET_CORE_ServiceInfo::service, service, shutdown_task, and stats.

Here is the call graph for this function:

◆ client_connect_cb()

static void * client_connect_cb ( void *  cls,
struct GNUNET_SERVICE_Client c,
struct GNUNET_MQ_Handle mq 
)
static

Callback called when a client connects to the service.

Parameters
clsclosure for the service
cthe new client that connected to the service
mqthe message queue used to send messages to the client
Returns
c

Definition at line 1578 of file gnunet-service-nse.c.

1581{
1582 (void) cls;
1583 (void) mq;
1584 return c;
1585}

References mq.

◆ client_disconnect_cb()

static void client_disconnect_cb ( void *  cls,
struct GNUNET_SERVICE_Client c,
void *  internal_cls 
)
static

Callback called when a client disconnected from the service.

Parameters
clsclosure for the service
cthe client that disconnected
internal_clsshould be equal to c

Definition at line 1596 of file gnunet-service-nse.c.

1599{
1600 (void) cls;
1601 GNUNET_assert (c == internal_cls);
1602}

References GNUNET_assert.

◆ GNUNET_SERVICE_MAIN()

GNUNET_SERVICE_MAIN ( GNUNET_OS_project_data_gnunet()  ,
"nse"  ,
GNUNET_SERVICE_OPTION_NONE  ,
run,
client_connect_cb,
client_disconnect_cb,
NULL  ,
GNUNET_MQ_hd_fixed_size(start, GNUNET_MESSAGE_TYPE_NSE_START, struct GNUNET_MessageHeader, NULL)  ,
GNUNET_MQ_handler_end()   
)

Define "main" method using service macro.

Variable Documentation

◆ nse_work_required

unsigned long long nse_work_required
static

Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.

Definition at line 86 of file gnunet-service-nse.c.

Referenced by check_proof_of_work(), find_proof(), run(), and verify_message_crypto().

◆ gnunet_nse_interval

struct GNUNET_TIME_Relative gnunet_nse_interval
static

Interval for sending network size estimation flood requests.

Definition at line 91 of file gnunet-service-nse.c.

Referenced by get_matching_bits_delay(), handle_p2p_estimate(), pils_id_change_cb(), run(), and update_flood_message().

◆ proof_find_delay

struct GNUNET_TIME_Relative proof_find_delay
static

Interval between proof find runs.

Definition at line 96 of file gnunet-service-nse.c.

Referenced by find_proof(), and run().

◆ salt

struct GNUNET_CRYPTO_PowSalt salt = { "gnunet-nse-proof" }
static

Salt for PoW calculations.

Definition at line 120 of file gnunet-service-nse.c.

120{ "gnunet-nse-proof" };

Referenced by check_proof_of_work(), and find_proof().

◆ cfg

Handle to our current configuration.

Definition at line 236 of file gnunet-service-nse.c.

Referenced by run(), and write_proof().

◆ stats

◆ pils

◆ core_api

struct GNUNET_CORE_Handle* core_api
static

Handle to the core service.

Definition at line 251 of file gnunet-service-nse.c.

Referenced by run(), and shutdown_task().

◆ peers

◆ current_size_estimate

double current_size_estimate
static

The current network size estimate.

Number of bits matching on average thus far.

Definition at line 262 of file gnunet-service-nse.c.

Referenced by get_matching_bits_delay(), and setup_estimate_message().

◆ current_std_dev

double current_std_dev = NAN
static

The standard deviation of the last HISTORY_SIZE network size estimates.

Definition at line 268 of file gnunet-service-nse.c.

Referenced by setup_estimate_message().

◆ hop_count_max

uint32_t hop_count_max
static

Current hop counter estimate (estimate for network diameter).

Definition at line 273 of file gnunet-service-nse.c.

Referenced by get_delay_randomization(), handle_p2p_estimate(), and update_flood_message().

◆ next_message

struct GNUNET_NSE_FloodMessage next_message
static

Message for the next round, if we got any.

Definition at line 278 of file gnunet-service-nse.c.

Referenced by dequeue_message_from_room(), handle_p2p_estimate(), and update_flood_message().

◆ size_estimate_messages

struct GNUNET_NSE_FloodMessage size_estimate_messages[64]
static

◆ estimate_index

unsigned int estimate_index
static

◆ estimate_count

unsigned int estimate_count
static

Number of valid entries in the history.

Definition at line 293 of file gnunet-service-nse.c.

Referenced by pils_id_change_cb(), setup_estimate_message(), and update_flood_message().

◆ flood_task

struct GNUNET_SCHEDULER_Task* flood_task
static

Task scheduled to update our flood message for the next round.

Definition at line 298 of file gnunet-service-nse.c.

Referenced by pils_id_change_cb(), shutdown_task(), and update_flood_message().

◆ proof_task

struct GNUNET_SCHEDULER_Task* proof_task
static

Task scheduled to compute our proof.

Definition at line 303 of file gnunet-service-nse.c.

Referenced by find_proof(), pils_id_change_cb(), shutdown_task(), and transmit_task_cb().

◆ nc

struct GNUNET_NotificationContext* nc
static

Notification context, simplifies client broadcasts.

Definition at line 308 of file gnunet-service-nse.c.

Referenced by handle_start(), run(), shutdown_task(), and update_network_size_estimate().

◆ next_timestamp

struct GNUNET_TIME_Absolute next_timestamp
static

The next major time.

Definition at line 313 of file gnunet-service-nse.c.

Referenced by handle_p2p_estimate(), pils_id_change_cb(), and update_flood_message().

◆ current_timestamp

struct GNUNET_TIME_Absolute current_timestamp
static

The current major time.

Definition at line 318 of file gnunet-service-nse.c.

Referenced by find_proof(), get_transmit_delay(), handle_p2p_estimate(), pils_id_change_cb(), and update_flood_message().

◆ my_proof

uint64_t my_proof
static

Proof of work for this peer.

Definition at line 323 of file gnunet-service-nse.c.

Referenced by find_proof(), pils_id_change_cb(), run(), setup_flood_message(), and write_proof().