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_client_lib.h"
44 #include "gnunet_constants.h"
45 #include "gnunet_container_lib.h"
46 #include "gnunet_protocols.h"
47 #include "gnunet_signatures.h"
48 #include "gnunet_service_lib.h"
49 #include "gnunet_server_lib.h"
50 #include "gnunet_statistics_service.h"
51 #include "gnunet_core_service.h"
52 #include "gnunet_time_lib.h"
53 #include "gnunet_nse_service.h"
57 * Over how many values do we calculate the weighted average?
59 #define HISTORY_SIZE 8
62 * Size of the queue to core.
64 #define CORE_QUEUE_SIZE 2
67 * Message priority to use.
69 #define NSE_PRIORITY 5
72 * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
74 #define NSE_WORK_REQUIRED 0
77 * Interval for sending network size estimation flood requests.
79 #define GNUNET_NSE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
83 * Per-peer information.
89 * Pending message for this peer.
91 struct GNUNET_MessageHeader *pending_message;
94 * Core handle for sending messages to this peer.
96 struct GNUNET_CORE_TransmitHandle *th;
99 * What is the identity of the peer?
101 struct GNUNET_PeerIdentity id;
104 * Task scheduled to send message to this peer.
106 GNUNET_SCHEDULER_TaskIdentifier transmit_task;
109 * Did we receive or send a message about the previous round
117 * Network size estimate reply; sent when "this"
118 * peer's timer has run out before receiving a
119 * valid reply from another peer.
121 struct GNUNET_NSE_FloodMessage
124 * Type: GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD
126 struct GNUNET_MessageHeader header;
129 * Number of hops this message has taken so far.
136 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
139 * The current timestamp value (which all
140 * peers should agree on).
142 struct GNUNET_TIME_AbsoluteNBO timestamp;
145 * Number of matching bits between the hash
146 * of timestamp and the initiator's public
149 uint32_t matching_bits;
152 * Public key of the originator.
154 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
157 * Proof of work, causing leading zeros when hashed with pkey.
159 uint64_t proof_of_work;
162 * Signature (over range specified in purpose).
164 struct GNUNET_CRYPTO_RsaSignature signature;
169 * Handle to our current configuration.
171 static const struct GNUNET_CONFIGURATION_Handle *cfg;
174 * Handle to the statistics service.
176 static struct GNUNET_STATISTICS_Handle *stats;
179 * Handle to the core service.
181 static struct GNUNET_CORE_Handle *coreAPI;
184 * Map of all connected peers.
186 static struct GNUNET_CONTAINER_MultiHashMap *peers;
189 * The current network size estimate. Number of bits matching on
192 static double current_size_estimate;
195 * The standard deviation of the last HISTORY_SIZE network
198 static double current_std_dev = NAN;
201 * Current hop counter estimate (estimate for network diameter).
203 static uint32_t hop_count_max;
206 * Array of recent size estimate messages.
208 static struct GNUNET_NSE_FloodMessage size_estimate_messages[HISTORY_SIZE];
211 * Index of most recent estimate.
213 static unsigned int estimate_index;
216 * Task scheduled to update our flood message for the next round.
218 static GNUNET_SCHEDULER_TaskIdentifier flood_task;
221 * Notification context, simplifies client broadcasts.
223 static struct GNUNET_SERVER_NotificationContext *nc;
226 * The next major time.
228 static struct GNUNET_TIME_Absolute next_timestamp;
231 * The current major time.
233 static struct GNUNET_TIME_Absolute current_timestamp;
236 * The public key of this peer.
238 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
241 * The private key of this peer.
243 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
246 * The peer identity of this peer.
248 static struct GNUNET_PeerIdentity my_identity;
251 * Proof of work for this peer.
253 static uint64_t my_proof;
257 * Initialize a message to clients with the current network
260 * @param em message to fill in
263 setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
277 /* Weighted incremental algorithm for stddev according to West (1979) */
281 for (i=0; i<HISTORY_SIZE; i++)
283 val = htonl (size_estimate_messages[i].matching_bits);
284 weight = HISTORY_SIZE - ((estimate_index + HISTORY_SIZE - i) % HISTORY_SIZE);
286 temp = weight + sumweight;
288 r = q * weight / temp;
289 sum += sumweight * q * r;
293 variance = sum / (sumweight - 1.0);
294 GNUNET_assert (variance >= 0);
295 std_dev = sqrt (variance);
296 current_std_dev = std_dev;
297 current_size_estimate = mean;
300 = htons (sizeof(struct GNUNET_NSE_ClientMessage));
302 = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
303 em->reserved = htonl (0);
304 em->size_estimate = mean - 0.5;
305 em->std_deviation = std_dev;
306 GNUNET_STATISTICS_set (stats,
307 "Current network size estimate",
308 (uint64_t) pow (2, mean - 0.5), GNUNET_NO);
313 * Handler for START message from client, triggers an
314 * immediate current network estimate notification.
315 * Also, we remember the client for updates upon future
316 * estimate measurements.
319 * @param client who sent the message
320 * @param message the message received
323 handle_start_message(void *cls, struct GNUNET_SERVER_Client *client,
324 const struct GNUNET_MessageHeader *message)
326 struct GNUNET_NSE_ClientMessage em;
328 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
329 "Received START message from client\n");
331 GNUNET_SERVER_notification_context_add (nc, client);
332 setup_estimate_message (&em);
333 GNUNET_SERVER_notification_context_unicast (nc, client, &em.header, GNUNET_YES);
334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
339 * How long should we delay a message to go the given number of
342 * @param matching_bits number of matching bits to consider
345 get_matching_bits_delay (uint32_t matching_bits)
347 /* Calculated as: S + f/2 - (f / pi) * (atan(x - p'))*/
348 // S is next_timestamp
349 // f is frequency (GNUNET_NSE_INTERVAL)
350 // x is matching_bits
351 // p' is current_size_estimate
352 return ((double) GNUNET_NSE_INTERVAL.rel_value / (double) 2)
353 - ((GNUNET_NSE_INTERVAL.rel_value / M_PI) * atan (matching_bits - current_size_estimate));
358 * What delay randomization should we apply for a given number of matching bits?
360 * @param matching_bits number of matching bits
361 * @return random delay to apply
363 static struct GNUNET_TIME_Relative
364 get_delay_randomization (uint32_t matching_bits)
366 struct GNUNET_TIME_Relative ret;
368 if (matching_bits == 0)
369 return GNUNET_TIME_UNIT_ZERO;
370 ret.rel_value = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
371 (uint32_t) (get_matching_bits_delay (matching_bits - 1) / (double) (hop_count_max + 1)));
377 * Get the number of matching bits that the given timestamp has to the given peer ID.
379 * @param timestamp time to generate key
380 * @param id peer identity to compare with
381 * @return number of matching bits
384 get_matching_bits (struct GNUNET_TIME_Absolute timestamp,
385 const struct GNUNET_PeerIdentity *id)
387 GNUNET_HashCode timestamp_hash;
389 GNUNET_CRYPTO_hash (×tamp.abs_value,
390 sizeof(timestamp.abs_value),
392 return GNUNET_CRYPTO_hash_matching_bits (×tamp_hash,
398 * Get the transmission delay that should be applied for a
401 * @param round_offset -1 for the previous round (random delay between 0 and 50ms)
402 * 0 for the current round (based on our proximity to time key)
403 * @return delay that should be applied
405 static struct GNUNET_TIME_Relative
406 get_transmit_delay (int round_offset)
408 struct GNUNET_TIME_Relative ret;
409 struct GNUNET_TIME_Absolute tgt;
411 uint32_t matching_bits;
413 switch (round_offset)
416 /* previous round is randomized between 0 and 50 ms */
417 ret.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
421 /* current round is based on best-known matching_bits */
422 matching_bits = ntohl (size_estimate_messages[estimate_index].matching_bits);
423 dist_delay = get_matching_bits_delay (matching_bits);
424 dist_delay += get_delay_randomization (matching_bits).rel_value;
425 ret.rel_value = (uint64_t) dist_delay;
426 /* now consider round start time and add delay to it */
427 tgt = GNUNET_TIME_absolute_add (current_timestamp, ret);
428 return GNUNET_TIME_absolute_get_remaining (tgt);
431 return GNUNET_TIME_UNIT_FOREVER_REL;
436 * Task that triggers a NSE P2P transmission.
438 * @param cls the 'struct NSEPeerEntry'
439 * @param tc scheduler context
442 transmit_task (void *cls,
443 const struct GNUNET_SCHEDULER_TaskContext *tc);
447 * Called when core is ready to send a message we asked for
448 * out to the destination.
450 * @param cls closure (NULL)
451 * @param size number of bytes available in buf
452 * @param buf where the callee should write the message
453 * @return number of bytes written to buf
456 transmit_ready (void *cls, size_t size, void *buf)
458 struct NSEPeerEntry *peer_entry = cls;
461 peer_entry->th = NULL;
464 /* client disconnected */
467 GNUNET_assert (size >= sizeof (struct GNUNET_NSE_FloodMessage));
469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
470 "Sending size estimate to `%s'\n",
471 GNUNET_i2s (&peer_entry->id));
473 idx = estimate_index;
474 if (peer_entry->previous_round == GNUNET_YES)
476 idx = (idx + HISTORY_SIZE -1) % HISTORY_SIZE;
477 peer_entry->previous_round = GNUNET_NO;
478 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (get_transmit_delay (0),
483 &size_estimate_messages[idx],
484 sizeof (struct GNUNET_NSE_FloodMessage));
485 GNUNET_STATISTICS_update (stats,
486 "# flood messages sent",
489 return sizeof (struct GNUNET_NSE_FloodMessage);
494 * Task that triggers a NSE P2P transmission.
496 * @param cls the 'struct NSEPeerEntry'
497 * @param tc scheduler context
500 transmit_task (void *cls,
501 const struct GNUNET_SCHEDULER_TaskContext *tc)
503 struct NSEPeerEntry *peer_entry = cls;
505 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
507 = GNUNET_CORE_notify_transmit_ready (coreAPI,
510 GNUNET_TIME_UNIT_FOREVER_REL,
512 sizeof (struct GNUNET_NSE_FloodMessage),
513 &transmit_ready, peer_entry);
518 * We've sent on our flood message or one that we received which was
519 * validated and closer than ours. Update the global list of recent
520 * messages and the average. Also re-broadcast the message to any
524 update_network_size_estimate ()
526 struct GNUNET_NSE_ClientMessage em;
528 setup_estimate_message (&em);
529 GNUNET_SERVER_notification_context_broadcast (nc,
536 * Setup a flood message in our history array at the given
537 * slot offset for the given timestamp.
539 * @param slot index to use
540 * @param ts timestamp to use
543 setup_flood_message (unsigned int slot,
544 struct GNUNET_TIME_Absolute ts)
546 struct GNUNET_NSE_FloodMessage *fm;
547 uint32_t matching_bits;
549 matching_bits = get_matching_bits (ts, &my_identity);
550 fm = &size_estimate_messages[slot];
551 fm->header.size = htons (sizeof(struct GNUNET_NSE_FloodMessage));
552 fm->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD);
553 fm->hop_count = htonl (0);
554 fm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_NSE_SEND);
555 fm->purpose.size = htonl (sizeof(struct GNUNET_NSE_FloodMessage)
556 - sizeof (struct GNUNET_MessageHeader)
558 - sizeof (struct GNUNET_CRYPTO_RsaSignature));
559 fm->matching_bits = htonl (matching_bits);
560 fm->timestamp = GNUNET_TIME_absolute_hton (ts);
561 fm->pkey = my_public_key;
562 fm->proof_of_work = my_proof;
563 GNUNET_CRYPTO_rsa_sign (my_private_key,
570 * Schedule transmission for the given peer for the current round based
571 * on what we know about the desired delay.
574 * @param key hash of peer identity
575 * @param value the 'struct NSEPeerEntry'
576 * @return GNUNET_OK (continue to iterate)
579 schedule_current_round (void *cls,
580 const GNUNET_HashCode *key,
583 struct NSEPeerEntry *peer_entry = value;
584 struct GNUNET_TIME_Relative delay;
586 if (peer_entry->th != NULL)
588 peer_entry->previous_round = GNUNET_NO;
591 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
593 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
594 peer_entry->previous_round = GNUNET_NO;
596 delay = get_transmit_delay ((peer_entry->previous_round == GNUNET_NO) ? -1 : 0);
597 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (delay,
605 * Update our flood message to be sent (and our timestamps).
608 * @param tc context for this message
611 update_flood_message(void *cls,
612 const struct GNUNET_SCHEDULER_TaskContext *tc)
614 struct GNUNET_TIME_Relative offset;
617 flood_task = GNUNET_SCHEDULER_NO_TASK;
618 offset = GNUNET_TIME_absolute_get_remaining (next_timestamp);
619 if (0 != offset.rel_value)
621 /* somehow run early, delay more */
623 = GNUNET_SCHEDULER_add_delayed (offset,
624 &update_flood_message,
628 current_timestamp = next_timestamp;
629 next_timestamp = GNUNET_TIME_absolute_add (current_timestamp,
630 GNUNET_NSE_INTERVAL);
631 estimate_index = (estimate_index + 1) % HISTORY_SIZE;
632 setup_flood_message (estimate_index, current_timestamp);
634 for (i=0;i<HISTORY_SIZE;i++)
635 hop_count_max = GNUNET_MAX (ntohl (size_estimate_messages[i].hop_count),
637 GNUNET_CONTAINER_multihashmap_iterate (peers,
638 &schedule_current_round,
640 flood_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_timestamp),
641 &update_flood_message, NULL);
646 * Count the trailing zeroes in hash.
649 * @return the number of trailing zero bits.
652 count_trailing_zeroes(const GNUNET_HashCode *hash)
654 unsigned int hash_count;
656 hash_count = sizeof(GNUNET_HashCode) * 8;
657 while ((0 == GNUNET_CRYPTO_hash_get_bit(hash, hash_count)))
659 return (sizeof(GNUNET_HashCode) * 8) - hash_count;
664 * Check whether the given public key
665 * and integer are a valid proof of work.
667 * @param pkey the public key
668 * @param val the integer
670 * @return GNUNET_YES if valid, GNUNET_NO if not
673 check_proof_of_work(const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
676 char buf[sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sizeof(val)];
677 GNUNET_HashCode result;
682 memcpy (&buf[sizeof(val)],
684 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
685 GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
686 return (count_trailing_zeroes (&result) >= NSE_WORK_REQUIRED) ? GNUNET_YES : GNUNET_NO;
691 * Given a public key, find an integer such that the hash of the key
692 * concatenated with the integer has NSE_WORK_REQUIRED trailing 0
693 * bits. FIXME: this is a synchronous function... bad
695 * @param pkey the public key
696 * @return 64 bit number that satisfies the requirements
699 find_proof_of_work(const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey)
702 char buf[sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sizeof(uint64_t)];
703 GNUNET_HashCode result;
705 memcpy (&buf[sizeof(uint64_t)],
707 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
709 while (counter != UINT64_MAX)
714 GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
715 if (NSE_WORK_REQUIRED <= count_trailing_zeroes(&result))
724 * An incoming flood message has been received which claims
725 * to have more bits matching than any we know in this time
726 * period. Verify the signature and/or proof of work.
728 * @param incoming_flood the message to verify
730 * @return GNUNET_YES if the message is verified
731 * GNUNET_NO if the key/signature don't verify
734 verify_message_crypto(const struct GNUNET_NSE_FloodMessage *incoming_flood)
737 check_proof_of_work (&incoming_flood->pkey,
738 incoming_flood->proof_of_work))
744 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NSE_SEND,
745 &incoming_flood->purpose,
746 &incoming_flood->signature,
747 &incoming_flood->pkey))
757 * Update transmissions for the given peer for the current round based
758 * on updated proximity information.
760 * @param cls peer entry to exclude from updates
761 * @param key hash of peer identity
762 * @param value the 'struct NSEPeerEntry'
763 * @return GNUNET_OK (continue to iterate)
766 update_flood_times (void *cls,
767 const GNUNET_HashCode *key,
770 struct NSEPeerEntry *exclude = cls;
771 struct NSEPeerEntry *peer_entry = value;
772 struct GNUNET_TIME_Relative delay;
774 if (peer_entry->th != NULL)
775 return GNUNET_OK; /* already active */
776 if (peer_entry == exclude)
777 return GNUNET_OK; /* trigger of the update */
778 if (peer_entry->previous_round == GNUNET_YES)
780 /* still stuck in previous round, no point to update, check that
781 we are active here though... */
782 GNUNET_break (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK);
785 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
787 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
788 peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
790 delay = get_transmit_delay (0);
791 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (delay,
799 * Core handler for size estimate flooding messages.
801 * @param cls closure unused
802 * @param message message
803 * @param peer peer identity this message is from (ignored)
804 * @param atsi performance data (ignored)
808 handle_p2p_size_estimate(void *cls,
809 const struct GNUNET_PeerIdentity *peer,
810 const struct GNUNET_MessageHeader *message,
811 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
813 const struct GNUNET_NSE_FloodMessage *incoming_flood;
814 struct GNUNET_TIME_Absolute ts;
815 struct NSEPeerEntry *peer_entry;
816 uint32_t matching_bits;
819 incoming_flood = (const struct GNUNET_NSE_FloodMessage *) message;
820 GNUNET_STATISTICS_update (stats,
821 "# flood messages received",
824 matching_bits = ntohl (incoming_flood->matching_bits);
825 peer_entry = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
826 if (NULL == peer_entry)
831 ts = GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp);
832 if (ts.abs_value == current_timestamp.abs_value)
833 idx = estimate_index;
834 else if (ts.abs_value == current_timestamp.abs_value - GNUNET_NSE_INTERVAL.rel_value)
835 idx = (estimate_index + HISTORY_SIZE - 1) % HISTORY_SIZE;
836 else if (ts.abs_value == next_timestamp.abs_value - GNUNET_NSE_INTERVAL.rel_value)
839 verify_message_crypto (incoming_flood))
844 /* FIXME: keep in special 'future' buffer until next round starts for us! */
845 GNUNET_break (0); /* not implemented */
850 GNUNET_STATISTICS_update (stats,
851 "# flood messages discarded (clock skew too large)",
857 if (0 == (memcmp (peer, &my_identity, sizeof(struct GNUNET_PeerIdentity))))
859 /* send to self, update our own estimate IF this also comes from us! */
860 if (0 == memcmp (&incoming_flood->pkey,
862 sizeof (my_public_key)))
863 update_network_size_estimate ();
866 if (matching_bits >= ntohl (size_estimate_messages[idx].matching_bits))
868 /* cancel transmission from us to this peer for this round */
869 if (idx == estimate_index)
871 if (peer_entry->previous_round == GNUNET_NO)
873 /* cancel any activity for current round */
874 if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
876 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
877 peer_entry->previous_round = GNUNET_NO;
879 if (peer_entry->th != NULL)
881 GNUNET_CORE_notify_transmit_ready_cancel (peer_entry->th);
882 peer_entry->th = NULL;
888 /* cancel previous round only */
889 peer_entry->previous_round = GNUNET_NO;
893 if (matching_bits <= ntohl (size_estimate_messages[idx].matching_bits))
895 /* Not closer than our most recent message, no need to do work here */
896 GNUNET_STATISTICS_update (stats,
897 "# flood messages ignored (had closer already)",
903 verify_message_crypto (incoming_flood))
908 size_estimate_messages[idx] = *incoming_flood;
909 size_estimate_messages[idx].hop_count = htonl (ntohl (incoming_flood->hop_count) + 1);
910 hop_count_max = GNUNET_MAX (ntohl (incoming_flood->hop_count) + 1,
913 /* have a new, better size estimate, inform clients */
914 update_network_size_estimate ();
917 GNUNET_CONTAINER_multihashmap_iterate (peers,
926 * Method called whenever a peer connects.
929 * @param peer peer identity this notification is about
930 * @param atsi performance data
933 handle_core_connect(void *cls, const struct GNUNET_PeerIdentity *peer,
934 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
936 struct NSEPeerEntry *peer_entry;
938 peer_entry = GNUNET_malloc(sizeof(struct NSEPeerEntry));
939 peer_entry->id = *peer;
940 GNUNET_CONTAINER_multihashmap_put (peers,
943 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
944 peer_entry->transmit_task = GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1),
951 * Method called whenever a peer disconnects.
954 * @param peer peer identity this notification is about
957 handle_core_disconnect(void *cls, const struct GNUNET_PeerIdentity *peer)
959 struct NSEPeerEntry *pos;
961 pos = GNUNET_CONTAINER_multihashmap_get (peers,
968 GNUNET_assert (GNUNET_YES ==
969 GNUNET_CONTAINER_multihashmap_remove (peers,
972 if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK)
973 GNUNET_SCHEDULER_cancel (pos->transmit_task);
975 GNUNET_CORE_notify_transmit_ready_cancel (pos->th);
981 * Task run during shutdown.
987 shutdown_task(void *cls,
988 const struct GNUNET_SCHEDULER_TaskContext *tc)
990 if (flood_task != GNUNET_SCHEDULER_NO_TASK)
992 GNUNET_SCHEDULER_cancel (flood_task);
993 flood_task = GNUNET_SCHEDULER_NO_TASK;
995 GNUNET_SERVER_notification_context_destroy (nc);
999 GNUNET_CORE_disconnect (coreAPI);
1004 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1011 * Called on core init/fail.
1013 * @param cls service closure
1014 * @param server handle to the server for this service
1015 * @param identity the public identity of this peer
1016 * @param publicKey the public key of this peer
1019 core_init(void *cls, struct GNUNET_CORE_Handle *server,
1020 const struct GNUNET_PeerIdentity *identity,
1021 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1023 struct GNUNET_TIME_Absolute now;
1024 struct GNUNET_TIME_Absolute prev_time;
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Connection to core FAILED!\n");
1033 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1036 my_identity = *identity;
1037 my_public_key = *publicKey;
1039 now = GNUNET_TIME_absolute_get ();
1040 current_timestamp.abs_value = (now.abs_value / GNUNET_NSE_INTERVAL.rel_value) * GNUNET_NSE_INTERVAL.rel_value;
1041 next_timestamp.abs_value = current_timestamp.abs_value + GNUNET_NSE_INTERVAL.rel_value;
1043 for (i=0;i<HISTORY_SIZE;i++)
1045 prev_time.abs_value = next_timestamp.abs_value - (HISTORY_SIZE - i - 1) * GNUNET_NSE_INTERVAL.rel_value;
1046 setup_flood_message (i, prev_time);
1048 estimate_index = HISTORY_SIZE - 1;
1050 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_timestamp),
1051 &update_flood_message, NULL);
1052 my_proof = find_proof_of_work (&my_public_key);
1057 * Handle network size estimate clients.
1059 * @param cls closure
1060 * @param server the initialized server
1061 * @param c configuration to use
1064 run(void *cls, struct GNUNET_SERVER_Handle *server,
1065 const struct GNUNET_CONFIGURATION_Handle *c)
1069 static const struct GNUNET_SERVER_MessageHandler handlers[] =
1071 { &handle_start_message, NULL, GNUNET_MESSAGE_TYPE_NSE_START, sizeof (struct GNUNET_MessageHeader) },
1072 { NULL, NULL, 0, 0 }
1074 static const struct GNUNET_CORE_MessageHandler core_handlers[] =
1076 { &handle_p2p_size_estimate, GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD, sizeof (struct GNUNET_NSE_FloodMessage) },
1081 GNUNET_CONFIGURATION_get_value_filename (cfg,
1082 "GNUNETD", "HOSTKEY",
1085 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1086 _ ("NSE service is lacking key configuration settings. Exiting.\n"));
1087 GNUNET_SCHEDULER_shutdown ();
1090 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1091 GNUNET_free (keyfile);
1092 if (my_private_key == NULL)
1094 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1095 _("NSE service could not access hostkey. Exiting.\n"));
1096 GNUNET_SCHEDULER_shutdown ();
1099 GNUNET_SERVER_add_handlers (server, handlers);
1100 nc = GNUNET_SERVER_notification_context_create (server, 1);
1101 /* Connect to core service and register core handlers */
1102 coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */
1103 CORE_QUEUE_SIZE, /* queue size */
1104 NULL, /* Closure passed to functions */
1105 &core_init, /* Call core_init once connected */
1106 &handle_core_connect, /* Handle connects */
1107 &handle_core_disconnect, /* Handle disconnects */
1108 NULL, /* Do we care about "status" updates? */
1109 NULL, /* Don't want notified about all incoming messages */
1110 GNUNET_NO, /* For header only inbound notification */
1111 NULL, /* Don't want notified about all outbound messages */
1112 GNUNET_NO, /* For header only outbound notification */
1113 core_handlers); /* Register these handlers */
1114 if (coreAPI == NULL)
1116 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1119 stats = GNUNET_STATISTICS_create ("NSE", cfg);
1124 * The main function for the statistics service.
1126 * @param argc number of arguments from the command line
1127 * @param argv command line arguments
1128 * @return 0 ok, 1 on error
1131 main(int argc, char * const *argv)
1133 return (GNUNET_OK == GNUNET_SERVICE_run (argc, argv,
1134 "gnunet-service-nse",
1135 GNUNET_SERVICE_OPTION_NONE, &run,
1139 /* end of gnunet-service-nse.c */