/*
This file is part of GNUnet.
- (C) 2009, 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
+ (C) 2009, 2010, 2011, 2012, 2013 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
#include "gnunet_statistics_service.h"
#include "gnunet_core_service.h"
#include "gnunet_nse_service.h"
+#if ENABLE_NSE_HISTOGRAM
+#include "gnunet_testbed_logger_service.h"
+#endif
#include "nse.h"
+#include <gcrypt.h>
+
/**
* Should messages be delayed randomly? This option should be set to
#define USE_RANDOM_DELAYS GNUNET_YES
/**
- * Should we generate a histogram with the time stamps of when we received
- * NSE messages to disk? (for performance evaluation only, not useful in
- * production). The associated code should also probably be removed
- * once we're done with experiments.
+ * Generate extensive debug-level log messages?
*/
-#define ENABLE_HISTOGRAM GNUNET_NO
+#define DEBUG_NSE GNUNET_NO
/**
* Over how many values do we calculate the weighted average?
*/
static struct GNUNET_TIME_Relative proof_find_delay;
-#if ENABLE_HISTOGRAM
+#if ENABLE_NSE_HISTOGRAM
/**
* Handle for writing when we received messages to disk.
*/
-static struct GNUNET_BIO_WriteHandle *wh;
+static struct GNUNET_TESTBED_LOGGER_Handle *lh;
#endif
*/
int previous_round;
-#if ENABLE_HISTOGRAM
+#if ENABLE_NSE_HISTOGRAM
/**
* Amount of messages received from this peer on this round.
/**
* Purpose.
*/
- struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
+ struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
/**
* The current timestamp value (which all
/**
* Public key of the originator.
*/
- struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
+ struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
/**
* Proof of work, causing leading zeros when hashed with pkey.
/**
* Signature (over range specified in purpose).
*/
- struct GNUNET_CRYPTO_RsaSignature signature;
+ struct GNUNET_CRYPTO_EccSignature signature;
};
GNUNET_NETWORK_STRUCT_END
/**
* The public key of this peer.
*/
-static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
+static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
/**
* The private key of this peer.
*/
-static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
+static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
/**
* The peer identity of this peer.
*/
static struct GNUNET_SERVER_Handle *srv;
-/**
- * Hostkey generation context
- */
-static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
-
/**
* Initialize a message to clients with the current network
}
+/**
+ * Calculate the 'proof-of-work' hash (an expensive hash).
+ *
+ * @param buf data to hash
+ * @param buf_len number of bytes in 'buf'
+ * @param result where to write the resulting hash
+ */
+static void
+pow_hash (const void *buf,
+ size_t buf_len,
+ struct GNUNET_HashCode *result)
+{
+ GNUNET_break (0 ==
+ gcry_kdf_derive (buf, buf_len,
+ GCRY_KDF_SCRYPT,
+ 1 /* subalgo */,
+ "gnunet-proof-of-work", strlen ("gnunet-proof-of-work"),
+ 2 /* iterations; keep cost of individual op small */,
+ sizeof (struct GNUNET_HashCode), result));
+}
+
+
/**
* Get the number of matching bits that the given timestamp has to the given peer ID.
*
GNUNET_STATISTICS_update (stats, "# flood messages started", 1, GNUNET_NO);
GNUNET_STATISTICS_update (stats, "# flood messages transmitted", 1,
GNUNET_NO);
-#if ENABLE_HISTOGRAM
+#if ENABLE_NSE_HISTOGRAM
peer_entry->transmitted_messages++;
peer_entry->last_transmitted_size =
ntohl(size_estimate_messages[idx].matching_bits);
fm->purpose.size =
htonl (sizeof (struct GNUNET_NSE_FloodMessage) -
sizeof (struct GNUNET_MessageHeader) - sizeof (uint32_t) -
- sizeof (struct GNUNET_CRYPTO_RsaSignature));
+ sizeof (struct GNUNET_CRYPTO_EccSignature));
fm->matching_bits = htonl (matching_bits);
fm->timestamp = GNUNET_TIME_absolute_hton (ts);
fm->pkey = my_public_key;
fm->proof_of_work = my_proof;
if (nse_work_required > 0)
GNUNET_assert (GNUNET_OK ==
- GNUNET_CRYPTO_rsa_sign (my_private_key, &fm->purpose,
+ GNUNET_CRYPTO_ecc_sign (my_private_key, &fm->purpose,
&fm->signature));
else
memset (&fm->signature, 0, sizeof (fm->signature));
GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
peer_entry->previous_round = GNUNET_NO;
}
-#if ENABLE_HISTOGRAM
+#if ENABLE_NSE_HISTOGRAM
if (peer_entry->received_messages > 1)
GNUNET_STATISTICS_update(stats, "# extra messages",
peer_entry->received_messages - 1, GNUNET_NO);
* @return GNUNET_YES if valid, GNUNET_NO if not
*/
static int
-check_proof_of_work (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey,
+check_proof_of_work (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pkey,
uint64_t val)
{
- char buf[sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
+ char buf[sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded) +
sizeof (val)] GNUNET_ALIGN;
struct GNUNET_HashCode result;
memcpy (buf, &val, sizeof (val));
memcpy (&buf[sizeof (val)], pkey,
- sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
- GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
+ sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded));
+ pow_hash (buf, sizeof (buf), &result);
return (count_leading_zeroes (&result) >=
nse_work_required) ? GNUNET_YES : GNUNET_NO;
}
{
#define ROUND_SIZE 10
uint64_t counter;
- char buf[sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
+ char buf[sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded) +
sizeof (uint64_t)] GNUNET_ALIGN;
struct GNUNET_HashCode result;
unsigned int i;
proof_task = GNUNET_SCHEDULER_NO_TASK;
memcpy (&buf[sizeof (uint64_t)], &my_public_key,
- sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
+ sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded));
i = 0;
counter = my_proof;
while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
{
memcpy (buf, &counter, sizeof (uint64_t));
- GNUNET_CRYPTO_hash (buf, sizeof (buf), &result);
+ pow_hash (buf, sizeof (buf), &result);
if (nse_work_required <= count_leading_zeroes (&result))
{
my_proof = counter;
check_proof_of_work (&incoming_flood->pkey,
incoming_flood->proof_of_work))
{
- GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Proof of work invalid: %llu!\n"),
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Proof of work invalid: %llu!\n",
(unsigned long long)
GNUNET_ntohll (incoming_flood->proof_of_work));
GNUNET_break_op (0);
}
if ((nse_work_required > 0) &&
(GNUNET_OK !=
- GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NSE_SEND,
+ GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_NSE_SEND,
&incoming_flood->purpose,
&incoming_flood->signature,
&incoming_flood->pkey)))
* @param cls closure unused
* @param message message
* @param peer peer identity this message is from (ignored)
- * @param atsi performance data (ignored)
- * @param atsi_count number of records in 'atsi'
*/
static int
handle_p2p_size_estimate (void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message,
- const struct GNUNET_ATS_Information *atsi,
- unsigned int atsi_count)
+ const struct GNUNET_MessageHeader *message)
{
const struct GNUNET_NSE_FloodMessage *incoming_flood;
struct GNUNET_TIME_Absolute ts;
uint32_t matching_bits;
unsigned int idx;
-#if ENABLE_HISTOGRAM
- if (NULL != wh)
- GNUNET_break (GNUNET_OK == GNUNET_BIO_write_int64 (wh, GNUNET_TIME_absolute_get ().abs_value));
+#if ENABLE_NSE_HISTOGRAM
+ {
+ uint64_t t;
+
+ t = GNUNET_TIME_absolute_get().abs_value;
+ GNUNET_TESTBED_LOGGER_write (lh, &t, sizeof (uint64_t));
+ }
#endif
incoming_flood = (const struct GNUNET_NSE_FloodMessage *) message;
GNUNET_STATISTICS_update (stats, "# flood messages received", 1, GNUNET_NO);
struct GNUNET_PeerIdentity os;
GNUNET_CRYPTO_hash (&incoming_flood->pkey,
- sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
+ sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
&os.hashPubKey);
GNUNET_snprintf (origin, sizeof (origin), "%s", GNUNET_i2s (&os));
GNUNET_snprintf (pred, sizeof (pred), "%s", GNUNET_i2s (peer));
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Flood at %llu from `%s' via `%s' at `%s' with bits %u\n",
- (unsigned long long)
- GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp).abs_value,
+ "Flood at %s from `%s' via `%s' at `%s' with bits %u\n",
+ GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp)),
origin, pred, GNUNET_i2s (&my_identity),
(unsigned int) matching_bits);
}
GNUNET_break (0);
return GNUNET_OK;
}
-#if ENABLE_HISTOGRAM
+#if ENABLE_NSE_HISTOGRAM
peer_entry->received_messages++;
if (peer_entry->transmitted_messages > 0 &&
peer_entry->last_transmitted_size >= matching_bits)
*
* @param cls closure
* @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
*/
static void
-handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_ATS_Information *atsi,
- unsigned int atsi_count)
+handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
{
struct NSEPeerEntry *peer_entry;
}
+#if ENABLE_NSE_HISTOGRAM
+/**
+ * Functions of this type are called to notify a successful transmission of the
+ * message to the logger service
+ *
+ * @param cls NULL
+ * @param size the amount of data sent
+ */
+static void
+flush_comp_cb (void *cls, size_t size)
+{
+ GNUNET_TESTBED_LOGGER_disconnect (lh);
+ lh = NULL;
+}
+#endif
+
+
/**
* Task run during shutdown.
*
proof_task = GNUNET_SCHEDULER_NO_TASK;
write_proof (); /* remember progress */
}
- if (NULL != keygen)
- {
- GNUNET_CRYPTO_rsa_key_create_stop (keygen);
- keygen = NULL;
- }
if (NULL != nc)
{
GNUNET_SERVER_notification_context_destroy (nc);
}
if (NULL != my_private_key)
{
- GNUNET_CRYPTO_rsa_key_free (my_private_key);
+ GNUNET_CRYPTO_ecc_key_free (my_private_key);
my_private_key = NULL;
}
-#if ENABLE_HISTOGRAM
- if (NULL != wh)
- {
- GNUNET_break (GNUNET_OK == GNUNET_BIO_write_close (wh));
- wh = NULL;
- }
+#if ENABLE_NSE_HISTOGRAM
+ struct GNUNET_TIME_Relative timeout;
+ timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
+ GNUNET_TESTBED_LOGGER_flush (lh, timeout, &flush_comp_cb, NULL);
#endif
}
/**
- * Callback for hostkey read/generation
+ * Handle network size estimate clients.
*
- * @param cls NULL
- * @param pk the private key
- * @param emsg error message
+ * @param cls closure
+ * @param server the initialized server
+ * @param c configuration to use
*/
static void
-key_generation_cb (void *cls,
- struct GNUNET_CRYPTO_RsaPrivateKey *pk,
- const char *emsg)
+run (void *cls,
+ struct GNUNET_SERVER_Handle *server,
+ const struct GNUNET_CONFIGURATION_Handle *c)
{
static const struct GNUNET_SERVER_MessageHandler handlers[] = {
{&handle_start_message, NULL, GNUNET_MESSAGE_TYPE_NSE_START,
{NULL, 0, 0}
};
char *proof;
+ char *keyfile;
+ struct GNUNET_CRYPTO_EccPrivateKey *pk;
- keygen = NULL;
- if (NULL == pk)
+ cfg = c;
+ srv = server;
+ if ((GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", "INTERVAL",
+ &gnunet_nse_interval)) ||
+ (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", "WORKDELAY",
+ &proof_find_delay)) ||
+ (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_number (cfg, "NSE", "WORKBITS",
+ &nse_work_required)))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _
+ ("%s service is lacking key configuration settings (%s). Exiting.\n"),
+ "NSE", "interval/workdelay/workbits");
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _("Invalid work requirement for NSE service. Exiting.\n"));
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
+ &keyfile))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("NSE service could not access hostkey: %s\n"),
- emsg);
+ _
+ ("%s service is lacking key configuration settings (%s). Exiting.\n"),
+ "NSE", "peer/privatekey");
GNUNET_SCHEDULER_shutdown ();
return;
}
+#if ENABLE_NSE_HISTOGRAM
+ if (NULL == (lh = GNUNET_TESTBED_LOGGER_connect (cfg)))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Cannot connect to the testbed logger. Exiting.\n");
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+#endif
+
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
+ NULL);
+ pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
+ GNUNET_free (keyfile);
+ GNUNET_assert (NULL != pk);
my_private_key = pk;
- GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
+ GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
&my_identity.hashPubKey);
if (GNUNET_OK !=
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_
("NSE service is lacking key configuration settings. Exiting.\n"));
- GNUNET_CRYPTO_rsa_key_free (my_private_key);
+ GNUNET_CRYPTO_ecc_key_free (my_private_key);
my_private_key = NULL;
GNUNET_SCHEDULER_shutdown ();
return;
GNUNET_SCHEDULER_shutdown ();
return;
}
-#if ENABLE_HISTOGRAM
- if (GNUNET_OK ==
- GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "HISTOGRAM", &proof))
- {
- wh = GNUNET_BIO_write_open (proof);
- GNUNET_free (proof);
- }
-#endif
stats = GNUNET_STATISTICS_create ("nse", cfg);
- GNUNET_SERVER_resume (srv);
-}
-
-
-/**
- * Handle network size estimate clients.
- *
- * @param cls closure
- * @param server the initialized server
- * @param c configuration to use
- */
-static void
-run (void *cls,
- struct GNUNET_SERVER_Handle *server,
- const struct GNUNET_CONFIGURATION_Handle *c)
-{
- char *keyfile;
-
- cfg = c;
- srv = server;
- if ((GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", "INTERVAL",
- &gnunet_nse_interval)) ||
- (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_time (cfg, "NSE", "WORKDELAY",
- &proof_find_delay)) ||
- (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_number (cfg, "NSE", "WORKBITS",
- &nse_work_required)))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _
- ("NSE service is lacking key configuration settings. Exiting.\n"));
- GNUNET_SCHEDULER_shutdown ();
- return;
- }
- if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("Invalid work requirement for NSE service. Exiting.\n"));
- GNUNET_SCHEDULER_shutdown ();
- return;
- }
- if (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
- &keyfile))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _
- ("NSE service is lacking key configuration settings. Exiting.\n"));
- GNUNET_SCHEDULER_shutdown ();
- return;
- }
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
- NULL);
- GNUNET_SERVER_suspend (srv);
- keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
- GNUNET_free (keyfile);
}
&run, NULL)) ? 0 : 1;
}
+
+#ifdef LINUX
+#include <malloc.h>
+
+/**
+ * MINIMIZE heap size (way below 128k) since this process doesn't need much.
+ */
+void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
+{
+ mallopt (M_TRIM_THRESHOLD, 4 * 1024);
+ mallopt (M_TOP_PAD, 1 * 1024);
+ malloc_trim (0);
+}
+#endif
+
+
+
/* end of gnunet-service-nse.c */