diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e2e65228..b00b1f1d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# v2.5.0 +- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on March 28.** +- Added full IPv6 support. +- Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. +- [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. +- [#428](https://github.com/xmrig/xmrig/issues/428) Fixed regression (version 2.4.5 affected) with CPU cache size detection. + # v2.4.5 - [#324](https://github.com/xmrig/xmrig/pull/324) Fixed build without libmicrohttpd (CMake cache issue). - [#341](https://github.com/xmrig/xmrig/issues/341) Fixed wrong exit code and added command line option `--dry-run`. diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e1673eac..342951f33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,8 +29,8 @@ set(HEADERS src/log/Log.h src/Mem.h src/net/Client.h + src/net/Id.h src/net/Job.h - src/net/JobId.h src/net/JobResult.h src/net/Network.h src/net/strategies/DonateStrategy.h @@ -57,6 +57,7 @@ set(HEADERS_CRYPTO src/crypto/c_keccak.h src/crypto/c_skein.h src/crypto/CryptoNight.h + src/crypto/CryptoNight_monero.h src/crypto/CryptoNight_test.h src/crypto/groestl_tables.h src/crypto/hash.h diff --git a/src/Cpu.cpp b/src/Cpu.cpp index a619781e0..eba993b37 100644 --- a/src/Cpu.cpp +++ b/src/Cpu.cpp @@ -100,9 +100,9 @@ void Cpu::initCommon() m_l2_cache = data.l2_cache * (m_totalCores / 2) * m_sockets; m_l2_exclusive = true; } - // Workaround for Intel Core Solo, Core Duo, Core 2 Duo, Core 2 Quad and their Xeon homologue + // Workaround for Intel Pentium Dual-Core, Core Duo, Core 2 Duo, Core 2 Quad and their Xeon homologue // These processors have L2 cache shared by 2 cores. - else if (data.vendor == VENDOR_INTEL && data.family == 0x06 && (data.model == 0x0E || data.model == 0x0F || data.model == 0x07)) { + else if (data.vendor == VENDOR_INTEL && data.ext_family == 0x06 && (data.ext_model == 0x0E || data.ext_model == 0x0F || data.ext_model == 0x17)) { int l2_count_per_socket = m_totalCores > 1 ? m_totalCores / 2 : 1; m_l2_cache = data.l2_cache > 0 ? data.l2_cache * l2_count_per_socket * m_sockets : 0; } diff --git a/src/Mem.cpp b/src/Mem.cpp index 4437c6739..1943b22ac 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,10 +47,10 @@ cryptonight_ctx *Mem::create(int threadId) } # endif - cryptonight_ctx *ctx = reinterpret_cast(&m_memory[MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); + cryptonight_ctx *ctx = reinterpret_cast(&m_memory[MONERO_MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); const int ratio = m_doubleHash ? 2 : 1; - ctx->memory = &m_memory[MEMORY * (threadId * ratio + 1)]; + ctx->memory = &m_memory[MONERO_MEMORY * (threadId * ratio + 1)]; return ctx; } @@ -72,15 +73,15 @@ cryptonight_ctx *Mem::createLite(int threadId) { cryptonight_ctx *ctx; if (!m_doubleHash) { - const size_t offset = MEMORY * (threadId + 1); + const size_t offset = MONERO_MEMORY * (threadId + 1); - ctx = reinterpret_cast(&m_memory[offset + MEMORY_LITE]); + ctx = reinterpret_cast(&m_memory[offset + AEON_MEMORY]); ctx->memory = &m_memory[offset]; return ctx; } - ctx = reinterpret_cast(&m_memory[MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); - ctx->memory = &m_memory[MEMORY * (threadId + 1)]; + ctx = reinterpret_cast(&m_memory[MONERO_MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); + ctx->memory = &m_memory[MONERO_MEMORY * (threadId + 1)]; return ctx; } diff --git a/src/Mem.h b/src/Mem.h index 58dba8485..a9635a177 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 43ffa6644..ec4017da9 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,7 +47,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_doubleHash = doubleHash; const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MEMORY * (threads * ratio + 1); + const size_t size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { m_memory = static_cast(_mm_malloc(size, 16)); @@ -83,7 +84,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) void Mem::release() { - const int size = MEMORY * (m_threads + 1); + const int size = MONERO_MEMORY * (m_threads + 1); if (m_flags & HugepagesEnabled) { if (m_flags & Lock) { diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index a4f92cfe3..79c6d12ee 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -151,7 +152,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_doubleHash = doubleHash; const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MEMORY * (threads * ratio + 1); + const size_t size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { m_memory = static_cast(_mm_malloc(size, 16)); diff --git a/src/Options.cpp b/src/Options.cpp index 4e7c75cac..f60b12858 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -74,6 +74,7 @@ Options:\n\ --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ --no-huge-pages disable huge pages support\n\ --no-color disable colored output\n\ + --no-monero disable Monero v7 PoW\n\ --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ --user-agent set custom user-agent string for pool\n\ -B, --background run the miner in the background\n\ @@ -118,6 +119,7 @@ static struct option const options[] = { { "nicehash", 0, nullptr, 1006 }, { "no-color", 0, nullptr, 1002 }, { "no-huge-pages", 0, nullptr, 1009 }, + { "no-monero", 0, nullptr, 1010 }, { "pass", 1, nullptr, 'p' }, { "print-time", 1, nullptr, 1007 }, { "retries", 1, nullptr, 'r' }, @@ -158,11 +160,12 @@ static struct option const config_options[] = { static struct option const pool_options[] = { - { "url", 1, nullptr, 'o' }, { "pass", 1, nullptr, 'p' }, + { "url", 1, nullptr, 'o' }, { "user", 1, nullptr, 'u' }, { "userpass", 1, nullptr, 'O' }, { "keepalive", 0, nullptr ,'k' }, + { "monero", 0, nullptr, 1010 }, { "nicehash", 0, nullptr, 1006 }, { 0, 0, 0, 0 } }; @@ -392,6 +395,7 @@ bool Options::parseArg(int key, const char *arg) case 1002: /* --no-color */ case 1009: /* --no-huge-pages */ + case 1010: /* --no-monero */ return parseBoolean(key, false); case 't': /* --threads */ @@ -557,6 +561,10 @@ bool Options::parseBoolean(int key, bool enable) m_hugePages = enable; break; + case 1010: /* monero */ + m_pools.back()->setMonero(enable); + break; + case 2000: /* colors */ m_colors = enable; break; diff --git a/src/Options.h b/src/Options.h index 6f0749179..625ac78d8 100644 --- a/src/Options.h +++ b/src/Options.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 51476e67c..a604b5de3 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,25 +32,10 @@ Httpd::Httpd(int port, const char *accessToken) : - m_idle(true), - m_accessToken(accessToken ? strdup(accessToken) : nullptr), + m_accessToken(accessToken), m_port(port), m_daemon(nullptr) { - uv_timer_init(uv_default_loop(), &m_timer); - m_timer.data = this; -} - - -Httpd::~Httpd() -{ - uv_timer_stop(&m_timer); - - if (m_daemon) { - MHD_stop_daemon(m_daemon); - } - - delete m_accessToken; } @@ -62,7 +47,14 @@ bool Httpd::start() unsigned int flags = 0; if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) { - flags |= MHD_USE_EPOLL_LINUX_ONLY; + flags = MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_EPOLL_INTERNALLY_LINUX_ONLY; + } + else { + flags = MHD_USE_SELECT_INTERNALLY; + } + + if (MHD_is_feature_supported(MHD_FEATURE_IPv6)) { + flags |= MHD_USE_DUAL_STACK; } m_daemon = MHD_start_daemon(flags, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); @@ -71,7 +63,6 @@ bool Httpd::start() return false; } - uv_timer_start(&m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval); return true; } @@ -95,22 +86,6 @@ int Httpd::auth(const char *header) } -void Httpd::run() -{ - MHD_run(m_daemon); - - const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS); - if (m_idle && info->num_connections) { - uv_timer_set_repeat(&m_timer, kActiveInterval); - m_idle = false; - } - else if (!m_idle && !info->num_connections) { - uv_timer_set_repeat(&m_timer, kIdleInterval); - m_idle = true; - } -} - - int Httpd::done(MHD_Connection *connection, int status, MHD_Response *rsp) { if (!rsp) { @@ -151,9 +126,3 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url MHD_Response *rsp = MHD_create_response_from_buffer(strlen(buf), (void*) buf, MHD_RESPMEM_MUST_FREE); return done(connection, status, rsp); } - - -void Httpd::onTimer(uv_timer_t *handle) -{ - static_cast(handle->data)->run(); -} diff --git a/src/api/Httpd.h b/src/api/Httpd.h index 1bdfeeb64..30618e2a8 100644 --- a/src/api/Httpd.h +++ b/src/api/Httpd.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,25 +37,17 @@ class Httpd { public: Httpd(int port, const char *accessToken); - ~Httpd(); bool start(); private: - constexpr static const int kIdleInterval = 200; - constexpr static const int kActiveInterval = 50; - int auth(const char *header); - void run(); static int done(MHD_Connection *connection, int status, MHD_Response *rsp); static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); - static void onTimer(uv_timer_t *handle); - bool m_idle; const char *m_accessToken; const int m_port; MHD_Daemon *m_daemon; - uv_timer_t m_timer; }; #endif /* __HTTPD_H__ */ diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 3ac9e94bb..8a818fda8 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +25,7 @@ #include "crypto/CryptoNight.h" + #if defined(XMRIG_ARM) # include "crypto/CryptoNight_arm.h" #else @@ -36,58 +38,79 @@ #include "Options.h" -void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx) = nullptr; + +void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) = nullptr; -static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx) { +static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, false>(input, size, output, ctx); + if (version > 6) { + cryptonight_hash(input, size, output, ctx, version); + } + else { + cryptonight_hash(input, size, output, ctx, version); + } # endif } -static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { +static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, false>(input, size, output, ctx); + if (version > 6) { + cryptonight_double_hash(input, size, output, ctx, version); + } + else { + cryptonight_double_hash(input, size, output, ctx, version); + } # endif } -static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, true>(input, size, output, ctx); +static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + if (version > 6) { + cryptonight_hash(input, size, output, ctx, version); + } + else { + cryptonight_hash(input, size, output, ctx, version); + } } -static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, true>(input, size, output, ctx); +static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + if (version > 6) { + cryptonight_double_hash(input, size, output, ctx, version); + } + else { + cryptonight_double_hash(input, size, output, ctx, version); + } } #ifndef XMRIG_NO_AEON -static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - # if !defined(XMRIG_ARMv7) - cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, false>(input, size, output, ctx); -#endif -} - - -static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { +static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, false>(input, size, output, ctx); + cryptonight_hash(input, size, output, ctx, version); # endif } -static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, true>(input, size, output, ctx); +static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +# if !defined(XMRIG_ARMv7) + cryptonight_double_hash(input, size, output, ctx, version); +# endif } -static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, true>(input, size, output, ctx); +static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + cryptonight_hash(input, size, output, ctx, version); } -void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx) = { + +static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + cryptonight_double_hash(input, size, output, ctx, version); +} + +void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -98,7 +121,7 @@ void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_lite_av4_softaes_double }; #else -void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx) = { +void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -109,7 +132,7 @@ void (*cryptonight_variations[4])(const void *input, size_t size, void *output, bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) { - cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx); + cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.version()); return *reinterpret_cast(result.result + 24) < job.target(); } @@ -133,9 +156,9 @@ bool CryptoNight::init(int algo, int variant) } -void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx) +void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_hash_ctx(input, size, output, ctx); + cryptonight_hash_ctx(input, size, output, ctx, version); } @@ -146,17 +169,25 @@ bool CryptoNight::selfTest(int algo) { char output[64]; - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); - ctx->memory = (uint8_t *) _mm_malloc(MEMORY * 2, 16); + struct cryptonight_ctx *ctx = static_cast(_mm_malloc(sizeof(cryptonight_ctx), 16)); + ctx->memory = static_cast(_mm_malloc(MONERO_MEMORY * 2, 16)); - cryptonight_hash_ctx(test_input, 76, output, ctx); + cryptonight_hash_ctx(test_input, 76, output, ctx, 0); + +# ifndef XMRIG_NO_AEON + bool rc = memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; +# else + bool rc = memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; +# endif + + if (rc && algo == Options::ALGO_CRYPTONIGHT) { + cryptonight_hash_ctx(test_input, 76, output, ctx, 7); + + rc = memcmp(output, test_output2, (Options::i()->doubleHash() ? 64 : 32)) == 0; + } _mm_free(ctx->memory); _mm_free(ctx); -# ifndef XMRIG_NO_AEON - return memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; -# else - return memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; -# endif + return rc; } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 64fc0fd1a..06bb98880 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,9 +32,13 @@ #include "align.h" +#define AEON_MEMORY 1048576 +#define AEON_MASK 0xFFFF0 +#define AEON_ITER 0x40000 -#define MEMORY 2097152 /* 2 MiB */ -#define MEMORY_LITE 1048576 /* 1 MiB */ +#define MONERO_MEMORY 2097152 +#define MONERO_MASK 0x1FFFF0 +#define MONERO_ITER 0x80000 struct cryptonight_ctx { @@ -52,7 +57,7 @@ class CryptoNight public: static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); static bool init(int algo, int variant); - static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx); + static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); private: static bool selfTest(int algo); diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 4ac14f34d..6d8c454ea 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2016 Imran Yusuff - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,6 +35,7 @@ #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_monero.h" #include "crypto/soft_aes.h" @@ -136,20 +138,6 @@ static inline __m128i sl_xor(__m128i tmp1) } -template -static inline void aes_genkey_sub(__m128i* xout0, __m128i* xout2) -{ -// __m128i xout1 = _mm_aeskeygenassist_si128(*xout2, rcon); -// xout1 = _mm_shuffle_epi32(xout1, 0xFF); // see PSHUFD, set all elems to 4th elem -// *xout0 = sl_xor(*xout0); -// *xout0 = _mm_xor_si128(*xout0, xout1); -// xout1 = _mm_aeskeygenassist_si128(*xout0, 0x00); -// xout1 = _mm_shuffle_epi32(xout1, 0xAA); // see PSHUFD, set all elems to 3rd elem -// *xout2 = sl_xor(*xout2); -// *xout2 = _mm_xor_si128(*xout2, xout1); -} - - template static inline void soft_aes_genkey_sub(__m128i* xout0, __m128i* xout2) { @@ -344,11 +332,13 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template -inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +template +inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak(static_cast(input), (int) size, ctx->state0, 200); + VARIANT1_INIT(0); + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); const uint8_t* l0 = ctx->memory; @@ -374,6 +364,7 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * } _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + VARIANT1_1(&l0[idx0 & MASK]); idx0 = EXTRACT64(cx); bx0 = cx; @@ -385,8 +376,10 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*)&l0[idx0 & MASK])[0] = al0; ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -400,12 +393,15 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * } -template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +template +inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEM; uint64_t* h0 = reinterpret_cast(ctx->state0); @@ -443,6 +439,8 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + VARIANT1_1(&l0[idx0 & MASK]); + VARIANT1_1(&l1[idx1 & MASK]); idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); @@ -458,8 +456,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & MASK])[0] = al0; ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -472,8 +472,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & MASK])[0] = al1; ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h new file mode 100644 index 000000000..9bf3db846 --- /dev/null +++ b/src/crypto/CryptoNight_monero.h @@ -0,0 +1,57 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CRYPTONIGHT_MONERO_H__ +#define __CRYPTONIGHT_MONERO_H__ + + +// VARIANT ALTERATIONS +#define VARIANT1_INIT(part) \ + uint64_t tweak1_2_##part = 0; \ + if (MONERO) { \ + if (version > 6) { \ + tweak1_2_##part = (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ + *(reinterpret_cast(ctx->state##part) + 24)); \ + } \ + } + +#define VARIANT1_1(p) \ + if (MONERO) { \ + if (version > 6) { \ + const uint8_t tmp = reinterpret_cast(p)[11]; \ + static const uint32_t table = 0x75310; \ + const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ + ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ + } \ + } + +#define VARIANT1_2(p, part) \ + if (MONERO) { \ + if (version > 6) { \ + (p) ^= tweak1_2_##part; \ + } \ + } + + +#endif /* __CRYPTONIGHT_MONERO_H__ */ diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index b2985379b..65200f755 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -57,4 +58,13 @@ const static uint8_t test_output1[64] = { #endif +// Monero v7 +const static uint8_t test_output2[64] = { + 0xC9, 0xFA, 0xE8, 0x42, 0x5D, 0x86, 0x88, 0xDC, 0x23, 0x6B, 0xCD, 0xBC, 0x42, 0xFD, 0xB4, 0x2D, + 0x37, 0x6C, 0x6E, 0xC1, 0x90, 0x50, 0x1A, 0xA8, 0x4B, 0x04, 0xA4, 0xB4, 0xCF, 0x1E, 0xE1, 0x22, + 0xF2, 0x2D, 0x3D, 0x62, 0x03, 0xD2, 0xA0, 0x8B, 0x41, 0xD9, 0x02, 0x72, 0x78, 0xD8, 0xBC, 0xC9, + 0x83, 0xAC, 0xAD, 0xA9, 0xB6, 0x8E, 0x52, 0xE3, 0xC6, 0x89, 0x69, 0x2A, 0x50, 0xE9, 0x21, 0xD9 +}; + + #endif /* __CRYPTONIGHT_TEST_H__ */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 927aab723..d39948e84 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,6 +35,7 @@ #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_monero.h" #include "crypto/soft_aes.h" @@ -307,11 +309,13 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template -inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +template +inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak(static_cast(input), (int) size, ctx->state0, 200); + VARIANT1_INIT(0); + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); const uint8_t* l0 = ctx->memory; @@ -334,6 +338,7 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); } _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + VARIANT1_1(&l0[idx0 & MASK]); idx0 = EXTRACT64(cx); bx0 = cx; @@ -345,8 +350,10 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*)&l0[idx0 & MASK])[0] = al0; ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -357,15 +364,19 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); + return true; } -template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +template +inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEM; uint64_t* h0 = reinterpret_cast(ctx->state0); @@ -401,6 +412,8 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + VARIANT1_1(&l0[idx0 & MASK]); + VARIANT1_1(&l1[idx1 & MASK]); idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); @@ -416,8 +429,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & MASK])[0] = al0; ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -430,8 +445,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & MASK])[0] = al1; ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; @@ -446,6 +463,7 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); + return true; } #endif /* __CRYPTONIGHT_X86_H__ */ diff --git a/src/net/Client.cpp b/src/net/Client.cpp index fb83acd2b..b01483366 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -54,6 +54,8 @@ int64_t Client::m_sequence = 1; Client::Client(int id, const char *agent, IClientListener *listener) : + m_ipv6(false), + m_nicehash(false), m_quiet(false), m_agent(agent), m_listener(listener), @@ -71,7 +73,7 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_resolver.data = this; - m_hints.ai_family = PF_INET; + m_hints.ai_family = AF_UNSPEC; m_hints.ai_socktype = SOCK_STREAM; m_hints.ai_protocol = IPPROTO_TCP; @@ -109,19 +111,6 @@ void Client::connect(const Url *url) } -void Client::disconnect() -{ -# ifndef XMRIG_PROXY_PROJECT - uv_timer_stop(&m_keepAliveTimer); -# endif - - m_expire = 0; - m_failures = -1; - - close(); -} - - void Client::setUrl(const Url *url) { if (!url || !url->isValid()) { @@ -150,6 +139,19 @@ void Client::tick(uint64_t now) } +bool Client::disconnect() +{ +# ifndef XMRIG_PROXY_PROJECT + uv_timer_stop(&m_keepAliveTimer); +# endif + + m_expire = 0; + m_failures = -1; + + return close(); +} + + int64_t Client::submit(const JobResult &result) { # ifdef XMRIG_PROXY_PROJECT @@ -167,13 +169,29 @@ int64_t Client::submit(const JobResult &result) # endif const size_t size = snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRIu64 ",\"jsonrpc\":\"2.0\",\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"}}\n", - m_sequence, m_rpcId, result.jobId.data(), nonce, data); + m_sequence, m_rpcId.data(), result.jobId.data(), nonce, data); m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff()); return send(size); } +bool Client::close() +{ + if (m_state == UnconnectedState || m_state == ClosingState || !m_socket) { + return false; + } + + setState(ClosingState); + + if (uv_is_closing(reinterpret_cast(m_socket)) == 0) { + uv_close(reinterpret_cast(m_socket), Client::onClose); + } + + return true; +} + + bool Client::isCriticalError(const char *message) { if (!message) { @@ -203,7 +221,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } - Job job(m_id, m_url.isNicehash()); + Job job(m_id, m_nicehash, m_url.isMonero()); if (!job.setId(params["job_id"].GetString())) { *code = 3; return false; @@ -219,6 +237,10 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } + if (params.HasMember("coin")) { + job.setCoin(params["coin"].GetString()); + } + if (m_job == job) { if (!m_quiet) { LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); @@ -235,14 +257,16 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) bool Client::parseLogin(const rapidjson::Value &result, int *code) { - const char *id = result["id"].GetString(); - if (!id || strlen(id) >= sizeof(m_rpcId)) { + if (!m_rpcId.setId(result["id"].GetString())) { *code = 1; return false; } - memset(m_rpcId, 0, sizeof(m_rpcId)); - memcpy(m_rpcId, id, strlen(id)); + m_nicehash = m_url.isNicehash(); + + if (result.HasMember("extensions")) { + parseExtensions(result["extensions"]); + } return parseJob(result["job"], code); } @@ -291,21 +315,25 @@ int64_t Client::send(size_t size) } -void Client::close() +void Client::connect(const std::vector &ipv4, const std::vector &ipv6) { - if (m_state == UnconnectedState || m_state == ClosingState || !m_socket) { - return; + addrinfo *addr = nullptr; + m_ipv6 = ipv4.empty() && !ipv6.empty(); + + if (m_ipv6) { + addr = ipv6[ipv6.size() == 1 ? 0 : rand() % ipv6.size()]; + uv_ip6_name(reinterpret_cast(addr->ai_addr), m_ip, 45); + } + else { + addr = ipv4[ipv4.size() == 1 ? 0 : rand() % ipv4.size()]; + uv_ip4_name(reinterpret_cast(addr->ai_addr), m_ip, 16); } - setState(ClosingState); - - if (uv_is_closing(reinterpret_cast(m_socket)) == 0) { - uv_close(reinterpret_cast(m_socket), Client::onClose); - } + connect(addr->ai_addr); } -void Client::connect(struct sockaddr *addr) +void Client::connect(sockaddr *addr) { setState(ConnectingState); @@ -374,6 +402,11 @@ void Client::parse(char *line, size_t len) LOG_DEBUG("[%s:%u] received (%d bytes): \"%s\"", m_url.host(), m_url.port(), len, line); + if (len < 32 || line[0] != '{') { + LOG_ERR("[%s:%u] JSON decode failed", m_url.host(), m_url.port()); + return; + } + rapidjson::Document doc; if (doc.ParseInsitu(line).HasParseError()) { if (!m_quiet) { @@ -397,6 +430,24 @@ void Client::parse(char *line, size_t len) } +void Client::parseExtensions(const rapidjson::Value &value) +{ + if (!value.IsArray()) { + return; + } + + for (const rapidjson::Value &ext : value.GetArray()) { + if (!ext.IsString()) { + continue; + } + + if (strcmp(ext.GetString(), "nicehash") == 0) { + m_nicehash = true; + } + } +} + + void Client::parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error) { if (error.IsObject()) { @@ -456,7 +507,8 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap LOG_ERR("[%s:%u] login error code: %d", m_url.host(), m_url.port(), code); } - return close(); + close(); + return; } m_failures = 0; @@ -476,7 +528,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap void Client::ping() { - send(snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, m_rpcId)); + send(snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, m_rpcId.data())); } @@ -532,7 +584,7 @@ void Client::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t auto client = getClient(handle->data); buf->base = &client->m_recvBuf.base[client->m_recvBufPos]; - buf->len = client->m_recvBuf.len - (unsigned long)client->m_recvBufPos; + buf->len = client->m_recvBuf.len - client->m_recvBufPos; } @@ -582,17 +634,19 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror((int) nread)); } - return client->close(); + client->close(); + return; } if ((size_t) nread > (sizeof(m_buf) - 8 - client->m_recvBufPos)) { - return client->close(); + client->close(); + return; } client->m_recvBufPos += nread; char* end; - char* start = buf->base; + char* start = client->m_recvBuf.base; size_t remaining = client->m_recvBufPos; while ((end = static_cast(memchr(start, '\n', remaining))) != nullptr) { @@ -609,11 +663,11 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) return; } - if (start == buf->base) { + if (start == client->m_recvBuf.base) { return; } - memcpy(buf->base, start, remaining); + memcpy(client->m_recvBuf.base, start, remaining); client->m_recvBufPos = remaining; } @@ -628,24 +682,27 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) addrinfo *ptr = res; std::vector ipv4; + std::vector ipv6; while (ptr != nullptr) { if (ptr->ai_family == AF_INET) { ipv4.push_back(ptr); } + if (ptr->ai_family == AF_INET6) { + ipv6.push_back(ptr); + } + ptr = ptr->ai_next; } - if (ipv4.empty()) { - LOG_ERR("[%s:%u] DNS error: \"No IPv4 records found\"", client->m_url.host(), client->m_url.port()); + if (ipv4.empty() && ipv6.empty()) { + LOG_ERR("[%s:%u] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", client->m_url.host(), client->m_url.port()); + + uv_freeaddrinfo(res); return client->reconnect(); } - ptr = ipv4[rand() % ipv4.size()]; - - uv_ip4_name(reinterpret_cast(ptr->ai_addr), client->m_ip, 16); - - client->connect(ptr->ai_addr); + client->connect(ipv4, ipv6); uv_freeaddrinfo(res); } diff --git a/src/net/Client.h b/src/net/Client.h index 699f49039..a927bd19d 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,8 +27,10 @@ #include #include +#include +#include "net/Id.h" #include "net/Job.h" #include "net/SubmitResult.h" #include "net/Url.h" @@ -56,10 +58,10 @@ public: Client(int id, const char *agent, IClientListener *listener); ~Client(); + bool disconnect(); int64_t submit(const JobResult &result); void connect(); void connect(const Url *url); - void disconnect(); void setUrl(const Url *url); void tick(uint64_t now); @@ -74,15 +76,17 @@ public: inline void setRetryPause(int ms) { m_retryPause = ms; } private: + bool close(); bool isCriticalError(const char *message); bool parseJob(const rapidjson::Value ¶ms, int *code); bool parseLogin(const rapidjson::Value &result, int *code); int resolve(const char *host); int64_t send(size_t size); - void close(); - void connect(struct sockaddr *addr); + void connect(const std::vector &ipv4, const std::vector &ipv6); + void connect(sockaddr *addr); void login(); void parse(char *line, size_t len); + void parseExtensions(const rapidjson::Value &value); void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error); void parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error); void ping(); @@ -99,10 +103,11 @@ private: static inline Client *getClient(void *data) { return static_cast(data); } addrinfo m_hints; + bool m_ipv6; + bool m_nicehash; bool m_quiet; char m_buf[2048]; - char m_ip[17]; - char m_rpcId[64]; + char m_ip[46]; char m_sendBuf[768]; const char *m_agent; IClientListener *m_listener; @@ -120,6 +125,7 @@ private: uv_getaddrinfo_t m_resolver; uv_stream_t *m_stream; uv_tcp_t *m_socket; + xmrig::Id m_rpcId; # ifndef XMRIG_PROXY_PROJECT uv_timer_t m_keepAliveTimer; diff --git a/src/net/JobId.h b/src/net/Id.h similarity index 75% rename from src/net/JobId.h rename to src/net/Id.h index 061897792..5c77d1d43 100644 --- a/src/net/JobId.h +++ b/src/net/Id.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,40 +21,51 @@ * along with this program. If not, see . */ -#ifndef __JOBID_H__ -#define __JOBID_H__ +#ifndef __ID_H__ +#define __ID_H__ #include -class JobId +namespace xmrig { + + +class Id { public: - inline JobId() + inline Id() { memset(m_data, 0, sizeof(m_data)); } - inline JobId(const char *id, size_t sizeFix = 0) + inline Id(const char *id, size_t sizeFix = 0) { setId(id, sizeFix); } - inline bool operator==(const JobId &other) const + inline bool operator==(const Id &other) const { return memcmp(m_data, other.m_data, sizeof(m_data)) == 0; } - inline bool operator!=(const JobId &other) const + inline bool operator!=(const Id &other) const { return memcmp(m_data, other.m_data, sizeof(m_data)) != 0; } + Id &operator=(const Id &other) + { + memcpy(m_data, other.m_data, sizeof(m_data)); + + return *this; + } + + inline bool setId(const char *id, size_t sizeFix = 0) { memset(m_data, 0, sizeof(m_data)); @@ -80,4 +91,8 @@ private: char m_data[64]; }; -#endif /* __JOBID_H__ */ + +} /* namespace xmrig */ + + +#endif /* __ID_H__ */ diff --git a/src/net/Job.cpp b/src/net/Job.cpp index c3d33739a..3faed6a18 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -55,7 +56,8 @@ static inline char hf_bin2hex(unsigned char c) } -Job::Job(int poolId, bool nicehash) : +Job::Job(int poolId, bool nicehash, bool monero) : + m_monero(monero), m_nicehash(nicehash), m_poolId(poolId), m_threadId(-1), @@ -63,6 +65,7 @@ Job::Job(int poolId, bool nicehash) : m_diff(0), m_target(0) { + memset(m_coin, 0, sizeof(m_coin)); } @@ -146,6 +149,18 @@ bool Job::setTarget(const char *target) } +void Job::setCoin(const char *coin) +{ + if (!coin || strlen(coin) > 4) { + memset(m_coin, 0, sizeof(m_coin)); + return; + } + + strncpy(m_coin, coin, sizeof(m_coin)); + m_monero = strcmp(m_coin, "XMR") == 0; +} + + bool Job::fromHex(const char* in, unsigned int len, unsigned char* out) { bool error = false; diff --git a/src/net/Job.h b/src/net/Job.h index bf8f88356..755de9ec4 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,30 +31,34 @@ #include "align.h" -#include "net/JobId.h" +#include "net/Id.h" class Job { public: - Job(int poolId = -2, bool nicehash = false); + Job(int poolId = -2, bool nicehash = false, bool monero = true); ~Job(); bool setBlob(const char *blob); bool setTarget(const char *target); + void setCoin(const char *coin); + inline bool isMonero() const { return m_monero; } inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } inline bool setId(const char *id) { return m_id.setId(id); } - inline const JobId &id() const { return m_id; } + inline const char *coin() const { return m_coin; } inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } inline const uint8_t *blob() const { return m_blob; } + inline const xmrig::Id &id() const { return m_id; } inline int poolId() const { return m_poolId; } inline int threadId() const { return m_threadId; } inline size_t size() const { return m_size; } inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } inline uint32_t diff() const { return (uint32_t) m_diff; } inline uint64_t target() const { return m_target; } + inline uint8_t version() const { return isMonero() ? m_blob[0] : 0; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setThreadId(int threadId) { m_threadId = threadId; } @@ -72,13 +77,15 @@ public: private: VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. + bool m_monero; bool m_nicehash; + char m_coin[5]; int m_poolId; int m_threadId; - JobId m_id; size_t m_size; uint64_t m_diff; uint64_t m_target; + xmrig::Id m_id; # ifdef XMRIG_PROXY_PROJECT VAR_ALIGN(16, char m_rawBlob[169]); diff --git a/src/net/JobResult.h b/src/net/JobResult.h index 520022a76..e32825847 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,11 +36,11 @@ class JobResult { public: inline JobResult() : poolId(0), diff(0), nonce(0) {} - inline JobResult(int poolId, const JobId &jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : + inline JobResult(int poolId, const xmrig::Id &jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : poolId(poolId), - jobId(jobId), diff(diff), - nonce(nonce) + nonce(nonce), + jobId(jobId) { memcpy(this->result, result, sizeof(this->result)); } @@ -71,10 +71,10 @@ public: int poolId; - JobId jobId; uint32_t diff; uint32_t nonce; uint8_t result[32]; + xmrig::Id jobId; }; #endif /* __JOBRESULT_H__ */ diff --git a/src/net/Url.cpp b/src/net/Url.cpp index dcbe82af5..cd020b322 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,10 +37,12 @@ Url::Url() : m_keepAlive(false), + m_monero(true), m_nicehash(false), m_host(nullptr), m_password(nullptr), m_user(nullptr), + m_url(nullptr), m_port(kDefaultPort) { } @@ -59,21 +61,25 @@ Url::Url() : */ Url::Url(const char *url) : m_keepAlive(false), + m_monero(true), m_nicehash(false), m_host(nullptr), m_password(nullptr), m_user(nullptr), + m_url(nullptr), m_port(kDefaultPort) { parse(url); } -Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash) : +Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash, bool monero) : m_keepAlive(keepAlive), + m_monero(monero), m_nicehash(nicehash), m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), + m_url(nullptr), m_port(port) { m_host = strdup(host); @@ -85,6 +91,10 @@ Url::~Url() free(m_host); free(m_password); free(m_user); + + if (m_url) { + delete [] m_url; + } } @@ -105,6 +115,10 @@ bool Url::parse(const char *url) return false; } + if (base[0] == '[') { + return parseIPv6(base); + } + const char *port = strchr(base, ':'); if (!port) { m_host = strdup(base); @@ -112,9 +126,8 @@ bool Url::parse(const char *url) } const size_t size = port++ - base + 1; - m_host = static_cast(malloc(size)); + m_host = new char[size](); memcpy(m_host, base, size - 1); - m_host[size - 1] = '\0'; m_port = (uint16_t) strtol(port, nullptr, 10); return true; @@ -139,6 +152,19 @@ bool Url::setUserpass(const char *userpass) } +const char *Url::url() const +{ + if (!m_url) { + const size_t size = strlen(m_host) + 8; + m_url = new char[size]; + + snprintf(m_url, size - 1, "%s:%d", m_host, m_port); + } + + return m_url; +} + + void Url::applyExceptions() { if (!isValid()) { @@ -178,9 +204,24 @@ void Url::setUser(const char *user) } +bool Url::operator==(const Url &other) const +{ + if (m_port != other.m_port || m_keepAlive != other.m_keepAlive || m_nicehash != other.m_nicehash) { + return false; + } + + if (strcmp(host(), other.host()) != 0 || strcmp(user(), other.user()) != 0 || strcmp(password(), other.password()) != 0) { + return false; + } + + return true; +} + + Url &Url::operator=(const Url *other) { m_keepAlive = other->m_keepAlive; + m_monero = other->m_monero; m_nicehash = other->m_nicehash; m_port = other->m_port; @@ -190,5 +231,32 @@ Url &Url::operator=(const Url *other) setPassword(other->m_password); setUser(other->m_user); + if (m_url) { + delete [] m_url; + m_url = nullptr; + } + return *this; } + + +bool Url::parseIPv6(const char *addr) +{ + const char *end = strchr(addr, ']'); + if (!end) { + return false; + } + + const char *port = strchr(end, ':'); + if (!port) { + return false; + } + + const size_t size = end - addr; + m_host = new char[size](); + memcpy(m_host, addr + 1, size - 1); + + m_port = (uint16_t) strtol(port + 1, nullptr, 10); + + return true; +} diff --git a/src/net/Url.h b/src/net/Url.h index a1982300c..32eea5bfd 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,10 +37,11 @@ public: Url(); Url(const char *url); - Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false ); + Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false, bool monero = true); ~Url(); inline bool isKeepAlive() const { return m_keepAlive; } + inline bool isMonero() const { return m_monero; } inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_host && m_port > 0; } inline const char *host() const { return m_host; } @@ -48,22 +49,29 @@ public: inline const char *user() const { return m_user ? m_user : kDefaultUser; } inline uint16_t port() const { return m_port; } inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; } + inline void setMonero(bool monero) { m_monero = monero; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } bool parse(const char *url); bool setUserpass(const char *userpass); + const char *url() const; void applyExceptions(); void setPassword(const char *password); void setUser(const char *user); + bool operator==(const Url &other) const; Url &operator=(const Url *other); private: + bool parseIPv6(const char *addr); + bool m_keepAlive; + bool m_monero; bool m_nicehash; char *m_host; char *m_password; char *m_user; + mutable char *m_url; uint16_t m_port; }; diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 46d6f3663..d29603207 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -85,7 +86,7 @@ void DoubleWorker::start() *Job::nonce(m_state->blob) = ++m_state->nonce1; *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx); + CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.version()); if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); diff --git a/src/workers/DoubleWorker.h b/src/workers/DoubleWorker.h index 711f4bd1c..9f29fa5b2 100644 --- a/src/workers/DoubleWorker.h +++ b/src/workers/DoubleWorker.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index ecb566f89..9f4a7484c 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/workers/SingleWorker.h b/src/workers/SingleWorker.h index 08ab1857e..f7d9cff88 100644 --- a/src/workers/SingleWorker.h +++ b/src/workers/SingleWorker.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by