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.
39 * - generate proof-of-work asynchronously, store it on disk & load it back
40 * - handle messages for future round (one into the future, see FIXME)
43 #include "gnunet_util_lib.h"
44 #include "gnunet_constants.h"
45 #include "gnunet_protocols.h"
46 #include "gnunet_signatures.h"
47 #include "gnunet_statistics_service.h"
48 #include "gnunet_core_service.h"
49 #include "gnunet_nse_service.h"
53 * Over how many values do we calculate the weighted average?
55 #define HISTORY_SIZE 8
58 * Size of the queue to core.
60 #define CORE_QUEUE_SIZE 2
63 * Message priority to use.
65 #define NSE_PRIORITY 5
68 * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
70 #define NSE_WORK_REQUIRED 0
73 * Interval for sending network size estimation flood requests.
75 #define GNUNET_NSE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
79 * Per-peer information.
85 * Pending message for this peer.
87 struct GNUNET_MessageHeader *pending_message;
90 * Core handle for sending messages to this peer.
92 struct GNUNET_CORE_TransmitHandle *th;
95 * What is the identity of the peer?
97 struct GNUNET_PeerIdentity id;
100 * Task scheduled to send message to this peer.
102 GNUNET_SCHEDULER_TaskIdentifier transmit_task;
105 * Did we receive or send a message about the previous round
113 * Network size estimate reply; sent when "this"
114 * peer's timer has run out before receiving a
115 * valid reply from another peer.
117 struct GNUNET_NSE_FloodMessage
120 * Type: GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD
122 struct GNUNET_MessageHeader header;
125 * Number of hops this message has taken so far.
132 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
135 * The current timestamp value (which all
136 * peers should agree on).
138 struct GNUNET_TIME_AbsoluteNBO timestamp;
141 * Number of matching bits between the hash
142 * of timestamp and the initiator's public
145 uint32_t matching_bits;
148 * Public key of the originator.
150 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
153 * Proof of work, causing leading zeros when hashed with pkey.
155 uint64_t proof_of_work;
158 * Signature (over range specified in purpose).
160 struct GNUNET_CRYPTO_RsaSignature signature;
165 * Handle to our current configuration.
167 static const struct GNUNET_CONFIGURATION_Handle *cfg;
170 * Handle to the statistics service.
172 static struct GNUNET_STATISTICS_Handle *stats;
175 * Handle to the core service.
177 static struct GNUNET_CORE_Handle *coreAPI;
180 * Map of all connected peers.
182 static struct GNUNET_CONTAINER_MultiHashMap *peers;
185 * The current network size estimate. Number of bits matching on
188 static double current_size_estimate;
191 * The standard deviation of the last HISTORY_SIZE network
194 static double current_std_dev = NAN;
197 * Current hop counter estimate (estimate for network diameter).
199 static uint32_t hop_count_max;
202 * Array of recent size estimate messages.
204 static struct GNUNET_NSE_FloodMessage size_estimate_messages[HISTORY_SIZE];
207 * Index of most recent estimate.
209 static unsigned int estimate_index;
212 * Task scheduled to update our flood message for the next round.
214 static GNUNET_SCHEDULER_TaskIdentifier flood_task;
217 * Notification context, simplifies client broadcasts.
219 static struct GNUNET_SERVER_NotificationContext *nc;
222 * The next major time.
224 static struct GNUNET_TIME_Absolute next_timestamp;
227 * The current major time.
229 static struct GNUNET_TIME_Absolute current_timestamp;
232 * The public key of this peer.
234 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
237 * The private key of this peer.
239 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
242 * The peer identity of this peer.
244 static struct GNUNET_PeerIdentity my_identity;
247 * Proof of work for this peer.
249 static uint64_t my_proof;
253 * Initialize a message to clients with the current network
256 * @param em message to fill in
259 setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
273 /* Weighted incremental algorithm for stddev according to West (1979) */
277 for (i=0; i<HISTORY_SIZE; i++)
279 val = htonl (size_estimate_messages[i].matching_bits);
280 weight = HISTORY_SIZE - ((estimate_index + HISTORY_SIZE - i) % HISTORY_SIZE);
282 temp = weight + sumweight;
284 r = q * weight / temp;
285 sum += sumweight * q * r;
289 variance = sum / (sumweight - 1.0);
290 GNUNET_assert (variance >= 0);
291 std_dev = sqrt (variance);
292 current_std_dev = std_dev;
293 current_size_estimate = mean;
296 = htons (sizeof(struct GNUNET_NSE_ClientMessage));
298 = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
299 em->reserved = htonl (0);
300 em->size_estimate = mean - 0.5;
301 em->std_deviation = std_dev;
302 GNUNET_STATISTICS_set (stats,
303 "Current network size estimate",
304 (uint64_t) pow (2, mean - 0.5), GNUNET_NO);
309 * Handler for START message from client, triggers an
310 * immediate current network estimate notification.
311 * Also, we remember the client for updates upon future
312 * estimate measurements.
315 * @param client who sent the message
316 * @param message the message received
319 handle_start_message(void *cls, struct GNUNET_SERVER_Client *client,
320 const struct GNUNET_MessageHeader *message)
322 struct GNUNET_NSE_ClientMessage em;
324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
325 "Received START message from client\n");
327 GNUNET_SERVER_notification_context_add (nc, client);
328 setup_estimate_message (&em);
329 GNUNET_SERVER_notification_context_unicast (nc, client, &em.header, GNUNET_YES);
330 GNUNET_SERVER_receive_done (client, GNUNET_OK);
335 * How long should we delay a message to go the given number of
338 * @param matching_bits number of matching bits to consider
341 get_matching_bits_delay (uint32_t matching_bits)
343 /* Calculated as: S + f/2 - (f / pi) * (atan(x - p'))*/
344 // S is next_timestamp
345 // f is frequency (GNUNET_NSE_INTERVAL)
346 // x is matching_bits
347 // p' is current_size_estimate
348 return ((double) GNUNET_NSE_INTERVAL.rel_value / (double) 2)
349 - ((GNUNET_NSE_INTERVAL.rel_value / M_PI) * atan (matching_bits - current_size_estimate));
354 * What delay randomization should we apply for a given number of matching bits?
356 * @param matching_bits number of matching bits
357 * @return random delay to apply
359 static struct GNUNET_TIME_Relative
360 get_delay_randomization (uint32_t matching_bits)
362 struct GNUNET_TIME_Relative ret;
364 if (matching_bits == 0)
365 return GNUNET_TIME_UNIT_ZERO;
366 ret.rel_value = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
367 (uint32_t) (get_matching_bits_delay (matching_bits - 1) / (double) (hop_count_max + 1)));
373 * Get the number of matching bits that the given timestamp has to the given peer ID.
375 * @param timestamp time to generate key
376 * @param id peer identity to compare with
377 * @return number of matching bits
380 get_matching_bits (struct GNUNET_TIME_Absolute timestamp,
381 const struct GNUNET_PeerIdentity *id)
383 GNUNET_HashCode timestamp_hash;
385 GNUNET_CRYPTO_hash (×tamp.abs_value,
386 sizeof(timestamp.abs_value),
388 return GNUNET_CRYPTO_hash_matching_bits (×tamp_hash,
394 * Get the transmission delay that should be applied for a
397 * @param round_offset -1 for the previous round (random delay between 0 and 50ms)
398 * 0 for the current round (based on our proximity to time key)
399 * @return delay that should be applied
401 static struct GNUNET_TIME_Relative
402 get_transmit_delay (int round_offset)
404 struct GNUNET_TIME_Relative ret;
405 struct GNUNET_TIME_Absolute tgt;
407 uint32_t matching_bits;
409 switch (round_offset)
412 /* previous round is randomized between 0 and 50 ms */
413 ret.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
417 /* current round is based on best-known matching_bits */
418 matching_bits = ntohl (size_estimate_messages[estimate_index].matching_bits);
419 dist_delay = get_matching_bits_delay (matching_bits);
420 dist_delay += get_delay_randomization (matching_bits).rel_value;
421 ret.rel_value = (uint64_t) dist_delay;
422 /* now consider round start time and add delay to it */
423 tgt = GNUNET_TIME_absolute_add (current_timestamp, ret);
424 return GNUNET_TIME_absolute_get_remaining (tgt);
427 return GNUNET_TIME_UNIT_FOREVER_REL;
432 * Task that triggers a NSE P2P transmission.
434 * @param cls the 'struct NSEPeerEntry'
435 * @param tc scheduler context
438 transmit_task (void *cls,
439 const struct GNUNET_SCHEDULER_TaskContext *tc);
443 * Called when core is ready to send a message we asked for
444 * out to the destination.
446 * @param cls closure (NULL)
447 * @param size number of bytes available in buf
448 * @param buf where the callee should write the message
449 * @return number of bytes written to buf
452 transmit_ready (void *cls, size_t size, void *buf)
454 struct NSEPeerEntry *peer_entry = cls;
457 peer_entry->th = NULL;
460 /* client disconnected */
463 GNUNET_assert (size >= sizeof (struct GNUNET_NSE_FloodMessage));
465 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
466 "Sending size estimate to `%s'\n",
467 GNUNET_i2s (&peer_entry->id));
469 idx = estimate_index;
470 if (peer_entry->previous_round == GNUNET_YES)
472 idx = (idx + HISTORY_SIZE -1) % HISTORY_SIZE;
473 peer_entry->previous_round = GNUNET_NO;
474 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (get_transmit_delay (0),
479 &size_estimate_messages[idx],
480 sizeof (struct GNUNET_NSE_FloodMessage));
481 GNUNET_STATISTICS_update (stats,
482 "# flood messages sent",
485 return sizeof (struct GNUNET_NSE_FloodMessage);
490 * Task that triggers a NSE P2P transmission.
492 * @param cls the 'struct NSEPeerEntry'
493 * @param tc scheduler context
496 transmit_task (void *cls,
497 const struct GNUNET_SCHEDULER_TaskContext *tc)
499 struct NSEPeerEntry *peer_entry = cls;
501 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
503 = GNUNET_CORE_notify_transmit_ready (coreAPI,
506 GNUNET_TIME_UNIT_FOREVER_REL,
508 sizeof (struct GNUNET_NSE_FloodMessage),
509 &transmit_ready, peer_entry);
514 * We've sent on our flood message or one that we received which was
515 * validated and closer than ours. Update the global list of recent
516 * messages and the average. Also re-broadcast the message to any
520 update_network_size_estimate ()
522 struct GNUNET_NSE_ClientMessage em;
524 setup_estimate_message (&em);
525 GNUNET_SERVER_notification_context_broadcast (nc,
532 * Setup a flood message in our history array at the given
533 * slot offset for the given timestamp.
535 * @param slot index to use
536 * @param ts timestamp to use
539 setup_flood_message (unsigned int slot,
540 struct GNUNET_TIME_Absolute ts)
542 struct GNUNET_NSE_FloodMessage *fm;
543 uint32_t matching_bits;
545 matching_bits = get_matching_bits (ts, &my_identity);
546 fm = &size_estimate_messages[slot];
547 fm->header.size = htons (sizeof(struct GNUNET_NSE_FloodMessage));
548 fm->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD);
549 fm->hop_count = htonl (0);
550 fm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_NSE_SEND);
551 fm->purpose.size = htonl (sizeof(struct GNUNET_NSE_FloodMessage)
552 - sizeof (struct GNUNET_MessageHeader)
554 - sizeof (struct GNUNET_CRYPTO_RsaSignature));
555 fm->matching_bits = htonl (matching_bits);
556 fm->timestamp = GNUNET_TIME_absolute_hton (ts);
557 fm->pkey = my_public_key;
558 fm->proof_of_work = my_proof;
559 GNUNET_CRYPTO_rsa_sign (my_private_key,
566 * Schedule transmission for the given peer for the current round based
567 * on what we know about the desired delay.
570 * @param key hash of peer identity
571 * @param value the 'struct NSEPeerEntry'
572 * @return GNUNET_OK (continue to iterate)
575 schedule_current_round (void *cls,
576 const GNUNET_HashCode *key,
579 struct NSEPeerEntry *peer_entry = value;
580 struct GNUNET_TIME_Relative delay;
582 if (peer_entry->th != NULL)
584 peer_entry->previous_round = GNUNET_NO;
587 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
589 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
590 peer_entry->previous_round = GNUNET_NO;
592 delay = get_transmit_delay ((peer_entry->previous_round == GNUNET_NO) ? -1 : 0);
593 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (delay,
601 * Update our flood message to be sent (and our timestamps).
604 * @param tc context for this message
607 update_flood_message(void *cls,
608 const struct GNUNET_SCHEDULER_TaskContext *tc)
610 struct GNUNET_TIME_Relative offset;
613 flood_task = GNUNET_SCHEDULER_NO_TASK;
614 offset = GNUNET_TIME_absolute_get_remaining (next_timestamp);
615 if (0 != offset.rel_value)
617 /* somehow run early, delay more */
619 = GNUNET_SCHEDULER_add_delayed (offset,
620 &update_flood_message,
624 current_timestamp = next_timestamp;
625 next_timestamp = GNUNET_TIME_absolute_add (current_timestamp,
626 GNUNET_NSE_INTERVAL);
627 estimate_index = (estimate_index + 1) % HISTORY_SIZE;
628 setup_flood_message (estimate_index, current_timestamp);
630 for (i=0;i<HISTORY_SIZE;i++)
631 hop_count_max = GNUNET_MAX (ntohl (size_estimate_messages[i].hop_count),
633 GNUNET_CONTAINER_multihashmap_iterate (peers,
634 &schedule_current_round,
636 flood_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_timestamp),
637 &update_flood_message, NULL);
642 * Count the leading zeroes in hash.
645 * @return the number of leading zero bits.
648 count_leading_zeroes(const GNUNET_HashCode *hash)
650 unsigned int hash_count;
653 while ((0 == GNUNET_CRYPTO_hash_get_bit(hash, hash_count)))
660 * Check whether the given public key
661 * and integer are a valid proof of work.
663 * @param pkey the public key
664 * @param val the integer
666 * @return GNUNET_YES if valid, GNUNET_NO if not
669 check_proof_of_work(const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
672 char buf[sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sizeof(val)];
673 GNUNET_HashCode result;
678 memcpy (&buf[sizeof(val)],
680 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
681 GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
682 return (count_leading_zeroes (&result) >= NSE_WORK_REQUIRED) ? GNUNET_YES : GNUNET_NO;
687 * Given a public key, find an integer such that the hash of the key
688 * concatenated with the integer has NSE_WORK_REQUIRED leading 0
689 * bits. FIXME: this is a synchronous function... bad
691 * @param pkey the public key
692 * @return 64 bit number that satisfies the requirements
695 find_proof_of_work(const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey)
698 char buf[sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sizeof(uint64_t)];
699 GNUNET_HashCode result;
701 memcpy (&buf[sizeof(uint64_t)],
703 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
705 while (counter != UINT64_MAX)
710 GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
711 if (NSE_WORK_REQUIRED <= count_leading_zeroes(&result))
720 * An incoming flood message has been received which claims
721 * to have more bits matching than any we know in this time
722 * period. Verify the signature and/or proof of work.
724 * @param incoming_flood the message to verify
726 * @return GNUNET_YES if the message is verified
727 * GNUNET_NO if the key/signature don't verify
730 verify_message_crypto(const struct GNUNET_NSE_FloodMessage *incoming_flood)
733 check_proof_of_work (&incoming_flood->pkey,
734 incoming_flood->proof_of_work))
740 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NSE_SEND,
741 &incoming_flood->purpose,
742 &incoming_flood->signature,
743 &incoming_flood->pkey))
753 * Update transmissions for the given peer for the current round based
754 * on updated proximity information.
756 * @param cls peer entry to exclude from updates
757 * @param key hash of peer identity
758 * @param value the 'struct NSEPeerEntry'
759 * @return GNUNET_OK (continue to iterate)
762 update_flood_times (void *cls,
763 const GNUNET_HashCode *key,
766 struct NSEPeerEntry *exclude = cls;
767 struct NSEPeerEntry *peer_entry = value;
768 struct GNUNET_TIME_Relative delay;
770 if (peer_entry->th != NULL)
771 return GNUNET_OK; /* already active */
772 if (peer_entry == exclude)
773 return GNUNET_OK; /* trigger of the update */
774 if (peer_entry->previous_round == GNUNET_YES)
776 /* still stuck in previous round, no point to update, check that
777 we are active here though... */
778 GNUNET_break (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK);
781 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
783 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
784 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
786 delay = get_transmit_delay (0);
787 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (delay,
795 * Core handler for size estimate flooding messages.
797 * @param cls closure unused
798 * @param message message
799 * @param peer peer identity this message is from (ignored)
800 * @param atsi performance data (ignored)
804 handle_p2p_size_estimate(void *cls,
805 const struct GNUNET_PeerIdentity *peer,
806 const struct GNUNET_MessageHeader *message,
807 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
809 const struct GNUNET_NSE_FloodMessage *incoming_flood;
810 struct GNUNET_TIME_Absolute ts;
811 struct NSEPeerEntry *peer_entry;
812 uint32_t matching_bits;
815 incoming_flood = (const struct GNUNET_NSE_FloodMessage *) message;
816 GNUNET_STATISTICS_update (stats,
817 "# flood messages received",
820 matching_bits = ntohl (incoming_flood->matching_bits);
821 peer_entry = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
822 if (NULL == peer_entry)
827 ts = GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp);
828 if (ts.abs_value == current_timestamp.abs_value)
829 idx = estimate_index;
830 else if (ts.abs_value == current_timestamp.abs_value - GNUNET_NSE_INTERVAL.rel_value)
831 idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE;
832 else if (ts.abs_value == next_timestamp.abs_value - GNUNET_NSE_INTERVAL.rel_value)
835 verify_message_crypto (incoming_flood))
840 /* FIXME: keep in special 'future' buffer until next round starts for us! */
841 GNUNET_break (0); /* not implemented */
846 GNUNET_STATISTICS_update (stats,
847 "# flood messages discarded (clock skew too large)",
853 if (0 == (memcmp (peer, &my_identity, sizeof(struct GNUNET_PeerIdentity))))
855 /* send to self, update our own estimate IF this also comes from us! */
856 if (0 == memcmp (&incoming_flood->pkey,
858 sizeof (my_public_key)))
859 update_network_size_estimate ();
862 if (matching_bits >= ntohl (size_estimate_messages[idx].matching_bits))
864 /* cancel transmission from us to this peer for this round */
865 if (idx == estimate_index)
867 if (peer_entry->previous_round == GNUNET_NO)
869 /* cancel any activity for current round */
870 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
872 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
873 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
874 peer_entry->previous_round = GNUNET_NO;
876 if (peer_entry->th != NULL)
878 GNUNET_CORE_notify_transmit_ready_cancel (peer_entry->th);
879 peer_entry->th = NULL;
885 /* cancel previous round only */
886 peer_entry->previous_round = GNUNET_NO;
890 if (matching_bits <= ntohl (size_estimate_messages[idx].matching_bits))
892 /* Not closer than our most recent message, no need to do work here */
893 GNUNET_STATISTICS_update (stats,
894 "# flood messages ignored (had closer already)",
900 verify_message_crypto (incoming_flood))
905 size_estimate_messages[idx] = *incoming_flood;
906 size_estimate_messages[idx].hop_count = htonl (ntohl (incoming_flood->hop_count) + 1);
907 hop_count_max = GNUNET_MAX (ntohl (incoming_flood->hop_count) + 1,
910 /* have a new, better size estimate, inform clients */
911 update_network_size_estimate ();
914 GNUNET_CONTAINER_multihashmap_iterate (peers,
923 * Method called whenever a peer connects.
926 * @param peer peer identity this notification is about
927 * @param atsi performance data
930 handle_core_connect(void *cls, const struct GNUNET_PeerIdentity *peer,
931 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
933 struct NSEPeerEntry *peer_entry;
935 peer_entry = GNUNET_malloc(sizeof(struct NSEPeerEntry));
936 peer_entry->id = *peer;
937 GNUNET_CONTAINER_multihashmap_put (peers,
940 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
941 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1),
948 * Method called whenever a peer disconnects.
951 * @param peer peer identity this notification is about
954 handle_core_disconnect(void *cls, const struct GNUNET_PeerIdentity *peer)
956 struct NSEPeerEntry *pos;
958 pos = GNUNET_CONTAINER_multihashmap_get (peers,
965 GNUNET_assert (GNUNET_YES ==
966 GNUNET_CONTAINER_multihashmap_remove (peers,
969 if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK)
970 GNUNET_SCHEDULER_cancel (pos->transmit_task);
972 GNUNET_CORE_notify_transmit_ready_cancel (pos->th);
978 * Task run during shutdown.
984 shutdown_task(void *cls,
985 const struct GNUNET_SCHEDULER_TaskContext *tc)
987 if (flood_task != GNUNET_SCHEDULER_NO_TASK)
989 GNUNET_SCHEDULER_cancel (flood_task);
990 flood_task = GNUNET_SCHEDULER_NO_TASK;
994 GNUNET_SERVER_notification_context_destroy (nc);
999 GNUNET_CORE_disconnect (coreAPI);
1004 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1009 GNUNET_CONTAINER_multihashmap_destroy (peers);
1016 * Called on core init/fail.
1018 * @param cls service closure
1019 * @param server handle to the server for this service
1020 * @param identity the public identity of this peer
1021 * @param publicKey the public key of this peer
1024 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1025 const struct GNUNET_PeerIdentity *identity,
1026 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1028 struct GNUNET_TIME_Absolute now;
1029 struct GNUNET_TIME_Absolute prev_time;
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Connection to core FAILED!\n");
1038 GNUNET_SCHEDULER_shutdown ();
1041 my_identity = *identity;
1042 my_public_key = *publicKey;
1044 now = GNUNET_TIME_absolute_get ();
1045 current_timestamp.abs_value = (now.abs_value / GNUNET_NSE_INTERVAL.rel_value) * GNUNET_NSE_INTERVAL.rel_value;
1046 next_timestamp.abs_value = current_timestamp.abs_value + GNUNET_NSE_INTERVAL.rel_value;
1048 for (i=0;i<HISTORY_SIZE;i++)
1050 prev_time.abs_value = current_timestamp.abs_value - (HISTORY_SIZE - i - 1) * GNUNET_NSE_INTERVAL.rel_value;
1051 setup_flood_message (i, prev_time);
1053 estimate_index = HISTORY_SIZE - 1;
1055 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_timestamp),
1056 &update_flood_message, NULL);
1057 my_proof = find_proof_of_work (&my_public_key);
1062 * Handle network size estimate clients.
1064 * @param cls closure
1065 * @param server the initialized server
1066 * @param c configuration to use
1069 run(void *cls, struct GNUNET_SERVER_Handle *server,
1070 const struct GNUNET_CONFIGURATION_Handle *c)
1074 static const struct GNUNET_SERVER_MessageHandler handlers[] =
1076 { &handle_start_message, NULL, GNUNET_MESSAGE_TYPE_NSE_START, sizeof (struct GNUNET_MessageHeader) },
1077 { NULL, NULL, 0, 0 }
1079 static const struct GNUNET_CORE_MessageHandler core_handlers[] =
1081 { &handle_p2p_size_estimate, GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD, sizeof (struct GNUNET_NSE_FloodMessage) },
1086 GNUNET_CONFIGURATION_get_value_filename (cfg,
1087 "GNUNETD", "HOSTKEY",
1090 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1091 _ ("NSE service is lacking key configuration settings. Exiting.\n"));
1092 GNUNET_SCHEDULER_shutdown ();
1095 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1096 GNUNET_free (keyfile);
1097 if (my_private_key == NULL)
1099 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1100 _("NSE service could not access hostkey. Exiting.\n"));
1101 GNUNET_SCHEDULER_shutdown ();
1104 peers = GNUNET_CONTAINER_multihashmap_create (128);
1105 GNUNET_SERVER_add_handlers (server, handlers);
1106 nc = GNUNET_SERVER_notification_context_create (server, 1);
1107 /* Connect to core service and register core handlers */
1108 coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */
1109 CORE_QUEUE_SIZE, /* queue size */
1110 NULL, /* Closure passed to functions */
1111 &core_init, /* Call core_init once connected */
1112 &handle_core_connect, /* Handle connects */
1113 &handle_core_disconnect, /* Handle disconnects */
1114 NULL, /* Do we care about "status" updates? */
1115 NULL, /* Don't want notified about all incoming messages */
1116 GNUNET_NO, /* For header only inbound notification */
1117 NULL, /* Don't want notified about all outbound messages */
1118 GNUNET_NO, /* For header only outbound notification */
1119 core_handlers); /* Register these handlers */
1120 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1121 &shutdown_task, NULL);
1122 if (coreAPI == NULL)
1124 GNUNET_SCHEDULER_shutdown ();
1127 stats = GNUNET_STATISTICS_create ("NSE", cfg);
1132 * The main function for the statistics service.
1134 * @param argc number of arguments from the command line
1135 * @param argv command line arguments
1136 * @return 0 ok, 1 on error
1139 main(int argc, char * const *argv)
1141 return (GNUNET_OK == GNUNET_SERVICE_run (argc, argv,
1143 GNUNET_SERVICE_OPTION_NONE, &run,
1147 /* end of gnunet-service-nse.c */