2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file nse/gnunet-service-nse.c
23 * @brief network size estimation service
24 * @author Nathan Evans
25 * @author Christian Grothoff
27 * The purpose of this service is to estimate the size of the network.
28 * Given a specified interval, each peer hashes the most recent
29 * timestamp which is evenly divisible by that interval. This hash is
30 * compared in distance to the peer identity to choose an offset. The
31 * closer the peer identity to the hashed timestamp, the earlier the
32 * peer sends out a "nearest peer" message. The closest peer's
33 * message should thus be received before any others, which stops
34 * those peer from sending their messages at a later duration. So
35 * every peer should receive the same nearest peer message, and from
36 * this can calculate the expected number of peers in the network.
40 #include "gnunet_util_lib.h"
41 #include "gnunet_constants.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_signatures.h"
44 #include "gnunet_statistics_service.h"
45 #include "gnunet_core_service.h"
46 #include "gnunet_nse_service.h"
50 * Should messages be delayed randomly? This option should be set to
51 * GNUNET_NO only for experiments, not in production. It should also
52 * be removed once the initial experiments have been completed.
54 #define USE_RANDOM_DELAYS GNUNET_YES
57 * Should we generate a histogram with the time stamps of when we received
58 * NSE messages to disk? (for performance evaluation only, not useful in
59 * production). The associated code should also probably be removed
60 * once we're done with experiments.
62 #define ENABLE_HISTOGRAM GNUNET_NO
65 * Over how many values do we calculate the weighted average?
67 #define HISTORY_SIZE 64
70 * Message priority to use.
72 #define NSE_PRIORITY 5
75 #define log2(a) (log(a)/log(2))
79 * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
81 static unsigned long long nse_work_required;
84 * Interval for sending network size estimation flood requests.
86 static struct GNUNET_TIME_Relative gnunet_nse_interval;
89 * Interval between proof find runs.
91 static struct GNUNET_TIME_Relative proof_find_delay;
95 * Handle for writing when we received messages to disk.
97 static struct GNUNET_BIO_WriteHandle *wh;
102 * Per-peer information.
108 * Core handle for sending messages to this peer.
110 struct GNUNET_CORE_TransmitHandle *th;
113 * What is the identity of the peer?
115 struct GNUNET_PeerIdentity id;
118 * Task scheduled to send message to this peer.
120 GNUNET_SCHEDULER_TaskIdentifier transmit_task;
123 * Did we receive or send a message about the previous round
124 * to this peer yet? GNUNET_YES if the previous round has
125 * been taken care of.
132 * Amount of messages received from this peer on this round.
134 unsigned int received_messages;
137 * Amount of messages transmitted to this peer on this round.
139 unsigned int transmitted_messages;
142 * Which size did we tell the peer the network is?
144 unsigned int last_transmitted_size;
151 GNUNET_NETWORK_STRUCT_BEGIN
154 * Network size estimate reply; sent when "this"
155 * peer's timer has run out before receiving a
156 * valid reply from another peer.
158 struct GNUNET_NSE_FloodMessage
161 * Type: GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD
163 struct GNUNET_MessageHeader header;
166 * Number of hops this message has taken so far.
168 uint32_t hop_count GNUNET_PACKED;
173 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
176 * The current timestamp value (which all
177 * peers should agree on).
179 struct GNUNET_TIME_AbsoluteNBO timestamp;
182 * Number of matching bits between the hash
183 * of timestamp and the initiator's public
186 uint32_t matching_bits GNUNET_PACKED;
189 * Public key of the originator.
191 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
194 * Proof of work, causing leading zeros when hashed with pkey.
196 uint64_t proof_of_work GNUNET_PACKED;
199 * Signature (over range specified in purpose).
201 struct GNUNET_CRYPTO_RsaSignature signature;
203 GNUNET_NETWORK_STRUCT_END
206 * Handle to our current configuration.
208 static const struct GNUNET_CONFIGURATION_Handle *cfg;
211 * Handle to the statistics service.
213 static struct GNUNET_STATISTICS_Handle *stats;
216 * Handle to the core service.
218 static struct GNUNET_CORE_Handle *coreAPI;
221 * Map of all connected peers.
223 static struct GNUNET_CONTAINER_MultiHashMap *peers;
226 * The current network size estimate. Number of bits matching on
229 static double current_size_estimate;
232 * The standard deviation of the last HISTORY_SIZE network
235 static double current_std_dev = NAN;
238 * Current hop counter estimate (estimate for network diameter).
240 static uint32_t hop_count_max;
243 * Message for the next round, if we got any.
245 static struct GNUNET_NSE_FloodMessage next_message;
248 * Array of recent size estimate messages.
250 static struct GNUNET_NSE_FloodMessage size_estimate_messages[HISTORY_SIZE];
253 * Index of most recent estimate.
255 static unsigned int estimate_index;
258 * Number of valid entries in the history.
260 static unsigned int estimate_count;
263 * Task scheduled to update our flood message for the next round.
265 static GNUNET_SCHEDULER_TaskIdentifier flood_task;
268 * Task scheduled to compute our proof.
270 static GNUNET_SCHEDULER_TaskIdentifier proof_task;
273 * Notification context, simplifies client broadcasts.
275 static struct GNUNET_SERVER_NotificationContext *nc;
278 * The next major time.
280 static struct GNUNET_TIME_Absolute next_timestamp;
283 * The current major time.
285 static struct GNUNET_TIME_Absolute current_timestamp;
288 * The public key of this peer.
290 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
293 * The private key of this peer.
295 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
298 * The peer identity of this peer.
300 static struct GNUNET_PeerIdentity my_identity;
303 * Proof of work for this peer.
305 static uint64_t my_proof;
309 * Initialize a message to clients with the current network
312 * @param em message to fill in
315 setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
327 /* Weighted incremental algorithm for stddev according to West (1979) */
339 for (i = 0; i < estimate_count; i++)
341 j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
342 val = htonl (size_estimate_messages[j].matching_bits);
343 weight = estimate_count + 1 - i;
345 temp = weight + sumweight;
347 r = q * weight / temp;
349 sum += sumweight * q * r;
352 if (estimate_count > 0)
353 variance = (sum / sumweight) * estimate_count / (estimate_count - 1.0);
355 /* trivial version for debugging */
358 /* non-weighted trivial version */
364 for (i = 0; i < estimate_count; i++)
366 j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
367 val = htonl (size_estimate_messages[j].matching_bits);
371 if (0 != estimate_count)
373 mean = sum / estimate_count;
374 variance = (vsq - mean * sum) / (estimate_count - 1.0); // terrible for numerical stability...
378 std_dev = sqrt (variance);
380 std_dev = variance; /* must be infinity due to estimate_count == 0 */
381 current_std_dev = std_dev;
382 current_size_estimate = mean;
384 em->header.size = htons (sizeof (struct GNUNET_NSE_ClientMessage));
385 em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
386 em->reserved = htonl (0);
387 em->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
388 double se = mean - 0.332747;
389 nsize = log2 (GNUNET_CONTAINER_multihashmap_size (peers) + 1);
390 if (em->size_estimate < nsize)
392 em->size_estimate = GNUNET_hton_double (se);
393 em->std_deviation = GNUNET_hton_double (std_dev);
394 GNUNET_STATISTICS_set (stats, "# nodes in the network (estimate)",
395 (uint64_t) pow (2, mean - 1.0 / 3.0), GNUNET_NO);
400 * Handler for START message from client, triggers an
401 * immediate current network estimate notification.
402 * Also, we remember the client for updates upon future
403 * estimate measurements.
406 * @param client who sent the message
407 * @param message the message received
410 handle_start_message (void *cls, struct GNUNET_SERVER_Client *client,
411 const struct GNUNET_MessageHeader *message)
413 struct GNUNET_NSE_ClientMessage em;
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received START message from client\n");
418 GNUNET_SERVER_notification_context_add (nc, client);
419 setup_estimate_message (&em);
420 GNUNET_SERVER_notification_context_unicast (nc, client, &em.header,
422 GNUNET_SERVER_receive_done (client, GNUNET_OK);
427 * How long should we delay a message to go the given number of
430 * @param matching_bits number of matching bits to consider
433 get_matching_bits_delay (uint32_t matching_bits)
435 /* Calculated as: S + f/2 - (f / pi) * (atan(x - p')) */
436 // S is next_timestamp (ignored in return value)
437 // f is frequency (gnunet_nse_interval)
438 // x is matching_bits
439 // p' is current_size_estimate
440 return ((double) gnunet_nse_interval.rel_value / (double) 2.0) -
441 ((gnunet_nse_interval.rel_value / M_PI) *
442 atan (matching_bits - current_size_estimate));
447 * What delay randomization should we apply for a given number of matching bits?
449 * @param matching_bits number of matching bits
450 * @return random delay to apply
452 static struct GNUNET_TIME_Relative
453 get_delay_randomization (uint32_t matching_bits)
455 #if USE_RANDOM_DELAYS
456 struct GNUNET_TIME_Relative ret;
460 d = get_matching_bits_delay (matching_bits);
461 i = (uint32_t) (d / (double) (hop_count_max + 1));
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 "Randomizing flood using latencies up to %u ms\n",
467 ret.rel_value = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, i + 1);
470 return GNUNET_TIME_UNIT_ZERO;
476 * Get the number of matching bits that the given timestamp has to the given peer ID.
478 * @param timestamp time to generate key
479 * @param id peer identity to compare with
480 * @return number of matching bits
483 get_matching_bits (struct GNUNET_TIME_Absolute timestamp,
484 const struct GNUNET_PeerIdentity *id)
486 GNUNET_HashCode timestamp_hash;
488 GNUNET_CRYPTO_hash (×tamp.abs_value, sizeof (timestamp.abs_value),
490 return GNUNET_CRYPTO_hash_matching_bits (×tamp_hash, &id->hashPubKey);
495 * Get the transmission delay that should be applied for a
498 * @param round_offset -1 for the previous round (random delay between 0 and 50ms)
499 * 0 for the current round (based on our proximity to time key)
500 * @return delay that should be applied
502 static struct GNUNET_TIME_Relative
503 get_transmit_delay (int round_offset)
505 struct GNUNET_TIME_Relative ret;
506 struct GNUNET_TIME_Absolute tgt;
508 uint32_t matching_bits;
510 switch (round_offset)
513 /* previous round is randomized between 0 and 50 ms */
514 #if USE_RANDOM_DELAYS
515 ret.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 50);
517 ret = GNUNET_TIME_UNIT_ZERO;
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "Transmitting previous round behind schedule in %llu ms\n",
522 (unsigned long long) ret.rel_value);
526 /* current round is based on best-known matching_bits */
528 ntohl (size_estimate_messages[estimate_index].matching_bits);
529 dist_delay = get_matching_bits_delay (matching_bits);
530 dist_delay += get_delay_randomization (matching_bits).rel_value;
531 ret.rel_value = (uint64_t) dist_delay;
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
534 "For round %llu, delay for %u matching bits is %llu ms\n",
535 (unsigned long long) current_timestamp.abs_value,
536 (unsigned int) matching_bits,
537 (unsigned long long) ret.rel_value);
539 /* now consider round start time and add delay to it */
540 tgt = GNUNET_TIME_absolute_add (current_timestamp, ret);
541 return GNUNET_TIME_absolute_get_remaining (tgt);
544 return GNUNET_TIME_UNIT_FOREVER_REL;
549 * Task that triggers a NSE P2P transmission.
551 * @param cls the 'struct NSEPeerEntry'
552 * @param tc scheduler context
555 transmit_task_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
559 * Called when core is ready to send a message we asked for
560 * out to the destination.
562 * @param cls closure (NULL)
563 * @param size number of bytes available in buf
564 * @param buf where the callee should write the message
565 * @return number of bytes written to buf
568 transmit_ready (void *cls, size_t size, void *buf)
570 struct NSEPeerEntry *peer_entry = cls;
573 peer_entry->th = NULL;
576 /* client disconnected */
579 GNUNET_assert (size >= sizeof (struct GNUNET_NSE_FloodMessage));
580 idx = estimate_index;
581 if (peer_entry->previous_round == GNUNET_NO)
583 idx = (idx + HISTORY_SIZE - 1) % HISTORY_SIZE;
584 peer_entry->previous_round = GNUNET_YES;
585 peer_entry->transmit_task =
586 GNUNET_SCHEDULER_add_delayed (get_transmit_delay (0), &transmit_task_cb,
589 if ((ntohl (size_estimate_messages[idx].hop_count) == 0) &&
590 (GNUNET_SCHEDULER_NO_TASK != proof_task))
592 GNUNET_STATISTICS_update (stats,
593 "# flood messages not generated (no proof yet)",
597 if (ntohs (size_estimate_messages[idx].header.size) == 0)
599 GNUNET_STATISTICS_update (stats,
600 "# flood messages not generated (lack of history)",
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "In round %llu, sending to `%s' estimate with %u bits\n",
608 GNUNET_TIME_absolute_ntoh (size_estimate_messages[idx].
609 timestamp).abs_value,
610 GNUNET_i2s (&peer_entry->id),
611 (unsigned int) ntohl (size_estimate_messages[idx].matching_bits));
613 if (ntohl (size_estimate_messages[idx].hop_count) == 0)
614 GNUNET_STATISTICS_update (stats, "# flood messages started", 1, GNUNET_NO);
615 GNUNET_STATISTICS_update (stats, "# flood messages transmitted", 1,
618 peer_entry->transmitted_messages++;
619 peer_entry->last_transmitted_size =
620 ntohl(size_estimate_messages[idx].matching_bits);
622 memcpy (buf, &size_estimate_messages[idx],
623 sizeof (struct GNUNET_NSE_FloodMessage));
624 return sizeof (struct GNUNET_NSE_FloodMessage);
629 * Task that triggers a NSE P2P transmission.
631 * @param cls the 'struct NSEPeerEntry'
632 * @param tc scheduler context
635 transmit_task_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
637 struct NSEPeerEntry *peer_entry = cls;
639 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
640 GNUNET_assert (NULL == peer_entry->th);
642 GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_NO, NSE_PRIORITY,
643 GNUNET_TIME_UNIT_FOREVER_REL,
646 GNUNET_NSE_FloodMessage),
647 &transmit_ready, peer_entry);
652 * We've sent on our flood message or one that we received which was
653 * validated and closer than ours. Update the global list of recent
654 * messages and the average. Also re-broadcast the message to any
658 update_network_size_estimate ()
660 struct GNUNET_NSE_ClientMessage em;
662 setup_estimate_message (&em);
663 GNUNET_SERVER_notification_context_broadcast (nc, &em.header, GNUNET_YES);
668 * Setup a flood message in our history array at the given
669 * slot offset for the given timestamp.
671 * @param slot index to use
672 * @param ts timestamp to use
675 setup_flood_message (unsigned int slot, struct GNUNET_TIME_Absolute ts)
677 struct GNUNET_NSE_FloodMessage *fm;
678 uint32_t matching_bits;
680 matching_bits = get_matching_bits (ts, &my_identity);
681 fm = &size_estimate_messages[slot];
682 fm->header.size = htons (sizeof (struct GNUNET_NSE_FloodMessage));
683 fm->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD);
684 fm->hop_count = htonl (0);
685 fm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_NSE_SEND);
687 htonl (sizeof (struct GNUNET_NSE_FloodMessage) -
688 sizeof (struct GNUNET_MessageHeader) - sizeof (uint32_t) -
689 sizeof (struct GNUNET_CRYPTO_RsaSignature));
690 fm->matching_bits = htonl (matching_bits);
691 fm->timestamp = GNUNET_TIME_absolute_hton (ts);
692 fm->pkey = my_public_key;
693 fm->proof_of_work = my_proof;
694 if (nse_work_required > 0)
695 GNUNET_assert (GNUNET_OK ==
696 GNUNET_CRYPTO_rsa_sign (my_private_key, &fm->purpose,
699 memset (&fm->signature, 0, sizeof (fm->signature));
704 * Schedule transmission for the given peer for the current round based
705 * on what we know about the desired delay.
708 * @param key hash of peer identity
709 * @param value the 'struct NSEPeerEntry'
710 * @return GNUNET_OK (continue to iterate)
713 schedule_current_round (void *cls, const GNUNET_HashCode * key, void *value)
715 struct NSEPeerEntry *peer_entry = value;
716 struct GNUNET_TIME_Relative delay;
718 if (peer_entry->th != NULL)
720 peer_entry->previous_round = GNUNET_NO;
723 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
725 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
726 peer_entry->previous_round = GNUNET_NO;
729 if (peer_entry->received_messages > 1)
730 GNUNET_STATISTICS_update(stats, "# extra messages",
731 peer_entry->received_messages - 1, GNUNET_NO);
732 peer_entry->transmitted_messages = 0;
733 peer_entry->last_transmitted_size = 0;
734 peer_entry->received_messages = 0;
737 get_transmit_delay ((peer_entry->previous_round == GNUNET_NO) ? -1 : 0);
738 peer_entry->transmit_task =
739 GNUNET_SCHEDULER_add_delayed (delay, &transmit_task_cb, peer_entry);
745 * Update our flood message to be sent (and our timestamps).
748 * @param tc context for this message
751 update_flood_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
753 struct GNUNET_TIME_Relative offset;
756 flood_task = GNUNET_SCHEDULER_NO_TASK;
757 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
759 offset = GNUNET_TIME_absolute_get_remaining (next_timestamp);
760 if (0 != offset.rel_value)
762 /* somehow run early, delay more */
764 GNUNET_SCHEDULER_add_delayed (offset, &update_flood_message, NULL);
767 current_timestamp = next_timestamp;
769 GNUNET_TIME_absolute_add (current_timestamp, gnunet_nse_interval);
770 estimate_index = (estimate_index + 1) % HISTORY_SIZE;
771 if (estimate_count < HISTORY_SIZE)
773 if (next_timestamp.abs_value ==
774 GNUNET_TIME_absolute_ntoh (next_message.timestamp).abs_value)
776 /* we received a message for this round way early, use it! */
777 size_estimate_messages[estimate_index] = next_message;
778 size_estimate_messages[estimate_index].hop_count =
779 htonl (1 + ntohl (next_message.hop_count));
782 setup_flood_message (estimate_index, current_timestamp);
783 next_message.matching_bits = htonl (0); /* reset for 'next' round */
785 for (i = 0; i < HISTORY_SIZE; i++)
787 GNUNET_MAX (ntohl (size_estimate_messages[i].hop_count), hop_count_max);
788 GNUNET_CONTAINER_multihashmap_iterate (peers, &schedule_current_round, NULL);
790 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
791 (next_timestamp), &update_flood_message,
797 * Count the leading zeroes in hash.
800 * @return the number of leading zero bits.
803 count_leading_zeroes (const GNUNET_HashCode * hash)
805 unsigned int hash_count;
808 while ((0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)))
815 * Check whether the given public key
816 * and integer are a valid proof of work.
818 * @param pkey the public key
819 * @param val the integer
821 * @return GNUNET_YES if valid, GNUNET_NO if not
824 check_proof_of_work (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
827 char buf[sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
829 GNUNET_HashCode result;
831 memcpy (buf, &val, sizeof (val));
832 memcpy (&buf[sizeof (val)], pkey,
833 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
834 GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
835 return (count_leading_zeroes (&result) >=
836 nse_work_required) ? GNUNET_YES : GNUNET_NO;
841 * Write our current proof to disk.
849 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "PROOFFILE", &proof))
851 if (sizeof (my_proof) !=
852 GNUNET_DISK_fn_write (proof, &my_proof, sizeof (my_proof),
853 GNUNET_DISK_PERM_USER_READ |
854 GNUNET_DISK_PERM_USER_WRITE))
855 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", proof);
862 * Find our proof of work.
864 * @param cls closure (unused)
865 * @param tc task context
868 find_proof (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
870 #define ROUND_SIZE 10
872 char buf[sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
874 GNUNET_HashCode result;
877 proof_task = GNUNET_SCHEDULER_NO_TASK;
878 memcpy (&buf[sizeof (uint64_t)], &my_public_key,
879 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
882 while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
884 memcpy (buf, &counter, sizeof (uint64_t));
885 GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
886 if (nse_work_required <= count_leading_zeroes (&result))
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Proof of work found: %llu!\n",
891 (unsigned long long) GNUNET_ntohll (counter));
894 setup_flood_message (estimate_index, current_timestamp);
900 if (my_proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing proofs currently at %llu\n",
904 (unsigned long long) counter);
906 /* remember progress every 100 rounds */
915 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay,
916 GNUNET_SCHEDULER_PRIORITY_IDLE,
922 * An incoming flood message has been received which claims
923 * to have more bits matching than any we know in this time
924 * period. Verify the signature and/or proof of work.
926 * @param incoming_flood the message to verify
928 * @return GNUNET_YES if the message is verified
929 * GNUNET_NO if the key/signature don't verify
932 verify_message_crypto (const struct GNUNET_NSE_FloodMessage *incoming_flood)
935 check_proof_of_work (&incoming_flood->pkey,
936 incoming_flood->proof_of_work))
938 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Proof of work invalid: %llu!\n"),
940 GNUNET_ntohll (incoming_flood->proof_of_work));
944 if ((nse_work_required > 0) &&
946 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NSE_SEND,
947 &incoming_flood->purpose,
948 &incoming_flood->signature,
949 &incoming_flood->pkey)))
959 * Update transmissions for the given peer for the current round based
960 * on updated proximity information.
962 * @param cls peer entry to exclude from updates
963 * @param key hash of peer identity
964 * @param value the 'struct NSEPeerEntry'
965 * @return GNUNET_OK (continue to iterate)
968 update_flood_times (void *cls, const GNUNET_HashCode * key, void *value)
970 struct NSEPeerEntry *exclude = cls;
971 struct NSEPeerEntry *peer_entry = value;
972 struct GNUNET_TIME_Relative delay;
974 if (peer_entry->th != NULL)
975 return GNUNET_OK; /* already active */
976 if (peer_entry == exclude)
977 return GNUNET_OK; /* trigger of the update */
978 if (peer_entry->previous_round == GNUNET_NO)
980 /* still stuck in previous round, no point to update, check that
981 * we are active here though... */
982 GNUNET_break (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK);
985 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
987 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
988 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
990 delay = get_transmit_delay (0);
991 peer_entry->transmit_task =
992 GNUNET_SCHEDULER_add_delayed (delay, &transmit_task_cb, peer_entry);
998 * Core handler for size estimate flooding messages.
1000 * @param cls closure unused
1001 * @param message message
1002 * @param peer peer identity this message is from (ignored)
1003 * @param atsi performance data (ignored)
1004 * @param atsi_count number of records in 'atsi'
1007 handle_p2p_size_estimate (void *cls, const struct GNUNET_PeerIdentity *peer,
1008 const struct GNUNET_MessageHeader *message,
1009 const struct GNUNET_ATS_Information *atsi,
1010 unsigned int atsi_count)
1012 const struct GNUNET_NSE_FloodMessage *incoming_flood;
1013 struct GNUNET_TIME_Absolute ts;
1014 struct NSEPeerEntry *peer_entry;
1015 uint32_t matching_bits;
1018 #if ENABLE_HISTOGRAM
1020 GNUNET_BIO_write_int64 (wh, GNUNET_TIME_absolute_get ().abs_value);
1022 incoming_flood = (const struct GNUNET_NSE_FloodMessage *) message;
1023 GNUNET_STATISTICS_update (stats, "# flood messages received", 1, GNUNET_NO);
1024 matching_bits = ntohl (incoming_flood->matching_bits);
1029 struct GNUNET_PeerIdentity os;
1031 GNUNET_CRYPTO_hash (&incoming_flood->pkey,
1032 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1034 GNUNET_snprintf (origin, sizeof (origin), "%s", GNUNET_i2s (&os));
1035 GNUNET_snprintf (pred, sizeof (pred), "%s", GNUNET_i2s (peer));
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "Flood at %llu from `%s' via `%s' at `%s' with bits %u\n",
1038 (unsigned long long)
1039 GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp).abs_value,
1040 origin, pred, GNUNET_i2s (&my_identity),
1041 (unsigned int) matching_bits);
1045 peer_entry = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1046 if (NULL == peer_entry)
1051 #if ENABLE_HISTOGRAM
1052 peer_entry->received_messages++;
1053 if (peer_entry->transmitted_messages > 0 &&
1054 peer_entry->last_transmitted_size >= matching_bits)
1055 GNUNET_STATISTICS_update(stats, "# cross messages", 1, GNUNET_NO);
1058 ts = GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp);
1060 if (ts.abs_value == current_timestamp.abs_value)
1061 idx = estimate_index;
1062 else if (ts.abs_value ==
1063 current_timestamp.abs_value - gnunet_nse_interval.rel_value)
1064 idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE;
1065 else if (ts.abs_value ==
1066 next_timestamp.abs_value - gnunet_nse_interval.rel_value)
1068 if (matching_bits <= ntohl (next_message.matching_bits))
1069 return GNUNET_OK; /* ignore, simply too early/late */
1070 if (GNUNET_YES != verify_message_crypto (incoming_flood))
1072 GNUNET_break_op (0);
1075 next_message = *incoming_flood;
1080 GNUNET_STATISTICS_update (stats,
1081 "# flood messages discarded (clock skew too large)",
1085 if (0 == (memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity))))
1087 /* send to self, update our own estimate IF this also comes from us! */
1089 memcmp (&incoming_flood->pkey, &my_public_key, sizeof (my_public_key)))
1090 update_network_size_estimate ();
1093 if (matching_bits == ntohl (size_estimate_messages[idx].matching_bits))
1095 /* Cancel transmission in the other direction, as this peer clearly has
1096 up-to-date information already. Even if we didn't talk to this peer in
1097 the previous round, we should no longer send it stale information as it
1098 told us about the current round! */
1099 peer_entry->previous_round = GNUNET_YES;
1100 if (idx != estimate_index)
1102 /* do not transmit information for the previous round to this peer
1103 anymore (but allow current round) */
1106 /* got up-to-date information for current round, cancel transmission to
1107 * this peer altogether */
1108 if (GNUNET_SCHEDULER_NO_TASK != peer_entry->transmit_task)
1110 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1111 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
1113 if (peer_entry->th != NULL)
1115 GNUNET_CORE_notify_transmit_ready_cancel (peer_entry->th);
1116 peer_entry->th = NULL;
1120 if (matching_bits < ntohl (size_estimate_messages[idx].matching_bits))
1122 if ((idx < estimate_index) && (peer_entry->previous_round == GNUNET_YES))
1123 peer_entry->previous_round = GNUNET_NO;
1124 /* push back our result now, that peer is spreading bad information... */
1125 if (NULL == peer_entry->th)
1127 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
1128 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1129 peer_entry->transmit_task =
1130 GNUNET_SCHEDULER_add_now (&transmit_task_cb, peer_entry);
1132 /* Not closer than our most recent message, no need to do work here */
1133 GNUNET_STATISTICS_update (stats,
1134 "# flood messages ignored (had closer already)",
1138 if (GNUNET_YES != verify_message_crypto (incoming_flood))
1140 GNUNET_break_op (0);
1143 GNUNET_assert (matching_bits >
1144 ntohl (size_estimate_messages[idx].matching_bits));
1145 /* cancel transmission from us to this peer for this round */
1146 if (idx == estimate_index)
1148 /* cancel any activity for current round */
1149 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
1151 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
1152 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
1154 if (peer_entry->th != NULL)
1156 GNUNET_CORE_notify_transmit_ready_cancel (peer_entry->th);
1157 peer_entry->th = NULL;
1162 /* cancel previous round only */
1163 peer_entry->previous_round = GNUNET_YES;
1165 size_estimate_messages[idx] = *incoming_flood;
1166 size_estimate_messages[idx].hop_count =
1167 htonl (ntohl (incoming_flood->hop_count) + 1);
1169 GNUNET_MAX (ntohl (incoming_flood->hop_count) + 1, hop_count_max);
1170 GNUNET_STATISTICS_set (stats,
1171 "# estimated network diameter",
1172 hop_count_max, GNUNET_NO);
1174 /* have a new, better size estimate, inform clients */
1175 update_network_size_estimate ();
1178 GNUNET_CONTAINER_multihashmap_iterate (peers, &update_flood_times,
1186 * Method called whenever a peer connects.
1188 * @param cls closure
1189 * @param peer peer identity this notification is about
1190 * @param atsi performance data
1191 * @param atsi_count number of records in 'atsi'
1194 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
1195 const struct GNUNET_ATS_Information *atsi,
1196 unsigned int atsi_count)
1198 struct NSEPeerEntry *peer_entry;
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' connected to us\n",
1204 peer_entry = GNUNET_malloc (sizeof (struct NSEPeerEntry));
1205 peer_entry->id = *peer;
1206 GNUNET_assert (GNUNET_OK ==
1207 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey,
1209 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1210 peer_entry->transmit_task =
1211 GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1), &transmit_task_cb,
1213 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
1218 * Method called whenever a peer disconnects.
1220 * @param cls closure
1221 * @param peer peer identity this notification is about
1224 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1226 struct NSEPeerEntry *pos;
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' disconnected from us\n",
1232 pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1238 GNUNET_assert (GNUNET_YES ==
1239 GNUNET_CONTAINER_multihashmap_remove (peers, &peer->hashPubKey,
1241 if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK)
1242 GNUNET_SCHEDULER_cancel (pos->transmit_task);
1243 if (pos->th != NULL)
1245 GNUNET_CORE_notify_transmit_ready_cancel (pos->th);
1249 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
1254 * Task run during shutdown.
1260 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1262 if (flood_task != GNUNET_SCHEDULER_NO_TASK)
1264 GNUNET_SCHEDULER_cancel (flood_task);
1265 flood_task = GNUNET_SCHEDULER_NO_TASK;
1267 if (proof_task != GNUNET_SCHEDULER_NO_TASK)
1269 GNUNET_SCHEDULER_cancel (proof_task);
1270 proof_task = GNUNET_SCHEDULER_NO_TASK;
1271 write_proof (); /* remember progress */
1275 GNUNET_SERVER_notification_context_destroy (nc);
1278 if (coreAPI != NULL)
1280 GNUNET_CORE_disconnect (coreAPI);
1285 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1290 GNUNET_CONTAINER_multihashmap_destroy (peers);
1293 if (my_private_key != NULL)
1295 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1296 my_private_key = NULL;
1298 #if ENABLE_HISTOGRAM
1301 GNUNET_BIO_write_close (wh);
1309 * Called on core init/fail.
1311 * @param cls service closure
1312 * @param server handle to the server for this service
1313 * @param identity the public identity of this peer
1316 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1317 const struct GNUNET_PeerIdentity *identity)
1319 struct GNUNET_TIME_Absolute now;
1320 struct GNUNET_TIME_Absolute prev_time;
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection to core FAILED!\n");
1327 GNUNET_SCHEDULER_shutdown ();
1331 memcmp (&my_identity, identity,
1332 sizeof (struct GNUNET_PeerIdentity)));
1333 now = GNUNET_TIME_absolute_get ();
1334 current_timestamp.abs_value =
1335 (now.abs_value / gnunet_nse_interval.rel_value) *
1336 gnunet_nse_interval.rel_value;
1338 GNUNET_TIME_absolute_add (current_timestamp, gnunet_nse_interval);
1339 estimate_index = HISTORY_SIZE - 1;
1341 if (GNUNET_YES == check_proof_of_work (&my_public_key, my_proof))
1343 prev_time.abs_value =
1344 current_timestamp.abs_value - gnunet_nse_interval.rel_value;
1345 setup_flood_message (estimate_index, prev_time);
1349 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1350 (next_timestamp), &update_flood_message,
1356 * Handle network size estimate clients.
1358 * @param cls closure
1359 * @param server the initialized server
1360 * @param c configuration to use
1363 run (void *cls, struct GNUNET_SERVER_Handle *server,
1364 const struct GNUNET_CONFIGURATION_Handle *c)
1369 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1370 {&handle_start_message, NULL, GNUNET_MESSAGE_TYPE_NSE_START,
1371 sizeof (struct GNUNET_MessageHeader)},
1374 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
1375 {&handle_p2p_size_estimate, GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD,
1376 sizeof (struct GNUNET_NSE_FloodMessage)},
1382 GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", "INTERVAL",
1383 &gnunet_nse_interval)) ||
1385 GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", "WORKDELAY",
1386 &proof_find_delay)) ||
1388 GNUNET_CONFIGURATION_get_value_number (cfg, "NSE", "WORKBITS",
1389 &nse_work_required)))
1391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1393 ("NSE service is lacking key configuration settings. Exiting.\n"));
1394 GNUNET_SCHEDULER_shutdown ();
1397 if (nse_work_required >= sizeof (GNUNET_HashCode) * 8)
1399 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1400 _("Invalid work requirement for NSE service. Exiting.\n"));
1401 GNUNET_SCHEDULER_shutdown ();
1407 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
1410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1412 ("NSE service is lacking key configuration settings. Exiting.\n"));
1413 GNUNET_SCHEDULER_shutdown ();
1416 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1417 GNUNET_free (keyfile);
1418 if (my_private_key == NULL)
1420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1421 _("NSE service could not access hostkey. Exiting.\n"));
1422 GNUNET_SCHEDULER_shutdown ();
1425 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1426 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
1427 &my_identity.hashPubKey);
1429 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "PROOFFILE", &proof))
1431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1433 ("NSE service is lacking key configuration settings. Exiting.\n"));
1434 if (my_private_key != NULL)
1436 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1437 my_private_key = NULL;
1439 GNUNET_SCHEDULER_shutdown ();
1442 if ((GNUNET_YES != GNUNET_DISK_file_test (proof)) ||
1443 (sizeof (my_proof) !=
1444 GNUNET_DISK_fn_read (proof, &my_proof, sizeof (my_proof))))
1446 GNUNET_free (proof);
1448 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1451 peers = GNUNET_CONTAINER_multihashmap_create (128);
1452 GNUNET_SERVER_add_handlers (server, handlers);
1453 nc = GNUNET_SERVER_notification_context_create (server, 1);
1454 /* Connect to core service and register core handlers */
1455 coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */
1456 1, NULL, /* Closure passed to functions */
1457 &core_init, /* Call core_init once connected */
1458 &handle_core_connect, /* Handle connects */
1459 &handle_core_disconnect, /* Handle disconnects */
1460 NULL, /* Don't want notified about all incoming messages */
1461 GNUNET_NO, /* For header only inbound notification */
1462 NULL, /* Don't want notified about all outbound messages */
1463 GNUNET_NO, /* For header only outbound notification */
1464 core_handlers); /* Register these handlers */
1465 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1467 #if ENABLE_HISTOGRAM
1469 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "HISTOGRAM", &proof))
1471 wh = GNUNET_BIO_write_open (proof);
1472 GNUNET_free (proof);
1475 if (coreAPI == NULL)
1477 GNUNET_SCHEDULER_shutdown ();
1480 stats = GNUNET_STATISTICS_create ("nse", cfg);
1485 * The main function for the statistics service.
1487 * @param argc number of arguments from the command line
1488 * @param argv command line arguments
1489 * @return 0 ok, 1 on error
1492 main (int argc, char *const *argv)
1494 return (GNUNET_OK ==
1495 GNUNET_SERVICE_run (argc, argv, "nse", GNUNET_SERVICE_OPTION_NONE,
1496 &run, NULL)) ? 0 : 1;
1499 /* end of gnunet-service-nse.c */